PR debug/64663
[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_cleared_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 }
4741
4742 /* Return TRUE if the language is Ada.  */
4743
4744 static inline bool
4745 is_ada (void)
4746 {
4747   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4748
4749   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4750 }
4751
4752 /* Remove the specified attribute if present.  */
4753
4754 static void
4755 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4756 {
4757   dw_attr_ref a;
4758   unsigned ix;
4759
4760   if (! die)
4761     return;
4762
4763   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4764     if (a->dw_attr == attr_kind)
4765       {
4766         if (AT_class (a) == dw_val_class_str)
4767           if (a->dw_attr_val.v.val_str->refcount)
4768             a->dw_attr_val.v.val_str->refcount--;
4769
4770         /* vec::ordered_remove should help reduce the number of abbrevs
4771            that are needed.  */
4772         die->die_attr->ordered_remove (ix);
4773         return;
4774       }
4775 }
4776
4777 /* Remove CHILD from its parent.  PREV must have the property that
4778    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
4779
4780 static void
4781 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4782 {
4783   gcc_assert (child->die_parent == prev->die_parent);
4784   gcc_assert (prev->die_sib == child);
4785   if (prev == child)
4786     {
4787       gcc_assert (child->die_parent->die_child == child);
4788       prev = NULL;
4789     }
4790   else
4791     prev->die_sib = child->die_sib;
4792   if (child->die_parent->die_child == child)
4793     child->die_parent->die_child = prev;
4794 }
4795
4796 /* Replace OLD_CHILD with NEW_CHILD.  PREV must have the property that
4797    PREV->DIE_SIB == OLD_CHILD.  Does not alter OLD_CHILD.  */
4798
4799 static void
4800 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4801 {
4802   dw_die_ref parent = old_child->die_parent;
4803
4804   gcc_assert (parent == prev->die_parent);
4805   gcc_assert (prev->die_sib == old_child);
4806
4807   new_child->die_parent = parent;
4808   if (prev == old_child)
4809     {
4810       gcc_assert (parent->die_child == old_child);
4811       new_child->die_sib = new_child;
4812     }
4813   else
4814     {
4815       prev->die_sib = new_child;
4816       new_child->die_sib = old_child->die_sib;
4817     }
4818   if (old_child->die_parent->die_child == old_child)
4819     old_child->die_parent->die_child = new_child;
4820 }
4821
4822 /* Move all children from OLD_PARENT to NEW_PARENT.  */
4823
4824 static void
4825 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4826 {
4827   dw_die_ref c;
4828   new_parent->die_child = old_parent->die_child;
4829   old_parent->die_child = NULL;
4830   FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4831 }
4832
4833 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
4834    matches TAG.  */
4835
4836 static void
4837 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4838 {
4839   dw_die_ref c;
4840
4841   c = die->die_child;
4842   if (c) do {
4843     dw_die_ref prev = c;
4844     c = c->die_sib;
4845     while (c->die_tag == tag)
4846       {
4847         remove_child_with_prev (c, prev);
4848         /* Might have removed every child.  */
4849         if (c == c->die_sib)
4850           return;
4851         c = c->die_sib;
4852       }
4853   } while (c != die->die_child);
4854 }
4855
4856 /* Add a CHILD_DIE as the last child of DIE.  */
4857
4858 static void
4859 add_child_die (dw_die_ref die, dw_die_ref child_die)
4860 {
4861   /* FIXME this should probably be an assert.  */
4862   if (! die || ! child_die)
4863     return;
4864   gcc_assert (die != child_die);
4865
4866   child_die->die_parent = die;
4867   if (die->die_child)
4868     {
4869       child_die->die_sib = die->die_child->die_sib;
4870       die->die_child->die_sib = child_die;
4871     }
4872   else
4873     child_die->die_sib = child_die;
4874   die->die_child = child_die;
4875 }
4876
4877 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4878    is the specification, to the end of PARENT's list of children.
4879    This is done by removing and re-adding it.  */
4880
4881 static void
4882 splice_child_die (dw_die_ref parent, dw_die_ref child)
4883 {
4884   dw_die_ref p;
4885
4886   /* We want the declaration DIE from inside the class, not the
4887      specification DIE at toplevel.  */
4888   if (child->die_parent != parent)
4889     {
4890       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4891
4892       if (tmp)
4893         child = tmp;
4894     }
4895
4896   gcc_assert (child->die_parent == parent
4897               || (child->die_parent
4898                   == get_AT_ref (parent, DW_AT_specification)));
4899
4900   for (p = child->die_parent->die_child; ; p = p->die_sib)
4901     if (p->die_sib == child)
4902       {
4903         remove_child_with_prev (child, p);
4904         break;
4905       }
4906
4907   add_child_die (parent, child);
4908 }
4909
4910 /* Return a pointer to a newly created DIE node.  */
4911
4912 static inline dw_die_ref
4913 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4914 {
4915   dw_die_ref die = ggc_cleared_alloc<die_node> ();
4916
4917   die->die_tag = tag_value;
4918
4919   if (parent_die != NULL)
4920     add_child_die (parent_die, die);
4921   else
4922     {
4923       limbo_die_node *limbo_node;
4924
4925       limbo_node = ggc_cleared_alloc<limbo_die_node> ();
4926       limbo_node->die = die;
4927       limbo_node->created_for = t;
4928       limbo_node->next = limbo_die_list;
4929       limbo_die_list = limbo_node;
4930     }
4931
4932   return die;
4933 }
4934
4935 /* Return the DIE associated with the given type specifier.  */
4936
4937 static inline dw_die_ref
4938 lookup_type_die (tree type)
4939 {
4940   return TYPE_SYMTAB_DIE (type);
4941 }
4942
4943 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4944    anonymous type named by the typedef TYPE_DIE, return the DIE of the
4945    anonymous type instead the one of the naming typedef.  */
4946
4947 static inline dw_die_ref
4948 strip_naming_typedef (tree type, dw_die_ref type_die)
4949 {
4950   if (type
4951       && TREE_CODE (type) == RECORD_TYPE
4952       && type_die
4953       && type_die->die_tag == DW_TAG_typedef
4954       && is_naming_typedef_decl (TYPE_NAME (type)))
4955     type_die = get_AT_ref (type_die, DW_AT_type);
4956   return type_die;
4957 }
4958
4959 /* Like lookup_type_die, but if type is an anonymous type named by a
4960    typedef[1], return the DIE of the anonymous type instead the one of
4961    the naming typedef.  This is because in gen_typedef_die, we did
4962    equate the anonymous struct named by the typedef with the DIE of
4963    the naming typedef. So by default, lookup_type_die on an anonymous
4964    struct yields the DIE of the naming typedef.
4965
4966    [1]: Read the comment of is_naming_typedef_decl to learn about what
4967    a naming typedef is.  */
4968
4969 static inline dw_die_ref
4970 lookup_type_die_strip_naming_typedef (tree type)
4971 {
4972   dw_die_ref die = lookup_type_die (type);
4973   return strip_naming_typedef (type, die);
4974 }
4975
4976 /* Equate a DIE to a given type specifier.  */
4977
4978 static inline void
4979 equate_type_number_to_die (tree type, dw_die_ref type_die)
4980 {
4981   TYPE_SYMTAB_DIE (type) = type_die;
4982 }
4983
4984 /* Returns a hash value for X (which really is a die_struct).  */
4985
4986 inline hashval_t
4987 decl_die_hasher::hash (die_node *x)
4988 {
4989   return (hashval_t) x->decl_id;
4990 }
4991
4992 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
4993
4994 inline bool
4995 decl_die_hasher::equal (die_node *x, tree y)
4996 {
4997   return (x->decl_id == DECL_UID (y));
4998 }
4999
5000 /* Return the DIE associated with a given declaration.  */
5001
5002 static inline dw_die_ref
5003 lookup_decl_die (tree decl)
5004 {
5005   return decl_die_table->find_with_hash (decl, DECL_UID (decl));
5006 }
5007
5008 /* Returns a hash value for X (which really is a var_loc_list).  */
5009
5010 inline hashval_t
5011 decl_loc_hasher::hash (var_loc_list *x)
5012 {
5013   return (hashval_t) x->decl_id;
5014 }
5015
5016 /* Return nonzero if decl_id of var_loc_list X is the same as
5017    UID of decl *Y.  */
5018
5019 inline bool
5020 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5021 {
5022   return (x->decl_id == DECL_UID (y));
5023 }
5024
5025 /* Return the var_loc list associated with a given declaration.  */
5026
5027 static inline var_loc_list *
5028 lookup_decl_loc (const_tree decl)
5029 {
5030   if (!decl_loc_table)
5031     return NULL;
5032   return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5033 }
5034
5035 /* Returns a hash value for X (which really is a cached_dw_loc_list_list).  */
5036
5037 inline hashval_t
5038 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5039 {
5040   return (hashval_t) x->decl_id;
5041 }
5042
5043 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5044    UID of decl *Y.  */
5045
5046 inline bool
5047 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5048 {
5049   return (x->decl_id == DECL_UID (y));
5050 }
5051
5052 /* Equate a DIE to a particular declaration.  */
5053
5054 static void
5055 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5056 {
5057   unsigned int decl_id = DECL_UID (decl);
5058
5059   *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5060   decl_die->decl_id = decl_id;
5061 }
5062
5063 /* Return how many bits covers PIECE EXPR_LIST.  */
5064
5065 static HOST_WIDE_INT
5066 decl_piece_bitsize (rtx piece)
5067 {
5068   int ret = (int) GET_MODE (piece);
5069   if (ret)
5070     return ret;
5071   gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5072               && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5073   return INTVAL (XEXP (XEXP (piece, 0), 0));
5074 }
5075
5076 /* Return pointer to the location of location note in PIECE EXPR_LIST.  */
5077
5078 static rtx *
5079 decl_piece_varloc_ptr (rtx piece)
5080 {
5081   if ((int) GET_MODE (piece))
5082     return &XEXP (piece, 0);
5083   else
5084     return &XEXP (XEXP (piece, 0), 1);
5085 }
5086
5087 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5088    Next is the chain of following piece nodes.  */
5089
5090 static rtx_expr_list *
5091 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5092 {
5093   if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
5094     return alloc_EXPR_LIST (bitsize, loc_note, next);
5095   else
5096     return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5097                                                GEN_INT (bitsize),
5098                                                loc_note), next);
5099 }
5100
5101 /* Return rtx that should be stored into loc field for
5102    LOC_NOTE and BITPOS/BITSIZE.  */
5103
5104 static rtx
5105 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5106                       HOST_WIDE_INT bitsize)
5107 {
5108   if (bitsize != -1)
5109     {
5110       loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5111       if (bitpos != 0)
5112         loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5113     }
5114   return loc_note;
5115 }
5116
5117 /* This function either modifies location piece list *DEST in
5118    place (if SRC and INNER is NULL), or copies location piece list
5119    *SRC to *DEST while modifying it.  Location BITPOS is modified
5120    to contain LOC_NOTE, any pieces overlapping it are removed resp.
5121    not copied and if needed some padding around it is added.
5122    When modifying in place, DEST should point to EXPR_LIST where
5123    earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5124    to the start of the whole list and INNER points to the EXPR_LIST
5125    where earlier pieces cover PIECE_BITPOS bits.  */
5126
5127 static void
5128 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5129                    HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5130                    HOST_WIDE_INT bitsize, rtx loc_note)
5131 {
5132   HOST_WIDE_INT diff;
5133   bool copy = inner != NULL;
5134
5135   if (copy)
5136     {
5137       /* First copy all nodes preceding the current bitpos.  */
5138       while (src != inner)
5139         {
5140           *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5141                                    decl_piece_bitsize (*src), NULL_RTX);
5142           dest = &XEXP (*dest, 1);
5143           src = &XEXP (*src, 1);
5144         }
5145     }
5146   /* Add padding if needed.  */
5147   if (bitpos != piece_bitpos)
5148     {
5149       *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5150                                copy ? NULL_RTX : *dest);
5151       dest = &XEXP (*dest, 1);
5152     }
5153   else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5154     {
5155       gcc_assert (!copy);
5156       /* A piece with correct bitpos and bitsize already exist,
5157          just update the location for it and return.  */
5158       *decl_piece_varloc_ptr (*dest) = loc_note;
5159       return;
5160     }
5161   /* Add the piece that changed.  */
5162   *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5163   dest = &XEXP (*dest, 1);
5164   /* Skip over pieces that overlap it.  */
5165   diff = bitpos - piece_bitpos + bitsize;
5166   if (!copy)
5167     src = dest;
5168   while (diff > 0 && *src)
5169     {
5170       rtx piece = *src;
5171       diff -= decl_piece_bitsize (piece);
5172       if (copy)
5173         src = &XEXP (piece, 1);
5174       else
5175         {
5176           *src = XEXP (piece, 1);
5177           free_EXPR_LIST_node (piece);
5178         }
5179     }
5180   /* Add padding if needed.  */
5181   if (diff < 0 && *src)
5182     {
5183       if (!copy)
5184         dest = src;
5185       *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5186       dest = &XEXP (*dest, 1);
5187     }
5188   if (!copy)
5189     return;
5190   /* Finally copy all nodes following it.  */
5191   while (*src)
5192     {
5193       *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5194                                decl_piece_bitsize (*src), NULL_RTX);
5195       dest = &XEXP (*dest, 1);
5196       src = &XEXP (*src, 1);
5197     }
5198 }
5199
5200 /* Add a variable location node to the linked list for DECL.  */
5201
5202 static struct var_loc_node *
5203 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5204 {
5205   unsigned int decl_id;
5206   var_loc_list *temp;
5207   struct var_loc_node *loc = NULL;
5208   HOST_WIDE_INT bitsize = -1, bitpos = -1;
5209
5210   if (TREE_CODE (decl) == VAR_DECL
5211       && DECL_HAS_DEBUG_EXPR_P (decl))
5212     {
5213       tree realdecl = DECL_DEBUG_EXPR (decl);
5214       if (handled_component_p (realdecl)
5215           || (TREE_CODE (realdecl) == MEM_REF
5216               && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5217         {
5218           HOST_WIDE_INT maxsize;
5219           tree innerdecl;
5220           innerdecl
5221             = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5222           if (!DECL_P (innerdecl)
5223               || DECL_IGNORED_P (innerdecl)
5224               || TREE_STATIC (innerdecl)
5225               || bitsize <= 0
5226               || bitpos + bitsize > 256
5227               || bitsize != maxsize)
5228             return NULL;
5229           decl = innerdecl;
5230         }
5231     }
5232
5233   decl_id = DECL_UID (decl);
5234   var_loc_list **slot
5235     = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5236   if (*slot == NULL)
5237     {
5238       temp = ggc_cleared_alloc<var_loc_list> ();
5239       temp->decl_id = decl_id;
5240       *slot = temp;
5241     }
5242   else
5243     temp = *slot;
5244
5245   /* For PARM_DECLs try to keep around the original incoming value,
5246      even if that means we'll emit a zero-range .debug_loc entry.  */
5247   if (temp->last
5248       && temp->first == temp->last
5249       && TREE_CODE (decl) == PARM_DECL
5250       && NOTE_P (temp->first->loc)
5251       && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5252       && DECL_INCOMING_RTL (decl)
5253       && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5254       && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5255          == GET_CODE (DECL_INCOMING_RTL (decl))
5256       && prev_real_insn (temp->first->loc) == NULL_RTX
5257       && (bitsize != -1
5258           || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5259                            NOTE_VAR_LOCATION_LOC (loc_note))
5260           || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5261               != NOTE_VAR_LOCATION_STATUS (loc_note))))
5262     {
5263       loc = ggc_cleared_alloc<var_loc_node> ();
5264       temp->first->next = loc;
5265       temp->last = loc;
5266       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5267     }
5268   else if (temp->last)
5269     {
5270       struct var_loc_node *last = temp->last, *unused = NULL;
5271       rtx *piece_loc = NULL, last_loc_note;
5272       HOST_WIDE_INT piece_bitpos = 0;
5273       if (last->next)
5274         {
5275           last = last->next;
5276           gcc_assert (last->next == NULL);
5277         }
5278       if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5279         {
5280           piece_loc = &last->loc;
5281           do
5282             {
5283               HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5284               if (piece_bitpos + cur_bitsize > bitpos)
5285                 break;
5286               piece_bitpos += cur_bitsize;
5287               piece_loc = &XEXP (*piece_loc, 1);
5288             }
5289           while (*piece_loc);
5290         }
5291       /* TEMP->LAST here is either pointer to the last but one or
5292          last element in the chained list, LAST is pointer to the
5293          last element.  */
5294       if (label && strcmp (last->label, label) == 0)
5295         {
5296           /* For SRA optimized variables if there weren't any real
5297              insns since last note, just modify the last node.  */
5298           if (piece_loc != NULL)
5299             {
5300               adjust_piece_list (piece_loc, NULL, NULL,
5301                                  bitpos, piece_bitpos, bitsize, loc_note);
5302               return NULL;
5303             }
5304           /* If the last note doesn't cover any instructions, remove it.  */
5305           if (temp->last != last)
5306             {
5307               temp->last->next = NULL;
5308               unused = last;
5309               last = temp->last;
5310               gcc_assert (strcmp (last->label, label) != 0);
5311             }
5312           else
5313             {
5314               gcc_assert (temp->first == temp->last
5315                           || (temp->first->next == temp->last
5316                               && TREE_CODE (decl) == PARM_DECL));
5317               memset (temp->last, '\0', sizeof (*temp->last));
5318               temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5319               return temp->last;
5320             }
5321         }
5322       if (bitsize == -1 && NOTE_P (last->loc))
5323         last_loc_note = last->loc;
5324       else if (piece_loc != NULL
5325                && *piece_loc != NULL_RTX
5326                && piece_bitpos == bitpos
5327                && decl_piece_bitsize (*piece_loc) == bitsize)
5328         last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5329       else
5330         last_loc_note = NULL_RTX;
5331       /* If the current location is the same as the end of the list,
5332          and either both or neither of the locations is uninitialized,
5333          we have nothing to do.  */
5334       if (last_loc_note == NULL_RTX
5335           || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5336                             NOTE_VAR_LOCATION_LOC (loc_note)))
5337           || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5338                != NOTE_VAR_LOCATION_STATUS (loc_note))
5339               && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5340                    == VAR_INIT_STATUS_UNINITIALIZED)
5341                   || (NOTE_VAR_LOCATION_STATUS (loc_note)
5342                       == VAR_INIT_STATUS_UNINITIALIZED))))
5343         {
5344           /* Add LOC to the end of list and update LAST.  If the last
5345              element of the list has been removed above, reuse its
5346              memory for the new node, otherwise allocate a new one.  */
5347           if (unused)
5348             {
5349               loc = unused;
5350               memset (loc, '\0', sizeof (*loc));
5351             }
5352           else
5353             loc = ggc_cleared_alloc<var_loc_node> ();
5354           if (bitsize == -1 || piece_loc == NULL)
5355             loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5356           else
5357             adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5358                                bitpos, piece_bitpos, bitsize, loc_note);
5359           last->next = loc;
5360           /* Ensure TEMP->LAST will point either to the new last but one
5361              element of the chain, or to the last element in it.  */
5362           if (last != temp->last)
5363             temp->last = last;
5364         }
5365       else if (unused)
5366         ggc_free (unused);
5367     }
5368   else
5369     {
5370       loc = ggc_cleared_alloc<var_loc_node> ();
5371       temp->first = loc;
5372       temp->last = loc;
5373       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5374     }
5375   return loc;
5376 }
5377 \f
5378 /* Keep track of the number of spaces used to indent the
5379    output of the debugging routines that print the structure of
5380    the DIE internal representation.  */
5381 static int print_indent;
5382
5383 /* Indent the line the number of spaces given by print_indent.  */
5384
5385 static inline void
5386 print_spaces (FILE *outfile)
5387 {
5388   fprintf (outfile, "%*s", print_indent, "");
5389 }
5390
5391 /* Print a type signature in hex.  */
5392
5393 static inline void
5394 print_signature (FILE *outfile, char *sig)
5395 {
5396   int i;
5397
5398   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5399     fprintf (outfile, "%02x", sig[i] & 0xff);
5400 }
5401
5402 static void print_loc_descr (dw_loc_descr_ref, FILE *);
5403
5404 /* Print the value associated to the VAL DWARF value node to OUTFILE.  If
5405    RECURSE, output location descriptor operations.  */
5406
5407 static void
5408 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
5409 {
5410   switch (val->val_class)
5411     {
5412     case dw_val_class_addr:
5413       fprintf (outfile, "address");
5414       break;
5415     case dw_val_class_offset:
5416       fprintf (outfile, "offset");
5417       break;
5418     case dw_val_class_loc:
5419       fprintf (outfile, "location descriptor");
5420       if (val->v.val_loc == NULL)
5421         fprintf (outfile, " -> <null>\n");
5422       else if (recurse)
5423         {
5424           fprintf (outfile, ":\n");
5425           print_indent += 4;
5426           print_loc_descr (val->v.val_loc, outfile);
5427           print_indent -= 4;
5428         }
5429       else
5430         fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
5431       break;
5432     case dw_val_class_loc_list:
5433       fprintf (outfile, "location list -> label:%s",
5434                val->v.val_loc_list->ll_symbol);
5435       break;
5436     case dw_val_class_range_list:
5437       fprintf (outfile, "range list");
5438       break;
5439     case dw_val_class_const:
5440       fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
5441       break;
5442     case dw_val_class_unsigned_const:
5443       fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
5444       break;
5445     case dw_val_class_const_double:
5446       fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
5447                         HOST_WIDE_INT_PRINT_UNSIGNED")",
5448                val->v.val_double.high,
5449                val->v.val_double.low);
5450       break;
5451     case dw_val_class_wide_int:
5452       {
5453         int i = val->v.val_wide->get_len ();
5454         fprintf (outfile, "constant (");
5455         gcc_assert (i > 0);
5456         if (val->v.val_wide->elt (i - 1) == 0)
5457           fprintf (outfile, "0x");
5458         fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5459                  val->v.val_wide->elt (--i));
5460         while (--i >= 0)
5461           fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5462                    val->v.val_wide->elt (i));
5463         fprintf (outfile, ")");
5464         break;
5465       }
5466     case dw_val_class_vec:
5467       fprintf (outfile, "floating-point or vector constant");
5468       break;
5469     case dw_val_class_flag:
5470       fprintf (outfile, "%u", val->v.val_flag);
5471       break;
5472     case dw_val_class_die_ref:
5473       if (val->v.val_die_ref.die != NULL)
5474         {
5475           dw_die_ref die = val->v.val_die_ref.die;
5476
5477           if (die->comdat_type_p)
5478             {
5479               fprintf (outfile, "die -> signature: ");
5480               print_signature (outfile,
5481                                die->die_id.die_type_node->signature);
5482             }
5483           else if (die->die_id.die_symbol)
5484             fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
5485           else
5486             fprintf (outfile, "die -> %ld", die->die_offset);
5487           fprintf (outfile, " (%p)", (void *) die);
5488         }
5489       else
5490         fprintf (outfile, "die -> <null>");
5491       break;
5492     case dw_val_class_vms_delta:
5493       fprintf (outfile, "delta: @slotcount(%s-%s)",
5494                val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
5495       break;
5496     case dw_val_class_lbl_id:
5497     case dw_val_class_lineptr:
5498     case dw_val_class_macptr:
5499     case dw_val_class_high_pc:
5500       fprintf (outfile, "label: %s", val->v.val_lbl_id);
5501       break;
5502     case dw_val_class_str:
5503       if (val->v.val_str->str != NULL)
5504         fprintf (outfile, "\"%s\"", val->v.val_str->str);
5505       else
5506         fprintf (outfile, "<null>");
5507       break;
5508     case dw_val_class_file:
5509       fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
5510                val->v.val_file->emitted_number);
5511       break;
5512     case dw_val_class_data8:
5513       {
5514         int i;
5515
5516         for (i = 0; i < 8; i++)
5517           fprintf (outfile, "%02x", val->v.val_data8[i]);
5518         break;
5519       }
5520     default:
5521       break;
5522     }
5523 }
5524
5525 /* Likewise, for a DIE attribute.  */
5526
5527 static void
5528 print_attribute (dw_attr_ref a, bool recurse, FILE *outfile)
5529 {
5530   print_dw_val (&a->dw_attr_val, recurse, outfile);
5531 }
5532
5533
5534 /* Print the list of operands in the LOC location description to OUTFILE.  This
5535    routine is a debugging aid only.  */
5536
5537 static void
5538 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
5539 {
5540   dw_loc_descr_ref l = loc;
5541
5542   if (loc == NULL)
5543     {
5544       print_spaces (outfile);
5545       fprintf (outfile, "<null>\n");
5546       return;
5547     }
5548
5549   for (l = loc; l != NULL; l = l->dw_loc_next)
5550     {
5551       print_spaces (outfile);
5552       fprintf (outfile, "(%p) %s",
5553                (void *) l,
5554                dwarf_stack_op_name (l->dw_loc_opc));
5555       if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
5556         {
5557           fprintf (outfile, " ");
5558           print_dw_val (&l->dw_loc_oprnd1, false, outfile);
5559         }
5560       if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
5561         {
5562           fprintf (outfile, ", ");
5563           print_dw_val (&l->dw_loc_oprnd2, false, outfile);
5564         }
5565       fprintf (outfile, "\n");
5566     }
5567 }
5568
5569 /* Print the information associated with a given DIE, and its children.
5570    This routine is a debugging aid only.  */
5571
5572 static void
5573 print_die (dw_die_ref die, FILE *outfile)
5574 {
5575   dw_attr_ref a;
5576   dw_die_ref c;
5577   unsigned ix;
5578
5579   print_spaces (outfile);
5580   fprintf (outfile, "DIE %4ld: %s (%p)\n",
5581            die->die_offset, dwarf_tag_name (die->die_tag),
5582            (void*) die);
5583   print_spaces (outfile);
5584   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
5585   fprintf (outfile, " offset: %ld", die->die_offset);
5586   fprintf (outfile, " mark: %d\n", die->die_mark);
5587
5588   if (die->comdat_type_p)
5589     {
5590       print_spaces (outfile);
5591       fprintf (outfile, "  signature: ");
5592       print_signature (outfile, die->die_id.die_type_node->signature);
5593       fprintf (outfile, "\n");
5594     }
5595
5596   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5597     {
5598       print_spaces (outfile);
5599       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
5600
5601       print_attribute (a, true, outfile);
5602       fprintf (outfile, "\n");
5603     }
5604
5605   if (die->die_child != NULL)
5606     {
5607       print_indent += 4;
5608       FOR_EACH_CHILD (die, c, print_die (c, outfile));
5609       print_indent -= 4;
5610     }
5611   if (print_indent == 0)
5612     fprintf (outfile, "\n");
5613 }
5614
5615 /* Print the list of operations in the LOC location description.  */
5616
5617 DEBUG_FUNCTION void
5618 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
5619 {
5620   print_loc_descr (loc, stderr);
5621 }
5622
5623 /* Print the information collected for a given DIE.  */
5624
5625 DEBUG_FUNCTION void
5626 debug_dwarf_die (dw_die_ref die)
5627 {
5628   print_die (die, stderr);
5629 }
5630
5631 DEBUG_FUNCTION void
5632 debug (die_struct &ref)
5633 {
5634   print_die (&ref, stderr);
5635 }
5636
5637 DEBUG_FUNCTION void
5638 debug (die_struct *ptr)
5639 {
5640   if (ptr)
5641     debug (*ptr);
5642   else
5643     fprintf (stderr, "<nil>\n");
5644 }
5645
5646
5647 /* Print all DWARF information collected for the compilation unit.
5648    This routine is a debugging aid only.  */
5649
5650 DEBUG_FUNCTION void
5651 debug_dwarf (void)
5652 {
5653   print_indent = 0;
5654   print_die (comp_unit_die (), stderr);
5655 }
5656 \f
5657 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
5658    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
5659    DIE that marks the start of the DIEs for this include file.  */
5660
5661 static dw_die_ref
5662 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5663 {
5664   const char *filename = get_AT_string (bincl_die, DW_AT_name);
5665   dw_die_ref new_unit = gen_compile_unit_die (filename);
5666
5667   new_unit->die_sib = old_unit;
5668   return new_unit;
5669 }
5670
5671 /* Close an include-file CU and reopen the enclosing one.  */
5672
5673 static dw_die_ref
5674 pop_compile_unit (dw_die_ref old_unit)
5675 {
5676   dw_die_ref new_unit = old_unit->die_sib;
5677
5678   old_unit->die_sib = NULL;
5679   return new_unit;
5680 }
5681
5682 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5683 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5684 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5685
5686 /* Calculate the checksum of a location expression.  */
5687
5688 static inline void
5689 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5690 {
5691   int tem;
5692   inchash::hash hstate;
5693   hashval_t hash;
5694
5695   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5696   CHECKSUM (tem);
5697   hash_loc_operands (loc, hstate);
5698   hash = hstate.end();
5699   CHECKSUM (hash);
5700 }
5701
5702 /* Calculate the checksum of an attribute.  */
5703
5704 static void
5705 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5706 {
5707   dw_loc_descr_ref loc;
5708   rtx r;
5709
5710   CHECKSUM (at->dw_attr);
5711
5712   /* We don't care that this was compiled with a different compiler
5713      snapshot; if the output is the same, that's what matters.  */
5714   if (at->dw_attr == DW_AT_producer)
5715     return;
5716
5717   switch (AT_class (at))
5718     {
5719     case dw_val_class_const:
5720       CHECKSUM (at->dw_attr_val.v.val_int);
5721       break;
5722     case dw_val_class_unsigned_const:
5723       CHECKSUM (at->dw_attr_val.v.val_unsigned);
5724       break;
5725     case dw_val_class_const_double:
5726       CHECKSUM (at->dw_attr_val.v.val_double);
5727       break;
5728     case dw_val_class_wide_int:
5729       CHECKSUM (*at->dw_attr_val.v.val_wide);
5730       break;
5731     case dw_val_class_vec:
5732       CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5733                       (at->dw_attr_val.v.val_vec.length
5734                        * at->dw_attr_val.v.val_vec.elt_size));
5735       break;
5736     case dw_val_class_flag:
5737       CHECKSUM (at->dw_attr_val.v.val_flag);
5738       break;
5739     case dw_val_class_str:
5740       CHECKSUM_STRING (AT_string (at));
5741       break;
5742
5743     case dw_val_class_addr:
5744       r = AT_addr (at);
5745       gcc_assert (GET_CODE (r) == SYMBOL_REF);
5746       CHECKSUM_STRING (XSTR (r, 0));
5747       break;
5748
5749     case dw_val_class_offset:
5750       CHECKSUM (at->dw_attr_val.v.val_offset);
5751       break;
5752
5753     case dw_val_class_loc:
5754       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5755         loc_checksum (loc, ctx);
5756       break;
5757
5758     case dw_val_class_die_ref:
5759       die_checksum (AT_ref (at), ctx, mark);
5760       break;
5761
5762     case dw_val_class_fde_ref:
5763     case dw_val_class_vms_delta:
5764     case dw_val_class_lbl_id:
5765     case dw_val_class_lineptr:
5766     case dw_val_class_macptr:
5767     case dw_val_class_high_pc:
5768       break;
5769
5770     case dw_val_class_file:
5771       CHECKSUM_STRING (AT_file (at)->filename);
5772       break;
5773
5774     case dw_val_class_data8:
5775       CHECKSUM (at->dw_attr_val.v.val_data8);
5776       break;
5777
5778     default:
5779       break;
5780     }
5781 }
5782
5783 /* Calculate the checksum of a DIE.  */
5784
5785 static void
5786 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5787 {
5788   dw_die_ref c;
5789   dw_attr_ref a;
5790   unsigned ix;
5791
5792   /* To avoid infinite recursion.  */
5793   if (die->die_mark)
5794     {
5795       CHECKSUM (die->die_mark);
5796       return;
5797     }
5798   die->die_mark = ++(*mark);
5799
5800   CHECKSUM (die->die_tag);
5801
5802   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5803     attr_checksum (a, ctx, mark);
5804
5805   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5806 }
5807
5808 #undef CHECKSUM
5809 #undef CHECKSUM_BLOCK
5810 #undef CHECKSUM_STRING
5811
5812 /* For DWARF-4 types, include the trailing NULL when checksumming strings.  */
5813 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5814 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5815 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5816 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5817 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5818 #define CHECKSUM_ATTR(FOO) \
5819   if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5820
5821 /* Calculate the checksum of a number in signed LEB128 format.  */
5822
5823 static void
5824 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5825 {
5826   unsigned char byte;
5827   bool more;
5828
5829   while (1)
5830     {
5831       byte = (value & 0x7f);
5832       value >>= 7;
5833       more = !((value == 0 && (byte & 0x40) == 0)
5834                 || (value == -1 && (byte & 0x40) != 0));
5835       if (more)
5836         byte |= 0x80;
5837       CHECKSUM (byte);
5838       if (!more)
5839         break;
5840     }
5841 }
5842
5843 /* Calculate the checksum of a number in unsigned LEB128 format.  */
5844
5845 static void
5846 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5847 {
5848   while (1)
5849     {
5850       unsigned char byte = (value & 0x7f);
5851       value >>= 7;
5852       if (value != 0)
5853         /* More bytes to follow.  */
5854         byte |= 0x80;
5855       CHECKSUM (byte);
5856       if (value == 0)
5857         break;
5858     }
5859 }
5860
5861 /* Checksum the context of the DIE.  This adds the names of any
5862    surrounding namespaces or structures to the checksum.  */
5863
5864 static void
5865 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5866 {
5867   const char *name;
5868   dw_die_ref spec;
5869   int tag = die->die_tag;
5870
5871   if (tag != DW_TAG_namespace
5872       && tag != DW_TAG_structure_type
5873       && tag != DW_TAG_class_type)
5874     return;
5875
5876   name = get_AT_string (die, DW_AT_name);
5877
5878   spec = get_AT_ref (die, DW_AT_specification);
5879   if (spec != NULL)
5880     die = spec;
5881
5882   if (die->die_parent != NULL)
5883     checksum_die_context (die->die_parent, ctx);
5884
5885   CHECKSUM_ULEB128 ('C');
5886   CHECKSUM_ULEB128 (tag);
5887   if (name != NULL)
5888     CHECKSUM_STRING (name);
5889 }
5890
5891 /* Calculate the checksum of a location expression.  */
5892
5893 static inline void
5894 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5895 {
5896   /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5897      were emitted as a DW_FORM_sdata instead of a location expression.  */
5898   if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5899     {
5900       CHECKSUM_ULEB128 (DW_FORM_sdata);
5901       CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5902       return;
5903     }
5904
5905   /* Otherwise, just checksum the raw location expression.  */
5906   while (loc != NULL)
5907     {
5908       inchash::hash hstate;
5909       hashval_t hash;
5910
5911       CHECKSUM_ULEB128 (loc->dtprel);
5912       CHECKSUM_ULEB128 (loc->dw_loc_opc);
5913       hash_loc_operands (loc, hstate);
5914       hash = hstate.end ();
5915       CHECKSUM (hash);
5916       loc = loc->dw_loc_next;
5917     }
5918 }
5919
5920 /* Calculate the checksum of an attribute.  */
5921
5922 static void
5923 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5924                        struct md5_ctx *ctx, int *mark)
5925 {
5926   dw_loc_descr_ref loc;
5927   rtx r;
5928
5929   if (AT_class (at) == dw_val_class_die_ref)
5930     {
5931       dw_die_ref target_die = AT_ref (at);
5932
5933       /* For pointer and reference types, we checksum only the (qualified)
5934          name of the target type (if there is a name).  For friend entries,
5935          we checksum only the (qualified) name of the target type or function.
5936          This allows the checksum to remain the same whether the target type
5937          is complete or not.  */
5938       if ((at->dw_attr == DW_AT_type
5939            && (tag == DW_TAG_pointer_type
5940                || tag == DW_TAG_reference_type
5941                || tag == DW_TAG_rvalue_reference_type
5942                || tag == DW_TAG_ptr_to_member_type))
5943           || (at->dw_attr == DW_AT_friend
5944               && tag == DW_TAG_friend))
5945         {
5946           dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5947
5948           if (name_attr != NULL)
5949             {
5950               dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5951
5952               if (decl == NULL)
5953                 decl = target_die;
5954               CHECKSUM_ULEB128 ('N');
5955               CHECKSUM_ULEB128 (at->dw_attr);
5956               if (decl->die_parent != NULL)
5957                 checksum_die_context (decl->die_parent, ctx);
5958               CHECKSUM_ULEB128 ('E');
5959               CHECKSUM_STRING (AT_string (name_attr));
5960               return;
5961             }
5962         }
5963
5964       /* For all other references to another DIE, we check to see if the
5965          target DIE has already been visited.  If it has, we emit a
5966          backward reference; if not, we descend recursively.  */
5967       if (target_die->die_mark > 0)
5968         {
5969           CHECKSUM_ULEB128 ('R');
5970           CHECKSUM_ULEB128 (at->dw_attr);
5971           CHECKSUM_ULEB128 (target_die->die_mark);
5972         }
5973       else
5974         {
5975           dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5976
5977           if (decl == NULL)
5978             decl = target_die;
5979           target_die->die_mark = ++(*mark);
5980           CHECKSUM_ULEB128 ('T');
5981           CHECKSUM_ULEB128 (at->dw_attr);
5982           if (decl->die_parent != NULL)
5983             checksum_die_context (decl->die_parent, ctx);
5984           die_checksum_ordered (target_die, ctx, mark);
5985         }
5986       return;
5987     }
5988
5989   CHECKSUM_ULEB128 ('A');
5990   CHECKSUM_ULEB128 (at->dw_attr);
5991
5992   switch (AT_class (at))
5993     {
5994     case dw_val_class_const:
5995       CHECKSUM_ULEB128 (DW_FORM_sdata);
5996       CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5997       break;
5998
5999     case dw_val_class_unsigned_const:
6000       CHECKSUM_ULEB128 (DW_FORM_sdata);
6001       CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6002       break;
6003
6004     case dw_val_class_const_double:
6005       CHECKSUM_ULEB128 (DW_FORM_block);
6006       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6007       CHECKSUM (at->dw_attr_val.v.val_double);
6008       break;
6009
6010     case dw_val_class_wide_int:
6011       CHECKSUM_ULEB128 (DW_FORM_block);
6012       CHECKSUM_ULEB128 (sizeof (*at->dw_attr_val.v.val_wide));
6013       CHECKSUM (*at->dw_attr_val.v.val_wide);
6014       break;
6015
6016     case dw_val_class_vec:
6017       CHECKSUM_ULEB128 (DW_FORM_block);
6018       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6019                         * at->dw_attr_val.v.val_vec.elt_size);
6020       CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6021                       (at->dw_attr_val.v.val_vec.length
6022                        * at->dw_attr_val.v.val_vec.elt_size));
6023       break;
6024
6025     case dw_val_class_flag:
6026       CHECKSUM_ULEB128 (DW_FORM_flag);
6027       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6028       break;
6029
6030     case dw_val_class_str:
6031       CHECKSUM_ULEB128 (DW_FORM_string);
6032       CHECKSUM_STRING (AT_string (at));
6033       break;
6034
6035     case dw_val_class_addr:
6036       r = AT_addr (at);
6037       gcc_assert (GET_CODE (r) == SYMBOL_REF);
6038       CHECKSUM_ULEB128 (DW_FORM_string);
6039       CHECKSUM_STRING (XSTR (r, 0));
6040       break;
6041
6042     case dw_val_class_offset:
6043       CHECKSUM_ULEB128 (DW_FORM_sdata);
6044       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6045       break;
6046
6047     case dw_val_class_loc:
6048       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6049         loc_checksum_ordered (loc, ctx);
6050       break;
6051
6052     case dw_val_class_fde_ref:
6053     case dw_val_class_lbl_id:
6054     case dw_val_class_lineptr:
6055     case dw_val_class_macptr:
6056     case dw_val_class_high_pc:
6057       break;
6058
6059     case dw_val_class_file:
6060       CHECKSUM_ULEB128 (DW_FORM_string);
6061       CHECKSUM_STRING (AT_file (at)->filename);
6062       break;
6063
6064     case dw_val_class_data8:
6065       CHECKSUM (at->dw_attr_val.v.val_data8);
6066       break;
6067
6068     default:
6069       break;
6070     }
6071 }
6072
6073 struct checksum_attributes
6074 {
6075   dw_attr_ref at_name;
6076   dw_attr_ref at_type;
6077   dw_attr_ref at_friend;
6078   dw_attr_ref at_accessibility;
6079   dw_attr_ref at_address_class;
6080   dw_attr_ref at_allocated;
6081   dw_attr_ref at_artificial;
6082   dw_attr_ref at_associated;
6083   dw_attr_ref at_binary_scale;
6084   dw_attr_ref at_bit_offset;
6085   dw_attr_ref at_bit_size;
6086   dw_attr_ref at_bit_stride;
6087   dw_attr_ref at_byte_size;
6088   dw_attr_ref at_byte_stride;
6089   dw_attr_ref at_const_value;
6090   dw_attr_ref at_containing_type;
6091   dw_attr_ref at_count;
6092   dw_attr_ref at_data_location;
6093   dw_attr_ref at_data_member_location;
6094   dw_attr_ref at_decimal_scale;
6095   dw_attr_ref at_decimal_sign;
6096   dw_attr_ref at_default_value;
6097   dw_attr_ref at_digit_count;
6098   dw_attr_ref at_discr;
6099   dw_attr_ref at_discr_list;
6100   dw_attr_ref at_discr_value;
6101   dw_attr_ref at_encoding;
6102   dw_attr_ref at_endianity;
6103   dw_attr_ref at_explicit;
6104   dw_attr_ref at_is_optional;
6105   dw_attr_ref at_location;
6106   dw_attr_ref at_lower_bound;
6107   dw_attr_ref at_mutable;
6108   dw_attr_ref at_ordering;
6109   dw_attr_ref at_picture_string;
6110   dw_attr_ref at_prototyped;
6111   dw_attr_ref at_small;
6112   dw_attr_ref at_segment;
6113   dw_attr_ref at_string_length;
6114   dw_attr_ref at_threads_scaled;
6115   dw_attr_ref at_upper_bound;
6116   dw_attr_ref at_use_location;
6117   dw_attr_ref at_use_UTF8;
6118   dw_attr_ref at_variable_parameter;
6119   dw_attr_ref at_virtuality;
6120   dw_attr_ref at_visibility;
6121   dw_attr_ref at_vtable_elem_location;
6122 };
6123
6124 /* Collect the attributes that we will want to use for the checksum.  */
6125
6126 static void
6127 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6128 {
6129   dw_attr_ref a;
6130   unsigned ix;
6131
6132   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6133     {
6134       switch (a->dw_attr)
6135         {
6136         case DW_AT_name:
6137           attrs->at_name = a;
6138           break;
6139         case DW_AT_type:
6140           attrs->at_type = a;
6141           break;
6142         case DW_AT_friend:
6143           attrs->at_friend = a;
6144           break;
6145         case DW_AT_accessibility:
6146           attrs->at_accessibility = a;
6147           break;
6148         case DW_AT_address_class:
6149           attrs->at_address_class = a;
6150           break;
6151         case DW_AT_allocated:
6152           attrs->at_allocated = a;
6153           break;
6154         case DW_AT_artificial:
6155           attrs->at_artificial = a;
6156           break;
6157         case DW_AT_associated:
6158           attrs->at_associated = a;
6159           break;
6160         case DW_AT_binary_scale:
6161           attrs->at_binary_scale = a;
6162           break;
6163         case DW_AT_bit_offset:
6164           attrs->at_bit_offset = a;
6165           break;
6166         case DW_AT_bit_size:
6167           attrs->at_bit_size = a;
6168           break;
6169         case DW_AT_bit_stride:
6170           attrs->at_bit_stride = a;
6171           break;
6172         case DW_AT_byte_size:
6173           attrs->at_byte_size = a;
6174           break;
6175         case DW_AT_byte_stride:
6176           attrs->at_byte_stride = a;
6177           break;
6178         case DW_AT_const_value:
6179           attrs->at_const_value = a;
6180           break;
6181         case DW_AT_containing_type:
6182           attrs->at_containing_type = a;
6183           break;
6184         case DW_AT_count:
6185           attrs->at_count = a;
6186           break;
6187         case DW_AT_data_location:
6188           attrs->at_data_location = a;
6189           break;
6190         case DW_AT_data_member_location:
6191           attrs->at_data_member_location = a;
6192           break;
6193         case DW_AT_decimal_scale:
6194           attrs->at_decimal_scale = a;
6195           break;
6196         case DW_AT_decimal_sign:
6197           attrs->at_decimal_sign = a;
6198           break;
6199         case DW_AT_default_value:
6200           attrs->at_default_value = a;
6201           break;
6202         case DW_AT_digit_count:
6203           attrs->at_digit_count = a;
6204           break;
6205         case DW_AT_discr:
6206           attrs->at_discr = a;
6207           break;
6208         case DW_AT_discr_list:
6209           attrs->at_discr_list = a;
6210           break;
6211         case DW_AT_discr_value:
6212           attrs->at_discr_value = a;
6213           break;
6214         case DW_AT_encoding:
6215           attrs->at_encoding = a;
6216           break;
6217         case DW_AT_endianity:
6218           attrs->at_endianity = a;
6219           break;
6220         case DW_AT_explicit:
6221           attrs->at_explicit = a;
6222           break;
6223         case DW_AT_is_optional:
6224           attrs->at_is_optional = a;
6225           break;
6226         case DW_AT_location:
6227           attrs->at_location = a;
6228           break;
6229         case DW_AT_lower_bound:
6230           attrs->at_lower_bound = a;
6231           break;
6232         case DW_AT_mutable:
6233           attrs->at_mutable = a;
6234           break;
6235         case DW_AT_ordering:
6236           attrs->at_ordering = a;
6237           break;
6238         case DW_AT_picture_string:
6239           attrs->at_picture_string = a;
6240           break;
6241         case DW_AT_prototyped:
6242           attrs->at_prototyped = a;
6243           break;
6244         case DW_AT_small:
6245           attrs->at_small = a;
6246           break;
6247         case DW_AT_segment:
6248           attrs->at_segment = a;
6249           break;
6250         case DW_AT_string_length:
6251           attrs->at_string_length = a;
6252           break;
6253         case DW_AT_threads_scaled:
6254           attrs->at_threads_scaled = a;
6255           break;
6256         case DW_AT_upper_bound:
6257           attrs->at_upper_bound = a;
6258           break;
6259         case DW_AT_use_location:
6260           attrs->at_use_location = a;
6261           break;
6262         case DW_AT_use_UTF8:
6263           attrs->at_use_UTF8 = a;
6264           break;
6265         case DW_AT_variable_parameter:
6266           attrs->at_variable_parameter = a;
6267           break;
6268         case DW_AT_virtuality:
6269           attrs->at_virtuality = a;
6270           break;
6271         case DW_AT_visibility:
6272           attrs->at_visibility = a;
6273           break;
6274         case DW_AT_vtable_elem_location:
6275           attrs->at_vtable_elem_location = a;
6276           break;
6277         default:
6278           break;
6279         }
6280     }
6281 }
6282
6283 /* Calculate the checksum of a DIE, using an ordered subset of attributes.  */
6284
6285 static void
6286 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6287 {
6288   dw_die_ref c;
6289   dw_die_ref decl;
6290   struct checksum_attributes attrs;
6291
6292   CHECKSUM_ULEB128 ('D');
6293   CHECKSUM_ULEB128 (die->die_tag);
6294
6295   memset (&attrs, 0, sizeof (attrs));
6296
6297   decl = get_AT_ref (die, DW_AT_specification);
6298   if (decl != NULL)
6299     collect_checksum_attributes (&attrs, decl);
6300   collect_checksum_attributes (&attrs, die);
6301
6302   CHECKSUM_ATTR (attrs.at_name);
6303   CHECKSUM_ATTR (attrs.at_accessibility);
6304   CHECKSUM_ATTR (attrs.at_address_class);
6305   CHECKSUM_ATTR (attrs.at_allocated);
6306   CHECKSUM_ATTR (attrs.at_artificial);
6307   CHECKSUM_ATTR (attrs.at_associated);
6308   CHECKSUM_ATTR (attrs.at_binary_scale);
6309   CHECKSUM_ATTR (attrs.at_bit_offset);
6310   CHECKSUM_ATTR (attrs.at_bit_size);
6311   CHECKSUM_ATTR (attrs.at_bit_stride);
6312   CHECKSUM_ATTR (attrs.at_byte_size);
6313   CHECKSUM_ATTR (attrs.at_byte_stride);
6314   CHECKSUM_ATTR (attrs.at_const_value);
6315   CHECKSUM_ATTR (attrs.at_containing_type);
6316   CHECKSUM_ATTR (attrs.at_count);
6317   CHECKSUM_ATTR (attrs.at_data_location);
6318   CHECKSUM_ATTR (attrs.at_data_member_location);
6319   CHECKSUM_ATTR (attrs.at_decimal_scale);
6320   CHECKSUM_ATTR (attrs.at_decimal_sign);
6321   CHECKSUM_ATTR (attrs.at_default_value);
6322   CHECKSUM_ATTR (attrs.at_digit_count);
6323   CHECKSUM_ATTR (attrs.at_discr);
6324   CHECKSUM_ATTR (attrs.at_discr_list);
6325   CHECKSUM_ATTR (attrs.at_discr_value);
6326   CHECKSUM_ATTR (attrs.at_encoding);
6327   CHECKSUM_ATTR (attrs.at_endianity);
6328   CHECKSUM_ATTR (attrs.at_explicit);
6329   CHECKSUM_ATTR (attrs.at_is_optional);
6330   CHECKSUM_ATTR (attrs.at_location);
6331   CHECKSUM_ATTR (attrs.at_lower_bound);
6332   CHECKSUM_ATTR (attrs.at_mutable);
6333   CHECKSUM_ATTR (attrs.at_ordering);
6334   CHECKSUM_ATTR (attrs.at_picture_string);
6335   CHECKSUM_ATTR (attrs.at_prototyped);
6336   CHECKSUM_ATTR (attrs.at_small);
6337   CHECKSUM_ATTR (attrs.at_segment);
6338   CHECKSUM_ATTR (attrs.at_string_length);
6339   CHECKSUM_ATTR (attrs.at_threads_scaled);
6340   CHECKSUM_ATTR (attrs.at_upper_bound);
6341   CHECKSUM_ATTR (attrs.at_use_location);
6342   CHECKSUM_ATTR (attrs.at_use_UTF8);
6343   CHECKSUM_ATTR (attrs.at_variable_parameter);
6344   CHECKSUM_ATTR (attrs.at_virtuality);
6345   CHECKSUM_ATTR (attrs.at_visibility);
6346   CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6347   CHECKSUM_ATTR (attrs.at_type);
6348   CHECKSUM_ATTR (attrs.at_friend);
6349
6350   /* Checksum the child DIEs.  */
6351   c = die->die_child;
6352   if (c) do {
6353     dw_attr_ref name_attr;
6354
6355     c = c->die_sib;
6356     name_attr = get_AT (c, DW_AT_name);
6357     if (is_template_instantiation (c))
6358       {
6359         /* Ignore instantiations of member type and function templates.  */
6360       }
6361     else if (name_attr != NULL
6362              && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6363       {
6364         /* Use a shallow checksum for named nested types and member
6365            functions.  */
6366         CHECKSUM_ULEB128 ('S');
6367         CHECKSUM_ULEB128 (c->die_tag);
6368         CHECKSUM_STRING (AT_string (name_attr));
6369       }
6370     else
6371       {
6372         /* Use a deep checksum for other children.  */
6373         /* Mark this DIE so it gets processed when unmarking.  */
6374         if (c->die_mark == 0)
6375           c->die_mark = -1;
6376         die_checksum_ordered (c, ctx, mark);
6377       }
6378   } while (c != die->die_child);
6379
6380   CHECKSUM_ULEB128 (0);
6381 }
6382
6383 /* Add a type name and tag to a hash.  */
6384 static void
6385 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6386 {
6387   CHECKSUM_ULEB128 (tag);
6388   CHECKSUM_STRING (name);
6389 }
6390
6391 #undef CHECKSUM
6392 #undef CHECKSUM_STRING
6393 #undef CHECKSUM_ATTR
6394 #undef CHECKSUM_LEB128
6395 #undef CHECKSUM_ULEB128
6396
6397 /* Generate the type signature for DIE.  This is computed by generating an
6398    MD5 checksum over the DIE's tag, its relevant attributes, and its
6399    children.  Attributes that are references to other DIEs are processed
6400    by recursion, using the MARK field to prevent infinite recursion.
6401    If the DIE is nested inside a namespace or another type, we also
6402    need to include that context in the signature.  The lower 64 bits
6403    of the resulting MD5 checksum comprise the signature.  */
6404
6405 static void
6406 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6407 {
6408   int mark;
6409   const char *name;
6410   unsigned char checksum[16];
6411   struct md5_ctx ctx;
6412   dw_die_ref decl;
6413   dw_die_ref parent;
6414
6415   name = get_AT_string (die, DW_AT_name);
6416   decl = get_AT_ref (die, DW_AT_specification);
6417   parent = get_die_parent (die);
6418
6419   /* First, compute a signature for just the type name (and its surrounding
6420      context, if any.  This is stored in the type unit DIE for link-time
6421      ODR (one-definition rule) checking.  */
6422
6423   if (is_cxx () && name != NULL)
6424     {
6425       md5_init_ctx (&ctx);
6426
6427       /* Checksum the names of surrounding namespaces and structures.  */
6428       if (parent != NULL)
6429         checksum_die_context (parent, &ctx);
6430
6431       /* Checksum the current DIE. */
6432       die_odr_checksum (die->die_tag, name, &ctx);
6433       md5_finish_ctx (&ctx, checksum);
6434
6435       add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6436     }
6437
6438   /* Next, compute the complete type signature.  */
6439
6440   md5_init_ctx (&ctx);
6441   mark = 1;
6442   die->die_mark = mark;
6443
6444   /* Checksum the names of surrounding namespaces and structures.  */
6445   if (parent != NULL)
6446     checksum_die_context (parent, &ctx);
6447
6448   /* Checksum the DIE and its children.  */
6449   die_checksum_ordered (die, &ctx, &mark);
6450   unmark_all_dies (die);
6451   md5_finish_ctx (&ctx, checksum);
6452
6453   /* Store the signature in the type node and link the type DIE and the
6454      type node together.  */
6455   memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6456           DWARF_TYPE_SIGNATURE_SIZE);
6457   die->comdat_type_p = true;
6458   die->die_id.die_type_node = type_node;
6459   type_node->type_die = die;
6460
6461   /* If the DIE is a specification, link its declaration to the type node
6462      as well.  */
6463   if (decl != NULL)
6464     {
6465       decl->comdat_type_p = true;
6466       decl->die_id.die_type_node = type_node;
6467     }
6468 }
6469
6470 /* Do the location expressions look same?  */
6471 static inline int
6472 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6473 {
6474   return loc1->dw_loc_opc == loc2->dw_loc_opc
6475          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6476          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6477 }
6478
6479 /* Do the values look the same?  */
6480 static int
6481 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6482 {
6483   dw_loc_descr_ref loc1, loc2;
6484   rtx r1, r2;
6485
6486   if (v1->val_class != v2->val_class)
6487     return 0;
6488
6489   switch (v1->val_class)
6490     {
6491     case dw_val_class_const:
6492       return v1->v.val_int == v2->v.val_int;
6493     case dw_val_class_unsigned_const:
6494       return v1->v.val_unsigned == v2->v.val_unsigned;
6495     case dw_val_class_const_double:
6496       return v1->v.val_double.high == v2->v.val_double.high
6497              && v1->v.val_double.low == v2->v.val_double.low;
6498     case dw_val_class_wide_int:
6499       return *v1->v.val_wide == *v2->v.val_wide;
6500     case dw_val_class_vec:
6501       if (v1->v.val_vec.length != v2->v.val_vec.length
6502           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6503         return 0;
6504       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6505                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
6506         return 0;
6507       return 1;
6508     case dw_val_class_flag:
6509       return v1->v.val_flag == v2->v.val_flag;
6510     case dw_val_class_str:
6511       return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6512
6513     case dw_val_class_addr:
6514       r1 = v1->v.val_addr;
6515       r2 = v2->v.val_addr;
6516       if (GET_CODE (r1) != GET_CODE (r2))
6517         return 0;
6518       return !rtx_equal_p (r1, r2);
6519
6520     case dw_val_class_offset:
6521       return v1->v.val_offset == v2->v.val_offset;
6522
6523     case dw_val_class_loc:
6524       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6525            loc1 && loc2;
6526            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6527         if (!same_loc_p (loc1, loc2, mark))
6528           return 0;
6529       return !loc1 && !loc2;
6530
6531     case dw_val_class_die_ref:
6532       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6533
6534     case dw_val_class_fde_ref:
6535     case dw_val_class_vms_delta:
6536     case dw_val_class_lbl_id:
6537     case dw_val_class_lineptr:
6538     case dw_val_class_macptr:
6539     case dw_val_class_high_pc:
6540       return 1;
6541
6542     case dw_val_class_file:
6543       return v1->v.val_file == v2->v.val_file;
6544
6545     case dw_val_class_data8:
6546       return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6547
6548     default:
6549       return 1;
6550     }
6551 }
6552
6553 /* Do the attributes look the same?  */
6554
6555 static int
6556 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6557 {
6558   if (at1->dw_attr != at2->dw_attr)
6559     return 0;
6560
6561   /* We don't care that this was compiled with a different compiler
6562      snapshot; if the output is the same, that's what matters. */
6563   if (at1->dw_attr == DW_AT_producer)
6564     return 1;
6565
6566   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6567 }
6568
6569 /* Do the dies look the same?  */
6570
6571 static int
6572 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6573 {
6574   dw_die_ref c1, c2;
6575   dw_attr_ref a1;
6576   unsigned ix;
6577
6578   /* To avoid infinite recursion.  */
6579   if (die1->die_mark)
6580     return die1->die_mark == die2->die_mark;
6581   die1->die_mark = die2->die_mark = ++(*mark);
6582
6583   if (die1->die_tag != die2->die_tag)
6584     return 0;
6585
6586   if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6587     return 0;
6588
6589   FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6590     if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6591       return 0;
6592
6593   c1 = die1->die_child;
6594   c2 = die2->die_child;
6595   if (! c1)
6596     {
6597       if (c2)
6598         return 0;
6599     }
6600   else
6601     for (;;)
6602       {
6603         if (!same_die_p (c1, c2, mark))
6604           return 0;
6605         c1 = c1->die_sib;
6606         c2 = c2->die_sib;
6607         if (c1 == die1->die_child)
6608           {
6609             if (c2 == die2->die_child)
6610               break;
6611             else
6612               return 0;
6613           }
6614     }
6615
6616   return 1;
6617 }
6618
6619 /* Do the dies look the same?  Wrapper around same_die_p.  */
6620
6621 static int
6622 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6623 {
6624   int mark = 0;
6625   int ret = same_die_p (die1, die2, &mark);
6626
6627   unmark_all_dies (die1);
6628   unmark_all_dies (die2);
6629
6630   return ret;
6631 }
6632
6633 /* The prefix to attach to symbols on DIEs in the current comdat debug
6634    info section.  */
6635 static const char *comdat_symbol_id;
6636
6637 /* The index of the current symbol within the current comdat CU.  */
6638 static unsigned int comdat_symbol_number;
6639
6640 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6641    children, and set comdat_symbol_id accordingly.  */
6642
6643 static void
6644 compute_section_prefix (dw_die_ref unit_die)
6645 {
6646   const char *die_name = get_AT_string (unit_die, DW_AT_name);
6647   const char *base = die_name ? lbasename (die_name) : "anonymous";
6648   char *name = XALLOCAVEC (char, strlen (base) + 64);
6649   char *p;
6650   int i, mark;
6651   unsigned char checksum[16];
6652   struct md5_ctx ctx;
6653
6654   /* Compute the checksum of the DIE, then append part of it as hex digits to
6655      the name filename of the unit.  */
6656
6657   md5_init_ctx (&ctx);
6658   mark = 0;
6659   die_checksum (unit_die, &ctx, &mark);
6660   unmark_all_dies (unit_die);
6661   md5_finish_ctx (&ctx, checksum);
6662
6663   sprintf (name, "%s.", base);
6664   clean_symbol_name (name);
6665
6666   p = name + strlen (name);
6667   for (i = 0; i < 4; i++)
6668     {
6669       sprintf (p, "%.2x", checksum[i]);
6670       p += 2;
6671     }
6672
6673   comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6674   comdat_symbol_number = 0;
6675 }
6676
6677 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
6678
6679 static int
6680 is_type_die (dw_die_ref die)
6681 {
6682   switch (die->die_tag)
6683     {
6684     case DW_TAG_array_type:
6685     case DW_TAG_class_type:
6686     case DW_TAG_interface_type:
6687     case DW_TAG_enumeration_type:
6688     case DW_TAG_pointer_type:
6689     case DW_TAG_reference_type:
6690     case DW_TAG_rvalue_reference_type:
6691     case DW_TAG_string_type:
6692     case DW_TAG_structure_type:
6693     case DW_TAG_subroutine_type:
6694     case DW_TAG_union_type:
6695     case DW_TAG_ptr_to_member_type:
6696     case DW_TAG_set_type:
6697     case DW_TAG_subrange_type:
6698     case DW_TAG_base_type:
6699     case DW_TAG_const_type:
6700     case DW_TAG_file_type:
6701     case DW_TAG_packed_type:
6702     case DW_TAG_volatile_type:
6703     case DW_TAG_typedef:
6704       return 1;
6705     default:
6706       return 0;
6707     }
6708 }
6709
6710 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6711    Basically, we want to choose the bits that are likely to be shared between
6712    compilations (types) and leave out the bits that are specific to individual
6713    compilations (functions).  */
6714
6715 static int
6716 is_comdat_die (dw_die_ref c)
6717 {
6718   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6719      we do for stabs.  The advantage is a greater likelihood of sharing between
6720      objects that don't include headers in the same order (and therefore would
6721      put the base types in a different comdat).  jason 8/28/00 */
6722
6723   if (c->die_tag == DW_TAG_base_type)
6724     return 0;
6725
6726   if (c->die_tag == DW_TAG_pointer_type
6727       || c->die_tag == DW_TAG_reference_type
6728       || c->die_tag == DW_TAG_rvalue_reference_type
6729       || c->die_tag == DW_TAG_const_type
6730       || c->die_tag == DW_TAG_volatile_type)
6731     {
6732       dw_die_ref t = get_AT_ref (c, DW_AT_type);
6733
6734       return t ? is_comdat_die (t) : 0;
6735     }
6736
6737   return is_type_die (c);
6738 }
6739
6740 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6741    compilation unit.  */
6742
6743 static int
6744 is_symbol_die (dw_die_ref c)
6745 {
6746   return (is_type_die (c)
6747           || is_declaration_die (c)
6748           || c->die_tag == DW_TAG_namespace
6749           || c->die_tag == DW_TAG_module);
6750 }
6751
6752 /* Returns true iff C is a compile-unit DIE.  */
6753
6754 static inline bool
6755 is_cu_die (dw_die_ref c)
6756 {
6757   return c && c->die_tag == DW_TAG_compile_unit;
6758 }
6759
6760 /* Returns true iff C is a unit DIE of some sort.  */
6761
6762 static inline bool
6763 is_unit_die (dw_die_ref c)
6764 {
6765   return c && (c->die_tag == DW_TAG_compile_unit
6766                || c->die_tag == DW_TAG_partial_unit
6767                || c->die_tag == DW_TAG_type_unit);
6768 }
6769
6770 /* Returns true iff C is a namespace DIE.  */
6771
6772 static inline bool
6773 is_namespace_die (dw_die_ref c)
6774 {
6775   return c && c->die_tag == DW_TAG_namespace;
6776 }
6777
6778 /* Returns true iff C is a class or structure DIE.  */
6779
6780 static inline bool
6781 is_class_die (dw_die_ref c)
6782 {
6783   return c && (c->die_tag == DW_TAG_class_type
6784                || c->die_tag == DW_TAG_structure_type);
6785 }
6786
6787 /* Return non-zero if this DIE is a template parameter.  */
6788
6789 static inline bool
6790 is_template_parameter (dw_die_ref die)
6791 {
6792   switch (die->die_tag)
6793     {
6794     case DW_TAG_template_type_param:
6795     case DW_TAG_template_value_param:
6796     case DW_TAG_GNU_template_template_param:
6797     case DW_TAG_GNU_template_parameter_pack:
6798       return true;
6799     default:
6800       return false;
6801     }
6802 }
6803
6804 /* Return non-zero if this DIE represents a template instantiation.  */
6805
6806 static inline bool
6807 is_template_instantiation (dw_die_ref die)
6808 {
6809   dw_die_ref c;
6810
6811   if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
6812     return false;
6813   FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
6814   return false;
6815 }
6816
6817 static char *
6818 gen_internal_sym (const char *prefix)
6819 {
6820   char buf[256];
6821
6822   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6823   return xstrdup (buf);
6824 }
6825
6826 /* Assign symbols to all worthy DIEs under DIE.  */
6827
6828 static void
6829 assign_symbol_names (dw_die_ref die)
6830 {
6831   dw_die_ref c;
6832
6833   if (is_symbol_die (die) && !die->comdat_type_p)
6834     {
6835       if (comdat_symbol_id)
6836         {
6837           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6838
6839           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6840                    comdat_symbol_id, comdat_symbol_number++);
6841           die->die_id.die_symbol = xstrdup (p);
6842         }
6843       else
6844         die->die_id.die_symbol = gen_internal_sym ("LDIE");
6845     }
6846
6847   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6848 }
6849
6850 struct cu_hash_table_entry
6851 {
6852   dw_die_ref cu;
6853   unsigned min_comdat_num, max_comdat_num;
6854   struct cu_hash_table_entry *next;
6855 };
6856
6857 /* Helpers to manipulate hash table of CUs.  */
6858
6859 struct cu_hash_table_entry_hasher
6860 {
6861   typedef cu_hash_table_entry value_type;
6862   typedef die_struct compare_type;
6863   static inline hashval_t hash (const value_type *);
6864   static inline bool equal (const value_type *, const compare_type *);
6865   static inline void remove (value_type *);
6866 };
6867
6868 inline hashval_t
6869 cu_hash_table_entry_hasher::hash (const value_type *entry)
6870 {
6871   return htab_hash_string (entry->cu->die_id.die_symbol);
6872 }
6873
6874 inline bool
6875 cu_hash_table_entry_hasher::equal (const value_type *entry1,
6876                                    const compare_type *entry2)
6877 {
6878   return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6879 }
6880
6881 inline void
6882 cu_hash_table_entry_hasher::remove (value_type *entry)
6883 {
6884   struct cu_hash_table_entry *next;
6885
6886   while (entry)
6887     {
6888       next = entry->next;
6889       free (entry);
6890       entry = next;
6891     }
6892 }
6893
6894 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
6895
6896 /* Check whether we have already seen this CU and set up SYM_NUM
6897    accordingly.  */
6898 static int
6899 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
6900 {
6901   struct cu_hash_table_entry dummy;
6902   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6903
6904   dummy.max_comdat_num = 0;
6905
6906   slot = htable->find_slot_with_hash (cu,
6907                                       htab_hash_string (cu->die_id.die_symbol),
6908                                       INSERT);
6909   entry = *slot;
6910
6911   for (; entry; last = entry, entry = entry->next)
6912     {
6913       if (same_die_p_wrap (cu, entry->cu))
6914         break;
6915     }
6916
6917   if (entry)
6918     {
6919       *sym_num = entry->min_comdat_num;
6920       return 1;
6921     }
6922
6923   entry = XCNEW (struct cu_hash_table_entry);
6924   entry->cu = cu;
6925   entry->min_comdat_num = *sym_num = last->max_comdat_num;
6926   entry->next = *slot;
6927   *slot = entry;
6928
6929   return 0;
6930 }
6931
6932 /* Record SYM_NUM to record of CU in HTABLE.  */
6933 static void
6934 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
6935                              unsigned int sym_num)
6936 {
6937   struct cu_hash_table_entry **slot, *entry;
6938
6939   slot = htable->find_slot_with_hash (cu,
6940                                       htab_hash_string (cu->die_id.die_symbol),
6941                                       NO_INSERT);
6942   entry = *slot;
6943
6944   entry->max_comdat_num = sym_num;
6945 }
6946
6947 /* Traverse the DIE (which is always comp_unit_die), and set up
6948    additional compilation units for each of the include files we see
6949    bracketed by BINCL/EINCL.  */
6950
6951 static void
6952 break_out_includes (dw_die_ref die)
6953 {
6954   dw_die_ref c;
6955   dw_die_ref unit = NULL;
6956   limbo_die_node *node, **pnode;
6957
6958   c = die->die_child;
6959   if (c) do {
6960     dw_die_ref prev = c;
6961     c = c->die_sib;
6962     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6963            || (unit && is_comdat_die (c)))
6964       {
6965         dw_die_ref next = c->die_sib;
6966
6967         /* This DIE is for a secondary CU; remove it from the main one.  */
6968         remove_child_with_prev (c, prev);
6969
6970         if (c->die_tag == DW_TAG_GNU_BINCL)
6971           unit = push_new_compile_unit (unit, c);
6972         else if (c->die_tag == DW_TAG_GNU_EINCL)
6973           unit = pop_compile_unit (unit);
6974         else
6975           add_child_die (unit, c);
6976         c = next;
6977         if (c == die->die_child)
6978           break;
6979       }
6980   } while (c != die->die_child);
6981
6982 #if 0
6983   /* We can only use this in debugging, since the frontend doesn't check
6984      to make sure that we leave every include file we enter.  */
6985   gcc_assert (!unit);
6986 #endif
6987
6988   assign_symbol_names (die);
6989   cu_hash_type cu_hash_table (10);
6990   for (node = limbo_die_list, pnode = &limbo_die_list;
6991        node;
6992        node = node->next)
6993     {
6994       int is_dupl;
6995
6996       compute_section_prefix (node->die);
6997       is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
6998                         &comdat_symbol_number);
6999       assign_symbol_names (node->die);
7000       if (is_dupl)
7001         *pnode = node->next;
7002       else
7003         {
7004           pnode = &node->next;
7005           record_comdat_symbol_number (node->die, &cu_hash_table,
7006                 comdat_symbol_number);
7007         }
7008     }
7009 }
7010
7011 /* Return non-zero if this DIE is a declaration.  */
7012
7013 static int
7014 is_declaration_die (dw_die_ref die)
7015 {
7016   dw_attr_ref a;
7017   unsigned ix;
7018
7019   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7020     if (a->dw_attr == DW_AT_declaration)
7021       return 1;
7022
7023   return 0;
7024 }
7025
7026 /* Return non-zero if this DIE is nested inside a subprogram.  */
7027
7028 static int
7029 is_nested_in_subprogram (dw_die_ref die)
7030 {
7031   dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7032
7033   if (decl == NULL)
7034     decl = die;
7035   return local_scope_p (decl);
7036 }
7037
7038 /* Return non-zero if this DIE contains a defining declaration of a
7039    subprogram.  */
7040
7041 static int
7042 contains_subprogram_definition (dw_die_ref die)
7043 {
7044   dw_die_ref c;
7045
7046   if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7047     return 1;
7048   FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7049   return 0;
7050 }
7051
7052 /* Return non-zero if this is a type DIE that should be moved to a
7053    COMDAT .debug_types section.  */
7054
7055 static int
7056 should_move_die_to_comdat (dw_die_ref die)
7057 {
7058   switch (die->die_tag)
7059     {
7060     case DW_TAG_class_type:
7061     case DW_TAG_structure_type:
7062     case DW_TAG_enumeration_type:
7063     case DW_TAG_union_type:
7064       /* Don't move declarations, inlined instances, types nested in a
7065          subprogram, or types that contain subprogram definitions.  */
7066       if (is_declaration_die (die)
7067           || get_AT (die, DW_AT_abstract_origin)
7068           || is_nested_in_subprogram (die)
7069           || contains_subprogram_definition (die))
7070         return 0;
7071       return 1;
7072     case DW_TAG_array_type:
7073     case DW_TAG_interface_type:
7074     case DW_TAG_pointer_type:
7075     case DW_TAG_reference_type:
7076     case DW_TAG_rvalue_reference_type:
7077     case DW_TAG_string_type:
7078     case DW_TAG_subroutine_type:
7079     case DW_TAG_ptr_to_member_type:
7080     case DW_TAG_set_type:
7081     case DW_TAG_subrange_type:
7082     case DW_TAG_base_type:
7083     case DW_TAG_const_type:
7084     case DW_TAG_file_type:
7085     case DW_TAG_packed_type:
7086     case DW_TAG_volatile_type:
7087     case DW_TAG_typedef:
7088     default:
7089       return 0;
7090     }
7091 }
7092
7093 /* Make a clone of DIE.  */
7094
7095 static dw_die_ref
7096 clone_die (dw_die_ref die)
7097 {
7098   dw_die_ref clone;
7099   dw_attr_ref a;
7100   unsigned ix;
7101
7102   clone = ggc_cleared_alloc<die_node> ();
7103   clone->die_tag = die->die_tag;
7104
7105   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7106     add_dwarf_attr (clone, a);
7107
7108   return clone;
7109 }
7110
7111 /* Make a clone of the tree rooted at DIE.  */
7112
7113 static dw_die_ref
7114 clone_tree (dw_die_ref die)
7115 {
7116   dw_die_ref c;
7117   dw_die_ref clone = clone_die (die);
7118
7119   FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7120
7121   return clone;
7122 }
7123
7124 /* Make a clone of DIE as a declaration.  */
7125
7126 static dw_die_ref
7127 clone_as_declaration (dw_die_ref die)
7128 {
7129   dw_die_ref clone;
7130   dw_die_ref decl;
7131   dw_attr_ref a;
7132   unsigned ix;
7133
7134   /* If the DIE is already a declaration, just clone it.  */
7135   if (is_declaration_die (die))
7136     return clone_die (die);
7137
7138   /* If the DIE is a specification, just clone its declaration DIE.  */
7139   decl = get_AT_ref (die, DW_AT_specification);
7140   if (decl != NULL)
7141     {
7142       clone = clone_die (decl);
7143       if (die->comdat_type_p)
7144         add_AT_die_ref (clone, DW_AT_signature, die);
7145       return clone;
7146     }
7147
7148   clone = ggc_cleared_alloc<die_node> ();
7149   clone->die_tag = die->die_tag;
7150
7151   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7152     {
7153       /* We don't want to copy over all attributes.
7154          For example we don't want DW_AT_byte_size because otherwise we will no
7155          longer have a declaration and GDB will treat it as a definition.  */
7156
7157       switch (a->dw_attr)
7158         {
7159         case DW_AT_abstract_origin:
7160         case DW_AT_artificial:
7161         case DW_AT_containing_type:
7162         case DW_AT_external:
7163         case DW_AT_name:
7164         case DW_AT_type:
7165         case DW_AT_virtuality:
7166         case DW_AT_linkage_name:
7167         case DW_AT_MIPS_linkage_name:
7168           add_dwarf_attr (clone, a);
7169           break;
7170         case DW_AT_byte_size:
7171         default:
7172           break;
7173         }
7174     }
7175
7176   if (die->comdat_type_p)
7177     add_AT_die_ref (clone, DW_AT_signature, die);
7178
7179   add_AT_flag (clone, DW_AT_declaration, 1);
7180   return clone;
7181 }
7182
7183
7184 /* Structure to map a DIE in one CU to its copy in a comdat type unit.  */
7185
7186 struct decl_table_entry
7187 {
7188   dw_die_ref orig;
7189   dw_die_ref copy;
7190 };
7191
7192 /* Helpers to manipulate hash table of copied declarations.  */
7193
7194 /* Hashtable helpers.  */
7195
7196 struct decl_table_entry_hasher : typed_free_remove <decl_table_entry>
7197 {
7198   typedef decl_table_entry value_type;
7199   typedef die_struct compare_type;
7200   static inline hashval_t hash (const value_type *);
7201   static inline bool equal (const value_type *, const compare_type *);
7202 };
7203
7204 inline hashval_t
7205 decl_table_entry_hasher::hash (const value_type *entry)
7206 {
7207   return htab_hash_pointer (entry->orig);
7208 }
7209
7210 inline bool
7211 decl_table_entry_hasher::equal (const value_type *entry1,
7212                                 const compare_type *entry2)
7213 {
7214   return entry1->orig == entry2;
7215 }
7216
7217 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7218
7219 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7220    or type unit entry, to a new tree.  Adds the new tree to UNIT and returns
7221    a pointer to the copy of DIE.  If DECL_TABLE is provided, it is used
7222    to check if the ancestor has already been copied into UNIT.  */
7223
7224 static dw_die_ref
7225 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7226                     decl_hash_type *decl_table)
7227 {
7228   dw_die_ref parent = die->die_parent;
7229   dw_die_ref new_parent = unit;
7230   dw_die_ref copy;
7231   decl_table_entry **slot = NULL;
7232   struct decl_table_entry *entry = NULL;
7233
7234   if (decl_table)
7235     {
7236       /* Check if the entry has already been copied to UNIT.  */
7237       slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7238                                               INSERT);
7239       if (*slot != HTAB_EMPTY_ENTRY)
7240         {
7241           entry = *slot;
7242           return entry->copy;
7243         }
7244
7245       /* Record in DECL_TABLE that DIE has been copied to UNIT.  */
7246       entry = XCNEW (struct decl_table_entry);
7247       entry->orig = die;
7248       entry->copy = NULL;
7249       *slot = entry;
7250     }
7251
7252   if (parent != NULL)
7253     {
7254       dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7255       if (spec != NULL)
7256         parent = spec;
7257       if (!is_unit_die (parent))
7258         new_parent = copy_ancestor_tree (unit, parent, decl_table);
7259     }
7260
7261   copy = clone_as_declaration (die);
7262   add_child_die (new_parent, copy);
7263
7264   if (decl_table)
7265     {
7266       /* Record the pointer to the copy.  */
7267       entry->copy = copy;
7268     }
7269
7270   return copy;
7271 }
7272 /* Copy the declaration context to the new type unit DIE.  This includes
7273    any surrounding namespace or type declarations.  If the DIE has an
7274    AT_specification attribute, it also includes attributes and children
7275    attached to the specification, and returns a pointer to the original
7276    parent of the declaration DIE.  Returns NULL otherwise.  */
7277
7278 static dw_die_ref
7279 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7280 {
7281   dw_die_ref decl;
7282   dw_die_ref new_decl;
7283   dw_die_ref orig_parent = NULL;
7284
7285   decl = get_AT_ref (die, DW_AT_specification);
7286   if (decl == NULL)
7287     decl = die;
7288   else
7289     {
7290       unsigned ix;
7291       dw_die_ref c;
7292       dw_attr_ref a;
7293
7294       /* The original DIE will be changed to a declaration, and must
7295          be moved to be a child of the original declaration DIE.  */
7296       orig_parent = decl->die_parent;
7297
7298       /* Copy the type node pointer from the new DIE to the original
7299          declaration DIE so we can forward references later.  */
7300       decl->comdat_type_p = true;
7301       decl->die_id.die_type_node = die->die_id.die_type_node;
7302
7303       remove_AT (die, DW_AT_specification);
7304
7305       FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7306         {
7307           if (a->dw_attr != DW_AT_name
7308               && a->dw_attr != DW_AT_declaration
7309               && a->dw_attr != DW_AT_external)
7310             add_dwarf_attr (die, a);
7311         }
7312
7313       FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7314     }
7315
7316   if (decl->die_parent != NULL
7317       && !is_unit_die (decl->die_parent))
7318     {
7319       new_decl = copy_ancestor_tree (unit, decl, NULL);
7320       if (new_decl != NULL)
7321         {
7322           remove_AT (new_decl, DW_AT_signature);
7323           add_AT_specification (die, new_decl);
7324         }
7325     }
7326
7327   return orig_parent;
7328 }
7329
7330 /* Generate the skeleton ancestor tree for the given NODE, then clone
7331    the DIE and add the clone into the tree.  */
7332
7333 static void
7334 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7335 {
7336   if (node->new_die != NULL)
7337     return;
7338
7339   node->new_die = clone_as_declaration (node->old_die);
7340
7341   if (node->parent != NULL)
7342     {
7343       generate_skeleton_ancestor_tree (node->parent);
7344       add_child_die (node->parent->new_die, node->new_die);
7345     }
7346 }
7347
7348 /* Generate a skeleton tree of DIEs containing any declarations that are
7349    found in the original tree.  We traverse the tree looking for declaration
7350    DIEs, and construct the skeleton from the bottom up whenever we find one.  */
7351
7352 static void
7353 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7354 {
7355   skeleton_chain_node node;
7356   dw_die_ref c;
7357   dw_die_ref first;
7358   dw_die_ref prev = NULL;
7359   dw_die_ref next = NULL;
7360
7361   node.parent = parent;
7362
7363   first = c = parent->old_die->die_child;
7364   if (c)
7365     next = c->die_sib;
7366   if (c) do {
7367     if (prev == NULL || prev->die_sib == c)
7368       prev = c;
7369     c = next;
7370     next = (c == first ? NULL : c->die_sib);
7371     node.old_die = c;
7372     node.new_die = NULL;
7373     if (is_declaration_die (c))
7374       {
7375         if (is_template_instantiation (c))
7376           {
7377             /* Instantiated templates do not need to be cloned into the
7378                type unit.  Just move the DIE and its children back to
7379                the skeleton tree (in the main CU).  */
7380             remove_child_with_prev (c, prev);
7381             add_child_die (parent->new_die, c);
7382             c = prev;
7383           }
7384         else
7385           {
7386             /* Clone the existing DIE, move the original to the skeleton
7387                tree (which is in the main CU), and put the clone, with
7388                all the original's children, where the original came from
7389                (which is about to be moved to the type unit).  */
7390             dw_die_ref clone = clone_die (c);
7391             move_all_children (c, clone);
7392
7393             /* If the original has a DW_AT_object_pointer attribute,
7394                it would now point to a child DIE just moved to the
7395                cloned tree, so we need to remove that attribute from
7396                the original.  */
7397             remove_AT (c, DW_AT_object_pointer);
7398
7399             replace_child (c, clone, prev);
7400             generate_skeleton_ancestor_tree (parent);
7401             add_child_die (parent->new_die, c);
7402             node.new_die = c;
7403             c = clone;
7404           }
7405       }
7406     generate_skeleton_bottom_up (&node);
7407   } while (next != NULL);
7408 }
7409
7410 /* Wrapper function for generate_skeleton_bottom_up.  */
7411
7412 static dw_die_ref
7413 generate_skeleton (dw_die_ref die)
7414 {
7415   skeleton_chain_node node;
7416
7417   node.old_die = die;
7418   node.new_die = NULL;
7419   node.parent = NULL;
7420
7421   /* If this type definition is nested inside another type,
7422      and is not an instantiation of a template, always leave
7423      at least a declaration in its place.  */
7424   if (die->die_parent != NULL
7425       && is_type_die (die->die_parent)
7426       && !is_template_instantiation (die))
7427     node.new_die = clone_as_declaration (die);
7428
7429   generate_skeleton_bottom_up (&node);
7430   return node.new_die;
7431 }
7432
7433 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7434    declaration.  The original DIE is moved to a new compile unit so that
7435    existing references to it follow it to the new location.  If any of the
7436    original DIE's descendants is a declaration, we need to replace the
7437    original DIE with a skeleton tree and move the declarations back into the
7438    skeleton tree.  */
7439
7440 static dw_die_ref
7441 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7442                                        dw_die_ref prev)
7443 {
7444   dw_die_ref skeleton, orig_parent;
7445
7446   /* Copy the declaration context to the type unit DIE.  If the returned
7447      ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7448      that DIE.  */
7449   orig_parent = copy_declaration_context (unit, child);
7450
7451   skeleton = generate_skeleton (child);
7452   if (skeleton == NULL)
7453     remove_child_with_prev (child, prev);
7454   else
7455     {
7456       skeleton->comdat_type_p = true;
7457       skeleton->die_id.die_type_node = child->die_id.die_type_node;
7458
7459       /* If the original DIE was a specification, we need to put
7460          the skeleton under the parent DIE of the declaration.
7461          This leaves the original declaration in the tree, but
7462          it will be pruned later since there are no longer any
7463          references to it.  */
7464       if (orig_parent != NULL)
7465         {
7466           remove_child_with_prev (child, prev);
7467           add_child_die (orig_parent, skeleton);
7468         }
7469       else
7470         replace_child (child, skeleton, prev);
7471     }
7472
7473   return skeleton;
7474 }
7475
7476 /* Traverse the DIE and set up additional .debug_types sections for each
7477    type worthy of being placed in a COMDAT section.  */
7478
7479 static void
7480 break_out_comdat_types (dw_die_ref die)
7481 {
7482   dw_die_ref c;
7483   dw_die_ref first;
7484   dw_die_ref prev = NULL;
7485   dw_die_ref next = NULL;
7486   dw_die_ref unit = NULL;
7487
7488   first = c = die->die_child;
7489   if (c)
7490     next = c->die_sib;
7491   if (c) do {
7492     if (prev == NULL || prev->die_sib == c)
7493       prev = c;
7494     c = next;
7495     next = (c == first ? NULL : c->die_sib);
7496     if (should_move_die_to_comdat (c))
7497       {
7498         dw_die_ref replacement;
7499         comdat_type_node_ref type_node;
7500
7501         /* Break out nested types into their own type units.  */
7502         break_out_comdat_types (c);
7503
7504         /* Create a new type unit DIE as the root for the new tree, and
7505            add it to the list of comdat types.  */
7506         unit = new_die (DW_TAG_type_unit, NULL, NULL);
7507         add_AT_unsigned (unit, DW_AT_language,
7508                          get_AT_unsigned (comp_unit_die (), DW_AT_language));
7509         type_node = ggc_cleared_alloc<comdat_type_node> ();
7510         type_node->root_die = unit;
7511         type_node->next = comdat_type_list;
7512         comdat_type_list = type_node;
7513
7514         /* Generate the type signature.  */
7515         generate_type_signature (c, type_node);
7516
7517         /* Copy the declaration context, attributes, and children of the
7518            declaration into the new type unit DIE, then remove this DIE
7519            from the main CU (or replace it with a skeleton if necessary).  */
7520         replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7521         type_node->skeleton_die = replacement;
7522
7523         /* Add the DIE to the new compunit.  */
7524         add_child_die (unit, c);
7525
7526         if (replacement != NULL)
7527           c = replacement;
7528       }
7529     else if (c->die_tag == DW_TAG_namespace
7530              || c->die_tag == DW_TAG_class_type
7531              || c->die_tag == DW_TAG_structure_type
7532              || c->die_tag == DW_TAG_union_type)
7533       {
7534         /* Look for nested types that can be broken out.  */
7535         break_out_comdat_types (c);
7536       }
7537   } while (next != NULL);
7538 }
7539
7540 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
7541    Enter all the cloned children into the hash table decl_table.  */
7542
7543 static dw_die_ref
7544 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
7545 {
7546   dw_die_ref c;
7547   dw_die_ref clone;
7548   struct decl_table_entry *entry;
7549   decl_table_entry **slot;
7550
7551   if (die->die_tag == DW_TAG_subprogram)
7552     clone = clone_as_declaration (die);
7553   else
7554     clone = clone_die (die);
7555
7556   slot = decl_table->find_slot_with_hash (die,
7557                                           htab_hash_pointer (die), INSERT);
7558
7559   /* Assert that DIE isn't in the hash table yet.  If it would be there
7560      before, the ancestors would be necessarily there as well, therefore
7561      clone_tree_partial wouldn't be called.  */
7562   gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7563
7564   entry = XCNEW (struct decl_table_entry);
7565   entry->orig = die;
7566   entry->copy = clone;
7567   *slot = entry;
7568
7569   if (die->die_tag != DW_TAG_subprogram)
7570     FOR_EACH_CHILD (die, c,
7571                     add_child_die (clone, clone_tree_partial (c, decl_table)));
7572
7573   return clone;
7574 }
7575
7576 /* Walk the DIE and its children, looking for references to incomplete
7577    or trivial types that are unmarked (i.e., that are not in the current
7578    type_unit).  */
7579
7580 static void
7581 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
7582 {
7583   dw_die_ref c;
7584   dw_attr_ref a;
7585   unsigned ix;
7586
7587   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7588     {
7589       if (AT_class (a) == dw_val_class_die_ref)
7590         {
7591           dw_die_ref targ = AT_ref (a);
7592           decl_table_entry **slot;
7593           struct decl_table_entry *entry;
7594
7595           if (targ->die_mark != 0 || targ->comdat_type_p)
7596             continue;
7597
7598           slot = decl_table->find_slot_with_hash (targ,
7599                                                   htab_hash_pointer (targ),
7600                                                   INSERT);
7601
7602           if (*slot != HTAB_EMPTY_ENTRY)
7603             {
7604               /* TARG has already been copied, so we just need to
7605                  modify the reference to point to the copy.  */
7606               entry = *slot;
7607               a->dw_attr_val.v.val_die_ref.die = entry->copy;
7608             }
7609           else
7610             {
7611               dw_die_ref parent = unit;
7612               dw_die_ref copy = clone_die (targ);
7613
7614               /* Record in DECL_TABLE that TARG has been copied.
7615                  Need to do this now, before the recursive call,
7616                  because DECL_TABLE may be expanded and SLOT
7617                  would no longer be a valid pointer.  */
7618               entry = XCNEW (struct decl_table_entry);
7619               entry->orig = targ;
7620               entry->copy = copy;
7621               *slot = entry;
7622
7623               /* If TARG is not a declaration DIE, we need to copy its
7624                  children.  */
7625               if (!is_declaration_die (targ))
7626                 {
7627                   FOR_EACH_CHILD (
7628                       targ, c,
7629                       add_child_die (copy,
7630                                      clone_tree_partial (c, decl_table)));
7631                 }
7632
7633               /* Make sure the cloned tree is marked as part of the
7634                  type unit.  */
7635               mark_dies (copy);
7636
7637               /* If TARG has surrounding context, copy its ancestor tree
7638                  into the new type unit.  */
7639               if (targ->die_parent != NULL
7640                   && !is_unit_die (targ->die_parent))
7641                 parent = copy_ancestor_tree (unit, targ->die_parent,
7642                                              decl_table);
7643
7644               add_child_die (parent, copy);
7645               a->dw_attr_val.v.val_die_ref.die = copy;
7646
7647               /* Make sure the newly-copied DIE is walked.  If it was
7648                  installed in a previously-added context, it won't
7649                  get visited otherwise.  */
7650               if (parent != unit)
7651                 {
7652                   /* Find the highest point of the newly-added tree,
7653                      mark each node along the way, and walk from there.  */
7654                   parent->die_mark = 1;
7655                   while (parent->die_parent
7656                          && parent->die_parent->die_mark == 0)
7657                     {
7658                       parent = parent->die_parent;
7659                       parent->die_mark = 1;
7660                     }
7661                   copy_decls_walk (unit, parent, decl_table);
7662                 }
7663             }
7664         }
7665     }
7666
7667   FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7668 }
7669
7670 /* Copy declarations for "unworthy" types into the new comdat section.
7671    Incomplete types, modified types, and certain other types aren't broken
7672    out into comdat sections of their own, so they don't have a signature,
7673    and we need to copy the declaration into the same section so that we
7674    don't have an external reference.  */
7675
7676 static void
7677 copy_decls_for_unworthy_types (dw_die_ref unit)
7678 {
7679   mark_dies (unit);
7680   decl_hash_type decl_table (10);
7681   copy_decls_walk (unit, unit, &decl_table);
7682   unmark_dies (unit);
7683 }
7684
7685 /* Traverse the DIE and add a sibling attribute if it may have the
7686    effect of speeding up access to siblings.  To save some space,
7687    avoid generating sibling attributes for DIE's without children.  */
7688
7689 static void
7690 add_sibling_attributes (dw_die_ref die)
7691 {
7692   dw_die_ref c;
7693
7694   if (! die->die_child)
7695     return;
7696
7697   if (die->die_parent && die != die->die_parent->die_child)
7698     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7699
7700   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7701 }
7702
7703 /* Output all location lists for the DIE and its children.  */
7704
7705 static void
7706 output_location_lists (dw_die_ref die)
7707 {
7708   dw_die_ref c;
7709   dw_attr_ref a;
7710   unsigned ix;
7711
7712   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7713     if (AT_class (a) == dw_val_class_loc_list)
7714       output_loc_list (AT_loc_list (a));
7715
7716   FOR_EACH_CHILD (die, c, output_location_lists (c));
7717 }
7718
7719 /* We want to limit the number of external references, because they are
7720    larger than local references: a relocation takes multiple words, and
7721    even a sig8 reference is always eight bytes, whereas a local reference
7722    can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7723    So if we encounter multiple external references to the same type DIE, we
7724    make a local typedef stub for it and redirect all references there.
7725
7726    This is the element of the hash table for keeping track of these
7727    references.  */
7728
7729 struct external_ref
7730 {
7731   dw_die_ref type;
7732   dw_die_ref stub;
7733   unsigned n_refs;
7734 };
7735
7736 /* Hashtable helpers.  */
7737
7738 struct external_ref_hasher : typed_free_remove <external_ref>
7739 {
7740   typedef external_ref value_type;
7741   typedef external_ref compare_type;
7742   static inline hashval_t hash (const value_type *);
7743   static inline bool equal (const value_type *, const compare_type *);
7744 };
7745
7746 inline hashval_t
7747 external_ref_hasher::hash (const value_type *r)
7748 {
7749   dw_die_ref die = r->type;
7750   hashval_t h = 0;
7751
7752   /* We can't use the address of the DIE for hashing, because
7753      that will make the order of the stub DIEs non-deterministic.  */
7754   if (! die->comdat_type_p)
7755     /* We have a symbol; use it to compute a hash.  */
7756     h = htab_hash_string (die->die_id.die_symbol);
7757   else
7758     {
7759       /* We have a type signature; use a subset of the bits as the hash.
7760          The 8-byte signature is at least as large as hashval_t.  */
7761       comdat_type_node_ref type_node = die->die_id.die_type_node;
7762       memcpy (&h, type_node->signature, sizeof (h));
7763     }
7764   return h;
7765 }
7766
7767 inline bool
7768 external_ref_hasher::equal (const value_type *r1, const compare_type *r2)
7769 {
7770   return r1->type == r2->type;
7771 }
7772
7773 typedef hash_table<external_ref_hasher> external_ref_hash_type;
7774
7775 /* Return a pointer to the external_ref for references to DIE.  */
7776
7777 static struct external_ref *
7778 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
7779 {
7780   struct external_ref ref, *ref_p;
7781   external_ref **slot;
7782
7783   ref.type = die;
7784   slot = map->find_slot (&ref, INSERT);
7785   if (*slot != HTAB_EMPTY_ENTRY)
7786     return *slot;
7787
7788   ref_p = XCNEW (struct external_ref);
7789   ref_p->type = die;
7790   *slot = ref_p;
7791   return ref_p;
7792 }
7793
7794 /* Subroutine of optimize_external_refs, below.
7795
7796    If we see a type skeleton, record it as our stub.  If we see external
7797    references, remember how many we've seen.  */
7798
7799 static void
7800 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
7801 {
7802   dw_die_ref c;
7803   dw_attr_ref a;
7804   unsigned ix;
7805   struct external_ref *ref_p;
7806
7807   if (is_type_die (die)
7808       && (c = get_AT_ref (die, DW_AT_signature)))
7809     {
7810       /* This is a local skeleton; use it for local references.  */
7811       ref_p = lookup_external_ref (map, c);
7812       ref_p->stub = die;
7813     }
7814
7815   /* Scan the DIE references, and remember any that refer to DIEs from
7816      other CUs (i.e. those which are not marked).  */
7817   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7818     if (AT_class (a) == dw_val_class_die_ref
7819         && (c = AT_ref (a))->die_mark == 0
7820         && is_type_die (c))
7821       {
7822         ref_p = lookup_external_ref (map, c);
7823         ref_p->n_refs++;
7824       }
7825
7826   FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7827 }
7828
7829 /* htab_traverse callback function for optimize_external_refs, below.  SLOT
7830    points to an external_ref, DATA is the CU we're processing.  If we don't
7831    already have a local stub, and we have multiple refs, build a stub.  */
7832
7833 int
7834 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
7835 {
7836   struct external_ref *ref_p = *slot;
7837
7838   if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7839     {
7840       /* We have multiple references to this type, so build a small stub.
7841          Both of these forms are a bit dodgy from the perspective of the
7842          DWARF standard, since technically they should have names.  */
7843       dw_die_ref cu = data;
7844       dw_die_ref type = ref_p->type;
7845       dw_die_ref stub = NULL;
7846
7847       if (type->comdat_type_p)
7848         {
7849           /* If we refer to this type via sig8, use AT_signature.  */
7850           stub = new_die (type->die_tag, cu, NULL_TREE);
7851           add_AT_die_ref (stub, DW_AT_signature, type);
7852         }
7853       else
7854         {
7855           /* Otherwise, use a typedef with no name.  */
7856           stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7857           add_AT_die_ref (stub, DW_AT_type, type);
7858         }
7859
7860       stub->die_mark++;
7861       ref_p->stub = stub;
7862     }
7863   return 1;
7864 }
7865
7866 /* DIE is a unit; look through all the DIE references to see if there are
7867    any external references to types, and if so, create local stubs for
7868    them which will be applied in build_abbrev_table.  This is useful because
7869    references to local DIEs are smaller.  */
7870
7871 static external_ref_hash_type *
7872 optimize_external_refs (dw_die_ref die)
7873 {
7874   external_ref_hash_type *map = new external_ref_hash_type (10);
7875   optimize_external_refs_1 (die, map);
7876   map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
7877   return map;
7878 }
7879
7880 /* The format of each DIE (and its attribute value pairs) is encoded in an
7881    abbreviation table.  This routine builds the abbreviation table and assigns
7882    a unique abbreviation id for each abbreviation entry.  The children of each
7883    die are visited recursively.  */
7884
7885 static void
7886 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
7887 {
7888   unsigned long abbrev_id;
7889   unsigned int n_alloc;
7890   dw_die_ref c;
7891   dw_attr_ref a;
7892   unsigned ix;
7893
7894   /* Scan the DIE references, and replace any that refer to
7895      DIEs from other CUs (i.e. those which are not marked) with
7896      the local stubs we built in optimize_external_refs.  */
7897   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7898     if (AT_class (a) == dw_val_class_die_ref
7899         && (c = AT_ref (a))->die_mark == 0)
7900       {
7901         struct external_ref *ref_p;
7902         gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
7903
7904         ref_p = lookup_external_ref (extern_map, c);
7905         if (ref_p->stub && ref_p->stub != die)
7906           change_AT_die_ref (a, ref_p->stub);
7907         else
7908           /* We aren't changing this reference, so mark it external.  */
7909           set_AT_ref_external (a, 1);
7910       }
7911
7912   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7913     {
7914       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7915       dw_attr_ref die_a, abbrev_a;
7916       unsigned ix;
7917       bool ok = true;
7918
7919       if (abbrev->die_tag != die->die_tag)
7920         continue;
7921       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7922         continue;
7923
7924       if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
7925         continue;
7926
7927       FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
7928         {
7929           abbrev_a = &(*abbrev->die_attr)[ix];
7930           if ((abbrev_a->dw_attr != die_a->dw_attr)
7931               || (value_format (abbrev_a) != value_format (die_a)))
7932             {
7933               ok = false;
7934               break;
7935             }
7936         }
7937       if (ok)
7938         break;
7939     }
7940
7941   if (abbrev_id >= abbrev_die_table_in_use)
7942     {
7943       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7944         {
7945           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7946           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7947                                             n_alloc);
7948
7949           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7950                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7951           abbrev_die_table_allocated = n_alloc;
7952         }
7953
7954       ++abbrev_die_table_in_use;
7955       abbrev_die_table[abbrev_id] = die;
7956     }
7957
7958   die->die_abbrev = abbrev_id;
7959   FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
7960 }
7961 \f
7962 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
7963
7964 static int
7965 constant_size (unsigned HOST_WIDE_INT value)
7966 {
7967   int log;
7968
7969   if (value == 0)
7970     log = 0;
7971   else
7972     log = floor_log2 (value);
7973
7974   log = log / 8;
7975   log = 1 << (floor_log2 (log) + 1);
7976
7977   return log;
7978 }
7979
7980 /* Return the size of a DIE as it is represented in the
7981    .debug_info section.  */
7982
7983 static unsigned long
7984 size_of_die (dw_die_ref die)
7985 {
7986   unsigned long size = 0;
7987   dw_attr_ref a;
7988   unsigned ix;
7989   enum dwarf_form form;
7990
7991   size += size_of_uleb128 (die->die_abbrev);
7992   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7993     {
7994       switch (AT_class (a))
7995         {
7996         case dw_val_class_addr:
7997           if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7998             {
7999               gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8000               size += size_of_uleb128 (AT_index (a));
8001             }
8002           else
8003             size += DWARF2_ADDR_SIZE;
8004           break;
8005         case dw_val_class_offset:
8006           size += DWARF_OFFSET_SIZE;
8007           break;
8008         case dw_val_class_loc:
8009           {
8010             unsigned long lsize = size_of_locs (AT_loc (a));
8011
8012             /* Block length.  */
8013             if (dwarf_version >= 4)
8014               size += size_of_uleb128 (lsize);
8015             else
8016               size += constant_size (lsize);
8017             size += lsize;
8018           }
8019           break;
8020         case dw_val_class_loc_list:
8021           if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8022             {
8023               gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8024               size += size_of_uleb128 (AT_index (a));
8025             }
8026           else
8027             size += DWARF_OFFSET_SIZE;
8028           break;
8029         case dw_val_class_range_list:
8030           size += DWARF_OFFSET_SIZE;
8031           break;
8032         case dw_val_class_const:
8033           size += size_of_sleb128 (AT_int (a));
8034           break;
8035         case dw_val_class_unsigned_const:
8036           {
8037             int csize = constant_size (AT_unsigned (a));
8038             if (dwarf_version == 3
8039                 && a->dw_attr == DW_AT_data_member_location
8040                 && csize >= 4)
8041               size += size_of_uleb128 (AT_unsigned (a));
8042             else
8043               size += csize;
8044           }
8045           break;
8046         case dw_val_class_const_double:
8047           size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
8048           if (HOST_BITS_PER_WIDE_INT >= 64)
8049             size++; /* block */
8050           break;
8051         case dw_val_class_wide_int:
8052           size += (get_full_len (*a->dw_attr_val.v.val_wide)
8053                    * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
8054           if (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT
8055               > 64)
8056             size++; /* block */
8057           break;
8058         case dw_val_class_vec:
8059           size += constant_size (a->dw_attr_val.v.val_vec.length
8060                                  * a->dw_attr_val.v.val_vec.elt_size)
8061                   + a->dw_attr_val.v.val_vec.length
8062                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
8063           break;
8064         case dw_val_class_flag:
8065           if (dwarf_version >= 4)
8066             /* Currently all add_AT_flag calls pass in 1 as last argument,
8067                so DW_FORM_flag_present can be used.  If that ever changes,
8068                we'll need to use DW_FORM_flag and have some optimization
8069                in build_abbrev_table that will change those to
8070                DW_FORM_flag_present if it is set to 1 in all DIEs using
8071                the same abbrev entry.  */
8072             gcc_assert (a->dw_attr_val.v.val_flag == 1);
8073           else
8074             size += 1;
8075           break;
8076         case dw_val_class_die_ref:
8077           if (AT_ref_external (a))
8078             {
8079               /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
8080                  we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
8081                  is sized by target address length, whereas in DWARF3
8082                  it's always sized as an offset.  */
8083               if (use_debug_types)
8084                 size += DWARF_TYPE_SIGNATURE_SIZE;
8085               else if (dwarf_version == 2)
8086                 size += DWARF2_ADDR_SIZE;
8087               else
8088                 size += DWARF_OFFSET_SIZE;
8089             }
8090           else
8091             size += DWARF_OFFSET_SIZE;
8092           break;
8093         case dw_val_class_fde_ref:
8094           size += DWARF_OFFSET_SIZE;
8095           break;
8096         case dw_val_class_lbl_id:
8097           if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8098             {
8099               gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8100               size += size_of_uleb128 (AT_index (a));
8101             }
8102           else
8103             size += DWARF2_ADDR_SIZE;
8104           break;
8105         case dw_val_class_lineptr:
8106         case dw_val_class_macptr:
8107           size += DWARF_OFFSET_SIZE;
8108           break;
8109         case dw_val_class_str:
8110           form = AT_string_form (a);
8111           if (form == DW_FORM_strp)
8112             size += DWARF_OFFSET_SIZE;
8113          else if (form == DW_FORM_GNU_str_index)
8114             size += size_of_uleb128 (AT_index (a));
8115           else
8116             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8117           break;
8118         case dw_val_class_file:
8119           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8120           break;
8121         case dw_val_class_data8:
8122           size += 8;
8123           break;
8124         case dw_val_class_vms_delta:
8125           size += DWARF_OFFSET_SIZE;
8126           break;
8127         case dw_val_class_high_pc:
8128           size += DWARF2_ADDR_SIZE;
8129           break;
8130         default:
8131           gcc_unreachable ();
8132         }
8133     }
8134
8135   return size;
8136 }
8137
8138 /* Size the debugging information associated with a given DIE.  Visits the
8139    DIE's children recursively.  Updates the global variable next_die_offset, on
8140    each time through.  Uses the current value of next_die_offset to update the
8141    die_offset field in each DIE.  */
8142
8143 static void
8144 calc_die_sizes (dw_die_ref die)
8145 {
8146   dw_die_ref c;
8147
8148   gcc_assert (die->die_offset == 0
8149               || (unsigned long int) die->die_offset == next_die_offset);
8150   die->die_offset = next_die_offset;
8151   next_die_offset += size_of_die (die);
8152
8153   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8154
8155   if (die->die_child != NULL)
8156     /* Count the null byte used to terminate sibling lists.  */
8157     next_die_offset += 1;
8158 }
8159
8160 /* Size just the base type children at the start of the CU.
8161    This is needed because build_abbrev needs to size locs
8162    and sizing of type based stack ops needs to know die_offset
8163    values for the base types.  */
8164
8165 static void
8166 calc_base_type_die_sizes (void)
8167 {
8168   unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8169   unsigned int i;
8170   dw_die_ref base_type;
8171 #if ENABLE_ASSERT_CHECKING
8172   dw_die_ref prev = comp_unit_die ()->die_child;
8173 #endif
8174
8175   die_offset += size_of_die (comp_unit_die ());
8176   for (i = 0; base_types.iterate (i, &base_type); i++)
8177     {
8178 #if ENABLE_ASSERT_CHECKING
8179       gcc_assert (base_type->die_offset == 0
8180                   && prev->die_sib == base_type
8181                   && base_type->die_child == NULL
8182                   && base_type->die_abbrev);
8183       prev = base_type;
8184 #endif
8185       base_type->die_offset = die_offset;
8186       die_offset += size_of_die (base_type);
8187     }
8188 }
8189
8190 /* Set the marks for a die and its children.  We do this so
8191    that we know whether or not a reference needs to use FORM_ref_addr; only
8192    DIEs in the same CU will be marked.  We used to clear out the offset
8193    and use that as the flag, but ran into ordering problems.  */
8194
8195 static void
8196 mark_dies (dw_die_ref die)
8197 {
8198   dw_die_ref c;
8199
8200   gcc_assert (!die->die_mark);
8201
8202   die->die_mark = 1;
8203   FOR_EACH_CHILD (die, c, mark_dies (c));
8204 }
8205
8206 /* Clear the marks for a die and its children.  */
8207
8208 static void
8209 unmark_dies (dw_die_ref die)
8210 {
8211   dw_die_ref c;
8212
8213   if (! use_debug_types)
8214     gcc_assert (die->die_mark);
8215
8216   die->die_mark = 0;
8217   FOR_EACH_CHILD (die, c, unmark_dies (c));
8218 }
8219
8220 /* Clear the marks for a die, its children and referred dies.  */
8221
8222 static void
8223 unmark_all_dies (dw_die_ref die)
8224 {
8225   dw_die_ref c;
8226   dw_attr_ref a;
8227   unsigned ix;
8228
8229   if (!die->die_mark)
8230     return;
8231   die->die_mark = 0;
8232
8233   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8234
8235   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8236     if (AT_class (a) == dw_val_class_die_ref)
8237       unmark_all_dies (AT_ref (a));
8238 }
8239
8240 /* Calculate if the entry should appear in the final output file.  It may be
8241    from a pruned a type.  */
8242
8243 static bool
8244 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
8245 {
8246   /* By limiting gnu pubnames to definitions only, gold can generate a
8247      gdb index without entries for declarations, which don't include
8248      enough information to be useful.  */
8249   if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
8250     return false;
8251
8252   if (table == pubname_table)
8253     {
8254       /* Enumerator names are part of the pubname table, but the
8255          parent DW_TAG_enumeration_type die may have been pruned.
8256          Don't output them if that is the case.  */
8257       if (p->die->die_tag == DW_TAG_enumerator &&
8258           (p->die->die_parent == NULL
8259            || !p->die->die_parent->die_perennial_p))
8260         return false;
8261
8262       /* Everything else in the pubname table is included.  */
8263       return true;
8264     }
8265
8266   /* The pubtypes table shouldn't include types that have been
8267      pruned.  */
8268   return (p->die->die_offset != 0
8269           || !flag_eliminate_unused_debug_types);
8270 }
8271
8272 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8273    generated for the compilation unit.  */
8274
8275 static unsigned long
8276 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8277 {
8278   unsigned long size;
8279   unsigned i;
8280   pubname_ref p;
8281   int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8282
8283   size = DWARF_PUBNAMES_HEADER_SIZE;
8284   FOR_EACH_VEC_ELT (*names, i, p)
8285     if (include_pubname_in_output (names, p))
8286       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8287
8288   size += DWARF_OFFSET_SIZE;
8289   return size;
8290 }
8291
8292 /* Return the size of the information in the .debug_aranges section.  */
8293
8294 static unsigned long
8295 size_of_aranges (void)
8296 {
8297   unsigned long size;
8298
8299   size = DWARF_ARANGES_HEADER_SIZE;
8300
8301   /* Count the address/length pair for this compilation unit.  */
8302   if (text_section_used)
8303     size += 2 * DWARF2_ADDR_SIZE;
8304   if (cold_text_section_used)
8305     size += 2 * DWARF2_ADDR_SIZE;
8306   if (have_multiple_function_sections)
8307     {
8308       unsigned fde_idx;
8309       dw_fde_ref fde;
8310
8311       FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8312         {
8313           if (DECL_IGNORED_P (fde->decl))
8314             continue;
8315           if (!fde->in_std_section)
8316             size += 2 * DWARF2_ADDR_SIZE;
8317           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8318             size += 2 * DWARF2_ADDR_SIZE;
8319         }
8320     }
8321
8322   /* Count the two zero words used to terminated the address range table.  */
8323   size += 2 * DWARF2_ADDR_SIZE;
8324   return size;
8325 }
8326 \f
8327 /* Select the encoding of an attribute value.  */
8328
8329 static enum dwarf_form
8330 value_format (dw_attr_ref a)
8331 {
8332   switch (AT_class (a))
8333     {
8334     case dw_val_class_addr:
8335       /* Only very few attributes allow DW_FORM_addr.  */
8336       switch (a->dw_attr)
8337         {
8338         case DW_AT_low_pc:
8339         case DW_AT_high_pc:
8340         case DW_AT_entry_pc:
8341         case DW_AT_trampoline:
8342           return (AT_index (a) == NOT_INDEXED
8343                   ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8344         default:
8345           break;
8346         }
8347       switch (DWARF2_ADDR_SIZE)
8348         {
8349         case 1:
8350           return DW_FORM_data1;
8351         case 2:
8352           return DW_FORM_data2;
8353         case 4:
8354           return DW_FORM_data4;
8355         case 8:
8356           return DW_FORM_data8;
8357         default:
8358           gcc_unreachable ();
8359         }
8360     case dw_val_class_range_list:
8361     case dw_val_class_loc_list:
8362       if (dwarf_version >= 4)
8363         return DW_FORM_sec_offset;
8364       /* FALLTHRU */
8365     case dw_val_class_vms_delta:
8366     case dw_val_class_offset:
8367       switch (DWARF_OFFSET_SIZE)
8368         {
8369         case 4:
8370           return DW_FORM_data4;
8371         case 8:
8372           return DW_FORM_data8;
8373         default:
8374           gcc_unreachable ();
8375         }
8376     case dw_val_class_loc:
8377       if (dwarf_version >= 4)
8378         return DW_FORM_exprloc;
8379       switch (constant_size (size_of_locs (AT_loc (a))))
8380         {
8381         case 1:
8382           return DW_FORM_block1;
8383         case 2:
8384           return DW_FORM_block2;
8385         case 4:
8386           return DW_FORM_block4;
8387         default:
8388           gcc_unreachable ();
8389         }
8390     case dw_val_class_const:
8391       return DW_FORM_sdata;
8392     case dw_val_class_unsigned_const:
8393       switch (constant_size (AT_unsigned (a)))
8394         {
8395         case 1:
8396           return DW_FORM_data1;
8397         case 2:
8398           return DW_FORM_data2;
8399         case 4:
8400           /* In DWARF3 DW_AT_data_member_location with
8401              DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8402              constant, so we need to use DW_FORM_udata if we need
8403              a large constant.  */
8404           if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8405             return DW_FORM_udata;
8406           return DW_FORM_data4;
8407         case 8:
8408           if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8409             return DW_FORM_udata;
8410           return DW_FORM_data8;
8411         default:
8412           gcc_unreachable ();
8413         }
8414     case dw_val_class_const_double:
8415       switch (HOST_BITS_PER_WIDE_INT)
8416         {
8417         case 8:
8418           return DW_FORM_data2;
8419         case 16:
8420           return DW_FORM_data4;
8421         case 32:
8422           return DW_FORM_data8;
8423         case 64:
8424         default:
8425           return DW_FORM_block1;
8426         }
8427     case dw_val_class_wide_int:
8428       switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
8429         {
8430         case 8:
8431           return DW_FORM_data1;
8432         case 16:
8433           return DW_FORM_data2;
8434         case 32:
8435           return DW_FORM_data4;
8436         case 64:
8437           return DW_FORM_data8;
8438         default:
8439           return DW_FORM_block1;
8440         }
8441     case dw_val_class_vec:
8442       switch (constant_size (a->dw_attr_val.v.val_vec.length
8443                              * a->dw_attr_val.v.val_vec.elt_size))
8444         {
8445         case 1:
8446           return DW_FORM_block1;
8447         case 2:
8448           return DW_FORM_block2;
8449         case 4:
8450           return DW_FORM_block4;
8451         default:
8452           gcc_unreachable ();
8453         }
8454     case dw_val_class_flag:
8455       if (dwarf_version >= 4)
8456         {
8457           /* Currently all add_AT_flag calls pass in 1 as last argument,
8458              so DW_FORM_flag_present can be used.  If that ever changes,
8459              we'll need to use DW_FORM_flag and have some optimization
8460              in build_abbrev_table that will change those to
8461              DW_FORM_flag_present if it is set to 1 in all DIEs using
8462              the same abbrev entry.  */
8463           gcc_assert (a->dw_attr_val.v.val_flag == 1);
8464           return DW_FORM_flag_present;
8465         }
8466       return DW_FORM_flag;
8467     case dw_val_class_die_ref:
8468       if (AT_ref_external (a))
8469         return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8470       else
8471         return DW_FORM_ref;
8472     case dw_val_class_fde_ref:
8473       return DW_FORM_data;
8474     case dw_val_class_lbl_id:
8475       return (AT_index (a) == NOT_INDEXED
8476               ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8477     case dw_val_class_lineptr:
8478     case dw_val_class_macptr:
8479       return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8480     case dw_val_class_str:
8481       return AT_string_form (a);
8482     case dw_val_class_file:
8483       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8484         {
8485         case 1:
8486           return DW_FORM_data1;
8487         case 2:
8488           return DW_FORM_data2;
8489         case 4:
8490           return DW_FORM_data4;
8491         default:
8492           gcc_unreachable ();
8493         }
8494
8495     case dw_val_class_data8:
8496       return DW_FORM_data8;
8497
8498     case dw_val_class_high_pc:
8499       switch (DWARF2_ADDR_SIZE)
8500         {
8501         case 1:
8502           return DW_FORM_data1;
8503         case 2:
8504           return DW_FORM_data2;
8505         case 4:
8506           return DW_FORM_data4;
8507         case 8:
8508           return DW_FORM_data8;
8509         default:
8510           gcc_unreachable ();
8511         }
8512
8513     default:
8514       gcc_unreachable ();
8515     }
8516 }
8517
8518 /* Output the encoding of an attribute value.  */
8519
8520 static void
8521 output_value_format (dw_attr_ref a)
8522 {
8523   enum dwarf_form form = value_format (a);
8524
8525   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8526 }
8527
8528 /* Given a die and id, produce the appropriate abbreviations.  */
8529
8530 static void
8531 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8532 {
8533   unsigned ix;
8534   dw_attr_ref a_attr;
8535
8536   dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8537   dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8538                                dwarf_tag_name (abbrev->die_tag));
8539
8540   if (abbrev->die_child != NULL)
8541     dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8542   else
8543     dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8544
8545   for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8546     {
8547       dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8548                                    dwarf_attr_name (a_attr->dw_attr));
8549       output_value_format (a_attr);
8550     }
8551
8552   dw2_asm_output_data (1, 0, NULL);
8553   dw2_asm_output_data (1, 0, NULL);
8554 }
8555
8556
8557 /* Output the .debug_abbrev section which defines the DIE abbreviation
8558    table.  */
8559
8560 static void
8561 output_abbrev_section (void)
8562 {
8563   unsigned long abbrev_id;
8564
8565   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8566     output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8567
8568   /* Terminate the table.  */
8569   dw2_asm_output_data (1, 0, NULL);
8570 }
8571
8572 /* Output a symbol we can use to refer to this DIE from another CU.  */
8573
8574 static inline void
8575 output_die_symbol (dw_die_ref die)
8576 {
8577   const char *sym = die->die_id.die_symbol;
8578
8579   gcc_assert (!die->comdat_type_p);
8580
8581   if (sym == 0)
8582     return;
8583
8584   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8585     /* We make these global, not weak; if the target doesn't support
8586        .linkonce, it doesn't support combining the sections, so debugging
8587        will break.  */
8588     targetm.asm_out.globalize_label (asm_out_file, sym);
8589
8590   ASM_OUTPUT_LABEL (asm_out_file, sym);
8591 }
8592
8593 /* Return a new location list, given the begin and end range, and the
8594    expression.  */
8595
8596 static inline dw_loc_list_ref
8597 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8598               const char *section)
8599 {
8600   dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
8601
8602   retlist->begin = begin;
8603   retlist->begin_entry = NULL;
8604   retlist->end = end;
8605   retlist->expr = expr;
8606   retlist->section = section;
8607
8608   return retlist;
8609 }
8610
8611 /* Generate a new internal symbol for this location list node, if it
8612    hasn't got one yet.  */
8613
8614 static inline void
8615 gen_llsym (dw_loc_list_ref list)
8616 {
8617   gcc_assert (!list->ll_symbol);
8618   list->ll_symbol = gen_internal_sym ("LLST");
8619 }
8620
8621 /* Output the location list given to us.  */
8622
8623 static void
8624 output_loc_list (dw_loc_list_ref list_head)
8625 {
8626   dw_loc_list_ref curr = list_head;
8627
8628   if (list_head->emitted)
8629     return;
8630   list_head->emitted = true;
8631
8632   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8633
8634   /* Walk the location list, and output each range + expression.  */
8635   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8636     {
8637       unsigned long size;
8638       /* Don't output an entry that starts and ends at the same address.  */
8639       if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8640         continue;
8641       size = size_of_locs (curr->expr);
8642       /* If the expression is too large, drop it on the floor.  We could
8643          perhaps put it into DW_TAG_dwarf_procedure and refer to that
8644          in the expression, but >= 64KB expressions for a single value
8645          in a single range are unlikely very useful.  */
8646       if (size > 0xffff)
8647         continue;
8648       if (dwarf_split_debug_info)
8649         {
8650           dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8651                                "Location list start/length entry (%s)",
8652                                list_head->ll_symbol);
8653           dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8654                                        "Location list range start index (%s)",
8655                                        curr->begin);
8656           /* The length field is 4 bytes.  If we ever need to support
8657             an 8-byte length, we can add a new DW_LLE code or fall back
8658             to DW_LLE_GNU_start_end_entry.  */
8659           dw2_asm_output_delta (4, curr->end, curr->begin,
8660                                 "Location list range length (%s)",
8661                                 list_head->ll_symbol);
8662         }
8663       else if (!have_multiple_function_sections)
8664         {
8665           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8666                                 "Location list begin address (%s)",
8667                                 list_head->ll_symbol);
8668           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8669                                 "Location list end address (%s)",
8670                                 list_head->ll_symbol);
8671         }
8672       else
8673         {
8674           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8675                                "Location list begin address (%s)",
8676                                list_head->ll_symbol);
8677           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8678                                "Location list end address (%s)",
8679                                list_head->ll_symbol);
8680         }
8681
8682       /* Output the block length for this list of location operations.  */
8683       gcc_assert (size <= 0xffff);
8684       dw2_asm_output_data (2, size, "%s", "Location expression size");
8685
8686       output_loc_sequence (curr->expr, -1);
8687     }
8688
8689   if (dwarf_split_debug_info)
8690     dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8691                          "Location list terminator (%s)",
8692                          list_head->ll_symbol);
8693   else
8694     {
8695       dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8696                            "Location list terminator begin (%s)",
8697                            list_head->ll_symbol);
8698       dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8699                            "Location list terminator end (%s)",
8700                            list_head->ll_symbol);
8701     }
8702 }
8703
8704 /* Output a range_list offset into the debug_range section.  Emit a
8705    relocated reference if val_entry is NULL, otherwise, emit an
8706    indirect reference.  */
8707
8708 static void
8709 output_range_list_offset (dw_attr_ref a)
8710 {
8711   const char *name = dwarf_attr_name (a->dw_attr);
8712
8713   if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8714     {
8715       char *p = strchr (ranges_section_label, '\0');
8716       sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8717       dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8718                              debug_ranges_section, "%s", name);
8719       *p = '\0';
8720     }
8721   else
8722     dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8723                          "%s (offset from %s)", name, ranges_section_label);
8724 }
8725
8726 /* Output the offset into the debug_loc section.  */
8727
8728 static void
8729 output_loc_list_offset (dw_attr_ref a)
8730 {
8731   char *sym = AT_loc_list (a)->ll_symbol;
8732
8733   gcc_assert (sym);
8734   if (dwarf_split_debug_info)
8735     dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8736                           "%s", dwarf_attr_name (a->dw_attr));
8737   else
8738     dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8739                            "%s", dwarf_attr_name (a->dw_attr));
8740 }
8741
8742 /* Output an attribute's index or value appropriately.  */
8743
8744 static void
8745 output_attr_index_or_value (dw_attr_ref a)
8746 {
8747   const char *name = dwarf_attr_name (a->dw_attr);
8748
8749   if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8750     {
8751       dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8752       return;
8753     }
8754   switch (AT_class (a))
8755     {
8756       case dw_val_class_addr:
8757         dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8758         break;
8759       case dw_val_class_high_pc:
8760       case dw_val_class_lbl_id:
8761         dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8762         break;
8763       case dw_val_class_loc_list:
8764         output_loc_list_offset (a);
8765         break;
8766       default:
8767         gcc_unreachable ();
8768     }
8769 }
8770
8771 /* Output a type signature.  */
8772
8773 static inline void
8774 output_signature (const char *sig, const char *name)
8775 {
8776   int i;
8777
8778   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8779     dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8780 }
8781
8782 /* Output the DIE and its attributes.  Called recursively to generate
8783    the definitions of each child DIE.  */
8784
8785 static void
8786 output_die (dw_die_ref die)
8787 {
8788   dw_attr_ref a;
8789   dw_die_ref c;
8790   unsigned long size;
8791   unsigned ix;
8792
8793   /* If someone in another CU might refer to us, set up a symbol for
8794      them to point to.  */
8795   if (! die->comdat_type_p && die->die_id.die_symbol)
8796     output_die_symbol (die);
8797
8798   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8799                                (unsigned long)die->die_offset,
8800                                dwarf_tag_name (die->die_tag));
8801
8802   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8803     {
8804       const char *name = dwarf_attr_name (a->dw_attr);
8805
8806       switch (AT_class (a))
8807         {
8808         case dw_val_class_addr:
8809           output_attr_index_or_value (a);
8810           break;
8811
8812         case dw_val_class_offset:
8813           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8814                                "%s", name);
8815           break;
8816
8817         case dw_val_class_range_list:
8818           output_range_list_offset (a);
8819           break;
8820
8821         case dw_val_class_loc:
8822           size = size_of_locs (AT_loc (a));
8823
8824           /* Output the block length for this list of location operations.  */
8825           if (dwarf_version >= 4)
8826             dw2_asm_output_data_uleb128 (size, "%s", name);
8827           else
8828             dw2_asm_output_data (constant_size (size), size, "%s", name);
8829
8830           output_loc_sequence (AT_loc (a), -1);
8831           break;
8832
8833         case dw_val_class_const:
8834           /* ??? It would be slightly more efficient to use a scheme like is
8835              used for unsigned constants below, but gdb 4.x does not sign
8836              extend.  Gdb 5.x does sign extend.  */
8837           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8838           break;
8839
8840         case dw_val_class_unsigned_const:
8841           {
8842             int csize = constant_size (AT_unsigned (a));
8843             if (dwarf_version == 3
8844                 && a->dw_attr == DW_AT_data_member_location
8845                 && csize >= 4)
8846               dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8847             else
8848               dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8849           }
8850           break;
8851
8852         case dw_val_class_const_double:
8853           {
8854             unsigned HOST_WIDE_INT first, second;
8855
8856             if (HOST_BITS_PER_WIDE_INT >= 64)
8857               dw2_asm_output_data (1,
8858                                    HOST_BITS_PER_DOUBLE_INT
8859                                    / HOST_BITS_PER_CHAR,
8860                                    NULL);
8861
8862             if (WORDS_BIG_ENDIAN)
8863               {
8864                 first = a->dw_attr_val.v.val_double.high;
8865                 second = a->dw_attr_val.v.val_double.low;
8866               }
8867             else
8868               {
8869                 first = a->dw_attr_val.v.val_double.low;
8870                 second = a->dw_attr_val.v.val_double.high;
8871               }
8872
8873             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8874                                  first, "%s", name);
8875             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8876                                  second, NULL);
8877           }
8878           break;
8879
8880         case dw_val_class_wide_int:
8881           {
8882             int i;
8883             int len = get_full_len (*a->dw_attr_val.v.val_wide);
8884             int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
8885             if (len * HOST_BITS_PER_WIDE_INT > 64)
8886               dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide) * l,
8887                                    NULL);
8888
8889             if (WORDS_BIG_ENDIAN)
8890               for (i = len - 1; i >= 0; --i)
8891                 {
8892                   dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8893                                        name);
8894                   name = NULL;
8895                 }
8896             else
8897               for (i = 0; i < len; ++i)
8898                 {
8899                   dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8900                                        name);
8901                   name = NULL;
8902                 }
8903           }
8904           break;
8905
8906         case dw_val_class_vec:
8907           {
8908             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8909             unsigned int len = a->dw_attr_val.v.val_vec.length;
8910             unsigned int i;
8911             unsigned char *p;
8912
8913             dw2_asm_output_data (constant_size (len * elt_size),
8914                                  len * elt_size, "%s", name);
8915             if (elt_size > sizeof (HOST_WIDE_INT))
8916               {
8917                 elt_size /= 2;
8918                 len *= 2;
8919               }
8920             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8921                  i < len;
8922                  i++, p += elt_size)
8923               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8924                                    "fp or vector constant word %u", i);
8925             break;
8926           }
8927
8928         case dw_val_class_flag:
8929           if (dwarf_version >= 4)
8930             {
8931               /* Currently all add_AT_flag calls pass in 1 as last argument,
8932                  so DW_FORM_flag_present can be used.  If that ever changes,
8933                  we'll need to use DW_FORM_flag and have some optimization
8934                  in build_abbrev_table that will change those to
8935                  DW_FORM_flag_present if it is set to 1 in all DIEs using
8936                  the same abbrev entry.  */
8937               gcc_assert (AT_flag (a) == 1);
8938               if (flag_debug_asm)
8939                 fprintf (asm_out_file, "\t\t\t%s %s\n",
8940                          ASM_COMMENT_START, name);
8941               break;
8942             }
8943           dw2_asm_output_data (1, AT_flag (a), "%s", name);
8944           break;
8945
8946         case dw_val_class_loc_list:
8947           output_attr_index_or_value (a);
8948           break;
8949
8950         case dw_val_class_die_ref:
8951           if (AT_ref_external (a))
8952             {
8953               if (AT_ref (a)->comdat_type_p)
8954                 {
8955                   comdat_type_node_ref type_node =
8956                     AT_ref (a)->die_id.die_type_node;
8957
8958                   gcc_assert (type_node);
8959                   output_signature (type_node->signature, name);
8960                 }
8961               else
8962                 {
8963                   const char *sym = AT_ref (a)->die_id.die_symbol;
8964                   int size;
8965
8966                   gcc_assert (sym);
8967                   /* In DWARF2, DW_FORM_ref_addr is sized by target address
8968                      length, whereas in DWARF3 it's always sized as an
8969                      offset.  */
8970                   if (dwarf_version == 2)
8971                     size = DWARF2_ADDR_SIZE;
8972                   else
8973                     size = DWARF_OFFSET_SIZE;
8974                   dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8975                                          name);
8976                 }
8977             }
8978           else
8979             {
8980               gcc_assert (AT_ref (a)->die_offset);
8981               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8982                                    "%s", name);
8983             }
8984           break;
8985
8986         case dw_val_class_fde_ref:
8987           {
8988             char l1[20];
8989
8990             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8991                                          a->dw_attr_val.v.val_fde_index * 2);
8992             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8993                                    "%s", name);
8994           }
8995           break;
8996
8997         case dw_val_class_vms_delta:
8998           dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
8999                                     AT_vms_delta2 (a), AT_vms_delta1 (a),
9000                                     "%s", name);
9001           break;
9002
9003         case dw_val_class_lbl_id:
9004           output_attr_index_or_value (a);
9005           break;
9006
9007         case dw_val_class_lineptr:
9008           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9009                                  debug_line_section, "%s", name);
9010           break;
9011
9012         case dw_val_class_macptr:
9013           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9014                                  debug_macinfo_section, "%s", name);
9015           break;
9016
9017         case dw_val_class_str:
9018           if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
9019             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9020                                    a->dw_attr_val.v.val_str->label,
9021                                    debug_str_section,
9022                                    "%s: \"%s\"", name, AT_string (a));
9023           else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
9024             dw2_asm_output_data_uleb128 (AT_index (a),
9025                                          "%s: \"%s\"", name, AT_string (a));
9026           else
9027             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
9028           break;
9029
9030         case dw_val_class_file:
9031           {
9032             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
9033
9034             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
9035                                  a->dw_attr_val.v.val_file->filename);
9036             break;
9037           }
9038
9039         case dw_val_class_data8:
9040           {
9041             int i;
9042
9043             for (i = 0; i < 8; i++)
9044               dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
9045                                    i == 0 ? "%s" : NULL, name);
9046             break;
9047           }
9048
9049         case dw_val_class_high_pc:
9050           dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
9051                                 get_AT_low_pc (die), "DW_AT_high_pc");
9052           break;
9053
9054         default:
9055           gcc_unreachable ();
9056         }
9057     }
9058
9059   FOR_EACH_CHILD (die, c, output_die (c));
9060
9061   /* Add null byte to terminate sibling list.  */
9062   if (die->die_child != NULL)
9063     dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
9064                          (unsigned long) die->die_offset);
9065 }
9066
9067 /* Output the compilation unit that appears at the beginning of the
9068    .debug_info section, and precedes the DIE descriptions.  */
9069
9070 static void
9071 output_compilation_unit_header (void)
9072 {
9073   /* We don't support actual DWARFv5 units yet, we just use some
9074      DWARFv5 draft DIE tags in DWARFv4 format.  */
9075   int ver = dwarf_version < 5 ? dwarf_version : 4;
9076
9077   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9078     dw2_asm_output_data (4, 0xffffffff,
9079       "Initial length escape value indicating 64-bit DWARF extension");
9080   dw2_asm_output_data (DWARF_OFFSET_SIZE,
9081                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
9082                        "Length of Compilation Unit Info");
9083   dw2_asm_output_data (2, ver, "DWARF version number");
9084   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
9085                          debug_abbrev_section,
9086                          "Offset Into Abbrev. Section");
9087   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9088 }
9089
9090 /* Output the compilation unit DIE and its children.  */
9091
9092 static void
9093 output_comp_unit (dw_die_ref die, int output_if_empty)
9094 {
9095   const char *secname, *oldsym;
9096   char *tmp;
9097
9098   /* Unless we are outputting main CU, we may throw away empty ones.  */
9099   if (!output_if_empty && die->die_child == NULL)
9100     return;
9101
9102   /* Even if there are no children of this DIE, we must output the information
9103      about the compilation unit.  Otherwise, on an empty translation unit, we
9104      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
9105      will then complain when examining the file.  First mark all the DIEs in
9106      this CU so we know which get local refs.  */
9107   mark_dies (die);
9108
9109   external_ref_hash_type *extern_map = optimize_external_refs (die);
9110
9111   build_abbrev_table (die, extern_map);
9112
9113   delete extern_map;
9114
9115   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
9116   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
9117   calc_die_sizes (die);
9118
9119   oldsym = die->die_id.die_symbol;
9120   if (oldsym)
9121     {
9122       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
9123
9124       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
9125       secname = tmp;
9126       die->die_id.die_symbol = NULL;
9127       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9128     }
9129   else
9130     {
9131       switch_to_section (debug_info_section);
9132       ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
9133       info_section_emitted = true;
9134     }
9135
9136   /* Output debugging information.  */
9137   output_compilation_unit_header ();
9138   output_die (die);
9139
9140   /* Leave the marks on the main CU, so we can check them in
9141      output_pubnames.  */
9142   if (oldsym)
9143     {
9144       unmark_dies (die);
9145       die->die_id.die_symbol = oldsym;
9146     }
9147 }
9148
9149 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
9150    and .debug_pubtypes.  This is configured per-target, but can be
9151    overridden by the -gpubnames or -gno-pubnames options.  */
9152
9153 static inline bool
9154 want_pubnames (void)
9155 {
9156   if (debug_info_level <= DINFO_LEVEL_TERSE)
9157     return false;
9158   if (debug_generate_pub_sections != -1)
9159     return debug_generate_pub_sections;
9160   return targetm.want_debug_pub_sections;
9161 }
9162
9163 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes.  */
9164
9165 static void
9166 add_AT_pubnames (dw_die_ref die)
9167 {
9168   if (want_pubnames ())
9169     add_AT_flag (die, DW_AT_GNU_pubnames, 1);
9170 }
9171
9172 /* Add a string attribute value to a skeleton DIE.  */
9173
9174 static inline void
9175 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
9176                         const char *str)
9177 {
9178   dw_attr_node attr;
9179   struct indirect_string_node *node;
9180
9181   if (! skeleton_debug_str_hash)
9182     skeleton_debug_str_hash
9183       = hash_table<indirect_string_hasher>::create_ggc (10);
9184
9185   node = find_AT_string_in_table (str, skeleton_debug_str_hash);
9186   find_string_form (node);
9187   if (node->form == DW_FORM_GNU_str_index)
9188     node->form = DW_FORM_strp;
9189
9190   attr.dw_attr = attr_kind;
9191   attr.dw_attr_val.val_class = dw_val_class_str;
9192   attr.dw_attr_val.val_entry = NULL;
9193   attr.dw_attr_val.v.val_str = node;
9194   add_dwarf_attr (die, &attr);
9195 }
9196
9197 /* Helper function to generate top-level dies for skeleton debug_info and
9198    debug_types.  */
9199
9200 static void
9201 add_top_level_skeleton_die_attrs (dw_die_ref die)
9202 {
9203   const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
9204   const char *comp_dir = comp_dir_string ();
9205
9206   add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
9207   if (comp_dir != NULL)
9208     add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
9209   add_AT_pubnames (die);
9210   add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
9211 }
9212
9213 /* Output skeleton debug sections that point to the dwo file.  */
9214
9215 static void
9216 output_skeleton_debug_sections (dw_die_ref comp_unit)
9217 {
9218   /* We don't support actual DWARFv5 units yet, we just use some
9219      DWARFv5 draft DIE tags in DWARFv4 format.  */
9220   int ver = dwarf_version < 5 ? dwarf_version : 4;
9221
9222   /* These attributes will be found in the full debug_info section.  */
9223   remove_AT (comp_unit, DW_AT_producer);
9224   remove_AT (comp_unit, DW_AT_language);
9225
9226   switch_to_section (debug_skeleton_info_section);
9227   ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
9228
9229   /* Produce the skeleton compilation-unit header.  This one differs enough from
9230      a normal CU header that it's better not to call output_compilation_unit
9231      header.  */
9232   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9233     dw2_asm_output_data (4, 0xffffffff,
9234       "Initial length escape value indicating 64-bit DWARF extension");
9235
9236   dw2_asm_output_data (DWARF_OFFSET_SIZE,
9237                        DWARF_COMPILE_UNIT_HEADER_SIZE
9238                        - DWARF_INITIAL_LENGTH_SIZE
9239                        + size_of_die (comp_unit),
9240                       "Length of Compilation Unit Info");
9241   dw2_asm_output_data (2, ver, "DWARF version number");
9242   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
9243                          debug_abbrev_section,
9244                          "Offset Into Abbrev. Section");
9245   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9246
9247   comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
9248   output_die (comp_unit);
9249
9250   /* Build the skeleton debug_abbrev section.  */
9251   switch_to_section (debug_skeleton_abbrev_section);
9252   ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
9253
9254   output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
9255
9256   dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
9257 }
9258
9259 /* Output a comdat type unit DIE and its children.  */
9260
9261 static void
9262 output_comdat_type_unit (comdat_type_node *node)
9263 {
9264   const char *secname;
9265   char *tmp;
9266   int i;
9267 #if defined (OBJECT_FORMAT_ELF)
9268   tree comdat_key;
9269 #endif
9270
9271   /* First mark all the DIEs in this CU so we know which get local refs.  */
9272   mark_dies (node->root_die);
9273
9274   external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
9275
9276   build_abbrev_table (node->root_die, extern_map);
9277
9278   delete extern_map;
9279   extern_map = NULL;
9280
9281   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
9282   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
9283   calc_die_sizes (node->root_die);
9284
9285 #if defined (OBJECT_FORMAT_ELF)
9286   if (!dwarf_split_debug_info)
9287     secname = ".debug_types";
9288   else
9289     secname = ".debug_types.dwo";
9290
9291   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9292   sprintf (tmp, "wt.");
9293   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9294     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9295   comdat_key = get_identifier (tmp);
9296   targetm.asm_out.named_section (secname,
9297                                  SECTION_DEBUG | SECTION_LINKONCE,
9298                                  comdat_key);
9299 #else
9300   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9301   sprintf (tmp, ".gnu.linkonce.wt.");
9302   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9303     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9304   secname = tmp;
9305   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9306 #endif
9307
9308   /* Output debugging information.  */
9309   output_compilation_unit_header ();
9310   output_signature (node->signature, "Type Signature");
9311   dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9312                        "Offset to Type DIE");
9313   output_die (node->root_die);
9314
9315   unmark_dies (node->root_die);
9316 }
9317
9318 /* Return the DWARF2/3 pubname associated with a decl.  */
9319
9320 static const char *
9321 dwarf2_name (tree decl, int scope)
9322 {
9323   if (DECL_NAMELESS (decl))
9324     return NULL;
9325   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9326 }
9327
9328 /* Add a new entry to .debug_pubnames if appropriate.  */
9329
9330 static void
9331 add_pubname_string (const char *str, dw_die_ref die)
9332 {
9333   pubname_entry e;
9334
9335   e.die = die;
9336   e.name = xstrdup (str);
9337   vec_safe_push (pubname_table, e);
9338 }
9339
9340 static void
9341 add_pubname (tree decl, dw_die_ref die)
9342 {
9343   if (!want_pubnames ())
9344     return;
9345
9346   /* Don't add items to the table when we expect that the consumer will have
9347      just read the enclosing die.  For example, if the consumer is looking at a
9348      class_member, it will either be inside the class already, or will have just
9349      looked up the class to find the member.  Either way, searching the class is
9350      faster than searching the index.  */
9351   if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9352       || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9353     {
9354       const char *name = dwarf2_name (decl, 1);
9355
9356       if (name)
9357         add_pubname_string (name, die);
9358     }
9359 }
9360
9361 /* Add an enumerator to the pubnames section.  */
9362
9363 static void
9364 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9365 {
9366   pubname_entry e;
9367
9368   gcc_assert (scope_name);
9369   e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9370   e.die = die;
9371   vec_safe_push (pubname_table, e);
9372 }
9373
9374 /* Add a new entry to .debug_pubtypes if appropriate.  */
9375
9376 static void
9377 add_pubtype (tree decl, dw_die_ref die)
9378 {
9379   pubname_entry e;
9380
9381   if (!want_pubnames ())
9382     return;
9383
9384   if ((TREE_PUBLIC (decl)
9385        || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9386       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9387     {
9388       tree scope = NULL;
9389       const char *scope_name = "";
9390       const char *sep = is_cxx () ? "::" : ".";
9391       const char *name;
9392
9393       scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9394       if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9395         {
9396           scope_name = lang_hooks.dwarf_name (scope, 1);
9397           if (scope_name != NULL && scope_name[0] != '\0')
9398             scope_name = concat (scope_name, sep, NULL);
9399           else
9400             scope_name = "";
9401         }
9402
9403       if (TYPE_P (decl))
9404         name = type_tag (decl);
9405       else
9406         name = lang_hooks.dwarf_name (decl, 1);
9407
9408       /* If we don't have a name for the type, there's no point in adding
9409          it to the table.  */
9410       if (name != NULL && name[0] != '\0')
9411         {
9412           e.die = die;
9413           e.name = concat (scope_name, name, NULL);
9414           vec_safe_push (pubtype_table, e);
9415         }
9416
9417       /* Although it might be more consistent to add the pubinfo for the
9418          enumerators as their dies are created, they should only be added if the
9419          enum type meets the criteria above.  So rather than re-check the parent
9420          enum type whenever an enumerator die is created, just output them all
9421          here.  This isn't protected by the name conditional because anonymous
9422          enums don't have names.  */
9423       if (die->die_tag == DW_TAG_enumeration_type)
9424         {
9425           dw_die_ref c;
9426
9427           FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9428         }
9429     }
9430 }
9431
9432 /* Output a single entry in the pubnames table.  */
9433
9434 static void
9435 output_pubname (dw_offset die_offset, pubname_entry *entry)
9436 {
9437   dw_die_ref die = entry->die;
9438   int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9439
9440   dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9441
9442   if (debug_generate_pub_sections == 2)
9443     {
9444       /* This logic follows gdb's method for determining the value of the flag
9445          byte.  */
9446       uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9447       switch (die->die_tag)
9448       {
9449         case DW_TAG_typedef:
9450         case DW_TAG_base_type:
9451         case DW_TAG_subrange_type:
9452           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9453           GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9454           break;
9455         case DW_TAG_enumerator:
9456           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9457                                           GDB_INDEX_SYMBOL_KIND_VARIABLE);
9458           if (!is_cxx () && !is_java ())
9459             GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9460           break;
9461         case DW_TAG_subprogram:
9462           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9463                                           GDB_INDEX_SYMBOL_KIND_FUNCTION);
9464           if (!is_ada ())
9465             GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9466           break;
9467         case DW_TAG_constant:
9468           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9469                                           GDB_INDEX_SYMBOL_KIND_VARIABLE);
9470           GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9471           break;
9472         case DW_TAG_variable:
9473           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9474                                           GDB_INDEX_SYMBOL_KIND_VARIABLE);
9475           GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9476           break;
9477         case DW_TAG_namespace:
9478         case DW_TAG_imported_declaration:
9479           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9480           break;
9481         case DW_TAG_class_type:
9482         case DW_TAG_interface_type:
9483         case DW_TAG_structure_type:
9484         case DW_TAG_union_type:
9485         case DW_TAG_enumeration_type:
9486           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9487           if (!is_cxx () && !is_java ())
9488             GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9489           break;
9490         default:
9491           /* An unusual tag.  Leave the flag-byte empty.  */
9492           break;
9493       }
9494       dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9495                            "GDB-index flags");
9496     }
9497
9498   dw2_asm_output_nstring (entry->name, -1, "external name");
9499 }
9500
9501
9502 /* Output the public names table used to speed up access to externally
9503    visible names; or the public types table used to find type definitions.  */
9504
9505 static void
9506 output_pubnames (vec<pubname_entry, va_gc> *names)
9507 {
9508   unsigned i;
9509   unsigned long pubnames_length = size_of_pubnames (names);
9510   pubname_ref pub;
9511
9512   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9513     dw2_asm_output_data (4, 0xffffffff,
9514       "Initial length escape value indicating 64-bit DWARF extension");
9515   dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length, "Pub Info Length");
9516
9517   /* Version number for pubnames/pubtypes is independent of dwarf version.  */
9518   dw2_asm_output_data (2, 2, "DWARF Version");
9519
9520   if (dwarf_split_debug_info)
9521     dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9522                            debug_skeleton_info_section,
9523                            "Offset of Compilation Unit Info");
9524   else
9525     dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9526                            debug_info_section,
9527                            "Offset of Compilation Unit Info");
9528   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9529                        "Compilation Unit Length");
9530
9531   FOR_EACH_VEC_ELT (*names, i, pub)
9532     {
9533       if (include_pubname_in_output (names, pub))
9534         {
9535           dw_offset die_offset = pub->die->die_offset;
9536
9537           /* We shouldn't see pubnames for DIEs outside of the main CU.  */
9538           if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9539             gcc_assert (pub->die->die_mark);
9540
9541           /* If we're putting types in their own .debug_types sections,
9542              the .debug_pubtypes table will still point to the compile
9543              unit (not the type unit), so we want to use the offset of
9544              the skeleton DIE (if there is one).  */
9545           if (pub->die->comdat_type_p && names == pubtype_table)
9546             {
9547               comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9548
9549               if (type_node != NULL)
9550                 die_offset = (type_node->skeleton_die != NULL
9551                               ? type_node->skeleton_die->die_offset
9552                               : comp_unit_die ()->die_offset);
9553             }
9554
9555           output_pubname (die_offset, pub);
9556         }
9557     }
9558
9559   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9560 }
9561
9562 /* Output public names and types tables if necessary.  */
9563
9564 static void
9565 output_pubtables (void)
9566 {
9567   if (!want_pubnames () || !info_section_emitted)
9568     return;
9569
9570   switch_to_section (debug_pubnames_section);
9571   output_pubnames (pubname_table);
9572   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
9573      It shouldn't hurt to emit it always, since pure DWARF2 consumers
9574      simply won't look for the section.  */
9575   switch_to_section (debug_pubtypes_section);
9576   output_pubnames (pubtype_table);
9577 }
9578
9579
9580 /* Output the information that goes into the .debug_aranges table.
9581    Namely, define the beginning and ending address range of the
9582    text section generated for this compilation unit.  */
9583
9584 static void
9585 output_aranges (unsigned long aranges_length)
9586 {
9587   unsigned i;
9588
9589   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9590     dw2_asm_output_data (4, 0xffffffff,
9591       "Initial length escape value indicating 64-bit DWARF extension");
9592   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9593                        "Length of Address Ranges Info");
9594   /* Version number for aranges is still 2, even up to DWARF5.  */
9595   dw2_asm_output_data (2, 2, "DWARF Version");
9596   if (dwarf_split_debug_info)
9597     dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9598                            debug_skeleton_info_section,
9599                            "Offset of Compilation Unit Info");
9600   else
9601     dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9602                            debug_info_section,
9603                            "Offset of Compilation Unit Info");
9604   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9605   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9606
9607   /* We need to align to twice the pointer size here.  */
9608   if (DWARF_ARANGES_PAD_SIZE)
9609     {
9610       /* Pad using a 2 byte words so that padding is correct for any
9611          pointer size.  */
9612       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9613                            2 * DWARF2_ADDR_SIZE);
9614       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9615         dw2_asm_output_data (2, 0, NULL);
9616     }
9617
9618   /* It is necessary not to output these entries if the sections were
9619      not used; if the sections were not used, the length will be 0 and
9620      the address may end up as 0 if the section is discarded by ld
9621      --gc-sections, leaving an invalid (0, 0) entry that can be
9622      confused with the terminator.  */
9623   if (text_section_used)
9624     {
9625       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9626       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9627                             text_section_label, "Length");
9628     }
9629   if (cold_text_section_used)
9630     {
9631       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9632                            "Address");
9633       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9634                             cold_text_section_label, "Length");
9635     }
9636
9637   if (have_multiple_function_sections)
9638     {
9639       unsigned fde_idx;
9640       dw_fde_ref fde;
9641
9642       FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9643         {
9644           if (DECL_IGNORED_P (fde->decl))
9645             continue;
9646           if (!fde->in_std_section)
9647             {
9648               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9649                                    "Address");
9650               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9651                                     fde->dw_fde_begin, "Length");
9652             }
9653           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9654             {
9655               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9656                                    "Address");
9657               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9658                                     fde->dw_fde_second_begin, "Length");
9659             }
9660         }
9661     }
9662
9663   /* Output the terminator words.  */
9664   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9665   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9666 }
9667
9668 /* Add a new entry to .debug_ranges.  Return the offset at which it
9669    was placed.  */
9670
9671 static unsigned int
9672 add_ranges_num (int num)
9673 {
9674   unsigned int in_use = ranges_table_in_use;
9675
9676   if (in_use == ranges_table_allocated)
9677     {
9678       ranges_table_allocated += RANGES_TABLE_INCREMENT;
9679       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9680                                     ranges_table_allocated);
9681       memset (ranges_table + ranges_table_in_use, 0,
9682               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9683     }
9684
9685   ranges_table[in_use].num = num;
9686   ranges_table_in_use = in_use + 1;
9687
9688   return in_use * 2 * DWARF2_ADDR_SIZE;
9689 }
9690
9691 /* Add a new entry to .debug_ranges corresponding to a block, or a
9692    range terminator if BLOCK is NULL.  */
9693
9694 static unsigned int
9695 add_ranges (const_tree block)
9696 {
9697   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9698 }
9699
9700 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9701    When using dwarf_split_debug_info, address attributes in dies destined
9702    for the final executable should be direct references--setting the
9703    parameter force_direct ensures this behavior.  */
9704
9705 static void
9706 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9707                       bool *added, bool force_direct)
9708 {
9709   unsigned int in_use = ranges_by_label_in_use;
9710   unsigned int offset;
9711
9712   if (in_use == ranges_by_label_allocated)
9713     {
9714       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9715       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9716                                        ranges_by_label,
9717                                        ranges_by_label_allocated);
9718       memset (ranges_by_label + ranges_by_label_in_use, 0,
9719               RANGES_TABLE_INCREMENT
9720               * sizeof (struct dw_ranges_by_label_struct));
9721     }
9722
9723   ranges_by_label[in_use].begin = begin;
9724   ranges_by_label[in_use].end = end;
9725   ranges_by_label_in_use = in_use + 1;
9726
9727   offset = add_ranges_num (-(int)in_use - 1);
9728   if (!*added)
9729     {
9730       add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9731       *added = true;
9732     }
9733 }
9734
9735 static void
9736 output_ranges (void)
9737 {
9738   unsigned i;
9739   static const char *const start_fmt = "Offset %#x";
9740   const char *fmt = start_fmt;
9741
9742   for (i = 0; i < ranges_table_in_use; i++)
9743     {
9744       int block_num = ranges_table[i].num;
9745
9746       if (block_num > 0)
9747         {
9748           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9749           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9750
9751           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9752           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9753
9754           /* If all code is in the text section, then the compilation
9755              unit base address defaults to DW_AT_low_pc, which is the
9756              base of the text section.  */
9757           if (!have_multiple_function_sections)
9758             {
9759               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9760                                     text_section_label,
9761                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
9762               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9763                                     text_section_label, NULL);
9764             }
9765
9766           /* Otherwise, the compilation unit base address is zero,
9767              which allows us to use absolute addresses, and not worry
9768              about whether the target supports cross-section
9769              arithmetic.  */
9770           else
9771             {
9772               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9773                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
9774               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9775             }
9776
9777           fmt = NULL;
9778         }
9779
9780       /* Negative block_num stands for an index into ranges_by_label.  */
9781       else if (block_num < 0)
9782         {
9783           int lab_idx = - block_num - 1;
9784
9785           if (!have_multiple_function_sections)
9786             {
9787               gcc_unreachable ();
9788 #if 0
9789               /* If we ever use add_ranges_by_labels () for a single
9790                  function section, all we have to do is to take out
9791                  the #if 0 above.  */
9792               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9793                                     ranges_by_label[lab_idx].begin,
9794                                     text_section_label,
9795                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
9796               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9797                                     ranges_by_label[lab_idx].end,
9798                                     text_section_label, NULL);
9799 #endif
9800             }
9801           else
9802             {
9803               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9804                                    ranges_by_label[lab_idx].begin,
9805                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
9806               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9807                                    ranges_by_label[lab_idx].end,
9808                                    NULL);
9809             }
9810         }
9811       else
9812         {
9813           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9814           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9815           fmt = start_fmt;
9816         }
9817     }
9818 }
9819
9820 /* Data structure containing information about input files.  */
9821 struct file_info
9822 {
9823   const char *path;     /* Complete file name.  */
9824   const char *fname;    /* File name part.  */
9825   int length;           /* Length of entire string.  */
9826   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
9827   int dir_idx;          /* Index in directory table.  */
9828 };
9829
9830 /* Data structure containing information about directories with source
9831    files.  */
9832 struct dir_info
9833 {
9834   const char *path;     /* Path including directory name.  */
9835   int length;           /* Path length.  */
9836   int prefix;           /* Index of directory entry which is a prefix.  */
9837   int count;            /* Number of files in this directory.  */
9838   int dir_idx;          /* Index of directory used as base.  */
9839 };
9840
9841 /* Callback function for file_info comparison.  We sort by looking at
9842    the directories in the path.  */
9843
9844 static int
9845 file_info_cmp (const void *p1, const void *p2)
9846 {
9847   const struct file_info *const s1 = (const struct file_info *) p1;
9848   const struct file_info *const s2 = (const struct file_info *) p2;
9849   const unsigned char *cp1;
9850   const unsigned char *cp2;
9851
9852   /* Take care of file names without directories.  We need to make sure that
9853      we return consistent values to qsort since some will get confused if
9854      we return the same value when identical operands are passed in opposite
9855      orders.  So if neither has a directory, return 0 and otherwise return
9856      1 or -1 depending on which one has the directory.  */
9857   if ((s1->path == s1->fname || s2->path == s2->fname))
9858     return (s2->path == s2->fname) - (s1->path == s1->fname);
9859
9860   cp1 = (const unsigned char *) s1->path;
9861   cp2 = (const unsigned char *) s2->path;
9862
9863   while (1)
9864     {
9865       ++cp1;
9866       ++cp2;
9867       /* Reached the end of the first path?  If so, handle like above.  */
9868       if ((cp1 == (const unsigned char *) s1->fname)
9869           || (cp2 == (const unsigned char *) s2->fname))
9870         return ((cp2 == (const unsigned char *) s2->fname)
9871                 - (cp1 == (const unsigned char *) s1->fname));
9872
9873       /* Character of current path component the same?  */
9874       else if (*cp1 != *cp2)
9875         return *cp1 - *cp2;
9876     }
9877 }
9878
9879 struct file_name_acquire_data
9880 {
9881   struct file_info *files;
9882   int used_files;
9883   int max_files;
9884 };
9885
9886 /* Traversal function for the hash table.  */
9887
9888 int
9889 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
9890 {
9891   struct dwarf_file_data *d = *slot;
9892   struct file_info *fi;
9893   const char *f;
9894
9895   gcc_assert (fnad->max_files >= d->emitted_number);
9896
9897   if (! d->emitted_number)
9898     return 1;
9899
9900   gcc_assert (fnad->max_files != fnad->used_files);
9901
9902   fi = fnad->files + fnad->used_files++;
9903
9904   /* Skip all leading "./".  */
9905   f = d->filename;
9906   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9907     f += 2;
9908
9909   /* Create a new array entry.  */
9910   fi->path = f;
9911   fi->length = strlen (f);
9912   fi->file_idx = d;
9913
9914   /* Search for the file name part.  */
9915   f = strrchr (f, DIR_SEPARATOR);
9916 #if defined (DIR_SEPARATOR_2)
9917   {
9918     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9919
9920     if (g != NULL)
9921       {
9922         if (f == NULL || f < g)
9923           f = g;
9924       }
9925   }
9926 #endif
9927
9928   fi->fname = f == NULL ? fi->path : f + 1;
9929   return 1;
9930 }
9931
9932 /* Output the directory table and the file name table.  We try to minimize
9933    the total amount of memory needed.  A heuristic is used to avoid large
9934    slowdowns with many input files.  */
9935
9936 static void
9937 output_file_names (void)
9938 {
9939   struct file_name_acquire_data fnad;
9940   int numfiles;
9941   struct file_info *files;
9942   struct dir_info *dirs;
9943   int *saved;
9944   int *savehere;
9945   int *backmap;
9946   int ndirs;
9947   int idx_offset;
9948   int i;
9949
9950   if (!last_emitted_file)
9951     {
9952       dw2_asm_output_data (1, 0, "End directory table");
9953       dw2_asm_output_data (1, 0, "End file name table");
9954       return;
9955     }
9956
9957   numfiles = last_emitted_file->emitted_number;
9958
9959   /* Allocate the various arrays we need.  */
9960   files = XALLOCAVEC (struct file_info, numfiles);
9961   dirs = XALLOCAVEC (struct dir_info, numfiles);
9962
9963   fnad.files = files;
9964   fnad.used_files = 0;
9965   fnad.max_files = numfiles;
9966   file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
9967   gcc_assert (fnad.used_files == fnad.max_files);
9968
9969   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9970
9971   /* Find all the different directories used.  */
9972   dirs[0].path = files[0].path;
9973   dirs[0].length = files[0].fname - files[0].path;
9974   dirs[0].prefix = -1;
9975   dirs[0].count = 1;
9976   dirs[0].dir_idx = 0;
9977   files[0].dir_idx = 0;
9978   ndirs = 1;
9979
9980   for (i = 1; i < numfiles; i++)
9981     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9982         && memcmp (dirs[ndirs - 1].path, files[i].path,
9983                    dirs[ndirs - 1].length) == 0)
9984       {
9985         /* Same directory as last entry.  */
9986         files[i].dir_idx = ndirs - 1;
9987         ++dirs[ndirs - 1].count;
9988       }
9989     else
9990       {
9991         int j;
9992
9993         /* This is a new directory.  */
9994         dirs[ndirs].path = files[i].path;
9995         dirs[ndirs].length = files[i].fname - files[i].path;
9996         dirs[ndirs].count = 1;
9997         dirs[ndirs].dir_idx = ndirs;
9998         files[i].dir_idx = ndirs;
9999
10000         /* Search for a prefix.  */
10001         dirs[ndirs].prefix = -1;
10002         for (j = 0; j < ndirs; j++)
10003           if (dirs[j].length < dirs[ndirs].length
10004               && dirs[j].length > 1
10005               && (dirs[ndirs].prefix == -1
10006                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
10007               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
10008             dirs[ndirs].prefix = j;
10009
10010         ++ndirs;
10011       }
10012
10013   /* Now to the actual work.  We have to find a subset of the directories which
10014      allow expressing the file name using references to the directory table
10015      with the least amount of characters.  We do not do an exhaustive search
10016      where we would have to check out every combination of every single
10017      possible prefix.  Instead we use a heuristic which provides nearly optimal
10018      results in most cases and never is much off.  */
10019   saved = XALLOCAVEC (int, ndirs);
10020   savehere = XALLOCAVEC (int, ndirs);
10021
10022   memset (saved, '\0', ndirs * sizeof (saved[0]));
10023   for (i = 0; i < ndirs; i++)
10024     {
10025       int j;
10026       int total;
10027
10028       /* We can always save some space for the current directory.  But this
10029          does not mean it will be enough to justify adding the directory.  */
10030       savehere[i] = dirs[i].length;
10031       total = (savehere[i] - saved[i]) * dirs[i].count;
10032
10033       for (j = i + 1; j < ndirs; j++)
10034         {
10035           savehere[j] = 0;
10036           if (saved[j] < dirs[i].length)
10037             {
10038               /* Determine whether the dirs[i] path is a prefix of the
10039                  dirs[j] path.  */
10040               int k;
10041
10042               k = dirs[j].prefix;
10043               while (k != -1 && k != (int) i)
10044                 k = dirs[k].prefix;
10045
10046               if (k == (int) i)
10047                 {
10048                   /* Yes it is.  We can possibly save some memory by
10049                      writing the filenames in dirs[j] relative to
10050                      dirs[i].  */
10051                   savehere[j] = dirs[i].length;
10052                   total += (savehere[j] - saved[j]) * dirs[j].count;
10053                 }
10054             }
10055         }
10056
10057       /* Check whether we can save enough to justify adding the dirs[i]
10058          directory.  */
10059       if (total > dirs[i].length + 1)
10060         {
10061           /* It's worthwhile adding.  */
10062           for (j = i; j < ndirs; j++)
10063             if (savehere[j] > 0)
10064               {
10065                 /* Remember how much we saved for this directory so far.  */
10066                 saved[j] = savehere[j];
10067
10068                 /* Remember the prefix directory.  */
10069                 dirs[j].dir_idx = i;
10070               }
10071         }
10072     }
10073
10074   /* Emit the directory name table.  */
10075   idx_offset = dirs[0].length > 0 ? 1 : 0;
10076   for (i = 1 - idx_offset; i < ndirs; i++)
10077     dw2_asm_output_nstring (dirs[i].path,
10078                             dirs[i].length
10079                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
10080                             "Directory Entry: %#x", i + idx_offset);
10081
10082   dw2_asm_output_data (1, 0, "End directory table");
10083
10084   /* We have to emit them in the order of emitted_number since that's
10085      used in the debug info generation.  To do this efficiently we
10086      generate a back-mapping of the indices first.  */
10087   backmap = XALLOCAVEC (int, numfiles);
10088   for (i = 0; i < numfiles; i++)
10089     backmap[files[i].file_idx->emitted_number - 1] = i;
10090
10091   /* Now write all the file names.  */
10092   for (i = 0; i < numfiles; i++)
10093     {
10094       int file_idx = backmap[i];
10095       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
10096
10097 #ifdef VMS_DEBUGGING_INFO
10098 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
10099
10100       /* Setting these fields can lead to debugger miscomparisons,
10101          but VMS Debug requires them to be set correctly.  */
10102
10103       int ver;
10104       long long cdt;
10105       long siz;
10106       int maxfilelen = strlen (files[file_idx].path)
10107                                + dirs[dir_idx].length
10108                                + MAX_VMS_VERSION_LEN + 1;
10109       char *filebuf = XALLOCAVEC (char, maxfilelen);
10110
10111       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
10112       snprintf (filebuf, maxfilelen, "%s;%d",
10113                 files[file_idx].path + dirs[dir_idx].length, ver);
10114
10115       dw2_asm_output_nstring
10116         (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
10117
10118       /* Include directory index.  */
10119       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10120
10121       /* Modification time.  */
10122       dw2_asm_output_data_uleb128
10123         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
10124           ? cdt : 0,
10125          NULL);
10126
10127       /* File length in bytes.  */
10128       dw2_asm_output_data_uleb128
10129         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
10130           ? siz : 0,
10131          NULL);
10132 #else
10133       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
10134                               "File Entry: %#x", (unsigned) i + 1);
10135
10136       /* Include directory index.  */
10137       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10138
10139       /* Modification time.  */
10140       dw2_asm_output_data_uleb128 (0, NULL);
10141
10142       /* File length in bytes.  */
10143       dw2_asm_output_data_uleb128 (0, NULL);
10144 #endif /* VMS_DEBUGGING_INFO */
10145     }
10146
10147   dw2_asm_output_data (1, 0, "End file name table");
10148 }
10149
10150
10151 /* Output one line number table into the .debug_line section.  */
10152
10153 static void
10154 output_one_line_info_table (dw_line_info_table *table)
10155 {
10156   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
10157   unsigned int current_line = 1;
10158   bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
10159   dw_line_info_entry *ent;
10160   size_t i;
10161
10162   FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
10163     {
10164       switch (ent->opcode)
10165         {
10166         case LI_set_address:
10167           /* ??? Unfortunately, we have little choice here currently, and
10168              must always use the most general form.  GCC does not know the
10169              address delta itself, so we can't use DW_LNS_advance_pc.  Many
10170              ports do have length attributes which will give an upper bound
10171              on the address range.  We could perhaps use length attributes
10172              to determine when it is safe to use DW_LNS_fixed_advance_pc.  */
10173           ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
10174
10175           /* This can handle any delta.  This takes
10176              4+DWARF2_ADDR_SIZE bytes.  */
10177           dw2_asm_output_data (1, 0, "set address %s", line_label);
10178           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10179           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10180           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10181           break;
10182
10183         case LI_set_line:
10184           if (ent->val == current_line)
10185             {
10186               /* We still need to start a new row, so output a copy insn.  */
10187               dw2_asm_output_data (1, DW_LNS_copy,
10188                                    "copy line %u", current_line);
10189             }
10190           else
10191             {
10192               int line_offset = ent->val - current_line;
10193               int line_delta = line_offset - DWARF_LINE_BASE;
10194
10195               current_line = ent->val;
10196               if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10197                 {
10198                   /* This can handle deltas from -10 to 234, using the current
10199                      definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
10200                      This takes 1 byte.  */
10201                   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10202                                        "line %u", current_line);
10203                 }
10204               else
10205                 {
10206                   /* This can handle any delta.  This takes at least 4 bytes,
10207                      depending on the value being encoded.  */
10208                   dw2_asm_output_data (1, DW_LNS_advance_line,
10209                                        "advance to line %u", current_line);
10210                   dw2_asm_output_data_sleb128 (line_offset, NULL);
10211                   dw2_asm_output_data (1, DW_LNS_copy, NULL);
10212                 }
10213             }
10214           break;
10215
10216         case LI_set_file:
10217           dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
10218           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10219           break;
10220
10221         case LI_set_column:
10222           dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
10223           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10224           break;
10225
10226         case LI_negate_stmt:
10227           current_is_stmt = !current_is_stmt;
10228           dw2_asm_output_data (1, DW_LNS_negate_stmt,
10229                                "is_stmt %d", current_is_stmt);
10230           break;
10231
10232         case LI_set_prologue_end:
10233           dw2_asm_output_data (1, DW_LNS_set_prologue_end,
10234                                "set prologue end");
10235           break;
10236           
10237         case LI_set_epilogue_begin:
10238           dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
10239                                "set epilogue begin");
10240           break;
10241
10242         case LI_set_discriminator:
10243           dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
10244           dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
10245           dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
10246           dw2_asm_output_data_uleb128 (ent->val, NULL);
10247           break;
10248         }
10249     }
10250
10251   /* Emit debug info for the address of the end of the table.  */
10252   dw2_asm_output_data (1, 0, "set address %s", table->end_label);
10253   dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10254   dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10255   dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10256
10257   dw2_asm_output_data (1, 0, "end sequence");
10258   dw2_asm_output_data_uleb128 (1, NULL);
10259   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10260 }
10261
10262 /* Output the source line number correspondence information.  This
10263    information goes into the .debug_line section.  */
10264
10265 static void
10266 output_line_info (bool prologue_only)
10267 {
10268   char l1[20], l2[20], p1[20], p2[20];
10269   /* We don't support DWARFv5 line tables yet.  */
10270   int ver = dwarf_version < 5 ? dwarf_version : 4;
10271   bool saw_one = false;
10272   int opc;
10273
10274   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10275   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10276   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10277   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10278
10279   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10280     dw2_asm_output_data (4, 0xffffffff,
10281       "Initial length escape value indicating 64-bit DWARF extension");
10282   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10283                         "Length of Source Line Info");
10284   ASM_OUTPUT_LABEL (asm_out_file, l1);
10285
10286   dw2_asm_output_data (2, ver, "DWARF Version");
10287   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10288   ASM_OUTPUT_LABEL (asm_out_file, p1);
10289
10290   /* Define the architecture-dependent minimum instruction length (in bytes).
10291      In this implementation of DWARF, this field is used for information
10292      purposes only.  Since GCC generates assembly language, we have no
10293      a priori knowledge of how many instruction bytes are generated for each
10294      source line, and therefore can use only the DW_LNE_set_address and
10295      DW_LNS_fixed_advance_pc line information commands.  Accordingly, we fix
10296      this as '1', which is "correct enough" for all architectures,
10297      and don't let the target override.  */
10298   dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10299
10300   if (ver >= 4)
10301     dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10302                          "Maximum Operations Per Instruction");
10303   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10304                        "Default is_stmt_start flag");
10305   dw2_asm_output_data (1, DWARF_LINE_BASE,
10306                        "Line Base Value (Special Opcodes)");
10307   dw2_asm_output_data (1, DWARF_LINE_RANGE,
10308                        "Line Range Value (Special Opcodes)");
10309   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10310                        "Special Opcode Base");
10311
10312   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10313     {
10314       int n_op_args;
10315       switch (opc)
10316         {
10317         case DW_LNS_advance_pc:
10318         case DW_LNS_advance_line:
10319         case DW_LNS_set_file:
10320         case DW_LNS_set_column:
10321         case DW_LNS_fixed_advance_pc:
10322         case DW_LNS_set_isa:
10323           n_op_args = 1;
10324           break;
10325         default:
10326           n_op_args = 0;
10327           break;
10328         }
10329
10330       dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10331                            opc, n_op_args);
10332     }
10333
10334   /* Write out the information about the files we use.  */
10335   output_file_names ();
10336   ASM_OUTPUT_LABEL (asm_out_file, p2);
10337   if (prologue_only)
10338     {
10339       /* Output the marker for the end of the line number info.  */
10340       ASM_OUTPUT_LABEL (asm_out_file, l2);
10341       return;
10342     }
10343
10344   if (separate_line_info)
10345     {
10346       dw_line_info_table *table;
10347       size_t i;
10348
10349       FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10350         if (table->in_use)
10351           {
10352             output_one_line_info_table (table);
10353             saw_one = true;
10354           }
10355     }
10356   if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10357     {
10358       output_one_line_info_table (cold_text_section_line_info);
10359       saw_one = true;
10360     }
10361
10362   /* ??? Some Darwin linkers crash on a .debug_line section with no
10363      sequences.  Further, merely a DW_LNE_end_sequence entry is not
10364      sufficient -- the address column must also be initialized.
10365      Make sure to output at least one set_address/end_sequence pair,
10366      choosing .text since that section is always present.  */
10367   if (text_section_line_info->in_use || !saw_one)
10368     output_one_line_info_table (text_section_line_info);
10369
10370   /* Output the marker for the end of the line number info.  */
10371   ASM_OUTPUT_LABEL (asm_out_file, l2);
10372 }
10373 \f
10374 /* Given a pointer to a tree node for some base type, return a pointer to
10375    a DIE that describes the given type.
10376
10377    This routine must only be called for GCC type nodes that correspond to
10378    Dwarf base (fundamental) types.  */
10379
10380 static dw_die_ref
10381 base_type_die (tree type)
10382 {
10383   dw_die_ref base_type_result;
10384   enum dwarf_type encoding;
10385
10386   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10387     return 0;
10388
10389   /* If this is a subtype that should not be emitted as a subrange type,
10390      use the base type.  See subrange_type_for_debug_p.  */
10391   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10392     type = TREE_TYPE (type);
10393
10394   switch (TREE_CODE (type))
10395     {
10396     case INTEGER_TYPE:
10397       if ((dwarf_version >= 4 || !dwarf_strict)
10398           && TYPE_NAME (type)
10399           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10400           && DECL_IS_BUILTIN (TYPE_NAME (type))
10401           && DECL_NAME (TYPE_NAME (type)))
10402         {
10403           const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10404           if (strcmp (name, "char16_t") == 0
10405               || strcmp (name, "char32_t") == 0)
10406             {
10407               encoding = DW_ATE_UTF;
10408               break;
10409             }
10410         }
10411       if (TYPE_STRING_FLAG (type))
10412         {
10413           if (TYPE_UNSIGNED (type))
10414             encoding = DW_ATE_unsigned_char;
10415           else
10416             encoding = DW_ATE_signed_char;
10417         }
10418       else if (TYPE_UNSIGNED (type))
10419         encoding = DW_ATE_unsigned;
10420       else
10421         encoding = DW_ATE_signed;
10422       break;
10423
10424     case REAL_TYPE:
10425       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10426         {
10427           if (dwarf_version >= 3 || !dwarf_strict)
10428             encoding = DW_ATE_decimal_float;
10429           else
10430             encoding = DW_ATE_lo_user;
10431         }
10432       else
10433         encoding = DW_ATE_float;
10434       break;
10435
10436     case FIXED_POINT_TYPE:
10437       if (!(dwarf_version >= 3 || !dwarf_strict))
10438         encoding = DW_ATE_lo_user;
10439       else if (TYPE_UNSIGNED (type))
10440         encoding = DW_ATE_unsigned_fixed;
10441       else
10442         encoding = DW_ATE_signed_fixed;
10443       break;
10444
10445       /* Dwarf2 doesn't know anything about complex ints, so use
10446          a user defined type for it.  */
10447     case COMPLEX_TYPE:
10448       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10449         encoding = DW_ATE_complex_float;
10450       else
10451         encoding = DW_ATE_lo_user;
10452       break;
10453
10454     case BOOLEAN_TYPE:
10455       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
10456       encoding = DW_ATE_boolean;
10457       break;
10458
10459     default:
10460       /* No other TREE_CODEs are Dwarf fundamental types.  */
10461       gcc_unreachable ();
10462     }
10463
10464   base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10465
10466   add_AT_unsigned (base_type_result, DW_AT_byte_size,
10467                    int_size_in_bytes (type));
10468   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10469   add_pubtype (type, base_type_result);
10470
10471   return base_type_result;
10472 }
10473
10474 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
10475    named 'auto' in its type: return true for it, false otherwise.  */
10476
10477 static inline bool
10478 is_cxx_auto (tree type)
10479 {
10480   if (is_cxx ())
10481     {
10482       tree name = TYPE_IDENTIFIER (type);
10483       if (name == get_identifier ("auto")
10484           || name == get_identifier ("decltype(auto)"))
10485         return true;
10486     }
10487   return false;
10488 }
10489
10490 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10491    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
10492
10493 static inline int
10494 is_base_type (tree type)
10495 {
10496   switch (TREE_CODE (type))
10497     {
10498     case ERROR_MARK:
10499     case VOID_TYPE:
10500     case INTEGER_TYPE:
10501     case REAL_TYPE:
10502     case FIXED_POINT_TYPE:
10503     case COMPLEX_TYPE:
10504     case BOOLEAN_TYPE:
10505     case POINTER_BOUNDS_TYPE:
10506       return 1;
10507
10508     case ARRAY_TYPE:
10509     case RECORD_TYPE:
10510     case UNION_TYPE:
10511     case QUAL_UNION_TYPE:
10512     case ENUMERAL_TYPE:
10513     case FUNCTION_TYPE:
10514     case METHOD_TYPE:
10515     case POINTER_TYPE:
10516     case REFERENCE_TYPE:
10517     case NULLPTR_TYPE:
10518     case OFFSET_TYPE:
10519     case LANG_TYPE:
10520     case VECTOR_TYPE:
10521       return 0;
10522
10523     default:
10524       if (is_cxx_auto (type))
10525         return 0;
10526       gcc_unreachable ();
10527     }
10528
10529   return 0;
10530 }
10531
10532 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10533    node, return the size in bits for the type if it is a constant, or else
10534    return the alignment for the type if the type's size is not constant, or
10535    else return BITS_PER_WORD if the type actually turns out to be an
10536    ERROR_MARK node.  */
10537
10538 static inline unsigned HOST_WIDE_INT
10539 simple_type_size_in_bits (const_tree type)
10540 {
10541   if (TREE_CODE (type) == ERROR_MARK)
10542     return BITS_PER_WORD;
10543   else if (TYPE_SIZE (type) == NULL_TREE)
10544     return 0;
10545   else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
10546     return tree_to_uhwi (TYPE_SIZE (type));
10547   else
10548     return TYPE_ALIGN (type);
10549 }
10550
10551 /* Similarly, but return an offset_int instead of UHWI.  */
10552
10553 static inline offset_int
10554 offset_int_type_size_in_bits (const_tree type)
10555 {
10556   if (TREE_CODE (type) == ERROR_MARK)
10557     return BITS_PER_WORD;
10558   else if (TYPE_SIZE (type) == NULL_TREE)
10559     return 0;
10560   else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10561     return wi::to_offset (TYPE_SIZE (type));
10562   else
10563     return TYPE_ALIGN (type);
10564 }
10565
10566 /*  Given a pointer to a tree node for a subrange type, return a pointer
10567     to a DIE that describes the given type.  */
10568
10569 static dw_die_ref
10570 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10571 {
10572   dw_die_ref subrange_die;
10573   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10574
10575   if (context_die == NULL)
10576     context_die = comp_unit_die ();
10577
10578   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10579
10580   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10581     {
10582       /* The size of the subrange type and its base type do not match,
10583          so we need to generate a size attribute for the subrange type.  */
10584       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10585     }
10586
10587   if (low)
10588     add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
10589   if (high)
10590     add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
10591
10592   return subrange_die;
10593 }
10594
10595 /* Returns the (const and/or volatile) cv_qualifiers associated with
10596    the decl node.  This will normally be augmented with the
10597    cv_qualifiers of the underlying type in add_type_attribute.  */
10598
10599 static int
10600 decl_quals (const_tree decl)
10601 {
10602   return ((TREE_READONLY (decl)
10603            ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
10604           | (TREE_THIS_VOLATILE (decl)
10605              ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
10606 }
10607
10608 /* Determine the TYPE whose qualifiers match the largest strict subset
10609    of the given TYPE_QUALS, and return its qualifiers.  Ignore all
10610    qualifiers outside QUAL_MASK.  */
10611
10612 static int
10613 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
10614 {
10615   tree t;
10616   int best_rank = 0, best_qual = 0, max_rank;
10617
10618   type_quals &= qual_mask;
10619   max_rank = popcount_hwi (type_quals) - 1;
10620
10621   for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
10622        t = TYPE_NEXT_VARIANT (t))
10623     {
10624       int q = TYPE_QUALS (t) & qual_mask;
10625
10626       if ((q & type_quals) == q && q != type_quals
10627           && check_base_type (t, type))
10628         {
10629           int rank = popcount_hwi (q);
10630
10631           if (rank > best_rank)
10632             {
10633               best_rank = rank;
10634               best_qual = q;
10635             }
10636         }
10637     }
10638
10639   return best_qual;
10640 }
10641
10642 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10643    entry that chains various modifiers in front of the given type.  */
10644
10645 static dw_die_ref
10646 modified_type_die (tree type, int cv_quals, dw_die_ref context_die)
10647 {
10648   enum tree_code code = TREE_CODE (type);
10649   dw_die_ref mod_type_die;
10650   dw_die_ref sub_die = NULL;
10651   tree item_type = NULL;
10652   tree qualified_type;
10653   tree name, low, high;
10654   dw_die_ref mod_scope;
10655   /* Only these cv-qualifiers are currently handled.  */
10656   const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
10657                             | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
10658
10659   if (code == ERROR_MARK)
10660     return NULL;
10661
10662   cv_quals &= cv_qual_mask;
10663
10664   /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
10665      tag modifier (and not an attribute) old consumers won't be able
10666      to handle it.  */
10667   if (dwarf_version < 3)
10668     cv_quals &= ~TYPE_QUAL_RESTRICT;
10669
10670   /* Likewise for DW_TAG_atomic_type for DWARFv5.  */
10671   if (dwarf_version < 5)
10672     cv_quals &= ~TYPE_QUAL_ATOMIC;
10673
10674   /* See if we already have the appropriately qualified variant of
10675      this type.  */
10676   qualified_type = get_qualified_type (type, cv_quals);
10677
10678   if (qualified_type == sizetype
10679       && TYPE_NAME (qualified_type)
10680       && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10681     {
10682       tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10683
10684       gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10685                            && TYPE_PRECISION (t)
10686                            == TYPE_PRECISION (qualified_type)
10687                            && TYPE_UNSIGNED (t)
10688                            == TYPE_UNSIGNED (qualified_type));
10689       qualified_type = t;
10690     }
10691
10692   /* If we do, then we can just use its DIE, if it exists.  */
10693   if (qualified_type)
10694     {
10695       mod_type_die = lookup_type_die (qualified_type);
10696       if (mod_type_die)
10697         return mod_type_die;
10698     }
10699
10700   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10701
10702   /* Handle C typedef types.  */
10703   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10704       && !DECL_ARTIFICIAL (name))
10705     {
10706       tree dtype = TREE_TYPE (name);
10707
10708       if (qualified_type == dtype)
10709         {
10710           /* For a named type, use the typedef.  */
10711           gen_type_die (qualified_type, context_die);
10712           return lookup_type_die (qualified_type);
10713         }
10714       else
10715         {
10716           int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
10717           dquals &= cv_qual_mask;
10718           if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
10719               || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
10720             /* cv-unqualified version of named type.  Just use
10721                the unnamed type to which it refers.  */
10722             return modified_type_die (DECL_ORIGINAL_TYPE (name),
10723                                       cv_quals, context_die);
10724           /* Else cv-qualified version of named type; fall through.  */
10725         }
10726     }
10727
10728   mod_scope = scope_die_for (type, context_die);
10729
10730   if (cv_quals)
10731     {
10732       struct qual_info { int q; enum dwarf_tag t; };
10733       static const struct qual_info qual_info[] =
10734         {
10735           { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type },
10736           { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
10737           { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
10738           { TYPE_QUAL_CONST, DW_TAG_const_type },
10739         };
10740       int sub_quals;
10741       unsigned i;
10742
10743       /* Determine a lesser qualified type that most closely matches
10744          this one.  Then generate DW_TAG_* entries for the remaining
10745          qualifiers.  */
10746       sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
10747                                                   cv_qual_mask);
10748       mod_type_die = modified_type_die (type, sub_quals, context_die);
10749
10750       for (i = 0; i < sizeof (qual_info) / sizeof (qual_info[0]); i++)
10751         if (qual_info[i].q & cv_quals & ~sub_quals)
10752           {
10753             dw_die_ref d = new_die (qual_info[i].t, mod_scope, type);
10754             if (mod_type_die)
10755               add_AT_die_ref (d, DW_AT_type, mod_type_die);
10756             mod_type_die = d;
10757           }
10758     }
10759   else if (code == POINTER_TYPE)
10760     {
10761       mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10762       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10763                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
10764       item_type = TREE_TYPE (type);
10765       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10766         add_AT_unsigned (mod_type_die, DW_AT_address_class,
10767                          TYPE_ADDR_SPACE (item_type));
10768     }
10769   else if (code == REFERENCE_TYPE)
10770     {
10771       if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10772         mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10773                                 type);
10774       else
10775         mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10776       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10777                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
10778       item_type = TREE_TYPE (type);
10779       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10780         add_AT_unsigned (mod_type_die, DW_AT_address_class,
10781                          TYPE_ADDR_SPACE (item_type));
10782     }
10783   else if (code == INTEGER_TYPE
10784            && TREE_TYPE (type) != NULL_TREE
10785            && subrange_type_for_debug_p (type, &low, &high))
10786     {
10787       mod_type_die = subrange_type_die (type, low, high, context_die);
10788       item_type = TREE_TYPE (type);
10789     }
10790   else if (is_base_type (type))
10791     mod_type_die = base_type_die (type);
10792   else
10793     {
10794       gen_type_die (type, context_die);
10795
10796       /* We have to get the type_main_variant here (and pass that to the
10797          `lookup_type_die' routine) because the ..._TYPE node we have
10798          might simply be a *copy* of some original type node (where the
10799          copy was created to help us keep track of typedef names) and
10800          that copy might have a different TYPE_UID from the original
10801          ..._TYPE node.  */
10802       if (TREE_CODE (type) != VECTOR_TYPE)
10803         return lookup_type_die (type_main_variant (type));
10804       else
10805         /* Vectors have the debugging information in the type,
10806            not the main variant.  */
10807         return lookup_type_die (type);
10808     }
10809
10810   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
10811      don't output a DW_TAG_typedef, since there isn't one in the
10812      user's program; just attach a DW_AT_name to the type.
10813      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10814      if the base type already has the same name.  */
10815   if (name
10816       && ((TREE_CODE (name) != TYPE_DECL
10817            && (qualified_type == TYPE_MAIN_VARIANT (type)
10818                || (cv_quals == TYPE_UNQUALIFIED)))
10819           || (TREE_CODE (name) == TYPE_DECL
10820               && TREE_TYPE (name) == qualified_type
10821               && DECL_NAME (name))))
10822     {
10823       if (TREE_CODE (name) == TYPE_DECL)
10824         /* Could just call add_name_and_src_coords_attributes here,
10825            but since this is a builtin type it doesn't have any
10826            useful source coordinates anyway.  */
10827         name = DECL_NAME (name);
10828       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10829     }
10830   /* This probably indicates a bug.  */
10831   else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10832     {
10833       name = TYPE_IDENTIFIER (type);
10834       add_name_attribute (mod_type_die,
10835                           name ? IDENTIFIER_POINTER (name) : "__unknown__");
10836     }
10837
10838   if (qualified_type)
10839     equate_type_number_to_die (qualified_type, mod_type_die);
10840
10841   if (item_type)
10842     /* We must do this after the equate_type_number_to_die call, in case
10843        this is a recursive type.  This ensures that the modified_type_die
10844        recursion will terminate even if the type is recursive.  Recursive
10845        types are possible in Ada.  */
10846     sub_die = modified_type_die (item_type,
10847                                  TYPE_QUALS_NO_ADDR_SPACE (item_type),
10848                                  context_die);
10849
10850   if (sub_die != NULL)
10851     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10852
10853   add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10854   if (TYPE_ARTIFICIAL (type))
10855     add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10856
10857   return mod_type_die;
10858 }
10859
10860 /* Generate DIEs for the generic parameters of T.
10861    T must be either a generic type or a generic function.
10862    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
10863
10864 static void
10865 gen_generic_params_dies (tree t)
10866 {
10867   tree parms, args;
10868   int parms_num, i;
10869   dw_die_ref die = NULL;
10870   int non_default;
10871
10872   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10873     return;
10874
10875   if (TYPE_P (t))
10876     die = lookup_type_die (t);
10877   else if (DECL_P (t))
10878     die = lookup_decl_die (t);
10879
10880   gcc_assert (die);
10881
10882   parms = lang_hooks.get_innermost_generic_parms (t);
10883   if (!parms)
10884     /* T has no generic parameter. It means T is neither a generic type
10885        or function. End of story.  */
10886     return;
10887
10888   parms_num = TREE_VEC_LENGTH (parms);
10889   args = lang_hooks.get_innermost_generic_args (t);
10890   if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
10891     non_default = int_cst_value (TREE_CHAIN (args));
10892   else
10893     non_default = TREE_VEC_LENGTH (args);
10894   for (i = 0; i < parms_num; i++)
10895     {
10896       tree parm, arg, arg_pack_elems;
10897       dw_die_ref parm_die;
10898
10899       parm = TREE_VEC_ELT (parms, i);
10900       arg = TREE_VEC_ELT (args, i);
10901       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10902       gcc_assert (parm && TREE_VALUE (parm) && arg);
10903
10904       if (parm && TREE_VALUE (parm) && arg)
10905         {
10906           /* If PARM represents a template parameter pack,
10907              emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10908              by DW_TAG_template_*_parameter DIEs for the argument
10909              pack elements of ARG. Note that ARG would then be
10910              an argument pack.  */
10911           if (arg_pack_elems)
10912             parm_die = template_parameter_pack_die (TREE_VALUE (parm),
10913                                                     arg_pack_elems,
10914                                                     die);
10915           else
10916             parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
10917                                               true /* emit name */, die);
10918           if (i >= non_default)
10919             add_AT_flag (parm_die, DW_AT_default_value, 1);
10920         }
10921     }
10922 }
10923
10924 /* Create and return a DIE for PARM which should be
10925    the representation of a generic type parameter.
10926    For instance, in the C++ front end, PARM would be a template parameter.
10927    ARG is the argument to PARM.
10928    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
10929    name of the PARM.
10930    PARENT_DIE is the parent DIE which the new created DIE should be added to,
10931    as a child node.  */
10932
10933 static dw_die_ref
10934 generic_parameter_die (tree parm, tree arg,
10935                        bool emit_name_p,
10936                        dw_die_ref parent_die)
10937 {
10938   dw_die_ref tmpl_die = NULL;
10939   const char *name = NULL;
10940
10941   if (!parm || !DECL_NAME (parm) || !arg)
10942     return NULL;
10943
10944   /* We support non-type generic parameters and arguments,
10945      type generic parameters and arguments, as well as
10946      generic generic parameters (a.k.a. template template parameters in C++)
10947      and arguments.  */
10948   if (TREE_CODE (parm) == PARM_DECL)
10949     /* PARM is a nontype generic parameter  */
10950     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10951   else if (TREE_CODE (parm) == TYPE_DECL)
10952     /* PARM is a type generic parameter.  */
10953     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10954   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10955     /* PARM is a generic generic parameter.
10956        Its DIE is a GNU extension. It shall have a
10957        DW_AT_name attribute to represent the name of the template template
10958        parameter, and a DW_AT_GNU_template_name attribute to represent the
10959        name of the template template argument.  */
10960     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10961                         parent_die, parm);
10962   else
10963     gcc_unreachable ();
10964
10965   if (tmpl_die)
10966     {
10967       tree tmpl_type;
10968
10969       /* If PARM is a generic parameter pack, it means we are
10970          emitting debug info for a template argument pack element.
10971          In other terms, ARG is a template argument pack element.
10972          In that case, we don't emit any DW_AT_name attribute for
10973          the die.  */
10974       if (emit_name_p)
10975         {
10976           name = IDENTIFIER_POINTER (DECL_NAME (parm));
10977           gcc_assert (name);
10978           add_AT_string (tmpl_die, DW_AT_name, name);
10979         }
10980
10981       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10982         {
10983           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10984              TMPL_DIE should have a child DW_AT_type attribute that is set
10985              to the type of the argument to PARM, which is ARG.
10986              If PARM is a type generic parameter, TMPL_DIE should have a
10987              child DW_AT_type that is set to ARG.  */
10988           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10989           add_type_attribute (tmpl_die, tmpl_type,
10990                               (TREE_THIS_VOLATILE (tmpl_type)
10991                                ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
10992                               parent_die);
10993         }
10994       else
10995         {
10996           /* So TMPL_DIE is a DIE representing a
10997              a generic generic template parameter, a.k.a template template
10998              parameter in C++ and arg is a template.  */
10999
11000           /* The DW_AT_GNU_template_name attribute of the DIE must be set
11001              to the name of the argument.  */
11002           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
11003           if (name)
11004             add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
11005         }
11006
11007       if (TREE_CODE (parm) == PARM_DECL)
11008         /* So PARM is a non-type generic parameter.
11009            DWARF3 5.6.8 says we must set a DW_AT_const_value child
11010            attribute of TMPL_DIE which value represents the value
11011            of ARG.
11012            We must be careful here:
11013            The value of ARG might reference some function decls.
11014            We might currently be emitting debug info for a generic
11015            type and types are emitted before function decls, we don't
11016            know if the function decls referenced by ARG will actually be
11017            emitted after cgraph computations.
11018            So must defer the generation of the DW_AT_const_value to
11019            after cgraph is ready.  */
11020         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
11021     }
11022
11023   return tmpl_die;
11024 }
11025
11026 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
11027    PARM_PACK must be a template parameter pack. The returned DIE
11028    will be child DIE of PARENT_DIE.  */
11029
11030 static dw_die_ref
11031 template_parameter_pack_die (tree parm_pack,
11032                              tree parm_pack_args,
11033                              dw_die_ref parent_die)
11034 {
11035   dw_die_ref die;
11036   int j;
11037
11038   gcc_assert (parent_die && parm_pack);
11039
11040   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
11041   add_name_and_src_coords_attributes (die, parm_pack);
11042   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
11043     generic_parameter_die (parm_pack,
11044                            TREE_VEC_ELT (parm_pack_args, j),
11045                            false /* Don't emit DW_AT_name */,
11046                            die);
11047   return die;
11048 }
11049
11050 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
11051    an enumerated type.  */
11052
11053 static inline int
11054 type_is_enum (const_tree type)
11055 {
11056   return TREE_CODE (type) == ENUMERAL_TYPE;
11057 }
11058
11059 /* Return the DBX register number described by a given RTL node.  */
11060
11061 static unsigned int
11062 dbx_reg_number (const_rtx rtl)
11063 {
11064   unsigned regno = REGNO (rtl);
11065
11066   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
11067
11068 #ifdef LEAF_REG_REMAP
11069   if (crtl->uses_only_leaf_regs)
11070     {
11071       int leaf_reg = LEAF_REG_REMAP (regno);
11072       if (leaf_reg != -1)
11073         regno = (unsigned) leaf_reg;
11074     }
11075 #endif
11076
11077   regno = DBX_REGISTER_NUMBER (regno);
11078   gcc_assert (regno != INVALID_REGNUM);
11079   return regno;
11080 }
11081
11082 /* Optionally add a DW_OP_piece term to a location description expression.
11083    DW_OP_piece is only added if the location description expression already
11084    doesn't end with DW_OP_piece.  */
11085
11086 static void
11087 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
11088 {
11089   dw_loc_descr_ref loc;
11090
11091   if (*list_head != NULL)
11092     {
11093       /* Find the end of the chain.  */
11094       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
11095         ;
11096
11097       if (loc->dw_loc_opc != DW_OP_piece)
11098         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
11099     }
11100 }
11101
11102 /* Return a location descriptor that designates a machine register or
11103    zero if there is none.  */
11104
11105 static dw_loc_descr_ref
11106 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
11107 {
11108   rtx regs;
11109
11110   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
11111     return 0;
11112
11113   /* We only use "frame base" when we're sure we're talking about the
11114      post-prologue local stack frame.  We do this by *not* running
11115      register elimination until this point, and recognizing the special
11116      argument pointer and soft frame pointer rtx's.
11117      Use DW_OP_fbreg offset DW_OP_stack_value in this case.  */
11118   if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
11119       && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
11120     {
11121       dw_loc_descr_ref result = NULL;
11122
11123       if (dwarf_version >= 4 || !dwarf_strict)
11124         {
11125           result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
11126                                        initialized);
11127           if (result)
11128             add_loc_descr (&result,
11129                            new_loc_descr (DW_OP_stack_value, 0, 0));
11130         }
11131       return result;
11132     }
11133
11134   regs = targetm.dwarf_register_span (rtl);
11135
11136   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
11137     return multiple_reg_loc_descriptor (rtl, regs, initialized);
11138   else
11139     {
11140       unsigned int dbx_regnum = dbx_reg_number (rtl);
11141       if (dbx_regnum == IGNORED_DWARF_REGNUM)
11142         return 0;
11143       return one_reg_loc_descriptor (dbx_regnum, initialized);
11144     }
11145 }
11146
11147 /* Return a location descriptor that designates a machine register for
11148    a given hard register number.  */
11149
11150 static dw_loc_descr_ref
11151 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
11152 {
11153   dw_loc_descr_ref reg_loc_descr;
11154
11155   if (regno <= 31)
11156     reg_loc_descr
11157       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
11158   else
11159     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
11160
11161   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11162     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11163
11164   return reg_loc_descr;
11165 }
11166
11167 /* Given an RTL of a register, return a location descriptor that
11168    designates a value that spans more than one register.  */
11169
11170 static dw_loc_descr_ref
11171 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
11172                              enum var_init_status initialized)
11173 {
11174   int size, i;
11175   dw_loc_descr_ref loc_result = NULL;
11176
11177   /* Simple, contiguous registers.  */
11178   if (regs == NULL_RTX)
11179     {
11180       unsigned reg = REGNO (rtl);
11181       int nregs;
11182
11183 #ifdef LEAF_REG_REMAP
11184       if (crtl->uses_only_leaf_regs)
11185         {
11186           int leaf_reg = LEAF_REG_REMAP (reg);
11187           if (leaf_reg != -1)
11188             reg = (unsigned) leaf_reg;
11189         }
11190 #endif
11191
11192       gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
11193       nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
11194
11195       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
11196
11197       loc_result = NULL;
11198       while (nregs--)
11199         {
11200           dw_loc_descr_ref t;
11201
11202           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
11203                                       VAR_INIT_STATUS_INITIALIZED);
11204           add_loc_descr (&loc_result, t);
11205           add_loc_descr_op_piece (&loc_result, size);
11206           ++reg;
11207         }
11208       return loc_result;
11209     }
11210
11211   /* Now onto stupid register sets in non contiguous locations.  */
11212
11213   gcc_assert (GET_CODE (regs) == PARALLEL);
11214
11215   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
11216   loc_result = NULL;
11217
11218   for (i = 0; i < XVECLEN (regs, 0); ++i)
11219     {
11220       dw_loc_descr_ref t;
11221
11222       t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
11223                                   VAR_INIT_STATUS_INITIALIZED);
11224       add_loc_descr (&loc_result, t);
11225       add_loc_descr_op_piece (&loc_result, size);
11226     }
11227
11228   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11229     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11230   return loc_result;
11231 }
11232
11233 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
11234
11235 /* Return a location descriptor that designates a constant i,
11236    as a compound operation from constant (i >> shift), constant shift
11237    and DW_OP_shl.  */
11238
11239 static dw_loc_descr_ref
11240 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11241 {
11242   dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
11243   add_loc_descr (&ret, int_loc_descriptor (shift));
11244   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11245   return ret;
11246 }
11247
11248 /* Return a location descriptor that designates a constant.  */
11249
11250 static dw_loc_descr_ref
11251 int_loc_descriptor (HOST_WIDE_INT i)
11252 {
11253   enum dwarf_location_atom op;
11254
11255   /* Pick the smallest representation of a constant, rather than just
11256      defaulting to the LEB encoding.  */
11257   if (i >= 0)
11258     {
11259       int clz = clz_hwi (i);
11260       int ctz = ctz_hwi (i);
11261       if (i <= 31)
11262         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
11263       else if (i <= 0xff)
11264         op = DW_OP_const1u;
11265       else if (i <= 0xffff)
11266         op = DW_OP_const2u;
11267       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11268                && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11269         /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
11270            DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
11271            while DW_OP_const4u is 5 bytes.  */
11272         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
11273       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11274                && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11275         /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
11276            while DW_OP_const4u is 5 bytes.  */
11277         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11278       else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11279         op = DW_OP_const4u;
11280       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11281                && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11282         /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
11283            while DW_OP_constu of constant >= 0x100000000 takes at least
11284            6 bytes.  */
11285         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11286       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11287                && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
11288                   >= HOST_BITS_PER_WIDE_INT)
11289         /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
11290            DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
11291            while DW_OP_constu takes in this case at least 6 bytes.  */
11292         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
11293       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11294                && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11295                && size_of_uleb128 (i) > 6)
11296         /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes.  */
11297         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
11298       else
11299         op = DW_OP_constu;
11300     }
11301   else
11302     {
11303       if (i >= -0x80)
11304         op = DW_OP_const1s;
11305       else if (i >= -0x8000)
11306         op = DW_OP_const2s;
11307       else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11308         {
11309           if (size_of_int_loc_descriptor (i) < 5)
11310             {
11311               dw_loc_descr_ref ret = int_loc_descriptor (-i);
11312               add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11313               return ret;
11314             }
11315           op = DW_OP_const4s;
11316         }
11317       else
11318         {
11319           if (size_of_int_loc_descriptor (i)
11320               < (unsigned long) 1 + size_of_sleb128 (i))
11321             {
11322               dw_loc_descr_ref ret = int_loc_descriptor (-i);
11323               add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11324               return ret;
11325             }
11326           op = DW_OP_consts;
11327         }
11328     }
11329
11330   return new_loc_descr (op, i, 0);
11331 }
11332
11333 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
11334    without actually allocating it.  */
11335
11336 static unsigned long
11337 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11338 {
11339   return size_of_int_loc_descriptor (i >> shift)
11340          + size_of_int_loc_descriptor (shift)
11341          + 1;
11342 }
11343
11344 /* Return size_of_locs (int_loc_descriptor (i)) without
11345    actually allocating it.  */
11346
11347 static unsigned long
11348 size_of_int_loc_descriptor (HOST_WIDE_INT i)
11349 {
11350   unsigned long s;
11351
11352   if (i >= 0)
11353     {
11354       int clz, ctz;
11355       if (i <= 31)
11356         return 1;
11357       else if (i <= 0xff)
11358         return 2;
11359       else if (i <= 0xffff)
11360         return 3;
11361       clz = clz_hwi (i);
11362       ctz = ctz_hwi (i);
11363       if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11364           && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11365         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11366                                                     - clz - 5);
11367       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11368                && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11369         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11370                                                     - clz - 8);
11371       else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11372         return 5;
11373       s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
11374       if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11375           && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11376         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11377                                                     - clz - 8);
11378       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11379                && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
11380         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11381                                                     - clz - 16);
11382       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11383                && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11384                && s > 6)
11385         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11386                                                     - clz - 32);
11387       else
11388         return 1 + s;
11389     }
11390   else
11391     {
11392       if (i >= -0x80)
11393         return 2;
11394       else if (i >= -0x8000)
11395         return 3;
11396       else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11397         {
11398           if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11399             {
11400               s = size_of_int_loc_descriptor (-i) + 1;
11401               if (s < 5)
11402                 return s;
11403             }
11404           return 5;
11405         }
11406       else
11407         {
11408           unsigned long r = 1 + size_of_sleb128 (i);
11409           if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11410             {
11411               s = size_of_int_loc_descriptor (-i) + 1;
11412               if (s < r)
11413                 return s;
11414             }
11415           return r;
11416         }
11417     }
11418 }
11419
11420 /* Return loc description representing "address" of integer value.
11421    This can appear only as toplevel expression.  */
11422
11423 static dw_loc_descr_ref
11424 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
11425 {
11426   int litsize;
11427   dw_loc_descr_ref loc_result = NULL;
11428
11429   if (!(dwarf_version >= 4 || !dwarf_strict))
11430     return NULL;
11431
11432   litsize = size_of_int_loc_descriptor (i);
11433   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
11434      is more compact.  For DW_OP_stack_value we need:
11435      litsize + 1 (DW_OP_stack_value)
11436      and for DW_OP_implicit_value:
11437      1 (DW_OP_implicit_value) + 1 (length) + size.  */
11438   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
11439     {
11440       loc_result = int_loc_descriptor (i);
11441       add_loc_descr (&loc_result,
11442                      new_loc_descr (DW_OP_stack_value, 0, 0));
11443       return loc_result;
11444     }
11445
11446   loc_result = new_loc_descr (DW_OP_implicit_value,
11447                               size, 0);
11448   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11449   loc_result->dw_loc_oprnd2.v.val_int = i;
11450   return loc_result;
11451 }
11452
11453 /* Return a location descriptor that designates a base+offset location.  */
11454
11455 static dw_loc_descr_ref
11456 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
11457                  enum var_init_status initialized)
11458 {
11459   unsigned int regno;
11460   dw_loc_descr_ref result;
11461   dw_fde_ref fde = cfun->fde;
11462
11463   /* We only use "frame base" when we're sure we're talking about the
11464      post-prologue local stack frame.  We do this by *not* running
11465      register elimination until this point, and recognizing the special
11466      argument pointer and soft frame pointer rtx's.  */
11467   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
11468     {
11469       rtx elim = (ira_use_lra_p
11470                   ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
11471                   : eliminate_regs (reg, VOIDmode, NULL_RTX));
11472
11473       if (elim != reg)
11474         {
11475           if (GET_CODE (elim) == PLUS)
11476             {
11477               offset += INTVAL (XEXP (elim, 1));
11478               elim = XEXP (elim, 0);
11479             }
11480           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11481                        && (elim == hard_frame_pointer_rtx
11482                            || elim == stack_pointer_rtx))
11483                       || elim == (frame_pointer_needed
11484                                   ? hard_frame_pointer_rtx
11485                                   : stack_pointer_rtx));
11486
11487           /* If drap register is used to align stack, use frame
11488              pointer + offset to access stack variables.  If stack
11489              is aligned without drap, use stack pointer + offset to
11490              access stack variables.  */
11491           if (crtl->stack_realign_tried
11492               && reg == frame_pointer_rtx)
11493             {
11494               int base_reg
11495                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
11496                                       ? HARD_FRAME_POINTER_REGNUM
11497                                       : REGNO (elim));
11498               return new_reg_loc_descr (base_reg, offset);
11499             }
11500
11501           gcc_assert (frame_pointer_fb_offset_valid);
11502           offset += frame_pointer_fb_offset;
11503           return new_loc_descr (DW_OP_fbreg, offset, 0);
11504         }
11505     }
11506
11507   regno = REGNO (reg);
11508 #ifdef LEAF_REG_REMAP
11509   if (crtl->uses_only_leaf_regs)
11510     {
11511       int leaf_reg = LEAF_REG_REMAP (regno);
11512       if (leaf_reg != -1)
11513         regno = (unsigned) leaf_reg;
11514     }
11515 #endif
11516   regno = DWARF_FRAME_REGNUM (regno);
11517
11518   if (!optimize && fde
11519       && (fde->drap_reg == regno || fde->vdrap_reg == regno))
11520     {
11521       /* Use cfa+offset to represent the location of arguments passed
11522          on the stack when drap is used to align stack.
11523          Only do this when not optimizing, for optimized code var-tracking
11524          is supposed to track where the arguments live and the register
11525          used as vdrap or drap in some spot might be used for something
11526          else in other part of the routine.  */
11527       return new_loc_descr (DW_OP_fbreg, offset, 0);
11528     }
11529
11530   if (regno <= 31)
11531     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11532                             offset, 0);
11533   else
11534     result = new_loc_descr (DW_OP_bregx, regno, offset);
11535
11536   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11537     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11538
11539   return result;
11540 }
11541
11542 /* Return true if this RTL expression describes a base+offset calculation.  */
11543
11544 static inline int
11545 is_based_loc (const_rtx rtl)
11546 {
11547   return (GET_CODE (rtl) == PLUS
11548           && ((REG_P (XEXP (rtl, 0))
11549                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11550                && CONST_INT_P (XEXP (rtl, 1)))));
11551 }
11552
11553 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11554    failed.  */
11555
11556 static dw_loc_descr_ref
11557 tls_mem_loc_descriptor (rtx mem)
11558 {
11559   tree base;
11560   dw_loc_descr_ref loc_result;
11561
11562   if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11563     return NULL;
11564
11565   base = get_base_address (MEM_EXPR (mem));
11566   if (base == NULL
11567       || TREE_CODE (base) != VAR_DECL
11568       || !DECL_THREAD_LOCAL_P (base))
11569     return NULL;
11570
11571   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
11572   if (loc_result == NULL)
11573     return NULL;
11574
11575   if (MEM_OFFSET (mem))
11576     loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11577
11578   return loc_result;
11579 }
11580
11581 /* Output debug info about reason why we failed to expand expression as dwarf
11582    expression.  */
11583
11584 static void
11585 expansion_failed (tree expr, rtx rtl, char const *reason)
11586 {
11587   if (dump_file && (dump_flags & TDF_DETAILS))
11588     {
11589       fprintf (dump_file, "Failed to expand as dwarf: ");
11590       if (expr)
11591         print_generic_expr (dump_file, expr, dump_flags);
11592       if (rtl)
11593         {
11594           fprintf (dump_file, "\n");
11595           print_rtl (dump_file, rtl);
11596         }
11597       fprintf (dump_file, "\nReason: %s\n", reason);
11598     }
11599 }
11600
11601 /* Helper function for const_ok_for_output.  */
11602
11603 static bool
11604 const_ok_for_output_1 (rtx rtl)
11605 {
11606   if (GET_CODE (rtl) == UNSPEC)
11607     {
11608       /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11609          we can't express it in the debug info.  */
11610 #ifdef ENABLE_CHECKING
11611       /* Don't complain about TLS UNSPECs, those are just too hard to
11612          delegitimize.  Note this could be a non-decl SYMBOL_REF such as
11613          one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11614          rather than DECL_THREAD_LOCAL_P is not just an optimization.  */
11615       if (XVECLEN (rtl, 0) == 0
11616           || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11617           || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11618         inform (current_function_decl
11619                 ? DECL_SOURCE_LOCATION (current_function_decl)
11620                 : UNKNOWN_LOCATION,
11621 #if NUM_UNSPEC_VALUES > 0
11622                 "non-delegitimized UNSPEC %s (%d) found in variable location",
11623                 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11624                  ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11625                 XINT (rtl, 1));
11626 #else
11627                 "non-delegitimized UNSPEC %d found in variable location",
11628                 XINT (rtl, 1));
11629 #endif
11630 #endif
11631       expansion_failed (NULL_TREE, rtl,
11632                         "UNSPEC hasn't been delegitimized.\n");
11633       return false;
11634     }
11635
11636   if (targetm.const_not_ok_for_debug_p (rtl))
11637     {
11638       expansion_failed (NULL_TREE, rtl,
11639                         "Expression rejected for debug by the backend.\n");
11640       return false;
11641     }
11642
11643   /* FIXME: Refer to PR60655. It is possible for simplification
11644      of rtl expressions in var tracking to produce such expressions.
11645      We should really identify / validate expressions
11646      enclosed in CONST that can be handled by assemblers on various
11647      targets and only handle legitimate cases here.  */
11648   if (GET_CODE (rtl) != SYMBOL_REF)
11649     {
11650       if (GET_CODE (rtl) == NOT)
11651         return false;
11652       return true;
11653     }
11654
11655   if (CONSTANT_POOL_ADDRESS_P (rtl))
11656     {
11657       bool marked;
11658       get_pool_constant_mark (rtl, &marked);
11659       /* If all references to this pool constant were optimized away,
11660          it was not output and thus we can't represent it.  */
11661       if (!marked)
11662         {
11663           expansion_failed (NULL_TREE, rtl,
11664                             "Constant was removed from constant pool.\n");
11665           return false;
11666         }
11667     }
11668
11669   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11670     return false;
11671
11672   /* Avoid references to external symbols in debug info, on several targets
11673      the linker might even refuse to link when linking a shared library,
11674      and in many other cases the relocations for .debug_info/.debug_loc are
11675      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
11676      to be defined within the same shared library or executable are fine.  */
11677   if (SYMBOL_REF_EXTERNAL_P (rtl))
11678     {
11679       tree decl = SYMBOL_REF_DECL (rtl);
11680
11681       if (decl == NULL || !targetm.binds_local_p (decl))
11682         {
11683           expansion_failed (NULL_TREE, rtl,
11684                             "Symbol not defined in current TU.\n");
11685           return false;
11686         }
11687     }
11688
11689   return true;
11690 }
11691
11692 /* Return true if constant RTL can be emitted in DW_OP_addr or
11693    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
11694    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
11695
11696 static bool
11697 const_ok_for_output (rtx rtl)
11698 {
11699   if (GET_CODE (rtl) == SYMBOL_REF)
11700     return const_ok_for_output_1 (rtl);
11701
11702   if (GET_CODE (rtl) == CONST)
11703     {
11704       subrtx_var_iterator::array_type array;
11705       FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
11706         if (!const_ok_for_output_1 (*iter))
11707           return false;
11708       return true;
11709     }
11710
11711   return true;
11712 }
11713
11714 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11715    if possible, NULL otherwise.  */
11716
11717 static dw_die_ref
11718 base_type_for_mode (machine_mode mode, bool unsignedp)
11719 {
11720   dw_die_ref type_die;
11721   tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11722
11723   if (type == NULL)
11724     return NULL;
11725   switch (TREE_CODE (type))
11726     {
11727     case INTEGER_TYPE:
11728     case REAL_TYPE:
11729       break;
11730     default:
11731       return NULL;
11732     }
11733   type_die = lookup_type_die (type);
11734   if (!type_die)
11735     type_die = modified_type_die (type, TYPE_UNQUALIFIED, comp_unit_die ());
11736   if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11737     return NULL;
11738   return type_die;
11739 }
11740
11741 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11742    type matching MODE, or, if MODE is narrower than or as wide as
11743    DWARF2_ADDR_SIZE, untyped.  Return NULL if the conversion is not
11744    possible.  */
11745
11746 static dw_loc_descr_ref
11747 convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
11748 {
11749   machine_mode outer_mode = mode;
11750   dw_die_ref type_die;
11751   dw_loc_descr_ref cvt;
11752
11753   if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11754     {
11755       add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11756       return op;
11757     }
11758   type_die = base_type_for_mode (outer_mode, 1);
11759   if (type_die == NULL)
11760     return NULL;
11761   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11762   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11763   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11764   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11765   add_loc_descr (&op, cvt);
11766   return op;
11767 }
11768
11769 /* Return location descriptor for comparison OP with operands OP0 and OP1.  */
11770
11771 static dw_loc_descr_ref
11772 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11773                         dw_loc_descr_ref op1)
11774 {
11775   dw_loc_descr_ref ret = op0;
11776   add_loc_descr (&ret, op1);
11777   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11778   if (STORE_FLAG_VALUE != 1)
11779     {
11780       add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11781       add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11782     }
11783   return ret;
11784 }
11785
11786 /* Return location descriptor for signed comparison OP RTL.  */
11787
11788 static dw_loc_descr_ref
11789 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11790                          machine_mode mem_mode)
11791 {
11792   machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11793   dw_loc_descr_ref op0, op1;
11794   int shift;
11795
11796   if (op_mode == VOIDmode)
11797     op_mode = GET_MODE (XEXP (rtl, 1));
11798   if (op_mode == VOIDmode)
11799     return NULL;
11800
11801   if (dwarf_strict
11802       && (GET_MODE_CLASS (op_mode) != MODE_INT
11803           || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11804     return NULL;
11805
11806   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11807                             VAR_INIT_STATUS_INITIALIZED);
11808   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11809                             VAR_INIT_STATUS_INITIALIZED);
11810
11811   if (op0 == NULL || op1 == NULL)
11812     return NULL;
11813
11814   if (GET_MODE_CLASS (op_mode) != MODE_INT
11815       || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11816     return compare_loc_descriptor (op, op0, op1);
11817
11818   if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11819     {
11820       dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11821       dw_loc_descr_ref cvt;
11822
11823       if (type_die == NULL)
11824         return NULL;
11825       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11826       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11827       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11828       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11829       add_loc_descr (&op0, cvt);
11830       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11831       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11832       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11833       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11834       add_loc_descr (&op1, cvt);
11835       return compare_loc_descriptor (op, op0, op1);
11836     }
11837
11838   shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11839   /* For eq/ne, if the operands are known to be zero-extended,
11840      there is no need to do the fancy shifting up.  */
11841   if (op == DW_OP_eq || op == DW_OP_ne)
11842     {
11843       dw_loc_descr_ref last0, last1;
11844       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11845         ;
11846       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11847         ;
11848       /* deref_size zero extends, and for constants we can check
11849          whether they are zero extended or not.  */
11850       if (((last0->dw_loc_opc == DW_OP_deref_size
11851             && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11852            || (CONST_INT_P (XEXP (rtl, 0))
11853                && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11854                   == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11855           && ((last1->dw_loc_opc == DW_OP_deref_size
11856                && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11857               || (CONST_INT_P (XEXP (rtl, 1))
11858                   && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11859                      == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11860         return compare_loc_descriptor (op, op0, op1);
11861
11862       /* EQ/NE comparison against constant in narrower type than
11863          DWARF2_ADDR_SIZE can be performed either as
11864          DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11865          DW_OP_{eq,ne}
11866          or
11867          DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11868          DW_OP_{eq,ne}.  Pick whatever is shorter.  */
11869       if (CONST_INT_P (XEXP (rtl, 1))
11870           && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11871           && (size_of_int_loc_descriptor (shift) + 1
11872               + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11873               >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11874                  + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11875                                                & GET_MODE_MASK (op_mode))))
11876         {
11877           add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11878           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11879           op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11880                                     & GET_MODE_MASK (op_mode));
11881           return compare_loc_descriptor (op, op0, op1);
11882         }
11883     }
11884   add_loc_descr (&op0, int_loc_descriptor (shift));
11885   add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11886   if (CONST_INT_P (XEXP (rtl, 1)))
11887     op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11888   else
11889     {
11890       add_loc_descr (&op1, int_loc_descriptor (shift));
11891       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11892     }
11893   return compare_loc_descriptor (op, op0, op1);
11894 }
11895
11896 /* Return location descriptor for unsigned comparison OP RTL.  */
11897
11898 static dw_loc_descr_ref
11899 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11900                          machine_mode mem_mode)
11901 {
11902   machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11903   dw_loc_descr_ref op0, op1;
11904
11905   if (op_mode == VOIDmode)
11906     op_mode = GET_MODE (XEXP (rtl, 1));
11907   if (op_mode == VOIDmode)
11908     return NULL;
11909   if (GET_MODE_CLASS (op_mode) != MODE_INT)
11910     return NULL;
11911
11912   if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11913     return NULL;
11914
11915   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11916                             VAR_INIT_STATUS_INITIALIZED);
11917   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11918                             VAR_INIT_STATUS_INITIALIZED);
11919
11920   if (op0 == NULL || op1 == NULL)
11921     return NULL;
11922
11923   if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
11924     {
11925       HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
11926       dw_loc_descr_ref last0, last1;
11927       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11928         ;
11929       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11930         ;
11931       if (CONST_INT_P (XEXP (rtl, 0)))
11932         op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
11933       /* deref_size zero extends, so no need to mask it again.  */
11934       else if (last0->dw_loc_opc != DW_OP_deref_size
11935                || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11936         {
11937           add_loc_descr (&op0, int_loc_descriptor (mask));
11938           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11939         }
11940       if (CONST_INT_P (XEXP (rtl, 1)))
11941         op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11942       /* deref_size zero extends, so no need to mask it again.  */
11943       else if (last1->dw_loc_opc != DW_OP_deref_size
11944                || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11945         {
11946           add_loc_descr (&op1, int_loc_descriptor (mask));
11947           add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11948         }
11949     }
11950   else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11951     {
11952       HOST_WIDE_INT bias = 1;
11953       bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11954       add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11955       if (CONST_INT_P (XEXP (rtl, 1)))
11956         op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11957                                   + INTVAL (XEXP (rtl, 1)));
11958       else
11959         add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
11960                                             bias, 0));
11961     }
11962   return compare_loc_descriptor (op, op0, op1);
11963 }
11964
11965 /* Return location descriptor for {U,S}{MIN,MAX}.  */
11966
11967 static dw_loc_descr_ref
11968 minmax_loc_descriptor (rtx rtl, machine_mode mode,
11969                        machine_mode mem_mode)
11970 {
11971   enum dwarf_location_atom op;
11972   dw_loc_descr_ref op0, op1, ret;
11973   dw_loc_descr_ref bra_node, drop_node;
11974
11975   if (dwarf_strict
11976       && (GET_MODE_CLASS (mode) != MODE_INT
11977           || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
11978     return NULL;
11979
11980   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11981                             VAR_INIT_STATUS_INITIALIZED);
11982   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11983                             VAR_INIT_STATUS_INITIALIZED);
11984
11985   if (op0 == NULL || op1 == NULL)
11986     return NULL;
11987
11988   add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11989   add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11990   add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11991   if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
11992     {
11993       if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11994         {
11995           HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11996           add_loc_descr (&op0, int_loc_descriptor (mask));
11997           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11998           add_loc_descr (&op1, int_loc_descriptor (mask));
11999           add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12000         }
12001       else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12002         {
12003           HOST_WIDE_INT bias = 1;
12004           bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12005           add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12006           add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12007         }
12008     }
12009   else if (GET_MODE_CLASS (mode) == MODE_INT
12010            && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12011     {
12012       int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
12013       add_loc_descr (&op0, int_loc_descriptor (shift));
12014       add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
12015       add_loc_descr (&op1, int_loc_descriptor (shift));
12016       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
12017     }
12018   else if (GET_MODE_CLASS (mode) == MODE_INT
12019            && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12020     {
12021       dw_die_ref type_die = base_type_for_mode (mode, 0);
12022       dw_loc_descr_ref cvt;
12023       if (type_die == NULL)
12024         return NULL;
12025       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12026       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12027       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12028       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12029       add_loc_descr (&op0, cvt);
12030       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12031       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12032       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12033       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12034       add_loc_descr (&op1, cvt);
12035     }
12036
12037   if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
12038     op = DW_OP_lt;
12039   else
12040     op = DW_OP_gt;
12041   ret = op0;
12042   add_loc_descr (&ret, op1);
12043   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12044   bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12045   add_loc_descr (&ret, bra_node);
12046   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12047   drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12048   add_loc_descr (&ret, drop_node);
12049   bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12050   bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12051   if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
12052       && GET_MODE_CLASS (mode) == MODE_INT
12053       && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12054     ret = convert_descriptor_to_mode (mode, ret);
12055   return ret;
12056 }
12057
12058 /* Helper function for mem_loc_descriptor.  Perform OP binary op,
12059    but after converting arguments to type_die, afterwards
12060    convert back to unsigned.  */
12061
12062 static dw_loc_descr_ref
12063 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
12064              machine_mode mode, machine_mode mem_mode)
12065 {
12066   dw_loc_descr_ref cvt, op0, op1;
12067
12068   if (type_die == NULL)
12069     return NULL;
12070   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12071                             VAR_INIT_STATUS_INITIALIZED);
12072   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12073                             VAR_INIT_STATUS_INITIALIZED);
12074   if (op0 == NULL || op1 == NULL)
12075     return NULL;
12076   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12077   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12078   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12079   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12080   add_loc_descr (&op0, cvt);
12081   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12082   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12083   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12084   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12085   add_loc_descr (&op1, cvt);
12086   add_loc_descr (&op0, op1);
12087   add_loc_descr (&op0, new_loc_descr (op, 0, 0));
12088   return convert_descriptor_to_mode (mode, op0);
12089 }
12090
12091 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
12092    const0 is DW_OP_lit0 or corresponding typed constant,
12093    const1 is DW_OP_lit1 or corresponding typed constant
12094    and constMSB is constant with just the MSB bit set
12095    for the mode):
12096        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12097    L1: const0 DW_OP_swap
12098    L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
12099        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12100    L3: DW_OP_drop
12101    L4: DW_OP_nop
12102
12103    CTZ is similar:
12104        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12105    L1: const0 DW_OP_swap
12106    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12107        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12108    L3: DW_OP_drop
12109    L4: DW_OP_nop
12110
12111    FFS is similar:
12112        DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
12113    L1: const1 DW_OP_swap
12114    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12115        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12116    L3: DW_OP_drop
12117    L4: DW_OP_nop  */
12118
12119 static dw_loc_descr_ref
12120 clz_loc_descriptor (rtx rtl, machine_mode mode,
12121                     machine_mode mem_mode)
12122 {
12123   dw_loc_descr_ref op0, ret, tmp;
12124   HOST_WIDE_INT valv;
12125   dw_loc_descr_ref l1jump, l1label;
12126   dw_loc_descr_ref l2jump, l2label;
12127   dw_loc_descr_ref l3jump, l3label;
12128   dw_loc_descr_ref l4jump, l4label;
12129   rtx msb;
12130
12131   if (GET_MODE_CLASS (mode) != MODE_INT
12132       || GET_MODE (XEXP (rtl, 0)) != mode)
12133     return NULL;
12134
12135   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12136                             VAR_INIT_STATUS_INITIALIZED);
12137   if (op0 == NULL)
12138     return NULL;
12139   ret = op0;
12140   if (GET_CODE (rtl) == CLZ)
12141     {
12142       if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12143         valv = GET_MODE_BITSIZE (mode);
12144     }
12145   else if (GET_CODE (rtl) == FFS)
12146     valv = 0;
12147   else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12148     valv = GET_MODE_BITSIZE (mode);
12149   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12150   l1jump = new_loc_descr (DW_OP_bra, 0, 0);
12151   add_loc_descr (&ret, l1jump);
12152   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12153   tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
12154                             VAR_INIT_STATUS_INITIALIZED);
12155   if (tmp == NULL)
12156     return NULL;
12157   add_loc_descr (&ret, tmp);
12158   l4jump = new_loc_descr (DW_OP_skip, 0, 0);
12159   add_loc_descr (&ret, l4jump);
12160   l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
12161                                 ? const1_rtx : const0_rtx,
12162                                 mode, mem_mode,
12163                                 VAR_INIT_STATUS_INITIALIZED);
12164   if (l1label == NULL)
12165     return NULL;
12166   add_loc_descr (&ret, l1label);
12167   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12168   l2label = new_loc_descr (DW_OP_dup, 0, 0);
12169   add_loc_descr (&ret, l2label);
12170   if (GET_CODE (rtl) != CLZ)
12171     msb = const1_rtx;
12172   else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
12173     msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
12174                    << (GET_MODE_BITSIZE (mode) - 1));
12175   else
12176     msb = immed_wide_int_const
12177       (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
12178                             GET_MODE_PRECISION (mode)), mode);
12179   if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
12180     tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12181                          ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
12182                          ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
12183   else
12184     tmp = mem_loc_descriptor (msb, mode, mem_mode,
12185                               VAR_INIT_STATUS_INITIALIZED);
12186   if (tmp == NULL)
12187     return NULL;
12188   add_loc_descr (&ret, tmp);
12189   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12190   l3jump = new_loc_descr (DW_OP_bra, 0, 0);
12191   add_loc_descr (&ret, l3jump);
12192   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12193                             VAR_INIT_STATUS_INITIALIZED);
12194   if (tmp == NULL)
12195     return NULL;
12196   add_loc_descr (&ret, tmp);
12197   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
12198                                       ? DW_OP_shl : DW_OP_shr, 0, 0));
12199   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12200   add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
12201   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12202   l2jump = new_loc_descr (DW_OP_skip, 0, 0);
12203   add_loc_descr (&ret, l2jump);
12204   l3label = new_loc_descr (DW_OP_drop, 0, 0);
12205   add_loc_descr (&ret, l3label);
12206   l4label = new_loc_descr (DW_OP_nop, 0, 0);
12207   add_loc_descr (&ret, l4label);
12208   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12209   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12210   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12211   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12212   l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12213   l3jump->dw_loc_oprnd1.v.val_loc = l3label;
12214   l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12215   l4jump->dw_loc_oprnd1.v.val_loc = l4label;
12216   return ret;
12217 }
12218
12219 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
12220    const1 is DW_OP_lit1 or corresponding typed constant):
12221        const0 DW_OP_swap
12222    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12223        DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12224    L2: DW_OP_drop
12225
12226    PARITY is similar:
12227    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12228        DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12229    L2: DW_OP_drop  */
12230
12231 static dw_loc_descr_ref
12232 popcount_loc_descriptor (rtx rtl, machine_mode mode,
12233                          machine_mode mem_mode)
12234 {
12235   dw_loc_descr_ref op0, ret, tmp;
12236   dw_loc_descr_ref l1jump, l1label;
12237   dw_loc_descr_ref l2jump, l2label;
12238
12239   if (GET_MODE_CLASS (mode) != MODE_INT
12240       || GET_MODE (XEXP (rtl, 0)) != mode)
12241     return NULL;
12242
12243   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12244                             VAR_INIT_STATUS_INITIALIZED);
12245   if (op0 == NULL)
12246     return NULL;
12247   ret = op0;
12248   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12249                             VAR_INIT_STATUS_INITIALIZED);
12250   if (tmp == NULL)
12251     return NULL;
12252   add_loc_descr (&ret, tmp);
12253   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12254   l1label = new_loc_descr (DW_OP_dup, 0, 0);
12255   add_loc_descr (&ret, l1label);
12256   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12257   add_loc_descr (&ret, l2jump);
12258   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12259   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12260   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12261                             VAR_INIT_STATUS_INITIALIZED);
12262   if (tmp == NULL)
12263     return NULL;
12264   add_loc_descr (&ret, tmp);
12265   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12266   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
12267                                       ? DW_OP_plus : DW_OP_xor, 0, 0));
12268   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12269   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12270                             VAR_INIT_STATUS_INITIALIZED);
12271   add_loc_descr (&ret, tmp);
12272   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12273   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12274   add_loc_descr (&ret, l1jump);
12275   l2label = new_loc_descr (DW_OP_drop, 0, 0);
12276   add_loc_descr (&ret, l2label);
12277   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12278   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12279   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12280   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12281   return ret;
12282 }
12283
12284 /* BSWAP (constS is initial shift count, either 56 or 24):
12285        constS const0
12286    L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
12287        const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
12288        DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
12289        DW_OP_minus DW_OP_swap DW_OP_skip <L1>
12290    L2: DW_OP_drop DW_OP_swap DW_OP_drop  */
12291
12292 static dw_loc_descr_ref
12293 bswap_loc_descriptor (rtx rtl, machine_mode mode,
12294                       machine_mode mem_mode)
12295 {
12296   dw_loc_descr_ref op0, ret, tmp;
12297   dw_loc_descr_ref l1jump, l1label;
12298   dw_loc_descr_ref l2jump, l2label;
12299
12300   if (GET_MODE_CLASS (mode) != MODE_INT
12301       || BITS_PER_UNIT != 8
12302       || (GET_MODE_BITSIZE (mode) != 32
12303           &&  GET_MODE_BITSIZE (mode) != 64))
12304     return NULL;
12305
12306   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12307                             VAR_INIT_STATUS_INITIALIZED);
12308   if (op0 == NULL)
12309     return NULL;
12310
12311   ret = op0;
12312   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12313                             mode, mem_mode,
12314                             VAR_INIT_STATUS_INITIALIZED);
12315   if (tmp == NULL)
12316     return NULL;
12317   add_loc_descr (&ret, tmp);
12318   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12319                             VAR_INIT_STATUS_INITIALIZED);
12320   if (tmp == NULL)
12321     return NULL;
12322   add_loc_descr (&ret, tmp);
12323   l1label = new_loc_descr (DW_OP_pick, 2, 0);
12324   add_loc_descr (&ret, l1label);
12325   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12326                             mode, mem_mode,
12327                             VAR_INIT_STATUS_INITIALIZED);
12328   add_loc_descr (&ret, tmp);
12329   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
12330   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12331   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12332   tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
12333                             VAR_INIT_STATUS_INITIALIZED);
12334   if (tmp == NULL)
12335     return NULL;
12336   add_loc_descr (&ret, tmp);
12337   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12338   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
12339   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12340   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12341   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12342   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12343   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12344                             VAR_INIT_STATUS_INITIALIZED);
12345   add_loc_descr (&ret, tmp);
12346   add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
12347   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12348   add_loc_descr (&ret, l2jump);
12349   tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
12350                             VAR_INIT_STATUS_INITIALIZED);
12351   add_loc_descr (&ret, tmp);
12352   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12353   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12354   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12355   add_loc_descr (&ret, l1jump);
12356   l2label = new_loc_descr (DW_OP_drop, 0, 0);
12357   add_loc_descr (&ret, l2label);
12358   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12359   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12360   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12361   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12362   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12363   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12364   return ret;
12365 }
12366
12367 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
12368    DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12369    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
12370    DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
12371
12372    ROTATERT is similar:
12373    DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
12374    DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12375    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or  */
12376
12377 static dw_loc_descr_ref
12378 rotate_loc_descriptor (rtx rtl, machine_mode mode,
12379                        machine_mode mem_mode)
12380 {
12381   rtx rtlop1 = XEXP (rtl, 1);
12382   dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
12383   int i;
12384
12385   if (GET_MODE_CLASS (mode) != MODE_INT)
12386     return NULL;
12387
12388   if (GET_MODE (rtlop1) != VOIDmode
12389       && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
12390     rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12391   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12392                             VAR_INIT_STATUS_INITIALIZED);
12393   op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12394                             VAR_INIT_STATUS_INITIALIZED);
12395   if (op0 == NULL || op1 == NULL)
12396     return NULL;
12397   if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12398     for (i = 0; i < 2; i++)
12399       {
12400         if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
12401           mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
12402                                         mode, mem_mode,
12403                                         VAR_INIT_STATUS_INITIALIZED);
12404         else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12405           mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12406                                    ? DW_OP_const4u
12407                                    : HOST_BITS_PER_WIDE_INT == 64
12408                                    ? DW_OP_const8u : DW_OP_constu,
12409                                    GET_MODE_MASK (mode), 0);
12410         else
12411           mask[i] = NULL;
12412         if (mask[i] == NULL)
12413           return NULL;
12414         add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
12415       }
12416   ret = op0;
12417   add_loc_descr (&ret, op1);
12418   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12419   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12420   if (GET_CODE (rtl) == ROTATERT)
12421     {
12422       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12423       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12424                                           GET_MODE_BITSIZE (mode), 0));
12425     }
12426   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12427   if (mask[0] != NULL)
12428     add_loc_descr (&ret, mask[0]);
12429   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12430   if (mask[1] != NULL)
12431     {
12432       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12433       add_loc_descr (&ret, mask[1]);
12434       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12435     }
12436   if (GET_CODE (rtl) == ROTATE)
12437     {
12438       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12439       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12440                                           GET_MODE_BITSIZE (mode), 0));
12441     }
12442   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12443   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12444   return ret;
12445 }
12446
12447 /* Helper function for mem_loc_descriptor.  Return DW_OP_GNU_parameter_ref
12448    for DEBUG_PARAMETER_REF RTL.  */
12449
12450 static dw_loc_descr_ref
12451 parameter_ref_descriptor (rtx rtl)
12452 {
12453   dw_loc_descr_ref ret;
12454   dw_die_ref ref;
12455
12456   if (dwarf_strict)
12457     return NULL;
12458   gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
12459   ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
12460   ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
12461   if (ref)
12462     {
12463       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12464       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12465       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12466     }
12467   else
12468     {
12469       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12470       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
12471     }
12472   return ret;
12473 }
12474
12475 /* The following routine converts the RTL for a variable or parameter
12476    (resident in memory) into an equivalent Dwarf representation of a
12477    mechanism for getting the address of that same variable onto the top of a
12478    hypothetical "address evaluation" stack.
12479
12480    When creating memory location descriptors, we are effectively transforming
12481    the RTL for a memory-resident object into its Dwarf postfix expression
12482    equivalent.  This routine recursively descends an RTL tree, turning
12483    it into Dwarf postfix code as it goes.
12484
12485    MODE is the mode that should be assumed for the rtl if it is VOIDmode.
12486
12487    MEM_MODE is the mode of the memory reference, needed to handle some
12488    autoincrement addressing modes.
12489
12490    Return 0 if we can't represent the location.  */
12491
12492 dw_loc_descr_ref
12493 mem_loc_descriptor (rtx rtl, machine_mode mode,
12494                     machine_mode mem_mode,
12495                     enum var_init_status initialized)
12496 {
12497   dw_loc_descr_ref mem_loc_result = NULL;
12498   enum dwarf_location_atom op;
12499   dw_loc_descr_ref op0, op1;
12500   rtx inner = NULL_RTX;
12501
12502   if (mode == VOIDmode)
12503     mode = GET_MODE (rtl);
12504
12505   /* Note that for a dynamically sized array, the location we will generate a
12506      description of here will be the lowest numbered location which is
12507      actually within the array.  That's *not* necessarily the same as the
12508      zeroth element of the array.  */
12509
12510   rtl = targetm.delegitimize_address (rtl);
12511
12512   if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
12513     return NULL;
12514
12515   switch (GET_CODE (rtl))
12516     {
12517     case POST_INC:
12518     case POST_DEC:
12519     case POST_MODIFY:
12520       return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
12521
12522     case SUBREG:
12523       /* The case of a subreg may arise when we have a local (register)
12524          variable or a formal (register) parameter which doesn't quite fill
12525          up an entire register.  For now, just assume that it is
12526          legitimate to make the Dwarf info refer to the whole register which
12527          contains the given subreg.  */
12528       if (!subreg_lowpart_p (rtl))
12529         break;
12530       inner = SUBREG_REG (rtl);
12531     case TRUNCATE:
12532       if (inner == NULL_RTX)
12533         inner = XEXP (rtl, 0);
12534       if (GET_MODE_CLASS (mode) == MODE_INT
12535           && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
12536           && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12537 #ifdef POINTERS_EXTEND_UNSIGNED
12538               || (mode == Pmode && mem_mode != VOIDmode)
12539 #endif
12540              )
12541           && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
12542         {
12543           mem_loc_result = mem_loc_descriptor (inner,
12544                                                GET_MODE (inner),
12545                                                mem_mode, initialized);
12546           break;
12547         }
12548       if (dwarf_strict)
12549         break;
12550       if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
12551         break;
12552       if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
12553           && (GET_MODE_CLASS (mode) != MODE_INT
12554               || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
12555         break;
12556       else
12557         {
12558           dw_die_ref type_die;
12559           dw_loc_descr_ref cvt;
12560
12561           mem_loc_result = mem_loc_descriptor (inner,
12562                                                GET_MODE (inner),
12563                                                mem_mode, initialized);
12564           if (mem_loc_result == NULL)
12565             break;
12566           type_die = base_type_for_mode (mode,
12567                                          GET_MODE_CLASS (mode) == MODE_INT);
12568           if (type_die == NULL)
12569             {
12570               mem_loc_result = NULL;
12571               break;
12572             }
12573           if (GET_MODE_SIZE (mode)
12574               != GET_MODE_SIZE (GET_MODE (inner)))
12575             cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12576           else
12577             cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12578           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12579           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12580           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12581           add_loc_descr (&mem_loc_result, cvt);
12582         }
12583       break;
12584
12585     case REG:
12586       if (GET_MODE_CLASS (mode) != MODE_INT
12587           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12588               && rtl != arg_pointer_rtx
12589               && rtl != frame_pointer_rtx
12590 #ifdef POINTERS_EXTEND_UNSIGNED
12591               && (mode != Pmode || mem_mode == VOIDmode)
12592 #endif
12593               ))
12594         {
12595           dw_die_ref type_die;
12596           unsigned int dbx_regnum;
12597
12598           if (dwarf_strict)
12599             break;
12600           if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12601             break;
12602           type_die = base_type_for_mode (mode,
12603                                          GET_MODE_CLASS (mode) == MODE_INT);
12604           if (type_die == NULL)
12605             break;
12606
12607           dbx_regnum = dbx_reg_number (rtl);
12608           if (dbx_regnum == IGNORED_DWARF_REGNUM)
12609             break;
12610           mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12611                                           dbx_regnum, 0);
12612           mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12613           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12614           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12615           break;
12616         }
12617       /* Whenever a register number forms a part of the description of the
12618          method for calculating the (dynamic) address of a memory resident
12619          object, DWARF rules require the register number be referred to as
12620          a "base register".  This distinction is not based in any way upon
12621          what category of register the hardware believes the given register
12622          belongs to.  This is strictly DWARF terminology we're dealing with
12623          here. Note that in cases where the location of a memory-resident
12624          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12625          OP_CONST (0)) the actual DWARF location descriptor that we generate
12626          may just be OP_BASEREG (basereg).  This may look deceptively like
12627          the object in question was allocated to a register (rather than in
12628          memory) so DWARF consumers need to be aware of the subtle
12629          distinction between OP_REG and OP_BASEREG.  */
12630       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12631         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12632       else if (stack_realign_drap
12633                && crtl->drap_reg
12634                && crtl->args.internal_arg_pointer == rtl
12635                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12636         {
12637           /* If RTL is internal_arg_pointer, which has been optimized
12638              out, use DRAP instead.  */
12639           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12640                                             VAR_INIT_STATUS_INITIALIZED);
12641         }
12642       break;
12643
12644     case SIGN_EXTEND:
12645     case ZERO_EXTEND:
12646       if (GET_MODE_CLASS (mode) != MODE_INT)
12647         break;
12648       op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12649                                 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12650       if (op0 == 0)
12651         break;
12652       else if (GET_CODE (rtl) == ZERO_EXTEND
12653                && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12654                && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12655                   < HOST_BITS_PER_WIDE_INT
12656                /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12657                   to expand zero extend as two shifts instead of
12658                   masking.  */
12659                && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12660         {
12661           machine_mode imode = GET_MODE (XEXP (rtl, 0));
12662           mem_loc_result = op0;
12663           add_loc_descr (&mem_loc_result,
12664                          int_loc_descriptor (GET_MODE_MASK (imode)));
12665           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12666         }
12667       else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12668         {
12669           int shift = DWARF2_ADDR_SIZE
12670                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12671           shift *= BITS_PER_UNIT;
12672           if (GET_CODE (rtl) == SIGN_EXTEND)
12673             op = DW_OP_shra;
12674           else
12675             op = DW_OP_shr;
12676           mem_loc_result = op0;
12677           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12678           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12679           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12680           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12681         }
12682       else if (!dwarf_strict)
12683         {
12684           dw_die_ref type_die1, type_die2;
12685           dw_loc_descr_ref cvt;
12686
12687           type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12688                                           GET_CODE (rtl) == ZERO_EXTEND);
12689           if (type_die1 == NULL)
12690             break;
12691           type_die2 = base_type_for_mode (mode, 1);
12692           if (type_die2 == NULL)
12693             break;
12694           mem_loc_result = op0;
12695           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12696           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12697           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12698           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12699           add_loc_descr (&mem_loc_result, cvt);
12700           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12701           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12702           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12703           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12704           add_loc_descr (&mem_loc_result, cvt);
12705         }
12706       break;
12707
12708     case MEM:
12709       {
12710         rtx new_rtl = avoid_constant_pool_reference (rtl);
12711         if (new_rtl != rtl)
12712           {
12713             mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12714                                                  initialized);
12715             if (mem_loc_result != NULL)
12716               return mem_loc_result;
12717           }
12718       }
12719       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12720                                            get_address_mode (rtl), mode,
12721                                            VAR_INIT_STATUS_INITIALIZED);
12722       if (mem_loc_result == NULL)
12723         mem_loc_result = tls_mem_loc_descriptor (rtl);
12724       if (mem_loc_result != NULL)
12725         {
12726           if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12727               || GET_MODE_CLASS (mode) != MODE_INT)
12728             {
12729               dw_die_ref type_die;
12730               dw_loc_descr_ref deref;
12731
12732               if (dwarf_strict)
12733                 return NULL;
12734               type_die
12735                 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12736               if (type_die == NULL)
12737                 return NULL;
12738               deref = new_loc_descr (DW_OP_GNU_deref_type,
12739                                      GET_MODE_SIZE (mode), 0);
12740               deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12741               deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12742               deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12743               add_loc_descr (&mem_loc_result, deref);
12744             }
12745           else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12746             add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12747           else
12748             add_loc_descr (&mem_loc_result,
12749                            new_loc_descr (DW_OP_deref_size,
12750                                           GET_MODE_SIZE (mode), 0));
12751         }
12752       break;
12753
12754     case LO_SUM:
12755       return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12756
12757     case LABEL_REF:
12758       /* Some ports can transform a symbol ref into a label ref, because
12759          the symbol ref is too far away and has to be dumped into a constant
12760          pool.  */
12761     case CONST:
12762     case SYMBOL_REF:
12763       if ((GET_MODE_CLASS (mode) != MODE_INT
12764            && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
12765           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12766 #ifdef POINTERS_EXTEND_UNSIGNED
12767               && (mode != Pmode || mem_mode == VOIDmode)
12768 #endif
12769               ))
12770         break;
12771       if (GET_CODE (rtl) == SYMBOL_REF
12772           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12773         {
12774           dw_loc_descr_ref temp;
12775
12776           /* If this is not defined, we have no way to emit the data.  */
12777           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12778             break;
12779
12780           temp = new_addr_loc_descr (rtl, dtprel_true);
12781
12782           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12783           add_loc_descr (&mem_loc_result, temp);
12784
12785           break;
12786         }
12787
12788       if (!const_ok_for_output (rtl))
12789         break;
12790
12791     symref:
12792       mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12793       vec_safe_push (used_rtx_array, rtl);
12794       break;
12795
12796     case CONCAT:
12797     case CONCATN:
12798     case VAR_LOCATION:
12799     case DEBUG_IMPLICIT_PTR:
12800       expansion_failed (NULL_TREE, rtl,
12801                         "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12802       return 0;
12803
12804     case ENTRY_VALUE:
12805       if (dwarf_strict)
12806         return NULL;
12807       if (REG_P (ENTRY_VALUE_EXP (rtl)))
12808         {
12809           if (GET_MODE_CLASS (mode) != MODE_INT
12810               || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12811             op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12812                                       VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12813           else
12814             {
12815               unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12816               if (dbx_regnum == IGNORED_DWARF_REGNUM)
12817                 return NULL;
12818               op0 = one_reg_loc_descriptor (dbx_regnum,
12819                                             VAR_INIT_STATUS_INITIALIZED);
12820             }
12821         }
12822       else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12823                && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12824         {
12825           op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12826                                     VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12827           if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12828             return NULL;
12829         }
12830       else
12831         gcc_unreachable ();
12832       if (op0 == NULL)
12833         return NULL;
12834       mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12835       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12836       mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12837       break;
12838
12839     case DEBUG_PARAMETER_REF:
12840       mem_loc_result = parameter_ref_descriptor (rtl);
12841       break;
12842
12843     case PRE_MODIFY:
12844       /* Extract the PLUS expression nested inside and fall into
12845          PLUS code below.  */
12846       rtl = XEXP (rtl, 1);
12847       goto plus;
12848
12849     case PRE_INC:
12850     case PRE_DEC:
12851       /* Turn these into a PLUS expression and fall into the PLUS code
12852          below.  */
12853       rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12854                           gen_int_mode (GET_CODE (rtl) == PRE_INC
12855                                         ? GET_MODE_UNIT_SIZE (mem_mode)
12856                                         : -GET_MODE_UNIT_SIZE (mem_mode),
12857                                         mode));
12858
12859       /* ... fall through ...  */
12860
12861     case PLUS:
12862     plus:
12863       if (is_based_loc (rtl)
12864           && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12865               || XEXP (rtl, 0) == arg_pointer_rtx
12866               || XEXP (rtl, 0) == frame_pointer_rtx)
12867           && GET_MODE_CLASS (mode) == MODE_INT)
12868         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12869                                           INTVAL (XEXP (rtl, 1)),
12870                                           VAR_INIT_STATUS_INITIALIZED);
12871       else
12872         {
12873           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12874                                                VAR_INIT_STATUS_INITIALIZED);
12875           if (mem_loc_result == 0)
12876             break;
12877
12878           if (CONST_INT_P (XEXP (rtl, 1))
12879               && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12880             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12881           else
12882             {
12883               op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12884                                         VAR_INIT_STATUS_INITIALIZED);
12885               if (op1 == 0)
12886                 return NULL;
12887               add_loc_descr (&mem_loc_result, op1);
12888               add_loc_descr (&mem_loc_result,
12889                              new_loc_descr (DW_OP_plus, 0, 0));
12890             }
12891         }
12892       break;
12893
12894     /* If a pseudo-reg is optimized away, it is possible for it to
12895        be replaced with a MEM containing a multiply or shift.  */
12896     case MINUS:
12897       op = DW_OP_minus;
12898       goto do_binop;
12899
12900     case MULT:
12901       op = DW_OP_mul;
12902       goto do_binop;
12903
12904     case DIV:
12905       if (!dwarf_strict
12906           && GET_MODE_CLASS (mode) == MODE_INT
12907           && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12908         {
12909           mem_loc_result = typed_binop (DW_OP_div, rtl,
12910                                         base_type_for_mode (mode, 0),
12911                                         mode, mem_mode);
12912           break;
12913         }
12914       op = DW_OP_div;
12915       goto do_binop;
12916
12917     case UMOD:
12918       op = DW_OP_mod;
12919       goto do_binop;
12920
12921     case ASHIFT:
12922       op = DW_OP_shl;
12923       goto do_shift;
12924
12925     case ASHIFTRT:
12926       op = DW_OP_shra;
12927       goto do_shift;
12928
12929     case LSHIFTRT:
12930       op = DW_OP_shr;
12931       goto do_shift;
12932
12933     do_shift:
12934       if (GET_MODE_CLASS (mode) != MODE_INT)
12935         break;
12936       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12937                                 VAR_INIT_STATUS_INITIALIZED);
12938       {
12939         rtx rtlop1 = XEXP (rtl, 1);
12940         if (GET_MODE (rtlop1) != VOIDmode
12941             && GET_MODE_BITSIZE (GET_MODE (rtlop1))
12942                < GET_MODE_BITSIZE (mode))
12943           rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12944         op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12945                                   VAR_INIT_STATUS_INITIALIZED);
12946       }
12947
12948       if (op0 == 0 || op1 == 0)
12949         break;
12950
12951       mem_loc_result = op0;
12952       add_loc_descr (&mem_loc_result, op1);
12953       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12954       break;
12955
12956     case AND:
12957       op = DW_OP_and;
12958       goto do_binop;
12959
12960     case IOR:
12961       op = DW_OP_or;
12962       goto do_binop;
12963
12964     case XOR:
12965       op = DW_OP_xor;
12966       goto do_binop;
12967
12968     do_binop:
12969       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12970                                 VAR_INIT_STATUS_INITIALIZED);
12971       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12972                                 VAR_INIT_STATUS_INITIALIZED);
12973
12974       if (op0 == 0 || op1 == 0)
12975         break;
12976
12977       mem_loc_result = op0;
12978       add_loc_descr (&mem_loc_result, op1);
12979       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12980       break;
12981
12982     case MOD:
12983       if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
12984         {
12985           mem_loc_result = typed_binop (DW_OP_mod, rtl,
12986                                         base_type_for_mode (mode, 0),
12987                                         mode, mem_mode);
12988           break;
12989         }
12990
12991       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12992                                 VAR_INIT_STATUS_INITIALIZED);
12993       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12994                                 VAR_INIT_STATUS_INITIALIZED);
12995
12996       if (op0 == 0 || op1 == 0)
12997         break;
12998
12999       mem_loc_result = op0;
13000       add_loc_descr (&mem_loc_result, op1);
13001       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13002       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13003       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
13004       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13005       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
13006       break;
13007
13008     case UDIV:
13009       if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
13010         {
13011           if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
13012             {
13013               op = DW_OP_div;
13014               goto do_binop;
13015             }
13016           mem_loc_result = typed_binop (DW_OP_div, rtl,
13017                                         base_type_for_mode (mode, 1),
13018                                         mode, mem_mode);
13019         }
13020       break;
13021
13022     case NOT:
13023       op = DW_OP_not;
13024       goto do_unop;
13025
13026     case ABS:
13027       op = DW_OP_abs;
13028       goto do_unop;
13029
13030     case NEG:
13031       op = DW_OP_neg;
13032       goto do_unop;
13033
13034     do_unop:
13035       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13036                                 VAR_INIT_STATUS_INITIALIZED);
13037
13038       if (op0 == 0)
13039         break;
13040
13041       mem_loc_result = op0;
13042       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13043       break;
13044
13045     case CONST_INT:
13046       if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13047 #ifdef POINTERS_EXTEND_UNSIGNED
13048           || (mode == Pmode
13049               && mem_mode != VOIDmode
13050               && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
13051 #endif
13052           )
13053         {
13054           mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13055           break;
13056         }
13057       if (!dwarf_strict
13058           && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
13059               || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
13060         {
13061           dw_die_ref type_die = base_type_for_mode (mode, 1);
13062           machine_mode amode;
13063           if (type_die == NULL)
13064             return NULL;
13065           amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
13066                                  MODE_INT, 0);
13067           if (INTVAL (rtl) >= 0
13068               && amode != BLKmode
13069               && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
13070               /* const DW_OP_GNU_convert <XXX> vs.
13071                  DW_OP_GNU_const_type <XXX, 1, const>.  */
13072               && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
13073                  < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
13074             {
13075               mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13076               op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13077               op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13078               op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13079               op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
13080               add_loc_descr (&mem_loc_result, op0);
13081               return mem_loc_result;
13082             }
13083           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
13084                                           INTVAL (rtl));
13085           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13086           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13087           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13088           if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
13089             mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13090           else
13091             {
13092               mem_loc_result->dw_loc_oprnd2.val_class
13093                 = dw_val_class_const_double;
13094               mem_loc_result->dw_loc_oprnd2.v.val_double
13095                 = double_int::from_shwi (INTVAL (rtl));
13096             }
13097         }
13098       break;
13099
13100     case CONST_DOUBLE:
13101       if (!dwarf_strict)
13102         {
13103           dw_die_ref type_die;
13104
13105           /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
13106              CONST_DOUBLE rtx could represent either a large integer
13107              or a floating-point constant.  If TARGET_SUPPORTS_WIDE_INT != 0,
13108              the value is always a floating point constant.
13109
13110              When it is an integer, a CONST_DOUBLE is used whenever
13111              the constant requires 2 HWIs to be adequately represented.
13112              We output CONST_DOUBLEs as blocks.  */
13113           if (mode == VOIDmode
13114               || (GET_MODE (rtl) == VOIDmode
13115                   && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
13116             break;
13117           type_die = base_type_for_mode (mode,
13118                                          GET_MODE_CLASS (mode) == MODE_INT);
13119           if (type_die == NULL)
13120             return NULL;
13121           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13122           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13123           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13124           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13125 #if TARGET_SUPPORTS_WIDE_INT == 0
13126           if (!SCALAR_FLOAT_MODE_P (mode))
13127             {
13128               mem_loc_result->dw_loc_oprnd2.val_class
13129                 = dw_val_class_const_double;
13130               mem_loc_result->dw_loc_oprnd2.v.val_double
13131                 = rtx_to_double_int (rtl);
13132             }
13133           else
13134 #endif
13135             {
13136               unsigned int length = GET_MODE_SIZE (mode);
13137               unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13138
13139               insert_float (rtl, array);
13140               mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13141               mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13142               mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13143               mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13144             }
13145         }
13146       break;
13147
13148     case CONST_WIDE_INT:
13149       if (!dwarf_strict)
13150         {
13151           dw_die_ref type_die;
13152
13153           type_die = base_type_for_mode (mode,
13154                                          GET_MODE_CLASS (mode) == MODE_INT);
13155           if (type_die == NULL)
13156             return NULL;
13157           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13158           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13159           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13160           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13161           mem_loc_result->dw_loc_oprnd2.val_class
13162             = dw_val_class_wide_int;
13163           mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_cleared_alloc<wide_int> ();
13164           *mem_loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13165         }
13166       break;
13167
13168     case EQ:
13169       mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
13170       break;
13171
13172     case GE:
13173       mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13174       break;
13175
13176     case GT:
13177       mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13178       break;
13179
13180     case LE:
13181       mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13182       break;
13183
13184     case LT:
13185       mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13186       break;
13187
13188     case NE:
13189       mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
13190       break;
13191
13192     case GEU:
13193       mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13194       break;
13195
13196     case GTU:
13197       mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13198       break;
13199
13200     case LEU:
13201       mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13202       break;
13203
13204     case LTU:
13205       mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13206       break;
13207
13208     case UMIN:
13209     case UMAX:
13210       if (GET_MODE_CLASS (mode) != MODE_INT)
13211         break;
13212       /* FALLTHRU */
13213     case SMIN:
13214     case SMAX:
13215       mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
13216       break;
13217
13218     case ZERO_EXTRACT:
13219     case SIGN_EXTRACT:
13220       if (CONST_INT_P (XEXP (rtl, 1))
13221           && CONST_INT_P (XEXP (rtl, 2))
13222           && ((unsigned) INTVAL (XEXP (rtl, 1))
13223               + (unsigned) INTVAL (XEXP (rtl, 2))
13224               <= GET_MODE_BITSIZE (mode))
13225           && GET_MODE_CLASS (mode) == MODE_INT
13226           && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13227           && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
13228         {
13229           int shift, size;
13230           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13231                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
13232           if (op0 == 0)
13233             break;
13234           if (GET_CODE (rtl) == SIGN_EXTRACT)
13235             op = DW_OP_shra;
13236           else
13237             op = DW_OP_shr;
13238           mem_loc_result = op0;
13239           size = INTVAL (XEXP (rtl, 1));
13240           shift = INTVAL (XEXP (rtl, 2));
13241           if (BITS_BIG_ENDIAN)
13242             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
13243                     - shift - size;
13244           if (shift + size != (int) DWARF2_ADDR_SIZE)
13245             {
13246               add_loc_descr (&mem_loc_result,
13247                              int_loc_descriptor (DWARF2_ADDR_SIZE
13248                                                  - shift - size));
13249               add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13250             }
13251           if (size != (int) DWARF2_ADDR_SIZE)
13252             {
13253               add_loc_descr (&mem_loc_result,
13254                              int_loc_descriptor (DWARF2_ADDR_SIZE - size));
13255               add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13256             }
13257         }
13258       break;
13259
13260     case IF_THEN_ELSE:
13261       {
13262         dw_loc_descr_ref op2, bra_node, drop_node;
13263         op0 = mem_loc_descriptor (XEXP (rtl, 0),
13264                                   GET_MODE (XEXP (rtl, 0)) == VOIDmode
13265                                   ? word_mode : GET_MODE (XEXP (rtl, 0)),
13266                                   mem_mode, VAR_INIT_STATUS_INITIALIZED);
13267         op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13268                                   VAR_INIT_STATUS_INITIALIZED);
13269         op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
13270                                   VAR_INIT_STATUS_INITIALIZED);
13271         if (op0 == NULL || op1 == NULL || op2 == NULL)
13272           break;
13273
13274         mem_loc_result = op1;
13275         add_loc_descr (&mem_loc_result, op2);
13276         add_loc_descr (&mem_loc_result, op0);
13277         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13278         add_loc_descr (&mem_loc_result, bra_node);
13279         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
13280         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
13281         add_loc_descr (&mem_loc_result, drop_node);
13282         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13283         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
13284       }
13285       break;
13286
13287     case FLOAT_EXTEND:
13288     case FLOAT_TRUNCATE:
13289     case FLOAT:
13290     case UNSIGNED_FLOAT:
13291     case FIX:
13292     case UNSIGNED_FIX:
13293       if (!dwarf_strict)
13294         {
13295           dw_die_ref type_die;
13296           dw_loc_descr_ref cvt;
13297
13298           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13299                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
13300           if (op0 == NULL)
13301             break;
13302           if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
13303               && (GET_CODE (rtl) == FLOAT
13304                   || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
13305                      <= DWARF2_ADDR_SIZE))
13306             {
13307               type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
13308                                              GET_CODE (rtl) == UNSIGNED_FLOAT);
13309               if (type_die == NULL)
13310                 break;
13311               cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13312               cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13313               cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13314               cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13315               add_loc_descr (&op0, cvt);
13316             }
13317           type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
13318           if (type_die == NULL)
13319             break;
13320           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13321           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13322           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13323           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13324           add_loc_descr (&op0, cvt);
13325           if (GET_MODE_CLASS (mode) == MODE_INT
13326               && (GET_CODE (rtl) == FIX
13327                   || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
13328             {
13329               op0 = convert_descriptor_to_mode (mode, op0);
13330               if (op0 == NULL)
13331                 break;
13332             }
13333           mem_loc_result = op0;
13334         }
13335       break;
13336
13337     case CLZ:
13338     case CTZ:
13339     case FFS:
13340       mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
13341       break;
13342
13343     case POPCOUNT:
13344     case PARITY:
13345       mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
13346       break;
13347
13348     case BSWAP:
13349       mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
13350       break;
13351
13352     case ROTATE:
13353     case ROTATERT:
13354       mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
13355       break;
13356
13357     case COMPARE:
13358       /* In theory, we could implement the above.  */
13359       /* DWARF cannot represent the unsigned compare operations
13360          natively.  */
13361     case SS_MULT:
13362     case US_MULT:
13363     case SS_DIV:
13364     case US_DIV:
13365     case SS_PLUS:
13366     case US_PLUS:
13367     case SS_MINUS:
13368     case US_MINUS:
13369     case SS_NEG:
13370     case US_NEG:
13371     case SS_ABS:
13372     case SS_ASHIFT:
13373     case US_ASHIFT:
13374     case SS_TRUNCATE:
13375     case US_TRUNCATE:
13376     case UNORDERED:
13377     case ORDERED:
13378     case UNEQ:
13379     case UNGE:
13380     case UNGT:
13381     case UNLE:
13382     case UNLT:
13383     case LTGT:
13384     case FRACT_CONVERT:
13385     case UNSIGNED_FRACT_CONVERT:
13386     case SAT_FRACT:
13387     case UNSIGNED_SAT_FRACT:
13388     case SQRT:
13389     case ASM_OPERANDS:
13390     case VEC_MERGE:
13391     case VEC_SELECT:
13392     case VEC_CONCAT:
13393     case VEC_DUPLICATE:
13394     case UNSPEC:
13395     case HIGH:
13396     case FMA:
13397     case STRICT_LOW_PART:
13398     case CONST_VECTOR:
13399     case CONST_FIXED:
13400     case CLRSB:
13401     case CLOBBER:
13402       /* If delegitimize_address couldn't do anything with the UNSPEC, we
13403          can't express it in the debug info.  This can happen e.g. with some
13404          TLS UNSPECs.  */
13405       break;
13406
13407     case CONST_STRING:
13408       resolve_one_addr (&rtl);
13409       goto symref;
13410
13411     default:
13412 #ifdef ENABLE_CHECKING
13413       print_rtl (stderr, rtl);
13414       gcc_unreachable ();
13415 #else
13416       break;
13417 #endif
13418     }
13419
13420   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13421     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13422
13423   return mem_loc_result;
13424 }
13425
13426 /* Return a descriptor that describes the concatenation of two locations.
13427    This is typically a complex variable.  */
13428
13429 static dw_loc_descr_ref
13430 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
13431 {
13432   dw_loc_descr_ref cc_loc_result = NULL;
13433   dw_loc_descr_ref x0_ref
13434     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13435   dw_loc_descr_ref x1_ref
13436     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13437
13438   if (x0_ref == 0 || x1_ref == 0)
13439     return 0;
13440
13441   cc_loc_result = x0_ref;
13442   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
13443
13444   add_loc_descr (&cc_loc_result, x1_ref);
13445   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
13446
13447   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13448     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13449
13450   return cc_loc_result;
13451 }
13452
13453 /* Return a descriptor that describes the concatenation of N
13454    locations.  */
13455
13456 static dw_loc_descr_ref
13457 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
13458 {
13459   unsigned int i;
13460   dw_loc_descr_ref cc_loc_result = NULL;
13461   unsigned int n = XVECLEN (concatn, 0);
13462
13463   for (i = 0; i < n; ++i)
13464     {
13465       dw_loc_descr_ref ref;
13466       rtx x = XVECEXP (concatn, 0, i);
13467
13468       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13469       if (ref == NULL)
13470         return NULL;
13471
13472       add_loc_descr (&cc_loc_result, ref);
13473       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
13474     }
13475
13476   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13477     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13478
13479   return cc_loc_result;
13480 }
13481
13482 /* Helper function for loc_descriptor.  Return DW_OP_GNU_implicit_pointer
13483    for DEBUG_IMPLICIT_PTR RTL.  */
13484
13485 static dw_loc_descr_ref
13486 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
13487 {
13488   dw_loc_descr_ref ret;
13489   dw_die_ref ref;
13490
13491   if (dwarf_strict)
13492     return NULL;
13493   gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
13494               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
13495               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
13496   ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
13497   ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
13498   ret->dw_loc_oprnd2.val_class = dw_val_class_const;
13499   if (ref)
13500     {
13501       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13502       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13503       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13504     }
13505   else
13506     {
13507       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13508       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
13509     }
13510   return ret;
13511 }
13512
13513 /* Output a proper Dwarf location descriptor for a variable or parameter
13514    which is either allocated in a register or in a memory location.  For a
13515    register, we just generate an OP_REG and the register number.  For a
13516    memory location we provide a Dwarf postfix expression describing how to
13517    generate the (dynamic) address of the object onto the address stack.
13518
13519    MODE is mode of the decl if this loc_descriptor is going to be used in
13520    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13521    allowed, VOIDmode otherwise.
13522
13523    If we don't know how to describe it, return 0.  */
13524
13525 static dw_loc_descr_ref
13526 loc_descriptor (rtx rtl, machine_mode mode,
13527                 enum var_init_status initialized)
13528 {
13529   dw_loc_descr_ref loc_result = NULL;
13530
13531   switch (GET_CODE (rtl))
13532     {
13533     case SUBREG:
13534       /* The case of a subreg may arise when we have a local (register)
13535          variable or a formal (register) parameter which doesn't quite fill
13536          up an entire register.  For now, just assume that it is
13537          legitimate to make the Dwarf info refer to the whole register which
13538          contains the given subreg.  */
13539       if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
13540         loc_result = loc_descriptor (SUBREG_REG (rtl),
13541                                      GET_MODE (SUBREG_REG (rtl)), initialized);
13542       else
13543         goto do_default;
13544       break;
13545
13546     case REG:
13547       loc_result = reg_loc_descriptor (rtl, initialized);
13548       break;
13549
13550     case MEM:
13551       loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13552                                        GET_MODE (rtl), initialized);
13553       if (loc_result == NULL)
13554         loc_result = tls_mem_loc_descriptor (rtl);
13555       if (loc_result == NULL)
13556         {
13557           rtx new_rtl = avoid_constant_pool_reference (rtl);
13558           if (new_rtl != rtl)
13559             loc_result = loc_descriptor (new_rtl, mode, initialized);
13560         }
13561       break;
13562
13563     case CONCAT:
13564       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13565                                           initialized);
13566       break;
13567
13568     case CONCATN:
13569       loc_result = concatn_loc_descriptor (rtl, initialized);
13570       break;
13571
13572     case VAR_LOCATION:
13573       /* Single part.  */
13574       if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
13575         {
13576           rtx loc = PAT_VAR_LOCATION_LOC (rtl);
13577           if (GET_CODE (loc) == EXPR_LIST)
13578             loc = XEXP (loc, 0);
13579           loc_result = loc_descriptor (loc, mode, initialized);
13580           break;
13581         }
13582
13583       rtl = XEXP (rtl, 1);
13584       /* FALLTHRU */
13585
13586     case PARALLEL:
13587       {
13588         rtvec par_elems = XVEC (rtl, 0);
13589         int num_elem = GET_NUM_ELEM (par_elems);
13590         machine_mode mode;
13591         int i;
13592
13593         /* Create the first one, so we have something to add to.  */
13594         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13595                                      VOIDmode, initialized);
13596         if (loc_result == NULL)
13597           return NULL;
13598         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13599         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13600         for (i = 1; i < num_elem; i++)
13601           {
13602             dw_loc_descr_ref temp;
13603
13604             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13605                                    VOIDmode, initialized);
13606             if (temp == NULL)
13607               return NULL;
13608             add_loc_descr (&loc_result, temp);
13609             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13610             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13611           }
13612       }
13613       break;
13614
13615     case CONST_INT:
13616       if (mode != VOIDmode && mode != BLKmode)
13617         loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13618                                                     INTVAL (rtl));
13619       break;
13620
13621     case CONST_DOUBLE:
13622       if (mode == VOIDmode)
13623         mode = GET_MODE (rtl);
13624
13625       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13626         {
13627           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13628
13629           /* Note that a CONST_DOUBLE rtx could represent either an integer
13630              or a floating-point constant.  A CONST_DOUBLE is used whenever
13631              the constant requires more than one word in order to be
13632              adequately represented.  We output CONST_DOUBLEs as blocks.  */
13633           loc_result = new_loc_descr (DW_OP_implicit_value,
13634                                       GET_MODE_SIZE (mode), 0);
13635 #if TARGET_SUPPORTS_WIDE_INT == 0
13636           if (!SCALAR_FLOAT_MODE_P (mode))
13637             {
13638               loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13639               loc_result->dw_loc_oprnd2.v.val_double
13640                 = rtx_to_double_int (rtl);
13641             }
13642           else
13643 #endif
13644             {
13645               unsigned int length = GET_MODE_SIZE (mode);
13646               unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13647
13648               insert_float (rtl, array);
13649               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13650               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13651               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13652               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13653             }
13654         }
13655       break;
13656
13657     case CONST_WIDE_INT:
13658       if (mode == VOIDmode)
13659         mode = GET_MODE (rtl);
13660
13661       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13662         {
13663           loc_result = new_loc_descr (DW_OP_implicit_value,
13664                                       GET_MODE_SIZE (mode), 0);
13665           loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
13666           loc_result->dw_loc_oprnd2.v.val_wide = ggc_cleared_alloc<wide_int> ();
13667           *loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13668         }
13669       break;
13670
13671     case CONST_VECTOR:
13672       if (mode == VOIDmode)
13673         mode = GET_MODE (rtl);
13674
13675       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13676         {
13677           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13678           unsigned int length = CONST_VECTOR_NUNITS (rtl);
13679           unsigned char *array
13680             = ggc_vec_alloc<unsigned char> (length * elt_size);
13681           unsigned int i;
13682           unsigned char *p;
13683           machine_mode imode = GET_MODE_INNER (mode);
13684
13685           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13686           switch (GET_MODE_CLASS (mode))
13687             {
13688             case MODE_VECTOR_INT:
13689               for (i = 0, p = array; i < length; i++, p += elt_size)
13690                 {
13691                   rtx elt = CONST_VECTOR_ELT (rtl, i);
13692                   insert_wide_int (std::make_pair (elt, imode), p, elt_size);
13693                 }
13694               break;
13695
13696             case MODE_VECTOR_FLOAT:
13697               for (i = 0, p = array; i < length; i++, p += elt_size)
13698                 {
13699                   rtx elt = CONST_VECTOR_ELT (rtl, i);
13700                   insert_float (elt, p);
13701                 }
13702               break;
13703
13704             default:
13705               gcc_unreachable ();
13706             }
13707
13708           loc_result = new_loc_descr (DW_OP_implicit_value,
13709                                       length * elt_size, 0);
13710           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13711           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13712           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13713           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13714         }
13715       break;
13716
13717     case CONST:
13718       if (mode == VOIDmode
13719           || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13720           || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13721           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13722         {
13723           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13724           break;
13725         }
13726       /* FALLTHROUGH */
13727     case SYMBOL_REF:
13728       if (!const_ok_for_output (rtl))
13729         break;
13730     case LABEL_REF:
13731       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13732           && (dwarf_version >= 4 || !dwarf_strict))
13733         {
13734          loc_result = new_addr_loc_descr (rtl, dtprel_false);
13735           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13736           vec_safe_push (used_rtx_array, rtl);
13737         }
13738       break;
13739
13740     case DEBUG_IMPLICIT_PTR:
13741       loc_result = implicit_ptr_descriptor (rtl, 0);
13742       break;
13743
13744     case PLUS:
13745       if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13746           && CONST_INT_P (XEXP (rtl, 1)))
13747         {
13748           loc_result
13749             = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13750           break;
13751         }
13752       /* FALLTHRU */
13753     do_default:
13754     default:
13755       if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13756            && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13757            && dwarf_version >= 4)
13758           || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13759         {
13760           /* Value expression.  */
13761           loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13762           if (loc_result)
13763             add_loc_descr (&loc_result,
13764                            new_loc_descr (DW_OP_stack_value, 0, 0));
13765         }
13766       break;
13767     }
13768
13769   return loc_result;
13770 }
13771
13772 /* We need to figure out what section we should use as the base for the
13773    address ranges where a given location is valid.
13774    1. If this particular DECL has a section associated with it, use that.
13775    2. If this function has a section associated with it, use that.
13776    3. Otherwise, use the text section.
13777    XXX: If you split a variable across multiple sections, we won't notice.  */
13778
13779 static const char *
13780 secname_for_decl (const_tree decl)
13781 {
13782   const char *secname;
13783
13784   if (VAR_OR_FUNCTION_DECL_P (decl)
13785       && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
13786       && DECL_SECTION_NAME (decl))
13787     secname = DECL_SECTION_NAME (decl);
13788   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13789     secname = DECL_SECTION_NAME (current_function_decl);
13790   else if (cfun && in_cold_section_p)
13791     secname = crtl->subsections.cold_section_label;
13792   else
13793     secname = text_section_label;
13794
13795   return secname;
13796 }
13797
13798 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
13799
13800 static bool
13801 decl_by_reference_p (tree decl)
13802 {
13803   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13804            || TREE_CODE (decl) == VAR_DECL)
13805           && DECL_BY_REFERENCE (decl));
13806 }
13807
13808 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
13809    for VARLOC.  */
13810
13811 static dw_loc_descr_ref
13812 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13813                enum var_init_status initialized)
13814 {
13815   int have_address = 0;
13816   dw_loc_descr_ref descr;
13817   machine_mode mode;
13818
13819   if (want_address != 2)
13820     {
13821       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13822       /* Single part.  */
13823       if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13824         {
13825           varloc = PAT_VAR_LOCATION_LOC (varloc);
13826           if (GET_CODE (varloc) == EXPR_LIST)
13827             varloc = XEXP (varloc, 0);
13828           mode = GET_MODE (varloc);
13829           if (MEM_P (varloc))
13830             {
13831               rtx addr = XEXP (varloc, 0);
13832               descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13833                                           mode, initialized);
13834               if (descr)
13835                 have_address = 1;
13836               else
13837                 {
13838                   rtx x = avoid_constant_pool_reference (varloc);
13839                   if (x != varloc)
13840                     descr = mem_loc_descriptor (x, mode, VOIDmode,
13841                                                 initialized);
13842                 }
13843             }
13844           else
13845             descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13846         }
13847       else
13848         return 0;
13849     }
13850   else
13851     {
13852       if (GET_CODE (varloc) == VAR_LOCATION)
13853         mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13854       else
13855         mode = DECL_MODE (loc);
13856       descr = loc_descriptor (varloc, mode, initialized);
13857       have_address = 1;
13858     }
13859
13860   if (!descr)
13861     return 0;
13862
13863   if (want_address == 2 && !have_address
13864       && (dwarf_version >= 4 || !dwarf_strict))
13865     {
13866       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13867         {
13868           expansion_failed (loc, NULL_RTX,
13869                             "DWARF address size mismatch");
13870           return 0;
13871         }
13872       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13873       have_address = 1;
13874     }
13875   /* Show if we can't fill the request for an address.  */
13876   if (want_address && !have_address)
13877     {
13878       expansion_failed (loc, NULL_RTX,
13879                         "Want address and only have value");
13880       return 0;
13881     }
13882
13883   /* If we've got an address and don't want one, dereference.  */
13884   if (!want_address && have_address)
13885     {
13886       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13887       enum dwarf_location_atom op;
13888
13889       if (size > DWARF2_ADDR_SIZE || size == -1)
13890         {
13891           expansion_failed (loc, NULL_RTX,
13892                             "DWARF address size mismatch");
13893           return 0;
13894         }
13895       else if (size == DWARF2_ADDR_SIZE)
13896         op = DW_OP_deref;
13897       else
13898         op = DW_OP_deref_size;
13899
13900       add_loc_descr (&descr, new_loc_descr (op, size, 0));
13901     }
13902
13903   return descr;
13904 }
13905
13906 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
13907    if it is not possible.  */
13908
13909 static dw_loc_descr_ref
13910 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
13911 {
13912   if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
13913     return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
13914   else if (dwarf_version >= 3 || !dwarf_strict)
13915     return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
13916   else
13917     return NULL;
13918 }
13919
13920 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
13921    for VAR_LOC_NOTE for variable DECL that has been optimized by SRA.  */
13922
13923 static dw_loc_descr_ref
13924 dw_sra_loc_expr (tree decl, rtx loc)
13925 {
13926   rtx p;
13927   unsigned HOST_WIDE_INT padsize = 0;
13928   dw_loc_descr_ref descr, *descr_tail;
13929   unsigned HOST_WIDE_INT decl_size;
13930   rtx varloc;
13931   enum var_init_status initialized;
13932
13933   if (DECL_SIZE (decl) == NULL
13934       || !tree_fits_uhwi_p (DECL_SIZE (decl)))
13935     return NULL;
13936
13937   decl_size = tree_to_uhwi (DECL_SIZE (decl));
13938   descr = NULL;
13939   descr_tail = &descr;
13940
13941   for (p = loc; p; p = XEXP (p, 1))
13942     {
13943       unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
13944       rtx loc_note = *decl_piece_varloc_ptr (p);
13945       dw_loc_descr_ref cur_descr;
13946       dw_loc_descr_ref *tail, last = NULL;
13947       unsigned HOST_WIDE_INT opsize = 0;
13948
13949       if (loc_note == NULL_RTX
13950           || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
13951         {
13952           padsize += bitsize;
13953           continue;
13954         }
13955       initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
13956       varloc = NOTE_VAR_LOCATION (loc_note);
13957       cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
13958       if (cur_descr == NULL)
13959         {
13960           padsize += bitsize;
13961           continue;
13962         }
13963
13964       /* Check that cur_descr either doesn't use
13965          DW_OP_*piece operations, or their sum is equal
13966          to bitsize.  Otherwise we can't embed it.  */
13967       for (tail = &cur_descr; *tail != NULL;
13968            tail = &(*tail)->dw_loc_next)
13969         if ((*tail)->dw_loc_opc == DW_OP_piece)
13970           {
13971             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
13972                       * BITS_PER_UNIT;
13973             last = *tail;
13974           }
13975         else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
13976           {
13977             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
13978             last = *tail;
13979           }
13980
13981       if (last != NULL && opsize != bitsize)
13982         {
13983           padsize += bitsize;
13984           /* Discard the current piece of the descriptor and release any
13985              addr_table entries it uses.  */
13986           remove_loc_list_addr_table_entries (cur_descr);
13987           continue;
13988         }
13989
13990       /* If there is a hole, add DW_OP_*piece after empty DWARF
13991          expression, which means that those bits are optimized out.  */
13992       if (padsize)
13993         {
13994           if (padsize > decl_size)
13995             {
13996               remove_loc_list_addr_table_entries (cur_descr);
13997               goto discard_descr;
13998             }
13999           decl_size -= padsize;
14000           *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
14001           if (*descr_tail == NULL)
14002             {
14003               remove_loc_list_addr_table_entries (cur_descr);
14004               goto discard_descr;
14005             }
14006           descr_tail = &(*descr_tail)->dw_loc_next;
14007           padsize = 0;
14008         }
14009       *descr_tail = cur_descr;
14010       descr_tail = tail;
14011       if (bitsize > decl_size)
14012         goto discard_descr;
14013       decl_size -= bitsize;
14014       if (last == NULL)
14015         {
14016           HOST_WIDE_INT offset = 0;
14017           if (GET_CODE (varloc) == VAR_LOCATION
14018               && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14019             {
14020               varloc = PAT_VAR_LOCATION_LOC (varloc);
14021               if (GET_CODE (varloc) == EXPR_LIST)
14022                 varloc = XEXP (varloc, 0);
14023             }
14024           do 
14025             {
14026               if (GET_CODE (varloc) == CONST
14027                   || GET_CODE (varloc) == SIGN_EXTEND
14028                   || GET_CODE (varloc) == ZERO_EXTEND)
14029                 varloc = XEXP (varloc, 0);
14030               else if (GET_CODE (varloc) == SUBREG)
14031                 varloc = SUBREG_REG (varloc);
14032               else
14033                 break;
14034             }
14035           while (1);
14036           /* DW_OP_bit_size offset should be zero for register
14037              or implicit location descriptions and empty location
14038              descriptions, but for memory addresses needs big endian
14039              adjustment.  */
14040           if (MEM_P (varloc))
14041             {
14042               unsigned HOST_WIDE_INT memsize
14043                 = MEM_SIZE (varloc) * BITS_PER_UNIT;
14044               if (memsize != bitsize)
14045                 {
14046                   if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
14047                       && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
14048                     goto discard_descr;
14049                   if (memsize < bitsize)
14050                     goto discard_descr;
14051                   if (BITS_BIG_ENDIAN)
14052                     offset = memsize - bitsize;
14053                 }
14054             }
14055
14056           *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
14057           if (*descr_tail == NULL)
14058             goto discard_descr;
14059           descr_tail = &(*descr_tail)->dw_loc_next;
14060         }
14061     }
14062
14063   /* If there were any non-empty expressions, add padding till the end of
14064      the decl.  */
14065   if (descr != NULL && decl_size != 0)
14066     {
14067       *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
14068       if (*descr_tail == NULL)
14069         goto discard_descr;
14070     }
14071   return descr;
14072
14073 discard_descr:
14074   /* Discard the descriptor and release any addr_table entries it uses.  */
14075   remove_loc_list_addr_table_entries (descr);
14076   return NULL;
14077 }
14078
14079 /* Return the dwarf representation of the location list LOC_LIST of
14080    DECL.  WANT_ADDRESS has the same meaning as in loc_list_from_tree
14081    function.  */
14082
14083 static dw_loc_list_ref
14084 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
14085 {
14086   const char *endname, *secname;
14087   rtx varloc;
14088   enum var_init_status initialized;
14089   struct var_loc_node *node;
14090   dw_loc_descr_ref descr;
14091   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
14092   dw_loc_list_ref list = NULL;
14093   dw_loc_list_ref *listp = &list;
14094
14095   /* Now that we know what section we are using for a base,
14096      actually construct the list of locations.
14097      The first location information is what is passed to the
14098      function that creates the location list, and the remaining
14099      locations just get added on to that list.
14100      Note that we only know the start address for a location
14101      (IE location changes), so to build the range, we use
14102      the range [current location start, next location start].
14103      This means we have to special case the last node, and generate
14104      a range of [last location start, end of function label].  */
14105
14106   secname = secname_for_decl (decl);
14107
14108   for (node = loc_list->first; node; node = node->next)
14109     if (GET_CODE (node->loc) == EXPR_LIST
14110         || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
14111       {
14112         if (GET_CODE (node->loc) == EXPR_LIST)
14113           {
14114             /* This requires DW_OP_{,bit_}piece, which is not usable
14115                inside DWARF expressions.  */
14116             if (want_address != 2)
14117               continue;
14118             descr = dw_sra_loc_expr (decl, node->loc);
14119             if (descr == NULL)
14120               continue;
14121           }
14122         else
14123           {
14124             initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14125             varloc = NOTE_VAR_LOCATION (node->loc);
14126             descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
14127           }
14128         if (descr)
14129           {
14130             bool range_across_switch = false;
14131             /* If section switch happens in between node->label
14132                and node->next->label (or end of function) and
14133                we can't emit it as a single entry list,
14134                emit two ranges, first one ending at the end
14135                of first partition and second one starting at the
14136                beginning of second partition.  */
14137             if (node == loc_list->last_before_switch
14138                 && (node != loc_list->first || loc_list->first->next)
14139                 && current_function_decl)
14140               {
14141                 endname = cfun->fde->dw_fde_end;
14142                 range_across_switch = true;
14143               }
14144             /* The variable has a location between NODE->LABEL and
14145                NODE->NEXT->LABEL.  */
14146             else if (node->next)
14147               endname = node->next->label;
14148             /* If the variable has a location at the last label
14149                it keeps its location until the end of function.  */
14150             else if (!current_function_decl)
14151               endname = text_end_label;
14152             else
14153               {
14154                 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14155                                              current_function_funcdef_no);
14156                 endname = ggc_strdup (label_id);
14157               }
14158
14159             *listp = new_loc_list (descr, node->label, endname, secname);
14160             if (TREE_CODE (decl) == PARM_DECL
14161                 && node == loc_list->first
14162                 && NOTE_P (node->loc)
14163                 && strcmp (node->label, endname) == 0)
14164               (*listp)->force = true;
14165             listp = &(*listp)->dw_loc_next;
14166
14167             if (range_across_switch)
14168               {
14169                 if (GET_CODE (node->loc) == EXPR_LIST)
14170                   descr = dw_sra_loc_expr (decl, node->loc);
14171                 else
14172                   {
14173                     initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14174                     varloc = NOTE_VAR_LOCATION (node->loc);
14175                     descr = dw_loc_list_1 (decl, varloc, want_address,
14176                                            initialized);
14177                   }
14178                 gcc_assert (descr);
14179                 /* The variable has a location between NODE->LABEL and
14180                    NODE->NEXT->LABEL.  */
14181                 if (node->next)
14182                   endname = node->next->label;
14183                 else
14184                   endname = cfun->fde->dw_fde_second_end;
14185                 *listp = new_loc_list (descr,
14186                                        cfun->fde->dw_fde_second_begin,
14187                                        endname, secname);
14188                 listp = &(*listp)->dw_loc_next;
14189               }
14190           }
14191       }
14192
14193   /* Try to avoid the overhead of a location list emitting a location
14194      expression instead, but only if we didn't have more than one
14195      location entry in the first place.  If some entries were not
14196      representable, we don't want to pretend a single entry that was
14197      applies to the entire scope in which the variable is
14198      available.  */
14199   if (list && loc_list->first->next)
14200     gen_llsym (list);
14201
14202   return list;
14203 }
14204
14205 /* Return if the loc_list has only single element and thus can be represented
14206    as location description.   */
14207
14208 static bool
14209 single_element_loc_list_p (dw_loc_list_ref list)
14210 {
14211   gcc_assert (!list->dw_loc_next || list->ll_symbol);
14212   return !list->ll_symbol;
14213 }
14214
14215 /* To each location in list LIST add loc descr REF.  */
14216
14217 static void
14218 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
14219 {
14220   dw_loc_descr_ref copy;
14221   add_loc_descr (&list->expr, ref);
14222   list = list->dw_loc_next;
14223   while (list)
14224     {
14225       copy = ggc_alloc<dw_loc_descr_node> ();
14226       memcpy (copy, ref, sizeof (dw_loc_descr_node));
14227       add_loc_descr (&list->expr, copy);
14228       while (copy->dw_loc_next)
14229         {
14230           dw_loc_descr_ref new_copy = ggc_alloc<dw_loc_descr_node> ();
14231           memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
14232           copy->dw_loc_next = new_copy;
14233           copy = new_copy;
14234         }
14235       list = list->dw_loc_next;
14236     }
14237 }
14238
14239 /* Given two lists RET and LIST
14240    produce location list that is result of adding expression in LIST
14241    to expression in RET on each position in program.
14242    Might be destructive on both RET and LIST.
14243
14244    TODO: We handle only simple cases of RET or LIST having at most one
14245    element. General case would inolve sorting the lists in program order
14246    and merging them that will need some additional work.
14247    Adding that will improve quality of debug info especially for SRA-ed
14248    structures.  */
14249
14250 static void
14251 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
14252 {
14253   if (!list)
14254     return;
14255   if (!*ret)
14256     {
14257       *ret = list;
14258       return;
14259     }
14260   if (!list->dw_loc_next)
14261     {
14262       add_loc_descr_to_each (*ret, list->expr);
14263       return;
14264     }
14265   if (!(*ret)->dw_loc_next)
14266     {
14267       add_loc_descr_to_each (list, (*ret)->expr);
14268       *ret = list;
14269       return;
14270     }
14271   expansion_failed (NULL_TREE, NULL_RTX,
14272                     "Don't know how to merge two non-trivial"
14273                     " location lists.\n");
14274   *ret = NULL;
14275   return;
14276 }
14277
14278 /* LOC is constant expression.  Try a luck, look it up in constant
14279    pool and return its loc_descr of its address.  */
14280
14281 static dw_loc_descr_ref
14282 cst_pool_loc_descr (tree loc)
14283 {
14284   /* Get an RTL for this, if something has been emitted.  */
14285   rtx rtl = lookup_constant_def (loc);
14286
14287   if (!rtl || !MEM_P (rtl))
14288     {
14289       gcc_assert (!rtl);
14290       return 0;
14291     }
14292   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
14293
14294   /* TODO: We might get more coverage if we was actually delaying expansion
14295      of all expressions till end of compilation when constant pools are fully
14296      populated.  */
14297   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
14298     {
14299       expansion_failed (loc, NULL_RTX,
14300                         "CST value in contant pool but not marked.");
14301       return 0;
14302     }
14303   return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
14304                              GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
14305 }
14306
14307 /* Return dw_loc_list representing address of addr_expr LOC
14308    by looking for inner INDIRECT_REF expression and turning
14309    it into simple arithmetics.
14310
14311    See loc_list_from_tree for the meaning of CONTEXT.  */
14312
14313 static dw_loc_list_ref
14314 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
14315                                                    const loc_descr_context *context)
14316 {
14317   tree obj, offset;
14318   HOST_WIDE_INT bitsize, bitpos, bytepos;
14319   machine_mode mode;
14320   int unsignedp, volatilep = 0;
14321   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14322
14323   obj = get_inner_reference (TREE_OPERAND (loc, 0),
14324                              &bitsize, &bitpos, &offset, &mode,
14325                              &unsignedp, &volatilep, false);
14326   STRIP_NOPS (obj);
14327   if (bitpos % BITS_PER_UNIT)
14328     {
14329       expansion_failed (loc, NULL_RTX, "bitfield access");
14330       return 0;
14331     }
14332   if (!INDIRECT_REF_P (obj))
14333     {
14334       expansion_failed (obj,
14335                         NULL_RTX, "no indirect ref in inner refrence");
14336       return 0;
14337     }
14338   if (!offset && !bitpos)
14339     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
14340                                    context);
14341   else if (toplev
14342            && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
14343            && (dwarf_version >= 4 || !dwarf_strict))
14344     {
14345       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
14346       if (!list_ret)
14347         return 0;
14348       if (offset)
14349         {
14350           /* Variable offset.  */
14351           list_ret1 = loc_list_from_tree (offset, 0, context);
14352           if (list_ret1 == 0)
14353             return 0;
14354           add_loc_list (&list_ret, list_ret1);
14355           if (!list_ret)
14356             return 0;
14357           add_loc_descr_to_each (list_ret,
14358                                  new_loc_descr (DW_OP_plus, 0, 0));
14359         }
14360       bytepos = bitpos / BITS_PER_UNIT;
14361       if (bytepos > 0)
14362         add_loc_descr_to_each (list_ret,
14363                                new_loc_descr (DW_OP_plus_uconst,
14364                                               bytepos, 0));
14365       else if (bytepos < 0)
14366         loc_list_plus_const (list_ret, bytepos);
14367       add_loc_descr_to_each (list_ret,
14368                              new_loc_descr (DW_OP_stack_value, 0, 0));
14369     }
14370   return list_ret;
14371 }
14372
14373
14374 /* Helper structure for location descriptions generation.  */
14375 struct loc_descr_context
14376 {
14377   /* The type that is implicitly referenced by DW_OP_push_object_address, or
14378      NULL_TREE if DW_OP_push_object_address in invalid for this location
14379      description.  This is used when processing PLACEHOLDER_EXPR nodes.  */
14380   tree context_type;
14381   /* The ..._DECL node that should be translated as a
14382      DW_OP_push_object_address operation.  */
14383   tree base_decl;
14384 };
14385
14386 /* Generate Dwarf location list representing LOC.
14387    If WANT_ADDRESS is false, expression computing LOC will be computed
14388    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
14389    if WANT_ADDRESS is 2, expression computing address useable in location
14390      will be returned (i.e. DW_OP_reg can be used
14391      to refer to register values).
14392
14393    CONTEXT provides information to customize the location descriptions
14394    generation.  Its context_type field specifies what type is implicitly
14395    referenced by DW_OP_push_object_address.  If it is NULL_TREE, this operation
14396    will not be generated.
14397
14398    If CONTEXT is NULL, the behavior is the same as if both context_type and
14399    base_decl fields were NULL_TREE.  */
14400
14401 static dw_loc_list_ref
14402 loc_list_from_tree (tree loc, int want_address,
14403                     const struct loc_descr_context *context)
14404 {
14405   dw_loc_descr_ref ret = NULL, ret1 = NULL;
14406   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14407   int have_address = 0;
14408   enum dwarf_location_atom op;
14409
14410   /* ??? Most of the time we do not take proper care for sign/zero
14411      extending the values properly.  Hopefully this won't be a real
14412      problem...  */
14413
14414   if (context != NULL
14415       && context->base_decl == loc
14416       && want_address == 0)
14417     {
14418       if (dwarf_version >= 3 || !dwarf_strict)
14419         return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
14420                              NULL, NULL, NULL);
14421       else
14422         return NULL;
14423     }
14424
14425   switch (TREE_CODE (loc))
14426     {
14427     case ERROR_MARK:
14428       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
14429       return 0;
14430
14431     case PLACEHOLDER_EXPR:
14432       /* This case involves extracting fields from an object to determine the
14433          position of other fields. It is supposed to appear only as the first
14434          operand of COMPONENT_REF nodes and to reference precisely the type
14435          that the context allows.  */
14436       if (context != NULL
14437           && TREE_TYPE (loc) == context->context_type
14438           && want_address >= 1)
14439         {
14440           if (dwarf_version >= 3 || !dwarf_strict)
14441             {
14442               ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
14443               have_address = 1;
14444               break;
14445             }
14446           else
14447             return NULL;
14448         }
14449       else
14450         expansion_failed (loc, NULL_RTX,
14451                           "PLACEHOLDER_EXPR for an unexpected type");
14452       break;
14453
14454     case CALL_EXPR:
14455       expansion_failed (loc, NULL_RTX, "CALL_EXPR");
14456       /* There are no opcodes for these operations.  */
14457       return 0;
14458
14459     case PREINCREMENT_EXPR:
14460     case PREDECREMENT_EXPR:
14461     case POSTINCREMENT_EXPR:
14462     case POSTDECREMENT_EXPR:
14463       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
14464       /* There are no opcodes for these operations.  */
14465       return 0;
14466
14467     case ADDR_EXPR:
14468       /* If we already want an address, see if there is INDIRECT_REF inside
14469          e.g. for &this->field.  */
14470       if (want_address)
14471         {
14472           list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
14473                        (loc, want_address == 2, context);
14474           if (list_ret)
14475             have_address = 1;
14476           else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
14477                    && (ret = cst_pool_loc_descr (loc)))
14478             have_address = 1;
14479         }
14480         /* Otherwise, process the argument and look for the address.  */
14481       if (!list_ret && !ret)
14482         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1, context);
14483       else
14484         {
14485           if (want_address)
14486             expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
14487           return NULL;
14488         }
14489       break;
14490
14491     case VAR_DECL:
14492       if (DECL_THREAD_LOCAL_P (loc))
14493         {
14494           rtx rtl;
14495          enum dwarf_location_atom tls_op;
14496          enum dtprel_bool dtprel = dtprel_false;
14497
14498           if (targetm.have_tls)
14499             {
14500               /* If this is not defined, we have no way to emit the
14501                  data.  */
14502               if (!targetm.asm_out.output_dwarf_dtprel)
14503                 return 0;
14504
14505                /* The way DW_OP_GNU_push_tls_address is specified, we
14506                   can only look up addresses of objects in the current
14507                   module.  We used DW_OP_addr as first op, but that's
14508                   wrong, because DW_OP_addr is relocated by the debug
14509                   info consumer, while DW_OP_GNU_push_tls_address
14510                   operand shouldn't be.  */
14511               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
14512                 return 0;
14513              dtprel = dtprel_true;
14514              tls_op = DW_OP_GNU_push_tls_address;
14515             }
14516           else
14517             {
14518               if (!targetm.emutls.debug_form_tls_address
14519                   || !(dwarf_version >= 3 || !dwarf_strict))
14520                 return 0;
14521               /* We stuffed the control variable into the DECL_VALUE_EXPR
14522                  to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
14523                  no longer appear in gimple code.  We used the control
14524                  variable in specific so that we could pick it up here.  */
14525               loc = DECL_VALUE_EXPR (loc);
14526               tls_op = DW_OP_form_tls_address;
14527             }
14528
14529           rtl = rtl_for_decl_location (loc);
14530           if (rtl == NULL_RTX)
14531             return 0;
14532
14533           if (!MEM_P (rtl))
14534             return 0;
14535           rtl = XEXP (rtl, 0);
14536           if (! CONSTANT_P (rtl))
14537             return 0;
14538
14539           ret = new_addr_loc_descr (rtl, dtprel);
14540           ret1 = new_loc_descr (tls_op, 0, 0);
14541           add_loc_descr (&ret, ret1);
14542
14543           have_address = 1;
14544           break;
14545         }
14546       /* FALLTHRU */
14547
14548     case PARM_DECL:
14549     case RESULT_DECL:
14550       if (DECL_HAS_VALUE_EXPR_P (loc))
14551         return loc_list_from_tree (DECL_VALUE_EXPR (loc),
14552                                    want_address, context);
14553       /* FALLTHRU */
14554
14555     case FUNCTION_DECL:
14556       {
14557         rtx rtl;
14558         var_loc_list *loc_list = lookup_decl_loc (loc);
14559
14560         if (loc_list && loc_list->first)
14561           {
14562             list_ret = dw_loc_list (loc_list, loc, want_address);
14563             have_address = want_address != 0;
14564             break;
14565           }
14566         rtl = rtl_for_decl_location (loc);
14567         if (rtl == NULL_RTX)
14568           {
14569             expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14570             return 0;
14571           }
14572         else if (CONST_INT_P (rtl))
14573           {
14574             HOST_WIDE_INT val = INTVAL (rtl);
14575             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14576               val &= GET_MODE_MASK (DECL_MODE (loc));
14577             ret = int_loc_descriptor (val);
14578           }
14579         else if (GET_CODE (rtl) == CONST_STRING)
14580           {
14581             expansion_failed (loc, NULL_RTX, "CONST_STRING");
14582             return 0;
14583           }
14584         else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14585           ret = new_addr_loc_descr (rtl, dtprel_false);
14586         else
14587           {
14588             machine_mode mode, mem_mode;
14589
14590             /* Certain constructs can only be represented at top-level.  */
14591             if (want_address == 2)
14592               {
14593                 ret = loc_descriptor (rtl, VOIDmode,
14594                                       VAR_INIT_STATUS_INITIALIZED);
14595                 have_address = 1;
14596               }
14597             else
14598               {
14599                 mode = GET_MODE (rtl);
14600                 mem_mode = VOIDmode;
14601                 if (MEM_P (rtl))
14602                   {
14603                     mem_mode = mode;
14604                     mode = get_address_mode (rtl);
14605                     rtl = XEXP (rtl, 0);
14606                     have_address = 1;
14607                   }
14608                 ret = mem_loc_descriptor (rtl, mode, mem_mode,
14609                                           VAR_INIT_STATUS_INITIALIZED);
14610               }
14611             if (!ret)
14612               expansion_failed (loc, rtl,
14613                                 "failed to produce loc descriptor for rtl");
14614           }
14615       }
14616       break;
14617
14618     case MEM_REF:
14619       if (!integer_zerop (TREE_OPERAND (loc, 1)))
14620         {
14621           have_address = 1;
14622           goto do_plus;
14623         }
14624       /* Fallthru.  */
14625     case INDIRECT_REF:
14626       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14627       have_address = 1;
14628       break;
14629
14630     case TARGET_MEM_REF:
14631     case SSA_NAME:
14632       return NULL;
14633
14634     case COMPOUND_EXPR:
14635       return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address, context);
14636
14637     CASE_CONVERT:
14638     case VIEW_CONVERT_EXPR:
14639     case SAVE_EXPR:
14640     case MODIFY_EXPR:
14641       return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address, context);
14642
14643     case COMPONENT_REF:
14644     case BIT_FIELD_REF:
14645     case ARRAY_REF:
14646     case ARRAY_RANGE_REF:
14647     case REALPART_EXPR:
14648     case IMAGPART_EXPR:
14649       {
14650         tree obj, offset;
14651         HOST_WIDE_INT bitsize, bitpos, bytepos;
14652         machine_mode mode;
14653         int unsignedp, volatilep = 0;
14654
14655         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14656                                    &unsignedp, &volatilep, false);
14657
14658         gcc_assert (obj != loc);
14659
14660         list_ret = loc_list_from_tree (obj,
14661                                        want_address == 2
14662                                        && !bitpos && !offset ? 2 : 1,
14663                                        context);
14664         /* TODO: We can extract value of the small expression via shifting even
14665            for nonzero bitpos.  */
14666         if (list_ret == 0)
14667           return 0;
14668         if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14669           {
14670             expansion_failed (loc, NULL_RTX,
14671                               "bitfield access");
14672             return 0;
14673           }
14674
14675         if (offset != NULL_TREE)
14676           {
14677             /* Variable offset.  */
14678             list_ret1 = loc_list_from_tree (offset, 0, context);
14679             if (list_ret1 == 0)
14680               return 0;
14681             add_loc_list (&list_ret, list_ret1);
14682             if (!list_ret)
14683               return 0;
14684             add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14685           }
14686
14687         bytepos = bitpos / BITS_PER_UNIT;
14688         if (bytepos > 0)
14689           add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14690         else if (bytepos < 0)
14691           loc_list_plus_const (list_ret, bytepos);
14692
14693         have_address = 1;
14694         break;
14695       }
14696
14697     case INTEGER_CST:
14698       if ((want_address || !tree_fits_shwi_p (loc))
14699           && (ret = cst_pool_loc_descr (loc)))
14700         have_address = 1;
14701       else if (want_address == 2
14702                && tree_fits_shwi_p (loc)
14703                && (ret = address_of_int_loc_descriptor
14704                            (int_size_in_bytes (TREE_TYPE (loc)),
14705                             tree_to_shwi (loc))))
14706         have_address = 1;
14707       else if (tree_fits_shwi_p (loc))
14708         ret = int_loc_descriptor (tree_to_shwi (loc));
14709       else
14710         {
14711           expansion_failed (loc, NULL_RTX,
14712                             "Integer operand is not host integer");
14713           return 0;
14714         }
14715       break;
14716
14717     case CONSTRUCTOR:
14718     case REAL_CST:
14719     case STRING_CST:
14720     case COMPLEX_CST:
14721       if ((ret = cst_pool_loc_descr (loc)))
14722         have_address = 1;
14723       else
14724       /* We can construct small constants here using int_loc_descriptor.  */
14725         expansion_failed (loc, NULL_RTX,
14726                           "constructor or constant not in constant pool");
14727       break;
14728
14729     case TRUTH_AND_EXPR:
14730     case TRUTH_ANDIF_EXPR:
14731     case BIT_AND_EXPR:
14732       op = DW_OP_and;
14733       goto do_binop;
14734
14735     case TRUTH_XOR_EXPR:
14736     case BIT_XOR_EXPR:
14737       op = DW_OP_xor;
14738       goto do_binop;
14739
14740     case TRUTH_OR_EXPR:
14741     case TRUTH_ORIF_EXPR:
14742     case BIT_IOR_EXPR:
14743       op = DW_OP_or;
14744       goto do_binop;
14745
14746     case FLOOR_DIV_EXPR:
14747     case CEIL_DIV_EXPR:
14748     case ROUND_DIV_EXPR:
14749     case TRUNC_DIV_EXPR:
14750       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14751         return 0;
14752       op = DW_OP_div;
14753       goto do_binop;
14754
14755     case MINUS_EXPR:
14756       op = DW_OP_minus;
14757       goto do_binop;
14758
14759     case FLOOR_MOD_EXPR:
14760     case CEIL_MOD_EXPR:
14761     case ROUND_MOD_EXPR:
14762     case TRUNC_MOD_EXPR:
14763       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14764         {
14765           op = DW_OP_mod;
14766           goto do_binop;
14767         }
14768       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14769       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
14770       if (list_ret == 0 || list_ret1 == 0)
14771         return 0;
14772
14773       add_loc_list (&list_ret, list_ret1);
14774       if (list_ret == 0)
14775         return 0;
14776       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14777       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14778       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14779       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14780       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14781       break;
14782
14783     case MULT_EXPR:
14784       op = DW_OP_mul;
14785       goto do_binop;
14786
14787     case LSHIFT_EXPR:
14788       op = DW_OP_shl;
14789       goto do_binop;
14790
14791     case RSHIFT_EXPR:
14792       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14793       goto do_binop;
14794
14795     case POINTER_PLUS_EXPR:
14796     case PLUS_EXPR:
14797     do_plus:
14798       if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
14799         {
14800           list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14801           if (list_ret == 0)
14802             return 0;
14803
14804           loc_list_plus_const (list_ret, tree_to_shwi (TREE_OPERAND (loc, 1)));
14805           break;
14806         }
14807
14808       op = DW_OP_plus;
14809       goto do_binop;
14810
14811     case LE_EXPR:
14812       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14813         return 0;
14814
14815       op = DW_OP_le;
14816       goto do_binop;
14817
14818     case GE_EXPR:
14819       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14820         return 0;
14821
14822       op = DW_OP_ge;
14823       goto do_binop;
14824
14825     case LT_EXPR:
14826       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14827         return 0;
14828
14829       op = DW_OP_lt;
14830       goto do_binop;
14831
14832     case GT_EXPR:
14833       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14834         return 0;
14835
14836       op = DW_OP_gt;
14837       goto do_binop;
14838
14839     case EQ_EXPR:
14840       op = DW_OP_eq;
14841       goto do_binop;
14842
14843     case NE_EXPR:
14844       op = DW_OP_ne;
14845       goto do_binop;
14846
14847     do_binop:
14848       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14849       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
14850       if (list_ret == 0 || list_ret1 == 0)
14851         return 0;
14852
14853       add_loc_list (&list_ret, list_ret1);
14854       if (list_ret == 0)
14855         return 0;
14856       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14857       break;
14858
14859     case TRUTH_NOT_EXPR:
14860     case BIT_NOT_EXPR:
14861       op = DW_OP_not;
14862       goto do_unop;
14863
14864     case ABS_EXPR:
14865       op = DW_OP_abs;
14866       goto do_unop;
14867
14868     case NEGATE_EXPR:
14869       op = DW_OP_neg;
14870       goto do_unop;
14871
14872     do_unop:
14873       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14874       if (list_ret == 0)
14875         return 0;
14876
14877       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14878       break;
14879
14880     case MIN_EXPR:
14881     case MAX_EXPR:
14882       {
14883         const enum tree_code code =
14884           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14885
14886         loc = build3 (COND_EXPR, TREE_TYPE (loc),
14887                       build2 (code, integer_type_node,
14888                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14889                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14890       }
14891
14892       /* ... fall through ...  */
14893
14894     case COND_EXPR:
14895       {
14896         dw_loc_descr_ref lhs
14897           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
14898         dw_loc_list_ref rhs
14899           = loc_list_from_tree (TREE_OPERAND (loc, 2), 0, context);
14900         dw_loc_descr_ref bra_node, jump_node, tmp;
14901
14902         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14903         if (list_ret == 0 || lhs == 0 || rhs == 0)
14904           return 0;
14905
14906         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14907         add_loc_descr_to_each (list_ret, bra_node);
14908
14909         add_loc_list (&list_ret, rhs);
14910         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14911         add_loc_descr_to_each (list_ret, jump_node);
14912
14913         add_loc_descr_to_each (list_ret, lhs);
14914         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14915         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14916
14917         /* ??? Need a node to point the skip at.  Use a nop.  */
14918         tmp = new_loc_descr (DW_OP_nop, 0, 0);
14919         add_loc_descr_to_each (list_ret, tmp);
14920         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14921         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14922       }
14923       break;
14924
14925     case FIX_TRUNC_EXPR:
14926       return 0;
14927
14928     default:
14929       /* Leave front-end specific codes as simply unknown.  This comes
14930          up, for instance, with the C STMT_EXPR.  */
14931       if ((unsigned int) TREE_CODE (loc)
14932           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14933         {
14934           expansion_failed (loc, NULL_RTX,
14935                             "language specific tree node");
14936           return 0;
14937         }
14938
14939 #ifdef ENABLE_CHECKING
14940       /* Otherwise this is a generic code; we should just lists all of
14941          these explicitly.  We forgot one.  */
14942       gcc_unreachable ();
14943 #else
14944       /* In a release build, we want to degrade gracefully: better to
14945          generate incomplete debugging information than to crash.  */
14946       return NULL;
14947 #endif
14948     }
14949
14950   if (!ret && !list_ret)
14951     return 0;
14952
14953   if (want_address == 2 && !have_address
14954       && (dwarf_version >= 4 || !dwarf_strict))
14955     {
14956       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14957         {
14958           expansion_failed (loc, NULL_RTX,
14959                             "DWARF address size mismatch");
14960           return 0;
14961         }
14962       if (ret)
14963         add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
14964       else
14965         add_loc_descr_to_each (list_ret,
14966                                new_loc_descr (DW_OP_stack_value, 0, 0));
14967       have_address = 1;
14968     }
14969   /* Show if we can't fill the request for an address.  */
14970   if (want_address && !have_address)
14971     {
14972       expansion_failed (loc, NULL_RTX,
14973                         "Want address and only have value");
14974       return 0;
14975     }
14976
14977   gcc_assert (!ret || !list_ret);
14978
14979   /* If we've got an address and don't want one, dereference.  */
14980   if (!want_address && have_address)
14981     {
14982       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14983
14984       if (size > DWARF2_ADDR_SIZE || size == -1)
14985         {
14986           expansion_failed (loc, NULL_RTX,
14987                             "DWARF address size mismatch");
14988           return 0;
14989         }
14990       else if (size == DWARF2_ADDR_SIZE)
14991         op = DW_OP_deref;
14992       else
14993         op = DW_OP_deref_size;
14994
14995       if (ret)
14996         add_loc_descr (&ret, new_loc_descr (op, size, 0));
14997       else
14998         add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
14999     }
15000   if (ret)
15001     list_ret = new_loc_list (ret, NULL, NULL, NULL);
15002
15003   return list_ret;
15004 }
15005
15006 /* Same as above but return only single location expression.  */
15007 static dw_loc_descr_ref
15008 loc_descriptor_from_tree (tree loc, int want_address,
15009                           const struct loc_descr_context *context)
15010 {
15011   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
15012   if (!ret)
15013     return NULL;
15014   if (ret->dw_loc_next)
15015     {
15016       expansion_failed (loc, NULL_RTX,
15017                         "Location list where only loc descriptor needed");
15018       return NULL;
15019     }
15020   return ret->expr;
15021 }
15022
15023 /* Given a value, round it up to the lowest multiple of `boundary'
15024    which is not less than the value itself.  */
15025
15026 static inline HOST_WIDE_INT
15027 ceiling (HOST_WIDE_INT value, unsigned int boundary)
15028 {
15029   return (((value + boundary - 1) / boundary) * boundary);
15030 }
15031
15032 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
15033    pointer to the declared type for the relevant field variable, or return
15034    `integer_type_node' if the given node turns out to be an
15035    ERROR_MARK node.  */
15036
15037 static inline tree
15038 field_type (const_tree decl)
15039 {
15040   tree type;
15041
15042   if (TREE_CODE (decl) == ERROR_MARK)
15043     return integer_type_node;
15044
15045   type = DECL_BIT_FIELD_TYPE (decl);
15046   if (type == NULL_TREE)
15047     type = TREE_TYPE (decl);
15048
15049   return type;
15050 }
15051
15052 /* Given a pointer to a tree node, return the alignment in bits for
15053    it, or else return BITS_PER_WORD if the node actually turns out to
15054    be an ERROR_MARK node.  */
15055
15056 static inline unsigned
15057 simple_type_align_in_bits (const_tree type)
15058 {
15059   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
15060 }
15061
15062 static inline unsigned
15063 simple_decl_align_in_bits (const_tree decl)
15064 {
15065   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
15066 }
15067
15068 /* Return the result of rounding T up to ALIGN.  */
15069
15070 static inline offset_int
15071 round_up_to_align (const offset_int &t, unsigned int align)
15072 {
15073   return wi::udiv_trunc (t + align - 1, align) * align;
15074 }
15075
15076 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
15077    lowest addressed byte of the "containing object" for the given FIELD_DECL,
15078    or return 0 if we are unable to determine what that offset is, either
15079    because the argument turns out to be a pointer to an ERROR_MARK node, or
15080    because the offset is actually variable.  (We can't handle the latter case
15081    just yet).  */
15082
15083 static HOST_WIDE_INT
15084 field_byte_offset (const_tree decl)
15085 {
15086   offset_int object_offset_in_bits;
15087   offset_int object_offset_in_bytes;
15088   offset_int bitpos_int;
15089
15090   if (TREE_CODE (decl) == ERROR_MARK)
15091     return 0;
15092
15093   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
15094
15095   /* We cannot yet cope with fields whose positions are variable, so
15096      for now, when we see such things, we simply return 0.  Someday, we may
15097      be able to handle such cases, but it will be damn difficult.  */
15098   if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
15099     return 0;
15100
15101   bitpos_int = wi::to_offset (bit_position (decl));
15102
15103 #ifdef PCC_BITFIELD_TYPE_MATTERS
15104   if (PCC_BITFIELD_TYPE_MATTERS)
15105     {
15106       tree type;
15107       tree field_size_tree;
15108       offset_int deepest_bitpos;
15109       offset_int field_size_in_bits;
15110       unsigned int type_align_in_bits;
15111       unsigned int decl_align_in_bits;
15112       offset_int type_size_in_bits;
15113
15114       type = field_type (decl);
15115       type_size_in_bits = offset_int_type_size_in_bits (type);
15116       type_align_in_bits = simple_type_align_in_bits (type);
15117
15118       field_size_tree = DECL_SIZE (decl);
15119
15120       /* The size could be unspecified if there was an error, or for
15121          a flexible array member.  */
15122       if (!field_size_tree)
15123         field_size_tree = bitsize_zero_node;
15124
15125       /* If the size of the field is not constant, use the type size.  */
15126       if (TREE_CODE (field_size_tree) == INTEGER_CST)
15127         field_size_in_bits = wi::to_offset (field_size_tree);
15128       else
15129         field_size_in_bits = type_size_in_bits;
15130
15131       decl_align_in_bits = simple_decl_align_in_bits (decl);
15132
15133       /* The GCC front-end doesn't make any attempt to keep track of the
15134          starting bit offset (relative to the start of the containing
15135          structure type) of the hypothetical "containing object" for a
15136          bit-field.  Thus, when computing the byte offset value for the
15137          start of the "containing object" of a bit-field, we must deduce
15138          this information on our own. This can be rather tricky to do in
15139          some cases.  For example, handling the following structure type
15140          definition when compiling for an i386/i486 target (which only
15141          aligns long long's to 32-bit boundaries) can be very tricky:
15142
15143          struct S { int field1; long long field2:31; };
15144
15145          Fortunately, there is a simple rule-of-thumb which can be used
15146          in such cases.  When compiling for an i386/i486, GCC will
15147          allocate 8 bytes for the structure shown above.  It decides to
15148          do this based upon one simple rule for bit-field allocation.
15149          GCC allocates each "containing object" for each bit-field at
15150          the first (i.e. lowest addressed) legitimate alignment boundary
15151          (based upon the required minimum alignment for the declared
15152          type of the field) which it can possibly use, subject to the
15153          condition that there is still enough available space remaining
15154          in the containing object (when allocated at the selected point)
15155          to fully accommodate all of the bits of the bit-field itself.
15156
15157          This simple rule makes it obvious why GCC allocates 8 bytes for
15158          each object of the structure type shown above.  When looking
15159          for a place to allocate the "containing object" for `field2',
15160          the compiler simply tries to allocate a 64-bit "containing
15161          object" at each successive 32-bit boundary (starting at zero)
15162          until it finds a place to allocate that 64- bit field such that
15163          at least 31 contiguous (and previously unallocated) bits remain
15164          within that selected 64 bit field.  (As it turns out, for the
15165          example above, the compiler finds it is OK to allocate the
15166          "containing object" 64-bit field at bit-offset zero within the
15167          structure type.)
15168
15169          Here we attempt to work backwards from the limited set of facts
15170          we're given, and we try to deduce from those facts, where GCC
15171          must have believed that the containing object started (within
15172          the structure type). The value we deduce is then used (by the
15173          callers of this routine) to generate DW_AT_location and
15174          DW_AT_bit_offset attributes for fields (both bit-fields and, in
15175          the case of DW_AT_location, regular fields as well).  */
15176
15177       /* Figure out the bit-distance from the start of the structure to
15178          the "deepest" bit of the bit-field.  */
15179       deepest_bitpos = bitpos_int + field_size_in_bits;
15180
15181       /* This is the tricky part.  Use some fancy footwork to deduce
15182          where the lowest addressed bit of the containing object must
15183          be.  */
15184       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15185
15186       /* Round up to type_align by default.  This works best for
15187          bitfields.  */
15188       object_offset_in_bits
15189         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
15190
15191       if (wi::gtu_p (object_offset_in_bits, bitpos_int))
15192         {
15193           object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15194
15195           /* Round up to decl_align instead.  */
15196           object_offset_in_bits
15197             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
15198         }
15199     }
15200   else
15201 #endif /* PCC_BITFIELD_TYPE_MATTERS */
15202     object_offset_in_bits = bitpos_int;
15203
15204   object_offset_in_bytes
15205     = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
15206   return object_offset_in_bytes.to_shwi ();
15207 }
15208 \f
15209 /* The following routines define various Dwarf attributes and any data
15210    associated with them.  */
15211
15212 /* Add a location description attribute value to a DIE.
15213
15214    This emits location attributes suitable for whole variables and
15215    whole parameters.  Note that the location attributes for struct fields are
15216    generated by the routine `data_member_location_attribute' below.  */
15217
15218 static inline void
15219 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
15220                              dw_loc_list_ref descr)
15221 {
15222   if (descr == 0)
15223     return;
15224   if (single_element_loc_list_p (descr))
15225     add_AT_loc (die, attr_kind, descr->expr);
15226   else
15227     add_AT_loc_list (die, attr_kind, descr);
15228 }
15229
15230 /* Add DW_AT_accessibility attribute to DIE if needed.  */
15231
15232 static void
15233 add_accessibility_attribute (dw_die_ref die, tree decl)
15234 {
15235   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
15236      children, otherwise the default is DW_ACCESS_public.  In DWARF2
15237      the default has always been DW_ACCESS_public.  */
15238   if (TREE_PROTECTED (decl))
15239     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
15240   else if (TREE_PRIVATE (decl))
15241     {
15242       if (dwarf_version == 2
15243           || die->die_parent == NULL
15244           || die->die_parent->die_tag != DW_TAG_class_type)
15245         add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
15246     }
15247   else if (dwarf_version > 2
15248            && die->die_parent
15249            && die->die_parent->die_tag == DW_TAG_class_type)
15250     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
15251 }
15252
15253 /* Attach the specialized form of location attribute used for data members of
15254    struct and union types.  In the special case of a FIELD_DECL node which
15255    represents a bit-field, the "offset" part of this special location
15256    descriptor must indicate the distance in bytes from the lowest-addressed
15257    byte of the containing struct or union type to the lowest-addressed byte of
15258    the "containing object" for the bit-field.  (See the `field_byte_offset'
15259    function above).
15260
15261    For any given bit-field, the "containing object" is a hypothetical object
15262    (of some integral or enum type) within which the given bit-field lives.  The
15263    type of this hypothetical "containing object" is always the same as the
15264    declared type of the individual bit-field itself (for GCC anyway... the
15265    DWARF spec doesn't actually mandate this).  Note that it is the size (in
15266    bytes) of the hypothetical "containing object" which will be given in the
15267    DW_AT_byte_size attribute for this bit-field.  (See the
15268    `byte_size_attribute' function below.)  It is also used when calculating the
15269    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
15270    function below.)  */
15271
15272 static void
15273 add_data_member_location_attribute (dw_die_ref die, tree decl)
15274 {
15275   HOST_WIDE_INT offset;
15276   dw_loc_descr_ref loc_descr = 0;
15277
15278   if (TREE_CODE (decl) == TREE_BINFO)
15279     {
15280       /* We're working on the TAG_inheritance for a base class.  */
15281       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
15282         {
15283           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
15284              aren't at a fixed offset from all (sub)objects of the same
15285              type.  We need to extract the appropriate offset from our
15286              vtable.  The following dwarf expression means
15287
15288                BaseAddr = ObAddr + *((*ObAddr) - Offset)
15289
15290              This is specific to the V3 ABI, of course.  */
15291
15292           dw_loc_descr_ref tmp;
15293
15294           /* Make a copy of the object address.  */
15295           tmp = new_loc_descr (DW_OP_dup, 0, 0);
15296           add_loc_descr (&loc_descr, tmp);
15297
15298           /* Extract the vtable address.  */
15299           tmp = new_loc_descr (DW_OP_deref, 0, 0);
15300           add_loc_descr (&loc_descr, tmp);
15301
15302           /* Calculate the address of the offset.  */
15303           offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
15304           gcc_assert (offset < 0);
15305
15306           tmp = int_loc_descriptor (-offset);
15307           add_loc_descr (&loc_descr, tmp);
15308           tmp = new_loc_descr (DW_OP_minus, 0, 0);
15309           add_loc_descr (&loc_descr, tmp);
15310
15311           /* Extract the offset.  */
15312           tmp = new_loc_descr (DW_OP_deref, 0, 0);
15313           add_loc_descr (&loc_descr, tmp);
15314
15315           /* Add it to the object address.  */
15316           tmp = new_loc_descr (DW_OP_plus, 0, 0);
15317           add_loc_descr (&loc_descr, tmp);
15318         }
15319       else
15320         offset = tree_to_shwi (BINFO_OFFSET (decl));
15321     }
15322   else
15323     offset = field_byte_offset (decl);
15324
15325   if (! loc_descr)
15326     {
15327       if (dwarf_version > 2)
15328         {
15329           /* Don't need to output a location expression, just the constant. */
15330           if (offset < 0)
15331             add_AT_int (die, DW_AT_data_member_location, offset);
15332           else
15333             add_AT_unsigned (die, DW_AT_data_member_location, offset);
15334           return;
15335         }
15336       else
15337         {
15338           enum dwarf_location_atom op;
15339
15340           /* The DWARF2 standard says that we should assume that the structure
15341              address is already on the stack, so we can specify a structure
15342              field address by using DW_OP_plus_uconst.  */
15343           op = DW_OP_plus_uconst;
15344           loc_descr = new_loc_descr (op, offset, 0);
15345         }
15346     }
15347
15348   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
15349 }
15350
15351 /* Writes integer values to dw_vec_const array.  */
15352
15353 static void
15354 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
15355 {
15356   while (size != 0)
15357     {
15358       *dest++ = val & 0xff;
15359       val >>= 8;
15360       --size;
15361     }
15362 }
15363
15364 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
15365
15366 static HOST_WIDE_INT
15367 extract_int (const unsigned char *src, unsigned int size)
15368 {
15369   HOST_WIDE_INT val = 0;
15370
15371   src += size;
15372   while (size != 0)
15373     {
15374       val <<= 8;
15375       val |= *--src & 0xff;
15376       --size;
15377     }
15378   return val;
15379 }
15380
15381 /* Writes wide_int values to dw_vec_const array.  */
15382
15383 static void
15384 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
15385 {
15386   int i;
15387
15388   if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
15389     {
15390       insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
15391       return;
15392     }
15393
15394   /* We'd have to extend this code to support odd sizes.  */
15395   gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
15396
15397   int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
15398
15399   if (WORDS_BIG_ENDIAN)
15400     for (i = n - 1; i >= 0; i--)
15401       {
15402         insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15403         dest += sizeof (HOST_WIDE_INT);
15404       }
15405   else
15406     for (i = 0; i < n; i++)
15407       {
15408         insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15409         dest += sizeof (HOST_WIDE_INT);
15410       }
15411 }
15412
15413 /* Writes floating point values to dw_vec_const array.  */
15414
15415 static void
15416 insert_float (const_rtx rtl, unsigned char *array)
15417 {
15418   REAL_VALUE_TYPE rv;
15419   long val[4];
15420   int i;
15421
15422   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
15423   real_to_target (val, &rv, GET_MODE (rtl));
15424
15425   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
15426   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
15427     {
15428       insert_int (val[i], 4, array);
15429       array += 4;
15430     }
15431 }
15432
15433 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
15434    does not have a "location" either in memory or in a register.  These
15435    things can arise in GNU C when a constant is passed as an actual parameter
15436    to an inlined function.  They can also arise in C++ where declared
15437    constants do not necessarily get memory "homes".  */
15438
15439 static bool
15440 add_const_value_attribute (dw_die_ref die, rtx rtl)
15441 {
15442   switch (GET_CODE (rtl))
15443     {
15444     case CONST_INT:
15445       {
15446         HOST_WIDE_INT val = INTVAL (rtl);
15447
15448         if (val < 0)
15449           add_AT_int (die, DW_AT_const_value, val);
15450         else
15451           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
15452       }
15453       return true;
15454
15455     case CONST_WIDE_INT:
15456       add_AT_wide (die, DW_AT_const_value,
15457                    std::make_pair (rtl, GET_MODE (rtl)));
15458       return true;
15459
15460     case CONST_DOUBLE:
15461       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
15462          floating-point constant.  A CONST_DOUBLE is used whenever the
15463          constant requires more than one word in order to be adequately
15464          represented.  */
15465       {
15466         machine_mode mode = GET_MODE (rtl);
15467
15468         if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
15469           add_AT_double (die, DW_AT_const_value,
15470                          CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
15471         else
15472           {
15473             unsigned int length = GET_MODE_SIZE (mode);
15474             unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15475
15476             insert_float (rtl, array);
15477             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
15478           }
15479       }
15480       return true;
15481
15482     case CONST_VECTOR:
15483       {
15484         machine_mode mode = GET_MODE (rtl);
15485         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
15486         unsigned int length = CONST_VECTOR_NUNITS (rtl);
15487         unsigned char *array
15488           = ggc_vec_alloc<unsigned char> (length * elt_size);
15489         unsigned int i;
15490         unsigned char *p;
15491         machine_mode imode = GET_MODE_INNER (mode);
15492
15493         switch (GET_MODE_CLASS (mode))
15494           {
15495           case MODE_VECTOR_INT:
15496             for (i = 0, p = array; i < length; i++, p += elt_size)
15497               {
15498                 rtx elt = CONST_VECTOR_ELT (rtl, i);
15499                 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
15500               }
15501             break;
15502
15503           case MODE_VECTOR_FLOAT:
15504             for (i = 0, p = array; i < length; i++, p += elt_size)
15505               {
15506                 rtx elt = CONST_VECTOR_ELT (rtl, i);
15507                 insert_float (elt, p);
15508               }
15509             break;
15510
15511           default:
15512             gcc_unreachable ();
15513           }
15514
15515         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
15516       }
15517       return true;
15518
15519     case CONST_STRING:
15520       if (dwarf_version >= 4 || !dwarf_strict)
15521         {
15522           dw_loc_descr_ref loc_result;
15523           resolve_one_addr (&rtl);
15524         rtl_addr:
15525           loc_result = new_addr_loc_descr (rtl, dtprel_false);
15526           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15527           add_AT_loc (die, DW_AT_location, loc_result);
15528           vec_safe_push (used_rtx_array, rtl);
15529           return true;
15530         }
15531       return false;
15532
15533     case CONST:
15534       if (CONSTANT_P (XEXP (rtl, 0)))
15535         return add_const_value_attribute (die, XEXP (rtl, 0));
15536       /* FALLTHROUGH */
15537     case SYMBOL_REF:
15538       if (!const_ok_for_output (rtl))
15539         return false;
15540     case LABEL_REF:
15541       if (dwarf_version >= 4 || !dwarf_strict)
15542         goto rtl_addr;
15543       return false;
15544
15545     case PLUS:
15546       /* In cases where an inlined instance of an inline function is passed
15547          the address of an `auto' variable (which is local to the caller) we
15548          can get a situation where the DECL_RTL of the artificial local
15549          variable (for the inlining) which acts as a stand-in for the
15550          corresponding formal parameter (of the inline function) will look
15551          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
15552          exactly a compile-time constant expression, but it isn't the address
15553          of the (artificial) local variable either.  Rather, it represents the
15554          *value* which the artificial local variable always has during its
15555          lifetime.  We currently have no way to represent such quasi-constant
15556          values in Dwarf, so for now we just punt and generate nothing.  */
15557       return false;
15558
15559     case HIGH:
15560     case CONST_FIXED:
15561       return false;
15562
15563     case MEM:
15564       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
15565           && MEM_READONLY_P (rtl)
15566           && GET_MODE (rtl) == BLKmode)
15567         {
15568           add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
15569           return true;
15570         }
15571       return false;
15572
15573     default:
15574       /* No other kinds of rtx should be possible here.  */
15575       gcc_unreachable ();
15576     }
15577   return false;
15578 }
15579
15580 /* Determine whether the evaluation of EXPR references any variables
15581    or functions which aren't otherwise used (and therefore may not be
15582    output).  */
15583 static tree
15584 reference_to_unused (tree * tp, int * walk_subtrees,
15585                      void * data ATTRIBUTE_UNUSED)
15586 {
15587   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15588     *walk_subtrees = 0;
15589
15590   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15591       && ! TREE_ASM_WRITTEN (*tp))
15592     return *tp;
15593   /* ???  The C++ FE emits debug information for using decls, so
15594      putting gcc_unreachable here falls over.  See PR31899.  For now
15595      be conservative.  */
15596   else if (!symtab->global_info_ready
15597            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15598     return *tp;
15599   else if (TREE_CODE (*tp) == VAR_DECL)
15600     {
15601       varpool_node *node = varpool_node::get (*tp);
15602       if (!node || !node->definition)
15603         return *tp;
15604     }
15605   else if (TREE_CODE (*tp) == FUNCTION_DECL
15606            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15607     {
15608       /* The call graph machinery must have finished analyzing,
15609          optimizing and gimplifying the CU by now.
15610          So if *TP has no call graph node associated
15611          to it, it means *TP will not be emitted.  */
15612       if (!cgraph_node::get (*tp))
15613         return *tp;
15614     }
15615   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15616     return *tp;
15617
15618   return NULL_TREE;
15619 }
15620
15621 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15622    for use in a later add_const_value_attribute call.  */
15623
15624 static rtx
15625 rtl_for_decl_init (tree init, tree type)
15626 {
15627   rtx rtl = NULL_RTX;
15628
15629   STRIP_NOPS (init);
15630
15631   /* If a variable is initialized with a string constant without embedded
15632      zeros, build CONST_STRING.  */
15633   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15634     {
15635       tree enttype = TREE_TYPE (type);
15636       tree domain = TYPE_DOMAIN (type);
15637       machine_mode mode = TYPE_MODE (enttype);
15638
15639       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15640           && domain
15641           && integer_zerop (TYPE_MIN_VALUE (domain))
15642           && compare_tree_int (TYPE_MAX_VALUE (domain),
15643                                TREE_STRING_LENGTH (init) - 1) == 0
15644           && ((size_t) TREE_STRING_LENGTH (init)
15645               == strlen (TREE_STRING_POINTER (init)) + 1))
15646         {
15647           rtl = gen_rtx_CONST_STRING (VOIDmode,
15648                                       ggc_strdup (TREE_STRING_POINTER (init)));
15649           rtl = gen_rtx_MEM (BLKmode, rtl);
15650           MEM_READONLY_P (rtl) = 1;
15651         }
15652     }
15653   /* Other aggregates, and complex values, could be represented using
15654      CONCAT: FIXME!  */
15655   else if (AGGREGATE_TYPE_P (type)
15656            || (TREE_CODE (init) == VIEW_CONVERT_EXPR
15657                && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
15658            || TREE_CODE (type) == COMPLEX_TYPE)
15659     ;
15660   /* Vectors only work if their mode is supported by the target.
15661      FIXME: generic vectors ought to work too.  */
15662   else if (TREE_CODE (type) == VECTOR_TYPE
15663            && !VECTOR_MODE_P (TYPE_MODE (type)))
15664     ;
15665   /* If the initializer is something that we know will expand into an
15666      immediate RTL constant, expand it now.  We must be careful not to
15667      reference variables which won't be output.  */
15668   else if (initializer_constant_valid_p (init, type)
15669            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15670     {
15671       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15672          possible.  */
15673       if (TREE_CODE (type) == VECTOR_TYPE)
15674         switch (TREE_CODE (init))
15675           {
15676           case VECTOR_CST:
15677             break;
15678           case CONSTRUCTOR:
15679             if (TREE_CONSTANT (init))
15680               {
15681                 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15682                 bool constant_p = true;
15683                 tree value;
15684                 unsigned HOST_WIDE_INT ix;
15685
15686                 /* Even when ctor is constant, it might contain non-*_CST
15687                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15688                    belong into VECTOR_CST nodes.  */
15689                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15690                   if (!CONSTANT_CLASS_P (value))
15691                     {
15692                       constant_p = false;
15693                       break;
15694                     }
15695
15696                 if (constant_p)
15697                   {
15698                     init = build_vector_from_ctor (type, elts);
15699                     break;
15700                   }
15701               }
15702             /* FALLTHRU */
15703
15704           default:
15705             return NULL;
15706           }
15707
15708       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15709
15710       /* If expand_expr returns a MEM, it wasn't immediate.  */
15711       gcc_assert (!rtl || !MEM_P (rtl));
15712     }
15713
15714   return rtl;
15715 }
15716
15717 /* Generate RTL for the variable DECL to represent its location.  */
15718
15719 static rtx
15720 rtl_for_decl_location (tree decl)
15721 {
15722   rtx rtl;
15723
15724   /* Here we have to decide where we are going to say the parameter "lives"
15725      (as far as the debugger is concerned).  We only have a couple of
15726      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15727
15728      DECL_RTL normally indicates where the parameter lives during most of the
15729      activation of the function.  If optimization is enabled however, this
15730      could be either NULL or else a pseudo-reg.  Both of those cases indicate
15731      that the parameter doesn't really live anywhere (as far as the code
15732      generation parts of GCC are concerned) during most of the function's
15733      activation.  That will happen (for example) if the parameter is never
15734      referenced within the function.
15735
15736      We could just generate a location descriptor here for all non-NULL
15737      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15738      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15739      where DECL_RTL is NULL or is a pseudo-reg.
15740
15741      Note however that we can only get away with using DECL_INCOMING_RTL as
15742      a backup substitute for DECL_RTL in certain limited cases.  In cases
15743      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15744      we can be sure that the parameter was passed using the same type as it is
15745      declared to have within the function, and that its DECL_INCOMING_RTL
15746      points us to a place where a value of that type is passed.
15747
15748      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15749      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15750      because in these cases DECL_INCOMING_RTL points us to a value of some
15751      type which is *different* from the type of the parameter itself.  Thus,
15752      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15753      such cases, the debugger would end up (for example) trying to fetch a
15754      `float' from a place which actually contains the first part of a
15755      `double'.  That would lead to really incorrect and confusing
15756      output at debug-time.
15757
15758      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15759      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
15760      are a couple of exceptions however.  On little-endian machines we can
15761      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15762      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15763      an integral type that is smaller than TREE_TYPE (decl). These cases arise
15764      when (on a little-endian machine) a non-prototyped function has a
15765      parameter declared to be of type `short' or `char'.  In such cases,
15766      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15767      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15768      passed `int' value.  If the debugger then uses that address to fetch
15769      a `short' or a `char' (on a little-endian machine) the result will be
15770      the correct data, so we allow for such exceptional cases below.
15771
15772      Note that our goal here is to describe the place where the given formal
15773      parameter lives during most of the function's activation (i.e. between the
15774      end of the prologue and the start of the epilogue).  We'll do that as best
15775      as we can. Note however that if the given formal parameter is modified
15776      sometime during the execution of the function, then a stack backtrace (at
15777      debug-time) will show the function as having been called with the *new*
15778      value rather than the value which was originally passed in.  This happens
15779      rarely enough that it is not a major problem, but it *is* a problem, and
15780      I'd like to fix it.
15781
15782      A future version of dwarf2out.c may generate two additional attributes for
15783      any given DW_TAG_formal_parameter DIE which will describe the "passed
15784      type" and the "passed location" for the given formal parameter in addition
15785      to the attributes we now generate to indicate the "declared type" and the
15786      "active location" for each parameter.  This additional set of attributes
15787      could be used by debuggers for stack backtraces. Separately, note that
15788      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15789      This happens (for example) for inlined-instances of inline function formal
15790      parameters which are never referenced.  This really shouldn't be
15791      happening.  All PARM_DECL nodes should get valid non-NULL
15792      DECL_INCOMING_RTL values.  FIXME.  */
15793
15794   /* Use DECL_RTL as the "location" unless we find something better.  */
15795   rtl = DECL_RTL_IF_SET (decl);
15796
15797   /* When generating abstract instances, ignore everything except
15798      constants, symbols living in memory, and symbols living in
15799      fixed registers.  */
15800   if (! reload_completed)
15801     {
15802       if (rtl
15803           && (CONSTANT_P (rtl)
15804               || (MEM_P (rtl)
15805                   && CONSTANT_P (XEXP (rtl, 0)))
15806               || (REG_P (rtl)
15807                   && TREE_CODE (decl) == VAR_DECL
15808                   && TREE_STATIC (decl))))
15809         {
15810           rtl = targetm.delegitimize_address (rtl);
15811           return rtl;
15812         }
15813       rtl = NULL_RTX;
15814     }
15815   else if (TREE_CODE (decl) == PARM_DECL)
15816     {
15817       if (rtl == NULL_RTX
15818           || is_pseudo_reg (rtl)
15819           || (MEM_P (rtl)
15820               && is_pseudo_reg (XEXP (rtl, 0))
15821               && DECL_INCOMING_RTL (decl)
15822               && MEM_P (DECL_INCOMING_RTL (decl))
15823               && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15824         {
15825           tree declared_type = TREE_TYPE (decl);
15826           tree passed_type = DECL_ARG_TYPE (decl);
15827           machine_mode dmode = TYPE_MODE (declared_type);
15828           machine_mode pmode = TYPE_MODE (passed_type);
15829
15830           /* This decl represents a formal parameter which was optimized out.
15831              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15832              all cases where (rtl == NULL_RTX) just below.  */
15833           if (dmode == pmode)
15834             rtl = DECL_INCOMING_RTL (decl);
15835           else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15836                    && SCALAR_INT_MODE_P (dmode)
15837                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15838                    && DECL_INCOMING_RTL (decl))
15839             {
15840               rtx inc = DECL_INCOMING_RTL (decl);
15841               if (REG_P (inc))
15842                 rtl = inc;
15843               else if (MEM_P (inc))
15844                 {
15845                   if (BYTES_BIG_ENDIAN)
15846                     rtl = adjust_address_nv (inc, dmode,
15847                                              GET_MODE_SIZE (pmode)
15848                                              - GET_MODE_SIZE (dmode));
15849                   else
15850                     rtl = inc;
15851                 }
15852             }
15853         }
15854
15855       /* If the parm was passed in registers, but lives on the stack, then
15856          make a big endian correction if the mode of the type of the
15857          parameter is not the same as the mode of the rtl.  */
15858       /* ??? This is the same series of checks that are made in dbxout.c before
15859          we reach the big endian correction code there.  It isn't clear if all
15860          of these checks are necessary here, but keeping them all is the safe
15861          thing to do.  */
15862       else if (MEM_P (rtl)
15863                && XEXP (rtl, 0) != const0_rtx
15864                && ! CONSTANT_P (XEXP (rtl, 0))
15865                /* Not passed in memory.  */
15866                && !MEM_P (DECL_INCOMING_RTL (decl))
15867                /* Not passed by invisible reference.  */
15868                && (!REG_P (XEXP (rtl, 0))
15869                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15870                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15871 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15872                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15873 #endif
15874                      )
15875                /* Big endian correction check.  */
15876                && BYTES_BIG_ENDIAN
15877                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15878                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15879                    < UNITS_PER_WORD))
15880         {
15881           machine_mode addr_mode = get_address_mode (rtl);
15882           int offset = (UNITS_PER_WORD
15883                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15884
15885           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15886                              plus_constant (addr_mode, XEXP (rtl, 0), offset));
15887         }
15888     }
15889   else if (TREE_CODE (decl) == VAR_DECL
15890            && rtl
15891            && MEM_P (rtl)
15892            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15893            && BYTES_BIG_ENDIAN)
15894     {
15895       machine_mode addr_mode = get_address_mode (rtl);
15896       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15897       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15898
15899       /* If a variable is declared "register" yet is smaller than
15900          a register, then if we store the variable to memory, it
15901          looks like we're storing a register-sized value, when in
15902          fact we are not.  We need to adjust the offset of the
15903          storage location to reflect the actual value's bytes,
15904          else gdb will not be able to display it.  */
15905       if (rsize > dsize)
15906         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15907                            plus_constant (addr_mode, XEXP (rtl, 0),
15908                                           rsize - dsize));
15909     }
15910
15911   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15912      and will have been substituted directly into all expressions that use it.
15913      C does not have such a concept, but C++ and other languages do.  */
15914   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15915     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15916
15917   if (rtl)
15918     rtl = targetm.delegitimize_address (rtl);
15919
15920   /* If we don't look past the constant pool, we risk emitting a
15921      reference to a constant pool entry that isn't referenced from
15922      code, and thus is not emitted.  */
15923   if (rtl)
15924     rtl = avoid_constant_pool_reference (rtl);
15925
15926   /* Try harder to get a rtl.  If this symbol ends up not being emitted
15927      in the current CU, resolve_addr will remove the expression referencing
15928      it.  */
15929   if (rtl == NULL_RTX
15930       && TREE_CODE (decl) == VAR_DECL
15931       && !DECL_EXTERNAL (decl)
15932       && TREE_STATIC (decl)
15933       && DECL_NAME (decl)
15934       && !DECL_HARD_REGISTER (decl)
15935       && DECL_MODE (decl) != VOIDmode)
15936     {
15937       rtl = make_decl_rtl_for_debug (decl);
15938       if (!MEM_P (rtl)
15939           || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
15940           || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
15941         rtl = NULL_RTX;
15942     }
15943
15944   return rtl;
15945 }
15946
15947 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
15948    returned.  If so, the decl for the COMMON block is returned, and the
15949    value is the offset into the common block for the symbol.  */
15950
15951 static tree
15952 fortran_common (tree decl, HOST_WIDE_INT *value)
15953 {
15954   tree val_expr, cvar;
15955   machine_mode mode;
15956   HOST_WIDE_INT bitsize, bitpos;
15957   tree offset;
15958   int unsignedp, volatilep = 0;
15959
15960   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
15961      it does not have a value (the offset into the common area), or if it
15962      is thread local (as opposed to global) then it isn't common, and shouldn't
15963      be handled as such.  */
15964   if (TREE_CODE (decl) != VAR_DECL
15965       || !TREE_STATIC (decl)
15966       || !DECL_HAS_VALUE_EXPR_P (decl)
15967       || !is_fortran ())
15968     return NULL_TREE;
15969
15970   val_expr = DECL_VALUE_EXPR (decl);
15971   if (TREE_CODE (val_expr) != COMPONENT_REF)
15972     return NULL_TREE;
15973
15974   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
15975                               &mode, &unsignedp, &volatilep, true);
15976
15977   if (cvar == NULL_TREE
15978       || TREE_CODE (cvar) != VAR_DECL
15979       || DECL_ARTIFICIAL (cvar)
15980       || !TREE_PUBLIC (cvar))
15981     return NULL_TREE;
15982
15983   *value = 0;
15984   if (offset != NULL)
15985     {
15986       if (!tree_fits_shwi_p (offset))
15987         return NULL_TREE;
15988       *value = tree_to_shwi (offset);
15989     }
15990   if (bitpos != 0)
15991     *value += bitpos / BITS_PER_UNIT;
15992
15993   return cvar;
15994 }
15995
15996 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
15997    data attribute for a variable or a parameter.  We generate the
15998    DW_AT_const_value attribute only in those cases where the given variable
15999    or parameter does not have a true "location" either in memory or in a
16000    register.  This can happen (for example) when a constant is passed as an
16001    actual argument in a call to an inline function.  (It's possible that
16002    these things can crop up in other ways also.)  Note that one type of
16003    constant value which can be passed into an inlined function is a constant
16004    pointer.  This can happen for example if an actual argument in an inlined
16005    function call evaluates to a compile-time constant address.
16006
16007    CACHE_P is true if it is worth caching the location list for DECL,
16008    so that future calls can reuse it rather than regenerate it from scratch.
16009    This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
16010    since we will need to refer to them each time the function is inlined.  */
16011
16012 static bool
16013 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
16014                                        enum dwarf_attribute attr)
16015 {
16016   rtx rtl;
16017   dw_loc_list_ref list;
16018   var_loc_list *loc_list;
16019   cached_dw_loc_list *cache;
16020
16021   if (TREE_CODE (decl) == ERROR_MARK)
16022     return false;
16023
16024   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
16025               || TREE_CODE (decl) == RESULT_DECL);
16026
16027   /* Try to get some constant RTL for this decl, and use that as the value of
16028      the location.  */
16029
16030   rtl = rtl_for_decl_location (decl);
16031   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16032       && add_const_value_attribute (die, rtl))
16033     return true;
16034
16035   /* See if we have single element location list that is equivalent to
16036      a constant value.  That way we are better to use add_const_value_attribute
16037      rather than expanding constant value equivalent.  */
16038   loc_list = lookup_decl_loc (decl);
16039   if (loc_list
16040       && loc_list->first
16041       && loc_list->first->next == NULL
16042       && NOTE_P (loc_list->first->loc)
16043       && NOTE_VAR_LOCATION (loc_list->first->loc)
16044       && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
16045     {
16046       struct var_loc_node *node;
16047
16048       node = loc_list->first;
16049       rtl = NOTE_VAR_LOCATION_LOC (node->loc);
16050       if (GET_CODE (rtl) == EXPR_LIST)
16051         rtl = XEXP (rtl, 0);
16052       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16053           && add_const_value_attribute (die, rtl))
16054          return true;
16055     }
16056   /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
16057      list several times.  See if we've already cached the contents.  */
16058   list = NULL;
16059   if (loc_list == NULL || cached_dw_loc_list_table == NULL)
16060     cache_p = false;
16061   if (cache_p)
16062     {
16063       cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
16064       if (cache)
16065         list = cache->loc_list;
16066     }
16067   if (list == NULL)
16068     {
16069       list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
16070                                  NULL);
16071       /* It is usually worth caching this result if the decl is from
16072          BLOCK_NONLOCALIZED_VARS and if the list has at least two elements.  */
16073       if (cache_p && list && list->dw_loc_next)
16074         {
16075           cached_dw_loc_list **slot
16076             = cached_dw_loc_list_table->find_slot_with_hash (decl,
16077                                                              DECL_UID (decl),
16078                                                              INSERT);
16079           cache = ggc_cleared_alloc<cached_dw_loc_list> ();
16080           cache->decl_id = DECL_UID (decl);
16081           cache->loc_list = list;
16082           *slot = cache;
16083         }
16084     }
16085   if (list)
16086     {
16087       add_AT_location_description (die, attr, list);
16088       return true;
16089     }
16090   /* None of that worked, so it must not really have a location;
16091      try adding a constant value attribute from the DECL_INITIAL.  */
16092   return tree_add_const_value_attribute_for_decl (die, decl);
16093 }
16094
16095 /* Add VARIABLE and DIE into deferred locations list.  */
16096
16097 static void
16098 defer_location (tree variable, dw_die_ref die)
16099 {
16100   deferred_locations entry;
16101   entry.variable = variable;
16102   entry.die = die;
16103   vec_safe_push (deferred_locations_list, entry);
16104 }
16105
16106 /* Helper function for tree_add_const_value_attribute.  Natively encode
16107    initializer INIT into an array.  Return true if successful.  */
16108
16109 static bool
16110 native_encode_initializer (tree init, unsigned char *array, int size)
16111 {
16112   tree type;
16113
16114   if (init == NULL_TREE)
16115     return false;
16116
16117   STRIP_NOPS (init);
16118   switch (TREE_CODE (init))
16119     {
16120     case STRING_CST:
16121       type = TREE_TYPE (init);
16122       if (TREE_CODE (type) == ARRAY_TYPE)
16123         {
16124           tree enttype = TREE_TYPE (type);
16125           machine_mode mode = TYPE_MODE (enttype);
16126
16127           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
16128             return false;
16129           if (int_size_in_bytes (type) != size)
16130             return false;
16131           if (size > TREE_STRING_LENGTH (init))
16132             {
16133               memcpy (array, TREE_STRING_POINTER (init),
16134                       TREE_STRING_LENGTH (init));
16135               memset (array + TREE_STRING_LENGTH (init),
16136                       '\0', size - TREE_STRING_LENGTH (init));
16137             }
16138           else
16139             memcpy (array, TREE_STRING_POINTER (init), size);
16140           return true;
16141         }
16142       return false;
16143     case CONSTRUCTOR:
16144       type = TREE_TYPE (init);
16145       if (int_size_in_bytes (type) != size)
16146         return false;
16147       if (TREE_CODE (type) == ARRAY_TYPE)
16148         {
16149           HOST_WIDE_INT min_index;
16150           unsigned HOST_WIDE_INT cnt;
16151           int curpos = 0, fieldsize;
16152           constructor_elt *ce;
16153
16154           if (TYPE_DOMAIN (type) == NULL_TREE
16155               || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
16156             return false;
16157
16158           fieldsize = int_size_in_bytes (TREE_TYPE (type));
16159           if (fieldsize <= 0)
16160             return false;
16161
16162           min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
16163           memset (array, '\0', size);
16164           FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16165             {
16166               tree val = ce->value;
16167               tree index = ce->index;
16168               int pos = curpos;
16169               if (index && TREE_CODE (index) == RANGE_EXPR)
16170                 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
16171                       * fieldsize;
16172               else if (index)
16173                 pos = (tree_to_shwi (index) - min_index) * fieldsize;
16174
16175               if (val)
16176                 {
16177                   STRIP_NOPS (val);
16178                   if (!native_encode_initializer (val, array + pos, fieldsize))
16179                     return false;
16180                 }
16181               curpos = pos + fieldsize;
16182               if (index && TREE_CODE (index) == RANGE_EXPR)
16183                 {
16184                   int count = tree_to_shwi (TREE_OPERAND (index, 1))
16185                               - tree_to_shwi (TREE_OPERAND (index, 0));
16186                   while (count-- > 0)
16187                     {
16188                       if (val)
16189                         memcpy (array + curpos, array + pos, fieldsize);
16190                       curpos += fieldsize;
16191                     }
16192                 }
16193               gcc_assert (curpos <= size);
16194             }
16195           return true;
16196         }
16197       else if (TREE_CODE (type) == RECORD_TYPE
16198                || TREE_CODE (type) == UNION_TYPE)
16199         {
16200           tree field = NULL_TREE;
16201           unsigned HOST_WIDE_INT cnt;
16202           constructor_elt *ce;
16203
16204           if (int_size_in_bytes (type) != size)
16205             return false;
16206
16207           if (TREE_CODE (type) == RECORD_TYPE)
16208             field = TYPE_FIELDS (type);
16209
16210           FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16211             {
16212               tree val = ce->value;
16213               int pos, fieldsize;
16214
16215               if (ce->index != 0)
16216                 field = ce->index;
16217
16218               if (val)
16219                 STRIP_NOPS (val);
16220
16221               if (field == NULL_TREE || DECL_BIT_FIELD (field))
16222                 return false;
16223
16224               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
16225                   && TYPE_DOMAIN (TREE_TYPE (field))
16226                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
16227                 return false;
16228               else if (DECL_SIZE_UNIT (field) == NULL_TREE
16229                        || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
16230                 return false;
16231               fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
16232               pos = int_byte_position (field);
16233               gcc_assert (pos + fieldsize <= size);
16234               if (val
16235                   && !native_encode_initializer (val, array + pos, fieldsize))
16236                 return false;
16237             }
16238           return true;
16239         }
16240       return false;
16241     case VIEW_CONVERT_EXPR:
16242     case NON_LVALUE_EXPR:
16243       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
16244     default:
16245       return native_encode_expr (init, array, size) == size;
16246     }
16247 }
16248
16249 /* Attach a DW_AT_const_value attribute to DIE. The value of the
16250    attribute is the const value T.  */
16251
16252 static bool
16253 tree_add_const_value_attribute (dw_die_ref die, tree t)
16254 {
16255   tree init;
16256   tree type = TREE_TYPE (t);
16257   rtx rtl;
16258
16259   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
16260     return false;
16261
16262   init = t;
16263   gcc_assert (!DECL_P (init));
16264
16265   rtl = rtl_for_decl_init (init, type);
16266   if (rtl)
16267     return add_const_value_attribute (die, rtl);
16268   /* If the host and target are sane, try harder.  */
16269   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
16270            && initializer_constant_valid_p (init, type))
16271     {
16272       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
16273       if (size > 0 && (int) size == size)
16274         {
16275           unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
16276
16277           if (native_encode_initializer (init, array, size))
16278             {
16279               add_AT_vec (die, DW_AT_const_value, size, 1, array);
16280               return true;
16281             }
16282           ggc_free (array);
16283         }
16284     }
16285   return false;
16286 }
16287
16288 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
16289    attribute is the const value of T, where T is an integral constant
16290    variable with static storage duration
16291    (so it can't be a PARM_DECL or a RESULT_DECL).  */
16292
16293 static bool
16294 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
16295 {
16296
16297   if (!decl
16298       || (TREE_CODE (decl) != VAR_DECL
16299           && TREE_CODE (decl) != CONST_DECL)
16300       || (TREE_CODE (decl) == VAR_DECL
16301           && !TREE_STATIC (decl)))
16302     return false;
16303
16304     if (TREE_READONLY (decl)
16305         && ! TREE_THIS_VOLATILE (decl)
16306         && DECL_INITIAL (decl))
16307       /* OK */;
16308     else
16309       return false;
16310
16311   /* Don't add DW_AT_const_value if abstract origin already has one.  */
16312   if (get_AT (var_die, DW_AT_const_value))
16313     return false;
16314
16315   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
16316 }
16317
16318 /* Convert the CFI instructions for the current function into a
16319    location list.  This is used for DW_AT_frame_base when we targeting
16320    a dwarf2 consumer that does not support the dwarf3
16321    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
16322    expressions.  */
16323
16324 static dw_loc_list_ref
16325 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
16326 {
16327   int ix;
16328   dw_fde_ref fde;
16329   dw_loc_list_ref list, *list_tail;
16330   dw_cfi_ref cfi;
16331   dw_cfa_location last_cfa, next_cfa;
16332   const char *start_label, *last_label, *section;
16333   dw_cfa_location remember;
16334
16335   fde = cfun->fde;
16336   gcc_assert (fde != NULL);
16337
16338   section = secname_for_decl (current_function_decl);
16339   list_tail = &list;
16340   list = NULL;
16341
16342   memset (&next_cfa, 0, sizeof (next_cfa));
16343   next_cfa.reg = INVALID_REGNUM;
16344   remember = next_cfa;
16345
16346   start_label = fde->dw_fde_begin;
16347
16348   /* ??? Bald assumption that the CIE opcode list does not contain
16349      advance opcodes.  */
16350   FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
16351     lookup_cfa_1 (cfi, &next_cfa, &remember);
16352
16353   last_cfa = next_cfa;
16354   last_label = start_label;
16355
16356   if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
16357     {
16358       /* If the first partition contained no CFI adjustments, the
16359          CIE opcodes apply to the whole first partition.  */
16360       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16361                                  fde->dw_fde_begin, fde->dw_fde_end, section);
16362       list_tail =&(*list_tail)->dw_loc_next;
16363       start_label = last_label = fde->dw_fde_second_begin;
16364     }
16365
16366   FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
16367     {
16368       switch (cfi->dw_cfi_opc)
16369         {
16370         case DW_CFA_set_loc:
16371         case DW_CFA_advance_loc1:
16372         case DW_CFA_advance_loc2:
16373         case DW_CFA_advance_loc4:
16374           if (!cfa_equal_p (&last_cfa, &next_cfa))
16375             {
16376               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16377                                          start_label, last_label, section);
16378
16379               list_tail = &(*list_tail)->dw_loc_next;
16380               last_cfa = next_cfa;
16381               start_label = last_label;
16382             }
16383           last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
16384           break;
16385
16386         case DW_CFA_advance_loc:
16387           /* The encoding is complex enough that we should never emit this.  */
16388           gcc_unreachable ();
16389
16390         default:
16391           lookup_cfa_1 (cfi, &next_cfa, &remember);
16392           break;
16393         }
16394       if (ix + 1 == fde->dw_fde_switch_cfi_index)
16395         {
16396           if (!cfa_equal_p (&last_cfa, &next_cfa))
16397             {
16398               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16399                                          start_label, last_label, section);
16400
16401               list_tail = &(*list_tail)->dw_loc_next;
16402               last_cfa = next_cfa;
16403               start_label = last_label;
16404             }
16405           *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16406                                      start_label, fde->dw_fde_end, section);
16407           list_tail = &(*list_tail)->dw_loc_next;
16408           start_label = last_label = fde->dw_fde_second_begin;
16409         }
16410     }
16411
16412   if (!cfa_equal_p (&last_cfa, &next_cfa))
16413     {
16414       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16415                                  start_label, last_label, section);
16416       list_tail = &(*list_tail)->dw_loc_next;
16417       start_label = last_label;
16418     }
16419
16420   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
16421                              start_label,
16422                              fde->dw_fde_second_begin
16423                              ? fde->dw_fde_second_end : fde->dw_fde_end,
16424                              section);
16425
16426   if (list && list->dw_loc_next)
16427     gen_llsym (list);
16428
16429   return list;
16430 }
16431
16432 /* Compute a displacement from the "steady-state frame pointer" to the
16433    frame base (often the same as the CFA), and store it in
16434    frame_pointer_fb_offset.  OFFSET is added to the displacement
16435    before the latter is negated.  */
16436
16437 static void
16438 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
16439 {
16440   rtx reg, elim;
16441
16442 #ifdef FRAME_POINTER_CFA_OFFSET
16443   reg = frame_pointer_rtx;
16444   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
16445 #else
16446   reg = arg_pointer_rtx;
16447   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
16448 #endif
16449
16450   elim = (ira_use_lra_p
16451           ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
16452           : eliminate_regs (reg, VOIDmode, NULL_RTX));
16453   if (GET_CODE (elim) == PLUS)
16454     {
16455       offset += INTVAL (XEXP (elim, 1));
16456       elim = XEXP (elim, 0);
16457     }
16458
16459   frame_pointer_fb_offset = -offset;
16460
16461   /* ??? AVR doesn't set up valid eliminations when there is no stack frame
16462      in which to eliminate.  This is because it's stack pointer isn't 
16463      directly accessible as a register within the ISA.  To work around
16464      this, assume that while we cannot provide a proper value for
16465      frame_pointer_fb_offset, we won't need one either.  */
16466   frame_pointer_fb_offset_valid
16467     = ((SUPPORTS_STACK_ALIGNMENT
16468         && (elim == hard_frame_pointer_rtx
16469             || elim == stack_pointer_rtx))
16470        || elim == (frame_pointer_needed
16471                    ? hard_frame_pointer_rtx
16472                    : stack_pointer_rtx));
16473 }
16474
16475 /* Generate a DW_AT_name attribute given some string value to be included as
16476    the value of the attribute.  */
16477
16478 static void
16479 add_name_attribute (dw_die_ref die, const char *name_string)
16480 {
16481   if (name_string != NULL && *name_string != 0)
16482     {
16483       if (demangle_name_func)
16484         name_string = (*demangle_name_func) (name_string);
16485
16486       add_AT_string (die, DW_AT_name, name_string);
16487     }
16488 }
16489
16490 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
16491    DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
16492    of TYPE accordingly.
16493
16494    ??? This is a temporary measure until after we're able to generate
16495    regular DWARF for the complex Ada type system.  */
16496
16497 static void 
16498 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
16499                                      dw_die_ref context_die)
16500 {
16501   tree dtype;
16502   dw_die_ref dtype_die;
16503
16504   if (!lang_hooks.types.descriptive_type)
16505     return;
16506
16507   dtype = lang_hooks.types.descriptive_type (type);
16508   if (!dtype)
16509     return;
16510
16511   dtype_die = lookup_type_die (dtype);
16512   if (!dtype_die)
16513     {
16514       gen_type_die (dtype, context_die);
16515       dtype_die = lookup_type_die (dtype);
16516       gcc_assert (dtype_die);
16517     }
16518
16519   add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
16520 }
16521
16522 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir.  */
16523
16524 static const char *
16525 comp_dir_string (void)
16526 {
16527   const char *wd;
16528   char *wd1;
16529   static const char *cached_wd = NULL;
16530
16531   if (cached_wd != NULL)
16532     return cached_wd;
16533
16534   wd = get_src_pwd ();
16535   if (wd == NULL)
16536     return NULL;
16537
16538   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16539     {
16540       int wdlen;
16541
16542       wdlen = strlen (wd);
16543       wd1 = ggc_vec_alloc<char> (wdlen + 2);
16544       strcpy (wd1, wd);
16545       wd1 [wdlen] = DIR_SEPARATOR;
16546       wd1 [wdlen + 1] = 0;
16547       wd = wd1;
16548     }
16549
16550   cached_wd = remap_debug_filename (wd);
16551   return cached_wd;
16552 }
16553
16554 /* Generate a DW_AT_comp_dir attribute for DIE.  */
16555
16556 static void
16557 add_comp_dir_attribute (dw_die_ref die)
16558 {
16559   const char * wd = comp_dir_string ();
16560   if (wd != NULL)
16561     add_AT_string (die, DW_AT_comp_dir, wd);
16562 }
16563
16564 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
16565    pointer computation, ...), output a representation for that bound according
16566    to the accepted FORMS (see enum dw_scalar_form) and add it to DIE.  See
16567    loc_list_from_tree for the meaning of CONTEXT.  */
16568
16569 static void
16570 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
16571                  int forms, const struct loc_descr_context *context)
16572 {
16573   dw_die_ref ctx, decl_die;
16574   dw_loc_list_ref list;
16575
16576   bool strip_conversions = true;
16577
16578   while (strip_conversions)
16579     switch (TREE_CODE (value))
16580       {
16581       case ERROR_MARK:
16582       case SAVE_EXPR:
16583         return;
16584
16585       CASE_CONVERT:
16586       case VIEW_CONVERT_EXPR:
16587         value = TREE_OPERAND (value, 0);
16588         break;
16589
16590       default:
16591         strip_conversions = false;
16592         break;
16593       }
16594
16595   /* If possible and permitted, output the attribute as a constant.  */
16596   if ((forms & dw_scalar_form_constant) != 0
16597       && TREE_CODE (value) == INTEGER_CST)
16598     {
16599       unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
16600
16601       /* If HOST_WIDE_INT is big enough then represent the bound as
16602          a constant value.  We need to choose a form based on
16603          whether the type is signed or unsigned.  We cannot just
16604          call add_AT_unsigned if the value itself is positive
16605          (add_AT_unsigned might add the unsigned value encoded as
16606          DW_FORM_data[1248]).  Some DWARF consumers will lookup the
16607          bounds type and then sign extend any unsigned values found
16608          for signed types.  This is needed only for
16609          DW_AT_{lower,upper}_bound, since for most other attributes,
16610          consumers will treat DW_FORM_data[1248] as unsigned values,
16611          regardless of the underlying type.  */
16612       if (prec <= HOST_BITS_PER_WIDE_INT
16613           || tree_fits_uhwi_p (value))
16614         {
16615           if (TYPE_UNSIGNED (TREE_TYPE (value)))
16616             add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
16617           else
16618             add_AT_int (die, attr, TREE_INT_CST_LOW (value));
16619         }
16620       else
16621         /* Otherwise represent the bound as an unsigned value with
16622            the precision of its type.  The precision and signedness
16623            of the type will be necessary to re-interpret it
16624            unambiguously.  */
16625         add_AT_wide (die, attr, value);
16626       return;
16627     }
16628
16629   /* Otherwise, if it's possible and permitted too, output a reference to
16630      another DIE.  */
16631   if ((forms & dw_scalar_form_reference) != 0)
16632     {
16633       tree decl = NULL_TREE;
16634
16635       /* Some type attributes reference an outer type.  For instance, the upper
16636          bound of an array may reference an embedding record (this happens in
16637          Ada).  */
16638       if (TREE_CODE (value) == COMPONENT_REF
16639           && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
16640           && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
16641         decl = TREE_OPERAND (value, 1);
16642
16643       else if (TREE_CODE (value) == VAR_DECL
16644                || TREE_CODE (value) == PARM_DECL
16645                || TREE_CODE (value) == RESULT_DECL)
16646         decl = value;
16647
16648       if (decl != NULL_TREE)
16649         {
16650           dw_die_ref decl_die = lookup_decl_die (decl);
16651
16652           /* ??? Can this happen, or should the variable have been bound
16653              first?  Probably it can, since I imagine that we try to create
16654              the types of parameters in the order in which they exist in
16655              the list, and won't have created a forward reference to a
16656              later parameter.  */
16657           if (decl_die != NULL)
16658             {
16659               add_AT_die_ref (die, attr, decl_die);
16660               return;
16661             }
16662         }
16663     }
16664
16665   /* Last chance: try to create a stack operation procedure to evaluate the
16666      value.  Do nothing if even that is not possible or permitted.  */
16667   if ((forms & dw_scalar_form_exprloc) == 0)
16668     return;
16669
16670   list = loc_list_from_tree (value, 2, context);
16671   if (list == NULL || single_element_loc_list_p (list))
16672     {
16673       /* If this attribute is not a reference nor constant, it is
16674          a DWARF expression rather than location description.  For that
16675          loc_list_from_tree (value, 0, &context) is needed.  */
16676       dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
16677       if (list2 && single_element_loc_list_p (list2))
16678         {
16679           add_AT_loc (die, attr, list2->expr);
16680           return;
16681         }
16682     }
16683
16684   /* If that failed to give a single element location list, fall back to
16685      outputting this as a reference... still if permitted.  */
16686   if (list == NULL || (forms & dw_scalar_form_reference) == 0)
16687     return;
16688
16689   if (current_function_decl == 0)
16690     ctx = comp_unit_die ();
16691   else
16692     ctx = lookup_decl_die (current_function_decl);
16693
16694   decl_die = new_die (DW_TAG_variable, ctx, value);
16695   add_AT_flag (decl_die, DW_AT_artificial, 1);
16696   add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, ctx);
16697   add_AT_location_description (decl_die, DW_AT_location, list);
16698   add_AT_die_ref (die, attr, decl_die);
16699 }
16700
16701 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
16702    default.  */
16703
16704 static int
16705 lower_bound_default (void)
16706 {
16707   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16708     {
16709     case DW_LANG_C:
16710     case DW_LANG_C89:
16711     case DW_LANG_C99:
16712     case DW_LANG_C11:
16713     case DW_LANG_C_plus_plus:
16714     case DW_LANG_C_plus_plus_11:
16715     case DW_LANG_C_plus_plus_14:
16716     case DW_LANG_ObjC:
16717     case DW_LANG_ObjC_plus_plus:
16718     case DW_LANG_Java:
16719       return 0;
16720     case DW_LANG_Fortran77:
16721     case DW_LANG_Fortran90:
16722     case DW_LANG_Fortran95:
16723       return 1;
16724     case DW_LANG_UPC:
16725     case DW_LANG_D:
16726     case DW_LANG_Python:
16727       return dwarf_version >= 4 ? 0 : -1;
16728     case DW_LANG_Ada95:
16729     case DW_LANG_Ada83:
16730     case DW_LANG_Cobol74:
16731     case DW_LANG_Cobol85:
16732     case DW_LANG_Pascal83:
16733     case DW_LANG_Modula2:
16734     case DW_LANG_PLI:
16735       return dwarf_version >= 4 ? 1 : -1;
16736     default:
16737       return -1;
16738     }
16739 }
16740
16741 /* Given a tree node describing an array bound (either lower or upper) output
16742    a representation for that bound.  */
16743
16744 static void
16745 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
16746                 tree bound, const struct loc_descr_context *context)
16747 {
16748   int dflt;
16749
16750   while (1)
16751     switch (TREE_CODE (bound))
16752       {
16753       /* Strip all conversions.  */
16754       CASE_CONVERT:
16755       case VIEW_CONVERT_EXPR:
16756         bound = TREE_OPERAND (bound, 0);
16757         break;
16758
16759       /* All fixed-bounds are represented by INTEGER_CST nodes.  Lower bounds
16760          are even omitted when they are the default.  */
16761       case INTEGER_CST:
16762         /* If the value for this bound is the default one, we can even omit the
16763            attribute.  */
16764         if (bound_attr == DW_AT_lower_bound
16765             && tree_fits_shwi_p (bound)
16766             && (dflt = lower_bound_default ()) != -1
16767             && tree_to_shwi (bound) == dflt)
16768           return;
16769
16770         /* FALLTHRU */
16771
16772       default:
16773         add_scalar_info (subrange_die, bound_attr, bound,
16774                          dw_scalar_form_constant
16775                          | dw_scalar_form_exprloc
16776                          | dw_scalar_form_reference,
16777                          context);
16778         return;
16779       }
16780 }
16781
16782 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16783    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16784    Note that the block of subscript information for an array type also
16785    includes information about the element type of the given array type.  */
16786
16787 static void
16788 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16789 {
16790   unsigned dimension_number;
16791   tree lower, upper;
16792   dw_die_ref subrange_die;
16793
16794   for (dimension_number = 0;
16795        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16796        type = TREE_TYPE (type), dimension_number++)
16797     {
16798       tree domain = TYPE_DOMAIN (type);
16799
16800       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16801         break;
16802
16803       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16804          and (in GNU C only) variable bounds.  Handle all three forms
16805          here.  */
16806       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16807       if (domain)
16808         {
16809           /* We have an array type with specified bounds.  */
16810           lower = TYPE_MIN_VALUE (domain);
16811           upper = TYPE_MAX_VALUE (domain);
16812
16813           /* Define the index type.  */
16814           if (TREE_TYPE (domain))
16815             {
16816               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
16817                  TREE_TYPE field.  We can't emit debug info for this
16818                  because it is an unnamed integral type.  */
16819               if (TREE_CODE (domain) == INTEGER_TYPE
16820                   && TYPE_NAME (domain) == NULL_TREE
16821                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16822                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16823                 ;
16824               else
16825                 add_type_attribute (subrange_die, TREE_TYPE (domain),
16826                                     TYPE_UNQUALIFIED, type_die);
16827             }
16828
16829           /* ??? If upper is NULL, the array has unspecified length,
16830              but it does have a lower bound.  This happens with Fortran
16831                dimension arr(N:*)
16832              Since the debugger is definitely going to need to know N
16833              to produce useful results, go ahead and output the lower
16834              bound solo, and hope the debugger can cope.  */
16835
16836           add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
16837           if (upper)
16838             add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
16839         }
16840
16841       /* Otherwise we have an array type with an unspecified length.  The
16842          DWARF-2 spec does not say how to handle this; let's just leave out the
16843          bounds.  */
16844     }
16845 }
16846
16847 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size.  */
16848
16849 static void
16850 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16851 {
16852   dw_die_ref decl_die;
16853   HOST_WIDE_INT size;
16854
16855   switch (TREE_CODE (tree_node))
16856     {
16857     case ERROR_MARK:
16858       size = 0;
16859       break;
16860     case ENUMERAL_TYPE:
16861     case RECORD_TYPE:
16862     case UNION_TYPE:
16863     case QUAL_UNION_TYPE:
16864       if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
16865           && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
16866         {
16867           add_AT_die_ref (die, DW_AT_byte_size, decl_die);
16868           return;
16869         }
16870       size = int_size_in_bytes (tree_node);
16871       break;
16872     case FIELD_DECL:
16873       /* For a data member of a struct or union, the DW_AT_byte_size is
16874          generally given as the number of bytes normally allocated for an
16875          object of the *declared* type of the member itself.  This is true
16876          even for bit-fields.  */
16877       size = int_size_in_bytes (field_type (tree_node));
16878       break;
16879     default:
16880       gcc_unreachable ();
16881     }
16882
16883   /* Note that `size' might be -1 when we get to this point.  If it is, that
16884      indicates that the byte size of the entity in question is variable.  We
16885      have no good way of expressing this fact in Dwarf at the present time,
16886      when location description was not used by the caller code instead.  */
16887   if (size >= 0)
16888     add_AT_unsigned (die, DW_AT_byte_size, size);
16889 }
16890
16891 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16892    which specifies the distance in bits from the highest order bit of the
16893    "containing object" for the bit-field to the highest order bit of the
16894    bit-field itself.
16895
16896    For any given bit-field, the "containing object" is a hypothetical object
16897    (of some integral or enum type) within which the given bit-field lives.  The
16898    type of this hypothetical "containing object" is always the same as the
16899    declared type of the individual bit-field itself.  The determination of the
16900    exact location of the "containing object" for a bit-field is rather
16901    complicated.  It's handled by the `field_byte_offset' function (above).
16902
16903    Note that it is the size (in bytes) of the hypothetical "containing object"
16904    which will be given in the DW_AT_byte_size attribute for this bit-field.
16905    (See `byte_size_attribute' above).  */
16906
16907 static inline void
16908 add_bit_offset_attribute (dw_die_ref die, tree decl)
16909 {
16910   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16911   tree type = DECL_BIT_FIELD_TYPE (decl);
16912   HOST_WIDE_INT bitpos_int;
16913   HOST_WIDE_INT highest_order_object_bit_offset;
16914   HOST_WIDE_INT highest_order_field_bit_offset;
16915   HOST_WIDE_INT bit_offset;
16916
16917   /* Must be a field and a bit field.  */
16918   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16919
16920   /* We can't yet handle bit-fields whose offsets are variable, so if we
16921      encounter such things, just return without generating any attribute
16922      whatsoever.  Likewise for variable or too large size.  */
16923   if (! tree_fits_shwi_p (bit_position (decl))
16924       || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
16925     return;
16926
16927   bitpos_int = int_bit_position (decl);
16928
16929   /* Note that the bit offset is always the distance (in bits) from the
16930      highest-order bit of the "containing object" to the highest-order bit of
16931      the bit-field itself.  Since the "high-order end" of any object or field
16932      is different on big-endian and little-endian machines, the computation
16933      below must take account of these differences.  */
16934   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
16935   highest_order_field_bit_offset = bitpos_int;
16936
16937   if (! BYTES_BIG_ENDIAN)
16938     {
16939       highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
16940       highest_order_object_bit_offset += simple_type_size_in_bits (type);
16941     }
16942
16943   bit_offset
16944     = (! BYTES_BIG_ENDIAN
16945        ? highest_order_object_bit_offset - highest_order_field_bit_offset
16946        : highest_order_field_bit_offset - highest_order_object_bit_offset);
16947
16948   if (bit_offset < 0)
16949     add_AT_int (die, DW_AT_bit_offset, bit_offset);
16950   else
16951     add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
16952 }
16953
16954 /* For a FIELD_DECL node which represents a bit field, output an attribute
16955    which specifies the length in bits of the given field.  */
16956
16957 static inline void
16958 add_bit_size_attribute (dw_die_ref die, tree decl)
16959 {
16960   /* Must be a field and a bit field.  */
16961   gcc_assert (TREE_CODE (decl) == FIELD_DECL
16962               && DECL_BIT_FIELD_TYPE (decl));
16963
16964   if (tree_fits_uhwi_p (DECL_SIZE (decl)))
16965     add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
16966 }
16967
16968 /* If the compiled language is ANSI C, then add a 'prototyped'
16969    attribute, if arg types are given for the parameters of a function.  */
16970
16971 static inline void
16972 add_prototyped_attribute (dw_die_ref die, tree func_type)
16973 {
16974   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16975     {
16976     case DW_LANG_C:
16977     case DW_LANG_C89:
16978     case DW_LANG_C99:
16979     case DW_LANG_C11:
16980     case DW_LANG_ObjC:
16981       if (prototype_p (func_type))
16982         add_AT_flag (die, DW_AT_prototyped, 1);
16983       break;
16984     default:
16985       break;
16986     }
16987 }
16988
16989 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
16990    by looking in either the type declaration or object declaration
16991    equate table.  */
16992
16993 static inline dw_die_ref
16994 add_abstract_origin_attribute (dw_die_ref die, tree origin)
16995 {
16996   dw_die_ref origin_die = NULL;
16997
16998   if (TREE_CODE (origin) != FUNCTION_DECL)
16999     {
17000       /* We may have gotten separated from the block for the inlined
17001          function, if we're in an exception handler or some such; make
17002          sure that the abstract function has been written out.
17003
17004          Doing this for nested functions is wrong, however; functions are
17005          distinct units, and our context might not even be inline.  */
17006       tree fn = origin;
17007
17008       if (TYPE_P (fn))
17009         fn = TYPE_STUB_DECL (fn);
17010
17011       fn = decl_function_context (fn);
17012       if (fn)
17013         dwarf2out_abstract_function (fn);
17014     }
17015
17016   if (DECL_P (origin))
17017     origin_die = lookup_decl_die (origin);
17018   else if (TYPE_P (origin))
17019     origin_die = lookup_type_die (origin);
17020
17021   /* XXX: Functions that are never lowered don't always have correct block
17022      trees (in the case of java, they simply have no block tree, in some other
17023      languages).  For these functions, there is nothing we can really do to
17024      output correct debug info for inlined functions in all cases.  Rather
17025      than die, we'll just produce deficient debug info now, in that we will
17026      have variables without a proper abstract origin.  In the future, when all
17027      functions are lowered, we should re-add a gcc_assert (origin_die)
17028      here.  */
17029
17030   if (origin_die)
17031     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
17032   return origin_die;
17033 }
17034
17035 /* We do not currently support the pure_virtual attribute.  */
17036
17037 static inline void
17038 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
17039 {
17040   if (DECL_VINDEX (func_decl))
17041     {
17042       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
17043
17044       if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
17045         add_AT_loc (die, DW_AT_vtable_elem_location,
17046                     new_loc_descr (DW_OP_constu,
17047                                    tree_to_shwi (DECL_VINDEX (func_decl)),
17048                                    0));
17049
17050       /* GNU extension: Record what type this method came from originally.  */
17051       if (debug_info_level > DINFO_LEVEL_TERSE
17052           && DECL_CONTEXT (func_decl))
17053         add_AT_die_ref (die, DW_AT_containing_type,
17054                         lookup_type_die (DECL_CONTEXT (func_decl)));
17055     }
17056 }
17057 \f
17058 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
17059    given decl.  This used to be a vendor extension until after DWARF 4
17060    standardized it.  */
17061
17062 static void
17063 add_linkage_attr (dw_die_ref die, tree decl)
17064 {
17065   const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
17066
17067   /* Mimic what assemble_name_raw does with a leading '*'.  */
17068   if (name[0] == '*')
17069     name = &name[1];
17070
17071   if (dwarf_version >= 4)
17072     add_AT_string (die, DW_AT_linkage_name, name);
17073   else
17074     add_AT_string (die, DW_AT_MIPS_linkage_name, name);
17075 }
17076
17077 /* Add source coordinate attributes for the given decl.  */
17078
17079 static void
17080 add_src_coords_attributes (dw_die_ref die, tree decl)
17081 {
17082   expanded_location s;
17083
17084   if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
17085     return;
17086   s = expand_location (DECL_SOURCE_LOCATION (decl));
17087   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
17088   add_AT_unsigned (die, DW_AT_decl_line, s.line);
17089 }
17090
17091 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl.  */
17092
17093 static void
17094 add_linkage_name (dw_die_ref die, tree decl)
17095 {
17096   if (debug_info_level > DINFO_LEVEL_NONE
17097       && (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
17098       && TREE_PUBLIC (decl)
17099       && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
17100       && die->die_tag != DW_TAG_member)
17101     {
17102       /* Defer until we have an assembler name set.  */
17103       if (!DECL_ASSEMBLER_NAME_SET_P (decl))
17104         {
17105           limbo_die_node *asm_name;
17106
17107           asm_name = ggc_cleared_alloc<limbo_die_node> ();
17108           asm_name->die = die;
17109           asm_name->created_for = decl;
17110           asm_name->next = deferred_asm_name;
17111           deferred_asm_name = asm_name;
17112         }
17113       else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
17114         add_linkage_attr (die, decl);
17115     }
17116 }
17117
17118 /* Add a DW_AT_name attribute and source coordinate attribute for the
17119    given decl, but only if it actually has a name.  */
17120
17121 static void
17122 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
17123 {
17124   tree decl_name;
17125
17126   decl_name = DECL_NAME (decl);
17127   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
17128     {
17129       const char *name = dwarf2_name (decl, 0);
17130       if (name)
17131         add_name_attribute (die, name);
17132       if (! DECL_ARTIFICIAL (decl))
17133         add_src_coords_attributes (die, decl);
17134
17135       add_linkage_name (die, decl);
17136     }
17137
17138 #ifdef VMS_DEBUGGING_INFO
17139   /* Get the function's name, as described by its RTL.  This may be different
17140      from the DECL_NAME name used in the source file.  */
17141   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
17142     {
17143       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
17144                   XEXP (DECL_RTL (decl), 0), false);
17145       vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
17146     }
17147 #endif /* VMS_DEBUGGING_INFO */
17148 }
17149
17150 #ifdef VMS_DEBUGGING_INFO
17151 /* Output the debug main pointer die for VMS */
17152
17153 void
17154 dwarf2out_vms_debug_main_pointer (void)
17155 {
17156   char label[MAX_ARTIFICIAL_LABEL_BYTES];
17157   dw_die_ref die;
17158
17159   /* Allocate the VMS debug main subprogram die.  */
17160   die = ggc_cleared_alloc<die_node> ();
17161   die->die_tag = DW_TAG_subprogram;
17162   add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
17163   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
17164                                current_function_funcdef_no);
17165   add_AT_lbl_id (die, DW_AT_entry_pc, label);
17166
17167   /* Make it the first child of comp_unit_die ().  */
17168   die->die_parent = comp_unit_die ();
17169   if (comp_unit_die ()->die_child)
17170     {
17171       die->die_sib = comp_unit_die ()->die_child->die_sib;
17172       comp_unit_die ()->die_child->die_sib = die;
17173     }
17174   else
17175     {
17176       die->die_sib = die;
17177       comp_unit_die ()->die_child = die;
17178     }
17179 }
17180 #endif /* VMS_DEBUGGING_INFO */
17181
17182 /* Push a new declaration scope.  */
17183
17184 static void
17185 push_decl_scope (tree scope)
17186 {
17187   vec_safe_push (decl_scope_table, scope);
17188 }
17189
17190 /* Pop a declaration scope.  */
17191
17192 static inline void
17193 pop_decl_scope (void)
17194 {
17195   decl_scope_table->pop ();
17196 }
17197
17198 /* walk_tree helper function for uses_local_type, below.  */
17199
17200 static tree
17201 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
17202 {
17203   if (!TYPE_P (*tp))
17204     *walk_subtrees = 0;
17205   else
17206     {
17207       tree name = TYPE_NAME (*tp);
17208       if (name && DECL_P (name) && decl_function_context (name))
17209         return *tp;
17210     }
17211   return NULL_TREE;
17212 }
17213
17214 /* If TYPE involves a function-local type (including a local typedef to a
17215    non-local type), returns that type; otherwise returns NULL_TREE.  */
17216
17217 static tree
17218 uses_local_type (tree type)
17219 {
17220   tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
17221   return used;
17222 }
17223
17224 /* Return the DIE for the scope that immediately contains this type.
17225    Non-named types that do not involve a function-local type get global
17226    scope.  Named types nested in namespaces or other types get their
17227    containing scope.  All other types (i.e. function-local named types) get
17228    the current active scope.  */
17229
17230 static dw_die_ref
17231 scope_die_for (tree t, dw_die_ref context_die)
17232 {
17233   dw_die_ref scope_die = NULL;
17234   tree containing_scope;
17235
17236   /* Non-types always go in the current scope.  */
17237   gcc_assert (TYPE_P (t));
17238
17239   /* Use the scope of the typedef, rather than the scope of the type
17240      it refers to.  */
17241   if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
17242     containing_scope = DECL_CONTEXT (TYPE_NAME (t));
17243   else
17244     containing_scope = TYPE_CONTEXT (t);
17245
17246   /* Use the containing namespace if there is one.  */
17247   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
17248     {
17249       if (context_die == lookup_decl_die (containing_scope))
17250         /* OK */;
17251       else if (debug_info_level > DINFO_LEVEL_TERSE)
17252         context_die = get_context_die (containing_scope);
17253       else
17254         containing_scope = NULL_TREE;
17255     }
17256
17257   /* Ignore function type "scopes" from the C frontend.  They mean that
17258      a tagged type is local to a parmlist of a function declarator, but
17259      that isn't useful to DWARF.  */
17260   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
17261     containing_scope = NULL_TREE;
17262
17263   if (SCOPE_FILE_SCOPE_P (containing_scope))
17264     {
17265       /* If T uses a local type keep it local as well, to avoid references
17266          to function-local DIEs from outside the function.  */
17267       if (current_function_decl && uses_local_type (t))
17268         scope_die = context_die;
17269       else
17270         scope_die = comp_unit_die ();
17271     }
17272   else if (TYPE_P (containing_scope))
17273     {
17274       /* For types, we can just look up the appropriate DIE.  */
17275       if (debug_info_level > DINFO_LEVEL_TERSE)
17276         scope_die = get_context_die (containing_scope);
17277       else
17278         {
17279           scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
17280           if (scope_die == NULL)
17281             scope_die = comp_unit_die ();
17282         }
17283     }
17284   else
17285     scope_die = context_die;
17286
17287   return scope_die;
17288 }
17289
17290 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
17291
17292 static inline int
17293 local_scope_p (dw_die_ref context_die)
17294 {
17295   for (; context_die; context_die = context_die->die_parent)
17296     if (context_die->die_tag == DW_TAG_inlined_subroutine
17297         || context_die->die_tag == DW_TAG_subprogram)
17298       return 1;
17299
17300   return 0;
17301 }
17302
17303 /* Returns nonzero if CONTEXT_DIE is a class.  */
17304
17305 static inline int
17306 class_scope_p (dw_die_ref context_die)
17307 {
17308   return (context_die
17309           && (context_die->die_tag == DW_TAG_structure_type
17310               || context_die->die_tag == DW_TAG_class_type
17311               || context_die->die_tag == DW_TAG_interface_type
17312               || context_die->die_tag == DW_TAG_union_type));
17313 }
17314
17315 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17316    whether or not to treat a DIE in this context as a declaration.  */
17317
17318 static inline int
17319 class_or_namespace_scope_p (dw_die_ref context_die)
17320 {
17321   return (class_scope_p (context_die)
17322           || (context_die && context_die->die_tag == DW_TAG_namespace));
17323 }
17324
17325 /* Many forms of DIEs require a "type description" attribute.  This
17326    routine locates the proper "type descriptor" die for the type given
17327    by 'type' plus any additional qualifiers given by 'cv_quals', and
17328    adds a DW_AT_type attribute below the given die.  */
17329
17330 static void
17331 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
17332                     dw_die_ref context_die)
17333 {
17334   enum tree_code code  = TREE_CODE (type);
17335   dw_die_ref type_die  = NULL;
17336
17337   /* ??? If this type is an unnamed subrange type of an integral, floating-point
17338      or fixed-point type, use the inner type.  This is because we have no
17339      support for unnamed types in base_type_die.  This can happen if this is
17340      an Ada subrange type.  Correct solution is emit a subrange type die.  */
17341   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
17342       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
17343     type = TREE_TYPE (type), code = TREE_CODE (type);
17344
17345   if (code == ERROR_MARK
17346       /* Handle a special case.  For functions whose return type is void, we
17347          generate *no* type attribute.  (Note that no object may have type
17348          `void', so this only applies to function return types).  */
17349       || code == VOID_TYPE)
17350     return;
17351
17352   type_die = modified_type_die (type,
17353                                 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
17354                                 context_die);
17355
17356   if (type_die != NULL)
17357     add_AT_die_ref (object_die, DW_AT_type, type_die);
17358 }
17359
17360 /* Given an object die, add the calling convention attribute for the
17361    function call type.  */
17362 static void
17363 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
17364 {
17365   enum dwarf_calling_convention value = DW_CC_normal;
17366
17367   value = ((enum dwarf_calling_convention)
17368            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
17369
17370   if (is_fortran ()
17371       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
17372     {
17373       /* DWARF 2 doesn't provide a way to identify a program's source-level
17374         entry point.  DW_AT_calling_convention attributes are only meant
17375         to describe functions' calling conventions.  However, lacking a
17376         better way to signal the Fortran main program, we used this for 
17377         a long time, following existing custom.  Now, DWARF 4 has 
17378         DW_AT_main_subprogram, which we add below, but some tools still
17379         rely on the old way, which we thus keep.  */
17380       value = DW_CC_program;
17381
17382       if (dwarf_version >= 4 || !dwarf_strict)
17383         add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
17384     }
17385
17386   /* Only add the attribute if the backend requests it, and
17387      is not DW_CC_normal.  */
17388   if (value && (value != DW_CC_normal))
17389     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
17390 }
17391
17392 /* Given a tree pointer to a struct, class, union, or enum type node, return
17393    a pointer to the (string) tag name for the given type, or zero if the type
17394    was declared without a tag.  */
17395
17396 static const char *
17397 type_tag (const_tree type)
17398 {
17399   const char *name = 0;
17400
17401   if (TYPE_NAME (type) != 0)
17402     {
17403       tree t = 0;
17404
17405       /* Find the IDENTIFIER_NODE for the type name.  */
17406       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
17407           && !TYPE_NAMELESS (type))
17408         t = TYPE_NAME (type);
17409
17410       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
17411          a TYPE_DECL node, regardless of whether or not a `typedef' was
17412          involved.  */
17413       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
17414                && ! DECL_IGNORED_P (TYPE_NAME (type)))
17415         {
17416           /* We want to be extra verbose.  Don't call dwarf_name if
17417              DECL_NAME isn't set.  The default hook for decl_printable_name
17418              doesn't like that, and in this context it's correct to return
17419              0, instead of "<anonymous>" or the like.  */
17420           if (DECL_NAME (TYPE_NAME (type))
17421               && !DECL_NAMELESS (TYPE_NAME (type)))
17422             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
17423         }
17424
17425       /* Now get the name as a string, or invent one.  */
17426       if (!name && t != 0)
17427         name = IDENTIFIER_POINTER (t);
17428     }
17429
17430   return (name == 0 || *name == '\0') ? 0 : name;
17431 }
17432
17433 /* Return the type associated with a data member, make a special check
17434    for bit field types.  */
17435
17436 static inline tree
17437 member_declared_type (const_tree member)
17438 {
17439   return (DECL_BIT_FIELD_TYPE (member)
17440           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
17441 }
17442
17443 /* Get the decl's label, as described by its RTL. This may be different
17444    from the DECL_NAME name used in the source file.  */
17445
17446 #if 0
17447 static const char *
17448 decl_start_label (tree decl)
17449 {
17450   rtx x;
17451   const char *fnname;
17452
17453   x = DECL_RTL (decl);
17454   gcc_assert (MEM_P (x));
17455
17456   x = XEXP (x, 0);
17457   gcc_assert (GET_CODE (x) == SYMBOL_REF);
17458
17459   fnname = XSTR (x, 0);
17460   return fnname;
17461 }
17462 #endif
17463 \f
17464 /* These routines generate the internal representation of the DIE's for
17465    the compilation unit.  Debugging information is collected by walking
17466    the declaration trees passed in from dwarf2out_decl().  */
17467
17468 static void
17469 gen_array_type_die (tree type, dw_die_ref context_die)
17470 {
17471   dw_die_ref scope_die = scope_die_for (type, context_die);
17472   dw_die_ref array_die;
17473
17474   /* GNU compilers represent multidimensional array types as sequences of one
17475      dimensional array types whose element types are themselves array types.
17476      We sometimes squish that down to a single array_type DIE with multiple
17477      subscripts in the Dwarf debugging info.  The draft Dwarf specification
17478      say that we are allowed to do this kind of compression in C, because
17479      there is no difference between an array of arrays and a multidimensional
17480      array.  We don't do this for Ada to remain as close as possible to the
17481      actual representation, which is especially important against the language
17482      flexibilty wrt arrays of variable size.  */
17483
17484   bool collapse_nested_arrays = !is_ada ();
17485   tree element_type;
17486
17487   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
17488      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
17489   if (TYPE_STRING_FLAG (type)
17490       && TREE_CODE (type) == ARRAY_TYPE
17491       && is_fortran ()
17492       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
17493     {
17494       HOST_WIDE_INT size;
17495
17496       array_die = new_die (DW_TAG_string_type, scope_die, type);
17497       add_name_attribute (array_die, type_tag (type));
17498       equate_type_number_to_die (type, array_die);
17499       size = int_size_in_bytes (type);
17500       if (size >= 0)
17501         add_AT_unsigned (array_die, DW_AT_byte_size, size);
17502       else if (TYPE_DOMAIN (type) != NULL_TREE
17503                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
17504                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17505         {
17506           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17507           dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2, NULL);
17508
17509           size = int_size_in_bytes (TREE_TYPE (szdecl));
17510           if (loc && size > 0)
17511             {
17512               add_AT_location_description (array_die, DW_AT_string_length, loc);
17513               if (size != DWARF2_ADDR_SIZE)
17514                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17515             }
17516         }
17517       return;
17518     }
17519
17520   array_die = new_die (DW_TAG_array_type, scope_die, type);
17521   add_name_attribute (array_die, type_tag (type));
17522   equate_type_number_to_die (type, array_die);
17523
17524   if (TREE_CODE (type) == VECTOR_TYPE)
17525     add_AT_flag (array_die, DW_AT_GNU_vector, 1);
17526
17527   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
17528   if (is_fortran ()
17529       && TREE_CODE (type) == ARRAY_TYPE
17530       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17531       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17532     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17533
17534 #if 0
17535   /* We default the array ordering.  SDB will probably do
17536      the right things even if DW_AT_ordering is not present.  It's not even
17537      an issue until we start to get into multidimensional arrays anyway.  If
17538      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17539      then we'll have to put the DW_AT_ordering attribute back in.  (But if
17540      and when we find out that we need to put these in, we will only do so
17541      for multidimensional arrays.  */
17542   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17543 #endif
17544
17545   if (TREE_CODE (type) == VECTOR_TYPE)
17546     {
17547       /* For VECTOR_TYPEs we use an array die with appropriate bounds.  */
17548       dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
17549       add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
17550       add_bound_info (subrange_die, DW_AT_upper_bound,
17551                       size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
17552     }
17553   else
17554     add_subscript_info (array_die, type, collapse_nested_arrays);
17555
17556   /* Add representation of the type of the elements of this array type and
17557      emit the corresponding DIE if we haven't done it already.  */
17558   element_type = TREE_TYPE (type);
17559   if (collapse_nested_arrays)
17560     while (TREE_CODE (element_type) == ARRAY_TYPE)
17561       {
17562         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17563           break;
17564         element_type = TREE_TYPE (element_type);
17565       }
17566
17567   add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED, context_die);
17568
17569   add_gnat_descriptive_type_attribute (array_die, type, context_die);
17570   if (TYPE_ARTIFICIAL (type))
17571     add_AT_flag (array_die, DW_AT_artificial, 1);
17572
17573   if (get_AT (array_die, DW_AT_name))
17574     add_pubtype (type, array_die);
17575 }
17576
17577 /* This routine generates DIE for array with hidden descriptor, details
17578    are filled into *info by a langhook.  */
17579
17580 static void
17581 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17582                           dw_die_ref context_die)
17583 {
17584   const dw_die_ref scope_die = scope_die_for (type, context_die);
17585   const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
17586   const struct loc_descr_context context = { type, info->base_decl };
17587   int dim;
17588
17589   add_name_attribute (array_die, type_tag (type));
17590   equate_type_number_to_die (type, array_die);
17591
17592   if (info->ndimensions > 1)
17593     switch (info->ordering)
17594       {
17595       case array_descr_ordering_row_major:
17596         add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17597         break;
17598       case array_descr_ordering_column_major:
17599         add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17600         break;
17601       default:
17602         break;
17603       }
17604
17605   if (dwarf_version >= 3 || !dwarf_strict)
17606     {
17607       if (info->data_location)
17608         add_scalar_info (array_die, DW_AT_data_location, info->data_location,
17609                          dw_scalar_form_exprloc, &context);
17610       if (info->associated)
17611         add_scalar_info (array_die, DW_AT_associated, info->associated,
17612                          dw_scalar_form_constant
17613                          | dw_scalar_form_exprloc
17614                          | dw_scalar_form_reference, &context);
17615       if (info->allocated)
17616         add_scalar_info (array_die, DW_AT_allocated, info->allocated,
17617                          dw_scalar_form_constant
17618                          | dw_scalar_form_exprloc
17619                          | dw_scalar_form_reference, &context);
17620     }
17621
17622   add_gnat_descriptive_type_attribute (array_die, type, context_die);
17623
17624   for (dim = 0; dim < info->ndimensions; dim++)
17625     {
17626       dw_die_ref subrange_die
17627         = new_die (DW_TAG_subrange_type, array_die, NULL);
17628
17629       if (info->dimen[dim].bounds_type)
17630         add_type_attribute (subrange_die,
17631                             info->dimen[dim].bounds_type, 0,
17632                             context_die);
17633       if (info->dimen[dim].lower_bound)
17634         add_bound_info (subrange_die, DW_AT_lower_bound,
17635                         info->dimen[dim].lower_bound, &context);
17636       if (info->dimen[dim].upper_bound)
17637         add_bound_info (subrange_die, DW_AT_upper_bound,
17638                         info->dimen[dim].upper_bound, &context);
17639       if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
17640         add_scalar_info (subrange_die, DW_AT_byte_stride,
17641                          info->dimen[dim].stride,
17642                          dw_scalar_form_constant
17643                          | dw_scalar_form_exprloc
17644                          | dw_scalar_form_reference,
17645                          &context);
17646     }
17647
17648   gen_type_die (info->element_type, context_die);
17649   add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
17650                       context_die);
17651
17652   if (get_AT (array_die, DW_AT_name))
17653     add_pubtype (type, array_die);
17654 }
17655
17656 #if 0
17657 static void
17658 gen_entry_point_die (tree decl, dw_die_ref context_die)
17659 {
17660   tree origin = decl_ultimate_origin (decl);
17661   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17662
17663   if (origin != NULL)
17664     add_abstract_origin_attribute (decl_die, origin);
17665   else
17666     {
17667       add_name_and_src_coords_attributes (decl_die, decl);
17668       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17669                           TYPE_UNQUALIFIED, context_die);
17670     }
17671
17672   if (DECL_ABSTRACT_P (decl))
17673     equate_decl_number_to_die (decl, decl_die);
17674   else
17675     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17676 }
17677 #endif
17678
17679 /* Walk through the list of incomplete types again, trying once more to
17680    emit full debugging info for them.  */
17681
17682 static void
17683 retry_incomplete_types (void)
17684 {
17685   int i;
17686
17687   for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17688     if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17689       gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17690 }
17691
17692 /* Determine what tag to use for a record type.  */
17693
17694 static enum dwarf_tag
17695 record_type_tag (tree type)
17696 {
17697   if (! lang_hooks.types.classify_record)
17698     return DW_TAG_structure_type;
17699
17700   switch (lang_hooks.types.classify_record (type))
17701     {
17702     case RECORD_IS_STRUCT:
17703       return DW_TAG_structure_type;
17704
17705     case RECORD_IS_CLASS:
17706       return DW_TAG_class_type;
17707
17708     case RECORD_IS_INTERFACE:
17709       if (dwarf_version >= 3 || !dwarf_strict)
17710         return DW_TAG_interface_type;
17711       return DW_TAG_structure_type;
17712
17713     default:
17714       gcc_unreachable ();
17715     }
17716 }
17717
17718 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
17719    include all of the information about the enumeration values also. Each
17720    enumerated type name/value is listed as a child of the enumerated type
17721    DIE.  */
17722
17723 static dw_die_ref
17724 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17725 {
17726   dw_die_ref type_die = lookup_type_die (type);
17727
17728   if (type_die == NULL)
17729     {
17730       type_die = new_die (DW_TAG_enumeration_type,
17731                           scope_die_for (type, context_die), type);
17732       equate_type_number_to_die (type, type_die);
17733       add_name_attribute (type_die, type_tag (type));
17734       if (dwarf_version >= 4 || !dwarf_strict)
17735         {
17736           if (ENUM_IS_SCOPED (type))
17737             add_AT_flag (type_die, DW_AT_enum_class, 1);
17738           if (ENUM_IS_OPAQUE (type))
17739             add_AT_flag (type_die, DW_AT_declaration, 1);
17740         }
17741     }
17742   else if (! TYPE_SIZE (type))
17743     return type_die;
17744   else
17745     remove_AT (type_die, DW_AT_declaration);
17746
17747   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
17748      given enum type is incomplete, do not generate the DW_AT_byte_size
17749      attribute or the DW_AT_element_list attribute.  */
17750   if (TYPE_SIZE (type))
17751     {
17752       tree link;
17753
17754       TREE_ASM_WRITTEN (type) = 1;
17755       add_byte_size_attribute (type_die, type);
17756       if (dwarf_version >= 3 || !dwarf_strict)
17757         {
17758           tree underlying = lang_hooks.types.enum_underlying_base_type (type);
17759           add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED,
17760                               context_die);
17761         }
17762       if (TYPE_STUB_DECL (type) != NULL_TREE)
17763         {
17764           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17765           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17766         }
17767
17768       /* If the first reference to this type was as the return type of an
17769          inline function, then it may not have a parent.  Fix this now.  */
17770       if (type_die->die_parent == NULL)
17771         add_child_die (scope_die_for (type, context_die), type_die);
17772
17773       for (link = TYPE_VALUES (type);
17774            link != NULL; link = TREE_CHAIN (link))
17775         {
17776           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17777           tree value = TREE_VALUE (link);
17778
17779           add_name_attribute (enum_die,
17780                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17781
17782           if (TREE_CODE (value) == CONST_DECL)
17783             value = DECL_INITIAL (value);
17784
17785           if (simple_type_size_in_bits (TREE_TYPE (value))
17786               <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
17787             {
17788               /* For constant forms created by add_AT_unsigned DWARF
17789                  consumers (GDB, elfutils, etc.) always zero extend
17790                  the value.  Only when the actual value is negative
17791                  do we need to use add_AT_int to generate a constant
17792                  form that can represent negative values.  */
17793               HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
17794               if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
17795                 add_AT_unsigned (enum_die, DW_AT_const_value,
17796                                  (unsigned HOST_WIDE_INT) val);
17797               else
17798                 add_AT_int (enum_die, DW_AT_const_value, val);
17799             }
17800           else
17801             /* Enumeration constants may be wider than HOST_WIDE_INT.  Handle
17802                that here.  TODO: This should be re-worked to use correct
17803                signed/unsigned double tags for all cases.  */
17804             add_AT_wide (enum_die, DW_AT_const_value, value);
17805         }
17806
17807       add_gnat_descriptive_type_attribute (type_die, type, context_die);
17808       if (TYPE_ARTIFICIAL (type))
17809         add_AT_flag (type_die, DW_AT_artificial, 1);
17810     }
17811   else
17812     add_AT_flag (type_die, DW_AT_declaration, 1);
17813
17814   add_pubtype (type, type_die);
17815
17816   return type_die;
17817 }
17818
17819 /* Generate a DIE to represent either a real live formal parameter decl or to
17820    represent just the type of some formal parameter position in some function
17821    type.
17822
17823    Note that this routine is a bit unusual because its argument may be a
17824    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17825    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17826    node.  If it's the former then this function is being called to output a
17827    DIE to represent a formal parameter object (or some inlining thereof).  If
17828    it's the latter, then this function is only being called to output a
17829    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17830    argument type of some subprogram type.
17831    If EMIT_NAME_P is true, name and source coordinate attributes
17832    are emitted.  */
17833
17834 static dw_die_ref
17835 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17836                           dw_die_ref context_die)
17837 {
17838   tree node_or_origin = node ? node : origin;
17839   tree ultimate_origin;
17840   dw_die_ref parm_die
17841     = new_die (DW_TAG_formal_parameter, context_die, node);
17842
17843   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
17844     {
17845     case tcc_declaration:
17846       ultimate_origin = decl_ultimate_origin (node_or_origin);
17847       if (node || ultimate_origin)
17848         origin = ultimate_origin;
17849       if (origin != NULL)
17850         add_abstract_origin_attribute (parm_die, origin);
17851       else if (emit_name_p)
17852         add_name_and_src_coords_attributes (parm_die, node);
17853       if (origin == NULL
17854           || (! DECL_ABSTRACT_P (node_or_origin)
17855               && variably_modified_type_p (TREE_TYPE (node_or_origin),
17856                                            decl_function_context
17857                                                             (node_or_origin))))
17858         {
17859           tree type = TREE_TYPE (node_or_origin);
17860           if (decl_by_reference_p (node_or_origin))
17861             add_type_attribute (parm_die, TREE_TYPE (type),
17862                                 TYPE_UNQUALIFIED, context_die);
17863           else
17864             add_type_attribute (parm_die, type,
17865                                 decl_quals (node_or_origin),
17866                                 context_die);
17867         }
17868       if (origin == NULL && DECL_ARTIFICIAL (node))
17869         add_AT_flag (parm_die, DW_AT_artificial, 1);
17870
17871       if (node && node != origin)
17872         equate_decl_number_to_die (node, parm_die);
17873       if (! DECL_ABSTRACT_P (node_or_origin))
17874         add_location_or_const_value_attribute (parm_die, node_or_origin,
17875                                                node == NULL, DW_AT_location);
17876
17877       break;
17878
17879     case tcc_type:
17880       /* We were called with some kind of a ..._TYPE node.  */
17881       add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED,
17882                           context_die);
17883       break;
17884
17885     default:
17886       gcc_unreachable ();
17887     }
17888
17889   return parm_die;
17890 }
17891
17892 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17893    children DW_TAG_formal_parameter DIEs representing the arguments of the
17894    parameter pack.
17895
17896    PARM_PACK must be a function parameter pack.
17897    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17898    must point to the subsequent arguments of the function PACK_ARG belongs to.
17899    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17900    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17901    following the last one for which a DIE was generated.  */
17902
17903 static dw_die_ref
17904 gen_formal_parameter_pack_die  (tree parm_pack,
17905                                 tree pack_arg,
17906                                 dw_die_ref subr_die,
17907                                 tree *next_arg)
17908 {
17909   tree arg;
17910   dw_die_ref parm_pack_die;
17911
17912   gcc_assert (parm_pack
17913               && lang_hooks.function_parameter_pack_p (parm_pack)
17914               && subr_die);
17915
17916   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17917   add_src_coords_attributes (parm_pack_die, parm_pack);
17918
17919   for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
17920     {
17921       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17922                                                                  parm_pack))
17923         break;
17924       gen_formal_parameter_die (arg, NULL,
17925                                 false /* Don't emit name attribute.  */,
17926                                 parm_pack_die);
17927     }
17928   if (next_arg)
17929     *next_arg = arg;
17930   return parm_pack_die;
17931 }
17932
17933 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17934    at the end of an (ANSI prototyped) formal parameters list.  */
17935
17936 static void
17937 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
17938 {
17939   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
17940 }
17941
17942 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
17943    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
17944    parameters as specified in some function type specification (except for
17945    those which appear as part of a function *definition*).  */
17946
17947 static void
17948 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
17949 {
17950   tree link;
17951   tree formal_type = NULL;
17952   tree first_parm_type;
17953   tree arg;
17954
17955   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
17956     {
17957       arg = DECL_ARGUMENTS (function_or_method_type);
17958       function_or_method_type = TREE_TYPE (function_or_method_type);
17959     }
17960   else
17961     arg = NULL_TREE;
17962
17963   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
17964
17965   /* Make our first pass over the list of formal parameter types and output a
17966      DW_TAG_formal_parameter DIE for each one.  */
17967   for (link = first_parm_type; link; )
17968     {
17969       dw_die_ref parm_die;
17970
17971       formal_type = TREE_VALUE (link);
17972       if (formal_type == void_type_node)
17973         break;
17974
17975       /* Output a (nameless) DIE to represent the formal parameter itself.  */
17976       if (!POINTER_BOUNDS_TYPE_P (formal_type))
17977         {
17978           parm_die = gen_formal_parameter_die (formal_type, NULL,
17979                                                true /* Emit name attribute.  */,
17980                                                context_die);
17981           if (TREE_CODE (function_or_method_type) == METHOD_TYPE
17982               && link == first_parm_type)
17983             {
17984               add_AT_flag (parm_die, DW_AT_artificial, 1);
17985               if (dwarf_version >= 3 || !dwarf_strict)
17986                 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
17987             }
17988           else if (arg && DECL_ARTIFICIAL (arg))
17989             add_AT_flag (parm_die, DW_AT_artificial, 1);
17990         }
17991
17992       link = TREE_CHAIN (link);
17993       if (arg)
17994         arg = DECL_CHAIN (arg);
17995     }
17996
17997   /* If this function type has an ellipsis, add a
17998      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
17999   if (formal_type != void_type_node)
18000     gen_unspecified_parameters_die (function_or_method_type, context_die);
18001
18002   /* Make our second (and final) pass over the list of formal parameter types
18003      and output DIEs to represent those types (as necessary).  */
18004   for (link = TYPE_ARG_TYPES (function_or_method_type);
18005        link && TREE_VALUE (link);
18006        link = TREE_CHAIN (link))
18007     gen_type_die (TREE_VALUE (link), context_die);
18008 }
18009
18010 /* We want to generate the DIE for TYPE so that we can generate the
18011    die for MEMBER, which has been defined; we will need to refer back
18012    to the member declaration nested within TYPE.  If we're trying to
18013    generate minimal debug info for TYPE, processing TYPE won't do the
18014    trick; we need to attach the member declaration by hand.  */
18015
18016 static void
18017 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
18018 {
18019   gen_type_die (type, context_die);
18020
18021   /* If we're trying to avoid duplicate debug info, we may not have
18022      emitted the member decl for this function.  Emit it now.  */
18023   if (TYPE_STUB_DECL (type)
18024       && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
18025       && ! lookup_decl_die (member))
18026     {
18027       dw_die_ref type_die;
18028       gcc_assert (!decl_ultimate_origin (member));
18029
18030       push_decl_scope (type);
18031       type_die = lookup_type_die_strip_naming_typedef (type);
18032       if (TREE_CODE (member) == FUNCTION_DECL)
18033         gen_subprogram_die (member, type_die);
18034       else if (TREE_CODE (member) == FIELD_DECL)
18035         {
18036           /* Ignore the nameless fields that are used to skip bits but handle
18037              C++ anonymous unions and structs.  */
18038           if (DECL_NAME (member) != NULL_TREE
18039               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
18040               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
18041             {
18042               gen_type_die (member_declared_type (member), type_die);
18043               gen_field_die (member, type_die);
18044             }
18045         }
18046       else
18047         gen_variable_die (member, NULL_TREE, type_die);
18048
18049       pop_decl_scope ();
18050     }
18051 }
18052 \f
18053 /* Forward declare these functions, because they are mutually recursive
18054   with their set_block_* pairing functions.  */
18055 static void set_decl_origin_self (tree);
18056 static void set_decl_abstract_flags (tree, int);
18057
18058 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
18059    given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
18060    that it points to the node itself, thus indicating that the node is its
18061    own (abstract) origin.  Additionally, if the BLOCK_ABSTRACT_ORIGIN for
18062    the given node is NULL, recursively descend the decl/block tree which
18063    it is the root of, and for each other ..._DECL or BLOCK node contained
18064    therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
18065    still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
18066    values to point to themselves.  */
18067
18068 static void
18069 set_block_origin_self (tree stmt)
18070 {
18071   if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
18072     {
18073       BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
18074
18075       {
18076         tree local_decl;
18077
18078         for (local_decl = BLOCK_VARS (stmt);
18079              local_decl != NULL_TREE;
18080              local_decl = DECL_CHAIN (local_decl))
18081           /* Do not recurse on nested functions since the inlining status
18082              of parent and child can be different as per the DWARF spec.  */
18083           if (TREE_CODE (local_decl) != FUNCTION_DECL
18084               && !DECL_EXTERNAL (local_decl))
18085             set_decl_origin_self (local_decl);
18086       }
18087
18088       {
18089         tree subblock;
18090
18091         for (subblock = BLOCK_SUBBLOCKS (stmt);
18092              subblock != NULL_TREE;
18093              subblock = BLOCK_CHAIN (subblock))
18094           set_block_origin_self (subblock);     /* Recurse.  */
18095       }
18096     }
18097 }
18098
18099 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
18100    the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
18101    node to so that it points to the node itself, thus indicating that the
18102    node represents its own (abstract) origin.  Additionally, if the
18103    DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
18104    the decl/block tree of which the given node is the root of, and for
18105    each other ..._DECL or BLOCK node contained therein whose
18106    DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
18107    set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
18108    point to themselves.  */
18109
18110 static void
18111 set_decl_origin_self (tree decl)
18112 {
18113   if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
18114     {
18115       DECL_ABSTRACT_ORIGIN (decl) = decl;
18116       if (TREE_CODE (decl) == FUNCTION_DECL)
18117         {
18118           tree arg;
18119
18120           for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
18121             DECL_ABSTRACT_ORIGIN (arg) = arg;
18122           if (DECL_INITIAL (decl) != NULL_TREE
18123               && DECL_INITIAL (decl) != error_mark_node)
18124             set_block_origin_self (DECL_INITIAL (decl));
18125         }
18126     }
18127 }
18128 \f
18129 /* Given a pointer to some BLOCK node, and a boolean value to set the
18130    "abstract" flags to, set that value into the BLOCK_ABSTRACT flag for
18131    the given block, and for all local decls and all local sub-blocks
18132    (recursively) which are contained therein.  */
18133
18134 static void
18135 set_block_abstract_flags (tree stmt, int setting)
18136 {
18137   tree local_decl;
18138   tree subblock;
18139   unsigned int i;
18140
18141   BLOCK_ABSTRACT (stmt) = setting;
18142
18143   for (local_decl = BLOCK_VARS (stmt);
18144        local_decl != NULL_TREE;
18145        local_decl = DECL_CHAIN (local_decl))
18146     if (! DECL_EXTERNAL (local_decl))
18147       set_decl_abstract_flags (local_decl, setting);
18148
18149   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
18150     {
18151       local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
18152       if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
18153           || TREE_CODE (local_decl) == PARM_DECL)
18154         set_decl_abstract_flags (local_decl, setting);
18155     }
18156
18157   for (subblock = BLOCK_SUBBLOCKS (stmt);
18158        subblock != NULL_TREE;
18159        subblock = BLOCK_CHAIN (subblock))
18160     set_block_abstract_flags (subblock, setting);
18161 }
18162
18163 /* Given a pointer to some ..._DECL node, and a boolean value to set the
18164    "abstract" flags to, set that value into the DECL_ABSTRACT_P flag for the
18165    given decl, and (in the case where the decl is a FUNCTION_DECL) also
18166    set the abstract flags for all of the parameters, local vars, local
18167    blocks and sub-blocks (recursively) to the same setting.  */
18168
18169 static void
18170 set_decl_abstract_flags (tree decl, int setting)
18171 {
18172   DECL_ABSTRACT_P (decl) = setting;
18173   if (TREE_CODE (decl) == FUNCTION_DECL)
18174     {
18175       tree arg;
18176
18177       for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
18178         DECL_ABSTRACT_P (arg) = setting;
18179       if (DECL_INITIAL (decl) != NULL_TREE
18180           && DECL_INITIAL (decl) != error_mark_node)
18181         set_block_abstract_flags (DECL_INITIAL (decl), setting);
18182     }
18183 }
18184
18185 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18186    may later generate inlined and/or out-of-line instances of.  */
18187
18188 static void
18189 dwarf2out_abstract_function (tree decl)
18190 {
18191   dw_die_ref old_die;
18192   tree save_fn;
18193   tree context;
18194   int was_abstract;
18195   hash_table<decl_loc_hasher> *old_decl_loc_table;
18196   hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
18197   int old_call_site_count, old_tail_call_site_count;
18198   struct call_arg_loc_node *old_call_arg_locations;
18199
18200   /* Make sure we have the actual abstract inline, not a clone.  */
18201   decl = DECL_ORIGIN (decl);
18202
18203   old_die = lookup_decl_die (decl);
18204   if (old_die && get_AT (old_die, DW_AT_inline))
18205     /* We've already generated the abstract instance.  */
18206     return;
18207
18208   /* We can be called while recursively when seeing block defining inlined subroutine
18209      DIE.  Be sure to not clobber the outer location table nor use it or we would
18210      get locations in abstract instantces.  */
18211   old_decl_loc_table = decl_loc_table;
18212   decl_loc_table = NULL;
18213   old_cached_dw_loc_list_table = cached_dw_loc_list_table;
18214   cached_dw_loc_list_table = NULL;
18215   old_call_arg_locations = call_arg_locations;
18216   call_arg_locations = NULL;
18217   old_call_site_count = call_site_count;
18218   call_site_count = -1;
18219   old_tail_call_site_count = tail_call_site_count;
18220   tail_call_site_count = -1;
18221
18222   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18223      we don't get confused by DECL_ABSTRACT_P.  */
18224   if (debug_info_level > DINFO_LEVEL_TERSE)
18225     {
18226       context = decl_class_context (decl);
18227       if (context)
18228         gen_type_die_for_member
18229           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
18230     }
18231
18232   /* Pretend we've just finished compiling this function.  */
18233   save_fn = current_function_decl;
18234   current_function_decl = decl;
18235
18236   was_abstract = DECL_ABSTRACT_P (decl);
18237   set_decl_abstract_flags (decl, 1);
18238   dwarf2out_decl (decl);
18239   if (! was_abstract)
18240     set_decl_abstract_flags (decl, 0);
18241
18242   current_function_decl = save_fn;
18243   decl_loc_table = old_decl_loc_table;
18244   cached_dw_loc_list_table = old_cached_dw_loc_list_table;
18245   call_arg_locations = old_call_arg_locations;
18246   call_site_count = old_call_site_count;
18247   tail_call_site_count = old_tail_call_site_count;
18248 }
18249
18250 /* Helper function of premark_used_types() which gets called through
18251    htab_traverse.
18252
18253    Marks the DIE of a given type in *SLOT as perennial, so it never gets
18254    marked as unused by prune_unused_types.  */
18255
18256 bool
18257 premark_used_types_helper (tree const &type, void *)
18258 {
18259   dw_die_ref die;
18260
18261   die = lookup_type_die (type);
18262   if (die != NULL)
18263     die->die_perennial_p = 1;
18264   return true;
18265 }
18266
18267 /* Helper function of premark_types_used_by_global_vars which gets called
18268    through htab_traverse.
18269
18270    Marks the DIE of a given type in *SLOT as perennial, so it never gets
18271    marked as unused by prune_unused_types. The DIE of the type is marked
18272    only if the global variable using the type will actually be emitted.  */
18273
18274 int
18275 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
18276                                           void *)
18277 {
18278   struct types_used_by_vars_entry *entry;
18279   dw_die_ref die;
18280
18281   entry = (struct types_used_by_vars_entry *) *slot;
18282   gcc_assert (entry->type != NULL
18283               && entry->var_decl != NULL);
18284   die = lookup_type_die (entry->type);
18285   if (die)
18286     {
18287       /* Ask cgraph if the global variable really is to be emitted.
18288          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
18289       varpool_node *node = varpool_node::get (entry->var_decl);
18290       if (node && node->definition)
18291         {
18292           die->die_perennial_p = 1;
18293           /* Keep the parent DIEs as well.  */
18294           while ((die = die->die_parent) && die->die_perennial_p == 0)
18295             die->die_perennial_p = 1;
18296         }
18297     }
18298   return 1;
18299 }
18300
18301 /* Mark all members of used_types_hash as perennial.  */
18302
18303 static void
18304 premark_used_types (struct function *fun)
18305 {
18306   if (fun && fun->used_types_hash)
18307     fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
18308 }
18309
18310 /* Mark all members of types_used_by_vars_entry as perennial.  */
18311
18312 static void
18313 premark_types_used_by_global_vars (void)
18314 {
18315   if (types_used_by_vars_hash)
18316     types_used_by_vars_hash
18317       ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
18318 }
18319
18320 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
18321    for CA_LOC call arg loc node.  */
18322
18323 static dw_die_ref
18324 gen_call_site_die (tree decl, dw_die_ref subr_die,
18325                    struct call_arg_loc_node *ca_loc)
18326 {
18327   dw_die_ref stmt_die = NULL, die;
18328   tree block = ca_loc->block;
18329
18330   while (block
18331          && block != DECL_INITIAL (decl)
18332          && TREE_CODE (block) == BLOCK)
18333     {
18334       if (block_map.length () > BLOCK_NUMBER (block))
18335         stmt_die = block_map[BLOCK_NUMBER (block)];
18336       if (stmt_die)
18337         break;
18338       block = BLOCK_SUPERCONTEXT (block);
18339     }
18340   if (stmt_die == NULL)
18341     stmt_die = subr_die;
18342   die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
18343   add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
18344   if (ca_loc->tail_call_p)
18345     add_AT_flag (die, DW_AT_GNU_tail_call, 1);
18346   if (ca_loc->symbol_ref)
18347     {
18348       dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
18349       if (tdie)
18350         add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
18351       else
18352         add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
18353     }
18354   return die;
18355 }
18356
18357 /* Generate a DIE to represent a declared function (either file-scope or
18358    block-local).  */
18359
18360 static void
18361 gen_subprogram_die (tree decl, dw_die_ref context_die)
18362 {
18363   tree origin = decl_ultimate_origin (decl);
18364   dw_die_ref subr_die;
18365   tree outer_scope;
18366   dw_die_ref old_die = lookup_decl_die (decl);
18367   int declaration = (current_function_decl != decl
18368                      || class_or_namespace_scope_p (context_die));
18369
18370   premark_used_types (DECL_STRUCT_FUNCTION (decl));
18371
18372   /* It is possible to have both DECL_ABSTRACT_P and DECLARATION be true if we
18373      started to generate the abstract instance of an inline, decided to output
18374      its containing class, and proceeded to emit the declaration of the inline
18375      from the member list for the class.  If so, DECLARATION takes priority;
18376      we'll get back to the abstract instance when done with the class.  */
18377
18378   /* The class-scope declaration DIE must be the primary DIE.  */
18379   if (origin && declaration && class_or_namespace_scope_p (context_die))
18380     {
18381       origin = NULL;
18382       gcc_assert (!old_die);
18383     }
18384
18385   /* Now that the C++ front end lazily declares artificial member fns, we
18386      might need to retrofit the declaration into its class.  */
18387   if (!declaration && !origin && !old_die
18388       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18389       && !class_or_namespace_scope_p (context_die)
18390       && debug_info_level > DINFO_LEVEL_TERSE)
18391     old_die = force_decl_die (decl);
18392
18393   if (origin != NULL)
18394     {
18395       gcc_assert (!declaration || local_scope_p (context_die));
18396
18397       /* Fixup die_parent for the abstract instance of a nested
18398          inline function.  */
18399       if (old_die && old_die->die_parent == NULL)
18400         add_child_die (context_die, old_die);
18401
18402       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18403       add_abstract_origin_attribute (subr_die, origin);
18404       /*  This is where the actual code for a cloned function is.
18405           Let's emit linkage name attribute for it.  This helps
18406           debuggers to e.g, set breakpoints into
18407           constructors/destructors when the user asks "break
18408           K::K".  */
18409       add_linkage_name (subr_die, decl);
18410     }
18411   else if (old_die)
18412     {
18413       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18414       struct dwarf_file_data * file_index = lookup_filename (s.file);
18415
18416       if (!get_AT_flag (old_die, DW_AT_declaration)
18417           /* We can have a normal definition following an inline one in the
18418              case of redefinition of GNU C extern inlines.
18419              It seems reasonable to use AT_specification in this case.  */
18420           && !get_AT (old_die, DW_AT_inline))
18421         {
18422           /* Detect and ignore this case, where we are trying to output
18423              something we have already output.  */
18424           return;
18425         }
18426
18427       /* If the definition comes from the same place as the declaration,
18428          maybe use the old DIE.  We always want the DIE for this function
18429          that has the *_pc attributes to be under comp_unit_die so the
18430          debugger can find it.  We also need to do this for abstract
18431          instances of inlines, since the spec requires the out-of-line copy
18432          to have the same parent.  For local class methods, this doesn't
18433          apply; we just use the old DIE.  */
18434       if ((is_cu_die (old_die->die_parent) || context_die == NULL)
18435           && (DECL_ARTIFICIAL (decl)
18436               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18437                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
18438                       == (unsigned) s.line))))
18439         {
18440           subr_die = old_die;
18441
18442           /* Clear out the declaration attribute and the formal parameters.
18443              Do not remove all children, because it is possible that this
18444              declaration die was forced using force_decl_die(). In such
18445              cases die that forced declaration die (e.g. TAG_imported_module)
18446              is one of the children that we do not want to remove.  */
18447           remove_AT (subr_die, DW_AT_declaration);
18448           remove_AT (subr_die, DW_AT_object_pointer);
18449           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18450         }
18451       else
18452         {
18453           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18454           add_AT_specification (subr_die, old_die);
18455           add_pubname (decl, subr_die);
18456           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18457             add_AT_file (subr_die, DW_AT_decl_file, file_index);
18458           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18459             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18460
18461           /* If the prototype had an 'auto' or 'decltype(auto)' return type,
18462              emit the real type on the definition die.  */
18463           if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
18464             {
18465               dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
18466               if (die == auto_die || die == decltype_auto_die)
18467                 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18468                                     TYPE_UNQUALIFIED, context_die);
18469             }
18470         }
18471     }
18472   else
18473     {
18474       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18475
18476       if (TREE_PUBLIC (decl))
18477         add_AT_flag (subr_die, DW_AT_external, 1);
18478
18479       add_name_and_src_coords_attributes (subr_die, decl);
18480       add_pubname (decl, subr_die);
18481       if (debug_info_level > DINFO_LEVEL_TERSE)
18482         {
18483           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18484           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18485                               TYPE_UNQUALIFIED, context_die);
18486         }
18487
18488       add_pure_or_virtual_attribute (subr_die, decl);
18489       if (DECL_ARTIFICIAL (decl))
18490         add_AT_flag (subr_die, DW_AT_artificial, 1);
18491
18492       if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
18493         add_AT_flag (subr_die, DW_AT_noreturn, 1);
18494
18495       add_accessibility_attribute (subr_die, decl);
18496     }
18497
18498   if (declaration)
18499     {
18500       if (!old_die || !get_AT (old_die, DW_AT_inline))
18501         {
18502           add_AT_flag (subr_die, DW_AT_declaration, 1);
18503
18504           /* If this is an explicit function declaration then generate
18505              a DW_AT_explicit attribute.  */
18506           if (lang_hooks.decls.function_decl_explicit_p (decl)
18507               && (dwarf_version >= 3 || !dwarf_strict))
18508             add_AT_flag (subr_die, DW_AT_explicit, 1);
18509
18510           /* If this is a C++11 deleted special function member then generate
18511              a DW_AT_GNU_deleted attribute.  */
18512           if (lang_hooks.decls.function_decl_deleted_p (decl)
18513               && (! dwarf_strict))
18514             add_AT_flag (subr_die, DW_AT_GNU_deleted, 1);
18515
18516           /* The first time we see a member function, it is in the context of
18517              the class to which it belongs.  We make sure of this by emitting
18518              the class first.  The next time is the definition, which is
18519              handled above.  The two may come from the same source text.
18520
18521              Note that force_decl_die() forces function declaration die. It is
18522              later reused to represent definition.  */
18523           equate_decl_number_to_die (decl, subr_die);
18524         }
18525     }
18526   else if (DECL_ABSTRACT_P (decl))
18527     {
18528       if (DECL_DECLARED_INLINE_P (decl))
18529         {
18530           if (cgraph_function_possibly_inlined_p (decl))
18531             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18532           else
18533             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18534         }
18535       else
18536         {
18537           if (cgraph_function_possibly_inlined_p (decl))
18538             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18539           else
18540             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18541         }
18542
18543       if (DECL_DECLARED_INLINE_P (decl)
18544           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18545         add_AT_flag (subr_die, DW_AT_artificial, 1);
18546
18547       equate_decl_number_to_die (decl, subr_die);
18548     }
18549   else if (!DECL_EXTERNAL (decl))
18550     {
18551       HOST_WIDE_INT cfa_fb_offset;
18552       struct function *fun = DECL_STRUCT_FUNCTION (decl);
18553
18554       if (!old_die || !get_AT (old_die, DW_AT_inline))
18555         equate_decl_number_to_die (decl, subr_die);
18556
18557       gcc_checking_assert (fun);
18558       if (!flag_reorder_blocks_and_partition)
18559         {
18560           dw_fde_ref fde = fun->fde;
18561           if (fde->dw_fde_begin)
18562             {
18563               /* We have already generated the labels.  */
18564              add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18565                                  fde->dw_fde_end, false);
18566             }
18567           else
18568             {
18569               /* Create start/end labels and add the range.  */
18570               char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
18571               char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
18572               ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
18573                                            current_function_funcdef_no);
18574               ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
18575                                            current_function_funcdef_no);
18576              add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
18577                                  false);
18578             }
18579
18580 #if VMS_DEBUGGING_INFO
18581       /* HP OpenVMS Industry Standard 64: DWARF Extensions
18582          Section 2.3 Prologue and Epilogue Attributes:
18583          When a breakpoint is set on entry to a function, it is generally
18584          desirable for execution to be suspended, not on the very first
18585          instruction of the function, but rather at a point after the
18586          function's frame has been set up, after any language defined local
18587          declaration processing has been completed, and before execution of
18588          the first statement of the function begins. Debuggers generally
18589          cannot properly determine where this point is.  Similarly for a
18590          breakpoint set on exit from a function. The prologue and epilogue
18591          attributes allow a compiler to communicate the location(s) to use.  */
18592
18593       {
18594         if (fde->dw_fde_vms_end_prologue)
18595           add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18596             fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18597
18598         if (fde->dw_fde_vms_begin_epilogue)
18599           add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18600             fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18601       }
18602 #endif
18603
18604         }
18605       else
18606         {
18607           /* Generate pubnames entries for the split function code ranges.  */
18608           dw_fde_ref fde = fun->fde;
18609
18610           if (fde->dw_fde_second_begin)
18611             {
18612               if (dwarf_version >= 3 || !dwarf_strict)
18613                 {
18614                   /* We should use ranges for non-contiguous code section 
18615                      addresses.  Use the actual code range for the initial
18616                      section, since the HOT/COLD labels might precede an 
18617                      alignment offset.  */
18618                   bool range_list_added = false;
18619                   add_ranges_by_labels (subr_die, fde->dw_fde_begin,
18620                                         fde->dw_fde_end, &range_list_added,
18621                                         false);
18622                   add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
18623                                         fde->dw_fde_second_end,
18624                                        &range_list_added, false);
18625                   if (range_list_added)
18626                     add_ranges (NULL);
18627                 }
18628               else
18629                 {
18630                   /* There is no real support in DW2 for this .. so we make
18631                      a work-around.  First, emit the pub name for the segment
18632                      containing the function label.  Then make and emit a
18633                      simplified subprogram DIE for the second segment with the
18634                      name pre-fixed by __hot/cold_sect_of_.  We use the same
18635                      linkage name for the second die so that gdb will find both
18636                      sections when given "b foo".  */
18637                   const char *name = NULL;
18638                   tree decl_name = DECL_NAME (decl);
18639                   dw_die_ref seg_die;
18640
18641                   /* Do the 'primary' section.   */
18642                   add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18643                                       fde->dw_fde_end, false);
18644
18645                   /* Build a minimal DIE for the secondary section.  */
18646                   seg_die = new_die (DW_TAG_subprogram,
18647                                      subr_die->die_parent, decl);
18648
18649                   if (TREE_PUBLIC (decl))
18650                     add_AT_flag (seg_die, DW_AT_external, 1);
18651
18652                   if (decl_name != NULL 
18653                       && IDENTIFIER_POINTER (decl_name) != NULL)
18654                     {
18655                       name = dwarf2_name (decl, 1);
18656                       if (! DECL_ARTIFICIAL (decl))
18657                         add_src_coords_attributes (seg_die, decl);
18658
18659                       add_linkage_name (seg_die, decl);
18660                     }
18661                   gcc_assert (name != NULL);
18662                   add_pure_or_virtual_attribute (seg_die, decl);
18663                   if (DECL_ARTIFICIAL (decl))
18664                     add_AT_flag (seg_die, DW_AT_artificial, 1);
18665
18666                   name = concat ("__second_sect_of_", name, NULL); 
18667                   add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
18668                                       fde->dw_fde_second_end, false);
18669                   add_name_attribute (seg_die, name);
18670                   if (want_pubnames ())
18671                     add_pubname_string (name, seg_die);
18672                 }
18673             }
18674           else
18675            add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
18676                                false);
18677         }
18678
18679       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
18680
18681       /* We define the "frame base" as the function's CFA.  This is more
18682          convenient for several reasons: (1) It's stable across the prologue
18683          and epilogue, which makes it better than just a frame pointer,
18684          (2) With dwarf3, there exists a one-byte encoding that allows us
18685          to reference the .debug_frame data by proxy, but failing that,
18686          (3) We can at least reuse the code inspection and interpretation
18687          code that determines the CFA position at various points in the
18688          function.  */
18689       if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
18690         {
18691           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
18692           add_AT_loc (subr_die, DW_AT_frame_base, op);
18693         }
18694       else
18695         {
18696           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
18697           if (list->dw_loc_next)
18698             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
18699           else
18700             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
18701         }
18702
18703       /* Compute a displacement from the "steady-state frame pointer" to
18704          the CFA.  The former is what all stack slots and argument slots
18705          will reference in the rtl; the latter is what we've told the
18706          debugger about.  We'll need to adjust all frame_base references
18707          by this displacement.  */
18708       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
18709
18710       if (fun->static_chain_decl)
18711         add_AT_location_description (subr_die, DW_AT_static_link,
18712                  loc_list_from_tree (fun->static_chain_decl, 2, NULL));
18713     }
18714
18715   /* Generate child dies for template paramaters.  */
18716   if (debug_info_level > DINFO_LEVEL_TERSE)
18717     gen_generic_params_dies (decl);
18718
18719   /* Now output descriptions of the arguments for this function. This gets
18720      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
18721      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
18722      `...' at the end of the formal parameter list.  In order to find out if
18723      there was a trailing ellipsis or not, we must instead look at the type
18724      associated with the FUNCTION_DECL.  This will be a node of type
18725      FUNCTION_TYPE. If the chain of type nodes hanging off of this
18726      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
18727      an ellipsis at the end.  */
18728
18729   /* In the case where we are describing a mere function declaration, all we
18730      need to do here (and all we *can* do here) is to describe the *types* of
18731      its formal parameters.  */
18732   if (debug_info_level <= DINFO_LEVEL_TERSE)
18733     ;
18734   else if (declaration)
18735     gen_formal_types_die (decl, subr_die);
18736   else
18737     {
18738       /* Generate DIEs to represent all known formal parameters.  */
18739       tree parm = DECL_ARGUMENTS (decl);
18740       tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
18741       tree generic_decl_parm = generic_decl
18742                                 ? DECL_ARGUMENTS (generic_decl)
18743                                 : NULL;
18744
18745       /* Now we want to walk the list of parameters of the function and
18746          emit their relevant DIEs.
18747
18748          We consider the case of DECL being an instance of a generic function
18749          as well as it being a normal function.
18750
18751          If DECL is an instance of a generic function we walk the
18752          parameters of the generic function declaration _and_ the parameters of
18753          DECL itself. This is useful because we want to emit specific DIEs for
18754          function parameter packs and those are declared as part of the
18755          generic function declaration. In that particular case,
18756          the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
18757          That DIE has children DIEs representing the set of arguments
18758          of the pack. Note that the set of pack arguments can be empty.
18759          In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
18760          children DIE.
18761
18762          Otherwise, we just consider the parameters of DECL.  */
18763       while (generic_decl_parm || parm)
18764         {
18765           if (generic_decl_parm
18766               && lang_hooks.function_parameter_pack_p (generic_decl_parm))
18767             gen_formal_parameter_pack_die (generic_decl_parm,
18768                                            parm, subr_die,
18769                                            &parm);
18770           else if (parm && !POINTER_BOUNDS_P (parm))
18771             {
18772               dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
18773
18774               if (parm == DECL_ARGUMENTS (decl)
18775                   && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
18776                   && parm_die
18777                   && (dwarf_version >= 3 || !dwarf_strict))
18778                 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
18779
18780               parm = DECL_CHAIN (parm);
18781             }
18782           else if (parm)
18783             parm = DECL_CHAIN (parm);
18784
18785           if (generic_decl_parm)
18786             generic_decl_parm = DECL_CHAIN (generic_decl_parm);
18787         }
18788
18789       /* Decide whether we need an unspecified_parameters DIE at the end.
18790          There are 2 more cases to do this for: 1) the ansi ... declaration -
18791          this is detectable when the end of the arg list is not a
18792          void_type_node 2) an unprototyped function declaration (not a
18793          definition).  This just means that we have no info about the
18794          parameters at all.  */
18795       if (prototype_p (TREE_TYPE (decl)))
18796         {
18797           /* This is the prototyped case, check for....  */
18798           if (stdarg_p (TREE_TYPE (decl)))
18799             gen_unspecified_parameters_die (decl, subr_die);
18800         }
18801       else if (DECL_INITIAL (decl) == NULL_TREE)
18802         gen_unspecified_parameters_die (decl, subr_die);
18803     }
18804
18805   /* Output Dwarf info for all of the stuff within the body of the function
18806      (if it has one - it may be just a declaration).  */
18807   outer_scope = DECL_INITIAL (decl);
18808
18809   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
18810      a function.  This BLOCK actually represents the outermost binding contour
18811      for the function, i.e. the contour in which the function's formal
18812      parameters and labels get declared. Curiously, it appears that the front
18813      end doesn't actually put the PARM_DECL nodes for the current function onto
18814      the BLOCK_VARS list for this outer scope, but are strung off of the
18815      DECL_ARGUMENTS list for the function instead.
18816
18817      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
18818      the LABEL_DECL nodes for the function however, and we output DWARF info
18819      for those in decls_for_scope.  Just within the `outer_scope' there will be
18820      a BLOCK node representing the function's outermost pair of curly braces,
18821      and any blocks used for the base and member initializers of a C++
18822      constructor function.  */
18823   if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
18824     {
18825       int call_site_note_count = 0;
18826       int tail_call_site_note_count = 0;
18827
18828       /* Emit a DW_TAG_variable DIE for a named return value.  */
18829       if (DECL_NAME (DECL_RESULT (decl)))
18830         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18831
18832       decls_for_scope (outer_scope, subr_die);
18833
18834       if (call_arg_locations && !dwarf_strict)
18835         {
18836           struct call_arg_loc_node *ca_loc;
18837           for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
18838             {
18839               dw_die_ref die = NULL;
18840               rtx tloc = NULL_RTX, tlocc = NULL_RTX;
18841               rtx arg, next_arg;
18842
18843               for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
18844                    arg; arg = next_arg)
18845                 {
18846                   dw_loc_descr_ref reg, val;
18847                   machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
18848                   dw_die_ref cdie, tdie = NULL;
18849
18850                   next_arg = XEXP (arg, 1);
18851                   if (REG_P (XEXP (XEXP (arg, 0), 0))
18852                       && next_arg
18853                       && MEM_P (XEXP (XEXP (next_arg, 0), 0))
18854                       && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
18855                       && REGNO (XEXP (XEXP (arg, 0), 0))
18856                          == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
18857                     next_arg = XEXP (next_arg, 1);
18858                   if (mode == VOIDmode)
18859                     {
18860                       mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
18861                       if (mode == VOIDmode)
18862                         mode = GET_MODE (XEXP (arg, 0));
18863                     }
18864                   if (mode == VOIDmode || mode == BLKmode)
18865                     continue;
18866                   if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
18867                     {
18868                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18869                       tloc = XEXP (XEXP (arg, 0), 1);
18870                       continue;
18871                     }
18872                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
18873                            && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
18874                     {
18875                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18876                       tlocc = XEXP (XEXP (arg, 0), 1);
18877                       continue;
18878                     }
18879                   reg = NULL;
18880                   if (REG_P (XEXP (XEXP (arg, 0), 0)))
18881                     reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
18882                                               VAR_INIT_STATUS_INITIALIZED);
18883                   else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
18884                     {
18885                       rtx mem = XEXP (XEXP (arg, 0), 0);
18886                       reg = mem_loc_descriptor (XEXP (mem, 0),
18887                                                 get_address_mode (mem),
18888                                                 GET_MODE (mem),
18889                                                 VAR_INIT_STATUS_INITIALIZED);
18890                     }
18891                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
18892                            == DEBUG_PARAMETER_REF)
18893                     {
18894                       tree tdecl
18895                         = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
18896                       tdie = lookup_decl_die (tdecl);
18897                       if (tdie == NULL)
18898                         continue;
18899                     }
18900                   else
18901                     continue;
18902                   if (reg == NULL
18903                       && GET_CODE (XEXP (XEXP (arg, 0), 0))
18904                          != DEBUG_PARAMETER_REF)
18905                     continue;
18906                   val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
18907                                             VOIDmode,
18908                                             VAR_INIT_STATUS_INITIALIZED);
18909                   if (val == NULL)
18910                     continue;
18911                   if (die == NULL)
18912                     die = gen_call_site_die (decl, subr_die, ca_loc);
18913                   cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
18914                                   NULL_TREE);
18915                   if (reg != NULL)
18916                     add_AT_loc (cdie, DW_AT_location, reg);
18917                   else if (tdie != NULL)
18918                     add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
18919                   add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
18920                   if (next_arg != XEXP (arg, 1))
18921                     {
18922                       mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
18923                       if (mode == VOIDmode)
18924                         mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
18925                       val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
18926                                                             0), 1),
18927                                                 mode, VOIDmode,
18928                                                 VAR_INIT_STATUS_INITIALIZED);
18929                       if (val != NULL)
18930                         add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
18931                     }
18932                 }
18933               if (die == NULL
18934                   && (ca_loc->symbol_ref || tloc))
18935                 die = gen_call_site_die (decl, subr_die, ca_loc);
18936               if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
18937                 {
18938                   dw_loc_descr_ref tval = NULL;
18939
18940                   if (tloc != NULL_RTX)
18941                     tval = mem_loc_descriptor (tloc,
18942                                                GET_MODE (tloc) == VOIDmode
18943                                                ? Pmode : GET_MODE (tloc),
18944                                                VOIDmode,
18945                                                VAR_INIT_STATUS_INITIALIZED);
18946                   if (tval)
18947                     add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
18948                   else if (tlocc != NULL_RTX)
18949                     {
18950                       tval = mem_loc_descriptor (tlocc,
18951                                                  GET_MODE (tlocc) == VOIDmode
18952                                                  ? Pmode : GET_MODE (tlocc),
18953                                                  VOIDmode,
18954                                                  VAR_INIT_STATUS_INITIALIZED);
18955                       if (tval)
18956                         add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
18957                                     tval);
18958                     }
18959                 }
18960               if (die != NULL)
18961                 {
18962                   call_site_note_count++;
18963                   if (ca_loc->tail_call_p)
18964                     tail_call_site_note_count++;
18965                 }
18966             }
18967         }
18968       call_arg_locations = NULL;
18969       call_arg_loc_last = NULL;
18970       if (tail_call_site_count >= 0
18971           && tail_call_site_count == tail_call_site_note_count
18972           && !dwarf_strict)
18973         {
18974           if (call_site_count >= 0
18975               && call_site_count == call_site_note_count)
18976             add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
18977           else
18978             add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
18979         }
18980       call_site_count = -1;
18981       tail_call_site_count = -1;
18982     }
18983
18984   if (subr_die != old_die)
18985     /* Add the calling convention attribute if requested.  */
18986     add_calling_convention_attribute (subr_die, decl);
18987 }
18988
18989 /* Returns a hash value for X (which really is a die_struct).  */
18990
18991 hashval_t
18992 block_die_hasher::hash (die_struct *d)
18993 {
18994   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
18995 }
18996
18997 /* Return nonzero if decl_id and die_parent of die_struct X is the same
18998    as decl_id and die_parent of die_struct Y.  */
18999
19000 bool
19001 block_die_hasher::equal (die_struct *x, die_struct *y)
19002 {
19003   return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
19004 }
19005
19006 /* Generate a DIE to represent a declared data object.
19007    Either DECL or ORIGIN must be non-null.  */
19008
19009 static void
19010 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
19011 {
19012   HOST_WIDE_INT off = 0;
19013   tree com_decl;
19014   tree decl_or_origin = decl ? decl : origin;
19015   tree ultimate_origin;
19016   dw_die_ref var_die;
19017   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
19018   dw_die_ref origin_die;
19019   bool declaration = (DECL_EXTERNAL (decl_or_origin)
19020                       || class_or_namespace_scope_p (context_die));
19021   bool specialization_p = false;
19022
19023   ultimate_origin = decl_ultimate_origin (decl_or_origin);
19024   if (decl || ultimate_origin)
19025     origin = ultimate_origin;
19026   com_decl = fortran_common (decl_or_origin, &off);
19027
19028   /* Symbol in common gets emitted as a child of the common block, in the form
19029      of a data member.  */
19030   if (com_decl)
19031     {
19032       dw_die_ref com_die;
19033       dw_loc_list_ref loc;
19034       die_node com_die_arg;
19035
19036       var_die = lookup_decl_die (decl_or_origin);
19037       if (var_die)
19038         {
19039           if (get_AT (var_die, DW_AT_location) == NULL)
19040             {
19041               loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
19042               if (loc)
19043                 {
19044                   if (off)
19045                     {
19046                       /* Optimize the common case.  */
19047                       if (single_element_loc_list_p (loc)
19048                           && loc->expr->dw_loc_opc == DW_OP_addr
19049                           && loc->expr->dw_loc_next == NULL
19050                           && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
19051                              == SYMBOL_REF)
19052                         {
19053                           rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
19054                           loc->expr->dw_loc_oprnd1.v.val_addr
19055                             = plus_constant (GET_MODE (x), x , off);
19056                         }
19057                       else
19058                         loc_list_plus_const (loc, off);
19059                     }
19060                   add_AT_location_description (var_die, DW_AT_location, loc);
19061                   remove_AT (var_die, DW_AT_declaration);
19062                 }
19063             }
19064           return;
19065         }
19066
19067       if (common_block_die_table == NULL)
19068         common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
19069
19070       com_die_arg.decl_id = DECL_UID (com_decl);
19071       com_die_arg.die_parent = context_die;
19072       com_die = common_block_die_table->find (&com_die_arg);
19073       loc = loc_list_from_tree (com_decl, 2, NULL);
19074       if (com_die == NULL)
19075         {
19076           const char *cnam
19077             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
19078           die_node **slot;
19079
19080           com_die = new_die (DW_TAG_common_block, context_die, decl);
19081           add_name_and_src_coords_attributes (com_die, com_decl);
19082           if (loc)
19083             {
19084               add_AT_location_description (com_die, DW_AT_location, loc);
19085               /* Avoid sharing the same loc descriptor between
19086                  DW_TAG_common_block and DW_TAG_variable.  */
19087               loc = loc_list_from_tree (com_decl, 2, NULL);
19088             }
19089           else if (DECL_EXTERNAL (decl))
19090             add_AT_flag (com_die, DW_AT_declaration, 1);
19091           if (want_pubnames ())
19092             add_pubname_string (cnam, com_die); /* ??? needed? */
19093           com_die->decl_id = DECL_UID (com_decl);
19094           slot = common_block_die_table->find_slot (com_die, INSERT);
19095           *slot = com_die;
19096         }
19097       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
19098         {
19099           add_AT_location_description (com_die, DW_AT_location, loc);
19100           loc = loc_list_from_tree (com_decl, 2, NULL);
19101           remove_AT (com_die, DW_AT_declaration);
19102         }
19103       var_die = new_die (DW_TAG_variable, com_die, decl);
19104       add_name_and_src_coords_attributes (var_die, decl);
19105       add_type_attribute (var_die, TREE_TYPE (decl), decl_quals (decl),
19106                           context_die);
19107       add_AT_flag (var_die, DW_AT_external, 1);
19108       if (loc)
19109         {
19110           if (off)
19111             {
19112               /* Optimize the common case.  */
19113               if (single_element_loc_list_p (loc)
19114                   && loc->expr->dw_loc_opc == DW_OP_addr
19115                   && loc->expr->dw_loc_next == NULL
19116                   && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
19117                 {
19118                   rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
19119                   loc->expr->dw_loc_oprnd1.v.val_addr
19120                     = plus_constant (GET_MODE (x), x, off);
19121                 }
19122               else
19123                 loc_list_plus_const (loc, off);
19124             }
19125           add_AT_location_description (var_die, DW_AT_location, loc);
19126         }
19127       else if (DECL_EXTERNAL (decl))
19128         add_AT_flag (var_die, DW_AT_declaration, 1);
19129       equate_decl_number_to_die (decl, var_die);
19130       return;
19131     }
19132
19133   /* If the compiler emitted a definition for the DECL declaration
19134      and if we already emitted a DIE for it, don't emit a second
19135      DIE for it again. Allow re-declarations of DECLs that are
19136      inside functions, though.  */
19137   if (old_die && declaration && !local_scope_p (context_die))
19138     return;
19139
19140   /* For static data members, the declaration in the class is supposed
19141      to have DW_TAG_member tag; the specification should still be
19142      DW_TAG_variable referencing the DW_TAG_member DIE.  */
19143   if (declaration && class_scope_p (context_die))
19144     var_die = new_die (DW_TAG_member, context_die, decl);
19145   else
19146     var_die = new_die (DW_TAG_variable, context_die, decl);
19147
19148   origin_die = NULL;
19149   if (origin != NULL)
19150     origin_die = add_abstract_origin_attribute (var_die, origin);
19151
19152   /* Loop unrolling can create multiple blocks that refer to the same
19153      static variable, so we must test for the DW_AT_declaration flag.
19154
19155      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
19156      copy decls and set the DECL_ABSTRACT_P flag on them instead of
19157      sharing them.
19158
19159      ??? Duplicated blocks have been rewritten to use .debug_ranges.
19160
19161      ??? The declare_in_namespace support causes us to get two DIEs for one
19162      variable, both of which are declarations.  We want to avoid considering
19163      one to be a specification, so we must test that this DIE is not a
19164      declaration.  */
19165   else if (old_die && TREE_STATIC (decl) && ! declaration
19166            && get_AT_flag (old_die, DW_AT_declaration) == 1)
19167     {
19168       /* This is a definition of a C++ class level static.  */
19169       add_AT_specification (var_die, old_die);
19170       specialization_p = true;
19171       if (DECL_NAME (decl))
19172         {
19173           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19174           struct dwarf_file_data * file_index = lookup_filename (s.file);
19175
19176           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19177             add_AT_file (var_die, DW_AT_decl_file, file_index);
19178
19179           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19180             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19181
19182           if (old_die->die_tag == DW_TAG_member)
19183             add_linkage_name (var_die, decl);
19184         }
19185     }
19186   else
19187     add_name_and_src_coords_attributes (var_die, decl);
19188
19189   if ((origin == NULL && !specialization_p)
19190       || (origin != NULL
19191           && !DECL_ABSTRACT_P (decl_or_origin)
19192           && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19193                                        decl_function_context
19194                                                         (decl_or_origin))))
19195     {
19196       tree type = TREE_TYPE (decl_or_origin);
19197
19198       if (decl_by_reference_p (decl_or_origin))
19199         add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19200                             context_die);
19201       else
19202         add_type_attribute (var_die, type, decl_quals (decl_or_origin),
19203                             context_die);
19204     }
19205
19206   if (origin == NULL && !specialization_p)
19207     {
19208       if (TREE_PUBLIC (decl))
19209         add_AT_flag (var_die, DW_AT_external, 1);
19210
19211       if (DECL_ARTIFICIAL (decl))
19212         add_AT_flag (var_die, DW_AT_artificial, 1);
19213
19214       add_accessibility_attribute (var_die, decl);
19215     }
19216
19217   if (declaration)
19218     add_AT_flag (var_die, DW_AT_declaration, 1);
19219
19220   if (decl && (DECL_ABSTRACT_P (decl) || declaration || old_die == NULL))
19221     equate_decl_number_to_die (decl, var_die);
19222
19223   if (! declaration
19224       && (! DECL_ABSTRACT_P (decl_or_origin)
19225           /* Local static vars are shared between all clones/inlines,
19226              so emit DW_AT_location on the abstract DIE if DECL_RTL is
19227              already set.  */
19228           || (TREE_CODE (decl_or_origin) == VAR_DECL
19229               && TREE_STATIC (decl_or_origin)
19230               && DECL_RTL_SET_P (decl_or_origin)))
19231       /* When abstract origin already has DW_AT_location attribute, no need
19232          to add it again.  */
19233       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19234     {
19235       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
19236           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
19237         defer_location (decl_or_origin, var_die);
19238       else
19239         add_location_or_const_value_attribute (var_die, decl_or_origin,
19240                                                decl == NULL, DW_AT_location);
19241       add_pubname (decl_or_origin, var_die);
19242     }
19243   else
19244     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19245 }
19246
19247 /* Generate a DIE to represent a named constant.  */
19248
19249 static void
19250 gen_const_die (tree decl, dw_die_ref context_die)
19251 {
19252   dw_die_ref const_die;
19253   tree type = TREE_TYPE (decl);
19254
19255   const_die = new_die (DW_TAG_constant, context_die, decl);
19256   add_name_and_src_coords_attributes (const_die, decl);
19257   add_type_attribute (const_die, type, TYPE_QUAL_CONST, context_die);
19258   if (TREE_PUBLIC (decl))
19259     add_AT_flag (const_die, DW_AT_external, 1);
19260   if (DECL_ARTIFICIAL (decl))
19261     add_AT_flag (const_die, DW_AT_artificial, 1);
19262   tree_add_const_value_attribute_for_decl (const_die, decl);
19263 }
19264
19265 /* Generate a DIE to represent a label identifier.  */
19266
19267 static void
19268 gen_label_die (tree decl, dw_die_ref context_die)
19269 {
19270   tree origin = decl_ultimate_origin (decl);
19271   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
19272   rtx insn;
19273   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19274
19275   if (origin != NULL)
19276     add_abstract_origin_attribute (lbl_die, origin);
19277   else
19278     add_name_and_src_coords_attributes (lbl_die, decl);
19279
19280   if (DECL_ABSTRACT_P (decl))
19281     equate_decl_number_to_die (decl, lbl_die);
19282   else
19283     {
19284       insn = DECL_RTL_IF_SET (decl);
19285
19286       /* Deleted labels are programmer specified labels which have been
19287          eliminated because of various optimizations.  We still emit them
19288          here so that it is possible to put breakpoints on them.  */
19289       if (insn
19290           && (LABEL_P (insn)
19291               || ((NOTE_P (insn)
19292                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19293         {
19294           /* When optimization is enabled (via -O) some parts of the compiler
19295              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19296              represent source-level labels which were explicitly declared by
19297              the user.  This really shouldn't be happening though, so catch
19298              it if it ever does happen.  */
19299           gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
19300
19301           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19302           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19303         }
19304       else if (insn
19305                && NOTE_P (insn)
19306                && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
19307                && CODE_LABEL_NUMBER (insn) != -1)
19308         {
19309           ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
19310           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19311         }
19312     }
19313 }
19314
19315 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
19316    attributes to the DIE for a block STMT, to describe where the inlined
19317    function was called from.  This is similar to add_src_coords_attributes.  */
19318
19319 static inline void
19320 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19321 {
19322   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19323
19324   if (dwarf_version >= 3 || !dwarf_strict)
19325     {
19326       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19327       add_AT_unsigned (die, DW_AT_call_line, s.line);
19328     }
19329 }
19330
19331
19332 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19333    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
19334
19335 static inline void
19336 add_high_low_attributes (tree stmt, dw_die_ref die)
19337 {
19338   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19339
19340   if (BLOCK_FRAGMENT_CHAIN (stmt)
19341       && (dwarf_version >= 3 || !dwarf_strict))
19342     {
19343       tree chain, superblock = NULL_TREE;
19344       dw_die_ref pdie;
19345       dw_attr_ref attr = NULL;
19346
19347       if (inlined_function_outer_scope_p (stmt))
19348         {
19349           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19350                                        BLOCK_NUMBER (stmt));
19351           add_AT_lbl_id (die, DW_AT_entry_pc, label);
19352         }
19353
19354       /* Optimize duplicate .debug_ranges lists or even tails of
19355          lists.  If this BLOCK has same ranges as its supercontext,
19356          lookup DW_AT_ranges attribute in the supercontext (and
19357          recursively so), verify that the ranges_table contains the
19358          right values and use it instead of adding a new .debug_range.  */
19359       for (chain = stmt, pdie = die;
19360            BLOCK_SAME_RANGE (chain);
19361            chain = BLOCK_SUPERCONTEXT (chain))
19362         {
19363           dw_attr_ref new_attr;
19364
19365           pdie = pdie->die_parent;
19366           if (pdie == NULL)
19367             break;
19368           if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
19369             break;
19370           new_attr = get_AT (pdie, DW_AT_ranges);
19371           if (new_attr == NULL
19372               || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
19373             break;
19374           attr = new_attr;
19375           superblock = BLOCK_SUPERCONTEXT (chain);
19376         }
19377       if (attr != NULL
19378           && (ranges_table[attr->dw_attr_val.v.val_offset
19379                            / 2 / DWARF2_ADDR_SIZE].num
19380               == BLOCK_NUMBER (superblock))
19381           && BLOCK_FRAGMENT_CHAIN (superblock))
19382         {
19383           unsigned long off = attr->dw_attr_val.v.val_offset
19384                               / 2 / DWARF2_ADDR_SIZE;
19385           unsigned long supercnt = 0, thiscnt = 0;
19386           for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
19387                chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19388             {
19389               ++supercnt;
19390               gcc_checking_assert (ranges_table[off + supercnt].num
19391                                    == BLOCK_NUMBER (chain));
19392             }
19393           gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
19394           for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
19395                chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19396             ++thiscnt;
19397           gcc_assert (supercnt >= thiscnt);
19398           add_AT_range_list (die, DW_AT_ranges,
19399                              ((off + supercnt - thiscnt)
19400                               * 2 * DWARF2_ADDR_SIZE),
19401                              false);
19402           return;
19403         }
19404
19405       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
19406
19407       chain = BLOCK_FRAGMENT_CHAIN (stmt);
19408       do
19409         {
19410           add_ranges (chain);
19411           chain = BLOCK_FRAGMENT_CHAIN (chain);
19412         }
19413       while (chain);
19414       add_ranges (NULL);
19415     }
19416   else
19417     {
19418       char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
19419       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19420                                    BLOCK_NUMBER (stmt));
19421       ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
19422                                    BLOCK_NUMBER (stmt));
19423       add_AT_low_high_pc (die, label, label_high, false);
19424     }
19425 }
19426
19427 /* Generate a DIE for a lexical block.  */
19428
19429 static void
19430 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
19431 {
19432   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19433
19434   if (call_arg_locations)
19435     {
19436       if (block_map.length () <= BLOCK_NUMBER (stmt))
19437         block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19438       block_map[BLOCK_NUMBER (stmt)] = stmt_die;
19439     }
19440
19441   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19442     add_high_low_attributes (stmt, stmt_die);
19443
19444   decls_for_scope (stmt, stmt_die);
19445 }
19446
19447 /* Generate a DIE for an inlined subprogram.  */
19448
19449 static void
19450 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
19451 {
19452   tree decl;
19453
19454   /* The instance of function that is effectively being inlined shall not
19455      be abstract.  */
19456   gcc_assert (! BLOCK_ABSTRACT (stmt));
19457
19458   decl = block_ultimate_origin (stmt);
19459
19460   /* Emit info for the abstract instance first, if we haven't yet.  We
19461      must emit this even if the block is abstract, otherwise when we
19462      emit the block below (or elsewhere), we may end up trying to emit
19463      a die whose origin die hasn't been emitted, and crashing.  */
19464   dwarf2out_abstract_function (decl);
19465
19466   if (! BLOCK_ABSTRACT (stmt))
19467     {
19468       dw_die_ref subr_die
19469         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19470
19471       if (call_arg_locations)
19472         {
19473           if (block_map.length () <= BLOCK_NUMBER (stmt))
19474             block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19475           block_map[BLOCK_NUMBER (stmt)] = subr_die;
19476         }
19477       add_abstract_origin_attribute (subr_die, decl);
19478       if (TREE_ASM_WRITTEN (stmt))
19479         add_high_low_attributes (stmt, subr_die);
19480       add_call_src_coords_attributes (stmt, subr_die);
19481
19482       decls_for_scope (stmt, subr_die);
19483     }
19484 }
19485
19486 /* Generate a DIE for a field in a record, or structure.  */
19487
19488 static void
19489 gen_field_die (tree decl, dw_die_ref context_die)
19490 {
19491   dw_die_ref decl_die;
19492
19493   if (TREE_TYPE (decl) == error_mark_node)
19494     return;
19495
19496   decl_die = new_die (DW_TAG_member, context_die, decl);
19497   add_name_and_src_coords_attributes (decl_die, decl);
19498   add_type_attribute (decl_die, member_declared_type (decl),
19499                       decl_quals (decl), context_die);
19500
19501   if (DECL_BIT_FIELD_TYPE (decl))
19502     {
19503       add_byte_size_attribute (decl_die, decl);
19504       add_bit_size_attribute (decl_die, decl);
19505       add_bit_offset_attribute (decl_die, decl);
19506     }
19507
19508   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19509     add_data_member_location_attribute (decl_die, decl);
19510
19511   if (DECL_ARTIFICIAL (decl))
19512     add_AT_flag (decl_die, DW_AT_artificial, 1);
19513
19514   add_accessibility_attribute (decl_die, decl);
19515
19516   /* Equate decl number to die, so that we can look up this decl later on.  */
19517   equate_decl_number_to_die (decl, decl_die);
19518 }
19519
19520 #if 0
19521 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19522    Use modified_type_die instead.
19523    We keep this code here just in case these types of DIEs may be needed to
19524    represent certain things in other languages (e.g. Pascal) someday.  */
19525
19526 static void
19527 gen_pointer_type_die (tree type, dw_die_ref context_die)
19528 {
19529   dw_die_ref ptr_die
19530     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19531
19532   equate_type_number_to_die (type, ptr_die);
19533   add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19534                       context_die);
19535   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19536 }
19537
19538 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19539    Use modified_type_die instead.
19540    We keep this code here just in case these types of DIEs may be needed to
19541    represent certain things in other languages (e.g. Pascal) someday.  */
19542
19543 static void
19544 gen_reference_type_die (tree type, dw_die_ref context_die)
19545 {
19546   dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19547
19548   if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19549     ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19550   else
19551     ref_die = new_die (DW_TAG_reference_type, scope_die, type);
19552
19553   equate_type_number_to_die (type, ref_die);
19554   add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19555                       context_die);
19556   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19557 }
19558 #endif
19559
19560 /* Generate a DIE for a pointer to a member type.  */
19561
19562 static void
19563 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
19564 {
19565   dw_die_ref ptr_die
19566     = new_die (DW_TAG_ptr_to_member_type,
19567                scope_die_for (type, context_die), type);
19568
19569   equate_type_number_to_die (type, ptr_die);
19570   add_AT_die_ref (ptr_die, DW_AT_containing_type,
19571                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
19572   add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19573                       context_die);
19574 }
19575
19576 typedef const char *dchar_p; /* For DEF_VEC_P.  */
19577
19578 static char *producer_string;
19579
19580 /* Return a heap allocated producer string including command line options
19581    if -grecord-gcc-switches.  */
19582
19583 static char *
19584 gen_producer_string (void)
19585 {
19586   size_t j;
19587   auto_vec<dchar_p> switches;
19588   const char *language_string = lang_hooks.name;
19589   char *producer, *tail;
19590   const char *p;
19591   size_t len = dwarf_record_gcc_switches ? 0 : 3;
19592   size_t plen = strlen (language_string) + 1 + strlen (version_string);
19593
19594   for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
19595     switch (save_decoded_options[j].opt_index)
19596       {
19597       case OPT_o:
19598       case OPT_d:
19599       case OPT_dumpbase:
19600       case OPT_dumpdir:
19601       case OPT_auxbase:
19602       case OPT_auxbase_strip:
19603       case OPT_quiet:
19604       case OPT_version:
19605       case OPT_v:
19606       case OPT_w:
19607       case OPT_L:
19608       case OPT_D:
19609       case OPT_I:
19610       case OPT_U:
19611       case OPT_SPECIAL_unknown:
19612       case OPT_SPECIAL_ignore:
19613       case OPT_SPECIAL_program_name:
19614       case OPT_SPECIAL_input_file:
19615       case OPT_grecord_gcc_switches:
19616       case OPT_gno_record_gcc_switches:
19617       case OPT__output_pch_:
19618       case OPT_fdiagnostics_show_location_:
19619       case OPT_fdiagnostics_show_option:
19620       case OPT_fdiagnostics_show_caret:
19621       case OPT_fdiagnostics_color_:
19622       case OPT_fverbose_asm:
19623       case OPT____:
19624       case OPT__sysroot_:
19625       case OPT_nostdinc:
19626       case OPT_nostdinc__:
19627       case OPT_fpreprocessed:
19628         /* Ignore these.  */
19629         continue;
19630       default:
19631         if (cl_options[save_decoded_options[j].opt_index].flags
19632             & CL_NO_DWARF_RECORD)
19633           continue;
19634         gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
19635                              == '-');
19636         switch (save_decoded_options[j].canonical_option[0][1])
19637           {
19638           case 'M':
19639           case 'i':
19640           case 'W':
19641             continue;
19642           case 'f':
19643             if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
19644                          "dump", 4) == 0)
19645               continue;
19646             break;
19647           default:
19648             break;
19649           }
19650         switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
19651         len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
19652         break;
19653       }
19654
19655   producer = XNEWVEC (char, plen + 1 + len + 1);
19656   tail = producer;
19657   sprintf (tail, "%s %s", language_string, version_string);
19658   tail += plen;
19659
19660   FOR_EACH_VEC_ELT (switches, j, p)
19661     {
19662       len = strlen (p);
19663       *tail = ' ';
19664       memcpy (tail + 1, p, len);
19665       tail += len + 1;
19666     }
19667
19668   *tail = '\0';
19669   return producer;
19670 }
19671
19672 /* Given a C and/or C++ language/version string return the "highest".
19673    C++ is assumed to be "higher" than C in this case.  Used for merging
19674    LTO translation unit languages.  */
19675 static const char *
19676 highest_c_language (const char *lang1, const char *lang2)
19677 {
19678   if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
19679     return "GNU C++14";
19680   if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
19681     return "GNU C++11";
19682   if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
19683     return "GNU C++98";
19684
19685   if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
19686     return "GNU C11";
19687   if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
19688     return "GNU C99";
19689   if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
19690     return "GNU C89";
19691
19692   gcc_unreachable ();
19693 }
19694
19695
19696 /* Generate the DIE for the compilation unit.  */
19697
19698 static dw_die_ref
19699 gen_compile_unit_die (const char *filename)
19700 {
19701   dw_die_ref die;
19702   const char *language_string = lang_hooks.name;
19703   int language;
19704
19705   die = new_die (DW_TAG_compile_unit, NULL, NULL);
19706
19707   if (filename)
19708     {
19709       add_name_attribute (die, filename);
19710       /* Don't add cwd for <built-in>.  */
19711       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19712         add_comp_dir_attribute (die);
19713     }
19714
19715   add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
19716
19717   /* If our producer is LTO try to figure out a common language to use
19718      from the global list of translation units.  */
19719   if (strcmp (language_string, "GNU GIMPLE") == 0)
19720     {
19721       unsigned i;
19722       tree t;
19723       const char *common_lang = NULL;
19724
19725       FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
19726         {
19727           if (!TRANSLATION_UNIT_LANGUAGE (t))
19728             continue;
19729           if (!common_lang)
19730             common_lang = TRANSLATION_UNIT_LANGUAGE (t);
19731           else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
19732             ;
19733           else if (strncmp (common_lang, "GNU C", 5) == 0
19734                     && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
19735             /* Mixing C and C++ is ok, use C++ in that case.  */
19736             common_lang = highest_c_language (common_lang,
19737                                               TRANSLATION_UNIT_LANGUAGE (t));
19738           else
19739             {
19740               /* Fall back to C.  */
19741               common_lang = NULL;
19742               break;
19743             }
19744         }
19745
19746       if (common_lang)
19747         language_string = common_lang;
19748     }
19749
19750   language = DW_LANG_C;
19751   if (strncmp (language_string, "GNU C", 5) == 0
19752       && ISDIGIT (language_string[5]))
19753     {
19754       language = DW_LANG_C89;
19755       if (dwarf_version >= 3 || !dwarf_strict)
19756         {
19757           if (strcmp (language_string, "GNU C89") != 0)
19758             language = DW_LANG_C99;
19759
19760           if (dwarf_version >= 5 /* || !dwarf_strict */)
19761             if (strcmp (language_string, "GNU C11") == 0)
19762               language = DW_LANG_C11;
19763         }
19764     }
19765   else if (strncmp (language_string, "GNU C++", 7) == 0)
19766     {
19767       language = DW_LANG_C_plus_plus;
19768       if (dwarf_version >= 5 /* || !dwarf_strict */)
19769         {
19770           if (strcmp (language_string, "GNU C++11") == 0)
19771             language = DW_LANG_C_plus_plus_11;
19772           else if (strcmp (language_string, "GNU C++14") == 0)
19773             language = DW_LANG_C_plus_plus_14;
19774         }
19775     }
19776   else if (strcmp (language_string, "GNU F77") == 0)
19777     language = DW_LANG_Fortran77;
19778   else if (strcmp (language_string, "GNU Pascal") == 0)
19779     language = DW_LANG_Pascal83;
19780   else if (dwarf_version >= 3 || !dwarf_strict)
19781     {
19782       if (strcmp (language_string, "GNU Ada") == 0)
19783         language = DW_LANG_Ada95;
19784       else if (strcmp (language_string, "GNU Fortran") == 0)
19785         language = DW_LANG_Fortran95;
19786       else if (strcmp (language_string, "GNU Java") == 0)
19787         language = DW_LANG_Java;
19788       else if (strcmp (language_string, "GNU Objective-C") == 0)
19789         language = DW_LANG_ObjC;
19790       else if (strcmp (language_string, "GNU Objective-C++") == 0)
19791         language = DW_LANG_ObjC_plus_plus;
19792       else if (dwarf_version >= 5 || !dwarf_strict)
19793         {
19794           if (strcmp (language_string, "GNU Go") == 0)
19795             language = DW_LANG_Go;
19796         }
19797     }
19798   /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works.  */
19799   else if (strcmp (language_string, "GNU Fortran") == 0)
19800     language = DW_LANG_Fortran90;
19801
19802   add_AT_unsigned (die, DW_AT_language, language);
19803
19804   switch (language)
19805     {
19806     case DW_LANG_Fortran77:
19807     case DW_LANG_Fortran90:
19808     case DW_LANG_Fortran95:
19809       /* Fortran has case insensitive identifiers and the front-end
19810          lowercases everything.  */
19811       add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19812       break;
19813     default:
19814       /* The default DW_ID_case_sensitive doesn't need to be specified.  */
19815       break;
19816     }
19817   return die;
19818 }
19819
19820 /* Generate the DIE for a base class.  */
19821
19822 static void
19823 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19824 {
19825   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19826
19827   add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, context_die);
19828   add_data_member_location_attribute (die, binfo);
19829
19830   if (BINFO_VIRTUAL_P (binfo))
19831     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19832
19833   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19834      children, otherwise the default is DW_ACCESS_public.  In DWARF2
19835      the default has always been DW_ACCESS_private.  */
19836   if (access == access_public_node)
19837     {
19838       if (dwarf_version == 2
19839           || context_die->die_tag == DW_TAG_class_type)
19840       add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19841     }
19842   else if (access == access_protected_node)
19843     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19844   else if (dwarf_version > 2
19845            && context_die->die_tag != DW_TAG_class_type)
19846     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19847 }
19848
19849 /* Generate a DIE for a class member.  */
19850
19851 static void
19852 gen_member_die (tree type, dw_die_ref context_die)
19853 {
19854   tree member;
19855   tree binfo = TYPE_BINFO (type);
19856   dw_die_ref child;
19857
19858   /* If this is not an incomplete type, output descriptions of each of its
19859      members. Note that as we output the DIEs necessary to represent the
19860      members of this record or union type, we will also be trying to output
19861      DIEs to represent the *types* of those members. However the `type'
19862      function (above) will specifically avoid generating type DIEs for member
19863      types *within* the list of member DIEs for this (containing) type except
19864      for those types (of members) which are explicitly marked as also being
19865      members of this (containing) type themselves.  The g++ front- end can
19866      force any given type to be treated as a member of some other (containing)
19867      type by setting the TYPE_CONTEXT of the given (member) type to point to
19868      the TREE node representing the appropriate (containing) type.  */
19869
19870   /* First output info about the base classes.  */
19871   if (binfo)
19872     {
19873       vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
19874       int i;
19875       tree base;
19876
19877       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19878         gen_inheritance_die (base,
19879                              (accesses ? (*accesses)[i] : access_public_node),
19880                              context_die);
19881     }
19882
19883   /* Now output info about the data members and type members.  */
19884   for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19885     {
19886       /* If we thought we were generating minimal debug info for TYPE
19887          and then changed our minds, some of the member declarations
19888          may have already been defined.  Don't define them again, but
19889          do put them in the right order.  */
19890
19891       child = lookup_decl_die (member);
19892       if (child)
19893         splice_child_die (context_die, child);
19894       else
19895         gen_decl_die (member, NULL, context_die);
19896     }
19897
19898   /* Now output info about the function members (if any).  */
19899   for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19900     {
19901       /* Don't include clones in the member list.  */
19902       if (DECL_ABSTRACT_ORIGIN (member))
19903         continue;
19904
19905       child = lookup_decl_die (member);
19906       if (child)
19907         splice_child_die (context_die, child);
19908       else
19909         gen_decl_die (member, NULL, context_die);
19910     }
19911 }
19912
19913 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
19914    is set, we pretend that the type was never defined, so we only get the
19915    member DIEs needed by later specification DIEs.  */
19916
19917 static void
19918 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19919                                 enum debug_info_usage usage)
19920 {
19921   dw_die_ref type_die = lookup_type_die (type);
19922   dw_die_ref scope_die = 0;
19923   int nested = 0;
19924   int complete = (TYPE_SIZE (type)
19925                   && (! TYPE_STUB_DECL (type)
19926                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19927   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19928   complete = complete && should_emit_struct_debug (type, usage);
19929
19930   if (type_die && ! complete)
19931     return;
19932
19933   if (TYPE_CONTEXT (type) != NULL_TREE
19934       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19935           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19936     nested = 1;
19937
19938   scope_die = scope_die_for (type, context_die);
19939
19940   /* Generate child dies for template paramaters.  */
19941   if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
19942     schedule_generic_params_dies_gen (type);
19943
19944   if (! type_die || (nested && is_cu_die (scope_die)))
19945     /* First occurrence of type or toplevel definition of nested class.  */
19946     {
19947       dw_die_ref old_die = type_die;
19948
19949       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19950                           ? record_type_tag (type) : DW_TAG_union_type,
19951                           scope_die, type);
19952       equate_type_number_to_die (type, type_die);
19953       if (old_die)
19954         add_AT_specification (type_die, old_die);
19955       else
19956         add_name_attribute (type_die, type_tag (type));
19957     }
19958   else
19959     remove_AT (type_die, DW_AT_declaration);
19960
19961   /* If this type has been completed, then give it a byte_size attribute and
19962      then give a list of members.  */
19963   if (complete && !ns_decl)
19964     {
19965       /* Prevent infinite recursion in cases where the type of some member of
19966          this type is expressed in terms of this type itself.  */
19967       TREE_ASM_WRITTEN (type) = 1;
19968       add_byte_size_attribute (type_die, type);
19969       if (TYPE_STUB_DECL (type) != NULL_TREE)
19970         {
19971           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19972           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19973         }
19974
19975       /* If the first reference to this type was as the return type of an
19976          inline function, then it may not have a parent.  Fix this now.  */
19977       if (type_die->die_parent == NULL)
19978         add_child_die (scope_die, type_die);
19979
19980       push_decl_scope (type);
19981       gen_member_die (type, type_die);
19982       pop_decl_scope ();
19983
19984       add_gnat_descriptive_type_attribute (type_die, type, context_die);
19985       if (TYPE_ARTIFICIAL (type))
19986         add_AT_flag (type_die, DW_AT_artificial, 1);
19987
19988       /* GNU extension: Record what type our vtable lives in.  */
19989       if (TYPE_VFIELD (type))
19990         {
19991           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
19992
19993           gen_type_die (vtype, context_die);
19994           add_AT_die_ref (type_die, DW_AT_containing_type,
19995                           lookup_type_die (vtype));
19996         }
19997     }
19998   else
19999     {
20000       add_AT_flag (type_die, DW_AT_declaration, 1);
20001
20002       /* We don't need to do this for function-local types.  */
20003       if (TYPE_STUB_DECL (type)
20004           && ! decl_function_context (TYPE_STUB_DECL (type)))
20005         vec_safe_push (incomplete_types, type);
20006     }
20007
20008   if (get_AT (type_die, DW_AT_name))
20009     add_pubtype (type, type_die);
20010 }
20011
20012 /* Generate a DIE for a subroutine _type_.  */
20013
20014 static void
20015 gen_subroutine_type_die (tree type, dw_die_ref context_die)
20016 {
20017   tree return_type = TREE_TYPE (type);
20018   dw_die_ref subr_die
20019     = new_die (DW_TAG_subroutine_type,
20020                scope_die_for (type, context_die), type);
20021
20022   equate_type_number_to_die (type, subr_die);
20023   add_prototyped_attribute (subr_die, type);
20024   add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, context_die);
20025   gen_formal_types_die (type, subr_die);
20026
20027   if (get_AT (subr_die, DW_AT_name))
20028     add_pubtype (type, subr_die);
20029 }
20030
20031 /* Generate a DIE for a type definition.  */
20032
20033 static void
20034 gen_typedef_die (tree decl, dw_die_ref context_die)
20035 {
20036   dw_die_ref type_die;
20037   tree origin;
20038
20039   if (TREE_ASM_WRITTEN (decl))
20040     return;
20041
20042   TREE_ASM_WRITTEN (decl) = 1;
20043   type_die = new_die (DW_TAG_typedef, context_die, decl);
20044   origin = decl_ultimate_origin (decl);
20045   if (origin != NULL)
20046     add_abstract_origin_attribute (type_die, origin);
20047   else
20048     {
20049       tree type;
20050
20051       add_name_and_src_coords_attributes (type_die, decl);
20052       if (DECL_ORIGINAL_TYPE (decl))
20053         {
20054           type = DECL_ORIGINAL_TYPE (decl);
20055
20056           gcc_assert (type != TREE_TYPE (decl));
20057           equate_type_number_to_die (TREE_TYPE (decl), type_die);
20058         }
20059       else
20060         {
20061           type = TREE_TYPE (decl);
20062
20063           if (is_naming_typedef_decl (TYPE_NAME (type)))
20064             {
20065               /* Here, we are in the case of decl being a typedef naming
20066                  an anonymous type, e.g:
20067                      typedef struct {...} foo;
20068                  In that case TREE_TYPE (decl) is not a typedef variant
20069                  type and TYPE_NAME of the anonymous type is set to the
20070                  TYPE_DECL of the typedef. This construct is emitted by
20071                  the C++ FE.
20072
20073                  TYPE is the anonymous struct named by the typedef
20074                  DECL. As we need the DW_AT_type attribute of the
20075                  DW_TAG_typedef to point to the DIE of TYPE, let's
20076                  generate that DIE right away. add_type_attribute
20077                  called below will then pick (via lookup_type_die) that
20078                  anonymous struct DIE.  */
20079               if (!TREE_ASM_WRITTEN (type))
20080                 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
20081
20082               /* This is a GNU Extension.  We are adding a
20083                  DW_AT_linkage_name attribute to the DIE of the
20084                  anonymous struct TYPE.  The value of that attribute
20085                  is the name of the typedef decl naming the anonymous
20086                  struct.  This greatly eases the work of consumers of
20087                  this debug info.  */
20088               add_linkage_attr (lookup_type_die (type), decl);
20089             }
20090         }
20091
20092       add_type_attribute (type_die, type, decl_quals (decl), context_die);
20093
20094       if (is_naming_typedef_decl (decl))
20095         /* We want that all subsequent calls to lookup_type_die with
20096            TYPE in argument yield the DW_TAG_typedef we have just
20097            created.  */
20098         equate_type_number_to_die (type, type_die);
20099
20100       add_accessibility_attribute (type_die, decl);
20101     }
20102
20103   if (DECL_ABSTRACT_P (decl))
20104     equate_decl_number_to_die (decl, type_die);
20105
20106   if (get_AT (type_die, DW_AT_name))
20107     add_pubtype (decl, type_die);
20108 }
20109
20110 /* Generate a DIE for a struct, class, enum or union type.  */
20111
20112 static void
20113 gen_tagged_type_die (tree type,
20114                      dw_die_ref context_die,
20115                      enum debug_info_usage usage)
20116 {
20117   int need_pop;
20118
20119   if (type == NULL_TREE
20120       || !is_tagged_type (type))
20121     return;
20122
20123   /* If this is a nested type whose containing class hasn't been written
20124      out yet, writing it out will cover this one, too.  This does not apply
20125      to instantiations of member class templates; they need to be added to
20126      the containing class as they are generated.  FIXME: This hurts the
20127      idea of combining type decls from multiple TUs, since we can't predict
20128      what set of template instantiations we'll get.  */
20129   if (TYPE_CONTEXT (type)
20130       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20131       && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
20132     {
20133       gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
20134
20135       if (TREE_ASM_WRITTEN (type))
20136         return;
20137
20138       /* If that failed, attach ourselves to the stub.  */
20139       push_decl_scope (TYPE_CONTEXT (type));
20140       context_die = lookup_type_die (TYPE_CONTEXT (type));
20141       need_pop = 1;
20142     }
20143   else if (TYPE_CONTEXT (type) != NULL_TREE
20144            && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
20145     {
20146       /* If this type is local to a function that hasn't been written
20147          out yet, use a NULL context for now; it will be fixed up in
20148          decls_for_scope.  */
20149       context_die = lookup_decl_die (TYPE_CONTEXT (type));
20150       /* A declaration DIE doesn't count; nested types need to go in the
20151          specification.  */
20152       if (context_die && is_declaration_die (context_die))
20153         context_die = NULL;
20154       need_pop = 0;
20155     }
20156   else
20157     {
20158       context_die = declare_in_namespace (type, context_die);
20159       need_pop = 0;
20160     }
20161
20162   if (TREE_CODE (type) == ENUMERAL_TYPE)
20163     {
20164       /* This might have been written out by the call to
20165          declare_in_namespace.  */
20166       if (!TREE_ASM_WRITTEN (type))
20167         gen_enumeration_type_die (type, context_die);
20168     }
20169   else
20170     gen_struct_or_union_type_die (type, context_die, usage);
20171
20172   if (need_pop)
20173     pop_decl_scope ();
20174
20175   /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
20176      it up if it is ever completed.  gen_*_type_die will set it for us
20177      when appropriate.  */
20178 }
20179
20180 /* Generate a type description DIE.  */
20181
20182 static void
20183 gen_type_die_with_usage (tree type, dw_die_ref context_die,
20184                          enum debug_info_usage usage)
20185 {
20186   struct array_descr_info info;
20187
20188   if (type == NULL_TREE || type == error_mark_node)
20189     return;
20190
20191   if (TYPE_NAME (type) != NULL_TREE
20192       && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20193       && is_redundant_typedef (TYPE_NAME (type))
20194       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
20195     /* The DECL of this type is a typedef we don't want to emit debug
20196        info for but we want debug info for its underlying typedef.
20197        This can happen for e.g, the injected-class-name of a C++
20198        type.  */
20199     type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
20200
20201   /* If TYPE is a typedef type variant, let's generate debug info
20202      for the parent typedef which TYPE is a type of.  */
20203   if (typedef_variant_p (type))
20204     {
20205       if (TREE_ASM_WRITTEN (type))
20206         return;
20207
20208       /* Prevent broken recursion; we can't hand off to the same type.  */
20209       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
20210
20211       /* Give typedefs the right scope.  */
20212       context_die = scope_die_for (type, context_die);
20213
20214       TREE_ASM_WRITTEN (type) = 1;
20215
20216       gen_decl_die (TYPE_NAME (type), NULL, context_die);
20217       return;
20218     }
20219
20220   /* If type is an anonymous tagged type named by a typedef, let's
20221      generate debug info for the typedef.  */
20222   if (is_naming_typedef_decl (TYPE_NAME (type)))
20223     {
20224       /* Use the DIE of the containing namespace as the parent DIE of
20225          the type description DIE we want to generate.  */
20226       if (DECL_CONTEXT (TYPE_NAME (type))
20227           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20228         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20229       
20230       gen_decl_die (TYPE_NAME (type), NULL, context_die);
20231       return;
20232     }
20233
20234   /* We are going to output a DIE to represent the unqualified version
20235      of this type (i.e. without any const or volatile qualifiers) so
20236      get the main variant (i.e. the unqualified version) of this type
20237      now.  (Vectors are special because the debugging info is in the
20238      cloned type itself).  */
20239   if (TREE_CODE (type) != VECTOR_TYPE)
20240     type = type_main_variant (type);
20241
20242   /* If this is an array type with hidden descriptor, handle it first.  */
20243   if (!TREE_ASM_WRITTEN (type)
20244       && lang_hooks.types.get_array_descr_info)
20245     {
20246       memset (&info, 0, sizeof (info));
20247       if (lang_hooks.types.get_array_descr_info (type, &info))
20248         {
20249           gen_descr_array_type_die (type, &info, context_die);
20250           TREE_ASM_WRITTEN (type) = 1;
20251           return;
20252         }
20253     }
20254
20255   if (TREE_ASM_WRITTEN (type))
20256     return;
20257
20258   switch (TREE_CODE (type))
20259     {
20260     case ERROR_MARK:
20261       break;
20262
20263     case POINTER_TYPE:
20264     case REFERENCE_TYPE:
20265       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
20266          ensures that the gen_type_die recursion will terminate even if the
20267          type is recursive.  Recursive types are possible in Ada.  */
20268       /* ??? We could perhaps do this for all types before the switch
20269          statement.  */
20270       TREE_ASM_WRITTEN (type) = 1;
20271
20272       /* For these types, all that is required is that we output a DIE (or a
20273          set of DIEs) to represent the "basis" type.  */
20274       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20275                                 DINFO_USAGE_IND_USE);
20276       break;
20277
20278     case OFFSET_TYPE:
20279       /* This code is used for C++ pointer-to-data-member types.
20280          Output a description of the relevant class type.  */
20281       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
20282                                         DINFO_USAGE_IND_USE);
20283
20284       /* Output a description of the type of the object pointed to.  */
20285       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20286                                         DINFO_USAGE_IND_USE);
20287
20288       /* Now output a DIE to represent this pointer-to-data-member type
20289          itself.  */
20290       gen_ptr_to_mbr_type_die (type, context_die);
20291       break;
20292
20293     case FUNCTION_TYPE:
20294       /* Force out return type (in case it wasn't forced out already).  */
20295       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20296                                         DINFO_USAGE_DIR_USE);
20297       gen_subroutine_type_die (type, context_die);
20298       break;
20299
20300     case METHOD_TYPE:
20301       /* Force out return type (in case it wasn't forced out already).  */
20302       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20303                                         DINFO_USAGE_DIR_USE);
20304       gen_subroutine_type_die (type, context_die);
20305       break;
20306
20307     case ARRAY_TYPE:
20308       gen_array_type_die (type, context_die);
20309       break;
20310
20311     case VECTOR_TYPE:
20312       gen_array_type_die (type, context_die);
20313       break;
20314
20315     case ENUMERAL_TYPE:
20316     case RECORD_TYPE:
20317     case UNION_TYPE:
20318     case QUAL_UNION_TYPE:
20319       gen_tagged_type_die (type, context_die, usage);
20320       return;
20321
20322     case VOID_TYPE:
20323     case INTEGER_TYPE:
20324     case REAL_TYPE:
20325     case FIXED_POINT_TYPE:
20326     case COMPLEX_TYPE:
20327     case BOOLEAN_TYPE:
20328     case POINTER_BOUNDS_TYPE:
20329       /* No DIEs needed for fundamental types.  */
20330       break;
20331
20332     case NULLPTR_TYPE:
20333     case LANG_TYPE:
20334       /* Just use DW_TAG_unspecified_type.  */
20335       {
20336         dw_die_ref type_die = lookup_type_die (type);
20337         if (type_die == NULL)
20338           {
20339             tree name = TYPE_IDENTIFIER (type);
20340             type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
20341                                 type);
20342             add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20343             equate_type_number_to_die (type, type_die);
20344           }
20345       }
20346       break;
20347
20348     default:
20349       if (is_cxx_auto (type))
20350         {
20351           tree name = TYPE_IDENTIFIER (type);
20352           dw_die_ref *die = (name == get_identifier ("auto")
20353                              ? &auto_die : &decltype_auto_die);
20354           if (!*die)
20355             {
20356               *die = new_die (DW_TAG_unspecified_type,
20357                               comp_unit_die (), NULL_TREE);
20358               add_name_attribute (*die, IDENTIFIER_POINTER (name));
20359             }
20360           equate_type_number_to_die (type, *die);
20361           break;
20362         }
20363       gcc_unreachable ();
20364     }
20365
20366   TREE_ASM_WRITTEN (type) = 1;
20367 }
20368
20369 static void
20370 gen_type_die (tree type, dw_die_ref context_die)
20371 {
20372   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20373 }
20374
20375 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20376    things which are local to the given block.  */
20377
20378 static void
20379 gen_block_die (tree stmt, dw_die_ref context_die)
20380 {
20381   int must_output_die = 0;
20382   bool inlined_func;
20383
20384   /* Ignore blocks that are NULL.  */
20385   if (stmt == NULL_TREE)
20386     return;
20387
20388   inlined_func = inlined_function_outer_scope_p (stmt);
20389
20390   /* If the block is one fragment of a non-contiguous block, do not
20391      process the variables, since they will have been done by the
20392      origin block.  Do process subblocks.  */
20393   if (BLOCK_FRAGMENT_ORIGIN (stmt))
20394     {
20395       tree sub;
20396
20397       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20398         gen_block_die (sub, context_die);
20399
20400       return;
20401     }
20402
20403   /* Determine if we need to output any Dwarf DIEs at all to represent this
20404      block.  */
20405   if (inlined_func)
20406     /* The outer scopes for inlinings *must* always be represented.  We
20407        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
20408     must_output_die = 1;
20409   else
20410     {
20411       /* Determine if this block directly contains any "significant"
20412          local declarations which we will need to output DIEs for.  */
20413       if (debug_info_level > DINFO_LEVEL_TERSE)
20414         /* We are not in terse mode so *any* local declaration counts
20415            as being a "significant" one.  */
20416         must_output_die = ((BLOCK_VARS (stmt) != NULL
20417                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20418                            && (TREE_USED (stmt)
20419                                || TREE_ASM_WRITTEN (stmt)
20420                                || BLOCK_ABSTRACT (stmt)));
20421       else if ((TREE_USED (stmt)
20422                 || TREE_ASM_WRITTEN (stmt)
20423                 || BLOCK_ABSTRACT (stmt))
20424                && !dwarf2out_ignore_block (stmt))
20425         must_output_die = 1;
20426     }
20427
20428   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20429      DIE for any block which contains no significant local declarations at
20430      all.  Rather, in such cases we just call `decls_for_scope' so that any
20431      needed Dwarf info for any sub-blocks will get properly generated. Note
20432      that in terse mode, our definition of what constitutes a "significant"
20433      local declaration gets restricted to include only inlined function
20434      instances and local (nested) function definitions.  */
20435   if (must_output_die)
20436     {
20437       if (inlined_func)
20438         {
20439           /* If STMT block is abstract, that means we have been called
20440              indirectly from dwarf2out_abstract_function.
20441              That function rightfully marks the descendent blocks (of
20442              the abstract function it is dealing with) as being abstract,
20443              precisely to prevent us from emitting any
20444              DW_TAG_inlined_subroutine DIE as a descendent
20445              of an abstract function instance. So in that case, we should
20446              not call gen_inlined_subroutine_die.
20447
20448              Later though, when cgraph asks dwarf2out to emit info
20449              for the concrete instance of the function decl into which
20450              the concrete instance of STMT got inlined, the later will lead
20451              to the generation of a DW_TAG_inlined_subroutine DIE.  */
20452           if (! BLOCK_ABSTRACT (stmt))
20453             gen_inlined_subroutine_die (stmt, context_die);
20454         }
20455       else
20456         gen_lexical_block_die (stmt, context_die);
20457     }
20458   else
20459     decls_for_scope (stmt, context_die);
20460 }
20461
20462 /* Process variable DECL (or variable with origin ORIGIN) within
20463    block STMT and add it to CONTEXT_DIE.  */
20464 static void
20465 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20466 {
20467   dw_die_ref die;
20468   tree decl_or_origin = decl ? decl : origin;
20469
20470   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20471     die = lookup_decl_die (decl_or_origin);
20472   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20473            && TYPE_DECL_IS_STUB (decl_or_origin))
20474     die = lookup_type_die (TREE_TYPE (decl_or_origin));
20475   else
20476     die = NULL;
20477
20478   if (die != NULL && die->die_parent == NULL)
20479     add_child_die (context_die, die);
20480   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20481     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20482                                          stmt, context_die);
20483   else
20484     gen_decl_die (decl, origin, context_die);
20485 }
20486
20487 /* Generate all of the decls declared within a given scope and (recursively)
20488    all of its sub-blocks.  */
20489
20490 static void
20491 decls_for_scope (tree stmt, dw_die_ref context_die)
20492 {
20493   tree decl;
20494   unsigned int i;
20495   tree subblocks;
20496
20497   /* Ignore NULL blocks.  */
20498   if (stmt == NULL_TREE)
20499     return;
20500
20501   /* Output the DIEs to represent all of the data objects and typedefs
20502      declared directly within this block but not within any nested
20503      sub-blocks.  Also, nested function and tag DIEs have been
20504      generated with a parent of NULL; fix that up now.  We don't
20505      have to do this if we're at -g1.  */
20506   if (debug_info_level > DINFO_LEVEL_TERSE)
20507     {
20508       for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20509         process_scope_var (stmt, decl, NULL_TREE, context_die);
20510       for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20511         process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20512                            context_die);
20513     }
20514
20515   /* Even if we're at -g1, we need to process the subblocks in order to get
20516      inlined call information.  */
20517
20518   /* Output the DIEs to represent all sub-blocks (and the items declared
20519      therein) of this block.  */
20520   for (subblocks = BLOCK_SUBBLOCKS (stmt);
20521        subblocks != NULL;
20522        subblocks = BLOCK_CHAIN (subblocks))
20523     gen_block_die (subblocks, context_die);
20524 }
20525
20526 /* Is this a typedef we can avoid emitting?  */
20527
20528 static inline int
20529 is_redundant_typedef (const_tree decl)
20530 {
20531   if (TYPE_DECL_IS_STUB (decl))
20532     return 1;
20533
20534   if (DECL_ARTIFICIAL (decl)
20535       && DECL_CONTEXT (decl)
20536       && is_tagged_type (DECL_CONTEXT (decl))
20537       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20538       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20539     /* Also ignore the artificial member typedef for the class name.  */
20540     return 1;
20541
20542   return 0;
20543 }
20544
20545 /* Return TRUE if TYPE is a typedef that names a type for linkage
20546    purposes. This kind of typedefs is produced by the C++ FE for
20547    constructs like:
20548
20549    typedef struct {...} foo;
20550
20551    In that case, there is no typedef variant type produced for foo.
20552    Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20553    struct type.  */
20554
20555 static bool
20556 is_naming_typedef_decl (const_tree decl)
20557 {
20558   if (decl == NULL_TREE
20559       || TREE_CODE (decl) != TYPE_DECL
20560       || !is_tagged_type (TREE_TYPE (decl))
20561       || DECL_IS_BUILTIN (decl)
20562       || is_redundant_typedef (decl)
20563       /* It looks like Ada produces TYPE_DECLs that are very similar
20564          to C++ naming typedefs but that have different
20565          semantics. Let's be specific to c++ for now.  */
20566       || !is_cxx ())
20567     return FALSE;
20568
20569   return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20570           && TYPE_NAME (TREE_TYPE (decl)) == decl
20571           && (TYPE_STUB_DECL (TREE_TYPE (decl))
20572               != TYPE_NAME (TREE_TYPE (decl))));
20573 }
20574
20575 /* Returns the DIE for a context.  */
20576
20577 static inline dw_die_ref
20578 get_context_die (tree context)
20579 {
20580   if (context)
20581     {
20582       /* Find die that represents this context.  */
20583       if (TYPE_P (context))
20584         {
20585           context = TYPE_MAIN_VARIANT (context);
20586           return strip_naming_typedef (context, force_type_die (context));
20587         }
20588       else
20589         return force_decl_die (context);
20590     }
20591   return comp_unit_die ();
20592 }
20593
20594 /* Returns the DIE for decl.  A DIE will always be returned.  */
20595
20596 static dw_die_ref
20597 force_decl_die (tree decl)
20598 {
20599   dw_die_ref decl_die;
20600   unsigned saved_external_flag;
20601   tree save_fn = NULL_TREE;
20602   decl_die = lookup_decl_die (decl);
20603   if (!decl_die)
20604     {
20605       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20606
20607       decl_die = lookup_decl_die (decl);
20608       if (decl_die)
20609         return decl_die;
20610
20611       switch (TREE_CODE (decl))
20612         {
20613         case FUNCTION_DECL:
20614           /* Clear current_function_decl, so that gen_subprogram_die thinks
20615              that this is a declaration. At this point, we just want to force
20616              declaration die.  */
20617           save_fn = current_function_decl;
20618           current_function_decl = NULL_TREE;
20619           gen_subprogram_die (decl, context_die);
20620           current_function_decl = save_fn;
20621           break;
20622
20623         case VAR_DECL:
20624           /* Set external flag to force declaration die. Restore it after
20625            gen_decl_die() call.  */
20626           saved_external_flag = DECL_EXTERNAL (decl);
20627           DECL_EXTERNAL (decl) = 1;
20628           gen_decl_die (decl, NULL, context_die);
20629           DECL_EXTERNAL (decl) = saved_external_flag;
20630           break;
20631
20632         case NAMESPACE_DECL:
20633           if (dwarf_version >= 3 || !dwarf_strict)
20634             dwarf2out_decl (decl);
20635           else
20636             /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
20637             decl_die = comp_unit_die ();
20638           break;
20639
20640         case TRANSLATION_UNIT_DECL:
20641           decl_die = comp_unit_die ();
20642           break;
20643
20644         default:
20645           gcc_unreachable ();
20646         }
20647
20648       /* We should be able to find the DIE now.  */
20649       if (!decl_die)
20650         decl_die = lookup_decl_die (decl);
20651       gcc_assert (decl_die);
20652     }
20653
20654   return decl_die;
20655 }
20656
20657 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
20658    always returned.  */
20659
20660 static dw_die_ref
20661 force_type_die (tree type)
20662 {
20663   dw_die_ref type_die;
20664
20665   type_die = lookup_type_die (type);
20666   if (!type_die)
20667     {
20668       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
20669
20670       type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
20671                                     context_die);
20672       gcc_assert (type_die);
20673     }
20674   return type_die;
20675 }
20676
20677 /* Force out any required namespaces to be able to output DECL,
20678    and return the new context_die for it, if it's changed.  */
20679
20680 static dw_die_ref
20681 setup_namespace_context (tree thing, dw_die_ref context_die)
20682 {
20683   tree context = (DECL_P (thing)
20684                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
20685   if (context && TREE_CODE (context) == NAMESPACE_DECL)
20686     /* Force out the namespace.  */
20687     context_die = force_decl_die (context);
20688
20689   return context_die;
20690 }
20691
20692 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
20693    type) within its namespace, if appropriate.
20694
20695    For compatibility with older debuggers, namespace DIEs only contain
20696    declarations; all definitions are emitted at CU scope.  */
20697
20698 static dw_die_ref
20699 declare_in_namespace (tree thing, dw_die_ref context_die)
20700 {
20701   dw_die_ref ns_context;
20702
20703   if (debug_info_level <= DINFO_LEVEL_TERSE)
20704     return context_die;
20705
20706   /* External declarations in the local scope only need to be emitted
20707      once, not once in the namespace and once in the scope.
20708
20709      This avoids declaring the `extern' below in the
20710      namespace DIE as well as in the innermost scope:
20711
20712           namespace S
20713           {
20714             int i=5;
20715             int foo()
20716             {
20717               int i=8;
20718               extern int i;
20719               return i;
20720             }
20721           }
20722   */
20723   if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
20724     return context_die;
20725
20726   /* If this decl is from an inlined function, then don't try to emit it in its
20727      namespace, as we will get confused.  It would have already been emitted
20728      when the abstract instance of the inline function was emitted anyways.  */
20729   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
20730     return context_die;
20731
20732   ns_context = setup_namespace_context (thing, context_die);
20733
20734   if (ns_context != context_die)
20735     {
20736       if (is_fortran ())
20737         return ns_context;
20738       if (DECL_P (thing))
20739         gen_decl_die (thing, NULL, ns_context);
20740       else
20741         gen_type_die (thing, ns_context);
20742     }
20743   return context_die;
20744 }
20745
20746 /* Generate a DIE for a namespace or namespace alias.  */
20747
20748 static void
20749 gen_namespace_die (tree decl, dw_die_ref context_die)
20750 {
20751   dw_die_ref namespace_die;
20752
20753   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
20754      they are an alias of.  */
20755   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
20756     {
20757       /* Output a real namespace or module.  */
20758       context_die = setup_namespace_context (decl, comp_unit_die ());
20759       namespace_die = new_die (is_fortran ()
20760                                ? DW_TAG_module : DW_TAG_namespace,
20761                                context_die, decl);
20762       /* For Fortran modules defined in different CU don't add src coords.  */
20763       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20764         {
20765           const char *name = dwarf2_name (decl, 0);
20766           if (name)
20767             add_name_attribute (namespace_die, name);
20768         }
20769       else
20770         add_name_and_src_coords_attributes (namespace_die, decl);
20771       if (DECL_EXTERNAL (decl))
20772         add_AT_flag (namespace_die, DW_AT_declaration, 1);
20773       equate_decl_number_to_die (decl, namespace_die);
20774     }
20775   else
20776     {
20777       /* Output a namespace alias.  */
20778
20779       /* Force out the namespace we are an alias of, if necessary.  */
20780       dw_die_ref origin_die
20781         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20782
20783       if (DECL_FILE_SCOPE_P (decl)
20784           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20785         context_die = setup_namespace_context (decl, comp_unit_die ());
20786       /* Now create the namespace alias DIE.  */
20787       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20788       add_name_and_src_coords_attributes (namespace_die, decl);
20789       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20790       equate_decl_number_to_die (decl, namespace_die);
20791     }
20792   /* Bypass dwarf2_name's check for DECL_NAMELESS.  */
20793   if (want_pubnames ())
20794     add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
20795 }
20796
20797 /* Generate Dwarf debug information for a decl described by DECL.
20798    The return value is currently only meaningful for PARM_DECLs,
20799    for all other decls it returns NULL.  */
20800
20801 static dw_die_ref
20802 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20803 {
20804   tree decl_or_origin = decl ? decl : origin;
20805   tree class_origin = NULL, ultimate_origin;
20806
20807   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20808     return NULL;
20809
20810   /* Ignore pointer bounds decls.  */
20811   if (DECL_P (decl_or_origin)
20812       && TREE_TYPE (decl_or_origin)
20813       && POINTER_BOUNDS_P (decl_or_origin))
20814     return NULL;
20815
20816   switch (TREE_CODE (decl_or_origin))
20817     {
20818     case ERROR_MARK:
20819       break;
20820
20821     case CONST_DECL:
20822       if (!is_fortran () && !is_ada ())
20823         {
20824           /* The individual enumerators of an enum type get output when we output
20825              the Dwarf representation of the relevant enum type itself.  */
20826           break;
20827         }
20828
20829       /* Emit its type.  */
20830       gen_type_die (TREE_TYPE (decl), context_die);
20831
20832       /* And its containing namespace.  */
20833       context_die = declare_in_namespace (decl, context_die);
20834
20835       gen_const_die (decl, context_die);
20836       break;
20837
20838     case FUNCTION_DECL:
20839       /* Don't output any DIEs to represent mere function declarations,
20840          unless they are class members or explicit block externs.  */
20841       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20842           && DECL_FILE_SCOPE_P (decl_or_origin)
20843           && (current_function_decl == NULL_TREE
20844               || DECL_ARTIFICIAL (decl_or_origin)))
20845         break;
20846
20847 #if 0
20848       /* FIXME */
20849       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20850          on local redeclarations of global functions.  That seems broken.  */
20851       if (current_function_decl != decl)
20852         /* This is only a declaration.  */;
20853 #endif
20854
20855       /* If we're emitting a clone, emit info for the abstract instance.  */
20856       if (origin || DECL_ORIGIN (decl) != decl)
20857         dwarf2out_abstract_function (origin
20858                                      ? DECL_ORIGIN (origin)
20859                                      : DECL_ABSTRACT_ORIGIN (decl));
20860
20861       /* If we're emitting an out-of-line copy of an inline function,
20862          emit info for the abstract instance and set up to refer to it.  */
20863       else if (cgraph_function_possibly_inlined_p (decl)
20864                && ! DECL_ABSTRACT_P (decl)
20865                && ! class_or_namespace_scope_p (context_die)
20866                /* dwarf2out_abstract_function won't emit a die if this is just
20867                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
20868                   that case, because that works only if we have a die.  */
20869                && DECL_INITIAL (decl) != NULL_TREE)
20870         {
20871           dwarf2out_abstract_function (decl);
20872           set_decl_origin_self (decl);
20873         }
20874
20875       /* Otherwise we're emitting the primary DIE for this decl.  */
20876       else if (debug_info_level > DINFO_LEVEL_TERSE)
20877         {
20878           /* Before we describe the FUNCTION_DECL itself, make sure that we
20879              have its containing type.  */
20880           if (!origin)
20881             origin = decl_class_context (decl);
20882           if (origin != NULL_TREE)
20883             gen_type_die (origin, context_die);
20884
20885           /* And its return type.  */
20886           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20887
20888           /* And its virtual context.  */
20889           if (DECL_VINDEX (decl) != NULL_TREE)
20890             gen_type_die (DECL_CONTEXT (decl), context_die);
20891
20892           /* Make sure we have a member DIE for decl.  */
20893           if (origin != NULL_TREE)
20894             gen_type_die_for_member (origin, decl, context_die);
20895
20896           /* And its containing namespace.  */
20897           context_die = declare_in_namespace (decl, context_die);
20898         }
20899
20900       /* Now output a DIE to represent the function itself.  */
20901       if (decl)
20902         gen_subprogram_die (decl, context_die);
20903       break;
20904
20905     case TYPE_DECL:
20906       /* If we are in terse mode, don't generate any DIEs to represent any
20907          actual typedefs.  */
20908       if (debug_info_level <= DINFO_LEVEL_TERSE)
20909         break;
20910
20911       /* In the special case of a TYPE_DECL node representing the declaration
20912          of some type tag, if the given TYPE_DECL is marked as having been
20913          instantiated from some other (original) TYPE_DECL node (e.g. one which
20914          was generated within the original definition of an inline function) we
20915          used to generate a special (abbreviated) DW_TAG_structure_type,
20916          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
20917          should be actually referencing those DIEs, as variable DIEs with that
20918          type would be emitted already in the abstract origin, so it was always
20919          removed during unused type prunning.  Don't add anything in this
20920          case.  */
20921       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20922         break;
20923
20924       if (is_redundant_typedef (decl))
20925         gen_type_die (TREE_TYPE (decl), context_die);
20926       else
20927         /* Output a DIE to represent the typedef itself.  */
20928         gen_typedef_die (decl, context_die);
20929       break;
20930
20931     case LABEL_DECL:
20932       if (debug_info_level >= DINFO_LEVEL_NORMAL)
20933         gen_label_die (decl, context_die);
20934       break;
20935
20936     case VAR_DECL:
20937     case RESULT_DECL:
20938       /* If we are in terse mode, don't generate any DIEs to represent any
20939          variable declarations or definitions.  */
20940       if (debug_info_level <= DINFO_LEVEL_TERSE)
20941         break;
20942
20943       /* Output any DIEs that are needed to specify the type of this data
20944          object.  */
20945       if (decl_by_reference_p (decl_or_origin))
20946         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20947       else
20948         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20949
20950       /* And its containing type.  */
20951       class_origin = decl_class_context (decl_or_origin);
20952       if (class_origin != NULL_TREE)
20953         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20954
20955       /* And its containing namespace.  */
20956       context_die = declare_in_namespace (decl_or_origin, context_die);
20957
20958       /* Now output the DIE to represent the data object itself.  This gets
20959          complicated because of the possibility that the VAR_DECL really
20960          represents an inlined instance of a formal parameter for an inline
20961          function.  */
20962       ultimate_origin = decl_ultimate_origin (decl_or_origin);
20963       if (ultimate_origin != NULL_TREE
20964           && TREE_CODE (ultimate_origin) == PARM_DECL)
20965         gen_formal_parameter_die (decl, origin,
20966                                   true /* Emit name attribute.  */,
20967                                   context_die);
20968       else
20969         gen_variable_die (decl, origin, context_die);
20970       break;
20971
20972     case FIELD_DECL:
20973       /* Ignore the nameless fields that are used to skip bits but handle C++
20974          anonymous unions and structs.  */
20975       if (DECL_NAME (decl) != NULL_TREE
20976           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20977           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20978         {
20979           gen_type_die (member_declared_type (decl), context_die);
20980           gen_field_die (decl, context_die);
20981         }
20982       break;
20983
20984     case PARM_DECL:
20985       if (DECL_BY_REFERENCE (decl_or_origin))
20986         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20987       else
20988         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20989       return gen_formal_parameter_die (decl, origin,
20990                                        true /* Emit name attribute.  */,
20991                                        context_die);
20992
20993     case NAMESPACE_DECL:
20994     case IMPORTED_DECL:
20995       if (dwarf_version >= 3 || !dwarf_strict)
20996         gen_namespace_die (decl, context_die);
20997       break;
20998
20999     case NAMELIST_DECL:
21000       gen_namelist_decl (DECL_NAME (decl), context_die,
21001                          NAMELIST_DECL_ASSOCIATED_DECL (decl));
21002       break;
21003
21004     default:
21005       /* Probably some frontend-internal decl.  Assume we don't care.  */
21006       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
21007       break;
21008     }
21009
21010   return NULL;
21011 }
21012 \f
21013 /* Output debug information for global decl DECL.  Called from toplev.c after
21014    compilation proper has finished.  */
21015
21016 static void
21017 dwarf2out_global_decl (tree decl)
21018 {
21019   /* Output DWARF2 information for file-scope tentative data object
21020      declarations, file-scope (extern) function declarations (which
21021      had no corresponding body) and file-scope tagged type declarations
21022      and definitions which have not yet been forced out.  */
21023   if ((TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
21024       && !POINTER_BOUNDS_P (decl))
21025     dwarf2out_decl (decl);
21026 }
21027
21028 /* Output debug information for type decl DECL.  Called from toplev.c
21029    and from language front ends (to record built-in types).  */
21030 static void
21031 dwarf2out_type_decl (tree decl, int local)
21032 {
21033   if (!local)
21034     dwarf2out_decl (decl);
21035 }
21036
21037 /* Output debug information for imported module or decl DECL.
21038    NAME is non-NULL name in the lexical block if the decl has been renamed.
21039    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
21040    that DECL belongs to.
21041    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
21042 static void
21043 dwarf2out_imported_module_or_decl_1 (tree decl,
21044                                      tree name,
21045                                      tree lexical_block,
21046                                      dw_die_ref lexical_block_die)
21047 {
21048   expanded_location xloc;
21049   dw_die_ref imported_die = NULL;
21050   dw_die_ref at_import_die;
21051
21052   if (TREE_CODE (decl) == IMPORTED_DECL)
21053     {
21054       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
21055       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
21056       gcc_assert (decl);
21057     }
21058   else
21059     xloc = expand_location (input_location);
21060
21061   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
21062     {
21063       at_import_die = force_type_die (TREE_TYPE (decl));
21064       /* For namespace N { typedef void T; } using N::T; base_type_die
21065          returns NULL, but DW_TAG_imported_declaration requires
21066          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
21067       if (!at_import_die)
21068         {
21069           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
21070           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
21071           at_import_die = lookup_type_die (TREE_TYPE (decl));
21072           gcc_assert (at_import_die);
21073         }
21074     }
21075   else
21076     {
21077       at_import_die = lookup_decl_die (decl);
21078       if (!at_import_die)
21079         {
21080           /* If we're trying to avoid duplicate debug info, we may not have
21081              emitted the member decl for this field.  Emit it now.  */
21082           if (TREE_CODE (decl) == FIELD_DECL)
21083             {
21084               tree type = DECL_CONTEXT (decl);
21085
21086               if (TYPE_CONTEXT (type)
21087                   && TYPE_P (TYPE_CONTEXT (type))
21088                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
21089                                                 DINFO_USAGE_DIR_USE))
21090                 return;
21091               gen_type_die_for_member (type, decl,
21092                                        get_context_die (TYPE_CONTEXT (type)));
21093             }
21094           if (TREE_CODE (decl) == NAMELIST_DECL)
21095             at_import_die = gen_namelist_decl (DECL_NAME (decl),
21096                                          get_context_die (DECL_CONTEXT (decl)),
21097                                          NULL_TREE);
21098           else
21099             at_import_die = force_decl_die (decl);
21100         }
21101     }
21102
21103   if (TREE_CODE (decl) == NAMESPACE_DECL)
21104     {
21105       if (dwarf_version >= 3 || !dwarf_strict)
21106         imported_die = new_die (DW_TAG_imported_module,
21107                                 lexical_block_die,
21108                                 lexical_block);
21109       else
21110         return;
21111     }
21112   else
21113     imported_die = new_die (DW_TAG_imported_declaration,
21114                             lexical_block_die,
21115                             lexical_block);
21116
21117   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
21118   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
21119   if (name)
21120     add_AT_string (imported_die, DW_AT_name,
21121                    IDENTIFIER_POINTER (name));
21122   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
21123 }
21124
21125 /* Output debug information for imported module or decl DECL.
21126    NAME is non-NULL name in context if the decl has been renamed.
21127    CHILD is true if decl is one of the renamed decls as part of
21128    importing whole module.  */
21129
21130 static void
21131 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
21132                                    bool child)
21133 {
21134   /* dw_die_ref at_import_die;  */
21135   dw_die_ref scope_die;
21136
21137   if (debug_info_level <= DINFO_LEVEL_TERSE)
21138     return;
21139
21140   gcc_assert (decl);
21141
21142   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
21143      We need decl DIE for reference and scope die. First, get DIE for the decl
21144      itself.  */
21145
21146   /* Get the scope die for decl context. Use comp_unit_die for global module
21147      or decl. If die is not found for non globals, force new die.  */
21148   if (context
21149       && TYPE_P (context)
21150       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
21151     return;
21152
21153   if (!(dwarf_version >= 3 || !dwarf_strict))
21154     return;
21155
21156   scope_die = get_context_die (context);
21157
21158   if (child)
21159     {
21160       gcc_assert (scope_die->die_child);
21161       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
21162       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
21163       scope_die = scope_die->die_child;
21164     }
21165
21166   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
21167   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
21168
21169 }
21170
21171 /* Output debug information for namelists.   */
21172
21173 static dw_die_ref
21174 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
21175 {
21176   dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
21177   tree value;
21178   unsigned i;
21179
21180   if (debug_info_level <= DINFO_LEVEL_TERSE)
21181     return NULL;
21182
21183   gcc_assert (scope_die != NULL);
21184   nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
21185   add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
21186
21187   /* If there are no item_decls, we have a nondefining namelist, e.g.
21188      with USE association; hence, set DW_AT_declaration.  */
21189   if (item_decls == NULL_TREE)
21190     {
21191       add_AT_flag (nml_die, DW_AT_declaration, 1);
21192       return nml_die;
21193     }
21194
21195   FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
21196     {
21197       nml_item_ref_die = lookup_decl_die (value);
21198       if (!nml_item_ref_die)
21199         nml_item_ref_die = force_decl_die (value);
21200
21201       nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
21202       add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
21203     }
21204   return nml_die;
21205 }
21206
21207
21208 /* Write the debugging output for DECL.  */
21209
21210 static void
21211 dwarf2out_decl (tree decl)
21212 {
21213   dw_die_ref context_die = comp_unit_die ();
21214
21215   switch (TREE_CODE (decl))
21216     {
21217     case ERROR_MARK:
21218       return;
21219
21220     case FUNCTION_DECL:
21221       /* What we would really like to do here is to filter out all mere
21222          file-scope declarations of file-scope functions which are never
21223          referenced later within this translation unit (and keep all of ones
21224          that *are* referenced later on) but we aren't clairvoyant, so we have
21225          no idea which functions will be referenced in the future (i.e. later
21226          on within the current translation unit). So here we just ignore all
21227          file-scope function declarations which are not also definitions.  If
21228          and when the debugger needs to know something about these functions,
21229          it will have to hunt around and find the DWARF information associated
21230          with the definition of the function.
21231
21232          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
21233          nodes represent definitions and which ones represent mere
21234          declarations.  We have to check DECL_INITIAL instead. That's because
21235          the C front-end supports some weird semantics for "extern inline"
21236          function definitions.  These can get inlined within the current
21237          translation unit (and thus, we need to generate Dwarf info for their
21238          abstract instances so that the Dwarf info for the concrete inlined
21239          instances can have something to refer to) but the compiler never
21240          generates any out-of-lines instances of such things (despite the fact
21241          that they *are* definitions).
21242
21243          The important point is that the C front-end marks these "extern
21244          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
21245          them anyway. Note that the C++ front-end also plays some similar games
21246          for inline function definitions appearing within include files which
21247          also contain `#pragma interface' pragmas.
21248
21249          If we are called from dwarf2out_abstract_function output a DIE
21250          anyway.  We can end up here this way with early inlining and LTO
21251          where the inlined function is output in a different LTRANS unit
21252          or not at all.  */
21253       if (DECL_INITIAL (decl) == NULL_TREE
21254           && ! DECL_ABSTRACT_P (decl))
21255         return;
21256
21257       /* If we're a nested function, initially use a parent of NULL; if we're
21258          a plain function, this will be fixed up in decls_for_scope.  If
21259          we're a method, it will be ignored, since we already have a DIE.  */
21260       if (decl_function_context (decl)
21261           /* But if we're in terse mode, we don't care about scope.  */
21262           && debug_info_level > DINFO_LEVEL_TERSE)
21263         context_die = NULL;
21264       break;
21265
21266     case VAR_DECL:
21267       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
21268          declaration and if the declaration was never even referenced from
21269          within this entire compilation unit.  We suppress these DIEs in
21270          order to save space in the .debug section (by eliminating entries
21271          which are probably useless).  Note that we must not suppress
21272          block-local extern declarations (whether used or not) because that
21273          would screw-up the debugger's name lookup mechanism and cause it to
21274          miss things which really ought to be in scope at a given point.  */
21275       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
21276         return;
21277
21278       /* For local statics lookup proper context die.  */
21279       if (TREE_STATIC (decl)
21280           && DECL_CONTEXT (decl)
21281           && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
21282         context_die = lookup_decl_die (DECL_CONTEXT (decl));
21283
21284       /* If we are in terse mode, don't generate any DIEs to represent any
21285          variable declarations or definitions.  */
21286       if (debug_info_level <= DINFO_LEVEL_TERSE)
21287         return;
21288       break;
21289
21290     case CONST_DECL:
21291       if (debug_info_level <= DINFO_LEVEL_TERSE)
21292         return;
21293       if (!is_fortran () && !is_ada ())
21294         return;
21295       if (TREE_STATIC (decl) && decl_function_context (decl))
21296         context_die = lookup_decl_die (DECL_CONTEXT (decl));
21297       break;
21298
21299     case NAMESPACE_DECL:
21300     case IMPORTED_DECL:
21301       if (debug_info_level <= DINFO_LEVEL_TERSE)
21302         return;
21303       if (lookup_decl_die (decl) != NULL)
21304         return;
21305       break;
21306
21307     case TYPE_DECL:
21308       /* Don't emit stubs for types unless they are needed by other DIEs.  */
21309       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
21310         return;
21311
21312       /* Don't bother trying to generate any DIEs to represent any of the
21313          normal built-in types for the language we are compiling.  */
21314       if (DECL_IS_BUILTIN (decl))
21315         return;
21316
21317       /* If we are in terse mode, don't generate any DIEs for types.  */
21318       if (debug_info_level <= DINFO_LEVEL_TERSE)
21319         return;
21320
21321       /* If we're a function-scope tag, initially use a parent of NULL;
21322          this will be fixed up in decls_for_scope.  */
21323       if (decl_function_context (decl))
21324         context_die = NULL;
21325
21326       break;
21327
21328     case NAMELIST_DECL:
21329       break;
21330
21331     default:
21332       return;
21333     }
21334
21335   gen_decl_die (decl, NULL, context_die);
21336 }
21337
21338 /* Write the debugging output for DECL.  */
21339
21340 static void
21341 dwarf2out_function_decl (tree decl)
21342 {
21343   dwarf2out_decl (decl);
21344   call_arg_locations = NULL;
21345   call_arg_loc_last = NULL;
21346   call_site_count = -1;
21347   tail_call_site_count = -1;
21348   block_map.release ();
21349   decl_loc_table->empty ();
21350   cached_dw_loc_list_table->empty ();
21351 }
21352
21353 /* Output a marker (i.e. a label) for the beginning of the generated code for
21354    a lexical block.  */
21355
21356 static void
21357 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21358                        unsigned int blocknum)
21359 {
21360   switch_to_section (current_function_section ());
21361   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
21362 }
21363
21364 /* Output a marker (i.e. a label) for the end of the generated code for a
21365    lexical block.  */
21366
21367 static void
21368 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
21369 {
21370   switch_to_section (current_function_section ());
21371   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
21372 }
21373
21374 /* Returns nonzero if it is appropriate not to emit any debugging
21375    information for BLOCK, because it doesn't contain any instructions.
21376
21377    Don't allow this for blocks with nested functions or local classes
21378    as we would end up with orphans, and in the presence of scheduling
21379    we may end up calling them anyway.  */
21380
21381 static bool
21382 dwarf2out_ignore_block (const_tree block)
21383 {
21384   tree decl;
21385   unsigned int i;
21386
21387   for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
21388     if (TREE_CODE (decl) == FUNCTION_DECL
21389         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21390       return 0;
21391   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
21392     {
21393       decl = BLOCK_NONLOCALIZED_VAR (block, i);
21394       if (TREE_CODE (decl) == FUNCTION_DECL
21395           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21396       return 0;
21397     }
21398
21399   return 1;
21400 }
21401
21402 /* Hash table routines for file_hash.  */
21403
21404 bool
21405 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
21406 {
21407   return filename_cmp (p1->filename, p2) == 0;
21408 }
21409
21410 hashval_t
21411 dwarf_file_hasher::hash (dwarf_file_data *p)
21412 {
21413   return htab_hash_string (p->filename);
21414 }
21415
21416 /* Lookup FILE_NAME (in the list of filenames that we know about here in
21417    dwarf2out.c) and return its "index".  The index of each (known) filename is
21418    just a unique number which is associated with only that one filename.  We
21419    need such numbers for the sake of generating labels (in the .debug_sfnames
21420    section) and references to those files numbers (in the .debug_srcinfo
21421    and.debug_macinfo sections).  If the filename given as an argument is not
21422    found in our current list, add it to the list and assign it the next
21423    available unique index number.  In order to speed up searches, we remember
21424    the index of the filename was looked up last.  This handles the majority of
21425    all searches.  */
21426
21427 static struct dwarf_file_data *
21428 lookup_filename (const char *file_name)
21429 {
21430   struct dwarf_file_data * created;
21431
21432   /* Check to see if the file name that was searched on the previous
21433      call matches this file name.  If so, return the index.  */
21434   if (file_table_last_lookup
21435       && (file_name == file_table_last_lookup->filename
21436           || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
21437     return file_table_last_lookup;
21438
21439   /* Didn't match the previous lookup, search the table.  */
21440   dwarf_file_data **slot
21441     = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
21442                                        INSERT);
21443   if (*slot)
21444     return *slot;
21445
21446   created = ggc_alloc<dwarf_file_data> ();
21447   created->filename = file_name;
21448   created->emitted_number = 0;
21449   *slot = created;
21450   return created;
21451 }
21452
21453 /* If the assembler will construct the file table, then translate the compiler
21454    internal file table number into the assembler file table number, and emit
21455    a .file directive if we haven't already emitted one yet.  The file table
21456    numbers are different because we prune debug info for unused variables and
21457    types, which may include filenames.  */
21458
21459 static int
21460 maybe_emit_file (struct dwarf_file_data * fd)
21461 {
21462   if (! fd->emitted_number)
21463     {
21464       if (last_emitted_file)
21465         fd->emitted_number = last_emitted_file->emitted_number + 1;
21466       else
21467         fd->emitted_number = 1;
21468       last_emitted_file = fd;
21469
21470       if (DWARF2_ASM_LINE_DEBUG_INFO)
21471         {
21472           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
21473           output_quoted_string (asm_out_file,
21474                                 remap_debug_filename (fd->filename));
21475           fputc ('\n', asm_out_file);
21476         }
21477     }
21478
21479   return fd->emitted_number;
21480 }
21481
21482 /* Schedule generation of a DW_AT_const_value attribute to DIE.
21483    That generation should happen after function debug info has been
21484    generated. The value of the attribute is the constant value of ARG.  */
21485
21486 static void
21487 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
21488 {
21489   die_arg_entry entry;
21490
21491   if (!die || !arg)
21492     return;
21493
21494   if (!tmpl_value_parm_die_table)
21495     vec_alloc (tmpl_value_parm_die_table, 32);
21496
21497   entry.die = die;
21498   entry.arg = arg;
21499   vec_safe_push (tmpl_value_parm_die_table, entry);
21500 }
21501
21502 /* Return TRUE if T is an instance of generic type, FALSE
21503    otherwise.  */
21504
21505 static bool
21506 generic_type_p (tree t)
21507 {
21508   if (t == NULL_TREE || !TYPE_P (t))
21509     return false;
21510   return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
21511 }
21512
21513 /* Schedule the generation of the generic parameter dies for the
21514   instance of generic type T. The proper generation itself is later
21515   done by gen_scheduled_generic_parms_dies. */
21516
21517 static void
21518 schedule_generic_params_dies_gen (tree t)
21519 {
21520   if (!generic_type_p (t))
21521     return;
21522
21523   if (!generic_type_instances)
21524     vec_alloc (generic_type_instances, 256);
21525
21526   vec_safe_push (generic_type_instances, t);
21527 }
21528
21529 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21530    by append_entry_to_tmpl_value_parm_die_table. This function must
21531    be called after function DIEs have been generated.  */
21532
21533 static void
21534 gen_remaining_tmpl_value_param_die_attribute (void)
21535 {
21536   if (tmpl_value_parm_die_table)
21537     {
21538       unsigned i;
21539       die_arg_entry *e;
21540
21541       FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
21542         tree_add_const_value_attribute (e->die, e->arg);
21543     }
21544 }
21545
21546 /* Generate generic parameters DIEs for instances of generic types
21547    that have been previously scheduled by
21548    schedule_generic_params_dies_gen. This function must be called
21549    after all the types of the CU have been laid out.  */
21550
21551 static void
21552 gen_scheduled_generic_parms_dies (void)
21553 {
21554   unsigned i;
21555   tree t;
21556
21557   if (!generic_type_instances)
21558     return;
21559   
21560   FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
21561     if (COMPLETE_TYPE_P (t))
21562       gen_generic_params_dies (t);
21563 }
21564
21565
21566 /* Replace DW_AT_name for the decl with name.  */
21567
21568 static void
21569 dwarf2out_set_name (tree decl, tree name)
21570 {
21571   dw_die_ref die;
21572   dw_attr_ref attr;
21573   const char *dname;
21574
21575   die = TYPE_SYMTAB_DIE (decl);
21576   if (!die)
21577     return;
21578
21579   dname = dwarf2_name (name, 0);
21580   if (!dname)
21581     return;
21582
21583   attr = get_AT (die, DW_AT_name);
21584   if (attr)
21585     {
21586       struct indirect_string_node *node;
21587
21588       node = find_AT_string (dname);
21589       /* replace the string.  */
21590       attr->dw_attr_val.v.val_str = node;
21591     }
21592
21593   else
21594     add_name_attribute (die, dname);
21595 }
21596
21597 /* True if before or during processing of the first function being emitted.  */
21598 static bool in_first_function_p = true;
21599 /* True if loc_note during dwarf2out_var_location call might still be
21600    before first real instruction at address equal to .Ltext0.  */
21601 static bool maybe_at_text_label_p = true;
21602 /* One above highest N where .LVLN label might be equal to .Ltext0 label.  */
21603 static unsigned int first_loclabel_num_not_at_text_label;
21604
21605 /* Called by the final INSN scan whenever we see a var location.  We
21606    use it to drop labels in the right places, and throw the location in
21607    our lookup table.  */
21608
21609 static void
21610 dwarf2out_var_location (rtx_insn *loc_note)
21611 {
21612   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21613   struct var_loc_node *newloc;
21614   rtx_insn *next_real, *next_note;
21615   static const char *last_label;
21616   static const char *last_postcall_label;
21617   static bool last_in_cold_section_p;
21618   static rtx_insn *expected_next_loc_note;
21619   tree decl;
21620   bool var_loc_p;
21621
21622   if (!NOTE_P (loc_note))
21623     {
21624       if (CALL_P (loc_note))
21625         {
21626           call_site_count++;
21627           if (SIBLING_CALL_P (loc_note))
21628             tail_call_site_count++;
21629         }
21630       return;
21631     }
21632
21633   var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
21634   if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21635     return;
21636
21637   /* Optimize processing a large consecutive sequence of location
21638      notes so we don't spend too much time in next_real_insn.  If the
21639      next insn is another location note, remember the next_real_insn
21640      calculation for next time.  */
21641   next_real = cached_next_real_insn;
21642   if (next_real)
21643     {
21644       if (expected_next_loc_note != loc_note)
21645         next_real = NULL;
21646     }
21647
21648   next_note = NEXT_INSN (loc_note);
21649   if (! next_note
21650       || next_note->deleted ()
21651       || ! NOTE_P (next_note)
21652       || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
21653           && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
21654     next_note = NULL;
21655
21656   if (! next_real)
21657     next_real = next_real_insn (loc_note);
21658
21659   if (next_note)
21660     {
21661       expected_next_loc_note = next_note;
21662       cached_next_real_insn = next_real;
21663     }
21664   else
21665     cached_next_real_insn = NULL;
21666
21667   /* If there are no instructions which would be affected by this note,
21668      don't do anything.  */
21669   if (var_loc_p
21670       && next_real == NULL_RTX
21671       && !NOTE_DURING_CALL_P (loc_note))
21672     return;
21673
21674   if (next_real == NULL_RTX)
21675     next_real = get_last_insn ();
21676
21677   /* If there were any real insns between note we processed last time
21678      and this note (or if it is the first note), clear
21679      last_{,postcall_}label so that they are not reused this time.  */
21680   if (last_var_location_insn == NULL_RTX
21681       || last_var_location_insn != next_real
21682       || last_in_cold_section_p != in_cold_section_p)
21683     {
21684       last_label = NULL;
21685       last_postcall_label = NULL;
21686     }
21687
21688   if (var_loc_p)
21689     {
21690       decl = NOTE_VAR_LOCATION_DECL (loc_note);
21691       newloc = add_var_loc_to_decl (decl, loc_note,
21692                                     NOTE_DURING_CALL_P (loc_note)
21693                                     ? last_postcall_label : last_label);
21694       if (newloc == NULL)
21695         return;
21696     }
21697   else
21698     {
21699       decl = NULL_TREE;
21700       newloc = NULL;
21701     }
21702
21703   /* If there were no real insns between note we processed last time
21704      and this note, use the label we emitted last time.  Otherwise
21705      create a new label and emit it.  */
21706   if (last_label == NULL)
21707     {
21708       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21709       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21710       loclabel_num++;
21711       last_label = ggc_strdup (loclabel);
21712       /* See if loclabel might be equal to .Ltext0.  If yes,
21713          bump first_loclabel_num_not_at_text_label.  */
21714       if (!have_multiple_function_sections
21715           && in_first_function_p
21716           && maybe_at_text_label_p)
21717         {
21718           static rtx_insn *last_start;
21719           rtx_insn *insn;
21720           for (insn = loc_note; insn; insn = previous_insn (insn))
21721             if (insn == last_start)
21722               break;
21723             else if (!NONDEBUG_INSN_P (insn))
21724               continue;
21725             else
21726               {
21727                 rtx body = PATTERN (insn);
21728                 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
21729                   continue;
21730                 /* Inline asm could occupy zero bytes.  */
21731                 else if (GET_CODE (body) == ASM_INPUT
21732                          || asm_noperands (body) >= 0)
21733                   continue;
21734 #ifdef HAVE_attr_length
21735                 else if (get_attr_min_length (insn) == 0)
21736                   continue;
21737 #endif
21738                 else
21739                   {
21740                     /* Assume insn has non-zero length.  */
21741                     maybe_at_text_label_p = false;
21742                     break;
21743                   }
21744               }
21745           if (maybe_at_text_label_p)
21746             {
21747               last_start = loc_note;
21748               first_loclabel_num_not_at_text_label = loclabel_num;
21749             }
21750         }
21751     }
21752
21753   if (!var_loc_p)
21754     {
21755       struct call_arg_loc_node *ca_loc
21756         = ggc_cleared_alloc<call_arg_loc_node> ();
21757       rtx_insn *prev = prev_real_insn (loc_note);
21758       rtx x;
21759       ca_loc->call_arg_loc_note = loc_note;
21760       ca_loc->next = NULL;
21761       ca_loc->label = last_label;
21762       gcc_assert (prev
21763                   && (CALL_P (prev)
21764                       || (NONJUMP_INSN_P (prev)
21765                           && GET_CODE (PATTERN (prev)) == SEQUENCE
21766                           && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
21767       if (!CALL_P (prev))
21768         prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
21769       ca_loc->tail_call_p = SIBLING_CALL_P (prev);
21770       x = get_call_rtx_from (PATTERN (prev));
21771       if (x)
21772         {
21773           x = XEXP (XEXP (x, 0), 0);
21774           if (GET_CODE (x) == SYMBOL_REF
21775               && SYMBOL_REF_DECL (x)
21776               && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
21777             ca_loc->symbol_ref = x;
21778         }
21779       ca_loc->block = insn_scope (prev);
21780       if (call_arg_locations)
21781         call_arg_loc_last->next = ca_loc;
21782       else
21783         call_arg_locations = ca_loc;
21784       call_arg_loc_last = ca_loc;
21785     }
21786   else if (!NOTE_DURING_CALL_P (loc_note))
21787     newloc->label = last_label;
21788   else
21789     {
21790       if (!last_postcall_label)
21791         {
21792           sprintf (loclabel, "%s-1", last_label);
21793           last_postcall_label = ggc_strdup (loclabel);
21794         }
21795       newloc->label = last_postcall_label;
21796     }
21797
21798   last_var_location_insn = next_real;
21799   last_in_cold_section_p = in_cold_section_p;
21800 }
21801
21802 /* Note in one location list that text section has changed.  */
21803
21804 int
21805 var_location_switch_text_section_1 (var_loc_list **slot, void *)
21806 {
21807   var_loc_list *list = *slot;
21808   if (list->first)
21809     list->last_before_switch
21810       = list->last->next ? list->last->next : list->last;
21811   return 1;
21812 }
21813
21814 /* Note in all location lists that text section has changed.  */
21815
21816 static void
21817 var_location_switch_text_section (void)
21818 {
21819   if (decl_loc_table == NULL)
21820     return;
21821
21822   decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
21823 }
21824
21825 /* Create a new line number table.  */
21826
21827 static dw_line_info_table *
21828 new_line_info_table (void)
21829 {
21830   dw_line_info_table *table;
21831
21832   table = ggc_cleared_alloc<dw_line_info_table_struct> ();
21833   table->file_num = 1;
21834   table->line_num = 1;
21835   table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
21836
21837   return table;
21838 }
21839
21840 /* Lookup the "current" table into which we emit line info, so
21841    that we don't have to do it for every source line.  */
21842
21843 static void
21844 set_cur_line_info_table (section *sec)
21845 {
21846   dw_line_info_table *table;
21847
21848   if (sec == text_section)
21849     table = text_section_line_info;
21850   else if (sec == cold_text_section)
21851     {
21852       table = cold_text_section_line_info;
21853       if (!table)
21854         {
21855           cold_text_section_line_info = table = new_line_info_table ();
21856           table->end_label = cold_end_label;
21857         }
21858     }
21859   else
21860     {
21861       const char *end_label;
21862
21863       if (flag_reorder_blocks_and_partition)
21864         {
21865           if (in_cold_section_p)
21866             end_label = crtl->subsections.cold_section_end_label;
21867           else
21868             end_label = crtl->subsections.hot_section_end_label;
21869         }
21870       else
21871         {
21872           char label[MAX_ARTIFICIAL_LABEL_BYTES];
21873           ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
21874                                        current_function_funcdef_no);
21875           end_label = ggc_strdup (label);
21876         }
21877
21878       table = new_line_info_table ();
21879       table->end_label = end_label;
21880
21881       vec_safe_push (separate_line_info, table);
21882     }
21883
21884   if (DWARF2_ASM_LINE_DEBUG_INFO)
21885     table->is_stmt = (cur_line_info_table
21886                       ? cur_line_info_table->is_stmt
21887                       : DWARF_LINE_DEFAULT_IS_STMT_START);
21888   cur_line_info_table = table;
21889 }
21890
21891
21892 /* We need to reset the locations at the beginning of each
21893    function. We can't do this in the end_function hook, because the
21894    declarations that use the locations won't have been output when
21895    that hook is called.  Also compute have_multiple_function_sections here.  */
21896
21897 static void
21898 dwarf2out_begin_function (tree fun)
21899 {
21900   section *sec = function_section (fun);
21901
21902   if (sec != text_section)
21903     have_multiple_function_sections = true;
21904
21905   if (flag_reorder_blocks_and_partition && !cold_text_section)
21906     {
21907       gcc_assert (current_function_decl == fun);
21908       cold_text_section = unlikely_text_section ();
21909       switch_to_section (cold_text_section);
21910       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21911       switch_to_section (sec);
21912     }
21913
21914   dwarf2out_note_section_used ();
21915   call_site_count = 0;
21916   tail_call_site_count = 0;
21917
21918   set_cur_line_info_table (sec);
21919 }
21920
21921 /* Helper function of dwarf2out_end_function, called only after emitting
21922    the very first function into assembly.  Check if some .debug_loc range
21923    might end with a .LVL* label that could be equal to .Ltext0.
21924    In that case we must force using absolute addresses in .debug_loc ranges,
21925    because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
21926    .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
21927    list terminator.
21928    Set have_multiple_function_sections to true in that case and
21929    terminate htab traversal.  */
21930
21931 int
21932 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
21933 {
21934   var_loc_list *entry = *slot;
21935   struct var_loc_node *node;
21936
21937   node = entry->first;
21938   if (node && node->next && node->next->label)
21939     {
21940       unsigned int i;
21941       const char *label = node->next->label;
21942       char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
21943
21944       for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
21945         {
21946           ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
21947           if (strcmp (label, loclabel) == 0)
21948             {
21949               have_multiple_function_sections = true;
21950               return 0;
21951             }
21952         }
21953     }
21954   return 1;
21955 }
21956
21957 /* Hook called after emitting a function into assembly.
21958    This does something only for the very first function emitted.  */
21959
21960 static void
21961 dwarf2out_end_function (unsigned int)
21962 {
21963   if (in_first_function_p
21964       && !have_multiple_function_sections
21965       && first_loclabel_num_not_at_text_label
21966       && decl_loc_table)
21967     decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
21968   in_first_function_p = false;
21969   maybe_at_text_label_p = false;
21970 }
21971
21972 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE.  */
21973
21974 static void
21975 push_dw_line_info_entry (dw_line_info_table *table,
21976                          enum dw_line_info_opcode opcode, unsigned int val)
21977 {
21978   dw_line_info_entry e;
21979   e.opcode = opcode;
21980   e.val = val;
21981   vec_safe_push (table->entries, e);
21982 }
21983
21984 /* Output a label to mark the beginning of a source code line entry
21985    and record information relating to this source line, in
21986    'line_info_table' for later output of the .debug_line section.  */
21987 /* ??? The discriminator parameter ought to be unsigned.  */
21988
21989 static void
21990 dwarf2out_source_line (unsigned int line, const char *filename,
21991                        int discriminator, bool is_stmt)
21992 {
21993   unsigned int file_num;
21994   dw_line_info_table *table;
21995
21996   if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
21997     return;
21998
21999   /* The discriminator column was added in dwarf4.  Simplify the below
22000      by simply removing it if we're not supposed to output it.  */
22001   if (dwarf_version < 4 && dwarf_strict)
22002     discriminator = 0;
22003
22004   table = cur_line_info_table;
22005   file_num = maybe_emit_file (lookup_filename (filename));
22006
22007   /* ??? TODO: Elide duplicate line number entries.  Traditionally,
22008      the debugger has used the second (possibly duplicate) line number
22009      at the beginning of the function to mark the end of the prologue.
22010      We could eliminate any other duplicates within the function.  For
22011      Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
22012      that second line number entry.  */
22013   /* Recall that this end-of-prologue indication is *not* the same thing
22014      as the end_prologue debug hook.  The NOTE_INSN_PROLOGUE_END note,
22015      to which the hook corresponds, follows the last insn that was 
22016      emitted by gen_prologue.  What we need is to precede the first insn
22017      that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
22018      insn that corresponds to something the user wrote.  These may be
22019      very different locations once scheduling is enabled.  */
22020
22021   if (0 && file_num == table->file_num
22022       && line == table->line_num
22023       && discriminator == table->discrim_num
22024       && is_stmt == table->is_stmt)
22025     return;
22026
22027   switch_to_section (current_function_section ());
22028
22029   /* If requested, emit something human-readable.  */
22030   if (flag_debug_asm)
22031     fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
22032
22033   if (DWARF2_ASM_LINE_DEBUG_INFO)
22034     {
22035       /* Emit the .loc directive understood by GNU as.  */
22036       /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
22037          file_num, line, is_stmt, discriminator */
22038       fputs ("\t.loc ", asm_out_file);
22039       fprint_ul (asm_out_file, file_num);
22040       putc (' ', asm_out_file);
22041       fprint_ul (asm_out_file, line);
22042       putc (' ', asm_out_file);
22043       putc ('0', asm_out_file);
22044
22045       if (is_stmt != table->is_stmt)
22046         {
22047           fputs (" is_stmt ", asm_out_file);
22048           putc (is_stmt ? '1' : '0', asm_out_file);
22049         }
22050       if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
22051         {
22052           gcc_assert (discriminator > 0);
22053           fputs (" discriminator ", asm_out_file);
22054           fprint_ul (asm_out_file, (unsigned long) discriminator);
22055         }
22056       putc ('\n', asm_out_file);
22057     }
22058   else
22059     {
22060       unsigned int label_num = ++line_info_label_num;
22061
22062       targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
22063
22064       push_dw_line_info_entry (table, LI_set_address, label_num);
22065       if (file_num != table->file_num)
22066         push_dw_line_info_entry (table, LI_set_file, file_num);
22067       if (discriminator != table->discrim_num)
22068         push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
22069       if (is_stmt != table->is_stmt)
22070         push_dw_line_info_entry (table, LI_negate_stmt, 0);
22071       push_dw_line_info_entry (table, LI_set_line, line);
22072     }
22073
22074   table->file_num = file_num;
22075   table->line_num = line;
22076   table->discrim_num = discriminator;
22077   table->is_stmt = is_stmt;
22078   table->in_use = true;
22079 }
22080
22081 /* Record the beginning of a new source file.  */
22082
22083 static void
22084 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
22085 {
22086   if (flag_eliminate_dwarf2_dups)
22087     {
22088       /* Record the beginning of the file for break_out_includes.  */
22089       dw_die_ref bincl_die;
22090
22091       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
22092       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
22093     }
22094
22095   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22096     {
22097       macinfo_entry e;
22098       e.code = DW_MACINFO_start_file;
22099       e.lineno = lineno;
22100       e.info = ggc_strdup (filename);
22101       vec_safe_push (macinfo_table, e);
22102     }
22103 }
22104
22105 /* Record the end of a source file.  */
22106
22107 static void
22108 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
22109 {
22110   if (flag_eliminate_dwarf2_dups)
22111     /* Record the end of the file for break_out_includes.  */
22112     new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
22113
22114   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22115     {
22116       macinfo_entry e;
22117       e.code = DW_MACINFO_end_file;
22118       e.lineno = lineno;
22119       e.info = NULL;
22120       vec_safe_push (macinfo_table, e);
22121     }
22122 }
22123
22124 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
22125    the tail part of the directive line, i.e. the part which is past the
22126    initial whitespace, #, whitespace, directive-name, whitespace part.  */
22127
22128 static void
22129 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
22130                   const char *buffer ATTRIBUTE_UNUSED)
22131 {
22132   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22133     {
22134       macinfo_entry e;
22135       /* Insert a dummy first entry to be able to optimize the whole
22136          predefined macro block using DW_MACRO_GNU_transparent_include.  */
22137       if (macinfo_table->is_empty () && lineno <= 1)
22138         {
22139           e.code = 0;
22140           e.lineno = 0;
22141           e.info = NULL;
22142           vec_safe_push (macinfo_table, e);
22143         }
22144       e.code = DW_MACINFO_define;
22145       e.lineno = lineno;
22146       e.info = ggc_strdup (buffer);
22147       vec_safe_push (macinfo_table, e);
22148     }
22149 }
22150
22151 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
22152    the tail part of the directive line, i.e. the part which is past the
22153    initial whitespace, #, whitespace, directive-name, whitespace part.  */
22154
22155 static void
22156 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
22157                  const char *buffer ATTRIBUTE_UNUSED)
22158 {
22159   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22160     {
22161       macinfo_entry e;
22162       /* Insert a dummy first entry to be able to optimize the whole
22163          predefined macro block using DW_MACRO_GNU_transparent_include.  */
22164       if (macinfo_table->is_empty () && lineno <= 1)
22165         {
22166           e.code = 0;
22167           e.lineno = 0;
22168           e.info = NULL;
22169           vec_safe_push (macinfo_table, e);
22170         }
22171       e.code = DW_MACINFO_undef;
22172       e.lineno = lineno;
22173       e.info = ggc_strdup (buffer);
22174       vec_safe_push (macinfo_table, e);
22175     }
22176 }
22177
22178 /* Helpers to manipulate hash table of CUs.  */
22179
22180 struct macinfo_entry_hasher : typed_noop_remove <macinfo_entry>
22181 {
22182   typedef macinfo_entry value_type;
22183   typedef macinfo_entry compare_type;
22184   static inline hashval_t hash (const value_type *);
22185   static inline bool equal (const value_type *, const compare_type *);
22186 };
22187
22188 inline hashval_t
22189 macinfo_entry_hasher::hash (const value_type *entry)
22190 {
22191   return htab_hash_string (entry->info);
22192 }
22193
22194 inline bool
22195 macinfo_entry_hasher::equal (const value_type *entry1,
22196                              const compare_type *entry2)
22197 {
22198   return !strcmp (entry1->info, entry2->info);
22199 }
22200
22201 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
22202
22203 /* Output a single .debug_macinfo entry.  */
22204
22205 static void
22206 output_macinfo_op (macinfo_entry *ref)
22207 {
22208   int file_num;
22209   size_t len;
22210   struct indirect_string_node *node;
22211   char label[MAX_ARTIFICIAL_LABEL_BYTES];
22212   struct dwarf_file_data *fd;
22213
22214   switch (ref->code)
22215     {
22216     case DW_MACINFO_start_file:
22217       fd = lookup_filename (ref->info);
22218       file_num = maybe_emit_file (fd);
22219       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
22220       dw2_asm_output_data_uleb128 (ref->lineno,
22221                                    "Included from line number %lu", 
22222                                    (unsigned long) ref->lineno);
22223       dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
22224       break;
22225     case DW_MACINFO_end_file:
22226       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
22227       break;
22228     case DW_MACINFO_define:
22229     case DW_MACINFO_undef:
22230       len = strlen (ref->info) + 1;
22231       if (!dwarf_strict
22232           && len > DWARF_OFFSET_SIZE
22233           && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
22234           && (debug_str_section->common.flags & SECTION_MERGE) != 0)
22235         {
22236           ref->code = ref->code == DW_MACINFO_define
22237                       ? DW_MACRO_GNU_define_indirect
22238                       : DW_MACRO_GNU_undef_indirect;
22239           output_macinfo_op (ref);
22240           return;
22241         }
22242       dw2_asm_output_data (1, ref->code,
22243                            ref->code == DW_MACINFO_define
22244                            ? "Define macro" : "Undefine macro");
22245       dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu", 
22246                                    (unsigned long) ref->lineno);
22247       dw2_asm_output_nstring (ref->info, -1, "The macro");
22248       break;
22249     case DW_MACRO_GNU_define_indirect:
22250     case DW_MACRO_GNU_undef_indirect:
22251       node = find_AT_string (ref->info);
22252       gcc_assert (node
22253                   && ((node->form == DW_FORM_strp)
22254                       || (node->form == DW_FORM_GNU_str_index)));
22255       dw2_asm_output_data (1, ref->code,
22256                            ref->code == DW_MACRO_GNU_define_indirect
22257                            ? "Define macro indirect"
22258                            : "Undefine macro indirect");
22259       dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22260                                    (unsigned long) ref->lineno);
22261       if (node->form == DW_FORM_strp)
22262         dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
22263                                debug_str_section, "The macro: \"%s\"",
22264                                ref->info);
22265       else
22266         dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
22267                                      ref->info);
22268       break;
22269     case DW_MACRO_GNU_transparent_include:
22270       dw2_asm_output_data (1, ref->code, "Transparent include");
22271       ASM_GENERATE_INTERNAL_LABEL (label,
22272                                    DEBUG_MACRO_SECTION_LABEL, ref->lineno);
22273       dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
22274       break;
22275     default:
22276       fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
22277                ASM_COMMENT_START, (unsigned long) ref->code);
22278       break;
22279     }
22280 }
22281
22282 /* Attempt to make a sequence of define/undef macinfo ops shareable with
22283    other compilation unit .debug_macinfo sections.  IDX is the first
22284    index of a define/undef, return the number of ops that should be
22285    emitted in a comdat .debug_macinfo section and emit
22286    a DW_MACRO_GNU_transparent_include entry referencing it.
22287    If the define/undef entry should be emitted normally, return 0.  */
22288
22289 static unsigned
22290 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
22291                         macinfo_hash_type **macinfo_htab)
22292 {
22293   macinfo_entry *first, *second, *cur, *inc;
22294   char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
22295   unsigned char checksum[16];
22296   struct md5_ctx ctx;
22297   char *grp_name, *tail;
22298   const char *base;
22299   unsigned int i, count, encoded_filename_len, linebuf_len;
22300   macinfo_entry **slot;
22301
22302   first = &(*macinfo_table)[idx];
22303   second = &(*macinfo_table)[idx + 1];
22304
22305   /* Optimize only if there are at least two consecutive define/undef ops,
22306      and either all of them are before first DW_MACINFO_start_file
22307      with lineno {0,1} (i.e. predefined macro block), or all of them are
22308      in some included header file.  */
22309   if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
22310     return 0;
22311   if (vec_safe_is_empty (files))
22312     {
22313       if (first->lineno > 1 || second->lineno > 1)
22314         return 0;
22315     }
22316   else if (first->lineno == 0)
22317     return 0;
22318
22319   /* Find the last define/undef entry that can be grouped together
22320      with first and at the same time compute md5 checksum of their
22321      codes, linenumbers and strings.  */
22322   md5_init_ctx (&ctx);
22323   for (i = idx; macinfo_table->iterate (i, &cur); i++)
22324     if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
22325       break;
22326     else if (vec_safe_is_empty (files) && cur->lineno > 1)
22327       break;
22328     else
22329       {
22330         unsigned char code = cur->code;
22331         md5_process_bytes (&code, 1, &ctx);
22332         checksum_uleb128 (cur->lineno, &ctx);
22333         md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
22334       }
22335   md5_finish_ctx (&ctx, checksum);
22336   count = i - idx;
22337
22338   /* From the containing include filename (if any) pick up just
22339      usable characters from its basename.  */
22340   if (vec_safe_is_empty (files))
22341     base = "";
22342   else
22343     base = lbasename (files->last ().info);
22344   for (encoded_filename_len = 0, i = 0; base[i]; i++)
22345     if (ISIDNUM (base[i]) || base[i] == '.')
22346       encoded_filename_len++;
22347   /* Count . at the end.  */
22348   if (encoded_filename_len)
22349     encoded_filename_len++;
22350
22351   sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
22352   linebuf_len = strlen (linebuf);
22353
22354   /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum>  */
22355   grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
22356                          + 16 * 2 + 1);
22357   memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
22358   tail = grp_name + 4;
22359   if (encoded_filename_len)
22360     {
22361       for (i = 0; base[i]; i++)
22362         if (ISIDNUM (base[i]) || base[i] == '.')
22363           *tail++ = base[i];
22364       *tail++ = '.';
22365     }
22366   memcpy (tail, linebuf, linebuf_len);
22367   tail += linebuf_len;
22368   *tail++ = '.';
22369   for (i = 0; i < 16; i++)
22370     sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
22371
22372   /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
22373      in the empty vector entry before the first define/undef.  */
22374   inc = &(*macinfo_table)[idx - 1];
22375   inc->code = DW_MACRO_GNU_transparent_include;
22376   inc->lineno = 0;
22377   inc->info = ggc_strdup (grp_name);
22378   if (!*macinfo_htab)
22379     *macinfo_htab = new macinfo_hash_type (10);
22380   /* Avoid emitting duplicates.  */
22381   slot = (*macinfo_htab)->find_slot (inc, INSERT);
22382   if (*slot != NULL)
22383     {
22384       inc->code = 0;
22385       inc->info = NULL;
22386       /* If such an entry has been used before, just emit
22387          a DW_MACRO_GNU_transparent_include op.  */
22388       inc = *slot;
22389       output_macinfo_op (inc);
22390       /* And clear all macinfo_entry in the range to avoid emitting them
22391          in the second pass.  */
22392       for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
22393         {
22394           cur->code = 0;
22395           cur->info = NULL;
22396         }
22397     }
22398   else
22399     {
22400       *slot = inc;
22401       inc->lineno = (*macinfo_htab)->elements ();
22402       output_macinfo_op (inc);
22403     }
22404   return count;
22405 }
22406
22407 /* Save any strings needed by the macinfo table in the debug str
22408    table.  All strings must be collected into the table by the time
22409    index_string is called.  */
22410
22411 static void
22412 save_macinfo_strings (void)
22413 {
22414   unsigned len;
22415   unsigned i;
22416   macinfo_entry *ref;
22417
22418   for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
22419     {
22420       switch (ref->code)
22421         {
22422           /* Match the logic in output_macinfo_op to decide on
22423              indirect strings.  */
22424           case DW_MACINFO_define:
22425           case DW_MACINFO_undef:
22426             len = strlen (ref->info) + 1;
22427             if (!dwarf_strict
22428                 && len > DWARF_OFFSET_SIZE
22429                 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
22430                 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
22431               set_indirect_string (find_AT_string (ref->info));
22432             break;
22433           case DW_MACRO_GNU_define_indirect:
22434           case DW_MACRO_GNU_undef_indirect:
22435             set_indirect_string (find_AT_string (ref->info));
22436             break;
22437           default:
22438             break;
22439         }
22440     }
22441 }
22442
22443 /* Output macinfo section(s).  */
22444
22445 static void
22446 output_macinfo (void)
22447 {
22448   unsigned i;
22449   unsigned long length = vec_safe_length (macinfo_table);
22450   macinfo_entry *ref;
22451   vec<macinfo_entry, va_gc> *files = NULL;
22452   macinfo_hash_type *macinfo_htab = NULL;
22453
22454   if (! length)
22455     return;
22456
22457   /* output_macinfo* uses these interchangeably.  */
22458   gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
22459               && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
22460               && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
22461               && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
22462
22463   /* For .debug_macro emit the section header.  */
22464   if (!dwarf_strict)
22465     {
22466       dw2_asm_output_data (2, 4, "DWARF macro version number");
22467       if (DWARF_OFFSET_SIZE == 8)
22468         dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
22469       else
22470         dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
22471       dw2_asm_output_offset (DWARF_OFFSET_SIZE,
22472                              (!dwarf_split_debug_info ? debug_line_section_label
22473                               : debug_skeleton_line_section_label),
22474                              debug_line_section, NULL);
22475     }
22476
22477   /* In the first loop, it emits the primary .debug_macinfo section
22478      and after each emitted op the macinfo_entry is cleared.
22479      If a longer range of define/undef ops can be optimized using
22480      DW_MACRO_GNU_transparent_include, the
22481      DW_MACRO_GNU_transparent_include op is emitted and kept in
22482      the vector before the first define/undef in the range and the
22483      whole range of define/undef ops is not emitted and kept.  */
22484   for (i = 0; macinfo_table->iterate (i, &ref); i++)
22485     {
22486       switch (ref->code)
22487         {
22488         case DW_MACINFO_start_file:
22489           vec_safe_push (files, *ref);
22490           break;
22491         case DW_MACINFO_end_file:
22492           if (!vec_safe_is_empty (files))
22493             files->pop ();
22494           break;
22495         case DW_MACINFO_define:
22496         case DW_MACINFO_undef:
22497           if (!dwarf_strict
22498               && HAVE_COMDAT_GROUP
22499               && vec_safe_length (files) != 1
22500               && i > 0
22501               && i + 1 < length
22502               && (*macinfo_table)[i - 1].code == 0)
22503             {
22504               unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
22505               if (count)
22506                 {
22507                   i += count - 1;
22508                   continue;
22509                 }
22510             }
22511           break;
22512         case 0:
22513           /* A dummy entry may be inserted at the beginning to be able
22514              to optimize the whole block of predefined macros.  */
22515           if (i == 0)
22516             continue;
22517         default:
22518           break;
22519         }
22520       output_macinfo_op (ref);
22521       ref->info = NULL;
22522       ref->code = 0;
22523     }
22524
22525   if (!macinfo_htab)
22526     return;
22527
22528   delete macinfo_htab;
22529   macinfo_htab = NULL;
22530
22531   /* If any DW_MACRO_GNU_transparent_include were used, on those
22532      DW_MACRO_GNU_transparent_include entries terminate the
22533      current chain and switch to a new comdat .debug_macinfo
22534      section and emit the define/undef entries within it.  */
22535   for (i = 0; macinfo_table->iterate (i, &ref); i++)
22536     switch (ref->code)
22537       {
22538       case 0:
22539         continue;
22540       case DW_MACRO_GNU_transparent_include:
22541         {
22542           char label[MAX_ARTIFICIAL_LABEL_BYTES];
22543           tree comdat_key = get_identifier (ref->info);
22544           /* Terminate the previous .debug_macinfo section.  */
22545           dw2_asm_output_data (1, 0, "End compilation unit");
22546           targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
22547                                          SECTION_DEBUG
22548                                          | SECTION_LINKONCE,
22549                                          comdat_key);
22550           ASM_GENERATE_INTERNAL_LABEL (label,
22551                                        DEBUG_MACRO_SECTION_LABEL,
22552                                        ref->lineno);
22553           ASM_OUTPUT_LABEL (asm_out_file, label);
22554           ref->code = 0;
22555           ref->info = NULL;
22556           dw2_asm_output_data (2, 4, "DWARF macro version number");
22557           if (DWARF_OFFSET_SIZE == 8)
22558             dw2_asm_output_data (1, 1, "Flags: 64-bit");
22559           else
22560             dw2_asm_output_data (1, 0, "Flags: 32-bit");
22561         }
22562         break;
22563       case DW_MACINFO_define:
22564       case DW_MACINFO_undef:
22565         output_macinfo_op (ref);
22566         ref->code = 0;
22567         ref->info = NULL;
22568         break;
22569       default:
22570         gcc_unreachable ();
22571       }
22572 }
22573
22574 /* Set up for Dwarf output at the start of compilation.  */
22575
22576 static void
22577 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
22578 {
22579   /* Allocate the file_table.  */
22580   file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
22581
22582   /* Allocate the decl_die_table.  */
22583   decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
22584
22585   /* Allocate the decl_loc_table.  */
22586   decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
22587
22588   /* Allocate the cached_dw_loc_list_table.  */
22589   cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
22590
22591   /* Allocate the initial hunk of the decl_scope_table.  */
22592   vec_alloc (decl_scope_table, 256);
22593
22594   /* Allocate the initial hunk of the abbrev_die_table.  */
22595   abbrev_die_table = ggc_cleared_vec_alloc<dw_die_ref>
22596     (ABBREV_DIE_TABLE_INCREMENT);
22597   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
22598   /* Zero-th entry is allocated, but unused.  */
22599   abbrev_die_table_in_use = 1;
22600
22601   /* Allocate the pubtypes and pubnames vectors.  */
22602   vec_alloc (pubname_table, 32);
22603   vec_alloc (pubtype_table, 32);
22604
22605   vec_alloc (incomplete_types, 64);
22606
22607   vec_alloc (used_rtx_array, 32);
22608
22609   if (!dwarf_split_debug_info)
22610     {
22611       debug_info_section = get_section (DEBUG_INFO_SECTION,
22612                                         SECTION_DEBUG, NULL);
22613       debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22614                                           SECTION_DEBUG, NULL);
22615       debug_loc_section = get_section (DEBUG_LOC_SECTION,
22616                                        SECTION_DEBUG, NULL);
22617     }
22618   else
22619     {
22620       debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
22621                                         SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22622       debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
22623                                           SECTION_DEBUG | SECTION_EXCLUDE,
22624                                           NULL);
22625       debug_addr_section = get_section (DEBUG_ADDR_SECTION,
22626                                         SECTION_DEBUG, NULL);
22627       debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
22628                                                  SECTION_DEBUG, NULL);
22629       debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22630                                                    SECTION_DEBUG, NULL);
22631       ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
22632                                   DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
22633
22634       /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
22635          the main .o, but the skeleton_line goes into the split off dwo.  */
22636       debug_skeleton_line_section
22637           = get_section (DEBUG_DWO_LINE_SECTION,
22638                          SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22639       ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
22640                                    DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
22641       debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
22642                                                SECTION_DEBUG | SECTION_EXCLUDE,
22643                                                NULL);
22644       ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
22645                                    DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
22646       debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
22647                                        SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22648       debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
22649                                            DEBUG_STR_DWO_SECTION_FLAGS, NULL);
22650     }
22651   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
22652                                        SECTION_DEBUG, NULL);
22653   debug_macinfo_section = get_section (dwarf_strict
22654                                        ? DEBUG_MACINFO_SECTION
22655                                        : DEBUG_MACRO_SECTION,
22656                                        DEBUG_MACRO_SECTION_FLAGS, NULL);
22657   debug_line_section = get_section (DEBUG_LINE_SECTION,
22658                                     SECTION_DEBUG, NULL);
22659   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
22660                                         SECTION_DEBUG, NULL);
22661   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
22662                                         SECTION_DEBUG, NULL);
22663   debug_str_section = get_section (DEBUG_STR_SECTION,
22664                                    DEBUG_STR_SECTION_FLAGS, NULL);
22665   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
22666                                       SECTION_DEBUG, NULL);
22667   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
22668                                      SECTION_DEBUG, NULL);
22669
22670   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
22671   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
22672                                DEBUG_ABBREV_SECTION_LABEL, 0);
22673   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
22674   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
22675                                COLD_TEXT_SECTION_LABEL, 0);
22676   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
22677
22678   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
22679                                DEBUG_INFO_SECTION_LABEL, 0);
22680   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
22681                                DEBUG_LINE_SECTION_LABEL, 0);
22682   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
22683                                DEBUG_RANGES_SECTION_LABEL, 0);
22684   ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
22685                                DEBUG_ADDR_SECTION_LABEL, 0);
22686   ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
22687                                dwarf_strict
22688                                ? DEBUG_MACINFO_SECTION_LABEL
22689                                : DEBUG_MACRO_SECTION_LABEL, 0);
22690   ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
22691
22692   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22693     vec_alloc (macinfo_table, 64);
22694
22695   switch_to_section (text_section);
22696   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
22697
22698   /* Make sure the line number table for .text always exists.  */
22699   text_section_line_info = new_line_info_table ();
22700   text_section_line_info->end_label = text_end_label;
22701 }
22702
22703 /* Called before compile () starts outputtting functions, variables
22704    and toplevel asms into assembly.  */
22705
22706 static void
22707 dwarf2out_assembly_start (void)
22708 {
22709   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
22710       && dwarf2out_do_cfi_asm ()
22711       && (!(flag_unwind_tables || flag_exceptions)
22712           || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
22713     fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
22714 }
22715
22716 /* A helper function for dwarf2out_finish called through
22717    htab_traverse.  Assign a string its index.  All strings must be
22718    collected into the table by the time index_string is called,
22719    because the indexing code relies on htab_traverse to traverse nodes
22720    in the same order for each run. */
22721
22722 int
22723 index_string (indirect_string_node **h, unsigned int *index)
22724 {
22725   indirect_string_node *node = *h;
22726
22727   find_string_form (node);
22728   if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22729     {
22730       gcc_assert (node->index == NO_INDEX_ASSIGNED);
22731       node->index = *index;
22732       *index += 1;
22733     }
22734   return 1;
22735 }
22736
22737 /* A helper function for output_indirect_strings called through
22738    htab_traverse.  Output the offset to a string and update the
22739    current offset.  */
22740
22741 int
22742 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
22743 {
22744   indirect_string_node *node = *h;
22745
22746   if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22747     {
22748       /* Assert that this node has been assigned an index.  */
22749       gcc_assert (node->index != NO_INDEX_ASSIGNED
22750                   && node->index != NOT_INDEXED);
22751       dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
22752                            "indexed string 0x%x: %s", node->index, node->str);
22753       *offset += strlen (node->str) + 1;
22754     }
22755   return 1;
22756 }
22757
22758 /* A helper function for dwarf2out_finish called through
22759    htab_traverse.  Output the indexed string.  */
22760
22761 int
22762 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
22763 {
22764   struct indirect_string_node *node = *h;
22765
22766   if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22767     {
22768       /* Assert that the strings are output in the same order as their
22769          indexes were assigned.  */
22770       gcc_assert (*cur_idx == node->index);
22771       assemble_string (node->str, strlen (node->str) + 1);
22772       *cur_idx += 1;
22773     }
22774   return 1;
22775 }
22776
22777 /* A helper function for dwarf2out_finish called through
22778    htab_traverse.  Emit one queued .debug_str string.  */
22779
22780 int
22781 output_indirect_string (indirect_string_node **h, void *)
22782 {
22783   struct indirect_string_node *node = *h;
22784
22785   node->form = find_string_form (node);
22786   if (node->form == DW_FORM_strp && node->refcount > 0)
22787     {
22788       ASM_OUTPUT_LABEL (asm_out_file, node->label);
22789       assemble_string (node->str, strlen (node->str) + 1);
22790     }
22791
22792   return 1;
22793 }
22794
22795 /* Output the indexed string table.  */
22796
22797 static void
22798 output_indirect_strings (void)
22799 {
22800   switch_to_section (debug_str_section);
22801   if (!dwarf_split_debug_info)
22802     debug_str_hash->traverse<void *, output_indirect_string> (NULL);
22803   else
22804     {
22805       unsigned int offset = 0;
22806       unsigned int cur_idx = 0;
22807
22808       skeleton_debug_str_hash->traverse<void *, output_indirect_string> (NULL);
22809
22810       switch_to_section (debug_str_offsets_section);
22811       debug_str_hash->traverse_noresize
22812         <unsigned int *, output_index_string_offset> (&offset);
22813       switch_to_section (debug_str_dwo_section);
22814       debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
22815         (&cur_idx);
22816     }
22817 }
22818
22819 /* Callback for htab_traverse to assign an index to an entry in the
22820    table, and to write that entry to the .debug_addr section.  */
22821
22822 int
22823 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
22824 {
22825   addr_table_entry *entry = *slot;
22826
22827   if (entry->refcount == 0)
22828     {
22829       gcc_assert (entry->index == NO_INDEX_ASSIGNED
22830                   || entry->index == NOT_INDEXED);
22831       return 1;
22832     }
22833
22834   gcc_assert (entry->index == *cur_index);
22835   (*cur_index)++;
22836
22837   switch (entry->kind)
22838     {
22839       case ate_kind_rtx:
22840         dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
22841                                  "0x%x", entry->index);
22842         break;
22843       case ate_kind_rtx_dtprel:
22844         gcc_assert (targetm.asm_out.output_dwarf_dtprel);
22845         targetm.asm_out.output_dwarf_dtprel (asm_out_file,
22846                                              DWARF2_ADDR_SIZE,
22847                                              entry->addr.rtl);
22848         fputc ('\n', asm_out_file);
22849         break;
22850       case ate_kind_label:
22851         dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
22852                                  "0x%x", entry->index);
22853         break;
22854       default:
22855         gcc_unreachable ();
22856     }
22857   return 1;
22858 }
22859
22860 /* Produce the .debug_addr section.  */
22861
22862 static void
22863 output_addr_table (void)
22864 {
22865   unsigned int index = 0;
22866   if (addr_index_table == NULL || addr_index_table->size () == 0)
22867     return;
22868
22869   switch_to_section (debug_addr_section);
22870   addr_index_table
22871     ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
22872 }
22873
22874 #if ENABLE_ASSERT_CHECKING
22875 /* Verify that all marks are clear.  */
22876
22877 static void
22878 verify_marks_clear (dw_die_ref die)
22879 {
22880   dw_die_ref c;
22881
22882   gcc_assert (! die->die_mark);
22883   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22884 }
22885 #endif /* ENABLE_ASSERT_CHECKING */
22886
22887 /* Clear the marks for a die and its children.
22888    Be cool if the mark isn't set.  */
22889
22890 static void
22891 prune_unmark_dies (dw_die_ref die)
22892 {
22893   dw_die_ref c;
22894
22895   if (die->die_mark)
22896     die->die_mark = 0;
22897   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22898 }
22899
22900 /* Given DIE that we're marking as used, find any other dies
22901    it references as attributes and mark them as used.  */
22902
22903 static void
22904 prune_unused_types_walk_attribs (dw_die_ref die)
22905 {
22906   dw_attr_ref a;
22907   unsigned ix;
22908
22909   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22910     {
22911       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22912         {
22913           /* A reference to another DIE.
22914              Make sure that it will get emitted.
22915              If it was broken out into a comdat group, don't follow it.  */
22916           if (! AT_ref (a)->comdat_type_p
22917               || a->dw_attr == DW_AT_specification)
22918             prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22919         }
22920       /* Set the string's refcount to 0 so that prune_unused_types_mark
22921          accounts properly for it.  */
22922       if (AT_class (a) == dw_val_class_str)
22923         a->dw_attr_val.v.val_str->refcount = 0;
22924     }
22925 }
22926
22927 /* Mark the generic parameters and arguments children DIEs of DIE.  */
22928
22929 static void
22930 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
22931 {
22932   dw_die_ref c;
22933
22934   if (die == NULL || die->die_child == NULL)
22935     return;
22936   c = die->die_child;
22937   do
22938     {
22939       if (is_template_parameter (c))
22940         prune_unused_types_mark (c, 1);
22941       c = c->die_sib;
22942     } while (c && c != die->die_child);
22943 }
22944
22945 /* Mark DIE as being used.  If DOKIDS is true, then walk down
22946    to DIE's children.  */
22947
22948 static void
22949 prune_unused_types_mark (dw_die_ref die, int dokids)
22950 {
22951   dw_die_ref c;
22952
22953   if (die->die_mark == 0)
22954     {
22955       /* We haven't done this node yet.  Mark it as used.  */
22956       die->die_mark = 1;
22957       /* If this is the DIE of a generic type instantiation,
22958          mark the children DIEs that describe its generic parms and
22959          args.  */
22960       prune_unused_types_mark_generic_parms_dies (die);
22961
22962       /* We also have to mark its parents as used.
22963          (But we don't want to mark our parent's kids due to this,
22964          unless it is a class.)  */
22965       if (die->die_parent)
22966         prune_unused_types_mark (die->die_parent,
22967                                  class_scope_p (die->die_parent));
22968
22969       /* Mark any referenced nodes.  */
22970       prune_unused_types_walk_attribs (die);
22971
22972       /* If this node is a specification,
22973          also mark the definition, if it exists.  */
22974       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22975         prune_unused_types_mark (die->die_definition, 1);
22976     }
22977
22978   if (dokids && die->die_mark != 2)
22979     {
22980       /* We need to walk the children, but haven't done so yet.
22981          Remember that we've walked the kids.  */
22982       die->die_mark = 2;
22983
22984       /* If this is an array type, we need to make sure our
22985          kids get marked, even if they're types.  If we're
22986          breaking out types into comdat sections, do this
22987          for all type definitions.  */
22988       if (die->die_tag == DW_TAG_array_type
22989           || (use_debug_types
22990               && is_type_die (die) && ! is_declaration_die (die)))
22991         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
22992       else
22993         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22994     }
22995 }
22996
22997 /* For local classes, look if any static member functions were emitted
22998    and if so, mark them.  */
22999
23000 static void
23001 prune_unused_types_walk_local_classes (dw_die_ref die)
23002 {
23003   dw_die_ref c;
23004
23005   if (die->die_mark == 2)
23006     return;
23007
23008   switch (die->die_tag)
23009     {
23010     case DW_TAG_structure_type:
23011     case DW_TAG_union_type:
23012     case DW_TAG_class_type:
23013       break;
23014
23015     case DW_TAG_subprogram:
23016       if (!get_AT_flag (die, DW_AT_declaration)
23017           || die->die_definition != NULL)
23018         prune_unused_types_mark (die, 1);
23019       return;
23020
23021     default:
23022       return;
23023     }
23024
23025   /* Mark children.  */
23026   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
23027 }
23028
23029 /* Walk the tree DIE and mark types that we actually use.  */
23030
23031 static void
23032 prune_unused_types_walk (dw_die_ref die)
23033 {
23034   dw_die_ref c;
23035
23036   /* Don't do anything if this node is already marked and
23037      children have been marked as well.  */
23038   if (die->die_mark == 2)
23039     return;
23040
23041   switch (die->die_tag)
23042     {
23043     case DW_TAG_structure_type:
23044     case DW_TAG_union_type:
23045     case DW_TAG_class_type:
23046       if (die->die_perennial_p)
23047         break;
23048
23049       for (c = die->die_parent; c; c = c->die_parent)
23050         if (c->die_tag == DW_TAG_subprogram)
23051           break;
23052
23053       /* Finding used static member functions inside of classes
23054          is needed just for local classes, because for other classes
23055          static member function DIEs with DW_AT_specification
23056          are emitted outside of the DW_TAG_*_type.  If we ever change
23057          it, we'd need to call this even for non-local classes.  */
23058       if (c)
23059         prune_unused_types_walk_local_classes (die);
23060
23061       /* It's a type node --- don't mark it.  */
23062       return;
23063
23064     case DW_TAG_const_type:
23065     case DW_TAG_packed_type:
23066     case DW_TAG_pointer_type:
23067     case DW_TAG_reference_type:
23068     case DW_TAG_rvalue_reference_type:
23069     case DW_TAG_volatile_type:
23070     case DW_TAG_typedef:
23071     case DW_TAG_array_type:
23072     case DW_TAG_interface_type:
23073     case DW_TAG_friend:
23074     case DW_TAG_variant_part:
23075     case DW_TAG_enumeration_type:
23076     case DW_TAG_subroutine_type:
23077     case DW_TAG_string_type:
23078     case DW_TAG_set_type:
23079     case DW_TAG_subrange_type:
23080     case DW_TAG_ptr_to_member_type:
23081     case DW_TAG_file_type:
23082       if (die->die_perennial_p)
23083         break;
23084
23085       /* It's a type node --- don't mark it.  */
23086       return;
23087
23088     default:
23089       /* Mark everything else.  */
23090       break;
23091   }
23092
23093   if (die->die_mark == 0)
23094     {
23095       die->die_mark = 1;
23096
23097       /* Now, mark any dies referenced from here.  */
23098       prune_unused_types_walk_attribs (die);
23099     }
23100
23101   die->die_mark = 2;
23102
23103   /* Mark children.  */
23104   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
23105 }
23106
23107 /* Increment the string counts on strings referred to from DIE's
23108    attributes.  */
23109
23110 static void
23111 prune_unused_types_update_strings (dw_die_ref die)
23112 {
23113   dw_attr_ref a;
23114   unsigned ix;
23115
23116   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23117     if (AT_class (a) == dw_val_class_str)
23118       {
23119         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
23120         s->refcount++;
23121         /* Avoid unnecessarily putting strings that are used less than
23122            twice in the hash table.  */
23123         if (s->refcount
23124             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
23125           {
23126             indirect_string_node **slot
23127               = debug_str_hash->find_slot_with_hash (s->str,
23128                                                      htab_hash_string (s->str),
23129                                                      INSERT);
23130             gcc_assert (*slot == NULL);
23131             *slot = s;
23132           }
23133       }
23134 }
23135
23136 /* Remove from the tree DIE any dies that aren't marked.  */
23137
23138 static void
23139 prune_unused_types_prune (dw_die_ref die)
23140 {
23141   dw_die_ref c;
23142
23143   gcc_assert (die->die_mark);
23144   prune_unused_types_update_strings (die);
23145
23146   if (! die->die_child)
23147     return;
23148
23149   c = die->die_child;
23150   do {
23151     dw_die_ref prev = c;
23152     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
23153       if (c == die->die_child)
23154         {
23155           /* No marked children between 'prev' and the end of the list.  */
23156           if (prev == c)
23157             /* No marked children at all.  */
23158             die->die_child = NULL;
23159           else
23160             {
23161               prev->die_sib = c->die_sib;
23162               die->die_child = prev;
23163             }
23164           return;
23165         }
23166
23167     if (c != prev->die_sib)
23168       prev->die_sib = c;
23169     prune_unused_types_prune (c);
23170   } while (c != die->die_child);
23171 }
23172
23173 /* Remove dies representing declarations that we never use.  */
23174
23175 static void
23176 prune_unused_types (void)
23177 {
23178   unsigned int i;
23179   limbo_die_node *node;
23180   comdat_type_node *ctnode;
23181   pubname_ref pub;
23182   dw_die_ref base_type;
23183
23184 #if ENABLE_ASSERT_CHECKING
23185   /* All the marks should already be clear.  */
23186   verify_marks_clear (comp_unit_die ());
23187   for (node = limbo_die_list; node; node = node->next)
23188     verify_marks_clear (node->die);
23189   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23190     verify_marks_clear (ctnode->root_die);
23191 #endif /* ENABLE_ASSERT_CHECKING */
23192
23193   /* Mark types that are used in global variables.  */
23194   premark_types_used_by_global_vars ();
23195
23196   /* Set the mark on nodes that are actually used.  */
23197   prune_unused_types_walk (comp_unit_die ());
23198   for (node = limbo_die_list; node; node = node->next)
23199     prune_unused_types_walk (node->die);
23200   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23201     {
23202       prune_unused_types_walk (ctnode->root_die);
23203       prune_unused_types_mark (ctnode->type_die, 1);
23204     }
23205
23206   /* Also set the mark on nodes referenced from the pubname_table.  Enumerators
23207      are unusual in that they are pubnames that are the children of pubtypes.
23208      They should only be marked via their parent DW_TAG_enumeration_type die,
23209      not as roots in themselves.  */
23210   FOR_EACH_VEC_ELT (*pubname_table, i, pub)
23211     if (pub->die->die_tag != DW_TAG_enumerator)
23212       prune_unused_types_mark (pub->die, 1);
23213   for (i = 0; base_types.iterate (i, &base_type); i++)
23214     prune_unused_types_mark (base_type, 1);
23215
23216   if (debug_str_hash)
23217     debug_str_hash->empty ();
23218   if (skeleton_debug_str_hash)
23219     skeleton_debug_str_hash->empty ();
23220   prune_unused_types_prune (comp_unit_die ());
23221   for (node = limbo_die_list; node; node = node->next)
23222     prune_unused_types_prune (node->die);
23223   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23224     prune_unused_types_prune (ctnode->root_die);
23225
23226   /* Leave the marks clear.  */
23227   prune_unmark_dies (comp_unit_die ());
23228   for (node = limbo_die_list; node; node = node->next)
23229     prune_unmark_dies (node->die);
23230   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23231     prune_unmark_dies (ctnode->root_die);
23232 }
23233
23234 /* Set the parameter to true if there are any relative pathnames in
23235    the file table.  */
23236 int
23237 file_table_relative_p (dwarf_file_data **slot, bool *p)
23238 {
23239   struct dwarf_file_data *d = *slot;
23240   if (!IS_ABSOLUTE_PATH (d->filename))
23241     {
23242       *p = true;
23243       return 0;
23244     }
23245   return 1;
23246 }
23247
23248 /* Helpers to manipulate hash table of comdat type units.  */
23249
23250 struct comdat_type_hasher : typed_noop_remove <comdat_type_node>
23251 {
23252   typedef comdat_type_node value_type;
23253   typedef comdat_type_node compare_type;
23254   static inline hashval_t hash (const value_type *);
23255   static inline bool equal (const value_type *, const compare_type *);
23256 };
23257
23258 inline hashval_t
23259 comdat_type_hasher::hash (const value_type *type_node)
23260 {
23261   hashval_t h;
23262   memcpy (&h, type_node->signature, sizeof (h));
23263   return h;
23264 }
23265
23266 inline bool
23267 comdat_type_hasher::equal (const value_type *type_node_1,
23268                            const compare_type *type_node_2)
23269 {
23270   return (! memcmp (type_node_1->signature, type_node_2->signature,
23271                     DWARF_TYPE_SIGNATURE_SIZE));
23272 }
23273
23274 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
23275    to the location it would have been added, should we know its
23276    DECL_ASSEMBLER_NAME when we added other attributes.  This will
23277    probably improve compactness of debug info, removing equivalent
23278    abbrevs, and hide any differences caused by deferring the
23279    computation of the assembler name, triggered by e.g. PCH.  */
23280
23281 static inline void
23282 move_linkage_attr (dw_die_ref die)
23283 {
23284   unsigned ix = vec_safe_length (die->die_attr);
23285   dw_attr_node linkage = (*die->die_attr)[ix - 1];
23286
23287   gcc_assert (linkage.dw_attr == DW_AT_linkage_name
23288               || linkage.dw_attr == DW_AT_MIPS_linkage_name);
23289
23290   while (--ix > 0)
23291     {
23292       dw_attr_node *prev = &(*die->die_attr)[ix - 1];
23293
23294       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
23295         break;
23296     }
23297
23298   if (ix != vec_safe_length (die->die_attr) - 1)
23299     {
23300       die->die_attr->pop ();
23301       die->die_attr->quick_insert (ix, linkage);
23302     }
23303 }
23304
23305 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
23306    referenced from typed stack ops and count how often they are used.  */
23307
23308 static void
23309 mark_base_types (dw_loc_descr_ref loc)
23310 {
23311   dw_die_ref base_type = NULL;
23312
23313   for (; loc; loc = loc->dw_loc_next)
23314     {
23315       switch (loc->dw_loc_opc)
23316         {
23317         case DW_OP_GNU_regval_type:
23318         case DW_OP_GNU_deref_type:
23319           base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
23320           break;
23321         case DW_OP_GNU_convert:
23322         case DW_OP_GNU_reinterpret:
23323           if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
23324             continue;
23325           /* FALLTHRU */
23326         case DW_OP_GNU_const_type:
23327           base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
23328           break;
23329         case DW_OP_GNU_entry_value:
23330           mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
23331           continue;
23332         default:
23333           continue;
23334         }
23335       gcc_assert (base_type->die_parent == comp_unit_die ());
23336       if (base_type->die_mark)
23337         base_type->die_mark++;
23338       else
23339         {
23340           base_types.safe_push (base_type);
23341           base_type->die_mark = 1;
23342         }
23343     }
23344 }
23345
23346 /* Comparison function for sorting marked base types.  */
23347
23348 static int
23349 base_type_cmp (const void *x, const void *y)
23350 {
23351   dw_die_ref dx = *(const dw_die_ref *) x;
23352   dw_die_ref dy = *(const dw_die_ref *) y;
23353   unsigned int byte_size1, byte_size2;
23354   unsigned int encoding1, encoding2;
23355   if (dx->die_mark > dy->die_mark)
23356     return -1;
23357   if (dx->die_mark < dy->die_mark)
23358     return 1;
23359   byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
23360   byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
23361   if (byte_size1 < byte_size2)
23362     return 1;
23363   if (byte_size1 > byte_size2)
23364     return -1;
23365   encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
23366   encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
23367   if (encoding1 < encoding2)
23368     return 1;
23369   if (encoding1 > encoding2)
23370     return -1;
23371   return 0;
23372 }
23373
23374 /* Move base types marked by mark_base_types as early as possible
23375    in the CU, sorted by decreasing usage count both to make the
23376    uleb128 references as small as possible and to make sure they
23377    will have die_offset already computed by calc_die_sizes when
23378    sizes of typed stack loc ops is computed.  */
23379
23380 static void
23381 move_marked_base_types (void)
23382 {
23383   unsigned int i;
23384   dw_die_ref base_type, die, c;
23385
23386   if (base_types.is_empty ())
23387     return;
23388
23389   /* Sort by decreasing usage count, they will be added again in that
23390      order later on.  */
23391   base_types.qsort (base_type_cmp);
23392   die = comp_unit_die ();
23393   c = die->die_child;
23394   do
23395     {
23396       dw_die_ref prev = c;
23397       c = c->die_sib;
23398       while (c->die_mark)
23399         {
23400           remove_child_with_prev (c, prev);
23401           /* As base types got marked, there must be at least
23402              one node other than DW_TAG_base_type.  */
23403           gcc_assert (c != c->die_sib);
23404           c = c->die_sib;
23405         }
23406     }
23407   while (c != die->die_child);
23408   gcc_assert (die->die_child);
23409   c = die->die_child;
23410   for (i = 0; base_types.iterate (i, &base_type); i++)
23411     {
23412       base_type->die_mark = 0;
23413       base_type->die_sib = c->die_sib;
23414       c->die_sib = base_type;
23415       c = base_type;
23416     }
23417 }
23418
23419 /* Helper function for resolve_addr, attempt to resolve
23420    one CONST_STRING, return true if successful.  Similarly verify that
23421    SYMBOL_REFs refer to variables emitted in the current CU.  */
23422
23423 static bool
23424 resolve_one_addr (rtx *addr)
23425 {
23426   rtx rtl = *addr;
23427
23428   if (GET_CODE (rtl) == CONST_STRING)
23429     {
23430       size_t len = strlen (XSTR (rtl, 0)) + 1;
23431       tree t = build_string (len, XSTR (rtl, 0));
23432       tree tlen = size_int (len - 1);
23433       TREE_TYPE (t)
23434         = build_array_type (char_type_node, build_index_type (tlen));
23435       rtl = lookup_constant_def (t);
23436       if (!rtl || !MEM_P (rtl))
23437         return false;
23438       rtl = XEXP (rtl, 0);
23439       if (GET_CODE (rtl) == SYMBOL_REF
23440           && SYMBOL_REF_DECL (rtl)
23441           && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23442         return false;
23443       vec_safe_push (used_rtx_array, rtl);
23444       *addr = rtl;
23445       return true;
23446     }
23447
23448   if (GET_CODE (rtl) == SYMBOL_REF
23449       && SYMBOL_REF_DECL (rtl))
23450     {
23451       if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
23452         {
23453           if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
23454             return false;
23455         }
23456       else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23457         return false;
23458     }
23459
23460   if (GET_CODE (rtl) == CONST)
23461     {
23462       subrtx_ptr_iterator::array_type array;
23463       FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
23464         if (!resolve_one_addr (*iter))
23465           return false;
23466     }
23467
23468   return true;
23469 }
23470
23471 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
23472    if possible, and create DW_TAG_dwarf_procedure that can be referenced
23473    from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet.  */
23474
23475 static rtx
23476 string_cst_pool_decl (tree t)
23477 {
23478   rtx rtl = output_constant_def (t, 1);
23479   unsigned char *array;
23480   dw_loc_descr_ref l;
23481   tree decl;
23482   size_t len;
23483   dw_die_ref ref;
23484
23485   if (!rtl || !MEM_P (rtl))
23486     return NULL_RTX;
23487   rtl = XEXP (rtl, 0);
23488   if (GET_CODE (rtl) != SYMBOL_REF
23489       || SYMBOL_REF_DECL (rtl) == NULL_TREE)
23490     return NULL_RTX;
23491
23492   decl = SYMBOL_REF_DECL (rtl);
23493   if (!lookup_decl_die (decl))
23494     {
23495       len = TREE_STRING_LENGTH (t);
23496       vec_safe_push (used_rtx_array, rtl);
23497       ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
23498       array = ggc_vec_alloc<unsigned char> (len);
23499       memcpy (array, TREE_STRING_POINTER (t), len);
23500       l = new_loc_descr (DW_OP_implicit_value, len, 0);
23501       l->dw_loc_oprnd2.val_class = dw_val_class_vec;
23502       l->dw_loc_oprnd2.v.val_vec.length = len;
23503       l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
23504       l->dw_loc_oprnd2.v.val_vec.array = array;
23505       add_AT_loc (ref, DW_AT_location, l);
23506       equate_decl_number_to_die (decl, ref);
23507     }
23508   return rtl;
23509 }
23510
23511 /* Helper function of resolve_addr_in_expr.  LOC is
23512    a DW_OP_addr followed by DW_OP_stack_value, either at the start
23513    of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
23514    resolved.  Replace it (both DW_OP_addr and DW_OP_stack_value)
23515    with DW_OP_GNU_implicit_pointer if possible
23516    and return true, if unsuccessful, return false.  */
23517
23518 static bool
23519 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
23520 {
23521   rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
23522   HOST_WIDE_INT offset = 0;
23523   dw_die_ref ref = NULL;
23524   tree decl;
23525
23526   if (GET_CODE (rtl) == CONST
23527       && GET_CODE (XEXP (rtl, 0)) == PLUS
23528       && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
23529     {
23530       offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
23531       rtl = XEXP (XEXP (rtl, 0), 0);
23532     }
23533   if (GET_CODE (rtl) == CONST_STRING)
23534     {
23535       size_t len = strlen (XSTR (rtl, 0)) + 1;
23536       tree t = build_string (len, XSTR (rtl, 0));
23537       tree tlen = size_int (len - 1);
23538
23539       TREE_TYPE (t)
23540         = build_array_type (char_type_node, build_index_type (tlen));
23541       rtl = string_cst_pool_decl (t);
23542       if (!rtl)
23543         return false;
23544     }
23545   if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
23546     {
23547       decl = SYMBOL_REF_DECL (rtl);
23548       if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
23549         {
23550           ref = lookup_decl_die (decl);
23551           if (ref && (get_AT (ref, DW_AT_location)
23552                       || get_AT (ref, DW_AT_const_value)))
23553             {
23554               loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
23555               loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23556               loc->dw_loc_oprnd1.val_entry = NULL;
23557               loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23558               loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23559               loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23560               loc->dw_loc_oprnd2.v.val_int = offset;
23561               return true;
23562             }
23563         }
23564     }
23565   return false;
23566 }
23567
23568 /* Helper function for resolve_addr, handle one location
23569    expression, return false if at least one CONST_STRING or SYMBOL_REF in
23570    the location list couldn't be resolved.  */
23571
23572 static bool
23573 resolve_addr_in_expr (dw_loc_descr_ref loc)
23574 {
23575   dw_loc_descr_ref keep = NULL;
23576   for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
23577     switch (loc->dw_loc_opc)
23578       {
23579       case DW_OP_addr:
23580         if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
23581           {
23582             if ((prev == NULL
23583                  || prev->dw_loc_opc == DW_OP_piece
23584                  || prev->dw_loc_opc == DW_OP_bit_piece)
23585                 && loc->dw_loc_next
23586                 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
23587                 && !dwarf_strict
23588                 && optimize_one_addr_into_implicit_ptr (loc))
23589               break;
23590             return false;
23591           }
23592         break;
23593       case DW_OP_GNU_addr_index:
23594       case DW_OP_GNU_const_index:
23595         if (loc->dw_loc_opc == DW_OP_GNU_addr_index
23596             || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
23597           {
23598             rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
23599             if (!resolve_one_addr (&rtl))
23600               return false;
23601             remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
23602             loc->dw_loc_oprnd1.val_entry =
23603                 add_addr_table_entry (rtl, ate_kind_rtx);
23604           }
23605         break;
23606       case DW_OP_const4u:
23607       case DW_OP_const8u:
23608         if (loc->dtprel
23609             && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
23610           return false;
23611         break;
23612       case DW_OP_plus_uconst:
23613         if (size_of_loc_descr (loc)
23614             > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
23615               + 1
23616             && loc->dw_loc_oprnd1.v.val_unsigned > 0)
23617           {
23618             dw_loc_descr_ref repl
23619               = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
23620             add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
23621             add_loc_descr (&repl, loc->dw_loc_next);
23622             *loc = *repl;
23623           }
23624         break;
23625       case DW_OP_implicit_value:
23626         if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
23627             && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
23628           return false;
23629         break;
23630       case DW_OP_GNU_implicit_pointer:
23631       case DW_OP_GNU_parameter_ref:
23632         if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
23633           {
23634             dw_die_ref ref
23635               = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
23636             if (ref == NULL)
23637               return false;
23638             loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23639             loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23640             loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23641           }
23642         break;
23643       case DW_OP_GNU_const_type:
23644       case DW_OP_GNU_regval_type:
23645       case DW_OP_GNU_deref_type:
23646       case DW_OP_GNU_convert:
23647       case DW_OP_GNU_reinterpret:
23648         while (loc->dw_loc_next
23649                && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
23650           {
23651             dw_die_ref base1, base2;
23652             unsigned enc1, enc2, size1, size2;
23653             if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23654                 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23655               base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
23656             else if (loc->dw_loc_oprnd1.val_class
23657                      == dw_val_class_unsigned_const)
23658               break;
23659             else
23660               base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
23661             if (loc->dw_loc_next->dw_loc_oprnd1.val_class
23662                 == dw_val_class_unsigned_const)
23663               break;
23664             base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
23665             gcc_assert (base1->die_tag == DW_TAG_base_type
23666                         && base2->die_tag == DW_TAG_base_type);
23667             enc1 = get_AT_unsigned (base1, DW_AT_encoding);
23668             enc2 = get_AT_unsigned (base2, DW_AT_encoding);
23669             size1 = get_AT_unsigned (base1, DW_AT_byte_size);
23670             size2 = get_AT_unsigned (base2, DW_AT_byte_size);
23671             if (size1 == size2
23672                 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
23673                      && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
23674                      && loc != keep)
23675                     || enc1 == enc2))
23676               {
23677                 /* Optimize away next DW_OP_GNU_convert after
23678                    adjusting LOC's base type die reference.  */
23679                 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23680                     || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23681                   loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
23682                 else
23683                   loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
23684                 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23685                 continue;
23686               }
23687             /* Don't change integer DW_OP_GNU_convert after e.g. floating
23688                point typed stack entry.  */
23689             else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
23690               keep = loc->dw_loc_next;
23691             break;
23692           }
23693         break;
23694       default:
23695         break;
23696       }
23697   return true;
23698 }
23699
23700 /* Helper function of resolve_addr.  DIE had DW_AT_location of
23701    DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
23702    and DW_OP_addr couldn't be resolved.  resolve_addr has already
23703    removed the DW_AT_location attribute.  This function attempts to
23704    add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
23705    to it or DW_AT_const_value attribute, if possible.  */
23706
23707 static void
23708 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
23709 {
23710   if (TREE_CODE (decl) != VAR_DECL
23711       || lookup_decl_die (decl) != die
23712       || DECL_EXTERNAL (decl)
23713       || !TREE_STATIC (decl)
23714       || DECL_INITIAL (decl) == NULL_TREE
23715       || DECL_P (DECL_INITIAL (decl))
23716       || get_AT (die, DW_AT_const_value))
23717     return;
23718
23719   tree init = DECL_INITIAL (decl);
23720   HOST_WIDE_INT offset = 0;
23721   /* For variables that have been optimized away and thus
23722      don't have a memory location, see if we can emit
23723      DW_AT_const_value instead.  */
23724   if (tree_add_const_value_attribute (die, init))
23725     return;
23726   if (dwarf_strict)
23727     return;
23728   /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
23729      and ADDR_EXPR refers to a decl that has DW_AT_location or
23730      DW_AT_const_value (but isn't addressable, otherwise
23731      resolving the original DW_OP_addr wouldn't fail), see if
23732      we can add DW_OP_GNU_implicit_pointer.  */
23733   STRIP_NOPS (init);
23734   if (TREE_CODE (init) == POINTER_PLUS_EXPR
23735       && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
23736     {
23737       offset = tree_to_shwi (TREE_OPERAND (init, 1));
23738       init = TREE_OPERAND (init, 0);
23739       STRIP_NOPS (init);
23740     }
23741   if (TREE_CODE (init) != ADDR_EXPR)
23742     return;
23743   if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
23744        && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
23745       || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
23746           && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
23747           && TREE_OPERAND (init, 0) != decl))
23748     {
23749       dw_die_ref ref;
23750       dw_loc_descr_ref l;
23751
23752       if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
23753         {
23754           rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
23755           if (!rtl)
23756             return;
23757           decl = SYMBOL_REF_DECL (rtl);
23758         }
23759       else
23760         decl = TREE_OPERAND (init, 0);
23761       ref = lookup_decl_die (decl);
23762       if (ref == NULL
23763           || (!get_AT (ref, DW_AT_location)
23764               && !get_AT (ref, DW_AT_const_value)))
23765         return;
23766       l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
23767       l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23768       l->dw_loc_oprnd1.v.val_die_ref.die = ref;
23769       l->dw_loc_oprnd1.v.val_die_ref.external = 0;
23770       add_AT_loc (die, DW_AT_location, l);
23771     }
23772 }
23773
23774 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
23775    an address in .rodata section if the string literal is emitted there,
23776    or remove the containing location list or replace DW_AT_const_value
23777    with DW_AT_location and empty location expression, if it isn't found
23778    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
23779    to something that has been emitted in the current CU.  */
23780
23781 static void
23782 resolve_addr (dw_die_ref die)
23783 {
23784   dw_die_ref c;
23785   dw_attr_ref a;
23786   dw_loc_list_ref *curr, *start, loc;
23787   unsigned ix;
23788
23789   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23790     switch (AT_class (a))
23791       {
23792       case dw_val_class_loc_list:
23793         start = curr = AT_loc_list_ptr (a);
23794         loc = *curr;
23795         gcc_assert (loc);
23796         /* The same list can be referenced more than once.  See if we have
23797            already recorded the result from a previous pass.  */
23798         if (loc->replaced)
23799           *curr = loc->dw_loc_next;
23800         else if (!loc->resolved_addr)
23801           {
23802             /* As things stand, we do not expect or allow one die to
23803                reference a suffix of another die's location list chain.
23804                References must be identical or completely separate.
23805                There is therefore no need to cache the result of this
23806                pass on any list other than the first; doing so
23807                would lead to unnecessary writes.  */
23808             while (*curr)
23809               {
23810                 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
23811                 if (!resolve_addr_in_expr ((*curr)->expr))
23812                   {
23813                     dw_loc_list_ref next = (*curr)->dw_loc_next;
23814                     dw_loc_descr_ref l = (*curr)->expr;
23815
23816                     if (next && (*curr)->ll_symbol)
23817                       {
23818                         gcc_assert (!next->ll_symbol);
23819                         next->ll_symbol = (*curr)->ll_symbol;
23820                       }
23821                     if (dwarf_split_debug_info)
23822                       remove_loc_list_addr_table_entries (l);
23823                     *curr = next;
23824                   }
23825                 else
23826                   {
23827                     mark_base_types ((*curr)->expr);
23828                     curr = &(*curr)->dw_loc_next;
23829                   }
23830               }
23831             if (loc == *start)
23832               loc->resolved_addr = 1;
23833             else
23834               {
23835                 loc->replaced = 1;
23836                 loc->dw_loc_next = *start;
23837               }
23838           }
23839         if (!*start)
23840           {
23841             remove_AT (die, a->dw_attr);
23842             ix--;
23843           }
23844         break;
23845       case dw_val_class_loc:
23846         {
23847           dw_loc_descr_ref l = AT_loc (a);
23848           /* For -gdwarf-2 don't attempt to optimize
23849              DW_AT_data_member_location containing
23850              DW_OP_plus_uconst - older consumers might
23851              rely on it being that op instead of a more complex,
23852              but shorter, location description.  */
23853           if ((dwarf_version > 2
23854                || a->dw_attr != DW_AT_data_member_location
23855                || l == NULL
23856                || l->dw_loc_opc != DW_OP_plus_uconst
23857                || l->dw_loc_next != NULL)
23858               && !resolve_addr_in_expr (l))
23859             {
23860               if (dwarf_split_debug_info)
23861                 remove_loc_list_addr_table_entries (l);
23862               if (l != NULL
23863                   && l->dw_loc_next == NULL
23864                   && l->dw_loc_opc == DW_OP_addr
23865                   && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
23866                   && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
23867                   && a->dw_attr == DW_AT_location)
23868                 {
23869                   tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
23870                   remove_AT (die, a->dw_attr);
23871                   ix--;
23872                   optimize_location_into_implicit_ptr (die, decl);
23873                   break;
23874                 }
23875               remove_AT (die, a->dw_attr);
23876               ix--;
23877             }
23878           else
23879             mark_base_types (l);
23880         }
23881         break;
23882       case dw_val_class_addr:
23883         if (a->dw_attr == DW_AT_const_value
23884             && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
23885           {
23886             if (AT_index (a) != NOT_INDEXED)
23887               remove_addr_table_entry (a->dw_attr_val.val_entry);
23888             remove_AT (die, a->dw_attr);
23889             ix--;
23890           }
23891         if (die->die_tag == DW_TAG_GNU_call_site
23892             && a->dw_attr == DW_AT_abstract_origin)
23893           {
23894             tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
23895             dw_die_ref tdie = lookup_decl_die (tdecl);
23896             if (tdie == NULL
23897                 && DECL_EXTERNAL (tdecl)
23898                 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
23899               {
23900                 force_decl_die (tdecl);
23901                 tdie = lookup_decl_die (tdecl);
23902               }
23903             if (tdie)
23904               {
23905                 a->dw_attr_val.val_class = dw_val_class_die_ref;
23906                 a->dw_attr_val.v.val_die_ref.die = tdie;
23907                 a->dw_attr_val.v.val_die_ref.external = 0;
23908               }
23909             else
23910               {
23911                 if (AT_index (a) != NOT_INDEXED)
23912                   remove_addr_table_entry (a->dw_attr_val.val_entry);
23913                 remove_AT (die, a->dw_attr);
23914                 ix--;
23915               }
23916           }
23917         break;
23918       default:
23919         break;
23920       }
23921
23922   FOR_EACH_CHILD (die, c, resolve_addr (c));
23923 }
23924 \f
23925 /* Helper routines for optimize_location_lists.
23926    This pass tries to share identical local lists in .debug_loc
23927    section.  */
23928
23929 /* Iteratively hash operands of LOC opcode into HSTATE.  */
23930
23931 static void
23932 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
23933 {
23934   dw_val_ref val1 = &loc->dw_loc_oprnd1;
23935   dw_val_ref val2 = &loc->dw_loc_oprnd2;
23936
23937   switch (loc->dw_loc_opc)
23938     {
23939     case DW_OP_const4u:
23940     case DW_OP_const8u:
23941       if (loc->dtprel)
23942         goto hash_addr;
23943       /* FALLTHRU */
23944     case DW_OP_const1u:
23945     case DW_OP_const1s:
23946     case DW_OP_const2u:
23947     case DW_OP_const2s:
23948     case DW_OP_const4s:
23949     case DW_OP_const8s:
23950     case DW_OP_constu:
23951     case DW_OP_consts:
23952     case DW_OP_pick:
23953     case DW_OP_plus_uconst:
23954     case DW_OP_breg0:
23955     case DW_OP_breg1:
23956     case DW_OP_breg2:
23957     case DW_OP_breg3:
23958     case DW_OP_breg4:
23959     case DW_OP_breg5:
23960     case DW_OP_breg6:
23961     case DW_OP_breg7:
23962     case DW_OP_breg8:
23963     case DW_OP_breg9:
23964     case DW_OP_breg10:
23965     case DW_OP_breg11:
23966     case DW_OP_breg12:
23967     case DW_OP_breg13:
23968     case DW_OP_breg14:
23969     case DW_OP_breg15:
23970     case DW_OP_breg16:
23971     case DW_OP_breg17:
23972     case DW_OP_breg18:
23973     case DW_OP_breg19:
23974     case DW_OP_breg20:
23975     case DW_OP_breg21:
23976     case DW_OP_breg22:
23977     case DW_OP_breg23:
23978     case DW_OP_breg24:
23979     case DW_OP_breg25:
23980     case DW_OP_breg26:
23981     case DW_OP_breg27:
23982     case DW_OP_breg28:
23983     case DW_OP_breg29:
23984     case DW_OP_breg30:
23985     case DW_OP_breg31:
23986     case DW_OP_regx:
23987     case DW_OP_fbreg:
23988     case DW_OP_piece:
23989     case DW_OP_deref_size:
23990     case DW_OP_xderef_size:
23991       hstate.add_object (val1->v.val_int);
23992       break;
23993     case DW_OP_skip:
23994     case DW_OP_bra:
23995       {
23996         int offset;
23997
23998         gcc_assert (val1->val_class == dw_val_class_loc);
23999         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
24000         hstate.add_object (offset);
24001       }
24002       break;
24003     case DW_OP_implicit_value:
24004       hstate.add_object (val1->v.val_unsigned);
24005       switch (val2->val_class)
24006         {
24007         case dw_val_class_const:
24008           hstate.add_object (val2->v.val_int);
24009           break;
24010         case dw_val_class_vec:
24011           {
24012             unsigned int elt_size = val2->v.val_vec.elt_size;
24013             unsigned int len = val2->v.val_vec.length;
24014
24015             hstate.add_int (elt_size);
24016             hstate.add_int (len);
24017             hstate.add (val2->v.val_vec.array, len * elt_size);
24018           }
24019           break;
24020         case dw_val_class_const_double:
24021           hstate.add_object (val2->v.val_double.low);
24022           hstate.add_object (val2->v.val_double.high);
24023           break;
24024         case dw_val_class_wide_int:
24025           hstate.add_object (*val2->v.val_wide);
24026           break;
24027         case dw_val_class_addr: 
24028           inchash::add_rtx (val2->v.val_addr, hstate);
24029           break;
24030         default:
24031           gcc_unreachable ();
24032         }
24033       break;
24034     case DW_OP_bregx:
24035     case DW_OP_bit_piece:
24036       hstate.add_object (val1->v.val_int);
24037       hstate.add_object (val2->v.val_int);
24038       break;
24039     case DW_OP_addr:
24040     hash_addr:
24041       if (loc->dtprel)
24042         {
24043           unsigned char dtprel = 0xd1;
24044           hstate.add_object (dtprel);
24045         }
24046       inchash::add_rtx (val1->v.val_addr, hstate);
24047       break;
24048     case DW_OP_GNU_addr_index:
24049     case DW_OP_GNU_const_index:
24050       {
24051         if (loc->dtprel)
24052           {
24053             unsigned char dtprel = 0xd1;
24054             hstate.add_object (dtprel);
24055           }
24056         inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
24057       }
24058       break;
24059     case DW_OP_GNU_implicit_pointer:
24060       hstate.add_int (val2->v.val_int);
24061       break;
24062     case DW_OP_GNU_entry_value:
24063       hstate.add_object (val1->v.val_loc);
24064       break;
24065     case DW_OP_GNU_regval_type:
24066     case DW_OP_GNU_deref_type:
24067       {
24068         unsigned int byte_size
24069           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
24070         unsigned int encoding
24071           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
24072         hstate.add_object (val1->v.val_int);
24073         hstate.add_object (byte_size);
24074         hstate.add_object (encoding);
24075       }
24076       break;
24077     case DW_OP_GNU_convert:
24078     case DW_OP_GNU_reinterpret:
24079       if (val1->val_class == dw_val_class_unsigned_const)
24080         {
24081           hstate.add_object (val1->v.val_unsigned);
24082           break;
24083         }
24084       /* FALLTHRU */
24085     case DW_OP_GNU_const_type:
24086       {
24087         unsigned int byte_size
24088           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
24089         unsigned int encoding
24090           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
24091         hstate.add_object (byte_size);
24092         hstate.add_object (encoding);
24093         if (loc->dw_loc_opc != DW_OP_GNU_const_type)
24094           break;
24095         hstate.add_object (val2->val_class);
24096         switch (val2->val_class)
24097           {
24098           case dw_val_class_const:
24099             hstate.add_object (val2->v.val_int);
24100             break;
24101           case dw_val_class_vec:
24102             {
24103               unsigned int elt_size = val2->v.val_vec.elt_size;
24104               unsigned int len = val2->v.val_vec.length;
24105
24106               hstate.add_object (elt_size);
24107               hstate.add_object (len);
24108               hstate.add (val2->v.val_vec.array, len * elt_size);
24109             }
24110             break;
24111           case dw_val_class_const_double:
24112             hstate.add_object (val2->v.val_double.low);
24113             hstate.add_object (val2->v.val_double.high);
24114             break;
24115           case dw_val_class_wide_int:
24116             hstate.add_object (*val2->v.val_wide);
24117             break;
24118           default:
24119             gcc_unreachable ();
24120           }
24121       }
24122       break;
24123
24124     default:
24125       /* Other codes have no operands.  */
24126       break;
24127     }
24128 }
24129
24130 /* Iteratively hash the whole DWARF location expression LOC into HSTATE.  */
24131
24132 static inline void
24133 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
24134 {
24135   dw_loc_descr_ref l;
24136   bool sizes_computed = false;
24137   /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed.  */
24138   size_of_locs (loc);
24139
24140   for (l = loc; l != NULL; l = l->dw_loc_next)
24141     {
24142       enum dwarf_location_atom opc = l->dw_loc_opc;
24143       hstate.add_object (opc);
24144       if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
24145         {
24146           size_of_locs (loc);
24147           sizes_computed = true;
24148         }
24149       hash_loc_operands (l, hstate);
24150     }
24151 }
24152
24153 /* Compute hash of the whole location list LIST_HEAD.  */
24154
24155 static inline void
24156 hash_loc_list (dw_loc_list_ref list_head)
24157 {
24158   dw_loc_list_ref curr = list_head;
24159   inchash::hash hstate;
24160
24161   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
24162     {
24163       hstate.add (curr->begin, strlen (curr->begin) + 1);
24164       hstate.add (curr->end, strlen (curr->end) + 1);
24165       if (curr->section)
24166         hstate.add (curr->section, strlen (curr->section) + 1);
24167       hash_locs (curr->expr, hstate);
24168     }
24169   list_head->hash = hstate.end ();
24170 }
24171
24172 /* Return true if X and Y opcodes have the same operands.  */
24173
24174 static inline bool
24175 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
24176 {
24177   dw_val_ref valx1 = &x->dw_loc_oprnd1;
24178   dw_val_ref valx2 = &x->dw_loc_oprnd2;
24179   dw_val_ref valy1 = &y->dw_loc_oprnd1;
24180   dw_val_ref valy2 = &y->dw_loc_oprnd2;
24181
24182   switch (x->dw_loc_opc)
24183     {
24184     case DW_OP_const4u:
24185     case DW_OP_const8u:
24186       if (x->dtprel)
24187         goto hash_addr;
24188       /* FALLTHRU */
24189     case DW_OP_const1u:
24190     case DW_OP_const1s:
24191     case DW_OP_const2u:
24192     case DW_OP_const2s:
24193     case DW_OP_const4s:
24194     case DW_OP_const8s:
24195     case DW_OP_constu:
24196     case DW_OP_consts:
24197     case DW_OP_pick:
24198     case DW_OP_plus_uconst:
24199     case DW_OP_breg0:
24200     case DW_OP_breg1:
24201     case DW_OP_breg2:
24202     case DW_OP_breg3:
24203     case DW_OP_breg4:
24204     case DW_OP_breg5:
24205     case DW_OP_breg6:
24206     case DW_OP_breg7:
24207     case DW_OP_breg8:
24208     case DW_OP_breg9:
24209     case DW_OP_breg10:
24210     case DW_OP_breg11:
24211     case DW_OP_breg12:
24212     case DW_OP_breg13:
24213     case DW_OP_breg14:
24214     case DW_OP_breg15:
24215     case DW_OP_breg16:
24216     case DW_OP_breg17:
24217     case DW_OP_breg18:
24218     case DW_OP_breg19:
24219     case DW_OP_breg20:
24220     case DW_OP_breg21:
24221     case DW_OP_breg22:
24222     case DW_OP_breg23:
24223     case DW_OP_breg24:
24224     case DW_OP_breg25:
24225     case DW_OP_breg26:
24226     case DW_OP_breg27:
24227     case DW_OP_breg28:
24228     case DW_OP_breg29:
24229     case DW_OP_breg30:
24230     case DW_OP_breg31:
24231     case DW_OP_regx:
24232     case DW_OP_fbreg:
24233     case DW_OP_piece:
24234     case DW_OP_deref_size:
24235     case DW_OP_xderef_size:
24236       return valx1->v.val_int == valy1->v.val_int;
24237     case DW_OP_skip:
24238     case DW_OP_bra:
24239       /* If splitting debug info, the use of DW_OP_GNU_addr_index
24240         can cause irrelevant differences in dw_loc_addr.  */
24241       gcc_assert (valx1->val_class == dw_val_class_loc
24242                   && valy1->val_class == dw_val_class_loc
24243                   && (dwarf_split_debug_info
24244                       || x->dw_loc_addr == y->dw_loc_addr));
24245       return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
24246     case DW_OP_implicit_value:
24247       if (valx1->v.val_unsigned != valy1->v.val_unsigned
24248           || valx2->val_class != valy2->val_class)
24249         return false;
24250       switch (valx2->val_class)
24251         {
24252         case dw_val_class_const:
24253           return valx2->v.val_int == valy2->v.val_int;
24254         case dw_val_class_vec:
24255           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24256                  && valx2->v.val_vec.length == valy2->v.val_vec.length
24257                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24258                             valx2->v.val_vec.elt_size
24259                             * valx2->v.val_vec.length) == 0;
24260         case dw_val_class_const_double:
24261           return valx2->v.val_double.low == valy2->v.val_double.low
24262                  && valx2->v.val_double.high == valy2->v.val_double.high;
24263         case dw_val_class_wide_int:
24264           return *valx2->v.val_wide == *valy2->v.val_wide;
24265         case dw_val_class_addr:
24266           return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
24267         default:
24268           gcc_unreachable ();
24269         }
24270     case DW_OP_bregx:
24271     case DW_OP_bit_piece:
24272       return valx1->v.val_int == valy1->v.val_int
24273              && valx2->v.val_int == valy2->v.val_int;
24274     case DW_OP_addr:
24275     hash_addr:
24276       return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
24277     case DW_OP_GNU_addr_index:
24278     case DW_OP_GNU_const_index:
24279       {
24280         rtx ax1 = valx1->val_entry->addr.rtl;
24281         rtx ay1 = valy1->val_entry->addr.rtl;
24282         return rtx_equal_p (ax1, ay1);
24283       }
24284     case DW_OP_GNU_implicit_pointer:
24285       return valx1->val_class == dw_val_class_die_ref
24286              && valx1->val_class == valy1->val_class
24287              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
24288              && valx2->v.val_int == valy2->v.val_int;
24289     case DW_OP_GNU_entry_value:
24290       return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
24291     case DW_OP_GNU_const_type:
24292       if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
24293           || valx2->val_class != valy2->val_class)
24294         return false;
24295       switch (valx2->val_class)
24296         {
24297         case dw_val_class_const:
24298           return valx2->v.val_int == valy2->v.val_int;
24299         case dw_val_class_vec:
24300           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24301                  && valx2->v.val_vec.length == valy2->v.val_vec.length
24302                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24303                             valx2->v.val_vec.elt_size
24304                             * valx2->v.val_vec.length) == 0;
24305         case dw_val_class_const_double:
24306           return valx2->v.val_double.low == valy2->v.val_double.low
24307                  && valx2->v.val_double.high == valy2->v.val_double.high;
24308         case dw_val_class_wide_int:
24309           return *valx2->v.val_wide == *valy2->v.val_wide;
24310         default:
24311           gcc_unreachable ();
24312         }
24313     case DW_OP_GNU_regval_type:
24314     case DW_OP_GNU_deref_type:
24315       return valx1->v.val_int == valy1->v.val_int
24316              && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
24317     case DW_OP_GNU_convert:
24318     case DW_OP_GNU_reinterpret:
24319       if (valx1->val_class != valy1->val_class)
24320         return false;
24321       if (valx1->val_class == dw_val_class_unsigned_const)
24322         return valx1->v.val_unsigned == valy1->v.val_unsigned;
24323       return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24324     case DW_OP_GNU_parameter_ref:
24325       return valx1->val_class == dw_val_class_die_ref
24326              && valx1->val_class == valy1->val_class
24327              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24328     default:
24329       /* Other codes have no operands.  */
24330       return true;
24331     }
24332 }
24333
24334 /* Return true if DWARF location expressions X and Y are the same.  */
24335
24336 static inline bool
24337 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
24338 {
24339   for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
24340     if (x->dw_loc_opc != y->dw_loc_opc
24341         || x->dtprel != y->dtprel
24342         || !compare_loc_operands (x, y))
24343       break;
24344   return x == NULL && y == NULL;
24345 }
24346
24347 /* Hashtable helpers.  */
24348
24349 struct loc_list_hasher : typed_noop_remove <dw_loc_list_struct>
24350 {
24351   typedef dw_loc_list_struct value_type;
24352   typedef dw_loc_list_struct compare_type;
24353   static inline hashval_t hash (const value_type *);
24354   static inline bool equal (const value_type *, const compare_type *);
24355 };
24356
24357 /* Return precomputed hash of location list X.  */
24358
24359 inline hashval_t
24360 loc_list_hasher::hash (const value_type *x)
24361 {
24362   return x->hash;
24363 }
24364
24365 /* Return true if location lists A and B are the same.  */
24366
24367 inline bool
24368 loc_list_hasher::equal (const value_type *a, const compare_type *b)
24369 {
24370   if (a == b)
24371     return 1;
24372   if (a->hash != b->hash)
24373     return 0;
24374   for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
24375     if (strcmp (a->begin, b->begin) != 0
24376         || strcmp (a->end, b->end) != 0
24377         || (a->section == NULL) != (b->section == NULL)
24378         || (a->section && strcmp (a->section, b->section) != 0)
24379         || !compare_locs (a->expr, b->expr))
24380       break;
24381   return a == NULL && b == NULL;
24382 }
24383
24384 typedef hash_table<loc_list_hasher> loc_list_hash_type;
24385
24386
24387 /* Recursively optimize location lists referenced from DIE
24388    children and share them whenever possible.  */
24389
24390 static void
24391 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
24392 {
24393   dw_die_ref c;
24394   dw_attr_ref a;
24395   unsigned ix;
24396   dw_loc_list_struct **slot;
24397
24398   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24399     if (AT_class (a) == dw_val_class_loc_list)
24400       {
24401         dw_loc_list_ref list = AT_loc_list (a);
24402         /* TODO: perform some optimizations here, before hashing
24403            it and storing into the hash table.  */
24404         hash_loc_list (list);
24405         slot = htab->find_slot_with_hash (list, list->hash, INSERT);
24406         if (*slot == NULL)
24407           *slot = list;
24408         else
24409           a->dw_attr_val.v.val_loc_list = *slot;
24410       }
24411
24412   FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
24413 }
24414
24415
24416 /* Recursively assign each location list a unique index into the debug_addr
24417    section.  */
24418
24419 static void
24420 index_location_lists (dw_die_ref die)
24421 {
24422   dw_die_ref c;
24423   dw_attr_ref a;
24424   unsigned ix;
24425
24426   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24427     if (AT_class (a) == dw_val_class_loc_list)
24428       {
24429         dw_loc_list_ref list = AT_loc_list (a);
24430         dw_loc_list_ref curr;
24431         for (curr = list; curr != NULL; curr = curr->dw_loc_next)
24432           {
24433             /* Don't index an entry that has already been indexed
24434                or won't be output.  */
24435             if (curr->begin_entry != NULL
24436                 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
24437               continue;
24438
24439             curr->begin_entry
24440                 = add_addr_table_entry (xstrdup (curr->begin),
24441                                         ate_kind_label);
24442           }
24443       }
24444
24445   FOR_EACH_CHILD (die, c, index_location_lists (c));
24446 }
24447
24448 /* Optimize location lists referenced from DIE
24449    children and share them whenever possible.  */
24450
24451 static void
24452 optimize_location_lists (dw_die_ref die)
24453 {
24454   loc_list_hash_type htab (500);
24455   optimize_location_lists_1 (die, &htab);
24456 }
24457 \f
24458 /* Output stuff that dwarf requires at the end of every file,
24459    and generate the DWARF-2 debugging info.  */
24460
24461 static void
24462 dwarf2out_finish (const char *filename)
24463 {
24464   limbo_die_node *node, *next_node;
24465   comdat_type_node *ctnode;
24466   unsigned int i;
24467   dw_die_ref main_comp_unit_die;
24468
24469   /* PCH might result in DW_AT_producer string being restored from the
24470      header compilation, so always fill it with empty string initially
24471      and overwrite only here.  */
24472   dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
24473   producer_string = gen_producer_string ();
24474   producer->dw_attr_val.v.val_str->refcount--;
24475   producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
24476
24477   gen_scheduled_generic_parms_dies ();
24478   gen_remaining_tmpl_value_param_die_attribute ();
24479
24480   /* Add the name for the main input file now.  We delayed this from
24481      dwarf2out_init to avoid complications with PCH.  */
24482   add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
24483   if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
24484     add_comp_dir_attribute (comp_unit_die ());
24485   else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
24486     {
24487       bool p = false;
24488       file_table->traverse<bool *, file_table_relative_p> (&p);
24489       if (p)
24490         add_comp_dir_attribute (comp_unit_die ());
24491     }
24492
24493   if (deferred_locations_list)
24494     for (i = 0; i < deferred_locations_list->length (); i++)
24495       {
24496         add_location_or_const_value_attribute (
24497             (*deferred_locations_list)[i].die,
24498             (*deferred_locations_list)[i].variable,
24499             false,
24500             DW_AT_location);
24501       }
24502
24503   /* Traverse the limbo die list, and add parent/child links.  The only
24504      dies without parents that should be here are concrete instances of
24505      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
24506      For concrete instances, we can get the parent die from the abstract
24507      instance.  */
24508   for (node = limbo_die_list; node; node = next_node)
24509     {
24510       dw_die_ref die = node->die;
24511       next_node = node->next;
24512
24513       if (die->die_parent == NULL)
24514         {
24515           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
24516
24517           if (origin && origin->die_parent)
24518             add_child_die (origin->die_parent, die);
24519           else if (is_cu_die (die))
24520             ;
24521           else if (seen_error ())
24522             /* It's OK to be confused by errors in the input.  */
24523             add_child_die (comp_unit_die (), die);
24524           else
24525             {
24526               /* In certain situations, the lexical block containing a
24527                  nested function can be optimized away, which results
24528                  in the nested function die being orphaned.  Likewise
24529                  with the return type of that nested function.  Force
24530                  this to be a child of the containing function.
24531
24532                  It may happen that even the containing function got fully
24533                  inlined and optimized out.  In that case we are lost and
24534                  assign the empty child.  This should not be big issue as
24535                  the function is likely unreachable too.  */
24536               gcc_assert (node->created_for);
24537
24538               if (DECL_P (node->created_for))
24539                 origin = get_context_die (DECL_CONTEXT (node->created_for));
24540               else if (TYPE_P (node->created_for))
24541                 origin = scope_die_for (node->created_for, comp_unit_die ());
24542               else
24543                 origin = comp_unit_die ();
24544
24545               add_child_die (origin, die);
24546             }
24547         }
24548     }
24549
24550   limbo_die_list = NULL;
24551
24552 #if ENABLE_ASSERT_CHECKING
24553   {
24554     dw_die_ref die = comp_unit_die (), c;
24555     FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
24556   }
24557 #endif
24558   resolve_addr (comp_unit_die ());
24559   move_marked_base_types ();
24560
24561   for (node = deferred_asm_name; node; node = node->next)
24562     {
24563       tree decl = node->created_for;
24564       /* When generating LTO bytecode we can not generate new assembler
24565          names at this point and all important decls got theirs via
24566          free-lang-data.  */
24567       if (((!flag_generate_lto && !flag_generate_offload)
24568            || DECL_ASSEMBLER_NAME_SET_P (decl))
24569           && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
24570         {
24571           add_linkage_attr (node->die, decl);
24572           move_linkage_attr (node->die);
24573         }
24574     }
24575
24576   deferred_asm_name = NULL;
24577
24578   /* Walk through the list of incomplete types again, trying once more to
24579      emit full debugging info for them.  */
24580   retry_incomplete_types ();
24581
24582   if (flag_eliminate_unused_debug_types)
24583     prune_unused_types ();
24584
24585   /* Generate separate COMDAT sections for type DIEs. */
24586   if (use_debug_types)
24587     {
24588       break_out_comdat_types (comp_unit_die ());
24589
24590       /* Each new type_unit DIE was added to the limbo die list when created.
24591          Since these have all been added to comdat_type_list, clear the
24592          limbo die list.  */
24593       limbo_die_list = NULL;
24594
24595       /* For each new comdat type unit, copy declarations for incomplete
24596          types to make the new unit self-contained (i.e., no direct
24597          references to the main compile unit).  */
24598       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24599         copy_decls_for_unworthy_types (ctnode->root_die);
24600       copy_decls_for_unworthy_types (comp_unit_die ());
24601
24602       /* In the process of copying declarations from one unit to another,
24603          we may have left some declarations behind that are no longer
24604          referenced.  Prune them.  */
24605       prune_unused_types ();
24606     }
24607
24608   /* Generate separate CUs for each of the include files we've seen.
24609      They will go into limbo_die_list.  */
24610   if (flag_eliminate_dwarf2_dups)
24611     break_out_includes (comp_unit_die ());
24612
24613   /* Traverse the DIE's and add add sibling attributes to those DIE's
24614      that have children.  */
24615   add_sibling_attributes (comp_unit_die ());
24616   for (node = limbo_die_list; node; node = node->next)
24617     add_sibling_attributes (node->die);
24618   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24619     add_sibling_attributes (ctnode->root_die);
24620
24621   /* When splitting DWARF info, we put some attributes in the
24622      skeleton compile_unit DIE that remains in the .o, while
24623      most attributes go in the DWO compile_unit_die.  */
24624   if (dwarf_split_debug_info)
24625     main_comp_unit_die = gen_compile_unit_die (NULL);
24626   else
24627     main_comp_unit_die = comp_unit_die ();
24628
24629   /* Output a terminator label for the .text section.  */
24630   switch_to_section (text_section);
24631   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
24632   if (cold_text_section)
24633     {
24634       switch_to_section (cold_text_section);
24635       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
24636     }
24637
24638   /* We can only use the low/high_pc attributes if all of the code was
24639      in .text.  */
24640   if (!have_multiple_function_sections 
24641       || (dwarf_version < 3 && dwarf_strict))
24642     {
24643       /* Don't add if the CU has no associated code.  */
24644       if (text_section_used)
24645         add_AT_low_high_pc (main_comp_unit_die, text_section_label,
24646                             text_end_label, true);
24647     }
24648   else
24649     {
24650       unsigned fde_idx;
24651       dw_fde_ref fde;
24652       bool range_list_added = false;
24653
24654       if (text_section_used)
24655         add_ranges_by_labels (main_comp_unit_die, text_section_label,
24656                               text_end_label, &range_list_added, true);
24657       if (cold_text_section_used)
24658         add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
24659                               cold_end_label, &range_list_added, true);
24660
24661       FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
24662         {
24663           if (DECL_IGNORED_P (fde->decl))
24664             continue;
24665           if (!fde->in_std_section)
24666             add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
24667                                   fde->dw_fde_end, &range_list_added,
24668                                   true);
24669           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
24670             add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
24671                                   fde->dw_fde_second_end, &range_list_added,
24672                                   true);
24673         }
24674
24675       if (range_list_added)
24676         {
24677           /* We need to give .debug_loc and .debug_ranges an appropriate
24678              "base address".  Use zero so that these addresses become
24679              absolute.  Historically, we've emitted the unexpected
24680              DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
24681              Emit both to give time for other tools to adapt.  */
24682           add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
24683           if (! dwarf_strict && dwarf_version < 4)
24684             add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
24685
24686           add_ranges (NULL);
24687         }
24688     }
24689
24690   if (debug_info_level >= DINFO_LEVEL_TERSE)
24691     add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
24692                     debug_line_section_label);
24693
24694   if (have_macinfo)
24695     add_AT_macptr (comp_unit_die (),
24696                    dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
24697                    macinfo_section_label);
24698
24699   if (dwarf_split_debug_info)
24700     {
24701       /* optimize_location_lists calculates the size of the lists,
24702          so index them first, and assign indices to the entries.
24703          Although optimize_location_lists will remove entries from
24704          the table, it only does so for duplicates, and therefore
24705          only reduces ref_counts to 1.  */
24706       index_location_lists (comp_unit_die ());
24707
24708       if (addr_index_table != NULL)
24709         {
24710           unsigned int index = 0;
24711           addr_index_table
24712             ->traverse_noresize<unsigned int *, index_addr_table_entry>
24713             (&index);
24714         }
24715     }
24716
24717   if (have_location_lists)
24718     optimize_location_lists (comp_unit_die ());
24719
24720   save_macinfo_strings ();
24721
24722   if (dwarf_split_debug_info)
24723     {
24724       unsigned int index = 0;
24725
24726       /* Add attributes common to skeleton compile_units and
24727          type_units.  Because these attributes include strings, it
24728          must be done before freezing the string table.  Top-level
24729          skeleton die attrs are added when the skeleton type unit is
24730          created, so ensure it is created by this point.  */
24731       add_top_level_skeleton_die_attrs (main_comp_unit_die);
24732       debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
24733     }
24734
24735   /* Output all of the compilation units.  We put the main one last so that
24736      the offsets are available to output_pubnames.  */
24737   for (node = limbo_die_list; node; node = node->next)
24738     output_comp_unit (node->die, 0);
24739
24740   hash_table<comdat_type_hasher> comdat_type_table (100);
24741   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24742     {
24743       comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
24744
24745       /* Don't output duplicate types.  */
24746       if (*slot != HTAB_EMPTY_ENTRY)
24747         continue;
24748
24749       /* Add a pointer to the line table for the main compilation unit
24750          so that the debugger can make sense of DW_AT_decl_file
24751          attributes.  */
24752       if (debug_info_level >= DINFO_LEVEL_TERSE)
24753         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
24754                         (!dwarf_split_debug_info
24755                          ? debug_line_section_label
24756                          : debug_skeleton_line_section_label));
24757
24758       output_comdat_type_unit (ctnode);
24759       *slot = ctnode;
24760     }
24761
24762   /* The AT_pubnames attribute needs to go in all skeleton dies, including
24763      both the main_cu and all skeleton TUs.  Making this call unconditional
24764      would end up either adding a second copy of the AT_pubnames attribute, or
24765      requiring a special case in add_top_level_skeleton_die_attrs.  */
24766   if (!dwarf_split_debug_info)
24767     add_AT_pubnames (comp_unit_die ());
24768
24769   if (dwarf_split_debug_info)
24770     {
24771       int mark;
24772       unsigned char checksum[16];
24773       struct md5_ctx ctx;
24774
24775       /* Compute a checksum of the comp_unit to use as the dwo_id.  */
24776       md5_init_ctx (&ctx);
24777       mark = 0;
24778       die_checksum (comp_unit_die (), &ctx, &mark);
24779       unmark_all_dies (comp_unit_die ());
24780       md5_finish_ctx (&ctx, checksum);
24781
24782       /* Use the first 8 bytes of the checksum as the dwo_id,
24783         and add it to both comp-unit DIEs.  */
24784       add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
24785       add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
24786
24787       /* Add the base offset of the ranges table to the skeleton
24788         comp-unit DIE.  */
24789       if (ranges_table_in_use)
24790         add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
24791                         ranges_section_label);
24792
24793       switch_to_section (debug_addr_section);
24794       ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
24795       output_addr_table ();
24796     }
24797
24798   /* Output the main compilation unit if non-empty or if .debug_macinfo
24799      or .debug_macro will be emitted.  */
24800   output_comp_unit (comp_unit_die (), have_macinfo);
24801
24802   if (dwarf_split_debug_info && info_section_emitted)
24803     output_skeleton_debug_sections (main_comp_unit_die);
24804
24805   /* Output the abbreviation table.  */
24806   if (abbrev_die_table_in_use != 1)
24807     {
24808       switch_to_section (debug_abbrev_section);
24809       ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
24810       output_abbrev_section ();
24811     }
24812
24813   /* Output location list section if necessary.  */
24814   if (have_location_lists)
24815     {
24816       /* Output the location lists info.  */
24817       switch_to_section (debug_loc_section);
24818       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
24819       output_location_lists (comp_unit_die ());
24820     }
24821
24822   output_pubtables ();
24823
24824   /* Output the address range information if a CU (.debug_info section)
24825      was emitted.  We output an empty table even if we had no functions
24826      to put in it.  This because the consumer has no way to tell the
24827      difference between an empty table that we omitted and failure to
24828      generate a table that would have contained data.  */
24829   if (info_section_emitted)
24830     {
24831       unsigned long aranges_length = size_of_aranges ();
24832
24833       switch_to_section (debug_aranges_section);
24834       output_aranges (aranges_length);
24835     }
24836
24837   /* Output ranges section if necessary.  */
24838   if (ranges_table_in_use)
24839     {
24840       switch_to_section (debug_ranges_section);
24841       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
24842       output_ranges ();
24843     }
24844
24845   /* Have to end the macro section.  */
24846   if (have_macinfo)
24847     {
24848       switch_to_section (debug_macinfo_section);
24849       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
24850       output_macinfo ();
24851       dw2_asm_output_data (1, 0, "End compilation unit");
24852     }
24853
24854   /* Output the source line correspondence table.  We must do this
24855      even if there is no line information.  Otherwise, on an empty
24856      translation unit, we will generate a present, but empty,
24857      .debug_info section.  IRIX 6.5 `nm' will then complain when
24858      examining the file.  This is done late so that any filenames
24859      used by the debug_info section are marked as 'used'.  */
24860   switch_to_section (debug_line_section);
24861   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
24862   if (! DWARF2_ASM_LINE_DEBUG_INFO)
24863     output_line_info (false);
24864
24865   if (dwarf_split_debug_info && info_section_emitted)
24866     {
24867       switch_to_section (debug_skeleton_line_section);
24868       ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
24869       output_line_info (true);
24870     }
24871
24872   /* If we emitted any indirect strings, output the string table too.  */
24873   if (debug_str_hash || skeleton_debug_str_hash)
24874     output_indirect_strings ();
24875 }
24876
24877 /* Reset all state within dwarf2out.c so that we can rerun the compiler
24878    within the same process.  For use by toplev::finalize.  */
24879
24880 void
24881 dwarf2out_c_finalize (void)
24882 {
24883   last_var_location_insn = NULL;
24884   cached_next_real_insn = NULL;
24885   used_rtx_array = NULL;
24886   incomplete_types = NULL;
24887   decl_scope_table = NULL;
24888   debug_info_section = NULL;
24889   debug_skeleton_info_section = NULL;
24890   debug_abbrev_section = NULL;
24891   debug_skeleton_abbrev_section = NULL;
24892   debug_aranges_section = NULL;
24893   debug_addr_section = NULL;
24894   debug_macinfo_section = NULL;
24895   debug_line_section = NULL;
24896   debug_skeleton_line_section = NULL;
24897   debug_loc_section = NULL;
24898   debug_pubnames_section = NULL;
24899   debug_pubtypes_section = NULL;
24900   debug_str_section = NULL;
24901   debug_str_dwo_section = NULL;
24902   debug_str_offsets_section = NULL;
24903   debug_ranges_section = NULL;
24904   debug_frame_section = NULL;
24905   fde_vec = NULL;
24906   debug_str_hash = NULL;
24907   skeleton_debug_str_hash = NULL;
24908   dw2_string_counter = 0;
24909   have_multiple_function_sections = false;
24910   text_section_used = false;
24911   cold_text_section_used = false;
24912   cold_text_section = NULL;
24913   current_unit_personality = NULL;
24914
24915   deferred_locations_list = NULL;
24916
24917   next_die_offset = 0;
24918   single_comp_unit_die = NULL;
24919   comdat_type_list = NULL;
24920   limbo_die_list = NULL;
24921   deferred_asm_name = NULL;
24922   file_table = NULL;
24923   decl_die_table = NULL;
24924   common_block_die_table = NULL;
24925   decl_loc_table = NULL;
24926   call_arg_locations = NULL;
24927   call_arg_loc_last = NULL;
24928   call_site_count = -1;
24929   tail_call_site_count = -1;
24930   //block_map = NULL;
24931   cached_dw_loc_list_table = NULL;
24932   abbrev_die_table = NULL;
24933   abbrev_die_table_allocated = 0;
24934   abbrev_die_table_in_use = 0;
24935   line_info_label_num = 0;
24936   cur_line_info_table = NULL;
24937   text_section_line_info = NULL;
24938   cold_text_section_line_info = NULL;
24939   separate_line_info = NULL;
24940   info_section_emitted = false;
24941   pubname_table = NULL;
24942   pubtype_table = NULL;
24943   macinfo_table = NULL;
24944   ranges_table = NULL;
24945   ranges_table_allocated = 0;
24946   ranges_table_in_use = 0;
24947   ranges_by_label = 0;
24948   ranges_by_label_allocated = 0;
24949   ranges_by_label_in_use = 0;
24950   have_location_lists = false;
24951   loclabel_num = 0;
24952   poc_label_num = 0;
24953   last_emitted_file = NULL;
24954   label_num = 0;
24955   file_table_last_lookup = NULL;
24956   tmpl_value_parm_die_table = NULL;
24957   generic_type_instances = NULL;
24958   frame_pointer_fb_offset = 0;
24959   frame_pointer_fb_offset_valid = false;
24960   base_types.release ();
24961   XDELETEVEC (producer_string);
24962   producer_string = NULL;
24963 }
24964
24965 #include "gt-dwarf2out.h"