2fd889a65341bdf0601197d0d151647232d75a5e
[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 "alias.h"
64 #include "tree.h"
65 #include "fold-const.h"
66 #include "stringpool.h"
67 #include "stor-layout.h"
68 #include "varasm.h"
69 #include "function.h"
70 #include "emit-rtl.h"
71 #include "version.h"
72 #include "flags.h"
73 #include "regs.h"
74 #include "rtlhash.h"
75 #include "insn-config.h"
76 #include "reload.h"
77 #include "output.h"
78 #include "expmed.h"
79 #include "dojump.h"
80 #include "explow.h"
81 #include "calls.h"
82 #include "stmt.h"
83 #include "expr.h"
84 #include "except.h"
85 #include "dwarf2.h"
86 #include "dwarf2out.h"
87 #include "dwarf2asm.h"
88 #include "toplev.h"
89 #include "md5.h"
90 #include "tm_p.h"
91 #include "diagnostic.h"
92 #include "tree-pretty-print.h"
93 #include "debug.h"
94 #include "target.h"
95 #include "common/common-target.h"
96 #include "langhooks.h"
97 #include "cgraph.h"
98 #include "ira.h"
99 #include "lra.h"
100 #include "dumpfile.h"
101 #include "opts.h"
102 #include "tree-dfa.h"
103 #include "gdb/gdb-index.h"
104 #include "rtl-iter.h"
105
106 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
107 static rtx_insn *last_var_location_insn;
108 static rtx_insn *cached_next_real_insn;
109 static void dwarf2out_decl (tree);
110
111 #ifdef VMS_DEBUGGING_INFO
112 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
113
114 /* Define this macro to be a nonzero value if the directory specifications
115     which are output in the debug info should end with a separator.  */
116 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
117 /* Define this macro to evaluate to a nonzero value if GCC should refrain
118    from generating indirect strings in DWARF2 debug information, for instance
119    if your target is stuck with an old version of GDB that is unable to
120    process them properly or uses VMS Debug.  */
121 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
122 #else
123 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
124 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
125 #endif
126
127 /* ??? Poison these here until it can be done generically.  They've been
128    totally replaced in this file; make sure it stays that way.  */
129 #undef DWARF2_UNWIND_INFO
130 #undef DWARF2_FRAME_INFO
131 #if (GCC_VERSION >= 3000)
132  #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
133 #endif
134
135 /* The size of the target's pointer type.  */
136 #ifndef PTR_SIZE
137 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
138 #endif
139
140 /* Array of RTXes referenced by the debugging information, which therefore
141    must be kept around forever.  */
142 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
143
144 /* A pointer to the base of a list of incomplete types which might be
145    completed at some later time.  incomplete_types_list needs to be a
146    vec<tree, va_gc> *because we want to tell the garbage collector about
147    it.  */
148 static GTY(()) vec<tree, va_gc> *incomplete_types;
149
150 /* A pointer to the base of a table of references to declaration
151    scopes.  This table is a display which tracks the nesting
152    of declaration scopes at the current scope and containing
153    scopes.  This table is used to find the proper place to
154    define type declaration DIE's.  */
155 static GTY(()) vec<tree, va_gc> *decl_scope_table;
156
157 /* Pointers to various DWARF2 sections.  */
158 static GTY(()) section *debug_info_section;
159 static GTY(()) section *debug_skeleton_info_section;
160 static GTY(()) section *debug_abbrev_section;
161 static GTY(()) section *debug_skeleton_abbrev_section;
162 static GTY(()) section *debug_aranges_section;
163 static GTY(()) section *debug_addr_section;
164 static GTY(()) section *debug_macinfo_section;
165 static GTY(()) section *debug_line_section;
166 static GTY(()) section *debug_skeleton_line_section;
167 static GTY(()) section *debug_loc_section;
168 static GTY(()) section *debug_pubnames_section;
169 static GTY(()) section *debug_pubtypes_section;
170 static GTY(()) section *debug_str_section;
171 static GTY(()) section *debug_str_dwo_section;
172 static GTY(()) section *debug_str_offsets_section;
173 static GTY(()) section *debug_ranges_section;
174 static GTY(()) section *debug_frame_section;
175
176 /* Maximum size (in bytes) of an artificially generated label.  */
177 #define MAX_ARTIFICIAL_LABEL_BYTES      30
178
179 /* According to the (draft) DWARF 3 specification, the initial length
180    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
181    bytes are 0xffffffff, followed by the length stored in the next 8
182    bytes.
183
184    However, the SGI/MIPS ABI uses an initial length which is equal to
185    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
186
187 #ifndef DWARF_INITIAL_LENGTH_SIZE
188 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
189 #endif
190
191 /* Round SIZE up to the nearest BOUNDARY.  */
192 #define DWARF_ROUND(SIZE,BOUNDARY) \
193   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
194
195 /* CIE identifier.  */
196 #if HOST_BITS_PER_WIDE_INT >= 64
197 #define DWARF_CIE_ID \
198   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
199 #else
200 #define DWARF_CIE_ID DW_CIE_ID
201 #endif
202
203
204 /* A vector for a table that contains frame description
205    information for each routine.  */
206 #define NOT_INDEXED (-1U)
207 #define NO_INDEX_ASSIGNED (-2U)
208
209 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
210
211 struct GTY((for_user)) indirect_string_node {
212   const char *str;
213   unsigned int refcount;
214   enum dwarf_form form;
215   char *label;
216   unsigned int index;
217 };
218
219 struct indirect_string_hasher : ggc_ptr_hash<indirect_string_node>
220 {
221   typedef const char *compare_type;
222
223   static hashval_t hash (indirect_string_node *);
224   static bool equal (indirect_string_node *, const char *);
225 };
226
227 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
228
229 /* With split_debug_info, both the comp_dir and dwo_name go in the
230    main object file, rather than the dwo, similar to the force_direct
231    parameter elsewhere but with additional complications:
232
233    1) The string is needed in both the main object file and the dwo.
234    That is, the comp_dir and dwo_name will appear in both places.
235
236    2) Strings can use three forms: DW_FORM_string, DW_FORM_strp or
237    DW_FORM_GNU_str_index.
238
239    3) GCC chooses the form to use late, depending on the size and
240    reference count.
241
242    Rather than forcing the all debug string handling functions and
243    callers to deal with these complications, simply use a separate,
244    special-cased string table for any attribute that should go in the
245    main object file.  This limits the complexity to just the places
246    that need it.  */
247
248 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
249
250 static GTY(()) int dw2_string_counter;
251
252 /* True if the compilation unit places functions in more than one section.  */
253 static GTY(()) bool have_multiple_function_sections = false;
254
255 /* Whether the default text and cold text sections have been used at all.  */
256
257 static GTY(()) bool text_section_used = false;
258 static GTY(()) bool cold_text_section_used = false;
259
260 /* The default cold text section.  */
261 static GTY(()) section *cold_text_section;
262
263 /* The DIE for C++14 'auto' in a function return type.  */
264 static GTY(()) dw_die_ref auto_die;
265
266 /* The DIE for C++14 'decltype(auto)' in a function return type.  */
267 static GTY(()) dw_die_ref decltype_auto_die;
268
269 /* Forward declarations for functions defined in this file.  */
270
271 static char *stripattributes (const char *);
272 static void output_call_frame_info (int);
273 static void dwarf2out_note_section_used (void);
274
275 /* Personality decl of current unit.  Used only when assembler does not support
276    personality CFI.  */
277 static GTY(()) rtx current_unit_personality;
278
279 /* Data and reference forms for relocatable data.  */
280 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
281 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
282
283 #ifndef DEBUG_FRAME_SECTION
284 #define DEBUG_FRAME_SECTION     ".debug_frame"
285 #endif
286
287 #ifndef FUNC_BEGIN_LABEL
288 #define FUNC_BEGIN_LABEL        "LFB"
289 #endif
290
291 #ifndef FUNC_END_LABEL
292 #define FUNC_END_LABEL          "LFE"
293 #endif
294
295 #ifndef PROLOGUE_END_LABEL
296 #define PROLOGUE_END_LABEL      "LPE"
297 #endif
298
299 #ifndef EPILOGUE_BEGIN_LABEL
300 #define EPILOGUE_BEGIN_LABEL    "LEB"
301 #endif
302
303 #ifndef FRAME_BEGIN_LABEL
304 #define FRAME_BEGIN_LABEL       "Lframe"
305 #endif
306 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
307 #define CIE_END_LABEL           "LECIE"
308 #define FDE_LABEL               "LSFDE"
309 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
310 #define FDE_END_LABEL           "LEFDE"
311 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
312 #define LINE_NUMBER_END_LABEL   "LELT"
313 #define LN_PROLOG_AS_LABEL      "LASLTP"
314 #define LN_PROLOG_END_LABEL     "LELTP"
315 #define DIE_LABEL_PREFIX        "DW"
316 \f
317 /* Match the base name of a file to the base name of a compilation unit. */
318
319 static int
320 matches_main_base (const char *path)
321 {
322   /* Cache the last query. */
323   static const char *last_path = NULL;
324   static int last_match = 0;
325   if (path != last_path)
326     {
327       const char *base;
328       int length = base_of_path (path, &base);
329       last_path = path;
330       last_match = (length == main_input_baselength
331                     && memcmp (base, main_input_basename, length) == 0);
332     }
333   return last_match;
334 }
335
336 #ifdef DEBUG_DEBUG_STRUCT
337
338 static int
339 dump_struct_debug (tree type, enum debug_info_usage usage,
340                    enum debug_struct_file criterion, int generic,
341                    int matches, int result)
342 {
343   /* Find the type name. */
344   tree type_decl = TYPE_STUB_DECL (type);
345   tree t = type_decl;
346   const char *name = 0;
347   if (TREE_CODE (t) == TYPE_DECL)
348     t = DECL_NAME (t);
349   if (t)
350     name = IDENTIFIER_POINTER (t);
351
352   fprintf (stderr, "    struct %d %s %s %s %s %d %p %s\n",
353            criterion,
354            DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
355            matches ? "bas" : "hdr",
356            generic ? "gen" : "ord",
357            usage == DINFO_USAGE_DFN ? ";" :
358              usage == DINFO_USAGE_DIR_USE ? "." : "*",
359            result,
360            (void*) type_decl, name);
361   return result;
362 }
363 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
364   dump_struct_debug (type, usage, criterion, generic, matches, result)
365
366 #else
367
368 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
369   (result)
370
371 #endif
372
373 /* Get the number of HOST_WIDE_INTs needed to represent the precision
374    of the number.  */
375
376 static unsigned int
377 get_full_len (const wide_int &op)
378 {
379   return ((op.get_precision () + HOST_BITS_PER_WIDE_INT - 1)
380           / HOST_BITS_PER_WIDE_INT);
381 }
382
383 static bool
384 should_emit_struct_debug (tree type, enum debug_info_usage usage)
385 {
386   enum debug_struct_file criterion;
387   tree type_decl;
388   bool generic = lang_hooks.types.generic_p (type);
389
390   if (generic)
391     criterion = debug_struct_generic[usage];
392   else
393     criterion = debug_struct_ordinary[usage];
394
395   if (criterion == DINFO_STRUCT_FILE_NONE)
396     return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
397   if (criterion == DINFO_STRUCT_FILE_ANY)
398     return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
399
400   type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
401
402   if (type_decl != NULL)
403     {
404      if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
405         return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
406
407       if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
408         return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
409     }
410
411   return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
412 }
413 \f
414 /* Return a pointer to a copy of the section string name S with all
415    attributes stripped off, and an asterisk prepended (for assemble_name).  */
416
417 static inline char *
418 stripattributes (const char *s)
419 {
420   char *stripped = XNEWVEC (char, strlen (s) + 2);
421   char *p = stripped;
422
423   *p++ = '*';
424
425   while (*s && *s != ',')
426     *p++ = *s++;
427
428   *p = '\0';
429   return stripped;
430 }
431
432 /* Switch [BACK] to eh_frame_section.  If we don't have an eh_frame_section,
433    switch to the data section instead, and write out a synthetic start label
434    for collect2 the first time around.  */
435
436 static void
437 switch_to_eh_frame_section (bool back)
438 {
439   tree label;
440
441 #ifdef EH_FRAME_SECTION_NAME
442   if (eh_frame_section == 0)
443     {
444       int flags;
445
446       if (EH_TABLES_CAN_BE_READ_ONLY)
447         {
448           int fde_encoding;
449           int per_encoding;
450           int lsda_encoding;
451
452           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
453                                                        /*global=*/0);
454           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
455                                                        /*global=*/1);
456           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
457                                                         /*global=*/0);
458           flags = ((! flag_pic
459                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
460                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
461                         && (per_encoding & 0x70) != DW_EH_PE_absptr
462                         && (per_encoding & 0x70) != DW_EH_PE_aligned
463                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
464                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
465                    ? 0 : SECTION_WRITE);
466         }
467       else
468         flags = SECTION_WRITE;
469       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
470     }
471 #endif /* EH_FRAME_SECTION_NAME */
472
473   if (eh_frame_section)
474     switch_to_section (eh_frame_section);
475   else
476     {
477       /* We have no special eh_frame section.  Put the information in
478          the data section and emit special labels to guide collect2.  */
479       switch_to_section (data_section);
480
481       if (!back)
482         {
483           label = get_file_function_name ("F");
484           ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
485           targetm.asm_out.globalize_label (asm_out_file,
486                                            IDENTIFIER_POINTER (label));
487           ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
488         }
489     }
490 }
491
492 /* Switch [BACK] to the eh or debug frame table section, depending on
493    FOR_EH.  */
494
495 static void
496 switch_to_frame_table_section (int for_eh, bool back)
497 {
498   if (for_eh)
499     switch_to_eh_frame_section (back);
500   else
501     {
502       if (!debug_frame_section)
503         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
504                                            SECTION_DEBUG, NULL);
505       switch_to_section (debug_frame_section);
506     }
507 }
508
509 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
510
511 enum dw_cfi_oprnd_type
512 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
513 {
514   switch (cfi)
515     {
516     case DW_CFA_nop:
517     case DW_CFA_GNU_window_save:
518     case DW_CFA_remember_state:
519     case DW_CFA_restore_state:
520       return dw_cfi_oprnd_unused;
521
522     case DW_CFA_set_loc:
523     case DW_CFA_advance_loc1:
524     case DW_CFA_advance_loc2:
525     case DW_CFA_advance_loc4:
526     case DW_CFA_MIPS_advance_loc8:
527       return dw_cfi_oprnd_addr;
528
529     case DW_CFA_offset:
530     case DW_CFA_offset_extended:
531     case DW_CFA_def_cfa:
532     case DW_CFA_offset_extended_sf:
533     case DW_CFA_def_cfa_sf:
534     case DW_CFA_restore:
535     case DW_CFA_restore_extended:
536     case DW_CFA_undefined:
537     case DW_CFA_same_value:
538     case DW_CFA_def_cfa_register:
539     case DW_CFA_register:
540     case DW_CFA_expression:
541       return dw_cfi_oprnd_reg_num;
542
543     case DW_CFA_def_cfa_offset:
544     case DW_CFA_GNU_args_size:
545     case DW_CFA_def_cfa_offset_sf:
546       return dw_cfi_oprnd_offset;
547
548     case DW_CFA_def_cfa_expression:
549       return dw_cfi_oprnd_loc;
550
551     default:
552       gcc_unreachable ();
553     }
554 }
555
556 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
557
558 enum dw_cfi_oprnd_type
559 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
560 {
561   switch (cfi)
562     {
563     case DW_CFA_def_cfa:
564     case DW_CFA_def_cfa_sf:
565     case DW_CFA_offset:
566     case DW_CFA_offset_extended_sf:
567     case DW_CFA_offset_extended:
568       return dw_cfi_oprnd_offset;
569
570     case DW_CFA_register:
571       return dw_cfi_oprnd_reg_num;
572
573     case DW_CFA_expression:
574       return dw_cfi_oprnd_loc;
575
576     default:
577       return dw_cfi_oprnd_unused;
578     }
579 }
580
581 /* Output one FDE.  */
582
583 static void
584 output_fde (dw_fde_ref fde, bool for_eh, bool second,
585             char *section_start_label, int fde_encoding, char *augmentation,
586             bool any_lsda_needed, int lsda_encoding)
587 {
588   const char *begin, *end;
589   static unsigned int j;
590   char l1[20], l2[20];
591
592   targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
593                                      /* empty */ 0);
594   targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
595                                   for_eh + j);
596   ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
597   ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
598   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
599     dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
600                          " indicating 64-bit DWARF extension");
601   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
602                         "FDE Length");
603   ASM_OUTPUT_LABEL (asm_out_file, l1);
604
605   if (for_eh)
606     dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
607   else
608     dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
609                            debug_frame_section, "FDE CIE offset");
610
611   begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
612   end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
613
614   if (for_eh)
615     {
616       rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
617       SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
618       dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
619                                        "FDE initial location");
620       dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
621                             end, begin, "FDE address range");
622     }
623   else
624     {
625       dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
626       dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
627     }
628
629   if (augmentation[0])
630     {
631       if (any_lsda_needed)
632         {
633           int size = size_of_encoded_value (lsda_encoding);
634
635           if (lsda_encoding == DW_EH_PE_aligned)
636             {
637               int offset = (  4         /* Length */
638                             + 4         /* CIE offset */
639                             + 2 * size_of_encoded_value (fde_encoding)
640                             + 1         /* Augmentation size */ );
641               int pad = -offset & (PTR_SIZE - 1);
642
643               size += pad;
644               gcc_assert (size_of_uleb128 (size) == 1);
645             }
646
647           dw2_asm_output_data_uleb128 (size, "Augmentation size");
648
649           if (fde->uses_eh_lsda)
650             {
651               ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
652                                            fde->funcdef_number);
653               dw2_asm_output_encoded_addr_rtx (lsda_encoding,
654                                                gen_rtx_SYMBOL_REF (Pmode, l1),
655                                                false,
656                                                "Language Specific Data Area");
657             }
658           else
659             {
660               if (lsda_encoding == DW_EH_PE_aligned)
661                 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
662               dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
663                                    "Language Specific Data Area (none)");
664             }
665         }
666       else
667         dw2_asm_output_data_uleb128 (0, "Augmentation size");
668     }
669
670   /* Loop through the Call Frame Instructions associated with this FDE.  */
671   fde->dw_fde_current_label = begin;
672   {
673     size_t from, until, i;
674
675     from = 0;
676     until = vec_safe_length (fde->dw_fde_cfi);
677
678     if (fde->dw_fde_second_begin == NULL)
679       ;
680     else if (!second)
681       until = fde->dw_fde_switch_cfi_index;
682     else
683       from = fde->dw_fde_switch_cfi_index;
684
685     for (i = from; i < until; i++)
686       output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
687   }
688
689   /* If we are to emit a ref/link from function bodies to their frame tables,
690      do it now.  This is typically performed to make sure that tables
691      associated with functions are dragged with them and not discarded in
692      garbage collecting links. We need to do this on a per function basis to
693      cope with -ffunction-sections.  */
694
695 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
696   /* Switch to the function section, emit the ref to the tables, and
697      switch *back* into the table section.  */
698   switch_to_section (function_section (fde->decl));
699   ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
700   switch_to_frame_table_section (for_eh, true);
701 #endif
702
703   /* Pad the FDE out to an address sized boundary.  */
704   ASM_OUTPUT_ALIGN (asm_out_file,
705                     floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
706   ASM_OUTPUT_LABEL (asm_out_file, l2);
707
708   j += 2;
709 }
710
711 /* Return true if frame description entry FDE is needed for EH.  */
712
713 static bool
714 fde_needed_for_eh_p (dw_fde_ref fde)
715 {
716   if (flag_asynchronous_unwind_tables)
717     return true;
718
719   if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
720     return true;
721
722   if (fde->uses_eh_lsda)
723     return true;
724
725   /* If exceptions are enabled, we have collected nothrow info.  */
726   if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
727     return false;
728
729   return true;
730 }
731
732 /* Output the call frame information used to record information
733    that relates to calculating the frame pointer, and records the
734    location of saved registers.  */
735
736 static void
737 output_call_frame_info (int for_eh)
738 {
739   unsigned int i;
740   dw_fde_ref fde;
741   dw_cfi_ref cfi;
742   char l1[20], l2[20], section_start_label[20];
743   bool any_lsda_needed = false;
744   char augmentation[6];
745   int augmentation_size;
746   int fde_encoding = DW_EH_PE_absptr;
747   int per_encoding = DW_EH_PE_absptr;
748   int lsda_encoding = DW_EH_PE_absptr;
749   int return_reg;
750   rtx personality = NULL;
751   int dw_cie_version;
752
753   /* Don't emit a CIE if there won't be any FDEs.  */
754   if (!fde_vec)
755     return;
756
757   /* Nothing to do if the assembler's doing it all.  */
758   if (dwarf2out_do_cfi_asm ())
759     return;
760
761   /* If we don't have any functions we'll want to unwind out of, don't emit
762      any EH unwind information.  If we make FDEs linkonce, we may have to
763      emit an empty label for an FDE that wouldn't otherwise be emitted.  We
764      want to avoid having an FDE kept around when the function it refers to
765      is discarded.  Example where this matters: a primary function template
766      in C++ requires EH information, an explicit specialization doesn't.  */
767   if (for_eh)
768     {
769       bool any_eh_needed = false;
770
771       FOR_EACH_VEC_ELT (*fde_vec, i, fde)
772         {
773           if (fde->uses_eh_lsda)
774             any_eh_needed = any_lsda_needed = true;
775           else if (fde_needed_for_eh_p (fde))
776             any_eh_needed = true;
777           else if (TARGET_USES_WEAK_UNWIND_INFO)
778             targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
779         }
780
781       if (!any_eh_needed)
782         return;
783     }
784
785   /* We're going to be generating comments, so turn on app.  */
786   if (flag_debug_asm)
787     app_enable ();
788
789   /* Switch to the proper frame section, first time.  */
790   switch_to_frame_table_section (for_eh, false);
791
792   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
793   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
794
795   /* Output the CIE.  */
796   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
797   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
798   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
799     dw2_asm_output_data (4, 0xffffffff,
800       "Initial length escape value indicating 64-bit DWARF extension");
801   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
802                         "Length of Common Information Entry");
803   ASM_OUTPUT_LABEL (asm_out_file, l1);
804
805   /* Now that the CIE pointer is PC-relative for EH,
806      use 0 to identify the CIE.  */
807   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
808                        (for_eh ? 0 : DWARF_CIE_ID),
809                        "CIE Identifier Tag");
810
811   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
812      use CIE version 1, unless that would produce incorrect results
813      due to overflowing the return register column.  */
814   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
815   dw_cie_version = 1;
816   if (return_reg >= 256 || dwarf_version > 2)
817     dw_cie_version = 3;
818   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
819
820   augmentation[0] = 0;
821   augmentation_size = 0;
822
823   personality = current_unit_personality;
824   if (for_eh)
825     {
826       char *p;
827
828       /* Augmentation:
829          z      Indicates that a uleb128 is present to size the
830                 augmentation section.
831          L      Indicates the encoding (and thus presence) of
832                 an LSDA pointer in the FDE augmentation.
833          R      Indicates a non-default pointer encoding for
834                 FDE code pointers.
835          P      Indicates the presence of an encoding + language
836                 personality routine in the CIE augmentation.  */
837
838       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
839       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
840       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
841
842       p = augmentation + 1;
843       if (personality)
844         {
845           *p++ = 'P';
846           augmentation_size += 1 + size_of_encoded_value (per_encoding);
847           assemble_external_libcall (personality);
848         }
849       if (any_lsda_needed)
850         {
851           *p++ = 'L';
852           augmentation_size += 1;
853         }
854       if (fde_encoding != DW_EH_PE_absptr)
855         {
856           *p++ = 'R';
857           augmentation_size += 1;
858         }
859       if (p > augmentation + 1)
860         {
861           augmentation[0] = 'z';
862           *p = '\0';
863         }
864
865       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
866       if (personality && per_encoding == DW_EH_PE_aligned)
867         {
868           int offset = (  4             /* Length */
869                         + 4             /* CIE Id */
870                         + 1             /* CIE version */
871                         + strlen (augmentation) + 1     /* Augmentation */
872                         + size_of_uleb128 (1)           /* Code alignment */
873                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
874                         + 1             /* RA column */
875                         + 1             /* Augmentation size */
876                         + 1             /* Personality encoding */ );
877           int pad = -offset & (PTR_SIZE - 1);
878
879           augmentation_size += pad;
880
881           /* Augmentations should be small, so there's scarce need to
882              iterate for a solution.  Die if we exceed one uleb128 byte.  */
883           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
884         }
885     }
886
887   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
888   if (dw_cie_version >= 4)
889     {
890       dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
891       dw2_asm_output_data (1, 0, "CIE Segment Size");
892     }
893   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
894   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
895                                "CIE Data Alignment Factor");
896
897   if (dw_cie_version == 1)
898     dw2_asm_output_data (1, return_reg, "CIE RA Column");
899   else
900     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
901
902   if (augmentation[0])
903     {
904       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
905       if (personality)
906         {
907           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
908                                eh_data_format_name (per_encoding));
909           dw2_asm_output_encoded_addr_rtx (per_encoding,
910                                            personality,
911                                            true, NULL);
912         }
913
914       if (any_lsda_needed)
915         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
916                              eh_data_format_name (lsda_encoding));
917
918       if (fde_encoding != DW_EH_PE_absptr)
919         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
920                              eh_data_format_name (fde_encoding));
921     }
922
923   FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
924     output_cfi (cfi, NULL, for_eh);
925
926   /* Pad the CIE out to an address sized boundary.  */
927   ASM_OUTPUT_ALIGN (asm_out_file,
928                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
929   ASM_OUTPUT_LABEL (asm_out_file, l2);
930
931   /* Loop through all of the FDE's.  */
932   FOR_EACH_VEC_ELT (*fde_vec, i, fde)
933     {
934       unsigned int k;
935
936       /* Don't emit EH unwind info for leaf functions that don't need it.  */
937       if (for_eh && !fde_needed_for_eh_p (fde))
938         continue;
939
940       for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
941         output_fde (fde, for_eh, k, section_start_label, fde_encoding,
942                     augmentation, any_lsda_needed, lsda_encoding);
943     }
944
945   if (for_eh && targetm.terminate_dw2_eh_frame_info)
946     dw2_asm_output_data (4, 0, "End of Table");
947
948   /* Turn off app to make assembly quicker.  */
949   if (flag_debug_asm)
950     app_disable ();
951 }
952
953 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed.  */
954
955 static void
956 dwarf2out_do_cfi_startproc (bool second)
957 {
958   int enc;
959   rtx ref;
960   rtx personality = get_personality_function (current_function_decl);
961
962   fprintf (asm_out_file, "\t.cfi_startproc\n");
963
964   if (personality)
965     {
966       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
967       ref = personality;
968
969       /* ??? The GAS support isn't entirely consistent.  We have to
970          handle indirect support ourselves, but PC-relative is done
971          in the assembler.  Further, the assembler can't handle any
972          of the weirder relocation types.  */
973       if (enc & DW_EH_PE_indirect)
974         ref = dw2_force_const_mem (ref, true);
975
976       fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
977       output_addr_const (asm_out_file, ref);
978       fputc ('\n', asm_out_file);
979     }
980
981   if (crtl->uses_eh_lsda)
982     {
983       char lab[20];
984
985       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
986       ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
987                                    current_function_funcdef_no);
988       ref = gen_rtx_SYMBOL_REF (Pmode, lab);
989       SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
990
991       if (enc & DW_EH_PE_indirect)
992         ref = dw2_force_const_mem (ref, true);
993
994       fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
995       output_addr_const (asm_out_file, ref);
996       fputc ('\n', asm_out_file);
997     }
998 }
999
1000 /* Allocate CURRENT_FDE.  Immediately initialize all we can, noting that
1001    this allocation may be done before pass_final.  */
1002
1003 dw_fde_ref
1004 dwarf2out_alloc_current_fde (void)
1005 {
1006   dw_fde_ref fde;
1007
1008   fde = ggc_cleared_alloc<dw_fde_node> ();
1009   fde->decl = current_function_decl;
1010   fde->funcdef_number = current_function_funcdef_no;
1011   fde->fde_index = vec_safe_length (fde_vec);
1012   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1013   fde->uses_eh_lsda = crtl->uses_eh_lsda;
1014   fde->nothrow = crtl->nothrow;
1015   fde->drap_reg = INVALID_REGNUM;
1016   fde->vdrap_reg = INVALID_REGNUM;
1017
1018   /* Record the FDE associated with this function.  */
1019   cfun->fde = fde;
1020   vec_safe_push (fde_vec, fde);
1021
1022   return fde;
1023 }
1024
1025 /* Output a marker (i.e. a label) for the beginning of a function, before
1026    the prologue.  */
1027
1028 void
1029 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1030                           const char *file ATTRIBUTE_UNUSED)
1031 {
1032   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1033   char * dup_label;
1034   dw_fde_ref fde;
1035   section *fnsec;
1036   bool do_frame;
1037
1038   current_function_func_begin_label = NULL;
1039
1040   do_frame = dwarf2out_do_frame ();
1041
1042   /* ??? current_function_func_begin_label is also used by except.c for
1043      call-site information.  We must emit this label if it might be used.  */
1044   if (!do_frame
1045       && (!flag_exceptions
1046           || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1047     return;
1048
1049   fnsec = function_section (current_function_decl);
1050   switch_to_section (fnsec);
1051   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1052                                current_function_funcdef_no);
1053   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1054                           current_function_funcdef_no);
1055   dup_label = xstrdup (label);
1056   current_function_func_begin_label = dup_label;
1057
1058   /* We can elide the fde allocation if we're not emitting debug info.  */
1059   if (!do_frame)
1060     return;
1061
1062   /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1063      emit insns as rtx but bypass the bulk of rest_of_compilation, which
1064      would include pass_dwarf2_frame.  If we've not created the FDE yet,
1065      do so now.  */
1066   fde = cfun->fde;
1067   if (fde == NULL)
1068     fde = dwarf2out_alloc_current_fde ();
1069
1070   /* Initialize the bits of CURRENT_FDE that were not available earlier.  */
1071   fde->dw_fde_begin = dup_label;
1072   fde->dw_fde_current_label = dup_label;
1073   fde->in_std_section = (fnsec == text_section
1074                          || (cold_text_section && fnsec == cold_text_section));
1075
1076   /* We only want to output line number information for the genuine dwarf2
1077      prologue case, not the eh frame case.  */
1078 #ifdef DWARF2_DEBUGGING_INFO
1079   if (file)
1080     dwarf2out_source_line (line, file, 0, true);
1081 #endif
1082
1083   if (dwarf2out_do_cfi_asm ())
1084     dwarf2out_do_cfi_startproc (false);
1085   else
1086     {
1087       rtx personality = get_personality_function (current_function_decl);
1088       if (!current_unit_personality)
1089         current_unit_personality = personality;
1090
1091       /* We cannot keep a current personality per function as without CFI
1092          asm, at the point where we emit the CFI data, there is no current
1093          function anymore.  */
1094       if (personality && current_unit_personality != personality)
1095         sorry ("multiple EH personalities are supported only with assemblers "
1096                "supporting .cfi_personality directive");
1097     }
1098 }
1099
1100 /* Output a marker (i.e. a label) for the end of the generated code
1101    for a function prologue.  This gets called *after* the prologue code has
1102    been generated.  */
1103
1104 void
1105 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1106                         const char *file ATTRIBUTE_UNUSED)
1107 {
1108   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1109
1110   /* Output a label to mark the endpoint of the code generated for this
1111      function.  */
1112   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1113                                current_function_funcdef_no);
1114   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1115                           current_function_funcdef_no);
1116   cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1117 }
1118
1119 /* Output a marker (i.e. a label) for the beginning of the generated code
1120    for a function epilogue.  This gets called *before* the prologue code has
1121    been generated.  */
1122
1123 void
1124 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1125                           const char *file ATTRIBUTE_UNUSED)
1126 {
1127   dw_fde_ref fde = cfun->fde;
1128   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1129
1130   if (fde->dw_fde_vms_begin_epilogue)
1131     return;
1132
1133   /* Output a label to mark the endpoint of the code generated for this
1134      function.  */
1135   ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1136                                current_function_funcdef_no);
1137   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1138                           current_function_funcdef_no);
1139   fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1140 }
1141
1142 /* Output a marker (i.e. a label) for the absolute end of the generated code
1143    for a function definition.  This gets called *after* the epilogue code has
1144    been generated.  */
1145
1146 void
1147 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1148                         const char *file ATTRIBUTE_UNUSED)
1149 {
1150   dw_fde_ref fde;
1151   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1152
1153   last_var_location_insn = NULL;
1154   cached_next_real_insn = NULL;
1155
1156   if (dwarf2out_do_cfi_asm ())
1157     fprintf (asm_out_file, "\t.cfi_endproc\n");
1158
1159   /* Output a label to mark the endpoint of the code generated for this
1160      function.  */
1161   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1162                                current_function_funcdef_no);
1163   ASM_OUTPUT_LABEL (asm_out_file, label);
1164   fde = cfun->fde;
1165   gcc_assert (fde != NULL);
1166   if (fde->dw_fde_second_begin == NULL)
1167     fde->dw_fde_end = xstrdup (label);
1168 }
1169
1170 void
1171 dwarf2out_frame_finish (void)
1172 {
1173   /* Output call frame information.  */
1174   if (targetm.debug_unwind_info () == UI_DWARF2)
1175     output_call_frame_info (0);
1176
1177   /* Output another copy for the unwinder.  */
1178   if ((flag_unwind_tables || flag_exceptions)
1179       && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1180     output_call_frame_info (1);
1181 }
1182
1183 /* Note that the current function section is being used for code.  */
1184
1185 static void
1186 dwarf2out_note_section_used (void)
1187 {
1188   section *sec = current_function_section ();
1189   if (sec == text_section)
1190     text_section_used = true;
1191   else if (sec == cold_text_section)
1192     cold_text_section_used = true;
1193 }
1194
1195 static void var_location_switch_text_section (void);
1196 static void set_cur_line_info_table (section *);
1197
1198 void
1199 dwarf2out_switch_text_section (void)
1200 {
1201   section *sect;
1202   dw_fde_ref fde = cfun->fde;
1203
1204   gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1205
1206   if (!in_cold_section_p)
1207     {
1208       fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1209       fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1210       fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1211     }
1212   else
1213     {
1214       fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1215       fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1216       fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1217     }
1218   have_multiple_function_sections = true;
1219
1220   /* There is no need to mark used sections when not debugging.  */
1221   if (cold_text_section != NULL)
1222     dwarf2out_note_section_used ();
1223
1224   if (dwarf2out_do_cfi_asm ())
1225     fprintf (asm_out_file, "\t.cfi_endproc\n");
1226
1227   /* Now do the real section switch.  */
1228   sect = current_function_section ();
1229   switch_to_section (sect);
1230
1231   fde->second_in_std_section
1232     = (sect == text_section
1233        || (cold_text_section && sect == cold_text_section));
1234
1235   if (dwarf2out_do_cfi_asm ())
1236     dwarf2out_do_cfi_startproc (true);
1237
1238   var_location_switch_text_section ();
1239
1240   if (cold_text_section != NULL)
1241     set_cur_line_info_table (sect);
1242 }
1243 \f
1244 /* And now, the subset of the debugging information support code necessary
1245    for emitting location expressions.  */
1246
1247 /* Data about a single source file.  */
1248 struct GTY((for_user)) dwarf_file_data {
1249   const char * filename;
1250   int emitted_number;
1251 };
1252
1253 /* Describe an entry into the .debug_addr section.  */
1254
1255 enum ate_kind {
1256   ate_kind_rtx,
1257   ate_kind_rtx_dtprel,
1258   ate_kind_label
1259 };
1260
1261 struct GTY((for_user)) addr_table_entry {
1262   enum ate_kind kind;
1263   unsigned int refcount;
1264   unsigned int index;
1265   union addr_table_entry_struct_union
1266     {
1267       rtx GTY ((tag ("0"))) rtl;
1268       char * GTY ((tag ("1"))) label;
1269     }
1270   GTY ((desc ("%1.kind"))) addr;
1271 };
1272
1273 /* Location lists are ranges + location descriptions for that range,
1274    so you can track variables that are in different places over
1275    their entire life.  */
1276 typedef struct GTY(()) dw_loc_list_struct {
1277   dw_loc_list_ref dw_loc_next;
1278   const char *begin; /* Label and addr_entry for start of range */
1279   addr_table_entry *begin_entry;
1280   const char *end;  /* Label for end of range */
1281   char *ll_symbol; /* Label for beginning of location list.
1282                       Only on head of list */
1283   const char *section; /* Section this loclist is relative to */
1284   dw_loc_descr_ref expr;
1285   hashval_t hash;
1286   /* True if all addresses in this and subsequent lists are known to be
1287      resolved.  */
1288   bool resolved_addr;
1289   /* True if this list has been replaced by dw_loc_next.  */
1290   bool replaced;
1291   bool emitted;
1292   /* True if the range should be emitted even if begin and end
1293      are the same.  */
1294   bool force;
1295 } dw_loc_list_node;
1296
1297 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1298
1299 /* Convert a DWARF stack opcode into its string name.  */
1300
1301 static const char *
1302 dwarf_stack_op_name (unsigned int op)
1303 {
1304   const char *name = get_DW_OP_name (op);
1305
1306   if (name != NULL)
1307     return name;
1308
1309   return "OP_<unknown>";
1310 }
1311
1312 /* Return a pointer to a newly allocated location description.  Location
1313    descriptions are simple expression terms that can be strung
1314    together to form more complicated location (address) descriptions.  */
1315
1316 static inline dw_loc_descr_ref
1317 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1318                unsigned HOST_WIDE_INT oprnd2)
1319 {
1320   dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1321
1322   descr->dw_loc_opc = op;
1323   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1324   descr->dw_loc_oprnd1.val_entry = NULL;
1325   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1326   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1327   descr->dw_loc_oprnd2.val_entry = NULL;
1328   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1329
1330   return descr;
1331 }
1332
1333 /* Return a pointer to a newly allocated location description for
1334    REG and OFFSET.  */
1335
1336 static inline dw_loc_descr_ref
1337 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
1338 {
1339   if (reg <= 31)
1340     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1341                           offset, 0);
1342   else
1343     return new_loc_descr (DW_OP_bregx, reg, offset);
1344 }
1345
1346 /* Add a location description term to a location description expression.  */
1347
1348 static inline void
1349 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1350 {
1351   dw_loc_descr_ref *d;
1352
1353   /* Find the end of the chain.  */
1354   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1355     ;
1356
1357   *d = descr;
1358 }
1359
1360 /* Compare two location operands for exact equality.  */
1361
1362 static bool
1363 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1364 {
1365   if (a->val_class != b->val_class)
1366     return false;
1367   switch (a->val_class)
1368     {
1369     case dw_val_class_none:
1370       return true;
1371     case dw_val_class_addr:
1372       return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1373
1374     case dw_val_class_offset:
1375     case dw_val_class_unsigned_const:
1376     case dw_val_class_const:
1377     case dw_val_class_range_list:
1378     case dw_val_class_lineptr:
1379     case dw_val_class_macptr:
1380       /* These are all HOST_WIDE_INT, signed or unsigned.  */
1381       return a->v.val_unsigned == b->v.val_unsigned;
1382
1383     case dw_val_class_loc:
1384       return a->v.val_loc == b->v.val_loc;
1385     case dw_val_class_loc_list:
1386       return a->v.val_loc_list == b->v.val_loc_list;
1387     case dw_val_class_die_ref:
1388       return a->v.val_die_ref.die == b->v.val_die_ref.die;
1389     case dw_val_class_fde_ref:
1390       return a->v.val_fde_index == b->v.val_fde_index;
1391     case dw_val_class_lbl_id:
1392     case dw_val_class_high_pc:
1393       return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1394     case dw_val_class_str:
1395       return a->v.val_str == b->v.val_str;
1396     case dw_val_class_flag:
1397       return a->v.val_flag == b->v.val_flag;
1398     case dw_val_class_file:
1399       return a->v.val_file == b->v.val_file;
1400     case dw_val_class_decl_ref:
1401       return a->v.val_decl_ref == b->v.val_decl_ref;
1402     
1403     case dw_val_class_const_double:
1404       return (a->v.val_double.high == b->v.val_double.high
1405               && a->v.val_double.low == b->v.val_double.low);
1406
1407     case dw_val_class_wide_int:
1408       return *a->v.val_wide == *b->v.val_wide;
1409
1410     case dw_val_class_vec:
1411       {
1412         size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1413         size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1414
1415         return (a_len == b_len
1416                 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1417       }
1418
1419     case dw_val_class_data8:
1420       return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1421
1422     case dw_val_class_vms_delta:
1423       return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1424               && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1425     }
1426   gcc_unreachable ();
1427 }
1428
1429 /* Compare two location atoms for exact equality.  */
1430
1431 static bool
1432 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1433 {
1434   if (a->dw_loc_opc != b->dw_loc_opc)
1435     return false;
1436
1437   /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1438      address size, but since we always allocate cleared storage it
1439      should be zero for other types of locations.  */
1440   if (a->dtprel != b->dtprel)
1441     return false;
1442
1443   return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1444           && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1445 }
1446
1447 /* Compare two complete location expressions for exact equality.  */
1448
1449 bool
1450 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1451 {
1452   while (1)
1453     {
1454       if (a == b)
1455         return true;
1456       if (a == NULL || b == NULL)
1457         return false;
1458       if (!loc_descr_equal_p_1 (a, b))
1459         return false;
1460
1461       a = a->dw_loc_next;
1462       b = b->dw_loc_next;
1463     }
1464 }
1465
1466
1467 /* Add a constant OFFSET to a location expression.  */
1468
1469 static void
1470 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1471 {
1472   dw_loc_descr_ref loc;
1473   HOST_WIDE_INT *p;
1474
1475   gcc_assert (*list_head != NULL);
1476
1477   if (!offset)
1478     return;
1479
1480   /* Find the end of the chain.  */
1481   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1482     ;
1483
1484   p = NULL;
1485   if (loc->dw_loc_opc == DW_OP_fbreg
1486       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1487     p = &loc->dw_loc_oprnd1.v.val_int;
1488   else if (loc->dw_loc_opc == DW_OP_bregx)
1489     p = &loc->dw_loc_oprnd2.v.val_int;
1490
1491   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1492      offset.  Don't optimize if an signed integer overflow would happen.  */
1493   if (p != NULL
1494       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1495           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1496     *p += offset;
1497
1498   else if (offset > 0)
1499     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1500
1501   else
1502     {
1503       loc->dw_loc_next = int_loc_descriptor (-offset);
1504       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1505     }
1506 }
1507
1508 /* Add a constant OFFSET to a location list.  */
1509
1510 static void
1511 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1512 {
1513   dw_loc_list_ref d;
1514   for (d = list_head; d != NULL; d = d->dw_loc_next)
1515     loc_descr_plus_const (&d->expr, offset);
1516 }
1517
1518 #define DWARF_REF_SIZE  \
1519   (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1520
1521 static unsigned long int get_base_type_offset (dw_die_ref);
1522
1523 /* Return the size of a location descriptor.  */
1524
1525 static unsigned long
1526 size_of_loc_descr (dw_loc_descr_ref loc)
1527 {
1528   unsigned long size = 1;
1529
1530   switch (loc->dw_loc_opc)
1531     {
1532     case DW_OP_addr:
1533       size += DWARF2_ADDR_SIZE;
1534       break;
1535     case DW_OP_GNU_addr_index:
1536     case DW_OP_GNU_const_index:
1537       gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1538       size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1539       break;
1540     case DW_OP_const1u:
1541     case DW_OP_const1s:
1542       size += 1;
1543       break;
1544     case DW_OP_const2u:
1545     case DW_OP_const2s:
1546       size += 2;
1547       break;
1548     case DW_OP_const4u:
1549     case DW_OP_const4s:
1550       size += 4;
1551       break;
1552     case DW_OP_const8u:
1553     case DW_OP_const8s:
1554       size += 8;
1555       break;
1556     case DW_OP_constu:
1557       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1558       break;
1559     case DW_OP_consts:
1560       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1561       break;
1562     case DW_OP_pick:
1563       size += 1;
1564       break;
1565     case DW_OP_plus_uconst:
1566       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1567       break;
1568     case DW_OP_skip:
1569     case DW_OP_bra:
1570       size += 2;
1571       break;
1572     case DW_OP_breg0:
1573     case DW_OP_breg1:
1574     case DW_OP_breg2:
1575     case DW_OP_breg3:
1576     case DW_OP_breg4:
1577     case DW_OP_breg5:
1578     case DW_OP_breg6:
1579     case DW_OP_breg7:
1580     case DW_OP_breg8:
1581     case DW_OP_breg9:
1582     case DW_OP_breg10:
1583     case DW_OP_breg11:
1584     case DW_OP_breg12:
1585     case DW_OP_breg13:
1586     case DW_OP_breg14:
1587     case DW_OP_breg15:
1588     case DW_OP_breg16:
1589     case DW_OP_breg17:
1590     case DW_OP_breg18:
1591     case DW_OP_breg19:
1592     case DW_OP_breg20:
1593     case DW_OP_breg21:
1594     case DW_OP_breg22:
1595     case DW_OP_breg23:
1596     case DW_OP_breg24:
1597     case DW_OP_breg25:
1598     case DW_OP_breg26:
1599     case DW_OP_breg27:
1600     case DW_OP_breg28:
1601     case DW_OP_breg29:
1602     case DW_OP_breg30:
1603     case DW_OP_breg31:
1604       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1605       break;
1606     case DW_OP_regx:
1607       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1608       break;
1609     case DW_OP_fbreg:
1610       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1611       break;
1612     case DW_OP_bregx:
1613       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1614       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1615       break;
1616     case DW_OP_piece:
1617       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1618       break;
1619     case DW_OP_bit_piece:
1620       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1621       size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1622       break;
1623     case DW_OP_deref_size:
1624     case DW_OP_xderef_size:
1625       size += 1;
1626       break;
1627     case DW_OP_call2:
1628       size += 2;
1629       break;
1630     case DW_OP_call4:
1631       size += 4;
1632       break;
1633     case DW_OP_call_ref:
1634       size += DWARF_REF_SIZE;
1635       break;
1636     case DW_OP_implicit_value:
1637       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1638               + loc->dw_loc_oprnd1.v.val_unsigned;
1639       break;
1640     case DW_OP_GNU_implicit_pointer:
1641       size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1642       break;
1643     case DW_OP_GNU_entry_value:
1644       {
1645         unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1646         size += size_of_uleb128 (op_size) + op_size;
1647         break;
1648       }
1649     case DW_OP_GNU_const_type:
1650       {
1651         unsigned long o
1652           = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1653         size += size_of_uleb128 (o) + 1;
1654         switch (loc->dw_loc_oprnd2.val_class)
1655           {
1656           case dw_val_class_vec:
1657             size += loc->dw_loc_oprnd2.v.val_vec.length
1658                     * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1659             break;
1660           case dw_val_class_const:
1661             size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1662             break;
1663           case dw_val_class_const_double:
1664             size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1665             break;
1666           case dw_val_class_wide_int:
1667             size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1668                      * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1669             break;
1670           default:
1671             gcc_unreachable ();
1672           }
1673         break;
1674       }
1675     case DW_OP_GNU_regval_type:
1676       {
1677         unsigned long o
1678           = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1679         size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1680                 + size_of_uleb128 (o);
1681       }
1682       break;
1683     case DW_OP_GNU_deref_type:
1684       {
1685         unsigned long o
1686           = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1687         size += 1 + size_of_uleb128 (o);
1688       }
1689       break;
1690     case DW_OP_GNU_convert:
1691     case DW_OP_GNU_reinterpret:
1692       if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1693         size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1694       else
1695         {
1696           unsigned long o
1697             = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1698           size += size_of_uleb128 (o);
1699         }
1700       break;
1701     case DW_OP_GNU_parameter_ref:
1702       size += 4;
1703       break;
1704     default:
1705       break;
1706     }
1707
1708   return size;
1709 }
1710
1711 /* Return the size of a series of location descriptors.  */
1712
1713 unsigned long
1714 size_of_locs (dw_loc_descr_ref loc)
1715 {
1716   dw_loc_descr_ref l;
1717   unsigned long size;
1718
1719   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1720      field, to avoid writing to a PCH file.  */
1721   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1722     {
1723       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1724         break;
1725       size += size_of_loc_descr (l);
1726     }
1727   if (! l)
1728     return size;
1729
1730   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1731     {
1732       l->dw_loc_addr = size;
1733       size += size_of_loc_descr (l);
1734     }
1735
1736   return size;
1737 }
1738
1739 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1740 static void get_ref_die_offset_label (char *, dw_die_ref);
1741 static unsigned long int get_ref_die_offset (dw_die_ref);
1742
1743 /* Output location description stack opcode's operands (if any).
1744    The for_eh_or_skip parameter controls whether register numbers are
1745    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1746    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1747    info).  This should be suppressed for the cases that have not been converted
1748    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
1749
1750 static void
1751 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1752 {
1753   dw_val_ref val1 = &loc->dw_loc_oprnd1;
1754   dw_val_ref val2 = &loc->dw_loc_oprnd2;
1755
1756   switch (loc->dw_loc_opc)
1757     {
1758 #ifdef DWARF2_DEBUGGING_INFO
1759     case DW_OP_const2u:
1760     case DW_OP_const2s:
1761       dw2_asm_output_data (2, val1->v.val_int, NULL);
1762       break;
1763     case DW_OP_const4u:
1764       if (loc->dtprel)
1765         {
1766           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1767           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1768                                                val1->v.val_addr);
1769           fputc ('\n', asm_out_file);
1770           break;
1771         }
1772       /* FALLTHRU */
1773     case DW_OP_const4s:
1774       dw2_asm_output_data (4, val1->v.val_int, NULL);
1775       break;
1776     case DW_OP_const8u:
1777       if (loc->dtprel)
1778         {
1779           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1780           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1781                                                val1->v.val_addr);
1782           fputc ('\n', asm_out_file);
1783           break;
1784         }
1785       /* FALLTHRU */
1786     case DW_OP_const8s:
1787       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1788       dw2_asm_output_data (8, val1->v.val_int, NULL);
1789       break;
1790     case DW_OP_skip:
1791     case DW_OP_bra:
1792       {
1793         int offset;
1794
1795         gcc_assert (val1->val_class == dw_val_class_loc);
1796         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1797
1798         dw2_asm_output_data (2, offset, NULL);
1799       }
1800       break;
1801     case DW_OP_implicit_value:
1802       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1803       switch (val2->val_class)
1804         {
1805         case dw_val_class_const:
1806           dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1807           break;
1808         case dw_val_class_vec:
1809           {
1810             unsigned int elt_size = val2->v.val_vec.elt_size;
1811             unsigned int len = val2->v.val_vec.length;
1812             unsigned int i;
1813             unsigned char *p;
1814
1815             if (elt_size > sizeof (HOST_WIDE_INT))
1816               {
1817                 elt_size /= 2;
1818                 len *= 2;
1819               }
1820             for (i = 0, p = val2->v.val_vec.array;
1821                  i < len;
1822                  i++, p += elt_size)
1823               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1824                                    "fp or vector constant word %u", i);
1825           }
1826           break;
1827         case dw_val_class_const_double:
1828           {
1829             unsigned HOST_WIDE_INT first, second;
1830
1831             if (WORDS_BIG_ENDIAN)
1832               {
1833                 first = val2->v.val_double.high;
1834                 second = val2->v.val_double.low;
1835               }
1836             else
1837               {
1838                 first = val2->v.val_double.low;
1839                 second = val2->v.val_double.high;
1840               }
1841             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1842                                  first, NULL);
1843             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1844                                  second, NULL);
1845           }
1846           break;
1847         case dw_val_class_wide_int:
1848           {
1849             int i;
1850             int len = get_full_len (*val2->v.val_wide);
1851             if (WORDS_BIG_ENDIAN)
1852               for (i = len - 1; i >= 0; --i)
1853                 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1854                                      val2->v.val_wide->elt (i), NULL);
1855             else
1856               for (i = 0; i < len; ++i)
1857                 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1858                                      val2->v.val_wide->elt (i), NULL);
1859           }
1860           break;
1861         case dw_val_class_addr:
1862           gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1863           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1864           break;
1865         default:
1866           gcc_unreachable ();
1867         }
1868       break;
1869 #else
1870     case DW_OP_const2u:
1871     case DW_OP_const2s:
1872     case DW_OP_const4u:
1873     case DW_OP_const4s:
1874     case DW_OP_const8u:
1875     case DW_OP_const8s:
1876     case DW_OP_skip:
1877     case DW_OP_bra:
1878     case DW_OP_implicit_value:
1879       /* We currently don't make any attempt to make sure these are
1880          aligned properly like we do for the main unwind info, so
1881          don't support emitting things larger than a byte if we're
1882          only doing unwinding.  */
1883       gcc_unreachable ();
1884 #endif
1885     case DW_OP_const1u:
1886     case DW_OP_const1s:
1887       dw2_asm_output_data (1, val1->v.val_int, NULL);
1888       break;
1889     case DW_OP_constu:
1890       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1891       break;
1892     case DW_OP_consts:
1893       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1894       break;
1895     case DW_OP_pick:
1896       dw2_asm_output_data (1, val1->v.val_int, NULL);
1897       break;
1898     case DW_OP_plus_uconst:
1899       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1900       break;
1901     case DW_OP_breg0:
1902     case DW_OP_breg1:
1903     case DW_OP_breg2:
1904     case DW_OP_breg3:
1905     case DW_OP_breg4:
1906     case DW_OP_breg5:
1907     case DW_OP_breg6:
1908     case DW_OP_breg7:
1909     case DW_OP_breg8:
1910     case DW_OP_breg9:
1911     case DW_OP_breg10:
1912     case DW_OP_breg11:
1913     case DW_OP_breg12:
1914     case DW_OP_breg13:
1915     case DW_OP_breg14:
1916     case DW_OP_breg15:
1917     case DW_OP_breg16:
1918     case DW_OP_breg17:
1919     case DW_OP_breg18:
1920     case DW_OP_breg19:
1921     case DW_OP_breg20:
1922     case DW_OP_breg21:
1923     case DW_OP_breg22:
1924     case DW_OP_breg23:
1925     case DW_OP_breg24:
1926     case DW_OP_breg25:
1927     case DW_OP_breg26:
1928     case DW_OP_breg27:
1929     case DW_OP_breg28:
1930     case DW_OP_breg29:
1931     case DW_OP_breg30:
1932     case DW_OP_breg31:
1933       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1934       break;
1935     case DW_OP_regx:
1936       {
1937         unsigned r = val1->v.val_unsigned;
1938         if (for_eh_or_skip >= 0)
1939           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1940         gcc_assert (size_of_uleb128 (r) 
1941                     == size_of_uleb128 (val1->v.val_unsigned));
1942         dw2_asm_output_data_uleb128 (r, NULL);  
1943       }
1944       break;
1945     case DW_OP_fbreg:
1946       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1947       break;
1948     case DW_OP_bregx:
1949       {
1950         unsigned r = val1->v.val_unsigned;
1951         if (for_eh_or_skip >= 0)
1952           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1953         gcc_assert (size_of_uleb128 (r) 
1954                     == size_of_uleb128 (val1->v.val_unsigned));
1955         dw2_asm_output_data_uleb128 (r, NULL);  
1956         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1957       }
1958       break;
1959     case DW_OP_piece:
1960       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1961       break;
1962     case DW_OP_bit_piece:
1963       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1964       dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1965       break;
1966     case DW_OP_deref_size:
1967     case DW_OP_xderef_size:
1968       dw2_asm_output_data (1, val1->v.val_int, NULL);
1969       break;
1970
1971     case DW_OP_addr:
1972       if (loc->dtprel)
1973         {
1974           if (targetm.asm_out.output_dwarf_dtprel)
1975             {
1976               targetm.asm_out.output_dwarf_dtprel (asm_out_file,
1977                                                    DWARF2_ADDR_SIZE,
1978                                                    val1->v.val_addr);
1979               fputc ('\n', asm_out_file);
1980             }
1981           else
1982             gcc_unreachable ();
1983         }
1984       else
1985         {
1986 #ifdef DWARF2_DEBUGGING_INFO
1987           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
1988 #else
1989           gcc_unreachable ();
1990 #endif
1991         }
1992       break;
1993
1994     case DW_OP_GNU_addr_index:
1995     case DW_OP_GNU_const_index:
1996       gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1997       dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
1998                                    "(index into .debug_addr)");
1999       break;
2000
2001     case DW_OP_GNU_implicit_pointer:
2002       {
2003         char label[MAX_ARTIFICIAL_LABEL_BYTES
2004                    + HOST_BITS_PER_WIDE_INT / 2 + 2];
2005         gcc_assert (val1->val_class == dw_val_class_die_ref);
2006         get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2007         dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2008         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2009       }
2010       break;
2011
2012     case DW_OP_GNU_entry_value:
2013       dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2014       output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2015       break;
2016
2017     case DW_OP_GNU_const_type:
2018       {
2019         unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2020         gcc_assert (o);
2021         dw2_asm_output_data_uleb128 (o, NULL);
2022         switch (val2->val_class)
2023           {
2024           case dw_val_class_const:
2025             l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2026             dw2_asm_output_data (1, l, NULL);
2027             dw2_asm_output_data (l, val2->v.val_int, NULL);
2028             break;
2029           case dw_val_class_vec:
2030             {
2031               unsigned int elt_size = val2->v.val_vec.elt_size;
2032               unsigned int len = val2->v.val_vec.length;
2033               unsigned int i;
2034               unsigned char *p;
2035
2036               l = len * elt_size;
2037               dw2_asm_output_data (1, l, NULL);
2038               if (elt_size > sizeof (HOST_WIDE_INT))
2039                 {
2040                   elt_size /= 2;
2041                   len *= 2;
2042                 }
2043               for (i = 0, p = val2->v.val_vec.array;
2044                    i < len;
2045                    i++, p += elt_size)
2046                 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2047                                      "fp or vector constant word %u", i);
2048             }
2049             break;
2050           case dw_val_class_const_double:
2051             {
2052               unsigned HOST_WIDE_INT first, second;
2053               l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2054
2055               dw2_asm_output_data (1, 2 * l, NULL);
2056               if (WORDS_BIG_ENDIAN)
2057                 {
2058                   first = val2->v.val_double.high;
2059                   second = val2->v.val_double.low;
2060                 }
2061               else
2062                 {
2063                   first = val2->v.val_double.low;
2064                   second = val2->v.val_double.high;
2065                 }
2066               dw2_asm_output_data (l, first, NULL);
2067               dw2_asm_output_data (l, second, NULL);
2068             }
2069             break;
2070           case dw_val_class_wide_int:
2071             {
2072               int i;
2073               int len = get_full_len (*val2->v.val_wide);
2074               l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2075
2076               dw2_asm_output_data (1, len * l, NULL);
2077               if (WORDS_BIG_ENDIAN)
2078                 for (i = len - 1; i >= 0; --i)
2079                   dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2080               else
2081                 for (i = 0; i < len; ++i)
2082                   dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2083             }
2084             break;
2085           default:
2086             gcc_unreachable ();
2087           }
2088       }
2089       break;
2090     case DW_OP_GNU_regval_type:
2091       {
2092         unsigned r = val1->v.val_unsigned;
2093         unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2094         gcc_assert (o);
2095         if (for_eh_or_skip >= 0)
2096           {
2097             r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2098             gcc_assert (size_of_uleb128 (r)
2099                         == size_of_uleb128 (val1->v.val_unsigned));
2100           }
2101         dw2_asm_output_data_uleb128 (r, NULL);
2102         dw2_asm_output_data_uleb128 (o, NULL);
2103       }
2104       break;
2105     case DW_OP_GNU_deref_type:
2106       {
2107         unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2108         gcc_assert (o);
2109         dw2_asm_output_data (1, val1->v.val_int, NULL);
2110         dw2_asm_output_data_uleb128 (o, NULL);
2111       }
2112       break;
2113     case DW_OP_GNU_convert:
2114     case DW_OP_GNU_reinterpret:
2115       if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2116         dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2117       else
2118         {
2119           unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2120           gcc_assert (o);
2121           dw2_asm_output_data_uleb128 (o, NULL);
2122         }
2123       break;
2124
2125     case DW_OP_GNU_parameter_ref:
2126       {
2127         unsigned long o;
2128         gcc_assert (val1->val_class == dw_val_class_die_ref);
2129         o = get_ref_die_offset (val1->v.val_die_ref.die);
2130         dw2_asm_output_data (4, o, NULL);
2131       }
2132       break;
2133
2134     default:
2135       /* Other codes have no operands.  */
2136       break;
2137     }
2138 }
2139
2140 /* Output a sequence of location operations.  
2141    The for_eh_or_skip parameter controls whether register numbers are
2142    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2143    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2144    info).  This should be suppressed for the cases that have not been converted
2145    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
2146
2147 void
2148 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2149 {
2150   for (; loc != NULL; loc = loc->dw_loc_next)
2151     {
2152       enum dwarf_location_atom opc = loc->dw_loc_opc;
2153       /* Output the opcode.  */
2154       if (for_eh_or_skip >= 0 
2155           && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2156         {
2157           unsigned r = (opc - DW_OP_breg0);
2158           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2159           gcc_assert (r <= 31);
2160           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2161         }
2162       else if (for_eh_or_skip >= 0 
2163                && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2164         {
2165           unsigned r = (opc - DW_OP_reg0);
2166           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2167           gcc_assert (r <= 31);
2168           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2169         }
2170
2171       dw2_asm_output_data (1, opc,
2172                              "%s", dwarf_stack_op_name (opc));
2173
2174       /* Output the operand(s) (if any).  */
2175       output_loc_operands (loc, for_eh_or_skip);
2176     }
2177 }
2178
2179 /* Output location description stack opcode's operands (if any).
2180    The output is single bytes on a line, suitable for .cfi_escape.  */
2181
2182 static void
2183 output_loc_operands_raw (dw_loc_descr_ref loc)
2184 {
2185   dw_val_ref val1 = &loc->dw_loc_oprnd1;
2186   dw_val_ref val2 = &loc->dw_loc_oprnd2;
2187
2188   switch (loc->dw_loc_opc)
2189     {
2190     case DW_OP_addr:
2191     case DW_OP_GNU_addr_index:
2192     case DW_OP_GNU_const_index:
2193     case DW_OP_implicit_value:
2194       /* We cannot output addresses in .cfi_escape, only bytes.  */
2195       gcc_unreachable ();
2196
2197     case DW_OP_const1u:
2198     case DW_OP_const1s:
2199     case DW_OP_pick:
2200     case DW_OP_deref_size:
2201     case DW_OP_xderef_size:
2202       fputc (',', asm_out_file);
2203       dw2_asm_output_data_raw (1, val1->v.val_int);
2204       break;
2205
2206     case DW_OP_const2u:
2207     case DW_OP_const2s:
2208       fputc (',', asm_out_file);
2209       dw2_asm_output_data_raw (2, val1->v.val_int);
2210       break;
2211
2212     case DW_OP_const4u:
2213     case DW_OP_const4s:
2214       fputc (',', asm_out_file);
2215       dw2_asm_output_data_raw (4, val1->v.val_int);
2216       break;
2217
2218     case DW_OP_const8u:
2219     case DW_OP_const8s:
2220       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2221       fputc (',', asm_out_file);
2222       dw2_asm_output_data_raw (8, val1->v.val_int);
2223       break;
2224
2225     case DW_OP_skip:
2226     case DW_OP_bra:
2227       {
2228         int offset;
2229
2230         gcc_assert (val1->val_class == dw_val_class_loc);
2231         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2232
2233         fputc (',', asm_out_file);
2234         dw2_asm_output_data_raw (2, offset);
2235       }
2236       break;
2237
2238     case DW_OP_regx:
2239       {
2240         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2241         gcc_assert (size_of_uleb128 (r) 
2242                     == size_of_uleb128 (val1->v.val_unsigned));
2243         fputc (',', asm_out_file);
2244         dw2_asm_output_data_uleb128_raw (r);
2245       }
2246       break;
2247       
2248     case DW_OP_constu:
2249     case DW_OP_plus_uconst:
2250     case DW_OP_piece:
2251       fputc (',', asm_out_file);
2252       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2253       break;
2254
2255     case DW_OP_bit_piece:
2256       fputc (',', asm_out_file);
2257       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2258       dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2259       break;
2260
2261     case DW_OP_consts:
2262     case DW_OP_breg0:
2263     case DW_OP_breg1:
2264     case DW_OP_breg2:
2265     case DW_OP_breg3:
2266     case DW_OP_breg4:
2267     case DW_OP_breg5:
2268     case DW_OP_breg6:
2269     case DW_OP_breg7:
2270     case DW_OP_breg8:
2271     case DW_OP_breg9:
2272     case DW_OP_breg10:
2273     case DW_OP_breg11:
2274     case DW_OP_breg12:
2275     case DW_OP_breg13:
2276     case DW_OP_breg14:
2277     case DW_OP_breg15:
2278     case DW_OP_breg16:
2279     case DW_OP_breg17:
2280     case DW_OP_breg18:
2281     case DW_OP_breg19:
2282     case DW_OP_breg20:
2283     case DW_OP_breg21:
2284     case DW_OP_breg22:
2285     case DW_OP_breg23:
2286     case DW_OP_breg24:
2287     case DW_OP_breg25:
2288     case DW_OP_breg26:
2289     case DW_OP_breg27:
2290     case DW_OP_breg28:
2291     case DW_OP_breg29:
2292     case DW_OP_breg30:
2293     case DW_OP_breg31:
2294     case DW_OP_fbreg:
2295       fputc (',', asm_out_file);
2296       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2297       break;
2298
2299     case DW_OP_bregx:
2300       {
2301         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2302         gcc_assert (size_of_uleb128 (r) 
2303                     == size_of_uleb128 (val1->v.val_unsigned));
2304         fputc (',', asm_out_file);
2305         dw2_asm_output_data_uleb128_raw (r);
2306         fputc (',', asm_out_file);
2307         dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2308       }
2309       break;
2310
2311     case DW_OP_GNU_implicit_pointer:
2312     case DW_OP_GNU_entry_value:
2313     case DW_OP_GNU_const_type:
2314     case DW_OP_GNU_regval_type:
2315     case DW_OP_GNU_deref_type:
2316     case DW_OP_GNU_convert:
2317     case DW_OP_GNU_reinterpret:
2318     case DW_OP_GNU_parameter_ref:
2319       gcc_unreachable ();
2320       break;
2321
2322     default:
2323       /* Other codes have no operands.  */
2324       break;
2325     }
2326 }
2327
2328 void
2329 output_loc_sequence_raw (dw_loc_descr_ref loc)
2330 {
2331   while (1)
2332     {
2333       enum dwarf_location_atom opc = loc->dw_loc_opc;
2334       /* Output the opcode.  */
2335       if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2336         {
2337           unsigned r = (opc - DW_OP_breg0);
2338           r = DWARF2_FRAME_REG_OUT (r, 1);
2339           gcc_assert (r <= 31);
2340           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2341         }
2342       else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2343         {
2344           unsigned r = (opc - DW_OP_reg0);
2345           r = DWARF2_FRAME_REG_OUT (r, 1);
2346           gcc_assert (r <= 31);
2347           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2348         }
2349       /* Output the opcode.  */
2350       fprintf (asm_out_file, "%#x", opc);
2351       output_loc_operands_raw (loc);
2352
2353       if (!loc->dw_loc_next)
2354         break;
2355       loc = loc->dw_loc_next;
2356
2357       fputc (',', asm_out_file);
2358     }
2359 }
2360
2361 /* This function builds a dwarf location descriptor sequence from a
2362    dw_cfa_location, adding the given OFFSET to the result of the
2363    expression.  */
2364
2365 struct dw_loc_descr_node *
2366 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2367 {
2368   struct dw_loc_descr_node *head, *tmp;
2369
2370   offset += cfa->offset;
2371
2372   if (cfa->indirect)
2373     {
2374       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2375       head->dw_loc_oprnd1.val_class = dw_val_class_const;
2376       head->dw_loc_oprnd1.val_entry = NULL;
2377       tmp = new_loc_descr (DW_OP_deref, 0, 0);
2378       add_loc_descr (&head, tmp);
2379       if (offset != 0)
2380         {
2381           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2382           add_loc_descr (&head, tmp);
2383         }
2384     }
2385   else
2386     head = new_reg_loc_descr (cfa->reg, offset);
2387
2388   return head;
2389 }
2390
2391 /* This function builds a dwarf location descriptor sequence for
2392    the address at OFFSET from the CFA when stack is aligned to
2393    ALIGNMENT byte.  */
2394
2395 struct dw_loc_descr_node *
2396 build_cfa_aligned_loc (dw_cfa_location *cfa,
2397                        HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2398 {
2399   struct dw_loc_descr_node *head;
2400   unsigned int dwarf_fp
2401     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2402
2403   /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
2404   if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2405     {
2406       head = new_reg_loc_descr (dwarf_fp, 0);
2407       add_loc_descr (&head, int_loc_descriptor (alignment));
2408       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2409       loc_descr_plus_const (&head, offset);
2410     }
2411   else
2412     head = new_reg_loc_descr (dwarf_fp, offset);
2413   return head;
2414 }
2415 \f
2416 /* And now, the support for symbolic debugging information.  */
2417
2418 /* .debug_str support.  */
2419
2420 static void dwarf2out_init (const char *);
2421 static void dwarf2out_finish (const char *);
2422 static void dwarf2out_early_finish (void);
2423 static void dwarf2out_assembly_start (void);
2424 static void dwarf2out_define (unsigned int, const char *);
2425 static void dwarf2out_undef (unsigned int, const char *);
2426 static void dwarf2out_start_source_file (unsigned, const char *);
2427 static void dwarf2out_end_source_file (unsigned);
2428 static void dwarf2out_function_decl (tree);
2429 static void dwarf2out_begin_block (unsigned, unsigned);
2430 static void dwarf2out_end_block (unsigned, unsigned);
2431 static bool dwarf2out_ignore_block (const_tree);
2432 static void dwarf2out_early_global_decl (tree);
2433 static void dwarf2out_late_global_decl (tree);
2434 static void dwarf2out_type_decl (tree, int);
2435 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2436 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2437                                                  dw_die_ref);
2438 static void dwarf2out_abstract_function (tree);
2439 static void dwarf2out_var_location (rtx_insn *);
2440 static void dwarf2out_begin_function (tree);
2441 static void dwarf2out_end_function (unsigned int);
2442 static void dwarf2out_register_main_translation_unit (tree unit);
2443 static void dwarf2out_set_name (tree, tree);
2444
2445 /* The debug hooks structure.  */
2446
2447 const struct gcc_debug_hooks dwarf2_debug_hooks =
2448 {
2449   dwarf2out_init,
2450   dwarf2out_finish,
2451   dwarf2out_early_finish,
2452   dwarf2out_assembly_start,
2453   dwarf2out_define,
2454   dwarf2out_undef,
2455   dwarf2out_start_source_file,
2456   dwarf2out_end_source_file,
2457   dwarf2out_begin_block,
2458   dwarf2out_end_block,
2459   dwarf2out_ignore_block,
2460   dwarf2out_source_line,
2461   dwarf2out_begin_prologue,
2462 #if VMS_DEBUGGING_INFO
2463   dwarf2out_vms_end_prologue,
2464   dwarf2out_vms_begin_epilogue,
2465 #else
2466   debug_nothing_int_charstar,
2467   debug_nothing_int_charstar,
2468 #endif
2469   dwarf2out_end_epilogue,
2470   dwarf2out_begin_function,
2471   dwarf2out_end_function,       /* end_function */
2472   dwarf2out_register_main_translation_unit,
2473   dwarf2out_function_decl,      /* function_decl */
2474   dwarf2out_early_global_decl,
2475   dwarf2out_late_global_decl,
2476   dwarf2out_type_decl,          /* type_decl */
2477   dwarf2out_imported_module_or_decl,
2478   debug_nothing_tree,           /* deferred_inline_function */
2479   /* The DWARF 2 backend tries to reduce debugging bloat by not
2480      emitting the abstract description of inline functions until
2481      something tries to reference them.  */
2482   dwarf2out_abstract_function,  /* outlining_inline_function */
2483   debug_nothing_rtx_code_label, /* label */
2484   debug_nothing_int,            /* handle_pch */
2485   dwarf2out_var_location,
2486   dwarf2out_switch_text_section,
2487   dwarf2out_set_name,
2488   1,                            /* start_end_main_source_file */
2489   TYPE_SYMTAB_IS_DIE            /* tree_type_symtab_field */
2490 };
2491
2492 const struct gcc_debug_hooks dwarf2_lineno_debug_hooks =
2493 {
2494   dwarf2out_init,
2495   debug_nothing_charstar,
2496   debug_nothing_void,
2497   debug_nothing_void,
2498   debug_nothing_int_charstar,
2499   debug_nothing_int_charstar,
2500   debug_nothing_int_charstar,
2501   debug_nothing_int,
2502   debug_nothing_int_int,                 /* begin_block */
2503   debug_nothing_int_int,                 /* end_block */
2504   debug_true_const_tree,                 /* ignore_block */
2505   dwarf2out_source_line,         /* source_line */
2506   debug_nothing_int_charstar,            /* begin_prologue */
2507   debug_nothing_int_charstar,            /* end_prologue */
2508   debug_nothing_int_charstar,            /* begin_epilogue */
2509   debug_nothing_int_charstar,            /* end_epilogue */
2510   debug_nothing_tree,                    /* begin_function */
2511   debug_nothing_int,                     /* end_function */
2512   debug_nothing_tree,                    /* register_main_translation_unit */
2513   debug_nothing_tree,                    /* function_decl */
2514   debug_nothing_tree,                    /* early_global_decl */
2515   debug_nothing_tree,                    /* late_global_decl */
2516   debug_nothing_tree_int,                /* type_decl */
2517   debug_nothing_tree_tree_tree_bool,     /* imported_module_or_decl */
2518   debug_nothing_tree,                    /* deferred_inline_function */
2519   debug_nothing_tree,                    /* outlining_inline_function */
2520   debug_nothing_rtx_code_label,          /* label */
2521   debug_nothing_int,                     /* handle_pch */
2522   debug_nothing_rtx_insn,                /* var_location */
2523   debug_nothing_void,                    /* switch_text_section */
2524   debug_nothing_tree_tree,               /* set_name */
2525   0,                                     /* start_end_main_source_file */
2526   TYPE_SYMTAB_IS_ADDRESS                 /* tree_type_symtab_field */
2527 };
2528 \f
2529 /* NOTE: In the comments in this file, many references are made to
2530    "Debugging Information Entries".  This term is abbreviated as `DIE'
2531    throughout the remainder of this file.  */
2532
2533 /* An internal representation of the DWARF output is built, and then
2534    walked to generate the DWARF debugging info.  The walk of the internal
2535    representation is done after the entire program has been compiled.
2536    The types below are used to describe the internal representation.  */
2537
2538 /* Whether to put type DIEs into their own section .debug_types instead
2539    of making them part of the .debug_info section.  Only supported for
2540    Dwarf V4 or higher and the user didn't disable them through
2541    -fno-debug-types-section.  It is more efficient to put them in a
2542    separate comdat sections since the linker will then be able to
2543    remove duplicates.  But not all tools support .debug_types sections
2544    yet.  */
2545
2546 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2547
2548 /* Various DIE's use offsets relative to the beginning of the
2549    .debug_info section to refer to each other.  */
2550
2551 typedef long int dw_offset;
2552
2553 struct comdat_type_node;
2554
2555 /* The entries in the line_info table more-or-less mirror the opcodes
2556    that are used in the real dwarf line table.  Arrays of these entries
2557    are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2558    supported.  */
2559
2560 enum dw_line_info_opcode {
2561   /* Emit DW_LNE_set_address; the operand is the label index.  */
2562   LI_set_address,
2563
2564   /* Emit a row to the matrix with the given line.  This may be done
2565      via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2566      special opcodes.  */
2567   LI_set_line,
2568
2569   /* Emit a DW_LNS_set_file.  */
2570   LI_set_file,
2571
2572   /* Emit a DW_LNS_set_column.  */
2573   LI_set_column,
2574
2575   /* Emit a DW_LNS_negate_stmt; the operand is ignored.  */
2576   LI_negate_stmt,
2577
2578   /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored.  */
2579   LI_set_prologue_end,
2580   LI_set_epilogue_begin,
2581
2582   /* Emit a DW_LNE_set_discriminator.  */
2583   LI_set_discriminator
2584 };
2585
2586 typedef struct GTY(()) dw_line_info_struct {
2587   enum dw_line_info_opcode opcode;
2588   unsigned int val;
2589 } dw_line_info_entry;
2590
2591
2592 struct GTY(()) dw_line_info_table {
2593   /* The label that marks the end of this section.  */
2594   const char *end_label;
2595
2596   /* The values for the last row of the matrix, as collected in the table.
2597      These are used to minimize the changes to the next row.  */
2598   unsigned int file_num;
2599   unsigned int line_num;
2600   unsigned int column_num;
2601   int discrim_num;
2602   bool is_stmt;
2603   bool in_use;
2604
2605   vec<dw_line_info_entry, va_gc> *entries;
2606 };
2607
2608
2609 /* Each DIE attribute has a field specifying the attribute kind,
2610    a link to the next attribute in the chain, and an attribute value.
2611    Attributes are typically linked below the DIE they modify.  */
2612
2613 typedef struct GTY(()) dw_attr_struct {
2614   enum dwarf_attribute dw_attr;
2615   dw_val_node dw_attr_val;
2616 }
2617 dw_attr_node;
2618
2619
2620 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
2621    The children of each node form a circular list linked by
2622    die_sib.  die_child points to the node *before* the "first" child node.  */
2623
2624 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
2625   union die_symbol_or_type_node
2626     {
2627       const char * GTY ((tag ("0"))) die_symbol;
2628       comdat_type_node *GTY ((tag ("1"))) die_type_node;
2629     }
2630   GTY ((desc ("%0.comdat_type_p"))) die_id;
2631   vec<dw_attr_node, va_gc> *die_attr;
2632   dw_die_ref die_parent;
2633   dw_die_ref die_child;
2634   dw_die_ref die_sib;
2635   dw_die_ref die_definition; /* ref from a specification to its definition */
2636   dw_offset die_offset;
2637   unsigned long die_abbrev;
2638   int die_mark;
2639   unsigned int decl_id;
2640   enum dwarf_tag die_tag;
2641   /* Die is used and must not be pruned as unused.  */
2642   BOOL_BITFIELD die_perennial_p : 1;
2643   BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2644   /* Lots of spare bits.  */
2645 }
2646 die_node;
2647
2648 /* Set to TRUE while dwarf2out_early_global_decl is running.  */
2649 static bool early_dwarf;
2650 struct set_early_dwarf {
2651   bool saved;
2652   set_early_dwarf () : saved(early_dwarf) { early_dwarf = true; }
2653   ~set_early_dwarf () { early_dwarf = saved; }
2654 };
2655
2656 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
2657 #define FOR_EACH_CHILD(die, c, expr) do {       \
2658   c = die->die_child;                           \
2659   if (c) do {                                   \
2660     c = c->die_sib;                             \
2661     expr;                                       \
2662   } while (c != die->die_child);                \
2663 } while (0)
2664
2665 /* The pubname structure */
2666
2667 typedef struct GTY(()) pubname_struct {
2668   dw_die_ref die;
2669   const char *name;
2670 }
2671 pubname_entry;
2672
2673
2674 struct GTY(()) dw_ranges {
2675   /* If this is positive, it's a block number, otherwise it's a
2676      bitwise-negated index into dw_ranges_by_label.  */
2677   int num;
2678 };
2679
2680 /* A structure to hold a macinfo entry.  */
2681
2682 typedef struct GTY(()) macinfo_struct {
2683   unsigned char code;
2684   unsigned HOST_WIDE_INT lineno;
2685   const char *info;
2686 }
2687 macinfo_entry;
2688
2689
2690 struct GTY(()) dw_ranges_by_label {
2691   const char *begin;
2692   const char *end;
2693 };
2694
2695 /* The comdat type node structure.  */
2696 struct GTY(()) comdat_type_node
2697 {
2698   dw_die_ref root_die;
2699   dw_die_ref type_die;
2700   dw_die_ref skeleton_die;
2701   char signature[DWARF_TYPE_SIGNATURE_SIZE];
2702   comdat_type_node *next;
2703 };
2704
2705 /* A list of DIEs for which we can't determine ancestry (parent_die
2706    field) just yet.  Later in dwarf2out_finish we will fill in the
2707    missing bits.  */
2708 typedef struct GTY(()) limbo_die_struct {
2709   dw_die_ref die;
2710   /* The tree for which this DIE was created.  We use this to
2711      determine ancestry later.  */
2712   tree created_for;
2713   struct limbo_die_struct *next;
2714 }
2715 limbo_die_node;
2716
2717 typedef struct skeleton_chain_struct
2718 {
2719   dw_die_ref old_die;
2720   dw_die_ref new_die;
2721   struct skeleton_chain_struct *parent;
2722 }
2723 skeleton_chain_node;
2724
2725 /* Define a macro which returns nonzero for a TYPE_DECL which was
2726    implicitly generated for a type.
2727
2728    Note that, unlike the C front-end (which generates a NULL named
2729    TYPE_DECL node for each complete tagged type, each array type,
2730    and each function type node created) the C++ front-end generates
2731    a _named_ TYPE_DECL node for each tagged type node created.
2732    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2733    generate a DW_TAG_typedef DIE for them.  Likewise with the Ada
2734    front-end, but for each type, tagged or not.  */
2735
2736 #define TYPE_DECL_IS_STUB(decl)                         \
2737   (DECL_NAME (decl) == NULL_TREE                        \
2738    || (DECL_ARTIFICIAL (decl)                           \
2739        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
2740            /* This is necessary for stub decls that     \
2741               appear in nested inline functions.  */    \
2742            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2743                && (decl_ultimate_origin (decl)          \
2744                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2745
2746 /* Information concerning the compilation unit's programming
2747    language, and compiler version.  */
2748
2749 /* Fixed size portion of the DWARF compilation unit header.  */
2750 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2751   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2752
2753 /* Fixed size portion of the DWARF comdat type unit header.  */
2754 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2755   (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2756    + DWARF_OFFSET_SIZE)
2757
2758 /* Fixed size portion of public names info.  */
2759 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2760
2761 /* Fixed size portion of the address range info.  */
2762 #define DWARF_ARANGES_HEADER_SIZE                                       \
2763   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
2764                 DWARF2_ADDR_SIZE * 2)                                   \
2765    - DWARF_INITIAL_LENGTH_SIZE)
2766
2767 /* Size of padding portion in the address range info.  It must be
2768    aligned to twice the pointer size.  */
2769 #define DWARF_ARANGES_PAD_SIZE \
2770   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2771                 DWARF2_ADDR_SIZE * 2)                              \
2772    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2773
2774 /* Use assembler line directives if available.  */
2775 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2776 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2777 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2778 #else
2779 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2780 #endif
2781 #endif
2782
2783 /* Minimum line offset in a special line info. opcode.
2784    This value was chosen to give a reasonable range of values.  */
2785 #define DWARF_LINE_BASE  -10
2786
2787 /* First special line opcode - leave room for the standard opcodes.  */
2788 #define DWARF_LINE_OPCODE_BASE  ((int)DW_LNS_set_isa + 1)
2789
2790 /* Range of line offsets in a special line info. opcode.  */
2791 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
2792
2793 /* Flag that indicates the initial value of the is_stmt_start flag.
2794    In the present implementation, we do not mark any lines as
2795    the beginning of a source statement, because that information
2796    is not made available by the GCC front-end.  */
2797 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2798
2799 /* Maximum number of operations per instruction bundle.  */
2800 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2801 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2802 #endif
2803
2804 /* This location is used by calc_die_sizes() to keep track
2805    the offset of each DIE within the .debug_info section.  */
2806 static unsigned long next_die_offset;
2807
2808 /* Record the root of the DIE's built for the current compilation unit.  */
2809 static GTY(()) dw_die_ref single_comp_unit_die;
2810
2811 /* A list of type DIEs that have been separated into comdat sections.  */
2812 static GTY(()) comdat_type_node *comdat_type_list;
2813
2814 /* A list of DIEs with a NULL parent waiting to be relocated.  */
2815 static GTY(()) limbo_die_node *limbo_die_list;
2816
2817 /* A list of DIEs for which we may have to generate
2818    DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set.  */
2819 static GTY(()) limbo_die_node *deferred_asm_name;
2820
2821 struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
2822 {
2823   typedef const char *compare_type;
2824
2825   static hashval_t hash (dwarf_file_data *);
2826   static bool equal (dwarf_file_data *, const char *);
2827 };
2828
2829 /* Filenames referenced by this compilation unit.  */
2830 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
2831
2832 struct decl_die_hasher : ggc_ptr_hash<die_node>
2833 {
2834   typedef tree compare_type;
2835
2836   static hashval_t hash (die_node *);
2837   static bool equal (die_node *, tree);
2838 };
2839 /* A hash table of references to DIE's that describe declarations.
2840    The key is a DECL_UID() which is a unique number identifying each decl.  */
2841 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
2842
2843 struct block_die_hasher : ggc_ptr_hash<die_struct>
2844 {
2845   static hashval_t hash (die_struct *);
2846   static bool equal (die_struct *, die_struct *);
2847 };
2848
2849 /* A hash table of references to DIE's that describe COMMON blocks.
2850    The key is DECL_UID() ^ die_parent.  */
2851 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
2852
2853 typedef struct GTY(()) die_arg_entry_struct {
2854     dw_die_ref die;
2855     tree arg;
2856 } die_arg_entry;
2857
2858
2859 /* Node of the variable location list.  */
2860 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2861   /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2862      EXPR_LIST chain.  For small bitsizes, bitsize is encoded
2863      in mode of the EXPR_LIST node and first EXPR_LIST operand
2864      is either NOTE_INSN_VAR_LOCATION for a piece with a known
2865      location or NULL for padding.  For larger bitsizes,
2866      mode is 0 and first operand is a CONCAT with bitsize
2867      as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2868      NULL as second operand.  */
2869   rtx GTY (()) loc;
2870   const char * GTY (()) label;
2871   struct var_loc_node * GTY (()) next;
2872 };
2873
2874 /* Variable location list.  */
2875 struct GTY ((for_user)) var_loc_list_def {
2876   struct var_loc_node * GTY (()) first;
2877
2878   /* Pointer to the last but one or last element of the
2879      chained list.  If the list is empty, both first and
2880      last are NULL, if the list contains just one node
2881      or the last node certainly is not redundant, it points
2882      to the last node, otherwise points to the last but one.
2883      Do not mark it for GC because it is marked through the chain.  */
2884   struct var_loc_node * GTY ((skip ("%h"))) last;
2885
2886   /* Pointer to the last element before section switch,
2887      if NULL, either sections weren't switched or first
2888      is after section switch.  */
2889   struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2890
2891   /* DECL_UID of the variable decl.  */
2892   unsigned int decl_id;
2893 };
2894 typedef struct var_loc_list_def var_loc_list;
2895
2896 /* Call argument location list.  */
2897 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2898   rtx GTY (()) call_arg_loc_note;
2899   const char * GTY (()) label;
2900   tree GTY (()) block;
2901   bool tail_call_p;
2902   rtx GTY (()) symbol_ref;
2903   struct call_arg_loc_node * GTY (()) next;
2904 };
2905
2906
2907 struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
2908 {
2909   typedef const_tree compare_type;
2910
2911   static hashval_t hash (var_loc_list *);
2912   static bool equal (var_loc_list *, const_tree);
2913 };
2914
2915 /* Table of decl location linked lists.  */
2916 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
2917
2918 /* Head and tail of call_arg_loc chain.  */
2919 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2920 static struct call_arg_loc_node *call_arg_loc_last;
2921
2922 /* Number of call sites in the current function.  */
2923 static int call_site_count = -1;
2924 /* Number of tail call sites in the current function.  */
2925 static int tail_call_site_count = -1;
2926
2927 /* A cached location list.  */
2928 struct GTY ((for_user)) cached_dw_loc_list_def {
2929   /* The DECL_UID of the decl that this entry describes.  */
2930   unsigned int decl_id;
2931
2932   /* The cached location list.  */
2933   dw_loc_list_ref loc_list;
2934 };
2935 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2936
2937 struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
2938 {
2939
2940   typedef const_tree compare_type;
2941   
2942   static hashval_t hash (cached_dw_loc_list *);
2943   static bool equal (cached_dw_loc_list *, const_tree);
2944 };
2945
2946 /* Table of cached location lists.  */
2947 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
2948
2949 /* A pointer to the base of a list of references to DIE's that
2950    are uniquely identified by their tag, presence/absence of
2951    children DIE's, and list of attribute/value pairs.  */
2952 static GTY((length ("abbrev_die_table_allocated")))
2953   dw_die_ref *abbrev_die_table;
2954
2955 /* Number of elements currently allocated for abbrev_die_table.  */
2956 static GTY(()) unsigned abbrev_die_table_allocated;
2957
2958 /* Number of elements in abbrev_die_table currently in use.  */
2959 static GTY(()) unsigned abbrev_die_table_in_use;
2960
2961 /* Size (in elements) of increments by which we may expand the
2962    abbrev_die_table.  */
2963 #define ABBREV_DIE_TABLE_INCREMENT 256
2964
2965 /* A global counter for generating labels for line number data.  */
2966 static unsigned int line_info_label_num;
2967
2968 /* The current table to which we should emit line number information
2969    for the current function.  This will be set up at the beginning of
2970    assembly for the function.  */
2971 static dw_line_info_table *cur_line_info_table;
2972
2973 /* The two default tables of line number info.  */
2974 static GTY(()) dw_line_info_table *text_section_line_info;
2975 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2976
2977 /* The set of all non-default tables of line number info.  */
2978 static GTY(()) vec<dw_line_info_table *, va_gc> *separate_line_info;
2979
2980 /* A flag to tell pubnames/types export if there is an info section to
2981    refer to.  */
2982 static bool info_section_emitted;
2983
2984 /* A pointer to the base of a table that contains a list of publicly
2985    accessible names.  */
2986 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
2987
2988 /* A pointer to the base of a table that contains a list of publicly
2989    accessible types.  */
2990 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
2991
2992 /* A pointer to the base of a table that contains a list of macro
2993    defines/undefines (and file start/end markers).  */
2994 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
2995
2996 /* True if .debug_macinfo or .debug_macros section is going to be
2997    emitted.  */
2998 #define have_macinfo \
2999   (debug_info_level >= DINFO_LEVEL_VERBOSE \
3000    && !macinfo_table->is_empty ())
3001
3002 /* Array of dies for which we should generate .debug_ranges info.  */
3003 static GTY ((length ("ranges_table_allocated"))) dw_ranges *ranges_table;
3004
3005 /* Number of elements currently allocated for ranges_table.  */
3006 static GTY(()) unsigned ranges_table_allocated;
3007
3008 /* Number of elements in ranges_table currently in use.  */
3009 static GTY(()) unsigned ranges_table_in_use;
3010
3011 /* Array of pairs of labels referenced in ranges_table.  */
3012 static GTY ((length ("ranges_by_label_allocated")))
3013      dw_ranges_by_label *ranges_by_label;
3014
3015 /* Number of elements currently allocated for ranges_by_label.  */
3016 static GTY(()) unsigned ranges_by_label_allocated;
3017
3018 /* Number of elements in ranges_by_label currently in use.  */
3019 static GTY(()) unsigned ranges_by_label_in_use;
3020
3021 /* Size (in elements) of increments by which we may expand the
3022    ranges_table.  */
3023 #define RANGES_TABLE_INCREMENT 64
3024
3025 /* Whether we have location lists that need outputting */
3026 static GTY(()) bool have_location_lists;
3027
3028 /* Unique label counter.  */
3029 static GTY(()) unsigned int loclabel_num;
3030
3031 /* Unique label counter for point-of-call tables.  */
3032 static GTY(()) unsigned int poc_label_num;
3033
3034 /* The last file entry emitted by maybe_emit_file().  */
3035 static GTY(()) struct dwarf_file_data * last_emitted_file;
3036
3037 /* Number of internal labels generated by gen_internal_sym().  */
3038 static GTY(()) int label_num;
3039
3040 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3041
3042 /* Instances of generic types for which we need to generate debug
3043    info that describe their generic parameters and arguments. That
3044    generation needs to happen once all types are properly laid out so
3045    we do it at the end of compilation.  */
3046 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3047
3048 /* Offset from the "steady-state frame pointer" to the frame base,
3049    within the current function.  */
3050 static HOST_WIDE_INT frame_pointer_fb_offset;
3051 static bool frame_pointer_fb_offset_valid;
3052
3053 static vec<dw_die_ref> base_types;
3054
3055 /* Flags to represent a set of attribute classes for attributes that represent
3056    a scalar value (bounds, pointers, ...).  */
3057 enum dw_scalar_form
3058 {
3059   dw_scalar_form_constant = 0x01,
3060   dw_scalar_form_exprloc = 0x02,
3061   dw_scalar_form_reference = 0x04
3062 };
3063
3064 /* Forward declarations for functions defined in this file.  */
3065
3066 static int is_pseudo_reg (const_rtx);
3067 static tree type_main_variant (tree);
3068 static int is_tagged_type (const_tree);
3069 static const char *dwarf_tag_name (unsigned);
3070 static const char *dwarf_attr_name (unsigned);
3071 static const char *dwarf_form_name (unsigned);
3072 static tree decl_ultimate_origin (const_tree);
3073 static tree decl_class_context (tree);
3074 static void add_dwarf_attr (dw_die_ref, dw_attr_node *);
3075 static inline enum dw_val_class AT_class (dw_attr_node *);
3076 static inline unsigned int AT_index (dw_attr_node *);
3077 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3078 static inline unsigned AT_flag (dw_attr_node *);
3079 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3080 static inline HOST_WIDE_INT AT_int (dw_attr_node *);
3081 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3082 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_node *);
3083 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3084                            HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3085 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3086                                unsigned int, unsigned char *);
3087 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3088 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3089 static inline const char *AT_string (dw_attr_node *);
3090 static enum dwarf_form AT_string_form (dw_attr_node *);
3091 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3092 static void add_AT_specification (dw_die_ref, dw_die_ref);
3093 static inline dw_die_ref AT_ref (dw_attr_node *);
3094 static inline int AT_ref_external (dw_attr_node *);
3095 static inline void set_AT_ref_external (dw_attr_node *, int);
3096 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3097 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3098 static inline dw_loc_descr_ref AT_loc (dw_attr_node *);
3099 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3100                              dw_loc_list_ref);
3101 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3102 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3103 static void remove_addr_table_entry (addr_table_entry *);
3104 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3105 static inline rtx AT_addr (dw_attr_node *);
3106 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3107 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3108 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3109 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3110                            unsigned HOST_WIDE_INT);
3111 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3112                                unsigned long, bool);
3113 static inline const char *AT_lbl (dw_attr_node *);
3114 static dw_attr_node *get_AT (dw_die_ref, enum dwarf_attribute);
3115 static const char *get_AT_low_pc (dw_die_ref);
3116 static const char *get_AT_hi_pc (dw_die_ref);
3117 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3118 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3119 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3120 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3121 static bool is_cxx (void);
3122 static bool is_fortran (void);
3123 static bool is_ada (void);
3124 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3125 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3126 static void add_child_die (dw_die_ref, dw_die_ref);
3127 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3128 static dw_die_ref lookup_type_die (tree);
3129 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3130 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3131 static void equate_type_number_to_die (tree, dw_die_ref);
3132 static dw_die_ref lookup_decl_die (tree);
3133 static var_loc_list *lookup_decl_loc (const_tree);
3134 static void equate_decl_number_to_die (tree, dw_die_ref);
3135 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3136 static void print_spaces (FILE *);
3137 static void print_die (dw_die_ref, FILE *);
3138 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3139 static dw_die_ref pop_compile_unit (dw_die_ref);
3140 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3141 static void attr_checksum (dw_attr_node *, struct md5_ctx *, int *);
3142 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3143 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3144 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3145 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3146 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_node *,
3147                                    struct md5_ctx *, int *);
3148 struct checksum_attributes;
3149 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3150 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3151 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3152 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3153 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3154 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3155 static int same_attr_p (dw_attr_node *, dw_attr_node *, int *);
3156 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3157 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3158 static void compute_section_prefix (dw_die_ref);
3159 static int is_type_die (dw_die_ref);
3160 static int is_comdat_die (dw_die_ref);
3161 static int is_symbol_die (dw_die_ref);
3162 static inline bool is_template_instantiation (dw_die_ref);
3163 static void assign_symbol_names (dw_die_ref);
3164 static void break_out_includes (dw_die_ref);
3165 static int is_declaration_die (dw_die_ref);
3166 static int should_move_die_to_comdat (dw_die_ref);
3167 static dw_die_ref clone_as_declaration (dw_die_ref);
3168 static dw_die_ref clone_die (dw_die_ref);
3169 static dw_die_ref clone_tree (dw_die_ref);
3170 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3171 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3172 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3173 static dw_die_ref generate_skeleton (dw_die_ref);
3174 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3175                                                          dw_die_ref,
3176                                                          dw_die_ref);
3177 static void break_out_comdat_types (dw_die_ref);
3178 static void copy_decls_for_unworthy_types (dw_die_ref);
3179
3180 static void add_sibling_attributes (dw_die_ref);
3181 static void output_location_lists (dw_die_ref);
3182 static int constant_size (unsigned HOST_WIDE_INT);
3183 static unsigned long size_of_die (dw_die_ref);
3184 static void calc_die_sizes (dw_die_ref);
3185 static void calc_base_type_die_sizes (void);
3186 static void mark_dies (dw_die_ref);
3187 static void unmark_dies (dw_die_ref);
3188 static void unmark_all_dies (dw_die_ref);
3189 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3190 static unsigned long size_of_aranges (void);
3191 static enum dwarf_form value_format (dw_attr_node *);
3192 static void output_value_format (dw_attr_node *);
3193 static void output_abbrev_section (void);
3194 static void output_die_abbrevs (unsigned long, dw_die_ref);
3195 static void output_die_symbol (dw_die_ref);
3196 static void output_die (dw_die_ref);
3197 static void output_compilation_unit_header (void);
3198 static void output_comp_unit (dw_die_ref, int);
3199 static void output_comdat_type_unit (comdat_type_node *);
3200 static const char *dwarf2_name (tree, int);
3201 static void add_pubname (tree, dw_die_ref);
3202 static void add_enumerator_pubname (const char *, dw_die_ref);
3203 static void add_pubname_string (const char *, dw_die_ref);
3204 static void add_pubtype (tree, dw_die_ref);
3205 static void output_pubnames (vec<pubname_entry, va_gc> *);
3206 static void output_aranges (unsigned long);
3207 static unsigned int add_ranges_num (int);
3208 static unsigned int add_ranges (const_tree);
3209 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3210                                   bool *, bool);
3211 static void output_ranges (void);
3212 static dw_line_info_table *new_line_info_table (void);
3213 static void output_line_info (bool);
3214 static void output_file_names (void);
3215 static dw_die_ref base_type_die (tree);
3216 static int is_base_type (tree);
3217 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3218 static int decl_quals (const_tree);
3219 static dw_die_ref modified_type_die (tree, int, dw_die_ref);
3220 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3221 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3222 static int type_is_enum (const_tree);
3223 static unsigned int dbx_reg_number (const_rtx);
3224 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3225 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3226 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3227                                                 enum var_init_status);
3228 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3229                                                      enum var_init_status);
3230 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3231                                          enum var_init_status);
3232 static int is_based_loc (const_rtx);
3233 static bool resolve_one_addr (rtx *);
3234 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3235                                                enum var_init_status);
3236 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3237                                         enum var_init_status);
3238 struct loc_descr_context;
3239 static dw_loc_list_ref loc_list_from_tree (tree, int,
3240                                            const struct loc_descr_context *);
3241 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3242                                                   const struct loc_descr_context *);
3243 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3244 static tree field_type (const_tree);
3245 static unsigned int simple_type_align_in_bits (const_tree);
3246 static unsigned int simple_decl_align_in_bits (const_tree);
3247 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3248 static HOST_WIDE_INT field_byte_offset (const_tree);
3249 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3250                                          dw_loc_list_ref);
3251 static void add_data_member_location_attribute (dw_die_ref, tree);
3252 static bool add_const_value_attribute (dw_die_ref, rtx);
3253 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3254 static void insert_wide_int (const wide_int &, unsigned char *, int);
3255 static void insert_float (const_rtx, unsigned char *);
3256 static rtx rtl_for_decl_location (tree);
3257 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3258                                                    enum dwarf_attribute);
3259 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3260 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3261 static void add_name_attribute (dw_die_ref, const char *);
3262 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3263 static void add_comp_dir_attribute (dw_die_ref);
3264 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3265                              const struct loc_descr_context *);
3266 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3267                             const struct loc_descr_context *);
3268 static void add_subscript_info (dw_die_ref, tree, bool);
3269 static void add_byte_size_attribute (dw_die_ref, tree);
3270 static void add_bit_offset_attribute (dw_die_ref, tree);
3271 static void add_bit_size_attribute (dw_die_ref, tree);
3272 static void add_prototyped_attribute (dw_die_ref, tree);
3273 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3274 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3275 static void add_src_coords_attributes (dw_die_ref, tree);
3276 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3277 static void push_decl_scope (tree);
3278 static void pop_decl_scope (void);
3279 static dw_die_ref scope_die_for (tree, dw_die_ref);
3280 static inline int local_scope_p (dw_die_ref);
3281 static inline int class_scope_p (dw_die_ref);
3282 static inline int class_or_namespace_scope_p (dw_die_ref);
3283 static void add_type_attribute (dw_die_ref, tree, int, dw_die_ref);
3284 static void add_calling_convention_attribute (dw_die_ref, tree);
3285 static const char *type_tag (const_tree);
3286 static tree member_declared_type (const_tree);
3287 #if 0
3288 static const char *decl_start_label (tree);
3289 #endif
3290 static void gen_array_type_die (tree, dw_die_ref);
3291 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3292 #if 0
3293 static void gen_entry_point_die (tree, dw_die_ref);
3294 #endif
3295 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3296 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3297 static dw_die_ref gen_formal_parameter_pack_die  (tree, tree, dw_die_ref, tree*);
3298 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3299 static void gen_formal_types_die (tree, dw_die_ref);
3300 static void gen_subprogram_die (tree, dw_die_ref);
3301 static void gen_variable_die (tree, tree, dw_die_ref);
3302 static void gen_const_die (tree, dw_die_ref);
3303 static void gen_label_die (tree, dw_die_ref);
3304 static void gen_lexical_block_die (tree, dw_die_ref);
3305 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3306 static void gen_field_die (tree, dw_die_ref);
3307 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3308 static dw_die_ref gen_compile_unit_die (const char *);
3309 static void gen_inheritance_die (tree, tree, dw_die_ref);
3310 static void gen_member_die (tree, dw_die_ref);
3311 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3312                                                 enum debug_info_usage);
3313 static void gen_subroutine_type_die (tree, dw_die_ref);
3314 static void gen_typedef_die (tree, dw_die_ref);
3315 static void gen_type_die (tree, dw_die_ref);
3316 static void gen_block_die (tree, dw_die_ref);
3317 static void decls_for_scope (tree, dw_die_ref);
3318 static inline int is_redundant_typedef (const_tree);
3319 static bool is_naming_typedef_decl (const_tree);
3320 static inline dw_die_ref get_context_die (tree);
3321 static void gen_namespace_die (tree, dw_die_ref);
3322 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3323 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3324 static dw_die_ref force_decl_die (tree);
3325 static dw_die_ref force_type_die (tree);
3326 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3327 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3328 static struct dwarf_file_data * lookup_filename (const char *);
3329 static void retry_incomplete_types (void);
3330 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3331 static void gen_generic_params_dies (tree);
3332 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3333 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3334 static void splice_child_die (dw_die_ref, dw_die_ref);
3335 static int file_info_cmp (const void *, const void *);
3336 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3337                                      const char *, const char *);
3338 static void output_loc_list (dw_loc_list_ref);
3339 static char *gen_internal_sym (const char *);
3340 static bool want_pubnames (void);
3341
3342 static void prune_unmark_dies (dw_die_ref);
3343 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3344 static void prune_unused_types_mark (dw_die_ref, int);
3345 static void prune_unused_types_walk (dw_die_ref);
3346 static void prune_unused_types_walk_attribs (dw_die_ref);
3347 static void prune_unused_types_prune (dw_die_ref);
3348 static void prune_unused_types (void);
3349 static int maybe_emit_file (struct dwarf_file_data *fd);
3350 static inline const char *AT_vms_delta1 (dw_attr_node *);
3351 static inline const char *AT_vms_delta2 (dw_attr_node *);
3352 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3353                                      const char *, const char *);
3354 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3355 static void gen_remaining_tmpl_value_param_die_attribute (void);
3356 static bool generic_type_p (tree);
3357 static void schedule_generic_params_dies_gen (tree t);
3358 static void gen_scheduled_generic_parms_dies (void);
3359
3360 static const char *comp_dir_string (void);
3361
3362 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3363
3364 /* enum for tracking thread-local variables whose address is really an offset
3365    relative to the TLS pointer, which will need link-time relocation, but will
3366    not need relocation by the DWARF consumer.  */
3367
3368 enum dtprel_bool
3369 {
3370   dtprel_false = 0,
3371   dtprel_true = 1
3372 };
3373
3374 /* Return the operator to use for an address of a variable.  For dtprel_true, we
3375    use DW_OP_const*.  For regular variables, which need both link-time
3376    relocation and consumer-level relocation (e.g., to account for shared objects
3377    loaded at a random address), we use DW_OP_addr*.  */
3378
3379 static inline enum dwarf_location_atom
3380 dw_addr_op (enum dtprel_bool dtprel)
3381 {
3382   if (dtprel == dtprel_true)
3383     return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3384             : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3385   else
3386     return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3387 }
3388
3389 /* Return a pointer to a newly allocated address location description.  If
3390    dwarf_split_debug_info is true, then record the address with the appropriate
3391    relocation.  */
3392 static inline dw_loc_descr_ref
3393 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3394 {
3395   dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3396
3397   ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3398   ref->dw_loc_oprnd1.v.val_addr = addr;
3399   ref->dtprel = dtprel;
3400   if (dwarf_split_debug_info)
3401     ref->dw_loc_oprnd1.val_entry
3402         = add_addr_table_entry (addr,
3403                                 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3404   else
3405     ref->dw_loc_oprnd1.val_entry = NULL;
3406
3407   return ref;
3408 }
3409
3410 /* Section names used to hold DWARF debugging information.  */
3411
3412 #ifndef DEBUG_INFO_SECTION
3413 #define DEBUG_INFO_SECTION      ".debug_info"
3414 #endif
3415 #ifndef DEBUG_DWO_INFO_SECTION
3416 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3417 #endif
3418 #ifndef DEBUG_ABBREV_SECTION
3419 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
3420 #endif
3421 #ifndef DEBUG_DWO_ABBREV_SECTION
3422 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3423 #endif
3424 #ifndef DEBUG_ARANGES_SECTION
3425 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
3426 #endif
3427 #ifndef DEBUG_ADDR_SECTION
3428 #define DEBUG_ADDR_SECTION     ".debug_addr"
3429 #endif
3430 #ifndef DEBUG_NORM_MACINFO_SECTION
3431 #define DEBUG_NORM_MACINFO_SECTION     ".debug_macinfo"
3432 #endif
3433 #ifndef DEBUG_DWO_MACINFO_SECTION
3434 #define DEBUG_DWO_MACINFO_SECTION      ".debug_macinfo.dwo"
3435 #endif
3436 #ifndef DEBUG_MACINFO_SECTION
3437 #define DEBUG_MACINFO_SECTION                                           \
3438   (!dwarf_split_debug_info                                              \
3439    ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3440 #endif
3441 #ifndef DEBUG_NORM_MACRO_SECTION
3442 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3443 #endif
3444 #ifndef DEBUG_DWO_MACRO_SECTION
3445 #define DEBUG_DWO_MACRO_SECTION        ".debug_macro.dwo"
3446 #endif
3447 #ifndef DEBUG_MACRO_SECTION
3448 #define DEBUG_MACRO_SECTION                                             \
3449   (!dwarf_split_debug_info                                              \
3450    ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3451 #endif
3452 #ifndef DEBUG_LINE_SECTION
3453 #define DEBUG_LINE_SECTION      ".debug_line"
3454 #endif
3455 #ifndef DEBUG_DWO_LINE_SECTION
3456 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3457 #endif
3458 #ifndef DEBUG_LOC_SECTION
3459 #define DEBUG_LOC_SECTION       ".debug_loc"
3460 #endif
3461 #ifndef DEBUG_DWO_LOC_SECTION
3462 #define DEBUG_DWO_LOC_SECTION  ".debug_loc.dwo"
3463 #endif
3464 #ifndef DEBUG_PUBNAMES_SECTION
3465 #define DEBUG_PUBNAMES_SECTION  \
3466   ((debug_generate_pub_sections == 2) \
3467    ? ".debug_gnu_pubnames" : ".debug_pubnames")
3468 #endif
3469 #ifndef DEBUG_PUBTYPES_SECTION
3470 #define DEBUG_PUBTYPES_SECTION  \
3471   ((debug_generate_pub_sections == 2) \
3472    ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3473 #endif
3474 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3475 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3476 #ifndef DEBUG_STR_OFFSETS_SECTION
3477 #define DEBUG_STR_OFFSETS_SECTION                                       \
3478   (!dwarf_split_debug_info                                              \
3479    ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3480 #endif
3481 #ifndef DEBUG_STR_DWO_SECTION
3482 #define DEBUG_STR_DWO_SECTION   ".debug_str.dwo"
3483 #endif
3484 #ifndef DEBUG_STR_SECTION
3485 #define DEBUG_STR_SECTION  ".debug_str"
3486 #endif
3487 #ifndef DEBUG_RANGES_SECTION
3488 #define DEBUG_RANGES_SECTION    ".debug_ranges"
3489 #endif
3490
3491 /* Standard ELF section names for compiled code and data.  */
3492 #ifndef TEXT_SECTION_NAME
3493 #define TEXT_SECTION_NAME       ".text"
3494 #endif
3495
3496 /* Section flags for .debug_macinfo/.debug_macro section.  */
3497 #define DEBUG_MACRO_SECTION_FLAGS                                       \
3498   (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3499
3500 /* Section flags for .debug_str section.  */
3501 #define DEBUG_STR_SECTION_FLAGS                                 \
3502   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
3503    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
3504    : SECTION_DEBUG)
3505
3506 /* Section flags for .debug_str.dwo section.  */
3507 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3508
3509 /* Labels we insert at beginning sections we can reference instead of
3510    the section names themselves.  */
3511
3512 #ifndef TEXT_SECTION_LABEL
3513 #define TEXT_SECTION_LABEL                 "Ltext"
3514 #endif
3515 #ifndef COLD_TEXT_SECTION_LABEL
3516 #define COLD_TEXT_SECTION_LABEL             "Ltext_cold"
3517 #endif
3518 #ifndef DEBUG_LINE_SECTION_LABEL
3519 #define DEBUG_LINE_SECTION_LABEL           "Ldebug_line"
3520 #endif
3521 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3522 #define DEBUG_SKELETON_LINE_SECTION_LABEL   "Lskeleton_debug_line"
3523 #endif
3524 #ifndef DEBUG_INFO_SECTION_LABEL
3525 #define DEBUG_INFO_SECTION_LABEL           "Ldebug_info"
3526 #endif
3527 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3528 #define DEBUG_SKELETON_INFO_SECTION_LABEL   "Lskeleton_debug_info"
3529 #endif
3530 #ifndef DEBUG_ABBREV_SECTION_LABEL
3531 #define DEBUG_ABBREV_SECTION_LABEL         "Ldebug_abbrev"
3532 #endif
3533 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3534 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3535 #endif
3536 #ifndef DEBUG_ADDR_SECTION_LABEL
3537 #define DEBUG_ADDR_SECTION_LABEL           "Ldebug_addr"
3538 #endif
3539 #ifndef DEBUG_LOC_SECTION_LABEL
3540 #define DEBUG_LOC_SECTION_LABEL                    "Ldebug_loc"
3541 #endif
3542 #ifndef DEBUG_RANGES_SECTION_LABEL
3543 #define DEBUG_RANGES_SECTION_LABEL         "Ldebug_ranges"
3544 #endif
3545 #ifndef DEBUG_MACINFO_SECTION_LABEL
3546 #define DEBUG_MACINFO_SECTION_LABEL         "Ldebug_macinfo"
3547 #endif
3548 #ifndef DEBUG_MACRO_SECTION_LABEL
3549 #define DEBUG_MACRO_SECTION_LABEL          "Ldebug_macro"
3550 #endif
3551 #define SKELETON_COMP_DIE_ABBREV 1
3552 #define SKELETON_TYPE_DIE_ABBREV 2
3553
3554 /* Definitions of defaults for formats and names of various special
3555    (artificial) labels which may be generated within this file (when the -g
3556    options is used and DWARF2_DEBUGGING_INFO is in effect.
3557    If necessary, these may be overridden from within the tm.h file, but
3558    typically, overriding these defaults is unnecessary.  */
3559
3560 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3561 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3562 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3563 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3564 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3565 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3566 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3567 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3568 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3569 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3570 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3571 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3572 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3573 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3574
3575 #ifndef TEXT_END_LABEL
3576 #define TEXT_END_LABEL          "Letext"
3577 #endif
3578 #ifndef COLD_END_LABEL
3579 #define COLD_END_LABEL          "Letext_cold"
3580 #endif
3581 #ifndef BLOCK_BEGIN_LABEL
3582 #define BLOCK_BEGIN_LABEL       "LBB"
3583 #endif
3584 #ifndef BLOCK_END_LABEL
3585 #define BLOCK_END_LABEL         "LBE"
3586 #endif
3587 #ifndef LINE_CODE_LABEL
3588 #define LINE_CODE_LABEL         "LM"
3589 #endif
3590
3591 \f
3592 /* Return the root of the DIE's built for the current compilation unit.  */
3593 static dw_die_ref
3594 comp_unit_die (void)
3595 {
3596   if (!single_comp_unit_die)
3597     single_comp_unit_die = gen_compile_unit_die (NULL);
3598   return single_comp_unit_die;
3599 }
3600
3601 /* We allow a language front-end to designate a function that is to be
3602    called to "demangle" any name before it is put into a DIE.  */
3603
3604 static const char *(*demangle_name_func) (const char *);
3605
3606 void
3607 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3608 {
3609   demangle_name_func = func;
3610 }
3611
3612 /* Test if rtl node points to a pseudo register.  */
3613
3614 static inline int
3615 is_pseudo_reg (const_rtx rtl)
3616 {
3617   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3618           || (GET_CODE (rtl) == SUBREG
3619               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3620 }
3621
3622 /* Return a reference to a type, with its const and volatile qualifiers
3623    removed.  */
3624
3625 static inline tree
3626 type_main_variant (tree type)
3627 {
3628   type = TYPE_MAIN_VARIANT (type);
3629
3630   /* ??? There really should be only one main variant among any group of
3631      variants of a given type (and all of the MAIN_VARIANT values for all
3632      members of the group should point to that one type) but sometimes the C
3633      front-end messes this up for array types, so we work around that bug
3634      here.  */
3635   if (TREE_CODE (type) == ARRAY_TYPE)
3636     while (type != TYPE_MAIN_VARIANT (type))
3637       type = TYPE_MAIN_VARIANT (type);
3638
3639   return type;
3640 }
3641
3642 /* Return nonzero if the given type node represents a tagged type.  */
3643
3644 static inline int
3645 is_tagged_type (const_tree type)
3646 {
3647   enum tree_code code = TREE_CODE (type);
3648
3649   return (code == RECORD_TYPE || code == UNION_TYPE
3650           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3651 }
3652
3653 /* Set label to debug_info_section_label + die_offset of a DIE reference.  */
3654
3655 static void
3656 get_ref_die_offset_label (char *label, dw_die_ref ref)
3657 {
3658   sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3659 }
3660
3661 /* Return die_offset of a DIE reference to a base type.  */
3662
3663 static unsigned long int
3664 get_base_type_offset (dw_die_ref ref)
3665 {
3666   if (ref->die_offset)
3667     return ref->die_offset;
3668   if (comp_unit_die ()->die_abbrev)
3669     {
3670       calc_base_type_die_sizes ();
3671       gcc_assert (ref->die_offset);
3672     }
3673   return ref->die_offset;
3674 }
3675
3676 /* Return die_offset of a DIE reference other than base type.  */
3677
3678 static unsigned long int
3679 get_ref_die_offset (dw_die_ref ref)
3680 {
3681   gcc_assert (ref->die_offset);
3682   return ref->die_offset;
3683 }
3684
3685 /* Convert a DIE tag into its string name.  */
3686
3687 static const char *
3688 dwarf_tag_name (unsigned int tag)
3689 {
3690   const char *name = get_DW_TAG_name (tag);
3691
3692   if (name != NULL)
3693     return name;
3694
3695   return "DW_TAG_<unknown>";
3696 }
3697
3698 /* Convert a DWARF attribute code into its string name.  */
3699
3700 static const char *
3701 dwarf_attr_name (unsigned int attr)
3702 {
3703   const char *name;
3704
3705   switch (attr)
3706     {
3707 #if VMS_DEBUGGING_INFO
3708     case DW_AT_HP_prologue:
3709       return "DW_AT_HP_prologue";
3710 #else
3711     case DW_AT_MIPS_loop_unroll_factor:
3712       return "DW_AT_MIPS_loop_unroll_factor";
3713 #endif
3714
3715 #if VMS_DEBUGGING_INFO
3716     case DW_AT_HP_epilogue:
3717       return "DW_AT_HP_epilogue";
3718 #else
3719     case DW_AT_MIPS_stride:
3720       return "DW_AT_MIPS_stride";
3721 #endif
3722     }
3723
3724   name = get_DW_AT_name (attr);
3725
3726   if (name != NULL)
3727     return name;
3728
3729   return "DW_AT_<unknown>";
3730 }
3731
3732 /* Convert a DWARF value form code into its string name.  */
3733
3734 static const char *
3735 dwarf_form_name (unsigned int form)
3736 {
3737   const char *name = get_DW_FORM_name (form);
3738
3739   if (name != NULL)
3740     return name;
3741
3742   return "DW_FORM_<unknown>";
3743 }
3744 \f
3745 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
3746    instance of an inlined instance of a decl which is local to an inline
3747    function, so we have to trace all of the way back through the origin chain
3748    to find out what sort of node actually served as the original seed for the
3749    given block.  */
3750
3751 static tree
3752 decl_ultimate_origin (const_tree decl)
3753 {
3754   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3755     return NULL_TREE;
3756
3757   /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
3758      we're trying to output the abstract instance of this function.  */
3759   if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3760     return NULL_TREE;
3761
3762   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3763      most distant ancestor, this should never happen.  */
3764   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3765
3766   return DECL_ABSTRACT_ORIGIN (decl);
3767 }
3768
3769 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
3770    of a virtual function may refer to a base class, so we check the 'this'
3771    parameter.  */
3772
3773 static tree
3774 decl_class_context (tree decl)
3775 {
3776   tree context = NULL_TREE;
3777
3778   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3779     context = DECL_CONTEXT (decl);
3780   else
3781     context = TYPE_MAIN_VARIANT
3782       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3783
3784   if (context && !TYPE_P (context))
3785     context = NULL_TREE;
3786
3787   return context;
3788 }
3789 \f
3790 /* Add an attribute/value pair to a DIE.  */
3791
3792 static inline void
3793 add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
3794 {
3795   /* Maybe this should be an assert?  */
3796   if (die == NULL)
3797     return;
3798
3799   vec_safe_reserve (die->die_attr, 1);
3800   vec_safe_push (die->die_attr, *attr);
3801 }
3802
3803 static inline enum dw_val_class
3804 AT_class (dw_attr_node *a)
3805 {
3806   return a->dw_attr_val.val_class;
3807 }
3808
3809 /* Return the index for any attribute that will be referenced with a
3810    DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index.  String indices
3811    are stored in dw_attr_val.v.val_str for reference counting
3812    pruning.  */
3813
3814 static inline unsigned int
3815 AT_index (dw_attr_node *a)
3816 {
3817   if (AT_class (a) == dw_val_class_str)
3818     return a->dw_attr_val.v.val_str->index;
3819   else if (a->dw_attr_val.val_entry != NULL)
3820     return a->dw_attr_val.val_entry->index;
3821   return NOT_INDEXED;
3822 }
3823
3824 /* Add a flag value attribute to a DIE.  */
3825
3826 static inline void
3827 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3828 {
3829   dw_attr_node attr;
3830
3831   attr.dw_attr = attr_kind;
3832   attr.dw_attr_val.val_class = dw_val_class_flag;
3833   attr.dw_attr_val.val_entry = NULL;
3834   attr.dw_attr_val.v.val_flag = flag;
3835   add_dwarf_attr (die, &attr);
3836 }
3837
3838 static inline unsigned
3839 AT_flag (dw_attr_node *a)
3840 {
3841   gcc_assert (a && AT_class (a) == dw_val_class_flag);
3842   return a->dw_attr_val.v.val_flag;
3843 }
3844
3845 /* Add a signed integer attribute value to a DIE.  */
3846
3847 static inline void
3848 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3849 {
3850   dw_attr_node attr;
3851
3852   attr.dw_attr = attr_kind;
3853   attr.dw_attr_val.val_class = dw_val_class_const;
3854   attr.dw_attr_val.val_entry = NULL;
3855   attr.dw_attr_val.v.val_int = int_val;
3856   add_dwarf_attr (die, &attr);
3857 }
3858
3859 static inline HOST_WIDE_INT
3860 AT_int (dw_attr_node *a)
3861 {
3862   gcc_assert (a && AT_class (a) == dw_val_class_const);
3863   return a->dw_attr_val.v.val_int;
3864 }
3865
3866 /* Add an unsigned integer attribute value to a DIE.  */
3867
3868 static inline void
3869 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3870                  unsigned HOST_WIDE_INT unsigned_val)
3871 {
3872   dw_attr_node attr;
3873
3874   attr.dw_attr = attr_kind;
3875   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3876   attr.dw_attr_val.val_entry = NULL;
3877   attr.dw_attr_val.v.val_unsigned = unsigned_val;
3878   add_dwarf_attr (die, &attr);
3879 }
3880
3881 static inline unsigned HOST_WIDE_INT
3882 AT_unsigned (dw_attr_node *a)
3883 {
3884   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3885   return a->dw_attr_val.v.val_unsigned;
3886 }
3887
3888 /* Add an unsigned wide integer attribute value to a DIE.  */
3889
3890 static inline void
3891 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
3892              const wide_int& w)
3893 {
3894   dw_attr_node attr;
3895
3896   attr.dw_attr = attr_kind;
3897   attr.dw_attr_val.val_class = dw_val_class_wide_int;
3898   attr.dw_attr_val.val_entry = NULL;
3899   attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
3900   *attr.dw_attr_val.v.val_wide = w;
3901   add_dwarf_attr (die, &attr);
3902 }
3903
3904 /* Add an unsigned double integer attribute value to a DIE.  */
3905
3906 static inline void
3907 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3908                HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3909 {
3910   dw_attr_node attr;
3911
3912   attr.dw_attr = attr_kind;
3913   attr.dw_attr_val.val_class = dw_val_class_const_double;
3914   attr.dw_attr_val.val_entry = NULL;
3915   attr.dw_attr_val.v.val_double.high = high;
3916   attr.dw_attr_val.v.val_double.low = low;
3917   add_dwarf_attr (die, &attr);
3918 }
3919
3920 /* Add a floating point attribute value to a DIE and return it.  */
3921
3922 static inline void
3923 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3924             unsigned int length, unsigned int elt_size, unsigned char *array)
3925 {
3926   dw_attr_node attr;
3927
3928   attr.dw_attr = attr_kind;
3929   attr.dw_attr_val.val_class = dw_val_class_vec;
3930   attr.dw_attr_val.val_entry = NULL;
3931   attr.dw_attr_val.v.val_vec.length = length;
3932   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3933   attr.dw_attr_val.v.val_vec.array = array;
3934   add_dwarf_attr (die, &attr);
3935 }
3936
3937 /* Add an 8-byte data attribute value to a DIE.  */
3938
3939 static inline void
3940 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3941               unsigned char data8[8])
3942 {
3943   dw_attr_node attr;
3944
3945   attr.dw_attr = attr_kind;
3946   attr.dw_attr_val.val_class = dw_val_class_data8;
3947   attr.dw_attr_val.val_entry = NULL;
3948   memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3949   add_dwarf_attr (die, &attr);
3950 }
3951
3952 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE.  When using
3953    dwarf_split_debug_info, address attributes in dies destined for the
3954    final executable have force_direct set to avoid using indexed
3955    references.  */
3956
3957 static inline void
3958 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3959                     bool force_direct)
3960 {
3961   dw_attr_node attr;
3962   char * lbl_id;
3963
3964   lbl_id = xstrdup (lbl_low);
3965   attr.dw_attr = DW_AT_low_pc;
3966   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3967   attr.dw_attr_val.v.val_lbl_id = lbl_id;
3968   if (dwarf_split_debug_info && !force_direct)
3969     attr.dw_attr_val.val_entry
3970         = add_addr_table_entry (lbl_id, ate_kind_label);
3971   else
3972     attr.dw_attr_val.val_entry = NULL;
3973   add_dwarf_attr (die, &attr);
3974
3975   attr.dw_attr = DW_AT_high_pc;
3976   if (dwarf_version < 4)
3977     attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3978   else
3979     attr.dw_attr_val.val_class = dw_val_class_high_pc;
3980   lbl_id = xstrdup (lbl_high);
3981   attr.dw_attr_val.v.val_lbl_id = lbl_id;
3982   if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3983       && dwarf_split_debug_info && !force_direct)
3984     attr.dw_attr_val.val_entry
3985         = add_addr_table_entry (lbl_id, ate_kind_label);
3986   else
3987     attr.dw_attr_val.val_entry = NULL;
3988   add_dwarf_attr (die, &attr);
3989 }
3990
3991 /* Hash and equality functions for debug_str_hash.  */
3992
3993 hashval_t
3994 indirect_string_hasher::hash (indirect_string_node *x)
3995 {
3996   return htab_hash_string (x->str);
3997 }
3998
3999 bool
4000 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4001 {
4002   return strcmp (x1->str, x2) == 0;
4003 }
4004
4005 /* Add STR to the given string hash table.  */
4006
4007 static struct indirect_string_node *
4008 find_AT_string_in_table (const char *str,
4009                          hash_table<indirect_string_hasher> *table)
4010 {
4011   struct indirect_string_node *node;
4012
4013   indirect_string_node **slot
4014     = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
4015   if (*slot == NULL)
4016     {
4017       node = ggc_cleared_alloc<indirect_string_node> ();
4018       node->str = ggc_strdup (str);
4019       *slot = node;
4020     }
4021   else
4022     node = *slot;
4023
4024   node->refcount++;
4025   return node;
4026 }
4027
4028 /* Add STR to the indirect string hash table.  */
4029
4030 static struct indirect_string_node *
4031 find_AT_string (const char *str)
4032 {
4033   if (! debug_str_hash)
4034     debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4035
4036   return find_AT_string_in_table (str, debug_str_hash);
4037 }
4038
4039 /* Add a string attribute value to a DIE.  */
4040
4041 static inline void
4042 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4043 {
4044   dw_attr_node attr;
4045   struct indirect_string_node *node;
4046
4047   node = find_AT_string (str);
4048
4049   attr.dw_attr = attr_kind;
4050   attr.dw_attr_val.val_class = dw_val_class_str;
4051   attr.dw_attr_val.val_entry = NULL;
4052   attr.dw_attr_val.v.val_str = node;
4053   add_dwarf_attr (die, &attr);
4054 }
4055
4056 static inline const char *
4057 AT_string (dw_attr_node *a)
4058 {
4059   gcc_assert (a && AT_class (a) == dw_val_class_str);
4060   return a->dw_attr_val.v.val_str->str;
4061 }
4062
4063 /* Call this function directly to bypass AT_string_form's logic to put
4064    the string inline in the die. */
4065
4066 static void
4067 set_indirect_string (struct indirect_string_node *node)
4068 {
4069   char label[32];
4070   /* Already indirect is a no op.  */
4071   if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
4072     {
4073       gcc_assert (node->label);
4074       return;
4075     }
4076   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4077   ++dw2_string_counter;
4078   node->label = xstrdup (label);
4079
4080   if (!dwarf_split_debug_info)
4081     {
4082       node->form = DW_FORM_strp;
4083       node->index = NOT_INDEXED;
4084     }
4085   else
4086     {
4087       node->form = DW_FORM_GNU_str_index;
4088       node->index = NO_INDEX_ASSIGNED;
4089     }
4090 }
4091
4092 /* Find out whether a string should be output inline in DIE
4093    or out-of-line in .debug_str section.  */
4094
4095 static enum dwarf_form
4096 find_string_form (struct indirect_string_node *node)
4097 {
4098   unsigned int len;
4099
4100   if (node->form)
4101     return node->form;
4102
4103   len = strlen (node->str) + 1;
4104
4105   /* If the string is shorter or equal to the size of the reference, it is
4106      always better to put it inline.  */
4107   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4108     return node->form = DW_FORM_string;
4109
4110   /* If we cannot expect the linker to merge strings in .debug_str
4111      section, only put it into .debug_str if it is worth even in this
4112      single module.  */
4113   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4114       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4115       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4116     return node->form = DW_FORM_string;
4117
4118   set_indirect_string (node);
4119
4120   return node->form;
4121 }
4122
4123 /* Find out whether the string referenced from the attribute should be
4124    output inline in DIE or out-of-line in .debug_str section.  */
4125
4126 static enum dwarf_form
4127 AT_string_form (dw_attr_node *a)
4128 {
4129   gcc_assert (a && AT_class (a) == dw_val_class_str);
4130   return find_string_form (a->dw_attr_val.v.val_str);
4131 }
4132
4133 /* Add a DIE reference attribute value to a DIE.  */
4134
4135 static inline void
4136 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4137 {
4138   dw_attr_node attr;
4139
4140 #ifdef ENABLE_CHECKING
4141   gcc_assert (targ_die != NULL);
4142 #else
4143   /* With LTO we can end up trying to reference something we didn't create
4144      a DIE for.  Avoid crashing later on a NULL referenced DIE.  */
4145   if (targ_die == NULL)
4146     return;
4147 #endif
4148
4149   attr.dw_attr = attr_kind;
4150   attr.dw_attr_val.val_class = dw_val_class_die_ref;
4151   attr.dw_attr_val.val_entry = NULL;
4152   attr.dw_attr_val.v.val_die_ref.die = targ_die;
4153   attr.dw_attr_val.v.val_die_ref.external = 0;
4154   add_dwarf_attr (die, &attr);
4155 }
4156
4157 /* Change DIE reference REF to point to NEW_DIE instead.  */
4158
4159 static inline void
4160 change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4161 {
4162   gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4163   ref->dw_attr_val.v.val_die_ref.die = new_die;
4164   ref->dw_attr_val.v.val_die_ref.external = 0;
4165 }
4166
4167 /* Add an AT_specification attribute to a DIE, and also make the back
4168    pointer from the specification to the definition.  */
4169
4170 static inline void
4171 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4172 {
4173   add_AT_die_ref (die, DW_AT_specification, targ_die);
4174   gcc_assert (!targ_die->die_definition);
4175   targ_die->die_definition = die;
4176 }
4177
4178 static inline dw_die_ref
4179 AT_ref (dw_attr_node *a)
4180 {
4181   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4182   return a->dw_attr_val.v.val_die_ref.die;
4183 }
4184
4185 static inline int
4186 AT_ref_external (dw_attr_node *a)
4187 {
4188   if (a && AT_class (a) == dw_val_class_die_ref)
4189     return a->dw_attr_val.v.val_die_ref.external;
4190
4191   return 0;
4192 }
4193
4194 static inline void
4195 set_AT_ref_external (dw_attr_node *a, int i)
4196 {
4197   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4198   a->dw_attr_val.v.val_die_ref.external = i;
4199 }
4200
4201 /* Add an FDE reference attribute value to a DIE.  */
4202
4203 static inline void
4204 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4205 {
4206   dw_attr_node attr;
4207
4208   attr.dw_attr = attr_kind;
4209   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4210   attr.dw_attr_val.val_entry = NULL;
4211   attr.dw_attr_val.v.val_fde_index = targ_fde;
4212   add_dwarf_attr (die, &attr);
4213 }
4214
4215 /* Add a location description attribute value to a DIE.  */
4216
4217 static inline void
4218 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4219 {
4220   dw_attr_node attr;
4221
4222   attr.dw_attr = attr_kind;
4223   attr.dw_attr_val.val_class = dw_val_class_loc;
4224   attr.dw_attr_val.val_entry = NULL;
4225   attr.dw_attr_val.v.val_loc = loc;
4226   add_dwarf_attr (die, &attr);
4227 }
4228
4229 static inline dw_loc_descr_ref
4230 AT_loc (dw_attr_node *a)
4231 {
4232   gcc_assert (a && AT_class (a) == dw_val_class_loc);
4233   return a->dw_attr_val.v.val_loc;
4234 }
4235
4236 static inline void
4237 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4238 {
4239   dw_attr_node attr;
4240
4241   attr.dw_attr = attr_kind;
4242   attr.dw_attr_val.val_class = dw_val_class_loc_list;
4243   attr.dw_attr_val.val_entry = NULL;
4244   attr.dw_attr_val.v.val_loc_list = loc_list;
4245   add_dwarf_attr (die, &attr);
4246   have_location_lists = true;
4247 }
4248
4249 static inline dw_loc_list_ref
4250 AT_loc_list (dw_attr_node *a)
4251 {
4252   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4253   return a->dw_attr_val.v.val_loc_list;
4254 }
4255
4256 static inline dw_loc_list_ref *
4257 AT_loc_list_ptr (dw_attr_node *a)
4258 {
4259   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4260   return &a->dw_attr_val.v.val_loc_list;
4261 }
4262
4263 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
4264 {
4265   static hashval_t hash (addr_table_entry *);
4266   static bool equal (addr_table_entry *, addr_table_entry *);
4267 };
4268
4269 /* Table of entries into the .debug_addr section.  */
4270
4271 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4272
4273 /* Hash an address_table_entry.  */
4274
4275 hashval_t
4276 addr_hasher::hash (addr_table_entry *a)
4277 {
4278   inchash::hash hstate;
4279   switch (a->kind)
4280     {
4281       case ate_kind_rtx:
4282         hstate.add_int (0);
4283         break;
4284       case ate_kind_rtx_dtprel:
4285         hstate.add_int (1);
4286         break;
4287       case ate_kind_label:
4288         return htab_hash_string (a->addr.label);
4289       default:
4290         gcc_unreachable ();
4291     }
4292   inchash::add_rtx (a->addr.rtl, hstate);
4293   return hstate.end ();
4294 }
4295
4296 /* Determine equality for two address_table_entries.  */
4297
4298 bool
4299 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4300 {
4301   if (a1->kind != a2->kind)
4302     return 0;
4303   switch (a1->kind)
4304     {
4305       case ate_kind_rtx:
4306       case ate_kind_rtx_dtprel:
4307         return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4308       case ate_kind_label:
4309         return strcmp (a1->addr.label, a2->addr.label) == 0;
4310       default:
4311         gcc_unreachable ();
4312     }
4313 }
4314
4315 /* Initialize an addr_table_entry.  */
4316
4317 void
4318 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4319 {
4320   e->kind = kind;
4321   switch (kind)
4322     {
4323       case ate_kind_rtx:
4324       case ate_kind_rtx_dtprel:
4325         e->addr.rtl = (rtx) addr;
4326         break;
4327       case ate_kind_label:
4328         e->addr.label = (char *) addr;
4329         break;
4330     }
4331   e->refcount = 0;
4332   e->index = NO_INDEX_ASSIGNED;
4333 }
4334
4335 /* Add attr to the address table entry to the table.  Defer setting an
4336    index until output time.  */
4337
4338 static addr_table_entry *
4339 add_addr_table_entry (void *addr, enum ate_kind kind)
4340 {
4341   addr_table_entry *node;
4342   addr_table_entry finder;
4343
4344   gcc_assert (dwarf_split_debug_info);
4345   if (! addr_index_table)
4346     addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4347   init_addr_table_entry (&finder, kind, addr);
4348   addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4349
4350   if (*slot == HTAB_EMPTY_ENTRY)
4351     {
4352       node = ggc_cleared_alloc<addr_table_entry> ();
4353       init_addr_table_entry (node, kind, addr);
4354       *slot = node;
4355     }
4356   else
4357     node = *slot;
4358
4359   node->refcount++;
4360   return node;
4361 }
4362
4363 /* Remove an entry from the addr table by decrementing its refcount.
4364    Strictly, decrementing the refcount would be enough, but the
4365    assertion that the entry is actually in the table has found
4366    bugs.  */
4367
4368 static void
4369 remove_addr_table_entry (addr_table_entry *entry)
4370 {
4371   gcc_assert (dwarf_split_debug_info && addr_index_table);
4372   /* After an index is assigned, the table is frozen.  */
4373   gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4374   entry->refcount--;
4375 }
4376
4377 /* Given a location list, remove all addresses it refers to from the
4378    address_table.  */
4379
4380 static void
4381 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4382 {
4383   for (; descr; descr = descr->dw_loc_next)
4384     if (descr->dw_loc_oprnd1.val_entry != NULL)
4385       {
4386         gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4387         remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4388       }
4389 }
4390
4391 /* A helper function for dwarf2out_finish called through
4392    htab_traverse.  Assign an addr_table_entry its index.  All entries
4393    must be collected into the table when this function is called,
4394    because the indexing code relies on htab_traverse to traverse nodes
4395    in the same order for each run. */
4396
4397 int
4398 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4399 {
4400   addr_table_entry *node = *h;
4401
4402   /* Don't index unreferenced nodes.  */
4403   if (node->refcount == 0)
4404     return 1;
4405
4406   gcc_assert (node->index == NO_INDEX_ASSIGNED);
4407   node->index = *index;
4408   *index += 1;
4409
4410   return 1;
4411 }
4412
4413 /* Add an address constant attribute value to a DIE.  When using
4414    dwarf_split_debug_info, address attributes in dies destined for the
4415    final executable should be direct references--setting the parameter
4416    force_direct ensures this behavior.  */
4417
4418 static inline void
4419 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4420              bool force_direct)
4421 {
4422   dw_attr_node attr;
4423
4424   attr.dw_attr = attr_kind;
4425   attr.dw_attr_val.val_class = dw_val_class_addr;
4426   attr.dw_attr_val.v.val_addr = addr;
4427   if (dwarf_split_debug_info && !force_direct)
4428     attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4429   else
4430     attr.dw_attr_val.val_entry = NULL;
4431   add_dwarf_attr (die, &attr);
4432 }
4433
4434 /* Get the RTX from to an address DIE attribute.  */
4435
4436 static inline rtx
4437 AT_addr (dw_attr_node *a)
4438 {
4439   gcc_assert (a && AT_class (a) == dw_val_class_addr);
4440   return a->dw_attr_val.v.val_addr;
4441 }
4442
4443 /* Add a file attribute value to a DIE.  */
4444
4445 static inline void
4446 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4447              struct dwarf_file_data *fd)
4448 {
4449   dw_attr_node attr;
4450
4451   attr.dw_attr = attr_kind;
4452   attr.dw_attr_val.val_class = dw_val_class_file;
4453   attr.dw_attr_val.val_entry = NULL;
4454   attr.dw_attr_val.v.val_file = fd;
4455   add_dwarf_attr (die, &attr);
4456 }
4457
4458 /* Get the dwarf_file_data from a file DIE attribute.  */
4459
4460 static inline struct dwarf_file_data *
4461 AT_file (dw_attr_node *a)
4462 {
4463   gcc_assert (a && AT_class (a) == dw_val_class_file);
4464   return a->dw_attr_val.v.val_file;
4465 }
4466
4467 /* Add a vms delta attribute value to a DIE.  */
4468
4469 static inline void
4470 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4471                   const char *lbl1, const char *lbl2)
4472 {
4473   dw_attr_node attr;
4474
4475   attr.dw_attr = attr_kind;
4476   attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4477   attr.dw_attr_val.val_entry = NULL;
4478   attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4479   attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4480   add_dwarf_attr (die, &attr);
4481 }
4482
4483 /* Add a label identifier attribute value to a DIE.  */
4484
4485 static inline void
4486 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4487                const char *lbl_id)
4488 {
4489   dw_attr_node attr;
4490
4491   attr.dw_attr = attr_kind;
4492   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4493   attr.dw_attr_val.val_entry = NULL;
4494   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4495   if (dwarf_split_debug_info)
4496     attr.dw_attr_val.val_entry
4497         = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4498                                 ate_kind_label);
4499   add_dwarf_attr (die, &attr);
4500 }
4501
4502 /* Add a section offset attribute value to a DIE, an offset into the
4503    debug_line section.  */
4504
4505 static inline void
4506 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4507                 const char *label)
4508 {
4509   dw_attr_node attr;
4510
4511   attr.dw_attr = attr_kind;
4512   attr.dw_attr_val.val_class = dw_val_class_lineptr;
4513   attr.dw_attr_val.val_entry = NULL;
4514   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4515   add_dwarf_attr (die, &attr);
4516 }
4517
4518 /* Add a section offset attribute value to a DIE, an offset into the
4519    debug_macinfo section.  */
4520
4521 static inline void
4522 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4523                const char *label)
4524 {
4525   dw_attr_node attr;
4526
4527   attr.dw_attr = attr_kind;
4528   attr.dw_attr_val.val_class = dw_val_class_macptr;
4529   attr.dw_attr_val.val_entry = NULL;
4530   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4531   add_dwarf_attr (die, &attr);
4532 }
4533
4534 /* Add an offset attribute value to a DIE.  */
4535
4536 static inline void
4537 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4538                unsigned HOST_WIDE_INT offset)
4539 {
4540   dw_attr_node attr;
4541
4542   attr.dw_attr = attr_kind;
4543   attr.dw_attr_val.val_class = dw_val_class_offset;
4544   attr.dw_attr_val.val_entry = NULL;
4545   attr.dw_attr_val.v.val_offset = offset;
4546   add_dwarf_attr (die, &attr);
4547 }
4548
4549 /* Add a range_list attribute value to a DIE.  When using
4550    dwarf_split_debug_info, address attributes in dies destined for the
4551    final executable should be direct references--setting the parameter
4552    force_direct ensures this behavior.  */
4553
4554 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4555 #define RELOCATED_OFFSET (NULL)
4556
4557 static void
4558 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4559                    long unsigned int offset, bool force_direct)
4560 {
4561   dw_attr_node attr;
4562
4563   attr.dw_attr = attr_kind;
4564   attr.dw_attr_val.val_class = dw_val_class_range_list;
4565   /* For the range_list attribute, use val_entry to store whether the
4566      offset should follow split-debug-info or normal semantics.  This
4567      value is read in output_range_list_offset.  */
4568   if (dwarf_split_debug_info && !force_direct)
4569     attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4570   else
4571     attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4572   attr.dw_attr_val.v.val_offset = offset;
4573   add_dwarf_attr (die, &attr);
4574 }
4575
4576 /* Return the start label of a delta attribute.  */
4577
4578 static inline const char *
4579 AT_vms_delta1 (dw_attr_node *a)
4580 {
4581   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4582   return a->dw_attr_val.v.val_vms_delta.lbl1;
4583 }
4584
4585 /* Return the end label of a delta attribute.  */
4586
4587 static inline const char *
4588 AT_vms_delta2 (dw_attr_node *a)
4589 {
4590   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4591   return a->dw_attr_val.v.val_vms_delta.lbl2;
4592 }
4593
4594 static inline const char *
4595 AT_lbl (dw_attr_node *a)
4596 {
4597   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4598                     || AT_class (a) == dw_val_class_lineptr
4599                     || AT_class (a) == dw_val_class_macptr
4600                     || AT_class (a) == dw_val_class_high_pc));
4601   return a->dw_attr_val.v.val_lbl_id;
4602 }
4603
4604 /* Get the attribute of type attr_kind.  */
4605
4606 static dw_attr_node *
4607 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4608 {
4609   dw_attr_node *a;
4610   unsigned ix;
4611   dw_die_ref spec = NULL;
4612
4613   if (! die)
4614     return NULL;
4615
4616   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4617     if (a->dw_attr == attr_kind)
4618       return a;
4619     else if (a->dw_attr == DW_AT_specification
4620              || a->dw_attr == DW_AT_abstract_origin)
4621       spec = AT_ref (a);
4622
4623   if (spec)
4624     return get_AT (spec, attr_kind);
4625
4626   return NULL;
4627 }
4628
4629 /* Returns the parent of the declaration of DIE.  */
4630
4631 static dw_die_ref
4632 get_die_parent (dw_die_ref die)
4633 {
4634   dw_die_ref t;
4635
4636   if (!die)
4637     return NULL;
4638
4639   if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4640       || (t = get_AT_ref (die, DW_AT_specification)))
4641     die = t;
4642
4643   return die->die_parent;
4644 }
4645
4646 /* Return the "low pc" attribute value, typically associated with a subprogram
4647    DIE.  Return null if the "low pc" attribute is either not present, or if it
4648    cannot be represented as an assembler label identifier.  */
4649
4650 static inline const char *
4651 get_AT_low_pc (dw_die_ref die)
4652 {
4653   dw_attr_node *a = get_AT (die, DW_AT_low_pc);
4654
4655   return a ? AT_lbl (a) : NULL;
4656 }
4657
4658 /* Return the "high pc" attribute value, typically associated with a subprogram
4659    DIE.  Return null if the "high pc" attribute is either not present, or if it
4660    cannot be represented as an assembler label identifier.  */
4661
4662 static inline const char *
4663 get_AT_hi_pc (dw_die_ref die)
4664 {
4665   dw_attr_node *a = get_AT (die, DW_AT_high_pc);
4666
4667   return a ? AT_lbl (a) : NULL;
4668 }
4669
4670 /* Return the value of the string attribute designated by ATTR_KIND, or
4671    NULL if it is not present.  */
4672
4673 static inline const char *
4674 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4675 {
4676   dw_attr_node *a = get_AT (die, attr_kind);
4677
4678   return a ? AT_string (a) : NULL;
4679 }
4680
4681 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4682    if it is not present.  */
4683
4684 static inline int
4685 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4686 {
4687   dw_attr_node *a = get_AT (die, attr_kind);
4688
4689   return a ? AT_flag (a) : 0;
4690 }
4691
4692 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4693    if it is not present.  */
4694
4695 static inline unsigned
4696 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4697 {
4698   dw_attr_node *a = get_AT (die, attr_kind);
4699
4700   return a ? AT_unsigned (a) : 0;
4701 }
4702
4703 static inline dw_die_ref
4704 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4705 {
4706   dw_attr_node *a = get_AT (die, attr_kind);
4707
4708   return a ? AT_ref (a) : NULL;
4709 }
4710
4711 static inline struct dwarf_file_data *
4712 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4713 {
4714   dw_attr_node *a = get_AT (die, attr_kind);
4715
4716   return a ? AT_file (a) : NULL;
4717 }
4718
4719 /* Return TRUE if the language is C++.  */
4720
4721 static inline bool
4722 is_cxx (void)
4723 {
4724   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4725
4726   return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
4727           || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
4728 }
4729
4730 /* Return TRUE if the language is Java.  */
4731
4732 static inline bool
4733 is_java (void)
4734 {
4735   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4736
4737   return lang == DW_LANG_Java;
4738 }
4739
4740 /* Return TRUE if the language is Fortran.  */
4741
4742 static inline bool
4743 is_fortran (void)
4744 {
4745   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4746
4747   return (lang == DW_LANG_Fortran77
4748           || lang == DW_LANG_Fortran90
4749           || lang == DW_LANG_Fortran95
4750           || lang == DW_LANG_Fortran03
4751           || lang == DW_LANG_Fortran08);
4752 }
4753
4754 /* Return TRUE if the language is Ada.  */
4755
4756 static inline bool
4757 is_ada (void)
4758 {
4759   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4760
4761   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4762 }
4763
4764 /* Remove the specified attribute if present.  Return TRUE if removal
4765    was successful.  */
4766
4767 static bool
4768 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4769 {
4770   dw_attr_node *a;
4771   unsigned ix;
4772
4773   if (! die)
4774     return false;
4775
4776   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4777     if (a->dw_attr == attr_kind)
4778       {
4779         if (AT_class (a) == dw_val_class_str)
4780           if (a->dw_attr_val.v.val_str->refcount)
4781             a->dw_attr_val.v.val_str->refcount--;
4782
4783         /* vec::ordered_remove should help reduce the number of abbrevs
4784            that are needed.  */
4785         die->die_attr->ordered_remove (ix);
4786         return true;
4787       }
4788   return false;
4789 }
4790
4791 /* Remove CHILD from its parent.  PREV must have the property that
4792    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
4793
4794 static void
4795 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4796 {
4797   gcc_assert (child->die_parent == prev->die_parent);
4798   gcc_assert (prev->die_sib == child);
4799   if (prev == child)
4800     {
4801       gcc_assert (child->die_parent->die_child == child);
4802       prev = NULL;
4803     }
4804   else
4805     prev->die_sib = child->die_sib;
4806   if (child->die_parent->die_child == child)
4807     child->die_parent->die_child = prev;
4808 }
4809
4810 /* Replace OLD_CHILD with NEW_CHILD.  PREV must have the property that
4811    PREV->DIE_SIB == OLD_CHILD.  Does not alter OLD_CHILD.  */
4812
4813 static void
4814 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4815 {
4816   dw_die_ref parent = old_child->die_parent;
4817
4818   gcc_assert (parent == prev->die_parent);
4819   gcc_assert (prev->die_sib == old_child);
4820
4821   new_child->die_parent = parent;
4822   if (prev == old_child)
4823     {
4824       gcc_assert (parent->die_child == old_child);
4825       new_child->die_sib = new_child;
4826     }
4827   else
4828     {
4829       prev->die_sib = new_child;
4830       new_child->die_sib = old_child->die_sib;
4831     }
4832   if (old_child->die_parent->die_child == old_child)
4833     old_child->die_parent->die_child = new_child;
4834 }
4835
4836 /* Move all children from OLD_PARENT to NEW_PARENT.  */
4837
4838 static void
4839 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4840 {
4841   dw_die_ref c;
4842   new_parent->die_child = old_parent->die_child;
4843   old_parent->die_child = NULL;
4844   FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4845 }
4846
4847 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
4848    matches TAG.  */
4849
4850 static void
4851 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4852 {
4853   dw_die_ref c;
4854
4855   c = die->die_child;
4856   if (c) do {
4857     dw_die_ref prev = c;
4858     c = c->die_sib;
4859     while (c->die_tag == tag)
4860       {
4861         remove_child_with_prev (c, prev);
4862         c->die_parent = NULL;
4863         /* Might have removed every child.  */
4864         if (c == c->die_sib)
4865           return;
4866         c = c->die_sib;
4867       }
4868   } while (c != die->die_child);
4869 }
4870
4871 /* Add a CHILD_DIE as the last child of DIE.  */
4872
4873 static void
4874 add_child_die (dw_die_ref die, dw_die_ref child_die)
4875 {
4876   /* FIXME this should probably be an assert.  */
4877   if (! die || ! child_die)
4878     return;
4879   gcc_assert (die != child_die);
4880
4881   child_die->die_parent = die;
4882   if (die->die_child)
4883     {
4884       child_die->die_sib = die->die_child->die_sib;
4885       die->die_child->die_sib = child_die;
4886     }
4887   else
4888     child_die->die_sib = child_die;
4889   die->die_child = child_die;
4890 }
4891
4892 /* Unassociate CHILD from its parent, and make its parent be
4893    NEW_PARENT.  */
4894
4895 static void
4896 reparent_child (dw_die_ref child, dw_die_ref new_parent)
4897 {
4898   for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
4899     if (p->die_sib == child)
4900       {
4901         remove_child_with_prev (child, p);
4902         break;
4903       }
4904   add_child_die (new_parent, child);
4905 }
4906
4907 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4908    is the specification, to the end of PARENT's list of children.
4909    This is done by removing and re-adding it.  */
4910
4911 static void
4912 splice_child_die (dw_die_ref parent, dw_die_ref child)
4913 {
4914   /* We want the declaration DIE from inside the class, not the
4915      specification DIE at toplevel.  */
4916   if (child->die_parent != parent)
4917     {
4918       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4919
4920       if (tmp)
4921         child = tmp;
4922     }
4923
4924   gcc_assert (child->die_parent == parent
4925               || (child->die_parent
4926                   == get_AT_ref (parent, DW_AT_specification)));
4927
4928   reparent_child (child, parent);
4929 }
4930
4931 /* Create and return a new die with a parent of PARENT_DIE.  If
4932    PARENT_DIE is NULL, the new DIE is placed in limbo and an
4933    associated tree T must be supplied to determine parenthood
4934    later.  */
4935
4936 static inline dw_die_ref
4937 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4938 {
4939   dw_die_ref die = ggc_cleared_alloc<die_node> ();
4940
4941   die->die_tag = tag_value;
4942
4943   if (parent_die != NULL)
4944     add_child_die (parent_die, die);
4945   else
4946     {
4947       limbo_die_node *limbo_node;
4948
4949       /* No DIEs created after early dwarf should end up in limbo,
4950          because the limbo list should not persist past LTO
4951          streaming.  */
4952       if (tag_value != DW_TAG_compile_unit
4953           /* These are allowed because they're generated while
4954              breaking out COMDAT units late.  */
4955           && tag_value != DW_TAG_type_unit
4956           && !early_dwarf
4957           /* Allow nested functions to live in limbo because they will
4958              only temporarily live there, as decls_for_scope will fix
4959              them up.  */
4960           && (TREE_CODE (t) != FUNCTION_DECL
4961               || !decl_function_context (t))
4962           /* Same as nested functions above but for types.  Types that
4963              are local to a function will be fixed in
4964              decls_for_scope.  */
4965           && (!RECORD_OR_UNION_TYPE_P (t)
4966               || !TYPE_CONTEXT (t)
4967               || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
4968           /* FIXME debug-early: Allow late limbo DIE creation for LTO,
4969              especially in the ltrans stage, but once we implement LTO
4970              dwarf streaming, we should remove this exception.  */
4971           && !in_lto_p)
4972         {
4973           fprintf (stderr, "symbol ended up in limbo too late:");
4974           debug_generic_stmt (t);
4975           gcc_unreachable ();
4976         }
4977
4978       limbo_node = ggc_cleared_alloc<limbo_die_node> ();
4979       limbo_node->die = die;
4980       limbo_node->created_for = t;
4981       limbo_node->next = limbo_die_list;
4982       limbo_die_list = limbo_node;
4983     }
4984
4985   return die;
4986 }
4987
4988 /* Return the DIE associated with the given type specifier.  */
4989
4990 static inline dw_die_ref
4991 lookup_type_die (tree type)
4992 {
4993   return TYPE_SYMTAB_DIE (type);
4994 }
4995
4996 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4997    anonymous type named by the typedef TYPE_DIE, return the DIE of the
4998    anonymous type instead the one of the naming typedef.  */
4999
5000 static inline dw_die_ref
5001 strip_naming_typedef (tree type, dw_die_ref type_die)
5002 {
5003   if (type
5004       && TREE_CODE (type) == RECORD_TYPE
5005       && type_die
5006       && type_die->die_tag == DW_TAG_typedef
5007       && is_naming_typedef_decl (TYPE_NAME (type)))
5008     type_die = get_AT_ref (type_die, DW_AT_type);
5009   return type_die;
5010 }
5011
5012 /* Like lookup_type_die, but if type is an anonymous type named by a
5013    typedef[1], return the DIE of the anonymous type instead the one of
5014    the naming typedef.  This is because in gen_typedef_die, we did
5015    equate the anonymous struct named by the typedef with the DIE of
5016    the naming typedef. So by default, lookup_type_die on an anonymous
5017    struct yields the DIE of the naming typedef.
5018
5019    [1]: Read the comment of is_naming_typedef_decl to learn about what
5020    a naming typedef is.  */
5021
5022 static inline dw_die_ref
5023 lookup_type_die_strip_naming_typedef (tree type)
5024 {
5025   dw_die_ref die = lookup_type_die (type);
5026   return strip_naming_typedef (type, die);
5027 }
5028
5029 /* Equate a DIE to a given type specifier.  */
5030
5031 static inline void
5032 equate_type_number_to_die (tree type, dw_die_ref type_die)
5033 {
5034   TYPE_SYMTAB_DIE (type) = type_die;
5035 }
5036
5037 /* Returns a hash value for X (which really is a die_struct).  */
5038
5039 inline hashval_t
5040 decl_die_hasher::hash (die_node *x)
5041 {
5042   return (hashval_t) x->decl_id;
5043 }
5044
5045 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
5046
5047 inline bool
5048 decl_die_hasher::equal (die_node *x, tree y)
5049 {
5050   return (x->decl_id == DECL_UID (y));
5051 }
5052
5053 /* Return the DIE associated with a given declaration.  */
5054
5055 static inline dw_die_ref
5056 lookup_decl_die (tree decl)
5057 {
5058   return decl_die_table->find_with_hash (decl, DECL_UID (decl));
5059 }
5060
5061 /* Returns a hash value for X (which really is a var_loc_list).  */
5062
5063 inline hashval_t
5064 decl_loc_hasher::hash (var_loc_list *x)
5065 {
5066   return (hashval_t) x->decl_id;
5067 }
5068
5069 /* Return nonzero if decl_id of var_loc_list X is the same as
5070    UID of decl *Y.  */
5071
5072 inline bool
5073 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5074 {
5075   return (x->decl_id == DECL_UID (y));
5076 }
5077
5078 /* Return the var_loc list associated with a given declaration.  */
5079
5080 static inline var_loc_list *
5081 lookup_decl_loc (const_tree decl)
5082 {
5083   if (!decl_loc_table)
5084     return NULL;
5085   return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5086 }
5087
5088 /* Returns a hash value for X (which really is a cached_dw_loc_list_list).  */
5089
5090 inline hashval_t
5091 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5092 {
5093   return (hashval_t) x->decl_id;
5094 }
5095
5096 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5097    UID of decl *Y.  */
5098
5099 inline bool
5100 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5101 {
5102   return (x->decl_id == DECL_UID (y));
5103 }
5104
5105 /* Equate a DIE to a particular declaration.  */
5106
5107 static void
5108 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5109 {
5110   unsigned int decl_id = DECL_UID (decl);
5111
5112   *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5113   decl_die->decl_id = decl_id;
5114 }
5115
5116 /* Return how many bits covers PIECE EXPR_LIST.  */
5117
5118 static HOST_WIDE_INT
5119 decl_piece_bitsize (rtx piece)
5120 {
5121   int ret = (int) GET_MODE (piece);
5122   if (ret)
5123     return ret;
5124   gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5125               && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5126   return INTVAL (XEXP (XEXP (piece, 0), 0));
5127 }
5128
5129 /* Return pointer to the location of location note in PIECE EXPR_LIST.  */
5130
5131 static rtx *
5132 decl_piece_varloc_ptr (rtx piece)
5133 {
5134   if ((int) GET_MODE (piece))
5135     return &XEXP (piece, 0);
5136   else
5137     return &XEXP (XEXP (piece, 0), 1);
5138 }
5139
5140 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5141    Next is the chain of following piece nodes.  */
5142
5143 static rtx_expr_list *
5144 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5145 {
5146   if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
5147     return alloc_EXPR_LIST (bitsize, loc_note, next);
5148   else
5149     return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5150                                                GEN_INT (bitsize),
5151                                                loc_note), next);
5152 }
5153
5154 /* Return rtx that should be stored into loc field for
5155    LOC_NOTE and BITPOS/BITSIZE.  */
5156
5157 static rtx
5158 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5159                       HOST_WIDE_INT bitsize)
5160 {
5161   if (bitsize != -1)
5162     {
5163       loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5164       if (bitpos != 0)
5165         loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5166     }
5167   return loc_note;
5168 }
5169
5170 /* This function either modifies location piece list *DEST in
5171    place (if SRC and INNER is NULL), or copies location piece list
5172    *SRC to *DEST while modifying it.  Location BITPOS is modified
5173    to contain LOC_NOTE, any pieces overlapping it are removed resp.
5174    not copied and if needed some padding around it is added.
5175    When modifying in place, DEST should point to EXPR_LIST where
5176    earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5177    to the start of the whole list and INNER points to the EXPR_LIST
5178    where earlier pieces cover PIECE_BITPOS bits.  */
5179
5180 static void
5181 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5182                    HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5183                    HOST_WIDE_INT bitsize, rtx loc_note)
5184 {
5185   HOST_WIDE_INT diff;
5186   bool copy = inner != NULL;
5187
5188   if (copy)
5189     {
5190       /* First copy all nodes preceding the current bitpos.  */
5191       while (src != inner)
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   /* Add padding if needed.  */
5200   if (bitpos != piece_bitpos)
5201     {
5202       *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5203                                copy ? NULL_RTX : *dest);
5204       dest = &XEXP (*dest, 1);
5205     }
5206   else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5207     {
5208       gcc_assert (!copy);
5209       /* A piece with correct bitpos and bitsize already exist,
5210          just update the location for it and return.  */
5211       *decl_piece_varloc_ptr (*dest) = loc_note;
5212       return;
5213     }
5214   /* Add the piece that changed.  */
5215   *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5216   dest = &XEXP (*dest, 1);
5217   /* Skip over pieces that overlap it.  */
5218   diff = bitpos - piece_bitpos + bitsize;
5219   if (!copy)
5220     src = dest;
5221   while (diff > 0 && *src)
5222     {
5223       rtx piece = *src;
5224       diff -= decl_piece_bitsize (piece);
5225       if (copy)
5226         src = &XEXP (piece, 1);
5227       else
5228         {
5229           *src = XEXP (piece, 1);
5230           free_EXPR_LIST_node (piece);
5231         }
5232     }
5233   /* Add padding if needed.  */
5234   if (diff < 0 && *src)
5235     {
5236       if (!copy)
5237         dest = src;
5238       *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5239       dest = &XEXP (*dest, 1);
5240     }
5241   if (!copy)
5242     return;
5243   /* Finally copy all nodes following it.  */
5244   while (*src)
5245     {
5246       *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5247                                decl_piece_bitsize (*src), NULL_RTX);
5248       dest = &XEXP (*dest, 1);
5249       src = &XEXP (*src, 1);
5250     }
5251 }
5252
5253 /* Add a variable location node to the linked list for DECL.  */
5254
5255 static struct var_loc_node *
5256 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5257 {
5258   unsigned int decl_id;
5259   var_loc_list *temp;
5260   struct var_loc_node *loc = NULL;
5261   HOST_WIDE_INT bitsize = -1, bitpos = -1;
5262
5263   if (TREE_CODE (decl) == VAR_DECL
5264       && DECL_HAS_DEBUG_EXPR_P (decl))
5265     {
5266       tree realdecl = DECL_DEBUG_EXPR (decl);
5267       if (handled_component_p (realdecl)
5268           || (TREE_CODE (realdecl) == MEM_REF
5269               && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5270         {
5271           HOST_WIDE_INT maxsize;
5272           tree innerdecl;
5273           innerdecl
5274             = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5275           if (!DECL_P (innerdecl)
5276               || DECL_IGNORED_P (innerdecl)
5277               || TREE_STATIC (innerdecl)
5278               || bitsize <= 0
5279               || bitpos + bitsize > 256
5280               || bitsize != maxsize)
5281             return NULL;
5282           decl = innerdecl;
5283         }
5284     }
5285
5286   decl_id = DECL_UID (decl);
5287   var_loc_list **slot
5288     = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5289   if (*slot == NULL)
5290     {
5291       temp = ggc_cleared_alloc<var_loc_list> ();
5292       temp->decl_id = decl_id;
5293       *slot = temp;
5294     }
5295   else
5296     temp = *slot;
5297
5298   /* For PARM_DECLs try to keep around the original incoming value,
5299      even if that means we'll emit a zero-range .debug_loc entry.  */
5300   if (temp->last
5301       && temp->first == temp->last
5302       && TREE_CODE (decl) == PARM_DECL
5303       && NOTE_P (temp->first->loc)
5304       && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5305       && DECL_INCOMING_RTL (decl)
5306       && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5307       && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5308          == GET_CODE (DECL_INCOMING_RTL (decl))
5309       && prev_real_insn (temp->first->loc) == NULL_RTX
5310       && (bitsize != -1
5311           || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5312                            NOTE_VAR_LOCATION_LOC (loc_note))
5313           || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5314               != NOTE_VAR_LOCATION_STATUS (loc_note))))
5315     {
5316       loc = ggc_cleared_alloc<var_loc_node> ();
5317       temp->first->next = loc;
5318       temp->last = loc;
5319       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5320     }
5321   else if (temp->last)
5322     {
5323       struct var_loc_node *last = temp->last, *unused = NULL;
5324       rtx *piece_loc = NULL, last_loc_note;
5325       HOST_WIDE_INT piece_bitpos = 0;
5326       if (last->next)
5327         {
5328           last = last->next;
5329           gcc_assert (last->next == NULL);
5330         }
5331       if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5332         {
5333           piece_loc = &last->loc;
5334           do
5335             {
5336               HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5337               if (piece_bitpos + cur_bitsize > bitpos)
5338                 break;
5339               piece_bitpos += cur_bitsize;
5340               piece_loc = &XEXP (*piece_loc, 1);
5341             }
5342           while (*piece_loc);
5343         }
5344       /* TEMP->LAST here is either pointer to the last but one or
5345          last element in the chained list, LAST is pointer to the
5346          last element.  */
5347       if (label && strcmp (last->label, label) == 0)
5348         {
5349           /* For SRA optimized variables if there weren't any real
5350              insns since last note, just modify the last node.  */
5351           if (piece_loc != NULL)
5352             {
5353               adjust_piece_list (piece_loc, NULL, NULL,
5354                                  bitpos, piece_bitpos, bitsize, loc_note);
5355               return NULL;
5356             }
5357           /* If the last note doesn't cover any instructions, remove it.  */
5358           if (temp->last != last)
5359             {
5360               temp->last->next = NULL;
5361               unused = last;
5362               last = temp->last;
5363               gcc_assert (strcmp (last->label, label) != 0);
5364             }
5365           else
5366             {
5367               gcc_assert (temp->first == temp->last
5368                           || (temp->first->next == temp->last
5369                               && TREE_CODE (decl) == PARM_DECL));
5370               memset (temp->last, '\0', sizeof (*temp->last));
5371               temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5372               return temp->last;
5373             }
5374         }
5375       if (bitsize == -1 && NOTE_P (last->loc))
5376         last_loc_note = last->loc;
5377       else if (piece_loc != NULL
5378                && *piece_loc != NULL_RTX
5379                && piece_bitpos == bitpos
5380                && decl_piece_bitsize (*piece_loc) == bitsize)
5381         last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5382       else
5383         last_loc_note = NULL_RTX;
5384       /* If the current location is the same as the end of the list,
5385          and either both or neither of the locations is uninitialized,
5386          we have nothing to do.  */
5387       if (last_loc_note == NULL_RTX
5388           || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5389                             NOTE_VAR_LOCATION_LOC (loc_note)))
5390           || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5391                != NOTE_VAR_LOCATION_STATUS (loc_note))
5392               && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5393                    == VAR_INIT_STATUS_UNINITIALIZED)
5394                   || (NOTE_VAR_LOCATION_STATUS (loc_note)
5395                       == VAR_INIT_STATUS_UNINITIALIZED))))
5396         {
5397           /* Add LOC to the end of list and update LAST.  If the last
5398              element of the list has been removed above, reuse its
5399              memory for the new node, otherwise allocate a new one.  */
5400           if (unused)
5401             {
5402               loc = unused;
5403               memset (loc, '\0', sizeof (*loc));
5404             }
5405           else
5406             loc = ggc_cleared_alloc<var_loc_node> ();
5407           if (bitsize == -1 || piece_loc == NULL)
5408             loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5409           else
5410             adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5411                                bitpos, piece_bitpos, bitsize, loc_note);
5412           last->next = loc;
5413           /* Ensure TEMP->LAST will point either to the new last but one
5414              element of the chain, or to the last element in it.  */
5415           if (last != temp->last)
5416             temp->last = last;
5417         }
5418       else if (unused)
5419         ggc_free (unused);
5420     }
5421   else
5422     {
5423       loc = ggc_cleared_alloc<var_loc_node> ();
5424       temp->first = loc;
5425       temp->last = loc;
5426       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5427     }
5428   return loc;
5429 }
5430 \f
5431 /* Keep track of the number of spaces used to indent the
5432    output of the debugging routines that print the structure of
5433    the DIE internal representation.  */
5434 static int print_indent;
5435
5436 /* Indent the line the number of spaces given by print_indent.  */
5437
5438 static inline void
5439 print_spaces (FILE *outfile)
5440 {
5441   fprintf (outfile, "%*s", print_indent, "");
5442 }
5443
5444 /* Print a type signature in hex.  */
5445
5446 static inline void
5447 print_signature (FILE *outfile, char *sig)
5448 {
5449   int i;
5450
5451   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5452     fprintf (outfile, "%02x", sig[i] & 0xff);
5453 }
5454
5455 static void print_loc_descr (dw_loc_descr_ref, FILE *);
5456
5457 /* Print the value associated to the VAL DWARF value node to OUTFILE.  If
5458    RECURSE, output location descriptor operations.  */
5459
5460 static void
5461 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
5462 {
5463   switch (val->val_class)
5464     {
5465     case dw_val_class_addr:
5466       fprintf (outfile, "address");
5467       break;
5468     case dw_val_class_offset:
5469       fprintf (outfile, "offset");
5470       break;
5471     case dw_val_class_loc:
5472       fprintf (outfile, "location descriptor");
5473       if (val->v.val_loc == NULL)
5474         fprintf (outfile, " -> <null>\n");
5475       else if (recurse)
5476         {
5477           fprintf (outfile, ":\n");
5478           print_indent += 4;
5479           print_loc_descr (val->v.val_loc, outfile);
5480           print_indent -= 4;
5481         }
5482       else
5483         fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
5484       break;
5485     case dw_val_class_loc_list:
5486       fprintf (outfile, "location list -> label:%s",
5487                val->v.val_loc_list->ll_symbol);
5488       break;
5489     case dw_val_class_range_list:
5490       fprintf (outfile, "range list");
5491       break;
5492     case dw_val_class_const:
5493       fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
5494       break;
5495     case dw_val_class_unsigned_const:
5496       fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
5497       break;
5498     case dw_val_class_const_double:
5499       fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
5500                         HOST_WIDE_INT_PRINT_UNSIGNED")",
5501                val->v.val_double.high,
5502                val->v.val_double.low);
5503       break;
5504     case dw_val_class_wide_int:
5505       {
5506         int i = val->v.val_wide->get_len ();
5507         fprintf (outfile, "constant (");
5508         gcc_assert (i > 0);
5509         if (val->v.val_wide->elt (i - 1) == 0)
5510           fprintf (outfile, "0x");
5511         fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5512                  val->v.val_wide->elt (--i));
5513         while (--i >= 0)
5514           fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5515                    val->v.val_wide->elt (i));
5516         fprintf (outfile, ")");
5517         break;
5518       }
5519     case dw_val_class_vec:
5520       fprintf (outfile, "floating-point or vector constant");
5521       break;
5522     case dw_val_class_flag:
5523       fprintf (outfile, "%u", val->v.val_flag);
5524       break;
5525     case dw_val_class_die_ref:
5526       if (val->v.val_die_ref.die != NULL)
5527         {
5528           dw_die_ref die = val->v.val_die_ref.die;
5529
5530           if (die->comdat_type_p)
5531             {
5532               fprintf (outfile, "die -> signature: ");
5533               print_signature (outfile,
5534                                die->die_id.die_type_node->signature);
5535             }
5536           else if (die->die_id.die_symbol)
5537             fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
5538           else
5539             fprintf (outfile, "die -> %ld", die->die_offset);
5540           fprintf (outfile, " (%p)", (void *) die);
5541         }
5542       else
5543         fprintf (outfile, "die -> <null>");
5544       break;
5545     case dw_val_class_vms_delta:
5546       fprintf (outfile, "delta: @slotcount(%s-%s)",
5547                val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
5548       break;
5549     case dw_val_class_lbl_id:
5550     case dw_val_class_lineptr:
5551     case dw_val_class_macptr:
5552     case dw_val_class_high_pc:
5553       fprintf (outfile, "label: %s", val->v.val_lbl_id);
5554       break;
5555     case dw_val_class_str:
5556       if (val->v.val_str->str != NULL)
5557         fprintf (outfile, "\"%s\"", val->v.val_str->str);
5558       else
5559         fprintf (outfile, "<null>");
5560       break;
5561     case dw_val_class_file:
5562       fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
5563                val->v.val_file->emitted_number);
5564       break;
5565     case dw_val_class_data8:
5566       {
5567         int i;
5568
5569         for (i = 0; i < 8; i++)
5570           fprintf (outfile, "%02x", val->v.val_data8[i]);
5571         break;
5572       }
5573     default:
5574       break;
5575     }
5576 }
5577
5578 /* Likewise, for a DIE attribute.  */
5579
5580 static void
5581 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
5582 {
5583   print_dw_val (&a->dw_attr_val, recurse, outfile);
5584 }
5585
5586
5587 /* Print the list of operands in the LOC location description to OUTFILE.  This
5588    routine is a debugging aid only.  */
5589
5590 static void
5591 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
5592 {
5593   dw_loc_descr_ref l = loc;
5594
5595   if (loc == NULL)
5596     {
5597       print_spaces (outfile);
5598       fprintf (outfile, "<null>\n");
5599       return;
5600     }
5601
5602   for (l = loc; l != NULL; l = l->dw_loc_next)
5603     {
5604       print_spaces (outfile);
5605       fprintf (outfile, "(%p) %s",
5606                (void *) l,
5607                dwarf_stack_op_name (l->dw_loc_opc));
5608       if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
5609         {
5610           fprintf (outfile, " ");
5611           print_dw_val (&l->dw_loc_oprnd1, false, outfile);
5612         }
5613       if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
5614         {
5615           fprintf (outfile, ", ");
5616           print_dw_val (&l->dw_loc_oprnd2, false, outfile);
5617         }
5618       fprintf (outfile, "\n");
5619     }
5620 }
5621
5622 /* Print the information associated with a given DIE, and its children.
5623    This routine is a debugging aid only.  */
5624
5625 static void
5626 print_die (dw_die_ref die, FILE *outfile)
5627 {
5628   dw_attr_node *a;
5629   dw_die_ref c;
5630   unsigned ix;
5631
5632   print_spaces (outfile);
5633   fprintf (outfile, "DIE %4ld: %s (%p)\n",
5634            die->die_offset, dwarf_tag_name (die->die_tag),
5635            (void*) die);
5636   print_spaces (outfile);
5637   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
5638   fprintf (outfile, " offset: %ld", die->die_offset);
5639   fprintf (outfile, " mark: %d\n", die->die_mark);
5640
5641   if (die->comdat_type_p)
5642     {
5643       print_spaces (outfile);
5644       fprintf (outfile, "  signature: ");
5645       print_signature (outfile, die->die_id.die_type_node->signature);
5646       fprintf (outfile, "\n");
5647     }
5648
5649   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5650     {
5651       print_spaces (outfile);
5652       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
5653
5654       print_attribute (a, true, outfile);
5655       fprintf (outfile, "\n");
5656     }
5657
5658   if (die->die_child != NULL)
5659     {
5660       print_indent += 4;
5661       FOR_EACH_CHILD (die, c, print_die (c, outfile));
5662       print_indent -= 4;
5663     }
5664   if (print_indent == 0)
5665     fprintf (outfile, "\n");
5666 }
5667
5668 /* Print the list of operations in the LOC location description.  */
5669
5670 DEBUG_FUNCTION void
5671 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
5672 {
5673   print_loc_descr (loc, stderr);
5674 }
5675
5676 /* Print the information collected for a given DIE.  */
5677
5678 DEBUG_FUNCTION void
5679 debug_dwarf_die (dw_die_ref die)
5680 {
5681   print_die (die, stderr);
5682 }
5683
5684 DEBUG_FUNCTION void
5685 debug (die_struct &ref)
5686 {
5687   print_die (&ref, stderr);
5688 }
5689
5690 DEBUG_FUNCTION void
5691 debug (die_struct *ptr)
5692 {
5693   if (ptr)
5694     debug (*ptr);
5695   else
5696     fprintf (stderr, "<nil>\n");
5697 }
5698
5699
5700 /* Print all DWARF information collected for the compilation unit.
5701    This routine is a debugging aid only.  */
5702
5703 DEBUG_FUNCTION void
5704 debug_dwarf (void)
5705 {
5706   print_indent = 0;
5707   print_die (comp_unit_die (), stderr);
5708 }
5709
5710 #ifdef ENABLE_CHECKING
5711 /* Sanity checks on DIEs.  */
5712
5713 static void
5714 check_die (dw_die_ref die)
5715 {
5716   unsigned ix;
5717   dw_attr_node *a;
5718   bool inline_found = false;
5719   int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
5720   int n_decl_line = 0, n_decl_file = 0;
5721   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5722     {
5723       switch (a->dw_attr)
5724         {
5725         case DW_AT_inline:
5726           if (a->dw_attr_val.v.val_unsigned)
5727             inline_found = true;
5728           break;
5729         case DW_AT_location:
5730           ++n_location;
5731           break;
5732         case DW_AT_low_pc:
5733           ++n_low_pc;
5734           break;
5735         case DW_AT_high_pc:
5736           ++n_high_pc;
5737           break;
5738         case DW_AT_artificial:
5739           ++n_artificial;
5740           break;
5741         case DW_AT_decl_line:
5742           ++n_decl_line;
5743           break;
5744         case DW_AT_decl_file:
5745           ++n_decl_file;
5746           break;
5747         default:
5748           break;
5749         }
5750     }
5751   if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
5752       || n_decl_line > 1 || n_decl_file > 1)
5753     {
5754       fprintf (stderr, "Duplicate attributes in DIE:\n");
5755       debug_dwarf_die (die);
5756       gcc_unreachable ();
5757     }
5758   if (inline_found)
5759     {
5760       /* A debugging information entry that is a member of an abstract
5761          instance tree [that has DW_AT_inline] should not contain any
5762          attributes which describe aspects of the subroutine which vary
5763          between distinct inlined expansions or distinct out-of-line
5764          expansions.  */
5765       FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5766         gcc_assert (a->dw_attr != DW_AT_low_pc
5767                     && a->dw_attr != DW_AT_high_pc
5768                     && a->dw_attr != DW_AT_location
5769                     && a->dw_attr != DW_AT_frame_base
5770                     && a->dw_attr != DW_AT_GNU_all_call_sites);
5771     }
5772 }
5773 #endif
5774 \f
5775 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
5776    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
5777    DIE that marks the start of the DIEs for this include file.  */
5778
5779 static dw_die_ref
5780 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5781 {
5782   const char *filename = get_AT_string (bincl_die, DW_AT_name);
5783   dw_die_ref new_unit = gen_compile_unit_die (filename);
5784
5785   new_unit->die_sib = old_unit;
5786   return new_unit;
5787 }
5788
5789 /* Close an include-file CU and reopen the enclosing one.  */
5790
5791 static dw_die_ref
5792 pop_compile_unit (dw_die_ref old_unit)
5793 {
5794   dw_die_ref new_unit = old_unit->die_sib;
5795
5796   old_unit->die_sib = NULL;
5797   return new_unit;
5798 }
5799
5800 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5801 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5802 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5803
5804 /* Calculate the checksum of a location expression.  */
5805
5806 static inline void
5807 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5808 {
5809   int tem;
5810   inchash::hash hstate;
5811   hashval_t hash;
5812
5813   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5814   CHECKSUM (tem);
5815   hash_loc_operands (loc, hstate);
5816   hash = hstate.end();
5817   CHECKSUM (hash);
5818 }
5819
5820 /* Calculate the checksum of an attribute.  */
5821
5822 static void
5823 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
5824 {
5825   dw_loc_descr_ref loc;
5826   rtx r;
5827
5828   CHECKSUM (at->dw_attr);
5829
5830   /* We don't care that this was compiled with a different compiler
5831      snapshot; if the output is the same, that's what matters.  */
5832   if (at->dw_attr == DW_AT_producer)
5833     return;
5834
5835   switch (AT_class (at))
5836     {
5837     case dw_val_class_const:
5838       CHECKSUM (at->dw_attr_val.v.val_int);
5839       break;
5840     case dw_val_class_unsigned_const:
5841       CHECKSUM (at->dw_attr_val.v.val_unsigned);
5842       break;
5843     case dw_val_class_const_double:
5844       CHECKSUM (at->dw_attr_val.v.val_double);
5845       break;
5846     case dw_val_class_wide_int:
5847       CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
5848                       get_full_len (*at->dw_attr_val.v.val_wide)
5849                       * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
5850       break;
5851     case dw_val_class_vec:
5852       CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5853                       (at->dw_attr_val.v.val_vec.length
5854                        * at->dw_attr_val.v.val_vec.elt_size));
5855       break;
5856     case dw_val_class_flag:
5857       CHECKSUM (at->dw_attr_val.v.val_flag);
5858       break;
5859     case dw_val_class_str:
5860       CHECKSUM_STRING (AT_string (at));
5861       break;
5862
5863     case dw_val_class_addr:
5864       r = AT_addr (at);
5865       gcc_assert (GET_CODE (r) == SYMBOL_REF);
5866       CHECKSUM_STRING (XSTR (r, 0));
5867       break;
5868
5869     case dw_val_class_offset:
5870       CHECKSUM (at->dw_attr_val.v.val_offset);
5871       break;
5872
5873     case dw_val_class_loc:
5874       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5875         loc_checksum (loc, ctx);
5876       break;
5877
5878     case dw_val_class_die_ref:
5879       die_checksum (AT_ref (at), ctx, mark);
5880       break;
5881
5882     case dw_val_class_fde_ref:
5883     case dw_val_class_vms_delta:
5884     case dw_val_class_lbl_id:
5885     case dw_val_class_lineptr:
5886     case dw_val_class_macptr:
5887     case dw_val_class_high_pc:
5888       break;
5889
5890     case dw_val_class_file:
5891       CHECKSUM_STRING (AT_file (at)->filename);
5892       break;
5893
5894     case dw_val_class_data8:
5895       CHECKSUM (at->dw_attr_val.v.val_data8);
5896       break;
5897
5898     default:
5899       break;
5900     }
5901 }
5902
5903 /* Calculate the checksum of a DIE.  */
5904
5905 static void
5906 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5907 {
5908   dw_die_ref c;
5909   dw_attr_node *a;
5910   unsigned ix;
5911
5912   /* To avoid infinite recursion.  */
5913   if (die->die_mark)
5914     {
5915       CHECKSUM (die->die_mark);
5916       return;
5917     }
5918   die->die_mark = ++(*mark);
5919
5920   CHECKSUM (die->die_tag);
5921
5922   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5923     attr_checksum (a, ctx, mark);
5924
5925   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5926 }
5927
5928 #undef CHECKSUM
5929 #undef CHECKSUM_BLOCK
5930 #undef CHECKSUM_STRING
5931
5932 /* For DWARF-4 types, include the trailing NULL when checksumming strings.  */
5933 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5934 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5935 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5936 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5937 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5938 #define CHECKSUM_ATTR(FOO) \
5939   if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5940
5941 /* Calculate the checksum of a number in signed LEB128 format.  */
5942
5943 static void
5944 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5945 {
5946   unsigned char byte;
5947   bool more;
5948
5949   while (1)
5950     {
5951       byte = (value & 0x7f);
5952       value >>= 7;
5953       more = !((value == 0 && (byte & 0x40) == 0)
5954                 || (value == -1 && (byte & 0x40) != 0));
5955       if (more)
5956         byte |= 0x80;
5957       CHECKSUM (byte);
5958       if (!more)
5959         break;
5960     }
5961 }
5962
5963 /* Calculate the checksum of a number in unsigned LEB128 format.  */
5964
5965 static void
5966 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5967 {
5968   while (1)
5969     {
5970       unsigned char byte = (value & 0x7f);
5971       value >>= 7;
5972       if (value != 0)
5973         /* More bytes to follow.  */
5974         byte |= 0x80;
5975       CHECKSUM (byte);
5976       if (value == 0)
5977         break;
5978     }
5979 }
5980
5981 /* Checksum the context of the DIE.  This adds the names of any
5982    surrounding namespaces or structures to the checksum.  */
5983
5984 static void
5985 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5986 {
5987   const char *name;
5988   dw_die_ref spec;
5989   int tag = die->die_tag;
5990
5991   if (tag != DW_TAG_namespace
5992       && tag != DW_TAG_structure_type
5993       && tag != DW_TAG_class_type)
5994     return;
5995
5996   name = get_AT_string (die, DW_AT_name);
5997
5998   spec = get_AT_ref (die, DW_AT_specification);
5999   if (spec != NULL)
6000     die = spec;
6001
6002   if (die->die_parent != NULL)
6003     checksum_die_context (die->die_parent, ctx);
6004
6005   CHECKSUM_ULEB128 ('C');
6006   CHECKSUM_ULEB128 (tag);
6007   if (name != NULL)
6008     CHECKSUM_STRING (name);
6009 }
6010
6011 /* Calculate the checksum of a location expression.  */
6012
6013 static inline void
6014 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6015 {
6016   /* Special case for lone DW_OP_plus_uconst: checksum as if the location
6017      were emitted as a DW_FORM_sdata instead of a location expression.  */
6018   if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
6019     {
6020       CHECKSUM_ULEB128 (DW_FORM_sdata);
6021       CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
6022       return;
6023     }
6024
6025   /* Otherwise, just checksum the raw location expression.  */
6026   while (loc != NULL)
6027     {
6028       inchash::hash hstate;
6029       hashval_t hash;
6030
6031       CHECKSUM_ULEB128 (loc->dtprel);
6032       CHECKSUM_ULEB128 (loc->dw_loc_opc);
6033       hash_loc_operands (loc, hstate);
6034       hash = hstate.end ();
6035       CHECKSUM (hash);
6036       loc = loc->dw_loc_next;
6037     }
6038 }
6039
6040 /* Calculate the checksum of an attribute.  */
6041
6042 static void
6043 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
6044                        struct md5_ctx *ctx, int *mark)
6045 {
6046   dw_loc_descr_ref loc;
6047   rtx r;
6048
6049   if (AT_class (at) == dw_val_class_die_ref)
6050     {
6051       dw_die_ref target_die = AT_ref (at);
6052
6053       /* For pointer and reference types, we checksum only the (qualified)
6054          name of the target type (if there is a name).  For friend entries,
6055          we checksum only the (qualified) name of the target type or function.
6056          This allows the checksum to remain the same whether the target type
6057          is complete or not.  */
6058       if ((at->dw_attr == DW_AT_type
6059            && (tag == DW_TAG_pointer_type
6060                || tag == DW_TAG_reference_type
6061                || tag == DW_TAG_rvalue_reference_type
6062                || tag == DW_TAG_ptr_to_member_type))
6063           || (at->dw_attr == DW_AT_friend
6064               && tag == DW_TAG_friend))
6065         {
6066           dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
6067
6068           if (name_attr != NULL)
6069             {
6070               dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6071
6072               if (decl == NULL)
6073                 decl = target_die;
6074               CHECKSUM_ULEB128 ('N');
6075               CHECKSUM_ULEB128 (at->dw_attr);
6076               if (decl->die_parent != NULL)
6077                 checksum_die_context (decl->die_parent, ctx);
6078               CHECKSUM_ULEB128 ('E');
6079               CHECKSUM_STRING (AT_string (name_attr));
6080               return;
6081             }
6082         }
6083
6084       /* For all other references to another DIE, we check to see if the
6085          target DIE has already been visited.  If it has, we emit a
6086          backward reference; if not, we descend recursively.  */
6087       if (target_die->die_mark > 0)
6088         {
6089           CHECKSUM_ULEB128 ('R');
6090           CHECKSUM_ULEB128 (at->dw_attr);
6091           CHECKSUM_ULEB128 (target_die->die_mark);
6092         }
6093       else
6094         {
6095           dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6096
6097           if (decl == NULL)
6098             decl = target_die;
6099           target_die->die_mark = ++(*mark);
6100           CHECKSUM_ULEB128 ('T');
6101           CHECKSUM_ULEB128 (at->dw_attr);
6102           if (decl->die_parent != NULL)
6103             checksum_die_context (decl->die_parent, ctx);
6104           die_checksum_ordered (target_die, ctx, mark);
6105         }
6106       return;
6107     }
6108
6109   CHECKSUM_ULEB128 ('A');
6110   CHECKSUM_ULEB128 (at->dw_attr);
6111
6112   switch (AT_class (at))
6113     {
6114     case dw_val_class_const:
6115       CHECKSUM_ULEB128 (DW_FORM_sdata);
6116       CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
6117       break;
6118
6119     case dw_val_class_unsigned_const:
6120       CHECKSUM_ULEB128 (DW_FORM_sdata);
6121       CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6122       break;
6123
6124     case dw_val_class_const_double:
6125       CHECKSUM_ULEB128 (DW_FORM_block);
6126       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6127       CHECKSUM (at->dw_attr_val.v.val_double);
6128       break;
6129
6130     case dw_val_class_wide_int:
6131       CHECKSUM_ULEB128 (DW_FORM_block);
6132       CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
6133                         * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
6134       CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6135                       get_full_len (*at->dw_attr_val.v.val_wide)
6136                       * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6137       break;
6138
6139     case dw_val_class_vec:
6140       CHECKSUM_ULEB128 (DW_FORM_block);
6141       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6142                         * at->dw_attr_val.v.val_vec.elt_size);
6143       CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6144                       (at->dw_attr_val.v.val_vec.length
6145                        * at->dw_attr_val.v.val_vec.elt_size));
6146       break;
6147
6148     case dw_val_class_flag:
6149       CHECKSUM_ULEB128 (DW_FORM_flag);
6150       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6151       break;
6152
6153     case dw_val_class_str:
6154       CHECKSUM_ULEB128 (DW_FORM_string);
6155       CHECKSUM_STRING (AT_string (at));
6156       break;
6157
6158     case dw_val_class_addr:
6159       r = AT_addr (at);
6160       gcc_assert (GET_CODE (r) == SYMBOL_REF);
6161       CHECKSUM_ULEB128 (DW_FORM_string);
6162       CHECKSUM_STRING (XSTR (r, 0));
6163       break;
6164
6165     case dw_val_class_offset:
6166       CHECKSUM_ULEB128 (DW_FORM_sdata);
6167       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6168       break;
6169
6170     case dw_val_class_loc:
6171       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6172         loc_checksum_ordered (loc, ctx);
6173       break;
6174
6175     case dw_val_class_fde_ref:
6176     case dw_val_class_lbl_id:
6177     case dw_val_class_lineptr:
6178     case dw_val_class_macptr:
6179     case dw_val_class_high_pc:
6180       break;
6181
6182     case dw_val_class_file:
6183       CHECKSUM_ULEB128 (DW_FORM_string);
6184       CHECKSUM_STRING (AT_file (at)->filename);
6185       break;
6186
6187     case dw_val_class_data8:
6188       CHECKSUM (at->dw_attr_val.v.val_data8);
6189       break;
6190
6191     default:
6192       break;
6193     }
6194 }
6195
6196 struct checksum_attributes
6197 {
6198   dw_attr_node *at_name;
6199   dw_attr_node *at_type;
6200   dw_attr_node *at_friend;
6201   dw_attr_node *at_accessibility;
6202   dw_attr_node *at_address_class;
6203   dw_attr_node *at_allocated;
6204   dw_attr_node *at_artificial;
6205   dw_attr_node *at_associated;
6206   dw_attr_node *at_binary_scale;
6207   dw_attr_node *at_bit_offset;
6208   dw_attr_node *at_bit_size;
6209   dw_attr_node *at_bit_stride;
6210   dw_attr_node *at_byte_size;
6211   dw_attr_node *at_byte_stride;
6212   dw_attr_node *at_const_value;
6213   dw_attr_node *at_containing_type;
6214   dw_attr_node *at_count;
6215   dw_attr_node *at_data_location;
6216   dw_attr_node *at_data_member_location;
6217   dw_attr_node *at_decimal_scale;
6218   dw_attr_node *at_decimal_sign;
6219   dw_attr_node *at_default_value;
6220   dw_attr_node *at_digit_count;
6221   dw_attr_node *at_discr;
6222   dw_attr_node *at_discr_list;
6223   dw_attr_node *at_discr_value;
6224   dw_attr_node *at_encoding;
6225   dw_attr_node *at_endianity;
6226   dw_attr_node *at_explicit;
6227   dw_attr_node *at_is_optional;
6228   dw_attr_node *at_location;
6229   dw_attr_node *at_lower_bound;
6230   dw_attr_node *at_mutable;
6231   dw_attr_node *at_ordering;
6232   dw_attr_node *at_picture_string;
6233   dw_attr_node *at_prototyped;
6234   dw_attr_node *at_small;
6235   dw_attr_node *at_segment;
6236   dw_attr_node *at_string_length;
6237   dw_attr_node *at_threads_scaled;
6238   dw_attr_node *at_upper_bound;
6239   dw_attr_node *at_use_location;
6240   dw_attr_node *at_use_UTF8;
6241   dw_attr_node *at_variable_parameter;
6242   dw_attr_node *at_virtuality;
6243   dw_attr_node *at_visibility;
6244   dw_attr_node *at_vtable_elem_location;
6245 };
6246
6247 /* Collect the attributes that we will want to use for the checksum.  */
6248
6249 static void
6250 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6251 {
6252   dw_attr_node *a;
6253   unsigned ix;
6254
6255   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6256     {
6257       switch (a->dw_attr)
6258         {
6259         case DW_AT_name:
6260           attrs->at_name = a;
6261           break;
6262         case DW_AT_type:
6263           attrs->at_type = a;
6264           break;
6265         case DW_AT_friend:
6266           attrs->at_friend = a;
6267           break;
6268         case DW_AT_accessibility:
6269           attrs->at_accessibility = a;
6270           break;
6271         case DW_AT_address_class:
6272           attrs->at_address_class = a;
6273           break;
6274         case DW_AT_allocated:
6275           attrs->at_allocated = a;
6276           break;
6277         case DW_AT_artificial:
6278           attrs->at_artificial = a;
6279           break;
6280         case DW_AT_associated:
6281           attrs->at_associated = a;
6282           break;
6283         case DW_AT_binary_scale:
6284           attrs->at_binary_scale = a;
6285           break;
6286         case DW_AT_bit_offset:
6287           attrs->at_bit_offset = a;
6288           break;
6289         case DW_AT_bit_size:
6290           attrs->at_bit_size = a;
6291           break;
6292         case DW_AT_bit_stride:
6293           attrs->at_bit_stride = a;
6294           break;
6295         case DW_AT_byte_size:
6296           attrs->at_byte_size = a;
6297           break;
6298         case DW_AT_byte_stride:
6299           attrs->at_byte_stride = a;
6300           break;
6301         case DW_AT_const_value:
6302           attrs->at_const_value = a;
6303           break;
6304         case DW_AT_containing_type:
6305           attrs->at_containing_type = a;
6306           break;
6307         case DW_AT_count:
6308           attrs->at_count = a;
6309           break;
6310         case DW_AT_data_location:
6311           attrs->at_data_location = a;
6312           break;
6313         case DW_AT_data_member_location:
6314           attrs->at_data_member_location = a;
6315           break;
6316         case DW_AT_decimal_scale:
6317           attrs->at_decimal_scale = a;
6318           break;
6319         case DW_AT_decimal_sign:
6320           attrs->at_decimal_sign = a;
6321           break;
6322         case DW_AT_default_value:
6323           attrs->at_default_value = a;
6324           break;
6325         case DW_AT_digit_count:
6326           attrs->at_digit_count = a;
6327           break;
6328         case DW_AT_discr:
6329           attrs->at_discr = a;
6330           break;
6331         case DW_AT_discr_list:
6332           attrs->at_discr_list = a;
6333           break;
6334         case DW_AT_discr_value:
6335           attrs->at_discr_value = a;
6336           break;
6337         case DW_AT_encoding:
6338           attrs->at_encoding = a;
6339           break;
6340         case DW_AT_endianity:
6341           attrs->at_endianity = a;
6342           break;
6343         case DW_AT_explicit:
6344           attrs->at_explicit = a;
6345           break;
6346         case DW_AT_is_optional:
6347           attrs->at_is_optional = a;
6348           break;
6349         case DW_AT_location:
6350           attrs->at_location = a;
6351           break;
6352         case DW_AT_lower_bound:
6353           attrs->at_lower_bound = a;
6354           break;
6355         case DW_AT_mutable:
6356           attrs->at_mutable = a;
6357           break;
6358         case DW_AT_ordering:
6359           attrs->at_ordering = a;
6360           break;
6361         case DW_AT_picture_string:
6362           attrs->at_picture_string = a;
6363           break;
6364         case DW_AT_prototyped:
6365           attrs->at_prototyped = a;
6366           break;
6367         case DW_AT_small:
6368           attrs->at_small = a;
6369           break;
6370         case DW_AT_segment:
6371           attrs->at_segment = a;
6372           break;
6373         case DW_AT_string_length:
6374           attrs->at_string_length = a;
6375           break;
6376         case DW_AT_threads_scaled:
6377           attrs->at_threads_scaled = a;
6378           break;
6379         case DW_AT_upper_bound:
6380           attrs->at_upper_bound = a;
6381           break;
6382         case DW_AT_use_location:
6383           attrs->at_use_location = a;
6384           break;
6385         case DW_AT_use_UTF8:
6386           attrs->at_use_UTF8 = a;
6387           break;
6388         case DW_AT_variable_parameter:
6389           attrs->at_variable_parameter = a;
6390           break;
6391         case DW_AT_virtuality:
6392           attrs->at_virtuality = a;
6393           break;
6394         case DW_AT_visibility:
6395           attrs->at_visibility = a;
6396           break;
6397         case DW_AT_vtable_elem_location:
6398           attrs->at_vtable_elem_location = a;
6399           break;
6400         default:
6401           break;
6402         }
6403     }
6404 }
6405
6406 /* Calculate the checksum of a DIE, using an ordered subset of attributes.  */
6407
6408 static void
6409 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6410 {
6411   dw_die_ref c;
6412   dw_die_ref decl;
6413   struct checksum_attributes attrs;
6414
6415   CHECKSUM_ULEB128 ('D');
6416   CHECKSUM_ULEB128 (die->die_tag);
6417
6418   memset (&attrs, 0, sizeof (attrs));
6419
6420   decl = get_AT_ref (die, DW_AT_specification);
6421   if (decl != NULL)
6422     collect_checksum_attributes (&attrs, decl);
6423   collect_checksum_attributes (&attrs, die);
6424
6425   CHECKSUM_ATTR (attrs.at_name);
6426   CHECKSUM_ATTR (attrs.at_accessibility);
6427   CHECKSUM_ATTR (attrs.at_address_class);
6428   CHECKSUM_ATTR (attrs.at_allocated);
6429   CHECKSUM_ATTR (attrs.at_artificial);
6430   CHECKSUM_ATTR (attrs.at_associated);
6431   CHECKSUM_ATTR (attrs.at_binary_scale);
6432   CHECKSUM_ATTR (attrs.at_bit_offset);
6433   CHECKSUM_ATTR (attrs.at_bit_size);
6434   CHECKSUM_ATTR (attrs.at_bit_stride);
6435   CHECKSUM_ATTR (attrs.at_byte_size);
6436   CHECKSUM_ATTR (attrs.at_byte_stride);
6437   CHECKSUM_ATTR (attrs.at_const_value);
6438   CHECKSUM_ATTR (attrs.at_containing_type);
6439   CHECKSUM_ATTR (attrs.at_count);
6440   CHECKSUM_ATTR (attrs.at_data_location);
6441   CHECKSUM_ATTR (attrs.at_data_member_location);
6442   CHECKSUM_ATTR (attrs.at_decimal_scale);
6443   CHECKSUM_ATTR (attrs.at_decimal_sign);
6444   CHECKSUM_ATTR (attrs.at_default_value);
6445   CHECKSUM_ATTR (attrs.at_digit_count);
6446   CHECKSUM_ATTR (attrs.at_discr);
6447   CHECKSUM_ATTR (attrs.at_discr_list);
6448   CHECKSUM_ATTR (attrs.at_discr_value);
6449   CHECKSUM_ATTR (attrs.at_encoding);
6450   CHECKSUM_ATTR (attrs.at_endianity);
6451   CHECKSUM_ATTR (attrs.at_explicit);
6452   CHECKSUM_ATTR (attrs.at_is_optional);
6453   CHECKSUM_ATTR (attrs.at_location);
6454   CHECKSUM_ATTR (attrs.at_lower_bound);
6455   CHECKSUM_ATTR (attrs.at_mutable);
6456   CHECKSUM_ATTR (attrs.at_ordering);
6457   CHECKSUM_ATTR (attrs.at_picture_string);
6458   CHECKSUM_ATTR (attrs.at_prototyped);
6459   CHECKSUM_ATTR (attrs.at_small);
6460   CHECKSUM_ATTR (attrs.at_segment);
6461   CHECKSUM_ATTR (attrs.at_string_length);
6462   CHECKSUM_ATTR (attrs.at_threads_scaled);
6463   CHECKSUM_ATTR (attrs.at_upper_bound);
6464   CHECKSUM_ATTR (attrs.at_use_location);
6465   CHECKSUM_ATTR (attrs.at_use_UTF8);
6466   CHECKSUM_ATTR (attrs.at_variable_parameter);
6467   CHECKSUM_ATTR (attrs.at_virtuality);
6468   CHECKSUM_ATTR (attrs.at_visibility);
6469   CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6470   CHECKSUM_ATTR (attrs.at_type);
6471   CHECKSUM_ATTR (attrs.at_friend);
6472
6473   /* Checksum the child DIEs.  */
6474   c = die->die_child;
6475   if (c) do {
6476     dw_attr_node *name_attr;
6477
6478     c = c->die_sib;
6479     name_attr = get_AT (c, DW_AT_name);
6480     if (is_template_instantiation (c))
6481       {
6482         /* Ignore instantiations of member type and function templates.  */
6483       }
6484     else if (name_attr != NULL
6485              && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6486       {
6487         /* Use a shallow checksum for named nested types and member
6488            functions.  */
6489         CHECKSUM_ULEB128 ('S');
6490         CHECKSUM_ULEB128 (c->die_tag);
6491         CHECKSUM_STRING (AT_string (name_attr));
6492       }
6493     else
6494       {
6495         /* Use a deep checksum for other children.  */
6496         /* Mark this DIE so it gets processed when unmarking.  */
6497         if (c->die_mark == 0)
6498           c->die_mark = -1;
6499         die_checksum_ordered (c, ctx, mark);
6500       }
6501   } while (c != die->die_child);
6502
6503   CHECKSUM_ULEB128 (0);
6504 }
6505
6506 /* Add a type name and tag to a hash.  */
6507 static void
6508 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6509 {
6510   CHECKSUM_ULEB128 (tag);
6511   CHECKSUM_STRING (name);
6512 }
6513
6514 #undef CHECKSUM
6515 #undef CHECKSUM_STRING
6516 #undef CHECKSUM_ATTR
6517 #undef CHECKSUM_LEB128
6518 #undef CHECKSUM_ULEB128
6519
6520 /* Generate the type signature for DIE.  This is computed by generating an
6521    MD5 checksum over the DIE's tag, its relevant attributes, and its
6522    children.  Attributes that are references to other DIEs are processed
6523    by recursion, using the MARK field to prevent infinite recursion.
6524    If the DIE is nested inside a namespace or another type, we also
6525    need to include that context in the signature.  The lower 64 bits
6526    of the resulting MD5 checksum comprise the signature.  */
6527
6528 static void
6529 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6530 {
6531   int mark;
6532   const char *name;
6533   unsigned char checksum[16];
6534   struct md5_ctx ctx;
6535   dw_die_ref decl;
6536   dw_die_ref parent;
6537
6538   name = get_AT_string (die, DW_AT_name);
6539   decl = get_AT_ref (die, DW_AT_specification);
6540   parent = get_die_parent (die);
6541
6542   /* First, compute a signature for just the type name (and its surrounding
6543      context, if any.  This is stored in the type unit DIE for link-time
6544      ODR (one-definition rule) checking.  */
6545
6546   if (is_cxx () && name != NULL)
6547     {
6548       md5_init_ctx (&ctx);
6549
6550       /* Checksum the names of surrounding namespaces and structures.  */
6551       if (parent != NULL)
6552         checksum_die_context (parent, &ctx);
6553
6554       /* Checksum the current DIE. */
6555       die_odr_checksum (die->die_tag, name, &ctx);
6556       md5_finish_ctx (&ctx, checksum);
6557
6558       add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6559     }
6560
6561   /* Next, compute the complete type signature.  */
6562
6563   md5_init_ctx (&ctx);
6564   mark = 1;
6565   die->die_mark = mark;
6566
6567   /* Checksum the names of surrounding namespaces and structures.  */
6568   if (parent != NULL)
6569     checksum_die_context (parent, &ctx);
6570
6571   /* Checksum the DIE and its children.  */
6572   die_checksum_ordered (die, &ctx, &mark);
6573   unmark_all_dies (die);
6574   md5_finish_ctx (&ctx, checksum);
6575
6576   /* Store the signature in the type node and link the type DIE and the
6577      type node together.  */
6578   memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6579           DWARF_TYPE_SIGNATURE_SIZE);
6580   die->comdat_type_p = true;
6581   die->die_id.die_type_node = type_node;
6582   type_node->type_die = die;
6583
6584   /* If the DIE is a specification, link its declaration to the type node
6585      as well.  */
6586   if (decl != NULL)
6587     {
6588       decl->comdat_type_p = true;
6589       decl->die_id.die_type_node = type_node;
6590     }
6591 }
6592
6593 /* Do the location expressions look same?  */
6594 static inline int
6595 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6596 {
6597   return loc1->dw_loc_opc == loc2->dw_loc_opc
6598          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6599          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6600 }
6601
6602 /* Do the values look the same?  */
6603 static int
6604 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6605 {
6606   dw_loc_descr_ref loc1, loc2;
6607   rtx r1, r2;
6608
6609   if (v1->val_class != v2->val_class)
6610     return 0;
6611
6612   switch (v1->val_class)
6613     {
6614     case dw_val_class_const:
6615       return v1->v.val_int == v2->v.val_int;
6616     case dw_val_class_unsigned_const:
6617       return v1->v.val_unsigned == v2->v.val_unsigned;
6618     case dw_val_class_const_double:
6619       return v1->v.val_double.high == v2->v.val_double.high
6620              && v1->v.val_double.low == v2->v.val_double.low;
6621     case dw_val_class_wide_int:
6622       return *v1->v.val_wide == *v2->v.val_wide;
6623     case dw_val_class_vec:
6624       if (v1->v.val_vec.length != v2->v.val_vec.length
6625           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6626         return 0;
6627       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6628                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
6629         return 0;
6630       return 1;
6631     case dw_val_class_flag:
6632       return v1->v.val_flag == v2->v.val_flag;
6633     case dw_val_class_str:
6634       return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6635
6636     case dw_val_class_addr:
6637       r1 = v1->v.val_addr;
6638       r2 = v2->v.val_addr;
6639       if (GET_CODE (r1) != GET_CODE (r2))
6640         return 0;
6641       return !rtx_equal_p (r1, r2);
6642
6643     case dw_val_class_offset:
6644       return v1->v.val_offset == v2->v.val_offset;
6645
6646     case dw_val_class_loc:
6647       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6648            loc1 && loc2;
6649            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6650         if (!same_loc_p (loc1, loc2, mark))
6651           return 0;
6652       return !loc1 && !loc2;
6653
6654     case dw_val_class_die_ref:
6655       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6656
6657     case dw_val_class_fde_ref:
6658     case dw_val_class_vms_delta:
6659     case dw_val_class_lbl_id:
6660     case dw_val_class_lineptr:
6661     case dw_val_class_macptr:
6662     case dw_val_class_high_pc:
6663       return 1;
6664
6665     case dw_val_class_file:
6666       return v1->v.val_file == v2->v.val_file;
6667
6668     case dw_val_class_data8:
6669       return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6670
6671     default:
6672       return 1;
6673     }
6674 }
6675
6676 /* Do the attributes look the same?  */
6677
6678 static int
6679 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
6680 {
6681   if (at1->dw_attr != at2->dw_attr)
6682     return 0;
6683
6684   /* We don't care that this was compiled with a different compiler
6685      snapshot; if the output is the same, that's what matters. */
6686   if (at1->dw_attr == DW_AT_producer)
6687     return 1;
6688
6689   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6690 }
6691
6692 /* Do the dies look the same?  */
6693
6694 static int
6695 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6696 {
6697   dw_die_ref c1, c2;
6698   dw_attr_node *a1;
6699   unsigned ix;
6700
6701   /* To avoid infinite recursion.  */
6702   if (die1->die_mark)
6703     return die1->die_mark == die2->die_mark;
6704   die1->die_mark = die2->die_mark = ++(*mark);
6705
6706   if (die1->die_tag != die2->die_tag)
6707     return 0;
6708
6709   if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6710     return 0;
6711
6712   FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6713     if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6714       return 0;
6715
6716   c1 = die1->die_child;
6717   c2 = die2->die_child;
6718   if (! c1)
6719     {
6720       if (c2)
6721         return 0;
6722     }
6723   else
6724     for (;;)
6725       {
6726         if (!same_die_p (c1, c2, mark))
6727           return 0;
6728         c1 = c1->die_sib;
6729         c2 = c2->die_sib;
6730         if (c1 == die1->die_child)
6731           {
6732             if (c2 == die2->die_child)
6733               break;
6734             else
6735               return 0;
6736           }
6737     }
6738
6739   return 1;
6740 }
6741
6742 /* Do the dies look the same?  Wrapper around same_die_p.  */
6743
6744 static int
6745 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6746 {
6747   int mark = 0;
6748   int ret = same_die_p (die1, die2, &mark);
6749
6750   unmark_all_dies (die1);
6751   unmark_all_dies (die2);
6752
6753   return ret;
6754 }
6755
6756 /* The prefix to attach to symbols on DIEs in the current comdat debug
6757    info section.  */
6758 static const char *comdat_symbol_id;
6759
6760 /* The index of the current symbol within the current comdat CU.  */
6761 static unsigned int comdat_symbol_number;
6762
6763 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6764    children, and set comdat_symbol_id accordingly.  */
6765
6766 static void
6767 compute_section_prefix (dw_die_ref unit_die)
6768 {
6769   const char *die_name = get_AT_string (unit_die, DW_AT_name);
6770   const char *base = die_name ? lbasename (die_name) : "anonymous";
6771   char *name = XALLOCAVEC (char, strlen (base) + 64);
6772   char *p;
6773   int i, mark;
6774   unsigned char checksum[16];
6775   struct md5_ctx ctx;
6776
6777   /* Compute the checksum of the DIE, then append part of it as hex digits to
6778      the name filename of the unit.  */
6779
6780   md5_init_ctx (&ctx);
6781   mark = 0;
6782   die_checksum (unit_die, &ctx, &mark);
6783   unmark_all_dies (unit_die);
6784   md5_finish_ctx (&ctx, checksum);
6785
6786   sprintf (name, "%s.", base);
6787   clean_symbol_name (name);
6788
6789   p = name + strlen (name);
6790   for (i = 0; i < 4; i++)
6791     {
6792       sprintf (p, "%.2x", checksum[i]);
6793       p += 2;
6794     }
6795
6796   comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6797   comdat_symbol_number = 0;
6798 }
6799
6800 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
6801
6802 static int
6803 is_type_die (dw_die_ref die)
6804 {
6805   switch (die->die_tag)
6806     {
6807     case DW_TAG_array_type:
6808     case DW_TAG_class_type:
6809     case DW_TAG_interface_type:
6810     case DW_TAG_enumeration_type:
6811     case DW_TAG_pointer_type:
6812     case DW_TAG_reference_type:
6813     case DW_TAG_rvalue_reference_type:
6814     case DW_TAG_string_type:
6815     case DW_TAG_structure_type:
6816     case DW_TAG_subroutine_type:
6817     case DW_TAG_union_type:
6818     case DW_TAG_ptr_to_member_type:
6819     case DW_TAG_set_type:
6820     case DW_TAG_subrange_type:
6821     case DW_TAG_base_type:
6822     case DW_TAG_const_type:
6823     case DW_TAG_file_type:
6824     case DW_TAG_packed_type:
6825     case DW_TAG_volatile_type:
6826     case DW_TAG_typedef:
6827       return 1;
6828     default:
6829       return 0;
6830     }
6831 }
6832
6833 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6834    Basically, we want to choose the bits that are likely to be shared between
6835    compilations (types) and leave out the bits that are specific to individual
6836    compilations (functions).  */
6837
6838 static int
6839 is_comdat_die (dw_die_ref c)
6840 {
6841   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6842      we do for stabs.  The advantage is a greater likelihood of sharing between
6843      objects that don't include headers in the same order (and therefore would
6844      put the base types in a different comdat).  jason 8/28/00 */
6845
6846   if (c->die_tag == DW_TAG_base_type)
6847     return 0;
6848
6849   if (c->die_tag == DW_TAG_pointer_type
6850       || c->die_tag == DW_TAG_reference_type
6851       || c->die_tag == DW_TAG_rvalue_reference_type
6852       || c->die_tag == DW_TAG_const_type
6853       || c->die_tag == DW_TAG_volatile_type)
6854     {
6855       dw_die_ref t = get_AT_ref (c, DW_AT_type);
6856
6857       return t ? is_comdat_die (t) : 0;
6858     }
6859
6860   return is_type_die (c);
6861 }
6862
6863 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6864    compilation unit.  */
6865
6866 static int
6867 is_symbol_die (dw_die_ref c)
6868 {
6869   return (is_type_die (c)
6870           || is_declaration_die (c)
6871           || c->die_tag == DW_TAG_namespace
6872           || c->die_tag == DW_TAG_module);
6873 }
6874
6875 /* Returns true iff C is a compile-unit DIE.  */
6876
6877 static inline bool
6878 is_cu_die (dw_die_ref c)
6879 {
6880   return c && c->die_tag == DW_TAG_compile_unit;
6881 }
6882
6883 /* Returns true iff C is a unit DIE of some sort.  */
6884
6885 static inline bool
6886 is_unit_die (dw_die_ref c)
6887 {
6888   return c && (c->die_tag == DW_TAG_compile_unit
6889                || c->die_tag == DW_TAG_partial_unit
6890                || c->die_tag == DW_TAG_type_unit);
6891 }
6892
6893 /* Returns true iff C is a namespace DIE.  */
6894
6895 static inline bool
6896 is_namespace_die (dw_die_ref c)
6897 {
6898   return c && c->die_tag == DW_TAG_namespace;
6899 }
6900
6901 /* Returns true iff C is a class or structure DIE.  */
6902
6903 static inline bool
6904 is_class_die (dw_die_ref c)
6905 {
6906   return c && (c->die_tag == DW_TAG_class_type
6907                || c->die_tag == DW_TAG_structure_type);
6908 }
6909
6910 /* Return non-zero if this DIE is a template parameter.  */
6911
6912 static inline bool
6913 is_template_parameter (dw_die_ref die)
6914 {
6915   switch (die->die_tag)
6916     {
6917     case DW_TAG_template_type_param:
6918     case DW_TAG_template_value_param:
6919     case DW_TAG_GNU_template_template_param:
6920     case DW_TAG_GNU_template_parameter_pack:
6921       return true;
6922     default:
6923       return false;
6924     }
6925 }
6926
6927 /* Return non-zero if this DIE represents a template instantiation.  */
6928
6929 static inline bool
6930 is_template_instantiation (dw_die_ref die)
6931 {
6932   dw_die_ref c;
6933
6934   if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
6935     return false;
6936   FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
6937   return false;
6938 }
6939
6940 static char *
6941 gen_internal_sym (const char *prefix)
6942 {
6943   char buf[256];
6944
6945   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6946   return xstrdup (buf);
6947 }
6948
6949 /* Assign symbols to all worthy DIEs under DIE.  */
6950
6951 static void
6952 assign_symbol_names (dw_die_ref die)
6953 {
6954   dw_die_ref c;
6955
6956   if (is_symbol_die (die) && !die->comdat_type_p)
6957     {
6958       if (comdat_symbol_id)
6959         {
6960           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6961
6962           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6963                    comdat_symbol_id, comdat_symbol_number++);
6964           die->die_id.die_symbol = xstrdup (p);
6965         }
6966       else
6967         die->die_id.die_symbol = gen_internal_sym ("LDIE");
6968     }
6969
6970   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6971 }
6972
6973 struct cu_hash_table_entry
6974 {
6975   dw_die_ref cu;
6976   unsigned min_comdat_num, max_comdat_num;
6977   struct cu_hash_table_entry *next;
6978 };
6979
6980 /* Helpers to manipulate hash table of CUs.  */
6981
6982 struct cu_hash_table_entry_hasher : pointer_hash <cu_hash_table_entry>
6983 {
6984   typedef die_struct *compare_type;
6985   static inline hashval_t hash (const cu_hash_table_entry *);
6986   static inline bool equal (const cu_hash_table_entry *, const die_struct *);
6987   static inline void remove (cu_hash_table_entry *);
6988 };
6989
6990 inline hashval_t
6991 cu_hash_table_entry_hasher::hash (const cu_hash_table_entry *entry)
6992 {
6993   return htab_hash_string (entry->cu->die_id.die_symbol);
6994 }
6995
6996 inline bool
6997 cu_hash_table_entry_hasher::equal (const cu_hash_table_entry *entry1,
6998                                    const die_struct *entry2)
6999 {
7000   return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
7001 }
7002
7003 inline void
7004 cu_hash_table_entry_hasher::remove (cu_hash_table_entry *entry)
7005 {
7006   struct cu_hash_table_entry *next;
7007
7008   while (entry)
7009     {
7010       next = entry->next;
7011       free (entry);
7012       entry = next;
7013     }
7014 }
7015
7016 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
7017
7018 /* Check whether we have already seen this CU and set up SYM_NUM
7019    accordingly.  */
7020 static int
7021 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
7022 {
7023   struct cu_hash_table_entry dummy;
7024   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7025
7026   dummy.max_comdat_num = 0;
7027
7028   slot = htable->find_slot_with_hash (cu,
7029                                       htab_hash_string (cu->die_id.die_symbol),
7030                                       INSERT);
7031   entry = *slot;
7032
7033   for (; entry; last = entry, entry = entry->next)
7034     {
7035       if (same_die_p_wrap (cu, entry->cu))
7036         break;
7037     }
7038
7039   if (entry)
7040     {
7041       *sym_num = entry->min_comdat_num;
7042       return 1;
7043     }
7044
7045   entry = XCNEW (struct cu_hash_table_entry);
7046   entry->cu = cu;
7047   entry->min_comdat_num = *sym_num = last->max_comdat_num;
7048   entry->next = *slot;
7049   *slot = entry;
7050
7051   return 0;
7052 }
7053
7054 /* Record SYM_NUM to record of CU in HTABLE.  */
7055 static void
7056 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
7057                              unsigned int sym_num)
7058 {
7059   struct cu_hash_table_entry **slot, *entry;
7060
7061   slot = htable->find_slot_with_hash (cu,
7062                                       htab_hash_string (cu->die_id.die_symbol),
7063                                       NO_INSERT);
7064   entry = *slot;
7065
7066   entry->max_comdat_num = sym_num;
7067 }
7068
7069 /* Traverse the DIE (which is always comp_unit_die), and set up
7070    additional compilation units for each of the include files we see
7071    bracketed by BINCL/EINCL.  */
7072
7073 static void
7074 break_out_includes (dw_die_ref die)
7075 {
7076   dw_die_ref c;
7077   dw_die_ref unit = NULL;
7078   limbo_die_node *node, **pnode;
7079
7080   c = die->die_child;
7081   if (c) do {
7082     dw_die_ref prev = c;
7083     c = c->die_sib;
7084     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7085            || (unit && is_comdat_die (c)))
7086       {
7087         dw_die_ref next = c->die_sib;
7088
7089         /* This DIE is for a secondary CU; remove it from the main one.  */
7090         remove_child_with_prev (c, prev);
7091
7092         if (c->die_tag == DW_TAG_GNU_BINCL)
7093           unit = push_new_compile_unit (unit, c);
7094         else if (c->die_tag == DW_TAG_GNU_EINCL)
7095           unit = pop_compile_unit (unit);
7096         else
7097           add_child_die (unit, c);
7098         c = next;
7099         if (c == die->die_child)
7100           break;
7101       }
7102   } while (c != die->die_child);
7103
7104 #if 0
7105   /* We can only use this in debugging, since the frontend doesn't check
7106      to make sure that we leave every include file we enter.  */
7107   gcc_assert (!unit);
7108 #endif
7109
7110   assign_symbol_names (die);
7111   cu_hash_type cu_hash_table (10);
7112   for (node = limbo_die_list, pnode = &limbo_die_list;
7113        node;
7114        node = node->next)
7115     {
7116       int is_dupl;
7117
7118       compute_section_prefix (node->die);
7119       is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
7120                         &comdat_symbol_number);
7121       assign_symbol_names (node->die);
7122       if (is_dupl)
7123         *pnode = node->next;
7124       else
7125         {
7126           pnode = &node->next;
7127           record_comdat_symbol_number (node->die, &cu_hash_table,
7128                 comdat_symbol_number);
7129         }
7130     }
7131 }
7132
7133 /* Return non-zero if this DIE is a declaration.  */
7134
7135 static int
7136 is_declaration_die (dw_die_ref die)
7137 {
7138   dw_attr_node *a;
7139   unsigned ix;
7140
7141   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7142     if (a->dw_attr == DW_AT_declaration)
7143       return 1;
7144
7145   return 0;
7146 }
7147
7148 /* Return non-zero if this DIE is nested inside a subprogram.  */
7149
7150 static int
7151 is_nested_in_subprogram (dw_die_ref die)
7152 {
7153   dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7154
7155   if (decl == NULL)
7156     decl = die;
7157   return local_scope_p (decl);
7158 }
7159
7160 /* Return non-zero if this DIE contains a defining declaration of a
7161    subprogram.  */
7162
7163 static int
7164 contains_subprogram_definition (dw_die_ref die)
7165 {
7166   dw_die_ref c;
7167
7168   if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7169     return 1;
7170   FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7171   return 0;
7172 }
7173
7174 /* Return non-zero if this is a type DIE that should be moved to a
7175    COMDAT .debug_types section.  */
7176
7177 static int
7178 should_move_die_to_comdat (dw_die_ref die)
7179 {
7180   switch (die->die_tag)
7181     {
7182     case DW_TAG_class_type:
7183     case DW_TAG_structure_type:
7184     case DW_TAG_enumeration_type:
7185     case DW_TAG_union_type:
7186       /* Don't move declarations, inlined instances, types nested in a
7187          subprogram, or types that contain subprogram definitions.  */
7188       if (is_declaration_die (die)
7189           || get_AT (die, DW_AT_abstract_origin)
7190           || is_nested_in_subprogram (die)
7191           || contains_subprogram_definition (die))
7192         return 0;
7193       return 1;
7194     case DW_TAG_array_type:
7195     case DW_TAG_interface_type:
7196     case DW_TAG_pointer_type:
7197     case DW_TAG_reference_type:
7198     case DW_TAG_rvalue_reference_type:
7199     case DW_TAG_string_type:
7200     case DW_TAG_subroutine_type:
7201     case DW_TAG_ptr_to_member_type:
7202     case DW_TAG_set_type:
7203     case DW_TAG_subrange_type:
7204     case DW_TAG_base_type:
7205     case DW_TAG_const_type:
7206     case DW_TAG_file_type:
7207     case DW_TAG_packed_type:
7208     case DW_TAG_volatile_type:
7209     case DW_TAG_typedef:
7210     default:
7211       return 0;
7212     }
7213 }
7214
7215 /* Make a clone of DIE.  */
7216
7217 static dw_die_ref
7218 clone_die (dw_die_ref die)
7219 {
7220   dw_die_ref clone;
7221   dw_attr_node *a;
7222   unsigned ix;
7223
7224   clone = ggc_cleared_alloc<die_node> ();
7225   clone->die_tag = die->die_tag;
7226
7227   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7228     add_dwarf_attr (clone, a);
7229
7230   return clone;
7231 }
7232
7233 /* Make a clone of the tree rooted at DIE.  */
7234
7235 static dw_die_ref
7236 clone_tree (dw_die_ref die)
7237 {
7238   dw_die_ref c;
7239   dw_die_ref clone = clone_die (die);
7240
7241   FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7242
7243   return clone;
7244 }
7245
7246 /* Make a clone of DIE as a declaration.  */
7247
7248 static dw_die_ref
7249 clone_as_declaration (dw_die_ref die)
7250 {
7251   dw_die_ref clone;
7252   dw_die_ref decl;
7253   dw_attr_node *a;
7254   unsigned ix;
7255
7256   /* If the DIE is already a declaration, just clone it.  */
7257   if (is_declaration_die (die))
7258     return clone_die (die);
7259
7260   /* If the DIE is a specification, just clone its declaration DIE.  */
7261   decl = get_AT_ref (die, DW_AT_specification);
7262   if (decl != NULL)
7263     {
7264       clone = clone_die (decl);
7265       if (die->comdat_type_p)
7266         add_AT_die_ref (clone, DW_AT_signature, die);
7267       return clone;
7268     }
7269
7270   clone = ggc_cleared_alloc<die_node> ();
7271   clone->die_tag = die->die_tag;
7272
7273   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7274     {
7275       /* We don't want to copy over all attributes.
7276          For example we don't want DW_AT_byte_size because otherwise we will no
7277          longer have a declaration and GDB will treat it as a definition.  */
7278
7279       switch (a->dw_attr)
7280         {
7281         case DW_AT_abstract_origin:
7282         case DW_AT_artificial:
7283         case DW_AT_containing_type:
7284         case DW_AT_external:
7285         case DW_AT_name:
7286         case DW_AT_type:
7287         case DW_AT_virtuality:
7288         case DW_AT_linkage_name:
7289         case DW_AT_MIPS_linkage_name:
7290           add_dwarf_attr (clone, a);
7291           break;
7292         case DW_AT_byte_size:
7293         default:
7294           break;
7295         }
7296     }
7297
7298   if (die->comdat_type_p)
7299     add_AT_die_ref (clone, DW_AT_signature, die);
7300
7301   add_AT_flag (clone, DW_AT_declaration, 1);
7302   return clone;
7303 }
7304
7305
7306 /* Structure to map a DIE in one CU to its copy in a comdat type unit.  */
7307
7308 struct decl_table_entry
7309 {
7310   dw_die_ref orig;
7311   dw_die_ref copy;
7312 };
7313
7314 /* Helpers to manipulate hash table of copied declarations.  */
7315
7316 /* Hashtable helpers.  */
7317
7318 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
7319 {
7320   typedef die_struct *compare_type;
7321   static inline hashval_t hash (const decl_table_entry *);
7322   static inline bool equal (const decl_table_entry *, const die_struct *);
7323 };
7324
7325 inline hashval_t
7326 decl_table_entry_hasher::hash (const decl_table_entry *entry)
7327 {
7328   return htab_hash_pointer (entry->orig);
7329 }
7330
7331 inline bool
7332 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
7333                                 const die_struct *entry2)
7334 {
7335   return entry1->orig == entry2;
7336 }
7337
7338 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7339
7340 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7341    or type unit entry, to a new tree.  Adds the new tree to UNIT and returns
7342    a pointer to the copy of DIE.  If DECL_TABLE is provided, it is used
7343    to check if the ancestor has already been copied into UNIT.  */
7344
7345 static dw_die_ref
7346 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7347                     decl_hash_type *decl_table)
7348 {
7349   dw_die_ref parent = die->die_parent;
7350   dw_die_ref new_parent = unit;
7351   dw_die_ref copy;
7352   decl_table_entry **slot = NULL;
7353   struct decl_table_entry *entry = NULL;
7354
7355   if (decl_table)
7356     {
7357       /* Check if the entry has already been copied to UNIT.  */
7358       slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7359                                               INSERT);
7360       if (*slot != HTAB_EMPTY_ENTRY)
7361         {
7362           entry = *slot;
7363           return entry->copy;
7364         }
7365
7366       /* Record in DECL_TABLE that DIE has been copied to UNIT.  */
7367       entry = XCNEW (struct decl_table_entry);
7368       entry->orig = die;
7369       entry->copy = NULL;
7370       *slot = entry;
7371     }
7372
7373   if (parent != NULL)
7374     {
7375       dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7376       if (spec != NULL)
7377         parent = spec;
7378       if (!is_unit_die (parent))
7379         new_parent = copy_ancestor_tree (unit, parent, decl_table);
7380     }
7381
7382   copy = clone_as_declaration (die);
7383   add_child_die (new_parent, copy);
7384
7385   if (decl_table)
7386     {
7387       /* Record the pointer to the copy.  */
7388       entry->copy = copy;
7389     }
7390
7391   return copy;
7392 }
7393 /* Copy the declaration context to the new type unit DIE.  This includes
7394    any surrounding namespace or type declarations.  If the DIE has an
7395    AT_specification attribute, it also includes attributes and children
7396    attached to the specification, and returns a pointer to the original
7397    parent of the declaration DIE.  Returns NULL otherwise.  */
7398
7399 static dw_die_ref
7400 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7401 {
7402   dw_die_ref decl;
7403   dw_die_ref new_decl;
7404   dw_die_ref orig_parent = NULL;
7405
7406   decl = get_AT_ref (die, DW_AT_specification);
7407   if (decl == NULL)
7408     decl = die;
7409   else
7410     {
7411       unsigned ix;
7412       dw_die_ref c;
7413       dw_attr_node *a;
7414
7415       /* The original DIE will be changed to a declaration, and must
7416          be moved to be a child of the original declaration DIE.  */
7417       orig_parent = decl->die_parent;
7418
7419       /* Copy the type node pointer from the new DIE to the original
7420          declaration DIE so we can forward references later.  */
7421       decl->comdat_type_p = true;
7422       decl->die_id.die_type_node = die->die_id.die_type_node;
7423
7424       remove_AT (die, DW_AT_specification);
7425
7426       FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7427         {
7428           if (a->dw_attr != DW_AT_name
7429               && a->dw_attr != DW_AT_declaration
7430               && a->dw_attr != DW_AT_external)
7431             add_dwarf_attr (die, a);
7432         }
7433
7434       FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7435     }
7436
7437   if (decl->die_parent != NULL
7438       && !is_unit_die (decl->die_parent))
7439     {
7440       new_decl = copy_ancestor_tree (unit, decl, NULL);
7441       if (new_decl != NULL)
7442         {
7443           remove_AT (new_decl, DW_AT_signature);
7444           add_AT_specification (die, new_decl);
7445         }
7446     }
7447
7448   return orig_parent;
7449 }
7450
7451 /* Generate the skeleton ancestor tree for the given NODE, then clone
7452    the DIE and add the clone into the tree.  */
7453
7454 static void
7455 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7456 {
7457   if (node->new_die != NULL)
7458     return;
7459
7460   node->new_die = clone_as_declaration (node->old_die);
7461
7462   if (node->parent != NULL)
7463     {
7464       generate_skeleton_ancestor_tree (node->parent);
7465       add_child_die (node->parent->new_die, node->new_die);
7466     }
7467 }
7468
7469 /* Generate a skeleton tree of DIEs containing any declarations that are
7470    found in the original tree.  We traverse the tree looking for declaration
7471    DIEs, and construct the skeleton from the bottom up whenever we find one.  */
7472
7473 static void
7474 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7475 {
7476   skeleton_chain_node node;
7477   dw_die_ref c;
7478   dw_die_ref first;
7479   dw_die_ref prev = NULL;
7480   dw_die_ref next = NULL;
7481
7482   node.parent = parent;
7483
7484   first = c = parent->old_die->die_child;
7485   if (c)
7486     next = c->die_sib;
7487   if (c) do {
7488     if (prev == NULL || prev->die_sib == c)
7489       prev = c;
7490     c = next;
7491     next = (c == first ? NULL : c->die_sib);
7492     node.old_die = c;
7493     node.new_die = NULL;
7494     if (is_declaration_die (c))
7495       {
7496         if (is_template_instantiation (c))
7497           {
7498             /* Instantiated templates do not need to be cloned into the
7499                type unit.  Just move the DIE and its children back to
7500                the skeleton tree (in the main CU).  */
7501             remove_child_with_prev (c, prev);
7502             add_child_die (parent->new_die, c);
7503             c = prev;
7504           }
7505         else
7506           {
7507             /* Clone the existing DIE, move the original to the skeleton
7508                tree (which is in the main CU), and put the clone, with
7509                all the original's children, where the original came from
7510                (which is about to be moved to the type unit).  */
7511             dw_die_ref clone = clone_die (c);
7512             move_all_children (c, clone);
7513
7514             /* If the original has a DW_AT_object_pointer attribute,
7515                it would now point to a child DIE just moved to the
7516                cloned tree, so we need to remove that attribute from
7517                the original.  */
7518             remove_AT (c, DW_AT_object_pointer);
7519
7520             replace_child (c, clone, prev);
7521             generate_skeleton_ancestor_tree (parent);
7522             add_child_die (parent->new_die, c);
7523             node.new_die = c;
7524             c = clone;
7525           }
7526       }
7527     generate_skeleton_bottom_up (&node);
7528   } while (next != NULL);
7529 }
7530
7531 /* Wrapper function for generate_skeleton_bottom_up.  */
7532
7533 static dw_die_ref
7534 generate_skeleton (dw_die_ref die)
7535 {
7536   skeleton_chain_node node;
7537
7538   node.old_die = die;
7539   node.new_die = NULL;
7540   node.parent = NULL;
7541
7542   /* If this type definition is nested inside another type,
7543      and is not an instantiation of a template, always leave
7544      at least a declaration in its place.  */
7545   if (die->die_parent != NULL
7546       && is_type_die (die->die_parent)
7547       && !is_template_instantiation (die))
7548     node.new_die = clone_as_declaration (die);
7549
7550   generate_skeleton_bottom_up (&node);
7551   return node.new_die;
7552 }
7553
7554 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7555    declaration.  The original DIE is moved to a new compile unit so that
7556    existing references to it follow it to the new location.  If any of the
7557    original DIE's descendants is a declaration, we need to replace the
7558    original DIE with a skeleton tree and move the declarations back into the
7559    skeleton tree.  */
7560
7561 static dw_die_ref
7562 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7563                                        dw_die_ref prev)
7564 {
7565   dw_die_ref skeleton, orig_parent;
7566
7567   /* Copy the declaration context to the type unit DIE.  If the returned
7568      ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7569      that DIE.  */
7570   orig_parent = copy_declaration_context (unit, child);
7571
7572   skeleton = generate_skeleton (child);
7573   if (skeleton == NULL)
7574     remove_child_with_prev (child, prev);
7575   else
7576     {
7577       skeleton->comdat_type_p = true;
7578       skeleton->die_id.die_type_node = child->die_id.die_type_node;
7579
7580       /* If the original DIE was a specification, we need to put
7581          the skeleton under the parent DIE of the declaration.
7582          This leaves the original declaration in the tree, but
7583          it will be pruned later since there are no longer any
7584          references to it.  */
7585       if (orig_parent != NULL)
7586         {
7587           remove_child_with_prev (child, prev);
7588           add_child_die (orig_parent, skeleton);
7589         }
7590       else
7591         replace_child (child, skeleton, prev);
7592     }
7593
7594   return skeleton;
7595 }
7596
7597 /* Traverse the DIE and set up additional .debug_types sections for each
7598    type worthy of being placed in a COMDAT section.  */
7599
7600 static void
7601 break_out_comdat_types (dw_die_ref die)
7602 {
7603   dw_die_ref c;
7604   dw_die_ref first;
7605   dw_die_ref prev = NULL;
7606   dw_die_ref next = NULL;
7607   dw_die_ref unit = NULL;
7608
7609   first = c = die->die_child;
7610   if (c)
7611     next = c->die_sib;
7612   if (c) do {
7613     if (prev == NULL || prev->die_sib == c)
7614       prev = c;
7615     c = next;
7616     next = (c == first ? NULL : c->die_sib);
7617     if (should_move_die_to_comdat (c))
7618       {
7619         dw_die_ref replacement;
7620         comdat_type_node *type_node;
7621
7622         /* Break out nested types into their own type units.  */
7623         break_out_comdat_types (c);
7624
7625         /* Create a new type unit DIE as the root for the new tree, and
7626            add it to the list of comdat types.  */
7627         unit = new_die (DW_TAG_type_unit, NULL, NULL);
7628         add_AT_unsigned (unit, DW_AT_language,
7629                          get_AT_unsigned (comp_unit_die (), DW_AT_language));
7630         type_node = ggc_cleared_alloc<comdat_type_node> ();
7631         type_node->root_die = unit;
7632         type_node->next = comdat_type_list;
7633         comdat_type_list = type_node;
7634
7635         /* Generate the type signature.  */
7636         generate_type_signature (c, type_node);
7637
7638         /* Copy the declaration context, attributes, and children of the
7639            declaration into the new type unit DIE, then remove this DIE
7640            from the main CU (or replace it with a skeleton if necessary).  */
7641         replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7642         type_node->skeleton_die = replacement;
7643
7644         /* Add the DIE to the new compunit.  */
7645         add_child_die (unit, c);
7646
7647         if (replacement != NULL)
7648           c = replacement;
7649       }
7650     else if (c->die_tag == DW_TAG_namespace
7651              || c->die_tag == DW_TAG_class_type
7652              || c->die_tag == DW_TAG_structure_type
7653              || c->die_tag == DW_TAG_union_type)
7654       {
7655         /* Look for nested types that can be broken out.  */
7656         break_out_comdat_types (c);
7657       }
7658   } while (next != NULL);
7659 }
7660
7661 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
7662    Enter all the cloned children into the hash table decl_table.  */
7663
7664 static dw_die_ref
7665 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
7666 {
7667   dw_die_ref c;
7668   dw_die_ref clone;
7669   struct decl_table_entry *entry;
7670   decl_table_entry **slot;
7671
7672   if (die->die_tag == DW_TAG_subprogram)
7673     clone = clone_as_declaration (die);
7674   else
7675     clone = clone_die (die);
7676
7677   slot = decl_table->find_slot_with_hash (die,
7678                                           htab_hash_pointer (die), INSERT);
7679
7680   /* Assert that DIE isn't in the hash table yet.  If it would be there
7681      before, the ancestors would be necessarily there as well, therefore
7682      clone_tree_partial wouldn't be called.  */
7683   gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7684
7685   entry = XCNEW (struct decl_table_entry);
7686   entry->orig = die;
7687   entry->copy = clone;
7688   *slot = entry;
7689
7690   if (die->die_tag != DW_TAG_subprogram)
7691     FOR_EACH_CHILD (die, c,
7692                     add_child_die (clone, clone_tree_partial (c, decl_table)));
7693
7694   return clone;
7695 }
7696
7697 /* Walk the DIE and its children, looking for references to incomplete
7698    or trivial types that are unmarked (i.e., that are not in the current
7699    type_unit).  */
7700
7701 static void
7702 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
7703 {
7704   dw_die_ref c;
7705   dw_attr_node *a;
7706   unsigned ix;
7707
7708   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7709     {
7710       if (AT_class (a) == dw_val_class_die_ref)
7711         {
7712           dw_die_ref targ = AT_ref (a);
7713           decl_table_entry **slot;
7714           struct decl_table_entry *entry;
7715
7716           if (targ->die_mark != 0 || targ->comdat_type_p)
7717             continue;
7718
7719           slot = decl_table->find_slot_with_hash (targ,
7720                                                   htab_hash_pointer (targ),
7721                                                   INSERT);
7722
7723           if (*slot != HTAB_EMPTY_ENTRY)
7724             {
7725               /* TARG has already been copied, so we just need to
7726                  modify the reference to point to the copy.  */
7727               entry = *slot;
7728               a->dw_attr_val.v.val_die_ref.die = entry->copy;
7729             }
7730           else
7731             {
7732               dw_die_ref parent = unit;
7733               dw_die_ref copy = clone_die (targ);
7734
7735               /* Record in DECL_TABLE that TARG has been copied.
7736                  Need to do this now, before the recursive call,
7737                  because DECL_TABLE may be expanded and SLOT
7738                  would no longer be a valid pointer.  */
7739               entry = XCNEW (struct decl_table_entry);
7740               entry->orig = targ;
7741               entry->copy = copy;
7742               *slot = entry;
7743
7744               /* If TARG is not a declaration DIE, we need to copy its
7745                  children.  */
7746               if (!is_declaration_die (targ))
7747                 {
7748                   FOR_EACH_CHILD (
7749                       targ, c,
7750                       add_child_die (copy,
7751                                      clone_tree_partial (c, decl_table)));
7752                 }
7753
7754               /* Make sure the cloned tree is marked as part of the
7755                  type unit.  */
7756               mark_dies (copy);
7757
7758               /* If TARG has surrounding context, copy its ancestor tree
7759                  into the new type unit.  */
7760               if (targ->die_parent != NULL
7761                   && !is_unit_die (targ->die_parent))
7762                 parent = copy_ancestor_tree (unit, targ->die_parent,
7763                                              decl_table);
7764
7765               add_child_die (parent, copy);
7766               a->dw_attr_val.v.val_die_ref.die = copy;
7767
7768               /* Make sure the newly-copied DIE is walked.  If it was
7769                  installed in a previously-added context, it won't
7770                  get visited otherwise.  */
7771               if (parent != unit)
7772                 {
7773                   /* Find the highest point of the newly-added tree,
7774                      mark each node along the way, and walk from there.  */
7775                   parent->die_mark = 1;
7776                   while (parent->die_parent
7777                          && parent->die_parent->die_mark == 0)
7778                     {
7779                       parent = parent->die_parent;
7780                       parent->die_mark = 1;
7781                     }
7782                   copy_decls_walk (unit, parent, decl_table);
7783                 }
7784             }
7785         }
7786     }
7787
7788   FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7789 }
7790
7791 /* Copy declarations for "unworthy" types into the new comdat section.
7792    Incomplete types, modified types, and certain other types aren't broken
7793    out into comdat sections of their own, so they don't have a signature,
7794    and we need to copy the declaration into the same section so that we
7795    don't have an external reference.  */
7796
7797 static void
7798 copy_decls_for_unworthy_types (dw_die_ref unit)
7799 {
7800   mark_dies (unit);
7801   decl_hash_type decl_table (10);
7802   copy_decls_walk (unit, unit, &decl_table);
7803   unmark_dies (unit);
7804 }
7805
7806 /* Traverse the DIE and add a sibling attribute if it may have the
7807    effect of speeding up access to siblings.  To save some space,
7808    avoid generating sibling attributes for DIE's without children.  */
7809
7810 static void
7811 add_sibling_attributes (dw_die_ref die)
7812 {
7813   dw_die_ref c;
7814
7815   if (! die->die_child)
7816     return;
7817
7818   if (die->die_parent && die != die->die_parent->die_child)
7819     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7820
7821   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7822 }
7823
7824 /* Output all location lists for the DIE and its children.  */
7825
7826 static void
7827 output_location_lists (dw_die_ref die)
7828 {
7829   dw_die_ref c;
7830   dw_attr_node *a;
7831   unsigned ix;
7832
7833   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7834     if (AT_class (a) == dw_val_class_loc_list)
7835       output_loc_list (AT_loc_list (a));
7836
7837   FOR_EACH_CHILD (die, c, output_location_lists (c));
7838 }
7839
7840 /* We want to limit the number of external references, because they are
7841    larger than local references: a relocation takes multiple words, and
7842    even a sig8 reference is always eight bytes, whereas a local reference
7843    can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7844    So if we encounter multiple external references to the same type DIE, we
7845    make a local typedef stub for it and redirect all references there.
7846
7847    This is the element of the hash table for keeping track of these
7848    references.  */
7849
7850 struct external_ref
7851 {
7852   dw_die_ref type;
7853   dw_die_ref stub;
7854   unsigned n_refs;
7855 };
7856
7857 /* Hashtable helpers.  */
7858
7859 struct external_ref_hasher : free_ptr_hash <external_ref>
7860 {
7861   static inline hashval_t hash (const external_ref *);
7862   static inline bool equal (const external_ref *, const external_ref *);
7863 };
7864
7865 inline hashval_t
7866 external_ref_hasher::hash (const external_ref *r)
7867 {
7868   dw_die_ref die = r->type;
7869   hashval_t h = 0;
7870
7871   /* We can't use the address of the DIE for hashing, because
7872      that will make the order of the stub DIEs non-deterministic.  */
7873   if (! die->comdat_type_p)
7874     /* We have a symbol; use it to compute a hash.  */
7875     h = htab_hash_string (die->die_id.die_symbol);
7876   else
7877     {
7878       /* We have a type signature; use a subset of the bits as the hash.
7879          The 8-byte signature is at least as large as hashval_t.  */
7880       comdat_type_node *type_node = die->die_id.die_type_node;
7881       memcpy (&h, type_node->signature, sizeof (h));
7882     }
7883   return h;
7884 }
7885
7886 inline bool
7887 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
7888 {
7889   return r1->type == r2->type;
7890 }
7891
7892 typedef hash_table<external_ref_hasher> external_ref_hash_type;
7893
7894 /* Return a pointer to the external_ref for references to DIE.  */
7895
7896 static struct external_ref *
7897 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
7898 {
7899   struct external_ref ref, *ref_p;
7900   external_ref **slot;
7901
7902   ref.type = die;
7903   slot = map->find_slot (&ref, INSERT);
7904   if (*slot != HTAB_EMPTY_ENTRY)
7905     return *slot;
7906
7907   ref_p = XCNEW (struct external_ref);
7908   ref_p->type = die;
7909   *slot = ref_p;
7910   return ref_p;
7911 }
7912
7913 /* Subroutine of optimize_external_refs, below.
7914
7915    If we see a type skeleton, record it as our stub.  If we see external
7916    references, remember how many we've seen.  */
7917
7918 static void
7919 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
7920 {
7921   dw_die_ref c;
7922   dw_attr_node *a;
7923   unsigned ix;
7924   struct external_ref *ref_p;
7925
7926   if (is_type_die (die)
7927       && (c = get_AT_ref (die, DW_AT_signature)))
7928     {
7929       /* This is a local skeleton; use it for local references.  */
7930       ref_p = lookup_external_ref (map, c);
7931       ref_p->stub = die;
7932     }
7933
7934   /* Scan the DIE references, and remember any that refer to DIEs from
7935      other CUs (i.e. those which are not marked).  */
7936   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7937     if (AT_class (a) == dw_val_class_die_ref
7938         && (c = AT_ref (a))->die_mark == 0
7939         && is_type_die (c))
7940       {
7941         ref_p = lookup_external_ref (map, c);
7942         ref_p->n_refs++;
7943       }
7944
7945   FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7946 }
7947
7948 /* htab_traverse callback function for optimize_external_refs, below.  SLOT
7949    points to an external_ref, DATA is the CU we're processing.  If we don't
7950    already have a local stub, and we have multiple refs, build a stub.  */
7951
7952 int
7953 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
7954 {
7955   struct external_ref *ref_p = *slot;
7956
7957   if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7958     {
7959       /* We have multiple references to this type, so build a small stub.
7960          Both of these forms are a bit dodgy from the perspective of the
7961          DWARF standard, since technically they should have names.  */
7962       dw_die_ref cu = data;
7963       dw_die_ref type = ref_p->type;
7964       dw_die_ref stub = NULL;
7965
7966       if (type->comdat_type_p)
7967         {
7968           /* If we refer to this type via sig8, use AT_signature.  */
7969           stub = new_die (type->die_tag, cu, NULL_TREE);
7970           add_AT_die_ref (stub, DW_AT_signature, type);
7971         }
7972       else
7973         {
7974           /* Otherwise, use a typedef with no name.  */
7975           stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7976           add_AT_die_ref (stub, DW_AT_type, type);
7977         }
7978
7979       stub->die_mark++;
7980       ref_p->stub = stub;
7981     }
7982   return 1;
7983 }
7984
7985 /* DIE is a unit; look through all the DIE references to see if there are
7986    any external references to types, and if so, create local stubs for
7987    them which will be applied in build_abbrev_table.  This is useful because
7988    references to local DIEs are smaller.  */
7989
7990 static external_ref_hash_type *
7991 optimize_external_refs (dw_die_ref die)
7992 {
7993   external_ref_hash_type *map = new external_ref_hash_type (10);
7994   optimize_external_refs_1 (die, map);
7995   map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
7996   return map;
7997 }
7998
7999 /* The format of each DIE (and its attribute value pairs) is encoded in an
8000    abbreviation table.  This routine builds the abbreviation table and assigns
8001    a unique abbreviation id for each abbreviation entry.  The children of each
8002    die are visited recursively.  */
8003
8004 static void
8005 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
8006 {
8007   unsigned long abbrev_id;
8008   unsigned int n_alloc;
8009   dw_die_ref c;
8010   dw_attr_node *a;
8011   unsigned ix;
8012
8013   /* Scan the DIE references, and replace any that refer to
8014      DIEs from other CUs (i.e. those which are not marked) with
8015      the local stubs we built in optimize_external_refs.  */
8016   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8017     if (AT_class (a) == dw_val_class_die_ref
8018         && (c = AT_ref (a))->die_mark == 0)
8019       {
8020         struct external_ref *ref_p;
8021         gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
8022
8023         ref_p = lookup_external_ref (extern_map, c);
8024         if (ref_p->stub && ref_p->stub != die)
8025           change_AT_die_ref (a, ref_p->stub);
8026         else
8027           /* We aren't changing this reference, so mark it external.  */
8028           set_AT_ref_external (a, 1);
8029       }
8030
8031   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8032     {
8033       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
8034       dw_attr_node *die_a, *abbrev_a;
8035       unsigned ix;
8036       bool ok = true;
8037
8038       if (abbrev->die_tag != die->die_tag)
8039         continue;
8040       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8041         continue;
8042
8043       if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
8044         continue;
8045
8046       FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
8047         {
8048           abbrev_a = &(*abbrev->die_attr)[ix];
8049           if ((abbrev_a->dw_attr != die_a->dw_attr)
8050               || (value_format (abbrev_a) != value_format (die_a)))
8051             {
8052               ok = false;
8053               break;
8054             }
8055         }
8056       if (ok)
8057         break;
8058     }
8059
8060   if (abbrev_id >= abbrev_die_table_in_use)
8061     {
8062       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
8063         {
8064           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
8065           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
8066                                             n_alloc);
8067
8068           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
8069                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
8070           abbrev_die_table_allocated = n_alloc;
8071         }
8072
8073       ++abbrev_die_table_in_use;
8074       abbrev_die_table[abbrev_id] = die;
8075     }
8076
8077   die->die_abbrev = abbrev_id;
8078   FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
8079 }
8080 \f
8081 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
8082
8083 static int
8084 constant_size (unsigned HOST_WIDE_INT value)
8085 {
8086   int log;
8087
8088   if (value == 0)
8089     log = 0;
8090   else
8091     log = floor_log2 (value);
8092
8093   log = log / 8;
8094   log = 1 << (floor_log2 (log) + 1);
8095
8096   return log;
8097 }
8098
8099 /* Return the size of a DIE as it is represented in the
8100    .debug_info section.  */
8101
8102 static unsigned long
8103 size_of_die (dw_die_ref die)
8104 {
8105   unsigned long size = 0;
8106   dw_attr_node *a;
8107   unsigned ix;
8108   enum dwarf_form form;
8109
8110   size += size_of_uleb128 (die->die_abbrev);
8111   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8112     {
8113       switch (AT_class (a))
8114         {
8115         case dw_val_class_addr:
8116           if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8117             {
8118               gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8119               size += size_of_uleb128 (AT_index (a));
8120             }
8121           else
8122             size += DWARF2_ADDR_SIZE;
8123           break;
8124         case dw_val_class_offset:
8125           size += DWARF_OFFSET_SIZE;
8126           break;
8127         case dw_val_class_loc:
8128           {
8129             unsigned long lsize = size_of_locs (AT_loc (a));
8130
8131             /* Block length.  */
8132             if (dwarf_version >= 4)
8133               size += size_of_uleb128 (lsize);
8134             else
8135               size += constant_size (lsize);
8136             size += lsize;
8137           }
8138           break;
8139         case dw_val_class_loc_list:
8140           if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8141             {
8142               gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8143               size += size_of_uleb128 (AT_index (a));
8144             }
8145           else
8146             size += DWARF_OFFSET_SIZE;
8147           break;
8148         case dw_val_class_range_list:
8149           size += DWARF_OFFSET_SIZE;
8150           break;
8151         case dw_val_class_const:
8152           size += size_of_sleb128 (AT_int (a));
8153           break;
8154         case dw_val_class_unsigned_const:
8155           {
8156             int csize = constant_size (AT_unsigned (a));
8157             if (dwarf_version == 3
8158                 && a->dw_attr == DW_AT_data_member_location
8159                 && csize >= 4)
8160               size += size_of_uleb128 (AT_unsigned (a));
8161             else
8162               size += csize;
8163           }
8164           break;
8165         case dw_val_class_const_double:
8166           size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
8167           if (HOST_BITS_PER_WIDE_INT >= 64)
8168             size++; /* block */
8169           break;
8170         case dw_val_class_wide_int:
8171           size += (get_full_len (*a->dw_attr_val.v.val_wide)
8172                    * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
8173           if (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT
8174               > 64)
8175             size++; /* block */
8176           break;
8177         case dw_val_class_vec:
8178           size += constant_size (a->dw_attr_val.v.val_vec.length
8179                                  * a->dw_attr_val.v.val_vec.elt_size)
8180                   + a->dw_attr_val.v.val_vec.length
8181                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
8182           break;
8183         case dw_val_class_flag:
8184           if (dwarf_version >= 4)
8185             /* Currently all add_AT_flag calls pass in 1 as last argument,
8186                so DW_FORM_flag_present can be used.  If that ever changes,
8187                we'll need to use DW_FORM_flag and have some optimization
8188                in build_abbrev_table that will change those to
8189                DW_FORM_flag_present if it is set to 1 in all DIEs using
8190                the same abbrev entry.  */
8191             gcc_assert (a->dw_attr_val.v.val_flag == 1);
8192           else
8193             size += 1;
8194           break;
8195         case dw_val_class_die_ref:
8196           if (AT_ref_external (a))
8197             {
8198               /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
8199                  we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
8200                  is sized by target address length, whereas in DWARF3
8201                  it's always sized as an offset.  */
8202               if (use_debug_types)
8203                 size += DWARF_TYPE_SIGNATURE_SIZE;
8204               else if (dwarf_version == 2)
8205                 size += DWARF2_ADDR_SIZE;
8206               else
8207                 size += DWARF_OFFSET_SIZE;
8208             }
8209           else
8210             size += DWARF_OFFSET_SIZE;
8211           break;
8212         case dw_val_class_fde_ref:
8213           size += DWARF_OFFSET_SIZE;
8214           break;
8215         case dw_val_class_lbl_id:
8216           if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8217             {
8218               gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8219               size += size_of_uleb128 (AT_index (a));
8220             }
8221           else
8222             size += DWARF2_ADDR_SIZE;
8223           break;
8224         case dw_val_class_lineptr:
8225         case dw_val_class_macptr:
8226           size += DWARF_OFFSET_SIZE;
8227           break;
8228         case dw_val_class_str:
8229           form = AT_string_form (a);
8230           if (form == DW_FORM_strp)
8231             size += DWARF_OFFSET_SIZE;
8232          else if (form == DW_FORM_GNU_str_index)
8233             size += size_of_uleb128 (AT_index (a));
8234           else
8235             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8236           break;
8237         case dw_val_class_file:
8238           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8239           break;
8240         case dw_val_class_data8:
8241           size += 8;
8242           break;
8243         case dw_val_class_vms_delta:
8244           size += DWARF_OFFSET_SIZE;
8245           break;
8246         case dw_val_class_high_pc:
8247           size += DWARF2_ADDR_SIZE;
8248           break;
8249         default:
8250           gcc_unreachable ();
8251         }
8252     }
8253
8254   return size;
8255 }
8256
8257 /* Size the debugging information associated with a given DIE.  Visits the
8258    DIE's children recursively.  Updates the global variable next_die_offset, on
8259    each time through.  Uses the current value of next_die_offset to update the
8260    die_offset field in each DIE.  */
8261
8262 static void
8263 calc_die_sizes (dw_die_ref die)
8264 {
8265   dw_die_ref c;
8266
8267   gcc_assert (die->die_offset == 0
8268               || (unsigned long int) die->die_offset == next_die_offset);
8269   die->die_offset = next_die_offset;
8270   next_die_offset += size_of_die (die);
8271
8272   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8273
8274   if (die->die_child != NULL)
8275     /* Count the null byte used to terminate sibling lists.  */
8276     next_die_offset += 1;
8277 }
8278
8279 /* Size just the base type children at the start of the CU.
8280    This is needed because build_abbrev needs to size locs
8281    and sizing of type based stack ops needs to know die_offset
8282    values for the base types.  */
8283
8284 static void
8285 calc_base_type_die_sizes (void)
8286 {
8287   unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8288   unsigned int i;
8289   dw_die_ref base_type;
8290 #if ENABLE_ASSERT_CHECKING
8291   dw_die_ref prev = comp_unit_die ()->die_child;
8292 #endif
8293
8294   die_offset += size_of_die (comp_unit_die ());
8295   for (i = 0; base_types.iterate (i, &base_type); i++)
8296     {
8297 #if ENABLE_ASSERT_CHECKING
8298       gcc_assert (base_type->die_offset == 0
8299                   && prev->die_sib == base_type
8300                   && base_type->die_child == NULL
8301                   && base_type->die_abbrev);
8302       prev = base_type;
8303 #endif
8304       base_type->die_offset = die_offset;
8305       die_offset += size_of_die (base_type);
8306     }
8307 }
8308
8309 /* Set the marks for a die and its children.  We do this so
8310    that we know whether or not a reference needs to use FORM_ref_addr; only
8311    DIEs in the same CU will be marked.  We used to clear out the offset
8312    and use that as the flag, but ran into ordering problems.  */
8313
8314 static void
8315 mark_dies (dw_die_ref die)
8316 {
8317   dw_die_ref c;
8318
8319   gcc_assert (!die->die_mark);
8320
8321   die->die_mark = 1;
8322   FOR_EACH_CHILD (die, c, mark_dies (c));
8323 }
8324
8325 /* Clear the marks for a die and its children.  */
8326
8327 static void
8328 unmark_dies (dw_die_ref die)
8329 {
8330   dw_die_ref c;
8331
8332   if (! use_debug_types)
8333     gcc_assert (die->die_mark);
8334
8335   die->die_mark = 0;
8336   FOR_EACH_CHILD (die, c, unmark_dies (c));
8337 }
8338
8339 /* Clear the marks for a die, its children and referred dies.  */
8340
8341 static void
8342 unmark_all_dies (dw_die_ref die)
8343 {
8344   dw_die_ref c;
8345   dw_attr_node *a;
8346   unsigned ix;
8347
8348   if (!die->die_mark)
8349     return;
8350   die->die_mark = 0;
8351
8352   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8353
8354   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8355     if (AT_class (a) == dw_val_class_die_ref)
8356       unmark_all_dies (AT_ref (a));
8357 }
8358
8359 /* Calculate if the entry should appear in the final output file.  It may be
8360    from a pruned a type.  */
8361
8362 static bool
8363 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
8364 {
8365   /* By limiting gnu pubnames to definitions only, gold can generate a
8366      gdb index without entries for declarations, which don't include
8367      enough information to be useful.  */
8368   if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
8369     return false;
8370
8371   if (table == pubname_table)
8372     {
8373       /* Enumerator names are part of the pubname table, but the
8374          parent DW_TAG_enumeration_type die may have been pruned.
8375          Don't output them if that is the case.  */
8376       if (p->die->die_tag == DW_TAG_enumerator &&
8377           (p->die->die_parent == NULL
8378            || !p->die->die_parent->die_perennial_p))
8379         return false;
8380
8381       /* Everything else in the pubname table is included.  */
8382       return true;
8383     }
8384
8385   /* The pubtypes table shouldn't include types that have been
8386      pruned.  */
8387   return (p->die->die_offset != 0
8388           || !flag_eliminate_unused_debug_types);
8389 }
8390
8391 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8392    generated for the compilation unit.  */
8393
8394 static unsigned long
8395 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8396 {
8397   unsigned long size;
8398   unsigned i;
8399   pubname_entry *p;
8400   int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8401
8402   size = DWARF_PUBNAMES_HEADER_SIZE;
8403   FOR_EACH_VEC_ELT (*names, i, p)
8404     if (include_pubname_in_output (names, p))
8405       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8406
8407   size += DWARF_OFFSET_SIZE;
8408   return size;
8409 }
8410
8411 /* Return the size of the information in the .debug_aranges section.  */
8412
8413 static unsigned long
8414 size_of_aranges (void)
8415 {
8416   unsigned long size;
8417
8418   size = DWARF_ARANGES_HEADER_SIZE;
8419
8420   /* Count the address/length pair for this compilation unit.  */
8421   if (text_section_used)
8422     size += 2 * DWARF2_ADDR_SIZE;
8423   if (cold_text_section_used)
8424     size += 2 * DWARF2_ADDR_SIZE;
8425   if (have_multiple_function_sections)
8426     {
8427       unsigned fde_idx;
8428       dw_fde_ref fde;
8429
8430       FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8431         {
8432           if (DECL_IGNORED_P (fde->decl))
8433             continue;
8434           if (!fde->in_std_section)
8435             size += 2 * DWARF2_ADDR_SIZE;
8436           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8437             size += 2 * DWARF2_ADDR_SIZE;
8438         }
8439     }
8440
8441   /* Count the two zero words used to terminated the address range table.  */
8442   size += 2 * DWARF2_ADDR_SIZE;
8443   return size;
8444 }
8445 \f
8446 /* Select the encoding of an attribute value.  */
8447
8448 static enum dwarf_form
8449 value_format (dw_attr_node *a)
8450 {
8451   switch (AT_class (a))
8452     {
8453     case dw_val_class_addr:
8454       /* Only very few attributes allow DW_FORM_addr.  */
8455       switch (a->dw_attr)
8456         {
8457         case DW_AT_low_pc:
8458         case DW_AT_high_pc:
8459         case DW_AT_entry_pc:
8460         case DW_AT_trampoline:
8461           return (AT_index (a) == NOT_INDEXED
8462                   ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8463         default:
8464           break;
8465         }
8466       switch (DWARF2_ADDR_SIZE)
8467         {
8468         case 1:
8469           return DW_FORM_data1;
8470         case 2:
8471           return DW_FORM_data2;
8472         case 4:
8473           return DW_FORM_data4;
8474         case 8:
8475           return DW_FORM_data8;
8476         default:
8477           gcc_unreachable ();
8478         }
8479     case dw_val_class_range_list:
8480     case dw_val_class_loc_list:
8481       if (dwarf_version >= 4)
8482         return DW_FORM_sec_offset;
8483       /* FALLTHRU */
8484     case dw_val_class_vms_delta:
8485     case dw_val_class_offset:
8486       switch (DWARF_OFFSET_SIZE)
8487         {
8488         case 4:
8489           return DW_FORM_data4;
8490         case 8:
8491           return DW_FORM_data8;
8492         default:
8493           gcc_unreachable ();
8494         }
8495     case dw_val_class_loc:
8496       if (dwarf_version >= 4)
8497         return DW_FORM_exprloc;
8498       switch (constant_size (size_of_locs (AT_loc (a))))
8499         {
8500         case 1:
8501           return DW_FORM_block1;
8502         case 2:
8503           return DW_FORM_block2;
8504         case 4:
8505           return DW_FORM_block4;
8506         default:
8507           gcc_unreachable ();
8508         }
8509     case dw_val_class_const:
8510       return DW_FORM_sdata;
8511     case dw_val_class_unsigned_const:
8512       switch (constant_size (AT_unsigned (a)))
8513         {
8514         case 1:
8515           return DW_FORM_data1;
8516         case 2:
8517           return DW_FORM_data2;
8518         case 4:
8519           /* In DWARF3 DW_AT_data_member_location with
8520              DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8521              constant, so we need to use DW_FORM_udata if we need
8522              a large constant.  */
8523           if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8524             return DW_FORM_udata;
8525           return DW_FORM_data4;
8526         case 8:
8527           if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8528             return DW_FORM_udata;
8529           return DW_FORM_data8;
8530         default:
8531           gcc_unreachable ();
8532         }
8533     case dw_val_class_const_double:
8534       switch (HOST_BITS_PER_WIDE_INT)
8535         {
8536         case 8:
8537           return DW_FORM_data2;
8538         case 16:
8539           return DW_FORM_data4;
8540         case 32:
8541           return DW_FORM_data8;
8542         case 64:
8543         default:
8544           return DW_FORM_block1;
8545         }
8546     case dw_val_class_wide_int:
8547       switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
8548         {
8549         case 8:
8550           return DW_FORM_data1;
8551         case 16:
8552           return DW_FORM_data2;
8553         case 32:
8554           return DW_FORM_data4;
8555         case 64:
8556           return DW_FORM_data8;
8557         default:
8558           return DW_FORM_block1;
8559         }
8560     case dw_val_class_vec:
8561       switch (constant_size (a->dw_attr_val.v.val_vec.length
8562                              * a->dw_attr_val.v.val_vec.elt_size))
8563         {
8564         case 1:
8565           return DW_FORM_block1;
8566         case 2:
8567           return DW_FORM_block2;
8568         case 4:
8569           return DW_FORM_block4;
8570         default:
8571           gcc_unreachable ();
8572         }
8573     case dw_val_class_flag:
8574       if (dwarf_version >= 4)
8575         {
8576           /* Currently all add_AT_flag calls pass in 1 as last argument,
8577              so DW_FORM_flag_present can be used.  If that ever changes,
8578              we'll need to use DW_FORM_flag and have some optimization
8579              in build_abbrev_table that will change those to
8580              DW_FORM_flag_present if it is set to 1 in all DIEs using
8581              the same abbrev entry.  */
8582           gcc_assert (a->dw_attr_val.v.val_flag == 1);
8583           return DW_FORM_flag_present;
8584         }
8585       return DW_FORM_flag;
8586     case dw_val_class_die_ref:
8587       if (AT_ref_external (a))
8588         return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8589       else
8590         return DW_FORM_ref;
8591     case dw_val_class_fde_ref:
8592       return DW_FORM_data;
8593     case dw_val_class_lbl_id:
8594       return (AT_index (a) == NOT_INDEXED
8595               ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8596     case dw_val_class_lineptr:
8597     case dw_val_class_macptr:
8598       return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8599     case dw_val_class_str:
8600       return AT_string_form (a);
8601     case dw_val_class_file:
8602       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8603         {
8604         case 1:
8605           return DW_FORM_data1;
8606         case 2:
8607           return DW_FORM_data2;
8608         case 4:
8609           return DW_FORM_data4;
8610         default:
8611           gcc_unreachable ();
8612         }
8613
8614     case dw_val_class_data8:
8615       return DW_FORM_data8;
8616
8617     case dw_val_class_high_pc:
8618       switch (DWARF2_ADDR_SIZE)
8619         {
8620         case 1:
8621           return DW_FORM_data1;
8622         case 2:
8623           return DW_FORM_data2;
8624         case 4:
8625           return DW_FORM_data4;
8626         case 8:
8627           return DW_FORM_data8;
8628         default:
8629           gcc_unreachable ();
8630         }
8631
8632     default:
8633       gcc_unreachable ();
8634     }
8635 }
8636
8637 /* Output the encoding of an attribute value.  */
8638
8639 static void
8640 output_value_format (dw_attr_node *a)
8641 {
8642   enum dwarf_form form = value_format (a);
8643
8644   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8645 }
8646
8647 /* Given a die and id, produce the appropriate abbreviations.  */
8648
8649 static void
8650 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8651 {
8652   unsigned ix;
8653   dw_attr_node *a_attr;
8654
8655   dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8656   dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8657                                dwarf_tag_name (abbrev->die_tag));
8658
8659   if (abbrev->die_child != NULL)
8660     dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8661   else
8662     dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8663
8664   for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8665     {
8666       dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8667                                    dwarf_attr_name (a_attr->dw_attr));
8668       output_value_format (a_attr);
8669     }
8670
8671   dw2_asm_output_data (1, 0, NULL);
8672   dw2_asm_output_data (1, 0, NULL);
8673 }
8674
8675
8676 /* Output the .debug_abbrev section which defines the DIE abbreviation
8677    table.  */
8678
8679 static void
8680 output_abbrev_section (void)
8681 {
8682   unsigned long abbrev_id;
8683
8684   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8685     output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8686
8687   /* Terminate the table.  */
8688   dw2_asm_output_data (1, 0, NULL);
8689 }
8690
8691 /* Output a symbol we can use to refer to this DIE from another CU.  */
8692
8693 static inline void
8694 output_die_symbol (dw_die_ref die)
8695 {
8696   const char *sym = die->die_id.die_symbol;
8697
8698   gcc_assert (!die->comdat_type_p);
8699
8700   if (sym == 0)
8701     return;
8702
8703   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8704     /* We make these global, not weak; if the target doesn't support
8705        .linkonce, it doesn't support combining the sections, so debugging
8706        will break.  */
8707     targetm.asm_out.globalize_label (asm_out_file, sym);
8708
8709   ASM_OUTPUT_LABEL (asm_out_file, sym);
8710 }
8711
8712 /* Return a new location list, given the begin and end range, and the
8713    expression.  */
8714
8715 static inline dw_loc_list_ref
8716 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8717               const char *section)
8718 {
8719   dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
8720
8721   retlist->begin = begin;
8722   retlist->begin_entry = NULL;
8723   retlist->end = end;
8724   retlist->expr = expr;
8725   retlist->section = section;
8726
8727   return retlist;
8728 }
8729
8730 /* Generate a new internal symbol for this location list node, if it
8731    hasn't got one yet.  */
8732
8733 static inline void
8734 gen_llsym (dw_loc_list_ref list)
8735 {
8736   gcc_assert (!list->ll_symbol);
8737   list->ll_symbol = gen_internal_sym ("LLST");
8738 }
8739
8740 /* Output the location list given to us.  */
8741
8742 static void
8743 output_loc_list (dw_loc_list_ref list_head)
8744 {
8745   dw_loc_list_ref curr = list_head;
8746
8747   if (list_head->emitted)
8748     return;
8749   list_head->emitted = true;
8750
8751   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8752
8753   /* Walk the location list, and output each range + expression.  */
8754   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8755     {
8756       unsigned long size;
8757       /* Don't output an entry that starts and ends at the same address.  */
8758       if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8759         continue;
8760       size = size_of_locs (curr->expr);
8761       /* If the expression is too large, drop it on the floor.  We could
8762          perhaps put it into DW_TAG_dwarf_procedure and refer to that
8763          in the expression, but >= 64KB expressions for a single value
8764          in a single range are unlikely very useful.  */
8765       if (size > 0xffff)
8766         continue;
8767       if (dwarf_split_debug_info)
8768         {
8769           dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8770                                "Location list start/length entry (%s)",
8771                                list_head->ll_symbol);
8772           dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8773                                        "Location list range start index (%s)",
8774                                        curr->begin);
8775           /* The length field is 4 bytes.  If we ever need to support
8776             an 8-byte length, we can add a new DW_LLE code or fall back
8777             to DW_LLE_GNU_start_end_entry.  */
8778           dw2_asm_output_delta (4, curr->end, curr->begin,
8779                                 "Location list range length (%s)",
8780                                 list_head->ll_symbol);
8781         }
8782       else if (!have_multiple_function_sections)
8783         {
8784           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8785                                 "Location list begin address (%s)",
8786                                 list_head->ll_symbol);
8787           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8788                                 "Location list end address (%s)",
8789                                 list_head->ll_symbol);
8790         }
8791       else
8792         {
8793           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8794                                "Location list begin address (%s)",
8795                                list_head->ll_symbol);
8796           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8797                                "Location list end address (%s)",
8798                                list_head->ll_symbol);
8799         }
8800
8801       /* Output the block length for this list of location operations.  */
8802       gcc_assert (size <= 0xffff);
8803       dw2_asm_output_data (2, size, "%s", "Location expression size");
8804
8805       output_loc_sequence (curr->expr, -1);
8806     }
8807
8808   if (dwarf_split_debug_info)
8809     dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8810                          "Location list terminator (%s)",
8811                          list_head->ll_symbol);
8812   else
8813     {
8814       dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8815                            "Location list terminator begin (%s)",
8816                            list_head->ll_symbol);
8817       dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8818                            "Location list terminator end (%s)",
8819                            list_head->ll_symbol);
8820     }
8821 }
8822
8823 /* Output a range_list offset into the debug_range section.  Emit a
8824    relocated reference if val_entry is NULL, otherwise, emit an
8825    indirect reference.  */
8826
8827 static void
8828 output_range_list_offset (dw_attr_node *a)
8829 {
8830   const char *name = dwarf_attr_name (a->dw_attr);
8831
8832   if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8833     {
8834       char *p = strchr (ranges_section_label, '\0');
8835       sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8836       dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8837                              debug_ranges_section, "%s", name);
8838       *p = '\0';
8839     }
8840   else
8841     dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8842                          "%s (offset from %s)", name, ranges_section_label);
8843 }
8844
8845 /* Output the offset into the debug_loc section.  */
8846
8847 static void
8848 output_loc_list_offset (dw_attr_node *a)
8849 {
8850   char *sym = AT_loc_list (a)->ll_symbol;
8851
8852   gcc_assert (sym);
8853   if (dwarf_split_debug_info)
8854     dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8855                           "%s", dwarf_attr_name (a->dw_attr));
8856   else
8857     dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8858                            "%s", dwarf_attr_name (a->dw_attr));
8859 }
8860
8861 /* Output an attribute's index or value appropriately.  */
8862
8863 static void
8864 output_attr_index_or_value (dw_attr_node *a)
8865 {
8866   const char *name = dwarf_attr_name (a->dw_attr);
8867
8868   if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8869     {
8870       dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8871       return;
8872     }
8873   switch (AT_class (a))
8874     {
8875       case dw_val_class_addr:
8876         dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8877         break;
8878       case dw_val_class_high_pc:
8879       case dw_val_class_lbl_id:
8880         dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8881         break;
8882       case dw_val_class_loc_list:
8883         output_loc_list_offset (a);
8884         break;
8885       default:
8886         gcc_unreachable ();
8887     }
8888 }
8889
8890 /* Output a type signature.  */
8891
8892 static inline void
8893 output_signature (const char *sig, const char *name)
8894 {
8895   int i;
8896
8897   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8898     dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8899 }
8900
8901 /* Output the DIE and its attributes.  Called recursively to generate
8902    the definitions of each child DIE.  */
8903
8904 static void
8905 output_die (dw_die_ref die)
8906 {
8907   dw_attr_node *a;
8908   dw_die_ref c;
8909   unsigned long size;
8910   unsigned ix;
8911
8912   /* If someone in another CU might refer to us, set up a symbol for
8913      them to point to.  */
8914   if (! die->comdat_type_p && die->die_id.die_symbol)
8915     output_die_symbol (die);
8916
8917   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8918                                (unsigned long)die->die_offset,
8919                                dwarf_tag_name (die->die_tag));
8920
8921   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8922     {
8923       const char *name = dwarf_attr_name (a->dw_attr);
8924
8925       switch (AT_class (a))
8926         {
8927         case dw_val_class_addr:
8928           output_attr_index_or_value (a);
8929           break;
8930
8931         case dw_val_class_offset:
8932           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8933                                "%s", name);
8934           break;
8935
8936         case dw_val_class_range_list:
8937           output_range_list_offset (a);
8938           break;
8939
8940         case dw_val_class_loc:
8941           size = size_of_locs (AT_loc (a));
8942
8943           /* Output the block length for this list of location operations.  */
8944           if (dwarf_version >= 4)
8945             dw2_asm_output_data_uleb128 (size, "%s", name);
8946           else
8947             dw2_asm_output_data (constant_size (size), size, "%s", name);
8948
8949           output_loc_sequence (AT_loc (a), -1);
8950           break;
8951
8952         case dw_val_class_const:
8953           /* ??? It would be slightly more efficient to use a scheme like is
8954              used for unsigned constants below, but gdb 4.x does not sign
8955              extend.  Gdb 5.x does sign extend.  */
8956           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8957           break;
8958
8959         case dw_val_class_unsigned_const:
8960           {
8961             int csize = constant_size (AT_unsigned (a));
8962             if (dwarf_version == 3
8963                 && a->dw_attr == DW_AT_data_member_location
8964                 && csize >= 4)
8965               dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8966             else
8967               dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8968           }
8969           break;
8970
8971         case dw_val_class_const_double:
8972           {
8973             unsigned HOST_WIDE_INT first, second;
8974
8975             if (HOST_BITS_PER_WIDE_INT >= 64)
8976               dw2_asm_output_data (1,
8977                                    HOST_BITS_PER_DOUBLE_INT
8978                                    / HOST_BITS_PER_CHAR,
8979                                    NULL);
8980
8981             if (WORDS_BIG_ENDIAN)
8982               {
8983                 first = a->dw_attr_val.v.val_double.high;
8984                 second = a->dw_attr_val.v.val_double.low;
8985               }
8986             else
8987               {
8988                 first = a->dw_attr_val.v.val_double.low;
8989                 second = a->dw_attr_val.v.val_double.high;
8990               }
8991
8992             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8993                                  first, "%s", name);
8994             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8995                                  second, NULL);
8996           }
8997           break;
8998
8999         case dw_val_class_wide_int:
9000           {
9001             int i;
9002             int len = get_full_len (*a->dw_attr_val.v.val_wide);
9003             int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
9004             if (len * HOST_BITS_PER_WIDE_INT > 64)
9005               dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide) * l,
9006                                    NULL);
9007
9008             if (WORDS_BIG_ENDIAN)
9009               for (i = len - 1; i >= 0; --i)
9010                 {
9011                   dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
9012                                        "%s", name);
9013                   name = NULL;
9014                 }
9015             else
9016               for (i = 0; i < len; ++i)
9017                 {
9018                   dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
9019                                        "%s", name);
9020                   name = NULL;
9021                 }
9022           }
9023           break;
9024
9025         case dw_val_class_vec:
9026           {
9027             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
9028             unsigned int len = a->dw_attr_val.v.val_vec.length;
9029             unsigned int i;
9030             unsigned char *p;
9031
9032             dw2_asm_output_data (constant_size (len * elt_size),
9033                                  len * elt_size, "%s", name);
9034             if (elt_size > sizeof (HOST_WIDE_INT))
9035               {
9036                 elt_size /= 2;
9037                 len *= 2;
9038               }
9039             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
9040                  i < len;
9041                  i++, p += elt_size)
9042               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
9043                                    "fp or vector constant word %u", i);
9044             break;
9045           }
9046
9047         case dw_val_class_flag:
9048           if (dwarf_version >= 4)
9049             {
9050               /* Currently all add_AT_flag calls pass in 1 as last argument,
9051                  so DW_FORM_flag_present can be used.  If that ever changes,
9052                  we'll need to use DW_FORM_flag and have some optimization
9053                  in build_abbrev_table that will change those to
9054                  DW_FORM_flag_present if it is set to 1 in all DIEs using
9055                  the same abbrev entry.  */
9056               gcc_assert (AT_flag (a) == 1);
9057               if (flag_debug_asm)
9058                 fprintf (asm_out_file, "\t\t\t%s %s\n",
9059                          ASM_COMMENT_START, name);
9060               break;
9061             }
9062           dw2_asm_output_data (1, AT_flag (a), "%s", name);
9063           break;
9064
9065         case dw_val_class_loc_list:
9066           output_attr_index_or_value (a);
9067           break;
9068
9069         case dw_val_class_die_ref:
9070           if (AT_ref_external (a))
9071             {
9072               if (AT_ref (a)->comdat_type_p)
9073                 {
9074                   comdat_type_node *type_node =
9075                     AT_ref (a)->die_id.die_type_node;
9076
9077                   gcc_assert (type_node);
9078                   output_signature (type_node->signature, name);
9079                 }
9080               else
9081                 {
9082                   const char *sym = AT_ref (a)->die_id.die_symbol;
9083                   int size;
9084
9085                   gcc_assert (sym);
9086                   /* In DWARF2, DW_FORM_ref_addr is sized by target address
9087                      length, whereas in DWARF3 it's always sized as an
9088                      offset.  */
9089                   if (dwarf_version == 2)
9090                     size = DWARF2_ADDR_SIZE;
9091                   else
9092                     size = DWARF_OFFSET_SIZE;
9093                   dw2_asm_output_offset (size, sym, debug_info_section, "%s",
9094                                          name);
9095                 }
9096             }
9097           else
9098             {
9099               gcc_assert (AT_ref (a)->die_offset);
9100               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
9101                                    "%s", name);
9102             }
9103           break;
9104
9105         case dw_val_class_fde_ref:
9106           {
9107             char l1[20];
9108
9109             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
9110                                          a->dw_attr_val.v.val_fde_index * 2);
9111             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
9112                                    "%s", name);
9113           }
9114           break;
9115
9116         case dw_val_class_vms_delta:
9117 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
9118           dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
9119                                     AT_vms_delta2 (a), AT_vms_delta1 (a),
9120                                     "%s", name);
9121 #else
9122           dw2_asm_output_delta (DWARF_OFFSET_SIZE,
9123                                 AT_vms_delta2 (a), AT_vms_delta1 (a),
9124                                 "%s", name);
9125 #endif
9126           break;
9127
9128         case dw_val_class_lbl_id:
9129           output_attr_index_or_value (a);
9130           break;
9131
9132         case dw_val_class_lineptr:
9133           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9134                                  debug_line_section, "%s", name);
9135           break;
9136
9137         case dw_val_class_macptr:
9138           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9139                                  debug_macinfo_section, "%s", name);
9140           break;
9141
9142         case dw_val_class_str:
9143           if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
9144             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9145                                    a->dw_attr_val.v.val_str->label,
9146                                    debug_str_section,
9147                                    "%s: \"%s\"", name, AT_string (a));
9148           else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
9149             dw2_asm_output_data_uleb128 (AT_index (a),
9150                                          "%s: \"%s\"", name, AT_string (a));
9151           else
9152             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
9153           break;
9154
9155         case dw_val_class_file:
9156           {
9157             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
9158
9159             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
9160                                  a->dw_attr_val.v.val_file->filename);
9161             break;
9162           }
9163
9164         case dw_val_class_data8:
9165           {
9166             int i;
9167
9168             for (i = 0; i < 8; i++)
9169               dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
9170                                    i == 0 ? "%s" : NULL, name);
9171             break;
9172           }
9173
9174         case dw_val_class_high_pc:
9175           dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
9176                                 get_AT_low_pc (die), "DW_AT_high_pc");
9177           break;
9178
9179         default:
9180           gcc_unreachable ();
9181         }
9182     }
9183
9184   FOR_EACH_CHILD (die, c, output_die (c));
9185
9186   /* Add null byte to terminate sibling list.  */
9187   if (die->die_child != NULL)
9188     dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
9189                          (unsigned long) die->die_offset);
9190 }
9191
9192 /* Output the compilation unit that appears at the beginning of the
9193    .debug_info section, and precedes the DIE descriptions.  */
9194
9195 static void
9196 output_compilation_unit_header (void)
9197 {
9198   /* We don't support actual DWARFv5 units yet, we just use some
9199      DWARFv5 draft DIE tags in DWARFv4 format.  */
9200   int ver = dwarf_version < 5 ? dwarf_version : 4;
9201
9202   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9203     dw2_asm_output_data (4, 0xffffffff,
9204       "Initial length escape value indicating 64-bit DWARF extension");
9205   dw2_asm_output_data (DWARF_OFFSET_SIZE,
9206                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
9207                        "Length of Compilation Unit Info");
9208   dw2_asm_output_data (2, ver, "DWARF version number");
9209   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
9210                          debug_abbrev_section,
9211                          "Offset Into Abbrev. Section");
9212   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9213 }
9214
9215 /* Output the compilation unit DIE and its children.  */
9216
9217 static void
9218 output_comp_unit (dw_die_ref die, int output_if_empty)
9219 {
9220   const char *secname, *oldsym;
9221   char *tmp;
9222
9223   /* Unless we are outputting main CU, we may throw away empty ones.  */
9224   if (!output_if_empty && die->die_child == NULL)
9225     return;
9226
9227   /* Even if there are no children of this DIE, we must output the information
9228      about the compilation unit.  Otherwise, on an empty translation unit, we
9229      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
9230      will then complain when examining the file.  First mark all the DIEs in
9231      this CU so we know which get local refs.  */
9232   mark_dies (die);
9233
9234   external_ref_hash_type *extern_map = optimize_external_refs (die);
9235
9236   build_abbrev_table (die, extern_map);
9237
9238   delete extern_map;
9239
9240   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
9241   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
9242   calc_die_sizes (die);
9243
9244   oldsym = die->die_id.die_symbol;
9245   if (oldsym)
9246     {
9247       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
9248
9249       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
9250       secname = tmp;
9251       die->die_id.die_symbol = NULL;
9252       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9253     }
9254   else
9255     {
9256       switch_to_section (debug_info_section);
9257       ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
9258       info_section_emitted = true;
9259     }
9260
9261   /* Output debugging information.  */
9262   output_compilation_unit_header ();
9263   output_die (die);
9264
9265   /* Leave the marks on the main CU, so we can check them in
9266      output_pubnames.  */
9267   if (oldsym)
9268     {
9269       unmark_dies (die);
9270       die->die_id.die_symbol = oldsym;
9271     }
9272 }
9273
9274 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
9275    and .debug_pubtypes.  This is configured per-target, but can be
9276    overridden by the -gpubnames or -gno-pubnames options.  */
9277
9278 static inline bool
9279 want_pubnames (void)
9280 {
9281   if (debug_info_level <= DINFO_LEVEL_TERSE)
9282     return false;
9283   if (debug_generate_pub_sections != -1)
9284     return debug_generate_pub_sections;
9285   return targetm.want_debug_pub_sections;
9286 }
9287
9288 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes.  */
9289
9290 static void
9291 add_AT_pubnames (dw_die_ref die)
9292 {
9293   if (want_pubnames ())
9294     add_AT_flag (die, DW_AT_GNU_pubnames, 1);
9295 }
9296
9297 /* Add a string attribute value to a skeleton DIE.  */
9298
9299 static inline void
9300 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
9301                         const char *str)
9302 {
9303   dw_attr_node attr;
9304   struct indirect_string_node *node;
9305
9306   if (! skeleton_debug_str_hash)
9307     skeleton_debug_str_hash
9308       = hash_table<indirect_string_hasher>::create_ggc (10);
9309
9310   node = find_AT_string_in_table (str, skeleton_debug_str_hash);
9311   find_string_form (node);
9312   if (node->form == DW_FORM_GNU_str_index)
9313     node->form = DW_FORM_strp;
9314
9315   attr.dw_attr = attr_kind;
9316   attr.dw_attr_val.val_class = dw_val_class_str;
9317   attr.dw_attr_val.val_entry = NULL;
9318   attr.dw_attr_val.v.val_str = node;
9319   add_dwarf_attr (die, &attr);
9320 }
9321
9322 /* Helper function to generate top-level dies for skeleton debug_info and
9323    debug_types.  */
9324
9325 static void
9326 add_top_level_skeleton_die_attrs (dw_die_ref die)
9327 {
9328   const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
9329   const char *comp_dir = comp_dir_string ();
9330
9331   add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
9332   if (comp_dir != NULL)
9333     add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
9334   add_AT_pubnames (die);
9335   add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
9336 }
9337
9338 /* Output skeleton debug sections that point to the dwo file.  */
9339
9340 static void
9341 output_skeleton_debug_sections (dw_die_ref comp_unit)
9342 {
9343   /* We don't support actual DWARFv5 units yet, we just use some
9344      DWARFv5 draft DIE tags in DWARFv4 format.  */
9345   int ver = dwarf_version < 5 ? dwarf_version : 4;
9346
9347   /* These attributes will be found in the full debug_info section.  */
9348   remove_AT (comp_unit, DW_AT_producer);
9349   remove_AT (comp_unit, DW_AT_language);
9350
9351   switch_to_section (debug_skeleton_info_section);
9352   ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
9353
9354   /* Produce the skeleton compilation-unit header.  This one differs enough from
9355      a normal CU header that it's better not to call output_compilation_unit
9356      header.  */
9357   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9358     dw2_asm_output_data (4, 0xffffffff,
9359       "Initial length escape value indicating 64-bit DWARF extension");
9360
9361   dw2_asm_output_data (DWARF_OFFSET_SIZE,
9362                        DWARF_COMPILE_UNIT_HEADER_SIZE
9363                        - DWARF_INITIAL_LENGTH_SIZE
9364                        + size_of_die (comp_unit),
9365                       "Length of Compilation Unit Info");
9366   dw2_asm_output_data (2, ver, "DWARF version number");
9367   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
9368                          debug_abbrev_section,
9369                          "Offset Into Abbrev. Section");
9370   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9371
9372   comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
9373   output_die (comp_unit);
9374
9375   /* Build the skeleton debug_abbrev section.  */
9376   switch_to_section (debug_skeleton_abbrev_section);
9377   ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
9378
9379   output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
9380
9381   dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
9382 }
9383
9384 /* Output a comdat type unit DIE and its children.  */
9385
9386 static void
9387 output_comdat_type_unit (comdat_type_node *node)
9388 {
9389   const char *secname;
9390   char *tmp;
9391   int i;
9392 #if defined (OBJECT_FORMAT_ELF)
9393   tree comdat_key;
9394 #endif
9395
9396   /* First mark all the DIEs in this CU so we know which get local refs.  */
9397   mark_dies (node->root_die);
9398
9399   external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
9400
9401   build_abbrev_table (node->root_die, extern_map);
9402
9403   delete extern_map;
9404   extern_map = NULL;
9405
9406   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
9407   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
9408   calc_die_sizes (node->root_die);
9409
9410 #if defined (OBJECT_FORMAT_ELF)
9411   if (!dwarf_split_debug_info)
9412     secname = ".debug_types";
9413   else
9414     secname = ".debug_types.dwo";
9415
9416   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9417   sprintf (tmp, "wt.");
9418   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9419     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9420   comdat_key = get_identifier (tmp);
9421   targetm.asm_out.named_section (secname,
9422                                  SECTION_DEBUG | SECTION_LINKONCE,
9423                                  comdat_key);
9424 #else
9425   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9426   sprintf (tmp, ".gnu.linkonce.wt.");
9427   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9428     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9429   secname = tmp;
9430   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9431 #endif
9432
9433   /* Output debugging information.  */
9434   output_compilation_unit_header ();
9435   output_signature (node->signature, "Type Signature");
9436   dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9437                        "Offset to Type DIE");
9438   output_die (node->root_die);
9439
9440   unmark_dies (node->root_die);
9441 }
9442
9443 /* Return the DWARF2/3 pubname associated with a decl.  */
9444
9445 static const char *
9446 dwarf2_name (tree decl, int scope)
9447 {
9448   if (DECL_NAMELESS (decl))
9449     return NULL;
9450   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9451 }
9452
9453 /* Add a new entry to .debug_pubnames if appropriate.  */
9454
9455 static void
9456 add_pubname_string (const char *str, dw_die_ref die)
9457 {
9458   pubname_entry e;
9459
9460   e.die = die;
9461   e.name = xstrdup (str);
9462   vec_safe_push (pubname_table, e);
9463 }
9464
9465 static void
9466 add_pubname (tree decl, dw_die_ref die)
9467 {
9468   if (!want_pubnames ())
9469     return;
9470
9471   /* Don't add items to the table when we expect that the consumer will have
9472      just read the enclosing die.  For example, if the consumer is looking at a
9473      class_member, it will either be inside the class already, or will have just
9474      looked up the class to find the member.  Either way, searching the class is
9475      faster than searching the index.  */
9476   if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9477       || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9478     {
9479       const char *name = dwarf2_name (decl, 1);
9480
9481       if (name)
9482         add_pubname_string (name, die);
9483     }
9484 }
9485
9486 /* Add an enumerator to the pubnames section.  */
9487
9488 static void
9489 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9490 {
9491   pubname_entry e;
9492
9493   gcc_assert (scope_name);
9494   e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9495   e.die = die;
9496   vec_safe_push (pubname_table, e);
9497 }
9498
9499 /* Add a new entry to .debug_pubtypes if appropriate.  */
9500
9501 static void
9502 add_pubtype (tree decl, dw_die_ref die)
9503 {
9504   pubname_entry e;
9505
9506   if (!want_pubnames ())
9507     return;
9508
9509   if ((TREE_PUBLIC (decl)
9510        || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9511       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9512     {
9513       tree scope = NULL;
9514       const char *scope_name = "";
9515       const char *sep = is_cxx () ? "::" : ".";
9516       const char *name;
9517
9518       scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9519       if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9520         {
9521           scope_name = lang_hooks.dwarf_name (scope, 1);
9522           if (scope_name != NULL && scope_name[0] != '\0')
9523             scope_name = concat (scope_name, sep, NULL);
9524           else
9525             scope_name = "";
9526         }
9527
9528       if (TYPE_P (decl))
9529         name = type_tag (decl);
9530       else
9531         name = lang_hooks.dwarf_name (decl, 1);
9532
9533       /* If we don't have a name for the type, there's no point in adding
9534          it to the table.  */
9535       if (name != NULL && name[0] != '\0')
9536         {
9537           e.die = die;
9538           e.name = concat (scope_name, name, NULL);
9539           vec_safe_push (pubtype_table, e);
9540         }
9541
9542       /* Although it might be more consistent to add the pubinfo for the
9543          enumerators as their dies are created, they should only be added if the
9544          enum type meets the criteria above.  So rather than re-check the parent
9545          enum type whenever an enumerator die is created, just output them all
9546          here.  This isn't protected by the name conditional because anonymous
9547          enums don't have names.  */
9548       if (die->die_tag == DW_TAG_enumeration_type)
9549         {
9550           dw_die_ref c;
9551
9552           FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9553         }
9554     }
9555 }
9556
9557 /* Output a single entry in the pubnames table.  */
9558
9559 static void
9560 output_pubname (dw_offset die_offset, pubname_entry *entry)
9561 {
9562   dw_die_ref die = entry->die;
9563   int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9564
9565   dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9566
9567   if (debug_generate_pub_sections == 2)
9568     {
9569       /* This logic follows gdb's method for determining the value of the flag
9570          byte.  */
9571       uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9572       switch (die->die_tag)
9573       {
9574         case DW_TAG_typedef:
9575         case DW_TAG_base_type:
9576         case DW_TAG_subrange_type:
9577           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9578           GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9579           break;
9580         case DW_TAG_enumerator:
9581           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9582                                           GDB_INDEX_SYMBOL_KIND_VARIABLE);
9583           if (!is_cxx () && !is_java ())
9584             GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9585           break;
9586         case DW_TAG_subprogram:
9587           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9588                                           GDB_INDEX_SYMBOL_KIND_FUNCTION);
9589           if (!is_ada ())
9590             GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9591           break;
9592         case DW_TAG_constant:
9593           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9594                                           GDB_INDEX_SYMBOL_KIND_VARIABLE);
9595           GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9596           break;
9597         case DW_TAG_variable:
9598           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9599                                           GDB_INDEX_SYMBOL_KIND_VARIABLE);
9600           GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9601           break;
9602         case DW_TAG_namespace:
9603         case DW_TAG_imported_declaration:
9604           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9605           break;
9606         case DW_TAG_class_type:
9607         case DW_TAG_interface_type:
9608         case DW_TAG_structure_type:
9609         case DW_TAG_union_type:
9610         case DW_TAG_enumeration_type:
9611           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9612           if (!is_cxx () && !is_java ())
9613             GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9614           break;
9615         default:
9616           /* An unusual tag.  Leave the flag-byte empty.  */
9617           break;
9618       }
9619       dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9620                            "GDB-index flags");
9621     }
9622
9623   dw2_asm_output_nstring (entry->name, -1, "external name");
9624 }
9625
9626
9627 /* Output the public names table used to speed up access to externally
9628    visible names; or the public types table used to find type definitions.  */
9629
9630 static void
9631 output_pubnames (vec<pubname_entry, va_gc> *names)
9632 {
9633   unsigned i;
9634   unsigned long pubnames_length = size_of_pubnames (names);
9635   pubname_entry *pub;
9636
9637   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9638     dw2_asm_output_data (4, 0xffffffff,
9639       "Initial length escape value indicating 64-bit DWARF extension");
9640   dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length, "Pub Info Length");
9641
9642   /* Version number for pubnames/pubtypes is independent of dwarf version.  */
9643   dw2_asm_output_data (2, 2, "DWARF Version");
9644
9645   if (dwarf_split_debug_info)
9646     dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9647                            debug_skeleton_info_section,
9648                            "Offset of Compilation Unit Info");
9649   else
9650     dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9651                            debug_info_section,
9652                            "Offset of Compilation Unit Info");
9653   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9654                        "Compilation Unit Length");
9655
9656   FOR_EACH_VEC_ELT (*names, i, pub)
9657     {
9658       if (include_pubname_in_output (names, pub))
9659         {
9660           dw_offset die_offset = pub->die->die_offset;
9661
9662           /* We shouldn't see pubnames for DIEs outside of the main CU.  */
9663           if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9664             gcc_assert (pub->die->die_mark);
9665
9666           /* If we're putting types in their own .debug_types sections,
9667              the .debug_pubtypes table will still point to the compile
9668              unit (not the type unit), so we want to use the offset of
9669              the skeleton DIE (if there is one).  */
9670           if (pub->die->comdat_type_p && names == pubtype_table)
9671             {
9672               comdat_type_node *type_node = pub->die->die_id.die_type_node;
9673
9674               if (type_node != NULL)
9675                 die_offset = (type_node->skeleton_die != NULL
9676                               ? type_node->skeleton_die->die_offset
9677                               : comp_unit_die ()->die_offset);
9678             }
9679
9680           output_pubname (die_offset, pub);
9681         }
9682     }
9683
9684   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9685 }
9686
9687 /* Output public names and types tables if necessary.  */
9688
9689 static void
9690 output_pubtables (void)
9691 {
9692   if (!want_pubnames () || !info_section_emitted)
9693     return;
9694
9695   switch_to_section (debug_pubnames_section);
9696   output_pubnames (pubname_table);
9697   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
9698      It shouldn't hurt to emit it always, since pure DWARF2 consumers
9699      simply won't look for the section.  */
9700   switch_to_section (debug_pubtypes_section);
9701   output_pubnames (pubtype_table);
9702 }
9703
9704
9705 /* Output the information that goes into the .debug_aranges table.
9706    Namely, define the beginning and ending address range of the
9707    text section generated for this compilation unit.  */
9708
9709 static void
9710 output_aranges (unsigned long aranges_length)
9711 {
9712   unsigned i;
9713
9714   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9715     dw2_asm_output_data (4, 0xffffffff,
9716       "Initial length escape value indicating 64-bit DWARF extension");
9717   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9718                        "Length of Address Ranges Info");
9719   /* Version number for aranges is still 2, even up to DWARF5.  */
9720   dw2_asm_output_data (2, 2, "DWARF Version");
9721   if (dwarf_split_debug_info)
9722     dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9723                            debug_skeleton_info_section,
9724                            "Offset of Compilation Unit Info");
9725   else
9726     dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9727                            debug_info_section,
9728                            "Offset of Compilation Unit Info");
9729   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9730   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9731
9732   /* We need to align to twice the pointer size here.  */
9733   if (DWARF_ARANGES_PAD_SIZE)
9734     {
9735       /* Pad using a 2 byte words so that padding is correct for any
9736          pointer size.  */
9737       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9738                            2 * DWARF2_ADDR_SIZE);
9739       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9740         dw2_asm_output_data (2, 0, NULL);
9741     }
9742
9743   /* It is necessary not to output these entries if the sections were
9744      not used; if the sections were not used, the length will be 0 and
9745      the address may end up as 0 if the section is discarded by ld
9746      --gc-sections, leaving an invalid (0, 0) entry that can be
9747      confused with the terminator.  */
9748   if (text_section_used)
9749     {
9750       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9751       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9752                             text_section_label, "Length");
9753     }
9754   if (cold_text_section_used)
9755     {
9756       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9757                            "Address");
9758       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9759                             cold_text_section_label, "Length");
9760     }
9761
9762   if (have_multiple_function_sections)
9763     {
9764       unsigned fde_idx;
9765       dw_fde_ref fde;
9766
9767       FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9768         {
9769           if (DECL_IGNORED_P (fde->decl))
9770             continue;
9771           if (!fde->in_std_section)
9772             {
9773               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9774                                    "Address");
9775               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9776                                     fde->dw_fde_begin, "Length");
9777             }
9778           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9779             {
9780               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9781                                    "Address");
9782               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9783                                     fde->dw_fde_second_begin, "Length");
9784             }
9785         }
9786     }
9787
9788   /* Output the terminator words.  */
9789   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9790   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9791 }
9792
9793 /* Add a new entry to .debug_ranges.  Return the offset at which it
9794    was placed.  */
9795
9796 static unsigned int
9797 add_ranges_num (int num)
9798 {
9799   unsigned int in_use = ranges_table_in_use;
9800
9801   if (in_use == ranges_table_allocated)
9802     {
9803       ranges_table_allocated += RANGES_TABLE_INCREMENT;
9804       ranges_table = GGC_RESIZEVEC (dw_ranges, ranges_table,
9805                                     ranges_table_allocated);
9806       memset (ranges_table + ranges_table_in_use, 0,
9807               RANGES_TABLE_INCREMENT * sizeof (dw_ranges));
9808     }
9809
9810   ranges_table[in_use].num = num;
9811   ranges_table_in_use = in_use + 1;
9812
9813   return in_use * 2 * DWARF2_ADDR_SIZE;
9814 }
9815
9816 /* Add a new entry to .debug_ranges corresponding to a block, or a
9817    range terminator if BLOCK is NULL.  */
9818
9819 static unsigned int
9820 add_ranges (const_tree block)
9821 {
9822   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9823 }
9824
9825 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9826    When using dwarf_split_debug_info, address attributes in dies destined
9827    for the final executable should be direct references--setting the
9828    parameter force_direct ensures this behavior.  */
9829
9830 static void
9831 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9832                       bool *added, bool force_direct)
9833 {
9834   unsigned int in_use = ranges_by_label_in_use;
9835   unsigned int offset;
9836
9837   if (in_use == ranges_by_label_allocated)
9838     {
9839       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9840       ranges_by_label = GGC_RESIZEVEC (dw_ranges_by_label, ranges_by_label,
9841                                        ranges_by_label_allocated);
9842       memset (ranges_by_label + ranges_by_label_in_use, 0,
9843               RANGES_TABLE_INCREMENT * sizeof (dw_ranges_by_label));
9844     }
9845
9846   ranges_by_label[in_use].begin = begin;
9847   ranges_by_label[in_use].end = end;
9848   ranges_by_label_in_use = in_use + 1;
9849
9850   offset = add_ranges_num (-(int)in_use - 1);
9851   if (!*added)
9852     {
9853       add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9854       *added = true;
9855     }
9856 }
9857
9858 static void
9859 output_ranges (void)
9860 {
9861   unsigned i;
9862   static const char *const start_fmt = "Offset %#x";
9863   const char *fmt = start_fmt;
9864
9865   for (i = 0; i < ranges_table_in_use; i++)
9866     {
9867       int block_num = ranges_table[i].num;
9868
9869       if (block_num > 0)
9870         {
9871           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9872           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9873
9874           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9875           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9876
9877           /* If all code is in the text section, then the compilation
9878              unit base address defaults to DW_AT_low_pc, which is the
9879              base of the text section.  */
9880           if (!have_multiple_function_sections)
9881             {
9882               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9883                                     text_section_label,
9884                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
9885               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9886                                     text_section_label, NULL);
9887             }
9888
9889           /* Otherwise, the compilation unit base address is zero,
9890              which allows us to use absolute addresses, and not worry
9891              about whether the target supports cross-section
9892              arithmetic.  */
9893           else
9894             {
9895               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9896                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
9897               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9898             }
9899
9900           fmt = NULL;
9901         }
9902
9903       /* Negative block_num stands for an index into ranges_by_label.  */
9904       else if (block_num < 0)
9905         {
9906           int lab_idx = - block_num - 1;
9907
9908           if (!have_multiple_function_sections)
9909             {
9910               gcc_unreachable ();
9911 #if 0
9912               /* If we ever use add_ranges_by_labels () for a single
9913                  function section, all we have to do is to take out
9914                  the #if 0 above.  */
9915               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9916                                     ranges_by_label[lab_idx].begin,
9917                                     text_section_label,
9918                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
9919               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9920                                     ranges_by_label[lab_idx].end,
9921                                     text_section_label, NULL);
9922 #endif
9923             }
9924           else
9925             {
9926               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9927                                    ranges_by_label[lab_idx].begin,
9928                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
9929               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9930                                    ranges_by_label[lab_idx].end,
9931                                    NULL);
9932             }
9933         }
9934       else
9935         {
9936           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9937           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9938           fmt = start_fmt;
9939         }
9940     }
9941 }
9942
9943 /* Data structure containing information about input files.  */
9944 struct file_info
9945 {
9946   const char *path;     /* Complete file name.  */
9947   const char *fname;    /* File name part.  */
9948   int length;           /* Length of entire string.  */
9949   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
9950   int dir_idx;          /* Index in directory table.  */
9951 };
9952
9953 /* Data structure containing information about directories with source
9954    files.  */
9955 struct dir_info
9956 {
9957   const char *path;     /* Path including directory name.  */
9958   int length;           /* Path length.  */
9959   int prefix;           /* Index of directory entry which is a prefix.  */
9960   int count;            /* Number of files in this directory.  */
9961   int dir_idx;          /* Index of directory used as base.  */
9962 };
9963
9964 /* Callback function for file_info comparison.  We sort by looking at
9965    the directories in the path.  */
9966
9967 static int
9968 file_info_cmp (const void *p1, const void *p2)
9969 {
9970   const struct file_info *const s1 = (const struct file_info *) p1;
9971   const struct file_info *const s2 = (const struct file_info *) p2;
9972   const unsigned char *cp1;
9973   const unsigned char *cp2;
9974
9975   /* Take care of file names without directories.  We need to make sure that
9976      we return consistent values to qsort since some will get confused if
9977      we return the same value when identical operands are passed in opposite
9978      orders.  So if neither has a directory, return 0 and otherwise return
9979      1 or -1 depending on which one has the directory.  */
9980   if ((s1->path == s1->fname || s2->path == s2->fname))
9981     return (s2->path == s2->fname) - (s1->path == s1->fname);
9982
9983   cp1 = (const unsigned char *) s1->path;
9984   cp2 = (const unsigned char *) s2->path;
9985
9986   while (1)
9987     {
9988       ++cp1;
9989       ++cp2;
9990       /* Reached the end of the first path?  If so, handle like above.  */
9991       if ((cp1 == (const unsigned char *) s1->fname)
9992           || (cp2 == (const unsigned char *) s2->fname))
9993         return ((cp2 == (const unsigned char *) s2->fname)
9994                 - (cp1 == (const unsigned char *) s1->fname));
9995
9996       /* Character of current path component the same?  */
9997       else if (*cp1 != *cp2)
9998         return *cp1 - *cp2;
9999     }
10000 }
10001
10002 struct file_name_acquire_data
10003 {
10004   struct file_info *files;
10005   int used_files;
10006   int max_files;
10007 };
10008
10009 /* Traversal function for the hash table.  */
10010
10011 int
10012 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
10013 {
10014   struct dwarf_file_data *d = *slot;
10015   struct file_info *fi;
10016   const char *f;
10017
10018   gcc_assert (fnad->max_files >= d->emitted_number);
10019
10020   if (! d->emitted_number)
10021     return 1;
10022
10023   gcc_assert (fnad->max_files != fnad->used_files);
10024
10025   fi = fnad->files + fnad->used_files++;
10026
10027   /* Skip all leading "./".  */
10028   f = d->filename;
10029   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
10030     f += 2;
10031
10032   /* Create a new array entry.  */
10033   fi->path = f;
10034   fi->length = strlen (f);
10035   fi->file_idx = d;
10036
10037   /* Search for the file name part.  */
10038   f = strrchr (f, DIR_SEPARATOR);
10039 #if defined (DIR_SEPARATOR_2)
10040   {
10041     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
10042
10043     if (g != NULL)
10044       {
10045         if (f == NULL || f < g)
10046           f = g;
10047       }
10048   }
10049 #endif
10050
10051   fi->fname = f == NULL ? fi->path : f + 1;
10052   return 1;
10053 }
10054
10055 /* Output the directory table and the file name table.  We try to minimize
10056    the total amount of memory needed.  A heuristic is used to avoid large
10057    slowdowns with many input files.  */
10058
10059 static void
10060 output_file_names (void)
10061 {
10062   struct file_name_acquire_data fnad;
10063   int numfiles;
10064   struct file_info *files;
10065   struct dir_info *dirs;
10066   int *saved;
10067   int *savehere;
10068   int *backmap;
10069   int ndirs;
10070   int idx_offset;
10071   int i;
10072
10073   if (!last_emitted_file)
10074     {
10075       dw2_asm_output_data (1, 0, "End directory table");
10076       dw2_asm_output_data (1, 0, "End file name table");
10077       return;
10078     }
10079
10080   numfiles = last_emitted_file->emitted_number;
10081
10082   /* Allocate the various arrays we need.  */
10083   files = XALLOCAVEC (struct file_info, numfiles);
10084   dirs = XALLOCAVEC (struct dir_info, numfiles);
10085
10086   fnad.files = files;
10087   fnad.used_files = 0;
10088   fnad.max_files = numfiles;
10089   file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
10090   gcc_assert (fnad.used_files == fnad.max_files);
10091
10092   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
10093
10094   /* Find all the different directories used.  */
10095   dirs[0].path = files[0].path;
10096   dirs[0].length = files[0].fname - files[0].path;
10097   dirs[0].prefix = -1;
10098   dirs[0].count = 1;
10099   dirs[0].dir_idx = 0;
10100   files[0].dir_idx = 0;
10101   ndirs = 1;
10102
10103   for (i = 1; i < numfiles; i++)
10104     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
10105         && memcmp (dirs[ndirs - 1].path, files[i].path,
10106                    dirs[ndirs - 1].length) == 0)
10107       {
10108         /* Same directory as last entry.  */
10109         files[i].dir_idx = ndirs - 1;
10110         ++dirs[ndirs - 1].count;
10111       }
10112     else
10113       {
10114         int j;
10115
10116         /* This is a new directory.  */
10117         dirs[ndirs].path = files[i].path;
10118         dirs[ndirs].length = files[i].fname - files[i].path;
10119         dirs[ndirs].count = 1;
10120         dirs[ndirs].dir_idx = ndirs;
10121         files[i].dir_idx = ndirs;
10122
10123         /* Search for a prefix.  */
10124         dirs[ndirs].prefix = -1;
10125         for (j = 0; j < ndirs; j++)
10126           if (dirs[j].length < dirs[ndirs].length
10127               && dirs[j].length > 1
10128               && (dirs[ndirs].prefix == -1
10129                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
10130               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
10131             dirs[ndirs].prefix = j;
10132
10133         ++ndirs;
10134       }
10135
10136   /* Now to the actual work.  We have to find a subset of the directories which
10137      allow expressing the file name using references to the directory table
10138      with the least amount of characters.  We do not do an exhaustive search
10139      where we would have to check out every combination of every single
10140      possible prefix.  Instead we use a heuristic which provides nearly optimal
10141      results in most cases and never is much off.  */
10142   saved = XALLOCAVEC (int, ndirs);
10143   savehere = XALLOCAVEC (int, ndirs);
10144
10145   memset (saved, '\0', ndirs * sizeof (saved[0]));
10146   for (i = 0; i < ndirs; i++)
10147     {
10148       int j;
10149       int total;
10150
10151       /* We can always save some space for the current directory.  But this
10152          does not mean it will be enough to justify adding the directory.  */
10153       savehere[i] = dirs[i].length;
10154       total = (savehere[i] - saved[i]) * dirs[i].count;
10155
10156       for (j = i + 1; j < ndirs; j++)
10157         {
10158           savehere[j] = 0;
10159           if (saved[j] < dirs[i].length)
10160             {
10161               /* Determine whether the dirs[i] path is a prefix of the
10162                  dirs[j] path.  */
10163               int k;
10164
10165               k = dirs[j].prefix;
10166               while (k != -1 && k != (int) i)
10167                 k = dirs[k].prefix;
10168
10169               if (k == (int) i)
10170                 {
10171                   /* Yes it is.  We can possibly save some memory by
10172                      writing the filenames in dirs[j] relative to
10173                      dirs[i].  */
10174                   savehere[j] = dirs[i].length;
10175                   total += (savehere[j] - saved[j]) * dirs[j].count;
10176                 }
10177             }
10178         }
10179
10180       /* Check whether we can save enough to justify adding the dirs[i]
10181          directory.  */
10182       if (total > dirs[i].length + 1)
10183         {
10184           /* It's worthwhile adding.  */
10185           for (j = i; j < ndirs; j++)
10186             if (savehere[j] > 0)
10187               {
10188                 /* Remember how much we saved for this directory so far.  */
10189                 saved[j] = savehere[j];
10190
10191                 /* Remember the prefix directory.  */
10192                 dirs[j].dir_idx = i;
10193               }
10194         }
10195     }
10196
10197   /* Emit the directory name table.  */
10198   idx_offset = dirs[0].length > 0 ? 1 : 0;
10199   for (i = 1 - idx_offset; i < ndirs; i++)
10200     dw2_asm_output_nstring (dirs[i].path,
10201                             dirs[i].length
10202                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
10203                             "Directory Entry: %#x", i + idx_offset);
10204
10205   dw2_asm_output_data (1, 0, "End directory table");
10206
10207   /* We have to emit them in the order of emitted_number since that's
10208      used in the debug info generation.  To do this efficiently we
10209      generate a back-mapping of the indices first.  */
10210   backmap = XALLOCAVEC (int, numfiles);
10211   for (i = 0; i < numfiles; i++)
10212     backmap[files[i].file_idx->emitted_number - 1] = i;
10213
10214   /* Now write all the file names.  */
10215   for (i = 0; i < numfiles; i++)
10216     {
10217       int file_idx = backmap[i];
10218       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
10219
10220 #ifdef VMS_DEBUGGING_INFO
10221 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
10222
10223       /* Setting these fields can lead to debugger miscomparisons,
10224          but VMS Debug requires them to be set correctly.  */
10225
10226       int ver;
10227       long long cdt;
10228       long siz;
10229       int maxfilelen = strlen (files[file_idx].path)
10230                                + dirs[dir_idx].length
10231                                + MAX_VMS_VERSION_LEN + 1;
10232       char *filebuf = XALLOCAVEC (char, maxfilelen);
10233
10234       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
10235       snprintf (filebuf, maxfilelen, "%s;%d",
10236                 files[file_idx].path + dirs[dir_idx].length, ver);
10237
10238       dw2_asm_output_nstring
10239         (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
10240
10241       /* Include directory index.  */
10242       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10243
10244       /* Modification time.  */
10245       dw2_asm_output_data_uleb128
10246         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
10247           ? cdt : 0,
10248          NULL);
10249
10250       /* File length in bytes.  */
10251       dw2_asm_output_data_uleb128
10252         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
10253           ? siz : 0,
10254          NULL);
10255 #else
10256       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
10257                               "File Entry: %#x", (unsigned) i + 1);
10258
10259       /* Include directory index.  */
10260       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10261
10262       /* Modification time.  */
10263       dw2_asm_output_data_uleb128 (0, NULL);
10264
10265       /* File length in bytes.  */
10266       dw2_asm_output_data_uleb128 (0, NULL);
10267 #endif /* VMS_DEBUGGING_INFO */
10268     }
10269
10270   dw2_asm_output_data (1, 0, "End file name table");
10271 }
10272
10273
10274 /* Output one line number table into the .debug_line section.  */
10275
10276 static void
10277 output_one_line_info_table (dw_line_info_table *table)
10278 {
10279   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
10280   unsigned int current_line = 1;
10281   bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
10282   dw_line_info_entry *ent;
10283   size_t i;
10284
10285   FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
10286     {
10287       switch (ent->opcode)
10288         {
10289         case LI_set_address:
10290           /* ??? Unfortunately, we have little choice here currently, and
10291              must always use the most general form.  GCC does not know the
10292              address delta itself, so we can't use DW_LNS_advance_pc.  Many
10293              ports do have length attributes which will give an upper bound
10294              on the address range.  We could perhaps use length attributes
10295              to determine when it is safe to use DW_LNS_fixed_advance_pc.  */
10296           ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
10297
10298           /* This can handle any delta.  This takes
10299              4+DWARF2_ADDR_SIZE bytes.  */
10300           dw2_asm_output_data (1, 0, "set address %s", line_label);
10301           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10302           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10303           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10304           break;
10305
10306         case LI_set_line:
10307           if (ent->val == current_line)
10308             {
10309               /* We still need to start a new row, so output a copy insn.  */
10310               dw2_asm_output_data (1, DW_LNS_copy,
10311                                    "copy line %u", current_line);
10312             }
10313           else
10314             {
10315               int line_offset = ent->val - current_line;
10316               int line_delta = line_offset - DWARF_LINE_BASE;
10317
10318               current_line = ent->val;
10319               if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10320                 {
10321                   /* This can handle deltas from -10 to 234, using the current
10322                      definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
10323                      This takes 1 byte.  */
10324                   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10325                                        "line %u", current_line);
10326                 }
10327               else
10328                 {
10329                   /* This can handle any delta.  This takes at least 4 bytes,
10330                      depending on the value being encoded.  */
10331                   dw2_asm_output_data (1, DW_LNS_advance_line,
10332                                        "advance to line %u", current_line);
10333                   dw2_asm_output_data_sleb128 (line_offset, NULL);
10334                   dw2_asm_output_data (1, DW_LNS_copy, NULL);
10335                 }
10336             }
10337           break;
10338
10339         case LI_set_file:
10340           dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
10341           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10342           break;
10343
10344         case LI_set_column:
10345           dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
10346           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10347           break;
10348
10349         case LI_negate_stmt:
10350           current_is_stmt = !current_is_stmt;
10351           dw2_asm_output_data (1, DW_LNS_negate_stmt,
10352                                "is_stmt %d", current_is_stmt);
10353           break;
10354
10355         case LI_set_prologue_end:
10356           dw2_asm_output_data (1, DW_LNS_set_prologue_end,
10357                                "set prologue end");
10358           break;
10359           
10360         case LI_set_epilogue_begin:
10361           dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
10362                                "set epilogue begin");
10363           break;
10364
10365         case LI_set_discriminator:
10366           dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
10367           dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
10368           dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
10369           dw2_asm_output_data_uleb128 (ent->val, NULL);
10370           break;
10371         }
10372     }
10373
10374   /* Emit debug info for the address of the end of the table.  */
10375   dw2_asm_output_data (1, 0, "set address %s", table->end_label);
10376   dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10377   dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10378   dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10379
10380   dw2_asm_output_data (1, 0, "end sequence");
10381   dw2_asm_output_data_uleb128 (1, NULL);
10382   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10383 }
10384
10385 /* Output the source line number correspondence information.  This
10386    information goes into the .debug_line section.  */
10387
10388 static void
10389 output_line_info (bool prologue_only)
10390 {
10391   char l1[20], l2[20], p1[20], p2[20];
10392   /* We don't support DWARFv5 line tables yet.  */
10393   int ver = dwarf_version < 5 ? dwarf_version : 4;
10394   bool saw_one = false;
10395   int opc;
10396
10397   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10398   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10399   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10400   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10401
10402   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10403     dw2_asm_output_data (4, 0xffffffff,
10404       "Initial length escape value indicating 64-bit DWARF extension");
10405   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10406                         "Length of Source Line Info");
10407   ASM_OUTPUT_LABEL (asm_out_file, l1);
10408
10409   dw2_asm_output_data (2, ver, "DWARF Version");
10410   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10411   ASM_OUTPUT_LABEL (asm_out_file, p1);
10412
10413   /* Define the architecture-dependent minimum instruction length (in bytes).
10414      In this implementation of DWARF, this field is used for information
10415      purposes only.  Since GCC generates assembly language, we have no
10416      a priori knowledge of how many instruction bytes are generated for each
10417      source line, and therefore can use only the DW_LNE_set_address and
10418      DW_LNS_fixed_advance_pc line information commands.  Accordingly, we fix
10419      this as '1', which is "correct enough" for all architectures,
10420      and don't let the target override.  */
10421   dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10422
10423   if (ver >= 4)
10424     dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10425                          "Maximum Operations Per Instruction");
10426   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10427                        "Default is_stmt_start flag");
10428   dw2_asm_output_data (1, DWARF_LINE_BASE,
10429                        "Line Base Value (Special Opcodes)");
10430   dw2_asm_output_data (1, DWARF_LINE_RANGE,
10431                        "Line Range Value (Special Opcodes)");
10432   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10433                        "Special Opcode Base");
10434
10435   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10436     {
10437       int n_op_args;
10438       switch (opc)
10439         {
10440         case DW_LNS_advance_pc:
10441         case DW_LNS_advance_line:
10442         case DW_LNS_set_file:
10443         case DW_LNS_set_column:
10444         case DW_LNS_fixed_advance_pc:
10445         case DW_LNS_set_isa:
10446           n_op_args = 1;
10447           break;
10448         default:
10449           n_op_args = 0;
10450           break;
10451         }
10452
10453       dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10454                            opc, n_op_args);
10455     }
10456
10457   /* Write out the information about the files we use.  */
10458   output_file_names ();
10459   ASM_OUTPUT_LABEL (asm_out_file, p2);
10460   if (prologue_only)
10461     {
10462       /* Output the marker for the end of the line number info.  */
10463       ASM_OUTPUT_LABEL (asm_out_file, l2);
10464       return;
10465     }
10466
10467   if (separate_line_info)
10468     {
10469       dw_line_info_table *table;
10470       size_t i;
10471
10472       FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10473         if (table->in_use)
10474           {
10475             output_one_line_info_table (table);
10476             saw_one = true;
10477           }
10478     }
10479   if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10480     {
10481       output_one_line_info_table (cold_text_section_line_info);
10482       saw_one = true;
10483     }
10484
10485   /* ??? Some Darwin linkers crash on a .debug_line section with no
10486      sequences.  Further, merely a DW_LNE_end_sequence entry is not
10487      sufficient -- the address column must also be initialized.
10488      Make sure to output at least one set_address/end_sequence pair,
10489      choosing .text since that section is always present.  */
10490   if (text_section_line_info->in_use || !saw_one)
10491     output_one_line_info_table (text_section_line_info);
10492
10493   /* Output the marker for the end of the line number info.  */
10494   ASM_OUTPUT_LABEL (asm_out_file, l2);
10495 }
10496 \f
10497 /* Given a pointer to a tree node for some base type, return a pointer to
10498    a DIE that describes the given type.
10499
10500    This routine must only be called for GCC type nodes that correspond to
10501    Dwarf base (fundamental) types.  */
10502
10503 static dw_die_ref
10504 base_type_die (tree type)
10505 {
10506   dw_die_ref base_type_result;
10507   enum dwarf_type encoding;
10508
10509   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10510     return 0;
10511
10512   /* If this is a subtype that should not be emitted as a subrange type,
10513      use the base type.  See subrange_type_for_debug_p.  */
10514   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10515     type = TREE_TYPE (type);
10516
10517   switch (TREE_CODE (type))
10518     {
10519     case INTEGER_TYPE:
10520       if ((dwarf_version >= 4 || !dwarf_strict)
10521           && TYPE_NAME (type)
10522           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10523           && DECL_IS_BUILTIN (TYPE_NAME (type))
10524           && DECL_NAME (TYPE_NAME (type)))
10525         {
10526           const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10527           if (strcmp (name, "char16_t") == 0
10528               || strcmp (name, "char32_t") == 0)
10529             {
10530               encoding = DW_ATE_UTF;
10531               break;
10532             }
10533         }
10534       if (TYPE_STRING_FLAG (type))
10535         {
10536           if (TYPE_UNSIGNED (type))
10537             encoding = DW_ATE_unsigned_char;
10538           else
10539             encoding = DW_ATE_signed_char;
10540         }
10541       else if (TYPE_UNSIGNED (type))
10542         encoding = DW_ATE_unsigned;
10543       else
10544         encoding = DW_ATE_signed;
10545       break;
10546
10547     case REAL_TYPE:
10548       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10549         {
10550           if (dwarf_version >= 3 || !dwarf_strict)
10551             encoding = DW_ATE_decimal_float;
10552           else
10553             encoding = DW_ATE_lo_user;
10554         }
10555       else
10556         encoding = DW_ATE_float;
10557       break;
10558
10559     case FIXED_POINT_TYPE:
10560       if (!(dwarf_version >= 3 || !dwarf_strict))
10561         encoding = DW_ATE_lo_user;
10562       else if (TYPE_UNSIGNED (type))
10563         encoding = DW_ATE_unsigned_fixed;
10564       else
10565         encoding = DW_ATE_signed_fixed;
10566       break;
10567
10568       /* Dwarf2 doesn't know anything about complex ints, so use
10569          a user defined type for it.  */
10570     case COMPLEX_TYPE:
10571       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10572         encoding = DW_ATE_complex_float;
10573       else
10574         encoding = DW_ATE_lo_user;
10575       break;
10576
10577     case BOOLEAN_TYPE:
10578       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
10579       encoding = DW_ATE_boolean;
10580       break;
10581
10582     default:
10583       /* No other TREE_CODEs are Dwarf fundamental types.  */
10584       gcc_unreachable ();
10585     }
10586
10587   base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10588
10589   add_AT_unsigned (base_type_result, DW_AT_byte_size,
10590                    int_size_in_bytes (type));
10591   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10592   add_pubtype (type, base_type_result);
10593
10594   return base_type_result;
10595 }
10596
10597 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
10598    named 'auto' in its type: return true for it, false otherwise.  */
10599
10600 static inline bool
10601 is_cxx_auto (tree type)
10602 {
10603   if (is_cxx ())
10604     {
10605       tree name = TYPE_IDENTIFIER (type);
10606       if (name == get_identifier ("auto")
10607           || name == get_identifier ("decltype(auto)"))
10608         return true;
10609     }
10610   return false;
10611 }
10612
10613 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10614    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
10615
10616 static inline int
10617 is_base_type (tree type)
10618 {
10619   switch (TREE_CODE (type))
10620     {
10621     case ERROR_MARK:
10622     case VOID_TYPE:
10623     case INTEGER_TYPE:
10624     case REAL_TYPE:
10625     case FIXED_POINT_TYPE:
10626     case COMPLEX_TYPE:
10627     case BOOLEAN_TYPE:
10628     case POINTER_BOUNDS_TYPE:
10629       return 1;
10630
10631     case ARRAY_TYPE:
10632     case RECORD_TYPE:
10633     case UNION_TYPE:
10634     case QUAL_UNION_TYPE:
10635     case ENUMERAL_TYPE:
10636     case FUNCTION_TYPE:
10637     case METHOD_TYPE:
10638     case POINTER_TYPE:
10639     case REFERENCE_TYPE:
10640     case NULLPTR_TYPE:
10641     case OFFSET_TYPE:
10642     case LANG_TYPE:
10643     case VECTOR_TYPE:
10644       return 0;
10645
10646     default:
10647       if (is_cxx_auto (type))
10648         return 0;
10649       gcc_unreachable ();
10650     }
10651
10652   return 0;
10653 }
10654
10655 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10656    node, return the size in bits for the type if it is a constant, or else
10657    return the alignment for the type if the type's size is not constant, or
10658    else return BITS_PER_WORD if the type actually turns out to be an
10659    ERROR_MARK node.  */
10660
10661 static inline unsigned HOST_WIDE_INT
10662 simple_type_size_in_bits (const_tree type)
10663 {
10664   if (TREE_CODE (type) == ERROR_MARK)
10665     return BITS_PER_WORD;
10666   else if (TYPE_SIZE (type) == NULL_TREE)
10667     return 0;
10668   else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
10669     return tree_to_uhwi (TYPE_SIZE (type));
10670   else
10671     return TYPE_ALIGN (type);
10672 }
10673
10674 /* Similarly, but return an offset_int instead of UHWI.  */
10675
10676 static inline offset_int
10677 offset_int_type_size_in_bits (const_tree type)
10678 {
10679   if (TREE_CODE (type) == ERROR_MARK)
10680     return BITS_PER_WORD;
10681   else if (TYPE_SIZE (type) == NULL_TREE)
10682     return 0;
10683   else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10684     return wi::to_offset (TYPE_SIZE (type));
10685   else
10686     return TYPE_ALIGN (type);
10687 }
10688
10689 /*  Given a pointer to a tree node for a subrange type, return a pointer
10690     to a DIE that describes the given type.  */
10691
10692 static dw_die_ref
10693 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10694 {
10695   dw_die_ref subrange_die;
10696   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10697
10698   if (context_die == NULL)
10699     context_die = comp_unit_die ();
10700
10701   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10702
10703   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10704     {
10705       /* The size of the subrange type and its base type do not match,
10706          so we need to generate a size attribute for the subrange type.  */
10707       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10708     }
10709
10710   if (low)
10711     add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
10712   if (high)
10713     add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
10714
10715   return subrange_die;
10716 }
10717
10718 /* Returns the (const and/or volatile) cv_qualifiers associated with
10719    the decl node.  This will normally be augmented with the
10720    cv_qualifiers of the underlying type in add_type_attribute.  */
10721
10722 static int
10723 decl_quals (const_tree decl)
10724 {
10725   return ((TREE_READONLY (decl)
10726            ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
10727           | (TREE_THIS_VOLATILE (decl)
10728              ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
10729 }
10730
10731 /* Determine the TYPE whose qualifiers match the largest strict subset
10732    of the given TYPE_QUALS, and return its qualifiers.  Ignore all
10733    qualifiers outside QUAL_MASK.  */
10734
10735 static int
10736 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
10737 {
10738   tree t;
10739   int best_rank = 0, best_qual = 0, max_rank;
10740
10741   type_quals &= qual_mask;
10742   max_rank = popcount_hwi (type_quals) - 1;
10743
10744   for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
10745        t = TYPE_NEXT_VARIANT (t))
10746     {
10747       int q = TYPE_QUALS (t) & qual_mask;
10748
10749       if ((q & type_quals) == q && q != type_quals
10750           && check_base_type (t, type))
10751         {
10752           int rank = popcount_hwi (q);
10753
10754           if (rank > best_rank)
10755             {
10756               best_rank = rank;
10757               best_qual = q;
10758             }
10759         }
10760     }
10761
10762   return best_qual;
10763 }
10764
10765 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10766    entry that chains various modifiers in front of the given type.  */
10767
10768 static dw_die_ref
10769 modified_type_die (tree type, int cv_quals, dw_die_ref context_die)
10770 {
10771   enum tree_code code = TREE_CODE (type);
10772   dw_die_ref mod_type_die;
10773   dw_die_ref sub_die = NULL;
10774   tree item_type = NULL;
10775   tree qualified_type;
10776   tree name, low, high;
10777   dw_die_ref mod_scope;
10778   /* Only these cv-qualifiers are currently handled.  */
10779   const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
10780                             | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
10781
10782   if (code == ERROR_MARK)
10783     return NULL;
10784
10785   cv_quals &= cv_qual_mask;
10786
10787   /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
10788      tag modifier (and not an attribute) old consumers won't be able
10789      to handle it.  */
10790   if (dwarf_version < 3)
10791     cv_quals &= ~TYPE_QUAL_RESTRICT;
10792
10793   /* Likewise for DW_TAG_atomic_type for DWARFv5.  */
10794   if (dwarf_version < 5)
10795     cv_quals &= ~TYPE_QUAL_ATOMIC;
10796
10797   /* See if we already have the appropriately qualified variant of
10798      this type.  */
10799   qualified_type = get_qualified_type (type, cv_quals);
10800
10801   if (qualified_type == sizetype
10802       && TYPE_NAME (qualified_type)
10803       && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10804     {
10805       tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10806
10807       gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10808                            && TYPE_PRECISION (t)
10809                            == TYPE_PRECISION (qualified_type)
10810                            && TYPE_UNSIGNED (t)
10811                            == TYPE_UNSIGNED (qualified_type));
10812       qualified_type = t;
10813     }
10814
10815   /* If we do, then we can just use its DIE, if it exists.  */
10816   if (qualified_type)
10817     {
10818       mod_type_die = lookup_type_die (qualified_type);
10819       if (mod_type_die)
10820         return mod_type_die;
10821     }
10822
10823   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10824
10825   /* Handle C typedef types.  */
10826   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10827       && !DECL_ARTIFICIAL (name))
10828     {
10829       tree dtype = TREE_TYPE (name);
10830
10831       if (qualified_type == dtype)
10832         {
10833           /* For a named type, use the typedef.  */
10834           gen_type_die (qualified_type, context_die);
10835           return lookup_type_die (qualified_type);
10836         }
10837       else
10838         {
10839           int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
10840           dquals &= cv_qual_mask;
10841           if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
10842               || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
10843             /* cv-unqualified version of named type.  Just use
10844                the unnamed type to which it refers.  */
10845             return modified_type_die (DECL_ORIGINAL_TYPE (name),
10846                                       cv_quals, context_die);
10847           /* Else cv-qualified version of named type; fall through.  */
10848         }
10849     }
10850
10851   mod_scope = scope_die_for (type, context_die);
10852
10853   if (cv_quals)
10854     {
10855       struct qual_info { int q; enum dwarf_tag t; };
10856       static const struct qual_info qual_info[] =
10857         {
10858           { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type },
10859           { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
10860           { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
10861           { TYPE_QUAL_CONST, DW_TAG_const_type },
10862         };
10863       int sub_quals;
10864       unsigned i;
10865
10866       /* Determine a lesser qualified type that most closely matches
10867          this one.  Then generate DW_TAG_* entries for the remaining
10868          qualifiers.  */
10869       sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
10870                                                   cv_qual_mask);
10871       mod_type_die = modified_type_die (type, sub_quals, context_die);
10872
10873       for (i = 0; i < sizeof (qual_info) / sizeof (qual_info[0]); i++)
10874         if (qual_info[i].q & cv_quals & ~sub_quals)
10875           {
10876             dw_die_ref d = new_die (qual_info[i].t, mod_scope, type);
10877             if (mod_type_die)
10878               add_AT_die_ref (d, DW_AT_type, mod_type_die);
10879             mod_type_die = d;
10880           }
10881     }
10882   else if (code == POINTER_TYPE)
10883     {
10884       mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10885       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10886                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
10887       item_type = TREE_TYPE (type);
10888       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10889         add_AT_unsigned (mod_type_die, DW_AT_address_class,
10890                          TYPE_ADDR_SPACE (item_type));
10891     }
10892   else if (code == REFERENCE_TYPE)
10893     {
10894       if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10895         mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10896                                 type);
10897       else
10898         mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10899       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10900                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
10901       item_type = TREE_TYPE (type);
10902       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10903         add_AT_unsigned (mod_type_die, DW_AT_address_class,
10904                          TYPE_ADDR_SPACE (item_type));
10905     }
10906   else if (code == INTEGER_TYPE
10907            && TREE_TYPE (type) != NULL_TREE
10908            && subrange_type_for_debug_p (type, &low, &high))
10909     {
10910       mod_type_die = subrange_type_die (type, low, high, context_die);
10911       item_type = TREE_TYPE (type);
10912     }
10913   else if (is_base_type (type))
10914     mod_type_die = base_type_die (type);
10915   else
10916     {
10917       gen_type_die (type, context_die);
10918
10919       /* We have to get the type_main_variant here (and pass that to the
10920          `lookup_type_die' routine) because the ..._TYPE node we have
10921          might simply be a *copy* of some original type node (where the
10922          copy was created to help us keep track of typedef names) and
10923          that copy might have a different TYPE_UID from the original
10924          ..._TYPE node.  */
10925       if (TREE_CODE (type) != VECTOR_TYPE)
10926         return lookup_type_die (type_main_variant (type));
10927       else
10928         /* Vectors have the debugging information in the type,
10929            not the main variant.  */
10930         return lookup_type_die (type);
10931     }
10932
10933   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
10934      don't output a DW_TAG_typedef, since there isn't one in the
10935      user's program; just attach a DW_AT_name to the type.
10936      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10937      if the base type already has the same name.  */
10938   if (name
10939       && ((TREE_CODE (name) != TYPE_DECL
10940            && (qualified_type == TYPE_MAIN_VARIANT (type)
10941                || (cv_quals == TYPE_UNQUALIFIED)))
10942           || (TREE_CODE (name) == TYPE_DECL
10943               && TREE_TYPE (name) == qualified_type
10944               && DECL_NAME (name))))
10945     {
10946       if (TREE_CODE (name) == TYPE_DECL)
10947         /* Could just call add_name_and_src_coords_attributes here,
10948            but since this is a builtin type it doesn't have any
10949            useful source coordinates anyway.  */
10950         name = DECL_NAME (name);
10951       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10952     }
10953   /* This probably indicates a bug.  */
10954   else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10955     {
10956       name = TYPE_IDENTIFIER (type);
10957       add_name_attribute (mod_type_die,
10958                           name ? IDENTIFIER_POINTER (name) : "__unknown__");
10959     }
10960
10961   if (qualified_type)
10962     equate_type_number_to_die (qualified_type, mod_type_die);
10963
10964   if (item_type)
10965     /* We must do this after the equate_type_number_to_die call, in case
10966        this is a recursive type.  This ensures that the modified_type_die
10967        recursion will terminate even if the type is recursive.  Recursive
10968        types are possible in Ada.  */
10969     sub_die = modified_type_die (item_type,
10970                                  TYPE_QUALS_NO_ADDR_SPACE (item_type),
10971                                  context_die);
10972
10973   if (sub_die != NULL)
10974     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10975
10976   add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10977   if (TYPE_ARTIFICIAL (type))
10978     add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10979
10980   return mod_type_die;
10981 }
10982
10983 /* Generate DIEs for the generic parameters of T.
10984    T must be either a generic type or a generic function.
10985    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
10986
10987 static void
10988 gen_generic_params_dies (tree t)
10989 {
10990   tree parms, args;
10991   int parms_num, i;
10992   dw_die_ref die = NULL;
10993   int non_default;
10994
10995   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10996     return;
10997
10998   if (TYPE_P (t))
10999     die = lookup_type_die (t);
11000   else if (DECL_P (t))
11001     die = lookup_decl_die (t);
11002
11003   gcc_assert (die);
11004
11005   parms = lang_hooks.get_innermost_generic_parms (t);
11006   if (!parms)
11007     /* T has no generic parameter. It means T is neither a generic type
11008        or function. End of story.  */
11009     return;
11010
11011   parms_num = TREE_VEC_LENGTH (parms);
11012   args = lang_hooks.get_innermost_generic_args (t);
11013   if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
11014     non_default = int_cst_value (TREE_CHAIN (args));
11015   else
11016     non_default = TREE_VEC_LENGTH (args);
11017   for (i = 0; i < parms_num; i++)
11018     {
11019       tree parm, arg, arg_pack_elems;
11020       dw_die_ref parm_die;
11021
11022       parm = TREE_VEC_ELT (parms, i);
11023       arg = TREE_VEC_ELT (args, i);
11024       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
11025       gcc_assert (parm && TREE_VALUE (parm) && arg);
11026
11027       if (parm && TREE_VALUE (parm) && arg)
11028         {
11029           /* If PARM represents a template parameter pack,
11030              emit a DW_TAG_GNU_template_parameter_pack DIE, followed
11031              by DW_TAG_template_*_parameter DIEs for the argument
11032              pack elements of ARG. Note that ARG would then be
11033              an argument pack.  */
11034           if (arg_pack_elems)
11035             parm_die = template_parameter_pack_die (TREE_VALUE (parm),
11036                                                     arg_pack_elems,
11037                                                     die);
11038           else
11039             parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
11040                                               true /* emit name */, die);
11041           if (i >= non_default)
11042             add_AT_flag (parm_die, DW_AT_default_value, 1);
11043         }
11044     }
11045 }
11046
11047 /* Create and return a DIE for PARM which should be
11048    the representation of a generic type parameter.
11049    For instance, in the C++ front end, PARM would be a template parameter.
11050    ARG is the argument to PARM.
11051    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
11052    name of the PARM.
11053    PARENT_DIE is the parent DIE which the new created DIE should be added to,
11054    as a child node.  */
11055
11056 static dw_die_ref
11057 generic_parameter_die (tree parm, tree arg,
11058                        bool emit_name_p,
11059                        dw_die_ref parent_die)
11060 {
11061   dw_die_ref tmpl_die = NULL;
11062   const char *name = NULL;
11063
11064   if (!parm || !DECL_NAME (parm) || !arg)
11065     return NULL;
11066
11067   /* We support non-type generic parameters and arguments,
11068      type generic parameters and arguments, as well as
11069      generic generic parameters (a.k.a. template template parameters in C++)
11070      and arguments.  */
11071   if (TREE_CODE (parm) == PARM_DECL)
11072     /* PARM is a nontype generic parameter  */
11073     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
11074   else if (TREE_CODE (parm) == TYPE_DECL)
11075     /* PARM is a type generic parameter.  */
11076     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
11077   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
11078     /* PARM is a generic generic parameter.
11079        Its DIE is a GNU extension. It shall have a
11080        DW_AT_name attribute to represent the name of the template template
11081        parameter, and a DW_AT_GNU_template_name attribute to represent the
11082        name of the template template argument.  */
11083     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
11084                         parent_die, parm);
11085   else
11086     gcc_unreachable ();
11087
11088   if (tmpl_die)
11089     {
11090       tree tmpl_type;
11091
11092       /* If PARM is a generic parameter pack, it means we are
11093          emitting debug info for a template argument pack element.
11094          In other terms, ARG is a template argument pack element.
11095          In that case, we don't emit any DW_AT_name attribute for
11096          the die.  */
11097       if (emit_name_p)
11098         {
11099           name = IDENTIFIER_POINTER (DECL_NAME (parm));
11100           gcc_assert (name);
11101           add_AT_string (tmpl_die, DW_AT_name, name);
11102         }
11103
11104       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
11105         {
11106           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
11107              TMPL_DIE should have a child DW_AT_type attribute that is set
11108              to the type of the argument to PARM, which is ARG.
11109              If PARM is a type generic parameter, TMPL_DIE should have a
11110              child DW_AT_type that is set to ARG.  */
11111           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
11112           add_type_attribute (tmpl_die, tmpl_type,
11113                               (TREE_THIS_VOLATILE (tmpl_type)
11114                                ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
11115                               parent_die);
11116         }
11117       else
11118         {
11119           /* So TMPL_DIE is a DIE representing a
11120              a generic generic template parameter, a.k.a template template
11121              parameter in C++ and arg is a template.  */
11122
11123           /* The DW_AT_GNU_template_name attribute of the DIE must be set
11124              to the name of the argument.  */
11125           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
11126           if (name)
11127             add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
11128         }
11129
11130       if (TREE_CODE (parm) == PARM_DECL)
11131         /* So PARM is a non-type generic parameter.
11132            DWARF3 5.6.8 says we must set a DW_AT_const_value child
11133            attribute of TMPL_DIE which value represents the value
11134            of ARG.
11135            We must be careful here:
11136            The value of ARG might reference some function decls.
11137            We might currently be emitting debug info for a generic
11138            type and types are emitted before function decls, we don't
11139            know if the function decls referenced by ARG will actually be
11140            emitted after cgraph computations.
11141            So must defer the generation of the DW_AT_const_value to
11142            after cgraph is ready.  */
11143         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
11144     }
11145
11146   return tmpl_die;
11147 }
11148
11149 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
11150    PARM_PACK must be a template parameter pack. The returned DIE
11151    will be child DIE of PARENT_DIE.  */
11152
11153 static dw_die_ref
11154 template_parameter_pack_die (tree parm_pack,
11155                              tree parm_pack_args,
11156                              dw_die_ref parent_die)
11157 {
11158   dw_die_ref die;
11159   int j;
11160
11161   gcc_assert (parent_die && parm_pack);
11162
11163   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
11164   add_name_and_src_coords_attributes (die, parm_pack);
11165   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
11166     generic_parameter_die (parm_pack,
11167                            TREE_VEC_ELT (parm_pack_args, j),
11168                            false /* Don't emit DW_AT_name */,
11169                            die);
11170   return die;
11171 }
11172
11173 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
11174    an enumerated type.  */
11175
11176 static inline int
11177 type_is_enum (const_tree type)
11178 {
11179   return TREE_CODE (type) == ENUMERAL_TYPE;
11180 }
11181
11182 /* Return the DBX register number described by a given RTL node.  */
11183
11184 static unsigned int
11185 dbx_reg_number (const_rtx rtl)
11186 {
11187   unsigned regno = REGNO (rtl);
11188
11189   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
11190
11191 #ifdef LEAF_REG_REMAP
11192   if (crtl->uses_only_leaf_regs)
11193     {
11194       int leaf_reg = LEAF_REG_REMAP (regno);
11195       if (leaf_reg != -1)
11196         regno = (unsigned) leaf_reg;
11197     }
11198 #endif
11199
11200   regno = DBX_REGISTER_NUMBER (regno);
11201   gcc_assert (regno != INVALID_REGNUM);
11202   return regno;
11203 }
11204
11205 /* Optionally add a DW_OP_piece term to a location description expression.
11206    DW_OP_piece is only added if the location description expression already
11207    doesn't end with DW_OP_piece.  */
11208
11209 static void
11210 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
11211 {
11212   dw_loc_descr_ref loc;
11213
11214   if (*list_head != NULL)
11215     {
11216       /* Find the end of the chain.  */
11217       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
11218         ;
11219
11220       if (loc->dw_loc_opc != DW_OP_piece)
11221         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
11222     }
11223 }
11224
11225 /* Return a location descriptor that designates a machine register or
11226    zero if there is none.  */
11227
11228 static dw_loc_descr_ref
11229 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
11230 {
11231   rtx regs;
11232
11233   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
11234     return 0;
11235
11236   /* We only use "frame base" when we're sure we're talking about the
11237      post-prologue local stack frame.  We do this by *not* running
11238      register elimination until this point, and recognizing the special
11239      argument pointer and soft frame pointer rtx's.
11240      Use DW_OP_fbreg offset DW_OP_stack_value in this case.  */
11241   if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
11242       && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
11243     {
11244       dw_loc_descr_ref result = NULL;
11245
11246       if (dwarf_version >= 4 || !dwarf_strict)
11247         {
11248           result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
11249                                        initialized);
11250           if (result)
11251             add_loc_descr (&result,
11252                            new_loc_descr (DW_OP_stack_value, 0, 0));
11253         }
11254       return result;
11255     }
11256
11257   regs = targetm.dwarf_register_span (rtl);
11258
11259   if (REG_NREGS (rtl) > 1 || regs)
11260     return multiple_reg_loc_descriptor (rtl, regs, initialized);
11261   else
11262     {
11263       unsigned int dbx_regnum = dbx_reg_number (rtl);
11264       if (dbx_regnum == IGNORED_DWARF_REGNUM)
11265         return 0;
11266       return one_reg_loc_descriptor (dbx_regnum, initialized);
11267     }
11268 }
11269
11270 /* Return a location descriptor that designates a machine register for
11271    a given hard register number.  */
11272
11273 static dw_loc_descr_ref
11274 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
11275 {
11276   dw_loc_descr_ref reg_loc_descr;
11277
11278   if (regno <= 31)
11279     reg_loc_descr
11280       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
11281   else
11282     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
11283
11284   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11285     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11286
11287   return reg_loc_descr;
11288 }
11289
11290 /* Given an RTL of a register, return a location descriptor that
11291    designates a value that spans more than one register.  */
11292
11293 static dw_loc_descr_ref
11294 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
11295                              enum var_init_status initialized)
11296 {
11297   int size, i;
11298   dw_loc_descr_ref loc_result = NULL;
11299
11300   /* Simple, contiguous registers.  */
11301   if (regs == NULL_RTX)
11302     {
11303       unsigned reg = REGNO (rtl);
11304       int nregs;
11305
11306 #ifdef LEAF_REG_REMAP
11307       if (crtl->uses_only_leaf_regs)
11308         {
11309           int leaf_reg = LEAF_REG_REMAP (reg);
11310           if (leaf_reg != -1)
11311             reg = (unsigned) leaf_reg;
11312         }
11313 #endif
11314
11315       gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
11316       nregs = REG_NREGS (rtl);
11317
11318       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
11319
11320       loc_result = NULL;
11321       while (nregs--)
11322         {
11323           dw_loc_descr_ref t;
11324
11325           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
11326                                       VAR_INIT_STATUS_INITIALIZED);
11327           add_loc_descr (&loc_result, t);
11328           add_loc_descr_op_piece (&loc_result, size);
11329           ++reg;
11330         }
11331       return loc_result;
11332     }
11333
11334   /* Now onto stupid register sets in non contiguous locations.  */
11335
11336   gcc_assert (GET_CODE (regs) == PARALLEL);
11337
11338   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
11339   loc_result = NULL;
11340
11341   for (i = 0; i < XVECLEN (regs, 0); ++i)
11342     {
11343       dw_loc_descr_ref t;
11344
11345       t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
11346                                   VAR_INIT_STATUS_INITIALIZED);
11347       add_loc_descr (&loc_result, t);
11348       add_loc_descr_op_piece (&loc_result, size);
11349     }
11350
11351   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11352     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11353   return loc_result;
11354 }
11355
11356 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
11357
11358 /* Return a location descriptor that designates a constant i,
11359    as a compound operation from constant (i >> shift), constant shift
11360    and DW_OP_shl.  */
11361
11362 static dw_loc_descr_ref
11363 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11364 {
11365   dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
11366   add_loc_descr (&ret, int_loc_descriptor (shift));
11367   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11368   return ret;
11369 }
11370
11371 /* Return a location descriptor that designates a constant.  */
11372
11373 static dw_loc_descr_ref
11374 int_loc_descriptor (HOST_WIDE_INT i)
11375 {
11376   enum dwarf_location_atom op;
11377
11378   /* Pick the smallest representation of a constant, rather than just
11379      defaulting to the LEB encoding.  */
11380   if (i >= 0)
11381     {
11382       int clz = clz_hwi (i);
11383       int ctz = ctz_hwi (i);
11384       if (i <= 31)
11385         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
11386       else if (i <= 0xff)
11387         op = DW_OP_const1u;
11388       else if (i <= 0xffff)
11389         op = DW_OP_const2u;
11390       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11391                && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11392         /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
11393            DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
11394            while DW_OP_const4u is 5 bytes.  */
11395         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
11396       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11397                && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11398         /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
11399            while DW_OP_const4u is 5 bytes.  */
11400         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11401       else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11402         op = DW_OP_const4u;
11403       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11404                && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11405         /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
11406            while DW_OP_constu of constant >= 0x100000000 takes at least
11407            6 bytes.  */
11408         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11409       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11410                && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
11411                   >= HOST_BITS_PER_WIDE_INT)
11412         /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
11413            DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
11414            while DW_OP_constu takes in this case at least 6 bytes.  */
11415         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
11416       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11417                && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11418                && size_of_uleb128 (i) > 6)
11419         /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes.  */
11420         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
11421       else
11422         op = DW_OP_constu;
11423     }
11424   else
11425     {
11426       if (i >= -0x80)
11427         op = DW_OP_const1s;
11428       else if (i >= -0x8000)
11429         op = DW_OP_const2s;
11430       else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11431         {
11432           if (size_of_int_loc_descriptor (i) < 5)
11433             {
11434               dw_loc_descr_ref ret = int_loc_descriptor (-i);
11435               add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11436               return ret;
11437             }
11438           op = DW_OP_const4s;
11439         }
11440       else
11441         {
11442           if (size_of_int_loc_descriptor (i)
11443               < (unsigned long) 1 + size_of_sleb128 (i))
11444             {
11445               dw_loc_descr_ref ret = int_loc_descriptor (-i);
11446               add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11447               return ret;
11448             }
11449           op = DW_OP_consts;
11450         }
11451     }
11452
11453   return new_loc_descr (op, i, 0);
11454 }
11455
11456 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
11457    without actually allocating it.  */
11458
11459 static unsigned long
11460 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11461 {
11462   return size_of_int_loc_descriptor (i >> shift)
11463          + size_of_int_loc_descriptor (shift)
11464          + 1;
11465 }
11466
11467 /* Return size_of_locs (int_loc_descriptor (i)) without
11468    actually allocating it.  */
11469
11470 static unsigned long
11471 size_of_int_loc_descriptor (HOST_WIDE_INT i)
11472 {
11473   unsigned long s;
11474
11475   if (i >= 0)
11476     {
11477       int clz, ctz;
11478       if (i <= 31)
11479         return 1;
11480       else if (i <= 0xff)
11481         return 2;
11482       else if (i <= 0xffff)
11483         return 3;
11484       clz = clz_hwi (i);
11485       ctz = ctz_hwi (i);
11486       if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11487           && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11488         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11489                                                     - clz - 5);
11490       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11491                && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11492         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11493                                                     - clz - 8);
11494       else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11495         return 5;
11496       s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
11497       if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11498           && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11499         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11500                                                     - clz - 8);
11501       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11502                && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
11503         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11504                                                     - clz - 16);
11505       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11506                && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11507                && s > 6)
11508         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11509                                                     - clz - 32);
11510       else
11511         return 1 + s;
11512     }
11513   else
11514     {
11515       if (i >= -0x80)
11516         return 2;
11517       else if (i >= -0x8000)
11518         return 3;
11519       else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11520         {
11521           if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11522             {
11523               s = size_of_int_loc_descriptor (-i) + 1;
11524               if (s < 5)
11525                 return s;
11526             }
11527           return 5;
11528         }
11529       else
11530         {
11531           unsigned long r = 1 + size_of_sleb128 (i);
11532           if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11533             {
11534               s = size_of_int_loc_descriptor (-i) + 1;
11535               if (s < r)
11536                 return s;
11537             }
11538           return r;
11539         }
11540     }
11541 }
11542
11543 /* Return loc description representing "address" of integer value.
11544    This can appear only as toplevel expression.  */
11545
11546 static dw_loc_descr_ref
11547 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
11548 {
11549   int litsize;
11550   dw_loc_descr_ref loc_result = NULL;
11551
11552   if (!(dwarf_version >= 4 || !dwarf_strict))
11553     return NULL;
11554
11555   litsize = size_of_int_loc_descriptor (i);
11556   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
11557      is more compact.  For DW_OP_stack_value we need:
11558      litsize + 1 (DW_OP_stack_value)
11559      and for DW_OP_implicit_value:
11560      1 (DW_OP_implicit_value) + 1 (length) + size.  */
11561   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
11562     {
11563       loc_result = int_loc_descriptor (i);
11564       add_loc_descr (&loc_result,
11565                      new_loc_descr (DW_OP_stack_value, 0, 0));
11566       return loc_result;
11567     }
11568
11569   loc_result = new_loc_descr (DW_OP_implicit_value,
11570                               size, 0);
11571   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11572   loc_result->dw_loc_oprnd2.v.val_int = i;
11573   return loc_result;
11574 }
11575
11576 /* Return a location descriptor that designates a base+offset location.  */
11577
11578 static dw_loc_descr_ref
11579 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
11580                  enum var_init_status initialized)
11581 {
11582   unsigned int regno;
11583   dw_loc_descr_ref result;
11584   dw_fde_ref fde = cfun->fde;
11585
11586   /* We only use "frame base" when we're sure we're talking about the
11587      post-prologue local stack frame.  We do this by *not* running
11588      register elimination until this point, and recognizing the special
11589      argument pointer and soft frame pointer rtx's.  */
11590   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
11591     {
11592       rtx elim = (ira_use_lra_p
11593                   ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
11594                   : eliminate_regs (reg, VOIDmode, NULL_RTX));
11595
11596       if (elim != reg)
11597         {
11598           if (GET_CODE (elim) == PLUS)
11599             {
11600               offset += INTVAL (XEXP (elim, 1));
11601               elim = XEXP (elim, 0);
11602             }
11603           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11604                        && (elim == hard_frame_pointer_rtx
11605                            || elim == stack_pointer_rtx))
11606                       || elim == (frame_pointer_needed
11607                                   ? hard_frame_pointer_rtx
11608                                   : stack_pointer_rtx));
11609
11610           /* If drap register is used to align stack, use frame
11611              pointer + offset to access stack variables.  If stack
11612              is aligned without drap, use stack pointer + offset to
11613              access stack variables.  */
11614           if (crtl->stack_realign_tried
11615               && reg == frame_pointer_rtx)
11616             {
11617               int base_reg
11618                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
11619                                       ? HARD_FRAME_POINTER_REGNUM
11620                                       : REGNO (elim));
11621               return new_reg_loc_descr (base_reg, offset);
11622             }
11623
11624           gcc_assert (frame_pointer_fb_offset_valid);
11625           offset += frame_pointer_fb_offset;
11626           return new_loc_descr (DW_OP_fbreg, offset, 0);
11627         }
11628     }
11629
11630   regno = REGNO (reg);
11631 #ifdef LEAF_REG_REMAP
11632   if (crtl->uses_only_leaf_regs)
11633     {
11634       int leaf_reg = LEAF_REG_REMAP (regno);
11635       if (leaf_reg != -1)
11636         regno = (unsigned) leaf_reg;
11637     }
11638 #endif
11639   regno = DWARF_FRAME_REGNUM (regno);
11640
11641   if (!optimize && fde
11642       && (fde->drap_reg == regno || fde->vdrap_reg == regno))
11643     {
11644       /* Use cfa+offset to represent the location of arguments passed
11645          on the stack when drap is used to align stack.
11646          Only do this when not optimizing, for optimized code var-tracking
11647          is supposed to track where the arguments live and the register
11648          used as vdrap or drap in some spot might be used for something
11649          else in other part of the routine.  */
11650       return new_loc_descr (DW_OP_fbreg, offset, 0);
11651     }
11652
11653   if (regno <= 31)
11654     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11655                             offset, 0);
11656   else
11657     result = new_loc_descr (DW_OP_bregx, regno, offset);
11658
11659   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11660     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11661
11662   return result;
11663 }
11664
11665 /* Return true if this RTL expression describes a base+offset calculation.  */
11666
11667 static inline int
11668 is_based_loc (const_rtx rtl)
11669 {
11670   return (GET_CODE (rtl) == PLUS
11671           && ((REG_P (XEXP (rtl, 0))
11672                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11673                && CONST_INT_P (XEXP (rtl, 1)))));
11674 }
11675
11676 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11677    failed.  */
11678
11679 static dw_loc_descr_ref
11680 tls_mem_loc_descriptor (rtx mem)
11681 {
11682   tree base;
11683   dw_loc_descr_ref loc_result;
11684
11685   if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11686     return NULL;
11687
11688   base = get_base_address (MEM_EXPR (mem));
11689   if (base == NULL
11690       || TREE_CODE (base) != VAR_DECL
11691       || !DECL_THREAD_LOCAL_P (base))
11692     return NULL;
11693
11694   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
11695   if (loc_result == NULL)
11696     return NULL;
11697
11698   if (MEM_OFFSET (mem))
11699     loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11700
11701   return loc_result;
11702 }
11703
11704 /* Output debug info about reason why we failed to expand expression as dwarf
11705    expression.  */
11706
11707 static void
11708 expansion_failed (tree expr, rtx rtl, char const *reason)
11709 {
11710   if (dump_file && (dump_flags & TDF_DETAILS))
11711     {
11712       fprintf (dump_file, "Failed to expand as dwarf: ");
11713       if (expr)
11714         print_generic_expr (dump_file, expr, dump_flags);
11715       if (rtl)
11716         {
11717           fprintf (dump_file, "\n");
11718           print_rtl (dump_file, rtl);
11719         }
11720       fprintf (dump_file, "\nReason: %s\n", reason);
11721     }
11722 }
11723
11724 /* Helper function for const_ok_for_output.  */
11725
11726 static bool
11727 const_ok_for_output_1 (rtx rtl)
11728 {
11729   if (GET_CODE (rtl) == UNSPEC)
11730     {
11731       /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11732          we can't express it in the debug info.  */
11733 #ifdef ENABLE_CHECKING
11734       /* Don't complain about TLS UNSPECs, those are just too hard to
11735          delegitimize.  Note this could be a non-decl SYMBOL_REF such as
11736          one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11737          rather than DECL_THREAD_LOCAL_P is not just an optimization.  */
11738       if (XVECLEN (rtl, 0) == 0
11739           || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11740           || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11741         inform (current_function_decl
11742                 ? DECL_SOURCE_LOCATION (current_function_decl)
11743                 : UNKNOWN_LOCATION,
11744 #if NUM_UNSPEC_VALUES > 0
11745                 "non-delegitimized UNSPEC %s (%d) found in variable location",
11746                 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11747                  ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11748                 XINT (rtl, 1));
11749 #else
11750                 "non-delegitimized UNSPEC %d found in variable location",
11751                 XINT (rtl, 1));
11752 #endif
11753 #endif
11754       expansion_failed (NULL_TREE, rtl,
11755                         "UNSPEC hasn't been delegitimized.\n");
11756       return false;
11757     }
11758
11759   if (targetm.const_not_ok_for_debug_p (rtl))
11760     {
11761       expansion_failed (NULL_TREE, rtl,
11762                         "Expression rejected for debug by the backend.\n");
11763       return false;
11764     }
11765
11766   /* FIXME: Refer to PR60655. It is possible for simplification
11767      of rtl expressions in var tracking to produce such expressions.
11768      We should really identify / validate expressions
11769      enclosed in CONST that can be handled by assemblers on various
11770      targets and only handle legitimate cases here.  */
11771   if (GET_CODE (rtl) != SYMBOL_REF)
11772     {
11773       if (GET_CODE (rtl) == NOT)
11774         return false;
11775       return true;
11776     }
11777
11778   if (CONSTANT_POOL_ADDRESS_P (rtl))
11779     {
11780       bool marked;
11781       get_pool_constant_mark (rtl, &marked);
11782       /* If all references to this pool constant were optimized away,
11783          it was not output and thus we can't represent it.  */
11784       if (!marked)
11785         {
11786           expansion_failed (NULL_TREE, rtl,
11787                             "Constant was removed from constant pool.\n");
11788           return false;
11789         }
11790     }
11791
11792   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11793     return false;
11794
11795   /* Avoid references to external symbols in debug info, on several targets
11796      the linker might even refuse to link when linking a shared library,
11797      and in many other cases the relocations for .debug_info/.debug_loc are
11798      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
11799      to be defined within the same shared library or executable are fine.  */
11800   if (SYMBOL_REF_EXTERNAL_P (rtl))
11801     {
11802       tree decl = SYMBOL_REF_DECL (rtl);
11803
11804       if (decl == NULL || !targetm.binds_local_p (decl))
11805         {
11806           expansion_failed (NULL_TREE, rtl,
11807                             "Symbol not defined in current TU.\n");
11808           return false;
11809         }
11810     }
11811
11812   return true;
11813 }
11814
11815 /* Return true if constant RTL can be emitted in DW_OP_addr or
11816    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
11817    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
11818
11819 static bool
11820 const_ok_for_output (rtx rtl)
11821 {
11822   if (GET_CODE (rtl) == SYMBOL_REF)
11823     return const_ok_for_output_1 (rtl);
11824
11825   if (GET_CODE (rtl) == CONST)
11826     {
11827       subrtx_var_iterator::array_type array;
11828       FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
11829         if (!const_ok_for_output_1 (*iter))
11830           return false;
11831       return true;
11832     }
11833
11834   return true;
11835 }
11836
11837 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11838    if possible, NULL otherwise.  */
11839
11840 static dw_die_ref
11841 base_type_for_mode (machine_mode mode, bool unsignedp)
11842 {
11843   dw_die_ref type_die;
11844   tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11845
11846   if (type == NULL)
11847     return NULL;
11848   switch (TREE_CODE (type))
11849     {
11850     case INTEGER_TYPE:
11851     case REAL_TYPE:
11852       break;
11853     default:
11854       return NULL;
11855     }
11856   type_die = lookup_type_die (type);
11857   if (!type_die)
11858     type_die = modified_type_die (type, TYPE_UNQUALIFIED, comp_unit_die ());
11859   if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11860     return NULL;
11861   return type_die;
11862 }
11863
11864 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11865    type matching MODE, or, if MODE is narrower than or as wide as
11866    DWARF2_ADDR_SIZE, untyped.  Return NULL if the conversion is not
11867    possible.  */
11868
11869 static dw_loc_descr_ref
11870 convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
11871 {
11872   machine_mode outer_mode = mode;
11873   dw_die_ref type_die;
11874   dw_loc_descr_ref cvt;
11875
11876   if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11877     {
11878       add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11879       return op;
11880     }
11881   type_die = base_type_for_mode (outer_mode, 1);
11882   if (type_die == NULL)
11883     return NULL;
11884   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11885   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11886   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11887   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11888   add_loc_descr (&op, cvt);
11889   return op;
11890 }
11891
11892 /* Return location descriptor for comparison OP with operands OP0 and OP1.  */
11893
11894 static dw_loc_descr_ref
11895 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11896                         dw_loc_descr_ref op1)
11897 {
11898   dw_loc_descr_ref ret = op0;
11899   add_loc_descr (&ret, op1);
11900   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11901   if (STORE_FLAG_VALUE != 1)
11902     {
11903       add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11904       add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11905     }
11906   return ret;
11907 }
11908
11909 /* Return location descriptor for signed comparison OP RTL.  */
11910
11911 static dw_loc_descr_ref
11912 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11913                          machine_mode mem_mode)
11914 {
11915   machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11916   dw_loc_descr_ref op0, op1;
11917   int shift;
11918
11919   if (op_mode == VOIDmode)
11920     op_mode = GET_MODE (XEXP (rtl, 1));
11921   if (op_mode == VOIDmode)
11922     return NULL;
11923
11924   if (dwarf_strict
11925       && (GET_MODE_CLASS (op_mode) != MODE_INT
11926           || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11927     return NULL;
11928
11929   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11930                             VAR_INIT_STATUS_INITIALIZED);
11931   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11932                             VAR_INIT_STATUS_INITIALIZED);
11933
11934   if (op0 == NULL || op1 == NULL)
11935     return NULL;
11936
11937   if (GET_MODE_CLASS (op_mode) != MODE_INT
11938       || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11939     return compare_loc_descriptor (op, op0, op1);
11940
11941   if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11942     {
11943       dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11944       dw_loc_descr_ref cvt;
11945
11946       if (type_die == NULL)
11947         return NULL;
11948       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11949       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11950       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11951       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11952       add_loc_descr (&op0, cvt);
11953       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11954       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11955       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11956       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11957       add_loc_descr (&op1, cvt);
11958       return compare_loc_descriptor (op, op0, op1);
11959     }
11960
11961   shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11962   /* For eq/ne, if the operands are known to be zero-extended,
11963      there is no need to do the fancy shifting up.  */
11964   if (op == DW_OP_eq || op == DW_OP_ne)
11965     {
11966       dw_loc_descr_ref last0, last1;
11967       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11968         ;
11969       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11970         ;
11971       /* deref_size zero extends, and for constants we can check
11972          whether they are zero extended or not.  */
11973       if (((last0->dw_loc_opc == DW_OP_deref_size
11974             && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11975            || (CONST_INT_P (XEXP (rtl, 0))
11976                && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11977                   == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11978           && ((last1->dw_loc_opc == DW_OP_deref_size
11979                && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11980               || (CONST_INT_P (XEXP (rtl, 1))
11981                   && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11982                      == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11983         return compare_loc_descriptor (op, op0, op1);
11984
11985       /* EQ/NE comparison against constant in narrower type than
11986          DWARF2_ADDR_SIZE can be performed either as
11987          DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11988          DW_OP_{eq,ne}
11989          or
11990          DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11991          DW_OP_{eq,ne}.  Pick whatever is shorter.  */
11992       if (CONST_INT_P (XEXP (rtl, 1))
11993           && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11994           && (size_of_int_loc_descriptor (shift) + 1
11995               + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11996               >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11997                  + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11998                                                & GET_MODE_MASK (op_mode))))
11999         {
12000           add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
12001           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12002           op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
12003                                     & GET_MODE_MASK (op_mode));
12004           return compare_loc_descriptor (op, op0, op1);
12005         }
12006     }
12007   add_loc_descr (&op0, int_loc_descriptor (shift));
12008   add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
12009   if (CONST_INT_P (XEXP (rtl, 1)))
12010     op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
12011   else
12012     {
12013       add_loc_descr (&op1, int_loc_descriptor (shift));
12014       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
12015     }
12016   return compare_loc_descriptor (op, op0, op1);
12017 }
12018
12019 /* Return location descriptor for unsigned comparison OP RTL.  */
12020
12021 static dw_loc_descr_ref
12022 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
12023                          machine_mode mem_mode)
12024 {
12025   machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
12026   dw_loc_descr_ref op0, op1;
12027
12028   if (op_mode == VOIDmode)
12029     op_mode = GET_MODE (XEXP (rtl, 1));
12030   if (op_mode == VOIDmode)
12031     return NULL;
12032   if (GET_MODE_CLASS (op_mode) != MODE_INT)
12033     return NULL;
12034
12035   if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
12036     return NULL;
12037
12038   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
12039                             VAR_INIT_STATUS_INITIALIZED);
12040   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
12041                             VAR_INIT_STATUS_INITIALIZED);
12042
12043   if (op0 == NULL || op1 == NULL)
12044     return NULL;
12045
12046   if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
12047     {
12048       HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
12049       dw_loc_descr_ref last0, last1;
12050       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
12051         ;
12052       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
12053         ;
12054       if (CONST_INT_P (XEXP (rtl, 0)))
12055         op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
12056       /* deref_size zero extends, so no need to mask it again.  */
12057       else if (last0->dw_loc_opc != DW_OP_deref_size
12058                || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
12059         {
12060           add_loc_descr (&op0, int_loc_descriptor (mask));
12061           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12062         }
12063       if (CONST_INT_P (XEXP (rtl, 1)))
12064         op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
12065       /* deref_size zero extends, so no need to mask it again.  */
12066       else if (last1->dw_loc_opc != DW_OP_deref_size
12067                || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
12068         {
12069           add_loc_descr (&op1, int_loc_descriptor (mask));
12070           add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12071         }
12072     }
12073   else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
12074     {
12075       HOST_WIDE_INT bias = 1;
12076       bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12077       add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12078       if (CONST_INT_P (XEXP (rtl, 1)))
12079         op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
12080                                   + INTVAL (XEXP (rtl, 1)));
12081       else
12082         add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
12083                                             bias, 0));
12084     }
12085   return compare_loc_descriptor (op, op0, op1);
12086 }
12087
12088 /* Return location descriptor for {U,S}{MIN,MAX}.  */
12089
12090 static dw_loc_descr_ref
12091 minmax_loc_descriptor (rtx rtl, machine_mode mode,
12092                        machine_mode mem_mode)
12093 {
12094   enum dwarf_location_atom op;
12095   dw_loc_descr_ref op0, op1, ret;
12096   dw_loc_descr_ref bra_node, drop_node;
12097
12098   if (dwarf_strict
12099       && (GET_MODE_CLASS (mode) != MODE_INT
12100           || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
12101     return NULL;
12102
12103   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12104                             VAR_INIT_STATUS_INITIALIZED);
12105   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12106                             VAR_INIT_STATUS_INITIALIZED);
12107
12108   if (op0 == NULL || op1 == NULL)
12109     return NULL;
12110
12111   add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
12112   add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
12113   add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
12114   if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
12115     {
12116       if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12117         {
12118           HOST_WIDE_INT mask = GET_MODE_MASK (mode);
12119           add_loc_descr (&op0, int_loc_descriptor (mask));
12120           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12121           add_loc_descr (&op1, int_loc_descriptor (mask));
12122           add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12123         }
12124       else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12125         {
12126           HOST_WIDE_INT bias = 1;
12127           bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12128           add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12129           add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12130         }
12131     }
12132   else if (GET_MODE_CLASS (mode) == MODE_INT
12133            && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12134     {
12135       int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
12136       add_loc_descr (&op0, int_loc_descriptor (shift));
12137       add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
12138       add_loc_descr (&op1, int_loc_descriptor (shift));
12139       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
12140     }
12141   else if (GET_MODE_CLASS (mode) == MODE_INT
12142            && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12143     {
12144       dw_die_ref type_die = base_type_for_mode (mode, 0);
12145       dw_loc_descr_ref cvt;
12146       if (type_die == NULL)
12147         return NULL;
12148       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12149       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12150       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12151       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12152       add_loc_descr (&op0, cvt);
12153       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12154       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12155       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12156       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12157       add_loc_descr (&op1, cvt);
12158     }
12159
12160   if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
12161     op = DW_OP_lt;
12162   else
12163     op = DW_OP_gt;
12164   ret = op0;
12165   add_loc_descr (&ret, op1);
12166   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12167   bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12168   add_loc_descr (&ret, bra_node);
12169   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12170   drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12171   add_loc_descr (&ret, drop_node);
12172   bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12173   bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12174   if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
12175       && GET_MODE_CLASS (mode) == MODE_INT
12176       && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12177     ret = convert_descriptor_to_mode (mode, ret);
12178   return ret;
12179 }
12180
12181 /* Helper function for mem_loc_descriptor.  Perform OP binary op,
12182    but after converting arguments to type_die, afterwards
12183    convert back to unsigned.  */
12184
12185 static dw_loc_descr_ref
12186 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
12187              machine_mode mode, machine_mode mem_mode)
12188 {
12189   dw_loc_descr_ref cvt, op0, op1;
12190
12191   if (type_die == NULL)
12192     return NULL;
12193   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12194                             VAR_INIT_STATUS_INITIALIZED);
12195   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12196                             VAR_INIT_STATUS_INITIALIZED);
12197   if (op0 == NULL || op1 == NULL)
12198     return NULL;
12199   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12200   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12201   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12202   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12203   add_loc_descr (&op0, cvt);
12204   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12205   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12206   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12207   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12208   add_loc_descr (&op1, cvt);
12209   add_loc_descr (&op0, op1);
12210   add_loc_descr (&op0, new_loc_descr (op, 0, 0));
12211   return convert_descriptor_to_mode (mode, op0);
12212 }
12213
12214 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
12215    const0 is DW_OP_lit0 or corresponding typed constant,
12216    const1 is DW_OP_lit1 or corresponding typed constant
12217    and constMSB is constant with just the MSB bit set
12218    for the mode):
12219        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12220    L1: const0 DW_OP_swap
12221    L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
12222        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12223    L3: DW_OP_drop
12224    L4: DW_OP_nop
12225
12226    CTZ is similar:
12227        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12228    L1: const0 DW_OP_swap
12229    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12230        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12231    L3: DW_OP_drop
12232    L4: DW_OP_nop
12233
12234    FFS is similar:
12235        DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
12236    L1: const1 DW_OP_swap
12237    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12238        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12239    L3: DW_OP_drop
12240    L4: DW_OP_nop  */
12241
12242 static dw_loc_descr_ref
12243 clz_loc_descriptor (rtx rtl, machine_mode mode,
12244                     machine_mode mem_mode)
12245 {
12246   dw_loc_descr_ref op0, ret, tmp;
12247   HOST_WIDE_INT valv;
12248   dw_loc_descr_ref l1jump, l1label;
12249   dw_loc_descr_ref l2jump, l2label;
12250   dw_loc_descr_ref l3jump, l3label;
12251   dw_loc_descr_ref l4jump, l4label;
12252   rtx msb;
12253
12254   if (GET_MODE_CLASS (mode) != MODE_INT
12255       || GET_MODE (XEXP (rtl, 0)) != mode)
12256     return NULL;
12257
12258   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12259                             VAR_INIT_STATUS_INITIALIZED);
12260   if (op0 == NULL)
12261     return NULL;
12262   ret = op0;
12263   if (GET_CODE (rtl) == CLZ)
12264     {
12265       if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12266         valv = GET_MODE_BITSIZE (mode);
12267     }
12268   else if (GET_CODE (rtl) == FFS)
12269     valv = 0;
12270   else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12271     valv = GET_MODE_BITSIZE (mode);
12272   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12273   l1jump = new_loc_descr (DW_OP_bra, 0, 0);
12274   add_loc_descr (&ret, l1jump);
12275   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12276   tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
12277                             VAR_INIT_STATUS_INITIALIZED);
12278   if (tmp == NULL)
12279     return NULL;
12280   add_loc_descr (&ret, tmp);
12281   l4jump = new_loc_descr (DW_OP_skip, 0, 0);
12282   add_loc_descr (&ret, l4jump);
12283   l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
12284                                 ? const1_rtx : const0_rtx,
12285                                 mode, mem_mode,
12286                                 VAR_INIT_STATUS_INITIALIZED);
12287   if (l1label == NULL)
12288     return NULL;
12289   add_loc_descr (&ret, l1label);
12290   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12291   l2label = new_loc_descr (DW_OP_dup, 0, 0);
12292   add_loc_descr (&ret, l2label);
12293   if (GET_CODE (rtl) != CLZ)
12294     msb = const1_rtx;
12295   else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
12296     msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
12297                    << (GET_MODE_BITSIZE (mode) - 1));
12298   else
12299     msb = immed_wide_int_const
12300       (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
12301                             GET_MODE_PRECISION (mode)), mode);
12302   if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
12303     tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12304                          ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
12305                          ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
12306   else
12307     tmp = mem_loc_descriptor (msb, mode, mem_mode,
12308                               VAR_INIT_STATUS_INITIALIZED);
12309   if (tmp == NULL)
12310     return NULL;
12311   add_loc_descr (&ret, tmp);
12312   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12313   l3jump = new_loc_descr (DW_OP_bra, 0, 0);
12314   add_loc_descr (&ret, l3jump);
12315   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12316                             VAR_INIT_STATUS_INITIALIZED);
12317   if (tmp == NULL)
12318     return NULL;
12319   add_loc_descr (&ret, tmp);
12320   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
12321                                       ? DW_OP_shl : DW_OP_shr, 0, 0));
12322   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12323   add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
12324   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12325   l2jump = new_loc_descr (DW_OP_skip, 0, 0);
12326   add_loc_descr (&ret, l2jump);
12327   l3label = new_loc_descr (DW_OP_drop, 0, 0);
12328   add_loc_descr (&ret, l3label);
12329   l4label = new_loc_descr (DW_OP_nop, 0, 0);
12330   add_loc_descr (&ret, l4label);
12331   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12332   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12333   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12334   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12335   l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12336   l3jump->dw_loc_oprnd1.v.val_loc = l3label;
12337   l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12338   l4jump->dw_loc_oprnd1.v.val_loc = l4label;
12339   return ret;
12340 }
12341
12342 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
12343    const1 is DW_OP_lit1 or corresponding typed constant):
12344        const0 DW_OP_swap
12345    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12346        DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12347    L2: DW_OP_drop
12348
12349    PARITY is similar:
12350    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12351        DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12352    L2: DW_OP_drop  */
12353
12354 static dw_loc_descr_ref
12355 popcount_loc_descriptor (rtx rtl, machine_mode mode,
12356                          machine_mode mem_mode)
12357 {
12358   dw_loc_descr_ref op0, ret, tmp;
12359   dw_loc_descr_ref l1jump, l1label;
12360   dw_loc_descr_ref l2jump, l2label;
12361
12362   if (GET_MODE_CLASS (mode) != MODE_INT
12363       || GET_MODE (XEXP (rtl, 0)) != mode)
12364     return NULL;
12365
12366   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12367                             VAR_INIT_STATUS_INITIALIZED);
12368   if (op0 == NULL)
12369     return NULL;
12370   ret = op0;
12371   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12372                             VAR_INIT_STATUS_INITIALIZED);
12373   if (tmp == NULL)
12374     return NULL;
12375   add_loc_descr (&ret, tmp);
12376   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12377   l1label = new_loc_descr (DW_OP_dup, 0, 0);
12378   add_loc_descr (&ret, l1label);
12379   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12380   add_loc_descr (&ret, l2jump);
12381   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12382   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12383   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12384                             VAR_INIT_STATUS_INITIALIZED);
12385   if (tmp == NULL)
12386     return NULL;
12387   add_loc_descr (&ret, tmp);
12388   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12389   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
12390                                       ? DW_OP_plus : DW_OP_xor, 0, 0));
12391   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12392   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12393                             VAR_INIT_STATUS_INITIALIZED);
12394   add_loc_descr (&ret, tmp);
12395   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12396   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12397   add_loc_descr (&ret, l1jump);
12398   l2label = new_loc_descr (DW_OP_drop, 0, 0);
12399   add_loc_descr (&ret, l2label);
12400   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12401   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12402   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12403   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12404   return ret;
12405 }
12406
12407 /* BSWAP (constS is initial shift count, either 56 or 24):
12408        constS const0
12409    L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
12410        const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
12411        DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
12412        DW_OP_minus DW_OP_swap DW_OP_skip <L1>
12413    L2: DW_OP_drop DW_OP_swap DW_OP_drop  */
12414
12415 static dw_loc_descr_ref
12416 bswap_loc_descriptor (rtx rtl, machine_mode mode,
12417                       machine_mode mem_mode)
12418 {
12419   dw_loc_descr_ref op0, ret, tmp;
12420   dw_loc_descr_ref l1jump, l1label;
12421   dw_loc_descr_ref l2jump, l2label;
12422
12423   if (GET_MODE_CLASS (mode) != MODE_INT
12424       || BITS_PER_UNIT != 8
12425       || (GET_MODE_BITSIZE (mode) != 32
12426           &&  GET_MODE_BITSIZE (mode) != 64))
12427     return NULL;
12428
12429   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12430                             VAR_INIT_STATUS_INITIALIZED);
12431   if (op0 == NULL)
12432     return NULL;
12433
12434   ret = op0;
12435   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12436                             mode, mem_mode,
12437                             VAR_INIT_STATUS_INITIALIZED);
12438   if (tmp == NULL)
12439     return NULL;
12440   add_loc_descr (&ret, tmp);
12441   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12442                             VAR_INIT_STATUS_INITIALIZED);
12443   if (tmp == NULL)
12444     return NULL;
12445   add_loc_descr (&ret, tmp);
12446   l1label = new_loc_descr (DW_OP_pick, 2, 0);
12447   add_loc_descr (&ret, l1label);
12448   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12449                             mode, mem_mode,
12450                             VAR_INIT_STATUS_INITIALIZED);
12451   add_loc_descr (&ret, tmp);
12452   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
12453   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12454   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12455   tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
12456                             VAR_INIT_STATUS_INITIALIZED);
12457   if (tmp == NULL)
12458     return NULL;
12459   add_loc_descr (&ret, tmp);
12460   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12461   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
12462   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12463   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12464   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12465   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12466   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12467                             VAR_INIT_STATUS_INITIALIZED);
12468   add_loc_descr (&ret, tmp);
12469   add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
12470   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12471   add_loc_descr (&ret, l2jump);
12472   tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
12473                             VAR_INIT_STATUS_INITIALIZED);
12474   add_loc_descr (&ret, tmp);
12475   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12476   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12477   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12478   add_loc_descr (&ret, l1jump);
12479   l2label = new_loc_descr (DW_OP_drop, 0, 0);
12480   add_loc_descr (&ret, l2label);
12481   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12482   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12483   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12484   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12485   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12486   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12487   return ret;
12488 }
12489
12490 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
12491    DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12492    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
12493    DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
12494
12495    ROTATERT is similar:
12496    DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
12497    DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12498    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or  */
12499
12500 static dw_loc_descr_ref
12501 rotate_loc_descriptor (rtx rtl, machine_mode mode,
12502                        machine_mode mem_mode)
12503 {
12504   rtx rtlop1 = XEXP (rtl, 1);
12505   dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
12506   int i;
12507
12508   if (GET_MODE_CLASS (mode) != MODE_INT)
12509     return NULL;
12510
12511   if (GET_MODE (rtlop1) != VOIDmode
12512       && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
12513     rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12514   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12515                             VAR_INIT_STATUS_INITIALIZED);
12516   op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12517                             VAR_INIT_STATUS_INITIALIZED);
12518   if (op0 == NULL || op1 == NULL)
12519     return NULL;
12520   if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12521     for (i = 0; i < 2; i++)
12522       {
12523         if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
12524           mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
12525                                         mode, mem_mode,
12526                                         VAR_INIT_STATUS_INITIALIZED);
12527         else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12528           mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12529                                    ? DW_OP_const4u
12530                                    : HOST_BITS_PER_WIDE_INT == 64
12531                                    ? DW_OP_const8u : DW_OP_constu,
12532                                    GET_MODE_MASK (mode), 0);
12533         else
12534           mask[i] = NULL;
12535         if (mask[i] == NULL)
12536           return NULL;
12537         add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
12538       }
12539   ret = op0;
12540   add_loc_descr (&ret, op1);
12541   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12542   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12543   if (GET_CODE (rtl) == ROTATERT)
12544     {
12545       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12546       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12547                                           GET_MODE_BITSIZE (mode), 0));
12548     }
12549   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12550   if (mask[0] != NULL)
12551     add_loc_descr (&ret, mask[0]);
12552   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12553   if (mask[1] != NULL)
12554     {
12555       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12556       add_loc_descr (&ret, mask[1]);
12557       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12558     }
12559   if (GET_CODE (rtl) == ROTATE)
12560     {
12561       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12562       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12563                                           GET_MODE_BITSIZE (mode), 0));
12564     }
12565   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12566   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12567   return ret;
12568 }
12569
12570 /* Helper function for mem_loc_descriptor.  Return DW_OP_GNU_parameter_ref
12571    for DEBUG_PARAMETER_REF RTL.  */
12572
12573 static dw_loc_descr_ref
12574 parameter_ref_descriptor (rtx rtl)
12575 {
12576   dw_loc_descr_ref ret;
12577   dw_die_ref ref;
12578
12579   if (dwarf_strict)
12580     return NULL;
12581   gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
12582   ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
12583   ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
12584   if (ref)
12585     {
12586       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12587       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12588       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12589     }
12590   else
12591     {
12592       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12593       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
12594     }
12595   return ret;
12596 }
12597
12598 /* The following routine converts the RTL for a variable or parameter
12599    (resident in memory) into an equivalent Dwarf representation of a
12600    mechanism for getting the address of that same variable onto the top of a
12601    hypothetical "address evaluation" stack.
12602
12603    When creating memory location descriptors, we are effectively transforming
12604    the RTL for a memory-resident object into its Dwarf postfix expression
12605    equivalent.  This routine recursively descends an RTL tree, turning
12606    it into Dwarf postfix code as it goes.
12607
12608    MODE is the mode that should be assumed for the rtl if it is VOIDmode.
12609
12610    MEM_MODE is the mode of the memory reference, needed to handle some
12611    autoincrement addressing modes.
12612
12613    Return 0 if we can't represent the location.  */
12614
12615 dw_loc_descr_ref
12616 mem_loc_descriptor (rtx rtl, machine_mode mode,
12617                     machine_mode mem_mode,
12618                     enum var_init_status initialized)
12619 {
12620   dw_loc_descr_ref mem_loc_result = NULL;
12621   enum dwarf_location_atom op;
12622   dw_loc_descr_ref op0, op1;
12623   rtx inner = NULL_RTX;
12624
12625   if (mode == VOIDmode)
12626     mode = GET_MODE (rtl);
12627
12628   /* Note that for a dynamically sized array, the location we will generate a
12629      description of here will be the lowest numbered location which is
12630      actually within the array.  That's *not* necessarily the same as the
12631      zeroth element of the array.  */
12632
12633   rtl = targetm.delegitimize_address (rtl);
12634
12635   if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
12636     return NULL;
12637
12638   switch (GET_CODE (rtl))
12639     {
12640     case POST_INC:
12641     case POST_DEC:
12642     case POST_MODIFY:
12643       return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
12644
12645     case SUBREG:
12646       /* The case of a subreg may arise when we have a local (register)
12647          variable or a formal (register) parameter which doesn't quite fill
12648          up an entire register.  For now, just assume that it is
12649          legitimate to make the Dwarf info refer to the whole register which
12650          contains the given subreg.  */
12651       if (!subreg_lowpart_p (rtl))
12652         break;
12653       inner = SUBREG_REG (rtl);
12654     case TRUNCATE:
12655       if (inner == NULL_RTX)
12656         inner = XEXP (rtl, 0);
12657       if (GET_MODE_CLASS (mode) == MODE_INT
12658           && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
12659           && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12660 #ifdef POINTERS_EXTEND_UNSIGNED
12661               || (mode == Pmode && mem_mode != VOIDmode)
12662 #endif
12663              )
12664           && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
12665         {
12666           mem_loc_result = mem_loc_descriptor (inner,
12667                                                GET_MODE (inner),
12668                                                mem_mode, initialized);
12669           break;
12670         }
12671       if (dwarf_strict)
12672         break;
12673       if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
12674         break;
12675       if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
12676           && (GET_MODE_CLASS (mode) != MODE_INT
12677               || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
12678         break;
12679       else
12680         {
12681           dw_die_ref type_die;
12682           dw_loc_descr_ref cvt;
12683
12684           mem_loc_result = mem_loc_descriptor (inner,
12685                                                GET_MODE (inner),
12686                                                mem_mode, initialized);
12687           if (mem_loc_result == NULL)
12688             break;
12689           type_die = base_type_for_mode (mode,
12690                                          GET_MODE_CLASS (mode) == MODE_INT);
12691           if (type_die == NULL)
12692             {
12693               mem_loc_result = NULL;
12694               break;
12695             }
12696           if (GET_MODE_SIZE (mode)
12697               != GET_MODE_SIZE (GET_MODE (inner)))
12698             cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12699           else
12700             cvt = new_loc_descr (DW_OP_GNU_reinterpret, 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_die;
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 REG:
12709       if (GET_MODE_CLASS (mode) != MODE_INT
12710           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12711               && rtl != arg_pointer_rtx
12712               && rtl != frame_pointer_rtx
12713 #ifdef POINTERS_EXTEND_UNSIGNED
12714               && (mode != Pmode || mem_mode == VOIDmode)
12715 #endif
12716               ))
12717         {
12718           dw_die_ref type_die;
12719           unsigned int dbx_regnum;
12720
12721           if (dwarf_strict)
12722             break;
12723           if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12724             break;
12725           type_die = base_type_for_mode (mode,
12726                                          GET_MODE_CLASS (mode) == MODE_INT);
12727           if (type_die == NULL)
12728             break;
12729
12730           dbx_regnum = dbx_reg_number (rtl);
12731           if (dbx_regnum == IGNORED_DWARF_REGNUM)
12732             break;
12733           mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12734                                           dbx_regnum, 0);
12735           mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12736           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12737           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12738           break;
12739         }
12740       /* Whenever a register number forms a part of the description of the
12741          method for calculating the (dynamic) address of a memory resident
12742          object, DWARF rules require the register number be referred to as
12743          a "base register".  This distinction is not based in any way upon
12744          what category of register the hardware believes the given register
12745          belongs to.  This is strictly DWARF terminology we're dealing with
12746          here. Note that in cases where the location of a memory-resident
12747          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12748          OP_CONST (0)) the actual DWARF location descriptor that we generate
12749          may just be OP_BASEREG (basereg).  This may look deceptively like
12750          the object in question was allocated to a register (rather than in
12751          memory) so DWARF consumers need to be aware of the subtle
12752          distinction between OP_REG and OP_BASEREG.  */
12753       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12754         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12755       else if (stack_realign_drap
12756                && crtl->drap_reg
12757                && crtl->args.internal_arg_pointer == rtl
12758                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12759         {
12760           /* If RTL is internal_arg_pointer, which has been optimized
12761              out, use DRAP instead.  */
12762           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12763                                             VAR_INIT_STATUS_INITIALIZED);
12764         }
12765       break;
12766
12767     case SIGN_EXTEND:
12768     case ZERO_EXTEND:
12769       if (GET_MODE_CLASS (mode) != MODE_INT)
12770         break;
12771       op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12772                                 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12773       if (op0 == 0)
12774         break;
12775       else if (GET_CODE (rtl) == ZERO_EXTEND
12776                && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12777                && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12778                   < HOST_BITS_PER_WIDE_INT
12779                /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12780                   to expand zero extend as two shifts instead of
12781                   masking.  */
12782                && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12783         {
12784           machine_mode imode = GET_MODE (XEXP (rtl, 0));
12785           mem_loc_result = op0;
12786           add_loc_descr (&mem_loc_result,
12787                          int_loc_descriptor (GET_MODE_MASK (imode)));
12788           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12789         }
12790       else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12791         {
12792           int shift = DWARF2_ADDR_SIZE
12793                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12794           shift *= BITS_PER_UNIT;
12795           if (GET_CODE (rtl) == SIGN_EXTEND)
12796             op = DW_OP_shra;
12797           else
12798             op = DW_OP_shr;
12799           mem_loc_result = op0;
12800           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12801           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12802           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12803           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12804         }
12805       else if (!dwarf_strict)
12806         {
12807           dw_die_ref type_die1, type_die2;
12808           dw_loc_descr_ref cvt;
12809
12810           type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12811                                           GET_CODE (rtl) == ZERO_EXTEND);
12812           if (type_die1 == NULL)
12813             break;
12814           type_die2 = base_type_for_mode (mode, 1);
12815           if (type_die2 == NULL)
12816             break;
12817           mem_loc_result = op0;
12818           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12819           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12820           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12821           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12822           add_loc_descr (&mem_loc_result, cvt);
12823           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12824           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12825           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12826           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12827           add_loc_descr (&mem_loc_result, cvt);
12828         }
12829       break;
12830
12831     case MEM:
12832       {
12833         rtx new_rtl = avoid_constant_pool_reference (rtl);
12834         if (new_rtl != rtl)
12835           {
12836             mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12837                                                  initialized);
12838             if (mem_loc_result != NULL)
12839               return mem_loc_result;
12840           }
12841       }
12842       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12843                                            get_address_mode (rtl), mode,
12844                                            VAR_INIT_STATUS_INITIALIZED);
12845       if (mem_loc_result == NULL)
12846         mem_loc_result = tls_mem_loc_descriptor (rtl);
12847       if (mem_loc_result != NULL)
12848         {
12849           if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12850               || GET_MODE_CLASS (mode) != MODE_INT)
12851             {
12852               dw_die_ref type_die;
12853               dw_loc_descr_ref deref;
12854
12855               if (dwarf_strict)
12856                 return NULL;
12857               type_die
12858                 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12859               if (type_die == NULL)
12860                 return NULL;
12861               deref = new_loc_descr (DW_OP_GNU_deref_type,
12862                                      GET_MODE_SIZE (mode), 0);
12863               deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12864               deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12865               deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12866               add_loc_descr (&mem_loc_result, deref);
12867             }
12868           else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12869             add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12870           else
12871             add_loc_descr (&mem_loc_result,
12872                            new_loc_descr (DW_OP_deref_size,
12873                                           GET_MODE_SIZE (mode), 0));
12874         }
12875       break;
12876
12877     case LO_SUM:
12878       return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12879
12880     case LABEL_REF:
12881       /* Some ports can transform a symbol ref into a label ref, because
12882          the symbol ref is too far away and has to be dumped into a constant
12883          pool.  */
12884     case CONST:
12885     case SYMBOL_REF:
12886       if ((GET_MODE_CLASS (mode) != MODE_INT
12887            && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
12888           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12889 #ifdef POINTERS_EXTEND_UNSIGNED
12890               && (mode != Pmode || mem_mode == VOIDmode)
12891 #endif
12892               ))
12893         break;
12894       if (GET_CODE (rtl) == SYMBOL_REF
12895           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12896         {
12897           dw_loc_descr_ref temp;
12898
12899           /* If this is not defined, we have no way to emit the data.  */
12900           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12901             break;
12902
12903           temp = new_addr_loc_descr (rtl, dtprel_true);
12904
12905           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12906           add_loc_descr (&mem_loc_result, temp);
12907
12908           break;
12909         }
12910
12911       if (!const_ok_for_output (rtl))
12912         {
12913           if (GET_CODE (rtl) == CONST)
12914             mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12915                                                  initialized);
12916           break;
12917         }
12918
12919     symref:
12920       mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12921       vec_safe_push (used_rtx_array, rtl);
12922       break;
12923
12924     case CONCAT:
12925     case CONCATN:
12926     case VAR_LOCATION:
12927     case DEBUG_IMPLICIT_PTR:
12928       expansion_failed (NULL_TREE, rtl,
12929                         "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12930       return 0;
12931
12932     case ENTRY_VALUE:
12933       if (dwarf_strict)
12934         return NULL;
12935       if (REG_P (ENTRY_VALUE_EXP (rtl)))
12936         {
12937           if (GET_MODE_CLASS (mode) != MODE_INT
12938               || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12939             op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12940                                       VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12941           else
12942             {
12943               unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12944               if (dbx_regnum == IGNORED_DWARF_REGNUM)
12945                 return NULL;
12946               op0 = one_reg_loc_descriptor (dbx_regnum,
12947                                             VAR_INIT_STATUS_INITIALIZED);
12948             }
12949         }
12950       else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12951                && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12952         {
12953           op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12954                                     VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12955           if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12956             return NULL;
12957         }
12958       else
12959         gcc_unreachable ();
12960       if (op0 == NULL)
12961         return NULL;
12962       mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12963       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12964       mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12965       break;
12966
12967     case DEBUG_PARAMETER_REF:
12968       mem_loc_result = parameter_ref_descriptor (rtl);
12969       break;
12970
12971     case PRE_MODIFY:
12972       /* Extract the PLUS expression nested inside and fall into
12973          PLUS code below.  */
12974       rtl = XEXP (rtl, 1);
12975       goto plus;
12976
12977     case PRE_INC:
12978     case PRE_DEC:
12979       /* Turn these into a PLUS expression and fall into the PLUS code
12980          below.  */
12981       rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12982                           gen_int_mode (GET_CODE (rtl) == PRE_INC
12983                                         ? GET_MODE_UNIT_SIZE (mem_mode)
12984                                         : -GET_MODE_UNIT_SIZE (mem_mode),
12985                                         mode));
12986
12987       /* ... fall through ...  */
12988
12989     case PLUS:
12990     plus:
12991       if (is_based_loc (rtl)
12992           && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12993               || XEXP (rtl, 0) == arg_pointer_rtx
12994               || XEXP (rtl, 0) == frame_pointer_rtx)
12995           && GET_MODE_CLASS (mode) == MODE_INT)
12996         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12997                                           INTVAL (XEXP (rtl, 1)),
12998                                           VAR_INIT_STATUS_INITIALIZED);
12999       else
13000         {
13001           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13002                                                VAR_INIT_STATUS_INITIALIZED);
13003           if (mem_loc_result == 0)
13004             break;
13005
13006           if (CONST_INT_P (XEXP (rtl, 1))
13007               && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13008             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
13009           else
13010             {
13011               op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13012                                         VAR_INIT_STATUS_INITIALIZED);
13013               if (op1 == 0)
13014                 return NULL;
13015               add_loc_descr (&mem_loc_result, op1);
13016               add_loc_descr (&mem_loc_result,
13017                              new_loc_descr (DW_OP_plus, 0, 0));
13018             }
13019         }
13020       break;
13021
13022     /* If a pseudo-reg is optimized away, it is possible for it to
13023        be replaced with a MEM containing a multiply or shift.  */
13024     case MINUS:
13025       op = DW_OP_minus;
13026       goto do_binop;
13027
13028     case MULT:
13029       op = DW_OP_mul;
13030       goto do_binop;
13031
13032     case DIV:
13033       if (!dwarf_strict
13034           && GET_MODE_CLASS (mode) == MODE_INT
13035           && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
13036         {
13037           mem_loc_result = typed_binop (DW_OP_div, rtl,
13038                                         base_type_for_mode (mode, 0),
13039                                         mode, mem_mode);
13040           break;
13041         }
13042       op = DW_OP_div;
13043       goto do_binop;
13044
13045     case UMOD:
13046       op = DW_OP_mod;
13047       goto do_binop;
13048
13049     case ASHIFT:
13050       op = DW_OP_shl;
13051       goto do_shift;
13052
13053     case ASHIFTRT:
13054       op = DW_OP_shra;
13055       goto do_shift;
13056
13057     case LSHIFTRT:
13058       op = DW_OP_shr;
13059       goto do_shift;
13060
13061     do_shift:
13062       if (GET_MODE_CLASS (mode) != MODE_INT)
13063         break;
13064       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13065                                 VAR_INIT_STATUS_INITIALIZED);
13066       {
13067         rtx rtlop1 = XEXP (rtl, 1);
13068         if (GET_MODE (rtlop1) != VOIDmode
13069             && GET_MODE_BITSIZE (GET_MODE (rtlop1))
13070                < GET_MODE_BITSIZE (mode))
13071           rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
13072         op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
13073                                   VAR_INIT_STATUS_INITIALIZED);
13074       }
13075
13076       if (op0 == 0 || op1 == 0)
13077         break;
13078
13079       mem_loc_result = op0;
13080       add_loc_descr (&mem_loc_result, op1);
13081       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13082       break;
13083
13084     case AND:
13085       op = DW_OP_and;
13086       goto do_binop;
13087
13088     case IOR:
13089       op = DW_OP_or;
13090       goto do_binop;
13091
13092     case XOR:
13093       op = DW_OP_xor;
13094       goto do_binop;
13095
13096     do_binop:
13097       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13098                                 VAR_INIT_STATUS_INITIALIZED);
13099       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13100                                 VAR_INIT_STATUS_INITIALIZED);
13101
13102       if (op0 == 0 || op1 == 0)
13103         break;
13104
13105       mem_loc_result = op0;
13106       add_loc_descr (&mem_loc_result, op1);
13107       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13108       break;
13109
13110     case MOD:
13111       if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
13112         {
13113           mem_loc_result = typed_binop (DW_OP_mod, rtl,
13114                                         base_type_for_mode (mode, 0),
13115                                         mode, mem_mode);
13116           break;
13117         }
13118
13119       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13120                                 VAR_INIT_STATUS_INITIALIZED);
13121       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13122                                 VAR_INIT_STATUS_INITIALIZED);
13123
13124       if (op0 == 0 || op1 == 0)
13125         break;
13126
13127       mem_loc_result = op0;
13128       add_loc_descr (&mem_loc_result, op1);
13129       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13130       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13131       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
13132       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13133       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
13134       break;
13135
13136     case UDIV:
13137       if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
13138         {
13139           if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
13140             {
13141               op = DW_OP_div;
13142               goto do_binop;
13143             }
13144           mem_loc_result = typed_binop (DW_OP_div, rtl,
13145                                         base_type_for_mode (mode, 1),
13146                                         mode, mem_mode);
13147         }
13148       break;
13149
13150     case NOT:
13151       op = DW_OP_not;
13152       goto do_unop;
13153
13154     case ABS:
13155       op = DW_OP_abs;
13156       goto do_unop;
13157
13158     case NEG:
13159       op = DW_OP_neg;
13160       goto do_unop;
13161
13162     do_unop:
13163       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13164                                 VAR_INIT_STATUS_INITIALIZED);
13165
13166       if (op0 == 0)
13167         break;
13168
13169       mem_loc_result = op0;
13170       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13171       break;
13172
13173     case CONST_INT:
13174       if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13175 #ifdef POINTERS_EXTEND_UNSIGNED
13176           || (mode == Pmode
13177               && mem_mode != VOIDmode
13178               && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
13179 #endif
13180           )
13181         {
13182           mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13183           break;
13184         }
13185       if (!dwarf_strict
13186           && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
13187               || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
13188         {
13189           dw_die_ref type_die = base_type_for_mode (mode, 1);
13190           machine_mode amode;
13191           if (type_die == NULL)
13192             return NULL;
13193           amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
13194                                  MODE_INT, 0);
13195           if (INTVAL (rtl) >= 0
13196               && amode != BLKmode
13197               && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
13198               /* const DW_OP_GNU_convert <XXX> vs.
13199                  DW_OP_GNU_const_type <XXX, 1, const>.  */
13200               && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
13201                  < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
13202             {
13203               mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13204               op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13205               op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13206               op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13207               op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
13208               add_loc_descr (&mem_loc_result, op0);
13209               return mem_loc_result;
13210             }
13211           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
13212                                           INTVAL (rtl));
13213           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13214           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13215           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13216           if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
13217             mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13218           else
13219             {
13220               mem_loc_result->dw_loc_oprnd2.val_class
13221                 = dw_val_class_const_double;
13222               mem_loc_result->dw_loc_oprnd2.v.val_double
13223                 = double_int::from_shwi (INTVAL (rtl));
13224             }
13225         }
13226       break;
13227
13228     case CONST_DOUBLE:
13229       if (!dwarf_strict)
13230         {
13231           dw_die_ref type_die;
13232
13233           /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
13234              CONST_DOUBLE rtx could represent either a large integer
13235              or a floating-point constant.  If TARGET_SUPPORTS_WIDE_INT != 0,
13236              the value is always a floating point constant.
13237
13238              When it is an integer, a CONST_DOUBLE is used whenever
13239              the constant requires 2 HWIs to be adequately represented.
13240              We output CONST_DOUBLEs as blocks.  */
13241           if (mode == VOIDmode
13242               || (GET_MODE (rtl) == VOIDmode
13243                   && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
13244             break;
13245           type_die = base_type_for_mode (mode,
13246                                          GET_MODE_CLASS (mode) == MODE_INT);
13247           if (type_die == NULL)
13248             return NULL;
13249           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13250           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13251           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13252           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13253 #if TARGET_SUPPORTS_WIDE_INT == 0
13254           if (!SCALAR_FLOAT_MODE_P (mode))
13255             {
13256               mem_loc_result->dw_loc_oprnd2.val_class
13257                 = dw_val_class_const_double;
13258               mem_loc_result->dw_loc_oprnd2.v.val_double
13259                 = rtx_to_double_int (rtl);
13260             }
13261           else
13262 #endif
13263             {
13264               unsigned int length = GET_MODE_SIZE (mode);
13265               unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13266
13267               insert_float (rtl, array);
13268               mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13269               mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13270               mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13271               mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13272             }
13273         }
13274       break;
13275
13276     case CONST_WIDE_INT:
13277       if (!dwarf_strict)
13278         {
13279           dw_die_ref type_die;
13280
13281           type_die = base_type_for_mode (mode,
13282                                          GET_MODE_CLASS (mode) == MODE_INT);
13283           if (type_die == NULL)
13284             return NULL;
13285           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13286           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13287           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13288           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13289           mem_loc_result->dw_loc_oprnd2.val_class
13290             = dw_val_class_wide_int;
13291           mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
13292           *mem_loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13293         }
13294       break;
13295
13296     case EQ:
13297       mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
13298       break;
13299
13300     case GE:
13301       mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13302       break;
13303
13304     case GT:
13305       mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13306       break;
13307
13308     case LE:
13309       mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13310       break;
13311
13312     case LT:
13313       mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13314       break;
13315
13316     case NE:
13317       mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
13318       break;
13319
13320     case GEU:
13321       mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13322       break;
13323
13324     case GTU:
13325       mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13326       break;
13327
13328     case LEU:
13329       mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13330       break;
13331
13332     case LTU:
13333       mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13334       break;
13335
13336     case UMIN:
13337     case UMAX:
13338       if (GET_MODE_CLASS (mode) != MODE_INT)
13339         break;
13340       /* FALLTHRU */
13341     case SMIN:
13342     case SMAX:
13343       mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
13344       break;
13345
13346     case ZERO_EXTRACT:
13347     case SIGN_EXTRACT:
13348       if (CONST_INT_P (XEXP (rtl, 1))
13349           && CONST_INT_P (XEXP (rtl, 2))
13350           && ((unsigned) INTVAL (XEXP (rtl, 1))
13351               + (unsigned) INTVAL (XEXP (rtl, 2))
13352               <= GET_MODE_BITSIZE (mode))
13353           && GET_MODE_CLASS (mode) == MODE_INT
13354           && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13355           && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
13356         {
13357           int shift, size;
13358           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13359                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
13360           if (op0 == 0)
13361             break;
13362           if (GET_CODE (rtl) == SIGN_EXTRACT)
13363             op = DW_OP_shra;
13364           else
13365             op = DW_OP_shr;
13366           mem_loc_result = op0;
13367           size = INTVAL (XEXP (rtl, 1));
13368           shift = INTVAL (XEXP (rtl, 2));
13369           if (BITS_BIG_ENDIAN)
13370             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
13371                     - shift - size;
13372           if (shift + size != (int) DWARF2_ADDR_SIZE)
13373             {
13374               add_loc_descr (&mem_loc_result,
13375                              int_loc_descriptor (DWARF2_ADDR_SIZE
13376                                                  - shift - size));
13377               add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13378             }
13379           if (size != (int) DWARF2_ADDR_SIZE)
13380             {
13381               add_loc_descr (&mem_loc_result,
13382                              int_loc_descriptor (DWARF2_ADDR_SIZE - size));
13383               add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13384             }
13385         }
13386       break;
13387
13388     case IF_THEN_ELSE:
13389       {
13390         dw_loc_descr_ref op2, bra_node, drop_node;
13391         op0 = mem_loc_descriptor (XEXP (rtl, 0),
13392                                   GET_MODE (XEXP (rtl, 0)) == VOIDmode
13393                                   ? word_mode : GET_MODE (XEXP (rtl, 0)),
13394                                   mem_mode, VAR_INIT_STATUS_INITIALIZED);
13395         op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13396                                   VAR_INIT_STATUS_INITIALIZED);
13397         op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
13398                                   VAR_INIT_STATUS_INITIALIZED);
13399         if (op0 == NULL || op1 == NULL || op2 == NULL)
13400           break;
13401
13402         mem_loc_result = op1;
13403         add_loc_descr (&mem_loc_result, op2);
13404         add_loc_descr (&mem_loc_result, op0);
13405         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13406         add_loc_descr (&mem_loc_result, bra_node);
13407         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
13408         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
13409         add_loc_descr (&mem_loc_result, drop_node);
13410         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13411         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
13412       }
13413       break;
13414
13415     case FLOAT_EXTEND:
13416     case FLOAT_TRUNCATE:
13417     case FLOAT:
13418     case UNSIGNED_FLOAT:
13419     case FIX:
13420     case UNSIGNED_FIX:
13421       if (!dwarf_strict)
13422         {
13423           dw_die_ref type_die;
13424           dw_loc_descr_ref cvt;
13425
13426           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13427                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
13428           if (op0 == NULL)
13429             break;
13430           if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
13431               && (GET_CODE (rtl) == FLOAT
13432                   || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
13433                      <= DWARF2_ADDR_SIZE))
13434             {
13435               type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
13436                                              GET_CODE (rtl) == UNSIGNED_FLOAT);
13437               if (type_die == NULL)
13438                 break;
13439               cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13440               cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13441               cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13442               cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13443               add_loc_descr (&op0, cvt);
13444             }
13445           type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
13446           if (type_die == NULL)
13447             break;
13448           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13449           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13450           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13451           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13452           add_loc_descr (&op0, cvt);
13453           if (GET_MODE_CLASS (mode) == MODE_INT
13454               && (GET_CODE (rtl) == FIX
13455                   || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
13456             {
13457               op0 = convert_descriptor_to_mode (mode, op0);
13458               if (op0 == NULL)
13459                 break;
13460             }
13461           mem_loc_result = op0;
13462         }
13463       break;
13464
13465     case CLZ:
13466     case CTZ:
13467     case FFS:
13468       mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
13469       break;
13470
13471     case POPCOUNT:
13472     case PARITY:
13473       mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
13474       break;
13475
13476     case BSWAP:
13477       mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
13478       break;
13479
13480     case ROTATE:
13481     case ROTATERT:
13482       mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
13483       break;
13484
13485     case COMPARE:
13486       /* In theory, we could implement the above.  */
13487       /* DWARF cannot represent the unsigned compare operations
13488          natively.  */
13489     case SS_MULT:
13490     case US_MULT:
13491     case SS_DIV:
13492     case US_DIV:
13493     case SS_PLUS:
13494     case US_PLUS:
13495     case SS_MINUS:
13496     case US_MINUS:
13497     case SS_NEG:
13498     case US_NEG:
13499     case SS_ABS:
13500     case SS_ASHIFT:
13501     case US_ASHIFT:
13502     case SS_TRUNCATE:
13503     case US_TRUNCATE:
13504     case UNORDERED:
13505     case ORDERED:
13506     case UNEQ:
13507     case UNGE:
13508     case UNGT:
13509     case UNLE:
13510     case UNLT:
13511     case LTGT:
13512     case FRACT_CONVERT:
13513     case UNSIGNED_FRACT_CONVERT:
13514     case SAT_FRACT:
13515     case UNSIGNED_SAT_FRACT:
13516     case SQRT:
13517     case ASM_OPERANDS:
13518     case VEC_MERGE:
13519     case VEC_SELECT:
13520     case VEC_CONCAT:
13521     case VEC_DUPLICATE:
13522     case UNSPEC:
13523     case HIGH:
13524     case FMA:
13525     case STRICT_LOW_PART:
13526     case CONST_VECTOR:
13527     case CONST_FIXED:
13528     case CLRSB:
13529     case CLOBBER:
13530       /* If delegitimize_address couldn't do anything with the UNSPEC, we
13531          can't express it in the debug info.  This can happen e.g. with some
13532          TLS UNSPECs.  */
13533       break;
13534
13535     case CONST_STRING:
13536       resolve_one_addr (&rtl);
13537       goto symref;
13538
13539     default:
13540 #ifdef ENABLE_CHECKING
13541       print_rtl (stderr, rtl);
13542       gcc_unreachable ();
13543 #else
13544       break;
13545 #endif
13546     }
13547
13548   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13549     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13550
13551   return mem_loc_result;
13552 }
13553
13554 /* Return a descriptor that describes the concatenation of two locations.
13555    This is typically a complex variable.  */
13556
13557 static dw_loc_descr_ref
13558 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
13559 {
13560   dw_loc_descr_ref cc_loc_result = NULL;
13561   dw_loc_descr_ref x0_ref
13562     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13563   dw_loc_descr_ref x1_ref
13564     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13565
13566   if (x0_ref == 0 || x1_ref == 0)
13567     return 0;
13568
13569   cc_loc_result = x0_ref;
13570   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
13571
13572   add_loc_descr (&cc_loc_result, x1_ref);
13573   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
13574
13575   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13576     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13577
13578   return cc_loc_result;
13579 }
13580
13581 /* Return a descriptor that describes the concatenation of N
13582    locations.  */
13583
13584 static dw_loc_descr_ref
13585 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
13586 {
13587   unsigned int i;
13588   dw_loc_descr_ref cc_loc_result = NULL;
13589   unsigned int n = XVECLEN (concatn, 0);
13590
13591   for (i = 0; i < n; ++i)
13592     {
13593       dw_loc_descr_ref ref;
13594       rtx x = XVECEXP (concatn, 0, i);
13595
13596       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13597       if (ref == NULL)
13598         return NULL;
13599
13600       add_loc_descr (&cc_loc_result, ref);
13601       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
13602     }
13603
13604   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13605     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13606
13607   return cc_loc_result;
13608 }
13609
13610 /* Helper function for loc_descriptor.  Return DW_OP_GNU_implicit_pointer
13611    for DEBUG_IMPLICIT_PTR RTL.  */
13612
13613 static dw_loc_descr_ref
13614 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
13615 {
13616   dw_loc_descr_ref ret;
13617   dw_die_ref ref;
13618
13619   if (dwarf_strict)
13620     return NULL;
13621   gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
13622               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
13623               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
13624   ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
13625   ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
13626   ret->dw_loc_oprnd2.val_class = dw_val_class_const;
13627   if (ref)
13628     {
13629       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13630       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13631       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13632     }
13633   else
13634     {
13635       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13636       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
13637     }
13638   return ret;
13639 }
13640
13641 /* Output a proper Dwarf location descriptor for a variable or parameter
13642    which is either allocated in a register or in a memory location.  For a
13643    register, we just generate an OP_REG and the register number.  For a
13644    memory location we provide a Dwarf postfix expression describing how to
13645    generate the (dynamic) address of the object onto the address stack.
13646
13647    MODE is mode of the decl if this loc_descriptor is going to be used in
13648    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13649    allowed, VOIDmode otherwise.
13650
13651    If we don't know how to describe it, return 0.  */
13652
13653 static dw_loc_descr_ref
13654 loc_descriptor (rtx rtl, machine_mode mode,
13655                 enum var_init_status initialized)
13656 {
13657   dw_loc_descr_ref loc_result = NULL;
13658
13659   switch (GET_CODE (rtl))
13660     {
13661     case SUBREG:
13662       /* The case of a subreg may arise when we have a local (register)
13663          variable or a formal (register) parameter which doesn't quite fill
13664          up an entire register.  For now, just assume that it is
13665          legitimate to make the Dwarf info refer to the whole register which
13666          contains the given subreg.  */
13667       if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
13668         loc_result = loc_descriptor (SUBREG_REG (rtl),
13669                                      GET_MODE (SUBREG_REG (rtl)), initialized);
13670       else
13671         goto do_default;
13672       break;
13673
13674     case REG:
13675       loc_result = reg_loc_descriptor (rtl, initialized);
13676       break;
13677
13678     case MEM:
13679       loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13680                                        GET_MODE (rtl), initialized);
13681       if (loc_result == NULL)
13682         loc_result = tls_mem_loc_descriptor (rtl);
13683       if (loc_result == NULL)
13684         {
13685           rtx new_rtl = avoid_constant_pool_reference (rtl);
13686           if (new_rtl != rtl)
13687             loc_result = loc_descriptor (new_rtl, mode, initialized);
13688         }
13689       break;
13690
13691     case CONCAT:
13692       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13693                                           initialized);
13694       break;
13695
13696     case CONCATN:
13697       loc_result = concatn_loc_descriptor (rtl, initialized);
13698       break;
13699
13700     case VAR_LOCATION:
13701       /* Single part.  */
13702       if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
13703         {
13704           rtx loc = PAT_VAR_LOCATION_LOC (rtl);
13705           if (GET_CODE (loc) == EXPR_LIST)
13706             loc = XEXP (loc, 0);
13707           loc_result = loc_descriptor (loc, mode, initialized);
13708           break;
13709         }
13710
13711       rtl = XEXP (rtl, 1);
13712       /* FALLTHRU */
13713
13714     case PARALLEL:
13715       {
13716         rtvec par_elems = XVEC (rtl, 0);
13717         int num_elem = GET_NUM_ELEM (par_elems);
13718         machine_mode mode;
13719         int i;
13720
13721         /* Create the first one, so we have something to add to.  */
13722         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13723                                      VOIDmode, initialized);
13724         if (loc_result == NULL)
13725           return NULL;
13726         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13727         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13728         for (i = 1; i < num_elem; i++)
13729           {
13730             dw_loc_descr_ref temp;
13731
13732             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13733                                    VOIDmode, initialized);
13734             if (temp == NULL)
13735               return NULL;
13736             add_loc_descr (&loc_result, temp);
13737             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13738             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13739           }
13740       }
13741       break;
13742
13743     case CONST_INT:
13744       if (mode != VOIDmode && mode != BLKmode)
13745         loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13746                                                     INTVAL (rtl));
13747       break;
13748
13749     case CONST_DOUBLE:
13750       if (mode == VOIDmode)
13751         mode = GET_MODE (rtl);
13752
13753       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13754         {
13755           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13756
13757           /* Note that a CONST_DOUBLE rtx could represent either an integer
13758              or a floating-point constant.  A CONST_DOUBLE is used whenever
13759              the constant requires more than one word in order to be
13760              adequately represented.  We output CONST_DOUBLEs as blocks.  */
13761           loc_result = new_loc_descr (DW_OP_implicit_value,
13762                                       GET_MODE_SIZE (mode), 0);
13763 #if TARGET_SUPPORTS_WIDE_INT == 0
13764           if (!SCALAR_FLOAT_MODE_P (mode))
13765             {
13766               loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13767               loc_result->dw_loc_oprnd2.v.val_double
13768                 = rtx_to_double_int (rtl);
13769             }
13770           else
13771 #endif
13772             {
13773               unsigned int length = GET_MODE_SIZE (mode);
13774               unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13775
13776               insert_float (rtl, array);
13777               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13778               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13779               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13780               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13781             }
13782         }
13783       break;
13784
13785     case CONST_WIDE_INT:
13786       if (mode == VOIDmode)
13787         mode = GET_MODE (rtl);
13788
13789       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13790         {
13791           loc_result = new_loc_descr (DW_OP_implicit_value,
13792                                       GET_MODE_SIZE (mode), 0);
13793           loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
13794           loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
13795           *loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13796         }
13797       break;
13798
13799     case CONST_VECTOR:
13800       if (mode == VOIDmode)
13801         mode = GET_MODE (rtl);
13802
13803       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13804         {
13805           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13806           unsigned int length = CONST_VECTOR_NUNITS (rtl);
13807           unsigned char *array
13808             = ggc_vec_alloc<unsigned char> (length * elt_size);
13809           unsigned int i;
13810           unsigned char *p;
13811           machine_mode imode = GET_MODE_INNER (mode);
13812
13813           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13814           switch (GET_MODE_CLASS (mode))
13815             {
13816             case MODE_VECTOR_INT:
13817               for (i = 0, p = array; i < length; i++, p += elt_size)
13818                 {
13819                   rtx elt = CONST_VECTOR_ELT (rtl, i);
13820                   insert_wide_int (std::make_pair (elt, imode), p, elt_size);
13821                 }
13822               break;
13823
13824             case MODE_VECTOR_FLOAT:
13825               for (i = 0, p = array; i < length; i++, p += elt_size)
13826                 {
13827                   rtx elt = CONST_VECTOR_ELT (rtl, i);
13828                   insert_float (elt, p);
13829                 }
13830               break;
13831
13832             default:
13833               gcc_unreachable ();
13834             }
13835
13836           loc_result = new_loc_descr (DW_OP_implicit_value,
13837                                       length * elt_size, 0);
13838           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13839           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13840           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13841           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13842         }
13843       break;
13844
13845     case CONST:
13846       if (mode == VOIDmode
13847           || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13848           || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13849           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13850         {
13851           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13852           break;
13853         }
13854       /* FALLTHROUGH */
13855     case SYMBOL_REF:
13856       if (!const_ok_for_output (rtl))
13857         break;
13858     case LABEL_REF:
13859       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13860           && (dwarf_version >= 4 || !dwarf_strict))
13861         {
13862          loc_result = new_addr_loc_descr (rtl, dtprel_false);
13863           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13864           vec_safe_push (used_rtx_array, rtl);
13865         }
13866       break;
13867
13868     case DEBUG_IMPLICIT_PTR:
13869       loc_result = implicit_ptr_descriptor (rtl, 0);
13870       break;
13871
13872     case PLUS:
13873       if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13874           && CONST_INT_P (XEXP (rtl, 1)))
13875         {
13876           loc_result
13877             = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13878           break;
13879         }
13880       /* FALLTHRU */
13881     do_default:
13882     default:
13883       if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13884            && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13885            && dwarf_version >= 4)
13886           || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13887         {
13888           /* Value expression.  */
13889           loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13890           if (loc_result)
13891             add_loc_descr (&loc_result,
13892                            new_loc_descr (DW_OP_stack_value, 0, 0));
13893         }
13894       break;
13895     }
13896
13897   return loc_result;
13898 }
13899
13900 /* We need to figure out what section we should use as the base for the
13901    address ranges where a given location is valid.
13902    1. If this particular DECL has a section associated with it, use that.
13903    2. If this function has a section associated with it, use that.
13904    3. Otherwise, use the text section.
13905    XXX: If you split a variable across multiple sections, we won't notice.  */
13906
13907 static const char *
13908 secname_for_decl (const_tree decl)
13909 {
13910   const char *secname;
13911
13912   if (VAR_OR_FUNCTION_DECL_P (decl)
13913       && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
13914       && DECL_SECTION_NAME (decl))
13915     secname = DECL_SECTION_NAME (decl);
13916   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13917     secname = DECL_SECTION_NAME (current_function_decl);
13918   else if (cfun && in_cold_section_p)
13919     secname = crtl->subsections.cold_section_label;
13920   else
13921     secname = text_section_label;
13922
13923   return secname;
13924 }
13925
13926 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
13927
13928 static bool
13929 decl_by_reference_p (tree decl)
13930 {
13931   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13932            || TREE_CODE (decl) == VAR_DECL)
13933           && DECL_BY_REFERENCE (decl));
13934 }
13935
13936 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
13937    for VARLOC.  */
13938
13939 static dw_loc_descr_ref
13940 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13941                enum var_init_status initialized)
13942 {
13943   int have_address = 0;
13944   dw_loc_descr_ref descr;
13945   machine_mode mode;
13946
13947   if (want_address != 2)
13948     {
13949       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13950       /* Single part.  */
13951       if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13952         {
13953           varloc = PAT_VAR_LOCATION_LOC (varloc);
13954           if (GET_CODE (varloc) == EXPR_LIST)
13955             varloc = XEXP (varloc, 0);
13956           mode = GET_MODE (varloc);
13957           if (MEM_P (varloc))
13958             {
13959               rtx addr = XEXP (varloc, 0);
13960               descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13961                                           mode, initialized);
13962               if (descr)
13963                 have_address = 1;
13964               else
13965                 {
13966                   rtx x = avoid_constant_pool_reference (varloc);
13967                   if (x != varloc)
13968                     descr = mem_loc_descriptor (x, mode, VOIDmode,
13969                                                 initialized);
13970                 }
13971             }
13972           else
13973             descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13974         }
13975       else
13976         return 0;
13977     }
13978   else
13979     {
13980       if (GET_CODE (varloc) == VAR_LOCATION)
13981         mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13982       else
13983         mode = DECL_MODE (loc);
13984       descr = loc_descriptor (varloc, mode, initialized);
13985       have_address = 1;
13986     }
13987
13988   if (!descr)
13989     return 0;
13990
13991   if (want_address == 2 && !have_address
13992       && (dwarf_version >= 4 || !dwarf_strict))
13993     {
13994       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13995         {
13996           expansion_failed (loc, NULL_RTX,
13997                             "DWARF address size mismatch");
13998           return 0;
13999         }
14000       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
14001       have_address = 1;
14002     }
14003   /* Show if we can't fill the request for an address.  */
14004   if (want_address && !have_address)
14005     {
14006       expansion_failed (loc, NULL_RTX,
14007                         "Want address and only have value");
14008       return 0;
14009     }
14010
14011   /* If we've got an address and don't want one, dereference.  */
14012   if (!want_address && have_address)
14013     {
14014       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14015       enum dwarf_location_atom op;
14016
14017       if (size > DWARF2_ADDR_SIZE || size == -1)
14018         {
14019           expansion_failed (loc, NULL_RTX,
14020                             "DWARF address size mismatch");
14021           return 0;
14022         }
14023       else if (size == DWARF2_ADDR_SIZE)
14024         op = DW_OP_deref;
14025       else
14026         op = DW_OP_deref_size;
14027
14028       add_loc_descr (&descr, new_loc_descr (op, size, 0));
14029     }
14030
14031   return descr;
14032 }
14033
14034 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
14035    if it is not possible.  */
14036
14037 static dw_loc_descr_ref
14038 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
14039 {
14040   if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
14041     return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
14042   else if (dwarf_version >= 3 || !dwarf_strict)
14043     return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
14044   else
14045     return NULL;
14046 }
14047
14048 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
14049    for VAR_LOC_NOTE for variable DECL that has been optimized by SRA.  */
14050
14051 static dw_loc_descr_ref
14052 dw_sra_loc_expr (tree decl, rtx loc)
14053 {
14054   rtx p;
14055   unsigned HOST_WIDE_INT padsize = 0;
14056   dw_loc_descr_ref descr, *descr_tail;
14057   unsigned HOST_WIDE_INT decl_size;
14058   rtx varloc;
14059   enum var_init_status initialized;
14060
14061   if (DECL_SIZE (decl) == NULL
14062       || !tree_fits_uhwi_p (DECL_SIZE (decl)))
14063     return NULL;
14064
14065   decl_size = tree_to_uhwi (DECL_SIZE (decl));
14066   descr = NULL;
14067   descr_tail = &descr;
14068
14069   for (p = loc; p; p = XEXP (p, 1))
14070     {
14071       unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
14072       rtx loc_note = *decl_piece_varloc_ptr (p);
14073       dw_loc_descr_ref cur_descr;
14074       dw_loc_descr_ref *tail, last = NULL;
14075       unsigned HOST_WIDE_INT opsize = 0;
14076
14077       if (loc_note == NULL_RTX
14078           || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
14079         {
14080           padsize += bitsize;
14081           continue;
14082         }
14083       initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
14084       varloc = NOTE_VAR_LOCATION (loc_note);
14085       cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
14086       if (cur_descr == NULL)
14087         {
14088           padsize += bitsize;
14089           continue;
14090         }
14091
14092       /* Check that cur_descr either doesn't use
14093          DW_OP_*piece operations, or their sum is equal
14094          to bitsize.  Otherwise we can't embed it.  */
14095       for (tail = &cur_descr; *tail != NULL;
14096            tail = &(*tail)->dw_loc_next)
14097         if ((*tail)->dw_loc_opc == DW_OP_piece)
14098           {
14099             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
14100                       * BITS_PER_UNIT;
14101             last = *tail;
14102           }
14103         else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
14104           {
14105             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
14106             last = *tail;
14107           }
14108
14109       if (last != NULL && opsize != bitsize)
14110         {
14111           padsize += bitsize;
14112           /* Discard the current piece of the descriptor and release any
14113              addr_table entries it uses.  */
14114           remove_loc_list_addr_table_entries (cur_descr);
14115           continue;
14116         }
14117
14118       /* If there is a hole, add DW_OP_*piece after empty DWARF
14119          expression, which means that those bits are optimized out.  */
14120       if (padsize)
14121         {
14122           if (padsize > decl_size)
14123             {
14124               remove_loc_list_addr_table_entries (cur_descr);
14125               goto discard_descr;
14126             }
14127           decl_size -= padsize;
14128           *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
14129           if (*descr_tail == NULL)
14130             {
14131               remove_loc_list_addr_table_entries (cur_descr);
14132               goto discard_descr;
14133             }
14134           descr_tail = &(*descr_tail)->dw_loc_next;
14135           padsize = 0;
14136         }
14137       *descr_tail = cur_descr;
14138       descr_tail = tail;
14139       if (bitsize > decl_size)
14140         goto discard_descr;
14141       decl_size -= bitsize;
14142       if (last == NULL)
14143         {
14144           HOST_WIDE_INT offset = 0;
14145           if (GET_CODE (varloc) == VAR_LOCATION
14146               && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14147             {
14148               varloc = PAT_VAR_LOCATION_LOC (varloc);
14149               if (GET_CODE (varloc) == EXPR_LIST)
14150                 varloc = XEXP (varloc, 0);
14151             }
14152           do 
14153             {
14154               if (GET_CODE (varloc) == CONST
14155                   || GET_CODE (varloc) == SIGN_EXTEND
14156                   || GET_CODE (varloc) == ZERO_EXTEND)
14157                 varloc = XEXP (varloc, 0);
14158               else if (GET_CODE (varloc) == SUBREG)
14159                 varloc = SUBREG_REG (varloc);
14160               else
14161                 break;
14162             }
14163           while (1);
14164           /* DW_OP_bit_size offset should be zero for register
14165              or implicit location descriptions and empty location
14166              descriptions, but for memory addresses needs big endian
14167              adjustment.  */
14168           if (MEM_P (varloc))
14169             {
14170               unsigned HOST_WIDE_INT memsize
14171                 = MEM_SIZE (varloc) * BITS_PER_UNIT;
14172               if (memsize != bitsize)
14173                 {
14174                   if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
14175                       && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
14176                     goto discard_descr;
14177                   if (memsize < bitsize)
14178                     goto discard_descr;
14179                   if (BITS_BIG_ENDIAN)
14180                     offset = memsize - bitsize;
14181                 }
14182             }
14183
14184           *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
14185           if (*descr_tail == NULL)
14186             goto discard_descr;
14187           descr_tail = &(*descr_tail)->dw_loc_next;
14188         }
14189     }
14190
14191   /* If there were any non-empty expressions, add padding till the end of
14192      the decl.  */
14193   if (descr != NULL && decl_size != 0)
14194     {
14195       *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
14196       if (*descr_tail == NULL)
14197         goto discard_descr;
14198     }
14199   return descr;
14200
14201 discard_descr:
14202   /* Discard the descriptor and release any addr_table entries it uses.  */
14203   remove_loc_list_addr_table_entries (descr);
14204   return NULL;
14205 }
14206
14207 /* Return the dwarf representation of the location list LOC_LIST of
14208    DECL.  WANT_ADDRESS has the same meaning as in loc_list_from_tree
14209    function.  */
14210
14211 static dw_loc_list_ref
14212 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
14213 {
14214   const char *endname, *secname;
14215   rtx varloc;
14216   enum var_init_status initialized;
14217   struct var_loc_node *node;
14218   dw_loc_descr_ref descr;
14219   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
14220   dw_loc_list_ref list = NULL;
14221   dw_loc_list_ref *listp = &list;
14222
14223   /* Now that we know what section we are using for a base,
14224      actually construct the list of locations.
14225      The first location information is what is passed to the
14226      function that creates the location list, and the remaining
14227      locations just get added on to that list.
14228      Note that we only know the start address for a location
14229      (IE location changes), so to build the range, we use
14230      the range [current location start, next location start].
14231      This means we have to special case the last node, and generate
14232      a range of [last location start, end of function label].  */
14233
14234   secname = secname_for_decl (decl);
14235
14236   for (node = loc_list->first; node; node = node->next)
14237     if (GET_CODE (node->loc) == EXPR_LIST
14238         || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
14239       {
14240         if (GET_CODE (node->loc) == EXPR_LIST)
14241           {
14242             /* This requires DW_OP_{,bit_}piece, which is not usable
14243                inside DWARF expressions.  */
14244             if (want_address != 2)
14245               continue;
14246             descr = dw_sra_loc_expr (decl, node->loc);
14247             if (descr == NULL)
14248               continue;
14249           }
14250         else
14251           {
14252             initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14253             varloc = NOTE_VAR_LOCATION (node->loc);
14254             descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
14255           }
14256         if (descr)
14257           {
14258             bool range_across_switch = false;
14259             /* If section switch happens in between node->label
14260                and node->next->label (or end of function) and
14261                we can't emit it as a single entry list,
14262                emit two ranges, first one ending at the end
14263                of first partition and second one starting at the
14264                beginning of second partition.  */
14265             if (node == loc_list->last_before_switch
14266                 && (node != loc_list->first || loc_list->first->next)
14267                 && current_function_decl)
14268               {
14269                 endname = cfun->fde->dw_fde_end;
14270                 range_across_switch = true;
14271               }
14272             /* The variable has a location between NODE->LABEL and
14273                NODE->NEXT->LABEL.  */
14274             else if (node->next)
14275               endname = node->next->label;
14276             /* If the variable has a location at the last label
14277                it keeps its location until the end of function.  */
14278             else if (!current_function_decl)
14279               endname = text_end_label;
14280             else
14281               {
14282                 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14283                                              current_function_funcdef_no);
14284                 endname = ggc_strdup (label_id);
14285               }
14286
14287             *listp = new_loc_list (descr, node->label, endname, secname);
14288             if (TREE_CODE (decl) == PARM_DECL
14289                 && node == loc_list->first
14290                 && NOTE_P (node->loc)
14291                 && strcmp (node->label, endname) == 0)
14292               (*listp)->force = true;
14293             listp = &(*listp)->dw_loc_next;
14294
14295             if (range_across_switch)
14296               {
14297                 if (GET_CODE (node->loc) == EXPR_LIST)
14298                   descr = dw_sra_loc_expr (decl, node->loc);
14299                 else
14300                   {
14301                     initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14302                     varloc = NOTE_VAR_LOCATION (node->loc);
14303                     descr = dw_loc_list_1 (decl, varloc, want_address,
14304                                            initialized);
14305                   }
14306                 gcc_assert (descr);
14307                 /* The variable has a location between NODE->LABEL and
14308                    NODE->NEXT->LABEL.  */
14309                 if (node->next)
14310                   endname = node->next->label;
14311                 else
14312                   endname = cfun->fde->dw_fde_second_end;
14313                 *listp = new_loc_list (descr,
14314                                        cfun->fde->dw_fde_second_begin,
14315                                        endname, secname);
14316                 listp = &(*listp)->dw_loc_next;
14317               }
14318           }
14319       }
14320
14321   /* Try to avoid the overhead of a location list emitting a location
14322      expression instead, but only if we didn't have more than one
14323      location entry in the first place.  If some entries were not
14324      representable, we don't want to pretend a single entry that was
14325      applies to the entire scope in which the variable is
14326      available.  */
14327   if (list && loc_list->first->next)
14328     gen_llsym (list);
14329
14330   return list;
14331 }
14332
14333 /* Return if the loc_list has only single element and thus can be represented
14334    as location description.   */
14335
14336 static bool
14337 single_element_loc_list_p (dw_loc_list_ref list)
14338 {
14339   gcc_assert (!list->dw_loc_next || list->ll_symbol);
14340   return !list->ll_symbol;
14341 }
14342
14343 /* To each location in list LIST add loc descr REF.  */
14344
14345 static void
14346 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
14347 {
14348   dw_loc_descr_ref copy;
14349   add_loc_descr (&list->expr, ref);
14350   list = list->dw_loc_next;
14351   while (list)
14352     {
14353       copy = ggc_alloc<dw_loc_descr_node> ();
14354       memcpy (copy, ref, sizeof (dw_loc_descr_node));
14355       add_loc_descr (&list->expr, copy);
14356       while (copy->dw_loc_next)
14357         {
14358           dw_loc_descr_ref new_copy = ggc_alloc<dw_loc_descr_node> ();
14359           memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
14360           copy->dw_loc_next = new_copy;
14361           copy = new_copy;
14362         }
14363       list = list->dw_loc_next;
14364     }
14365 }
14366
14367 /* Given two lists RET and LIST
14368    produce location list that is result of adding expression in LIST
14369    to expression in RET on each position in program.
14370    Might be destructive on both RET and LIST.
14371
14372    TODO: We handle only simple cases of RET or LIST having at most one
14373    element. General case would inolve sorting the lists in program order
14374    and merging them that will need some additional work.
14375    Adding that will improve quality of debug info especially for SRA-ed
14376    structures.  */
14377
14378 static void
14379 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
14380 {
14381   if (!list)
14382     return;
14383   if (!*ret)
14384     {
14385       *ret = list;
14386       return;
14387     }
14388   if (!list->dw_loc_next)
14389     {
14390       add_loc_descr_to_each (*ret, list->expr);
14391       return;
14392     }
14393   if (!(*ret)->dw_loc_next)
14394     {
14395       add_loc_descr_to_each (list, (*ret)->expr);
14396       *ret = list;
14397       return;
14398     }
14399   expansion_failed (NULL_TREE, NULL_RTX,
14400                     "Don't know how to merge two non-trivial"
14401                     " location lists.\n");
14402   *ret = NULL;
14403   return;
14404 }
14405
14406 /* LOC is constant expression.  Try a luck, look it up in constant
14407    pool and return its loc_descr of its address.  */
14408
14409 static dw_loc_descr_ref
14410 cst_pool_loc_descr (tree loc)
14411 {
14412   /* Get an RTL for this, if something has been emitted.  */
14413   rtx rtl = lookup_constant_def (loc);
14414
14415   if (!rtl || !MEM_P (rtl))
14416     {
14417       gcc_assert (!rtl);
14418       return 0;
14419     }
14420   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
14421
14422   /* TODO: We might get more coverage if we was actually delaying expansion
14423      of all expressions till end of compilation when constant pools are fully
14424      populated.  */
14425   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
14426     {
14427       expansion_failed (loc, NULL_RTX,
14428                         "CST value in contant pool but not marked.");
14429       return 0;
14430     }
14431   return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
14432                              GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
14433 }
14434
14435 /* Return dw_loc_list representing address of addr_expr LOC
14436    by looking for inner INDIRECT_REF expression and turning
14437    it into simple arithmetics.
14438
14439    See loc_list_from_tree for the meaning of CONTEXT.  */
14440
14441 static dw_loc_list_ref
14442 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
14443                                                    const loc_descr_context *context)
14444 {
14445   tree obj, offset;
14446   HOST_WIDE_INT bitsize, bitpos, bytepos;
14447   machine_mode mode;
14448   int unsignedp, volatilep = 0;
14449   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14450
14451   obj = get_inner_reference (TREE_OPERAND (loc, 0),
14452                              &bitsize, &bitpos, &offset, &mode,
14453                              &unsignedp, &volatilep, false);
14454   STRIP_NOPS (obj);
14455   if (bitpos % BITS_PER_UNIT)
14456     {
14457       expansion_failed (loc, NULL_RTX, "bitfield access");
14458       return 0;
14459     }
14460   if (!INDIRECT_REF_P (obj))
14461     {
14462       expansion_failed (obj,
14463                         NULL_RTX, "no indirect ref in inner refrence");
14464       return 0;
14465     }
14466   if (!offset && !bitpos)
14467     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
14468                                    context);
14469   else if (toplev
14470            && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
14471            && (dwarf_version >= 4 || !dwarf_strict))
14472     {
14473       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
14474       if (!list_ret)
14475         return 0;
14476       if (offset)
14477         {
14478           /* Variable offset.  */
14479           list_ret1 = loc_list_from_tree (offset, 0, context);
14480           if (list_ret1 == 0)
14481             return 0;
14482           add_loc_list (&list_ret, list_ret1);
14483           if (!list_ret)
14484             return 0;
14485           add_loc_descr_to_each (list_ret,
14486                                  new_loc_descr (DW_OP_plus, 0, 0));
14487         }
14488       bytepos = bitpos / BITS_PER_UNIT;
14489       if (bytepos > 0)
14490         add_loc_descr_to_each (list_ret,
14491                                new_loc_descr (DW_OP_plus_uconst,
14492                                               bytepos, 0));
14493       else if (bytepos < 0)
14494         loc_list_plus_const (list_ret, bytepos);
14495       add_loc_descr_to_each (list_ret,
14496                              new_loc_descr (DW_OP_stack_value, 0, 0));
14497     }
14498   return list_ret;
14499 }
14500
14501
14502 /* Helper structure for location descriptions generation.  */
14503 struct loc_descr_context
14504 {
14505   /* The type that is implicitly referenced by DW_OP_push_object_address, or
14506      NULL_TREE if DW_OP_push_object_address in invalid for this location
14507      description.  This is used when processing PLACEHOLDER_EXPR nodes.  */
14508   tree context_type;
14509   /* The ..._DECL node that should be translated as a
14510      DW_OP_push_object_address operation.  */
14511   tree base_decl;
14512 };
14513
14514 /* Generate Dwarf location list representing LOC.
14515    If WANT_ADDRESS is false, expression computing LOC will be computed
14516    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
14517    if WANT_ADDRESS is 2, expression computing address useable in location
14518      will be returned (i.e. DW_OP_reg can be used
14519      to refer to register values).
14520
14521    CONTEXT provides information to customize the location descriptions
14522    generation.  Its context_type field specifies what type is implicitly
14523    referenced by DW_OP_push_object_address.  If it is NULL_TREE, this operation
14524    will not be generated.
14525
14526    If CONTEXT is NULL, the behavior is the same as if both context_type and
14527    base_decl fields were NULL_TREE.  */
14528
14529 static dw_loc_list_ref
14530 loc_list_from_tree (tree loc, int want_address,
14531                     const struct loc_descr_context *context)
14532 {
14533   dw_loc_descr_ref ret = NULL, ret1 = NULL;
14534   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14535   int have_address = 0;
14536   enum dwarf_location_atom op;
14537
14538   /* ??? Most of the time we do not take proper care for sign/zero
14539      extending the values properly.  Hopefully this won't be a real
14540      problem...  */
14541
14542   if (context != NULL
14543       && context->base_decl == loc
14544       && want_address == 0)
14545     {
14546       if (dwarf_version >= 3 || !dwarf_strict)
14547         return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
14548                              NULL, NULL, NULL);
14549       else
14550         return NULL;
14551     }
14552
14553   switch (TREE_CODE (loc))
14554     {
14555     case ERROR_MARK:
14556       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
14557       return 0;
14558
14559     case PLACEHOLDER_EXPR:
14560       /* This case involves extracting fields from an object to determine the
14561          position of other fields. It is supposed to appear only as the first
14562          operand of COMPONENT_REF nodes and to reference precisely the type
14563          that the context allows.  */
14564       if (context != NULL
14565           && TREE_TYPE (loc) == context->context_type
14566           && want_address >= 1)
14567         {
14568           if (dwarf_version >= 3 || !dwarf_strict)
14569             {
14570               ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
14571               have_address = 1;
14572               break;
14573             }
14574           else
14575             return NULL;
14576         }
14577       else
14578         expansion_failed (loc, NULL_RTX,
14579                           "PLACEHOLDER_EXPR for an unexpected type");
14580       break;
14581
14582     case CALL_EXPR:
14583       expansion_failed (loc, NULL_RTX, "CALL_EXPR");
14584       /* There are no opcodes for these operations.  */
14585       return 0;
14586
14587     case PREINCREMENT_EXPR:
14588     case PREDECREMENT_EXPR:
14589     case POSTINCREMENT_EXPR:
14590     case POSTDECREMENT_EXPR:
14591       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
14592       /* There are no opcodes for these operations.  */
14593       return 0;
14594
14595     case ADDR_EXPR:
14596       /* If we already want an address, see if there is INDIRECT_REF inside
14597          e.g. for &this->field.  */
14598       if (want_address)
14599         {
14600           list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
14601                        (loc, want_address == 2, context);
14602           if (list_ret)
14603             have_address = 1;
14604           else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
14605                    && (ret = cst_pool_loc_descr (loc)))
14606             have_address = 1;
14607         }
14608         /* Otherwise, process the argument and look for the address.  */
14609       if (!list_ret && !ret)
14610         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1, context);
14611       else
14612         {
14613           if (want_address)
14614             expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
14615           return NULL;
14616         }
14617       break;
14618
14619     case VAR_DECL:
14620       if (DECL_THREAD_LOCAL_P (loc))
14621         {
14622           rtx rtl;
14623          enum dwarf_location_atom tls_op;
14624          enum dtprel_bool dtprel = dtprel_false;
14625
14626           if (targetm.have_tls)
14627             {
14628               /* If this is not defined, we have no way to emit the
14629                  data.  */
14630               if (!targetm.asm_out.output_dwarf_dtprel)
14631                 return 0;
14632
14633                /* The way DW_OP_GNU_push_tls_address is specified, we
14634                   can only look up addresses of objects in the current
14635                   module.  We used DW_OP_addr as first op, but that's
14636                   wrong, because DW_OP_addr is relocated by the debug
14637                   info consumer, while DW_OP_GNU_push_tls_address
14638                   operand shouldn't be.  */
14639               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
14640                 return 0;
14641              dtprel = dtprel_true;
14642              tls_op = DW_OP_GNU_push_tls_address;
14643             }
14644           else
14645             {
14646               if (!targetm.emutls.debug_form_tls_address
14647                   || !(dwarf_version >= 3 || !dwarf_strict))
14648                 return 0;
14649               /* We stuffed the control variable into the DECL_VALUE_EXPR
14650                  to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
14651                  no longer appear in gimple code.  We used the control
14652                  variable in specific so that we could pick it up here.  */
14653               loc = DECL_VALUE_EXPR (loc);
14654               tls_op = DW_OP_form_tls_address;
14655             }
14656
14657           rtl = rtl_for_decl_location (loc);
14658           if (rtl == NULL_RTX)
14659             return 0;
14660
14661           if (!MEM_P (rtl))
14662             return 0;
14663           rtl = XEXP (rtl, 0);
14664           if (! CONSTANT_P (rtl))
14665             return 0;
14666
14667           ret = new_addr_loc_descr (rtl, dtprel);
14668           ret1 = new_loc_descr (tls_op, 0, 0);
14669           add_loc_descr (&ret, ret1);
14670
14671           have_address = 1;
14672           break;
14673         }
14674       /* FALLTHRU */
14675
14676     case PARM_DECL:
14677     case RESULT_DECL:
14678       if (DECL_HAS_VALUE_EXPR_P (loc))
14679         return loc_list_from_tree (DECL_VALUE_EXPR (loc),
14680                                    want_address, context);
14681       /* FALLTHRU */
14682
14683     case FUNCTION_DECL:
14684       {
14685         rtx rtl;
14686         var_loc_list *loc_list = lookup_decl_loc (loc);
14687
14688         if (loc_list && loc_list->first)
14689           {
14690             list_ret = dw_loc_list (loc_list, loc, want_address);
14691             have_address = want_address != 0;
14692             break;
14693           }
14694         rtl = rtl_for_decl_location (loc);
14695         if (rtl == NULL_RTX)
14696           {
14697             expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14698             return 0;
14699           }
14700         else if (CONST_INT_P (rtl))
14701           {
14702             HOST_WIDE_INT val = INTVAL (rtl);
14703             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14704               val &= GET_MODE_MASK (DECL_MODE (loc));
14705             ret = int_loc_descriptor (val);
14706           }
14707         else if (GET_CODE (rtl) == CONST_STRING)
14708           {
14709             expansion_failed (loc, NULL_RTX, "CONST_STRING");
14710             return 0;
14711           }
14712         else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14713           ret = new_addr_loc_descr (rtl, dtprel_false);
14714         else
14715           {
14716             machine_mode mode, mem_mode;
14717
14718             /* Certain constructs can only be represented at top-level.  */
14719             if (want_address == 2)
14720               {
14721                 ret = loc_descriptor (rtl, VOIDmode,
14722                                       VAR_INIT_STATUS_INITIALIZED);
14723                 have_address = 1;
14724               }
14725             else
14726               {
14727                 mode = GET_MODE (rtl);
14728                 mem_mode = VOIDmode;
14729                 if (MEM_P (rtl))
14730                   {
14731                     mem_mode = mode;
14732                     mode = get_address_mode (rtl);
14733                     rtl = XEXP (rtl, 0);
14734                     have_address = 1;
14735                   }
14736                 ret = mem_loc_descriptor (rtl, mode, mem_mode,
14737                                           VAR_INIT_STATUS_INITIALIZED);
14738               }
14739             if (!ret)
14740               expansion_failed (loc, rtl,
14741                                 "failed to produce loc descriptor for rtl");
14742           }
14743       }
14744       break;
14745
14746     case MEM_REF:
14747       if (!integer_zerop (TREE_OPERAND (loc, 1)))
14748         {
14749           have_address = 1;
14750           goto do_plus;
14751         }
14752       /* Fallthru.  */
14753     case INDIRECT_REF:
14754       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14755       have_address = 1;
14756       break;
14757
14758     case TARGET_MEM_REF:
14759     case SSA_NAME:
14760     case DEBUG_EXPR_DECL:
14761       return NULL;
14762
14763     case COMPOUND_EXPR:
14764       return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address, context);
14765
14766     CASE_CONVERT:
14767     case VIEW_CONVERT_EXPR:
14768     case SAVE_EXPR:
14769     case MODIFY_EXPR:
14770       return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address, context);
14771
14772     case COMPONENT_REF:
14773     case BIT_FIELD_REF:
14774     case ARRAY_REF:
14775     case ARRAY_RANGE_REF:
14776     case REALPART_EXPR:
14777     case IMAGPART_EXPR:
14778       {
14779         tree obj, offset;
14780         HOST_WIDE_INT bitsize, bitpos, bytepos;
14781         machine_mode mode;
14782         int unsignedp, volatilep = 0;
14783
14784         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14785                                    &unsignedp, &volatilep, false);
14786
14787         gcc_assert (obj != loc);
14788
14789         list_ret = loc_list_from_tree (obj,
14790                                        want_address == 2
14791                                        && !bitpos && !offset ? 2 : 1,
14792                                        context);
14793         /* TODO: We can extract value of the small expression via shifting even
14794            for nonzero bitpos.  */
14795         if (list_ret == 0)
14796           return 0;
14797         if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14798           {
14799             expansion_failed (loc, NULL_RTX,
14800                               "bitfield access");
14801             return 0;
14802           }
14803
14804         if (offset != NULL_TREE)
14805           {
14806             /* Variable offset.  */
14807             list_ret1 = loc_list_from_tree (offset, 0, context);
14808             if (list_ret1 == 0)
14809               return 0;
14810             add_loc_list (&list_ret, list_ret1);
14811             if (!list_ret)
14812               return 0;
14813             add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14814           }
14815
14816         bytepos = bitpos / BITS_PER_UNIT;
14817         if (bytepos > 0)
14818           add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14819         else if (bytepos < 0)
14820           loc_list_plus_const (list_ret, bytepos);
14821
14822         have_address = 1;
14823         break;
14824       }
14825
14826     case INTEGER_CST:
14827       if ((want_address || !tree_fits_shwi_p (loc))
14828           && (ret = cst_pool_loc_descr (loc)))
14829         have_address = 1;
14830       else if (want_address == 2
14831                && tree_fits_shwi_p (loc)
14832                && (ret = address_of_int_loc_descriptor
14833                            (int_size_in_bytes (TREE_TYPE (loc)),
14834                             tree_to_shwi (loc))))
14835         have_address = 1;
14836       else if (tree_fits_shwi_p (loc))
14837         ret = int_loc_descriptor (tree_to_shwi (loc));
14838       else
14839         {
14840           expansion_failed (loc, NULL_RTX,
14841                             "Integer operand is not host integer");
14842           return 0;
14843         }
14844       break;
14845
14846     case CONSTRUCTOR:
14847     case REAL_CST:
14848     case STRING_CST:
14849     case COMPLEX_CST:
14850       if ((ret = cst_pool_loc_descr (loc)))
14851         have_address = 1;
14852       else
14853       /* We can construct small constants here using int_loc_descriptor.  */
14854         expansion_failed (loc, NULL_RTX,
14855                           "constructor or constant not in constant pool");
14856       break;
14857
14858     case TRUTH_AND_EXPR:
14859     case TRUTH_ANDIF_EXPR:
14860     case BIT_AND_EXPR:
14861       op = DW_OP_and;
14862       goto do_binop;
14863
14864     case TRUTH_XOR_EXPR:
14865     case BIT_XOR_EXPR:
14866       op = DW_OP_xor;
14867       goto do_binop;
14868
14869     case TRUTH_OR_EXPR:
14870     case TRUTH_ORIF_EXPR:
14871     case BIT_IOR_EXPR:
14872       op = DW_OP_or;
14873       goto do_binop;
14874
14875     case FLOOR_DIV_EXPR:
14876     case CEIL_DIV_EXPR:
14877     case ROUND_DIV_EXPR:
14878     case TRUNC_DIV_EXPR:
14879       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14880         return 0;
14881       op = DW_OP_div;
14882       goto do_binop;
14883
14884     case MINUS_EXPR:
14885       op = DW_OP_minus;
14886       goto do_binop;
14887
14888     case FLOOR_MOD_EXPR:
14889     case CEIL_MOD_EXPR:
14890     case ROUND_MOD_EXPR:
14891     case TRUNC_MOD_EXPR:
14892       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14893         {
14894           op = DW_OP_mod;
14895           goto do_binop;
14896         }
14897       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14898       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
14899       if (list_ret == 0 || list_ret1 == 0)
14900         return 0;
14901
14902       add_loc_list (&list_ret, list_ret1);
14903       if (list_ret == 0)
14904         return 0;
14905       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14906       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14907       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14908       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14909       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14910       break;
14911
14912     case MULT_EXPR:
14913       op = DW_OP_mul;
14914       goto do_binop;
14915
14916     case LSHIFT_EXPR:
14917       op = DW_OP_shl;
14918       goto do_binop;
14919
14920     case RSHIFT_EXPR:
14921       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14922       goto do_binop;
14923
14924     case POINTER_PLUS_EXPR:
14925     case PLUS_EXPR:
14926     do_plus:
14927       if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
14928         {
14929           list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14930           if (list_ret == 0)
14931             return 0;
14932
14933           loc_list_plus_const (list_ret, tree_to_shwi (TREE_OPERAND (loc, 1)));
14934           break;
14935         }
14936
14937       op = DW_OP_plus;
14938       goto do_binop;
14939
14940     case LE_EXPR:
14941       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14942         return 0;
14943
14944       op = DW_OP_le;
14945       goto do_binop;
14946
14947     case GE_EXPR:
14948       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14949         return 0;
14950
14951       op = DW_OP_ge;
14952       goto do_binop;
14953
14954     case LT_EXPR:
14955       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14956         return 0;
14957
14958       op = DW_OP_lt;
14959       goto do_binop;
14960
14961     case GT_EXPR:
14962       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14963         return 0;
14964
14965       op = DW_OP_gt;
14966       goto do_binop;
14967
14968     case EQ_EXPR:
14969       op = DW_OP_eq;
14970       goto do_binop;
14971
14972     case NE_EXPR:
14973       op = DW_OP_ne;
14974       goto do_binop;
14975
14976     do_binop:
14977       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14978       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
14979       if (list_ret == 0 || list_ret1 == 0)
14980         return 0;
14981
14982       add_loc_list (&list_ret, list_ret1);
14983       if (list_ret == 0)
14984         return 0;
14985       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14986       break;
14987
14988     case TRUTH_NOT_EXPR:
14989     case BIT_NOT_EXPR:
14990       op = DW_OP_not;
14991       goto do_unop;
14992
14993     case ABS_EXPR:
14994       op = DW_OP_abs;
14995       goto do_unop;
14996
14997     case NEGATE_EXPR:
14998       op = DW_OP_neg;
14999       goto do_unop;
15000
15001     do_unop:
15002       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
15003       if (list_ret == 0)
15004         return 0;
15005
15006       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15007       break;
15008
15009     case MIN_EXPR:
15010     case MAX_EXPR:
15011       {
15012         const enum tree_code code =
15013           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
15014
15015         loc = build3 (COND_EXPR, TREE_TYPE (loc),
15016                       build2 (code, integer_type_node,
15017                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
15018                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
15019       }
15020
15021       /* ... fall through ...  */
15022
15023     case COND_EXPR:
15024       {
15025         dw_loc_descr_ref lhs
15026           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
15027         dw_loc_list_ref rhs
15028           = loc_list_from_tree (TREE_OPERAND (loc, 2), 0, context);
15029         dw_loc_descr_ref bra_node, jump_node, tmp;
15030
15031         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
15032         if (list_ret == 0 || lhs == 0 || rhs == 0)
15033           return 0;
15034
15035         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15036         add_loc_descr_to_each (list_ret, bra_node);
15037
15038         add_loc_list (&list_ret, rhs);
15039         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
15040         add_loc_descr_to_each (list_ret, jump_node);
15041
15042         add_loc_descr_to_each (list_ret, lhs);
15043         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15044         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
15045
15046         /* ??? Need a node to point the skip at.  Use a nop.  */
15047         tmp = new_loc_descr (DW_OP_nop, 0, 0);
15048         add_loc_descr_to_each (list_ret, tmp);
15049         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15050         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
15051       }
15052       break;
15053
15054     case FIX_TRUNC_EXPR:
15055       return 0;
15056
15057     default:
15058       /* Leave front-end specific codes as simply unknown.  This comes
15059          up, for instance, with the C STMT_EXPR.  */
15060       if ((unsigned int) TREE_CODE (loc)
15061           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
15062         {
15063           expansion_failed (loc, NULL_RTX,
15064                             "language specific tree node");
15065           return 0;
15066         }
15067
15068 #ifdef ENABLE_CHECKING
15069       /* Otherwise this is a generic code; we should just lists all of
15070          these explicitly.  We forgot one.  */
15071       gcc_unreachable ();
15072 #else
15073       /* In a release build, we want to degrade gracefully: better to
15074          generate incomplete debugging information than to crash.  */
15075       return NULL;
15076 #endif
15077     }
15078
15079   if (!ret && !list_ret)
15080     return 0;
15081
15082   if (want_address == 2 && !have_address
15083       && (dwarf_version >= 4 || !dwarf_strict))
15084     {
15085       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15086         {
15087           expansion_failed (loc, NULL_RTX,
15088                             "DWARF address size mismatch");
15089           return 0;
15090         }
15091       if (ret)
15092         add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
15093       else
15094         add_loc_descr_to_each (list_ret,
15095                                new_loc_descr (DW_OP_stack_value, 0, 0));
15096       have_address = 1;
15097     }
15098   /* Show if we can't fill the request for an address.  */
15099   if (want_address && !have_address)
15100     {
15101       expansion_failed (loc, NULL_RTX,
15102                         "Want address and only have value");
15103       return 0;
15104     }
15105
15106   gcc_assert (!ret || !list_ret);
15107
15108   /* If we've got an address and don't want one, dereference.  */
15109   if (!want_address && have_address)
15110     {
15111       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15112
15113       if (size > DWARF2_ADDR_SIZE || size == -1)
15114         {
15115           expansion_failed (loc, NULL_RTX,
15116                             "DWARF address size mismatch");
15117           return 0;
15118         }
15119       else if (size == DWARF2_ADDR_SIZE)
15120         op = DW_OP_deref;
15121       else
15122         op = DW_OP_deref_size;
15123
15124       if (ret)
15125         add_loc_descr (&ret, new_loc_descr (op, size, 0));
15126       else
15127         add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
15128     }
15129   if (ret)
15130     list_ret = new_loc_list (ret, NULL, NULL, NULL);
15131
15132   return list_ret;
15133 }
15134
15135 /* Same as above but return only single location expression.  */
15136 static dw_loc_descr_ref
15137 loc_descriptor_from_tree (tree loc, int want_address,
15138                           const struct loc_descr_context *context)
15139 {
15140   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
15141   if (!ret)
15142     return NULL;
15143   if (ret->dw_loc_next)
15144     {
15145       expansion_failed (loc, NULL_RTX,
15146                         "Location list where only loc descriptor needed");
15147       return NULL;
15148     }
15149   return ret->expr;
15150 }
15151
15152 /* Given a value, round it up to the lowest multiple of `boundary'
15153    which is not less than the value itself.  */
15154
15155 static inline HOST_WIDE_INT
15156 ceiling (HOST_WIDE_INT value, unsigned int boundary)
15157 {
15158   return (((value + boundary - 1) / boundary) * boundary);
15159 }
15160
15161 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
15162    pointer to the declared type for the relevant field variable, or return
15163    `integer_type_node' if the given node turns out to be an
15164    ERROR_MARK node.  */
15165
15166 static inline tree
15167 field_type (const_tree decl)
15168 {
15169   tree type;
15170
15171   if (TREE_CODE (decl) == ERROR_MARK)
15172     return integer_type_node;
15173
15174   type = DECL_BIT_FIELD_TYPE (decl);
15175   if (type == NULL_TREE)
15176     type = TREE_TYPE (decl);
15177
15178   return type;
15179 }
15180
15181 /* Given a pointer to a tree node, return the alignment in bits for
15182    it, or else return BITS_PER_WORD if the node actually turns out to
15183    be an ERROR_MARK node.  */
15184
15185 static inline unsigned
15186 simple_type_align_in_bits (const_tree type)
15187 {
15188   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
15189 }
15190
15191 static inline unsigned
15192 simple_decl_align_in_bits (const_tree decl)
15193 {
15194   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
15195 }
15196
15197 /* Return the result of rounding T up to ALIGN.  */
15198
15199 static inline offset_int
15200 round_up_to_align (const offset_int &t, unsigned int align)
15201 {
15202   return wi::udiv_trunc (t + align - 1, align) * align;
15203 }
15204
15205 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
15206    lowest addressed byte of the "containing object" for the given FIELD_DECL,
15207    or return 0 if we are unable to determine what that offset is, either
15208    because the argument turns out to be a pointer to an ERROR_MARK node, or
15209    because the offset is actually variable.  (We can't handle the latter case
15210    just yet).  */
15211
15212 static HOST_WIDE_INT
15213 field_byte_offset (const_tree decl)
15214 {
15215   offset_int object_offset_in_bits;
15216   offset_int object_offset_in_bytes;
15217   offset_int bitpos_int;
15218
15219   if (TREE_CODE (decl) == ERROR_MARK)
15220     return 0;
15221
15222   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
15223
15224   /* We cannot yet cope with fields whose positions are variable, so
15225      for now, when we see such things, we simply return 0.  Someday, we may
15226      be able to handle such cases, but it will be damn difficult.  */
15227   if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
15228     return 0;
15229
15230   bitpos_int = wi::to_offset (bit_position (decl));
15231
15232   if (PCC_BITFIELD_TYPE_MATTERS)
15233     {
15234       tree type;
15235       tree field_size_tree;
15236       offset_int deepest_bitpos;
15237       offset_int field_size_in_bits;
15238       unsigned int type_align_in_bits;
15239       unsigned int decl_align_in_bits;
15240       offset_int type_size_in_bits;
15241
15242       type = field_type (decl);
15243       type_size_in_bits = offset_int_type_size_in_bits (type);
15244       type_align_in_bits = simple_type_align_in_bits (type);
15245
15246       field_size_tree = DECL_SIZE (decl);
15247
15248       /* The size could be unspecified if there was an error, or for
15249          a flexible array member.  */
15250       if (!field_size_tree)
15251         field_size_tree = bitsize_zero_node;
15252
15253       /* If the size of the field is not constant, use the type size.  */
15254       if (TREE_CODE (field_size_tree) == INTEGER_CST)
15255         field_size_in_bits = wi::to_offset (field_size_tree);
15256       else
15257         field_size_in_bits = type_size_in_bits;
15258
15259       decl_align_in_bits = simple_decl_align_in_bits (decl);
15260
15261       /* The GCC front-end doesn't make any attempt to keep track of the
15262          starting bit offset (relative to the start of the containing
15263          structure type) of the hypothetical "containing object" for a
15264          bit-field.  Thus, when computing the byte offset value for the
15265          start of the "containing object" of a bit-field, we must deduce
15266          this information on our own. This can be rather tricky to do in
15267          some cases.  For example, handling the following structure type
15268          definition when compiling for an i386/i486 target (which only
15269          aligns long long's to 32-bit boundaries) can be very tricky:
15270
15271          struct S { int field1; long long field2:31; };
15272
15273          Fortunately, there is a simple rule-of-thumb which can be used
15274          in such cases.  When compiling for an i386/i486, GCC will
15275          allocate 8 bytes for the structure shown above.  It decides to
15276          do this based upon one simple rule for bit-field allocation.
15277          GCC allocates each "containing object" for each bit-field at
15278          the first (i.e. lowest addressed) legitimate alignment boundary
15279          (based upon the required minimum alignment for the declared
15280          type of the field) which it can possibly use, subject to the
15281          condition that there is still enough available space remaining
15282          in the containing object (when allocated at the selected point)
15283          to fully accommodate all of the bits of the bit-field itself.
15284
15285          This simple rule makes it obvious why GCC allocates 8 bytes for
15286          each object of the structure type shown above.  When looking
15287          for a place to allocate the "containing object" for `field2',
15288          the compiler simply tries to allocate a 64-bit "containing
15289          object" at each successive 32-bit boundary (starting at zero)
15290          until it finds a place to allocate that 64- bit field such that
15291          at least 31 contiguous (and previously unallocated) bits remain
15292          within that selected 64 bit field.  (As it turns out, for the
15293          example above, the compiler finds it is OK to allocate the
15294          "containing object" 64-bit field at bit-offset zero within the
15295          structure type.)
15296
15297          Here we attempt to work backwards from the limited set of facts
15298          we're given, and we try to deduce from those facts, where GCC
15299          must have believed that the containing object started (within
15300          the structure type). The value we deduce is then used (by the
15301          callers of this routine) to generate DW_AT_location and
15302          DW_AT_bit_offset attributes for fields (both bit-fields and, in
15303          the case of DW_AT_location, regular fields as well).  */
15304
15305       /* Figure out the bit-distance from the start of the structure to
15306          the "deepest" bit of the bit-field.  */
15307       deepest_bitpos = bitpos_int + field_size_in_bits;
15308
15309       /* This is the tricky part.  Use some fancy footwork to deduce
15310          where the lowest addressed bit of the containing object must
15311          be.  */
15312       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15313
15314       /* Round up to type_align by default.  This works best for
15315          bitfields.  */
15316       object_offset_in_bits
15317         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
15318
15319       if (wi::gtu_p (object_offset_in_bits, bitpos_int))
15320         {
15321           object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15322
15323           /* Round up to decl_align instead.  */
15324           object_offset_in_bits
15325             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
15326         }
15327     }
15328   else
15329     object_offset_in_bits = bitpos_int;
15330
15331   object_offset_in_bytes
15332     = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
15333   return object_offset_in_bytes.to_shwi ();
15334 }
15335 \f
15336 /* The following routines define various Dwarf attributes and any data
15337    associated with them.  */
15338
15339 /* Add a location description attribute value to a DIE.
15340
15341    This emits location attributes suitable for whole variables and
15342    whole parameters.  Note that the location attributes for struct fields are
15343    generated by the routine `data_member_location_attribute' below.  */
15344
15345 static inline void
15346 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
15347                              dw_loc_list_ref descr)
15348 {
15349   if (descr == 0)
15350     return;
15351   if (single_element_loc_list_p (descr))
15352     add_AT_loc (die, attr_kind, descr->expr);
15353   else
15354     add_AT_loc_list (die, attr_kind, descr);
15355 }
15356
15357 /* Add DW_AT_accessibility attribute to DIE if needed.  */
15358
15359 static void
15360 add_accessibility_attribute (dw_die_ref die, tree decl)
15361 {
15362   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
15363      children, otherwise the default is DW_ACCESS_public.  In DWARF2
15364      the default has always been DW_ACCESS_public.  */
15365   if (TREE_PROTECTED (decl))
15366     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
15367   else if (TREE_PRIVATE (decl))
15368     {
15369       if (dwarf_version == 2
15370           || die->die_parent == NULL
15371           || die->die_parent->die_tag != DW_TAG_class_type)
15372         add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
15373     }
15374   else if (dwarf_version > 2
15375            && die->die_parent
15376            && die->die_parent->die_tag == DW_TAG_class_type)
15377     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
15378 }
15379
15380 /* Attach the specialized form of location attribute used for data members of
15381    struct and union types.  In the special case of a FIELD_DECL node which
15382    represents a bit-field, the "offset" part of this special location
15383    descriptor must indicate the distance in bytes from the lowest-addressed
15384    byte of the containing struct or union type to the lowest-addressed byte of
15385    the "containing object" for the bit-field.  (See the `field_byte_offset'
15386    function above).
15387
15388    For any given bit-field, the "containing object" is a hypothetical object
15389    (of some integral or enum type) within which the given bit-field lives.  The
15390    type of this hypothetical "containing object" is always the same as the
15391    declared type of the individual bit-field itself (for GCC anyway... the
15392    DWARF spec doesn't actually mandate this).  Note that it is the size (in
15393    bytes) of the hypothetical "containing object" which will be given in the
15394    DW_AT_byte_size attribute for this bit-field.  (See the
15395    `byte_size_attribute' function below.)  It is also used when calculating the
15396    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
15397    function below.)  */
15398
15399 static void
15400 add_data_member_location_attribute (dw_die_ref die, tree decl)
15401 {
15402   HOST_WIDE_INT offset;
15403   dw_loc_descr_ref loc_descr = 0;
15404
15405   if (TREE_CODE (decl) == TREE_BINFO)
15406     {
15407       /* We're working on the TAG_inheritance for a base class.  */
15408       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
15409         {
15410           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
15411              aren't at a fixed offset from all (sub)objects of the same
15412              type.  We need to extract the appropriate offset from our
15413              vtable.  The following dwarf expression means
15414
15415                BaseAddr = ObAddr + *((*ObAddr) - Offset)
15416
15417              This is specific to the V3 ABI, of course.  */
15418
15419           dw_loc_descr_ref tmp;
15420
15421           /* Make a copy of the object address.  */
15422           tmp = new_loc_descr (DW_OP_dup, 0, 0);
15423           add_loc_descr (&loc_descr, tmp);
15424
15425           /* Extract the vtable address.  */
15426           tmp = new_loc_descr (DW_OP_deref, 0, 0);
15427           add_loc_descr (&loc_descr, tmp);
15428
15429           /* Calculate the address of the offset.  */
15430           offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
15431           gcc_assert (offset < 0);
15432
15433           tmp = int_loc_descriptor (-offset);
15434           add_loc_descr (&loc_descr, tmp);
15435           tmp = new_loc_descr (DW_OP_minus, 0, 0);
15436           add_loc_descr (&loc_descr, tmp);
15437
15438           /* Extract the offset.  */
15439           tmp = new_loc_descr (DW_OP_deref, 0, 0);
15440           add_loc_descr (&loc_descr, tmp);
15441
15442           /* Add it to the object address.  */
15443           tmp = new_loc_descr (DW_OP_plus, 0, 0);
15444           add_loc_descr (&loc_descr, tmp);
15445         }
15446       else
15447         offset = tree_to_shwi (BINFO_OFFSET (decl));
15448     }
15449   else
15450     offset = field_byte_offset (decl);
15451
15452   if (! loc_descr)
15453     {
15454       if (dwarf_version > 2)
15455         {
15456           /* Don't need to output a location expression, just the constant. */
15457           if (offset < 0)
15458             add_AT_int (die, DW_AT_data_member_location, offset);
15459           else
15460             add_AT_unsigned (die, DW_AT_data_member_location, offset);
15461           return;
15462         }
15463       else
15464         {
15465           enum dwarf_location_atom op;
15466
15467           /* The DWARF2 standard says that we should assume that the structure
15468              address is already on the stack, so we can specify a structure
15469              field address by using DW_OP_plus_uconst.  */
15470           op = DW_OP_plus_uconst;
15471           loc_descr = new_loc_descr (op, offset, 0);
15472         }
15473     }
15474
15475   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
15476 }
15477
15478 /* Writes integer values to dw_vec_const array.  */
15479
15480 static void
15481 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
15482 {
15483   while (size != 0)
15484     {
15485       *dest++ = val & 0xff;
15486       val >>= 8;
15487       --size;
15488     }
15489 }
15490
15491 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
15492
15493 static HOST_WIDE_INT
15494 extract_int (const unsigned char *src, unsigned int size)
15495 {
15496   HOST_WIDE_INT val = 0;
15497
15498   src += size;
15499   while (size != 0)
15500     {
15501       val <<= 8;
15502       val |= *--src & 0xff;
15503       --size;
15504     }
15505   return val;
15506 }
15507
15508 /* Writes wide_int values to dw_vec_const array.  */
15509
15510 static void
15511 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
15512 {
15513   int i;
15514
15515   if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
15516     {
15517       insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
15518       return;
15519     }
15520
15521   /* We'd have to extend this code to support odd sizes.  */
15522   gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
15523
15524   int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
15525
15526   if (WORDS_BIG_ENDIAN)
15527     for (i = n - 1; i >= 0; i--)
15528       {
15529         insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15530         dest += sizeof (HOST_WIDE_INT);
15531       }
15532   else
15533     for (i = 0; i < n; i++)
15534       {
15535         insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15536         dest += sizeof (HOST_WIDE_INT);
15537       }
15538 }
15539
15540 /* Writes floating point values to dw_vec_const array.  */
15541
15542 static void
15543 insert_float (const_rtx rtl, unsigned char *array)
15544 {
15545   REAL_VALUE_TYPE rv;
15546   long val[4];
15547   int i;
15548
15549   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
15550   real_to_target (val, &rv, GET_MODE (rtl));
15551
15552   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
15553   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
15554     {
15555       insert_int (val[i], 4, array);
15556       array += 4;
15557     }
15558 }
15559
15560 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
15561    does not have a "location" either in memory or in a register.  These
15562    things can arise in GNU C when a constant is passed as an actual parameter
15563    to an inlined function.  They can also arise in C++ where declared
15564    constants do not necessarily get memory "homes".  */
15565
15566 static bool
15567 add_const_value_attribute (dw_die_ref die, rtx rtl)
15568 {
15569   switch (GET_CODE (rtl))
15570     {
15571     case CONST_INT:
15572       {
15573         HOST_WIDE_INT val = INTVAL (rtl);
15574
15575         if (val < 0)
15576           add_AT_int (die, DW_AT_const_value, val);
15577         else
15578           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
15579       }
15580       return true;
15581
15582     case CONST_WIDE_INT:
15583       add_AT_wide (die, DW_AT_const_value,
15584                    std::make_pair (rtl, GET_MODE (rtl)));
15585       return true;
15586
15587     case CONST_DOUBLE:
15588       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
15589          floating-point constant.  A CONST_DOUBLE is used whenever the
15590          constant requires more than one word in order to be adequately
15591          represented.  */
15592       {
15593         machine_mode mode = GET_MODE (rtl);
15594
15595         if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
15596           add_AT_double (die, DW_AT_const_value,
15597                          CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
15598         else
15599           {
15600             unsigned int length = GET_MODE_SIZE (mode);
15601             unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15602
15603             insert_float (rtl, array);
15604             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
15605           }
15606       }
15607       return true;
15608
15609     case CONST_VECTOR:
15610       {
15611         machine_mode mode = GET_MODE (rtl);
15612         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
15613         unsigned int length = CONST_VECTOR_NUNITS (rtl);
15614         unsigned char *array
15615           = ggc_vec_alloc<unsigned char> (length * elt_size);
15616         unsigned int i;
15617         unsigned char *p;
15618         machine_mode imode = GET_MODE_INNER (mode);
15619
15620         switch (GET_MODE_CLASS (mode))
15621           {
15622           case MODE_VECTOR_INT:
15623             for (i = 0, p = array; i < length; i++, p += elt_size)
15624               {
15625                 rtx elt = CONST_VECTOR_ELT (rtl, i);
15626                 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
15627               }
15628             break;
15629
15630           case MODE_VECTOR_FLOAT:
15631             for (i = 0, p = array; i < length; i++, p += elt_size)
15632               {
15633                 rtx elt = CONST_VECTOR_ELT (rtl, i);
15634                 insert_float (elt, p);
15635               }
15636             break;
15637
15638           default:
15639             gcc_unreachable ();
15640           }
15641
15642         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
15643       }
15644       return true;
15645
15646     case CONST_STRING:
15647       if (dwarf_version >= 4 || !dwarf_strict)
15648         {
15649           dw_loc_descr_ref loc_result;
15650           resolve_one_addr (&rtl);
15651         rtl_addr:
15652           loc_result = new_addr_loc_descr (rtl, dtprel_false);
15653           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15654           add_AT_loc (die, DW_AT_location, loc_result);
15655           vec_safe_push (used_rtx_array, rtl);
15656           return true;
15657         }
15658       return false;
15659
15660     case CONST:
15661       if (CONSTANT_P (XEXP (rtl, 0)))
15662         return add_const_value_attribute (die, XEXP (rtl, 0));
15663       /* FALLTHROUGH */
15664     case SYMBOL_REF:
15665       if (!const_ok_for_output (rtl))
15666         return false;
15667     case LABEL_REF:
15668       if (dwarf_version >= 4 || !dwarf_strict)
15669         goto rtl_addr;
15670       return false;
15671
15672     case PLUS:
15673       /* In cases where an inlined instance of an inline function is passed
15674          the address of an `auto' variable (which is local to the caller) we
15675          can get a situation where the DECL_RTL of the artificial local
15676          variable (for the inlining) which acts as a stand-in for the
15677          corresponding formal parameter (of the inline function) will look
15678          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
15679          exactly a compile-time constant expression, but it isn't the address
15680          of the (artificial) local variable either.  Rather, it represents the
15681          *value* which the artificial local variable always has during its
15682          lifetime.  We currently have no way to represent such quasi-constant
15683          values in Dwarf, so for now we just punt and generate nothing.  */
15684       return false;
15685
15686     case HIGH:
15687     case CONST_FIXED:
15688       return false;
15689
15690     case MEM:
15691       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
15692           && MEM_READONLY_P (rtl)
15693           && GET_MODE (rtl) == BLKmode)
15694         {
15695           add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
15696           return true;
15697         }
15698       return false;
15699
15700     default:
15701       /* No other kinds of rtx should be possible here.  */
15702       gcc_unreachable ();
15703     }
15704   return false;
15705 }
15706
15707 /* Determine whether the evaluation of EXPR references any variables
15708    or functions which aren't otherwise used (and therefore may not be
15709    output).  */
15710 static tree
15711 reference_to_unused (tree * tp, int * walk_subtrees,
15712                      void * data ATTRIBUTE_UNUSED)
15713 {
15714   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15715     *walk_subtrees = 0;
15716
15717   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15718       && ! TREE_ASM_WRITTEN (*tp))
15719     return *tp;
15720   /* ???  The C++ FE emits debug information for using decls, so
15721      putting gcc_unreachable here falls over.  See PR31899.  For now
15722      be conservative.  */
15723   else if (!symtab->global_info_ready
15724            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15725     return *tp;
15726   else if (TREE_CODE (*tp) == VAR_DECL)
15727     {
15728       varpool_node *node = varpool_node::get (*tp);
15729       if (!node || !node->definition)
15730         return *tp;
15731     }
15732   else if (TREE_CODE (*tp) == FUNCTION_DECL
15733            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15734     {
15735       /* The call graph machinery must have finished analyzing,
15736          optimizing and gimplifying the CU by now.
15737          So if *TP has no call graph node associated
15738          to it, it means *TP will not be emitted.  */
15739       if (!cgraph_node::get (*tp))
15740         return *tp;
15741     }
15742   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15743     return *tp;
15744
15745   return NULL_TREE;
15746 }
15747
15748 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15749    for use in a later add_const_value_attribute call.  */
15750
15751 static rtx
15752 rtl_for_decl_init (tree init, tree type)
15753 {
15754   rtx rtl = NULL_RTX;
15755
15756   STRIP_NOPS (init);
15757
15758   /* If a variable is initialized with a string constant without embedded
15759      zeros, build CONST_STRING.  */
15760   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15761     {
15762       tree enttype = TREE_TYPE (type);
15763       tree domain = TYPE_DOMAIN (type);
15764       machine_mode mode = TYPE_MODE (enttype);
15765
15766       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15767           && domain
15768           && integer_zerop (TYPE_MIN_VALUE (domain))
15769           && compare_tree_int (TYPE_MAX_VALUE (domain),
15770                                TREE_STRING_LENGTH (init) - 1) == 0
15771           && ((size_t) TREE_STRING_LENGTH (init)
15772               == strlen (TREE_STRING_POINTER (init)) + 1))
15773         {
15774           rtl = gen_rtx_CONST_STRING (VOIDmode,
15775                                       ggc_strdup (TREE_STRING_POINTER (init)));
15776           rtl = gen_rtx_MEM (BLKmode, rtl);
15777           MEM_READONLY_P (rtl) = 1;
15778         }
15779     }
15780   /* Other aggregates, and complex values, could be represented using
15781      CONCAT: FIXME!  */
15782   else if (AGGREGATE_TYPE_P (type)
15783            || (TREE_CODE (init) == VIEW_CONVERT_EXPR
15784                && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
15785            || TREE_CODE (type) == COMPLEX_TYPE)
15786     ;
15787   /* Vectors only work if their mode is supported by the target.
15788      FIXME: generic vectors ought to work too.  */
15789   else if (TREE_CODE (type) == VECTOR_TYPE
15790            && !VECTOR_MODE_P (TYPE_MODE (type)))
15791     ;
15792   /* If the initializer is something that we know will expand into an
15793      immediate RTL constant, expand it now.  We must be careful not to
15794      reference variables which won't be output.  */
15795   else if (initializer_constant_valid_p (init, type)
15796            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15797     {
15798       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15799          possible.  */
15800       if (TREE_CODE (type) == VECTOR_TYPE)
15801         switch (TREE_CODE (init))
15802           {
15803           case VECTOR_CST:
15804             break;
15805           case CONSTRUCTOR:
15806             if (TREE_CONSTANT (init))
15807               {
15808                 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15809                 bool constant_p = true;
15810                 tree value;
15811                 unsigned HOST_WIDE_INT ix;
15812
15813                 /* Even when ctor is constant, it might contain non-*_CST
15814                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15815                    belong into VECTOR_CST nodes.  */
15816                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15817                   if (!CONSTANT_CLASS_P (value))
15818                     {
15819                       constant_p = false;
15820                       break;
15821                     }
15822
15823                 if (constant_p)
15824                   {
15825                     init = build_vector_from_ctor (type, elts);
15826                     break;
15827                   }
15828               }
15829             /* FALLTHRU */
15830
15831           default:
15832             return NULL;
15833           }
15834
15835       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15836
15837       /* If expand_expr returns a MEM, it wasn't immediate.  */
15838       gcc_assert (!rtl || !MEM_P (rtl));
15839     }
15840
15841   return rtl;
15842 }
15843
15844 /* Generate RTL for the variable DECL to represent its location.  */
15845
15846 static rtx
15847 rtl_for_decl_location (tree decl)
15848 {
15849   rtx rtl;
15850
15851   /* Here we have to decide where we are going to say the parameter "lives"
15852      (as far as the debugger is concerned).  We only have a couple of
15853      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15854
15855      DECL_RTL normally indicates where the parameter lives during most of the
15856      activation of the function.  If optimization is enabled however, this
15857      could be either NULL or else a pseudo-reg.  Both of those cases indicate
15858      that the parameter doesn't really live anywhere (as far as the code
15859      generation parts of GCC are concerned) during most of the function's
15860      activation.  That will happen (for example) if the parameter is never
15861      referenced within the function.
15862
15863      We could just generate a location descriptor here for all non-NULL
15864      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15865      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15866      where DECL_RTL is NULL or is a pseudo-reg.
15867
15868      Note however that we can only get away with using DECL_INCOMING_RTL as
15869      a backup substitute for DECL_RTL in certain limited cases.  In cases
15870      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15871      we can be sure that the parameter was passed using the same type as it is
15872      declared to have within the function, and that its DECL_INCOMING_RTL
15873      points us to a place where a value of that type is passed.
15874
15875      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15876      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15877      because in these cases DECL_INCOMING_RTL points us to a value of some
15878      type which is *different* from the type of the parameter itself.  Thus,
15879      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15880      such cases, the debugger would end up (for example) trying to fetch a
15881      `float' from a place which actually contains the first part of a
15882      `double'.  That would lead to really incorrect and confusing
15883      output at debug-time.
15884
15885      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15886      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
15887      are a couple of exceptions however.  On little-endian machines we can
15888      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15889      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15890      an integral type that is smaller than TREE_TYPE (decl). These cases arise
15891      when (on a little-endian machine) a non-prototyped function has a
15892      parameter declared to be of type `short' or `char'.  In such cases,
15893      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15894      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15895      passed `int' value.  If the debugger then uses that address to fetch
15896      a `short' or a `char' (on a little-endian machine) the result will be
15897      the correct data, so we allow for such exceptional cases below.
15898
15899      Note that our goal here is to describe the place where the given formal
15900      parameter lives during most of the function's activation (i.e. between the
15901      end of the prologue and the start of the epilogue).  We'll do that as best
15902      as we can. Note however that if the given formal parameter is modified
15903      sometime during the execution of the function, then a stack backtrace (at
15904      debug-time) will show the function as having been called with the *new*
15905      value rather than the value which was originally passed in.  This happens
15906      rarely enough that it is not a major problem, but it *is* a problem, and
15907      I'd like to fix it.
15908
15909      A future version of dwarf2out.c may generate two additional attributes for
15910      any given DW_TAG_formal_parameter DIE which will describe the "passed
15911      type" and the "passed location" for the given formal parameter in addition
15912      to the attributes we now generate to indicate the "declared type" and the
15913      "active location" for each parameter.  This additional set of attributes
15914      could be used by debuggers for stack backtraces. Separately, note that
15915      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15916      This happens (for example) for inlined-instances of inline function formal
15917      parameters which are never referenced.  This really shouldn't be
15918      happening.  All PARM_DECL nodes should get valid non-NULL
15919      DECL_INCOMING_RTL values.  FIXME.  */
15920
15921   /* Use DECL_RTL as the "location" unless we find something better.  */
15922   rtl = DECL_RTL_IF_SET (decl);
15923
15924   /* When generating abstract instances, ignore everything except
15925      constants, symbols living in memory, and symbols living in
15926      fixed registers.  */
15927   if (! reload_completed)
15928     {
15929       if (rtl
15930           && (CONSTANT_P (rtl)
15931               || (MEM_P (rtl)
15932                   && CONSTANT_P (XEXP (rtl, 0)))
15933               || (REG_P (rtl)
15934                   && TREE_CODE (decl) == VAR_DECL
15935                   && TREE_STATIC (decl))))
15936         {
15937           rtl = targetm.delegitimize_address (rtl);
15938           return rtl;
15939         }
15940       rtl = NULL_RTX;
15941     }
15942   else if (TREE_CODE (decl) == PARM_DECL)
15943     {
15944       if (rtl == NULL_RTX
15945           || is_pseudo_reg (rtl)
15946           || (MEM_P (rtl)
15947               && is_pseudo_reg (XEXP (rtl, 0))
15948               && DECL_INCOMING_RTL (decl)
15949               && MEM_P (DECL_INCOMING_RTL (decl))
15950               && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15951         {
15952           tree declared_type = TREE_TYPE (decl);
15953           tree passed_type = DECL_ARG_TYPE (decl);
15954           machine_mode dmode = TYPE_MODE (declared_type);
15955           machine_mode pmode = TYPE_MODE (passed_type);
15956
15957           /* This decl represents a formal parameter which was optimized out.
15958              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15959              all cases where (rtl == NULL_RTX) just below.  */
15960           if (dmode == pmode)
15961             rtl = DECL_INCOMING_RTL (decl);
15962           else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15963                    && SCALAR_INT_MODE_P (dmode)
15964                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15965                    && DECL_INCOMING_RTL (decl))
15966             {
15967               rtx inc = DECL_INCOMING_RTL (decl);
15968               if (REG_P (inc))
15969                 rtl = inc;
15970               else if (MEM_P (inc))
15971                 {
15972                   if (BYTES_BIG_ENDIAN)
15973                     rtl = adjust_address_nv (inc, dmode,
15974                                              GET_MODE_SIZE (pmode)
15975                                              - GET_MODE_SIZE (dmode));
15976                   else
15977                     rtl = inc;
15978                 }
15979             }
15980         }
15981
15982       /* If the parm was passed in registers, but lives on the stack, then
15983          make a big endian correction if the mode of the type of the
15984          parameter is not the same as the mode of the rtl.  */
15985       /* ??? This is the same series of checks that are made in dbxout.c before
15986          we reach the big endian correction code there.  It isn't clear if all
15987          of these checks are necessary here, but keeping them all is the safe
15988          thing to do.  */
15989       else if (MEM_P (rtl)
15990                && XEXP (rtl, 0) != const0_rtx
15991                && ! CONSTANT_P (XEXP (rtl, 0))
15992                /* Not passed in memory.  */
15993                && !MEM_P (DECL_INCOMING_RTL (decl))
15994                /* Not passed by invisible reference.  */
15995                && (!REG_P (XEXP (rtl, 0))
15996                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15997                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15998 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15999                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
16000 #endif
16001                      )
16002                /* Big endian correction check.  */
16003                && BYTES_BIG_ENDIAN
16004                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
16005                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
16006                    < UNITS_PER_WORD))
16007         {
16008           machine_mode addr_mode = get_address_mode (rtl);
16009           int offset = (UNITS_PER_WORD
16010                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
16011
16012           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16013                              plus_constant (addr_mode, XEXP (rtl, 0), offset));
16014         }
16015     }
16016   else if (TREE_CODE (decl) == VAR_DECL
16017            && rtl
16018            && MEM_P (rtl)
16019            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
16020            && BYTES_BIG_ENDIAN)
16021     {
16022       machine_mode addr_mode = get_address_mode (rtl);
16023       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
16024       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
16025
16026       /* If a variable is declared "register" yet is smaller than
16027          a register, then if we store the variable to memory, it
16028          looks like we're storing a register-sized value, when in
16029          fact we are not.  We need to adjust the offset of the
16030          storage location to reflect the actual value's bytes,
16031          else gdb will not be able to display it.  */
16032       if (rsize > dsize)
16033         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16034                            plus_constant (addr_mode, XEXP (rtl, 0),
16035                                           rsize - dsize));
16036     }
16037
16038   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
16039      and will have been substituted directly into all expressions that use it.
16040      C does not have such a concept, but C++ and other languages do.  */
16041   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
16042     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
16043
16044   if (rtl)
16045     rtl = targetm.delegitimize_address (rtl);
16046
16047   /* If we don't look past the constant pool, we risk emitting a
16048      reference to a constant pool entry that isn't referenced from
16049      code, and thus is not emitted.  */
16050   if (rtl)
16051     rtl = avoid_constant_pool_reference (rtl);
16052
16053   /* Try harder to get a rtl.  If this symbol ends up not being emitted
16054      in the current CU, resolve_addr will remove the expression referencing
16055      it.  */
16056   if (rtl == NULL_RTX
16057       && TREE_CODE (decl) == VAR_DECL
16058       && !DECL_EXTERNAL (decl)
16059       && TREE_STATIC (decl)
16060       && DECL_NAME (decl)
16061       && !DECL_HARD_REGISTER (decl)
16062       && DECL_MODE (decl) != VOIDmode)
16063     {
16064       rtl = make_decl_rtl_for_debug (decl);
16065       if (!MEM_P (rtl)
16066           || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
16067           || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
16068         rtl = NULL_RTX;
16069     }
16070
16071   return rtl;
16072 }
16073
16074 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
16075    returned.  If so, the decl for the COMMON block is returned, and the
16076    value is the offset into the common block for the symbol.  */
16077
16078 static tree
16079 fortran_common (tree decl, HOST_WIDE_INT *value)
16080 {
16081   tree val_expr, cvar;
16082   machine_mode mode;
16083   HOST_WIDE_INT bitsize, bitpos;
16084   tree offset;
16085   int unsignedp, volatilep = 0;
16086
16087   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
16088      it does not have a value (the offset into the common area), or if it
16089      is thread local (as opposed to global) then it isn't common, and shouldn't
16090      be handled as such.  */
16091   if (TREE_CODE (decl) != VAR_DECL
16092       || !TREE_STATIC (decl)
16093       || !DECL_HAS_VALUE_EXPR_P (decl)
16094       || !is_fortran ())
16095     return NULL_TREE;
16096
16097   val_expr = DECL_VALUE_EXPR (decl);
16098   if (TREE_CODE (val_expr) != COMPONENT_REF)
16099     return NULL_TREE;
16100
16101   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
16102                               &mode, &unsignedp, &volatilep, true);
16103
16104   if (cvar == NULL_TREE
16105       || TREE_CODE (cvar) != VAR_DECL
16106       || DECL_ARTIFICIAL (cvar)
16107       || !TREE_PUBLIC (cvar))
16108     return NULL_TREE;
16109
16110   *value = 0;
16111   if (offset != NULL)
16112     {
16113       if (!tree_fits_shwi_p (offset))
16114         return NULL_TREE;
16115       *value = tree_to_shwi (offset);
16116     }
16117   if (bitpos != 0)
16118     *value += bitpos / BITS_PER_UNIT;
16119
16120   return cvar;
16121 }
16122
16123 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
16124    data attribute for a variable or a parameter.  We generate the
16125    DW_AT_const_value attribute only in those cases where the given variable
16126    or parameter does not have a true "location" either in memory or in a
16127    register.  This can happen (for example) when a constant is passed as an
16128    actual argument in a call to an inline function.  (It's possible that
16129    these things can crop up in other ways also.)  Note that one type of
16130    constant value which can be passed into an inlined function is a constant
16131    pointer.  This can happen for example if an actual argument in an inlined
16132    function call evaluates to a compile-time constant address.
16133
16134    CACHE_P is true if it is worth caching the location list for DECL,
16135    so that future calls can reuse it rather than regenerate it from scratch.
16136    This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
16137    since we will need to refer to them each time the function is inlined.  */
16138
16139 static bool
16140 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
16141                                        enum dwarf_attribute attr)
16142 {
16143   rtx rtl;
16144   dw_loc_list_ref list;
16145   var_loc_list *loc_list;
16146   cached_dw_loc_list *cache;
16147
16148   if (early_dwarf)
16149     return false;
16150
16151   if (TREE_CODE (decl) == ERROR_MARK)
16152     return false;
16153
16154   if (get_AT (die, attr))
16155     return true;
16156
16157   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
16158               || TREE_CODE (decl) == RESULT_DECL);
16159
16160   /* Try to get some constant RTL for this decl, and use that as the value of
16161      the location.  */
16162
16163   rtl = rtl_for_decl_location (decl);
16164   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16165       && add_const_value_attribute (die, rtl))
16166     return true;
16167
16168   /* See if we have single element location list that is equivalent to
16169      a constant value.  That way we are better to use add_const_value_attribute
16170      rather than expanding constant value equivalent.  */
16171   loc_list = lookup_decl_loc (decl);
16172   if (loc_list
16173       && loc_list->first
16174       && loc_list->first->next == NULL
16175       && NOTE_P (loc_list->first->loc)
16176       && NOTE_VAR_LOCATION (loc_list->first->loc)
16177       && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
16178     {
16179       struct var_loc_node *node;
16180
16181       node = loc_list->first;
16182       rtl = NOTE_VAR_LOCATION_LOC (node->loc);
16183       if (GET_CODE (rtl) == EXPR_LIST)
16184         rtl = XEXP (rtl, 0);
16185       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16186           && add_const_value_attribute (die, rtl))
16187          return true;
16188     }
16189   /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
16190      list several times.  See if we've already cached the contents.  */
16191   list = NULL;
16192   if (loc_list == NULL || cached_dw_loc_list_table == NULL)
16193     cache_p = false;
16194   if (cache_p)
16195     {
16196       cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
16197       if (cache)
16198         list = cache->loc_list;
16199     }
16200   if (list == NULL)
16201     {
16202       list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
16203                                  NULL);
16204       /* It is usually worth caching this result if the decl is from
16205          BLOCK_NONLOCALIZED_VARS and if the list has at least two elements.  */
16206       if (cache_p && list && list->dw_loc_next)
16207         {
16208           cached_dw_loc_list **slot
16209             = cached_dw_loc_list_table->find_slot_with_hash (decl,
16210                                                              DECL_UID (decl),
16211                                                              INSERT);
16212           cache = ggc_cleared_alloc<cached_dw_loc_list> ();
16213           cache->decl_id = DECL_UID (decl);
16214           cache->loc_list = list;
16215           *slot = cache;
16216         }
16217     }
16218   if (list)
16219     {
16220       add_AT_location_description (die, attr, list);
16221       return true;
16222     }
16223   /* None of that worked, so it must not really have a location;
16224      try adding a constant value attribute from the DECL_INITIAL.  */
16225   return tree_add_const_value_attribute_for_decl (die, decl);
16226 }
16227
16228 /* Helper function for tree_add_const_value_attribute.  Natively encode
16229    initializer INIT into an array.  Return true if successful.  */
16230
16231 static bool
16232 native_encode_initializer (tree init, unsigned char *array, int size)
16233 {
16234   tree type;
16235
16236   if (init == NULL_TREE)
16237     return false;
16238
16239   STRIP_NOPS (init);
16240   switch (TREE_CODE (init))
16241     {
16242     case STRING_CST:
16243       type = TREE_TYPE (init);
16244       if (TREE_CODE (type) == ARRAY_TYPE)
16245         {
16246           tree enttype = TREE_TYPE (type);
16247           machine_mode mode = TYPE_MODE (enttype);
16248
16249           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
16250             return false;
16251           if (int_size_in_bytes (type) != size)
16252             return false;
16253           if (size > TREE_STRING_LENGTH (init))
16254             {
16255               memcpy (array, TREE_STRING_POINTER (init),
16256                       TREE_STRING_LENGTH (init));
16257               memset (array + TREE_STRING_LENGTH (init),
16258                       '\0', size - TREE_STRING_LENGTH (init));
16259             }
16260           else
16261             memcpy (array, TREE_STRING_POINTER (init), size);
16262           return true;
16263         }
16264       return false;
16265     case CONSTRUCTOR:
16266       type = TREE_TYPE (init);
16267       if (int_size_in_bytes (type) != size)
16268         return false;
16269       if (TREE_CODE (type) == ARRAY_TYPE)
16270         {
16271           HOST_WIDE_INT min_index;
16272           unsigned HOST_WIDE_INT cnt;
16273           int curpos = 0, fieldsize;
16274           constructor_elt *ce;
16275
16276           if (TYPE_DOMAIN (type) == NULL_TREE
16277               || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
16278             return false;
16279
16280           fieldsize = int_size_in_bytes (TREE_TYPE (type));
16281           if (fieldsize <= 0)
16282             return false;
16283
16284           min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
16285           memset (array, '\0', size);
16286           FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16287             {
16288               tree val = ce->value;
16289               tree index = ce->index;
16290               int pos = curpos;
16291               if (index && TREE_CODE (index) == RANGE_EXPR)
16292                 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
16293                       * fieldsize;
16294               else if (index)
16295                 pos = (tree_to_shwi (index) - min_index) * fieldsize;
16296
16297               if (val)
16298                 {
16299                   STRIP_NOPS (val);
16300                   if (!native_encode_initializer (val, array + pos, fieldsize))
16301                     return false;
16302                 }
16303               curpos = pos + fieldsize;
16304               if (index && TREE_CODE (index) == RANGE_EXPR)
16305                 {
16306                   int count = tree_to_shwi (TREE_OPERAND (index, 1))
16307                               - tree_to_shwi (TREE_OPERAND (index, 0));
16308                   while (count-- > 0)
16309                     {
16310                       if (val)
16311                         memcpy (array + curpos, array + pos, fieldsize);
16312                       curpos += fieldsize;
16313                     }
16314                 }
16315               gcc_assert (curpos <= size);
16316             }
16317           return true;
16318         }
16319       else if (TREE_CODE (type) == RECORD_TYPE
16320                || TREE_CODE (type) == UNION_TYPE)
16321         {
16322           tree field = NULL_TREE;
16323           unsigned HOST_WIDE_INT cnt;
16324           constructor_elt *ce;
16325
16326           if (int_size_in_bytes (type) != size)
16327             return false;
16328
16329           if (TREE_CODE (type) == RECORD_TYPE)
16330             field = TYPE_FIELDS (type);
16331
16332           FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16333             {
16334               tree val = ce->value;
16335               int pos, fieldsize;
16336
16337               if (ce->index != 0)
16338                 field = ce->index;
16339
16340               if (val)
16341                 STRIP_NOPS (val);
16342
16343               if (field == NULL_TREE || DECL_BIT_FIELD (field))
16344                 return false;
16345
16346               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
16347                   && TYPE_DOMAIN (TREE_TYPE (field))
16348                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
16349                 return false;
16350               else if (DECL_SIZE_UNIT (field) == NULL_TREE
16351                        || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
16352                 return false;
16353               fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
16354               pos = int_byte_position (field);
16355               gcc_assert (pos + fieldsize <= size);
16356               if (val
16357                   && !native_encode_initializer (val, array + pos, fieldsize))
16358                 return false;
16359             }
16360           return true;
16361         }
16362       return false;
16363     case VIEW_CONVERT_EXPR:
16364     case NON_LVALUE_EXPR:
16365       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
16366     default:
16367       return native_encode_expr (init, array, size) == size;
16368     }
16369 }
16370
16371 /* Attach a DW_AT_const_value attribute to DIE. The value of the
16372    attribute is the const value T.  */
16373
16374 static bool
16375 tree_add_const_value_attribute (dw_die_ref die, tree t)
16376 {
16377   tree init;
16378   tree type = TREE_TYPE (t);
16379   rtx rtl;
16380
16381   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
16382     return false;
16383
16384   init = t;
16385   gcc_assert (!DECL_P (init));
16386
16387   rtl = rtl_for_decl_init (init, type);
16388   if (rtl)
16389     return add_const_value_attribute (die, rtl);
16390   /* If the host and target are sane, try harder.  */
16391   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
16392            && initializer_constant_valid_p (init, type))
16393     {
16394       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
16395       if (size > 0 && (int) size == size)
16396         {
16397           unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
16398
16399           if (native_encode_initializer (init, array, size))
16400             {
16401               add_AT_vec (die, DW_AT_const_value, size, 1, array);
16402               return true;
16403             }
16404           ggc_free (array);
16405         }
16406     }
16407   return false;
16408 }
16409
16410 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
16411    attribute is the const value of T, where T is an integral constant
16412    variable with static storage duration
16413    (so it can't be a PARM_DECL or a RESULT_DECL).  */
16414
16415 static bool
16416 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
16417 {
16418
16419   if (!decl
16420       || (TREE_CODE (decl) != VAR_DECL
16421           && TREE_CODE (decl) != CONST_DECL)
16422       || (TREE_CODE (decl) == VAR_DECL
16423           && !TREE_STATIC (decl)))
16424     return false;
16425
16426   if (TREE_READONLY (decl)
16427       && ! TREE_THIS_VOLATILE (decl)
16428       && DECL_INITIAL (decl))
16429     /* OK */;
16430   else
16431     return false;
16432
16433   /* Don't add DW_AT_const_value if abstract origin already has one.  */
16434   if (get_AT (var_die, DW_AT_const_value))
16435     return false;
16436
16437   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
16438 }
16439
16440 /* Convert the CFI instructions for the current function into a
16441    location list.  This is used for DW_AT_frame_base when we targeting
16442    a dwarf2 consumer that does not support the dwarf3
16443    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
16444    expressions.  */
16445
16446 static dw_loc_list_ref
16447 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
16448 {
16449   int ix;
16450   dw_fde_ref fde;
16451   dw_loc_list_ref list, *list_tail;
16452   dw_cfi_ref cfi;
16453   dw_cfa_location last_cfa, next_cfa;
16454   const char *start_label, *last_label, *section;
16455   dw_cfa_location remember;
16456
16457   fde = cfun->fde;
16458   gcc_assert (fde != NULL);
16459
16460   section = secname_for_decl (current_function_decl);
16461   list_tail = &list;
16462   list = NULL;
16463
16464   memset (&next_cfa, 0, sizeof (next_cfa));
16465   next_cfa.reg = INVALID_REGNUM;
16466   remember = next_cfa;
16467
16468   start_label = fde->dw_fde_begin;
16469
16470   /* ??? Bald assumption that the CIE opcode list does not contain
16471      advance opcodes.  */
16472   FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
16473     lookup_cfa_1 (cfi, &next_cfa, &remember);
16474
16475   last_cfa = next_cfa;
16476   last_label = start_label;
16477
16478   if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
16479     {
16480       /* If the first partition contained no CFI adjustments, the
16481          CIE opcodes apply to the whole first partition.  */
16482       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16483                                  fde->dw_fde_begin, fde->dw_fde_end, section);
16484       list_tail =&(*list_tail)->dw_loc_next;
16485       start_label = last_label = fde->dw_fde_second_begin;
16486     }
16487
16488   FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
16489     {
16490       switch (cfi->dw_cfi_opc)
16491         {
16492         case DW_CFA_set_loc:
16493         case DW_CFA_advance_loc1:
16494         case DW_CFA_advance_loc2:
16495         case DW_CFA_advance_loc4:
16496           if (!cfa_equal_p (&last_cfa, &next_cfa))
16497             {
16498               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16499                                          start_label, last_label, section);
16500
16501               list_tail = &(*list_tail)->dw_loc_next;
16502               last_cfa = next_cfa;
16503               start_label = last_label;
16504             }
16505           last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
16506           break;
16507
16508         case DW_CFA_advance_loc:
16509           /* The encoding is complex enough that we should never emit this.  */
16510           gcc_unreachable ();
16511
16512         default:
16513           lookup_cfa_1 (cfi, &next_cfa, &remember);
16514           break;
16515         }
16516       if (ix + 1 == fde->dw_fde_switch_cfi_index)
16517         {
16518           if (!cfa_equal_p (&last_cfa, &next_cfa))
16519             {
16520               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16521                                          start_label, last_label, section);
16522
16523               list_tail = &(*list_tail)->dw_loc_next;
16524               last_cfa = next_cfa;
16525               start_label = last_label;
16526             }
16527           *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16528                                      start_label, fde->dw_fde_end, section);
16529           list_tail = &(*list_tail)->dw_loc_next;
16530           start_label = last_label = fde->dw_fde_second_begin;
16531         }
16532     }
16533
16534   if (!cfa_equal_p (&last_cfa, &next_cfa))
16535     {
16536       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16537                                  start_label, last_label, section);
16538       list_tail = &(*list_tail)->dw_loc_next;
16539       start_label = last_label;
16540     }
16541
16542   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
16543                              start_label,
16544                              fde->dw_fde_second_begin
16545                              ? fde->dw_fde_second_end : fde->dw_fde_end,
16546                              section);
16547
16548   if (list && list->dw_loc_next)
16549     gen_llsym (list);
16550
16551   return list;
16552 }
16553
16554 /* Compute a displacement from the "steady-state frame pointer" to the
16555    frame base (often the same as the CFA), and store it in
16556    frame_pointer_fb_offset.  OFFSET is added to the displacement
16557    before the latter is negated.  */
16558
16559 static void
16560 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
16561 {
16562   rtx reg, elim;
16563
16564 #ifdef FRAME_POINTER_CFA_OFFSET
16565   reg = frame_pointer_rtx;
16566   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
16567 #else
16568   reg = arg_pointer_rtx;
16569   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
16570 #endif
16571
16572   elim = (ira_use_lra_p
16573           ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
16574           : eliminate_regs (reg, VOIDmode, NULL_RTX));
16575   if (GET_CODE (elim) == PLUS)
16576     {
16577       offset += INTVAL (XEXP (elim, 1));
16578       elim = XEXP (elim, 0);
16579     }
16580
16581   frame_pointer_fb_offset = -offset;
16582
16583   /* ??? AVR doesn't set up valid eliminations when there is no stack frame
16584      in which to eliminate.  This is because it's stack pointer isn't 
16585      directly accessible as a register within the ISA.  To work around
16586      this, assume that while we cannot provide a proper value for
16587      frame_pointer_fb_offset, we won't need one either.  */
16588   frame_pointer_fb_offset_valid
16589     = ((SUPPORTS_STACK_ALIGNMENT
16590         && (elim == hard_frame_pointer_rtx
16591             || elim == stack_pointer_rtx))
16592        || elim == (frame_pointer_needed
16593                    ? hard_frame_pointer_rtx
16594                    : stack_pointer_rtx));
16595 }
16596
16597 /* Generate a DW_AT_name attribute given some string value to be included as
16598    the value of the attribute.  */
16599
16600 static void
16601 add_name_attribute (dw_die_ref die, const char *name_string)
16602 {
16603   if (name_string != NULL && *name_string != 0)
16604     {
16605       if (demangle_name_func)
16606         name_string = (*demangle_name_func) (name_string);
16607
16608       add_AT_string (die, DW_AT_name, name_string);
16609     }
16610 }
16611
16612 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
16613    DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
16614    of TYPE accordingly.
16615
16616    ??? This is a temporary measure until after we're able to generate
16617    regular DWARF for the complex Ada type system.  */
16618
16619 static void 
16620 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
16621                                      dw_die_ref context_die)
16622 {
16623   tree dtype;
16624   dw_die_ref dtype_die;
16625
16626   if (!lang_hooks.types.descriptive_type)
16627     return;
16628
16629   dtype = lang_hooks.types.descriptive_type (type);
16630   if (!dtype)
16631     return;
16632
16633   dtype_die = lookup_type_die (dtype);
16634   if (!dtype_die)
16635     {
16636       gen_type_die (dtype, context_die);
16637       dtype_die = lookup_type_die (dtype);
16638       gcc_assert (dtype_die);
16639     }
16640
16641   add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
16642 }
16643
16644 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir.  */
16645
16646 static const char *
16647 comp_dir_string (void)
16648 {
16649   const char *wd;
16650   char *wd1;
16651   static const char *cached_wd = NULL;
16652
16653   if (cached_wd != NULL)
16654     return cached_wd;
16655
16656   wd = get_src_pwd ();
16657   if (wd == NULL)
16658     return NULL;
16659
16660   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16661     {
16662       int wdlen;
16663
16664       wdlen = strlen (wd);
16665       wd1 = ggc_vec_alloc<char> (wdlen + 2);
16666       strcpy (wd1, wd);
16667       wd1 [wdlen] = DIR_SEPARATOR;
16668       wd1 [wdlen + 1] = 0;
16669       wd = wd1;
16670     }
16671
16672   cached_wd = remap_debug_filename (wd);
16673   return cached_wd;
16674 }
16675
16676 /* Generate a DW_AT_comp_dir attribute for DIE.  */
16677
16678 static void
16679 add_comp_dir_attribute (dw_die_ref die)
16680 {
16681   const char * wd = comp_dir_string ();
16682   if (wd != NULL)
16683     add_AT_string (die, DW_AT_comp_dir, wd);
16684 }
16685
16686 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
16687    pointer computation, ...), output a representation for that bound according
16688    to the accepted FORMS (see enum dw_scalar_form) and add it to DIE.  See
16689    loc_list_from_tree for the meaning of CONTEXT.  */
16690
16691 static void
16692 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
16693                  int forms, const struct loc_descr_context *context)
16694 {
16695   dw_die_ref ctx, decl_die;
16696   dw_loc_list_ref list;
16697
16698   bool strip_conversions = true;
16699
16700   while (strip_conversions)
16701     switch (TREE_CODE (value))
16702       {
16703       case ERROR_MARK:
16704       case SAVE_EXPR:
16705         return;
16706
16707       CASE_CONVERT:
16708       case VIEW_CONVERT_EXPR:
16709         value = TREE_OPERAND (value, 0);
16710         break;
16711
16712       default:
16713         strip_conversions = false;
16714         break;
16715       }
16716
16717   /* If possible and permitted, output the attribute as a constant.  */
16718   if ((forms & dw_scalar_form_constant) != 0
16719       && TREE_CODE (value) == INTEGER_CST)
16720     {
16721       unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
16722
16723       /* If HOST_WIDE_INT is big enough then represent the bound as
16724          a constant value.  We need to choose a form based on
16725          whether the type is signed or unsigned.  We cannot just
16726          call add_AT_unsigned if the value itself is positive
16727          (add_AT_unsigned might add the unsigned value encoded as
16728          DW_FORM_data[1248]).  Some DWARF consumers will lookup the
16729          bounds type and then sign extend any unsigned values found
16730          for signed types.  This is needed only for
16731          DW_AT_{lower,upper}_bound, since for most other attributes,
16732          consumers will treat DW_FORM_data[1248] as unsigned values,
16733          regardless of the underlying type.  */
16734       if (prec <= HOST_BITS_PER_WIDE_INT
16735           || tree_fits_uhwi_p (value))
16736         {
16737           if (TYPE_UNSIGNED (TREE_TYPE (value)))
16738             add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
16739           else
16740             add_AT_int (die, attr, TREE_INT_CST_LOW (value));
16741         }
16742       else
16743         /* Otherwise represent the bound as an unsigned value with
16744            the precision of its type.  The precision and signedness
16745            of the type will be necessary to re-interpret it
16746            unambiguously.  */
16747         add_AT_wide (die, attr, value);
16748       return;
16749     }
16750
16751   /* Otherwise, if it's possible and permitted too, output a reference to
16752      another DIE.  */
16753   if ((forms & dw_scalar_form_reference) != 0)
16754     {
16755       tree decl = NULL_TREE;
16756
16757       /* Some type attributes reference an outer type.  For instance, the upper
16758          bound of an array may reference an embedding record (this happens in
16759          Ada).  */
16760       if (TREE_CODE (value) == COMPONENT_REF
16761           && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
16762           && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
16763         decl = TREE_OPERAND (value, 1);
16764
16765       else if (TREE_CODE (value) == VAR_DECL
16766                || TREE_CODE (value) == PARM_DECL
16767                || TREE_CODE (value) == RESULT_DECL)
16768         decl = value;
16769
16770       if (decl != NULL_TREE)
16771         {
16772           dw_die_ref decl_die = lookup_decl_die (decl);
16773
16774           /* ??? Can this happen, or should the variable have been bound
16775              first?  Probably it can, since I imagine that we try to create
16776              the types of parameters in the order in which they exist in
16777              the list, and won't have created a forward reference to a
16778              later parameter.  */
16779           if (decl_die != NULL)
16780             {
16781               add_AT_die_ref (die, attr, decl_die);
16782               return;
16783             }
16784         }
16785     }
16786
16787   /* Last chance: try to create a stack operation procedure to evaluate the
16788      value.  Do nothing if even that is not possible or permitted.  */
16789   if ((forms & dw_scalar_form_exprloc) == 0)
16790     return;
16791
16792   list = loc_list_from_tree (value, 2, context);
16793   if (list == NULL || single_element_loc_list_p (list))
16794     {
16795       /* If this attribute is not a reference nor constant, it is
16796          a DWARF expression rather than location description.  For that
16797          loc_list_from_tree (value, 0, &context) is needed.  */
16798       dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
16799       if (list2 && single_element_loc_list_p (list2))
16800         {
16801           add_AT_loc (die, attr, list2->expr);
16802           return;
16803         }
16804     }
16805
16806   /* If that failed to give a single element location list, fall back to
16807      outputting this as a reference... still if permitted.  */
16808   if (list == NULL || (forms & dw_scalar_form_reference) == 0)
16809     return;
16810
16811   if (current_function_decl == 0)
16812     ctx = comp_unit_die ();
16813   else
16814     ctx = lookup_decl_die (current_function_decl);
16815
16816   decl_die = new_die (DW_TAG_variable, ctx, value);
16817   add_AT_flag (decl_die, DW_AT_artificial, 1);
16818   add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, ctx);
16819   add_AT_location_description (decl_die, DW_AT_location, list);
16820   add_AT_die_ref (die, attr, decl_die);
16821 }
16822
16823 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
16824    default.  */
16825
16826 static int
16827 lower_bound_default (void)
16828 {
16829   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16830     {
16831     case DW_LANG_C:
16832     case DW_LANG_C89:
16833     case DW_LANG_C99:
16834     case DW_LANG_C11:
16835     case DW_LANG_C_plus_plus:
16836     case DW_LANG_C_plus_plus_11:
16837     case DW_LANG_C_plus_plus_14:
16838     case DW_LANG_ObjC:
16839     case DW_LANG_ObjC_plus_plus:
16840     case DW_LANG_Java:
16841       return 0;
16842     case DW_LANG_Fortran77:
16843     case DW_LANG_Fortran90:
16844     case DW_LANG_Fortran95:
16845     case DW_LANG_Fortran03:
16846     case DW_LANG_Fortran08:
16847       return 1;
16848     case DW_LANG_UPC:
16849     case DW_LANG_D:
16850     case DW_LANG_Python:
16851       return dwarf_version >= 4 ? 0 : -1;
16852     case DW_LANG_Ada95:
16853     case DW_LANG_Ada83:
16854     case DW_LANG_Cobol74:
16855     case DW_LANG_Cobol85:
16856     case DW_LANG_Pascal83:
16857     case DW_LANG_Modula2:
16858     case DW_LANG_PLI:
16859       return dwarf_version >= 4 ? 1 : -1;
16860     default:
16861       return -1;
16862     }
16863 }
16864
16865 /* Given a tree node describing an array bound (either lower or upper) output
16866    a representation for that bound.  */
16867
16868 static void
16869 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
16870                 tree bound, const struct loc_descr_context *context)
16871 {
16872   int dflt;
16873
16874   while (1)
16875     switch (TREE_CODE (bound))
16876       {
16877       /* Strip all conversions.  */
16878       CASE_CONVERT:
16879       case VIEW_CONVERT_EXPR:
16880         bound = TREE_OPERAND (bound, 0);
16881         break;
16882
16883       /* All fixed-bounds are represented by INTEGER_CST nodes.  Lower bounds
16884          are even omitted when they are the default.  */
16885       case INTEGER_CST:
16886         /* If the value for this bound is the default one, we can even omit the
16887            attribute.  */
16888         if (bound_attr == DW_AT_lower_bound
16889             && tree_fits_shwi_p (bound)
16890             && (dflt = lower_bound_default ()) != -1
16891             && tree_to_shwi (bound) == dflt)
16892           return;
16893
16894         /* FALLTHRU */
16895
16896       default:
16897         add_scalar_info (subrange_die, bound_attr, bound,
16898                          dw_scalar_form_constant
16899                          | dw_scalar_form_exprloc
16900                          | dw_scalar_form_reference,
16901                          context);
16902         return;
16903       }
16904 }
16905
16906 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16907    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16908    Note that the block of subscript information for an array type also
16909    includes information about the element type of the given array type.
16910
16911    This function reuses previously set type and bound information if
16912    available.  */
16913
16914 static void
16915 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16916 {
16917   unsigned dimension_number;
16918   tree lower, upper;
16919   dw_die_ref child = type_die->die_child;
16920
16921   for (dimension_number = 0;
16922        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16923        type = TREE_TYPE (type), dimension_number++)
16924     {
16925       tree domain = TYPE_DOMAIN (type);
16926
16927       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16928         break;
16929
16930       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16931          and (in GNU C only) variable bounds.  Handle all three forms
16932          here.  */
16933
16934       /* Find and reuse a previously generated DW_TAG_subrange_type if
16935          available.
16936
16937          For multi-dimensional arrays, as we iterate through the
16938          various dimensions in the enclosing for loop above, we also
16939          iterate through the DIE children and pick at each
16940          DW_TAG_subrange_type previously generated (if available).
16941          Each child DW_TAG_subrange_type DIE describes the range of
16942          the current dimension.  At this point we should have as many
16943          DW_TAG_subrange_type's as we have dimensions in the
16944          array.  */
16945       dw_die_ref subrange_die = NULL;
16946       if (child)
16947         while (1)
16948           {
16949             child = child->die_sib;
16950             if (child->die_tag == DW_TAG_subrange_type)
16951               subrange_die = child;
16952             if (child == type_die->die_child)
16953               {
16954                 /* If we wrapped around, stop looking next time.  */
16955                 child = NULL;
16956                 break;
16957               }
16958             if (child->die_tag == DW_TAG_subrange_type)
16959               break;
16960           }
16961       if (!subrange_die)
16962         subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16963
16964       if (domain)
16965         {
16966           /* We have an array type with specified bounds.  */
16967           lower = TYPE_MIN_VALUE (domain);
16968           upper = TYPE_MAX_VALUE (domain);
16969
16970           /* Define the index type.  */
16971           if (TREE_TYPE (domain)
16972               && !get_AT (subrange_die, DW_AT_type))
16973             {
16974               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
16975                  TREE_TYPE field.  We can't emit debug info for this
16976                  because it is an unnamed integral type.  */
16977               if (TREE_CODE (domain) == INTEGER_TYPE
16978                   && TYPE_NAME (domain) == NULL_TREE
16979                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16980                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16981                 ;
16982               else
16983                 add_type_attribute (subrange_die, TREE_TYPE (domain),
16984                                     TYPE_UNQUALIFIED, type_die);
16985             }
16986
16987           /* ??? If upper is NULL, the array has unspecified length,
16988              but it does have a lower bound.  This happens with Fortran
16989                dimension arr(N:*)
16990              Since the debugger is definitely going to need to know N
16991              to produce useful results, go ahead and output the lower
16992              bound solo, and hope the debugger can cope.  */
16993
16994           if (!get_AT (subrange_die, DW_AT_lower_bound))
16995             add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
16996           if (upper && !get_AT (subrange_die, DW_AT_upper_bound))
16997             add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
16998         }
16999
17000       /* Otherwise we have an array type with an unspecified length.  The
17001          DWARF-2 spec does not say how to handle this; let's just leave out the
17002          bounds.  */
17003     }
17004 }
17005
17006 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size.  */
17007
17008 static void
17009 add_byte_size_attribute (dw_die_ref die, tree tree_node)
17010 {
17011   dw_die_ref decl_die;
17012   HOST_WIDE_INT size;
17013
17014   switch (TREE_CODE (tree_node))
17015     {
17016     case ERROR_MARK:
17017       size = 0;
17018       break;
17019     case ENUMERAL_TYPE:
17020     case RECORD_TYPE:
17021     case UNION_TYPE:
17022     case QUAL_UNION_TYPE:
17023       if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
17024           && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
17025         {
17026           add_AT_die_ref (die, DW_AT_byte_size, decl_die);
17027           return;
17028         }
17029       size = int_size_in_bytes (tree_node);
17030       break;
17031     case FIELD_DECL:
17032       /* For a data member of a struct or union, the DW_AT_byte_size is
17033          generally given as the number of bytes normally allocated for an
17034          object of the *declared* type of the member itself.  This is true
17035          even for bit-fields.  */
17036       size = int_size_in_bytes (field_type (tree_node));
17037       break;
17038     default:
17039       gcc_unreachable ();
17040     }
17041
17042   /* Note that `size' might be -1 when we get to this point.  If it is, that
17043      indicates that the byte size of the entity in question is variable.  We
17044      have no good way of expressing this fact in Dwarf at the present time,
17045      when location description was not used by the caller code instead.  */
17046   if (size >= 0)
17047     add_AT_unsigned (die, DW_AT_byte_size, size);
17048 }
17049
17050 /* For a FIELD_DECL node which represents a bit-field, output an attribute
17051    which specifies the distance in bits from the highest order bit of the
17052    "containing object" for the bit-field to the highest order bit of the
17053    bit-field itself.
17054
17055    For any given bit-field, the "containing object" is a hypothetical object
17056    (of some integral or enum type) within which the given bit-field lives.  The
17057    type of this hypothetical "containing object" is always the same as the
17058    declared type of the individual bit-field itself.  The determination of the
17059    exact location of the "containing object" for a bit-field is rather
17060    complicated.  It's handled by the `field_byte_offset' function (above).
17061
17062    Note that it is the size (in bytes) of the hypothetical "containing object"
17063    which will be given in the DW_AT_byte_size attribute for this bit-field.
17064    (See `byte_size_attribute' above).  */
17065
17066 static inline void
17067 add_bit_offset_attribute (dw_die_ref die, tree decl)
17068 {
17069   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
17070   tree type = DECL_BIT_FIELD_TYPE (decl);
17071   HOST_WIDE_INT bitpos_int;
17072   HOST_WIDE_INT highest_order_object_bit_offset;
17073   HOST_WIDE_INT highest_order_field_bit_offset;
17074   HOST_WIDE_INT bit_offset;
17075
17076   /* Must be a field and a bit field.  */
17077   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
17078
17079   /* We can't yet handle bit-fields whose offsets are variable, so if we
17080      encounter such things, just return without generating any attribute
17081      whatsoever.  Likewise for variable or too large size.  */
17082   if (! tree_fits_shwi_p (bit_position (decl))
17083       || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
17084     return;
17085
17086   bitpos_int = int_bit_position (decl);
17087
17088   /* Note that the bit offset is always the distance (in bits) from the
17089      highest-order bit of the "containing object" to the highest-order bit of
17090      the bit-field itself.  Since the "high-order end" of any object or field
17091      is different on big-endian and little-endian machines, the computation
17092      below must take account of these differences.  */
17093   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
17094   highest_order_field_bit_offset = bitpos_int;
17095
17096   if (! BYTES_BIG_ENDIAN)
17097     {
17098       highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
17099       highest_order_object_bit_offset += simple_type_size_in_bits (type);
17100     }
17101
17102   bit_offset
17103     = (! BYTES_BIG_ENDIAN
17104        ? highest_order_object_bit_offset - highest_order_field_bit_offset
17105        : highest_order_field_bit_offset - highest_order_object_bit_offset);
17106
17107   if (bit_offset < 0)
17108     add_AT_int (die, DW_AT_bit_offset, bit_offset);
17109   else
17110     add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
17111 }
17112
17113 /* For a FIELD_DECL node which represents a bit field, output an attribute
17114    which specifies the length in bits of the given field.  */
17115
17116 static inline void
17117 add_bit_size_attribute (dw_die_ref die, tree decl)
17118 {
17119   /* Must be a field and a bit field.  */
17120   gcc_assert (TREE_CODE (decl) == FIELD_DECL
17121               && DECL_BIT_FIELD_TYPE (decl));
17122
17123   if (tree_fits_uhwi_p (DECL_SIZE (decl)))
17124     add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
17125 }
17126
17127 /* If the compiled language is ANSI C, then add a 'prototyped'
17128    attribute, if arg types are given for the parameters of a function.  */
17129
17130 static inline void
17131 add_prototyped_attribute (dw_die_ref die, tree func_type)
17132 {
17133   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
17134     {
17135     case DW_LANG_C:
17136     case DW_LANG_C89:
17137     case DW_LANG_C99:
17138     case DW_LANG_C11:
17139     case DW_LANG_ObjC:
17140       if (prototype_p (func_type))
17141         add_AT_flag (die, DW_AT_prototyped, 1);
17142       break;
17143     default:
17144       break;
17145     }
17146 }
17147
17148 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
17149    by looking in either the type declaration or object declaration
17150    equate table.  */
17151
17152 static inline dw_die_ref
17153 add_abstract_origin_attribute (dw_die_ref die, tree origin)
17154 {
17155   dw_die_ref origin_die = NULL;
17156
17157   if (TREE_CODE (origin) != FUNCTION_DECL)
17158     {
17159       /* We may have gotten separated from the block for the inlined
17160          function, if we're in an exception handler or some such; make
17161          sure that the abstract function has been written out.
17162
17163          Doing this for nested functions is wrong, however; functions are
17164          distinct units, and our context might not even be inline.  */
17165       tree fn = origin;
17166
17167       if (TYPE_P (fn))
17168         fn = TYPE_STUB_DECL (fn);
17169
17170       fn = decl_function_context (fn);
17171       if (fn)
17172         dwarf2out_abstract_function (fn);
17173     }
17174
17175   if (DECL_P (origin))
17176     origin_die = lookup_decl_die (origin);
17177   else if (TYPE_P (origin))
17178     origin_die = lookup_type_die (origin);
17179
17180   /* XXX: Functions that are never lowered don't always have correct block
17181      trees (in the case of java, they simply have no block tree, in some other
17182      languages).  For these functions, there is nothing we can really do to
17183      output correct debug info for inlined functions in all cases.  Rather
17184      than die, we'll just produce deficient debug info now, in that we will
17185      have variables without a proper abstract origin.  In the future, when all
17186      functions are lowered, we should re-add a gcc_assert (origin_die)
17187      here.  */
17188
17189   if (origin_die)
17190     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
17191   return origin_die;
17192 }
17193
17194 /* We do not currently support the pure_virtual attribute.  */
17195
17196 static inline void
17197 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
17198 {
17199   if (DECL_VINDEX (func_decl))
17200     {
17201       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
17202
17203       if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
17204         add_AT_loc (die, DW_AT_vtable_elem_location,
17205                     new_loc_descr (DW_OP_constu,
17206                                    tree_to_shwi (DECL_VINDEX (func_decl)),
17207                                    0));
17208
17209       /* GNU extension: Record what type this method came from originally.  */
17210       if (debug_info_level > DINFO_LEVEL_TERSE
17211           && DECL_CONTEXT (func_decl))
17212         add_AT_die_ref (die, DW_AT_containing_type,
17213                         lookup_type_die (DECL_CONTEXT (func_decl)));
17214     }
17215 }
17216 \f
17217 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
17218    given decl.  This used to be a vendor extension until after DWARF 4
17219    standardized it.  */
17220
17221 static void
17222 add_linkage_attr (dw_die_ref die, tree decl)
17223 {
17224   const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
17225
17226   /* Mimic what assemble_name_raw does with a leading '*'.  */
17227   if (name[0] == '*')
17228     name = &name[1];
17229
17230   if (dwarf_version >= 4)
17231     add_AT_string (die, DW_AT_linkage_name, name);
17232   else
17233     add_AT_string (die, DW_AT_MIPS_linkage_name, name);
17234 }
17235
17236 /* Add source coordinate attributes for the given decl.  */
17237
17238 static void
17239 add_src_coords_attributes (dw_die_ref die, tree decl)
17240 {
17241   expanded_location s;
17242
17243   if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
17244     return;
17245   s = expand_location (DECL_SOURCE_LOCATION (decl));
17246   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
17247   add_AT_unsigned (die, DW_AT_decl_line, s.line);
17248 }
17249
17250 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl.  */
17251
17252 static void
17253 add_linkage_name (dw_die_ref die, tree decl)
17254 {
17255   if (debug_info_level > DINFO_LEVEL_NONE
17256       && (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
17257       && TREE_PUBLIC (decl)
17258       && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
17259       && die->die_tag != DW_TAG_member)
17260     {
17261       /* Defer until we have an assembler name set.  */
17262       if (!DECL_ASSEMBLER_NAME_SET_P (decl))
17263         {
17264           limbo_die_node *asm_name;
17265
17266           asm_name = ggc_cleared_alloc<limbo_die_node> ();
17267           asm_name->die = die;
17268           asm_name->created_for = decl;
17269           asm_name->next = deferred_asm_name;
17270           deferred_asm_name = asm_name;
17271         }
17272       else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
17273         add_linkage_attr (die, decl);
17274     }
17275 }
17276
17277 /* Add a DW_AT_name attribute and source coordinate attribute for the
17278    given decl, but only if it actually has a name.  */
17279
17280 static void
17281 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
17282 {
17283   tree decl_name;
17284
17285   decl_name = DECL_NAME (decl);
17286   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
17287     {
17288       const char *name = dwarf2_name (decl, 0);
17289       if (name)
17290         add_name_attribute (die, name);
17291       if (! DECL_ARTIFICIAL (decl))
17292         add_src_coords_attributes (die, decl);
17293
17294       add_linkage_name (die, decl);
17295     }
17296
17297 #ifdef VMS_DEBUGGING_INFO
17298   /* Get the function's name, as described by its RTL.  This may be different
17299      from the DECL_NAME name used in the source file.  */
17300   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
17301     {
17302       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
17303                   XEXP (DECL_RTL (decl), 0), false);
17304       vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
17305     }
17306 #endif /* VMS_DEBUGGING_INFO */
17307 }
17308
17309 #ifdef VMS_DEBUGGING_INFO
17310 /* Output the debug main pointer die for VMS */
17311
17312 void
17313 dwarf2out_vms_debug_main_pointer (void)
17314 {
17315   char label[MAX_ARTIFICIAL_LABEL_BYTES];
17316   dw_die_ref die;
17317
17318   /* Allocate the VMS debug main subprogram die.  */
17319   die = ggc_cleared_alloc<die_node> ();
17320   die->die_tag = DW_TAG_subprogram;
17321   add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
17322   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
17323                                current_function_funcdef_no);
17324   add_AT_lbl_id (die, DW_AT_entry_pc, label);
17325
17326   /* Make it the first child of comp_unit_die ().  */
17327   die->die_parent = comp_unit_die ();
17328   if (comp_unit_die ()->die_child)
17329     {
17330       die->die_sib = comp_unit_die ()->die_child->die_sib;
17331       comp_unit_die ()->die_child->die_sib = die;
17332     }
17333   else
17334     {
17335       die->die_sib = die;
17336       comp_unit_die ()->die_child = die;
17337     }
17338 }
17339 #endif /* VMS_DEBUGGING_INFO */
17340
17341 /* Push a new declaration scope.  */
17342
17343 static void
17344 push_decl_scope (tree scope)
17345 {
17346   vec_safe_push (decl_scope_table, scope);
17347 }
17348
17349 /* Pop a declaration scope.  */
17350
17351 static inline void
17352 pop_decl_scope (void)
17353 {
17354   decl_scope_table->pop ();
17355 }
17356
17357 /* walk_tree helper function for uses_local_type, below.  */
17358
17359 static tree
17360 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
17361 {
17362   if (!TYPE_P (*tp))
17363     *walk_subtrees = 0;
17364   else
17365     {
17366       tree name = TYPE_NAME (*tp);
17367       if (name && DECL_P (name) && decl_function_context (name))
17368         return *tp;
17369     }
17370   return NULL_TREE;
17371 }
17372
17373 /* If TYPE involves a function-local type (including a local typedef to a
17374    non-local type), returns that type; otherwise returns NULL_TREE.  */
17375
17376 static tree
17377 uses_local_type (tree type)
17378 {
17379   tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
17380   return used;
17381 }
17382
17383 /* Return the DIE for the scope that immediately contains this type.
17384    Non-named types that do not involve a function-local type get global
17385    scope.  Named types nested in namespaces or other types get their
17386    containing scope.  All other types (i.e. function-local named types) get
17387    the current active scope.  */
17388
17389 static dw_die_ref
17390 scope_die_for (tree t, dw_die_ref context_die)
17391 {
17392   dw_die_ref scope_die = NULL;
17393   tree containing_scope;
17394
17395   /* Non-types always go in the current scope.  */
17396   gcc_assert (TYPE_P (t));
17397
17398   /* Use the scope of the typedef, rather than the scope of the type
17399      it refers to.  */
17400   if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
17401     containing_scope = DECL_CONTEXT (TYPE_NAME (t));
17402   else
17403     containing_scope = TYPE_CONTEXT (t);
17404
17405   /* Use the containing namespace if there is one.  */
17406   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
17407     {
17408       if (context_die == lookup_decl_die (containing_scope))
17409         /* OK */;
17410       else if (debug_info_level > DINFO_LEVEL_TERSE)
17411         context_die = get_context_die (containing_scope);
17412       else
17413         containing_scope = NULL_TREE;
17414     }
17415
17416   /* Ignore function type "scopes" from the C frontend.  They mean that
17417      a tagged type is local to a parmlist of a function declarator, but
17418      that isn't useful to DWARF.  */
17419   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
17420     containing_scope = NULL_TREE;
17421
17422   if (SCOPE_FILE_SCOPE_P (containing_scope))
17423     {
17424       /* If T uses a local type keep it local as well, to avoid references
17425          to function-local DIEs from outside the function.  */
17426       if (current_function_decl && uses_local_type (t))
17427         scope_die = context_die;
17428       else
17429         scope_die = comp_unit_die ();
17430     }
17431   else if (TYPE_P (containing_scope))
17432     {
17433       /* For types, we can just look up the appropriate DIE.  */
17434       if (debug_info_level > DINFO_LEVEL_TERSE)
17435         scope_die = get_context_die (containing_scope);
17436       else
17437         {
17438           scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
17439           if (scope_die == NULL)
17440             scope_die = comp_unit_die ();
17441         }
17442     }
17443   else
17444     scope_die = context_die;
17445
17446   return scope_die;
17447 }
17448
17449 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
17450
17451 static inline int
17452 local_scope_p (dw_die_ref context_die)
17453 {
17454   for (; context_die; context_die = context_die->die_parent)
17455     if (context_die->die_tag == DW_TAG_inlined_subroutine
17456         || context_die->die_tag == DW_TAG_subprogram)
17457       return 1;
17458
17459   return 0;
17460 }
17461
17462 /* Returns nonzero if CONTEXT_DIE is a class.  */
17463
17464 static inline int
17465 class_scope_p (dw_die_ref context_die)
17466 {
17467   return (context_die
17468           && (context_die->die_tag == DW_TAG_structure_type
17469               || context_die->die_tag == DW_TAG_class_type
17470               || context_die->die_tag == DW_TAG_interface_type
17471               || context_die->die_tag == DW_TAG_union_type));
17472 }
17473
17474 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17475    whether or not to treat a DIE in this context as a declaration.  */
17476
17477 static inline int
17478 class_or_namespace_scope_p (dw_die_ref context_die)
17479 {
17480   return (class_scope_p (context_die)
17481           || (context_die && context_die->die_tag == DW_TAG_namespace));
17482 }
17483
17484 /* Many forms of DIEs require a "type description" attribute.  This
17485    routine locates the proper "type descriptor" die for the type given
17486    by 'type' plus any additional qualifiers given by 'cv_quals', and
17487    adds a DW_AT_type attribute below the given die.  */
17488
17489 static void
17490 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
17491                     dw_die_ref context_die)
17492 {
17493   enum tree_code code  = TREE_CODE (type);
17494   dw_die_ref type_die  = NULL;
17495
17496   /* ??? If this type is an unnamed subrange type of an integral, floating-point
17497      or fixed-point type, use the inner type.  This is because we have no
17498      support for unnamed types in base_type_die.  This can happen if this is
17499      an Ada subrange type.  Correct solution is emit a subrange type die.  */
17500   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
17501       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
17502     type = TREE_TYPE (type), code = TREE_CODE (type);
17503
17504   if (code == ERROR_MARK
17505       /* Handle a special case.  For functions whose return type is void, we
17506          generate *no* type attribute.  (Note that no object may have type
17507          `void', so this only applies to function return types).  */
17508       || code == VOID_TYPE)
17509     return;
17510
17511   type_die = modified_type_die (type,
17512                                 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
17513                                 context_die);
17514
17515   if (type_die != NULL)
17516     add_AT_die_ref (object_die, DW_AT_type, type_die);
17517 }
17518
17519 /* Given an object die, add the calling convention attribute for the
17520    function call type.  */
17521 static void
17522 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
17523 {
17524   enum dwarf_calling_convention value = DW_CC_normal;
17525
17526   value = ((enum dwarf_calling_convention)
17527            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
17528
17529   if (is_fortran ()
17530       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
17531     {
17532       /* DWARF 2 doesn't provide a way to identify a program's source-level
17533         entry point.  DW_AT_calling_convention attributes are only meant
17534         to describe functions' calling conventions.  However, lacking a
17535         better way to signal the Fortran main program, we used this for 
17536         a long time, following existing custom.  Now, DWARF 4 has 
17537         DW_AT_main_subprogram, which we add below, but some tools still
17538         rely on the old way, which we thus keep.  */
17539       value = DW_CC_program;
17540
17541       if (dwarf_version >= 4 || !dwarf_strict)
17542         add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
17543     }
17544
17545   /* Only add the attribute if the backend requests it, and
17546      is not DW_CC_normal.  */
17547   if (value && (value != DW_CC_normal))
17548     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
17549 }
17550
17551 /* Given a tree pointer to a struct, class, union, or enum type node, return
17552    a pointer to the (string) tag name for the given type, or zero if the type
17553    was declared without a tag.  */
17554
17555 static const char *
17556 type_tag (const_tree type)
17557 {
17558   const char *name = 0;
17559
17560   if (TYPE_NAME (type) != 0)
17561     {
17562       tree t = 0;
17563
17564       /* Find the IDENTIFIER_NODE for the type name.  */
17565       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
17566           && !TYPE_NAMELESS (type))
17567         t = TYPE_NAME (type);
17568
17569       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
17570          a TYPE_DECL node, regardless of whether or not a `typedef' was
17571          involved.  */
17572       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
17573                && ! DECL_IGNORED_P (TYPE_NAME (type)))
17574         {
17575           /* We want to be extra verbose.  Don't call dwarf_name if
17576              DECL_NAME isn't set.  The default hook for decl_printable_name
17577              doesn't like that, and in this context it's correct to return
17578              0, instead of "<anonymous>" or the like.  */
17579           if (DECL_NAME (TYPE_NAME (type))
17580               && !DECL_NAMELESS (TYPE_NAME (type)))
17581             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
17582         }
17583
17584       /* Now get the name as a string, or invent one.  */
17585       if (!name && t != 0)
17586         name = IDENTIFIER_POINTER (t);
17587     }
17588
17589   return (name == 0 || *name == '\0') ? 0 : name;
17590 }
17591
17592 /* Return the type associated with a data member, make a special check
17593    for bit field types.  */
17594
17595 static inline tree
17596 member_declared_type (const_tree member)
17597 {
17598   return (DECL_BIT_FIELD_TYPE (member)
17599           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
17600 }
17601
17602 /* Get the decl's label, as described by its RTL. This may be different
17603    from the DECL_NAME name used in the source file.  */
17604
17605 #if 0
17606 static const char *
17607 decl_start_label (tree decl)
17608 {
17609   rtx x;
17610   const char *fnname;
17611
17612   x = DECL_RTL (decl);
17613   gcc_assert (MEM_P (x));
17614
17615   x = XEXP (x, 0);
17616   gcc_assert (GET_CODE (x) == SYMBOL_REF);
17617
17618   fnname = XSTR (x, 0);
17619   return fnname;
17620 }
17621 #endif
17622 \f
17623 /* For variable-length arrays that have been previously generated, but
17624    may be incomplete due to missing subscript info, fill the subscript
17625    info.  Return TRUE if this is one of those cases.  */
17626 static bool
17627 fill_variable_array_bounds (tree type)
17628 {
17629   if (TREE_ASM_WRITTEN (type)
17630       && TREE_CODE (type) == ARRAY_TYPE
17631       && variably_modified_type_p (type, NULL))
17632     {
17633       dw_die_ref array_die = lookup_type_die (type);
17634       if (!array_die)
17635         return false;
17636       add_subscript_info (array_die, type, !is_ada ());
17637       return true;
17638     }
17639   return false;
17640 }
17641
17642 /* These routines generate the internal representation of the DIE's for
17643    the compilation unit.  Debugging information is collected by walking
17644    the declaration trees passed in from dwarf2out_decl().  */
17645
17646 static void
17647 gen_array_type_die (tree type, dw_die_ref context_die)
17648 {
17649   dw_die_ref array_die;
17650
17651   /* GNU compilers represent multidimensional array types as sequences of one
17652      dimensional array types whose element types are themselves array types.
17653      We sometimes squish that down to a single array_type DIE with multiple
17654      subscripts in the Dwarf debugging info.  The draft Dwarf specification
17655      say that we are allowed to do this kind of compression in C, because
17656      there is no difference between an array of arrays and a multidimensional
17657      array.  We don't do this for Ada to remain as close as possible to the
17658      actual representation, which is especially important against the language
17659      flexibilty wrt arrays of variable size.  */
17660
17661   bool collapse_nested_arrays = !is_ada ();
17662
17663   if (fill_variable_array_bounds (type))
17664     return;
17665
17666   dw_die_ref scope_die = scope_die_for (type, context_die);
17667   tree element_type;
17668
17669   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
17670      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
17671   if (TYPE_STRING_FLAG (type)
17672       && TREE_CODE (type) == ARRAY_TYPE
17673       && is_fortran ()
17674       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
17675     {
17676       HOST_WIDE_INT size;
17677
17678       array_die = new_die (DW_TAG_string_type, scope_die, type);
17679       add_name_attribute (array_die, type_tag (type));
17680       equate_type_number_to_die (type, array_die);
17681       size = int_size_in_bytes (type);
17682       if (size >= 0)
17683         add_AT_unsigned (array_die, DW_AT_byte_size, size);
17684       else if (TYPE_DOMAIN (type) != NULL_TREE
17685                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
17686                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17687         {
17688           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17689           dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2, NULL);
17690
17691           size = int_size_in_bytes (TREE_TYPE (szdecl));
17692           if (loc && size > 0)
17693             {
17694               add_AT_location_description (array_die, DW_AT_string_length, loc);
17695               if (size != DWARF2_ADDR_SIZE)
17696                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17697             }
17698         }
17699       return;
17700     }
17701
17702   array_die = new_die (DW_TAG_array_type, scope_die, type);
17703   add_name_attribute (array_die, type_tag (type));
17704   equate_type_number_to_die (type, array_die);
17705
17706   if (TREE_CODE (type) == VECTOR_TYPE)
17707     add_AT_flag (array_die, DW_AT_GNU_vector, 1);
17708
17709   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
17710   if (is_fortran ()
17711       && TREE_CODE (type) == ARRAY_TYPE
17712       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17713       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17714     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17715
17716 #if 0
17717   /* We default the array ordering.  SDB will probably do
17718      the right things even if DW_AT_ordering is not present.  It's not even
17719      an issue until we start to get into multidimensional arrays anyway.  If
17720      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17721      then we'll have to put the DW_AT_ordering attribute back in.  (But if
17722      and when we find out that we need to put these in, we will only do so
17723      for multidimensional arrays.  */
17724   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17725 #endif
17726
17727   if (TREE_CODE (type) == VECTOR_TYPE)
17728     {
17729       /* For VECTOR_TYPEs we use an array die with appropriate bounds.  */
17730       dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
17731       add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
17732       add_bound_info (subrange_die, DW_AT_upper_bound,
17733                       size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
17734     }
17735   else
17736     add_subscript_info (array_die, type, collapse_nested_arrays);
17737
17738   /* Add representation of the type of the elements of this array type and
17739      emit the corresponding DIE if we haven't done it already.  */
17740   element_type = TREE_TYPE (type);
17741   if (collapse_nested_arrays)
17742     while (TREE_CODE (element_type) == ARRAY_TYPE)
17743       {
17744         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17745           break;
17746         element_type = TREE_TYPE (element_type);
17747       }
17748
17749   add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED, context_die);
17750
17751   add_gnat_descriptive_type_attribute (array_die, type, context_die);
17752   if (TYPE_ARTIFICIAL (type))
17753     add_AT_flag (array_die, DW_AT_artificial, 1);
17754
17755   if (get_AT (array_die, DW_AT_name))
17756     add_pubtype (type, array_die);
17757 }
17758
17759 /* This routine generates DIE for array with hidden descriptor, details
17760    are filled into *info by a langhook.  */
17761
17762 static void
17763 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17764                           dw_die_ref context_die)
17765 {
17766   const dw_die_ref scope_die = scope_die_for (type, context_die);
17767   const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
17768   const struct loc_descr_context context = { type, info->base_decl };
17769   int dim;
17770
17771   add_name_attribute (array_die, type_tag (type));
17772   equate_type_number_to_die (type, array_die);
17773
17774   if (info->ndimensions > 1)
17775     switch (info->ordering)
17776       {
17777       case array_descr_ordering_row_major:
17778         add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17779         break;
17780       case array_descr_ordering_column_major:
17781         add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17782         break;
17783       default:
17784         break;
17785       }
17786
17787   if (dwarf_version >= 3 || !dwarf_strict)
17788     {
17789       if (info->data_location)
17790         add_scalar_info (array_die, DW_AT_data_location, info->data_location,
17791                          dw_scalar_form_exprloc, &context);
17792       if (info->associated)
17793         add_scalar_info (array_die, DW_AT_associated, info->associated,
17794                          dw_scalar_form_constant
17795                          | dw_scalar_form_exprloc
17796                          | dw_scalar_form_reference, &context);
17797       if (info->allocated)
17798         add_scalar_info (array_die, DW_AT_allocated, info->allocated,
17799                          dw_scalar_form_constant
17800                          | dw_scalar_form_exprloc
17801                          | dw_scalar_form_reference, &context);
17802     }
17803
17804   add_gnat_descriptive_type_attribute (array_die, type, context_die);
17805
17806   for (dim = 0; dim < info->ndimensions; dim++)
17807     {
17808       dw_die_ref subrange_die
17809         = new_die (DW_TAG_subrange_type, array_die, NULL);
17810
17811       if (info->dimen[dim].bounds_type)
17812         add_type_attribute (subrange_die,
17813                             info->dimen[dim].bounds_type, 0,
17814                             context_die);
17815       if (info->dimen[dim].lower_bound)
17816         add_bound_info (subrange_die, DW_AT_lower_bound,
17817                         info->dimen[dim].lower_bound, &context);
17818       if (info->dimen[dim].upper_bound)
17819         add_bound_info (subrange_die, DW_AT_upper_bound,
17820                         info->dimen[dim].upper_bound, &context);
17821       if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
17822         add_scalar_info (subrange_die, DW_AT_byte_stride,
17823                          info->dimen[dim].stride,
17824                          dw_scalar_form_constant
17825                          | dw_scalar_form_exprloc
17826                          | dw_scalar_form_reference,
17827                          &context);
17828     }
17829
17830   gen_type_die (info->element_type, context_die);
17831   add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
17832                       context_die);
17833
17834   if (get_AT (array_die, DW_AT_name))
17835     add_pubtype (type, array_die);
17836 }
17837
17838 #if 0
17839 static void
17840 gen_entry_point_die (tree decl, dw_die_ref context_die)
17841 {
17842   tree origin = decl_ultimate_origin (decl);
17843   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17844
17845   if (origin != NULL)
17846     add_abstract_origin_attribute (decl_die, origin);
17847   else
17848     {
17849       add_name_and_src_coords_attributes (decl_die, decl);
17850       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17851                           TYPE_UNQUALIFIED, context_die);
17852     }
17853
17854   if (DECL_ABSTRACT_P (decl))
17855     equate_decl_number_to_die (decl, decl_die);
17856   else
17857     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17858 }
17859 #endif
17860
17861 /* Walk through the list of incomplete types again, trying once more to
17862    emit full debugging info for them.  */
17863
17864 static void
17865 retry_incomplete_types (void)
17866 {
17867   int i;
17868
17869   for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17870     if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17871       gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17872 }
17873
17874 /* Determine what tag to use for a record type.  */
17875
17876 static enum dwarf_tag
17877 record_type_tag (tree type)
17878 {
17879   if (! lang_hooks.types.classify_record)
17880     return DW_TAG_structure_type;
17881
17882   switch (lang_hooks.types.classify_record (type))
17883     {
17884     case RECORD_IS_STRUCT:
17885       return DW_TAG_structure_type;
17886
17887     case RECORD_IS_CLASS:
17888       return DW_TAG_class_type;
17889
17890     case RECORD_IS_INTERFACE:
17891       if (dwarf_version >= 3 || !dwarf_strict)
17892         return DW_TAG_interface_type;
17893       return DW_TAG_structure_type;
17894
17895     default:
17896       gcc_unreachable ();
17897     }
17898 }
17899
17900 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
17901    include all of the information about the enumeration values also. Each
17902    enumerated type name/value is listed as a child of the enumerated type
17903    DIE.  */
17904
17905 static dw_die_ref
17906 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17907 {
17908   dw_die_ref type_die = lookup_type_die (type);
17909
17910   if (type_die == NULL)
17911     {
17912       type_die = new_die (DW_TAG_enumeration_type,
17913                           scope_die_for (type, context_die), type);
17914       equate_type_number_to_die (type, type_die);
17915       add_name_attribute (type_die, type_tag (type));
17916       if (dwarf_version >= 4 || !dwarf_strict)
17917         {
17918           if (ENUM_IS_SCOPED (type))
17919             add_AT_flag (type_die, DW_AT_enum_class, 1);
17920           if (ENUM_IS_OPAQUE (type))
17921             add_AT_flag (type_die, DW_AT_declaration, 1);
17922         }
17923     }
17924   else if (! TYPE_SIZE (type))
17925     return type_die;
17926   else
17927     remove_AT (type_die, DW_AT_declaration);
17928
17929   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
17930      given enum type is incomplete, do not generate the DW_AT_byte_size
17931      attribute or the DW_AT_element_list attribute.  */
17932   if (TYPE_SIZE (type))
17933     {
17934       tree link;
17935
17936       TREE_ASM_WRITTEN (type) = 1;
17937       add_byte_size_attribute (type_die, type);
17938       if (dwarf_version >= 3 || !dwarf_strict)
17939         {
17940           tree underlying = lang_hooks.types.enum_underlying_base_type (type);
17941           add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED,
17942                               context_die);
17943         }
17944       if (TYPE_STUB_DECL (type) != NULL_TREE)
17945         {
17946           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17947           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17948         }
17949
17950       /* If the first reference to this type was as the return type of an
17951          inline function, then it may not have a parent.  Fix this now.  */
17952       if (type_die->die_parent == NULL)
17953         add_child_die (scope_die_for (type, context_die), type_die);
17954
17955       for (link = TYPE_VALUES (type);
17956            link != NULL; link = TREE_CHAIN (link))
17957         {
17958           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17959           tree value = TREE_VALUE (link);
17960
17961           add_name_attribute (enum_die,
17962                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17963
17964           if (TREE_CODE (value) == CONST_DECL)
17965             value = DECL_INITIAL (value);
17966
17967           if (simple_type_size_in_bits (TREE_TYPE (value))
17968               <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
17969             {
17970               /* For constant forms created by add_AT_unsigned DWARF
17971                  consumers (GDB, elfutils, etc.) always zero extend
17972                  the value.  Only when the actual value is negative
17973                  do we need to use add_AT_int to generate a constant
17974                  form that can represent negative values.  */
17975               HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
17976               if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
17977                 add_AT_unsigned (enum_die, DW_AT_const_value,
17978                                  (unsigned HOST_WIDE_INT) val);
17979               else
17980                 add_AT_int (enum_die, DW_AT_const_value, val);
17981             }
17982           else
17983             /* Enumeration constants may be wider than HOST_WIDE_INT.  Handle
17984                that here.  TODO: This should be re-worked to use correct
17985                signed/unsigned double tags for all cases.  */
17986             add_AT_wide (enum_die, DW_AT_const_value, value);
17987         }
17988
17989       add_gnat_descriptive_type_attribute (type_die, type, context_die);
17990       if (TYPE_ARTIFICIAL (type))
17991         add_AT_flag (type_die, DW_AT_artificial, 1);
17992     }
17993   else
17994     add_AT_flag (type_die, DW_AT_declaration, 1);
17995
17996   add_pubtype (type, type_die);
17997
17998   return type_die;
17999 }
18000
18001 /* Generate a DIE to represent either a real live formal parameter decl or to
18002    represent just the type of some formal parameter position in some function
18003    type.
18004
18005    Note that this routine is a bit unusual because its argument may be a
18006    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
18007    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
18008    node.  If it's the former then this function is being called to output a
18009    DIE to represent a formal parameter object (or some inlining thereof).  If
18010    it's the latter, then this function is only being called to output a
18011    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
18012    argument type of some subprogram type.
18013    If EMIT_NAME_P is true, name and source coordinate attributes
18014    are emitted.  */
18015
18016 static dw_die_ref
18017 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
18018                           dw_die_ref context_die)
18019 {
18020   tree node_or_origin = node ? node : origin;
18021   tree ultimate_origin;
18022   dw_die_ref parm_die = NULL;
18023   
18024   if (TREE_CODE_CLASS (TREE_CODE (node_or_origin)) == tcc_declaration)
18025     {
18026       parm_die = lookup_decl_die (node);
18027
18028       /* If the contexts differ, we may not be talking about the same
18029          thing.  */
18030       if (parm_die && parm_die->die_parent != context_die)
18031         {
18032           if (!DECL_ABSTRACT_P (node))
18033             {
18034               /* This can happen when creating an inlined instance, in
18035                  which case we need to create a new DIE that will get
18036                  annotated with DW_AT_abstract_origin.  */
18037               parm_die = NULL;
18038             }
18039           else
18040             {
18041               /* FIXME: Reuse DIE even with a differing context.
18042
18043                  This can happen when calling
18044                  dwarf2out_abstract_function to build debug info for
18045                  the abstract instance of a function for which we have
18046                  already generated a DIE in
18047                  dwarf2out_early_global_decl.
18048
18049                  Once we remove dwarf2out_abstract_function, we should
18050                  have a call to gcc_unreachable here.  */
18051             }
18052         }
18053
18054       if (parm_die && parm_die->die_parent == NULL)
18055         {
18056           /* Check that parm_die already has the right attributes that
18057              we would have added below.  If any attributes are
18058              missing, fall through to add them.  */
18059           if (! DECL_ABSTRACT_P (node_or_origin)
18060               && !get_AT (parm_die, DW_AT_location)
18061               && !get_AT (parm_die, DW_AT_const_value))
18062             /* We are missing  location info, and are about to add it.  */
18063             ;
18064           else
18065             {
18066               add_child_die (context_die, parm_die);
18067               return parm_die;
18068             }
18069         }
18070     }
18071
18072   /* If we have a previously generated DIE, use it, unless this is an
18073      concrete instance (origin != NULL), in which case we need a new
18074      DIE with a corresponding DW_AT_abstract_origin.  */
18075   bool reusing_die;
18076   if (parm_die && origin == NULL)
18077     reusing_die = true;
18078   else
18079     {
18080       parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
18081       reusing_die = false;
18082     }
18083
18084   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
18085     {
18086     case tcc_declaration:
18087       ultimate_origin = decl_ultimate_origin (node_or_origin);
18088       if (node || ultimate_origin)
18089         origin = ultimate_origin;
18090
18091       if (reusing_die)
18092         goto add_location;
18093
18094       if (origin != NULL)
18095         add_abstract_origin_attribute (parm_die, origin);
18096       else if (emit_name_p)
18097         add_name_and_src_coords_attributes (parm_die, node);
18098       if (origin == NULL
18099           || (! DECL_ABSTRACT_P (node_or_origin)
18100               && variably_modified_type_p (TREE_TYPE (node_or_origin),
18101                                            decl_function_context
18102                                                             (node_or_origin))))
18103         {
18104           tree type = TREE_TYPE (node_or_origin);
18105           if (decl_by_reference_p (node_or_origin))
18106             add_type_attribute (parm_die, TREE_TYPE (type),
18107                                 TYPE_UNQUALIFIED, context_die);
18108           else
18109             add_type_attribute (parm_die, type,
18110                                 decl_quals (node_or_origin),
18111                                 context_die);
18112         }
18113       if (origin == NULL && DECL_ARTIFICIAL (node))
18114         add_AT_flag (parm_die, DW_AT_artificial, 1);
18115     add_location:
18116       if (node && node != origin)
18117         equate_decl_number_to_die (node, parm_die);
18118       if (! DECL_ABSTRACT_P (node_or_origin))
18119         add_location_or_const_value_attribute (parm_die, node_or_origin,
18120                                                node == NULL, DW_AT_location);
18121
18122       break;
18123
18124     case tcc_type:
18125       /* We were called with some kind of a ..._TYPE node.  */
18126       add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED,
18127                           context_die);
18128       break;
18129
18130     default:
18131       gcc_unreachable ();
18132     }
18133
18134   return parm_die;
18135 }
18136
18137 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
18138    children DW_TAG_formal_parameter DIEs representing the arguments of the
18139    parameter pack.
18140
18141    PARM_PACK must be a function parameter pack.
18142    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
18143    must point to the subsequent arguments of the function PACK_ARG belongs to.
18144    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
18145    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
18146    following the last one for which a DIE was generated.  */
18147
18148 static dw_die_ref
18149 gen_formal_parameter_pack_die  (tree parm_pack,
18150                                 tree pack_arg,
18151                                 dw_die_ref subr_die,
18152                                 tree *next_arg)
18153 {
18154   tree arg;
18155   dw_die_ref parm_pack_die;
18156
18157   gcc_assert (parm_pack
18158               && lang_hooks.function_parameter_pack_p (parm_pack)
18159               && subr_die);
18160
18161   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
18162   add_src_coords_attributes (parm_pack_die, parm_pack);
18163
18164   for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
18165     {
18166       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
18167                                                                  parm_pack))
18168         break;
18169       gen_formal_parameter_die (arg, NULL,
18170                                 false /* Don't emit name attribute.  */,
18171                                 parm_pack_die);
18172     }
18173   if (next_arg)
18174     *next_arg = arg;
18175   return parm_pack_die;
18176 }
18177
18178 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
18179    at the end of an (ANSI prototyped) formal parameters list.  */
18180
18181 static void
18182 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
18183 {
18184   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
18185 }
18186
18187 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
18188    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
18189    parameters as specified in some function type specification (except for
18190    those which appear as part of a function *definition*).  */
18191
18192 static void
18193 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
18194 {
18195   tree link;
18196   tree formal_type = NULL;
18197   tree first_parm_type;
18198   tree arg;
18199
18200   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
18201     {
18202       arg = DECL_ARGUMENTS (function_or_method_type);
18203       function_or_method_type = TREE_TYPE (function_or_method_type);
18204     }
18205   else
18206     arg = NULL_TREE;
18207
18208   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
18209
18210   /* Make our first pass over the list of formal parameter types and output a
18211      DW_TAG_formal_parameter DIE for each one.  */
18212   for (link = first_parm_type; link; )
18213     {
18214       dw_die_ref parm_die;
18215
18216       formal_type = TREE_VALUE (link);
18217       if (formal_type == void_type_node)
18218         break;
18219
18220       /* Output a (nameless) DIE to represent the formal parameter itself.  */
18221       if (!POINTER_BOUNDS_TYPE_P (formal_type))
18222         {
18223           parm_die = gen_formal_parameter_die (formal_type, NULL,
18224                                                true /* Emit name attribute.  */,
18225                                                context_die);
18226           if (TREE_CODE (function_or_method_type) == METHOD_TYPE
18227               && link == first_parm_type)
18228             {
18229               add_AT_flag (parm_die, DW_AT_artificial, 1);
18230               if (dwarf_version >= 3 || !dwarf_strict)
18231                 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
18232             }
18233           else if (arg && DECL_ARTIFICIAL (arg))
18234             add_AT_flag (parm_die, DW_AT_artificial, 1);
18235         }
18236
18237       link = TREE_CHAIN (link);
18238       if (arg)
18239         arg = DECL_CHAIN (arg);
18240     }
18241
18242   /* If this function type has an ellipsis, add a
18243      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
18244   if (formal_type != void_type_node)
18245     gen_unspecified_parameters_die (function_or_method_type, context_die);
18246
18247   /* Make our second (and final) pass over the list of formal parameter types
18248      and output DIEs to represent those types (as necessary).  */
18249   for (link = TYPE_ARG_TYPES (function_or_method_type);
18250        link && TREE_VALUE (link);
18251        link = TREE_CHAIN (link))
18252     gen_type_die (TREE_VALUE (link), context_die);
18253 }
18254
18255 /* We want to generate the DIE for TYPE so that we can generate the
18256    die for MEMBER, which has been defined; we will need to refer back
18257    to the member declaration nested within TYPE.  If we're trying to
18258    generate minimal debug info for TYPE, processing TYPE won't do the
18259    trick; we need to attach the member declaration by hand.  */
18260
18261 static void
18262 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
18263 {
18264   gen_type_die (type, context_die);
18265
18266   /* If we're trying to avoid duplicate debug info, we may not have
18267      emitted the member decl for this function.  Emit it now.  */
18268   if (TYPE_STUB_DECL (type)
18269       && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
18270       && ! lookup_decl_die (member))
18271     {
18272       dw_die_ref type_die;
18273       gcc_assert (!decl_ultimate_origin (member));
18274
18275       push_decl_scope (type);
18276       type_die = lookup_type_die_strip_naming_typedef (type);
18277       if (TREE_CODE (member) == FUNCTION_DECL)
18278         gen_subprogram_die (member, type_die);
18279       else if (TREE_CODE (member) == FIELD_DECL)
18280         {
18281           /* Ignore the nameless fields that are used to skip bits but handle
18282              C++ anonymous unions and structs.  */
18283           if (DECL_NAME (member) != NULL_TREE
18284               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
18285               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
18286             {
18287               gen_type_die (member_declared_type (member), type_die);
18288               gen_field_die (member, type_die);
18289             }
18290         }
18291       else
18292         gen_variable_die (member, NULL_TREE, type_die);
18293
18294       pop_decl_scope ();
18295     }
18296 }
18297 \f
18298 /* Forward declare these functions, because they are mutually recursive
18299   with their set_block_* pairing functions.  */
18300 static void set_decl_origin_self (tree);
18301 static void set_decl_abstract_flags (tree, vec<tree> &);
18302
18303 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
18304    given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
18305    that it points to the node itself, thus indicating that the node is its
18306    own (abstract) origin.  Additionally, if the BLOCK_ABSTRACT_ORIGIN for
18307    the given node is NULL, recursively descend the decl/block tree which
18308    it is the root of, and for each other ..._DECL or BLOCK node contained
18309    therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
18310    still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
18311    values to point to themselves.  */
18312
18313 static void
18314 set_block_origin_self (tree stmt)
18315 {
18316   if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
18317     {
18318       BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
18319
18320       {
18321         tree local_decl;
18322
18323         for (local_decl = BLOCK_VARS (stmt);
18324              local_decl != NULL_TREE;
18325              local_decl = DECL_CHAIN (local_decl))
18326           /* Do not recurse on nested functions since the inlining status
18327              of parent and child can be different as per the DWARF spec.  */
18328           if (TREE_CODE (local_decl) != FUNCTION_DECL
18329               && !DECL_EXTERNAL (local_decl))
18330             set_decl_origin_self (local_decl);
18331       }
18332
18333       {
18334         tree subblock;
18335
18336         for (subblock = BLOCK_SUBBLOCKS (stmt);
18337              subblock != NULL_TREE;
18338              subblock = BLOCK_CHAIN (subblock))
18339           set_block_origin_self (subblock);     /* Recurse.  */
18340       }
18341     }
18342 }
18343
18344 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
18345    the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
18346    node to so that it points to the node itself, thus indicating that the
18347    node represents its own (abstract) origin.  Additionally, if the
18348    DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
18349    the decl/block tree of which the given node is the root of, and for
18350    each other ..._DECL or BLOCK node contained therein whose
18351    DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
18352    set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
18353    point to themselves.  */
18354
18355 static void
18356 set_decl_origin_self (tree decl)
18357 {
18358   if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
18359     {
18360       DECL_ABSTRACT_ORIGIN (decl) = decl;
18361       if (TREE_CODE (decl) == FUNCTION_DECL)
18362         {
18363           tree arg;
18364
18365           for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
18366             DECL_ABSTRACT_ORIGIN (arg) = arg;
18367           if (DECL_INITIAL (decl) != NULL_TREE
18368               && DECL_INITIAL (decl) != error_mark_node)
18369             set_block_origin_self (DECL_INITIAL (decl));
18370         }
18371     }
18372 }
18373 \f
18374 /* Given a pointer to some BLOCK node, set the BLOCK_ABSTRACT flag to 1
18375    and if it wasn't 1 before, push it to abstract_vec vector.
18376    For all local decls and all local sub-blocks (recursively) do it
18377    too.  */
18378
18379 static void
18380 set_block_abstract_flags (tree stmt, vec<tree> &abstract_vec)
18381 {
18382   tree local_decl;
18383   tree subblock;
18384   unsigned int i;
18385
18386   if (!BLOCK_ABSTRACT (stmt))
18387     {
18388       abstract_vec.safe_push (stmt);
18389       BLOCK_ABSTRACT (stmt) = 1;
18390     }
18391
18392   for (local_decl = BLOCK_VARS (stmt);
18393        local_decl != NULL_TREE;
18394        local_decl = DECL_CHAIN (local_decl))
18395     if (! DECL_EXTERNAL (local_decl))
18396       set_decl_abstract_flags (local_decl, abstract_vec);
18397
18398   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
18399     {
18400       local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
18401       if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
18402           || TREE_CODE (local_decl) == PARM_DECL)
18403         set_decl_abstract_flags (local_decl, abstract_vec);
18404     }
18405
18406   for (subblock = BLOCK_SUBBLOCKS (stmt);
18407        subblock != NULL_TREE;
18408        subblock = BLOCK_CHAIN (subblock))
18409     set_block_abstract_flags (subblock, abstract_vec);
18410 }
18411
18412 /* Given a pointer to some ..._DECL node, set DECL_ABSTRACT_P flag on it
18413    to 1 and if it wasn't 1 before, push to abstract_vec vector.
18414    In the case where the decl is a FUNCTION_DECL also set the abstract
18415    flags for all of the parameters, local vars, local
18416    blocks and sub-blocks (recursively).  */
18417
18418 static void
18419 set_decl_abstract_flags (tree decl, vec<tree> &abstract_vec)
18420 {
18421   if (!DECL_ABSTRACT_P (decl))
18422     {
18423       abstract_vec.safe_push (decl);
18424       DECL_ABSTRACT_P (decl) = 1;
18425     }
18426
18427   if (TREE_CODE (decl) == FUNCTION_DECL)
18428     {
18429       tree arg;
18430
18431       for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
18432         if (!DECL_ABSTRACT_P (arg))
18433           {
18434             abstract_vec.safe_push (arg);
18435             DECL_ABSTRACT_P (arg) = 1;
18436           }
18437       if (DECL_INITIAL (decl) != NULL_TREE
18438           && DECL_INITIAL (decl) != error_mark_node)
18439         set_block_abstract_flags (DECL_INITIAL (decl), abstract_vec);
18440     }
18441 }
18442
18443 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18444    may later generate inlined and/or out-of-line instances of.
18445
18446    FIXME: In the early-dwarf world, this function, and most of the
18447           DECL_ABSTRACT code should be obsoleted.  The early DIE _is_
18448           the abstract instance.  All we would need to do is annotate
18449           the early DIE with the appropriate DW_AT_inline in late
18450           dwarf (perhaps in gen_inlined_subroutine_die).
18451
18452           However, we can't do this yet, because LTO streaming of DIEs
18453           has not been implemented yet.  */
18454
18455 static void
18456 dwarf2out_abstract_function (tree decl)
18457 {
18458   dw_die_ref old_die;
18459   tree save_fn;
18460   tree context;
18461   hash_table<decl_loc_hasher> *old_decl_loc_table;
18462   hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
18463   int old_call_site_count, old_tail_call_site_count;
18464   struct call_arg_loc_node *old_call_arg_locations;
18465
18466   /* Make sure we have the actual abstract inline, not a clone.  */
18467   decl = DECL_ORIGIN (decl);
18468
18469   old_die = lookup_decl_die (decl);
18470   if (old_die && get_AT (old_die, DW_AT_inline))
18471     /* We've already generated the abstract instance.  */
18472     return;
18473
18474   /* We can be called while recursively when seeing block defining inlined subroutine
18475      DIE.  Be sure to not clobber the outer location table nor use it or we would
18476      get locations in abstract instantces.  */
18477   old_decl_loc_table = decl_loc_table;
18478   decl_loc_table = NULL;
18479   old_cached_dw_loc_list_table = cached_dw_loc_list_table;
18480   cached_dw_loc_list_table = NULL;
18481   old_call_arg_locations = call_arg_locations;
18482   call_arg_locations = NULL;
18483   old_call_site_count = call_site_count;
18484   call_site_count = -1;
18485   old_tail_call_site_count = tail_call_site_count;
18486   tail_call_site_count = -1;
18487
18488   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18489      we don't get confused by DECL_ABSTRACT_P.  */
18490   if (debug_info_level > DINFO_LEVEL_TERSE)
18491     {
18492       context = decl_class_context (decl);
18493       if (context)
18494         gen_type_die_for_member
18495           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
18496     }
18497
18498   /* Pretend we've just finished compiling this function.  */
18499   save_fn = current_function_decl;
18500   current_function_decl = decl;
18501
18502   auto_vec<tree, 64> abstract_vec;
18503   set_decl_abstract_flags (decl, abstract_vec);
18504   dwarf2out_decl (decl);
18505   unsigned int i;
18506   tree t;
18507   FOR_EACH_VEC_ELT (abstract_vec, i, t)
18508     if (TREE_CODE (t) == BLOCK)
18509       BLOCK_ABSTRACT (t) = 0;
18510     else
18511       DECL_ABSTRACT_P (t) = 0;
18512
18513   current_function_decl = save_fn;
18514   decl_loc_table = old_decl_loc_table;
18515   cached_dw_loc_list_table = old_cached_dw_loc_list_table;
18516   call_arg_locations = old_call_arg_locations;
18517   call_site_count = old_call_site_count;
18518   tail_call_site_count = old_tail_call_site_count;
18519 }
18520
18521 /* Helper function of premark_used_types() which gets called through
18522    htab_traverse.
18523
18524    Marks the DIE of a given type in *SLOT as perennial, so it never gets
18525    marked as unused by prune_unused_types.  */
18526
18527 bool
18528 premark_used_types_helper (tree const &type, void *)
18529 {
18530   dw_die_ref die;
18531
18532   die = lookup_type_die (type);
18533   if (die != NULL)
18534     die->die_perennial_p = 1;
18535   return true;
18536 }
18537
18538 /* Helper function of premark_types_used_by_global_vars which gets called
18539    through htab_traverse.
18540
18541    Marks the DIE of a given type in *SLOT as perennial, so it never gets
18542    marked as unused by prune_unused_types. The DIE of the type is marked
18543    only if the global variable using the type will actually be emitted.  */
18544
18545 int
18546 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
18547                                           void *)
18548 {
18549   struct types_used_by_vars_entry *entry;
18550   dw_die_ref die;
18551
18552   entry = (struct types_used_by_vars_entry *) *slot;
18553   gcc_assert (entry->type != NULL
18554               && entry->var_decl != NULL);
18555   die = lookup_type_die (entry->type);
18556   if (die)
18557     {
18558       /* Ask cgraph if the global variable really is to be emitted.
18559          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
18560       varpool_node *node = varpool_node::get (entry->var_decl);
18561       if (node && node->definition)
18562         {
18563           die->die_perennial_p = 1;
18564           /* Keep the parent DIEs as well.  */
18565           while ((die = die->die_parent) && die->die_perennial_p == 0)
18566             die->die_perennial_p = 1;
18567         }
18568     }
18569   return 1;
18570 }
18571
18572 /* Mark all members of used_types_hash as perennial.  */
18573
18574 static void
18575 premark_used_types (struct function *fun)
18576 {
18577   if (fun && fun->used_types_hash)
18578     fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
18579 }
18580
18581 /* Mark all members of types_used_by_vars_entry as perennial.  */
18582
18583 static void
18584 premark_types_used_by_global_vars (void)
18585 {
18586   if (types_used_by_vars_hash)
18587     types_used_by_vars_hash
18588       ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
18589 }
18590
18591 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
18592    for CA_LOC call arg loc node.  */
18593
18594 static dw_die_ref
18595 gen_call_site_die (tree decl, dw_die_ref subr_die,
18596                    struct call_arg_loc_node *ca_loc)
18597 {
18598   dw_die_ref stmt_die = NULL, die;
18599   tree block = ca_loc->block;
18600
18601   while (block
18602          && block != DECL_INITIAL (decl)
18603          && TREE_CODE (block) == BLOCK)
18604     {
18605       stmt_die = BLOCK_DIE (block);
18606       if (stmt_die)
18607         break;
18608       block = BLOCK_SUPERCONTEXT (block);
18609     }
18610   if (stmt_die == NULL)
18611     stmt_die = subr_die;
18612   die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
18613   add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
18614   if (ca_loc->tail_call_p)
18615     add_AT_flag (die, DW_AT_GNU_tail_call, 1);
18616   if (ca_loc->symbol_ref)
18617     {
18618       dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
18619       if (tdie)
18620         add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
18621       else
18622         add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
18623     }
18624   return die;
18625 }
18626
18627 /* Generate a DIE to represent a declared function (either file-scope or
18628    block-local).  */
18629
18630 static void
18631 gen_subprogram_die (tree decl, dw_die_ref context_die)
18632 {
18633   tree origin = decl_ultimate_origin (decl);
18634   dw_die_ref subr_die;
18635   dw_die_ref old_die = lookup_decl_die (decl);
18636
18637   /* This function gets called multiple times for different stages of
18638      the debug process.  For example, for func() in this code:
18639
18640         namespace S
18641         {
18642           void func() { ... }
18643         }
18644
18645      ...we get called 4 times.  Twice in early debug and twice in
18646      late debug:
18647
18648      Early debug
18649      -----------
18650
18651        1. Once while generating func() within the namespace.  This is
18652           the declaration.  The declaration bit below is set, as the
18653           context is the namespace.
18654
18655           A new DIE will be generated with DW_AT_declaration set.
18656
18657        2. Once for func() itself.  This is the specification.  The
18658           declaration bit below is clear as the context is the CU.
18659
18660           We will use the cached DIE from (1) to create a new DIE with
18661           DW_AT_specification pointing to the declaration in (1).
18662
18663      Late debug via rest_of_handle_final()
18664      -------------------------------------
18665
18666        3. Once generating func() within the namespace.  This is also the
18667           declaration, as in (1), but this time we will early exit below
18668           as we have a cached DIE and a declaration needs no additional
18669           annotations (no locations), as the source declaration line
18670           info is enough.
18671
18672        4. Once for func() itself.  As in (2), this is the specification,
18673           but this time we will re-use the cached DIE, and just annotate
18674           it with the location information that should now be available.
18675
18676      For something without namespaces, but with abstract instances, we
18677      are also called a multiple times:
18678
18679         class Base
18680         {
18681         public:
18682           Base ();        // constructor declaration (1)
18683         };
18684
18685         Base::Base () { } // constructor specification (2)
18686
18687     Early debug
18688     -----------
18689
18690        1. Once for the Base() constructor by virtue of it being a
18691           member of the Base class.  This is done via
18692           rest_of_type_compilation.
18693
18694           This is a declaration, so a new DIE will be created with
18695           DW_AT_declaration.
18696
18697        2. Once for the Base() constructor definition, but this time
18698           while generating the abstract instance of the base
18699           constructor (__base_ctor) which is being generated via early
18700           debug of reachable functions.
18701
18702           Even though we have a cached version of the declaration (1),
18703           we will create a DW_AT_specification of the declaration DIE
18704           in (1).
18705
18706        3. Once for the __base_ctor itself, but this time, we generate
18707           an DW_AT_abstract_origin version of the DW_AT_specification in
18708           (2).
18709
18710     Late debug via rest_of_handle_final
18711     -----------------------------------
18712
18713        4. One final time for the __base_ctor (which will have a cached
18714           DIE with DW_AT_abstract_origin created in (3).  This time,
18715           we will just annotate the location information now
18716           available.
18717   */
18718   int declaration = (current_function_decl != decl
18719                      || class_or_namespace_scope_p (context_die));
18720
18721   premark_used_types (DECL_STRUCT_FUNCTION (decl));
18722
18723   /* Now that the C++ front end lazily declares artificial member fns, we
18724      might need to retrofit the declaration into its class.  */
18725   if (!declaration && !origin && !old_die
18726       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18727       && !class_or_namespace_scope_p (context_die)
18728       && debug_info_level > DINFO_LEVEL_TERSE)
18729     old_die = force_decl_die (decl);
18730
18731   /* An inlined instance, tag a new DIE with DW_AT_abstract_origin.  */
18732   if (origin != NULL)
18733     {
18734       gcc_assert (!declaration || local_scope_p (context_die));
18735
18736       /* Fixup die_parent for the abstract instance of a nested
18737          inline function.  */
18738       if (old_die && old_die->die_parent == NULL)
18739         add_child_die (context_die, old_die);
18740
18741       if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
18742         {
18743           /* If we have a DW_AT_abstract_origin we have a working
18744              cached version.  */
18745           subr_die = old_die;
18746         }
18747       else
18748         {
18749           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18750           add_abstract_origin_attribute (subr_die, origin);
18751           /*  This is where the actual code for a cloned function is.
18752               Let's emit linkage name attribute for it.  This helps
18753               debuggers to e.g, set breakpoints into
18754               constructors/destructors when the user asks "break
18755               K::K".  */
18756           add_linkage_name (subr_die, decl);
18757         }
18758     }
18759   /* A cached copy, possibly from early dwarf generation.  Reuse as
18760      much as possible.  */
18761   else if (old_die)
18762     {
18763       /* A declaration that has been previously dumped needs no
18764          additional information.  */
18765       if (declaration)
18766         return;
18767
18768       if (!get_AT_flag (old_die, DW_AT_declaration)
18769           /* We can have a normal definition following an inline one in the
18770              case of redefinition of GNU C extern inlines.
18771              It seems reasonable to use AT_specification in this case.  */
18772           && !get_AT (old_die, DW_AT_inline))
18773         {
18774           /* Detect and ignore this case, where we are trying to output
18775              something we have already output.  */
18776           if (get_AT (old_die, DW_AT_low_pc)
18777               || get_AT (old_die, DW_AT_ranges))
18778             return;
18779
18780           /* If we have no location information, this must be a
18781              partially generated DIE from early dwarf generation.
18782              Fall through and generate it.  */
18783         }
18784
18785       /* If the definition comes from the same place as the declaration,
18786          maybe use the old DIE.  We always want the DIE for this function
18787          that has the *_pc attributes to be under comp_unit_die so the
18788          debugger can find it.  We also need to do this for abstract
18789          instances of inlines, since the spec requires the out-of-line copy
18790          to have the same parent.  For local class methods, this doesn't
18791          apply; we just use the old DIE.  */
18792       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18793       struct dwarf_file_data * file_index = lookup_filename (s.file);
18794       if ((is_cu_die (old_die->die_parent)
18795            /* This condition fixes the inconsistency/ICE with the
18796               following Fortran test (or some derivative thereof) while
18797               building libgfortran:
18798
18799                  module some_m
18800                  contains
18801                     logical function funky (FLAG)
18802                       funky = .true.
18803                    end function
18804                  end module
18805            */
18806            || (old_die->die_parent
18807                && old_die->die_parent->die_tag == DW_TAG_module)
18808            || context_die == NULL)
18809            && (DECL_ARTIFICIAL (decl)
18810                || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18811                    && (get_AT_unsigned (old_die, DW_AT_decl_line)
18812                        == (unsigned) s.line))))
18813         {
18814           subr_die = old_die;
18815
18816           /* Clear out the declaration attribute, but leave the
18817              parameters so they can be augmented with location
18818              information later.  Unless this was a declaration, in
18819              which case, wipe out the nameless parameters and recreate
18820              them further down.  */
18821           if (remove_AT (subr_die, DW_AT_declaration))
18822             {
18823
18824               remove_AT (subr_die, DW_AT_object_pointer);
18825               remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18826             }
18827         }
18828       /* Make a specification pointing to the previously built
18829          declaration.  */
18830       else
18831         {
18832           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18833           add_AT_specification (subr_die, old_die);
18834           add_pubname (decl, subr_die);
18835           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18836             add_AT_file (subr_die, DW_AT_decl_file, file_index);
18837           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18838             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18839
18840           /* If the prototype had an 'auto' or 'decltype(auto)' return type,
18841              emit the real type on the definition die.  */
18842           if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
18843             {
18844               dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
18845               if (die == auto_die || die == decltype_auto_die)
18846                 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18847                                     TYPE_UNQUALIFIED, context_die);
18848             }
18849         }
18850     }
18851   /* Create a fresh DIE for anything else.  */
18852   else
18853     {
18854       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18855
18856       if (TREE_PUBLIC (decl))
18857         add_AT_flag (subr_die, DW_AT_external, 1);
18858
18859       add_name_and_src_coords_attributes (subr_die, decl);
18860       add_pubname (decl, subr_die);
18861       if (debug_info_level > DINFO_LEVEL_TERSE)
18862         {
18863           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18864           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18865                               TYPE_UNQUALIFIED, context_die);
18866         }
18867
18868       add_pure_or_virtual_attribute (subr_die, decl);
18869       if (DECL_ARTIFICIAL (decl))
18870         add_AT_flag (subr_die, DW_AT_artificial, 1);
18871
18872       if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
18873         add_AT_flag (subr_die, DW_AT_noreturn, 1);
18874
18875       add_accessibility_attribute (subr_die, decl);
18876     }
18877
18878   /* Unless we have an existing non-declaration DIE, equate the new
18879      DIE.  */
18880   if (!old_die || is_declaration_die (old_die))
18881     equate_decl_number_to_die (decl, subr_die);
18882
18883   if (declaration)
18884     {
18885       if (!old_die || !get_AT (old_die, DW_AT_inline))
18886         {
18887           add_AT_flag (subr_die, DW_AT_declaration, 1);
18888
18889           /* If this is an explicit function declaration then generate
18890              a DW_AT_explicit attribute.  */
18891           if (lang_hooks.decls.function_decl_explicit_p (decl)
18892               && (dwarf_version >= 3 || !dwarf_strict))
18893             add_AT_flag (subr_die, DW_AT_explicit, 1);
18894
18895           /* If this is a C++11 deleted special function member then generate
18896              a DW_AT_GNU_deleted attribute.  */
18897           if (lang_hooks.decls.function_decl_deleted_p (decl)
18898               && (! dwarf_strict))
18899             add_AT_flag (subr_die, DW_AT_GNU_deleted, 1);
18900         }
18901     }
18902   /* Tag abstract instances with DW_AT_inline.  */
18903   else if (DECL_ABSTRACT_P (decl))
18904     {
18905       if (DECL_DECLARED_INLINE_P (decl))
18906         {
18907           if (cgraph_function_possibly_inlined_p (decl))
18908             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18909           else
18910             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18911         }
18912       else
18913         {
18914           if (cgraph_function_possibly_inlined_p (decl))
18915             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18916           else
18917             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18918         }
18919
18920       if (DECL_DECLARED_INLINE_P (decl)
18921           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18922         add_AT_flag (subr_die, DW_AT_artificial, 1);
18923     }
18924   /* For non DECL_EXTERNALs, if range information is available, fill
18925      the DIE with it.  */
18926   else if (!DECL_EXTERNAL (decl) && !early_dwarf)
18927     {
18928       HOST_WIDE_INT cfa_fb_offset;
18929
18930       struct function *fun = DECL_STRUCT_FUNCTION (decl);
18931
18932       if (!flag_reorder_blocks_and_partition)
18933         {
18934           dw_fde_ref fde = fun->fde;
18935           if (fde->dw_fde_begin)
18936             {
18937               /* We have already generated the labels.  */
18938              add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18939                                  fde->dw_fde_end, false);
18940             }
18941           else
18942             {
18943               /* Create start/end labels and add the range.  */
18944               char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
18945               char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
18946               ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
18947                                            current_function_funcdef_no);
18948               ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
18949                                            current_function_funcdef_no);
18950              add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
18951                                  false);
18952             }
18953
18954 #if VMS_DEBUGGING_INFO
18955       /* HP OpenVMS Industry Standard 64: DWARF Extensions
18956          Section 2.3 Prologue and Epilogue Attributes:
18957          When a breakpoint is set on entry to a function, it is generally
18958          desirable for execution to be suspended, not on the very first
18959          instruction of the function, but rather at a point after the
18960          function's frame has been set up, after any language defined local
18961          declaration processing has been completed, and before execution of
18962          the first statement of the function begins. Debuggers generally
18963          cannot properly determine where this point is.  Similarly for a
18964          breakpoint set on exit from a function. The prologue and epilogue
18965          attributes allow a compiler to communicate the location(s) to use.  */
18966
18967       {
18968         if (fde->dw_fde_vms_end_prologue)
18969           add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18970             fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18971
18972         if (fde->dw_fde_vms_begin_epilogue)
18973           add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18974             fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18975       }
18976 #endif
18977
18978         }
18979       else
18980         {
18981           /* Generate pubnames entries for the split function code ranges.  */
18982           dw_fde_ref fde = fun->fde;
18983
18984           if (fde->dw_fde_second_begin)
18985             {
18986               if (dwarf_version >= 3 || !dwarf_strict)
18987                 {
18988                   /* We should use ranges for non-contiguous code section 
18989                      addresses.  Use the actual code range for the initial
18990                      section, since the HOT/COLD labels might precede an 
18991                      alignment offset.  */
18992                   bool range_list_added = false;
18993                   add_ranges_by_labels (subr_die, fde->dw_fde_begin,
18994                                         fde->dw_fde_end, &range_list_added,
18995                                         false);
18996                   add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
18997                                         fde->dw_fde_second_end,
18998                                        &range_list_added, false);
18999                   if (range_list_added)
19000                     add_ranges (NULL);
19001                 }
19002               else
19003                 {
19004                   /* There is no real support in DW2 for this .. so we make
19005                      a work-around.  First, emit the pub name for the segment
19006                      containing the function label.  Then make and emit a
19007                      simplified subprogram DIE for the second segment with the
19008                      name pre-fixed by __hot/cold_sect_of_.  We use the same
19009                      linkage name for the second die so that gdb will find both
19010                      sections when given "b foo".  */
19011                   const char *name = NULL;
19012                   tree decl_name = DECL_NAME (decl);
19013                   dw_die_ref seg_die;
19014
19015                   /* Do the 'primary' section.   */
19016                   add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
19017                                       fde->dw_fde_end, false);
19018
19019                   /* Build a minimal DIE for the secondary section.  */
19020                   seg_die = new_die (DW_TAG_subprogram,
19021                                      subr_die->die_parent, decl);
19022
19023                   if (TREE_PUBLIC (decl))
19024                     add_AT_flag (seg_die, DW_AT_external, 1);
19025
19026                   if (decl_name != NULL 
19027                       && IDENTIFIER_POINTER (decl_name) != NULL)
19028                     {
19029                       name = dwarf2_name (decl, 1);
19030                       if (! DECL_ARTIFICIAL (decl))
19031                         add_src_coords_attributes (seg_die, decl);
19032
19033                       add_linkage_name (seg_die, decl);
19034                     }
19035                   gcc_assert (name != NULL);
19036                   add_pure_or_virtual_attribute (seg_die, decl);
19037                   if (DECL_ARTIFICIAL (decl))
19038                     add_AT_flag (seg_die, DW_AT_artificial, 1);
19039
19040                   name = concat ("__second_sect_of_", name, NULL); 
19041                   add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
19042                                       fde->dw_fde_second_end, false);
19043                   add_name_attribute (seg_die, name);
19044                   if (want_pubnames ())
19045                     add_pubname_string (name, seg_die);
19046                 }
19047             }
19048           else
19049            add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
19050                                false);
19051         }
19052
19053       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
19054
19055       /* We define the "frame base" as the function's CFA.  This is more
19056          convenient for several reasons: (1) It's stable across the prologue
19057          and epilogue, which makes it better than just a frame pointer,
19058          (2) With dwarf3, there exists a one-byte encoding that allows us
19059          to reference the .debug_frame data by proxy, but failing that,
19060          (3) We can at least reuse the code inspection and interpretation
19061          code that determines the CFA position at various points in the
19062          function.  */
19063       if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
19064         {
19065           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
19066           add_AT_loc (subr_die, DW_AT_frame_base, op);
19067         }
19068       else
19069         {
19070           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
19071           if (list->dw_loc_next)
19072             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
19073           else
19074             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
19075         }
19076
19077       /* Compute a displacement from the "steady-state frame pointer" to
19078          the CFA.  The former is what all stack slots and argument slots
19079          will reference in the rtl; the latter is what we've told the
19080          debugger about.  We'll need to adjust all frame_base references
19081          by this displacement.  */
19082       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
19083
19084       if (fun->static_chain_decl)
19085         add_AT_location_description
19086           (subr_die, DW_AT_static_link,
19087            loc_list_from_tree (fun->static_chain_decl, 2, NULL));
19088     }
19089
19090   /* Generate child dies for template paramaters.  */
19091   if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
19092     gen_generic_params_dies (decl);
19093
19094   /* Now output descriptions of the arguments for this function. This gets
19095      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
19096      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
19097      `...' at the end of the formal parameter list.  In order to find out if
19098      there was a trailing ellipsis or not, we must instead look at the type
19099      associated with the FUNCTION_DECL.  This will be a node of type
19100      FUNCTION_TYPE. If the chain of type nodes hanging off of this
19101      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
19102      an ellipsis at the end.  */
19103
19104   /* In the case where we are describing a mere function declaration, all we
19105      need to do here (and all we *can* do here) is to describe the *types* of
19106      its formal parameters.  */
19107   if (debug_info_level <= DINFO_LEVEL_TERSE)
19108     ;
19109   else if (declaration)
19110     gen_formal_types_die (decl, subr_die);
19111   else
19112     {
19113       /* Generate DIEs to represent all known formal parameters.  */
19114       tree parm = DECL_ARGUMENTS (decl);
19115       tree generic_decl = early_dwarf
19116         ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
19117       tree generic_decl_parm = generic_decl
19118                                 ? DECL_ARGUMENTS (generic_decl)
19119                                 : NULL;
19120
19121       /* Now we want to walk the list of parameters of the function and
19122          emit their relevant DIEs.
19123
19124          We consider the case of DECL being an instance of a generic function
19125          as well as it being a normal function.
19126
19127          If DECL is an instance of a generic function we walk the
19128          parameters of the generic function declaration _and_ the parameters of
19129          DECL itself. This is useful because we want to emit specific DIEs for
19130          function parameter packs and those are declared as part of the
19131          generic function declaration. In that particular case,
19132          the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
19133          That DIE has children DIEs representing the set of arguments
19134          of the pack. Note that the set of pack arguments can be empty.
19135          In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
19136          children DIE.
19137
19138          Otherwise, we just consider the parameters of DECL.  */
19139       while (generic_decl_parm || parm)
19140         {
19141           if (generic_decl_parm
19142               && lang_hooks.function_parameter_pack_p (generic_decl_parm))
19143             gen_formal_parameter_pack_die (generic_decl_parm,
19144                                            parm, subr_die,
19145                                            &parm);
19146           else if (parm && !POINTER_BOUNDS_P (parm))
19147             {
19148               dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
19149
19150               if (parm == DECL_ARGUMENTS (decl)
19151                   && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
19152                   && parm_die
19153                   && (dwarf_version >= 3 || !dwarf_strict))
19154                 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
19155
19156               parm = DECL_CHAIN (parm);
19157             }
19158           else if (parm)
19159             parm = DECL_CHAIN (parm);
19160
19161           if (generic_decl_parm)
19162             generic_decl_parm = DECL_CHAIN (generic_decl_parm);
19163         }
19164
19165       /* Decide whether we need an unspecified_parameters DIE at the end.
19166          There are 2 more cases to do this for: 1) the ansi ... declaration -
19167          this is detectable when the end of the arg list is not a
19168          void_type_node 2) an unprototyped function declaration (not a
19169          definition).  This just means that we have no info about the
19170          parameters at all.  */
19171       if (prototype_p (TREE_TYPE (decl)))
19172         {
19173           /* This is the prototyped case, check for....  */
19174           if (stdarg_p (TREE_TYPE (decl)))
19175             gen_unspecified_parameters_die (decl, subr_die);
19176         }
19177       else if (DECL_INITIAL (decl) == NULL_TREE)
19178         gen_unspecified_parameters_die (decl, subr_die);
19179     }
19180
19181   if (subr_die != old_die)
19182     /* Add the calling convention attribute if requested.  */
19183     add_calling_convention_attribute (subr_die, decl);
19184
19185   /* Output Dwarf info for all of the stuff within the body of the function
19186      (if it has one - it may be just a declaration).
19187
19188      OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
19189      a function.  This BLOCK actually represents the outermost binding contour
19190      for the function, i.e. the contour in which the function's formal
19191      parameters and labels get declared. Curiously, it appears that the front
19192      end doesn't actually put the PARM_DECL nodes for the current function onto
19193      the BLOCK_VARS list for this outer scope, but are strung off of the
19194      DECL_ARGUMENTS list for the function instead.
19195
19196      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
19197      the LABEL_DECL nodes for the function however, and we output DWARF info
19198      for those in decls_for_scope.  Just within the `outer_scope' there will be
19199      a BLOCK node representing the function's outermost pair of curly braces,
19200      and any blocks used for the base and member initializers of a C++
19201      constructor function.  */
19202   tree outer_scope = DECL_INITIAL (decl);
19203   if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
19204     {
19205       int call_site_note_count = 0;
19206       int tail_call_site_note_count = 0;
19207
19208       /* Emit a DW_TAG_variable DIE for a named return value.  */
19209       if (DECL_NAME (DECL_RESULT (decl)))
19210         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
19211
19212       /* The first time through decls_for_scope we will generate the
19213          DIEs for the locals.  The second time, we fill in the
19214          location info.  */
19215       decls_for_scope (outer_scope, subr_die);
19216
19217       if (call_arg_locations && !dwarf_strict)
19218         {
19219           struct call_arg_loc_node *ca_loc;
19220           for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
19221             {
19222               dw_die_ref die = NULL;
19223               rtx tloc = NULL_RTX, tlocc = NULL_RTX;
19224               rtx arg, next_arg;
19225
19226               for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
19227                    arg; arg = next_arg)
19228                 {
19229                   dw_loc_descr_ref reg, val;
19230                   machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
19231                   dw_die_ref cdie, tdie = NULL;
19232
19233                   next_arg = XEXP (arg, 1);
19234                   if (REG_P (XEXP (XEXP (arg, 0), 0))
19235                       && next_arg
19236                       && MEM_P (XEXP (XEXP (next_arg, 0), 0))
19237                       && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
19238                       && REGNO (XEXP (XEXP (arg, 0), 0))
19239                          == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
19240                     next_arg = XEXP (next_arg, 1);
19241                   if (mode == VOIDmode)
19242                     {
19243                       mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
19244                       if (mode == VOIDmode)
19245                         mode = GET_MODE (XEXP (arg, 0));
19246                     }
19247                   if (mode == VOIDmode || mode == BLKmode)
19248                     continue;
19249                   if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
19250                     {
19251                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
19252                       tloc = XEXP (XEXP (arg, 0), 1);
19253                       continue;
19254                     }
19255                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
19256                            && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
19257                     {
19258                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
19259                       tlocc = XEXP (XEXP (arg, 0), 1);
19260                       continue;
19261                     }
19262                   reg = NULL;
19263                   if (REG_P (XEXP (XEXP (arg, 0), 0)))
19264                     reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
19265                                               VAR_INIT_STATUS_INITIALIZED);
19266                   else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
19267                     {
19268                       rtx mem = XEXP (XEXP (arg, 0), 0);
19269                       reg = mem_loc_descriptor (XEXP (mem, 0),
19270                                                 get_address_mode (mem),
19271                                                 GET_MODE (mem),
19272                                                 VAR_INIT_STATUS_INITIALIZED);
19273                     }
19274                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
19275                            == DEBUG_PARAMETER_REF)
19276                     {
19277                       tree tdecl
19278                         = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
19279                       tdie = lookup_decl_die (tdecl);
19280                       if (tdie == NULL)
19281                         continue;
19282                     }
19283                   else
19284                     continue;
19285                   if (reg == NULL
19286                       && GET_CODE (XEXP (XEXP (arg, 0), 0))
19287                          != DEBUG_PARAMETER_REF)
19288                     continue;
19289                   val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
19290                                             VOIDmode,
19291                                             VAR_INIT_STATUS_INITIALIZED);
19292                   if (val == NULL)
19293                     continue;
19294                   if (die == NULL)
19295                     die = gen_call_site_die (decl, subr_die, ca_loc);
19296                   cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
19297                                   NULL_TREE);
19298                   if (reg != NULL)
19299                     add_AT_loc (cdie, DW_AT_location, reg);
19300                   else if (tdie != NULL)
19301                     add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
19302                   add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
19303                   if (next_arg != XEXP (arg, 1))
19304                     {
19305                       mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
19306                       if (mode == VOIDmode)
19307                         mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
19308                       val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
19309                                                             0), 1),
19310                                                 mode, VOIDmode,
19311                                                 VAR_INIT_STATUS_INITIALIZED);
19312                       if (val != NULL)
19313                         add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
19314                     }
19315                 }
19316               if (die == NULL
19317                   && (ca_loc->symbol_ref || tloc))
19318                 die = gen_call_site_die (decl, subr_die, ca_loc);
19319               if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
19320                 {
19321                   dw_loc_descr_ref tval = NULL;
19322
19323                   if (tloc != NULL_RTX)
19324                     tval = mem_loc_descriptor (tloc,
19325                                                GET_MODE (tloc) == VOIDmode
19326                                                ? Pmode : GET_MODE (tloc),
19327                                                VOIDmode,
19328                                                VAR_INIT_STATUS_INITIALIZED);
19329                   if (tval)
19330                     add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
19331                   else if (tlocc != NULL_RTX)
19332                     {
19333                       tval = mem_loc_descriptor (tlocc,
19334                                                  GET_MODE (tlocc) == VOIDmode
19335                                                  ? Pmode : GET_MODE (tlocc),
19336                                                  VOIDmode,
19337                                                  VAR_INIT_STATUS_INITIALIZED);
19338                       if (tval)
19339                         add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
19340                                     tval);
19341                     }
19342                 }
19343               if (die != NULL)
19344                 {
19345                   call_site_note_count++;
19346                   if (ca_loc->tail_call_p)
19347                     tail_call_site_note_count++;
19348                 }
19349             }
19350         }
19351       call_arg_locations = NULL;
19352       call_arg_loc_last = NULL;
19353       if (tail_call_site_count >= 0
19354           && tail_call_site_count == tail_call_site_note_count
19355           && !dwarf_strict)
19356         {
19357           if (call_site_count >= 0
19358               && call_site_count == call_site_note_count)
19359             add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
19360           else
19361             add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
19362         }
19363       call_site_count = -1;
19364       tail_call_site_count = -1;
19365     }
19366 }
19367
19368 /* Returns a hash value for X (which really is a die_struct).  */
19369
19370 hashval_t
19371 block_die_hasher::hash (die_struct *d)
19372 {
19373   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
19374 }
19375
19376 /* Return nonzero if decl_id and die_parent of die_struct X is the same
19377    as decl_id and die_parent of die_struct Y.  */
19378
19379 bool
19380 block_die_hasher::equal (die_struct *x, die_struct *y)
19381 {
19382   return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
19383 }
19384
19385 /* Return TRUE if DECL, which may have been previously generated as
19386    OLD_DIE, is a candidate for a DW_AT_specification.  DECLARATION is
19387    true if decl (or its origin) is either an extern declaration or a
19388    class/namespace scoped declaration.
19389
19390    The declare_in_namespace support causes us to get two DIEs for one
19391    variable, both of which are declarations.  We want to avoid
19392    considering one to be a specification, so we must test for
19393    DECLARATION and DW_AT_declaration.  */
19394 static inline bool
19395 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
19396 {
19397   return (old_die && TREE_STATIC (decl) && !declaration
19398           && get_AT_flag (old_die, DW_AT_declaration) == 1);
19399 }
19400
19401 /* Return true if DECL is a local static.  */
19402
19403 static inline bool
19404 local_function_static (tree decl)
19405 {
19406   gcc_assert (TREE_CODE (decl) == VAR_DECL);
19407   return TREE_STATIC (decl)
19408     && DECL_CONTEXT (decl)
19409     && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
19410 }
19411
19412 /* Generate a DIE to represent a declared data object.
19413    Either DECL or ORIGIN must be non-null.  */
19414
19415 static void
19416 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
19417 {
19418   HOST_WIDE_INT off = 0;
19419   tree com_decl;
19420   tree decl_or_origin = decl ? decl : origin;
19421   tree ultimate_origin;
19422   dw_die_ref var_die;
19423   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
19424   dw_die_ref origin_die = NULL;
19425   bool declaration = (DECL_EXTERNAL (decl_or_origin)
19426                       || class_or_namespace_scope_p (context_die));
19427   bool specialization_p = false;
19428
19429   ultimate_origin = decl_ultimate_origin (decl_or_origin);
19430   if (decl || ultimate_origin)
19431     origin = ultimate_origin;
19432   com_decl = fortran_common (decl_or_origin, &off);
19433
19434   /* Symbol in common gets emitted as a child of the common block, in the form
19435      of a data member.  */
19436   if (com_decl)
19437     {
19438       dw_die_ref com_die;
19439       dw_loc_list_ref loc;
19440       die_node com_die_arg;
19441
19442       var_die = lookup_decl_die (decl_or_origin);
19443       if (var_die)
19444         {
19445           if (get_AT (var_die, DW_AT_location) == NULL)
19446             {
19447               loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
19448               if (loc)
19449                 {
19450                   if (off)
19451                     {
19452                       /* Optimize the common case.  */
19453                       if (single_element_loc_list_p (loc)
19454                           && loc->expr->dw_loc_opc == DW_OP_addr
19455                           && loc->expr->dw_loc_next == NULL
19456                           && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
19457                              == SYMBOL_REF)
19458                         {
19459                           rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
19460                           loc->expr->dw_loc_oprnd1.v.val_addr
19461                             = plus_constant (GET_MODE (x), x , off);
19462                         }
19463                       else
19464                         loc_list_plus_const (loc, off);
19465                     }
19466                   add_AT_location_description (var_die, DW_AT_location, loc);
19467                   remove_AT (var_die, DW_AT_declaration);
19468                 }
19469             }
19470           return;
19471         }
19472
19473       if (common_block_die_table == NULL)
19474         common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
19475
19476       com_die_arg.decl_id = DECL_UID (com_decl);
19477       com_die_arg.die_parent = context_die;
19478       com_die = common_block_die_table->find (&com_die_arg);
19479       loc = loc_list_from_tree (com_decl, 2, NULL);
19480       if (com_die == NULL)
19481         {
19482           const char *cnam
19483             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
19484           die_node **slot;
19485
19486           com_die = new_die (DW_TAG_common_block, context_die, decl);
19487           add_name_and_src_coords_attributes (com_die, com_decl);
19488           if (loc)
19489             {
19490               add_AT_location_description (com_die, DW_AT_location, loc);
19491               /* Avoid sharing the same loc descriptor between
19492                  DW_TAG_common_block and DW_TAG_variable.  */
19493               loc = loc_list_from_tree (com_decl, 2, NULL);
19494             }
19495           else if (DECL_EXTERNAL (decl))
19496             add_AT_flag (com_die, DW_AT_declaration, 1);
19497           if (want_pubnames ())
19498             add_pubname_string (cnam, com_die); /* ??? needed? */
19499           com_die->decl_id = DECL_UID (com_decl);
19500           slot = common_block_die_table->find_slot (com_die, INSERT);
19501           *slot = com_die;
19502         }
19503       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
19504         {
19505           add_AT_location_description (com_die, DW_AT_location, loc);
19506           loc = loc_list_from_tree (com_decl, 2, NULL);
19507           remove_AT (com_die, DW_AT_declaration);
19508         }
19509       var_die = new_die (DW_TAG_variable, com_die, decl);
19510       add_name_and_src_coords_attributes (var_die, decl);
19511       add_type_attribute (var_die, TREE_TYPE (decl), decl_quals (decl),
19512                           context_die);
19513       add_AT_flag (var_die, DW_AT_external, 1);
19514       if (loc)
19515         {
19516           if (off)
19517             {
19518               /* Optimize the common case.  */
19519               if (single_element_loc_list_p (loc)
19520                   && loc->expr->dw_loc_opc == DW_OP_addr
19521                   && loc->expr->dw_loc_next == NULL
19522                   && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
19523                 {
19524                   rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
19525                   loc->expr->dw_loc_oprnd1.v.val_addr
19526                     = plus_constant (GET_MODE (x), x, off);
19527                 }
19528               else
19529                 loc_list_plus_const (loc, off);
19530             }
19531           add_AT_location_description (var_die, DW_AT_location, loc);
19532         }
19533       else if (DECL_EXTERNAL (decl))
19534         add_AT_flag (var_die, DW_AT_declaration, 1);
19535       equate_decl_number_to_die (decl, var_die);
19536       return;
19537     }
19538
19539   if (old_die)
19540     {
19541       if (declaration)
19542         {
19543           /* A declaration that has been previously dumped, needs no
19544              further annotations, since it doesn't need location on
19545              the second pass.  */
19546           return;
19547         }
19548       else if (decl_will_get_specification_p (old_die, decl, declaration)
19549                && !get_AT (old_die, DW_AT_specification))
19550         {
19551           /* Fall-thru so we can make a new variable die along with a
19552              DW_AT_specification.  */
19553         }
19554       else if (origin && old_die->die_parent != context_die)
19555         {
19556           /* If we will be creating an inlined instance, we need a
19557              new DIE that will get annotated with
19558              DW_AT_abstract_origin.  Clear things so we can get a
19559              new DIE.  */
19560           gcc_assert (!DECL_ABSTRACT_P (decl));
19561           old_die = NULL;
19562         }
19563       else
19564         {
19565           /* If a DIE was dumped early, it still needs location info.
19566              Skip to where we fill the location bits.  */
19567           var_die = old_die;
19568           goto gen_variable_die_location;
19569         }
19570     }
19571
19572   /* For static data members, the declaration in the class is supposed
19573      to have DW_TAG_member tag; the specification should still be
19574      DW_TAG_variable referencing the DW_TAG_member DIE.  */
19575   if (declaration && class_scope_p (context_die))
19576     var_die = new_die (DW_TAG_member, context_die, decl);
19577   else
19578     var_die = new_die (DW_TAG_variable, context_die, decl);
19579
19580   if (origin != NULL)
19581     origin_die = add_abstract_origin_attribute (var_die, origin);
19582
19583   /* Loop unrolling can create multiple blocks that refer to the same
19584      static variable, so we must test for the DW_AT_declaration flag.
19585
19586      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
19587      copy decls and set the DECL_ABSTRACT_P flag on them instead of
19588      sharing them.
19589
19590      ??? Duplicated blocks have been rewritten to use .debug_ranges.  */
19591   else if (decl_will_get_specification_p (old_die, decl, declaration))
19592     {
19593       /* This is a definition of a C++ class level static.  */
19594       add_AT_specification (var_die, old_die);
19595       specialization_p = true;
19596       if (DECL_NAME (decl))
19597         {
19598           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19599           struct dwarf_file_data * file_index = lookup_filename (s.file);
19600
19601           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19602             add_AT_file (var_die, DW_AT_decl_file, file_index);
19603
19604           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19605             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19606
19607           if (old_die->die_tag == DW_TAG_member)
19608             add_linkage_name (var_die, decl);
19609         }
19610     }
19611   else
19612     add_name_and_src_coords_attributes (var_die, decl);
19613
19614   if ((origin == NULL && !specialization_p)
19615       || (origin != NULL
19616           && !DECL_ABSTRACT_P (decl_or_origin)
19617           && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19618                                        decl_function_context
19619                                                         (decl_or_origin))))
19620     {
19621       tree type = TREE_TYPE (decl_or_origin);
19622
19623       if (decl_by_reference_p (decl_or_origin))
19624         add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19625                             context_die);
19626       else
19627         add_type_attribute (var_die, type, decl_quals (decl_or_origin),
19628                             context_die);
19629     }
19630
19631   if (origin == NULL && !specialization_p)
19632     {
19633       if (TREE_PUBLIC (decl))
19634         add_AT_flag (var_die, DW_AT_external, 1);
19635
19636       if (DECL_ARTIFICIAL (decl))
19637         add_AT_flag (var_die, DW_AT_artificial, 1);
19638
19639       add_accessibility_attribute (var_die, decl);
19640     }
19641
19642   if (declaration)
19643     add_AT_flag (var_die, DW_AT_declaration, 1);
19644
19645   if (decl && (DECL_ABSTRACT_P (decl)
19646                || !old_die || is_declaration_die (old_die)))
19647     equate_decl_number_to_die (decl, var_die);
19648
19649  gen_variable_die_location:
19650   if (! declaration
19651       && (! DECL_ABSTRACT_P (decl_or_origin)
19652           /* Local static vars are shared between all clones/inlines,
19653              so emit DW_AT_location on the abstract DIE if DECL_RTL is
19654              already set.  */
19655           || (TREE_CODE (decl_or_origin) == VAR_DECL
19656               && TREE_STATIC (decl_or_origin)
19657               && DECL_RTL_SET_P (decl_or_origin)))
19658       /* When abstract origin already has DW_AT_location attribute, no need
19659          to add it again.  */
19660       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19661     {
19662       if (early_dwarf)
19663         add_pubname (decl_or_origin, var_die);
19664       else
19665         add_location_or_const_value_attribute (var_die, decl_or_origin,
19666                                                decl == NULL, DW_AT_location);
19667     }
19668   else
19669     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19670 }
19671
19672 /* Generate a DIE to represent a named constant.  */
19673
19674 static void
19675 gen_const_die (tree decl, dw_die_ref context_die)
19676 {
19677   dw_die_ref const_die;
19678   tree type = TREE_TYPE (decl);
19679
19680   const_die = lookup_decl_die (decl);
19681   if (const_die)
19682     return;
19683
19684   const_die = new_die (DW_TAG_constant, context_die, decl);
19685   equate_decl_number_to_die (decl, const_die);
19686   add_name_and_src_coords_attributes (const_die, decl);
19687   add_type_attribute (const_die, type, TYPE_QUAL_CONST, context_die);
19688   if (TREE_PUBLIC (decl))
19689     add_AT_flag (const_die, DW_AT_external, 1);
19690   if (DECL_ARTIFICIAL (decl))
19691     add_AT_flag (const_die, DW_AT_artificial, 1);
19692   tree_add_const_value_attribute_for_decl (const_die, decl);
19693 }
19694
19695 /* Generate a DIE to represent a label identifier.  */
19696
19697 static void
19698 gen_label_die (tree decl, dw_die_ref context_die)
19699 {
19700   tree origin = decl_ultimate_origin (decl);
19701   dw_die_ref lbl_die = lookup_decl_die (decl);
19702   rtx insn;
19703   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19704
19705   if (!lbl_die)
19706     {
19707       lbl_die = new_die (DW_TAG_label, context_die, decl);
19708       equate_decl_number_to_die (decl, lbl_die);
19709
19710       if (origin != NULL)
19711         add_abstract_origin_attribute (lbl_die, origin);
19712       else
19713         add_name_and_src_coords_attributes (lbl_die, decl);
19714     }
19715
19716   if (DECL_ABSTRACT_P (decl))
19717     equate_decl_number_to_die (decl, lbl_die);
19718   else
19719     {
19720       insn = DECL_RTL_IF_SET (decl);
19721
19722       /* Deleted labels are programmer specified labels which have been
19723          eliminated because of various optimizations.  We still emit them
19724          here so that it is possible to put breakpoints on them.  */
19725       if (insn
19726           && (LABEL_P (insn)
19727               || ((NOTE_P (insn)
19728                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19729         {
19730           /* When optimization is enabled (via -O) some parts of the compiler
19731              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19732              represent source-level labels which were explicitly declared by
19733              the user.  This really shouldn't be happening though, so catch
19734              it if it ever does happen.  */
19735           gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
19736
19737           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19738           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19739         }
19740       else if (insn
19741                && NOTE_P (insn)
19742                && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
19743                && CODE_LABEL_NUMBER (insn) != -1)
19744         {
19745           ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
19746           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19747         }
19748     }
19749 }
19750
19751 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
19752    attributes to the DIE for a block STMT, to describe where the inlined
19753    function was called from.  This is similar to add_src_coords_attributes.  */
19754
19755 static inline void
19756 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19757 {
19758   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19759
19760   if (dwarf_version >= 3 || !dwarf_strict)
19761     {
19762       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19763       add_AT_unsigned (die, DW_AT_call_line, s.line);
19764     }
19765 }
19766
19767
19768 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19769    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
19770
19771 static inline void
19772 add_high_low_attributes (tree stmt, dw_die_ref die)
19773 {
19774   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19775
19776   if (BLOCK_FRAGMENT_CHAIN (stmt)
19777       && (dwarf_version >= 3 || !dwarf_strict))
19778     {
19779       tree chain, superblock = NULL_TREE;
19780       dw_die_ref pdie;
19781       dw_attr_node *attr = NULL;
19782
19783       if (inlined_function_outer_scope_p (stmt))
19784         {
19785           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19786                                        BLOCK_NUMBER (stmt));
19787           add_AT_lbl_id (die, DW_AT_entry_pc, label);
19788         }
19789
19790       /* Optimize duplicate .debug_ranges lists or even tails of
19791          lists.  If this BLOCK has same ranges as its supercontext,
19792          lookup DW_AT_ranges attribute in the supercontext (and
19793          recursively so), verify that the ranges_table contains the
19794          right values and use it instead of adding a new .debug_range.  */
19795       for (chain = stmt, pdie = die;
19796            BLOCK_SAME_RANGE (chain);
19797            chain = BLOCK_SUPERCONTEXT (chain))
19798         {
19799           dw_attr_node *new_attr;
19800
19801           pdie = pdie->die_parent;
19802           if (pdie == NULL)
19803             break;
19804           if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
19805             break;
19806           new_attr = get_AT (pdie, DW_AT_ranges);
19807           if (new_attr == NULL
19808               || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
19809             break;
19810           attr = new_attr;
19811           superblock = BLOCK_SUPERCONTEXT (chain);
19812         }
19813       if (attr != NULL
19814           && (ranges_table[attr->dw_attr_val.v.val_offset
19815                            / 2 / DWARF2_ADDR_SIZE].num
19816               == BLOCK_NUMBER (superblock))
19817           && BLOCK_FRAGMENT_CHAIN (superblock))
19818         {
19819           unsigned long off = attr->dw_attr_val.v.val_offset
19820                               / 2 / DWARF2_ADDR_SIZE;
19821           unsigned long supercnt = 0, thiscnt = 0;
19822           for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
19823                chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19824             {
19825               ++supercnt;
19826               gcc_checking_assert (ranges_table[off + supercnt].num
19827                                    == BLOCK_NUMBER (chain));
19828             }
19829           gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
19830           for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
19831                chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19832             ++thiscnt;
19833           gcc_assert (supercnt >= thiscnt);
19834           add_AT_range_list (die, DW_AT_ranges,
19835                              ((off + supercnt - thiscnt)
19836                               * 2 * DWARF2_ADDR_SIZE),
19837                              false);
19838           return;
19839         }
19840
19841       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
19842
19843       chain = BLOCK_FRAGMENT_CHAIN (stmt);
19844       do
19845         {
19846           add_ranges (chain);
19847           chain = BLOCK_FRAGMENT_CHAIN (chain);
19848         }
19849       while (chain);
19850       add_ranges (NULL);
19851     }
19852   else
19853     {
19854       char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
19855       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19856                                    BLOCK_NUMBER (stmt));
19857       ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
19858                                    BLOCK_NUMBER (stmt));
19859       add_AT_low_high_pc (die, label, label_high, false);
19860     }
19861 }
19862
19863 /* Generate a DIE for a lexical block.  */
19864
19865 static void
19866 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
19867 {
19868   dw_die_ref old_die = BLOCK_DIE (stmt);
19869   dw_die_ref stmt_die = NULL;
19870   if (!old_die)
19871     {
19872       stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19873       BLOCK_DIE (stmt) = stmt_die;
19874     }
19875
19876   if (BLOCK_ABSTRACT (stmt))
19877     {
19878       if (old_die)
19879         {
19880 #ifdef ENABLE_CHECKING
19881           /* This must have been generated early and it won't even
19882              need location information since it's a DW_AT_inline
19883              function.  */
19884           for (dw_die_ref c = context_die; c; c = c->die_parent)
19885             if (c->die_tag == DW_TAG_inlined_subroutine
19886                 || c->die_tag == DW_TAG_subprogram)
19887               {
19888                 gcc_assert (get_AT (c, DW_AT_inline));
19889                 break;
19890               }
19891 #endif
19892           return;
19893         }
19894     }
19895   else if (BLOCK_ABSTRACT_ORIGIN (stmt))
19896     {
19897       /* If this is an inlined instance, create a new lexical die for
19898          anything below to attach DW_AT_abstract_origin to.  */
19899       if (old_die)
19900         {
19901           stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19902           BLOCK_DIE (stmt) = stmt_die;
19903           old_die = NULL;
19904         }
19905     }
19906
19907   if (old_die)
19908     stmt_die = old_die;
19909
19910   /* A non abstract block whose blocks have already been reordered
19911      should have the instruction range for this block.  If so, set the
19912      high/low attributes.  */
19913   if (!early_dwarf && !BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19914     {
19915       gcc_assert (stmt_die);
19916       add_high_low_attributes (stmt, stmt_die);
19917     }
19918
19919   decls_for_scope (stmt, stmt_die);
19920 }
19921
19922 /* Generate a DIE for an inlined subprogram.  */
19923
19924 static void
19925 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
19926 {
19927   tree decl;
19928
19929   /* The instance of function that is effectively being inlined shall not
19930      be abstract.  */
19931   gcc_assert (! BLOCK_ABSTRACT (stmt));
19932
19933   decl = block_ultimate_origin (stmt);
19934
19935   /* Make sure any inlined functions are known to be inlineable.  */
19936   gcc_checking_assert (DECL_ABSTRACT_P (decl)
19937                        || cgraph_function_possibly_inlined_p (decl));
19938
19939   /* Emit info for the abstract instance first, if we haven't yet.  We
19940      must emit this even if the block is abstract, otherwise when we
19941      emit the block below (or elsewhere), we may end up trying to emit
19942      a die whose origin die hasn't been emitted, and crashing.  */
19943   dwarf2out_abstract_function (decl);
19944
19945   if (! BLOCK_ABSTRACT (stmt))
19946     {
19947       dw_die_ref subr_die
19948         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19949
19950       if (call_arg_locations)
19951         BLOCK_DIE (stmt) = subr_die;
19952       add_abstract_origin_attribute (subr_die, decl);
19953       if (TREE_ASM_WRITTEN (stmt))
19954         add_high_low_attributes (stmt, subr_die);
19955       add_call_src_coords_attributes (stmt, subr_die);
19956
19957       decls_for_scope (stmt, subr_die);
19958     }
19959 }
19960
19961 /* Generate a DIE for a field in a record, or structure.  */
19962
19963 static void
19964 gen_field_die (tree decl, dw_die_ref context_die)
19965 {
19966   dw_die_ref decl_die;
19967
19968   if (TREE_TYPE (decl) == error_mark_node)
19969     return;
19970
19971   decl_die = new_die (DW_TAG_member, context_die, decl);
19972   add_name_and_src_coords_attributes (decl_die, decl);
19973   add_type_attribute (decl_die, member_declared_type (decl),
19974                       decl_quals (decl), context_die);
19975
19976   if (DECL_BIT_FIELD_TYPE (decl))
19977     {
19978       add_byte_size_attribute (decl_die, decl);
19979       add_bit_size_attribute (decl_die, decl);
19980       add_bit_offset_attribute (decl_die, decl);
19981     }
19982
19983   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19984     add_data_member_location_attribute (decl_die, decl);
19985
19986   if (DECL_ARTIFICIAL (decl))
19987     add_AT_flag (decl_die, DW_AT_artificial, 1);
19988
19989   add_accessibility_attribute (decl_die, decl);
19990
19991   /* Equate decl number to die, so that we can look up this decl later on.  */
19992   equate_decl_number_to_die (decl, decl_die);
19993 }
19994
19995 #if 0
19996 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19997    Use modified_type_die instead.
19998    We keep this code here just in case these types of DIEs may be needed to
19999    represent certain things in other languages (e.g. Pascal) someday.  */
20000
20001 static void
20002 gen_pointer_type_die (tree type, dw_die_ref context_die)
20003 {
20004   dw_die_ref ptr_die
20005     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
20006
20007   equate_type_number_to_die (type, ptr_die);
20008   add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
20009                       context_die);
20010   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
20011 }
20012
20013 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
20014    Use modified_type_die instead.
20015    We keep this code here just in case these types of DIEs may be needed to
20016    represent certain things in other languages (e.g. Pascal) someday.  */
20017
20018 static void
20019 gen_reference_type_die (tree type, dw_die_ref context_die)
20020 {
20021   dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
20022
20023   if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
20024     ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
20025   else
20026     ref_die = new_die (DW_TAG_reference_type, scope_die, type);
20027
20028   equate_type_number_to_die (type, ref_die);
20029   add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
20030                       context_die);
20031   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
20032 }
20033 #endif
20034
20035 /* Generate a DIE for a pointer to a member type.  */
20036
20037 static void
20038 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
20039 {
20040   dw_die_ref ptr_die
20041     = new_die (DW_TAG_ptr_to_member_type,
20042                scope_die_for (type, context_die), type);
20043
20044   equate_type_number_to_die (type, ptr_die);
20045   add_AT_die_ref (ptr_die, DW_AT_containing_type,
20046                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
20047   add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
20048                       context_die);
20049 }
20050
20051 static char *producer_string;
20052
20053 /* Return a heap allocated producer string including command line options
20054    if -grecord-gcc-switches.  */
20055
20056 static char *
20057 gen_producer_string (void)
20058 {
20059   size_t j;
20060   auto_vec<const char *> switches;
20061   const char *language_string = lang_hooks.name;
20062   char *producer, *tail;
20063   const char *p;
20064   size_t len = dwarf_record_gcc_switches ? 0 : 3;
20065   size_t plen = strlen (language_string) + 1 + strlen (version_string);
20066
20067   for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
20068     switch (save_decoded_options[j].opt_index)
20069       {
20070       case OPT_o:
20071       case OPT_d:
20072       case OPT_dumpbase:
20073       case OPT_dumpdir:
20074       case OPT_auxbase:
20075       case OPT_auxbase_strip:
20076       case OPT_quiet:
20077       case OPT_version:
20078       case OPT_v:
20079       case OPT_w:
20080       case OPT_L:
20081       case OPT_D:
20082       case OPT_I:
20083       case OPT_U:
20084       case OPT_SPECIAL_unknown:
20085       case OPT_SPECIAL_ignore:
20086       case OPT_SPECIAL_program_name:
20087       case OPT_SPECIAL_input_file:
20088       case OPT_grecord_gcc_switches:
20089       case OPT_gno_record_gcc_switches:
20090       case OPT__output_pch_:
20091       case OPT_fdiagnostics_show_location_:
20092       case OPT_fdiagnostics_show_option:
20093       case OPT_fdiagnostics_show_caret:
20094       case OPT_fdiagnostics_color_:
20095       case OPT_fverbose_asm:
20096       case OPT____:
20097       case OPT__sysroot_:
20098       case OPT_nostdinc:
20099       case OPT_nostdinc__:
20100       case OPT_fpreprocessed:
20101       case OPT_fltrans_output_list_:
20102       case OPT_fresolution_:
20103         /* Ignore these.  */
20104         continue;
20105       default:
20106         if (cl_options[save_decoded_options[j].opt_index].flags
20107             & CL_NO_DWARF_RECORD)
20108           continue;
20109         gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
20110                              == '-');
20111         switch (save_decoded_options[j].canonical_option[0][1])
20112           {
20113           case 'M':
20114           case 'i':
20115           case 'W':
20116             continue;
20117           case 'f':
20118             if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
20119                          "dump", 4) == 0)
20120               continue;
20121             break;
20122           default:
20123             break;
20124           }
20125         switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
20126         len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
20127         break;
20128       }
20129
20130   producer = XNEWVEC (char, plen + 1 + len + 1);
20131   tail = producer;
20132   sprintf (tail, "%s %s", language_string, version_string);
20133   tail += plen;
20134
20135   FOR_EACH_VEC_ELT (switches, j, p)
20136     {
20137       len = strlen (p);
20138       *tail = ' ';
20139       memcpy (tail + 1, p, len);
20140       tail += len + 1;
20141     }
20142
20143   *tail = '\0';
20144   return producer;
20145 }
20146
20147 /* Given a C and/or C++ language/version string return the "highest".
20148    C++ is assumed to be "higher" than C in this case.  Used for merging
20149    LTO translation unit languages.  */
20150 static const char *
20151 highest_c_language (const char *lang1, const char *lang2)
20152 {
20153   if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
20154     return "GNU C++14";
20155   if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
20156     return "GNU C++11";
20157   if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
20158     return "GNU C++98";
20159
20160   if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
20161     return "GNU C11";
20162   if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
20163     return "GNU C99";
20164   if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
20165     return "GNU C89";
20166
20167   gcc_unreachable ();
20168 }
20169
20170
20171 /* Generate the DIE for the compilation unit.  */
20172
20173 static dw_die_ref
20174 gen_compile_unit_die (const char *filename)
20175 {
20176   dw_die_ref die;
20177   const char *language_string = lang_hooks.name;
20178   int language;
20179
20180   die = new_die (DW_TAG_compile_unit, NULL, NULL);
20181
20182   if (filename)
20183     {
20184       add_name_attribute (die, filename);
20185       /* Don't add cwd for <built-in>.  */
20186       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
20187         add_comp_dir_attribute (die);
20188     }
20189
20190   add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
20191
20192   /* If our producer is LTO try to figure out a common language to use
20193      from the global list of translation units.  */
20194   if (strcmp (language_string, "GNU GIMPLE") == 0)
20195     {
20196       unsigned i;
20197       tree t;
20198       const char *common_lang = NULL;
20199
20200       FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
20201         {
20202           if (!TRANSLATION_UNIT_LANGUAGE (t))
20203             continue;
20204           if (!common_lang)
20205             common_lang = TRANSLATION_UNIT_LANGUAGE (t);
20206           else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
20207             ;
20208           else if (strncmp (common_lang, "GNU C", 5) == 0
20209                     && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
20210             /* Mixing C and C++ is ok, use C++ in that case.  */
20211             common_lang = highest_c_language (common_lang,
20212                                               TRANSLATION_UNIT_LANGUAGE (t));
20213           else
20214             {
20215               /* Fall back to C.  */
20216               common_lang = NULL;
20217               break;
20218             }
20219         }
20220
20221       if (common_lang)
20222         language_string = common_lang;
20223     }
20224
20225   language = DW_LANG_C;
20226   if (strncmp (language_string, "GNU C", 5) == 0
20227       && ISDIGIT (language_string[5]))
20228     {
20229       language = DW_LANG_C89;
20230       if (dwarf_version >= 3 || !dwarf_strict)
20231         {
20232           if (strcmp (language_string, "GNU C89") != 0)
20233             language = DW_LANG_C99;
20234
20235           if (dwarf_version >= 5 /* || !dwarf_strict */)
20236             if (strcmp (language_string, "GNU C11") == 0)
20237               language = DW_LANG_C11;
20238         }
20239     }
20240   else if (strncmp (language_string, "GNU C++", 7) == 0)
20241     {
20242       language = DW_LANG_C_plus_plus;
20243       if (dwarf_version >= 5 /* || !dwarf_strict */)
20244         {
20245           if (strcmp (language_string, "GNU C++11") == 0)
20246             language = DW_LANG_C_plus_plus_11;
20247           else if (strcmp (language_string, "GNU C++14") == 0)
20248             language = DW_LANG_C_plus_plus_14;
20249         }
20250     }
20251   else if (strcmp (language_string, "GNU F77") == 0)
20252     language = DW_LANG_Fortran77;
20253   else if (strcmp (language_string, "GNU Pascal") == 0)
20254     language = DW_LANG_Pascal83;
20255   else if (dwarf_version >= 3 || !dwarf_strict)
20256     {
20257       if (strcmp (language_string, "GNU Ada") == 0)
20258         language = DW_LANG_Ada95;
20259       else if (strncmp (language_string, "GNU Fortran", 11) == 0)
20260         {
20261           language = DW_LANG_Fortran95;
20262           if (dwarf_version >= 5 /* || !dwarf_strict */)
20263             {
20264               if (strcmp (language_string, "GNU Fortran2003") == 0)
20265                 language = DW_LANG_Fortran03;
20266               else if (strcmp (language_string, "GNU Fortran2008") == 0)
20267                 language = DW_LANG_Fortran08;
20268             }
20269         }
20270       else if (strcmp (language_string, "GNU Java") == 0)
20271         language = DW_LANG_Java;
20272       else if (strcmp (language_string, "GNU Objective-C") == 0)
20273         language = DW_LANG_ObjC;
20274       else if (strcmp (language_string, "GNU Objective-C++") == 0)
20275         language = DW_LANG_ObjC_plus_plus;
20276       else if (dwarf_version >= 5 || !dwarf_strict)
20277         {
20278           if (strcmp (language_string, "GNU Go") == 0)
20279             language = DW_LANG_Go;
20280         }
20281     }
20282   /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works.  */
20283   else if (strncmp (language_string, "GNU Fortran", 11) == 0)
20284     language = DW_LANG_Fortran90;
20285
20286   add_AT_unsigned (die, DW_AT_language, language);
20287
20288   switch (language)
20289     {
20290     case DW_LANG_Fortran77:
20291     case DW_LANG_Fortran90:
20292     case DW_LANG_Fortran95:
20293     case DW_LANG_Fortran03:
20294     case DW_LANG_Fortran08:
20295       /* Fortran has case insensitive identifiers and the front-end
20296          lowercases everything.  */
20297       add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
20298       break;
20299     default:
20300       /* The default DW_ID_case_sensitive doesn't need to be specified.  */
20301       break;
20302     }
20303   return die;
20304 }
20305
20306 /* Generate the DIE for a base class.  */
20307
20308 static void
20309 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
20310 {
20311   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
20312
20313   add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, context_die);
20314   add_data_member_location_attribute (die, binfo);
20315
20316   if (BINFO_VIRTUAL_P (binfo))
20317     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20318
20319   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
20320      children, otherwise the default is DW_ACCESS_public.  In DWARF2
20321      the default has always been DW_ACCESS_private.  */
20322   if (access == access_public_node)
20323     {
20324       if (dwarf_version == 2
20325           || context_die->die_tag == DW_TAG_class_type)
20326       add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
20327     }
20328   else if (access == access_protected_node)
20329     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
20330   else if (dwarf_version > 2
20331            && context_die->die_tag != DW_TAG_class_type)
20332     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
20333 }
20334
20335 /* Generate a DIE for a class member.  */
20336
20337 static void
20338 gen_member_die (tree type, dw_die_ref context_die)
20339 {
20340   tree member;
20341   tree binfo = TYPE_BINFO (type);
20342   dw_die_ref child;
20343
20344   /* If this is not an incomplete type, output descriptions of each of its
20345      members. Note that as we output the DIEs necessary to represent the
20346      members of this record or union type, we will also be trying to output
20347      DIEs to represent the *types* of those members. However the `type'
20348      function (above) will specifically avoid generating type DIEs for member
20349      types *within* the list of member DIEs for this (containing) type except
20350      for those types (of members) which are explicitly marked as also being
20351      members of this (containing) type themselves.  The g++ front- end can
20352      force any given type to be treated as a member of some other (containing)
20353      type by setting the TYPE_CONTEXT of the given (member) type to point to
20354      the TREE node representing the appropriate (containing) type.  */
20355
20356   /* First output info about the base classes.  */
20357   if (binfo)
20358     {
20359       vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
20360       int i;
20361       tree base;
20362
20363       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
20364         gen_inheritance_die (base,
20365                              (accesses ? (*accesses)[i] : access_public_node),
20366                              context_die);
20367     }
20368
20369   /* Now output info about the data members and type members.  */
20370   for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
20371     {
20372       /* If we thought we were generating minimal debug info for TYPE
20373          and then changed our minds, some of the member declarations
20374          may have already been defined.  Don't define them again, but
20375          do put them in the right order.  */
20376
20377       child = lookup_decl_die (member);
20378       if (child)
20379         splice_child_die (context_die, child);
20380       else
20381         gen_decl_die (member, NULL, context_die);
20382     }
20383
20384   /* We do not keep type methods in type variants.  */
20385   gcc_assert (TYPE_MAIN_VARIANT (type) == type);
20386   /* Now output info about the function members (if any).  */
20387   if (TYPE_METHODS (type) != error_mark_node)
20388     for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
20389       {
20390         /* Don't include clones in the member list.  */
20391         if (DECL_ABSTRACT_ORIGIN (member))
20392           continue;
20393         /* Nor constructors for anonymous classes.  */
20394         if (DECL_ARTIFICIAL (member)
20395             && dwarf2_name (member, 0) == NULL)
20396           continue;
20397
20398         child = lookup_decl_die (member);
20399         if (child)
20400           splice_child_die (context_die, child);
20401         else
20402           gen_decl_die (member, NULL, context_die);
20403       }
20404 }
20405
20406 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
20407    is set, we pretend that the type was never defined, so we only get the
20408    member DIEs needed by later specification DIEs.  */
20409
20410 static void
20411 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
20412                                 enum debug_info_usage usage)
20413 {
20414   if (TREE_ASM_WRITTEN (type))
20415     {
20416       /* Fill in the bound of variable-length fields in late dwarf if
20417          still incomplete.  */
20418       if (!early_dwarf && variably_modified_type_p (type, NULL))
20419         for (tree member = TYPE_FIELDS (type);
20420              member;
20421              member = DECL_CHAIN (member))
20422           fill_variable_array_bounds (TREE_TYPE (member));
20423       return;
20424     }
20425
20426   dw_die_ref type_die = lookup_type_die (type);
20427   dw_die_ref scope_die = 0;
20428   int nested = 0;
20429   int complete = (TYPE_SIZE (type)
20430                   && (! TYPE_STUB_DECL (type)
20431                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
20432   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
20433   complete = complete && should_emit_struct_debug (type, usage);
20434
20435   if (type_die && ! complete)
20436     return;
20437
20438   if (TYPE_CONTEXT (type) != NULL_TREE
20439       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20440           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
20441     nested = 1;
20442
20443   scope_die = scope_die_for (type, context_die);
20444
20445   /* Generate child dies for template paramaters.  */
20446   if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
20447     schedule_generic_params_dies_gen (type);
20448
20449   if (! type_die || (nested && is_cu_die (scope_die)))
20450     /* First occurrence of type or toplevel definition of nested class.  */
20451     {
20452       dw_die_ref old_die = type_die;
20453
20454       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
20455                           ? record_type_tag (type) : DW_TAG_union_type,
20456                           scope_die, type);
20457       equate_type_number_to_die (type, type_die);
20458       if (old_die)
20459         add_AT_specification (type_die, old_die);
20460       else
20461         add_name_attribute (type_die, type_tag (type));
20462     }
20463   else
20464     remove_AT (type_die, DW_AT_declaration);
20465
20466   /* If this type has been completed, then give it a byte_size attribute and
20467      then give a list of members.  */
20468   if (complete && !ns_decl)
20469     {
20470       /* Prevent infinite recursion in cases where the type of some member of
20471          this type is expressed in terms of this type itself.  */
20472       TREE_ASM_WRITTEN (type) = 1;
20473       add_byte_size_attribute (type_die, type);
20474       if (TYPE_STUB_DECL (type) != NULL_TREE)
20475         {
20476           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
20477           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
20478         }
20479
20480       /* If the first reference to this type was as the return type of an
20481          inline function, then it may not have a parent.  Fix this now.  */
20482       if (type_die->die_parent == NULL)
20483         add_child_die (scope_die, type_die);
20484
20485       push_decl_scope (type);
20486       gen_member_die (type, type_die);
20487       pop_decl_scope ();
20488
20489       add_gnat_descriptive_type_attribute (type_die, type, context_die);
20490       if (TYPE_ARTIFICIAL (type))
20491         add_AT_flag (type_die, DW_AT_artificial, 1);
20492
20493       /* GNU extension: Record what type our vtable lives in.  */
20494       if (TYPE_VFIELD (type))
20495         {
20496           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
20497
20498           gen_type_die (vtype, context_die);
20499           add_AT_die_ref (type_die, DW_AT_containing_type,
20500                           lookup_type_die (vtype));
20501         }
20502     }
20503   else
20504     {
20505       add_AT_flag (type_die, DW_AT_declaration, 1);
20506
20507       /* We don't need to do this for function-local types.  */
20508       if (TYPE_STUB_DECL (type)
20509           && ! decl_function_context (TYPE_STUB_DECL (type)))
20510         vec_safe_push (incomplete_types, type);
20511     }
20512
20513   if (get_AT (type_die, DW_AT_name))
20514     add_pubtype (type, type_die);
20515 }
20516
20517 /* Generate a DIE for a subroutine _type_.  */
20518
20519 static void
20520 gen_subroutine_type_die (tree type, dw_die_ref context_die)
20521 {
20522   tree return_type = TREE_TYPE (type);
20523   dw_die_ref subr_die
20524     = new_die (DW_TAG_subroutine_type,
20525                scope_die_for (type, context_die), type);
20526
20527   equate_type_number_to_die (type, subr_die);
20528   add_prototyped_attribute (subr_die, type);
20529   add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, context_die);
20530   gen_formal_types_die (type, subr_die);
20531
20532   if (get_AT (subr_die, DW_AT_name))
20533     add_pubtype (type, subr_die);
20534 }
20535
20536 /* Generate a DIE for a type definition.  */
20537
20538 static void
20539 gen_typedef_die (tree decl, dw_die_ref context_die)
20540 {
20541   dw_die_ref type_die;
20542   tree origin;
20543
20544   if (TREE_ASM_WRITTEN (decl))
20545     {
20546       if (DECL_ORIGINAL_TYPE (decl))
20547         fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
20548       return;
20549     }
20550
20551   TREE_ASM_WRITTEN (decl) = 1;
20552   type_die = new_die (DW_TAG_typedef, context_die, decl);
20553   origin = decl_ultimate_origin (decl);
20554   if (origin != NULL)
20555     add_abstract_origin_attribute (type_die, origin);
20556   else
20557     {
20558       tree type;
20559
20560       add_name_and_src_coords_attributes (type_die, decl);
20561       if (DECL_ORIGINAL_TYPE (decl))
20562         {
20563           type = DECL_ORIGINAL_TYPE (decl);
20564
20565           if (type == error_mark_node)
20566             return;
20567
20568           gcc_assert (type != TREE_TYPE (decl));
20569           equate_type_number_to_die (TREE_TYPE (decl), type_die);
20570         }
20571       else
20572         {
20573           type = TREE_TYPE (decl);
20574
20575           if (type == error_mark_node)
20576             return;
20577
20578           if (is_naming_typedef_decl (TYPE_NAME (type)))
20579             {
20580               /* Here, we are in the case of decl being a typedef naming
20581                  an anonymous type, e.g:
20582                      typedef struct {...} foo;
20583                  In that case TREE_TYPE (decl) is not a typedef variant
20584                  type and TYPE_NAME of the anonymous type is set to the
20585                  TYPE_DECL of the typedef. This construct is emitted by
20586                  the C++ FE.
20587
20588                  TYPE is the anonymous struct named by the typedef
20589                  DECL. As we need the DW_AT_type attribute of the
20590                  DW_TAG_typedef to point to the DIE of TYPE, let's
20591                  generate that DIE right away. add_type_attribute
20592                  called below will then pick (via lookup_type_die) that
20593                  anonymous struct DIE.  */
20594               if (!TREE_ASM_WRITTEN (type))
20595                 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
20596
20597               /* This is a GNU Extension.  We are adding a
20598                  DW_AT_linkage_name attribute to the DIE of the
20599                  anonymous struct TYPE.  The value of that attribute
20600                  is the name of the typedef decl naming the anonymous
20601                  struct.  This greatly eases the work of consumers of
20602                  this debug info.  */
20603               add_linkage_attr (lookup_type_die (type), decl);
20604             }
20605         }
20606
20607       add_type_attribute (type_die, type, decl_quals (decl), context_die);
20608
20609       if (is_naming_typedef_decl (decl))
20610         /* We want that all subsequent calls to lookup_type_die with
20611            TYPE in argument yield the DW_TAG_typedef we have just
20612            created.  */
20613         equate_type_number_to_die (type, type_die);
20614
20615       add_accessibility_attribute (type_die, decl);
20616     }
20617
20618   if (DECL_ABSTRACT_P (decl))
20619     equate_decl_number_to_die (decl, type_die);
20620
20621   if (get_AT (type_die, DW_AT_name))
20622     add_pubtype (decl, type_die);
20623 }
20624
20625 /* Generate a DIE for a struct, class, enum or union type.  */
20626
20627 static void
20628 gen_tagged_type_die (tree type,
20629                      dw_die_ref context_die,
20630                      enum debug_info_usage usage)
20631 {
20632   int need_pop;
20633
20634   if (type == NULL_TREE
20635       || !is_tagged_type (type))
20636     return;
20637
20638   if (TREE_ASM_WRITTEN (type))
20639     need_pop = 0;
20640   /* If this is a nested type whose containing class hasn't been written
20641      out yet, writing it out will cover this one, too.  This does not apply
20642      to instantiations of member class templates; they need to be added to
20643      the containing class as they are generated.  FIXME: This hurts the
20644      idea of combining type decls from multiple TUs, since we can't predict
20645      what set of template instantiations we'll get.  */
20646   else if (TYPE_CONTEXT (type)
20647       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20648       && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
20649     {
20650       gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
20651
20652       if (TREE_ASM_WRITTEN (type))
20653         return;
20654
20655       /* If that failed, attach ourselves to the stub.  */
20656       push_decl_scope (TYPE_CONTEXT (type));
20657       context_die = lookup_type_die (TYPE_CONTEXT (type));
20658       need_pop = 1;
20659     }
20660   else if (TYPE_CONTEXT (type) != NULL_TREE
20661            && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
20662     {
20663       /* If this type is local to a function that hasn't been written
20664          out yet, use a NULL context for now; it will be fixed up in
20665          decls_for_scope.  */
20666       context_die = lookup_decl_die (TYPE_CONTEXT (type));
20667       /* A declaration DIE doesn't count; nested types need to go in the
20668          specification.  */
20669       if (context_die && is_declaration_die (context_die))
20670         context_die = NULL;
20671       need_pop = 0;
20672     }
20673   else
20674     {
20675       context_die = declare_in_namespace (type, context_die);
20676       need_pop = 0;
20677     }
20678
20679   if (TREE_CODE (type) == ENUMERAL_TYPE)
20680     {
20681       /* This might have been written out by the call to
20682          declare_in_namespace.  */
20683       if (!TREE_ASM_WRITTEN (type))
20684         gen_enumeration_type_die (type, context_die);
20685     }
20686   else
20687     gen_struct_or_union_type_die (type, context_die, usage);
20688
20689   if (need_pop)
20690     pop_decl_scope ();
20691
20692   /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
20693      it up if it is ever completed.  gen_*_type_die will set it for us
20694      when appropriate.  */
20695 }
20696
20697 /* Generate a type description DIE.  */
20698
20699 static void
20700 gen_type_die_with_usage (tree type, dw_die_ref context_die,
20701                          enum debug_info_usage usage)
20702 {
20703   struct array_descr_info info;
20704
20705   if (type == NULL_TREE || type == error_mark_node)
20706     return;
20707
20708 #ifdef ENABLE_CHECKING
20709   if (type)
20710      verify_type (type);
20711 #endif
20712
20713   if (TYPE_NAME (type) != NULL_TREE
20714       && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20715       && is_redundant_typedef (TYPE_NAME (type))
20716       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
20717     /* The DECL of this type is a typedef we don't want to emit debug
20718        info for but we want debug info for its underlying typedef.
20719        This can happen for e.g, the injected-class-name of a C++
20720        type.  */
20721     type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
20722
20723   /* If TYPE is a typedef type variant, let's generate debug info
20724      for the parent typedef which TYPE is a type of.  */
20725   if (typedef_variant_p (type))
20726     {
20727       if (TREE_ASM_WRITTEN (type))
20728         return;
20729
20730       /* Prevent broken recursion; we can't hand off to the same type.  */
20731       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
20732
20733       /* Give typedefs the right scope.  */
20734       context_die = scope_die_for (type, context_die);
20735
20736       TREE_ASM_WRITTEN (type) = 1;
20737
20738       gen_decl_die (TYPE_NAME (type), NULL, context_die);
20739       return;
20740     }
20741
20742   /* If type is an anonymous tagged type named by a typedef, let's
20743      generate debug info for the typedef.  */
20744   if (is_naming_typedef_decl (TYPE_NAME (type)))
20745     {
20746       /* Use the DIE of the containing namespace as the parent DIE of
20747          the type description DIE we want to generate.  */
20748       if (DECL_CONTEXT (TYPE_NAME (type))
20749           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20750         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20751       
20752       gen_decl_die (TYPE_NAME (type), NULL, context_die);
20753       return;
20754     }
20755
20756   /* We are going to output a DIE to represent the unqualified version
20757      of this type (i.e. without any const or volatile qualifiers) so
20758      get the main variant (i.e. the unqualified version) of this type
20759      now.  (Vectors are special because the debugging info is in the
20760      cloned type itself).  */
20761   if (TREE_CODE (type) != VECTOR_TYPE)
20762     type = type_main_variant (type);
20763
20764   /* If this is an array type with hidden descriptor, handle it first.  */
20765   if (!TREE_ASM_WRITTEN (type)
20766       && lang_hooks.types.get_array_descr_info)
20767     {
20768       memset (&info, 0, sizeof (info));
20769       if (lang_hooks.types.get_array_descr_info (type, &info))
20770         {
20771           gen_descr_array_type_die (type, &info, context_die);
20772           TREE_ASM_WRITTEN (type) = 1;
20773           return;
20774         }
20775     }
20776
20777   if (TREE_ASM_WRITTEN (type))
20778     {
20779       /* Variable-length types may be incomplete even if
20780          TREE_ASM_WRITTEN.  For such types, fall through to
20781          gen_array_type_die() and possibly fill in
20782          DW_AT_{upper,lower}_bound attributes.  */
20783       if ((TREE_CODE (type) != ARRAY_TYPE
20784            && TREE_CODE (type) != RECORD_TYPE
20785            && TREE_CODE (type) != UNION_TYPE
20786            && TREE_CODE (type) != QUAL_UNION_TYPE)
20787           || !variably_modified_type_p (type, NULL))
20788         return;
20789     }
20790
20791   switch (TREE_CODE (type))
20792     {
20793     case ERROR_MARK:
20794       break;
20795
20796     case POINTER_TYPE:
20797     case REFERENCE_TYPE:
20798       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
20799          ensures that the gen_type_die recursion will terminate even if the
20800          type is recursive.  Recursive types are possible in Ada.  */
20801       /* ??? We could perhaps do this for all types before the switch
20802          statement.  */
20803       TREE_ASM_WRITTEN (type) = 1;
20804
20805       /* For these types, all that is required is that we output a DIE (or a
20806          set of DIEs) to represent the "basis" type.  */
20807       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20808                                 DINFO_USAGE_IND_USE);
20809       break;
20810
20811     case OFFSET_TYPE:
20812       /* This code is used for C++ pointer-to-data-member types.
20813          Output a description of the relevant class type.  */
20814       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
20815                                         DINFO_USAGE_IND_USE);
20816
20817       /* Output a description of the type of the object pointed to.  */
20818       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20819                                         DINFO_USAGE_IND_USE);
20820
20821       /* Now output a DIE to represent this pointer-to-data-member type
20822          itself.  */
20823       gen_ptr_to_mbr_type_die (type, context_die);
20824       break;
20825
20826     case FUNCTION_TYPE:
20827       /* Force out return type (in case it wasn't forced out already).  */
20828       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20829                                         DINFO_USAGE_DIR_USE);
20830       gen_subroutine_type_die (type, context_die);
20831       break;
20832
20833     case METHOD_TYPE:
20834       /* Force out return type (in case it wasn't forced out already).  */
20835       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20836                                         DINFO_USAGE_DIR_USE);
20837       gen_subroutine_type_die (type, context_die);
20838       break;
20839
20840     case ARRAY_TYPE:
20841     case VECTOR_TYPE:
20842       gen_array_type_die (type, context_die);
20843       break;
20844
20845     case ENUMERAL_TYPE:
20846     case RECORD_TYPE:
20847     case UNION_TYPE:
20848     case QUAL_UNION_TYPE:
20849       gen_tagged_type_die (type, context_die, usage);
20850       return;
20851
20852     case VOID_TYPE:
20853     case INTEGER_TYPE:
20854     case REAL_TYPE:
20855     case FIXED_POINT_TYPE:
20856     case COMPLEX_TYPE:
20857     case BOOLEAN_TYPE:
20858     case POINTER_BOUNDS_TYPE:
20859       /* No DIEs needed for fundamental types.  */
20860       break;
20861
20862     case NULLPTR_TYPE:
20863     case LANG_TYPE:
20864       /* Just use DW_TAG_unspecified_type.  */
20865       {
20866         dw_die_ref type_die = lookup_type_die (type);
20867         if (type_die == NULL)
20868           {
20869             tree name = TYPE_IDENTIFIER (type);
20870             type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
20871                                 type);
20872             add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20873             equate_type_number_to_die (type, type_die);
20874           }
20875       }
20876       break;
20877
20878     default:
20879       if (is_cxx_auto (type))
20880         {
20881           tree name = TYPE_IDENTIFIER (type);
20882           dw_die_ref *die = (name == get_identifier ("auto")
20883                              ? &auto_die : &decltype_auto_die);
20884           if (!*die)
20885             {
20886               *die = new_die (DW_TAG_unspecified_type,
20887                               comp_unit_die (), NULL_TREE);
20888               add_name_attribute (*die, IDENTIFIER_POINTER (name));
20889             }
20890           equate_type_number_to_die (type, *die);
20891           break;
20892         }
20893       gcc_unreachable ();
20894     }
20895
20896   TREE_ASM_WRITTEN (type) = 1;
20897 }
20898
20899 static void
20900 gen_type_die (tree type, dw_die_ref context_die)
20901 {
20902   if (type != error_mark_node)
20903     {
20904       gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20905 #ifdef ENABLE_CHECKING
20906       dw_die_ref die = lookup_type_die (type);
20907       if (die)
20908         check_die (die);
20909 #endif
20910     }
20911 }
20912
20913 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20914    things which are local to the given block.  */
20915
20916 static void
20917 gen_block_die (tree stmt, dw_die_ref context_die)
20918 {
20919   int must_output_die = 0;
20920   bool inlined_func;
20921
20922   /* Ignore blocks that are NULL.  */
20923   if (stmt == NULL_TREE)
20924     return;
20925
20926   inlined_func = inlined_function_outer_scope_p (stmt);
20927
20928   /* If the block is one fragment of a non-contiguous block, do not
20929      process the variables, since they will have been done by the
20930      origin block.  Do process subblocks.  */
20931   if (BLOCK_FRAGMENT_ORIGIN (stmt))
20932     {
20933       tree sub;
20934
20935       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20936         gen_block_die (sub, context_die);
20937
20938       return;
20939     }
20940
20941   /* Determine if we need to output any Dwarf DIEs at all to represent this
20942      block.  */
20943   if (inlined_func)
20944     /* The outer scopes for inlinings *must* always be represented.  We
20945        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
20946     must_output_die = 1;
20947   else
20948     {
20949       /* Determine if this block directly contains any "significant"
20950          local declarations which we will need to output DIEs for.  */
20951       if (debug_info_level > DINFO_LEVEL_TERSE)
20952         /* We are not in terse mode so *any* local declaration counts
20953            as being a "significant" one.  */
20954         must_output_die = ((BLOCK_VARS (stmt) != NULL
20955                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20956                            && (TREE_USED (stmt)
20957                                || TREE_ASM_WRITTEN (stmt)
20958                                || BLOCK_ABSTRACT (stmt)));
20959       else if ((TREE_USED (stmt)
20960                 || TREE_ASM_WRITTEN (stmt)
20961                 || BLOCK_ABSTRACT (stmt))
20962                && !dwarf2out_ignore_block (stmt))
20963         must_output_die = 1;
20964     }
20965
20966   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20967      DIE for any block which contains no significant local declarations at
20968      all.  Rather, in such cases we just call `decls_for_scope' so that any
20969      needed Dwarf info for any sub-blocks will get properly generated. Note
20970      that in terse mode, our definition of what constitutes a "significant"
20971      local declaration gets restricted to include only inlined function
20972      instances and local (nested) function definitions.  */
20973   if (must_output_die)
20974     {
20975       if (inlined_func)
20976         {
20977           /* If STMT block is abstract, that means we have been called
20978              indirectly from dwarf2out_abstract_function.
20979              That function rightfully marks the descendent blocks (of
20980              the abstract function it is dealing with) as being abstract,
20981              precisely to prevent us from emitting any
20982              DW_TAG_inlined_subroutine DIE as a descendent
20983              of an abstract function instance. So in that case, we should
20984              not call gen_inlined_subroutine_die.
20985
20986              Later though, when cgraph asks dwarf2out to emit info
20987              for the concrete instance of the function decl into which
20988              the concrete instance of STMT got inlined, the later will lead
20989              to the generation of a DW_TAG_inlined_subroutine DIE.  */
20990           if (! BLOCK_ABSTRACT (stmt))
20991             gen_inlined_subroutine_die (stmt, context_die);
20992         }
20993       else
20994         gen_lexical_block_die (stmt, context_die);
20995     }
20996   else
20997     decls_for_scope (stmt, context_die);
20998 }
20999
21000 /* Process variable DECL (or variable with origin ORIGIN) within
21001    block STMT and add it to CONTEXT_DIE.  */
21002 static void
21003 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
21004 {
21005   dw_die_ref die;
21006   tree decl_or_origin = decl ? decl : origin;
21007
21008   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
21009     die = lookup_decl_die (decl_or_origin);
21010   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
21011            && TYPE_DECL_IS_STUB (decl_or_origin))
21012     die = lookup_type_die (TREE_TYPE (decl_or_origin));
21013   else
21014     die = NULL;
21015
21016   if (die != NULL && die->die_parent == NULL)
21017     add_child_die (context_die, die);
21018   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
21019     {
21020       if (early_dwarf)
21021         dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
21022                                              stmt, context_die);
21023     }
21024   else
21025     gen_decl_die (decl, origin, context_die);
21026 }
21027
21028 /* Generate all of the decls declared within a given scope and (recursively)
21029    all of its sub-blocks.  */
21030
21031 static void
21032 decls_for_scope (tree stmt, dw_die_ref context_die)
21033 {
21034   tree decl;
21035   unsigned int i;
21036   tree subblocks;
21037
21038   /* Ignore NULL blocks.  */
21039   if (stmt == NULL_TREE)
21040     return;
21041
21042   /* Output the DIEs to represent all of the data objects and typedefs
21043      declared directly within this block but not within any nested
21044      sub-blocks.  Also, nested function and tag DIEs have been
21045      generated with a parent of NULL; fix that up now.  We don't
21046      have to do this if we're at -g1.  */
21047   if (debug_info_level > DINFO_LEVEL_TERSE)
21048     {
21049       for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
21050         process_scope_var (stmt, decl, NULL_TREE, context_die);
21051       for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
21052         process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
21053                            context_die);
21054     }
21055
21056   /* Even if we're at -g1, we need to process the subblocks in order to get
21057      inlined call information.  */
21058
21059   /* Output the DIEs to represent all sub-blocks (and the items declared
21060      therein) of this block.  */
21061   for (subblocks = BLOCK_SUBBLOCKS (stmt);
21062        subblocks != NULL;
21063        subblocks = BLOCK_CHAIN (subblocks))
21064     gen_block_die (subblocks, context_die);
21065 }
21066
21067 /* Is this a typedef we can avoid emitting?  */
21068
21069 static inline int
21070 is_redundant_typedef (const_tree decl)
21071 {
21072   if (TYPE_DECL_IS_STUB (decl))
21073     return 1;
21074
21075   if (DECL_ARTIFICIAL (decl)
21076       && DECL_CONTEXT (decl)
21077       && is_tagged_type (DECL_CONTEXT (decl))
21078       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
21079       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
21080     /* Also ignore the artificial member typedef for the class name.  */
21081     return 1;
21082
21083   return 0;
21084 }
21085
21086 /* Return TRUE if TYPE is a typedef that names a type for linkage
21087    purposes. This kind of typedefs is produced by the C++ FE for
21088    constructs like:
21089
21090    typedef struct {...} foo;
21091
21092    In that case, there is no typedef variant type produced for foo.
21093    Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
21094    struct type.  */
21095
21096 static bool
21097 is_naming_typedef_decl (const_tree decl)
21098 {
21099   if (decl == NULL_TREE
21100       || TREE_CODE (decl) != TYPE_DECL
21101       || !is_tagged_type (TREE_TYPE (decl))
21102       || DECL_IS_BUILTIN (decl)
21103       || is_redundant_typedef (decl)
21104       /* It looks like Ada produces TYPE_DECLs that are very similar
21105          to C++ naming typedefs but that have different
21106          semantics. Let's be specific to c++ for now.  */
21107       || !is_cxx ())
21108     return FALSE;
21109
21110   return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
21111           && TYPE_NAME (TREE_TYPE (decl)) == decl
21112           && (TYPE_STUB_DECL (TREE_TYPE (decl))
21113               != TYPE_NAME (TREE_TYPE (decl))));
21114 }
21115
21116 /* Looks up the DIE for a context.  */
21117
21118 static inline dw_die_ref
21119 lookup_context_die (tree context)
21120 {
21121   if (context)
21122     {
21123       /* Find die that represents this context.  */
21124       if (TYPE_P (context))
21125         {
21126           context = TYPE_MAIN_VARIANT (context);
21127           dw_die_ref ctx = lookup_type_die (context);
21128           if (!ctx)
21129             return NULL;
21130           return strip_naming_typedef (context, ctx);
21131         }
21132       else
21133         return lookup_decl_die (context);
21134     }
21135   return comp_unit_die ();
21136 }
21137
21138 /* Returns the DIE for a context.  */
21139
21140 static inline dw_die_ref
21141 get_context_die (tree context)
21142 {
21143   if (context)
21144     {
21145       /* Find die that represents this context.  */
21146       if (TYPE_P (context))
21147         {
21148           context = TYPE_MAIN_VARIANT (context);
21149           return strip_naming_typedef (context, force_type_die (context));
21150         }
21151       else
21152         return force_decl_die (context);
21153     }
21154   return comp_unit_die ();
21155 }
21156
21157 /* Returns the DIE for decl.  A DIE will always be returned.  */
21158
21159 static dw_die_ref
21160 force_decl_die (tree decl)
21161 {
21162   dw_die_ref decl_die;
21163   unsigned saved_external_flag;
21164   tree save_fn = NULL_TREE;
21165   decl_die = lookup_decl_die (decl);
21166   if (!decl_die)
21167     {
21168       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
21169
21170       decl_die = lookup_decl_die (decl);
21171       if (decl_die)
21172         return decl_die;
21173
21174       switch (TREE_CODE (decl))
21175         {
21176         case FUNCTION_DECL:
21177           /* Clear current_function_decl, so that gen_subprogram_die thinks
21178              that this is a declaration. At this point, we just want to force
21179              declaration die.  */
21180           save_fn = current_function_decl;
21181           current_function_decl = NULL_TREE;
21182           gen_subprogram_die (decl, context_die);
21183           current_function_decl = save_fn;
21184           break;
21185
21186         case VAR_DECL:
21187           /* Set external flag to force declaration die. Restore it after
21188            gen_decl_die() call.  */
21189           saved_external_flag = DECL_EXTERNAL (decl);
21190           DECL_EXTERNAL (decl) = 1;
21191           gen_decl_die (decl, NULL, context_die);
21192           DECL_EXTERNAL (decl) = saved_external_flag;
21193           break;
21194
21195         case NAMESPACE_DECL:
21196           if (dwarf_version >= 3 || !dwarf_strict)
21197             dwarf2out_decl (decl);
21198           else
21199             /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
21200             decl_die = comp_unit_die ();
21201           break;
21202
21203         case TRANSLATION_UNIT_DECL:
21204           decl_die = comp_unit_die ();
21205           break;
21206
21207         default:
21208           gcc_unreachable ();
21209         }
21210
21211       /* We should be able to find the DIE now.  */
21212       if (!decl_die)
21213         decl_die = lookup_decl_die (decl);
21214       gcc_assert (decl_die);
21215     }
21216
21217   return decl_die;
21218 }
21219
21220 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
21221    always returned.  */
21222
21223 static dw_die_ref
21224 force_type_die (tree type)
21225 {
21226   dw_die_ref type_die;
21227
21228   type_die = lookup_type_die (type);
21229   if (!type_die)
21230     {
21231       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
21232
21233       type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
21234                                     context_die);
21235       gcc_assert (type_die);
21236     }
21237   return type_die;
21238 }
21239
21240 /* Force out any required namespaces to be able to output DECL,
21241    and return the new context_die for it, if it's changed.  */
21242
21243 static dw_die_ref
21244 setup_namespace_context (tree thing, dw_die_ref context_die)
21245 {
21246   tree context = (DECL_P (thing)
21247                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
21248   if (context && TREE_CODE (context) == NAMESPACE_DECL)
21249     /* Force out the namespace.  */
21250     context_die = force_decl_die (context);
21251
21252   return context_die;
21253 }
21254
21255 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
21256    type) within its namespace, if appropriate.
21257
21258    For compatibility with older debuggers, namespace DIEs only contain
21259    declarations; all definitions are emitted at CU scope, with
21260    DW_AT_specification pointing to the declaration (like with class
21261    members).  */
21262
21263 static dw_die_ref
21264 declare_in_namespace (tree thing, dw_die_ref context_die)
21265 {
21266   dw_die_ref ns_context;
21267
21268   if (debug_info_level <= DINFO_LEVEL_TERSE)
21269     return context_die;
21270
21271   /* External declarations in the local scope only need to be emitted
21272      once, not once in the namespace and once in the scope.
21273
21274      This avoids declaring the `extern' below in the
21275      namespace DIE as well as in the innermost scope:
21276
21277           namespace S
21278           {
21279             int i=5;
21280             int foo()
21281             {
21282               int i=8;
21283               extern int i;
21284               return i;
21285             }
21286           }
21287   */
21288   if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
21289     return context_die;
21290
21291   /* If this decl is from an inlined function, then don't try to emit it in its
21292      namespace, as we will get confused.  It would have already been emitted
21293      when the abstract instance of the inline function was emitted anyways.  */
21294   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
21295     return context_die;
21296
21297   ns_context = setup_namespace_context (thing, context_die);
21298
21299   if (ns_context != context_die)
21300     {
21301       if (is_fortran ())
21302         return ns_context;
21303       if (DECL_P (thing))
21304         gen_decl_die (thing, NULL, ns_context);
21305       else
21306         gen_type_die (thing, ns_context);
21307     }
21308   return context_die;
21309 }
21310
21311 /* Generate a DIE for a namespace or namespace alias.  */
21312
21313 static void
21314 gen_namespace_die (tree decl, dw_die_ref context_die)
21315 {
21316   dw_die_ref namespace_die;
21317
21318   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
21319      they are an alias of.  */
21320   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
21321     {
21322       /* Output a real namespace or module.  */
21323       context_die = setup_namespace_context (decl, comp_unit_die ());
21324       namespace_die = new_die (is_fortran ()
21325                                ? DW_TAG_module : DW_TAG_namespace,
21326                                context_die, decl);
21327       /* For Fortran modules defined in different CU don't add src coords.  */
21328       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
21329         {
21330           const char *name = dwarf2_name (decl, 0);
21331           if (name)
21332             add_name_attribute (namespace_die, name);
21333         }
21334       else
21335         add_name_and_src_coords_attributes (namespace_die, decl);
21336       if (DECL_EXTERNAL (decl))
21337         add_AT_flag (namespace_die, DW_AT_declaration, 1);
21338       equate_decl_number_to_die (decl, namespace_die);
21339     }
21340   else
21341     {
21342       /* Output a namespace alias.  */
21343
21344       /* Force out the namespace we are an alias of, if necessary.  */
21345       dw_die_ref origin_die
21346         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
21347
21348       if (DECL_FILE_SCOPE_P (decl)
21349           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
21350         context_die = setup_namespace_context (decl, comp_unit_die ());
21351       /* Now create the namespace alias DIE.  */
21352       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
21353       add_name_and_src_coords_attributes (namespace_die, decl);
21354       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
21355       equate_decl_number_to_die (decl, namespace_die);
21356     }
21357   /* Bypass dwarf2_name's check for DECL_NAMELESS.  */
21358   if (want_pubnames ())
21359     add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
21360 }
21361
21362 /* Generate Dwarf debug information for a decl described by DECL.
21363    The return value is currently only meaningful for PARM_DECLs,
21364    for all other decls it returns NULL.  */
21365
21366 static dw_die_ref
21367 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
21368 {
21369   tree decl_or_origin = decl ? decl : origin;
21370   tree class_origin = NULL, ultimate_origin;
21371
21372   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
21373     return NULL;
21374
21375   /* Ignore pointer bounds decls.  */
21376   if (DECL_P (decl_or_origin)
21377       && TREE_TYPE (decl_or_origin)
21378       && POINTER_BOUNDS_P (decl_or_origin))
21379     return NULL;
21380
21381   switch (TREE_CODE (decl_or_origin))
21382     {
21383     case ERROR_MARK:
21384       break;
21385
21386     case CONST_DECL:
21387       if (!is_fortran () && !is_ada ())
21388         {
21389           /* The individual enumerators of an enum type get output when we output
21390              the Dwarf representation of the relevant enum type itself.  */
21391           break;
21392         }
21393
21394       /* Emit its type.  */
21395       gen_type_die (TREE_TYPE (decl), context_die);
21396
21397       /* And its containing namespace.  */
21398       context_die = declare_in_namespace (decl, context_die);
21399
21400       gen_const_die (decl, context_die);
21401       break;
21402
21403     case FUNCTION_DECL:
21404       /* Don't output any DIEs to represent mere function declarations,
21405          unless they are class members or explicit block externs.  */
21406       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
21407           && DECL_FILE_SCOPE_P (decl_or_origin)
21408           && (current_function_decl == NULL_TREE
21409               || DECL_ARTIFICIAL (decl_or_origin)))
21410         break;
21411
21412 #if 0
21413       /* FIXME */
21414       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
21415          on local redeclarations of global functions.  That seems broken.  */
21416       if (current_function_decl != decl)
21417         /* This is only a declaration.  */;
21418 #endif
21419
21420       /* If we're emitting a clone, emit info for the abstract instance.  */
21421       if (origin || DECL_ORIGIN (decl) != decl)
21422         dwarf2out_abstract_function (origin
21423                                      ? DECL_ORIGIN (origin)
21424                                      : DECL_ABSTRACT_ORIGIN (decl));
21425
21426       /* If we're emitting an out-of-line copy of an inline function,
21427          emit info for the abstract instance and set up to refer to it.  */
21428       else if (cgraph_function_possibly_inlined_p (decl)
21429                && ! DECL_ABSTRACT_P (decl)
21430                && ! class_or_namespace_scope_p (context_die)
21431                /* dwarf2out_abstract_function won't emit a die if this is just
21432                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
21433                   that case, because that works only if we have a die.  */
21434                && DECL_INITIAL (decl) != NULL_TREE)
21435         {
21436           dwarf2out_abstract_function (decl);
21437           set_decl_origin_self (decl);
21438         }
21439
21440       /* Otherwise we're emitting the primary DIE for this decl.  */
21441       else if (debug_info_level > DINFO_LEVEL_TERSE)
21442         {
21443           /* Before we describe the FUNCTION_DECL itself, make sure that we
21444              have its containing type.  */
21445           if (!origin)
21446             origin = decl_class_context (decl);
21447           if (origin != NULL_TREE)
21448             gen_type_die (origin, context_die);
21449
21450           /* And its return type.  */
21451           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
21452
21453           /* And its virtual context.  */
21454           if (DECL_VINDEX (decl) != NULL_TREE)
21455             gen_type_die (DECL_CONTEXT (decl), context_die);
21456
21457           /* Make sure we have a member DIE for decl.  */
21458           if (origin != NULL_TREE)
21459             gen_type_die_for_member (origin, decl, context_die);
21460
21461           /* And its containing namespace.  */
21462           context_die = declare_in_namespace (decl, context_die);
21463         }
21464
21465       /* Now output a DIE to represent the function itself.  */
21466       if (decl)
21467         gen_subprogram_die (decl, context_die);
21468       break;
21469
21470     case TYPE_DECL:
21471       /* If we are in terse mode, don't generate any DIEs to represent any
21472          actual typedefs.  */
21473       if (debug_info_level <= DINFO_LEVEL_TERSE)
21474         break;
21475
21476       /* In the special case of a TYPE_DECL node representing the declaration
21477          of some type tag, if the given TYPE_DECL is marked as having been
21478          instantiated from some other (original) TYPE_DECL node (e.g. one which
21479          was generated within the original definition of an inline function) we
21480          used to generate a special (abbreviated) DW_TAG_structure_type,
21481          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
21482          should be actually referencing those DIEs, as variable DIEs with that
21483          type would be emitted already in the abstract origin, so it was always
21484          removed during unused type prunning.  Don't add anything in this
21485          case.  */
21486       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
21487         break;
21488
21489       if (is_redundant_typedef (decl))
21490         gen_type_die (TREE_TYPE (decl), context_die);
21491       else
21492         /* Output a DIE to represent the typedef itself.  */
21493         gen_typedef_die (decl, context_die);
21494       break;
21495
21496     case LABEL_DECL:
21497       if (debug_info_level >= DINFO_LEVEL_NORMAL)
21498         gen_label_die (decl, context_die);
21499       break;
21500
21501     case VAR_DECL:
21502     case RESULT_DECL:
21503       /* If we are in terse mode, don't generate any DIEs to represent any
21504          variable declarations or definitions.  */
21505       if (debug_info_level <= DINFO_LEVEL_TERSE)
21506         break;
21507
21508       /* Output any DIEs that are needed to specify the type of this data
21509          object.  */
21510       if (decl_by_reference_p (decl_or_origin))
21511         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21512       else
21513         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21514
21515       /* And its containing type.  */
21516       class_origin = decl_class_context (decl_or_origin);
21517       if (class_origin != NULL_TREE)
21518         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
21519
21520       /* And its containing namespace.  */
21521       context_die = declare_in_namespace (decl_or_origin, context_die);
21522
21523       /* Now output the DIE to represent the data object itself.  This gets
21524          complicated because of the possibility that the VAR_DECL really
21525          represents an inlined instance of a formal parameter for an inline
21526          function.  */
21527       ultimate_origin = decl_ultimate_origin (decl_or_origin);
21528       if (ultimate_origin != NULL_TREE
21529           && TREE_CODE (ultimate_origin) == PARM_DECL)
21530         gen_formal_parameter_die (decl, origin,
21531                                   true /* Emit name attribute.  */,
21532                                   context_die);
21533       else
21534         gen_variable_die (decl, origin, context_die);
21535       break;
21536
21537     case FIELD_DECL:
21538       /* Ignore the nameless fields that are used to skip bits but handle C++
21539          anonymous unions and structs.  */
21540       if (DECL_NAME (decl) != NULL_TREE
21541           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
21542           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
21543         {
21544           gen_type_die (member_declared_type (decl), context_die);
21545           gen_field_die (decl, context_die);
21546         }
21547       break;
21548
21549     case PARM_DECL:
21550       if (DECL_BY_REFERENCE (decl_or_origin))
21551         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21552       else
21553         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21554       return gen_formal_parameter_die (decl, origin,
21555                                        true /* Emit name attribute.  */,
21556                                        context_die);
21557
21558     case NAMESPACE_DECL:
21559     case IMPORTED_DECL:
21560       if (dwarf_version >= 3 || !dwarf_strict)
21561         gen_namespace_die (decl, context_die);
21562       break;
21563
21564     case NAMELIST_DECL:
21565       gen_namelist_decl (DECL_NAME (decl), context_die,
21566                          NAMELIST_DECL_ASSOCIATED_DECL (decl));
21567       break;
21568
21569     default:
21570       /* Probably some frontend-internal decl.  Assume we don't care.  */
21571       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
21572       break;
21573     }
21574
21575   return NULL;
21576 }
21577 \f
21578 /* Output initial debug information for global DECL.  Called at the
21579    end of the parsing process.
21580
21581    This is the initial debug generation process.  As such, the DIEs
21582    generated may be incomplete.  A later debug generation pass
21583    (dwarf2out_late_global_decl) will augment the information generated
21584    in this pass (e.g., with complete location info).  */
21585
21586 static void
21587 dwarf2out_early_global_decl (tree decl)
21588 {
21589   set_early_dwarf s;
21590
21591   /* gen_decl_die() will set DECL_ABSTRACT because
21592      cgraph_function_possibly_inlined_p() returns true.  This is in
21593      turn will cause DW_AT_inline attributes to be set.
21594
21595      This happens because at early dwarf generation, there is no
21596      cgraph information, causing cgraph_function_possibly_inlined_p()
21597      to return true.  Trick cgraph_function_possibly_inlined_p()
21598      while we generate dwarf early.  */
21599   bool save = symtab->global_info_ready;
21600   symtab->global_info_ready = true;
21601
21602   /* We don't handle TYPE_DECLs.  If required, they'll be reached via
21603      other DECLs and they can point to template types or other things
21604      that dwarf2out can't handle when done via dwarf2out_decl.  */
21605   if (TREE_CODE (decl) != TYPE_DECL
21606       && TREE_CODE (decl) != PARM_DECL)
21607     {
21608       tree save_fndecl = current_function_decl;
21609       if (TREE_CODE (decl) == FUNCTION_DECL)
21610         {
21611           /* No cfun means the symbol has no body, so there's nothing
21612              to emit.  */
21613           if (!DECL_STRUCT_FUNCTION (decl))
21614             goto early_decl_exit;
21615
21616           current_function_decl = decl;
21617         }
21618       dwarf2out_decl (decl);
21619       if (TREE_CODE (decl) == FUNCTION_DECL)
21620         current_function_decl = save_fndecl;
21621     }
21622  early_decl_exit:
21623   symtab->global_info_ready = save;
21624 }
21625
21626 /* Output debug information for global decl DECL.  Called from
21627    toplev.c after compilation proper has finished.  */
21628
21629 static void
21630 dwarf2out_late_global_decl (tree decl)
21631 {
21632   /* We have to generate early debug late for LTO.  */
21633   if (in_lto_p)
21634     dwarf2out_early_global_decl (decl);
21635
21636     /* Fill-in any location information we were unable to determine
21637        on the first pass.  */
21638   if (TREE_CODE (decl) == VAR_DECL
21639       && !POINTER_BOUNDS_P (decl))
21640     {
21641       dw_die_ref die = lookup_decl_die (decl);
21642       if (die)
21643         add_location_or_const_value_attribute (die, decl, false,
21644                                                DW_AT_location);
21645     }
21646 }
21647
21648 /* Output debug information for type decl DECL.  Called from toplev.c
21649    and from language front ends (to record built-in types).  */
21650 static void
21651 dwarf2out_type_decl (tree decl, int local)
21652 {
21653   if (!local)
21654     {
21655       set_early_dwarf s;
21656       dwarf2out_decl (decl);
21657     }
21658 }
21659
21660 /* Output debug information for imported module or decl DECL.
21661    NAME is non-NULL name in the lexical block if the decl has been renamed.
21662    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
21663    that DECL belongs to.
21664    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
21665 static void
21666 dwarf2out_imported_module_or_decl_1 (tree decl,
21667                                      tree name,
21668                                      tree lexical_block,
21669                                      dw_die_ref lexical_block_die)
21670 {
21671   expanded_location xloc;
21672   dw_die_ref imported_die = NULL;
21673   dw_die_ref at_import_die;
21674
21675   if (TREE_CODE (decl) == IMPORTED_DECL)
21676     {
21677       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
21678       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
21679       gcc_assert (decl);
21680     }
21681   else
21682     xloc = expand_location (input_location);
21683
21684   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
21685     {
21686       at_import_die = force_type_die (TREE_TYPE (decl));
21687       /* For namespace N { typedef void T; } using N::T; base_type_die
21688          returns NULL, but DW_TAG_imported_declaration requires
21689          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
21690       if (!at_import_die)
21691         {
21692           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
21693           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
21694           at_import_die = lookup_type_die (TREE_TYPE (decl));
21695           gcc_assert (at_import_die);
21696         }
21697     }
21698   else
21699     {
21700       at_import_die = lookup_decl_die (decl);
21701       if (!at_import_die)
21702         {
21703           /* If we're trying to avoid duplicate debug info, we may not have
21704              emitted the member decl for this field.  Emit it now.  */
21705           if (TREE_CODE (decl) == FIELD_DECL)
21706             {
21707               tree type = DECL_CONTEXT (decl);
21708
21709               if (TYPE_CONTEXT (type)
21710                   && TYPE_P (TYPE_CONTEXT (type))
21711                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
21712                                                 DINFO_USAGE_DIR_USE))
21713                 return;
21714               gen_type_die_for_member (type, decl,
21715                                        get_context_die (TYPE_CONTEXT (type)));
21716             }
21717           if (TREE_CODE (decl) == NAMELIST_DECL)
21718             at_import_die = gen_namelist_decl (DECL_NAME (decl),
21719                                          get_context_die (DECL_CONTEXT (decl)),
21720                                          NULL_TREE);
21721           else
21722             at_import_die = force_decl_die (decl);
21723         }
21724     }
21725
21726   if (TREE_CODE (decl) == NAMESPACE_DECL)
21727     {
21728       if (dwarf_version >= 3 || !dwarf_strict)
21729         imported_die = new_die (DW_TAG_imported_module,
21730                                 lexical_block_die,
21731                                 lexical_block);
21732       else
21733         return;
21734     }
21735   else
21736     imported_die = new_die (DW_TAG_imported_declaration,
21737                             lexical_block_die,
21738                             lexical_block);
21739
21740   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
21741   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
21742   if (name)
21743     add_AT_string (imported_die, DW_AT_name,
21744                    IDENTIFIER_POINTER (name));
21745   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
21746 }
21747
21748 /* Output debug information for imported module or decl DECL.
21749    NAME is non-NULL name in context if the decl has been renamed.
21750    CHILD is true if decl is one of the renamed decls as part of
21751    importing whole module.  */
21752
21753 static void
21754 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
21755                                    bool child)
21756 {
21757   /* dw_die_ref at_import_die;  */
21758   dw_die_ref scope_die;
21759
21760   if (debug_info_level <= DINFO_LEVEL_TERSE)
21761     return;
21762
21763   gcc_assert (decl);
21764
21765   set_early_dwarf s;
21766
21767   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
21768      We need decl DIE for reference and scope die. First, get DIE for the decl
21769      itself.  */
21770
21771   /* Get the scope die for decl context. Use comp_unit_die for global module
21772      or decl. If die is not found for non globals, force new die.  */
21773   if (context
21774       && TYPE_P (context)
21775       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
21776     return;
21777
21778   if (!(dwarf_version >= 3 || !dwarf_strict))
21779     return;
21780
21781   scope_die = get_context_die (context);
21782
21783   if (child)
21784     {
21785       gcc_assert (scope_die->die_child);
21786       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
21787       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
21788       scope_die = scope_die->die_child;
21789     }
21790
21791   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
21792   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
21793 }
21794
21795 /* Output debug information for namelists.   */
21796
21797 static dw_die_ref
21798 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
21799 {
21800   dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
21801   tree value;
21802   unsigned i;
21803
21804   if (debug_info_level <= DINFO_LEVEL_TERSE)
21805     return NULL;
21806
21807   gcc_assert (scope_die != NULL);
21808   nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
21809   add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
21810
21811   /* If there are no item_decls, we have a nondefining namelist, e.g.
21812      with USE association; hence, set DW_AT_declaration.  */
21813   if (item_decls == NULL_TREE)
21814     {
21815       add_AT_flag (nml_die, DW_AT_declaration, 1);
21816       return nml_die;
21817     }
21818
21819   FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
21820     {
21821       nml_item_ref_die = lookup_decl_die (value);
21822       if (!nml_item_ref_die)
21823         nml_item_ref_die = force_decl_die (value);
21824
21825       nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
21826       add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
21827     }
21828   return nml_die;
21829 }
21830
21831
21832 /* Write the debugging output for DECL and return the DIE.  */
21833
21834 static void
21835 dwarf2out_decl (tree decl)
21836 {
21837   dw_die_ref context_die = comp_unit_die ();
21838
21839   switch (TREE_CODE (decl))
21840     {
21841     case ERROR_MARK:
21842       return;
21843
21844     case FUNCTION_DECL:
21845       /* What we would really like to do here is to filter out all mere
21846          file-scope declarations of file-scope functions which are never
21847          referenced later within this translation unit (and keep all of ones
21848          that *are* referenced later on) but we aren't clairvoyant, so we have
21849          no idea which functions will be referenced in the future (i.e. later
21850          on within the current translation unit). So here we just ignore all
21851          file-scope function declarations which are not also definitions.  If
21852          and when the debugger needs to know something about these functions,
21853          it will have to hunt around and find the DWARF information associated
21854          with the definition of the function.
21855
21856          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
21857          nodes represent definitions and which ones represent mere
21858          declarations.  We have to check DECL_INITIAL instead. That's because
21859          the C front-end supports some weird semantics for "extern inline"
21860          function definitions.  These can get inlined within the current
21861          translation unit (and thus, we need to generate Dwarf info for their
21862          abstract instances so that the Dwarf info for the concrete inlined
21863          instances can have something to refer to) but the compiler never
21864          generates any out-of-lines instances of such things (despite the fact
21865          that they *are* definitions).
21866
21867          The important point is that the C front-end marks these "extern
21868          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
21869          them anyway. Note that the C++ front-end also plays some similar games
21870          for inline function definitions appearing within include files which
21871          also contain `#pragma interface' pragmas.
21872
21873          If we are called from dwarf2out_abstract_function output a DIE
21874          anyway.  We can end up here this way with early inlining and LTO
21875          where the inlined function is output in a different LTRANS unit
21876          or not at all.  */
21877       if (DECL_INITIAL (decl) == NULL_TREE
21878           && ! DECL_ABSTRACT_P (decl))
21879         return;
21880
21881       /* If we're a nested function, initially use a parent of NULL; if we're
21882          a plain function, this will be fixed up in decls_for_scope.  If
21883          we're a method, it will be ignored, since we already have a DIE.  */
21884       if (decl_function_context (decl)
21885           /* But if we're in terse mode, we don't care about scope.  */
21886           && debug_info_level > DINFO_LEVEL_TERSE)
21887         context_die = NULL;
21888       break;
21889
21890     case VAR_DECL:
21891       /* For local statics lookup proper context die.  */
21892       if (local_function_static (decl))
21893         context_die = lookup_decl_die (DECL_CONTEXT (decl));
21894
21895       /* If we are in terse mode, don't generate any DIEs to represent any
21896          variable declarations or definitions.  */
21897       if (debug_info_level <= DINFO_LEVEL_TERSE)
21898         return;
21899       break;
21900
21901     case CONST_DECL:
21902       if (debug_info_level <= DINFO_LEVEL_TERSE)
21903         return;
21904       if (!is_fortran () && !is_ada ())
21905         return;
21906       if (TREE_STATIC (decl) && decl_function_context (decl))
21907         context_die = lookup_decl_die (DECL_CONTEXT (decl));
21908       break;
21909
21910     case NAMESPACE_DECL:
21911     case IMPORTED_DECL:
21912       if (debug_info_level <= DINFO_LEVEL_TERSE)
21913         return;
21914       if (lookup_decl_die (decl) != NULL)
21915         return;
21916       break;
21917
21918     case TYPE_DECL:
21919       /* Don't emit stubs for types unless they are needed by other DIEs.  */
21920       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
21921         return;
21922
21923       /* Don't bother trying to generate any DIEs to represent any of the
21924          normal built-in types for the language we are compiling.  */
21925       if (DECL_IS_BUILTIN (decl))
21926         return;
21927
21928       /* If we are in terse mode, don't generate any DIEs for types.  */
21929       if (debug_info_level <= DINFO_LEVEL_TERSE)
21930         return;
21931
21932       /* If we're a function-scope tag, initially use a parent of NULL;
21933          this will be fixed up in decls_for_scope.  */
21934       if (decl_function_context (decl))
21935         context_die = NULL;
21936
21937       break;
21938
21939     case NAMELIST_DECL:
21940       break;
21941
21942     default:
21943       return;
21944     }
21945
21946   gen_decl_die (decl, NULL, context_die);
21947
21948 #ifdef ENABLE_CHECKING
21949   dw_die_ref die = lookup_decl_die (decl);
21950   if (die)
21951     check_die (die);
21952 #endif
21953 }
21954
21955 /* Write the debugging output for DECL.  */
21956
21957 static void
21958 dwarf2out_function_decl (tree decl)
21959 {
21960   dwarf2out_decl (decl);
21961   call_arg_locations = NULL;
21962   call_arg_loc_last = NULL;
21963   call_site_count = -1;
21964   tail_call_site_count = -1;
21965   decl_loc_table->empty ();
21966   cached_dw_loc_list_table->empty ();
21967 }
21968
21969 /* Output a marker (i.e. a label) for the beginning of the generated code for
21970    a lexical block.  */
21971
21972 static void
21973 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21974                        unsigned int blocknum)
21975 {
21976   switch_to_section (current_function_section ());
21977   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
21978 }
21979
21980 /* Output a marker (i.e. a label) for the end of the generated code for a
21981    lexical block.  */
21982
21983 static void
21984 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
21985 {
21986   switch_to_section (current_function_section ());
21987   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
21988 }
21989
21990 /* Returns nonzero if it is appropriate not to emit any debugging
21991    information for BLOCK, because it doesn't contain any instructions.
21992
21993    Don't allow this for blocks with nested functions or local classes
21994    as we would end up with orphans, and in the presence of scheduling
21995    we may end up calling them anyway.  */
21996
21997 static bool
21998 dwarf2out_ignore_block (const_tree block)
21999 {
22000   tree decl;
22001   unsigned int i;
22002
22003   for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
22004     if (TREE_CODE (decl) == FUNCTION_DECL
22005         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
22006       return 0;
22007   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
22008     {
22009       decl = BLOCK_NONLOCALIZED_VAR (block, i);
22010       if (TREE_CODE (decl) == FUNCTION_DECL
22011           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
22012       return 0;
22013     }
22014
22015   return 1;
22016 }
22017
22018 /* Hash table routines for file_hash.  */
22019
22020 bool
22021 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
22022 {
22023   return filename_cmp (p1->filename, p2) == 0;
22024 }
22025
22026 hashval_t
22027 dwarf_file_hasher::hash (dwarf_file_data *p)
22028 {
22029   return htab_hash_string (p->filename);
22030 }
22031
22032 /* Lookup FILE_NAME (in the list of filenames that we know about here in
22033    dwarf2out.c) and return its "index".  The index of each (known) filename is
22034    just a unique number which is associated with only that one filename.  We
22035    need such numbers for the sake of generating labels (in the .debug_sfnames
22036    section) and references to those files numbers (in the .debug_srcinfo
22037    and.debug_macinfo sections).  If the filename given as an argument is not
22038    found in our current list, add it to the list and assign it the next
22039    available unique index number.  */
22040
22041 static struct dwarf_file_data *
22042 lookup_filename (const char *file_name)
22043 {
22044   struct dwarf_file_data * created;
22045
22046   if (!file_name)
22047     return NULL;
22048
22049   dwarf_file_data **slot
22050     = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
22051                                        INSERT);
22052   if (*slot)
22053     return *slot;
22054
22055   created = ggc_alloc<dwarf_file_data> ();
22056   created->filename = file_name;
22057   created->emitted_number = 0;
22058   *slot = created;
22059   return created;
22060 }
22061
22062 /* If the assembler will construct the file table, then translate the compiler
22063    internal file table number into the assembler file table number, and emit
22064    a .file directive if we haven't already emitted one yet.  The file table
22065    numbers are different because we prune debug info for unused variables and
22066    types, which may include filenames.  */
22067
22068 static int
22069 maybe_emit_file (struct dwarf_file_data * fd)
22070 {
22071   if (! fd->emitted_number)
22072     {
22073       if (last_emitted_file)
22074         fd->emitted_number = last_emitted_file->emitted_number + 1;
22075       else
22076         fd->emitted_number = 1;
22077       last_emitted_file = fd;
22078
22079       if (DWARF2_ASM_LINE_DEBUG_INFO)
22080         {
22081           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
22082           output_quoted_string (asm_out_file,
22083                                 remap_debug_filename (fd->filename));
22084           fputc ('\n', asm_out_file);
22085         }
22086     }
22087
22088   return fd->emitted_number;
22089 }
22090
22091 /* Schedule generation of a DW_AT_const_value attribute to DIE.
22092    That generation should happen after function debug info has been
22093    generated. The value of the attribute is the constant value of ARG.  */
22094
22095 static void
22096 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
22097 {
22098   die_arg_entry entry;
22099
22100   if (!die || !arg)
22101     return;
22102
22103   gcc_assert (early_dwarf);
22104
22105   if (!tmpl_value_parm_die_table)
22106     vec_alloc (tmpl_value_parm_die_table, 32);
22107
22108   entry.die = die;
22109   entry.arg = arg;
22110   vec_safe_push (tmpl_value_parm_die_table, entry);
22111 }
22112
22113 /* Return TRUE if T is an instance of generic type, FALSE
22114    otherwise.  */
22115
22116 static bool
22117 generic_type_p (tree t)
22118 {
22119   if (t == NULL_TREE || !TYPE_P (t))
22120     return false;
22121   return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
22122 }
22123
22124 /* Schedule the generation of the generic parameter dies for the
22125   instance of generic type T. The proper generation itself is later
22126   done by gen_scheduled_generic_parms_dies. */
22127
22128 static void
22129 schedule_generic_params_dies_gen (tree t)
22130 {
22131   if (!generic_type_p (t))
22132     return;
22133
22134   gcc_assert (early_dwarf);
22135
22136   if (!generic_type_instances)
22137     vec_alloc (generic_type_instances, 256);
22138
22139   vec_safe_push (generic_type_instances, t);
22140 }
22141
22142 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
22143    by append_entry_to_tmpl_value_parm_die_table. This function must
22144    be called after function DIEs have been generated.  */
22145
22146 static void
22147 gen_remaining_tmpl_value_param_die_attribute (void)
22148 {
22149   if (tmpl_value_parm_die_table)
22150     {
22151       unsigned i, j;
22152       die_arg_entry *e;
22153
22154       /* We do this in two phases - first get the cases we can
22155          handle during early-finish, preserving those we cannot
22156          (containing symbolic constants where we don't yet know
22157          whether we are going to output the referenced symbols).
22158          For those we try again at late-finish.  */
22159       j = 0;
22160       FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
22161         {
22162           if (!tree_add_const_value_attribute (e->die, e->arg))
22163             (*tmpl_value_parm_die_table)[j++] = *e;
22164         }
22165       tmpl_value_parm_die_table->truncate (j);
22166     }
22167 }
22168
22169 /* Generate generic parameters DIEs for instances of generic types
22170    that have been previously scheduled by
22171    schedule_generic_params_dies_gen. This function must be called
22172    after all the types of the CU have been laid out.  */
22173
22174 static void
22175 gen_scheduled_generic_parms_dies (void)
22176 {
22177   unsigned i;
22178   tree t;
22179
22180   if (!generic_type_instances)
22181     return;
22182   
22183   /* We end up "recursing" into schedule_generic_params_dies_gen, so
22184      pretend this generation is part of "early dwarf" as well.  */
22185   set_early_dwarf s;
22186
22187   FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
22188     if (COMPLETE_TYPE_P (t))
22189       gen_generic_params_dies (t);
22190
22191   generic_type_instances = NULL;
22192 }
22193
22194
22195 /* Replace DW_AT_name for the decl with name.  */
22196
22197 static void
22198 dwarf2out_set_name (tree decl, tree name)
22199 {
22200   dw_die_ref die;
22201   dw_attr_node *attr;
22202   const char *dname;
22203
22204   die = TYPE_SYMTAB_DIE (decl);
22205   if (!die)
22206     return;
22207
22208   dname = dwarf2_name (name, 0);
22209   if (!dname)
22210     return;
22211
22212   attr = get_AT (die, DW_AT_name);
22213   if (attr)
22214     {
22215       struct indirect_string_node *node;
22216
22217       node = find_AT_string (dname);
22218       /* replace the string.  */
22219       attr->dw_attr_val.v.val_str = node;
22220     }
22221
22222   else
22223     add_name_attribute (die, dname);
22224 }
22225
22226 /* True if before or during processing of the first function being emitted.  */
22227 static bool in_first_function_p = true;
22228 /* True if loc_note during dwarf2out_var_location call might still be
22229    before first real instruction at address equal to .Ltext0.  */
22230 static bool maybe_at_text_label_p = true;
22231 /* One above highest N where .LVLN label might be equal to .Ltext0 label.  */
22232 static unsigned int first_loclabel_num_not_at_text_label;
22233
22234 /* Called by the final INSN scan whenever we see a var location.  We
22235    use it to drop labels in the right places, and throw the location in
22236    our lookup table.  */
22237
22238 static void
22239 dwarf2out_var_location (rtx_insn *loc_note)
22240 {
22241   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
22242   struct var_loc_node *newloc;
22243   rtx_insn *next_real, *next_note;
22244   static const char *last_label;
22245   static const char *last_postcall_label;
22246   static bool last_in_cold_section_p;
22247   static rtx_insn *expected_next_loc_note;
22248   tree decl;
22249   bool var_loc_p;
22250
22251   if (!NOTE_P (loc_note))
22252     {
22253       if (CALL_P (loc_note))
22254         {
22255           call_site_count++;
22256           if (SIBLING_CALL_P (loc_note))
22257             tail_call_site_count++;
22258         }
22259       return;
22260     }
22261
22262   var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
22263   if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
22264     return;
22265
22266   /* Optimize processing a large consecutive sequence of location
22267      notes so we don't spend too much time in next_real_insn.  If the
22268      next insn is another location note, remember the next_real_insn
22269      calculation for next time.  */
22270   next_real = cached_next_real_insn;
22271   if (next_real)
22272     {
22273       if (expected_next_loc_note != loc_note)
22274         next_real = NULL;
22275     }
22276
22277   next_note = NEXT_INSN (loc_note);
22278   if (! next_note
22279       || next_note->deleted ()
22280       || ! NOTE_P (next_note)
22281       || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
22282           && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
22283     next_note = NULL;
22284
22285   if (! next_real)
22286     next_real = next_real_insn (loc_note);
22287
22288   if (next_note)
22289     {
22290       expected_next_loc_note = next_note;
22291       cached_next_real_insn = next_real;
22292     }
22293   else
22294     cached_next_real_insn = NULL;
22295
22296   /* If there are no instructions which would be affected by this note,
22297      don't do anything.  */
22298   if (var_loc_p
22299       && next_real == NULL_RTX
22300       && !NOTE_DURING_CALL_P (loc_note))
22301     return;
22302
22303   if (next_real == NULL_RTX)
22304     next_real = get_last_insn ();
22305
22306   /* If there were any real insns between note we processed last time
22307      and this note (or if it is the first note), clear
22308      last_{,postcall_}label so that they are not reused this time.  */
22309   if (last_var_location_insn == NULL_RTX
22310       || last_var_location_insn != next_real
22311       || last_in_cold_section_p != in_cold_section_p)
22312     {
22313       last_label = NULL;
22314       last_postcall_label = NULL;
22315     }
22316
22317   if (var_loc_p)
22318     {
22319       decl = NOTE_VAR_LOCATION_DECL (loc_note);
22320       newloc = add_var_loc_to_decl (decl, loc_note,
22321                                     NOTE_DURING_CALL_P (loc_note)
22322                                     ? last_postcall_label : last_label);
22323       if (newloc == NULL)
22324         return;
22325     }
22326   else
22327     {
22328       decl = NULL_TREE;
22329       newloc = NULL;
22330     }
22331
22332   /* If there were no real insns between note we processed last time
22333      and this note, use the label we emitted last time.  Otherwise
22334      create a new label and emit it.  */
22335   if (last_label == NULL)
22336     {
22337       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
22338       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
22339       loclabel_num++;
22340       last_label = ggc_strdup (loclabel);
22341       /* See if loclabel might be equal to .Ltext0.  If yes,
22342          bump first_loclabel_num_not_at_text_label.  */
22343       if (!have_multiple_function_sections
22344           && in_first_function_p
22345           && maybe_at_text_label_p)
22346         {
22347           static rtx_insn *last_start;
22348           rtx_insn *insn;
22349           for (insn = loc_note; insn; insn = previous_insn (insn))
22350             if (insn == last_start)
22351               break;
22352             else if (!NONDEBUG_INSN_P (insn))
22353               continue;
22354             else
22355               {
22356                 rtx body = PATTERN (insn);
22357                 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
22358                   continue;
22359                 /* Inline asm could occupy zero bytes.  */
22360                 else if (GET_CODE (body) == ASM_INPUT
22361                          || asm_noperands (body) >= 0)
22362                   continue;
22363 #ifdef HAVE_attr_length
22364                 else if (get_attr_min_length (insn) == 0)
22365                   continue;
22366 #endif
22367                 else
22368                   {
22369                     /* Assume insn has non-zero length.  */
22370                     maybe_at_text_label_p = false;
22371                     break;
22372                   }
22373               }
22374           if (maybe_at_text_label_p)
22375             {
22376               last_start = loc_note;
22377               first_loclabel_num_not_at_text_label = loclabel_num;
22378             }
22379         }
22380     }
22381
22382   if (!var_loc_p)
22383     {
22384       struct call_arg_loc_node *ca_loc
22385         = ggc_cleared_alloc<call_arg_loc_node> ();
22386       rtx_insn *prev = prev_real_insn (loc_note);
22387       rtx x;
22388       ca_loc->call_arg_loc_note = loc_note;
22389       ca_loc->next = NULL;
22390       ca_loc->label = last_label;
22391       gcc_assert (prev
22392                   && (CALL_P (prev)
22393                       || (NONJUMP_INSN_P (prev)
22394                           && GET_CODE (PATTERN (prev)) == SEQUENCE
22395                           && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
22396       if (!CALL_P (prev))
22397         prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
22398       ca_loc->tail_call_p = SIBLING_CALL_P (prev);
22399       x = get_call_rtx_from (PATTERN (prev));
22400       if (x)
22401         {
22402           x = XEXP (XEXP (x, 0), 0);
22403           if (GET_CODE (x) == SYMBOL_REF
22404               && SYMBOL_REF_DECL (x)
22405               && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
22406             ca_loc->symbol_ref = x;
22407         }
22408       ca_loc->block = insn_scope (prev);
22409       if (call_arg_locations)
22410         call_arg_loc_last->next = ca_loc;
22411       else
22412         call_arg_locations = ca_loc;
22413       call_arg_loc_last = ca_loc;
22414     }
22415   else if (!NOTE_DURING_CALL_P (loc_note))
22416     newloc->label = last_label;
22417   else
22418     {
22419       if (!last_postcall_label)
22420         {
22421           sprintf (loclabel, "%s-1", last_label);
22422           last_postcall_label = ggc_strdup (loclabel);
22423         }
22424       newloc->label = last_postcall_label;
22425     }
22426
22427   last_var_location_insn = next_real;
22428   last_in_cold_section_p = in_cold_section_p;
22429 }
22430
22431 /* Note in one location list that text section has changed.  */
22432
22433 int
22434 var_location_switch_text_section_1 (var_loc_list **slot, void *)
22435 {
22436   var_loc_list *list = *slot;
22437   if (list->first)
22438     list->last_before_switch
22439       = list->last->next ? list->last->next : list->last;
22440   return 1;
22441 }
22442
22443 /* Note in all location lists that text section has changed.  */
22444
22445 static void
22446 var_location_switch_text_section (void)
22447 {
22448   if (decl_loc_table == NULL)
22449     return;
22450
22451   decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
22452 }
22453
22454 /* Create a new line number table.  */
22455
22456 static dw_line_info_table *
22457 new_line_info_table (void)
22458 {
22459   dw_line_info_table *table;
22460
22461   table = ggc_cleared_alloc<dw_line_info_table> ();
22462   table->file_num = 1;
22463   table->line_num = 1;
22464   table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
22465
22466   return table;
22467 }
22468
22469 /* Lookup the "current" table into which we emit line info, so
22470    that we don't have to do it for every source line.  */
22471
22472 static void
22473 set_cur_line_info_table (section *sec)
22474 {
22475   dw_line_info_table *table;
22476
22477   if (sec == text_section)
22478     table = text_section_line_info;
22479   else if (sec == cold_text_section)
22480     {
22481       table = cold_text_section_line_info;
22482       if (!table)
22483         {
22484           cold_text_section_line_info = table = new_line_info_table ();
22485           table->end_label = cold_end_label;
22486         }
22487     }
22488   else
22489     {
22490       const char *end_label;
22491
22492       if (flag_reorder_blocks_and_partition)
22493         {
22494           if (in_cold_section_p)
22495             end_label = crtl->subsections.cold_section_end_label;
22496           else
22497             end_label = crtl->subsections.hot_section_end_label;
22498         }
22499       else
22500         {
22501           char label[MAX_ARTIFICIAL_LABEL_BYTES];
22502           ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
22503                                        current_function_funcdef_no);
22504           end_label = ggc_strdup (label);
22505         }
22506
22507       table = new_line_info_table ();
22508       table->end_label = end_label;
22509
22510       vec_safe_push (separate_line_info, table);
22511     }
22512
22513   if (DWARF2_ASM_LINE_DEBUG_INFO)
22514     table->is_stmt = (cur_line_info_table
22515                       ? cur_line_info_table->is_stmt
22516                       : DWARF_LINE_DEFAULT_IS_STMT_START);
22517   cur_line_info_table = table;
22518 }
22519
22520
22521 /* We need to reset the locations at the beginning of each
22522    function. We can't do this in the end_function hook, because the
22523    declarations that use the locations won't have been output when
22524    that hook is called.  Also compute have_multiple_function_sections here.  */
22525
22526 static void
22527 dwarf2out_begin_function (tree fun)
22528 {
22529   section *sec = function_section (fun);
22530
22531   if (sec != text_section)
22532     have_multiple_function_sections = true;
22533
22534   if (flag_reorder_blocks_and_partition && !cold_text_section)
22535     {
22536       gcc_assert (current_function_decl == fun);
22537       cold_text_section = unlikely_text_section ();
22538       switch_to_section (cold_text_section);
22539       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
22540       switch_to_section (sec);
22541     }
22542
22543   dwarf2out_note_section_used ();
22544   call_site_count = 0;
22545   tail_call_site_count = 0;
22546
22547   set_cur_line_info_table (sec);
22548 }
22549
22550 /* Helper function of dwarf2out_end_function, called only after emitting
22551    the very first function into assembly.  Check if some .debug_loc range
22552    might end with a .LVL* label that could be equal to .Ltext0.
22553    In that case we must force using absolute addresses in .debug_loc ranges,
22554    because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
22555    .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
22556    list terminator.
22557    Set have_multiple_function_sections to true in that case and
22558    terminate htab traversal.  */
22559
22560 int
22561 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
22562 {
22563   var_loc_list *entry = *slot;
22564   struct var_loc_node *node;
22565
22566   node = entry->first;
22567   if (node && node->next && node->next->label)
22568     {
22569       unsigned int i;
22570       const char *label = node->next->label;
22571       char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
22572
22573       for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
22574         {
22575           ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
22576           if (strcmp (label, loclabel) == 0)
22577             {
22578               have_multiple_function_sections = true;
22579               return 0;
22580             }
22581         }
22582     }
22583   return 1;
22584 }
22585
22586 /* Hook called after emitting a function into assembly.
22587    This does something only for the very first function emitted.  */
22588
22589 static void
22590 dwarf2out_end_function (unsigned int)
22591 {
22592   if (in_first_function_p
22593       && !have_multiple_function_sections
22594       && first_loclabel_num_not_at_text_label
22595       && decl_loc_table)
22596     decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
22597   in_first_function_p = false;
22598   maybe_at_text_label_p = false;
22599 }
22600
22601 /* Temporary holder for dwarf2out_register_main_translation_unit.  Used to let
22602    front-ends register a translation unit even before dwarf2out_init is
22603    called.  */
22604 static tree main_translation_unit = NULL_TREE;
22605
22606 /* Hook called by front-ends after they built their main translation unit.
22607    Associate comp_unit_die to UNIT.  */
22608
22609 static void
22610 dwarf2out_register_main_translation_unit (tree unit)
22611 {
22612   gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
22613               && main_translation_unit == NULL_TREE);
22614   main_translation_unit = unit;
22615   /* If dwarf2out_init has not been called yet, it will perform the association
22616      itself looking at main_translation_unit.  */
22617   if (decl_die_table != NULL)
22618     equate_decl_number_to_die (unit, comp_unit_die ());
22619 }
22620
22621 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE.  */
22622
22623 static void
22624 push_dw_line_info_entry (dw_line_info_table *table,
22625                          enum dw_line_info_opcode opcode, unsigned int val)
22626 {
22627   dw_line_info_entry e;
22628   e.opcode = opcode;
22629   e.val = val;
22630   vec_safe_push (table->entries, e);
22631 }
22632
22633 /* Output a label to mark the beginning of a source code line entry
22634    and record information relating to this source line, in
22635    'line_info_table' for later output of the .debug_line section.  */
22636 /* ??? The discriminator parameter ought to be unsigned.  */
22637
22638 static void
22639 dwarf2out_source_line (unsigned int line, const char *filename,
22640                        int discriminator, bool is_stmt)
22641 {
22642   unsigned int file_num;
22643   dw_line_info_table *table;
22644
22645   if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
22646     return;
22647
22648   /* The discriminator column was added in dwarf4.  Simplify the below
22649      by simply removing it if we're not supposed to output it.  */
22650   if (dwarf_version < 4 && dwarf_strict)
22651     discriminator = 0;
22652
22653   table = cur_line_info_table;
22654   file_num = maybe_emit_file (lookup_filename (filename));
22655
22656   /* ??? TODO: Elide duplicate line number entries.  Traditionally,
22657      the debugger has used the second (possibly duplicate) line number
22658      at the beginning of the function to mark the end of the prologue.
22659      We could eliminate any other duplicates within the function.  For
22660      Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
22661      that second line number entry.  */
22662   /* Recall that this end-of-prologue indication is *not* the same thing
22663      as the end_prologue debug hook.  The NOTE_INSN_PROLOGUE_END note,
22664      to which the hook corresponds, follows the last insn that was 
22665      emitted by gen_prologue.  What we need is to precede the first insn
22666      that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
22667      insn that corresponds to something the user wrote.  These may be
22668      very different locations once scheduling is enabled.  */
22669
22670   if (0 && file_num == table->file_num
22671       && line == table->line_num
22672       && discriminator == table->discrim_num
22673       && is_stmt == table->is_stmt)
22674     return;
22675
22676   switch_to_section (current_function_section ());
22677
22678   /* If requested, emit something human-readable.  */
22679   if (flag_debug_asm)
22680     fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
22681
22682   if (DWARF2_ASM_LINE_DEBUG_INFO)
22683     {
22684       /* Emit the .loc directive understood by GNU as.  */
22685       /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
22686          file_num, line, is_stmt, discriminator */
22687       fputs ("\t.loc ", asm_out_file);
22688       fprint_ul (asm_out_file, file_num);
22689       putc (' ', asm_out_file);
22690       fprint_ul (asm_out_file, line);
22691       putc (' ', asm_out_file);
22692       putc ('0', asm_out_file);
22693
22694       if (is_stmt != table->is_stmt)
22695         {
22696           fputs (" is_stmt ", asm_out_file);
22697           putc (is_stmt ? '1' : '0', asm_out_file);
22698         }
22699       if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
22700         {
22701           gcc_assert (discriminator > 0);
22702           fputs (" discriminator ", asm_out_file);
22703           fprint_ul (asm_out_file, (unsigned long) discriminator);
22704         }
22705       putc ('\n', asm_out_file);
22706     }
22707   else
22708     {
22709       unsigned int label_num = ++line_info_label_num;
22710
22711       targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
22712
22713       push_dw_line_info_entry (table, LI_set_address, label_num);
22714       if (file_num != table->file_num)
22715         push_dw_line_info_entry (table, LI_set_file, file_num);
22716       if (discriminator != table->discrim_num)
22717         push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
22718       if (is_stmt != table->is_stmt)
22719         push_dw_line_info_entry (table, LI_negate_stmt, 0);
22720       push_dw_line_info_entry (table, LI_set_line, line);
22721     }
22722
22723   table->file_num = file_num;
22724   table->line_num = line;
22725   table->discrim_num = discriminator;
22726   table->is_stmt = is_stmt;
22727   table->in_use = true;
22728 }
22729
22730 /* Record the beginning of a new source file.  */
22731
22732 static void
22733 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
22734 {
22735   if (flag_eliminate_dwarf2_dups)
22736     {
22737       /* Record the beginning of the file for break_out_includes.  */
22738       dw_die_ref bincl_die;
22739
22740       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
22741       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
22742     }
22743
22744   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22745     {
22746       macinfo_entry e;
22747       e.code = DW_MACINFO_start_file;
22748       e.lineno = lineno;
22749       e.info = ggc_strdup (filename);
22750       vec_safe_push (macinfo_table, e);
22751     }
22752 }
22753
22754 /* Record the end of a source file.  */
22755
22756 static void
22757 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
22758 {
22759   if (flag_eliminate_dwarf2_dups)
22760     /* Record the end of the file for break_out_includes.  */
22761     new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
22762
22763   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22764     {
22765       macinfo_entry e;
22766       e.code = DW_MACINFO_end_file;
22767       e.lineno = lineno;
22768       e.info = NULL;
22769       vec_safe_push (macinfo_table, e);
22770     }
22771 }
22772
22773 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
22774    the tail part of the directive line, i.e. the part which is past the
22775    initial whitespace, #, whitespace, directive-name, whitespace part.  */
22776
22777 static void
22778 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
22779                   const char *buffer ATTRIBUTE_UNUSED)
22780 {
22781   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22782     {
22783       macinfo_entry e;
22784       /* Insert a dummy first entry to be able to optimize the whole
22785          predefined macro block using DW_MACRO_GNU_transparent_include.  */
22786       if (macinfo_table->is_empty () && lineno <= 1)
22787         {
22788           e.code = 0;
22789           e.lineno = 0;
22790           e.info = NULL;
22791           vec_safe_push (macinfo_table, e);
22792         }
22793       e.code = DW_MACINFO_define;
22794       e.lineno = lineno;
22795       e.info = ggc_strdup (buffer);
22796       vec_safe_push (macinfo_table, e);
22797     }
22798 }
22799
22800 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
22801    the tail part of the directive line, i.e. the part which is past the
22802    initial whitespace, #, whitespace, directive-name, whitespace part.  */
22803
22804 static void
22805 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
22806                  const char *buffer ATTRIBUTE_UNUSED)
22807 {
22808   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22809     {
22810       macinfo_entry e;
22811       /* Insert a dummy first entry to be able to optimize the whole
22812          predefined macro block using DW_MACRO_GNU_transparent_include.  */
22813       if (macinfo_table->is_empty () && lineno <= 1)
22814         {
22815           e.code = 0;
22816           e.lineno = 0;
22817           e.info = NULL;
22818           vec_safe_push (macinfo_table, e);
22819         }
22820       e.code = DW_MACINFO_undef;
22821       e.lineno = lineno;
22822       e.info = ggc_strdup (buffer);
22823       vec_safe_push (macinfo_table, e);
22824     }
22825 }
22826
22827 /* Helpers to manipulate hash table of CUs.  */
22828
22829 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
22830 {
22831   static inline hashval_t hash (const macinfo_entry *);
22832   static inline bool equal (const macinfo_entry *, const macinfo_entry *);
22833 };
22834
22835 inline hashval_t
22836 macinfo_entry_hasher::hash (const macinfo_entry *entry)
22837 {
22838   return htab_hash_string (entry->info);
22839 }
22840
22841 inline bool
22842 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
22843                              const macinfo_entry *entry2)
22844 {
22845   return !strcmp (entry1->info, entry2->info);
22846 }
22847
22848 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
22849
22850 /* Output a single .debug_macinfo entry.  */
22851
22852 static void
22853 output_macinfo_op (macinfo_entry *ref)
22854 {
22855   int file_num;
22856   size_t len;
22857   struct indirect_string_node *node;
22858   char label[MAX_ARTIFICIAL_LABEL_BYTES];
22859   struct dwarf_file_data *fd;
22860
22861   switch (ref->code)
22862     {
22863     case DW_MACINFO_start_file:
22864       fd = lookup_filename (ref->info);
22865       file_num = maybe_emit_file (fd);
22866       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
22867       dw2_asm_output_data_uleb128 (ref->lineno,
22868                                    "Included from line number %lu", 
22869                                    (unsigned long) ref->lineno);
22870       dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
22871       break;
22872     case DW_MACINFO_end_file:
22873       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
22874       break;
22875     case DW_MACINFO_define:
22876     case DW_MACINFO_undef:
22877       len = strlen (ref->info) + 1;
22878       if (!dwarf_strict
22879           && len > DWARF_OFFSET_SIZE
22880           && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
22881           && (debug_str_section->common.flags & SECTION_MERGE) != 0)
22882         {
22883           ref->code = ref->code == DW_MACINFO_define
22884                       ? DW_MACRO_GNU_define_indirect
22885                       : DW_MACRO_GNU_undef_indirect;
22886           output_macinfo_op (ref);
22887           return;
22888         }
22889       dw2_asm_output_data (1, ref->code,
22890                            ref->code == DW_MACINFO_define
22891                            ? "Define macro" : "Undefine macro");
22892       dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu", 
22893                                    (unsigned long) ref->lineno);
22894       dw2_asm_output_nstring (ref->info, -1, "The macro");
22895       break;
22896     case DW_MACRO_GNU_define_indirect:
22897     case DW_MACRO_GNU_undef_indirect:
22898       node = find_AT_string (ref->info);
22899       gcc_assert (node
22900                   && ((node->form == DW_FORM_strp)
22901                       || (node->form == DW_FORM_GNU_str_index)));
22902       dw2_asm_output_data (1, ref->code,
22903                            ref->code == DW_MACRO_GNU_define_indirect
22904                            ? "Define macro indirect"
22905                            : "Undefine macro indirect");
22906       dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22907                                    (unsigned long) ref->lineno);
22908       if (node->form == DW_FORM_strp)
22909         dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
22910                                debug_str_section, "The macro: \"%s\"",
22911                                ref->info);
22912       else
22913         dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
22914                                      ref->info);
22915       break;
22916     case DW_MACRO_GNU_transparent_include:
22917       dw2_asm_output_data (1, ref->code, "Transparent include");
22918       ASM_GENERATE_INTERNAL_LABEL (label,
22919                                    DEBUG_MACRO_SECTION_LABEL, ref->lineno);
22920       dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
22921       break;
22922     default:
22923       fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
22924                ASM_COMMENT_START, (unsigned long) ref->code);
22925       break;
22926     }
22927 }
22928
22929 /* Attempt to make a sequence of define/undef macinfo ops shareable with
22930    other compilation unit .debug_macinfo sections.  IDX is the first
22931    index of a define/undef, return the number of ops that should be
22932    emitted in a comdat .debug_macinfo section and emit
22933    a DW_MACRO_GNU_transparent_include entry referencing it.
22934    If the define/undef entry should be emitted normally, return 0.  */
22935
22936 static unsigned
22937 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
22938                         macinfo_hash_type **macinfo_htab)
22939 {
22940   macinfo_entry *first, *second, *cur, *inc;
22941   char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
22942   unsigned char checksum[16];
22943   struct md5_ctx ctx;
22944   char *grp_name, *tail;
22945   const char *base;
22946   unsigned int i, count, encoded_filename_len, linebuf_len;
22947   macinfo_entry **slot;
22948
22949   first = &(*macinfo_table)[idx];
22950   second = &(*macinfo_table)[idx + 1];
22951
22952   /* Optimize only if there are at least two consecutive define/undef ops,
22953      and either all of them are before first DW_MACINFO_start_file
22954      with lineno {0,1} (i.e. predefined macro block), or all of them are
22955      in some included header file.  */
22956   if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
22957     return 0;
22958   if (vec_safe_is_empty (files))
22959     {
22960       if (first->lineno > 1 || second->lineno > 1)
22961         return 0;
22962     }
22963   else if (first->lineno == 0)
22964     return 0;
22965
22966   /* Find the last define/undef entry that can be grouped together
22967      with first and at the same time compute md5 checksum of their
22968      codes, linenumbers and strings.  */
22969   md5_init_ctx (&ctx);
22970   for (i = idx; macinfo_table->iterate (i, &cur); i++)
22971     if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
22972       break;
22973     else if (vec_safe_is_empty (files) && cur->lineno > 1)
22974       break;
22975     else
22976       {
22977         unsigned char code = cur->code;
22978         md5_process_bytes (&code, 1, &ctx);
22979         checksum_uleb128 (cur->lineno, &ctx);
22980         md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
22981       }
22982   md5_finish_ctx (&ctx, checksum);
22983   count = i - idx;
22984
22985   /* From the containing include filename (if any) pick up just
22986      usable characters from its basename.  */
22987   if (vec_safe_is_empty (files))
22988     base = "";
22989   else
22990     base = lbasename (files->last ().info);
22991   for (encoded_filename_len = 0, i = 0; base[i]; i++)
22992     if (ISIDNUM (base[i]) || base[i] == '.')
22993       encoded_filename_len++;
22994   /* Count . at the end.  */
22995   if (encoded_filename_len)
22996     encoded_filename_len++;
22997
22998   sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
22999   linebuf_len = strlen (linebuf);
23000
23001   /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum>  */
23002   grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
23003                          + 16 * 2 + 1);
23004   memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
23005   tail = grp_name + 4;
23006   if (encoded_filename_len)
23007     {
23008       for (i = 0; base[i]; i++)
23009         if (ISIDNUM (base[i]) || base[i] == '.')
23010           *tail++ = base[i];
23011       *tail++ = '.';
23012     }
23013   memcpy (tail, linebuf, linebuf_len);
23014   tail += linebuf_len;
23015   *tail++ = '.';
23016   for (i = 0; i < 16; i++)
23017     sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
23018
23019   /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
23020      in the empty vector entry before the first define/undef.  */
23021   inc = &(*macinfo_table)[idx - 1];
23022   inc->code = DW_MACRO_GNU_transparent_include;
23023   inc->lineno = 0;
23024   inc->info = ggc_strdup (grp_name);
23025   if (!*macinfo_htab)
23026     *macinfo_htab = new macinfo_hash_type (10);
23027   /* Avoid emitting duplicates.  */
23028   slot = (*macinfo_htab)->find_slot (inc, INSERT);
23029   if (*slot != NULL)
23030     {
23031       inc->code = 0;
23032       inc->info = NULL;
23033       /* If such an entry has been used before, just emit
23034          a DW_MACRO_GNU_transparent_include op.  */
23035       inc = *slot;
23036       output_macinfo_op (inc);
23037       /* And clear all macinfo_entry in the range to avoid emitting them
23038          in the second pass.  */
23039       for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
23040         {
23041           cur->code = 0;
23042           cur->info = NULL;
23043         }
23044     }
23045   else
23046     {
23047       *slot = inc;
23048       inc->lineno = (*macinfo_htab)->elements ();
23049       output_macinfo_op (inc);
23050     }
23051   return count;
23052 }
23053
23054 /* Save any strings needed by the macinfo table in the debug str
23055    table.  All strings must be collected into the table by the time
23056    index_string is called.  */
23057
23058 static void
23059 save_macinfo_strings (void)
23060 {
23061   unsigned len;
23062   unsigned i;
23063   macinfo_entry *ref;
23064
23065   for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
23066     {
23067       switch (ref->code)
23068         {
23069           /* Match the logic in output_macinfo_op to decide on
23070              indirect strings.  */
23071           case DW_MACINFO_define:
23072           case DW_MACINFO_undef:
23073             len = strlen (ref->info) + 1;
23074             if (!dwarf_strict
23075                 && len > DWARF_OFFSET_SIZE
23076                 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
23077                 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
23078               set_indirect_string (find_AT_string (ref->info));
23079             break;
23080           case DW_MACRO_GNU_define_indirect:
23081           case DW_MACRO_GNU_undef_indirect:
23082             set_indirect_string (find_AT_string (ref->info));
23083             break;
23084           default:
23085             break;
23086         }
23087     }
23088 }
23089
23090 /* Output macinfo section(s).  */
23091
23092 static void
23093 output_macinfo (void)
23094 {
23095   unsigned i;
23096   unsigned long length = vec_safe_length (macinfo_table);
23097   macinfo_entry *ref;
23098   vec<macinfo_entry, va_gc> *files = NULL;
23099   macinfo_hash_type *macinfo_htab = NULL;
23100
23101   if (! length)
23102     return;
23103
23104   /* output_macinfo* uses these interchangeably.  */
23105   gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
23106               && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
23107               && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
23108               && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
23109
23110   /* For .debug_macro emit the section header.  */
23111   if (!dwarf_strict)
23112     {
23113       dw2_asm_output_data (2, 4, "DWARF macro version number");
23114       if (DWARF_OFFSET_SIZE == 8)
23115         dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
23116       else
23117         dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
23118       dw2_asm_output_offset (DWARF_OFFSET_SIZE,
23119                              (!dwarf_split_debug_info ? debug_line_section_label
23120                               : debug_skeleton_line_section_label),
23121                              debug_line_section, NULL);
23122     }
23123
23124   /* In the first loop, it emits the primary .debug_macinfo section
23125      and after each emitted op the macinfo_entry is cleared.
23126      If a longer range of define/undef ops can be optimized using
23127      DW_MACRO_GNU_transparent_include, the
23128      DW_MACRO_GNU_transparent_include op is emitted and kept in
23129      the vector before the first define/undef in the range and the
23130      whole range of define/undef ops is not emitted and kept.  */
23131   for (i = 0; macinfo_table->iterate (i, &ref); i++)
23132     {
23133       switch (ref->code)
23134         {
23135         case DW_MACINFO_start_file:
23136           vec_safe_push (files, *ref);
23137           break;
23138         case DW_MACINFO_end_file:
23139           if (!vec_safe_is_empty (files))
23140             files->pop ();
23141           break;
23142         case DW_MACINFO_define:
23143         case DW_MACINFO_undef:
23144           if (!dwarf_strict
23145               && HAVE_COMDAT_GROUP
23146               && vec_safe_length (files) != 1
23147               && i > 0
23148               && i + 1 < length
23149               && (*macinfo_table)[i - 1].code == 0)
23150             {
23151               unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
23152               if (count)
23153                 {
23154                   i += count - 1;
23155                   continue;
23156                 }
23157             }
23158           break;
23159         case 0:
23160           /* A dummy entry may be inserted at the beginning to be able
23161              to optimize the whole block of predefined macros.  */
23162           if (i == 0)
23163             continue;
23164         default:
23165           break;
23166         }
23167       output_macinfo_op (ref);
23168       ref->info = NULL;
23169       ref->code = 0;
23170     }
23171
23172   if (!macinfo_htab)
23173     return;
23174
23175   delete macinfo_htab;
23176   macinfo_htab = NULL;
23177
23178   /* If any DW_MACRO_GNU_transparent_include were used, on those
23179      DW_MACRO_GNU_transparent_include entries terminate the
23180      current chain and switch to a new comdat .debug_macinfo
23181      section and emit the define/undef entries within it.  */
23182   for (i = 0; macinfo_table->iterate (i, &ref); i++)
23183     switch (ref->code)
23184       {
23185       case 0:
23186         continue;
23187       case DW_MACRO_GNU_transparent_include:
23188         {
23189           char label[MAX_ARTIFICIAL_LABEL_BYTES];
23190           tree comdat_key = get_identifier (ref->info);
23191           /* Terminate the previous .debug_macinfo section.  */
23192           dw2_asm_output_data (1, 0, "End compilation unit");
23193           targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
23194                                          SECTION_DEBUG
23195                                          | SECTION_LINKONCE,
23196                                          comdat_key);
23197           ASM_GENERATE_INTERNAL_LABEL (label,
23198                                        DEBUG_MACRO_SECTION_LABEL,
23199                                        ref->lineno);
23200           ASM_OUTPUT_LABEL (asm_out_file, label);
23201           ref->code = 0;
23202           ref->info = NULL;
23203           dw2_asm_output_data (2, 4, "DWARF macro version number");
23204           if (DWARF_OFFSET_SIZE == 8)
23205             dw2_asm_output_data (1, 1, "Flags: 64-bit");
23206           else
23207             dw2_asm_output_data (1, 0, "Flags: 32-bit");
23208         }
23209         break;
23210       case DW_MACINFO_define:
23211       case DW_MACINFO_undef:
23212         output_macinfo_op (ref);
23213         ref->code = 0;
23214         ref->info = NULL;
23215         break;
23216       default:
23217         gcc_unreachable ();
23218       }
23219 }
23220
23221 /* Set up for Dwarf output at the start of compilation.  */
23222
23223 static void
23224 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
23225 {
23226   /* This option is currently broken, see (PR53118 and PR46102).  */
23227   if (flag_eliminate_dwarf2_dups
23228       && strstr (lang_hooks.name, "C++"))
23229     {
23230       warning (0, "-feliminate-dwarf2-dups is broken for C++, ignoring");
23231       flag_eliminate_dwarf2_dups = 0;
23232     }
23233
23234   /* Allocate the file_table.  */
23235   file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
23236
23237 #ifndef DWARF2_LINENO_DEBUGGING_INFO
23238   /* Allocate the decl_die_table.  */
23239   decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
23240
23241   /* Allocate the decl_loc_table.  */
23242   decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
23243
23244   /* Allocate the cached_dw_loc_list_table.  */
23245   cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
23246
23247   /* Allocate the initial hunk of the decl_scope_table.  */
23248   vec_alloc (decl_scope_table, 256);
23249
23250   /* Allocate the initial hunk of the abbrev_die_table.  */
23251   abbrev_die_table = ggc_cleared_vec_alloc<dw_die_ref>
23252     (ABBREV_DIE_TABLE_INCREMENT);
23253   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
23254   /* Zero-th entry is allocated, but unused.  */
23255   abbrev_die_table_in_use = 1;
23256
23257   /* Allocate the pubtypes and pubnames vectors.  */
23258   vec_alloc (pubname_table, 32);
23259   vec_alloc (pubtype_table, 32);
23260
23261   vec_alloc (incomplete_types, 64);
23262
23263   vec_alloc (used_rtx_array, 32);
23264
23265   if (!dwarf_split_debug_info)
23266     {
23267       debug_info_section = get_section (DEBUG_INFO_SECTION,
23268                                         SECTION_DEBUG, NULL);
23269       debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
23270                                           SECTION_DEBUG, NULL);
23271       debug_loc_section = get_section (DEBUG_LOC_SECTION,
23272                                        SECTION_DEBUG, NULL);
23273     }
23274   else
23275     {
23276       debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
23277                                         SECTION_DEBUG | SECTION_EXCLUDE, NULL);
23278       debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
23279                                           SECTION_DEBUG | SECTION_EXCLUDE,
23280                                           NULL);
23281       debug_addr_section = get_section (DEBUG_ADDR_SECTION,
23282                                         SECTION_DEBUG, NULL);
23283       debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
23284                                                  SECTION_DEBUG, NULL);
23285       debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
23286                                                    SECTION_DEBUG, NULL);
23287       ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
23288                                   DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
23289
23290       /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
23291          the main .o, but the skeleton_line goes into the split off dwo.  */
23292       debug_skeleton_line_section
23293           = get_section (DEBUG_DWO_LINE_SECTION,
23294                          SECTION_DEBUG | SECTION_EXCLUDE, NULL);
23295       ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
23296                                    DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
23297       debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
23298                                                SECTION_DEBUG | SECTION_EXCLUDE,
23299                                                NULL);
23300       ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
23301                                    DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
23302       debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
23303                                        SECTION_DEBUG | SECTION_EXCLUDE, NULL);
23304       debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
23305                                            DEBUG_STR_DWO_SECTION_FLAGS, NULL);
23306     }
23307   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
23308                                        SECTION_DEBUG, NULL);
23309   debug_macinfo_section = get_section (dwarf_strict
23310                                        ? DEBUG_MACINFO_SECTION
23311                                        : DEBUG_MACRO_SECTION,
23312                                        DEBUG_MACRO_SECTION_FLAGS, NULL);
23313   debug_line_section = get_section (DEBUG_LINE_SECTION,
23314                                     SECTION_DEBUG, NULL);
23315   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
23316                                         SECTION_DEBUG, NULL);
23317   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
23318                                         SECTION_DEBUG, NULL);
23319   debug_str_section = get_section (DEBUG_STR_SECTION,
23320                                    DEBUG_STR_SECTION_FLAGS, NULL);
23321   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
23322                                       SECTION_DEBUG, NULL);
23323   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
23324                                      SECTION_DEBUG, NULL);
23325
23326   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
23327   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
23328                                DEBUG_ABBREV_SECTION_LABEL, 0);
23329   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
23330   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
23331                                COLD_TEXT_SECTION_LABEL, 0);
23332   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
23333
23334   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
23335                                DEBUG_INFO_SECTION_LABEL, 0);
23336   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
23337                                DEBUG_LINE_SECTION_LABEL, 0);
23338   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
23339                                DEBUG_RANGES_SECTION_LABEL, 0);
23340   ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
23341                                DEBUG_ADDR_SECTION_LABEL, 0);
23342   ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
23343                                dwarf_strict
23344                                ? DEBUG_MACINFO_SECTION_LABEL
23345                                : DEBUG_MACRO_SECTION_LABEL, 0);
23346   ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
23347
23348   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23349     vec_alloc (macinfo_table, 64);
23350
23351   switch_to_section (text_section);
23352   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
23353 #endif
23354
23355   /* Make sure the line number table for .text always exists.  */
23356   text_section_line_info = new_line_info_table ();
23357   text_section_line_info->end_label = text_end_label;
23358
23359 #ifdef DWARF2_LINENO_DEBUGGING_INFO
23360   cur_line_info_table = text_section_line_info;
23361 #endif
23362
23363   /* If front-ends already registered a main translation unit but we were not
23364      ready to perform the association, do this now.  */
23365   if (main_translation_unit != NULL_TREE)
23366     equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
23367 }
23368
23369 /* Called before compile () starts outputtting functions, variables
23370    and toplevel asms into assembly.  */
23371
23372 static void
23373 dwarf2out_assembly_start (void)
23374 {
23375   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
23376       && dwarf2out_do_cfi_asm ()
23377       && (!(flag_unwind_tables || flag_exceptions)
23378           || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
23379     fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
23380 }
23381
23382 /* A helper function for dwarf2out_finish called through
23383    htab_traverse.  Assign a string its index.  All strings must be
23384    collected into the table by the time index_string is called,
23385    because the indexing code relies on htab_traverse to traverse nodes
23386    in the same order for each run. */
23387
23388 int
23389 index_string (indirect_string_node **h, unsigned int *index)
23390 {
23391   indirect_string_node *node = *h;
23392
23393   find_string_form (node);
23394   if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
23395     {
23396       gcc_assert (node->index == NO_INDEX_ASSIGNED);
23397       node->index = *index;
23398       *index += 1;
23399     }
23400   return 1;
23401 }
23402
23403 /* A helper function for output_indirect_strings called through
23404    htab_traverse.  Output the offset to a string and update the
23405    current offset.  */
23406
23407 int
23408 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
23409 {
23410   indirect_string_node *node = *h;
23411
23412   if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
23413     {
23414       /* Assert that this node has been assigned an index.  */
23415       gcc_assert (node->index != NO_INDEX_ASSIGNED
23416                   && node->index != NOT_INDEXED);
23417       dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
23418                            "indexed string 0x%x: %s", node->index, node->str);
23419       *offset += strlen (node->str) + 1;
23420     }
23421   return 1;
23422 }
23423
23424 /* A helper function for dwarf2out_finish called through
23425    htab_traverse.  Output the indexed string.  */
23426
23427 int
23428 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
23429 {
23430   struct indirect_string_node *node = *h;
23431
23432   if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
23433     {
23434       /* Assert that the strings are output in the same order as their
23435          indexes were assigned.  */
23436       gcc_assert (*cur_idx == node->index);
23437       assemble_string (node->str, strlen (node->str) + 1);
23438       *cur_idx += 1;
23439     }
23440   return 1;
23441 }
23442
23443 /* A helper function for dwarf2out_finish called through
23444    htab_traverse.  Emit one queued .debug_str string.  */
23445
23446 int
23447 output_indirect_string (indirect_string_node **h, void *)
23448 {
23449   struct indirect_string_node *node = *h;
23450
23451   node->form = find_string_form (node);
23452   if (node->form == DW_FORM_strp && node->refcount > 0)
23453     {
23454       ASM_OUTPUT_LABEL (asm_out_file, node->label);
23455       assemble_string (node->str, strlen (node->str) + 1);
23456     }
23457
23458   return 1;
23459 }
23460
23461 /* Output the indexed string table.  */
23462
23463 static void
23464 output_indirect_strings (void)
23465 {
23466   switch_to_section (debug_str_section);
23467   if (!dwarf_split_debug_info)
23468     debug_str_hash->traverse<void *, output_indirect_string> (NULL);
23469   else
23470     {
23471       unsigned int offset = 0;
23472       unsigned int cur_idx = 0;
23473
23474       skeleton_debug_str_hash->traverse<void *, output_indirect_string> (NULL);
23475
23476       switch_to_section (debug_str_offsets_section);
23477       debug_str_hash->traverse_noresize
23478         <unsigned int *, output_index_string_offset> (&offset);
23479       switch_to_section (debug_str_dwo_section);
23480       debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
23481         (&cur_idx);
23482     }
23483 }
23484
23485 /* Callback for htab_traverse to assign an index to an entry in the
23486    table, and to write that entry to the .debug_addr section.  */
23487
23488 int
23489 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
23490 {
23491   addr_table_entry *entry = *slot;
23492
23493   if (entry->refcount == 0)
23494     {
23495       gcc_assert (entry->index == NO_INDEX_ASSIGNED
23496                   || entry->index == NOT_INDEXED);
23497       return 1;
23498     }
23499
23500   gcc_assert (entry->index == *cur_index);
23501   (*cur_index)++;
23502
23503   switch (entry->kind)
23504     {
23505       case ate_kind_rtx:
23506         dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
23507                                  "0x%x", entry->index);
23508         break;
23509       case ate_kind_rtx_dtprel:
23510         gcc_assert (targetm.asm_out.output_dwarf_dtprel);
23511         targetm.asm_out.output_dwarf_dtprel (asm_out_file,
23512                                              DWARF2_ADDR_SIZE,
23513                                              entry->addr.rtl);
23514         fputc ('\n', asm_out_file);
23515         break;
23516       case ate_kind_label:
23517         dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
23518                                  "0x%x", entry->index);
23519         break;
23520       default:
23521         gcc_unreachable ();
23522     }
23523   return 1;
23524 }
23525
23526 /* Produce the .debug_addr section.  */
23527
23528 static void
23529 output_addr_table (void)
23530 {
23531   unsigned int index = 0;
23532   if (addr_index_table == NULL || addr_index_table->size () == 0)
23533     return;
23534
23535   switch_to_section (debug_addr_section);
23536   addr_index_table
23537     ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
23538 }
23539
23540 #if ENABLE_ASSERT_CHECKING
23541 /* Verify that all marks are clear.  */
23542
23543 static void
23544 verify_marks_clear (dw_die_ref die)
23545 {
23546   dw_die_ref c;
23547
23548   gcc_assert (! die->die_mark);
23549   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
23550 }
23551 #endif /* ENABLE_ASSERT_CHECKING */
23552
23553 /* Clear the marks for a die and its children.
23554    Be cool if the mark isn't set.  */
23555
23556 static void
23557 prune_unmark_dies (dw_die_ref die)
23558 {
23559   dw_die_ref c;
23560
23561   if (die->die_mark)
23562     die->die_mark = 0;
23563   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
23564 }
23565
23566 /* Given DIE that we're marking as used, find any other dies
23567    it references as attributes and mark them as used.  */
23568
23569 static void
23570 prune_unused_types_walk_attribs (dw_die_ref die)
23571 {
23572   dw_attr_node *a;
23573   unsigned ix;
23574
23575   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23576     {
23577       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
23578         {
23579           /* A reference to another DIE.
23580              Make sure that it will get emitted.
23581              If it was broken out into a comdat group, don't follow it.  */
23582           if (! AT_ref (a)->comdat_type_p
23583               || a->dw_attr == DW_AT_specification)
23584             prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
23585         }
23586       /* Set the string's refcount to 0 so that prune_unused_types_mark
23587          accounts properly for it.  */
23588       if (AT_class (a) == dw_val_class_str)
23589         a->dw_attr_val.v.val_str->refcount = 0;
23590     }
23591 }
23592
23593 /* Mark the generic parameters and arguments children DIEs of DIE.  */
23594
23595 static void
23596 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
23597 {
23598   dw_die_ref c;
23599
23600   if (die == NULL || die->die_child == NULL)
23601     return;
23602   c = die->die_child;
23603   do
23604     {
23605       if (is_template_parameter (c))
23606         prune_unused_types_mark (c, 1);
23607       c = c->die_sib;
23608     } while (c && c != die->die_child);
23609 }
23610
23611 /* Mark DIE as being used.  If DOKIDS is true, then walk down
23612    to DIE's children.  */
23613
23614 static void
23615 prune_unused_types_mark (dw_die_ref die, int dokids)
23616 {
23617   dw_die_ref c;
23618
23619   if (die->die_mark == 0)
23620     {
23621       /* We haven't done this node yet.  Mark it as used.  */
23622       die->die_mark = 1;
23623       /* If this is the DIE of a generic type instantiation,
23624          mark the children DIEs that describe its generic parms and
23625          args.  */
23626       prune_unused_types_mark_generic_parms_dies (die);
23627
23628       /* We also have to mark its parents as used.
23629          (But we don't want to mark our parent's kids due to this,
23630          unless it is a class.)  */
23631       if (die->die_parent)
23632         prune_unused_types_mark (die->die_parent,
23633                                  class_scope_p (die->die_parent));
23634
23635       /* Mark any referenced nodes.  */
23636       prune_unused_types_walk_attribs (die);
23637
23638       /* If this node is a specification,
23639          also mark the definition, if it exists.  */
23640       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
23641         prune_unused_types_mark (die->die_definition, 1);
23642     }
23643
23644   if (dokids && die->die_mark != 2)
23645     {
23646       /* We need to walk the children, but haven't done so yet.
23647          Remember that we've walked the kids.  */
23648       die->die_mark = 2;
23649
23650       /* If this is an array type, we need to make sure our
23651          kids get marked, even if they're types.  If we're
23652          breaking out types into comdat sections, do this
23653          for all type definitions.  */
23654       if (die->die_tag == DW_TAG_array_type
23655           || (use_debug_types
23656               && is_type_die (die) && ! is_declaration_die (die)))
23657         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
23658       else
23659         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
23660     }
23661 }
23662
23663 /* For local classes, look if any static member functions were emitted
23664    and if so, mark them.  */
23665
23666 static void
23667 prune_unused_types_walk_local_classes (dw_die_ref die)
23668 {
23669   dw_die_ref c;
23670
23671   if (die->die_mark == 2)
23672     return;
23673
23674   switch (die->die_tag)
23675     {
23676     case DW_TAG_structure_type:
23677     case DW_TAG_union_type:
23678     case DW_TAG_class_type:
23679       break;
23680
23681     case DW_TAG_subprogram:
23682       if (!get_AT_flag (die, DW_AT_declaration)
23683           || die->die_definition != NULL)
23684         prune_unused_types_mark (die, 1);
23685       return;
23686
23687     default:
23688       return;
23689     }
23690
23691   /* Mark children.  */
23692   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
23693 }
23694
23695 /* Walk the tree DIE and mark types that we actually use.  */
23696
23697 static void
23698 prune_unused_types_walk (dw_die_ref die)
23699 {
23700   dw_die_ref c;
23701
23702   /* Don't do anything if this node is already marked and
23703      children have been marked as well.  */
23704   if (die->die_mark == 2)
23705     return;
23706
23707   switch (die->die_tag)
23708     {
23709     case DW_TAG_structure_type:
23710     case DW_TAG_union_type:
23711     case DW_TAG_class_type:
23712       if (die->die_perennial_p)
23713         break;
23714
23715       for (c = die->die_parent; c; c = c->die_parent)
23716         if (c->die_tag == DW_TAG_subprogram)
23717           break;
23718
23719       /* Finding used static member functions inside of classes
23720          is needed just for local classes, because for other classes
23721          static member function DIEs with DW_AT_specification
23722          are emitted outside of the DW_TAG_*_type.  If we ever change
23723          it, we'd need to call this even for non-local classes.  */
23724       if (c)
23725         prune_unused_types_walk_local_classes (die);
23726
23727       /* It's a type node --- don't mark it.  */
23728       return;
23729
23730     case DW_TAG_const_type:
23731     case DW_TAG_packed_type:
23732     case DW_TAG_pointer_type:
23733     case DW_TAG_reference_type:
23734     case DW_TAG_rvalue_reference_type:
23735     case DW_TAG_volatile_type:
23736     case DW_TAG_typedef:
23737     case DW_TAG_array_type:
23738     case DW_TAG_interface_type:
23739     case DW_TAG_friend:
23740     case DW_TAG_variant_part:
23741     case DW_TAG_enumeration_type:
23742     case DW_TAG_subroutine_type:
23743     case DW_TAG_string_type:
23744     case DW_TAG_set_type:
23745     case DW_TAG_subrange_type:
23746     case DW_TAG_ptr_to_member_type:
23747     case DW_TAG_file_type:
23748       if (die->die_perennial_p)
23749         break;
23750
23751       /* It's a type node --- don't mark it.  */
23752       return;
23753
23754     default:
23755       /* Mark everything else.  */
23756       break;
23757   }
23758
23759   if (die->die_mark == 0)
23760     {
23761       die->die_mark = 1;
23762
23763       /* Now, mark any dies referenced from here.  */
23764       prune_unused_types_walk_attribs (die);
23765     }
23766
23767   die->die_mark = 2;
23768
23769   /* Mark children.  */
23770   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
23771 }
23772
23773 /* Increment the string counts on strings referred to from DIE's
23774    attributes.  */
23775
23776 static void
23777 prune_unused_types_update_strings (dw_die_ref die)
23778 {
23779   dw_attr_node *a;
23780   unsigned ix;
23781
23782   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23783     if (AT_class (a) == dw_val_class_str)
23784       {
23785         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
23786         s->refcount++;
23787         /* Avoid unnecessarily putting strings that are used less than
23788            twice in the hash table.  */
23789         if (s->refcount
23790             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
23791           {
23792             indirect_string_node **slot
23793               = debug_str_hash->find_slot_with_hash (s->str,
23794                                                      htab_hash_string (s->str),
23795                                                      INSERT);
23796             gcc_assert (*slot == NULL);
23797             *slot = s;
23798           }
23799       }
23800 }
23801
23802 /* Remove from the tree DIE any dies that aren't marked.  */
23803
23804 static void
23805 prune_unused_types_prune (dw_die_ref die)
23806 {
23807   dw_die_ref c;
23808
23809   gcc_assert (die->die_mark);
23810   prune_unused_types_update_strings (die);
23811
23812   if (! die->die_child)
23813     return;
23814
23815   c = die->die_child;
23816   do {
23817     dw_die_ref prev = c;
23818     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
23819       if (c == die->die_child)
23820         {
23821           /* No marked children between 'prev' and the end of the list.  */
23822           if (prev == c)
23823             /* No marked children at all.  */
23824             die->die_child = NULL;
23825           else
23826             {
23827               prev->die_sib = c->die_sib;
23828               die->die_child = prev;
23829             }
23830           return;
23831         }
23832
23833     if (c != prev->die_sib)
23834       prev->die_sib = c;
23835     prune_unused_types_prune (c);
23836   } while (c != die->die_child);
23837 }
23838
23839 /* Remove dies representing declarations that we never use.  */
23840
23841 static void
23842 prune_unused_types (void)
23843 {
23844   unsigned int i;
23845   limbo_die_node *node;
23846   comdat_type_node *ctnode;
23847   pubname_entry *pub;
23848   dw_die_ref base_type;
23849
23850 #if ENABLE_ASSERT_CHECKING
23851   /* All the marks should already be clear.  */
23852   verify_marks_clear (comp_unit_die ());
23853   for (node = limbo_die_list; node; node = node->next)
23854     verify_marks_clear (node->die);
23855   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23856     verify_marks_clear (ctnode->root_die);
23857 #endif /* ENABLE_ASSERT_CHECKING */
23858
23859   /* Mark types that are used in global variables.  */
23860   premark_types_used_by_global_vars ();
23861
23862   /* Set the mark on nodes that are actually used.  */
23863   prune_unused_types_walk (comp_unit_die ());
23864   for (node = limbo_die_list; node; node = node->next)
23865     prune_unused_types_walk (node->die);
23866   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23867     {
23868       prune_unused_types_walk (ctnode->root_die);
23869       prune_unused_types_mark (ctnode->type_die, 1);
23870     }
23871
23872   /* Also set the mark on nodes referenced from the pubname_table.  Enumerators
23873      are unusual in that they are pubnames that are the children of pubtypes.
23874      They should only be marked via their parent DW_TAG_enumeration_type die,
23875      not as roots in themselves.  */
23876   FOR_EACH_VEC_ELT (*pubname_table, i, pub)
23877     if (pub->die->die_tag != DW_TAG_enumerator)
23878       prune_unused_types_mark (pub->die, 1);
23879   for (i = 0; base_types.iterate (i, &base_type); i++)
23880     prune_unused_types_mark (base_type, 1);
23881
23882   if (debug_str_hash)
23883     debug_str_hash->empty ();
23884   if (skeleton_debug_str_hash)
23885     skeleton_debug_str_hash->empty ();
23886   prune_unused_types_prune (comp_unit_die ());
23887   for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
23888     {
23889       node = *pnode;
23890       if (!node->die->die_mark)
23891         *pnode = node->next;
23892       else
23893         {
23894           prune_unused_types_prune (node->die);
23895           pnode = &node->next;
23896         }
23897     }
23898   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23899     prune_unused_types_prune (ctnode->root_die);
23900
23901   /* Leave the marks clear.  */
23902   prune_unmark_dies (comp_unit_die ());
23903   for (node = limbo_die_list; node; node = node->next)
23904     prune_unmark_dies (node->die);
23905   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23906     prune_unmark_dies (ctnode->root_die);
23907 }
23908
23909 /* Set the parameter to true if there are any relative pathnames in
23910    the file table.  */
23911 int
23912 file_table_relative_p (dwarf_file_data **slot, bool *p)
23913 {
23914   struct dwarf_file_data *d = *slot;
23915   if (!IS_ABSOLUTE_PATH (d->filename))
23916     {
23917       *p = true;
23918       return 0;
23919     }
23920   return 1;
23921 }
23922
23923 /* Helpers to manipulate hash table of comdat type units.  */
23924
23925 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
23926 {
23927   static inline hashval_t hash (const comdat_type_node *);
23928   static inline bool equal (const comdat_type_node *, const comdat_type_node *);
23929 };
23930
23931 inline hashval_t
23932 comdat_type_hasher::hash (const comdat_type_node *type_node)
23933 {
23934   hashval_t h;
23935   memcpy (&h, type_node->signature, sizeof (h));
23936   return h;
23937 }
23938
23939 inline bool
23940 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
23941                            const comdat_type_node *type_node_2)
23942 {
23943   return (! memcmp (type_node_1->signature, type_node_2->signature,
23944                     DWARF_TYPE_SIGNATURE_SIZE));
23945 }
23946
23947 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
23948    to the location it would have been added, should we know its
23949    DECL_ASSEMBLER_NAME when we added other attributes.  This will
23950    probably improve compactness of debug info, removing equivalent
23951    abbrevs, and hide any differences caused by deferring the
23952    computation of the assembler name, triggered by e.g. PCH.  */
23953
23954 static inline void
23955 move_linkage_attr (dw_die_ref die)
23956 {
23957   unsigned ix = vec_safe_length (die->die_attr);
23958   dw_attr_node linkage = (*die->die_attr)[ix - 1];
23959
23960   gcc_assert (linkage.dw_attr == DW_AT_linkage_name
23961               || linkage.dw_attr == DW_AT_MIPS_linkage_name);
23962
23963   while (--ix > 0)
23964     {
23965       dw_attr_node *prev = &(*die->die_attr)[ix - 1];
23966
23967       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
23968         break;
23969     }
23970
23971   if (ix != vec_safe_length (die->die_attr) - 1)
23972     {
23973       die->die_attr->pop ();
23974       die->die_attr->quick_insert (ix, linkage);
23975     }
23976 }
23977
23978 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
23979    referenced from typed stack ops and count how often they are used.  */
23980
23981 static void
23982 mark_base_types (dw_loc_descr_ref loc)
23983 {
23984   dw_die_ref base_type = NULL;
23985
23986   for (; loc; loc = loc->dw_loc_next)
23987     {
23988       switch (loc->dw_loc_opc)
23989         {
23990         case DW_OP_GNU_regval_type:
23991         case DW_OP_GNU_deref_type:
23992           base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
23993           break;
23994         case DW_OP_GNU_convert:
23995         case DW_OP_GNU_reinterpret:
23996           if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
23997             continue;
23998           /* FALLTHRU */
23999         case DW_OP_GNU_const_type:
24000           base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
24001           break;
24002         case DW_OP_GNU_entry_value:
24003           mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
24004           continue;
24005         default:
24006           continue;
24007         }
24008       gcc_assert (base_type->die_parent == comp_unit_die ());
24009       if (base_type->die_mark)
24010         base_type->die_mark++;
24011       else
24012         {
24013           base_types.safe_push (base_type);
24014           base_type->die_mark = 1;
24015         }
24016     }
24017 }
24018
24019 /* Comparison function for sorting marked base types.  */
24020
24021 static int
24022 base_type_cmp (const void *x, const void *y)
24023 {
24024   dw_die_ref dx = *(const dw_die_ref *) x;
24025   dw_die_ref dy = *(const dw_die_ref *) y;
24026   unsigned int byte_size1, byte_size2;
24027   unsigned int encoding1, encoding2;
24028   if (dx->die_mark > dy->die_mark)
24029     return -1;
24030   if (dx->die_mark < dy->die_mark)
24031     return 1;
24032   byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
24033   byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
24034   if (byte_size1 < byte_size2)
24035     return 1;
24036   if (byte_size1 > byte_size2)
24037     return -1;
24038   encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
24039   encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
24040   if (encoding1 < encoding2)
24041     return 1;
24042   if (encoding1 > encoding2)
24043     return -1;
24044   return 0;
24045 }
24046
24047 /* Move base types marked by mark_base_types as early as possible
24048    in the CU, sorted by decreasing usage count both to make the
24049    uleb128 references as small as possible and to make sure they
24050    will have die_offset already computed by calc_die_sizes when
24051    sizes of typed stack loc ops is computed.  */
24052
24053 static void
24054 move_marked_base_types (void)
24055 {
24056   unsigned int i;
24057   dw_die_ref base_type, die, c;
24058
24059   if (base_types.is_empty ())
24060     return;
24061
24062   /* Sort by decreasing usage count, they will be added again in that
24063      order later on.  */
24064   base_types.qsort (base_type_cmp);
24065   die = comp_unit_die ();
24066   c = die->die_child;
24067   do
24068     {
24069       dw_die_ref prev = c;
24070       c = c->die_sib;
24071       while (c->die_mark)
24072         {
24073           remove_child_with_prev (c, prev);
24074           /* As base types got marked, there must be at least
24075              one node other than DW_TAG_base_type.  */
24076           gcc_assert (c != c->die_sib);
24077           c = c->die_sib;
24078         }
24079     }
24080   while (c != die->die_child);
24081   gcc_assert (die->die_child);
24082   c = die->die_child;
24083   for (i = 0; base_types.iterate (i, &base_type); i++)
24084     {
24085       base_type->die_mark = 0;
24086       base_type->die_sib = c->die_sib;
24087       c->die_sib = base_type;
24088       c = base_type;
24089     }
24090 }
24091
24092 /* Helper function for resolve_addr, attempt to resolve
24093    one CONST_STRING, return true if successful.  Similarly verify that
24094    SYMBOL_REFs refer to variables emitted in the current CU.  */
24095
24096 static bool
24097 resolve_one_addr (rtx *addr)
24098 {
24099   rtx rtl = *addr;
24100
24101   if (GET_CODE (rtl) == CONST_STRING)
24102     {
24103       size_t len = strlen (XSTR (rtl, 0)) + 1;
24104       tree t = build_string (len, XSTR (rtl, 0));
24105       tree tlen = size_int (len - 1);
24106       TREE_TYPE (t)
24107         = build_array_type (char_type_node, build_index_type (tlen));
24108       rtl = lookup_constant_def (t);
24109       if (!rtl || !MEM_P (rtl))
24110         return false;
24111       rtl = XEXP (rtl, 0);
24112       if (GET_CODE (rtl) == SYMBOL_REF
24113           && SYMBOL_REF_DECL (rtl)
24114           && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
24115         return false;
24116       vec_safe_push (used_rtx_array, rtl);
24117       *addr = rtl;
24118       return true;
24119     }
24120
24121   if (GET_CODE (rtl) == SYMBOL_REF
24122       && SYMBOL_REF_DECL (rtl))
24123     {
24124       if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
24125         {
24126           if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
24127             return false;
24128         }
24129       else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
24130         return false;
24131     }
24132
24133   if (GET_CODE (rtl) == CONST)
24134     {
24135       subrtx_ptr_iterator::array_type array;
24136       FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
24137         if (!resolve_one_addr (*iter))
24138           return false;
24139     }
24140
24141   return true;
24142 }
24143
24144 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
24145    if possible, and create DW_TAG_dwarf_procedure that can be referenced
24146    from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet.  */
24147
24148 static rtx
24149 string_cst_pool_decl (tree t)
24150 {
24151   rtx rtl = output_constant_def (t, 1);
24152   unsigned char *array;
24153   dw_loc_descr_ref l;
24154   tree decl;
24155   size_t len;
24156   dw_die_ref ref;
24157
24158   if (!rtl || !MEM_P (rtl))
24159     return NULL_RTX;
24160   rtl = XEXP (rtl, 0);
24161   if (GET_CODE (rtl) != SYMBOL_REF
24162       || SYMBOL_REF_DECL (rtl) == NULL_TREE)
24163     return NULL_RTX;
24164
24165   decl = SYMBOL_REF_DECL (rtl);
24166   if (!lookup_decl_die (decl))
24167     {
24168       len = TREE_STRING_LENGTH (t);
24169       vec_safe_push (used_rtx_array, rtl);
24170       ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
24171       array = ggc_vec_alloc<unsigned char> (len);
24172       memcpy (array, TREE_STRING_POINTER (t), len);
24173       l = new_loc_descr (DW_OP_implicit_value, len, 0);
24174       l->dw_loc_oprnd2.val_class = dw_val_class_vec;
24175       l->dw_loc_oprnd2.v.val_vec.length = len;
24176       l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
24177       l->dw_loc_oprnd2.v.val_vec.array = array;
24178       add_AT_loc (ref, DW_AT_location, l);
24179       equate_decl_number_to_die (decl, ref);
24180     }
24181   return rtl;
24182 }
24183
24184 /* Helper function of resolve_addr_in_expr.  LOC is
24185    a DW_OP_addr followed by DW_OP_stack_value, either at the start
24186    of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
24187    resolved.  Replace it (both DW_OP_addr and DW_OP_stack_value)
24188    with DW_OP_GNU_implicit_pointer if possible
24189    and return true, if unsuccessful, return false.  */
24190
24191 static bool
24192 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
24193 {
24194   rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
24195   HOST_WIDE_INT offset = 0;
24196   dw_die_ref ref = NULL;
24197   tree decl;
24198
24199   if (GET_CODE (rtl) == CONST
24200       && GET_CODE (XEXP (rtl, 0)) == PLUS
24201       && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
24202     {
24203       offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
24204       rtl = XEXP (XEXP (rtl, 0), 0);
24205     }
24206   if (GET_CODE (rtl) == CONST_STRING)
24207     {
24208       size_t len = strlen (XSTR (rtl, 0)) + 1;
24209       tree t = build_string (len, XSTR (rtl, 0));
24210       tree tlen = size_int (len - 1);
24211
24212       TREE_TYPE (t)
24213         = build_array_type (char_type_node, build_index_type (tlen));
24214       rtl = string_cst_pool_decl (t);
24215       if (!rtl)
24216         return false;
24217     }
24218   if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
24219     {
24220       decl = SYMBOL_REF_DECL (rtl);
24221       if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
24222         {
24223           ref = lookup_decl_die (decl);
24224           if (ref && (get_AT (ref, DW_AT_location)
24225                       || get_AT (ref, DW_AT_const_value)))
24226             {
24227               loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
24228               loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
24229               loc->dw_loc_oprnd1.val_entry = NULL;
24230               loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
24231               loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
24232               loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
24233               loc->dw_loc_oprnd2.v.val_int = offset;
24234               return true;
24235             }
24236         }
24237     }
24238   return false;
24239 }
24240
24241 /* Helper function for resolve_addr, handle one location
24242    expression, return false if at least one CONST_STRING or SYMBOL_REF in
24243    the location list couldn't be resolved.  */
24244
24245 static bool
24246 resolve_addr_in_expr (dw_loc_descr_ref loc)
24247 {
24248   dw_loc_descr_ref keep = NULL;
24249   for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
24250     switch (loc->dw_loc_opc)
24251       {
24252       case DW_OP_addr:
24253         if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
24254           {
24255             if ((prev == NULL
24256                  || prev->dw_loc_opc == DW_OP_piece
24257                  || prev->dw_loc_opc == DW_OP_bit_piece)
24258                 && loc->dw_loc_next
24259                 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
24260                 && !dwarf_strict
24261                 && optimize_one_addr_into_implicit_ptr (loc))
24262               break;
24263             return false;
24264           }
24265         break;
24266       case DW_OP_GNU_addr_index:
24267       case DW_OP_GNU_const_index:
24268         if (loc->dw_loc_opc == DW_OP_GNU_addr_index
24269             || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
24270           {
24271             rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
24272             if (!resolve_one_addr (&rtl))
24273               return false;
24274             remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
24275             loc->dw_loc_oprnd1.val_entry =
24276                 add_addr_table_entry (rtl, ate_kind_rtx);
24277           }
24278         break;
24279       case DW_OP_const4u:
24280       case DW_OP_const8u:
24281         if (loc->dtprel
24282             && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
24283           return false;
24284         break;
24285       case DW_OP_plus_uconst:
24286         if (size_of_loc_descr (loc)
24287             > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
24288               + 1
24289             && loc->dw_loc_oprnd1.v.val_unsigned > 0)
24290           {
24291             dw_loc_descr_ref repl
24292               = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
24293             add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
24294             add_loc_descr (&repl, loc->dw_loc_next);
24295             *loc = *repl;
24296           }
24297         break;
24298       case DW_OP_implicit_value:
24299         if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
24300             && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
24301           return false;
24302         break;
24303       case DW_OP_GNU_implicit_pointer:
24304       case DW_OP_GNU_parameter_ref:
24305         if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
24306           {
24307             dw_die_ref ref
24308               = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
24309             if (ref == NULL)
24310               return false;
24311             loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
24312             loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
24313             loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
24314           }
24315         break;
24316       case DW_OP_GNU_const_type:
24317       case DW_OP_GNU_regval_type:
24318       case DW_OP_GNU_deref_type:
24319       case DW_OP_GNU_convert:
24320       case DW_OP_GNU_reinterpret:
24321         while (loc->dw_loc_next
24322                && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
24323           {
24324             dw_die_ref base1, base2;
24325             unsigned enc1, enc2, size1, size2;
24326             if (loc->dw_loc_opc == DW_OP_GNU_regval_type
24327                 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
24328               base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
24329             else if (loc->dw_loc_oprnd1.val_class
24330                      == dw_val_class_unsigned_const)
24331               break;
24332             else
24333               base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
24334             if (loc->dw_loc_next->dw_loc_oprnd1.val_class
24335                 == dw_val_class_unsigned_const)
24336               break;
24337             base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
24338             gcc_assert (base1->die_tag == DW_TAG_base_type
24339                         && base2->die_tag == DW_TAG_base_type);
24340             enc1 = get_AT_unsigned (base1, DW_AT_encoding);
24341             enc2 = get_AT_unsigned (base2, DW_AT_encoding);
24342             size1 = get_AT_unsigned (base1, DW_AT_byte_size);
24343             size2 = get_AT_unsigned (base2, DW_AT_byte_size);
24344             if (size1 == size2
24345                 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
24346                      && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
24347                      && loc != keep)
24348                     || enc1 == enc2))
24349               {
24350                 /* Optimize away next DW_OP_GNU_convert after
24351                    adjusting LOC's base type die reference.  */
24352                 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
24353                     || loc->dw_loc_opc == DW_OP_GNU_deref_type)
24354                   loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
24355                 else
24356                   loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
24357                 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
24358                 continue;
24359               }
24360             /* Don't change integer DW_OP_GNU_convert after e.g. floating
24361                point typed stack entry.  */
24362             else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
24363               keep = loc->dw_loc_next;
24364             break;
24365           }
24366         break;
24367       default:
24368         break;
24369       }
24370   return true;
24371 }
24372
24373 /* Helper function of resolve_addr.  DIE had DW_AT_location of
24374    DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
24375    and DW_OP_addr couldn't be resolved.  resolve_addr has already
24376    removed the DW_AT_location attribute.  This function attempts to
24377    add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
24378    to it or DW_AT_const_value attribute, if possible.  */
24379
24380 static void
24381 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
24382 {
24383   if (TREE_CODE (decl) != VAR_DECL
24384       || lookup_decl_die (decl) != die
24385       || DECL_EXTERNAL (decl)
24386       || !TREE_STATIC (decl)
24387       || DECL_INITIAL (decl) == NULL_TREE
24388       || DECL_P (DECL_INITIAL (decl))
24389       || get_AT (die, DW_AT_const_value))
24390     return;
24391
24392   tree init = DECL_INITIAL (decl);
24393   HOST_WIDE_INT offset = 0;
24394   /* For variables that have been optimized away and thus
24395      don't have a memory location, see if we can emit
24396      DW_AT_const_value instead.  */
24397   if (tree_add_const_value_attribute (die, init))
24398     return;
24399   if (dwarf_strict)
24400     return;
24401   /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
24402      and ADDR_EXPR refers to a decl that has DW_AT_location or
24403      DW_AT_const_value (but isn't addressable, otherwise
24404      resolving the original DW_OP_addr wouldn't fail), see if
24405      we can add DW_OP_GNU_implicit_pointer.  */
24406   STRIP_NOPS (init);
24407   if (TREE_CODE (init) == POINTER_PLUS_EXPR
24408       && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
24409     {
24410       offset = tree_to_shwi (TREE_OPERAND (init, 1));
24411       init = TREE_OPERAND (init, 0);
24412       STRIP_NOPS (init);
24413     }
24414   if (TREE_CODE (init) != ADDR_EXPR)
24415     return;
24416   if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
24417        && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
24418       || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
24419           && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
24420           && TREE_OPERAND (init, 0) != decl))
24421     {
24422       dw_die_ref ref;
24423       dw_loc_descr_ref l;
24424
24425       if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
24426         {
24427           rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
24428           if (!rtl)
24429             return;
24430           decl = SYMBOL_REF_DECL (rtl);
24431         }
24432       else
24433         decl = TREE_OPERAND (init, 0);
24434       ref = lookup_decl_die (decl);
24435       if (ref == NULL
24436           || (!get_AT (ref, DW_AT_location)
24437               && !get_AT (ref, DW_AT_const_value)))
24438         return;
24439       l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
24440       l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
24441       l->dw_loc_oprnd1.v.val_die_ref.die = ref;
24442       l->dw_loc_oprnd1.v.val_die_ref.external = 0;
24443       add_AT_loc (die, DW_AT_location, l);
24444     }
24445 }
24446
24447 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
24448    an address in .rodata section if the string literal is emitted there,
24449    or remove the containing location list or replace DW_AT_const_value
24450    with DW_AT_location and empty location expression, if it isn't found
24451    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
24452    to something that has been emitted in the current CU.  */
24453
24454 static void
24455 resolve_addr (dw_die_ref die)
24456 {
24457   dw_die_ref c;
24458   dw_attr_node *a;
24459   dw_loc_list_ref *curr, *start, loc;
24460   unsigned ix;
24461
24462   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24463     switch (AT_class (a))
24464       {
24465       case dw_val_class_loc_list:
24466         start = curr = AT_loc_list_ptr (a);
24467         loc = *curr;
24468         gcc_assert (loc);
24469         /* The same list can be referenced more than once.  See if we have
24470            already recorded the result from a previous pass.  */
24471         if (loc->replaced)
24472           *curr = loc->dw_loc_next;
24473         else if (!loc->resolved_addr)
24474           {
24475             /* As things stand, we do not expect or allow one die to
24476                reference a suffix of another die's location list chain.
24477                References must be identical or completely separate.
24478                There is therefore no need to cache the result of this
24479                pass on any list other than the first; doing so
24480                would lead to unnecessary writes.  */
24481             while (*curr)
24482               {
24483                 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
24484                 if (!resolve_addr_in_expr ((*curr)->expr))
24485                   {
24486                     dw_loc_list_ref next = (*curr)->dw_loc_next;
24487                     dw_loc_descr_ref l = (*curr)->expr;
24488
24489                     if (next && (*curr)->ll_symbol)
24490                       {
24491                         gcc_assert (!next->ll_symbol);
24492                         next->ll_symbol = (*curr)->ll_symbol;
24493                       }
24494                     if (dwarf_split_debug_info)
24495                       remove_loc_list_addr_table_entries (l);
24496                     *curr = next;
24497                   }
24498                 else
24499                   {
24500                     mark_base_types ((*curr)->expr);
24501                     curr = &(*curr)->dw_loc_next;
24502                   }
24503               }
24504             if (loc == *start)
24505               loc->resolved_addr = 1;
24506             else
24507               {
24508                 loc->replaced = 1;
24509                 loc->dw_loc_next = *start;
24510               }
24511           }
24512         if (!*start)
24513           {
24514             remove_AT (die, a->dw_attr);
24515             ix--;
24516           }
24517         break;
24518       case dw_val_class_loc:
24519         {
24520           dw_loc_descr_ref l = AT_loc (a);
24521           /* For -gdwarf-2 don't attempt to optimize
24522              DW_AT_data_member_location containing
24523              DW_OP_plus_uconst - older consumers might
24524              rely on it being that op instead of a more complex,
24525              but shorter, location description.  */
24526           if ((dwarf_version > 2
24527                || a->dw_attr != DW_AT_data_member_location
24528                || l == NULL
24529                || l->dw_loc_opc != DW_OP_plus_uconst
24530                || l->dw_loc_next != NULL)
24531               && !resolve_addr_in_expr (l))
24532             {
24533               if (dwarf_split_debug_info)
24534                 remove_loc_list_addr_table_entries (l);
24535               if (l != NULL
24536                   && l->dw_loc_next == NULL
24537                   && l->dw_loc_opc == DW_OP_addr
24538                   && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
24539                   && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
24540                   && a->dw_attr == DW_AT_location)
24541                 {
24542                   tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
24543                   remove_AT (die, a->dw_attr);
24544                   ix--;
24545                   optimize_location_into_implicit_ptr (die, decl);
24546                   break;
24547                 }
24548               remove_AT (die, a->dw_attr);
24549               ix--;
24550             }
24551           else
24552             mark_base_types (l);
24553         }
24554         break;
24555       case dw_val_class_addr:
24556         if (a->dw_attr == DW_AT_const_value
24557             && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
24558           {
24559             if (AT_index (a) != NOT_INDEXED)
24560               remove_addr_table_entry (a->dw_attr_val.val_entry);
24561             remove_AT (die, a->dw_attr);
24562             ix--;
24563           }
24564         if (die->die_tag == DW_TAG_GNU_call_site
24565             && a->dw_attr == DW_AT_abstract_origin)
24566           {
24567             tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
24568             dw_die_ref tdie = lookup_decl_die (tdecl);
24569             dw_die_ref cdie;
24570             if (tdie == NULL
24571                 && DECL_EXTERNAL (tdecl)
24572                 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
24573                 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
24574               {
24575                 /* Creating a full DIE for tdecl is overly expensive and
24576                    at this point even wrong when in the LTO phase
24577                    as it can end up generating new type DIEs we didn't
24578                    output and thus optimize_external_refs will crash.  */
24579                 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
24580                 add_AT_flag (tdie, DW_AT_external, 1);
24581                 add_AT_flag (tdie, DW_AT_declaration, 1);
24582                 add_linkage_attr (tdie, tdecl);
24583                 add_name_and_src_coords_attributes (tdie, tdecl);
24584                 equate_decl_number_to_die (tdecl, tdie);
24585               }
24586             if (tdie)
24587               {
24588                 a->dw_attr_val.val_class = dw_val_class_die_ref;
24589                 a->dw_attr_val.v.val_die_ref.die = tdie;
24590                 a->dw_attr_val.v.val_die_ref.external = 0;
24591               }
24592             else
24593               {
24594                 if (AT_index (a) != NOT_INDEXED)
24595                   remove_addr_table_entry (a->dw_attr_val.val_entry);
24596                 remove_AT (die, a->dw_attr);
24597                 ix--;
24598               }
24599           }
24600         break;
24601       default:
24602         break;
24603       }
24604
24605   FOR_EACH_CHILD (die, c, resolve_addr (c));
24606 }
24607 \f
24608 /* Helper routines for optimize_location_lists.
24609    This pass tries to share identical local lists in .debug_loc
24610    section.  */
24611
24612 /* Iteratively hash operands of LOC opcode into HSTATE.  */
24613
24614 static void
24615 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
24616 {
24617   dw_val_ref val1 = &loc->dw_loc_oprnd1;
24618   dw_val_ref val2 = &loc->dw_loc_oprnd2;
24619
24620   switch (loc->dw_loc_opc)
24621     {
24622     case DW_OP_const4u:
24623     case DW_OP_const8u:
24624       if (loc->dtprel)
24625         goto hash_addr;
24626       /* FALLTHRU */
24627     case DW_OP_const1u:
24628     case DW_OP_const1s:
24629     case DW_OP_const2u:
24630     case DW_OP_const2s:
24631     case DW_OP_const4s:
24632     case DW_OP_const8s:
24633     case DW_OP_constu:
24634     case DW_OP_consts:
24635     case DW_OP_pick:
24636     case DW_OP_plus_uconst:
24637     case DW_OP_breg0:
24638     case DW_OP_breg1:
24639     case DW_OP_breg2:
24640     case DW_OP_breg3:
24641     case DW_OP_breg4:
24642     case DW_OP_breg5:
24643     case DW_OP_breg6:
24644     case DW_OP_breg7:
24645     case DW_OP_breg8:
24646     case DW_OP_breg9:
24647     case DW_OP_breg10:
24648     case DW_OP_breg11:
24649     case DW_OP_breg12:
24650     case DW_OP_breg13:
24651     case DW_OP_breg14:
24652     case DW_OP_breg15:
24653     case DW_OP_breg16:
24654     case DW_OP_breg17:
24655     case DW_OP_breg18:
24656     case DW_OP_breg19:
24657     case DW_OP_breg20:
24658     case DW_OP_breg21:
24659     case DW_OP_breg22:
24660     case DW_OP_breg23:
24661     case DW_OP_breg24:
24662     case DW_OP_breg25:
24663     case DW_OP_breg26:
24664     case DW_OP_breg27:
24665     case DW_OP_breg28:
24666     case DW_OP_breg29:
24667     case DW_OP_breg30:
24668     case DW_OP_breg31:
24669     case DW_OP_regx:
24670     case DW_OP_fbreg:
24671     case DW_OP_piece:
24672     case DW_OP_deref_size:
24673     case DW_OP_xderef_size:
24674       hstate.add_object (val1->v.val_int);
24675       break;
24676     case DW_OP_skip:
24677     case DW_OP_bra:
24678       {
24679         int offset;
24680
24681         gcc_assert (val1->val_class == dw_val_class_loc);
24682         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
24683         hstate.add_object (offset);
24684       }
24685       break;
24686     case DW_OP_implicit_value:
24687       hstate.add_object (val1->v.val_unsigned);
24688       switch (val2->val_class)
24689         {
24690         case dw_val_class_const:
24691           hstate.add_object (val2->v.val_int);
24692           break;
24693         case dw_val_class_vec:
24694           {
24695             unsigned int elt_size = val2->v.val_vec.elt_size;
24696             unsigned int len = val2->v.val_vec.length;
24697
24698             hstate.add_int (elt_size);
24699             hstate.add_int (len);
24700             hstate.add (val2->v.val_vec.array, len * elt_size);
24701           }
24702           break;
24703         case dw_val_class_const_double:
24704           hstate.add_object (val2->v.val_double.low);
24705           hstate.add_object (val2->v.val_double.high);
24706           break;
24707         case dw_val_class_wide_int:
24708           hstate.add (val2->v.val_wide->get_val (),
24709                       get_full_len (*val2->v.val_wide)
24710                       * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
24711           break;
24712         case dw_val_class_addr: 
24713           inchash::add_rtx (val2->v.val_addr, hstate);
24714           break;
24715         default:
24716           gcc_unreachable ();
24717         }
24718       break;
24719     case DW_OP_bregx:
24720     case DW_OP_bit_piece:
24721       hstate.add_object (val1->v.val_int);
24722       hstate.add_object (val2->v.val_int);
24723       break;
24724     case DW_OP_addr:
24725     hash_addr:
24726       if (loc->dtprel)
24727         {
24728           unsigned char dtprel = 0xd1;
24729           hstate.add_object (dtprel);
24730         }
24731       inchash::add_rtx (val1->v.val_addr, hstate);
24732       break;
24733     case DW_OP_GNU_addr_index:
24734     case DW_OP_GNU_const_index:
24735       {
24736         if (loc->dtprel)
24737           {
24738             unsigned char dtprel = 0xd1;
24739             hstate.add_object (dtprel);
24740           }
24741         inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
24742       }
24743       break;
24744     case DW_OP_GNU_implicit_pointer:
24745       hstate.add_int (val2->v.val_int);
24746       break;
24747     case DW_OP_GNU_entry_value:
24748       hstate.add_object (val1->v.val_loc);
24749       break;
24750     case DW_OP_GNU_regval_type:
24751     case DW_OP_GNU_deref_type:
24752       {
24753         unsigned int byte_size
24754           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
24755         unsigned int encoding
24756           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
24757         hstate.add_object (val1->v.val_int);
24758         hstate.add_object (byte_size);
24759         hstate.add_object (encoding);
24760       }
24761       break;
24762     case DW_OP_GNU_convert:
24763     case DW_OP_GNU_reinterpret:
24764       if (val1->val_class == dw_val_class_unsigned_const)
24765         {
24766           hstate.add_object (val1->v.val_unsigned);
24767           break;
24768         }
24769       /* FALLTHRU */
24770     case DW_OP_GNU_const_type:
24771       {
24772         unsigned int byte_size
24773           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
24774         unsigned int encoding
24775           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
24776         hstate.add_object (byte_size);
24777         hstate.add_object (encoding);
24778         if (loc->dw_loc_opc != DW_OP_GNU_const_type)
24779           break;
24780         hstate.add_object (val2->val_class);
24781         switch (val2->val_class)
24782           {
24783           case dw_val_class_const:
24784             hstate.add_object (val2->v.val_int);
24785             break;
24786           case dw_val_class_vec:
24787             {
24788               unsigned int elt_size = val2->v.val_vec.elt_size;
24789               unsigned int len = val2->v.val_vec.length;
24790
24791               hstate.add_object (elt_size);
24792               hstate.add_object (len);
24793               hstate.add (val2->v.val_vec.array, len * elt_size);
24794             }
24795             break;
24796           case dw_val_class_const_double:
24797             hstate.add_object (val2->v.val_double.low);
24798             hstate.add_object (val2->v.val_double.high);
24799             break;
24800           case dw_val_class_wide_int:
24801             hstate.add (val2->v.val_wide->get_val (),
24802                         get_full_len (*val2->v.val_wide)
24803                         * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
24804             break;
24805           default:
24806             gcc_unreachable ();
24807           }
24808       }
24809       break;
24810
24811     default:
24812       /* Other codes have no operands.  */
24813       break;
24814     }
24815 }
24816
24817 /* Iteratively hash the whole DWARF location expression LOC into HSTATE.  */
24818
24819 static inline void
24820 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
24821 {
24822   dw_loc_descr_ref l;
24823   bool sizes_computed = false;
24824   /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed.  */
24825   size_of_locs (loc);
24826
24827   for (l = loc; l != NULL; l = l->dw_loc_next)
24828     {
24829       enum dwarf_location_atom opc = l->dw_loc_opc;
24830       hstate.add_object (opc);
24831       if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
24832         {
24833           size_of_locs (loc);
24834           sizes_computed = true;
24835         }
24836       hash_loc_operands (l, hstate);
24837     }
24838 }
24839
24840 /* Compute hash of the whole location list LIST_HEAD.  */
24841
24842 static inline void
24843 hash_loc_list (dw_loc_list_ref list_head)
24844 {
24845   dw_loc_list_ref curr = list_head;
24846   inchash::hash hstate;
24847
24848   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
24849     {
24850       hstate.add (curr->begin, strlen (curr->begin) + 1);
24851       hstate.add (curr->end, strlen (curr->end) + 1);
24852       if (curr->section)
24853         hstate.add (curr->section, strlen (curr->section) + 1);
24854       hash_locs (curr->expr, hstate);
24855     }
24856   list_head->hash = hstate.end ();
24857 }
24858
24859 /* Return true if X and Y opcodes have the same operands.  */
24860
24861 static inline bool
24862 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
24863 {
24864   dw_val_ref valx1 = &x->dw_loc_oprnd1;
24865   dw_val_ref valx2 = &x->dw_loc_oprnd2;
24866   dw_val_ref valy1 = &y->dw_loc_oprnd1;
24867   dw_val_ref valy2 = &y->dw_loc_oprnd2;
24868
24869   switch (x->dw_loc_opc)
24870     {
24871     case DW_OP_const4u:
24872     case DW_OP_const8u:
24873       if (x->dtprel)
24874         goto hash_addr;
24875       /* FALLTHRU */
24876     case DW_OP_const1u:
24877     case DW_OP_const1s:
24878     case DW_OP_const2u:
24879     case DW_OP_const2s:
24880     case DW_OP_const4s:
24881     case DW_OP_const8s:
24882     case DW_OP_constu:
24883     case DW_OP_consts:
24884     case DW_OP_pick:
24885     case DW_OP_plus_uconst:
24886     case DW_OP_breg0:
24887     case DW_OP_breg1:
24888     case DW_OP_breg2:
24889     case DW_OP_breg3:
24890     case DW_OP_breg4:
24891     case DW_OP_breg5:
24892     case DW_OP_breg6:
24893     case DW_OP_breg7:
24894     case DW_OP_breg8:
24895     case DW_OP_breg9:
24896     case DW_OP_breg10:
24897     case DW_OP_breg11:
24898     case DW_OP_breg12:
24899     case DW_OP_breg13:
24900     case DW_OP_breg14:
24901     case DW_OP_breg15:
24902     case DW_OP_breg16:
24903     case DW_OP_breg17:
24904     case DW_OP_breg18:
24905     case DW_OP_breg19:
24906     case DW_OP_breg20:
24907     case DW_OP_breg21:
24908     case DW_OP_breg22:
24909     case DW_OP_breg23:
24910     case DW_OP_breg24:
24911     case DW_OP_breg25:
24912     case DW_OP_breg26:
24913     case DW_OP_breg27:
24914     case DW_OP_breg28:
24915     case DW_OP_breg29:
24916     case DW_OP_breg30:
24917     case DW_OP_breg31:
24918     case DW_OP_regx:
24919     case DW_OP_fbreg:
24920     case DW_OP_piece:
24921     case DW_OP_deref_size:
24922     case DW_OP_xderef_size:
24923       return valx1->v.val_int == valy1->v.val_int;
24924     case DW_OP_skip:
24925     case DW_OP_bra:
24926       /* If splitting debug info, the use of DW_OP_GNU_addr_index
24927         can cause irrelevant differences in dw_loc_addr.  */
24928       gcc_assert (valx1->val_class == dw_val_class_loc
24929                   && valy1->val_class == dw_val_class_loc
24930                   && (dwarf_split_debug_info
24931                       || x->dw_loc_addr == y->dw_loc_addr));
24932       return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
24933     case DW_OP_implicit_value:
24934       if (valx1->v.val_unsigned != valy1->v.val_unsigned
24935           || valx2->val_class != valy2->val_class)
24936         return false;
24937       switch (valx2->val_class)
24938         {
24939         case dw_val_class_const:
24940           return valx2->v.val_int == valy2->v.val_int;
24941         case dw_val_class_vec:
24942           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24943                  && valx2->v.val_vec.length == valy2->v.val_vec.length
24944                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24945                             valx2->v.val_vec.elt_size
24946                             * valx2->v.val_vec.length) == 0;
24947         case dw_val_class_const_double:
24948           return valx2->v.val_double.low == valy2->v.val_double.low
24949                  && valx2->v.val_double.high == valy2->v.val_double.high;
24950         case dw_val_class_wide_int:
24951           return *valx2->v.val_wide == *valy2->v.val_wide;
24952         case dw_val_class_addr:
24953           return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
24954         default:
24955           gcc_unreachable ();
24956         }
24957     case DW_OP_bregx:
24958     case DW_OP_bit_piece:
24959       return valx1->v.val_int == valy1->v.val_int
24960              && valx2->v.val_int == valy2->v.val_int;
24961     case DW_OP_addr:
24962     hash_addr:
24963       return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
24964     case DW_OP_GNU_addr_index:
24965     case DW_OP_GNU_const_index:
24966       {
24967         rtx ax1 = valx1->val_entry->addr.rtl;
24968         rtx ay1 = valy1->val_entry->addr.rtl;
24969         return rtx_equal_p (ax1, ay1);
24970       }
24971     case DW_OP_GNU_implicit_pointer:
24972       return valx1->val_class == dw_val_class_die_ref
24973              && valx1->val_class == valy1->val_class
24974              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
24975              && valx2->v.val_int == valy2->v.val_int;
24976     case DW_OP_GNU_entry_value:
24977       return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
24978     case DW_OP_GNU_const_type:
24979       if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
24980           || valx2->val_class != valy2->val_class)
24981         return false;
24982       switch (valx2->val_class)
24983         {
24984         case dw_val_class_const:
24985           return valx2->v.val_int == valy2->v.val_int;
24986         case dw_val_class_vec:
24987           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24988                  && valx2->v.val_vec.length == valy2->v.val_vec.length
24989                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24990                             valx2->v.val_vec.elt_size
24991                             * valx2->v.val_vec.length) == 0;
24992         case dw_val_class_const_double:
24993           return valx2->v.val_double.low == valy2->v.val_double.low
24994                  && valx2->v.val_double.high == valy2->v.val_double.high;
24995         case dw_val_class_wide_int:
24996           return *valx2->v.val_wide == *valy2->v.val_wide;
24997         default:
24998           gcc_unreachable ();
24999         }
25000     case DW_OP_GNU_regval_type:
25001     case DW_OP_GNU_deref_type:
25002       return valx1->v.val_int == valy1->v.val_int
25003              && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
25004     case DW_OP_GNU_convert:
25005     case DW_OP_GNU_reinterpret:
25006       if (valx1->val_class != valy1->val_class)
25007         return false;
25008       if (valx1->val_class == dw_val_class_unsigned_const)
25009         return valx1->v.val_unsigned == valy1->v.val_unsigned;
25010       return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
25011     case DW_OP_GNU_parameter_ref:
25012       return valx1->val_class == dw_val_class_die_ref
25013              && valx1->val_class == valy1->val_class
25014              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
25015     default:
25016       /* Other codes have no operands.  */
25017       return true;
25018     }
25019 }
25020
25021 /* Return true if DWARF location expressions X and Y are the same.  */
25022
25023 static inline bool
25024 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
25025 {
25026   for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
25027     if (x->dw_loc_opc != y->dw_loc_opc
25028         || x->dtprel != y->dtprel
25029         || !compare_loc_operands (x, y))
25030       break;
25031   return x == NULL && y == NULL;
25032 }
25033
25034 /* Hashtable helpers.  */
25035
25036 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
25037 {
25038   static inline hashval_t hash (const dw_loc_list_struct *);
25039   static inline bool equal (const dw_loc_list_struct *,
25040                             const dw_loc_list_struct *);
25041 };
25042
25043 /* Return precomputed hash of location list X.  */
25044
25045 inline hashval_t
25046 loc_list_hasher::hash (const dw_loc_list_struct *x)
25047 {
25048   return x->hash;
25049 }
25050
25051 /* Return true if location lists A and B are the same.  */
25052
25053 inline bool
25054 loc_list_hasher::equal (const dw_loc_list_struct *a,
25055                         const dw_loc_list_struct *b)
25056 {
25057   if (a == b)
25058     return 1;
25059   if (a->hash != b->hash)
25060     return 0;
25061   for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
25062     if (strcmp (a->begin, b->begin) != 0
25063         || strcmp (a->end, b->end) != 0
25064         || (a->section == NULL) != (b->section == NULL)
25065         || (a->section && strcmp (a->section, b->section) != 0)
25066         || !compare_locs (a->expr, b->expr))
25067       break;
25068   return a == NULL && b == NULL;
25069 }
25070
25071 typedef hash_table<loc_list_hasher> loc_list_hash_type;
25072
25073
25074 /* Recursively optimize location lists referenced from DIE
25075    children and share them whenever possible.  */
25076
25077 static void
25078 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
25079 {
25080   dw_die_ref c;
25081   dw_attr_node *a;
25082   unsigned ix;
25083   dw_loc_list_struct **slot;
25084
25085   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
25086     if (AT_class (a) == dw_val_class_loc_list)
25087       {
25088         dw_loc_list_ref list = AT_loc_list (a);
25089         /* TODO: perform some optimizations here, before hashing
25090            it and storing into the hash table.  */
25091         hash_loc_list (list);
25092         slot = htab->find_slot_with_hash (list, list->hash, INSERT);
25093         if (*slot == NULL)
25094           *slot = list;
25095         else
25096           a->dw_attr_val.v.val_loc_list = *slot;
25097       }
25098
25099   FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
25100 }
25101
25102
25103 /* Recursively assign each location list a unique index into the debug_addr
25104    section.  */
25105
25106 static void
25107 index_location_lists (dw_die_ref die)
25108 {
25109   dw_die_ref c;
25110   dw_attr_node *a;
25111   unsigned ix;
25112
25113   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
25114     if (AT_class (a) == dw_val_class_loc_list)
25115       {
25116         dw_loc_list_ref list = AT_loc_list (a);
25117         dw_loc_list_ref curr;
25118         for (curr = list; curr != NULL; curr = curr->dw_loc_next)
25119           {
25120             /* Don't index an entry that has already been indexed
25121                or won't be output.  */
25122             if (curr->begin_entry != NULL
25123                 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
25124               continue;
25125
25126             curr->begin_entry
25127                 = add_addr_table_entry (xstrdup (curr->begin),
25128                                         ate_kind_label);
25129           }
25130       }
25131
25132   FOR_EACH_CHILD (die, c, index_location_lists (c));
25133 }
25134
25135 /* Optimize location lists referenced from DIE
25136    children and share them whenever possible.  */
25137
25138 static void
25139 optimize_location_lists (dw_die_ref die)
25140 {
25141   loc_list_hash_type htab (500);
25142   optimize_location_lists_1 (die, &htab);
25143 }
25144 \f
25145 /* Traverse the limbo die list, and add parent/child links.  The only
25146    dies without parents that should be here are concrete instances of
25147    inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
25148    For concrete instances, we can get the parent die from the abstract
25149    instance.  */
25150
25151 static void
25152 flush_limbo_die_list (void)
25153 {
25154   limbo_die_node *node, *next_node;
25155
25156   for (node = limbo_die_list; node; node = next_node)
25157     {
25158       dw_die_ref die = node->die;
25159       next_node = node->next;
25160
25161       if (die->die_parent == NULL)
25162         {
25163           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
25164
25165           if (origin && origin->die_parent)
25166             add_child_die (origin->die_parent, die);
25167           else if (is_cu_die (die))
25168             ;
25169           else if (seen_error ())
25170             /* It's OK to be confused by errors in the input.  */
25171             add_child_die (comp_unit_die (), die);
25172           else
25173             {
25174               /* In certain situations, the lexical block containing a
25175                  nested function can be optimized away, which results
25176                  in the nested function die being orphaned.  Likewise
25177                  with the return type of that nested function.  Force
25178                  this to be a child of the containing function.
25179
25180                  It may happen that even the containing function got fully
25181                  inlined and optimized out.  In that case we are lost and
25182                  assign the empty child.  This should not be big issue as
25183                  the function is likely unreachable too.  */
25184               gcc_assert (node->created_for);
25185
25186               if (DECL_P (node->created_for))
25187                 origin = get_context_die (DECL_CONTEXT (node->created_for));
25188               else if (TYPE_P (node->created_for))
25189                 origin = scope_die_for (node->created_for, comp_unit_die ());
25190               else
25191                 origin = comp_unit_die ();
25192
25193               add_child_die (origin, die);
25194             }
25195         }
25196     }
25197
25198   limbo_die_list = NULL;
25199 }
25200
25201 /* Output stuff that dwarf requires at the end of every file,
25202    and generate the DWARF-2 debugging info.  */
25203
25204 static void
25205 dwarf2out_finish (const char *filename)
25206 {
25207   comdat_type_node *ctnode;
25208   dw_die_ref main_comp_unit_die;
25209
25210   /* Flush out any latecomers to the limbo party.  */
25211   flush_limbo_die_list ();
25212
25213   /* We shouldn't have any symbols with delayed asm names for
25214      DIEs generated after early finish.  */
25215   gcc_assert (deferred_asm_name == NULL);
25216
25217   /* PCH might result in DW_AT_producer string being restored from the
25218      header compilation, so always fill it with empty string initially
25219      and overwrite only here.  */
25220   dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
25221   producer_string = gen_producer_string ();
25222   producer->dw_attr_val.v.val_str->refcount--;
25223   producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
25224
25225   gen_remaining_tmpl_value_param_die_attribute ();
25226
25227   /* Add the name for the main input file now.  We delayed this from
25228      dwarf2out_init to avoid complications with PCH.
25229      For LTO produced units use a fixed artificial name to avoid
25230      leaking tempfile names into the dwarf.  */
25231   if (!in_lto_p)
25232     add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
25233   else
25234     add_name_attribute (comp_unit_die (), "<artificial>");
25235   if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
25236     add_comp_dir_attribute (comp_unit_die ());
25237   else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
25238     {
25239       bool p = false;
25240       file_table->traverse<bool *, file_table_relative_p> (&p);
25241       if (p)
25242         add_comp_dir_attribute (comp_unit_die ());
25243     }
25244
25245 #if ENABLE_ASSERT_CHECKING
25246   {
25247     dw_die_ref die = comp_unit_die (), c;
25248     FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
25249   }
25250 #endif
25251   resolve_addr (comp_unit_die ());
25252   move_marked_base_types ();
25253
25254   /* Walk through the list of incomplete types again, trying once more to
25255      emit full debugging info for them.  */
25256   retry_incomplete_types ();
25257
25258   if (flag_eliminate_unused_debug_types)
25259     prune_unused_types ();
25260
25261   /* Generate separate COMDAT sections for type DIEs. */
25262   if (use_debug_types)
25263     {
25264       break_out_comdat_types (comp_unit_die ());
25265
25266       /* Each new type_unit DIE was added to the limbo die list when created.
25267          Since these have all been added to comdat_type_list, clear the
25268          limbo die list.  */
25269       limbo_die_list = NULL;
25270
25271       /* For each new comdat type unit, copy declarations for incomplete
25272          types to make the new unit self-contained (i.e., no direct
25273          references to the main compile unit).  */
25274       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
25275         copy_decls_for_unworthy_types (ctnode->root_die);
25276       copy_decls_for_unworthy_types (comp_unit_die ());
25277
25278       /* In the process of copying declarations from one unit to another,
25279          we may have left some declarations behind that are no longer
25280          referenced.  Prune them.  */
25281       prune_unused_types ();
25282     }
25283
25284   /* Generate separate CUs for each of the include files we've seen.
25285      They will go into limbo_die_list.  */
25286   if (flag_eliminate_dwarf2_dups)
25287     break_out_includes (comp_unit_die ());
25288
25289   /* Traverse the DIE's and add sibling attributes to those DIE's that
25290      have children.  */
25291   add_sibling_attributes (comp_unit_die ());
25292   limbo_die_node *node;
25293   for (node = limbo_die_list; node; node = node->next)
25294     add_sibling_attributes (node->die);
25295   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
25296     add_sibling_attributes (ctnode->root_die);
25297
25298   /* When splitting DWARF info, we put some attributes in the
25299      skeleton compile_unit DIE that remains in the .o, while
25300      most attributes go in the DWO compile_unit_die.  */
25301   if (dwarf_split_debug_info)
25302     main_comp_unit_die = gen_compile_unit_die (NULL);
25303   else
25304     main_comp_unit_die = comp_unit_die ();
25305
25306   /* Output a terminator label for the .text section.  */
25307   switch_to_section (text_section);
25308   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
25309   if (cold_text_section)
25310     {
25311       switch_to_section (cold_text_section);
25312       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
25313     }
25314
25315   /* We can only use the low/high_pc attributes if all of the code was
25316      in .text.  */
25317   if (!have_multiple_function_sections 
25318       || (dwarf_version < 3 && dwarf_strict))
25319     {
25320       /* Don't add if the CU has no associated code.  */
25321       if (text_section_used)
25322         add_AT_low_high_pc (main_comp_unit_die, text_section_label,
25323                             text_end_label, true);
25324     }
25325   else
25326     {
25327       unsigned fde_idx;
25328       dw_fde_ref fde;
25329       bool range_list_added = false;
25330
25331       if (text_section_used)
25332         add_ranges_by_labels (main_comp_unit_die, text_section_label,
25333                               text_end_label, &range_list_added, true);
25334       if (cold_text_section_used)
25335         add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
25336                               cold_end_label, &range_list_added, true);
25337
25338       FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
25339         {
25340           if (DECL_IGNORED_P (fde->decl))
25341             continue;
25342           if (!fde->in_std_section)
25343             add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
25344                                   fde->dw_fde_end, &range_list_added,
25345                                   true);
25346           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
25347             add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
25348                                   fde->dw_fde_second_end, &range_list_added,
25349                                   true);
25350         }
25351
25352       if (range_list_added)
25353         {
25354           /* We need to give .debug_loc and .debug_ranges an appropriate
25355              "base address".  Use zero so that these addresses become
25356              absolute.  Historically, we've emitted the unexpected
25357              DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
25358              Emit both to give time for other tools to adapt.  */
25359           add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
25360           if (! dwarf_strict && dwarf_version < 4)
25361             add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
25362
25363           add_ranges (NULL);
25364         }
25365     }
25366
25367   if (debug_info_level >= DINFO_LEVEL_TERSE)
25368     add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
25369                     debug_line_section_label);
25370
25371   if (have_macinfo)
25372     add_AT_macptr (comp_unit_die (),
25373                    dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
25374                    macinfo_section_label);
25375
25376   if (dwarf_split_debug_info)
25377     {
25378       /* optimize_location_lists calculates the size of the lists,
25379          so index them first, and assign indices to the entries.
25380          Although optimize_location_lists will remove entries from
25381          the table, it only does so for duplicates, and therefore
25382          only reduces ref_counts to 1.  */
25383       index_location_lists (comp_unit_die ());
25384
25385       if (addr_index_table != NULL)
25386         {
25387           unsigned int index = 0;
25388           addr_index_table
25389             ->traverse_noresize<unsigned int *, index_addr_table_entry>
25390             (&index);
25391         }
25392     }
25393
25394   if (have_location_lists)
25395     optimize_location_lists (comp_unit_die ());
25396
25397   save_macinfo_strings ();
25398
25399   if (dwarf_split_debug_info)
25400     {
25401       unsigned int index = 0;
25402
25403       /* Add attributes common to skeleton compile_units and
25404          type_units.  Because these attributes include strings, it
25405          must be done before freezing the string table.  Top-level
25406          skeleton die attrs are added when the skeleton type unit is
25407          created, so ensure it is created by this point.  */
25408       add_top_level_skeleton_die_attrs (main_comp_unit_die);
25409       debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
25410     }
25411
25412   /* Output all of the compilation units.  We put the main one last so that
25413      the offsets are available to output_pubnames.  */
25414   for (node = limbo_die_list; node; node = node->next)
25415     output_comp_unit (node->die, 0);
25416
25417   hash_table<comdat_type_hasher> comdat_type_table (100);
25418   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
25419     {
25420       comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
25421
25422       /* Don't output duplicate types.  */
25423       if (*slot != HTAB_EMPTY_ENTRY)
25424         continue;
25425
25426       /* Add a pointer to the line table for the main compilation unit
25427          so that the debugger can make sense of DW_AT_decl_file
25428          attributes.  */
25429       if (debug_info_level >= DINFO_LEVEL_TERSE)
25430         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
25431                         (!dwarf_split_debug_info
25432                          ? debug_line_section_label
25433                          : debug_skeleton_line_section_label));
25434
25435       output_comdat_type_unit (ctnode);
25436       *slot = ctnode;
25437     }
25438
25439   /* The AT_pubnames attribute needs to go in all skeleton dies, including
25440      both the main_cu and all skeleton TUs.  Making this call unconditional
25441      would end up either adding a second copy of the AT_pubnames attribute, or
25442      requiring a special case in add_top_level_skeleton_die_attrs.  */
25443   if (!dwarf_split_debug_info)
25444     add_AT_pubnames (comp_unit_die ());
25445
25446   if (dwarf_split_debug_info)
25447     {
25448       int mark;
25449       unsigned char checksum[16];
25450       struct md5_ctx ctx;
25451
25452       /* Compute a checksum of the comp_unit to use as the dwo_id.  */
25453       md5_init_ctx (&ctx);
25454       mark = 0;
25455       die_checksum (comp_unit_die (), &ctx, &mark);
25456       unmark_all_dies (comp_unit_die ());
25457       md5_finish_ctx (&ctx, checksum);
25458
25459       /* Use the first 8 bytes of the checksum as the dwo_id,
25460         and add it to both comp-unit DIEs.  */
25461       add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
25462       add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
25463
25464       /* Add the base offset of the ranges table to the skeleton
25465         comp-unit DIE.  */
25466       if (ranges_table_in_use)
25467         add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
25468                         ranges_section_label);
25469
25470       switch_to_section (debug_addr_section);
25471       ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
25472       output_addr_table ();
25473     }
25474
25475   /* Output the main compilation unit if non-empty or if .debug_macinfo
25476      or .debug_macro will be emitted.  */
25477   output_comp_unit (comp_unit_die (), have_macinfo);
25478
25479   if (dwarf_split_debug_info && info_section_emitted)
25480     output_skeleton_debug_sections (main_comp_unit_die);
25481
25482   /* Output the abbreviation table.  */
25483   if (abbrev_die_table_in_use != 1)
25484     {
25485       switch_to_section (debug_abbrev_section);
25486       ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
25487       output_abbrev_section ();
25488     }
25489
25490   /* Output location list section if necessary.  */
25491   if (have_location_lists)
25492     {
25493       /* Output the location lists info.  */
25494       switch_to_section (debug_loc_section);
25495       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
25496       output_location_lists (comp_unit_die ());
25497     }
25498
25499   output_pubtables ();
25500
25501   /* Output the address range information if a CU (.debug_info section)
25502      was emitted.  We output an empty table even if we had no functions
25503      to put in it.  This because the consumer has no way to tell the
25504      difference between an empty table that we omitted and failure to
25505      generate a table that would have contained data.  */
25506   if (info_section_emitted)
25507     {
25508       unsigned long aranges_length = size_of_aranges ();
25509
25510       switch_to_section (debug_aranges_section);
25511       output_aranges (aranges_length);
25512     }
25513
25514   /* Output ranges section if necessary.  */
25515   if (ranges_table_in_use)
25516     {
25517       switch_to_section (debug_ranges_section);
25518       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
25519       output_ranges ();
25520     }
25521
25522   /* Have to end the macro section.  */
25523   if (have_macinfo)
25524     {
25525       switch_to_section (debug_macinfo_section);
25526       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
25527       output_macinfo ();
25528       dw2_asm_output_data (1, 0, "End compilation unit");
25529     }
25530
25531   /* Output the source line correspondence table.  We must do this
25532      even if there is no line information.  Otherwise, on an empty
25533      translation unit, we will generate a present, but empty,
25534      .debug_info section.  IRIX 6.5 `nm' will then complain when
25535      examining the file.  This is done late so that any filenames
25536      used by the debug_info section are marked as 'used'.  */
25537   switch_to_section (debug_line_section);
25538   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
25539   if (! DWARF2_ASM_LINE_DEBUG_INFO)
25540     output_line_info (false);
25541
25542   if (dwarf_split_debug_info && info_section_emitted)
25543     {
25544       switch_to_section (debug_skeleton_line_section);
25545       ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
25546       output_line_info (true);
25547     }
25548
25549   /* If we emitted any indirect strings, output the string table too.  */
25550   if (debug_str_hash || skeleton_debug_str_hash)
25551     output_indirect_strings ();
25552 }
25553
25554 /* Perform any cleanups needed after the early debug generation pass
25555    has run.  */
25556
25557 static void
25558 dwarf2out_early_finish (void)
25559 {
25560   limbo_die_node *node;
25561
25562   /* Add DW_AT_linkage_name for all deferred DIEs.  */
25563   for (node = deferred_asm_name; node; node = node->next)
25564     {
25565       tree decl = node->created_for;
25566       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
25567           /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
25568              ended up in deferred_asm_name before we knew it was
25569              constant and never written to disk.  */
25570           && DECL_ASSEMBLER_NAME (decl))
25571         {
25572           add_linkage_attr (node->die, decl);
25573           move_linkage_attr (node->die);
25574         }
25575     }
25576   deferred_asm_name = NULL;
25577
25578   /* The point here is to flush out the limbo list so that it is empty
25579      and we don't need to stream it for LTO.  */
25580   flush_limbo_die_list ();
25581
25582   gen_scheduled_generic_parms_dies ();
25583   gen_remaining_tmpl_value_param_die_attribute ();
25584 }
25585
25586 /* Reset all state within dwarf2out.c so that we can rerun the compiler
25587    within the same process.  For use by toplev::finalize.  */
25588
25589 void
25590 dwarf2out_c_finalize (void)
25591 {
25592   last_var_location_insn = NULL;
25593   cached_next_real_insn = NULL;
25594   used_rtx_array = NULL;
25595   incomplete_types = NULL;
25596   decl_scope_table = NULL;
25597   debug_info_section = NULL;
25598   debug_skeleton_info_section = NULL;
25599   debug_abbrev_section = NULL;
25600   debug_skeleton_abbrev_section = NULL;
25601   debug_aranges_section = NULL;
25602   debug_addr_section = NULL;
25603   debug_macinfo_section = NULL;
25604   debug_line_section = NULL;
25605   debug_skeleton_line_section = NULL;
25606   debug_loc_section = NULL;
25607   debug_pubnames_section = NULL;
25608   debug_pubtypes_section = NULL;
25609   debug_str_section = NULL;
25610   debug_str_dwo_section = NULL;
25611   debug_str_offsets_section = NULL;
25612   debug_ranges_section = NULL;
25613   debug_frame_section = NULL;
25614   fde_vec = NULL;
25615   debug_str_hash = NULL;
25616   skeleton_debug_str_hash = NULL;
25617   dw2_string_counter = 0;
25618   have_multiple_function_sections = false;
25619   text_section_used = false;
25620   cold_text_section_used = false;
25621   cold_text_section = NULL;
25622   current_unit_personality = NULL;
25623
25624   next_die_offset = 0;
25625   single_comp_unit_die = NULL;
25626   comdat_type_list = NULL;
25627   limbo_die_list = NULL;
25628   file_table = NULL;
25629   decl_die_table = NULL;
25630   common_block_die_table = NULL;
25631   decl_loc_table = NULL;
25632   call_arg_locations = NULL;
25633   call_arg_loc_last = NULL;
25634   call_site_count = -1;
25635   tail_call_site_count = -1;
25636   cached_dw_loc_list_table = NULL;
25637   abbrev_die_table = NULL;
25638   abbrev_die_table_allocated = 0;
25639   abbrev_die_table_in_use = 0;
25640   line_info_label_num = 0;
25641   cur_line_info_table = NULL;
25642   text_section_line_info = NULL;
25643   cold_text_section_line_info = NULL;
25644   separate_line_info = NULL;
25645   info_section_emitted = false;
25646   pubname_table = NULL;
25647   pubtype_table = NULL;
25648   macinfo_table = NULL;
25649   ranges_table = NULL;
25650   ranges_table_allocated = 0;
25651   ranges_table_in_use = 0;
25652   ranges_by_label = 0;
25653   ranges_by_label_allocated = 0;
25654   ranges_by_label_in_use = 0;
25655   have_location_lists = false;
25656   loclabel_num = 0;
25657   poc_label_num = 0;
25658   last_emitted_file = NULL;
25659   label_num = 0;
25660   tmpl_value_parm_die_table = NULL;
25661   generic_type_instances = NULL;
25662   frame_pointer_fb_offset = 0;
25663   frame_pointer_fb_offset_valid = false;
25664   base_types.release ();
25665   XDELETEVEC (producer_string);
25666   producer_string = NULL;
25667 }
25668
25669 #include "gt-dwarf2out.h"