* dwarf2out.c (gen_type_die_with_usage): Call verify_type.
[platform/upstream/linaro-gcc.git] / gcc / dwarf2out.c
1 /* Output Dwarf2 format symbol table information from GCC.
2    Copyright (C) 1992-2015 Free Software Foundation, Inc.
3    Contributed by Gary Funck (gary@intrepid.com).
4    Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
5    Extensively modified by Jason Merrill (jason@cygnus.com).
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
13
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
17 for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3.  If not see
21 <http://www.gnu.org/licenses/>.  */
22
23 /* TODO: Emit .debug_line header even when there are no functions, since
24            the file numbers are used by .debug_info.  Alternately, leave
25            out locations for types and decls.
26          Avoid talking about ctors and op= for PODs.
27          Factor out common prologue sequences into multiple CIEs.  */
28
29 /* The first part of this file deals with the DWARF 2 frame unwind
30    information, which is also used by the GCC efficient exception handling
31    mechanism.  The second part, controlled only by an #ifdef
32    DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
33    information.  */
34
35 /* DWARF2 Abbreviation Glossary:
36
37    CFA = Canonical Frame Address
38            a fixed address on the stack which identifies a call frame.
39            We define it to be the value of SP just before the call insn.
40            The CFA register and offset, which may change during the course
41            of the function, are used to calculate its value at runtime.
42
43    CFI = Call Frame Instruction
44            an instruction for the DWARF2 abstract machine
45
46    CIE = Common Information Entry
47            information describing information common to one or more FDEs
48
49    DIE = Debugging Information Entry
50
51    FDE = Frame Description Entry
52            information describing the stack call frame, in particular,
53            how to restore registers
54
55    DW_CFA_... = DWARF2 CFA call frame instruction
56    DW_TAG_... = DWARF2 DIE tag */
57
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "tm.h"
62 #include "rtl.h"
63 #include "hash-set.h"
64 #include "machmode.h"
65 #include "vec.h"
66 #include "double-int.h"
67 #include "input.h"
68 #include "alias.h"
69 #include "symtab.h"
70 #include "wide-int.h"
71 #include "inchash.h"
72 #include "real.h"
73 #include "tree.h"
74 #include "fold-const.h"
75 #include "stringpool.h"
76 #include "stor-layout.h"
77 #include "varasm.h"
78 #include "hashtab.h"
79 #include "hard-reg-set.h"
80 #include "function.h"
81 #include "emit-rtl.h"
82 #include "hash-table.h"
83 #include "version.h"
84 #include "flags.h"
85 #include "regs.h"
86 #include "rtlhash.h"
87 #include "insn-config.h"
88 #include "reload.h"
89 #include "output.h"
90 #include "statistics.h"
91 #include "fixed-value.h"
92 #include "expmed.h"
93 #include "dojump.h"
94 #include "explow.h"
95 #include "calls.h"
96 #include "stmt.h"
97 #include "expr.h"
98 #include "except.h"
99 #include "dwarf2.h"
100 #include "dwarf2out.h"
101 #include "dwarf2asm.h"
102 #include "toplev.h"
103 #include "md5.h"
104 #include "tm_p.h"
105 #include "diagnostic.h"
106 #include "tree-pretty-print.h"
107 #include "debug.h"
108 #include "target.h"
109 #include "common/common-target.h"
110 #include "langhooks.h"
111 #include "hash-map.h"
112 #include "is-a.h"
113 #include "plugin-api.h"
114 #include "ipa-ref.h"
115 #include "cgraph.h"
116 #include "ira.h"
117 #include "lra.h"
118 #include "dumpfile.h"
119 #include "opts.h"
120 #include "tree-dfa.h"
121 #include "gdb/gdb-index.h"
122 #include "rtl-iter.h"
123
124 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
125 static rtx_insn *last_var_location_insn;
126 static rtx_insn *cached_next_real_insn;
127 static void dwarf2out_decl (tree);
128
129 #ifdef VMS_DEBUGGING_INFO
130 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
131
132 /* Define this macro to be a nonzero value if the directory specifications
133     which are output in the debug info should end with a separator.  */
134 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
135 /* Define this macro to evaluate to a nonzero value if GCC should refrain
136    from generating indirect strings in DWARF2 debug information, for instance
137    if your target is stuck with an old version of GDB that is unable to
138    process them properly or uses VMS Debug.  */
139 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
140 #else
141 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
142 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
143 #endif
144
145 /* ??? Poison these here until it can be done generically.  They've been
146    totally replaced in this file; make sure it stays that way.  */
147 #undef DWARF2_UNWIND_INFO
148 #undef DWARF2_FRAME_INFO
149 #if (GCC_VERSION >= 3000)
150  #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
151 #endif
152
153 /* The size of the target's pointer type.  */
154 #ifndef PTR_SIZE
155 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
156 #endif
157
158 /* Array of RTXes referenced by the debugging information, which therefore
159    must be kept around forever.  */
160 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
161
162 /* A pointer to the base of a list of incomplete types which might be
163    completed at some later time.  incomplete_types_list needs to be a
164    vec<tree, va_gc> *because we want to tell the garbage collector about
165    it.  */
166 static GTY(()) vec<tree, va_gc> *incomplete_types;
167
168 /* A pointer to the base of a table of references to declaration
169    scopes.  This table is a display which tracks the nesting
170    of declaration scopes at the current scope and containing
171    scopes.  This table is used to find the proper place to
172    define type declaration DIE's.  */
173 static GTY(()) vec<tree, va_gc> *decl_scope_table;
174
175 /* Pointers to various DWARF2 sections.  */
176 static GTY(()) section *debug_info_section;
177 static GTY(()) section *debug_skeleton_info_section;
178 static GTY(()) section *debug_abbrev_section;
179 static GTY(()) section *debug_skeleton_abbrev_section;
180 static GTY(()) section *debug_aranges_section;
181 static GTY(()) section *debug_addr_section;
182 static GTY(()) section *debug_macinfo_section;
183 static GTY(()) section *debug_line_section;
184 static GTY(()) section *debug_skeleton_line_section;
185 static GTY(()) section *debug_loc_section;
186 static GTY(()) section *debug_pubnames_section;
187 static GTY(()) section *debug_pubtypes_section;
188 static GTY(()) section *debug_str_section;
189 static GTY(()) section *debug_str_dwo_section;
190 static GTY(()) section *debug_str_offsets_section;
191 static GTY(()) section *debug_ranges_section;
192 static GTY(()) section *debug_frame_section;
193
194 /* Maximum size (in bytes) of an artificially generated label.  */
195 #define MAX_ARTIFICIAL_LABEL_BYTES      30
196
197 /* According to the (draft) DWARF 3 specification, the initial length
198    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
199    bytes are 0xffffffff, followed by the length stored in the next 8
200    bytes.
201
202    However, the SGI/MIPS ABI uses an initial length which is equal to
203    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
204
205 #ifndef DWARF_INITIAL_LENGTH_SIZE
206 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
207 #endif
208
209 /* Round SIZE up to the nearest BOUNDARY.  */
210 #define DWARF_ROUND(SIZE,BOUNDARY) \
211   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
212
213 /* CIE identifier.  */
214 #if HOST_BITS_PER_WIDE_INT >= 64
215 #define DWARF_CIE_ID \
216   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
217 #else
218 #define DWARF_CIE_ID DW_CIE_ID
219 #endif
220
221
222 /* A vector for a table that contains frame description
223    information for each routine.  */
224 #define NOT_INDEXED (-1U)
225 #define NO_INDEX_ASSIGNED (-2U)
226
227 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
228
229 struct GTY((for_user)) indirect_string_node {
230   const char *str;
231   unsigned int refcount;
232   enum dwarf_form form;
233   char *label;
234   unsigned int index;
235 };
236
237 struct indirect_string_hasher : ggc_hasher<indirect_string_node *>
238 {
239   typedef const char *compare_type;
240
241   static hashval_t hash (indirect_string_node *);
242   static bool equal (indirect_string_node *, const char *);
243 };
244
245 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
246
247 /* With split_debug_info, both the comp_dir and dwo_name go in the
248    main object file, rather than the dwo, similar to the force_direct
249    parameter elsewhere but with additional complications:
250
251    1) The string is needed in both the main object file and the dwo.
252    That is, the comp_dir and dwo_name will appear in both places.
253
254    2) Strings can use three forms: DW_FORM_string, DW_FORM_strp or
255    DW_FORM_GNU_str_index.
256
257    3) GCC chooses the form to use late, depending on the size and
258    reference count.
259
260    Rather than forcing the all debug string handling functions and
261    callers to deal with these complications, simply use a separate,
262    special-cased string table for any attribute that should go in the
263    main object file.  This limits the complexity to just the places
264    that need it.  */
265
266 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
267
268 static GTY(()) int dw2_string_counter;
269
270 /* True if the compilation unit places functions in more than one section.  */
271 static GTY(()) bool have_multiple_function_sections = false;
272
273 /* Whether the default text and cold text sections have been used at all.  */
274
275 static GTY(()) bool text_section_used = false;
276 static GTY(()) bool cold_text_section_used = false;
277
278 /* The default cold text section.  */
279 static GTY(()) section *cold_text_section;
280
281 /* The DIE for C++14 'auto' in a function return type.  */
282 static GTY(()) dw_die_ref auto_die;
283
284 /* The DIE for C++14 'decltype(auto)' in a function return type.  */
285 static GTY(()) dw_die_ref decltype_auto_die;
286
287 /* Forward declarations for functions defined in this file.  */
288
289 static char *stripattributes (const char *);
290 static void output_call_frame_info (int);
291 static void dwarf2out_note_section_used (void);
292
293 /* Personality decl of current unit.  Used only when assembler does not support
294    personality CFI.  */
295 static GTY(()) rtx current_unit_personality;
296
297 /* Data and reference forms for relocatable data.  */
298 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
299 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
300
301 #ifndef DEBUG_FRAME_SECTION
302 #define DEBUG_FRAME_SECTION     ".debug_frame"
303 #endif
304
305 #ifndef FUNC_BEGIN_LABEL
306 #define FUNC_BEGIN_LABEL        "LFB"
307 #endif
308
309 #ifndef FUNC_END_LABEL
310 #define FUNC_END_LABEL          "LFE"
311 #endif
312
313 #ifndef PROLOGUE_END_LABEL
314 #define PROLOGUE_END_LABEL      "LPE"
315 #endif
316
317 #ifndef EPILOGUE_BEGIN_LABEL
318 #define EPILOGUE_BEGIN_LABEL    "LEB"
319 #endif
320
321 #ifndef FRAME_BEGIN_LABEL
322 #define FRAME_BEGIN_LABEL       "Lframe"
323 #endif
324 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
325 #define CIE_END_LABEL           "LECIE"
326 #define FDE_LABEL               "LSFDE"
327 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
328 #define FDE_END_LABEL           "LEFDE"
329 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
330 #define LINE_NUMBER_END_LABEL   "LELT"
331 #define LN_PROLOG_AS_LABEL      "LASLTP"
332 #define LN_PROLOG_END_LABEL     "LELTP"
333 #define DIE_LABEL_PREFIX        "DW"
334 \f
335 /* Match the base name of a file to the base name of a compilation unit. */
336
337 static int
338 matches_main_base (const char *path)
339 {
340   /* Cache the last query. */
341   static const char *last_path = NULL;
342   static int last_match = 0;
343   if (path != last_path)
344     {
345       const char *base;
346       int length = base_of_path (path, &base);
347       last_path = path;
348       last_match = (length == main_input_baselength
349                     && memcmp (base, main_input_basename, length) == 0);
350     }
351   return last_match;
352 }
353
354 #ifdef DEBUG_DEBUG_STRUCT
355
356 static int
357 dump_struct_debug (tree type, enum debug_info_usage usage,
358                    enum debug_struct_file criterion, int generic,
359                    int matches, int result)
360 {
361   /* Find the type name. */
362   tree type_decl = TYPE_STUB_DECL (type);
363   tree t = type_decl;
364   const char *name = 0;
365   if (TREE_CODE (t) == TYPE_DECL)
366     t = DECL_NAME (t);
367   if (t)
368     name = IDENTIFIER_POINTER (t);
369
370   fprintf (stderr, "    struct %d %s %s %s %s %d %p %s\n",
371            criterion,
372            DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
373            matches ? "bas" : "hdr",
374            generic ? "gen" : "ord",
375            usage == DINFO_USAGE_DFN ? ";" :
376              usage == DINFO_USAGE_DIR_USE ? "." : "*",
377            result,
378            (void*) type_decl, name);
379   return result;
380 }
381 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
382   dump_struct_debug (type, usage, criterion, generic, matches, result)
383
384 #else
385
386 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
387   (result)
388
389 #endif
390
391 /* Get the number of HOST_WIDE_INTs needed to represent the precision
392    of the number.  */
393
394 static unsigned int
395 get_full_len (const wide_int &op)
396 {
397   return ((op.get_precision () + HOST_BITS_PER_WIDE_INT - 1)
398           / HOST_BITS_PER_WIDE_INT);
399 }
400
401 static bool
402 should_emit_struct_debug (tree type, enum debug_info_usage usage)
403 {
404   enum debug_struct_file criterion;
405   tree type_decl;
406   bool generic = lang_hooks.types.generic_p (type);
407
408   if (generic)
409     criterion = debug_struct_generic[usage];
410   else
411     criterion = debug_struct_ordinary[usage];
412
413   if (criterion == DINFO_STRUCT_FILE_NONE)
414     return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
415   if (criterion == DINFO_STRUCT_FILE_ANY)
416     return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
417
418   type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
419
420   if (type_decl != NULL)
421     {
422      if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
423         return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
424
425       if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
426         return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
427     }
428
429   return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
430 }
431 \f
432 /* Return a pointer to a copy of the section string name S with all
433    attributes stripped off, and an asterisk prepended (for assemble_name).  */
434
435 static inline char *
436 stripattributes (const char *s)
437 {
438   char *stripped = XNEWVEC (char, strlen (s) + 2);
439   char *p = stripped;
440
441   *p++ = '*';
442
443   while (*s && *s != ',')
444     *p++ = *s++;
445
446   *p = '\0';
447   return stripped;
448 }
449
450 /* Switch [BACK] to eh_frame_section.  If we don't have an eh_frame_section,
451    switch to the data section instead, and write out a synthetic start label
452    for collect2 the first time around.  */
453
454 static void
455 switch_to_eh_frame_section (bool back)
456 {
457   tree label;
458
459 #ifdef EH_FRAME_SECTION_NAME
460   if (eh_frame_section == 0)
461     {
462       int flags;
463
464       if (EH_TABLES_CAN_BE_READ_ONLY)
465         {
466           int fde_encoding;
467           int per_encoding;
468           int lsda_encoding;
469
470           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
471                                                        /*global=*/0);
472           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
473                                                        /*global=*/1);
474           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
475                                                         /*global=*/0);
476           flags = ((! flag_pic
477                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
478                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
479                         && (per_encoding & 0x70) != DW_EH_PE_absptr
480                         && (per_encoding & 0x70) != DW_EH_PE_aligned
481                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
482                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
483                    ? 0 : SECTION_WRITE);
484         }
485       else
486         flags = SECTION_WRITE;
487       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
488     }
489 #endif /* EH_FRAME_SECTION_NAME */
490
491   if (eh_frame_section)
492     switch_to_section (eh_frame_section);
493   else
494     {
495       /* We have no special eh_frame section.  Put the information in
496          the data section and emit special labels to guide collect2.  */
497       switch_to_section (data_section);
498
499       if (!back)
500         {
501           label = get_file_function_name ("F");
502           ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
503           targetm.asm_out.globalize_label (asm_out_file,
504                                            IDENTIFIER_POINTER (label));
505           ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
506         }
507     }
508 }
509
510 /* Switch [BACK] to the eh or debug frame table section, depending on
511    FOR_EH.  */
512
513 static void
514 switch_to_frame_table_section (int for_eh, bool back)
515 {
516   if (for_eh)
517     switch_to_eh_frame_section (back);
518   else
519     {
520       if (!debug_frame_section)
521         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
522                                            SECTION_DEBUG, NULL);
523       switch_to_section (debug_frame_section);
524     }
525 }
526
527 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
528
529 enum dw_cfi_oprnd_type
530 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
531 {
532   switch (cfi)
533     {
534     case DW_CFA_nop:
535     case DW_CFA_GNU_window_save:
536     case DW_CFA_remember_state:
537     case DW_CFA_restore_state:
538       return dw_cfi_oprnd_unused;
539
540     case DW_CFA_set_loc:
541     case DW_CFA_advance_loc1:
542     case DW_CFA_advance_loc2:
543     case DW_CFA_advance_loc4:
544     case DW_CFA_MIPS_advance_loc8:
545       return dw_cfi_oprnd_addr;
546
547     case DW_CFA_offset:
548     case DW_CFA_offset_extended:
549     case DW_CFA_def_cfa:
550     case DW_CFA_offset_extended_sf:
551     case DW_CFA_def_cfa_sf:
552     case DW_CFA_restore:
553     case DW_CFA_restore_extended:
554     case DW_CFA_undefined:
555     case DW_CFA_same_value:
556     case DW_CFA_def_cfa_register:
557     case DW_CFA_register:
558     case DW_CFA_expression:
559       return dw_cfi_oprnd_reg_num;
560
561     case DW_CFA_def_cfa_offset:
562     case DW_CFA_GNU_args_size:
563     case DW_CFA_def_cfa_offset_sf:
564       return dw_cfi_oprnd_offset;
565
566     case DW_CFA_def_cfa_expression:
567       return dw_cfi_oprnd_loc;
568
569     default:
570       gcc_unreachable ();
571     }
572 }
573
574 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
575
576 enum dw_cfi_oprnd_type
577 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
578 {
579   switch (cfi)
580     {
581     case DW_CFA_def_cfa:
582     case DW_CFA_def_cfa_sf:
583     case DW_CFA_offset:
584     case DW_CFA_offset_extended_sf:
585     case DW_CFA_offset_extended:
586       return dw_cfi_oprnd_offset;
587
588     case DW_CFA_register:
589       return dw_cfi_oprnd_reg_num;
590
591     case DW_CFA_expression:
592       return dw_cfi_oprnd_loc;
593
594     default:
595       return dw_cfi_oprnd_unused;
596     }
597 }
598
599 /* Output one FDE.  */
600
601 static void
602 output_fde (dw_fde_ref fde, bool for_eh, bool second,
603             char *section_start_label, int fde_encoding, char *augmentation,
604             bool any_lsda_needed, int lsda_encoding)
605 {
606   const char *begin, *end;
607   static unsigned int j;
608   char l1[20], l2[20];
609
610   targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
611                                      /* empty */ 0);
612   targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
613                                   for_eh + j);
614   ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
615   ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
616   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
617     dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
618                          " indicating 64-bit DWARF extension");
619   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
620                         "FDE Length");
621   ASM_OUTPUT_LABEL (asm_out_file, l1);
622
623   if (for_eh)
624     dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
625   else
626     dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
627                            debug_frame_section, "FDE CIE offset");
628
629   begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
630   end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
631
632   if (for_eh)
633     {
634       rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
635       SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
636       dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
637                                        "FDE initial location");
638       dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
639                             end, begin, "FDE address range");
640     }
641   else
642     {
643       dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
644       dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
645     }
646
647   if (augmentation[0])
648     {
649       if (any_lsda_needed)
650         {
651           int size = size_of_encoded_value (lsda_encoding);
652
653           if (lsda_encoding == DW_EH_PE_aligned)
654             {
655               int offset = (  4         /* Length */
656                             + 4         /* CIE offset */
657                             + 2 * size_of_encoded_value (fde_encoding)
658                             + 1         /* Augmentation size */ );
659               int pad = -offset & (PTR_SIZE - 1);
660
661               size += pad;
662               gcc_assert (size_of_uleb128 (size) == 1);
663             }
664
665           dw2_asm_output_data_uleb128 (size, "Augmentation size");
666
667           if (fde->uses_eh_lsda)
668             {
669               ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
670                                            fde->funcdef_number);
671               dw2_asm_output_encoded_addr_rtx (lsda_encoding,
672                                                gen_rtx_SYMBOL_REF (Pmode, l1),
673                                                false,
674                                                "Language Specific Data Area");
675             }
676           else
677             {
678               if (lsda_encoding == DW_EH_PE_aligned)
679                 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
680               dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
681                                    "Language Specific Data Area (none)");
682             }
683         }
684       else
685         dw2_asm_output_data_uleb128 (0, "Augmentation size");
686     }
687
688   /* Loop through the Call Frame Instructions associated with this FDE.  */
689   fde->dw_fde_current_label = begin;
690   {
691     size_t from, until, i;
692
693     from = 0;
694     until = vec_safe_length (fde->dw_fde_cfi);
695
696     if (fde->dw_fde_second_begin == NULL)
697       ;
698     else if (!second)
699       until = fde->dw_fde_switch_cfi_index;
700     else
701       from = fde->dw_fde_switch_cfi_index;
702
703     for (i = from; i < until; i++)
704       output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
705   }
706
707   /* If we are to emit a ref/link from function bodies to their frame tables,
708      do it now.  This is typically performed to make sure that tables
709      associated with functions are dragged with them and not discarded in
710      garbage collecting links. We need to do this on a per function basis to
711      cope with -ffunction-sections.  */
712
713 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
714   /* Switch to the function section, emit the ref to the tables, and
715      switch *back* into the table section.  */
716   switch_to_section (function_section (fde->decl));
717   ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
718   switch_to_frame_table_section (for_eh, true);
719 #endif
720
721   /* Pad the FDE out to an address sized boundary.  */
722   ASM_OUTPUT_ALIGN (asm_out_file,
723                     floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
724   ASM_OUTPUT_LABEL (asm_out_file, l2);
725
726   j += 2;
727 }
728
729 /* Return true if frame description entry FDE is needed for EH.  */
730
731 static bool
732 fde_needed_for_eh_p (dw_fde_ref fde)
733 {
734   if (flag_asynchronous_unwind_tables)
735     return true;
736
737   if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
738     return true;
739
740   if (fde->uses_eh_lsda)
741     return true;
742
743   /* If exceptions are enabled, we have collected nothrow info.  */
744   if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
745     return false;
746
747   return true;
748 }
749
750 /* Output the call frame information used to record information
751    that relates to calculating the frame pointer, and records the
752    location of saved registers.  */
753
754 static void
755 output_call_frame_info (int for_eh)
756 {
757   unsigned int i;
758   dw_fde_ref fde;
759   dw_cfi_ref cfi;
760   char l1[20], l2[20], section_start_label[20];
761   bool any_lsda_needed = false;
762   char augmentation[6];
763   int augmentation_size;
764   int fde_encoding = DW_EH_PE_absptr;
765   int per_encoding = DW_EH_PE_absptr;
766   int lsda_encoding = DW_EH_PE_absptr;
767   int return_reg;
768   rtx personality = NULL;
769   int dw_cie_version;
770
771   /* Don't emit a CIE if there won't be any FDEs.  */
772   if (!fde_vec)
773     return;
774
775   /* Nothing to do if the assembler's doing it all.  */
776   if (dwarf2out_do_cfi_asm ())
777     return;
778
779   /* If we don't have any functions we'll want to unwind out of, don't emit
780      any EH unwind information.  If we make FDEs linkonce, we may have to
781      emit an empty label for an FDE that wouldn't otherwise be emitted.  We
782      want to avoid having an FDE kept around when the function it refers to
783      is discarded.  Example where this matters: a primary function template
784      in C++ requires EH information, an explicit specialization doesn't.  */
785   if (for_eh)
786     {
787       bool any_eh_needed = false;
788
789       FOR_EACH_VEC_ELT (*fde_vec, i, fde)
790         {
791           if (fde->uses_eh_lsda)
792             any_eh_needed = any_lsda_needed = true;
793           else if (fde_needed_for_eh_p (fde))
794             any_eh_needed = true;
795           else if (TARGET_USES_WEAK_UNWIND_INFO)
796             targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
797         }
798
799       if (!any_eh_needed)
800         return;
801     }
802
803   /* We're going to be generating comments, so turn on app.  */
804   if (flag_debug_asm)
805     app_enable ();
806
807   /* Switch to the proper frame section, first time.  */
808   switch_to_frame_table_section (for_eh, false);
809
810   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
811   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
812
813   /* Output the CIE.  */
814   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
815   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
816   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
817     dw2_asm_output_data (4, 0xffffffff,
818       "Initial length escape value indicating 64-bit DWARF extension");
819   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
820                         "Length of Common Information Entry");
821   ASM_OUTPUT_LABEL (asm_out_file, l1);
822
823   /* Now that the CIE pointer is PC-relative for EH,
824      use 0 to identify the CIE.  */
825   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
826                        (for_eh ? 0 : DWARF_CIE_ID),
827                        "CIE Identifier Tag");
828
829   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
830      use CIE version 1, unless that would produce incorrect results
831      due to overflowing the return register column.  */
832   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
833   dw_cie_version = 1;
834   if (return_reg >= 256 || dwarf_version > 2)
835     dw_cie_version = 3;
836   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
837
838   augmentation[0] = 0;
839   augmentation_size = 0;
840
841   personality = current_unit_personality;
842   if (for_eh)
843     {
844       char *p;
845
846       /* Augmentation:
847          z      Indicates that a uleb128 is present to size the
848                 augmentation section.
849          L      Indicates the encoding (and thus presence) of
850                 an LSDA pointer in the FDE augmentation.
851          R      Indicates a non-default pointer encoding for
852                 FDE code pointers.
853          P      Indicates the presence of an encoding + language
854                 personality routine in the CIE augmentation.  */
855
856       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
857       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
858       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
859
860       p = augmentation + 1;
861       if (personality)
862         {
863           *p++ = 'P';
864           augmentation_size += 1 + size_of_encoded_value (per_encoding);
865           assemble_external_libcall (personality);
866         }
867       if (any_lsda_needed)
868         {
869           *p++ = 'L';
870           augmentation_size += 1;
871         }
872       if (fde_encoding != DW_EH_PE_absptr)
873         {
874           *p++ = 'R';
875           augmentation_size += 1;
876         }
877       if (p > augmentation + 1)
878         {
879           augmentation[0] = 'z';
880           *p = '\0';
881         }
882
883       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
884       if (personality && per_encoding == DW_EH_PE_aligned)
885         {
886           int offset = (  4             /* Length */
887                         + 4             /* CIE Id */
888                         + 1             /* CIE version */
889                         + strlen (augmentation) + 1     /* Augmentation */
890                         + size_of_uleb128 (1)           /* Code alignment */
891                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
892                         + 1             /* RA column */
893                         + 1             /* Augmentation size */
894                         + 1             /* Personality encoding */ );
895           int pad = -offset & (PTR_SIZE - 1);
896
897           augmentation_size += pad;
898
899           /* Augmentations should be small, so there's scarce need to
900              iterate for a solution.  Die if we exceed one uleb128 byte.  */
901           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
902         }
903     }
904
905   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
906   if (dw_cie_version >= 4)
907     {
908       dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
909       dw2_asm_output_data (1, 0, "CIE Segment Size");
910     }
911   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
912   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
913                                "CIE Data Alignment Factor");
914
915   if (dw_cie_version == 1)
916     dw2_asm_output_data (1, return_reg, "CIE RA Column");
917   else
918     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
919
920   if (augmentation[0])
921     {
922       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
923       if (personality)
924         {
925           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
926                                eh_data_format_name (per_encoding));
927           dw2_asm_output_encoded_addr_rtx (per_encoding,
928                                            personality,
929                                            true, NULL);
930         }
931
932       if (any_lsda_needed)
933         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
934                              eh_data_format_name (lsda_encoding));
935
936       if (fde_encoding != DW_EH_PE_absptr)
937         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
938                              eh_data_format_name (fde_encoding));
939     }
940
941   FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
942     output_cfi (cfi, NULL, for_eh);
943
944   /* Pad the CIE out to an address sized boundary.  */
945   ASM_OUTPUT_ALIGN (asm_out_file,
946                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
947   ASM_OUTPUT_LABEL (asm_out_file, l2);
948
949   /* Loop through all of the FDE's.  */
950   FOR_EACH_VEC_ELT (*fde_vec, i, fde)
951     {
952       unsigned int k;
953
954       /* Don't emit EH unwind info for leaf functions that don't need it.  */
955       if (for_eh && !fde_needed_for_eh_p (fde))
956         continue;
957
958       for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
959         output_fde (fde, for_eh, k, section_start_label, fde_encoding,
960                     augmentation, any_lsda_needed, lsda_encoding);
961     }
962
963   if (for_eh && targetm.terminate_dw2_eh_frame_info)
964     dw2_asm_output_data (4, 0, "End of Table");
965
966   /* Turn off app to make assembly quicker.  */
967   if (flag_debug_asm)
968     app_disable ();
969 }
970
971 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed.  */
972
973 static void
974 dwarf2out_do_cfi_startproc (bool second)
975 {
976   int enc;
977   rtx ref;
978   rtx personality = get_personality_function (current_function_decl);
979
980   fprintf (asm_out_file, "\t.cfi_startproc\n");
981
982   if (personality)
983     {
984       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
985       ref = personality;
986
987       /* ??? The GAS support isn't entirely consistent.  We have to
988          handle indirect support ourselves, but PC-relative is done
989          in the assembler.  Further, the assembler can't handle any
990          of the weirder relocation types.  */
991       if (enc & DW_EH_PE_indirect)
992         ref = dw2_force_const_mem (ref, true);
993
994       fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
995       output_addr_const (asm_out_file, ref);
996       fputc ('\n', asm_out_file);
997     }
998
999   if (crtl->uses_eh_lsda)
1000     {
1001       char lab[20];
1002
1003       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
1004       ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
1005                                    current_function_funcdef_no);
1006       ref = gen_rtx_SYMBOL_REF (Pmode, lab);
1007       SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
1008
1009       if (enc & DW_EH_PE_indirect)
1010         ref = dw2_force_const_mem (ref, true);
1011
1012       fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
1013       output_addr_const (asm_out_file, ref);
1014       fputc ('\n', asm_out_file);
1015     }
1016 }
1017
1018 /* Allocate CURRENT_FDE.  Immediately initialize all we can, noting that
1019    this allocation may be done before pass_final.  */
1020
1021 dw_fde_ref
1022 dwarf2out_alloc_current_fde (void)
1023 {
1024   dw_fde_ref fde;
1025
1026   fde = ggc_cleared_alloc<dw_fde_node> ();
1027   fde->decl = current_function_decl;
1028   fde->funcdef_number = current_function_funcdef_no;
1029   fde->fde_index = vec_safe_length (fde_vec);
1030   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1031   fde->uses_eh_lsda = crtl->uses_eh_lsda;
1032   fde->nothrow = crtl->nothrow;
1033   fde->drap_reg = INVALID_REGNUM;
1034   fde->vdrap_reg = INVALID_REGNUM;
1035
1036   /* Record the FDE associated with this function.  */
1037   cfun->fde = fde;
1038   vec_safe_push (fde_vec, fde);
1039
1040   return fde;
1041 }
1042
1043 /* Output a marker (i.e. a label) for the beginning of a function, before
1044    the prologue.  */
1045
1046 void
1047 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1048                           const char *file ATTRIBUTE_UNUSED)
1049 {
1050   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1051   char * dup_label;
1052   dw_fde_ref fde;
1053   section *fnsec;
1054   bool do_frame;
1055
1056   current_function_func_begin_label = NULL;
1057
1058   do_frame = dwarf2out_do_frame ();
1059
1060   /* ??? current_function_func_begin_label is also used by except.c for
1061      call-site information.  We must emit this label if it might be used.  */
1062   if (!do_frame
1063       && (!flag_exceptions
1064           || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1065     return;
1066
1067   fnsec = function_section (current_function_decl);
1068   switch_to_section (fnsec);
1069   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1070                                current_function_funcdef_no);
1071   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1072                           current_function_funcdef_no);
1073   dup_label = xstrdup (label);
1074   current_function_func_begin_label = dup_label;
1075
1076   /* We can elide the fde allocation if we're not emitting debug info.  */
1077   if (!do_frame)
1078     return;
1079
1080   /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1081      emit insns as rtx but bypass the bulk of rest_of_compilation, which
1082      would include pass_dwarf2_frame.  If we've not created the FDE yet,
1083      do so now.  */
1084   fde = cfun->fde;
1085   if (fde == NULL)
1086     fde = dwarf2out_alloc_current_fde ();
1087
1088   /* Initialize the bits of CURRENT_FDE that were not available earlier.  */
1089   fde->dw_fde_begin = dup_label;
1090   fde->dw_fde_current_label = dup_label;
1091   fde->in_std_section = (fnsec == text_section
1092                          || (cold_text_section && fnsec == cold_text_section));
1093
1094   /* We only want to output line number information for the genuine dwarf2
1095      prologue case, not the eh frame case.  */
1096 #ifdef DWARF2_DEBUGGING_INFO
1097   if (file)
1098     dwarf2out_source_line (line, file, 0, true);
1099 #endif
1100
1101   if (dwarf2out_do_cfi_asm ())
1102     dwarf2out_do_cfi_startproc (false);
1103   else
1104     {
1105       rtx personality = get_personality_function (current_function_decl);
1106       if (!current_unit_personality)
1107         current_unit_personality = personality;
1108
1109       /* We cannot keep a current personality per function as without CFI
1110          asm, at the point where we emit the CFI data, there is no current
1111          function anymore.  */
1112       if (personality && current_unit_personality != personality)
1113         sorry ("multiple EH personalities are supported only with assemblers "
1114                "supporting .cfi_personality directive");
1115     }
1116 }
1117
1118 /* Output a marker (i.e. a label) for the end of the generated code
1119    for a function prologue.  This gets called *after* the prologue code has
1120    been generated.  */
1121
1122 void
1123 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1124                         const char *file ATTRIBUTE_UNUSED)
1125 {
1126   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1127
1128   /* Output a label to mark the endpoint of the code generated for this
1129      function.  */
1130   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1131                                current_function_funcdef_no);
1132   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1133                           current_function_funcdef_no);
1134   cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1135 }
1136
1137 /* Output a marker (i.e. a label) for the beginning of the generated code
1138    for a function epilogue.  This gets called *before* the prologue code has
1139    been generated.  */
1140
1141 void
1142 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1143                           const char *file ATTRIBUTE_UNUSED)
1144 {
1145   dw_fde_ref fde = cfun->fde;
1146   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1147
1148   if (fde->dw_fde_vms_begin_epilogue)
1149     return;
1150
1151   /* Output a label to mark the endpoint of the code generated for this
1152      function.  */
1153   ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1154                                current_function_funcdef_no);
1155   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1156                           current_function_funcdef_no);
1157   fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1158 }
1159
1160 /* Output a marker (i.e. a label) for the absolute end of the generated code
1161    for a function definition.  This gets called *after* the epilogue code has
1162    been generated.  */
1163
1164 void
1165 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1166                         const char *file ATTRIBUTE_UNUSED)
1167 {
1168   dw_fde_ref fde;
1169   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1170
1171   last_var_location_insn = NULL;
1172   cached_next_real_insn = NULL;
1173
1174   if (dwarf2out_do_cfi_asm ())
1175     fprintf (asm_out_file, "\t.cfi_endproc\n");
1176
1177   /* Output a label to mark the endpoint of the code generated for this
1178      function.  */
1179   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1180                                current_function_funcdef_no);
1181   ASM_OUTPUT_LABEL (asm_out_file, label);
1182   fde = cfun->fde;
1183   gcc_assert (fde != NULL);
1184   if (fde->dw_fde_second_begin == NULL)
1185     fde->dw_fde_end = xstrdup (label);
1186 }
1187
1188 void
1189 dwarf2out_frame_finish (void)
1190 {
1191   /* Output call frame information.  */
1192   if (targetm.debug_unwind_info () == UI_DWARF2)
1193     output_call_frame_info (0);
1194
1195   /* Output another copy for the unwinder.  */
1196   if ((flag_unwind_tables || flag_exceptions)
1197       && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1198     output_call_frame_info (1);
1199 }
1200
1201 /* Note that the current function section is being used for code.  */
1202
1203 static void
1204 dwarf2out_note_section_used (void)
1205 {
1206   section *sec = current_function_section ();
1207   if (sec == text_section)
1208     text_section_used = true;
1209   else if (sec == cold_text_section)
1210     cold_text_section_used = true;
1211 }
1212
1213 static void var_location_switch_text_section (void);
1214 static void set_cur_line_info_table (section *);
1215
1216 void
1217 dwarf2out_switch_text_section (void)
1218 {
1219   section *sect;
1220   dw_fde_ref fde = cfun->fde;
1221
1222   gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1223
1224   if (!in_cold_section_p)
1225     {
1226       fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1227       fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1228       fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1229     }
1230   else
1231     {
1232       fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1233       fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1234       fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1235     }
1236   have_multiple_function_sections = true;
1237
1238   /* There is no need to mark used sections when not debugging.  */
1239   if (cold_text_section != NULL)
1240     dwarf2out_note_section_used ();
1241
1242   if (dwarf2out_do_cfi_asm ())
1243     fprintf (asm_out_file, "\t.cfi_endproc\n");
1244
1245   /* Now do the real section switch.  */
1246   sect = current_function_section ();
1247   switch_to_section (sect);
1248
1249   fde->second_in_std_section
1250     = (sect == text_section
1251        || (cold_text_section && sect == cold_text_section));
1252
1253   if (dwarf2out_do_cfi_asm ())
1254     dwarf2out_do_cfi_startproc (true);
1255
1256   var_location_switch_text_section ();
1257
1258   if (cold_text_section != NULL)
1259     set_cur_line_info_table (sect);
1260 }
1261 \f
1262 /* And now, the subset of the debugging information support code necessary
1263    for emitting location expressions.  */
1264
1265 /* Data about a single source file.  */
1266 struct GTY((for_user)) dwarf_file_data {
1267   const char * filename;
1268   int emitted_number;
1269 };
1270
1271 typedef struct GTY(()) deferred_locations_struct
1272 {
1273   tree variable;
1274   dw_die_ref die;
1275 } deferred_locations;
1276
1277
1278 static GTY(()) vec<deferred_locations, va_gc> *deferred_locations_list;
1279
1280
1281 /* Describe an entry into the .debug_addr section.  */
1282
1283 enum ate_kind {
1284   ate_kind_rtx,
1285   ate_kind_rtx_dtprel,
1286   ate_kind_label
1287 };
1288
1289 typedef struct GTY((for_user)) addr_table_entry_struct {
1290   enum ate_kind kind;
1291   unsigned int refcount;
1292   unsigned int index;
1293   union addr_table_entry_struct_union
1294     {
1295       rtx GTY ((tag ("0"))) rtl;
1296       char * GTY ((tag ("1"))) label;
1297     }
1298   GTY ((desc ("%1.kind"))) addr;
1299 }
1300 addr_table_entry;
1301
1302 /* Location lists are ranges + location descriptions for that range,
1303    so you can track variables that are in different places over
1304    their entire life.  */
1305 typedef struct GTY(()) dw_loc_list_struct {
1306   dw_loc_list_ref dw_loc_next;
1307   const char *begin; /* Label and addr_entry for start of range */
1308   addr_table_entry *begin_entry;
1309   const char *end;  /* Label for end of range */
1310   char *ll_symbol; /* Label for beginning of location list.
1311                       Only on head of list */
1312   const char *section; /* Section this loclist is relative to */
1313   dw_loc_descr_ref expr;
1314   hashval_t hash;
1315   /* True if all addresses in this and subsequent lists are known to be
1316      resolved.  */
1317   bool resolved_addr;
1318   /* True if this list has been replaced by dw_loc_next.  */
1319   bool replaced;
1320   bool emitted;
1321   /* True if the range should be emitted even if begin and end
1322      are the same.  */
1323   bool force;
1324 } dw_loc_list_node;
1325
1326 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1327
1328 /* Convert a DWARF stack opcode into its string name.  */
1329
1330 static const char *
1331 dwarf_stack_op_name (unsigned int op)
1332 {
1333   const char *name = get_DW_OP_name (op);
1334
1335   if (name != NULL)
1336     return name;
1337
1338   return "OP_<unknown>";
1339 }
1340
1341 /* Return a pointer to a newly allocated location description.  Location
1342    descriptions are simple expression terms that can be strung
1343    together to form more complicated location (address) descriptions.  */
1344
1345 static inline dw_loc_descr_ref
1346 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1347                unsigned HOST_WIDE_INT oprnd2)
1348 {
1349   dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1350
1351   descr->dw_loc_opc = op;
1352   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1353   descr->dw_loc_oprnd1.val_entry = NULL;
1354   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1355   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1356   descr->dw_loc_oprnd2.val_entry = NULL;
1357   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1358
1359   return descr;
1360 }
1361
1362 /* Return a pointer to a newly allocated location description for
1363    REG and OFFSET.  */
1364
1365 static inline dw_loc_descr_ref
1366 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
1367 {
1368   if (reg <= 31)
1369     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1370                           offset, 0);
1371   else
1372     return new_loc_descr (DW_OP_bregx, reg, offset);
1373 }
1374
1375 /* Add a location description term to a location description expression.  */
1376
1377 static inline void
1378 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1379 {
1380   dw_loc_descr_ref *d;
1381
1382   /* Find the end of the chain.  */
1383   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1384     ;
1385
1386   *d = descr;
1387 }
1388
1389 /* Compare two location operands for exact equality.  */
1390
1391 static bool
1392 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1393 {
1394   if (a->val_class != b->val_class)
1395     return false;
1396   switch (a->val_class)
1397     {
1398     case dw_val_class_none:
1399       return true;
1400     case dw_val_class_addr:
1401       return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1402
1403     case dw_val_class_offset:
1404     case dw_val_class_unsigned_const:
1405     case dw_val_class_const:
1406     case dw_val_class_range_list:
1407     case dw_val_class_lineptr:
1408     case dw_val_class_macptr:
1409       /* These are all HOST_WIDE_INT, signed or unsigned.  */
1410       return a->v.val_unsigned == b->v.val_unsigned;
1411
1412     case dw_val_class_loc:
1413       return a->v.val_loc == b->v.val_loc;
1414     case dw_val_class_loc_list:
1415       return a->v.val_loc_list == b->v.val_loc_list;
1416     case dw_val_class_die_ref:
1417       return a->v.val_die_ref.die == b->v.val_die_ref.die;
1418     case dw_val_class_fde_ref:
1419       return a->v.val_fde_index == b->v.val_fde_index;
1420     case dw_val_class_lbl_id:
1421     case dw_val_class_high_pc:
1422       return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1423     case dw_val_class_str:
1424       return a->v.val_str == b->v.val_str;
1425     case dw_val_class_flag:
1426       return a->v.val_flag == b->v.val_flag;
1427     case dw_val_class_file:
1428       return a->v.val_file == b->v.val_file;
1429     case dw_val_class_decl_ref:
1430       return a->v.val_decl_ref == b->v.val_decl_ref;
1431     
1432     case dw_val_class_const_double:
1433       return (a->v.val_double.high == b->v.val_double.high
1434               && a->v.val_double.low == b->v.val_double.low);
1435
1436     case dw_val_class_wide_int:
1437       return *a->v.val_wide == *b->v.val_wide;
1438
1439     case dw_val_class_vec:
1440       {
1441         size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1442         size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1443
1444         return (a_len == b_len
1445                 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1446       }
1447
1448     case dw_val_class_data8:
1449       return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1450
1451     case dw_val_class_vms_delta:
1452       return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1453               && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1454     }
1455   gcc_unreachable ();
1456 }
1457
1458 /* Compare two location atoms for exact equality.  */
1459
1460 static bool
1461 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1462 {
1463   if (a->dw_loc_opc != b->dw_loc_opc)
1464     return false;
1465
1466   /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1467      address size, but since we always allocate cleared storage it
1468      should be zero for other types of locations.  */
1469   if (a->dtprel != b->dtprel)
1470     return false;
1471
1472   return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1473           && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1474 }
1475
1476 /* Compare two complete location expressions for exact equality.  */
1477
1478 bool
1479 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1480 {
1481   while (1)
1482     {
1483       if (a == b)
1484         return true;
1485       if (a == NULL || b == NULL)
1486         return false;
1487       if (!loc_descr_equal_p_1 (a, b))
1488         return false;
1489
1490       a = a->dw_loc_next;
1491       b = b->dw_loc_next;
1492     }
1493 }
1494
1495
1496 /* Add a constant OFFSET to a location expression.  */
1497
1498 static void
1499 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1500 {
1501   dw_loc_descr_ref loc;
1502   HOST_WIDE_INT *p;
1503
1504   gcc_assert (*list_head != NULL);
1505
1506   if (!offset)
1507     return;
1508
1509   /* Find the end of the chain.  */
1510   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1511     ;
1512
1513   p = NULL;
1514   if (loc->dw_loc_opc == DW_OP_fbreg
1515       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1516     p = &loc->dw_loc_oprnd1.v.val_int;
1517   else if (loc->dw_loc_opc == DW_OP_bregx)
1518     p = &loc->dw_loc_oprnd2.v.val_int;
1519
1520   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1521      offset.  Don't optimize if an signed integer overflow would happen.  */
1522   if (p != NULL
1523       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1524           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1525     *p += offset;
1526
1527   else if (offset > 0)
1528     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1529
1530   else
1531     {
1532       loc->dw_loc_next = int_loc_descriptor (-offset);
1533       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1534     }
1535 }
1536
1537 /* Add a constant OFFSET to a location list.  */
1538
1539 static void
1540 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1541 {
1542   dw_loc_list_ref d;
1543   for (d = list_head; d != NULL; d = d->dw_loc_next)
1544     loc_descr_plus_const (&d->expr, offset);
1545 }
1546
1547 #define DWARF_REF_SIZE  \
1548   (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1549
1550 static unsigned long int get_base_type_offset (dw_die_ref);
1551
1552 /* Return the size of a location descriptor.  */
1553
1554 static unsigned long
1555 size_of_loc_descr (dw_loc_descr_ref loc)
1556 {
1557   unsigned long size = 1;
1558
1559   switch (loc->dw_loc_opc)
1560     {
1561     case DW_OP_addr:
1562       size += DWARF2_ADDR_SIZE;
1563       break;
1564     case DW_OP_GNU_addr_index:
1565     case DW_OP_GNU_const_index:
1566       gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1567       size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1568       break;
1569     case DW_OP_const1u:
1570     case DW_OP_const1s:
1571       size += 1;
1572       break;
1573     case DW_OP_const2u:
1574     case DW_OP_const2s:
1575       size += 2;
1576       break;
1577     case DW_OP_const4u:
1578     case DW_OP_const4s:
1579       size += 4;
1580       break;
1581     case DW_OP_const8u:
1582     case DW_OP_const8s:
1583       size += 8;
1584       break;
1585     case DW_OP_constu:
1586       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1587       break;
1588     case DW_OP_consts:
1589       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1590       break;
1591     case DW_OP_pick:
1592       size += 1;
1593       break;
1594     case DW_OP_plus_uconst:
1595       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1596       break;
1597     case DW_OP_skip:
1598     case DW_OP_bra:
1599       size += 2;
1600       break;
1601     case DW_OP_breg0:
1602     case DW_OP_breg1:
1603     case DW_OP_breg2:
1604     case DW_OP_breg3:
1605     case DW_OP_breg4:
1606     case DW_OP_breg5:
1607     case DW_OP_breg6:
1608     case DW_OP_breg7:
1609     case DW_OP_breg8:
1610     case DW_OP_breg9:
1611     case DW_OP_breg10:
1612     case DW_OP_breg11:
1613     case DW_OP_breg12:
1614     case DW_OP_breg13:
1615     case DW_OP_breg14:
1616     case DW_OP_breg15:
1617     case DW_OP_breg16:
1618     case DW_OP_breg17:
1619     case DW_OP_breg18:
1620     case DW_OP_breg19:
1621     case DW_OP_breg20:
1622     case DW_OP_breg21:
1623     case DW_OP_breg22:
1624     case DW_OP_breg23:
1625     case DW_OP_breg24:
1626     case DW_OP_breg25:
1627     case DW_OP_breg26:
1628     case DW_OP_breg27:
1629     case DW_OP_breg28:
1630     case DW_OP_breg29:
1631     case DW_OP_breg30:
1632     case DW_OP_breg31:
1633       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1634       break;
1635     case DW_OP_regx:
1636       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1637       break;
1638     case DW_OP_fbreg:
1639       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1640       break;
1641     case DW_OP_bregx:
1642       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1643       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1644       break;
1645     case DW_OP_piece:
1646       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1647       break;
1648     case DW_OP_bit_piece:
1649       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1650       size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1651       break;
1652     case DW_OP_deref_size:
1653     case DW_OP_xderef_size:
1654       size += 1;
1655       break;
1656     case DW_OP_call2:
1657       size += 2;
1658       break;
1659     case DW_OP_call4:
1660       size += 4;
1661       break;
1662     case DW_OP_call_ref:
1663       size += DWARF_REF_SIZE;
1664       break;
1665     case DW_OP_implicit_value:
1666       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1667               + loc->dw_loc_oprnd1.v.val_unsigned;
1668       break;
1669     case DW_OP_GNU_implicit_pointer:
1670       size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1671       break;
1672     case DW_OP_GNU_entry_value:
1673       {
1674         unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1675         size += size_of_uleb128 (op_size) + op_size;
1676         break;
1677       }
1678     case DW_OP_GNU_const_type:
1679       {
1680         unsigned long o
1681           = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1682         size += size_of_uleb128 (o) + 1;
1683         switch (loc->dw_loc_oprnd2.val_class)
1684           {
1685           case dw_val_class_vec:
1686             size += loc->dw_loc_oprnd2.v.val_vec.length
1687                     * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1688             break;
1689           case dw_val_class_const:
1690             size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1691             break;
1692           case dw_val_class_const_double:
1693             size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1694             break;
1695           case dw_val_class_wide_int:
1696             size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1697                      * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1698             break;
1699           default:
1700             gcc_unreachable ();
1701           }
1702         break;
1703       }
1704     case DW_OP_GNU_regval_type:
1705       {
1706         unsigned long o
1707           = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1708         size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1709                 + size_of_uleb128 (o);
1710       }
1711       break;
1712     case DW_OP_GNU_deref_type:
1713       {
1714         unsigned long o
1715           = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1716         size += 1 + size_of_uleb128 (o);
1717       }
1718       break;
1719     case DW_OP_GNU_convert:
1720     case DW_OP_GNU_reinterpret:
1721       if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1722         size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1723       else
1724         {
1725           unsigned long o
1726             = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1727           size += size_of_uleb128 (o);
1728         }
1729       break;
1730     case DW_OP_GNU_parameter_ref:
1731       size += 4;
1732       break;
1733     default:
1734       break;
1735     }
1736
1737   return size;
1738 }
1739
1740 /* Return the size of a series of location descriptors.  */
1741
1742 unsigned long
1743 size_of_locs (dw_loc_descr_ref loc)
1744 {
1745   dw_loc_descr_ref l;
1746   unsigned long size;
1747
1748   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1749      field, to avoid writing to a PCH file.  */
1750   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1751     {
1752       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1753         break;
1754       size += size_of_loc_descr (l);
1755     }
1756   if (! l)
1757     return size;
1758
1759   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1760     {
1761       l->dw_loc_addr = size;
1762       size += size_of_loc_descr (l);
1763     }
1764
1765   return size;
1766 }
1767
1768 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1769 static void get_ref_die_offset_label (char *, dw_die_ref);
1770 static unsigned long int get_ref_die_offset (dw_die_ref);
1771
1772 /* Output location description stack opcode's operands (if any).
1773    The for_eh_or_skip parameter controls whether register numbers are
1774    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1775    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1776    info).  This should be suppressed for the cases that have not been converted
1777    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
1778
1779 static void
1780 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1781 {
1782   dw_val_ref val1 = &loc->dw_loc_oprnd1;
1783   dw_val_ref val2 = &loc->dw_loc_oprnd2;
1784
1785   switch (loc->dw_loc_opc)
1786     {
1787 #ifdef DWARF2_DEBUGGING_INFO
1788     case DW_OP_const2u:
1789     case DW_OP_const2s:
1790       dw2_asm_output_data (2, val1->v.val_int, NULL);
1791       break;
1792     case DW_OP_const4u:
1793       if (loc->dtprel)
1794         {
1795           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1796           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1797                                                val1->v.val_addr);
1798           fputc ('\n', asm_out_file);
1799           break;
1800         }
1801       /* FALLTHRU */
1802     case DW_OP_const4s:
1803       dw2_asm_output_data (4, val1->v.val_int, NULL);
1804       break;
1805     case DW_OP_const8u:
1806       if (loc->dtprel)
1807         {
1808           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1809           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1810                                                val1->v.val_addr);
1811           fputc ('\n', asm_out_file);
1812           break;
1813         }
1814       /* FALLTHRU */
1815     case DW_OP_const8s:
1816       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1817       dw2_asm_output_data (8, val1->v.val_int, NULL);
1818       break;
1819     case DW_OP_skip:
1820     case DW_OP_bra:
1821       {
1822         int offset;
1823
1824         gcc_assert (val1->val_class == dw_val_class_loc);
1825         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1826
1827         dw2_asm_output_data (2, offset, NULL);
1828       }
1829       break;
1830     case DW_OP_implicit_value:
1831       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1832       switch (val2->val_class)
1833         {
1834         case dw_val_class_const:
1835           dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1836           break;
1837         case dw_val_class_vec:
1838           {
1839             unsigned int elt_size = val2->v.val_vec.elt_size;
1840             unsigned int len = val2->v.val_vec.length;
1841             unsigned int i;
1842             unsigned char *p;
1843
1844             if (elt_size > sizeof (HOST_WIDE_INT))
1845               {
1846                 elt_size /= 2;
1847                 len *= 2;
1848               }
1849             for (i = 0, p = val2->v.val_vec.array;
1850                  i < len;
1851                  i++, p += elt_size)
1852               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1853                                    "fp or vector constant word %u", i);
1854           }
1855           break;
1856         case dw_val_class_const_double:
1857           {
1858             unsigned HOST_WIDE_INT first, second;
1859
1860             if (WORDS_BIG_ENDIAN)
1861               {
1862                 first = val2->v.val_double.high;
1863                 second = val2->v.val_double.low;
1864               }
1865             else
1866               {
1867                 first = val2->v.val_double.low;
1868                 second = val2->v.val_double.high;
1869               }
1870             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1871                                  first, NULL);
1872             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1873                                  second, NULL);
1874           }
1875           break;
1876         case dw_val_class_wide_int:
1877           {
1878             int i;
1879             int len = get_full_len (*val2->v.val_wide);
1880             if (WORDS_BIG_ENDIAN)
1881               for (i = len - 1; i >= 0; --i)
1882                 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1883                                      val2->v.val_wide->elt (i), NULL);
1884             else
1885               for (i = 0; i < len; ++i)
1886                 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1887                                      val2->v.val_wide->elt (i), NULL);
1888           }
1889           break;
1890         case dw_val_class_addr:
1891           gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1892           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1893           break;
1894         default:
1895           gcc_unreachable ();
1896         }
1897       break;
1898 #else
1899     case DW_OP_const2u:
1900     case DW_OP_const2s:
1901     case DW_OP_const4u:
1902     case DW_OP_const4s:
1903     case DW_OP_const8u:
1904     case DW_OP_const8s:
1905     case DW_OP_skip:
1906     case DW_OP_bra:
1907     case DW_OP_implicit_value:
1908       /* We currently don't make any attempt to make sure these are
1909          aligned properly like we do for the main unwind info, so
1910          don't support emitting things larger than a byte if we're
1911          only doing unwinding.  */
1912       gcc_unreachable ();
1913 #endif
1914     case DW_OP_const1u:
1915     case DW_OP_const1s:
1916       dw2_asm_output_data (1, val1->v.val_int, NULL);
1917       break;
1918     case DW_OP_constu:
1919       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1920       break;
1921     case DW_OP_consts:
1922       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1923       break;
1924     case DW_OP_pick:
1925       dw2_asm_output_data (1, val1->v.val_int, NULL);
1926       break;
1927     case DW_OP_plus_uconst:
1928       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1929       break;
1930     case DW_OP_breg0:
1931     case DW_OP_breg1:
1932     case DW_OP_breg2:
1933     case DW_OP_breg3:
1934     case DW_OP_breg4:
1935     case DW_OP_breg5:
1936     case DW_OP_breg6:
1937     case DW_OP_breg7:
1938     case DW_OP_breg8:
1939     case DW_OP_breg9:
1940     case DW_OP_breg10:
1941     case DW_OP_breg11:
1942     case DW_OP_breg12:
1943     case DW_OP_breg13:
1944     case DW_OP_breg14:
1945     case DW_OP_breg15:
1946     case DW_OP_breg16:
1947     case DW_OP_breg17:
1948     case DW_OP_breg18:
1949     case DW_OP_breg19:
1950     case DW_OP_breg20:
1951     case DW_OP_breg21:
1952     case DW_OP_breg22:
1953     case DW_OP_breg23:
1954     case DW_OP_breg24:
1955     case DW_OP_breg25:
1956     case DW_OP_breg26:
1957     case DW_OP_breg27:
1958     case DW_OP_breg28:
1959     case DW_OP_breg29:
1960     case DW_OP_breg30:
1961     case DW_OP_breg31:
1962       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1963       break;
1964     case DW_OP_regx:
1965       {
1966         unsigned r = val1->v.val_unsigned;
1967         if (for_eh_or_skip >= 0)
1968           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1969         gcc_assert (size_of_uleb128 (r) 
1970                     == size_of_uleb128 (val1->v.val_unsigned));
1971         dw2_asm_output_data_uleb128 (r, NULL);  
1972       }
1973       break;
1974     case DW_OP_fbreg:
1975       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1976       break;
1977     case DW_OP_bregx:
1978       {
1979         unsigned r = val1->v.val_unsigned;
1980         if (for_eh_or_skip >= 0)
1981           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1982         gcc_assert (size_of_uleb128 (r) 
1983                     == size_of_uleb128 (val1->v.val_unsigned));
1984         dw2_asm_output_data_uleb128 (r, NULL);  
1985         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1986       }
1987       break;
1988     case DW_OP_piece:
1989       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1990       break;
1991     case DW_OP_bit_piece:
1992       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1993       dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1994       break;
1995     case DW_OP_deref_size:
1996     case DW_OP_xderef_size:
1997       dw2_asm_output_data (1, val1->v.val_int, NULL);
1998       break;
1999
2000     case DW_OP_addr:
2001       if (loc->dtprel)
2002         {
2003           if (targetm.asm_out.output_dwarf_dtprel)
2004             {
2005               targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2006                                                    DWARF2_ADDR_SIZE,
2007                                                    val1->v.val_addr);
2008               fputc ('\n', asm_out_file);
2009             }
2010           else
2011             gcc_unreachable ();
2012         }
2013       else
2014         {
2015 #ifdef DWARF2_DEBUGGING_INFO
2016           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2017 #else
2018           gcc_unreachable ();
2019 #endif
2020         }
2021       break;
2022
2023     case DW_OP_GNU_addr_index:
2024     case DW_OP_GNU_const_index:
2025       gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2026       dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2027                                    "(index into .debug_addr)");
2028       break;
2029
2030     case DW_OP_GNU_implicit_pointer:
2031       {
2032         char label[MAX_ARTIFICIAL_LABEL_BYTES
2033                    + HOST_BITS_PER_WIDE_INT / 2 + 2];
2034         gcc_assert (val1->val_class == dw_val_class_die_ref);
2035         get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2036         dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2037         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2038       }
2039       break;
2040
2041     case DW_OP_GNU_entry_value:
2042       dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2043       output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2044       break;
2045
2046     case DW_OP_GNU_const_type:
2047       {
2048         unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2049         gcc_assert (o);
2050         dw2_asm_output_data_uleb128 (o, NULL);
2051         switch (val2->val_class)
2052           {
2053           case dw_val_class_const:
2054             l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2055             dw2_asm_output_data (1, l, NULL);
2056             dw2_asm_output_data (l, val2->v.val_int, NULL);
2057             break;
2058           case dw_val_class_vec:
2059             {
2060               unsigned int elt_size = val2->v.val_vec.elt_size;
2061               unsigned int len = val2->v.val_vec.length;
2062               unsigned int i;
2063               unsigned char *p;
2064
2065               l = len * elt_size;
2066               dw2_asm_output_data (1, l, NULL);
2067               if (elt_size > sizeof (HOST_WIDE_INT))
2068                 {
2069                   elt_size /= 2;
2070                   len *= 2;
2071                 }
2072               for (i = 0, p = val2->v.val_vec.array;
2073                    i < len;
2074                    i++, p += elt_size)
2075                 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2076                                      "fp or vector constant word %u", i);
2077             }
2078             break;
2079           case dw_val_class_const_double:
2080             {
2081               unsigned HOST_WIDE_INT first, second;
2082               l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2083
2084               dw2_asm_output_data (1, 2 * l, NULL);
2085               if (WORDS_BIG_ENDIAN)
2086                 {
2087                   first = val2->v.val_double.high;
2088                   second = val2->v.val_double.low;
2089                 }
2090               else
2091                 {
2092                   first = val2->v.val_double.low;
2093                   second = val2->v.val_double.high;
2094                 }
2095               dw2_asm_output_data (l, first, NULL);
2096               dw2_asm_output_data (l, second, NULL);
2097             }
2098             break;
2099           case dw_val_class_wide_int:
2100             {
2101               int i;
2102               int len = get_full_len (*val2->v.val_wide);
2103               l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2104
2105               dw2_asm_output_data (1, len * l, NULL);
2106               if (WORDS_BIG_ENDIAN)
2107                 for (i = len - 1; i >= 0; --i)
2108                   dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2109               else
2110                 for (i = 0; i < len; ++i)
2111                   dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2112             }
2113             break;
2114           default:
2115             gcc_unreachable ();
2116           }
2117       }
2118       break;
2119     case DW_OP_GNU_regval_type:
2120       {
2121         unsigned r = val1->v.val_unsigned;
2122         unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2123         gcc_assert (o);
2124         if (for_eh_or_skip >= 0)
2125           {
2126             r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2127             gcc_assert (size_of_uleb128 (r)
2128                         == size_of_uleb128 (val1->v.val_unsigned));
2129           }
2130         dw2_asm_output_data_uleb128 (r, NULL);
2131         dw2_asm_output_data_uleb128 (o, NULL);
2132       }
2133       break;
2134     case DW_OP_GNU_deref_type:
2135       {
2136         unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2137         gcc_assert (o);
2138         dw2_asm_output_data (1, val1->v.val_int, NULL);
2139         dw2_asm_output_data_uleb128 (o, NULL);
2140       }
2141       break;
2142     case DW_OP_GNU_convert:
2143     case DW_OP_GNU_reinterpret:
2144       if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2145         dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2146       else
2147         {
2148           unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2149           gcc_assert (o);
2150           dw2_asm_output_data_uleb128 (o, NULL);
2151         }
2152       break;
2153
2154     case DW_OP_GNU_parameter_ref:
2155       {
2156         unsigned long o;
2157         gcc_assert (val1->val_class == dw_val_class_die_ref);
2158         o = get_ref_die_offset (val1->v.val_die_ref.die);
2159         dw2_asm_output_data (4, o, NULL);
2160       }
2161       break;
2162
2163     default:
2164       /* Other codes have no operands.  */
2165       break;
2166     }
2167 }
2168
2169 /* Output a sequence of location operations.  
2170    The for_eh_or_skip parameter controls whether register numbers are
2171    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2172    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2173    info).  This should be suppressed for the cases that have not been converted
2174    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
2175
2176 void
2177 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2178 {
2179   for (; loc != NULL; loc = loc->dw_loc_next)
2180     {
2181       enum dwarf_location_atom opc = loc->dw_loc_opc;
2182       /* Output the opcode.  */
2183       if (for_eh_or_skip >= 0 
2184           && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2185         {
2186           unsigned r = (opc - DW_OP_breg0);
2187           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2188           gcc_assert (r <= 31);
2189           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2190         }
2191       else if (for_eh_or_skip >= 0 
2192                && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2193         {
2194           unsigned r = (opc - DW_OP_reg0);
2195           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2196           gcc_assert (r <= 31);
2197           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2198         }
2199
2200       dw2_asm_output_data (1, opc,
2201                              "%s", dwarf_stack_op_name (opc));
2202
2203       /* Output the operand(s) (if any).  */
2204       output_loc_operands (loc, for_eh_or_skip);
2205     }
2206 }
2207
2208 /* Output location description stack opcode's operands (if any).
2209    The output is single bytes on a line, suitable for .cfi_escape.  */
2210
2211 static void
2212 output_loc_operands_raw (dw_loc_descr_ref loc)
2213 {
2214   dw_val_ref val1 = &loc->dw_loc_oprnd1;
2215   dw_val_ref val2 = &loc->dw_loc_oprnd2;
2216
2217   switch (loc->dw_loc_opc)
2218     {
2219     case DW_OP_addr:
2220     case DW_OP_GNU_addr_index:
2221     case DW_OP_GNU_const_index:
2222     case DW_OP_implicit_value:
2223       /* We cannot output addresses in .cfi_escape, only bytes.  */
2224       gcc_unreachable ();
2225
2226     case DW_OP_const1u:
2227     case DW_OP_const1s:
2228     case DW_OP_pick:
2229     case DW_OP_deref_size:
2230     case DW_OP_xderef_size:
2231       fputc (',', asm_out_file);
2232       dw2_asm_output_data_raw (1, val1->v.val_int);
2233       break;
2234
2235     case DW_OP_const2u:
2236     case DW_OP_const2s:
2237       fputc (',', asm_out_file);
2238       dw2_asm_output_data_raw (2, val1->v.val_int);
2239       break;
2240
2241     case DW_OP_const4u:
2242     case DW_OP_const4s:
2243       fputc (',', asm_out_file);
2244       dw2_asm_output_data_raw (4, val1->v.val_int);
2245       break;
2246
2247     case DW_OP_const8u:
2248     case DW_OP_const8s:
2249       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2250       fputc (',', asm_out_file);
2251       dw2_asm_output_data_raw (8, val1->v.val_int);
2252       break;
2253
2254     case DW_OP_skip:
2255     case DW_OP_bra:
2256       {
2257         int offset;
2258
2259         gcc_assert (val1->val_class == dw_val_class_loc);
2260         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2261
2262         fputc (',', asm_out_file);
2263         dw2_asm_output_data_raw (2, offset);
2264       }
2265       break;
2266
2267     case DW_OP_regx:
2268       {
2269         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2270         gcc_assert (size_of_uleb128 (r) 
2271                     == size_of_uleb128 (val1->v.val_unsigned));
2272         fputc (',', asm_out_file);
2273         dw2_asm_output_data_uleb128_raw (r);
2274       }
2275       break;
2276       
2277     case DW_OP_constu:
2278     case DW_OP_plus_uconst:
2279     case DW_OP_piece:
2280       fputc (',', asm_out_file);
2281       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2282       break;
2283
2284     case DW_OP_bit_piece:
2285       fputc (',', asm_out_file);
2286       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2287       dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2288       break;
2289
2290     case DW_OP_consts:
2291     case DW_OP_breg0:
2292     case DW_OP_breg1:
2293     case DW_OP_breg2:
2294     case DW_OP_breg3:
2295     case DW_OP_breg4:
2296     case DW_OP_breg5:
2297     case DW_OP_breg6:
2298     case DW_OP_breg7:
2299     case DW_OP_breg8:
2300     case DW_OP_breg9:
2301     case DW_OP_breg10:
2302     case DW_OP_breg11:
2303     case DW_OP_breg12:
2304     case DW_OP_breg13:
2305     case DW_OP_breg14:
2306     case DW_OP_breg15:
2307     case DW_OP_breg16:
2308     case DW_OP_breg17:
2309     case DW_OP_breg18:
2310     case DW_OP_breg19:
2311     case DW_OP_breg20:
2312     case DW_OP_breg21:
2313     case DW_OP_breg22:
2314     case DW_OP_breg23:
2315     case DW_OP_breg24:
2316     case DW_OP_breg25:
2317     case DW_OP_breg26:
2318     case DW_OP_breg27:
2319     case DW_OP_breg28:
2320     case DW_OP_breg29:
2321     case DW_OP_breg30:
2322     case DW_OP_breg31:
2323     case DW_OP_fbreg:
2324       fputc (',', asm_out_file);
2325       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2326       break;
2327
2328     case DW_OP_bregx:
2329       {
2330         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2331         gcc_assert (size_of_uleb128 (r) 
2332                     == size_of_uleb128 (val1->v.val_unsigned));
2333         fputc (',', asm_out_file);
2334         dw2_asm_output_data_uleb128_raw (r);
2335         fputc (',', asm_out_file);
2336         dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2337       }
2338       break;
2339
2340     case DW_OP_GNU_implicit_pointer:
2341     case DW_OP_GNU_entry_value:
2342     case DW_OP_GNU_const_type:
2343     case DW_OP_GNU_regval_type:
2344     case DW_OP_GNU_deref_type:
2345     case DW_OP_GNU_convert:
2346     case DW_OP_GNU_reinterpret:
2347     case DW_OP_GNU_parameter_ref:
2348       gcc_unreachable ();
2349       break;
2350
2351     default:
2352       /* Other codes have no operands.  */
2353       break;
2354     }
2355 }
2356
2357 void
2358 output_loc_sequence_raw (dw_loc_descr_ref loc)
2359 {
2360   while (1)
2361     {
2362       enum dwarf_location_atom opc = loc->dw_loc_opc;
2363       /* Output the opcode.  */
2364       if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2365         {
2366           unsigned r = (opc - DW_OP_breg0);
2367           r = DWARF2_FRAME_REG_OUT (r, 1);
2368           gcc_assert (r <= 31);
2369           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2370         }
2371       else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2372         {
2373           unsigned r = (opc - DW_OP_reg0);
2374           r = DWARF2_FRAME_REG_OUT (r, 1);
2375           gcc_assert (r <= 31);
2376           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2377         }
2378       /* Output the opcode.  */
2379       fprintf (asm_out_file, "%#x", opc);
2380       output_loc_operands_raw (loc);
2381
2382       if (!loc->dw_loc_next)
2383         break;
2384       loc = loc->dw_loc_next;
2385
2386       fputc (',', asm_out_file);
2387     }
2388 }
2389
2390 /* This function builds a dwarf location descriptor sequence from a
2391    dw_cfa_location, adding the given OFFSET to the result of the
2392    expression.  */
2393
2394 struct dw_loc_descr_node *
2395 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2396 {
2397   struct dw_loc_descr_node *head, *tmp;
2398
2399   offset += cfa->offset;
2400
2401   if (cfa->indirect)
2402     {
2403       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2404       head->dw_loc_oprnd1.val_class = dw_val_class_const;
2405       head->dw_loc_oprnd1.val_entry = NULL;
2406       tmp = new_loc_descr (DW_OP_deref, 0, 0);
2407       add_loc_descr (&head, tmp);
2408       if (offset != 0)
2409         {
2410           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2411           add_loc_descr (&head, tmp);
2412         }
2413     }
2414   else
2415     head = new_reg_loc_descr (cfa->reg, offset);
2416
2417   return head;
2418 }
2419
2420 /* This function builds a dwarf location descriptor sequence for
2421    the address at OFFSET from the CFA when stack is aligned to
2422    ALIGNMENT byte.  */
2423
2424 struct dw_loc_descr_node *
2425 build_cfa_aligned_loc (dw_cfa_location *cfa,
2426                        HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2427 {
2428   struct dw_loc_descr_node *head;
2429   unsigned int dwarf_fp
2430     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2431
2432   /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
2433   if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2434     {
2435       head = new_reg_loc_descr (dwarf_fp, 0);
2436       add_loc_descr (&head, int_loc_descriptor (alignment));
2437       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2438       loc_descr_plus_const (&head, offset);
2439     }
2440   else
2441     head = new_reg_loc_descr (dwarf_fp, offset);
2442   return head;
2443 }
2444 \f
2445 /* And now, the support for symbolic debugging information.  */
2446
2447 /* .debug_str support.  */
2448
2449 static void dwarf2out_init (const char *);
2450 static void dwarf2out_finish (const char *);
2451 static void dwarf2out_assembly_start (void);
2452 static void dwarf2out_define (unsigned int, const char *);
2453 static void dwarf2out_undef (unsigned int, const char *);
2454 static void dwarf2out_start_source_file (unsigned, const char *);
2455 static void dwarf2out_end_source_file (unsigned);
2456 static void dwarf2out_function_decl (tree);
2457 static void dwarf2out_begin_block (unsigned, unsigned);
2458 static void dwarf2out_end_block (unsigned, unsigned);
2459 static bool dwarf2out_ignore_block (const_tree);
2460 static void dwarf2out_global_decl (tree);
2461 static void dwarf2out_type_decl (tree, int);
2462 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2463 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2464                                                  dw_die_ref);
2465 static void dwarf2out_abstract_function (tree);
2466 static void dwarf2out_var_location (rtx_insn *);
2467 static void dwarf2out_begin_function (tree);
2468 static void dwarf2out_end_function (unsigned int);
2469 static void dwarf2out_set_name (tree, tree);
2470
2471 /* The debug hooks structure.  */
2472
2473 const struct gcc_debug_hooks dwarf2_debug_hooks =
2474 {
2475   dwarf2out_init,
2476   dwarf2out_finish,
2477   dwarf2out_assembly_start,
2478   dwarf2out_define,
2479   dwarf2out_undef,
2480   dwarf2out_start_source_file,
2481   dwarf2out_end_source_file,
2482   dwarf2out_begin_block,
2483   dwarf2out_end_block,
2484   dwarf2out_ignore_block,
2485   dwarf2out_source_line,
2486   dwarf2out_begin_prologue,
2487 #if VMS_DEBUGGING_INFO
2488   dwarf2out_vms_end_prologue,
2489   dwarf2out_vms_begin_epilogue,
2490 #else
2491   debug_nothing_int_charstar,
2492   debug_nothing_int_charstar,
2493 #endif
2494   dwarf2out_end_epilogue,
2495   dwarf2out_begin_function,
2496   dwarf2out_end_function,       /* end_function */
2497   dwarf2out_function_decl,      /* function_decl */
2498   dwarf2out_global_decl,
2499   dwarf2out_type_decl,          /* type_decl */
2500   dwarf2out_imported_module_or_decl,
2501   debug_nothing_tree,           /* deferred_inline_function */
2502   /* The DWARF 2 backend tries to reduce debugging bloat by not
2503      emitting the abstract description of inline functions until
2504      something tries to reference them.  */
2505   dwarf2out_abstract_function,  /* outlining_inline_function */
2506   debug_nothing_rtx_code_label, /* label */
2507   debug_nothing_int,            /* handle_pch */
2508   dwarf2out_var_location,
2509   dwarf2out_switch_text_section,
2510   dwarf2out_set_name,
2511   1,                            /* start_end_main_source_file */
2512   TYPE_SYMTAB_IS_DIE            /* tree_type_symtab_field */
2513 };
2514 \f
2515 /* NOTE: In the comments in this file, many references are made to
2516    "Debugging Information Entries".  This term is abbreviated as `DIE'
2517    throughout the remainder of this file.  */
2518
2519 /* An internal representation of the DWARF output is built, and then
2520    walked to generate the DWARF debugging info.  The walk of the internal
2521    representation is done after the entire program has been compiled.
2522    The types below are used to describe the internal representation.  */
2523
2524 /* Whether to put type DIEs into their own section .debug_types instead
2525    of making them part of the .debug_info section.  Only supported for
2526    Dwarf V4 or higher and the user didn't disable them through
2527    -fno-debug-types-section.  It is more efficient to put them in a
2528    separate comdat sections since the linker will then be able to
2529    remove duplicates.  But not all tools support .debug_types sections
2530    yet.  */
2531
2532 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2533
2534 /* Various DIE's use offsets relative to the beginning of the
2535    .debug_info section to refer to each other.  */
2536
2537 typedef long int dw_offset;
2538
2539 /* Define typedefs here to avoid circular dependencies.  */
2540
2541 typedef struct dw_attr_struct *dw_attr_ref;
2542 typedef struct dw_line_info_struct *dw_line_info_ref;
2543 typedef struct pubname_struct *pubname_ref;
2544 typedef struct dw_ranges_struct *dw_ranges_ref;
2545 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2546 typedef struct comdat_type_struct *comdat_type_node_ref;
2547
2548 /* The entries in the line_info table more-or-less mirror the opcodes
2549    that are used in the real dwarf line table.  Arrays of these entries
2550    are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2551    supported.  */
2552
2553 enum dw_line_info_opcode {
2554   /* Emit DW_LNE_set_address; the operand is the label index.  */
2555   LI_set_address,
2556
2557   /* Emit a row to the matrix with the given line.  This may be done
2558      via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2559      special opcodes.  */
2560   LI_set_line,
2561
2562   /* Emit a DW_LNS_set_file.  */
2563   LI_set_file,
2564
2565   /* Emit a DW_LNS_set_column.  */
2566   LI_set_column,
2567
2568   /* Emit a DW_LNS_negate_stmt; the operand is ignored.  */
2569   LI_negate_stmt,
2570
2571   /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored.  */
2572   LI_set_prologue_end,
2573   LI_set_epilogue_begin,
2574
2575   /* Emit a DW_LNE_set_discriminator.  */
2576   LI_set_discriminator
2577 };
2578
2579 typedef struct GTY(()) dw_line_info_struct {
2580   enum dw_line_info_opcode opcode;
2581   unsigned int val;
2582 } dw_line_info_entry;
2583
2584
2585 typedef struct GTY(()) dw_line_info_table_struct {
2586   /* The label that marks the end of this section.  */
2587   const char *end_label;
2588
2589   /* The values for the last row of the matrix, as collected in the table.
2590      These are used to minimize the changes to the next row.  */
2591   unsigned int file_num;
2592   unsigned int line_num;
2593   unsigned int column_num;
2594   int discrim_num;
2595   bool is_stmt;
2596   bool in_use;
2597
2598   vec<dw_line_info_entry, va_gc> *entries;
2599 } dw_line_info_table;
2600
2601 typedef dw_line_info_table *dw_line_info_table_p;
2602
2603
2604 /* Each DIE attribute has a field specifying the attribute kind,
2605    a link to the next attribute in the chain, and an attribute value.
2606    Attributes are typically linked below the DIE they modify.  */
2607
2608 typedef struct GTY(()) dw_attr_struct {
2609   enum dwarf_attribute dw_attr;
2610   dw_val_node dw_attr_val;
2611 }
2612 dw_attr_node;
2613
2614
2615 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
2616    The children of each node form a circular list linked by
2617    die_sib.  die_child points to the node *before* the "first" child node.  */
2618
2619 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
2620   union die_symbol_or_type_node
2621     {
2622       const char * GTY ((tag ("0"))) die_symbol;
2623       comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2624     }
2625   GTY ((desc ("%0.comdat_type_p"))) die_id;
2626   vec<dw_attr_node, va_gc> *die_attr;
2627   dw_die_ref die_parent;
2628   dw_die_ref die_child;
2629   dw_die_ref die_sib;
2630   dw_die_ref die_definition; /* ref from a specification to its definition */
2631   dw_offset die_offset;
2632   unsigned long die_abbrev;
2633   int die_mark;
2634   unsigned int decl_id;
2635   enum dwarf_tag die_tag;
2636   /* Die is used and must not be pruned as unused.  */
2637   BOOL_BITFIELD die_perennial_p : 1;
2638   BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2639   /* Lots of spare bits.  */
2640 }
2641 die_node;
2642
2643 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
2644 #define FOR_EACH_CHILD(die, c, expr) do {       \
2645   c = die->die_child;                           \
2646   if (c) do {                                   \
2647     c = c->die_sib;                             \
2648     expr;                                       \
2649   } while (c != die->die_child);                \
2650 } while (0)
2651
2652 /* The pubname structure */
2653
2654 typedef struct GTY(()) pubname_struct {
2655   dw_die_ref die;
2656   const char *name;
2657 }
2658 pubname_entry;
2659
2660
2661 struct GTY(()) dw_ranges_struct {
2662   /* If this is positive, it's a block number, otherwise it's a
2663      bitwise-negated index into dw_ranges_by_label.  */
2664   int num;
2665 };
2666
2667 /* A structure to hold a macinfo entry.  */
2668
2669 typedef struct GTY(()) macinfo_struct {
2670   unsigned char code;
2671   unsigned HOST_WIDE_INT lineno;
2672   const char *info;
2673 }
2674 macinfo_entry;
2675
2676
2677 struct GTY(()) dw_ranges_by_label_struct {
2678   const char *begin;
2679   const char *end;
2680 };
2681
2682 /* The comdat type node structure.  */
2683 typedef struct GTY(()) comdat_type_struct
2684 {
2685   dw_die_ref root_die;
2686   dw_die_ref type_die;
2687   dw_die_ref skeleton_die;
2688   char signature[DWARF_TYPE_SIGNATURE_SIZE];
2689   struct comdat_type_struct *next;
2690 }
2691 comdat_type_node;
2692
2693 /* The limbo die list structure.  */
2694 typedef struct GTY(()) limbo_die_struct {
2695   dw_die_ref die;
2696   tree created_for;
2697   struct limbo_die_struct *next;
2698 }
2699 limbo_die_node;
2700
2701 typedef struct skeleton_chain_struct
2702 {
2703   dw_die_ref old_die;
2704   dw_die_ref new_die;
2705   struct skeleton_chain_struct *parent;
2706 }
2707 skeleton_chain_node;
2708
2709 /* Define a macro which returns nonzero for a TYPE_DECL which was
2710    implicitly generated for a type.
2711
2712    Note that, unlike the C front-end (which generates a NULL named
2713    TYPE_DECL node for each complete tagged type, each array type,
2714    and each function type node created) the C++ front-end generates
2715    a _named_ TYPE_DECL node for each tagged type node created.
2716    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2717    generate a DW_TAG_typedef DIE for them.  Likewise with the Ada
2718    front-end, but for each type, tagged or not.  */
2719
2720 #define TYPE_DECL_IS_STUB(decl)                         \
2721   (DECL_NAME (decl) == NULL_TREE                        \
2722    || (DECL_ARTIFICIAL (decl)                           \
2723        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
2724            /* This is necessary for stub decls that     \
2725               appear in nested inline functions.  */    \
2726            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2727                && (decl_ultimate_origin (decl)          \
2728                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2729
2730 /* Information concerning the compilation unit's programming
2731    language, and compiler version.  */
2732
2733 /* Fixed size portion of the DWARF compilation unit header.  */
2734 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2735   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2736
2737 /* Fixed size portion of the DWARF comdat type unit header.  */
2738 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2739   (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2740    + DWARF_OFFSET_SIZE)
2741
2742 /* Fixed size portion of public names info.  */
2743 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2744
2745 /* Fixed size portion of the address range info.  */
2746 #define DWARF_ARANGES_HEADER_SIZE                                       \
2747   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
2748                 DWARF2_ADDR_SIZE * 2)                                   \
2749    - DWARF_INITIAL_LENGTH_SIZE)
2750
2751 /* Size of padding portion in the address range info.  It must be
2752    aligned to twice the pointer size.  */
2753 #define DWARF_ARANGES_PAD_SIZE \
2754   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2755                 DWARF2_ADDR_SIZE * 2)                              \
2756    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2757
2758 /* Use assembler line directives if available.  */
2759 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2760 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2761 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2762 #else
2763 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2764 #endif
2765 #endif
2766
2767 /* Minimum line offset in a special line info. opcode.
2768    This value was chosen to give a reasonable range of values.  */
2769 #define DWARF_LINE_BASE  -10
2770
2771 /* First special line opcode - leave room for the standard opcodes.  */
2772 #define DWARF_LINE_OPCODE_BASE  ((int)DW_LNS_set_isa + 1)
2773
2774 /* Range of line offsets in a special line info. opcode.  */
2775 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
2776
2777 /* Flag that indicates the initial value of the is_stmt_start flag.
2778    In the present implementation, we do not mark any lines as
2779    the beginning of a source statement, because that information
2780    is not made available by the GCC front-end.  */
2781 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2782
2783 /* Maximum number of operations per instruction bundle.  */
2784 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2785 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2786 #endif
2787
2788 /* This location is used by calc_die_sizes() to keep track
2789    the offset of each DIE within the .debug_info section.  */
2790 static unsigned long next_die_offset;
2791
2792 /* Record the root of the DIE's built for the current compilation unit.  */
2793 static GTY(()) dw_die_ref single_comp_unit_die;
2794
2795 /* A list of type DIEs that have been separated into comdat sections.  */
2796 static GTY(()) comdat_type_node *comdat_type_list;
2797
2798 /* A list of DIEs with a NULL parent waiting to be relocated.  */
2799 static GTY(()) limbo_die_node *limbo_die_list;
2800
2801 /* A list of DIEs for which we may have to generate
2802    DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set.  */
2803 static GTY(()) limbo_die_node *deferred_asm_name;
2804
2805 struct dwarf_file_hasher : ggc_hasher<dwarf_file_data *>
2806 {
2807   typedef const char *compare_type;
2808
2809   static hashval_t hash (dwarf_file_data *);
2810   static bool equal (dwarf_file_data *, const char *);
2811 };
2812
2813 /* Filenames referenced by this compilation unit.  */
2814 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
2815
2816 struct decl_die_hasher : ggc_hasher<die_node *>
2817 {
2818   typedef tree compare_type;
2819
2820   static hashval_t hash (die_node *);
2821   static bool equal (die_node *, tree);
2822 };
2823 /* A hash table of references to DIE's that describe declarations.
2824    The key is a DECL_UID() which is a unique number identifying each decl.  */
2825 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
2826
2827 struct block_die_hasher : ggc_hasher<die_struct *>
2828 {
2829   static hashval_t hash (die_struct *);
2830   static bool equal (die_struct *, die_struct *);
2831 };
2832
2833 /* A hash table of references to DIE's that describe COMMON blocks.
2834    The key is DECL_UID() ^ die_parent.  */
2835 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
2836
2837 typedef struct GTY(()) die_arg_entry_struct {
2838     dw_die_ref die;
2839     tree arg;
2840 } die_arg_entry;
2841
2842
2843 /* Node of the variable location list.  */
2844 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2845   /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2846      EXPR_LIST chain.  For small bitsizes, bitsize is encoded
2847      in mode of the EXPR_LIST node and first EXPR_LIST operand
2848      is either NOTE_INSN_VAR_LOCATION for a piece with a known
2849      location or NULL for padding.  For larger bitsizes,
2850      mode is 0 and first operand is a CONCAT with bitsize
2851      as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2852      NULL as second operand.  */
2853   rtx GTY (()) loc;
2854   const char * GTY (()) label;
2855   struct var_loc_node * GTY (()) next;
2856 };
2857
2858 /* Variable location list.  */
2859 struct GTY ((for_user)) var_loc_list_def {
2860   struct var_loc_node * GTY (()) first;
2861
2862   /* Pointer to the last but one or last element of the
2863      chained list.  If the list is empty, both first and
2864      last are NULL, if the list contains just one node
2865      or the last node certainly is not redundant, it points
2866      to the last node, otherwise points to the last but one.
2867      Do not mark it for GC because it is marked through the chain.  */
2868   struct var_loc_node * GTY ((skip ("%h"))) last;
2869
2870   /* Pointer to the last element before section switch,
2871      if NULL, either sections weren't switched or first
2872      is after section switch.  */
2873   struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2874
2875   /* DECL_UID of the variable decl.  */
2876   unsigned int decl_id;
2877 };
2878 typedef struct var_loc_list_def var_loc_list;
2879
2880 /* Call argument location list.  */
2881 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2882   rtx GTY (()) call_arg_loc_note;
2883   const char * GTY (()) label;
2884   tree GTY (()) block;
2885   bool tail_call_p;
2886   rtx GTY (()) symbol_ref;
2887   struct call_arg_loc_node * GTY (()) next;
2888 };
2889
2890
2891 struct decl_loc_hasher : ggc_hasher<var_loc_list *>
2892 {
2893   typedef const_tree compare_type;
2894
2895   static hashval_t hash (var_loc_list *);
2896   static bool equal (var_loc_list *, const_tree);
2897 };
2898
2899 /* Table of decl location linked lists.  */
2900 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
2901
2902 /* Head and tail of call_arg_loc chain.  */
2903 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2904 static struct call_arg_loc_node *call_arg_loc_last;
2905
2906 /* Number of call sites in the current function.  */
2907 static int call_site_count = -1;
2908 /* Number of tail call sites in the current function.  */
2909 static int tail_call_site_count = -1;
2910
2911 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
2912    DIEs.  */
2913 static vec<dw_die_ref> block_map;
2914
2915 /* A cached location list.  */
2916 struct GTY ((for_user)) cached_dw_loc_list_def {
2917   /* The DECL_UID of the decl that this entry describes.  */
2918   unsigned int decl_id;
2919
2920   /* The cached location list.  */
2921   dw_loc_list_ref loc_list;
2922 };
2923 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2924
2925 struct dw_loc_list_hasher : ggc_hasher<cached_dw_loc_list *>
2926 {
2927
2928   typedef const_tree compare_type;
2929   
2930   static hashval_t hash (cached_dw_loc_list *);
2931   static bool equal (cached_dw_loc_list *, const_tree);
2932 };
2933
2934 /* Table of cached location lists.  */
2935 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
2936
2937 /* A pointer to the base of a list of references to DIE's that
2938    are uniquely identified by their tag, presence/absence of
2939    children DIE's, and list of attribute/value pairs.  */
2940 static GTY((length ("abbrev_die_table_allocated")))
2941   dw_die_ref *abbrev_die_table;
2942
2943 /* Number of elements currently allocated for abbrev_die_table.  */
2944 static GTY(()) unsigned abbrev_die_table_allocated;
2945
2946 /* Number of elements in type_die_table currently in use.  */
2947 static GTY(()) unsigned abbrev_die_table_in_use;
2948
2949 /* Size (in elements) of increments by which we may expand the
2950    abbrev_die_table.  */
2951 #define ABBREV_DIE_TABLE_INCREMENT 256
2952
2953 /* A global counter for generating labels for line number data.  */
2954 static unsigned int line_info_label_num;
2955
2956 /* The current table to which we should emit line number information
2957    for the current function.  This will be set up at the beginning of
2958    assembly for the function.  */
2959 static dw_line_info_table *cur_line_info_table;
2960
2961 /* The two default tables of line number info.  */
2962 static GTY(()) dw_line_info_table *text_section_line_info;
2963 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2964
2965 /* The set of all non-default tables of line number info.  */
2966 static GTY(()) vec<dw_line_info_table_p, va_gc> *separate_line_info;
2967
2968 /* A flag to tell pubnames/types export if there is an info section to
2969    refer to.  */
2970 static bool info_section_emitted;
2971
2972 /* A pointer to the base of a table that contains a list of publicly
2973    accessible names.  */
2974 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
2975
2976 /* A pointer to the base of a table that contains a list of publicly
2977    accessible types.  */
2978 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
2979
2980 /* A pointer to the base of a table that contains a list of macro
2981    defines/undefines (and file start/end markers).  */
2982 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
2983
2984 /* True if .debug_macinfo or .debug_macros section is going to be
2985    emitted.  */
2986 #define have_macinfo \
2987   (debug_info_level >= DINFO_LEVEL_VERBOSE \
2988    && !macinfo_table->is_empty ())
2989
2990 /* Array of dies for which we should generate .debug_ranges info.  */
2991 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
2992
2993 /* Number of elements currently allocated for ranges_table.  */
2994 static GTY(()) unsigned ranges_table_allocated;
2995
2996 /* Number of elements in ranges_table currently in use.  */
2997 static GTY(()) unsigned ranges_table_in_use;
2998
2999 /* Array of pairs of labels referenced in ranges_table.  */
3000 static GTY ((length ("ranges_by_label_allocated")))
3001      dw_ranges_by_label_ref ranges_by_label;
3002
3003 /* Number of elements currently allocated for ranges_by_label.  */
3004 static GTY(()) unsigned ranges_by_label_allocated;
3005
3006 /* Number of elements in ranges_by_label currently in use.  */
3007 static GTY(()) unsigned ranges_by_label_in_use;
3008
3009 /* Size (in elements) of increments by which we may expand the
3010    ranges_table.  */
3011 #define RANGES_TABLE_INCREMENT 64
3012
3013 /* Whether we have location lists that need outputting */
3014 static GTY(()) bool have_location_lists;
3015
3016 /* Unique label counter.  */
3017 static GTY(()) unsigned int loclabel_num;
3018
3019 /* Unique label counter for point-of-call tables.  */
3020 static GTY(()) unsigned int poc_label_num;
3021
3022 /* The last file entry emitted by maybe_emit_file().  */
3023 static GTY(()) struct dwarf_file_data * last_emitted_file;
3024
3025 /* Number of internal labels generated by gen_internal_sym().  */
3026 static GTY(()) int label_num;
3027
3028 /* Cached result of previous call to lookup_filename.  */
3029 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
3030
3031 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3032
3033 /* Instances of generic types for which we need to generate debug
3034    info that describe their generic parameters and arguments. That
3035    generation needs to happen once all types are properly laid out so
3036    we do it at the end of compilation.  */
3037 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3038
3039 /* Offset from the "steady-state frame pointer" to the frame base,
3040    within the current function.  */
3041 static HOST_WIDE_INT frame_pointer_fb_offset;
3042 static bool frame_pointer_fb_offset_valid;
3043
3044 static vec<dw_die_ref> base_types;
3045
3046 /* Flags to represent a set of attribute classes for attributes that represent
3047    a scalar value (bounds, pointers, ...).  */
3048 enum dw_scalar_form
3049 {
3050   dw_scalar_form_constant = 0x01,
3051   dw_scalar_form_exprloc = 0x02,
3052   dw_scalar_form_reference = 0x04
3053 };
3054
3055 /* Forward declarations for functions defined in this file.  */
3056
3057 static int is_pseudo_reg (const_rtx);
3058 static tree type_main_variant (tree);
3059 static int is_tagged_type (const_tree);
3060 static const char *dwarf_tag_name (unsigned);
3061 static const char *dwarf_attr_name (unsigned);
3062 static const char *dwarf_form_name (unsigned);
3063 static tree decl_ultimate_origin (const_tree);
3064 static tree decl_class_context (tree);
3065 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
3066 static inline enum dw_val_class AT_class (dw_attr_ref);
3067 static inline unsigned int AT_index (dw_attr_ref);
3068 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3069 static inline unsigned AT_flag (dw_attr_ref);
3070 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3071 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
3072 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3073 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
3074 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3075                            HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3076 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3077                                unsigned int, unsigned char *);
3078 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3079 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3080 static inline const char *AT_string (dw_attr_ref);
3081 static enum dwarf_form AT_string_form (dw_attr_ref);
3082 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3083 static void add_AT_specification (dw_die_ref, dw_die_ref);
3084 static inline dw_die_ref AT_ref (dw_attr_ref);
3085 static inline int AT_ref_external (dw_attr_ref);
3086 static inline void set_AT_ref_external (dw_attr_ref, int);
3087 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3088 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3089 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
3090 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3091                              dw_loc_list_ref);
3092 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
3093 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3094 static void remove_addr_table_entry (addr_table_entry *);
3095 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3096 static inline rtx AT_addr (dw_attr_ref);
3097 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3098 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3099 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3100 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3101                            unsigned HOST_WIDE_INT);
3102 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3103                                unsigned long, bool);
3104 static inline const char *AT_lbl (dw_attr_ref);
3105 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
3106 static const char *get_AT_low_pc (dw_die_ref);
3107 static const char *get_AT_hi_pc (dw_die_ref);
3108 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3109 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3110 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3111 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3112 static bool is_cxx (void);
3113 static bool is_fortran (void);
3114 static bool is_ada (void);
3115 static void remove_AT (dw_die_ref, enum dwarf_attribute);
3116 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3117 static void add_child_die (dw_die_ref, dw_die_ref);
3118 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3119 static dw_die_ref lookup_type_die (tree);
3120 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3121 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3122 static void equate_type_number_to_die (tree, dw_die_ref);
3123 static dw_die_ref lookup_decl_die (tree);
3124 static var_loc_list *lookup_decl_loc (const_tree);
3125 static void equate_decl_number_to_die (tree, dw_die_ref);
3126 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3127 static void print_spaces (FILE *);
3128 static void print_die (dw_die_ref, FILE *);
3129 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3130 static dw_die_ref pop_compile_unit (dw_die_ref);
3131 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3132 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3133 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3134 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3135 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3136 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3137 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
3138                                    struct md5_ctx *, int *);
3139 struct checksum_attributes;
3140 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3141 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3142 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3143 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3144 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3145 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3146 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3147 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3148 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3149 static void compute_section_prefix (dw_die_ref);
3150 static int is_type_die (dw_die_ref);
3151 static int is_comdat_die (dw_die_ref);
3152 static int is_symbol_die (dw_die_ref);
3153 static inline bool is_template_instantiation (dw_die_ref);
3154 static void assign_symbol_names (dw_die_ref);
3155 static void break_out_includes (dw_die_ref);
3156 static int is_declaration_die (dw_die_ref);
3157 static int should_move_die_to_comdat (dw_die_ref);
3158 static dw_die_ref clone_as_declaration (dw_die_ref);
3159 static dw_die_ref clone_die (dw_die_ref);
3160 static dw_die_ref clone_tree (dw_die_ref);
3161 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3162 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3163 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3164 static dw_die_ref generate_skeleton (dw_die_ref);
3165 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3166                                                          dw_die_ref,
3167                                                          dw_die_ref);
3168 static void break_out_comdat_types (dw_die_ref);
3169 static void copy_decls_for_unworthy_types (dw_die_ref);
3170
3171 static void add_sibling_attributes (dw_die_ref);
3172 static void output_location_lists (dw_die_ref);
3173 static int constant_size (unsigned HOST_WIDE_INT);
3174 static unsigned long size_of_die (dw_die_ref);
3175 static void calc_die_sizes (dw_die_ref);
3176 static void calc_base_type_die_sizes (void);
3177 static void mark_dies (dw_die_ref);
3178 static void unmark_dies (dw_die_ref);
3179 static void unmark_all_dies (dw_die_ref);
3180 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3181 static unsigned long size_of_aranges (void);
3182 static enum dwarf_form value_format (dw_attr_ref);
3183 static void output_value_format (dw_attr_ref);
3184 static void output_abbrev_section (void);
3185 static void output_die_abbrevs (unsigned long, dw_die_ref);
3186 static void output_die_symbol (dw_die_ref);
3187 static void output_die (dw_die_ref);
3188 static void output_compilation_unit_header (void);
3189 static void output_comp_unit (dw_die_ref, int);
3190 static void output_comdat_type_unit (comdat_type_node *);
3191 static const char *dwarf2_name (tree, int);
3192 static void add_pubname (tree, dw_die_ref);
3193 static void add_enumerator_pubname (const char *, dw_die_ref);
3194 static void add_pubname_string (const char *, dw_die_ref);
3195 static void add_pubtype (tree, dw_die_ref);
3196 static void output_pubnames (vec<pubname_entry, va_gc> *);
3197 static void output_aranges (unsigned long);
3198 static unsigned int add_ranges_num (int);
3199 static unsigned int add_ranges (const_tree);
3200 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3201                                   bool *, bool);
3202 static void output_ranges (void);
3203 static dw_line_info_table *new_line_info_table (void);
3204 static void output_line_info (bool);
3205 static void output_file_names (void);
3206 static dw_die_ref base_type_die (tree);
3207 static int is_base_type (tree);
3208 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3209 static int decl_quals (const_tree);
3210 static dw_die_ref modified_type_die (tree, int, dw_die_ref);
3211 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3212 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3213 static int type_is_enum (const_tree);
3214 static unsigned int dbx_reg_number (const_rtx);
3215 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3216 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3217 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3218                                                 enum var_init_status);
3219 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3220                                                      enum var_init_status);
3221 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3222                                          enum var_init_status);
3223 static int is_based_loc (const_rtx);
3224 static bool resolve_one_addr (rtx *);
3225 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3226                                                enum var_init_status);
3227 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3228                                         enum var_init_status);
3229 struct loc_descr_context;
3230 static dw_loc_list_ref loc_list_from_tree (tree, int,
3231                                            const struct loc_descr_context *);
3232 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3233                                                   const struct loc_descr_context *);
3234 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3235 static tree field_type (const_tree);
3236 static unsigned int simple_type_align_in_bits (const_tree);
3237 static unsigned int simple_decl_align_in_bits (const_tree);
3238 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3239 static HOST_WIDE_INT field_byte_offset (const_tree);
3240 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3241                                          dw_loc_list_ref);
3242 static void add_data_member_location_attribute (dw_die_ref, tree);
3243 static bool add_const_value_attribute (dw_die_ref, rtx);
3244 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3245 static void insert_wide_int (const wide_int &, unsigned char *, int);
3246 static void insert_float (const_rtx, unsigned char *);
3247 static rtx rtl_for_decl_location (tree);
3248 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3249                                                    enum dwarf_attribute);
3250 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3251 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3252 static void add_name_attribute (dw_die_ref, const char *);
3253 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3254 static void add_comp_dir_attribute (dw_die_ref);
3255 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3256                              const struct loc_descr_context *);
3257 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3258                             const struct loc_descr_context *);
3259 static void add_subscript_info (dw_die_ref, tree, bool);
3260 static void add_byte_size_attribute (dw_die_ref, tree);
3261 static void add_bit_offset_attribute (dw_die_ref, tree);
3262 static void add_bit_size_attribute (dw_die_ref, tree);
3263 static void add_prototyped_attribute (dw_die_ref, tree);
3264 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3265 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3266 static void add_src_coords_attributes (dw_die_ref, tree);
3267 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3268 static void push_decl_scope (tree);
3269 static void pop_decl_scope (void);
3270 static dw_die_ref scope_die_for (tree, dw_die_ref);
3271 static inline int local_scope_p (dw_die_ref);
3272 static inline int class_scope_p (dw_die_ref);
3273 static inline int class_or_namespace_scope_p (dw_die_ref);
3274 static void add_type_attribute (dw_die_ref, tree, int, dw_die_ref);
3275 static void add_calling_convention_attribute (dw_die_ref, tree);
3276 static const char *type_tag (const_tree);
3277 static tree member_declared_type (const_tree);
3278 #if 0
3279 static const char *decl_start_label (tree);
3280 #endif
3281 static void gen_array_type_die (tree, dw_die_ref);
3282 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3283 #if 0
3284 static void gen_entry_point_die (tree, dw_die_ref);
3285 #endif
3286 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3287 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3288 static dw_die_ref gen_formal_parameter_pack_die  (tree, tree, dw_die_ref, tree*);
3289 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3290 static void gen_formal_types_die (tree, dw_die_ref);
3291 static void gen_subprogram_die (tree, dw_die_ref);
3292 static void gen_variable_die (tree, tree, dw_die_ref);
3293 static void gen_const_die (tree, dw_die_ref);
3294 static void gen_label_die (tree, dw_die_ref);
3295 static void gen_lexical_block_die (tree, dw_die_ref);
3296 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3297 static void gen_field_die (tree, dw_die_ref);
3298 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3299 static dw_die_ref gen_compile_unit_die (const char *);
3300 static void gen_inheritance_die (tree, tree, dw_die_ref);
3301 static void gen_member_die (tree, dw_die_ref);
3302 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3303                                                 enum debug_info_usage);
3304 static void gen_subroutine_type_die (tree, dw_die_ref);
3305 static void gen_typedef_die (tree, dw_die_ref);
3306 static void gen_type_die (tree, dw_die_ref);
3307 static void gen_block_die (tree, dw_die_ref);
3308 static void decls_for_scope (tree, dw_die_ref);
3309 static inline int is_redundant_typedef (const_tree);
3310 static bool is_naming_typedef_decl (const_tree);
3311 static inline dw_die_ref get_context_die (tree);
3312 static void gen_namespace_die (tree, dw_die_ref);
3313 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3314 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3315 static dw_die_ref force_decl_die (tree);
3316 static dw_die_ref force_type_die (tree);
3317 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3318 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3319 static struct dwarf_file_data * lookup_filename (const char *);
3320 static void retry_incomplete_types (void);
3321 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3322 static void gen_generic_params_dies (tree);
3323 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3324 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3325 static void splice_child_die (dw_die_ref, dw_die_ref);
3326 static int file_info_cmp (const void *, const void *);
3327 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3328                                      const char *, const char *);
3329 static void output_loc_list (dw_loc_list_ref);
3330 static char *gen_internal_sym (const char *);
3331 static bool want_pubnames (void);
3332
3333 static void prune_unmark_dies (dw_die_ref);
3334 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3335 static void prune_unused_types_mark (dw_die_ref, int);
3336 static void prune_unused_types_walk (dw_die_ref);
3337 static void prune_unused_types_walk_attribs (dw_die_ref);
3338 static void prune_unused_types_prune (dw_die_ref);
3339 static void prune_unused_types (void);
3340 static int maybe_emit_file (struct dwarf_file_data *fd);
3341 static inline const char *AT_vms_delta1 (dw_attr_ref);
3342 static inline const char *AT_vms_delta2 (dw_attr_ref);
3343 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3344                                      const char *, const char *);
3345 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3346 static void gen_remaining_tmpl_value_param_die_attribute (void);
3347 static bool generic_type_p (tree);
3348 static void schedule_generic_params_dies_gen (tree t);
3349 static void gen_scheduled_generic_parms_dies (void);
3350
3351 static const char *comp_dir_string (void);
3352
3353 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3354
3355 /* enum for tracking thread-local variables whose address is really an offset
3356    relative to the TLS pointer, which will need link-time relocation, but will
3357    not need relocation by the DWARF consumer.  */
3358
3359 enum dtprel_bool
3360 {
3361   dtprel_false = 0,
3362   dtprel_true = 1
3363 };
3364
3365 /* Return the operator to use for an address of a variable.  For dtprel_true, we
3366    use DW_OP_const*.  For regular variables, which need both link-time
3367    relocation and consumer-level relocation (e.g., to account for shared objects
3368    loaded at a random address), we use DW_OP_addr*.  */
3369
3370 static inline enum dwarf_location_atom
3371 dw_addr_op (enum dtprel_bool dtprel)
3372 {
3373   if (dtprel == dtprel_true)
3374     return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3375             : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3376   else
3377     return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3378 }
3379
3380 /* Return a pointer to a newly allocated address location description.  If
3381    dwarf_split_debug_info is true, then record the address with the appropriate
3382    relocation.  */
3383 static inline dw_loc_descr_ref
3384 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3385 {
3386   dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3387
3388   ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3389   ref->dw_loc_oprnd1.v.val_addr = addr;
3390   ref->dtprel = dtprel;
3391   if (dwarf_split_debug_info)
3392     ref->dw_loc_oprnd1.val_entry
3393         = add_addr_table_entry (addr,
3394                                 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3395   else
3396     ref->dw_loc_oprnd1.val_entry = NULL;
3397
3398   return ref;
3399 }
3400
3401 /* Section names used to hold DWARF debugging information.  */
3402
3403 #ifndef DEBUG_INFO_SECTION
3404 #define DEBUG_INFO_SECTION      ".debug_info"
3405 #endif
3406 #ifndef DEBUG_DWO_INFO_SECTION
3407 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3408 #endif
3409 #ifndef DEBUG_ABBREV_SECTION
3410 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
3411 #endif
3412 #ifndef DEBUG_DWO_ABBREV_SECTION
3413 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3414 #endif
3415 #ifndef DEBUG_ARANGES_SECTION
3416 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
3417 #endif
3418 #ifndef DEBUG_ADDR_SECTION
3419 #define DEBUG_ADDR_SECTION     ".debug_addr"
3420 #endif
3421 #ifndef DEBUG_NORM_MACINFO_SECTION
3422 #define DEBUG_NORM_MACINFO_SECTION     ".debug_macinfo"
3423 #endif
3424 #ifndef DEBUG_DWO_MACINFO_SECTION
3425 #define DEBUG_DWO_MACINFO_SECTION      ".debug_macinfo.dwo"
3426 #endif
3427 #ifndef DEBUG_MACINFO_SECTION
3428 #define DEBUG_MACINFO_SECTION                                           \
3429   (!dwarf_split_debug_info                                              \
3430    ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3431 #endif
3432 #ifndef DEBUG_NORM_MACRO_SECTION
3433 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3434 #endif
3435 #ifndef DEBUG_DWO_MACRO_SECTION
3436 #define DEBUG_DWO_MACRO_SECTION        ".debug_macro.dwo"
3437 #endif
3438 #ifndef DEBUG_MACRO_SECTION
3439 #define DEBUG_MACRO_SECTION                                             \
3440   (!dwarf_split_debug_info                                              \
3441    ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3442 #endif
3443 #ifndef DEBUG_LINE_SECTION
3444 #define DEBUG_LINE_SECTION      ".debug_line"
3445 #endif
3446 #ifndef DEBUG_DWO_LINE_SECTION
3447 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3448 #endif
3449 #ifndef DEBUG_LOC_SECTION
3450 #define DEBUG_LOC_SECTION       ".debug_loc"
3451 #endif
3452 #ifndef DEBUG_DWO_LOC_SECTION
3453 #define DEBUG_DWO_LOC_SECTION  ".debug_loc.dwo"
3454 #endif
3455 #ifndef DEBUG_PUBNAMES_SECTION
3456 #define DEBUG_PUBNAMES_SECTION  \
3457   ((debug_generate_pub_sections == 2) \
3458    ? ".debug_gnu_pubnames" : ".debug_pubnames")
3459 #endif
3460 #ifndef DEBUG_PUBTYPES_SECTION
3461 #define DEBUG_PUBTYPES_SECTION  \
3462   ((debug_generate_pub_sections == 2) \
3463    ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3464 #endif
3465 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3466 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3467 #ifndef DEBUG_STR_OFFSETS_SECTION
3468 #define DEBUG_STR_OFFSETS_SECTION                                       \
3469   (!dwarf_split_debug_info                                              \
3470    ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3471 #endif
3472 #ifndef DEBUG_STR_DWO_SECTION
3473 #define DEBUG_STR_DWO_SECTION   ".debug_str.dwo"
3474 #endif
3475 #ifndef DEBUG_STR_SECTION
3476 #define DEBUG_STR_SECTION  ".debug_str"
3477 #endif
3478 #ifndef DEBUG_RANGES_SECTION
3479 #define DEBUG_RANGES_SECTION    ".debug_ranges"
3480 #endif
3481
3482 /* Standard ELF section names for compiled code and data.  */
3483 #ifndef TEXT_SECTION_NAME
3484 #define TEXT_SECTION_NAME       ".text"
3485 #endif
3486
3487 /* Section flags for .debug_macinfo/.debug_macro section.  */
3488 #define DEBUG_MACRO_SECTION_FLAGS                                       \
3489   (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3490
3491 /* Section flags for .debug_str section.  */
3492 #define DEBUG_STR_SECTION_FLAGS                                 \
3493   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
3494    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
3495    : SECTION_DEBUG)
3496
3497 /* Section flags for .debug_str.dwo section.  */
3498 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3499
3500 /* Labels we insert at beginning sections we can reference instead of
3501    the section names themselves.  */
3502
3503 #ifndef TEXT_SECTION_LABEL
3504 #define TEXT_SECTION_LABEL                 "Ltext"
3505 #endif
3506 #ifndef COLD_TEXT_SECTION_LABEL
3507 #define COLD_TEXT_SECTION_LABEL             "Ltext_cold"
3508 #endif
3509 #ifndef DEBUG_LINE_SECTION_LABEL
3510 #define DEBUG_LINE_SECTION_LABEL           "Ldebug_line"
3511 #endif
3512 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3513 #define DEBUG_SKELETON_LINE_SECTION_LABEL   "Lskeleton_debug_line"
3514 #endif
3515 #ifndef DEBUG_INFO_SECTION_LABEL
3516 #define DEBUG_INFO_SECTION_LABEL           "Ldebug_info"
3517 #endif
3518 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3519 #define DEBUG_SKELETON_INFO_SECTION_LABEL   "Lskeleton_debug_info"
3520 #endif
3521 #ifndef DEBUG_ABBREV_SECTION_LABEL
3522 #define DEBUG_ABBREV_SECTION_LABEL         "Ldebug_abbrev"
3523 #endif
3524 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3525 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3526 #endif
3527 #ifndef DEBUG_ADDR_SECTION_LABEL
3528 #define DEBUG_ADDR_SECTION_LABEL           "Ldebug_addr"
3529 #endif
3530 #ifndef DEBUG_LOC_SECTION_LABEL
3531 #define DEBUG_LOC_SECTION_LABEL                    "Ldebug_loc"
3532 #endif
3533 #ifndef DEBUG_RANGES_SECTION_LABEL
3534 #define DEBUG_RANGES_SECTION_LABEL         "Ldebug_ranges"
3535 #endif
3536 #ifndef DEBUG_MACINFO_SECTION_LABEL
3537 #define DEBUG_MACINFO_SECTION_LABEL         "Ldebug_macinfo"
3538 #endif
3539 #ifndef DEBUG_MACRO_SECTION_LABEL
3540 #define DEBUG_MACRO_SECTION_LABEL          "Ldebug_macro"
3541 #endif
3542 #define SKELETON_COMP_DIE_ABBREV 1
3543 #define SKELETON_TYPE_DIE_ABBREV 2
3544
3545 /* Definitions of defaults for formats and names of various special
3546    (artificial) labels which may be generated within this file (when the -g
3547    options is used and DWARF2_DEBUGGING_INFO is in effect.
3548    If necessary, these may be overridden from within the tm.h file, but
3549    typically, overriding these defaults is unnecessary.  */
3550
3551 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3552 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3553 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3554 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3555 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3556 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3557 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3558 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3559 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3560 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3561 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3562 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3563 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3564 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3565
3566 #ifndef TEXT_END_LABEL
3567 #define TEXT_END_LABEL          "Letext"
3568 #endif
3569 #ifndef COLD_END_LABEL
3570 #define COLD_END_LABEL          "Letext_cold"
3571 #endif
3572 #ifndef BLOCK_BEGIN_LABEL
3573 #define BLOCK_BEGIN_LABEL       "LBB"
3574 #endif
3575 #ifndef BLOCK_END_LABEL
3576 #define BLOCK_END_LABEL         "LBE"
3577 #endif
3578 #ifndef LINE_CODE_LABEL
3579 #define LINE_CODE_LABEL         "LM"
3580 #endif
3581
3582 \f
3583 /* Return the root of the DIE's built for the current compilation unit.  */
3584 static dw_die_ref
3585 comp_unit_die (void)
3586 {
3587   if (!single_comp_unit_die)
3588     single_comp_unit_die = gen_compile_unit_die (NULL);
3589   return single_comp_unit_die;
3590 }
3591
3592 /* We allow a language front-end to designate a function that is to be
3593    called to "demangle" any name before it is put into a DIE.  */
3594
3595 static const char *(*demangle_name_func) (const char *);
3596
3597 void
3598 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3599 {
3600   demangle_name_func = func;
3601 }
3602
3603 /* Test if rtl node points to a pseudo register.  */
3604
3605 static inline int
3606 is_pseudo_reg (const_rtx rtl)
3607 {
3608   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3609           || (GET_CODE (rtl) == SUBREG
3610               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3611 }
3612
3613 /* Return a reference to a type, with its const and volatile qualifiers
3614    removed.  */
3615
3616 static inline tree
3617 type_main_variant (tree type)
3618 {
3619   type = TYPE_MAIN_VARIANT (type);
3620
3621   /* ??? There really should be only one main variant among any group of
3622      variants of a given type (and all of the MAIN_VARIANT values for all
3623      members of the group should point to that one type) but sometimes the C
3624      front-end messes this up for array types, so we work around that bug
3625      here.  */
3626   if (TREE_CODE (type) == ARRAY_TYPE)
3627     while (type != TYPE_MAIN_VARIANT (type))
3628       type = TYPE_MAIN_VARIANT (type);
3629
3630   return type;
3631 }
3632
3633 /* Return nonzero if the given type node represents a tagged type.  */
3634
3635 static inline int
3636 is_tagged_type (const_tree type)
3637 {
3638   enum tree_code code = TREE_CODE (type);
3639
3640   return (code == RECORD_TYPE || code == UNION_TYPE
3641           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3642 }
3643
3644 /* Set label to debug_info_section_label + die_offset of a DIE reference.  */
3645
3646 static void
3647 get_ref_die_offset_label (char *label, dw_die_ref ref)
3648 {
3649   sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3650 }
3651
3652 /* Return die_offset of a DIE reference to a base type.  */
3653
3654 static unsigned long int
3655 get_base_type_offset (dw_die_ref ref)
3656 {
3657   if (ref->die_offset)
3658     return ref->die_offset;
3659   if (comp_unit_die ()->die_abbrev)
3660     {
3661       calc_base_type_die_sizes ();
3662       gcc_assert (ref->die_offset);
3663     }
3664   return ref->die_offset;
3665 }
3666
3667 /* Return die_offset of a DIE reference other than base type.  */
3668
3669 static unsigned long int
3670 get_ref_die_offset (dw_die_ref ref)
3671 {
3672   gcc_assert (ref->die_offset);
3673   return ref->die_offset;
3674 }
3675
3676 /* Convert a DIE tag into its string name.  */
3677
3678 static const char *
3679 dwarf_tag_name (unsigned int tag)
3680 {
3681   const char *name = get_DW_TAG_name (tag);
3682
3683   if (name != NULL)
3684     return name;
3685
3686   return "DW_TAG_<unknown>";
3687 }
3688
3689 /* Convert a DWARF attribute code into its string name.  */
3690
3691 static const char *
3692 dwarf_attr_name (unsigned int attr)
3693 {
3694   const char *name;
3695
3696   switch (attr)
3697     {
3698 #if VMS_DEBUGGING_INFO
3699     case DW_AT_HP_prologue:
3700       return "DW_AT_HP_prologue";
3701 #else
3702     case DW_AT_MIPS_loop_unroll_factor:
3703       return "DW_AT_MIPS_loop_unroll_factor";
3704 #endif
3705
3706 #if VMS_DEBUGGING_INFO
3707     case DW_AT_HP_epilogue:
3708       return "DW_AT_HP_epilogue";
3709 #else
3710     case DW_AT_MIPS_stride:
3711       return "DW_AT_MIPS_stride";
3712 #endif
3713     }
3714
3715   name = get_DW_AT_name (attr);
3716
3717   if (name != NULL)
3718     return name;
3719
3720   return "DW_AT_<unknown>";
3721 }
3722
3723 /* Convert a DWARF value form code into its string name.  */
3724
3725 static const char *
3726 dwarf_form_name (unsigned int form)
3727 {
3728   const char *name = get_DW_FORM_name (form);
3729
3730   if (name != NULL)
3731     return name;
3732
3733   return "DW_FORM_<unknown>";
3734 }
3735 \f
3736 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
3737    instance of an inlined instance of a decl which is local to an inline
3738    function, so we have to trace all of the way back through the origin chain
3739    to find out what sort of node actually served as the original seed for the
3740    given block.  */
3741
3742 static tree
3743 decl_ultimate_origin (const_tree decl)
3744 {
3745   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3746     return NULL_TREE;
3747
3748   /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
3749      we're trying to output the abstract instance of this function.  */
3750   if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3751     return NULL_TREE;
3752
3753   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3754      most distant ancestor, this should never happen.  */
3755   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3756
3757   return DECL_ABSTRACT_ORIGIN (decl);
3758 }
3759
3760 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
3761    of a virtual function may refer to a base class, so we check the 'this'
3762    parameter.  */
3763
3764 static tree
3765 decl_class_context (tree decl)
3766 {
3767   tree context = NULL_TREE;
3768
3769   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3770     context = DECL_CONTEXT (decl);
3771   else
3772     context = TYPE_MAIN_VARIANT
3773       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3774
3775   if (context && !TYPE_P (context))
3776     context = NULL_TREE;
3777
3778   return context;
3779 }
3780 \f
3781 /* Add an attribute/value pair to a DIE.  */
3782
3783 static inline void
3784 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
3785 {
3786   /* Maybe this should be an assert?  */
3787   if (die == NULL)
3788     return;
3789
3790   vec_safe_reserve (die->die_attr, 1);
3791   vec_safe_push (die->die_attr, *attr);
3792 }
3793
3794 static inline enum dw_val_class
3795 AT_class (dw_attr_ref a)
3796 {
3797   return a->dw_attr_val.val_class;
3798 }
3799
3800 /* Return the index for any attribute that will be referenced with a
3801    DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index.  String indices
3802    are stored in dw_attr_val.v.val_str for reference counting
3803    pruning.  */
3804
3805 static inline unsigned int
3806 AT_index (dw_attr_ref a)
3807 {
3808   if (AT_class (a) == dw_val_class_str)
3809     return a->dw_attr_val.v.val_str->index;
3810   else if (a->dw_attr_val.val_entry != NULL)
3811     return a->dw_attr_val.val_entry->index;
3812   return NOT_INDEXED;
3813 }
3814
3815 /* Add a flag value attribute to a DIE.  */
3816
3817 static inline void
3818 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3819 {
3820   dw_attr_node attr;
3821
3822   attr.dw_attr = attr_kind;
3823   attr.dw_attr_val.val_class = dw_val_class_flag;
3824   attr.dw_attr_val.val_entry = NULL;
3825   attr.dw_attr_val.v.val_flag = flag;
3826   add_dwarf_attr (die, &attr);
3827 }
3828
3829 static inline unsigned
3830 AT_flag (dw_attr_ref a)
3831 {
3832   gcc_assert (a && AT_class (a) == dw_val_class_flag);
3833   return a->dw_attr_val.v.val_flag;
3834 }
3835
3836 /* Add a signed integer attribute value to a DIE.  */
3837
3838 static inline void
3839 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3840 {
3841   dw_attr_node attr;
3842
3843   attr.dw_attr = attr_kind;
3844   attr.dw_attr_val.val_class = dw_val_class_const;
3845   attr.dw_attr_val.val_entry = NULL;
3846   attr.dw_attr_val.v.val_int = int_val;
3847   add_dwarf_attr (die, &attr);
3848 }
3849
3850 static inline HOST_WIDE_INT
3851 AT_int (dw_attr_ref a)
3852 {
3853   gcc_assert (a && AT_class (a) == dw_val_class_const);
3854   return a->dw_attr_val.v.val_int;
3855 }
3856
3857 /* Add an unsigned integer attribute value to a DIE.  */
3858
3859 static inline void
3860 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3861                  unsigned HOST_WIDE_INT unsigned_val)
3862 {
3863   dw_attr_node attr;
3864
3865   attr.dw_attr = attr_kind;
3866   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3867   attr.dw_attr_val.val_entry = NULL;
3868   attr.dw_attr_val.v.val_unsigned = unsigned_val;
3869   add_dwarf_attr (die, &attr);
3870 }
3871
3872 static inline unsigned HOST_WIDE_INT
3873 AT_unsigned (dw_attr_ref a)
3874 {
3875   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3876   return a->dw_attr_val.v.val_unsigned;
3877 }
3878
3879 /* Add an unsigned wide integer attribute value to a DIE.  */
3880
3881 static inline void
3882 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
3883              const wide_int& w)
3884 {
3885   dw_attr_node attr;
3886
3887   attr.dw_attr = attr_kind;
3888   attr.dw_attr_val.val_class = dw_val_class_wide_int;
3889   attr.dw_attr_val.val_entry = NULL;
3890   attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
3891   *attr.dw_attr_val.v.val_wide = w;
3892   add_dwarf_attr (die, &attr);
3893 }
3894
3895 /* Add an unsigned double integer attribute value to a DIE.  */
3896
3897 static inline void
3898 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3899                HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3900 {
3901   dw_attr_node attr;
3902
3903   attr.dw_attr = attr_kind;
3904   attr.dw_attr_val.val_class = dw_val_class_const_double;
3905   attr.dw_attr_val.val_entry = NULL;
3906   attr.dw_attr_val.v.val_double.high = high;
3907   attr.dw_attr_val.v.val_double.low = low;
3908   add_dwarf_attr (die, &attr);
3909 }
3910
3911 /* Add a floating point attribute value to a DIE and return it.  */
3912
3913 static inline void
3914 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3915             unsigned int length, unsigned int elt_size, unsigned char *array)
3916 {
3917   dw_attr_node attr;
3918
3919   attr.dw_attr = attr_kind;
3920   attr.dw_attr_val.val_class = dw_val_class_vec;
3921   attr.dw_attr_val.val_entry = NULL;
3922   attr.dw_attr_val.v.val_vec.length = length;
3923   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3924   attr.dw_attr_val.v.val_vec.array = array;
3925   add_dwarf_attr (die, &attr);
3926 }
3927
3928 /* Add an 8-byte data attribute value to a DIE.  */
3929
3930 static inline void
3931 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3932               unsigned char data8[8])
3933 {
3934   dw_attr_node attr;
3935
3936   attr.dw_attr = attr_kind;
3937   attr.dw_attr_val.val_class = dw_val_class_data8;
3938   attr.dw_attr_val.val_entry = NULL;
3939   memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3940   add_dwarf_attr (die, &attr);
3941 }
3942
3943 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE.  When using
3944    dwarf_split_debug_info, address attributes in dies destined for the
3945    final executable have force_direct set to avoid using indexed
3946    references.  */
3947
3948 static inline void
3949 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3950                     bool force_direct)
3951 {
3952   dw_attr_node attr;
3953   char * lbl_id;
3954
3955   lbl_id = xstrdup (lbl_low);
3956   attr.dw_attr = DW_AT_low_pc;
3957   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3958   attr.dw_attr_val.v.val_lbl_id = lbl_id;
3959   if (dwarf_split_debug_info && !force_direct)
3960     attr.dw_attr_val.val_entry
3961         = add_addr_table_entry (lbl_id, ate_kind_label);
3962   else
3963     attr.dw_attr_val.val_entry = NULL;
3964   add_dwarf_attr (die, &attr);
3965
3966   attr.dw_attr = DW_AT_high_pc;
3967   if (dwarf_version < 4)
3968     attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3969   else
3970     attr.dw_attr_val.val_class = dw_val_class_high_pc;
3971   lbl_id = xstrdup (lbl_high);
3972   attr.dw_attr_val.v.val_lbl_id = lbl_id;
3973   if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3974       && dwarf_split_debug_info && !force_direct)
3975     attr.dw_attr_val.val_entry
3976         = add_addr_table_entry (lbl_id, ate_kind_label);
3977   else
3978     attr.dw_attr_val.val_entry = NULL;
3979   add_dwarf_attr (die, &attr);
3980 }
3981
3982 /* Hash and equality functions for debug_str_hash.  */
3983
3984 hashval_t
3985 indirect_string_hasher::hash (indirect_string_node *x)
3986 {
3987   return htab_hash_string (x->str);
3988 }
3989
3990 bool
3991 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
3992 {
3993   return strcmp (x1->str, x2) == 0;
3994 }
3995
3996 /* Add STR to the given string hash table.  */
3997
3998 static struct indirect_string_node *
3999 find_AT_string_in_table (const char *str,
4000                          hash_table<indirect_string_hasher> *table)
4001 {
4002   struct indirect_string_node *node;
4003
4004   indirect_string_node **slot
4005     = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
4006   if (*slot == NULL)
4007     {
4008       node = ggc_cleared_alloc<indirect_string_node> ();
4009       node->str = ggc_strdup (str);
4010       *slot = node;
4011     }
4012   else
4013     node = *slot;
4014
4015   node->refcount++;
4016   return node;
4017 }
4018
4019 /* Add STR to the indirect string hash table.  */
4020
4021 static struct indirect_string_node *
4022 find_AT_string (const char *str)
4023 {
4024   if (! debug_str_hash)
4025     debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4026
4027   return find_AT_string_in_table (str, debug_str_hash);
4028 }
4029
4030 /* Add a string attribute value to a DIE.  */
4031
4032 static inline void
4033 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4034 {
4035   dw_attr_node attr;
4036   struct indirect_string_node *node;
4037
4038   node = find_AT_string (str);
4039
4040   attr.dw_attr = attr_kind;
4041   attr.dw_attr_val.val_class = dw_val_class_str;
4042   attr.dw_attr_val.val_entry = NULL;
4043   attr.dw_attr_val.v.val_str = node;
4044   add_dwarf_attr (die, &attr);
4045 }
4046
4047 static inline const char *
4048 AT_string (dw_attr_ref a)
4049 {
4050   gcc_assert (a && AT_class (a) == dw_val_class_str);
4051   return a->dw_attr_val.v.val_str->str;
4052 }
4053
4054 /* Call this function directly to bypass AT_string_form's logic to put
4055    the string inline in the die. */
4056
4057 static void
4058 set_indirect_string (struct indirect_string_node *node)
4059 {
4060   char label[32];
4061   /* Already indirect is a no op.  */
4062   if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
4063     {
4064       gcc_assert (node->label);
4065       return;
4066     }
4067   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4068   ++dw2_string_counter;
4069   node->label = xstrdup (label);
4070
4071   if (!dwarf_split_debug_info)
4072     {
4073       node->form = DW_FORM_strp;
4074       node->index = NOT_INDEXED;
4075     }
4076   else
4077     {
4078       node->form = DW_FORM_GNU_str_index;
4079       node->index = NO_INDEX_ASSIGNED;
4080     }
4081 }
4082
4083 /* Find out whether a string should be output inline in DIE
4084    or out-of-line in .debug_str section.  */
4085
4086 static enum dwarf_form
4087 find_string_form (struct indirect_string_node *node)
4088 {
4089   unsigned int len;
4090
4091   if (node->form)
4092     return node->form;
4093
4094   len = strlen (node->str) + 1;
4095
4096   /* If the string is shorter or equal to the size of the reference, it is
4097      always better to put it inline.  */
4098   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4099     return node->form = DW_FORM_string;
4100
4101   /* If we cannot expect the linker to merge strings in .debug_str
4102      section, only put it into .debug_str if it is worth even in this
4103      single module.  */
4104   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4105       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4106       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4107     return node->form = DW_FORM_string;
4108
4109   set_indirect_string (node);
4110
4111   return node->form;
4112 }
4113
4114 /* Find out whether the string referenced from the attribute should be
4115    output inline in DIE or out-of-line in .debug_str section.  */
4116
4117 static enum dwarf_form
4118 AT_string_form (dw_attr_ref a)
4119 {
4120   gcc_assert (a && AT_class (a) == dw_val_class_str);
4121   return find_string_form (a->dw_attr_val.v.val_str);
4122 }
4123
4124 /* Add a DIE reference attribute value to a DIE.  */
4125
4126 static inline void
4127 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4128 {
4129   dw_attr_node attr;
4130
4131 #ifdef ENABLE_CHECKING
4132   gcc_assert (targ_die != NULL);
4133 #else
4134   /* With LTO we can end up trying to reference something we didn't create
4135      a DIE for.  Avoid crashing later on a NULL referenced DIE.  */
4136   if (targ_die == NULL)
4137     return;
4138 #endif
4139
4140   attr.dw_attr = attr_kind;
4141   attr.dw_attr_val.val_class = dw_val_class_die_ref;
4142   attr.dw_attr_val.val_entry = NULL;
4143   attr.dw_attr_val.v.val_die_ref.die = targ_die;
4144   attr.dw_attr_val.v.val_die_ref.external = 0;
4145   add_dwarf_attr (die, &attr);
4146 }
4147
4148 /* Change DIE reference REF to point to NEW_DIE instead.  */
4149
4150 static inline void
4151 change_AT_die_ref (dw_attr_ref ref, dw_die_ref new_die)
4152 {
4153   gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4154   ref->dw_attr_val.v.val_die_ref.die = new_die;
4155   ref->dw_attr_val.v.val_die_ref.external = 0;
4156 }
4157
4158 /* Add an AT_specification attribute to a DIE, and also make the back
4159    pointer from the specification to the definition.  */
4160
4161 static inline void
4162 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4163 {
4164   add_AT_die_ref (die, DW_AT_specification, targ_die);
4165   gcc_assert (!targ_die->die_definition);
4166   targ_die->die_definition = die;
4167 }
4168
4169 static inline dw_die_ref
4170 AT_ref (dw_attr_ref a)
4171 {
4172   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4173   return a->dw_attr_val.v.val_die_ref.die;
4174 }
4175
4176 static inline int
4177 AT_ref_external (dw_attr_ref a)
4178 {
4179   if (a && AT_class (a) == dw_val_class_die_ref)
4180     return a->dw_attr_val.v.val_die_ref.external;
4181
4182   return 0;
4183 }
4184
4185 static inline void
4186 set_AT_ref_external (dw_attr_ref a, int i)
4187 {
4188   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4189   a->dw_attr_val.v.val_die_ref.external = i;
4190 }
4191
4192 /* Add an FDE reference attribute value to a DIE.  */
4193
4194 static inline void
4195 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4196 {
4197   dw_attr_node attr;
4198
4199   attr.dw_attr = attr_kind;
4200   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4201   attr.dw_attr_val.val_entry = NULL;
4202   attr.dw_attr_val.v.val_fde_index = targ_fde;
4203   add_dwarf_attr (die, &attr);
4204 }
4205
4206 /* Add a location description attribute value to a DIE.  */
4207
4208 static inline void
4209 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4210 {
4211   dw_attr_node attr;
4212
4213   attr.dw_attr = attr_kind;
4214   attr.dw_attr_val.val_class = dw_val_class_loc;
4215   attr.dw_attr_val.val_entry = NULL;
4216   attr.dw_attr_val.v.val_loc = loc;
4217   add_dwarf_attr (die, &attr);
4218 }
4219
4220 static inline dw_loc_descr_ref
4221 AT_loc (dw_attr_ref a)
4222 {
4223   gcc_assert (a && AT_class (a) == dw_val_class_loc);
4224   return a->dw_attr_val.v.val_loc;
4225 }
4226
4227 static inline void
4228 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4229 {
4230   dw_attr_node attr;
4231
4232   attr.dw_attr = attr_kind;
4233   attr.dw_attr_val.val_class = dw_val_class_loc_list;
4234   attr.dw_attr_val.val_entry = NULL;
4235   attr.dw_attr_val.v.val_loc_list = loc_list;
4236   add_dwarf_attr (die, &attr);
4237   have_location_lists = true;
4238 }
4239
4240 static inline dw_loc_list_ref
4241 AT_loc_list (dw_attr_ref a)
4242 {
4243   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4244   return a->dw_attr_val.v.val_loc_list;
4245 }
4246
4247 static inline dw_loc_list_ref *
4248 AT_loc_list_ptr (dw_attr_ref a)
4249 {
4250   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4251   return &a->dw_attr_val.v.val_loc_list;
4252 }
4253
4254 struct addr_hasher : ggc_hasher<addr_table_entry *>
4255 {
4256   static hashval_t hash (addr_table_entry *);
4257   static bool equal (addr_table_entry *, addr_table_entry *);
4258 };
4259
4260 /* Table of entries into the .debug_addr section.  */
4261
4262 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4263
4264 /* Hash an address_table_entry.  */
4265
4266 hashval_t
4267 addr_hasher::hash (addr_table_entry *a)
4268 {
4269   inchash::hash hstate;
4270   switch (a->kind)
4271     {
4272       case ate_kind_rtx:
4273         hstate.add_int (0);
4274         break;
4275       case ate_kind_rtx_dtprel:
4276         hstate.add_int (1);
4277         break;
4278       case ate_kind_label:
4279         return htab_hash_string (a->addr.label);
4280       default:
4281         gcc_unreachable ();
4282     }
4283   inchash::add_rtx (a->addr.rtl, hstate);
4284   return hstate.end ();
4285 }
4286
4287 /* Determine equality for two address_table_entries.  */
4288
4289 bool
4290 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4291 {
4292   if (a1->kind != a2->kind)
4293     return 0;
4294   switch (a1->kind)
4295     {
4296       case ate_kind_rtx:
4297       case ate_kind_rtx_dtprel:
4298         return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4299       case ate_kind_label:
4300         return strcmp (a1->addr.label, a2->addr.label) == 0;
4301       default:
4302         gcc_unreachable ();
4303     }
4304 }
4305
4306 /* Initialize an addr_table_entry.  */
4307
4308 void
4309 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4310 {
4311   e->kind = kind;
4312   switch (kind)
4313     {
4314       case ate_kind_rtx:
4315       case ate_kind_rtx_dtprel:
4316         e->addr.rtl = (rtx) addr;
4317         break;
4318       case ate_kind_label:
4319         e->addr.label = (char *) addr;
4320         break;
4321     }
4322   e->refcount = 0;
4323   e->index = NO_INDEX_ASSIGNED;
4324 }
4325
4326 /* Add attr to the address table entry to the table.  Defer setting an
4327    index until output time.  */
4328
4329 static addr_table_entry *
4330 add_addr_table_entry (void *addr, enum ate_kind kind)
4331 {
4332   addr_table_entry *node;
4333   addr_table_entry finder;
4334
4335   gcc_assert (dwarf_split_debug_info);
4336   if (! addr_index_table)
4337     addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4338   init_addr_table_entry (&finder, kind, addr);
4339   addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4340
4341   if (*slot == HTAB_EMPTY_ENTRY)
4342     {
4343       node = ggc_cleared_alloc<addr_table_entry> ();
4344       init_addr_table_entry (node, kind, addr);
4345       *slot = node;
4346     }
4347   else
4348     node = *slot;
4349
4350   node->refcount++;
4351   return node;
4352 }
4353
4354 /* Remove an entry from the addr table by decrementing its refcount.
4355    Strictly, decrementing the refcount would be enough, but the
4356    assertion that the entry is actually in the table has found
4357    bugs.  */
4358
4359 static void
4360 remove_addr_table_entry (addr_table_entry *entry)
4361 {
4362   gcc_assert (dwarf_split_debug_info && addr_index_table);
4363   /* After an index is assigned, the table is frozen.  */
4364   gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4365   entry->refcount--;
4366 }
4367
4368 /* Given a location list, remove all addresses it refers to from the
4369    address_table.  */
4370
4371 static void
4372 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4373 {
4374   for (; descr; descr = descr->dw_loc_next)
4375     if (descr->dw_loc_oprnd1.val_entry != NULL)
4376       {
4377         gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4378         remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4379       }
4380 }
4381
4382 /* A helper function for dwarf2out_finish called through
4383    htab_traverse.  Assign an addr_table_entry its index.  All entries
4384    must be collected into the table when this function is called,
4385    because the indexing code relies on htab_traverse to traverse nodes
4386    in the same order for each run. */
4387
4388 int
4389 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4390 {
4391   addr_table_entry *node = *h;
4392
4393   /* Don't index unreferenced nodes.  */
4394   if (node->refcount == 0)
4395     return 1;
4396
4397   gcc_assert (node->index == NO_INDEX_ASSIGNED);
4398   node->index = *index;
4399   *index += 1;
4400
4401   return 1;
4402 }
4403
4404 /* Add an address constant attribute value to a DIE.  When using
4405    dwarf_split_debug_info, address attributes in dies destined for the
4406    final executable should be direct references--setting the parameter
4407    force_direct ensures this behavior.  */
4408
4409 static inline void
4410 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4411              bool force_direct)
4412 {
4413   dw_attr_node attr;
4414
4415   attr.dw_attr = attr_kind;
4416   attr.dw_attr_val.val_class = dw_val_class_addr;
4417   attr.dw_attr_val.v.val_addr = addr;
4418   if (dwarf_split_debug_info && !force_direct)
4419     attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4420   else
4421     attr.dw_attr_val.val_entry = NULL;
4422   add_dwarf_attr (die, &attr);
4423 }
4424
4425 /* Get the RTX from to an address DIE attribute.  */
4426
4427 static inline rtx
4428 AT_addr (dw_attr_ref a)
4429 {
4430   gcc_assert (a && AT_class (a) == dw_val_class_addr);
4431   return a->dw_attr_val.v.val_addr;
4432 }
4433
4434 /* Add a file attribute value to a DIE.  */
4435
4436 static inline void
4437 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4438              struct dwarf_file_data *fd)
4439 {
4440   dw_attr_node attr;
4441
4442   attr.dw_attr = attr_kind;
4443   attr.dw_attr_val.val_class = dw_val_class_file;
4444   attr.dw_attr_val.val_entry = NULL;
4445   attr.dw_attr_val.v.val_file = fd;
4446   add_dwarf_attr (die, &attr);
4447 }
4448
4449 /* Get the dwarf_file_data from a file DIE attribute.  */
4450
4451 static inline struct dwarf_file_data *
4452 AT_file (dw_attr_ref a)
4453 {
4454   gcc_assert (a && AT_class (a) == dw_val_class_file);
4455   return a->dw_attr_val.v.val_file;
4456 }
4457
4458 /* Add a vms delta attribute value to a DIE.  */
4459
4460 static inline void
4461 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4462                   const char *lbl1, const char *lbl2)
4463 {
4464   dw_attr_node attr;
4465
4466   attr.dw_attr = attr_kind;
4467   attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4468   attr.dw_attr_val.val_entry = NULL;
4469   attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4470   attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4471   add_dwarf_attr (die, &attr);
4472 }
4473
4474 /* Add a label identifier attribute value to a DIE.  */
4475
4476 static inline void
4477 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4478                const char *lbl_id)
4479 {
4480   dw_attr_node attr;
4481
4482   attr.dw_attr = attr_kind;
4483   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4484   attr.dw_attr_val.val_entry = NULL;
4485   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4486   if (dwarf_split_debug_info)
4487     attr.dw_attr_val.val_entry
4488         = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4489                                 ate_kind_label);
4490   add_dwarf_attr (die, &attr);
4491 }
4492
4493 /* Add a section offset attribute value to a DIE, an offset into the
4494    debug_line section.  */
4495
4496 static inline void
4497 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4498                 const char *label)
4499 {
4500   dw_attr_node attr;
4501
4502   attr.dw_attr = attr_kind;
4503   attr.dw_attr_val.val_class = dw_val_class_lineptr;
4504   attr.dw_attr_val.val_entry = NULL;
4505   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4506   add_dwarf_attr (die, &attr);
4507 }
4508
4509 /* Add a section offset attribute value to a DIE, an offset into the
4510    debug_macinfo section.  */
4511
4512 static inline void
4513 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4514                const char *label)
4515 {
4516   dw_attr_node attr;
4517
4518   attr.dw_attr = attr_kind;
4519   attr.dw_attr_val.val_class = dw_val_class_macptr;
4520   attr.dw_attr_val.val_entry = NULL;
4521   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4522   add_dwarf_attr (die, &attr);
4523 }
4524
4525 /* Add an offset attribute value to a DIE.  */
4526
4527 static inline void
4528 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4529                unsigned HOST_WIDE_INT offset)
4530 {
4531   dw_attr_node attr;
4532
4533   attr.dw_attr = attr_kind;
4534   attr.dw_attr_val.val_class = dw_val_class_offset;
4535   attr.dw_attr_val.val_entry = NULL;
4536   attr.dw_attr_val.v.val_offset = offset;
4537   add_dwarf_attr (die, &attr);
4538 }
4539
4540 /* Add a range_list attribute value to a DIE.  When using
4541    dwarf_split_debug_info, address attributes in dies destined for the
4542    final executable should be direct references--setting the parameter
4543    force_direct ensures this behavior.  */
4544
4545 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4546 #define RELOCATED_OFFSET (NULL)
4547
4548 static void
4549 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4550                    long unsigned int offset, bool force_direct)
4551 {
4552   dw_attr_node attr;
4553
4554   attr.dw_attr = attr_kind;
4555   attr.dw_attr_val.val_class = dw_val_class_range_list;
4556   /* For the range_list attribute, use val_entry to store whether the
4557      offset should follow split-debug-info or normal semantics.  This
4558      value is read in output_range_list_offset.  */
4559   if (dwarf_split_debug_info && !force_direct)
4560     attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4561   else
4562     attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4563   attr.dw_attr_val.v.val_offset = offset;
4564   add_dwarf_attr (die, &attr);
4565 }
4566
4567 /* Return the start label of a delta attribute.  */
4568
4569 static inline const char *
4570 AT_vms_delta1 (dw_attr_ref a)
4571 {
4572   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4573   return a->dw_attr_val.v.val_vms_delta.lbl1;
4574 }
4575
4576 /* Return the end label of a delta attribute.  */
4577
4578 static inline const char *
4579 AT_vms_delta2 (dw_attr_ref a)
4580 {
4581   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4582   return a->dw_attr_val.v.val_vms_delta.lbl2;
4583 }
4584
4585 static inline const char *
4586 AT_lbl (dw_attr_ref a)
4587 {
4588   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4589                     || AT_class (a) == dw_val_class_lineptr
4590                     || AT_class (a) == dw_val_class_macptr
4591                     || AT_class (a) == dw_val_class_high_pc));
4592   return a->dw_attr_val.v.val_lbl_id;
4593 }
4594
4595 /* Get the attribute of type attr_kind.  */
4596
4597 static dw_attr_ref
4598 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4599 {
4600   dw_attr_ref a;
4601   unsigned ix;
4602   dw_die_ref spec = NULL;
4603
4604   if (! die)
4605     return NULL;
4606
4607   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4608     if (a->dw_attr == attr_kind)
4609       return a;
4610     else if (a->dw_attr == DW_AT_specification
4611              || a->dw_attr == DW_AT_abstract_origin)
4612       spec = AT_ref (a);
4613
4614   if (spec)
4615     return get_AT (spec, attr_kind);
4616
4617   return NULL;
4618 }
4619
4620 /* Returns the parent of the declaration of DIE.  */
4621
4622 static dw_die_ref
4623 get_die_parent (dw_die_ref die)
4624 {
4625   dw_die_ref t;
4626
4627   if (!die)
4628     return NULL;
4629
4630   if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4631       || (t = get_AT_ref (die, DW_AT_specification)))
4632     die = t;
4633
4634   return die->die_parent;
4635 }
4636
4637 /* Return the "low pc" attribute value, typically associated with a subprogram
4638    DIE.  Return null if the "low pc" attribute is either not present, or if it
4639    cannot be represented as an assembler label identifier.  */
4640
4641 static inline const char *
4642 get_AT_low_pc (dw_die_ref die)
4643 {
4644   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4645
4646   return a ? AT_lbl (a) : NULL;
4647 }
4648
4649 /* Return the "high pc" attribute value, typically associated with a subprogram
4650    DIE.  Return null if the "high pc" attribute is either not present, or if it
4651    cannot be represented as an assembler label identifier.  */
4652
4653 static inline const char *
4654 get_AT_hi_pc (dw_die_ref die)
4655 {
4656   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4657
4658   return a ? AT_lbl (a) : NULL;
4659 }
4660
4661 /* Return the value of the string attribute designated by ATTR_KIND, or
4662    NULL if it is not present.  */
4663
4664 static inline const char *
4665 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4666 {
4667   dw_attr_ref a = get_AT (die, attr_kind);
4668
4669   return a ? AT_string (a) : NULL;
4670 }
4671
4672 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4673    if it is not present.  */
4674
4675 static inline int
4676 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4677 {
4678   dw_attr_ref a = get_AT (die, attr_kind);
4679
4680   return a ? AT_flag (a) : 0;
4681 }
4682
4683 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4684    if it is not present.  */
4685
4686 static inline unsigned
4687 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4688 {
4689   dw_attr_ref a = get_AT (die, attr_kind);
4690
4691   return a ? AT_unsigned (a) : 0;
4692 }
4693
4694 static inline dw_die_ref
4695 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4696 {
4697   dw_attr_ref a = get_AT (die, attr_kind);
4698
4699   return a ? AT_ref (a) : NULL;
4700 }
4701
4702 static inline struct dwarf_file_data *
4703 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4704 {
4705   dw_attr_ref a = get_AT (die, attr_kind);
4706
4707   return a ? AT_file (a) : NULL;
4708 }
4709
4710 /* Return TRUE if the language is C++.  */
4711
4712 static inline bool
4713 is_cxx (void)
4714 {
4715   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4716
4717   return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
4718           || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
4719 }
4720
4721 /* Return TRUE if the language is Java.  */
4722
4723 static inline bool
4724 is_java (void)
4725 {
4726   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4727
4728   return lang == DW_LANG_Java;
4729 }
4730
4731 /* Return TRUE if the language is Fortran.  */
4732
4733 static inline bool
4734 is_fortran (void)
4735 {
4736   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4737
4738   return (lang == DW_LANG_Fortran77
4739           || lang == DW_LANG_Fortran90
4740           || lang == DW_LANG_Fortran95
4741           || lang == DW_LANG_Fortran03
4742           || lang == DW_LANG_Fortran08);
4743 }
4744
4745 /* Return TRUE if the language is Ada.  */
4746
4747 static inline bool
4748 is_ada (void)
4749 {
4750   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4751
4752   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4753 }
4754
4755 /* Remove the specified attribute if present.  */
4756
4757 static void
4758 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4759 {
4760   dw_attr_ref a;
4761   unsigned ix;
4762
4763   if (! die)
4764     return;
4765
4766   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4767     if (a->dw_attr == attr_kind)
4768       {
4769         if (AT_class (a) == dw_val_class_str)
4770           if (a->dw_attr_val.v.val_str->refcount)
4771             a->dw_attr_val.v.val_str->refcount--;
4772
4773         /* vec::ordered_remove should help reduce the number of abbrevs
4774            that are needed.  */
4775         die->die_attr->ordered_remove (ix);
4776         return;
4777       }
4778 }
4779
4780 /* Remove CHILD from its parent.  PREV must have the property that
4781    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
4782
4783 static void
4784 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4785 {
4786   gcc_assert (child->die_parent == prev->die_parent);
4787   gcc_assert (prev->die_sib == child);
4788   if (prev == child)
4789     {
4790       gcc_assert (child->die_parent->die_child == child);
4791       prev = NULL;
4792     }
4793   else
4794     prev->die_sib = child->die_sib;
4795   if (child->die_parent->die_child == child)
4796     child->die_parent->die_child = prev;
4797 }
4798
4799 /* Replace OLD_CHILD with NEW_CHILD.  PREV must have the property that
4800    PREV->DIE_SIB == OLD_CHILD.  Does not alter OLD_CHILD.  */
4801
4802 static void
4803 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4804 {
4805   dw_die_ref parent = old_child->die_parent;
4806
4807   gcc_assert (parent == prev->die_parent);
4808   gcc_assert (prev->die_sib == old_child);
4809
4810   new_child->die_parent = parent;
4811   if (prev == old_child)
4812     {
4813       gcc_assert (parent->die_child == old_child);
4814       new_child->die_sib = new_child;
4815     }
4816   else
4817     {
4818       prev->die_sib = new_child;
4819       new_child->die_sib = old_child->die_sib;
4820     }
4821   if (old_child->die_parent->die_child == old_child)
4822     old_child->die_parent->die_child = new_child;
4823 }
4824
4825 /* Move all children from OLD_PARENT to NEW_PARENT.  */
4826
4827 static void
4828 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4829 {
4830   dw_die_ref c;
4831   new_parent->die_child = old_parent->die_child;
4832   old_parent->die_child = NULL;
4833   FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4834 }
4835
4836 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
4837    matches TAG.  */
4838
4839 static void
4840 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4841 {
4842   dw_die_ref c;
4843
4844   c = die->die_child;
4845   if (c) do {
4846     dw_die_ref prev = c;
4847     c = c->die_sib;
4848     while (c->die_tag == tag)
4849       {
4850         remove_child_with_prev (c, prev);
4851         /* Might have removed every child.  */
4852         if (c == c->die_sib)
4853           return;
4854         c = c->die_sib;
4855       }
4856   } while (c != die->die_child);
4857 }
4858
4859 /* Add a CHILD_DIE as the last child of DIE.  */
4860
4861 static void
4862 add_child_die (dw_die_ref die, dw_die_ref child_die)
4863 {
4864   /* FIXME this should probably be an assert.  */
4865   if (! die || ! child_die)
4866     return;
4867   gcc_assert (die != child_die);
4868
4869   child_die->die_parent = die;
4870   if (die->die_child)
4871     {
4872       child_die->die_sib = die->die_child->die_sib;
4873       die->die_child->die_sib = child_die;
4874     }
4875   else
4876     child_die->die_sib = child_die;
4877   die->die_child = child_die;
4878 }
4879
4880 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4881    is the specification, to the end of PARENT's list of children.
4882    This is done by removing and re-adding it.  */
4883
4884 static void
4885 splice_child_die (dw_die_ref parent, dw_die_ref child)
4886 {
4887   dw_die_ref p;
4888
4889   /* We want the declaration DIE from inside the class, not the
4890      specification DIE at toplevel.  */
4891   if (child->die_parent != parent)
4892     {
4893       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4894
4895       if (tmp)
4896         child = tmp;
4897     }
4898
4899   gcc_assert (child->die_parent == parent
4900               || (child->die_parent
4901                   == get_AT_ref (parent, DW_AT_specification)));
4902
4903   for (p = child->die_parent->die_child; ; p = p->die_sib)
4904     if (p->die_sib == child)
4905       {
4906         remove_child_with_prev (child, p);
4907         break;
4908       }
4909
4910   add_child_die (parent, child);
4911 }
4912
4913 /* Return a pointer to a newly created DIE node.  */
4914
4915 static inline dw_die_ref
4916 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4917 {
4918   dw_die_ref die = ggc_cleared_alloc<die_node> ();
4919
4920   die->die_tag = tag_value;
4921
4922   if (parent_die != NULL)
4923     add_child_die (parent_die, die);
4924   else
4925     {
4926       limbo_die_node *limbo_node;
4927
4928       limbo_node = ggc_cleared_alloc<limbo_die_node> ();
4929       limbo_node->die = die;
4930       limbo_node->created_for = t;
4931       limbo_node->next = limbo_die_list;
4932       limbo_die_list = limbo_node;
4933     }
4934
4935   return die;
4936 }
4937
4938 /* Return the DIE associated with the given type specifier.  */
4939
4940 static inline dw_die_ref
4941 lookup_type_die (tree type)
4942 {
4943   return TYPE_SYMTAB_DIE (type);
4944 }
4945
4946 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4947    anonymous type named by the typedef TYPE_DIE, return the DIE of the
4948    anonymous type instead the one of the naming typedef.  */
4949
4950 static inline dw_die_ref
4951 strip_naming_typedef (tree type, dw_die_ref type_die)
4952 {
4953   if (type
4954       && TREE_CODE (type) == RECORD_TYPE
4955       && type_die
4956       && type_die->die_tag == DW_TAG_typedef
4957       && is_naming_typedef_decl (TYPE_NAME (type)))
4958     type_die = get_AT_ref (type_die, DW_AT_type);
4959   return type_die;
4960 }
4961
4962 /* Like lookup_type_die, but if type is an anonymous type named by a
4963    typedef[1], return the DIE of the anonymous type instead the one of
4964    the naming typedef.  This is because in gen_typedef_die, we did
4965    equate the anonymous struct named by the typedef with the DIE of
4966    the naming typedef. So by default, lookup_type_die on an anonymous
4967    struct yields the DIE of the naming typedef.
4968
4969    [1]: Read the comment of is_naming_typedef_decl to learn about what
4970    a naming typedef is.  */
4971
4972 static inline dw_die_ref
4973 lookup_type_die_strip_naming_typedef (tree type)
4974 {
4975   dw_die_ref die = lookup_type_die (type);
4976   return strip_naming_typedef (type, die);
4977 }
4978
4979 /* Equate a DIE to a given type specifier.  */
4980
4981 static inline void
4982 equate_type_number_to_die (tree type, dw_die_ref type_die)
4983 {
4984   TYPE_SYMTAB_DIE (type) = type_die;
4985 }
4986
4987 /* Returns a hash value for X (which really is a die_struct).  */
4988
4989 inline hashval_t
4990 decl_die_hasher::hash (die_node *x)
4991 {
4992   return (hashval_t) x->decl_id;
4993 }
4994
4995 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
4996
4997 inline bool
4998 decl_die_hasher::equal (die_node *x, tree y)
4999 {
5000   return (x->decl_id == DECL_UID (y));
5001 }
5002
5003 /* Return the DIE associated with a given declaration.  */
5004
5005 static inline dw_die_ref
5006 lookup_decl_die (tree decl)
5007 {
5008   return decl_die_table->find_with_hash (decl, DECL_UID (decl));
5009 }
5010
5011 /* Returns a hash value for X (which really is a var_loc_list).  */
5012
5013 inline hashval_t
5014 decl_loc_hasher::hash (var_loc_list *x)
5015 {
5016   return (hashval_t) x->decl_id;
5017 }
5018
5019 /* Return nonzero if decl_id of var_loc_list X is the same as
5020    UID of decl *Y.  */
5021
5022 inline bool
5023 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5024 {
5025   return (x->decl_id == DECL_UID (y));
5026 }
5027
5028 /* Return the var_loc list associated with a given declaration.  */
5029
5030 static inline var_loc_list *
5031 lookup_decl_loc (const_tree decl)
5032 {
5033   if (!decl_loc_table)
5034     return NULL;
5035   return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5036 }
5037
5038 /* Returns a hash value for X (which really is a cached_dw_loc_list_list).  */
5039
5040 inline hashval_t
5041 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5042 {
5043   return (hashval_t) x->decl_id;
5044 }
5045
5046 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5047    UID of decl *Y.  */
5048
5049 inline bool
5050 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5051 {
5052   return (x->decl_id == DECL_UID (y));
5053 }
5054
5055 /* Equate a DIE to a particular declaration.  */
5056
5057 static void
5058 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5059 {
5060   unsigned int decl_id = DECL_UID (decl);
5061
5062   *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5063   decl_die->decl_id = decl_id;
5064 }
5065
5066 /* Return how many bits covers PIECE EXPR_LIST.  */
5067
5068 static HOST_WIDE_INT
5069 decl_piece_bitsize (rtx piece)
5070 {
5071   int ret = (int) GET_MODE (piece);
5072   if (ret)
5073     return ret;
5074   gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5075               && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5076   return INTVAL (XEXP (XEXP (piece, 0), 0));
5077 }
5078
5079 /* Return pointer to the location of location note in PIECE EXPR_LIST.  */
5080
5081 static rtx *
5082 decl_piece_varloc_ptr (rtx piece)
5083 {
5084   if ((int) GET_MODE (piece))
5085     return &XEXP (piece, 0);
5086   else
5087     return &XEXP (XEXP (piece, 0), 1);
5088 }
5089
5090 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5091    Next is the chain of following piece nodes.  */
5092
5093 static rtx_expr_list *
5094 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5095 {
5096   if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
5097     return alloc_EXPR_LIST (bitsize, loc_note, next);
5098   else
5099     return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5100                                                GEN_INT (bitsize),
5101                                                loc_note), next);
5102 }
5103
5104 /* Return rtx that should be stored into loc field for
5105    LOC_NOTE and BITPOS/BITSIZE.  */
5106
5107 static rtx
5108 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5109                       HOST_WIDE_INT bitsize)
5110 {
5111   if (bitsize != -1)
5112     {
5113       loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5114       if (bitpos != 0)
5115         loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5116     }
5117   return loc_note;
5118 }
5119
5120 /* This function either modifies location piece list *DEST in
5121    place (if SRC and INNER is NULL), or copies location piece list
5122    *SRC to *DEST while modifying it.  Location BITPOS is modified
5123    to contain LOC_NOTE, any pieces overlapping it are removed resp.
5124    not copied and if needed some padding around it is added.
5125    When modifying in place, DEST should point to EXPR_LIST where
5126    earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5127    to the start of the whole list and INNER points to the EXPR_LIST
5128    where earlier pieces cover PIECE_BITPOS bits.  */
5129
5130 static void
5131 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5132                    HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5133                    HOST_WIDE_INT bitsize, rtx loc_note)
5134 {
5135   HOST_WIDE_INT diff;
5136   bool copy = inner != NULL;
5137
5138   if (copy)
5139     {
5140       /* First copy all nodes preceding the current bitpos.  */
5141       while (src != inner)
5142         {
5143           *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5144                                    decl_piece_bitsize (*src), NULL_RTX);
5145           dest = &XEXP (*dest, 1);
5146           src = &XEXP (*src, 1);
5147         }
5148     }
5149   /* Add padding if needed.  */
5150   if (bitpos != piece_bitpos)
5151     {
5152       *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5153                                copy ? NULL_RTX : *dest);
5154       dest = &XEXP (*dest, 1);
5155     }
5156   else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5157     {
5158       gcc_assert (!copy);
5159       /* A piece with correct bitpos and bitsize already exist,
5160          just update the location for it and return.  */
5161       *decl_piece_varloc_ptr (*dest) = loc_note;
5162       return;
5163     }
5164   /* Add the piece that changed.  */
5165   *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5166   dest = &XEXP (*dest, 1);
5167   /* Skip over pieces that overlap it.  */
5168   diff = bitpos - piece_bitpos + bitsize;
5169   if (!copy)
5170     src = dest;
5171   while (diff > 0 && *src)
5172     {
5173       rtx piece = *src;
5174       diff -= decl_piece_bitsize (piece);
5175       if (copy)
5176         src = &XEXP (piece, 1);
5177       else
5178         {
5179           *src = XEXP (piece, 1);
5180           free_EXPR_LIST_node (piece);
5181         }
5182     }
5183   /* Add padding if needed.  */
5184   if (diff < 0 && *src)
5185     {
5186       if (!copy)
5187         dest = src;
5188       *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5189       dest = &XEXP (*dest, 1);
5190     }
5191   if (!copy)
5192     return;
5193   /* Finally copy all nodes following it.  */
5194   while (*src)
5195     {
5196       *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5197                                decl_piece_bitsize (*src), NULL_RTX);
5198       dest = &XEXP (*dest, 1);
5199       src = &XEXP (*src, 1);
5200     }
5201 }
5202
5203 /* Add a variable location node to the linked list for DECL.  */
5204
5205 static struct var_loc_node *
5206 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5207 {
5208   unsigned int decl_id;
5209   var_loc_list *temp;
5210   struct var_loc_node *loc = NULL;
5211   HOST_WIDE_INT bitsize = -1, bitpos = -1;
5212
5213   if (TREE_CODE (decl) == VAR_DECL
5214       && DECL_HAS_DEBUG_EXPR_P (decl))
5215     {
5216       tree realdecl = DECL_DEBUG_EXPR (decl);
5217       if (handled_component_p (realdecl)
5218           || (TREE_CODE (realdecl) == MEM_REF
5219               && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5220         {
5221           HOST_WIDE_INT maxsize;
5222           tree innerdecl;
5223           innerdecl
5224             = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5225           if (!DECL_P (innerdecl)
5226               || DECL_IGNORED_P (innerdecl)
5227               || TREE_STATIC (innerdecl)
5228               || bitsize <= 0
5229               || bitpos + bitsize > 256
5230               || bitsize != maxsize)
5231             return NULL;
5232           decl = innerdecl;
5233         }
5234     }
5235
5236   decl_id = DECL_UID (decl);
5237   var_loc_list **slot
5238     = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5239   if (*slot == NULL)
5240     {
5241       temp = ggc_cleared_alloc<var_loc_list> ();
5242       temp->decl_id = decl_id;
5243       *slot = temp;
5244     }
5245   else
5246     temp = *slot;
5247
5248   /* For PARM_DECLs try to keep around the original incoming value,
5249      even if that means we'll emit a zero-range .debug_loc entry.  */
5250   if (temp->last
5251       && temp->first == temp->last
5252       && TREE_CODE (decl) == PARM_DECL
5253       && NOTE_P (temp->first->loc)
5254       && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5255       && DECL_INCOMING_RTL (decl)
5256       && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5257       && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5258          == GET_CODE (DECL_INCOMING_RTL (decl))
5259       && prev_real_insn (temp->first->loc) == NULL_RTX
5260       && (bitsize != -1
5261           || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5262                            NOTE_VAR_LOCATION_LOC (loc_note))
5263           || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5264               != NOTE_VAR_LOCATION_STATUS (loc_note))))
5265     {
5266       loc = ggc_cleared_alloc<var_loc_node> ();
5267       temp->first->next = loc;
5268       temp->last = loc;
5269       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5270     }
5271   else if (temp->last)
5272     {
5273       struct var_loc_node *last = temp->last, *unused = NULL;
5274       rtx *piece_loc = NULL, last_loc_note;
5275       HOST_WIDE_INT piece_bitpos = 0;
5276       if (last->next)
5277         {
5278           last = last->next;
5279           gcc_assert (last->next == NULL);
5280         }
5281       if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5282         {
5283           piece_loc = &last->loc;
5284           do
5285             {
5286               HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5287               if (piece_bitpos + cur_bitsize > bitpos)
5288                 break;
5289               piece_bitpos += cur_bitsize;
5290               piece_loc = &XEXP (*piece_loc, 1);
5291             }
5292           while (*piece_loc);
5293         }
5294       /* TEMP->LAST here is either pointer to the last but one or
5295          last element in the chained list, LAST is pointer to the
5296          last element.  */
5297       if (label && strcmp (last->label, label) == 0)
5298         {
5299           /* For SRA optimized variables if there weren't any real
5300              insns since last note, just modify the last node.  */
5301           if (piece_loc != NULL)
5302             {
5303               adjust_piece_list (piece_loc, NULL, NULL,
5304                                  bitpos, piece_bitpos, bitsize, loc_note);
5305               return NULL;
5306             }
5307           /* If the last note doesn't cover any instructions, remove it.  */
5308           if (temp->last != last)
5309             {
5310               temp->last->next = NULL;
5311               unused = last;
5312               last = temp->last;
5313               gcc_assert (strcmp (last->label, label) != 0);
5314             }
5315           else
5316             {
5317               gcc_assert (temp->first == temp->last
5318                           || (temp->first->next == temp->last
5319                               && TREE_CODE (decl) == PARM_DECL));
5320               memset (temp->last, '\0', sizeof (*temp->last));
5321               temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5322               return temp->last;
5323             }
5324         }
5325       if (bitsize == -1 && NOTE_P (last->loc))
5326         last_loc_note = last->loc;
5327       else if (piece_loc != NULL
5328                && *piece_loc != NULL_RTX
5329                && piece_bitpos == bitpos
5330                && decl_piece_bitsize (*piece_loc) == bitsize)
5331         last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5332       else
5333         last_loc_note = NULL_RTX;
5334       /* If the current location is the same as the end of the list,
5335          and either both or neither of the locations is uninitialized,
5336          we have nothing to do.  */
5337       if (last_loc_note == NULL_RTX
5338           || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5339                             NOTE_VAR_LOCATION_LOC (loc_note)))
5340           || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5341                != NOTE_VAR_LOCATION_STATUS (loc_note))
5342               && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5343                    == VAR_INIT_STATUS_UNINITIALIZED)
5344                   || (NOTE_VAR_LOCATION_STATUS (loc_note)
5345                       == VAR_INIT_STATUS_UNINITIALIZED))))
5346         {
5347           /* Add LOC to the end of list and update LAST.  If the last
5348              element of the list has been removed above, reuse its
5349              memory for the new node, otherwise allocate a new one.  */
5350           if (unused)
5351             {
5352               loc = unused;
5353               memset (loc, '\0', sizeof (*loc));
5354             }
5355           else
5356             loc = ggc_cleared_alloc<var_loc_node> ();
5357           if (bitsize == -1 || piece_loc == NULL)
5358             loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5359           else
5360             adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5361                                bitpos, piece_bitpos, bitsize, loc_note);
5362           last->next = loc;
5363           /* Ensure TEMP->LAST will point either to the new last but one
5364              element of the chain, or to the last element in it.  */
5365           if (last != temp->last)
5366             temp->last = last;
5367         }
5368       else if (unused)
5369         ggc_free (unused);
5370     }
5371   else
5372     {
5373       loc = ggc_cleared_alloc<var_loc_node> ();
5374       temp->first = loc;
5375       temp->last = loc;
5376       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5377     }
5378   return loc;
5379 }
5380 \f
5381 /* Keep track of the number of spaces used to indent the
5382    output of the debugging routines that print the structure of
5383    the DIE internal representation.  */
5384 static int print_indent;
5385
5386 /* Indent the line the number of spaces given by print_indent.  */
5387
5388 static inline void
5389 print_spaces (FILE *outfile)
5390 {
5391   fprintf (outfile, "%*s", print_indent, "");
5392 }
5393
5394 /* Print a type signature in hex.  */
5395
5396 static inline void
5397 print_signature (FILE *outfile, char *sig)
5398 {
5399   int i;
5400
5401   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5402     fprintf (outfile, "%02x", sig[i] & 0xff);
5403 }
5404
5405 static void print_loc_descr (dw_loc_descr_ref, FILE *);
5406
5407 /* Print the value associated to the VAL DWARF value node to OUTFILE.  If
5408    RECURSE, output location descriptor operations.  */
5409
5410 static void
5411 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
5412 {
5413   switch (val->val_class)
5414     {
5415     case dw_val_class_addr:
5416       fprintf (outfile, "address");
5417       break;
5418     case dw_val_class_offset:
5419       fprintf (outfile, "offset");
5420       break;
5421     case dw_val_class_loc:
5422       fprintf (outfile, "location descriptor");
5423       if (val->v.val_loc == NULL)
5424         fprintf (outfile, " -> <null>\n");
5425       else if (recurse)
5426         {
5427           fprintf (outfile, ":\n");
5428           print_indent += 4;
5429           print_loc_descr (val->v.val_loc, outfile);
5430           print_indent -= 4;
5431         }
5432       else
5433         fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
5434       break;
5435     case dw_val_class_loc_list:
5436       fprintf (outfile, "location list -> label:%s",
5437                val->v.val_loc_list->ll_symbol);
5438       break;
5439     case dw_val_class_range_list:
5440       fprintf (outfile, "range list");
5441       break;
5442     case dw_val_class_const:
5443       fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
5444       break;
5445     case dw_val_class_unsigned_const:
5446       fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
5447       break;
5448     case dw_val_class_const_double:
5449       fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
5450                         HOST_WIDE_INT_PRINT_UNSIGNED")",
5451                val->v.val_double.high,
5452                val->v.val_double.low);
5453       break;
5454     case dw_val_class_wide_int:
5455       {
5456         int i = val->v.val_wide->get_len ();
5457         fprintf (outfile, "constant (");
5458         gcc_assert (i > 0);
5459         if (val->v.val_wide->elt (i - 1) == 0)
5460           fprintf (outfile, "0x");
5461         fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5462                  val->v.val_wide->elt (--i));
5463         while (--i >= 0)
5464           fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5465                    val->v.val_wide->elt (i));
5466         fprintf (outfile, ")");
5467         break;
5468       }
5469     case dw_val_class_vec:
5470       fprintf (outfile, "floating-point or vector constant");
5471       break;
5472     case dw_val_class_flag:
5473       fprintf (outfile, "%u", val->v.val_flag);
5474       break;
5475     case dw_val_class_die_ref:
5476       if (val->v.val_die_ref.die != NULL)
5477         {
5478           dw_die_ref die = val->v.val_die_ref.die;
5479
5480           if (die->comdat_type_p)
5481             {
5482               fprintf (outfile, "die -> signature: ");
5483               print_signature (outfile,
5484                                die->die_id.die_type_node->signature);
5485             }
5486           else if (die->die_id.die_symbol)
5487             fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
5488           else
5489             fprintf (outfile, "die -> %ld", die->die_offset);
5490           fprintf (outfile, " (%p)", (void *) die);
5491         }
5492       else
5493         fprintf (outfile, "die -> <null>");
5494       break;
5495     case dw_val_class_vms_delta:
5496       fprintf (outfile, "delta: @slotcount(%s-%s)",
5497                val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
5498       break;
5499     case dw_val_class_lbl_id:
5500     case dw_val_class_lineptr:
5501     case dw_val_class_macptr:
5502     case dw_val_class_high_pc:
5503       fprintf (outfile, "label: %s", val->v.val_lbl_id);
5504       break;
5505     case dw_val_class_str:
5506       if (val->v.val_str->str != NULL)
5507         fprintf (outfile, "\"%s\"", val->v.val_str->str);
5508       else
5509         fprintf (outfile, "<null>");
5510       break;
5511     case dw_val_class_file:
5512       fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
5513                val->v.val_file->emitted_number);
5514       break;
5515     case dw_val_class_data8:
5516       {
5517         int i;
5518
5519         for (i = 0; i < 8; i++)
5520           fprintf (outfile, "%02x", val->v.val_data8[i]);
5521         break;
5522       }
5523     default:
5524       break;
5525     }
5526 }
5527
5528 /* Likewise, for a DIE attribute.  */
5529
5530 static void
5531 print_attribute (dw_attr_ref a, bool recurse, FILE *outfile)
5532 {
5533   print_dw_val (&a->dw_attr_val, recurse, outfile);
5534 }
5535
5536
5537 /* Print the list of operands in the LOC location description to OUTFILE.  This
5538    routine is a debugging aid only.  */
5539
5540 static void
5541 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
5542 {
5543   dw_loc_descr_ref l = loc;
5544
5545   if (loc == NULL)
5546     {
5547       print_spaces (outfile);
5548       fprintf (outfile, "<null>\n");
5549       return;
5550     }
5551
5552   for (l = loc; l != NULL; l = l->dw_loc_next)
5553     {
5554       print_spaces (outfile);
5555       fprintf (outfile, "(%p) %s",
5556                (void *) l,
5557                dwarf_stack_op_name (l->dw_loc_opc));
5558       if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
5559         {
5560           fprintf (outfile, " ");
5561           print_dw_val (&l->dw_loc_oprnd1, false, outfile);
5562         }
5563       if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
5564         {
5565           fprintf (outfile, ", ");
5566           print_dw_val (&l->dw_loc_oprnd2, false, outfile);
5567         }
5568       fprintf (outfile, "\n");
5569     }
5570 }
5571
5572 /* Print the information associated with a given DIE, and its children.
5573    This routine is a debugging aid only.  */
5574
5575 static void
5576 print_die (dw_die_ref die, FILE *outfile)
5577 {
5578   dw_attr_ref a;
5579   dw_die_ref c;
5580   unsigned ix;
5581
5582   print_spaces (outfile);
5583   fprintf (outfile, "DIE %4ld: %s (%p)\n",
5584            die->die_offset, dwarf_tag_name (die->die_tag),
5585            (void*) die);
5586   print_spaces (outfile);
5587   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
5588   fprintf (outfile, " offset: %ld", die->die_offset);
5589   fprintf (outfile, " mark: %d\n", die->die_mark);
5590
5591   if (die->comdat_type_p)
5592     {
5593       print_spaces (outfile);
5594       fprintf (outfile, "  signature: ");
5595       print_signature (outfile, die->die_id.die_type_node->signature);
5596       fprintf (outfile, "\n");
5597     }
5598
5599   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5600     {
5601       print_spaces (outfile);
5602       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
5603
5604       print_attribute (a, true, outfile);
5605       fprintf (outfile, "\n");
5606     }
5607
5608   if (die->die_child != NULL)
5609     {
5610       print_indent += 4;
5611       FOR_EACH_CHILD (die, c, print_die (c, outfile));
5612       print_indent -= 4;
5613     }
5614   if (print_indent == 0)
5615     fprintf (outfile, "\n");
5616 }
5617
5618 /* Print the list of operations in the LOC location description.  */
5619
5620 DEBUG_FUNCTION void
5621 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
5622 {
5623   print_loc_descr (loc, stderr);
5624 }
5625
5626 /* Print the information collected for a given DIE.  */
5627
5628 DEBUG_FUNCTION void
5629 debug_dwarf_die (dw_die_ref die)
5630 {
5631   print_die (die, stderr);
5632 }
5633
5634 DEBUG_FUNCTION void
5635 debug (die_struct &ref)
5636 {
5637   print_die (&ref, stderr);
5638 }
5639
5640 DEBUG_FUNCTION void
5641 debug (die_struct *ptr)
5642 {
5643   if (ptr)
5644     debug (*ptr);
5645   else
5646     fprintf (stderr, "<nil>\n");
5647 }
5648
5649
5650 /* Print all DWARF information collected for the compilation unit.
5651    This routine is a debugging aid only.  */
5652
5653 DEBUG_FUNCTION void
5654 debug_dwarf (void)
5655 {
5656   print_indent = 0;
5657   print_die (comp_unit_die (), stderr);
5658 }
5659 \f
5660 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
5661    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
5662    DIE that marks the start of the DIEs for this include file.  */
5663
5664 static dw_die_ref
5665 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5666 {
5667   const char *filename = get_AT_string (bincl_die, DW_AT_name);
5668   dw_die_ref new_unit = gen_compile_unit_die (filename);
5669
5670   new_unit->die_sib = old_unit;
5671   return new_unit;
5672 }
5673
5674 /* Close an include-file CU and reopen the enclosing one.  */
5675
5676 static dw_die_ref
5677 pop_compile_unit (dw_die_ref old_unit)
5678 {
5679   dw_die_ref new_unit = old_unit->die_sib;
5680
5681   old_unit->die_sib = NULL;
5682   return new_unit;
5683 }
5684
5685 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5686 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5687 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5688
5689 /* Calculate the checksum of a location expression.  */
5690
5691 static inline void
5692 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5693 {
5694   int tem;
5695   inchash::hash hstate;
5696   hashval_t hash;
5697
5698   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5699   CHECKSUM (tem);
5700   hash_loc_operands (loc, hstate);
5701   hash = hstate.end();
5702   CHECKSUM (hash);
5703 }
5704
5705 /* Calculate the checksum of an attribute.  */
5706
5707 static void
5708 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5709 {
5710   dw_loc_descr_ref loc;
5711   rtx r;
5712
5713   CHECKSUM (at->dw_attr);
5714
5715   /* We don't care that this was compiled with a different compiler
5716      snapshot; if the output is the same, that's what matters.  */
5717   if (at->dw_attr == DW_AT_producer)
5718     return;
5719
5720   switch (AT_class (at))
5721     {
5722     case dw_val_class_const:
5723       CHECKSUM (at->dw_attr_val.v.val_int);
5724       break;
5725     case dw_val_class_unsigned_const:
5726       CHECKSUM (at->dw_attr_val.v.val_unsigned);
5727       break;
5728     case dw_val_class_const_double:
5729       CHECKSUM (at->dw_attr_val.v.val_double);
5730       break;
5731     case dw_val_class_wide_int:
5732       CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
5733                       get_full_len (*at->dw_attr_val.v.val_wide)
5734                       * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
5735       break;
5736     case dw_val_class_vec:
5737       CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5738                       (at->dw_attr_val.v.val_vec.length
5739                        * at->dw_attr_val.v.val_vec.elt_size));
5740       break;
5741     case dw_val_class_flag:
5742       CHECKSUM (at->dw_attr_val.v.val_flag);
5743       break;
5744     case dw_val_class_str:
5745       CHECKSUM_STRING (AT_string (at));
5746       break;
5747
5748     case dw_val_class_addr:
5749       r = AT_addr (at);
5750       gcc_assert (GET_CODE (r) == SYMBOL_REF);
5751       CHECKSUM_STRING (XSTR (r, 0));
5752       break;
5753
5754     case dw_val_class_offset:
5755       CHECKSUM (at->dw_attr_val.v.val_offset);
5756       break;
5757
5758     case dw_val_class_loc:
5759       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5760         loc_checksum (loc, ctx);
5761       break;
5762
5763     case dw_val_class_die_ref:
5764       die_checksum (AT_ref (at), ctx, mark);
5765       break;
5766
5767     case dw_val_class_fde_ref:
5768     case dw_val_class_vms_delta:
5769     case dw_val_class_lbl_id:
5770     case dw_val_class_lineptr:
5771     case dw_val_class_macptr:
5772     case dw_val_class_high_pc:
5773       break;
5774
5775     case dw_val_class_file:
5776       CHECKSUM_STRING (AT_file (at)->filename);
5777       break;
5778
5779     case dw_val_class_data8:
5780       CHECKSUM (at->dw_attr_val.v.val_data8);
5781       break;
5782
5783     default:
5784       break;
5785     }
5786 }
5787
5788 /* Calculate the checksum of a DIE.  */
5789
5790 static void
5791 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5792 {
5793   dw_die_ref c;
5794   dw_attr_ref a;
5795   unsigned ix;
5796
5797   /* To avoid infinite recursion.  */
5798   if (die->die_mark)
5799     {
5800       CHECKSUM (die->die_mark);
5801       return;
5802     }
5803   die->die_mark = ++(*mark);
5804
5805   CHECKSUM (die->die_tag);
5806
5807   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5808     attr_checksum (a, ctx, mark);
5809
5810   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5811 }
5812
5813 #undef CHECKSUM
5814 #undef CHECKSUM_BLOCK
5815 #undef CHECKSUM_STRING
5816
5817 /* For DWARF-4 types, include the trailing NULL when checksumming strings.  */
5818 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5819 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5820 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5821 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5822 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5823 #define CHECKSUM_ATTR(FOO) \
5824   if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5825
5826 /* Calculate the checksum of a number in signed LEB128 format.  */
5827
5828 static void
5829 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5830 {
5831   unsigned char byte;
5832   bool more;
5833
5834   while (1)
5835     {
5836       byte = (value & 0x7f);
5837       value >>= 7;
5838       more = !((value == 0 && (byte & 0x40) == 0)
5839                 || (value == -1 && (byte & 0x40) != 0));
5840       if (more)
5841         byte |= 0x80;
5842       CHECKSUM (byte);
5843       if (!more)
5844         break;
5845     }
5846 }
5847
5848 /* Calculate the checksum of a number in unsigned LEB128 format.  */
5849
5850 static void
5851 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5852 {
5853   while (1)
5854     {
5855       unsigned char byte = (value & 0x7f);
5856       value >>= 7;
5857       if (value != 0)
5858         /* More bytes to follow.  */
5859         byte |= 0x80;
5860       CHECKSUM (byte);
5861       if (value == 0)
5862         break;
5863     }
5864 }
5865
5866 /* Checksum the context of the DIE.  This adds the names of any
5867    surrounding namespaces or structures to the checksum.  */
5868
5869 static void
5870 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5871 {
5872   const char *name;
5873   dw_die_ref spec;
5874   int tag = die->die_tag;
5875
5876   if (tag != DW_TAG_namespace
5877       && tag != DW_TAG_structure_type
5878       && tag != DW_TAG_class_type)
5879     return;
5880
5881   name = get_AT_string (die, DW_AT_name);
5882
5883   spec = get_AT_ref (die, DW_AT_specification);
5884   if (spec != NULL)
5885     die = spec;
5886
5887   if (die->die_parent != NULL)
5888     checksum_die_context (die->die_parent, ctx);
5889
5890   CHECKSUM_ULEB128 ('C');
5891   CHECKSUM_ULEB128 (tag);
5892   if (name != NULL)
5893     CHECKSUM_STRING (name);
5894 }
5895
5896 /* Calculate the checksum of a location expression.  */
5897
5898 static inline void
5899 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5900 {
5901   /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5902      were emitted as a DW_FORM_sdata instead of a location expression.  */
5903   if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5904     {
5905       CHECKSUM_ULEB128 (DW_FORM_sdata);
5906       CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5907       return;
5908     }
5909
5910   /* Otherwise, just checksum the raw location expression.  */
5911   while (loc != NULL)
5912     {
5913       inchash::hash hstate;
5914       hashval_t hash;
5915
5916       CHECKSUM_ULEB128 (loc->dtprel);
5917       CHECKSUM_ULEB128 (loc->dw_loc_opc);
5918       hash_loc_operands (loc, hstate);
5919       hash = hstate.end ();
5920       CHECKSUM (hash);
5921       loc = loc->dw_loc_next;
5922     }
5923 }
5924
5925 /* Calculate the checksum of an attribute.  */
5926
5927 static void
5928 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5929                        struct md5_ctx *ctx, int *mark)
5930 {
5931   dw_loc_descr_ref loc;
5932   rtx r;
5933
5934   if (AT_class (at) == dw_val_class_die_ref)
5935     {
5936       dw_die_ref target_die = AT_ref (at);
5937
5938       /* For pointer and reference types, we checksum only the (qualified)
5939          name of the target type (if there is a name).  For friend entries,
5940          we checksum only the (qualified) name of the target type or function.
5941          This allows the checksum to remain the same whether the target type
5942          is complete or not.  */
5943       if ((at->dw_attr == DW_AT_type
5944            && (tag == DW_TAG_pointer_type
5945                || tag == DW_TAG_reference_type
5946                || tag == DW_TAG_rvalue_reference_type
5947                || tag == DW_TAG_ptr_to_member_type))
5948           || (at->dw_attr == DW_AT_friend
5949               && tag == DW_TAG_friend))
5950         {
5951           dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5952
5953           if (name_attr != NULL)
5954             {
5955               dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5956
5957               if (decl == NULL)
5958                 decl = target_die;
5959               CHECKSUM_ULEB128 ('N');
5960               CHECKSUM_ULEB128 (at->dw_attr);
5961               if (decl->die_parent != NULL)
5962                 checksum_die_context (decl->die_parent, ctx);
5963               CHECKSUM_ULEB128 ('E');
5964               CHECKSUM_STRING (AT_string (name_attr));
5965               return;
5966             }
5967         }
5968
5969       /* For all other references to another DIE, we check to see if the
5970          target DIE has already been visited.  If it has, we emit a
5971          backward reference; if not, we descend recursively.  */
5972       if (target_die->die_mark > 0)
5973         {
5974           CHECKSUM_ULEB128 ('R');
5975           CHECKSUM_ULEB128 (at->dw_attr);
5976           CHECKSUM_ULEB128 (target_die->die_mark);
5977         }
5978       else
5979         {
5980           dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5981
5982           if (decl == NULL)
5983             decl = target_die;
5984           target_die->die_mark = ++(*mark);
5985           CHECKSUM_ULEB128 ('T');
5986           CHECKSUM_ULEB128 (at->dw_attr);
5987           if (decl->die_parent != NULL)
5988             checksum_die_context (decl->die_parent, ctx);
5989           die_checksum_ordered (target_die, ctx, mark);
5990         }
5991       return;
5992     }
5993
5994   CHECKSUM_ULEB128 ('A');
5995   CHECKSUM_ULEB128 (at->dw_attr);
5996
5997   switch (AT_class (at))
5998     {
5999     case dw_val_class_const:
6000       CHECKSUM_ULEB128 (DW_FORM_sdata);
6001       CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
6002       break;
6003
6004     case dw_val_class_unsigned_const:
6005       CHECKSUM_ULEB128 (DW_FORM_sdata);
6006       CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6007       break;
6008
6009     case dw_val_class_const_double:
6010       CHECKSUM_ULEB128 (DW_FORM_block);
6011       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6012       CHECKSUM (at->dw_attr_val.v.val_double);
6013       break;
6014
6015     case dw_val_class_wide_int:
6016       CHECKSUM_ULEB128 (DW_FORM_block);
6017       CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
6018                         * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
6019       CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6020                       get_full_len (*at->dw_attr_val.v.val_wide)
6021                       * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6022       break;
6023
6024     case dw_val_class_vec:
6025       CHECKSUM_ULEB128 (DW_FORM_block);
6026       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6027                         * at->dw_attr_val.v.val_vec.elt_size);
6028       CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6029                       (at->dw_attr_val.v.val_vec.length
6030                        * at->dw_attr_val.v.val_vec.elt_size));
6031       break;
6032
6033     case dw_val_class_flag:
6034       CHECKSUM_ULEB128 (DW_FORM_flag);
6035       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6036       break;
6037
6038     case dw_val_class_str:
6039       CHECKSUM_ULEB128 (DW_FORM_string);
6040       CHECKSUM_STRING (AT_string (at));
6041       break;
6042
6043     case dw_val_class_addr:
6044       r = AT_addr (at);
6045       gcc_assert (GET_CODE (r) == SYMBOL_REF);
6046       CHECKSUM_ULEB128 (DW_FORM_string);
6047       CHECKSUM_STRING (XSTR (r, 0));
6048       break;
6049
6050     case dw_val_class_offset:
6051       CHECKSUM_ULEB128 (DW_FORM_sdata);
6052       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6053       break;
6054
6055     case dw_val_class_loc:
6056       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6057         loc_checksum_ordered (loc, ctx);
6058       break;
6059
6060     case dw_val_class_fde_ref:
6061     case dw_val_class_lbl_id:
6062     case dw_val_class_lineptr:
6063     case dw_val_class_macptr:
6064     case dw_val_class_high_pc:
6065       break;
6066
6067     case dw_val_class_file:
6068       CHECKSUM_ULEB128 (DW_FORM_string);
6069       CHECKSUM_STRING (AT_file (at)->filename);
6070       break;
6071
6072     case dw_val_class_data8:
6073       CHECKSUM (at->dw_attr_val.v.val_data8);
6074       break;
6075
6076     default:
6077       break;
6078     }
6079 }
6080
6081 struct checksum_attributes
6082 {
6083   dw_attr_ref at_name;
6084   dw_attr_ref at_type;
6085   dw_attr_ref at_friend;
6086   dw_attr_ref at_accessibility;
6087   dw_attr_ref at_address_class;
6088   dw_attr_ref at_allocated;
6089   dw_attr_ref at_artificial;
6090   dw_attr_ref at_associated;
6091   dw_attr_ref at_binary_scale;
6092   dw_attr_ref at_bit_offset;
6093   dw_attr_ref at_bit_size;
6094   dw_attr_ref at_bit_stride;
6095   dw_attr_ref at_byte_size;
6096   dw_attr_ref at_byte_stride;
6097   dw_attr_ref at_const_value;
6098   dw_attr_ref at_containing_type;
6099   dw_attr_ref at_count;
6100   dw_attr_ref at_data_location;
6101   dw_attr_ref at_data_member_location;
6102   dw_attr_ref at_decimal_scale;
6103   dw_attr_ref at_decimal_sign;
6104   dw_attr_ref at_default_value;
6105   dw_attr_ref at_digit_count;
6106   dw_attr_ref at_discr;
6107   dw_attr_ref at_discr_list;
6108   dw_attr_ref at_discr_value;
6109   dw_attr_ref at_encoding;
6110   dw_attr_ref at_endianity;
6111   dw_attr_ref at_explicit;
6112   dw_attr_ref at_is_optional;
6113   dw_attr_ref at_location;
6114   dw_attr_ref at_lower_bound;
6115   dw_attr_ref at_mutable;
6116   dw_attr_ref at_ordering;
6117   dw_attr_ref at_picture_string;
6118   dw_attr_ref at_prototyped;
6119   dw_attr_ref at_small;
6120   dw_attr_ref at_segment;
6121   dw_attr_ref at_string_length;
6122   dw_attr_ref at_threads_scaled;
6123   dw_attr_ref at_upper_bound;
6124   dw_attr_ref at_use_location;
6125   dw_attr_ref at_use_UTF8;
6126   dw_attr_ref at_variable_parameter;
6127   dw_attr_ref at_virtuality;
6128   dw_attr_ref at_visibility;
6129   dw_attr_ref at_vtable_elem_location;
6130 };
6131
6132 /* Collect the attributes that we will want to use for the checksum.  */
6133
6134 static void
6135 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6136 {
6137   dw_attr_ref a;
6138   unsigned ix;
6139
6140   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6141     {
6142       switch (a->dw_attr)
6143         {
6144         case DW_AT_name:
6145           attrs->at_name = a;
6146           break;
6147         case DW_AT_type:
6148           attrs->at_type = a;
6149           break;
6150         case DW_AT_friend:
6151           attrs->at_friend = a;
6152           break;
6153         case DW_AT_accessibility:
6154           attrs->at_accessibility = a;
6155           break;
6156         case DW_AT_address_class:
6157           attrs->at_address_class = a;
6158           break;
6159         case DW_AT_allocated:
6160           attrs->at_allocated = a;
6161           break;
6162         case DW_AT_artificial:
6163           attrs->at_artificial = a;
6164           break;
6165         case DW_AT_associated:
6166           attrs->at_associated = a;
6167           break;
6168         case DW_AT_binary_scale:
6169           attrs->at_binary_scale = a;
6170           break;
6171         case DW_AT_bit_offset:
6172           attrs->at_bit_offset = a;
6173           break;
6174         case DW_AT_bit_size:
6175           attrs->at_bit_size = a;
6176           break;
6177         case DW_AT_bit_stride:
6178           attrs->at_bit_stride = a;
6179           break;
6180         case DW_AT_byte_size:
6181           attrs->at_byte_size = a;
6182           break;
6183         case DW_AT_byte_stride:
6184           attrs->at_byte_stride = a;
6185           break;
6186         case DW_AT_const_value:
6187           attrs->at_const_value = a;
6188           break;
6189         case DW_AT_containing_type:
6190           attrs->at_containing_type = a;
6191           break;
6192         case DW_AT_count:
6193           attrs->at_count = a;
6194           break;
6195         case DW_AT_data_location:
6196           attrs->at_data_location = a;
6197           break;
6198         case DW_AT_data_member_location:
6199           attrs->at_data_member_location = a;
6200           break;
6201         case DW_AT_decimal_scale:
6202           attrs->at_decimal_scale = a;
6203           break;
6204         case DW_AT_decimal_sign:
6205           attrs->at_decimal_sign = a;
6206           break;
6207         case DW_AT_default_value:
6208           attrs->at_default_value = a;
6209           break;
6210         case DW_AT_digit_count:
6211           attrs->at_digit_count = a;
6212           break;
6213         case DW_AT_discr:
6214           attrs->at_discr = a;
6215           break;
6216         case DW_AT_discr_list:
6217           attrs->at_discr_list = a;
6218           break;
6219         case DW_AT_discr_value:
6220           attrs->at_discr_value = a;
6221           break;
6222         case DW_AT_encoding:
6223           attrs->at_encoding = a;
6224           break;
6225         case DW_AT_endianity:
6226           attrs->at_endianity = a;
6227           break;
6228         case DW_AT_explicit:
6229           attrs->at_explicit = a;
6230           break;
6231         case DW_AT_is_optional:
6232           attrs->at_is_optional = a;
6233           break;
6234         case DW_AT_location:
6235           attrs->at_location = a;
6236           break;
6237         case DW_AT_lower_bound:
6238           attrs->at_lower_bound = a;
6239           break;
6240         case DW_AT_mutable:
6241           attrs->at_mutable = a;
6242           break;
6243         case DW_AT_ordering:
6244           attrs->at_ordering = a;
6245           break;
6246         case DW_AT_picture_string:
6247           attrs->at_picture_string = a;
6248           break;
6249         case DW_AT_prototyped:
6250           attrs->at_prototyped = a;
6251           break;
6252         case DW_AT_small:
6253           attrs->at_small = a;
6254           break;
6255         case DW_AT_segment:
6256           attrs->at_segment = a;
6257           break;
6258         case DW_AT_string_length:
6259           attrs->at_string_length = a;
6260           break;
6261         case DW_AT_threads_scaled:
6262           attrs->at_threads_scaled = a;
6263           break;
6264         case DW_AT_upper_bound:
6265           attrs->at_upper_bound = a;
6266           break;
6267         case DW_AT_use_location:
6268           attrs->at_use_location = a;
6269           break;
6270         case DW_AT_use_UTF8:
6271           attrs->at_use_UTF8 = a;
6272           break;
6273         case DW_AT_variable_parameter:
6274           attrs->at_variable_parameter = a;
6275           break;
6276         case DW_AT_virtuality:
6277           attrs->at_virtuality = a;
6278           break;
6279         case DW_AT_visibility:
6280           attrs->at_visibility = a;
6281           break;
6282         case DW_AT_vtable_elem_location:
6283           attrs->at_vtable_elem_location = a;
6284           break;
6285         default:
6286           break;
6287         }
6288     }
6289 }
6290
6291 /* Calculate the checksum of a DIE, using an ordered subset of attributes.  */
6292
6293 static void
6294 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6295 {
6296   dw_die_ref c;
6297   dw_die_ref decl;
6298   struct checksum_attributes attrs;
6299
6300   CHECKSUM_ULEB128 ('D');
6301   CHECKSUM_ULEB128 (die->die_tag);
6302
6303   memset (&attrs, 0, sizeof (attrs));
6304
6305   decl = get_AT_ref (die, DW_AT_specification);
6306   if (decl != NULL)
6307     collect_checksum_attributes (&attrs, decl);
6308   collect_checksum_attributes (&attrs, die);
6309
6310   CHECKSUM_ATTR (attrs.at_name);
6311   CHECKSUM_ATTR (attrs.at_accessibility);
6312   CHECKSUM_ATTR (attrs.at_address_class);
6313   CHECKSUM_ATTR (attrs.at_allocated);
6314   CHECKSUM_ATTR (attrs.at_artificial);
6315   CHECKSUM_ATTR (attrs.at_associated);
6316   CHECKSUM_ATTR (attrs.at_binary_scale);
6317   CHECKSUM_ATTR (attrs.at_bit_offset);
6318   CHECKSUM_ATTR (attrs.at_bit_size);
6319   CHECKSUM_ATTR (attrs.at_bit_stride);
6320   CHECKSUM_ATTR (attrs.at_byte_size);
6321   CHECKSUM_ATTR (attrs.at_byte_stride);
6322   CHECKSUM_ATTR (attrs.at_const_value);
6323   CHECKSUM_ATTR (attrs.at_containing_type);
6324   CHECKSUM_ATTR (attrs.at_count);
6325   CHECKSUM_ATTR (attrs.at_data_location);
6326   CHECKSUM_ATTR (attrs.at_data_member_location);
6327   CHECKSUM_ATTR (attrs.at_decimal_scale);
6328   CHECKSUM_ATTR (attrs.at_decimal_sign);
6329   CHECKSUM_ATTR (attrs.at_default_value);
6330   CHECKSUM_ATTR (attrs.at_digit_count);
6331   CHECKSUM_ATTR (attrs.at_discr);
6332   CHECKSUM_ATTR (attrs.at_discr_list);
6333   CHECKSUM_ATTR (attrs.at_discr_value);
6334   CHECKSUM_ATTR (attrs.at_encoding);
6335   CHECKSUM_ATTR (attrs.at_endianity);
6336   CHECKSUM_ATTR (attrs.at_explicit);
6337   CHECKSUM_ATTR (attrs.at_is_optional);
6338   CHECKSUM_ATTR (attrs.at_location);
6339   CHECKSUM_ATTR (attrs.at_lower_bound);
6340   CHECKSUM_ATTR (attrs.at_mutable);
6341   CHECKSUM_ATTR (attrs.at_ordering);
6342   CHECKSUM_ATTR (attrs.at_picture_string);
6343   CHECKSUM_ATTR (attrs.at_prototyped);
6344   CHECKSUM_ATTR (attrs.at_small);
6345   CHECKSUM_ATTR (attrs.at_segment);
6346   CHECKSUM_ATTR (attrs.at_string_length);
6347   CHECKSUM_ATTR (attrs.at_threads_scaled);
6348   CHECKSUM_ATTR (attrs.at_upper_bound);
6349   CHECKSUM_ATTR (attrs.at_use_location);
6350   CHECKSUM_ATTR (attrs.at_use_UTF8);
6351   CHECKSUM_ATTR (attrs.at_variable_parameter);
6352   CHECKSUM_ATTR (attrs.at_virtuality);
6353   CHECKSUM_ATTR (attrs.at_visibility);
6354   CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6355   CHECKSUM_ATTR (attrs.at_type);
6356   CHECKSUM_ATTR (attrs.at_friend);
6357
6358   /* Checksum the child DIEs.  */
6359   c = die->die_child;
6360   if (c) do {
6361     dw_attr_ref name_attr;
6362
6363     c = c->die_sib;
6364     name_attr = get_AT (c, DW_AT_name);
6365     if (is_template_instantiation (c))
6366       {
6367         /* Ignore instantiations of member type and function templates.  */
6368       }
6369     else if (name_attr != NULL
6370              && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6371       {
6372         /* Use a shallow checksum for named nested types and member
6373            functions.  */
6374         CHECKSUM_ULEB128 ('S');
6375         CHECKSUM_ULEB128 (c->die_tag);
6376         CHECKSUM_STRING (AT_string (name_attr));
6377       }
6378     else
6379       {
6380         /* Use a deep checksum for other children.  */
6381         /* Mark this DIE so it gets processed when unmarking.  */
6382         if (c->die_mark == 0)
6383           c->die_mark = -1;
6384         die_checksum_ordered (c, ctx, mark);
6385       }
6386   } while (c != die->die_child);
6387
6388   CHECKSUM_ULEB128 (0);
6389 }
6390
6391 /* Add a type name and tag to a hash.  */
6392 static void
6393 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6394 {
6395   CHECKSUM_ULEB128 (tag);
6396   CHECKSUM_STRING (name);
6397 }
6398
6399 #undef CHECKSUM
6400 #undef CHECKSUM_STRING
6401 #undef CHECKSUM_ATTR
6402 #undef CHECKSUM_LEB128
6403 #undef CHECKSUM_ULEB128
6404
6405 /* Generate the type signature for DIE.  This is computed by generating an
6406    MD5 checksum over the DIE's tag, its relevant attributes, and its
6407    children.  Attributes that are references to other DIEs are processed
6408    by recursion, using the MARK field to prevent infinite recursion.
6409    If the DIE is nested inside a namespace or another type, we also
6410    need to include that context in the signature.  The lower 64 bits
6411    of the resulting MD5 checksum comprise the signature.  */
6412
6413 static void
6414 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6415 {
6416   int mark;
6417   const char *name;
6418   unsigned char checksum[16];
6419   struct md5_ctx ctx;
6420   dw_die_ref decl;
6421   dw_die_ref parent;
6422
6423   name = get_AT_string (die, DW_AT_name);
6424   decl = get_AT_ref (die, DW_AT_specification);
6425   parent = get_die_parent (die);
6426
6427   /* First, compute a signature for just the type name (and its surrounding
6428      context, if any.  This is stored in the type unit DIE for link-time
6429      ODR (one-definition rule) checking.  */
6430
6431   if (is_cxx () && name != NULL)
6432     {
6433       md5_init_ctx (&ctx);
6434
6435       /* Checksum the names of surrounding namespaces and structures.  */
6436       if (parent != NULL)
6437         checksum_die_context (parent, &ctx);
6438
6439       /* Checksum the current DIE. */
6440       die_odr_checksum (die->die_tag, name, &ctx);
6441       md5_finish_ctx (&ctx, checksum);
6442
6443       add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6444     }
6445
6446   /* Next, compute the complete type signature.  */
6447
6448   md5_init_ctx (&ctx);
6449   mark = 1;
6450   die->die_mark = mark;
6451
6452   /* Checksum the names of surrounding namespaces and structures.  */
6453   if (parent != NULL)
6454     checksum_die_context (parent, &ctx);
6455
6456   /* Checksum the DIE and its children.  */
6457   die_checksum_ordered (die, &ctx, &mark);
6458   unmark_all_dies (die);
6459   md5_finish_ctx (&ctx, checksum);
6460
6461   /* Store the signature in the type node and link the type DIE and the
6462      type node together.  */
6463   memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6464           DWARF_TYPE_SIGNATURE_SIZE);
6465   die->comdat_type_p = true;
6466   die->die_id.die_type_node = type_node;
6467   type_node->type_die = die;
6468
6469   /* If the DIE is a specification, link its declaration to the type node
6470      as well.  */
6471   if (decl != NULL)
6472     {
6473       decl->comdat_type_p = true;
6474       decl->die_id.die_type_node = type_node;
6475     }
6476 }
6477
6478 /* Do the location expressions look same?  */
6479 static inline int
6480 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6481 {
6482   return loc1->dw_loc_opc == loc2->dw_loc_opc
6483          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6484          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6485 }
6486
6487 /* Do the values look the same?  */
6488 static int
6489 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6490 {
6491   dw_loc_descr_ref loc1, loc2;
6492   rtx r1, r2;
6493
6494   if (v1->val_class != v2->val_class)
6495     return 0;
6496
6497   switch (v1->val_class)
6498     {
6499     case dw_val_class_const:
6500       return v1->v.val_int == v2->v.val_int;
6501     case dw_val_class_unsigned_const:
6502       return v1->v.val_unsigned == v2->v.val_unsigned;
6503     case dw_val_class_const_double:
6504       return v1->v.val_double.high == v2->v.val_double.high
6505              && v1->v.val_double.low == v2->v.val_double.low;
6506     case dw_val_class_wide_int:
6507       return *v1->v.val_wide == *v2->v.val_wide;
6508     case dw_val_class_vec:
6509       if (v1->v.val_vec.length != v2->v.val_vec.length
6510           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6511         return 0;
6512       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6513                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
6514         return 0;
6515       return 1;
6516     case dw_val_class_flag:
6517       return v1->v.val_flag == v2->v.val_flag;
6518     case dw_val_class_str:
6519       return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6520
6521     case dw_val_class_addr:
6522       r1 = v1->v.val_addr;
6523       r2 = v2->v.val_addr;
6524       if (GET_CODE (r1) != GET_CODE (r2))
6525         return 0;
6526       return !rtx_equal_p (r1, r2);
6527
6528     case dw_val_class_offset:
6529       return v1->v.val_offset == v2->v.val_offset;
6530
6531     case dw_val_class_loc:
6532       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6533            loc1 && loc2;
6534            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6535         if (!same_loc_p (loc1, loc2, mark))
6536           return 0;
6537       return !loc1 && !loc2;
6538
6539     case dw_val_class_die_ref:
6540       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6541
6542     case dw_val_class_fde_ref:
6543     case dw_val_class_vms_delta:
6544     case dw_val_class_lbl_id:
6545     case dw_val_class_lineptr:
6546     case dw_val_class_macptr:
6547     case dw_val_class_high_pc:
6548       return 1;
6549
6550     case dw_val_class_file:
6551       return v1->v.val_file == v2->v.val_file;
6552
6553     case dw_val_class_data8:
6554       return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6555
6556     default:
6557       return 1;
6558     }
6559 }
6560
6561 /* Do the attributes look the same?  */
6562
6563 static int
6564 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6565 {
6566   if (at1->dw_attr != at2->dw_attr)
6567     return 0;
6568
6569   /* We don't care that this was compiled with a different compiler
6570      snapshot; if the output is the same, that's what matters. */
6571   if (at1->dw_attr == DW_AT_producer)
6572     return 1;
6573
6574   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6575 }
6576
6577 /* Do the dies look the same?  */
6578
6579 static int
6580 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6581 {
6582   dw_die_ref c1, c2;
6583   dw_attr_ref a1;
6584   unsigned ix;
6585
6586   /* To avoid infinite recursion.  */
6587   if (die1->die_mark)
6588     return die1->die_mark == die2->die_mark;
6589   die1->die_mark = die2->die_mark = ++(*mark);
6590
6591   if (die1->die_tag != die2->die_tag)
6592     return 0;
6593
6594   if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6595     return 0;
6596
6597   FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6598     if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6599       return 0;
6600
6601   c1 = die1->die_child;
6602   c2 = die2->die_child;
6603   if (! c1)
6604     {
6605       if (c2)
6606         return 0;
6607     }
6608   else
6609     for (;;)
6610       {
6611         if (!same_die_p (c1, c2, mark))
6612           return 0;
6613         c1 = c1->die_sib;
6614         c2 = c2->die_sib;
6615         if (c1 == die1->die_child)
6616           {
6617             if (c2 == die2->die_child)
6618               break;
6619             else
6620               return 0;
6621           }
6622     }
6623
6624   return 1;
6625 }
6626
6627 /* Do the dies look the same?  Wrapper around same_die_p.  */
6628
6629 static int
6630 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6631 {
6632   int mark = 0;
6633   int ret = same_die_p (die1, die2, &mark);
6634
6635   unmark_all_dies (die1);
6636   unmark_all_dies (die2);
6637
6638   return ret;
6639 }
6640
6641 /* The prefix to attach to symbols on DIEs in the current comdat debug
6642    info section.  */
6643 static const char *comdat_symbol_id;
6644
6645 /* The index of the current symbol within the current comdat CU.  */
6646 static unsigned int comdat_symbol_number;
6647
6648 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6649    children, and set comdat_symbol_id accordingly.  */
6650
6651 static void
6652 compute_section_prefix (dw_die_ref unit_die)
6653 {
6654   const char *die_name = get_AT_string (unit_die, DW_AT_name);
6655   const char *base = die_name ? lbasename (die_name) : "anonymous";
6656   char *name = XALLOCAVEC (char, strlen (base) + 64);
6657   char *p;
6658   int i, mark;
6659   unsigned char checksum[16];
6660   struct md5_ctx ctx;
6661
6662   /* Compute the checksum of the DIE, then append part of it as hex digits to
6663      the name filename of the unit.  */
6664
6665   md5_init_ctx (&ctx);
6666   mark = 0;
6667   die_checksum (unit_die, &ctx, &mark);
6668   unmark_all_dies (unit_die);
6669   md5_finish_ctx (&ctx, checksum);
6670
6671   sprintf (name, "%s.", base);
6672   clean_symbol_name (name);
6673
6674   p = name + strlen (name);
6675   for (i = 0; i < 4; i++)
6676     {
6677       sprintf (p, "%.2x", checksum[i]);
6678       p += 2;
6679     }
6680
6681   comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6682   comdat_symbol_number = 0;
6683 }
6684
6685 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
6686
6687 static int
6688 is_type_die (dw_die_ref die)
6689 {
6690   switch (die->die_tag)
6691     {
6692     case DW_TAG_array_type:
6693     case DW_TAG_class_type:
6694     case DW_TAG_interface_type:
6695     case DW_TAG_enumeration_type:
6696     case DW_TAG_pointer_type:
6697     case DW_TAG_reference_type:
6698     case DW_TAG_rvalue_reference_type:
6699     case DW_TAG_string_type:
6700     case DW_TAG_structure_type:
6701     case DW_TAG_subroutine_type:
6702     case DW_TAG_union_type:
6703     case DW_TAG_ptr_to_member_type:
6704     case DW_TAG_set_type:
6705     case DW_TAG_subrange_type:
6706     case DW_TAG_base_type:
6707     case DW_TAG_const_type:
6708     case DW_TAG_file_type:
6709     case DW_TAG_packed_type:
6710     case DW_TAG_volatile_type:
6711     case DW_TAG_typedef:
6712       return 1;
6713     default:
6714       return 0;
6715     }
6716 }
6717
6718 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6719    Basically, we want to choose the bits that are likely to be shared between
6720    compilations (types) and leave out the bits that are specific to individual
6721    compilations (functions).  */
6722
6723 static int
6724 is_comdat_die (dw_die_ref c)
6725 {
6726   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6727      we do for stabs.  The advantage is a greater likelihood of sharing between
6728      objects that don't include headers in the same order (and therefore would
6729      put the base types in a different comdat).  jason 8/28/00 */
6730
6731   if (c->die_tag == DW_TAG_base_type)
6732     return 0;
6733
6734   if (c->die_tag == DW_TAG_pointer_type
6735       || c->die_tag == DW_TAG_reference_type
6736       || c->die_tag == DW_TAG_rvalue_reference_type
6737       || c->die_tag == DW_TAG_const_type
6738       || c->die_tag == DW_TAG_volatile_type)
6739     {
6740       dw_die_ref t = get_AT_ref (c, DW_AT_type);
6741
6742       return t ? is_comdat_die (t) : 0;
6743     }
6744
6745   return is_type_die (c);
6746 }
6747
6748 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6749    compilation unit.  */
6750
6751 static int
6752 is_symbol_die (dw_die_ref c)
6753 {
6754   return (is_type_die (c)
6755           || is_declaration_die (c)
6756           || c->die_tag == DW_TAG_namespace
6757           || c->die_tag == DW_TAG_module);
6758 }
6759
6760 /* Returns true iff C is a compile-unit DIE.  */
6761
6762 static inline bool
6763 is_cu_die (dw_die_ref c)
6764 {
6765   return c && c->die_tag == DW_TAG_compile_unit;
6766 }
6767
6768 /* Returns true iff C is a unit DIE of some sort.  */
6769
6770 static inline bool
6771 is_unit_die (dw_die_ref c)
6772 {
6773   return c && (c->die_tag == DW_TAG_compile_unit
6774                || c->die_tag == DW_TAG_partial_unit
6775                || c->die_tag == DW_TAG_type_unit);
6776 }
6777
6778 /* Returns true iff C is a namespace DIE.  */
6779
6780 static inline bool
6781 is_namespace_die (dw_die_ref c)
6782 {
6783   return c && c->die_tag == DW_TAG_namespace;
6784 }
6785
6786 /* Returns true iff C is a class or structure DIE.  */
6787
6788 static inline bool
6789 is_class_die (dw_die_ref c)
6790 {
6791   return c && (c->die_tag == DW_TAG_class_type
6792                || c->die_tag == DW_TAG_structure_type);
6793 }
6794
6795 /* Return non-zero if this DIE is a template parameter.  */
6796
6797 static inline bool
6798 is_template_parameter (dw_die_ref die)
6799 {
6800   switch (die->die_tag)
6801     {
6802     case DW_TAG_template_type_param:
6803     case DW_TAG_template_value_param:
6804     case DW_TAG_GNU_template_template_param:
6805     case DW_TAG_GNU_template_parameter_pack:
6806       return true;
6807     default:
6808       return false;
6809     }
6810 }
6811
6812 /* Return non-zero if this DIE represents a template instantiation.  */
6813
6814 static inline bool
6815 is_template_instantiation (dw_die_ref die)
6816 {
6817   dw_die_ref c;
6818
6819   if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
6820     return false;
6821   FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
6822   return false;
6823 }
6824
6825 static char *
6826 gen_internal_sym (const char *prefix)
6827 {
6828   char buf[256];
6829
6830   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6831   return xstrdup (buf);
6832 }
6833
6834 /* Assign symbols to all worthy DIEs under DIE.  */
6835
6836 static void
6837 assign_symbol_names (dw_die_ref die)
6838 {
6839   dw_die_ref c;
6840
6841   if (is_symbol_die (die) && !die->comdat_type_p)
6842     {
6843       if (comdat_symbol_id)
6844         {
6845           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6846
6847           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6848                    comdat_symbol_id, comdat_symbol_number++);
6849           die->die_id.die_symbol = xstrdup (p);
6850         }
6851       else
6852         die->die_id.die_symbol = gen_internal_sym ("LDIE");
6853     }
6854
6855   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6856 }
6857
6858 struct cu_hash_table_entry
6859 {
6860   dw_die_ref cu;
6861   unsigned min_comdat_num, max_comdat_num;
6862   struct cu_hash_table_entry *next;
6863 };
6864
6865 /* Helpers to manipulate hash table of CUs.  */
6866
6867 struct cu_hash_table_entry_hasher
6868 {
6869   typedef cu_hash_table_entry *value_type;
6870   typedef die_struct *compare_type;
6871   static inline hashval_t hash (const cu_hash_table_entry *);
6872   static inline bool equal (const cu_hash_table_entry *, const die_struct *);
6873   static inline void remove (cu_hash_table_entry *);
6874 };
6875
6876 inline hashval_t
6877 cu_hash_table_entry_hasher::hash (const cu_hash_table_entry *entry)
6878 {
6879   return htab_hash_string (entry->cu->die_id.die_symbol);
6880 }
6881
6882 inline bool
6883 cu_hash_table_entry_hasher::equal (const cu_hash_table_entry *entry1,
6884                                    const die_struct *entry2)
6885 {
6886   return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6887 }
6888
6889 inline void
6890 cu_hash_table_entry_hasher::remove (cu_hash_table_entry *entry)
6891 {
6892   struct cu_hash_table_entry *next;
6893
6894   while (entry)
6895     {
6896       next = entry->next;
6897       free (entry);
6898       entry = next;
6899     }
6900 }
6901
6902 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
6903
6904 /* Check whether we have already seen this CU and set up SYM_NUM
6905    accordingly.  */
6906 static int
6907 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
6908 {
6909   struct cu_hash_table_entry dummy;
6910   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6911
6912   dummy.max_comdat_num = 0;
6913
6914   slot = htable->find_slot_with_hash (cu,
6915                                       htab_hash_string (cu->die_id.die_symbol),
6916                                       INSERT);
6917   entry = *slot;
6918
6919   for (; entry; last = entry, entry = entry->next)
6920     {
6921       if (same_die_p_wrap (cu, entry->cu))
6922         break;
6923     }
6924
6925   if (entry)
6926     {
6927       *sym_num = entry->min_comdat_num;
6928       return 1;
6929     }
6930
6931   entry = XCNEW (struct cu_hash_table_entry);
6932   entry->cu = cu;
6933   entry->min_comdat_num = *sym_num = last->max_comdat_num;
6934   entry->next = *slot;
6935   *slot = entry;
6936
6937   return 0;
6938 }
6939
6940 /* Record SYM_NUM to record of CU in HTABLE.  */
6941 static void
6942 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
6943                              unsigned int sym_num)
6944 {
6945   struct cu_hash_table_entry **slot, *entry;
6946
6947   slot = htable->find_slot_with_hash (cu,
6948                                       htab_hash_string (cu->die_id.die_symbol),
6949                                       NO_INSERT);
6950   entry = *slot;
6951
6952   entry->max_comdat_num = sym_num;
6953 }
6954
6955 /* Traverse the DIE (which is always comp_unit_die), and set up
6956    additional compilation units for each of the include files we see
6957    bracketed by BINCL/EINCL.  */
6958
6959 static void
6960 break_out_includes (dw_die_ref die)
6961 {
6962   dw_die_ref c;
6963   dw_die_ref unit = NULL;
6964   limbo_die_node *node, **pnode;
6965
6966   c = die->die_child;
6967   if (c) do {
6968     dw_die_ref prev = c;
6969     c = c->die_sib;
6970     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6971            || (unit && is_comdat_die (c)))
6972       {
6973         dw_die_ref next = c->die_sib;
6974
6975         /* This DIE is for a secondary CU; remove it from the main one.  */
6976         remove_child_with_prev (c, prev);
6977
6978         if (c->die_tag == DW_TAG_GNU_BINCL)
6979           unit = push_new_compile_unit (unit, c);
6980         else if (c->die_tag == DW_TAG_GNU_EINCL)
6981           unit = pop_compile_unit (unit);
6982         else
6983           add_child_die (unit, c);
6984         c = next;
6985         if (c == die->die_child)
6986           break;
6987       }
6988   } while (c != die->die_child);
6989
6990 #if 0
6991   /* We can only use this in debugging, since the frontend doesn't check
6992      to make sure that we leave every include file we enter.  */
6993   gcc_assert (!unit);
6994 #endif
6995
6996   assign_symbol_names (die);
6997   cu_hash_type cu_hash_table (10);
6998   for (node = limbo_die_list, pnode = &limbo_die_list;
6999        node;
7000        node = node->next)
7001     {
7002       int is_dupl;
7003
7004       compute_section_prefix (node->die);
7005       is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
7006                         &comdat_symbol_number);
7007       assign_symbol_names (node->die);
7008       if (is_dupl)
7009         *pnode = node->next;
7010       else
7011         {
7012           pnode = &node->next;
7013           record_comdat_symbol_number (node->die, &cu_hash_table,
7014                 comdat_symbol_number);
7015         }
7016     }
7017 }
7018
7019 /* Return non-zero if this DIE is a declaration.  */
7020
7021 static int
7022 is_declaration_die (dw_die_ref die)
7023 {
7024   dw_attr_ref a;
7025   unsigned ix;
7026
7027   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7028     if (a->dw_attr == DW_AT_declaration)
7029       return 1;
7030
7031   return 0;
7032 }
7033
7034 /* Return non-zero if this DIE is nested inside a subprogram.  */
7035
7036 static int
7037 is_nested_in_subprogram (dw_die_ref die)
7038 {
7039   dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7040
7041   if (decl == NULL)
7042     decl = die;
7043   return local_scope_p (decl);
7044 }
7045
7046 /* Return non-zero if this DIE contains a defining declaration of a
7047    subprogram.  */
7048
7049 static int
7050 contains_subprogram_definition (dw_die_ref die)
7051 {
7052   dw_die_ref c;
7053
7054   if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7055     return 1;
7056   FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7057   return 0;
7058 }
7059
7060 /* Return non-zero if this is a type DIE that should be moved to a
7061    COMDAT .debug_types section.  */
7062
7063 static int
7064 should_move_die_to_comdat (dw_die_ref die)
7065 {
7066   switch (die->die_tag)
7067     {
7068     case DW_TAG_class_type:
7069     case DW_TAG_structure_type:
7070     case DW_TAG_enumeration_type:
7071     case DW_TAG_union_type:
7072       /* Don't move declarations, inlined instances, types nested in a
7073          subprogram, or types that contain subprogram definitions.  */
7074       if (is_declaration_die (die)
7075           || get_AT (die, DW_AT_abstract_origin)
7076           || is_nested_in_subprogram (die)
7077           || contains_subprogram_definition (die))
7078         return 0;
7079       return 1;
7080     case DW_TAG_array_type:
7081     case DW_TAG_interface_type:
7082     case DW_TAG_pointer_type:
7083     case DW_TAG_reference_type:
7084     case DW_TAG_rvalue_reference_type:
7085     case DW_TAG_string_type:
7086     case DW_TAG_subroutine_type:
7087     case DW_TAG_ptr_to_member_type:
7088     case DW_TAG_set_type:
7089     case DW_TAG_subrange_type:
7090     case DW_TAG_base_type:
7091     case DW_TAG_const_type:
7092     case DW_TAG_file_type:
7093     case DW_TAG_packed_type:
7094     case DW_TAG_volatile_type:
7095     case DW_TAG_typedef:
7096     default:
7097       return 0;
7098     }
7099 }
7100
7101 /* Make a clone of DIE.  */
7102
7103 static dw_die_ref
7104 clone_die (dw_die_ref die)
7105 {
7106   dw_die_ref clone;
7107   dw_attr_ref a;
7108   unsigned ix;
7109
7110   clone = ggc_cleared_alloc<die_node> ();
7111   clone->die_tag = die->die_tag;
7112
7113   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7114     add_dwarf_attr (clone, a);
7115
7116   return clone;
7117 }
7118
7119 /* Make a clone of the tree rooted at DIE.  */
7120
7121 static dw_die_ref
7122 clone_tree (dw_die_ref die)
7123 {
7124   dw_die_ref c;
7125   dw_die_ref clone = clone_die (die);
7126
7127   FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7128
7129   return clone;
7130 }
7131
7132 /* Make a clone of DIE as a declaration.  */
7133
7134 static dw_die_ref
7135 clone_as_declaration (dw_die_ref die)
7136 {
7137   dw_die_ref clone;
7138   dw_die_ref decl;
7139   dw_attr_ref a;
7140   unsigned ix;
7141
7142   /* If the DIE is already a declaration, just clone it.  */
7143   if (is_declaration_die (die))
7144     return clone_die (die);
7145
7146   /* If the DIE is a specification, just clone its declaration DIE.  */
7147   decl = get_AT_ref (die, DW_AT_specification);
7148   if (decl != NULL)
7149     {
7150       clone = clone_die (decl);
7151       if (die->comdat_type_p)
7152         add_AT_die_ref (clone, DW_AT_signature, die);
7153       return clone;
7154     }
7155
7156   clone = ggc_cleared_alloc<die_node> ();
7157   clone->die_tag = die->die_tag;
7158
7159   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7160     {
7161       /* We don't want to copy over all attributes.
7162          For example we don't want DW_AT_byte_size because otherwise we will no
7163          longer have a declaration and GDB will treat it as a definition.  */
7164
7165       switch (a->dw_attr)
7166         {
7167         case DW_AT_abstract_origin:
7168         case DW_AT_artificial:
7169         case DW_AT_containing_type:
7170         case DW_AT_external:
7171         case DW_AT_name:
7172         case DW_AT_type:
7173         case DW_AT_virtuality:
7174         case DW_AT_linkage_name:
7175         case DW_AT_MIPS_linkage_name:
7176           add_dwarf_attr (clone, a);
7177           break;
7178         case DW_AT_byte_size:
7179         default:
7180           break;
7181         }
7182     }
7183
7184   if (die->comdat_type_p)
7185     add_AT_die_ref (clone, DW_AT_signature, die);
7186
7187   add_AT_flag (clone, DW_AT_declaration, 1);
7188   return clone;
7189 }
7190
7191
7192 /* Structure to map a DIE in one CU to its copy in a comdat type unit.  */
7193
7194 struct decl_table_entry
7195 {
7196   dw_die_ref orig;
7197   dw_die_ref copy;
7198 };
7199
7200 /* Helpers to manipulate hash table of copied declarations.  */
7201
7202 /* Hashtable helpers.  */
7203
7204 struct decl_table_entry_hasher : typed_free_remove <decl_table_entry>
7205 {
7206   typedef decl_table_entry *value_type;
7207   typedef die_struct *compare_type;
7208   static inline hashval_t hash (const decl_table_entry *);
7209   static inline bool equal (const decl_table_entry *, const die_struct *);
7210 };
7211
7212 inline hashval_t
7213 decl_table_entry_hasher::hash (const decl_table_entry *entry)
7214 {
7215   return htab_hash_pointer (entry->orig);
7216 }
7217
7218 inline bool
7219 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
7220                                 const die_struct *entry2)
7221 {
7222   return entry1->orig == entry2;
7223 }
7224
7225 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7226
7227 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7228    or type unit entry, to a new tree.  Adds the new tree to UNIT and returns
7229    a pointer to the copy of DIE.  If DECL_TABLE is provided, it is used
7230    to check if the ancestor has already been copied into UNIT.  */
7231
7232 static dw_die_ref
7233 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7234                     decl_hash_type *decl_table)
7235 {
7236   dw_die_ref parent = die->die_parent;
7237   dw_die_ref new_parent = unit;
7238   dw_die_ref copy;
7239   decl_table_entry **slot = NULL;
7240   struct decl_table_entry *entry = NULL;
7241
7242   if (decl_table)
7243     {
7244       /* Check if the entry has already been copied to UNIT.  */
7245       slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7246                                               INSERT);
7247       if (*slot != HTAB_EMPTY_ENTRY)
7248         {
7249           entry = *slot;
7250           return entry->copy;
7251         }
7252
7253       /* Record in DECL_TABLE that DIE has been copied to UNIT.  */
7254       entry = XCNEW (struct decl_table_entry);
7255       entry->orig = die;
7256       entry->copy = NULL;
7257       *slot = entry;
7258     }
7259
7260   if (parent != NULL)
7261     {
7262       dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7263       if (spec != NULL)
7264         parent = spec;
7265       if (!is_unit_die (parent))
7266         new_parent = copy_ancestor_tree (unit, parent, decl_table);
7267     }
7268
7269   copy = clone_as_declaration (die);
7270   add_child_die (new_parent, copy);
7271
7272   if (decl_table)
7273     {
7274       /* Record the pointer to the copy.  */
7275       entry->copy = copy;
7276     }
7277
7278   return copy;
7279 }
7280 /* Copy the declaration context to the new type unit DIE.  This includes
7281    any surrounding namespace or type declarations.  If the DIE has an
7282    AT_specification attribute, it also includes attributes and children
7283    attached to the specification, and returns a pointer to the original
7284    parent of the declaration DIE.  Returns NULL otherwise.  */
7285
7286 static dw_die_ref
7287 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7288 {
7289   dw_die_ref decl;
7290   dw_die_ref new_decl;
7291   dw_die_ref orig_parent = NULL;
7292
7293   decl = get_AT_ref (die, DW_AT_specification);
7294   if (decl == NULL)
7295     decl = die;
7296   else
7297     {
7298       unsigned ix;
7299       dw_die_ref c;
7300       dw_attr_ref a;
7301
7302       /* The original DIE will be changed to a declaration, and must
7303          be moved to be a child of the original declaration DIE.  */
7304       orig_parent = decl->die_parent;
7305
7306       /* Copy the type node pointer from the new DIE to the original
7307          declaration DIE so we can forward references later.  */
7308       decl->comdat_type_p = true;
7309       decl->die_id.die_type_node = die->die_id.die_type_node;
7310
7311       remove_AT (die, DW_AT_specification);
7312
7313       FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7314         {
7315           if (a->dw_attr != DW_AT_name
7316               && a->dw_attr != DW_AT_declaration
7317               && a->dw_attr != DW_AT_external)
7318             add_dwarf_attr (die, a);
7319         }
7320
7321       FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7322     }
7323
7324   if (decl->die_parent != NULL
7325       && !is_unit_die (decl->die_parent))
7326     {
7327       new_decl = copy_ancestor_tree (unit, decl, NULL);
7328       if (new_decl != NULL)
7329         {
7330           remove_AT (new_decl, DW_AT_signature);
7331           add_AT_specification (die, new_decl);
7332         }
7333     }
7334
7335   return orig_parent;
7336 }
7337
7338 /* Generate the skeleton ancestor tree for the given NODE, then clone
7339    the DIE and add the clone into the tree.  */
7340
7341 static void
7342 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7343 {
7344   if (node->new_die != NULL)
7345     return;
7346
7347   node->new_die = clone_as_declaration (node->old_die);
7348
7349   if (node->parent != NULL)
7350     {
7351       generate_skeleton_ancestor_tree (node->parent);
7352       add_child_die (node->parent->new_die, node->new_die);
7353     }
7354 }
7355
7356 /* Generate a skeleton tree of DIEs containing any declarations that are
7357    found in the original tree.  We traverse the tree looking for declaration
7358    DIEs, and construct the skeleton from the bottom up whenever we find one.  */
7359
7360 static void
7361 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7362 {
7363   skeleton_chain_node node;
7364   dw_die_ref c;
7365   dw_die_ref first;
7366   dw_die_ref prev = NULL;
7367   dw_die_ref next = NULL;
7368
7369   node.parent = parent;
7370
7371   first = c = parent->old_die->die_child;
7372   if (c)
7373     next = c->die_sib;
7374   if (c) do {
7375     if (prev == NULL || prev->die_sib == c)
7376       prev = c;
7377     c = next;
7378     next = (c == first ? NULL : c->die_sib);
7379     node.old_die = c;
7380     node.new_die = NULL;
7381     if (is_declaration_die (c))
7382       {
7383         if (is_template_instantiation (c))
7384           {
7385             /* Instantiated templates do not need to be cloned into the
7386                type unit.  Just move the DIE and its children back to
7387                the skeleton tree (in the main CU).  */
7388             remove_child_with_prev (c, prev);
7389             add_child_die (parent->new_die, c);
7390             c = prev;
7391           }
7392         else
7393           {
7394             /* Clone the existing DIE, move the original to the skeleton
7395                tree (which is in the main CU), and put the clone, with
7396                all the original's children, where the original came from
7397                (which is about to be moved to the type unit).  */
7398             dw_die_ref clone = clone_die (c);
7399             move_all_children (c, clone);
7400
7401             /* If the original has a DW_AT_object_pointer attribute,
7402                it would now point to a child DIE just moved to the
7403                cloned tree, so we need to remove that attribute from
7404                the original.  */
7405             remove_AT (c, DW_AT_object_pointer);
7406
7407             replace_child (c, clone, prev);
7408             generate_skeleton_ancestor_tree (parent);
7409             add_child_die (parent->new_die, c);
7410             node.new_die = c;
7411             c = clone;
7412           }
7413       }
7414     generate_skeleton_bottom_up (&node);
7415   } while (next != NULL);
7416 }
7417
7418 /* Wrapper function for generate_skeleton_bottom_up.  */
7419
7420 static dw_die_ref
7421 generate_skeleton (dw_die_ref die)
7422 {
7423   skeleton_chain_node node;
7424
7425   node.old_die = die;
7426   node.new_die = NULL;
7427   node.parent = NULL;
7428
7429   /* If this type definition is nested inside another type,
7430      and is not an instantiation of a template, always leave
7431      at least a declaration in its place.  */
7432   if (die->die_parent != NULL
7433       && is_type_die (die->die_parent)
7434       && !is_template_instantiation (die))
7435     node.new_die = clone_as_declaration (die);
7436
7437   generate_skeleton_bottom_up (&node);
7438   return node.new_die;
7439 }
7440
7441 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7442    declaration.  The original DIE is moved to a new compile unit so that
7443    existing references to it follow it to the new location.  If any of the
7444    original DIE's descendants is a declaration, we need to replace the
7445    original DIE with a skeleton tree and move the declarations back into the
7446    skeleton tree.  */
7447
7448 static dw_die_ref
7449 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7450                                        dw_die_ref prev)
7451 {
7452   dw_die_ref skeleton, orig_parent;
7453
7454   /* Copy the declaration context to the type unit DIE.  If the returned
7455      ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7456      that DIE.  */
7457   orig_parent = copy_declaration_context (unit, child);
7458
7459   skeleton = generate_skeleton (child);
7460   if (skeleton == NULL)
7461     remove_child_with_prev (child, prev);
7462   else
7463     {
7464       skeleton->comdat_type_p = true;
7465       skeleton->die_id.die_type_node = child->die_id.die_type_node;
7466
7467       /* If the original DIE was a specification, we need to put
7468          the skeleton under the parent DIE of the declaration.
7469          This leaves the original declaration in the tree, but
7470          it will be pruned later since there are no longer any
7471          references to it.  */
7472       if (orig_parent != NULL)
7473         {
7474           remove_child_with_prev (child, prev);
7475           add_child_die (orig_parent, skeleton);
7476         }
7477       else
7478         replace_child (child, skeleton, prev);
7479     }
7480
7481   return skeleton;
7482 }
7483
7484 /* Traverse the DIE and set up additional .debug_types sections for each
7485    type worthy of being placed in a COMDAT section.  */
7486
7487 static void
7488 break_out_comdat_types (dw_die_ref die)
7489 {
7490   dw_die_ref c;
7491   dw_die_ref first;
7492   dw_die_ref prev = NULL;
7493   dw_die_ref next = NULL;
7494   dw_die_ref unit = NULL;
7495
7496   first = c = die->die_child;
7497   if (c)
7498     next = c->die_sib;
7499   if (c) do {
7500     if (prev == NULL || prev->die_sib == c)
7501       prev = c;
7502     c = next;
7503     next = (c == first ? NULL : c->die_sib);
7504     if (should_move_die_to_comdat (c))
7505       {
7506         dw_die_ref replacement;
7507         comdat_type_node_ref type_node;
7508
7509         /* Break out nested types into their own type units.  */
7510         break_out_comdat_types (c);
7511
7512         /* Create a new type unit DIE as the root for the new tree, and
7513            add it to the list of comdat types.  */
7514         unit = new_die (DW_TAG_type_unit, NULL, NULL);
7515         add_AT_unsigned (unit, DW_AT_language,
7516                          get_AT_unsigned (comp_unit_die (), DW_AT_language));
7517         type_node = ggc_cleared_alloc<comdat_type_node> ();
7518         type_node->root_die = unit;
7519         type_node->next = comdat_type_list;
7520         comdat_type_list = type_node;
7521
7522         /* Generate the type signature.  */
7523         generate_type_signature (c, type_node);
7524
7525         /* Copy the declaration context, attributes, and children of the
7526            declaration into the new type unit DIE, then remove this DIE
7527            from the main CU (or replace it with a skeleton if necessary).  */
7528         replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7529         type_node->skeleton_die = replacement;
7530
7531         /* Add the DIE to the new compunit.  */
7532         add_child_die (unit, c);
7533
7534         if (replacement != NULL)
7535           c = replacement;
7536       }
7537     else if (c->die_tag == DW_TAG_namespace
7538              || c->die_tag == DW_TAG_class_type
7539              || c->die_tag == DW_TAG_structure_type
7540              || c->die_tag == DW_TAG_union_type)
7541       {
7542         /* Look for nested types that can be broken out.  */
7543         break_out_comdat_types (c);
7544       }
7545   } while (next != NULL);
7546 }
7547
7548 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
7549    Enter all the cloned children into the hash table decl_table.  */
7550
7551 static dw_die_ref
7552 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
7553 {
7554   dw_die_ref c;
7555   dw_die_ref clone;
7556   struct decl_table_entry *entry;
7557   decl_table_entry **slot;
7558
7559   if (die->die_tag == DW_TAG_subprogram)
7560     clone = clone_as_declaration (die);
7561   else
7562     clone = clone_die (die);
7563
7564   slot = decl_table->find_slot_with_hash (die,
7565                                           htab_hash_pointer (die), INSERT);
7566
7567   /* Assert that DIE isn't in the hash table yet.  If it would be there
7568      before, the ancestors would be necessarily there as well, therefore
7569      clone_tree_partial wouldn't be called.  */
7570   gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7571
7572   entry = XCNEW (struct decl_table_entry);
7573   entry->orig = die;
7574   entry->copy = clone;
7575   *slot = entry;
7576
7577   if (die->die_tag != DW_TAG_subprogram)
7578     FOR_EACH_CHILD (die, c,
7579                     add_child_die (clone, clone_tree_partial (c, decl_table)));
7580
7581   return clone;
7582 }
7583
7584 /* Walk the DIE and its children, looking for references to incomplete
7585    or trivial types that are unmarked (i.e., that are not in the current
7586    type_unit).  */
7587
7588 static void
7589 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
7590 {
7591   dw_die_ref c;
7592   dw_attr_ref a;
7593   unsigned ix;
7594
7595   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7596     {
7597       if (AT_class (a) == dw_val_class_die_ref)
7598         {
7599           dw_die_ref targ = AT_ref (a);
7600           decl_table_entry **slot;
7601           struct decl_table_entry *entry;
7602
7603           if (targ->die_mark != 0 || targ->comdat_type_p)
7604             continue;
7605
7606           slot = decl_table->find_slot_with_hash (targ,
7607                                                   htab_hash_pointer (targ),
7608                                                   INSERT);
7609
7610           if (*slot != HTAB_EMPTY_ENTRY)
7611             {
7612               /* TARG has already been copied, so we just need to
7613                  modify the reference to point to the copy.  */
7614               entry = *slot;
7615               a->dw_attr_val.v.val_die_ref.die = entry->copy;
7616             }
7617           else
7618             {
7619               dw_die_ref parent = unit;
7620               dw_die_ref copy = clone_die (targ);
7621
7622               /* Record in DECL_TABLE that TARG has been copied.
7623                  Need to do this now, before the recursive call,
7624                  because DECL_TABLE may be expanded and SLOT
7625                  would no longer be a valid pointer.  */
7626               entry = XCNEW (struct decl_table_entry);
7627               entry->orig = targ;
7628               entry->copy = copy;
7629               *slot = entry;
7630
7631               /* If TARG is not a declaration DIE, we need to copy its
7632                  children.  */
7633               if (!is_declaration_die (targ))
7634                 {
7635                   FOR_EACH_CHILD (
7636                       targ, c,
7637                       add_child_die (copy,
7638                                      clone_tree_partial (c, decl_table)));
7639                 }
7640
7641               /* Make sure the cloned tree is marked as part of the
7642                  type unit.  */
7643               mark_dies (copy);
7644
7645               /* If TARG has surrounding context, copy its ancestor tree
7646                  into the new type unit.  */
7647               if (targ->die_parent != NULL
7648                   && !is_unit_die (targ->die_parent))
7649                 parent = copy_ancestor_tree (unit, targ->die_parent,
7650                                              decl_table);
7651
7652               add_child_die (parent, copy);
7653               a->dw_attr_val.v.val_die_ref.die = copy;
7654
7655               /* Make sure the newly-copied DIE is walked.  If it was
7656                  installed in a previously-added context, it won't
7657                  get visited otherwise.  */
7658               if (parent != unit)
7659                 {
7660                   /* Find the highest point of the newly-added tree,
7661                      mark each node along the way, and walk from there.  */
7662                   parent->die_mark = 1;
7663                   while (parent->die_parent
7664                          && parent->die_parent->die_mark == 0)
7665                     {
7666                       parent = parent->die_parent;
7667                       parent->die_mark = 1;
7668                     }
7669                   copy_decls_walk (unit, parent, decl_table);
7670                 }
7671             }
7672         }
7673     }
7674
7675   FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7676 }
7677
7678 /* Copy declarations for "unworthy" types into the new comdat section.
7679    Incomplete types, modified types, and certain other types aren't broken
7680    out into comdat sections of their own, so they don't have a signature,
7681    and we need to copy the declaration into the same section so that we
7682    don't have an external reference.  */
7683
7684 static void
7685 copy_decls_for_unworthy_types (dw_die_ref unit)
7686 {
7687   mark_dies (unit);
7688   decl_hash_type decl_table (10);
7689   copy_decls_walk (unit, unit, &decl_table);
7690   unmark_dies (unit);
7691 }
7692
7693 /* Traverse the DIE and add a sibling attribute if it may have the
7694    effect of speeding up access to siblings.  To save some space,
7695    avoid generating sibling attributes for DIE's without children.  */
7696
7697 static void
7698 add_sibling_attributes (dw_die_ref die)
7699 {
7700   dw_die_ref c;
7701
7702   if (! die->die_child)
7703     return;
7704
7705   if (die->die_parent && die != die->die_parent->die_child)
7706     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7707
7708   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7709 }
7710
7711 /* Output all location lists for the DIE and its children.  */
7712
7713 static void
7714 output_location_lists (dw_die_ref die)
7715 {
7716   dw_die_ref c;
7717   dw_attr_ref a;
7718   unsigned ix;
7719
7720   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7721     if (AT_class (a) == dw_val_class_loc_list)
7722       output_loc_list (AT_loc_list (a));
7723
7724   FOR_EACH_CHILD (die, c, output_location_lists (c));
7725 }
7726
7727 /* We want to limit the number of external references, because they are
7728    larger than local references: a relocation takes multiple words, and
7729    even a sig8 reference is always eight bytes, whereas a local reference
7730    can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7731    So if we encounter multiple external references to the same type DIE, we
7732    make a local typedef stub for it and redirect all references there.
7733
7734    This is the element of the hash table for keeping track of these
7735    references.  */
7736
7737 struct external_ref
7738 {
7739   dw_die_ref type;
7740   dw_die_ref stub;
7741   unsigned n_refs;
7742 };
7743
7744 /* Hashtable helpers.  */
7745
7746 struct external_ref_hasher : typed_free_remove <external_ref>
7747 {
7748   typedef external_ref *value_type;
7749   typedef external_ref *compare_type;
7750   static inline hashval_t hash (const external_ref *);
7751   static inline bool equal (const external_ref *, const external_ref *);
7752 };
7753
7754 inline hashval_t
7755 external_ref_hasher::hash (const external_ref *r)
7756 {
7757   dw_die_ref die = r->type;
7758   hashval_t h = 0;
7759
7760   /* We can't use the address of the DIE for hashing, because
7761      that will make the order of the stub DIEs non-deterministic.  */
7762   if (! die->comdat_type_p)
7763     /* We have a symbol; use it to compute a hash.  */
7764     h = htab_hash_string (die->die_id.die_symbol);
7765   else
7766     {
7767       /* We have a type signature; use a subset of the bits as the hash.
7768          The 8-byte signature is at least as large as hashval_t.  */
7769       comdat_type_node_ref type_node = die->die_id.die_type_node;
7770       memcpy (&h, type_node->signature, sizeof (h));
7771     }
7772   return h;
7773 }
7774
7775 inline bool
7776 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
7777 {
7778   return r1->type == r2->type;
7779 }
7780
7781 typedef hash_table<external_ref_hasher> external_ref_hash_type;
7782
7783 /* Return a pointer to the external_ref for references to DIE.  */
7784
7785 static struct external_ref *
7786 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
7787 {
7788   struct external_ref ref, *ref_p;
7789   external_ref **slot;
7790
7791   ref.type = die;
7792   slot = map->find_slot (&ref, INSERT);
7793   if (*slot != HTAB_EMPTY_ENTRY)
7794     return *slot;
7795
7796   ref_p = XCNEW (struct external_ref);
7797   ref_p->type = die;
7798   *slot = ref_p;
7799   return ref_p;
7800 }
7801
7802 /* Subroutine of optimize_external_refs, below.
7803
7804    If we see a type skeleton, record it as our stub.  If we see external
7805    references, remember how many we've seen.  */
7806
7807 static void
7808 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
7809 {
7810   dw_die_ref c;
7811   dw_attr_ref a;
7812   unsigned ix;
7813   struct external_ref *ref_p;
7814
7815   if (is_type_die (die)
7816       && (c = get_AT_ref (die, DW_AT_signature)))
7817     {
7818       /* This is a local skeleton; use it for local references.  */
7819       ref_p = lookup_external_ref (map, c);
7820       ref_p->stub = die;
7821     }
7822
7823   /* Scan the DIE references, and remember any that refer to DIEs from
7824      other CUs (i.e. those which are not marked).  */
7825   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7826     if (AT_class (a) == dw_val_class_die_ref
7827         && (c = AT_ref (a))->die_mark == 0
7828         && is_type_die (c))
7829       {
7830         ref_p = lookup_external_ref (map, c);
7831         ref_p->n_refs++;
7832       }
7833
7834   FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7835 }
7836
7837 /* htab_traverse callback function for optimize_external_refs, below.  SLOT
7838    points to an external_ref, DATA is the CU we're processing.  If we don't
7839    already have a local stub, and we have multiple refs, build a stub.  */
7840
7841 int
7842 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
7843 {
7844   struct external_ref *ref_p = *slot;
7845
7846   if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7847     {
7848       /* We have multiple references to this type, so build a small stub.
7849          Both of these forms are a bit dodgy from the perspective of the
7850          DWARF standard, since technically they should have names.  */
7851       dw_die_ref cu = data;
7852       dw_die_ref type = ref_p->type;
7853       dw_die_ref stub = NULL;
7854
7855       if (type->comdat_type_p)
7856         {
7857           /* If we refer to this type via sig8, use AT_signature.  */
7858           stub = new_die (type->die_tag, cu, NULL_TREE);
7859           add_AT_die_ref (stub, DW_AT_signature, type);
7860         }
7861       else
7862         {
7863           /* Otherwise, use a typedef with no name.  */
7864           stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7865           add_AT_die_ref (stub, DW_AT_type, type);
7866         }
7867
7868       stub->die_mark++;
7869       ref_p->stub = stub;
7870     }
7871   return 1;
7872 }
7873
7874 /* DIE is a unit; look through all the DIE references to see if there are
7875    any external references to types, and if so, create local stubs for
7876    them which will be applied in build_abbrev_table.  This is useful because
7877    references to local DIEs are smaller.  */
7878
7879 static external_ref_hash_type *
7880 optimize_external_refs (dw_die_ref die)
7881 {
7882   external_ref_hash_type *map = new external_ref_hash_type (10);
7883   optimize_external_refs_1 (die, map);
7884   map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
7885   return map;
7886 }
7887
7888 /* The format of each DIE (and its attribute value pairs) is encoded in an
7889    abbreviation table.  This routine builds the abbreviation table and assigns
7890    a unique abbreviation id for each abbreviation entry.  The children of each
7891    die are visited recursively.  */
7892
7893 static void
7894 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
7895 {
7896   unsigned long abbrev_id;
7897   unsigned int n_alloc;
7898   dw_die_ref c;
7899   dw_attr_ref a;
7900   unsigned ix;
7901
7902   /* Scan the DIE references, and replace any that refer to
7903      DIEs from other CUs (i.e. those which are not marked) with
7904      the local stubs we built in optimize_external_refs.  */
7905   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7906     if (AT_class (a) == dw_val_class_die_ref
7907         && (c = AT_ref (a))->die_mark == 0)
7908       {
7909         struct external_ref *ref_p;
7910         gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
7911
7912         ref_p = lookup_external_ref (extern_map, c);
7913         if (ref_p->stub && ref_p->stub != die)
7914           change_AT_die_ref (a, ref_p->stub);
7915         else
7916           /* We aren't changing this reference, so mark it external.  */
7917           set_AT_ref_external (a, 1);
7918       }
7919
7920   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7921     {
7922       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7923       dw_attr_ref die_a, abbrev_a;
7924       unsigned ix;
7925       bool ok = true;
7926
7927       if (abbrev->die_tag != die->die_tag)
7928         continue;
7929       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7930         continue;
7931
7932       if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
7933         continue;
7934
7935       FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
7936         {
7937           abbrev_a = &(*abbrev->die_attr)[ix];
7938           if ((abbrev_a->dw_attr != die_a->dw_attr)
7939               || (value_format (abbrev_a) != value_format (die_a)))
7940             {
7941               ok = false;
7942               break;
7943             }
7944         }
7945       if (ok)
7946         break;
7947     }
7948
7949   if (abbrev_id >= abbrev_die_table_in_use)
7950     {
7951       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7952         {
7953           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7954           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7955                                             n_alloc);
7956
7957           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7958                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7959           abbrev_die_table_allocated = n_alloc;
7960         }
7961
7962       ++abbrev_die_table_in_use;
7963       abbrev_die_table[abbrev_id] = die;
7964     }
7965
7966   die->die_abbrev = abbrev_id;
7967   FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
7968 }
7969 \f
7970 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
7971
7972 static int
7973 constant_size (unsigned HOST_WIDE_INT value)
7974 {
7975   int log;
7976
7977   if (value == 0)
7978     log = 0;
7979   else
7980     log = floor_log2 (value);
7981
7982   log = log / 8;
7983   log = 1 << (floor_log2 (log) + 1);
7984
7985   return log;
7986 }
7987
7988 /* Return the size of a DIE as it is represented in the
7989    .debug_info section.  */
7990
7991 static unsigned long
7992 size_of_die (dw_die_ref die)
7993 {
7994   unsigned long size = 0;
7995   dw_attr_ref a;
7996   unsigned ix;
7997   enum dwarf_form form;
7998
7999   size += size_of_uleb128 (die->die_abbrev);
8000   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8001     {
8002       switch (AT_class (a))
8003         {
8004         case dw_val_class_addr:
8005           if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8006             {
8007               gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8008               size += size_of_uleb128 (AT_index (a));
8009             }
8010           else
8011             size += DWARF2_ADDR_SIZE;
8012           break;
8013         case dw_val_class_offset:
8014           size += DWARF_OFFSET_SIZE;
8015           break;
8016         case dw_val_class_loc:
8017           {
8018             unsigned long lsize = size_of_locs (AT_loc (a));
8019
8020             /* Block length.  */
8021             if (dwarf_version >= 4)
8022               size += size_of_uleb128 (lsize);
8023             else
8024               size += constant_size (lsize);
8025             size += lsize;
8026           }
8027           break;
8028         case dw_val_class_loc_list:
8029           if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8030             {
8031               gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8032               size += size_of_uleb128 (AT_index (a));
8033             }
8034           else
8035             size += DWARF_OFFSET_SIZE;
8036           break;
8037         case dw_val_class_range_list:
8038           size += DWARF_OFFSET_SIZE;
8039           break;
8040         case dw_val_class_const:
8041           size += size_of_sleb128 (AT_int (a));
8042           break;
8043         case dw_val_class_unsigned_const:
8044           {
8045             int csize = constant_size (AT_unsigned (a));
8046             if (dwarf_version == 3
8047                 && a->dw_attr == DW_AT_data_member_location
8048                 && csize >= 4)
8049               size += size_of_uleb128 (AT_unsigned (a));
8050             else
8051               size += csize;
8052           }
8053           break;
8054         case dw_val_class_const_double:
8055           size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
8056           if (HOST_BITS_PER_WIDE_INT >= 64)
8057             size++; /* block */
8058           break;
8059         case dw_val_class_wide_int:
8060           size += (get_full_len (*a->dw_attr_val.v.val_wide)
8061                    * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
8062           if (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT
8063               > 64)
8064             size++; /* block */
8065           break;
8066         case dw_val_class_vec:
8067           size += constant_size (a->dw_attr_val.v.val_vec.length
8068                                  * a->dw_attr_val.v.val_vec.elt_size)
8069                   + a->dw_attr_val.v.val_vec.length
8070                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
8071           break;
8072         case dw_val_class_flag:
8073           if (dwarf_version >= 4)
8074             /* Currently all add_AT_flag calls pass in 1 as last argument,
8075                so DW_FORM_flag_present can be used.  If that ever changes,
8076                we'll need to use DW_FORM_flag and have some optimization
8077                in build_abbrev_table that will change those to
8078                DW_FORM_flag_present if it is set to 1 in all DIEs using
8079                the same abbrev entry.  */
8080             gcc_assert (a->dw_attr_val.v.val_flag == 1);
8081           else
8082             size += 1;
8083           break;
8084         case dw_val_class_die_ref:
8085           if (AT_ref_external (a))
8086             {
8087               /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
8088                  we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
8089                  is sized by target address length, whereas in DWARF3
8090                  it's always sized as an offset.  */
8091               if (use_debug_types)
8092                 size += DWARF_TYPE_SIGNATURE_SIZE;
8093               else if (dwarf_version == 2)
8094                 size += DWARF2_ADDR_SIZE;
8095               else
8096                 size += DWARF_OFFSET_SIZE;
8097             }
8098           else
8099             size += DWARF_OFFSET_SIZE;
8100           break;
8101         case dw_val_class_fde_ref:
8102           size += DWARF_OFFSET_SIZE;
8103           break;
8104         case dw_val_class_lbl_id:
8105           if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8106             {
8107               gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8108               size += size_of_uleb128 (AT_index (a));
8109             }
8110           else
8111             size += DWARF2_ADDR_SIZE;
8112           break;
8113         case dw_val_class_lineptr:
8114         case dw_val_class_macptr:
8115           size += DWARF_OFFSET_SIZE;
8116           break;
8117         case dw_val_class_str:
8118           form = AT_string_form (a);
8119           if (form == DW_FORM_strp)
8120             size += DWARF_OFFSET_SIZE;
8121          else if (form == DW_FORM_GNU_str_index)
8122             size += size_of_uleb128 (AT_index (a));
8123           else
8124             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8125           break;
8126         case dw_val_class_file:
8127           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8128           break;
8129         case dw_val_class_data8:
8130           size += 8;
8131           break;
8132         case dw_val_class_vms_delta:
8133           size += DWARF_OFFSET_SIZE;
8134           break;
8135         case dw_val_class_high_pc:
8136           size += DWARF2_ADDR_SIZE;
8137           break;
8138         default:
8139           gcc_unreachable ();
8140         }
8141     }
8142
8143   return size;
8144 }
8145
8146 /* Size the debugging information associated with a given DIE.  Visits the
8147    DIE's children recursively.  Updates the global variable next_die_offset, on
8148    each time through.  Uses the current value of next_die_offset to update the
8149    die_offset field in each DIE.  */
8150
8151 static void
8152 calc_die_sizes (dw_die_ref die)
8153 {
8154   dw_die_ref c;
8155
8156   gcc_assert (die->die_offset == 0
8157               || (unsigned long int) die->die_offset == next_die_offset);
8158   die->die_offset = next_die_offset;
8159   next_die_offset += size_of_die (die);
8160
8161   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8162
8163   if (die->die_child != NULL)
8164     /* Count the null byte used to terminate sibling lists.  */
8165     next_die_offset += 1;
8166 }
8167
8168 /* Size just the base type children at the start of the CU.
8169    This is needed because build_abbrev needs to size locs
8170    and sizing of type based stack ops needs to know die_offset
8171    values for the base types.  */
8172
8173 static void
8174 calc_base_type_die_sizes (void)
8175 {
8176   unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8177   unsigned int i;
8178   dw_die_ref base_type;
8179 #if ENABLE_ASSERT_CHECKING
8180   dw_die_ref prev = comp_unit_die ()->die_child;
8181 #endif
8182
8183   die_offset += size_of_die (comp_unit_die ());
8184   for (i = 0; base_types.iterate (i, &base_type); i++)
8185     {
8186 #if ENABLE_ASSERT_CHECKING
8187       gcc_assert (base_type->die_offset == 0
8188                   && prev->die_sib == base_type
8189                   && base_type->die_child == NULL
8190                   && base_type->die_abbrev);
8191       prev = base_type;
8192 #endif
8193       base_type->die_offset = die_offset;
8194       die_offset += size_of_die (base_type);
8195     }
8196 }
8197
8198 /* Set the marks for a die and its children.  We do this so
8199    that we know whether or not a reference needs to use FORM_ref_addr; only
8200    DIEs in the same CU will be marked.  We used to clear out the offset
8201    and use that as the flag, but ran into ordering problems.  */
8202
8203 static void
8204 mark_dies (dw_die_ref die)
8205 {
8206   dw_die_ref c;
8207
8208   gcc_assert (!die->die_mark);
8209
8210   die->die_mark = 1;
8211   FOR_EACH_CHILD (die, c, mark_dies (c));
8212 }
8213
8214 /* Clear the marks for a die and its children.  */
8215
8216 static void
8217 unmark_dies (dw_die_ref die)
8218 {
8219   dw_die_ref c;
8220
8221   if (! use_debug_types)
8222     gcc_assert (die->die_mark);
8223
8224   die->die_mark = 0;
8225   FOR_EACH_CHILD (die, c, unmark_dies (c));
8226 }
8227
8228 /* Clear the marks for a die, its children and referred dies.  */
8229
8230 static void
8231 unmark_all_dies (dw_die_ref die)
8232 {
8233   dw_die_ref c;
8234   dw_attr_ref a;
8235   unsigned ix;
8236
8237   if (!die->die_mark)
8238     return;
8239   die->die_mark = 0;
8240
8241   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8242
8243   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8244     if (AT_class (a) == dw_val_class_die_ref)
8245       unmark_all_dies (AT_ref (a));
8246 }
8247
8248 /* Calculate if the entry should appear in the final output file.  It may be
8249    from a pruned a type.  */
8250
8251 static bool
8252 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
8253 {
8254   /* By limiting gnu pubnames to definitions only, gold can generate a
8255      gdb index without entries for declarations, which don't include
8256      enough information to be useful.  */
8257   if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
8258     return false;
8259
8260   if (table == pubname_table)
8261     {
8262       /* Enumerator names are part of the pubname table, but the
8263          parent DW_TAG_enumeration_type die may have been pruned.
8264          Don't output them if that is the case.  */
8265       if (p->die->die_tag == DW_TAG_enumerator &&
8266           (p->die->die_parent == NULL
8267            || !p->die->die_parent->die_perennial_p))
8268         return false;
8269
8270       /* Everything else in the pubname table is included.  */
8271       return true;
8272     }
8273
8274   /* The pubtypes table shouldn't include types that have been
8275      pruned.  */
8276   return (p->die->die_offset != 0
8277           || !flag_eliminate_unused_debug_types);
8278 }
8279
8280 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8281    generated for the compilation unit.  */
8282
8283 static unsigned long
8284 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8285 {
8286   unsigned long size;
8287   unsigned i;
8288   pubname_ref p;
8289   int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8290
8291   size = DWARF_PUBNAMES_HEADER_SIZE;
8292   FOR_EACH_VEC_ELT (*names, i, p)
8293     if (include_pubname_in_output (names, p))
8294       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8295
8296   size += DWARF_OFFSET_SIZE;
8297   return size;
8298 }
8299
8300 /* Return the size of the information in the .debug_aranges section.  */
8301
8302 static unsigned long
8303 size_of_aranges (void)
8304 {
8305   unsigned long size;
8306
8307   size = DWARF_ARANGES_HEADER_SIZE;
8308
8309   /* Count the address/length pair for this compilation unit.  */
8310   if (text_section_used)
8311     size += 2 * DWARF2_ADDR_SIZE;
8312   if (cold_text_section_used)
8313     size += 2 * DWARF2_ADDR_SIZE;
8314   if (have_multiple_function_sections)
8315     {
8316       unsigned fde_idx;
8317       dw_fde_ref fde;
8318
8319       FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8320         {
8321           if (DECL_IGNORED_P (fde->decl))
8322             continue;
8323           if (!fde->in_std_section)
8324             size += 2 * DWARF2_ADDR_SIZE;
8325           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8326             size += 2 * DWARF2_ADDR_SIZE;
8327         }
8328     }
8329
8330   /* Count the two zero words used to terminated the address range table.  */
8331   size += 2 * DWARF2_ADDR_SIZE;
8332   return size;
8333 }
8334 \f
8335 /* Select the encoding of an attribute value.  */
8336
8337 static enum dwarf_form
8338 value_format (dw_attr_ref a)
8339 {
8340   switch (AT_class (a))
8341     {
8342     case dw_val_class_addr:
8343       /* Only very few attributes allow DW_FORM_addr.  */
8344       switch (a->dw_attr)
8345         {
8346         case DW_AT_low_pc:
8347         case DW_AT_high_pc:
8348         case DW_AT_entry_pc:
8349         case DW_AT_trampoline:
8350           return (AT_index (a) == NOT_INDEXED
8351                   ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8352         default:
8353           break;
8354         }
8355       switch (DWARF2_ADDR_SIZE)
8356         {
8357         case 1:
8358           return DW_FORM_data1;
8359         case 2:
8360           return DW_FORM_data2;
8361         case 4:
8362           return DW_FORM_data4;
8363         case 8:
8364           return DW_FORM_data8;
8365         default:
8366           gcc_unreachable ();
8367         }
8368     case dw_val_class_range_list:
8369     case dw_val_class_loc_list:
8370       if (dwarf_version >= 4)
8371         return DW_FORM_sec_offset;
8372       /* FALLTHRU */
8373     case dw_val_class_vms_delta:
8374     case dw_val_class_offset:
8375       switch (DWARF_OFFSET_SIZE)
8376         {
8377         case 4:
8378           return DW_FORM_data4;
8379         case 8:
8380           return DW_FORM_data8;
8381         default:
8382           gcc_unreachable ();
8383         }
8384     case dw_val_class_loc:
8385       if (dwarf_version >= 4)
8386         return DW_FORM_exprloc;
8387       switch (constant_size (size_of_locs (AT_loc (a))))
8388         {
8389         case 1:
8390           return DW_FORM_block1;
8391         case 2:
8392           return DW_FORM_block2;
8393         case 4:
8394           return DW_FORM_block4;
8395         default:
8396           gcc_unreachable ();
8397         }
8398     case dw_val_class_const:
8399       return DW_FORM_sdata;
8400     case dw_val_class_unsigned_const:
8401       switch (constant_size (AT_unsigned (a)))
8402         {
8403         case 1:
8404           return DW_FORM_data1;
8405         case 2:
8406           return DW_FORM_data2;
8407         case 4:
8408           /* In DWARF3 DW_AT_data_member_location with
8409              DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8410              constant, so we need to use DW_FORM_udata if we need
8411              a large constant.  */
8412           if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8413             return DW_FORM_udata;
8414           return DW_FORM_data4;
8415         case 8:
8416           if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8417             return DW_FORM_udata;
8418           return DW_FORM_data8;
8419         default:
8420           gcc_unreachable ();
8421         }
8422     case dw_val_class_const_double:
8423       switch (HOST_BITS_PER_WIDE_INT)
8424         {
8425         case 8:
8426           return DW_FORM_data2;
8427         case 16:
8428           return DW_FORM_data4;
8429         case 32:
8430           return DW_FORM_data8;
8431         case 64:
8432         default:
8433           return DW_FORM_block1;
8434         }
8435     case dw_val_class_wide_int:
8436       switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
8437         {
8438         case 8:
8439           return DW_FORM_data1;
8440         case 16:
8441           return DW_FORM_data2;
8442         case 32:
8443           return DW_FORM_data4;
8444         case 64:
8445           return DW_FORM_data8;
8446         default:
8447           return DW_FORM_block1;
8448         }
8449     case dw_val_class_vec:
8450       switch (constant_size (a->dw_attr_val.v.val_vec.length
8451                              * a->dw_attr_val.v.val_vec.elt_size))
8452         {
8453         case 1:
8454           return DW_FORM_block1;
8455         case 2:
8456           return DW_FORM_block2;
8457         case 4:
8458           return DW_FORM_block4;
8459         default:
8460           gcc_unreachable ();
8461         }
8462     case dw_val_class_flag:
8463       if (dwarf_version >= 4)
8464         {
8465           /* Currently all add_AT_flag calls pass in 1 as last argument,
8466              so DW_FORM_flag_present can be used.  If that ever changes,
8467              we'll need to use DW_FORM_flag and have some optimization
8468              in build_abbrev_table that will change those to
8469              DW_FORM_flag_present if it is set to 1 in all DIEs using
8470              the same abbrev entry.  */
8471           gcc_assert (a->dw_attr_val.v.val_flag == 1);
8472           return DW_FORM_flag_present;
8473         }
8474       return DW_FORM_flag;
8475     case dw_val_class_die_ref:
8476       if (AT_ref_external (a))
8477         return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8478       else
8479         return DW_FORM_ref;
8480     case dw_val_class_fde_ref:
8481       return DW_FORM_data;
8482     case dw_val_class_lbl_id:
8483       return (AT_index (a) == NOT_INDEXED
8484               ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8485     case dw_val_class_lineptr:
8486     case dw_val_class_macptr:
8487       return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8488     case dw_val_class_str:
8489       return AT_string_form (a);
8490     case dw_val_class_file:
8491       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8492         {
8493         case 1:
8494           return DW_FORM_data1;
8495         case 2:
8496           return DW_FORM_data2;
8497         case 4:
8498           return DW_FORM_data4;
8499         default:
8500           gcc_unreachable ();
8501         }
8502
8503     case dw_val_class_data8:
8504       return DW_FORM_data8;
8505
8506     case dw_val_class_high_pc:
8507       switch (DWARF2_ADDR_SIZE)
8508         {
8509         case 1:
8510           return DW_FORM_data1;
8511         case 2:
8512           return DW_FORM_data2;
8513         case 4:
8514           return DW_FORM_data4;
8515         case 8:
8516           return DW_FORM_data8;
8517         default:
8518           gcc_unreachable ();
8519         }
8520
8521     default:
8522       gcc_unreachable ();
8523     }
8524 }
8525
8526 /* Output the encoding of an attribute value.  */
8527
8528 static void
8529 output_value_format (dw_attr_ref a)
8530 {
8531   enum dwarf_form form = value_format (a);
8532
8533   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8534 }
8535
8536 /* Given a die and id, produce the appropriate abbreviations.  */
8537
8538 static void
8539 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8540 {
8541   unsigned ix;
8542   dw_attr_ref a_attr;
8543
8544   dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8545   dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8546                                dwarf_tag_name (abbrev->die_tag));
8547
8548   if (abbrev->die_child != NULL)
8549     dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8550   else
8551     dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8552
8553   for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8554     {
8555       dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8556                                    dwarf_attr_name (a_attr->dw_attr));
8557       output_value_format (a_attr);
8558     }
8559
8560   dw2_asm_output_data (1, 0, NULL);
8561   dw2_asm_output_data (1, 0, NULL);
8562 }
8563
8564
8565 /* Output the .debug_abbrev section which defines the DIE abbreviation
8566    table.  */
8567
8568 static void
8569 output_abbrev_section (void)
8570 {
8571   unsigned long abbrev_id;
8572
8573   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8574     output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8575
8576   /* Terminate the table.  */
8577   dw2_asm_output_data (1, 0, NULL);
8578 }
8579
8580 /* Output a symbol we can use to refer to this DIE from another CU.  */
8581
8582 static inline void
8583 output_die_symbol (dw_die_ref die)
8584 {
8585   const char *sym = die->die_id.die_symbol;
8586
8587   gcc_assert (!die->comdat_type_p);
8588
8589   if (sym == 0)
8590     return;
8591
8592   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8593     /* We make these global, not weak; if the target doesn't support
8594        .linkonce, it doesn't support combining the sections, so debugging
8595        will break.  */
8596     targetm.asm_out.globalize_label (asm_out_file, sym);
8597
8598   ASM_OUTPUT_LABEL (asm_out_file, sym);
8599 }
8600
8601 /* Return a new location list, given the begin and end range, and the
8602    expression.  */
8603
8604 static inline dw_loc_list_ref
8605 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8606               const char *section)
8607 {
8608   dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
8609
8610   retlist->begin = begin;
8611   retlist->begin_entry = NULL;
8612   retlist->end = end;
8613   retlist->expr = expr;
8614   retlist->section = section;
8615
8616   return retlist;
8617 }
8618
8619 /* Generate a new internal symbol for this location list node, if it
8620    hasn't got one yet.  */
8621
8622 static inline void
8623 gen_llsym (dw_loc_list_ref list)
8624 {
8625   gcc_assert (!list->ll_symbol);
8626   list->ll_symbol = gen_internal_sym ("LLST");
8627 }
8628
8629 /* Output the location list given to us.  */
8630
8631 static void
8632 output_loc_list (dw_loc_list_ref list_head)
8633 {
8634   dw_loc_list_ref curr = list_head;
8635
8636   if (list_head->emitted)
8637     return;
8638   list_head->emitted = true;
8639
8640   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8641
8642   /* Walk the location list, and output each range + expression.  */
8643   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8644     {
8645       unsigned long size;
8646       /* Don't output an entry that starts and ends at the same address.  */
8647       if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8648         continue;
8649       size = size_of_locs (curr->expr);
8650       /* If the expression is too large, drop it on the floor.  We could
8651          perhaps put it into DW_TAG_dwarf_procedure and refer to that
8652          in the expression, but >= 64KB expressions for a single value
8653          in a single range are unlikely very useful.  */
8654       if (size > 0xffff)
8655         continue;
8656       if (dwarf_split_debug_info)
8657         {
8658           dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8659                                "Location list start/length entry (%s)",
8660                                list_head->ll_symbol);
8661           dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8662                                        "Location list range start index (%s)",
8663                                        curr->begin);
8664           /* The length field is 4 bytes.  If we ever need to support
8665             an 8-byte length, we can add a new DW_LLE code or fall back
8666             to DW_LLE_GNU_start_end_entry.  */
8667           dw2_asm_output_delta (4, curr->end, curr->begin,
8668                                 "Location list range length (%s)",
8669                                 list_head->ll_symbol);
8670         }
8671       else if (!have_multiple_function_sections)
8672         {
8673           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8674                                 "Location list begin address (%s)",
8675                                 list_head->ll_symbol);
8676           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8677                                 "Location list end address (%s)",
8678                                 list_head->ll_symbol);
8679         }
8680       else
8681         {
8682           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8683                                "Location list begin address (%s)",
8684                                list_head->ll_symbol);
8685           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8686                                "Location list end address (%s)",
8687                                list_head->ll_symbol);
8688         }
8689
8690       /* Output the block length for this list of location operations.  */
8691       gcc_assert (size <= 0xffff);
8692       dw2_asm_output_data (2, size, "%s", "Location expression size");
8693
8694       output_loc_sequence (curr->expr, -1);
8695     }
8696
8697   if (dwarf_split_debug_info)
8698     dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8699                          "Location list terminator (%s)",
8700                          list_head->ll_symbol);
8701   else
8702     {
8703       dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8704                            "Location list terminator begin (%s)",
8705                            list_head->ll_symbol);
8706       dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8707                            "Location list terminator end (%s)",
8708                            list_head->ll_symbol);
8709     }
8710 }
8711
8712 /* Output a range_list offset into the debug_range section.  Emit a
8713    relocated reference if val_entry is NULL, otherwise, emit an
8714    indirect reference.  */
8715
8716 static void
8717 output_range_list_offset (dw_attr_ref a)
8718 {
8719   const char *name = dwarf_attr_name (a->dw_attr);
8720
8721   if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8722     {
8723       char *p = strchr (ranges_section_label, '\0');
8724       sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8725       dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8726                              debug_ranges_section, "%s", name);
8727       *p = '\0';
8728     }
8729   else
8730     dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8731                          "%s (offset from %s)", name, ranges_section_label);
8732 }
8733
8734 /* Output the offset into the debug_loc section.  */
8735
8736 static void
8737 output_loc_list_offset (dw_attr_ref a)
8738 {
8739   char *sym = AT_loc_list (a)->ll_symbol;
8740
8741   gcc_assert (sym);
8742   if (dwarf_split_debug_info)
8743     dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8744                           "%s", dwarf_attr_name (a->dw_attr));
8745   else
8746     dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8747                            "%s", dwarf_attr_name (a->dw_attr));
8748 }
8749
8750 /* Output an attribute's index or value appropriately.  */
8751
8752 static void
8753 output_attr_index_or_value (dw_attr_ref a)
8754 {
8755   const char *name = dwarf_attr_name (a->dw_attr);
8756
8757   if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8758     {
8759       dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8760       return;
8761     }
8762   switch (AT_class (a))
8763     {
8764       case dw_val_class_addr:
8765         dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8766         break;
8767       case dw_val_class_high_pc:
8768       case dw_val_class_lbl_id:
8769         dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8770         break;
8771       case dw_val_class_loc_list:
8772         output_loc_list_offset (a);
8773         break;
8774       default:
8775         gcc_unreachable ();
8776     }
8777 }
8778
8779 /* Output a type signature.  */
8780
8781 static inline void
8782 output_signature (const char *sig, const char *name)
8783 {
8784   int i;
8785
8786   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8787     dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8788 }
8789
8790 /* Output the DIE and its attributes.  Called recursively to generate
8791    the definitions of each child DIE.  */
8792
8793 static void
8794 output_die (dw_die_ref die)
8795 {
8796   dw_attr_ref a;
8797   dw_die_ref c;
8798   unsigned long size;
8799   unsigned ix;
8800
8801   /* If someone in another CU might refer to us, set up a symbol for
8802      them to point to.  */
8803   if (! die->comdat_type_p && die->die_id.die_symbol)
8804     output_die_symbol (die);
8805
8806   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8807                                (unsigned long)die->die_offset,
8808                                dwarf_tag_name (die->die_tag));
8809
8810   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8811     {
8812       const char *name = dwarf_attr_name (a->dw_attr);
8813
8814       switch (AT_class (a))
8815         {
8816         case dw_val_class_addr:
8817           output_attr_index_or_value (a);
8818           break;
8819
8820         case dw_val_class_offset:
8821           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8822                                "%s", name);
8823           break;
8824
8825         case dw_val_class_range_list:
8826           output_range_list_offset (a);
8827           break;
8828
8829         case dw_val_class_loc:
8830           size = size_of_locs (AT_loc (a));
8831
8832           /* Output the block length for this list of location operations.  */
8833           if (dwarf_version >= 4)
8834             dw2_asm_output_data_uleb128 (size, "%s", name);
8835           else
8836             dw2_asm_output_data (constant_size (size), size, "%s", name);
8837
8838           output_loc_sequence (AT_loc (a), -1);
8839           break;
8840
8841         case dw_val_class_const:
8842           /* ??? It would be slightly more efficient to use a scheme like is
8843              used for unsigned constants below, but gdb 4.x does not sign
8844              extend.  Gdb 5.x does sign extend.  */
8845           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8846           break;
8847
8848         case dw_val_class_unsigned_const:
8849           {
8850             int csize = constant_size (AT_unsigned (a));
8851             if (dwarf_version == 3
8852                 && a->dw_attr == DW_AT_data_member_location
8853                 && csize >= 4)
8854               dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8855             else
8856               dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8857           }
8858           break;
8859
8860         case dw_val_class_const_double:
8861           {
8862             unsigned HOST_WIDE_INT first, second;
8863
8864             if (HOST_BITS_PER_WIDE_INT >= 64)
8865               dw2_asm_output_data (1,
8866                                    HOST_BITS_PER_DOUBLE_INT
8867                                    / HOST_BITS_PER_CHAR,
8868                                    NULL);
8869
8870             if (WORDS_BIG_ENDIAN)
8871               {
8872                 first = a->dw_attr_val.v.val_double.high;
8873                 second = a->dw_attr_val.v.val_double.low;
8874               }
8875             else
8876               {
8877                 first = a->dw_attr_val.v.val_double.low;
8878                 second = a->dw_attr_val.v.val_double.high;
8879               }
8880
8881             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8882                                  first, "%s", name);
8883             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8884                                  second, NULL);
8885           }
8886           break;
8887
8888         case dw_val_class_wide_int:
8889           {
8890             int i;
8891             int len = get_full_len (*a->dw_attr_val.v.val_wide);
8892             int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
8893             if (len * HOST_BITS_PER_WIDE_INT > 64)
8894               dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide) * l,
8895                                    NULL);
8896
8897             if (WORDS_BIG_ENDIAN)
8898               for (i = len - 1; i >= 0; --i)
8899                 {
8900                   dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8901                                        "%s", name);
8902                   name = NULL;
8903                 }
8904             else
8905               for (i = 0; i < len; ++i)
8906                 {
8907                   dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8908                                        "%s", name);
8909                   name = NULL;
8910                 }
8911           }
8912           break;
8913
8914         case dw_val_class_vec:
8915           {
8916             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8917             unsigned int len = a->dw_attr_val.v.val_vec.length;
8918             unsigned int i;
8919             unsigned char *p;
8920
8921             dw2_asm_output_data (constant_size (len * elt_size),
8922                                  len * elt_size, "%s", name);
8923             if (elt_size > sizeof (HOST_WIDE_INT))
8924               {
8925                 elt_size /= 2;
8926                 len *= 2;
8927               }
8928             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8929                  i < len;
8930                  i++, p += elt_size)
8931               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8932                                    "fp or vector constant word %u", i);
8933             break;
8934           }
8935
8936         case dw_val_class_flag:
8937           if (dwarf_version >= 4)
8938             {
8939               /* Currently all add_AT_flag calls pass in 1 as last argument,
8940                  so DW_FORM_flag_present can be used.  If that ever changes,
8941                  we'll need to use DW_FORM_flag and have some optimization
8942                  in build_abbrev_table that will change those to
8943                  DW_FORM_flag_present if it is set to 1 in all DIEs using
8944                  the same abbrev entry.  */
8945               gcc_assert (AT_flag (a) == 1);
8946               if (flag_debug_asm)
8947                 fprintf (asm_out_file, "\t\t\t%s %s\n",
8948                          ASM_COMMENT_START, name);
8949               break;
8950             }
8951           dw2_asm_output_data (1, AT_flag (a), "%s", name);
8952           break;
8953
8954         case dw_val_class_loc_list:
8955           output_attr_index_or_value (a);
8956           break;
8957
8958         case dw_val_class_die_ref:
8959           if (AT_ref_external (a))
8960             {
8961               if (AT_ref (a)->comdat_type_p)
8962                 {
8963                   comdat_type_node_ref type_node =
8964                     AT_ref (a)->die_id.die_type_node;
8965
8966                   gcc_assert (type_node);
8967                   output_signature (type_node->signature, name);
8968                 }
8969               else
8970                 {
8971                   const char *sym = AT_ref (a)->die_id.die_symbol;
8972                   int size;
8973
8974                   gcc_assert (sym);
8975                   /* In DWARF2, DW_FORM_ref_addr is sized by target address
8976                      length, whereas in DWARF3 it's always sized as an
8977                      offset.  */
8978                   if (dwarf_version == 2)
8979                     size = DWARF2_ADDR_SIZE;
8980                   else
8981                     size = DWARF_OFFSET_SIZE;
8982                   dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8983                                          name);
8984                 }
8985             }
8986           else
8987             {
8988               gcc_assert (AT_ref (a)->die_offset);
8989               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8990                                    "%s", name);
8991             }
8992           break;
8993
8994         case dw_val_class_fde_ref:
8995           {
8996             char l1[20];
8997
8998             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8999                                          a->dw_attr_val.v.val_fde_index * 2);
9000             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
9001                                    "%s", name);
9002           }
9003           break;
9004
9005         case dw_val_class_vms_delta:
9006 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
9007           dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
9008                                     AT_vms_delta2 (a), AT_vms_delta1 (a),
9009                                     "%s", name);
9010 #else
9011           dw2_asm_output_delta (DWARF_OFFSET_SIZE,
9012                                 AT_vms_delta2 (a), AT_vms_delta1 (a),
9013                                 "%s", name);
9014 #endif
9015           break;
9016
9017         case dw_val_class_lbl_id:
9018           output_attr_index_or_value (a);
9019           break;
9020
9021         case dw_val_class_lineptr:
9022           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9023                                  debug_line_section, "%s", name);
9024           break;
9025
9026         case dw_val_class_macptr:
9027           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9028                                  debug_macinfo_section, "%s", name);
9029           break;
9030
9031         case dw_val_class_str:
9032           if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
9033             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9034                                    a->dw_attr_val.v.val_str->label,
9035                                    debug_str_section,
9036                                    "%s: \"%s\"", name, AT_string (a));
9037           else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
9038             dw2_asm_output_data_uleb128 (AT_index (a),
9039                                          "%s: \"%s\"", name, AT_string (a));
9040           else
9041             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
9042           break;
9043
9044         case dw_val_class_file:
9045           {
9046             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
9047
9048             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
9049                                  a->dw_attr_val.v.val_file->filename);
9050             break;
9051           }
9052
9053         case dw_val_class_data8:
9054           {
9055             int i;
9056
9057             for (i = 0; i < 8; i++)
9058               dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
9059                                    i == 0 ? "%s" : NULL, name);
9060             break;
9061           }
9062
9063         case dw_val_class_high_pc:
9064           dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
9065                                 get_AT_low_pc (die), "DW_AT_high_pc");
9066           break;
9067
9068         default:
9069           gcc_unreachable ();
9070         }
9071     }
9072
9073   FOR_EACH_CHILD (die, c, output_die (c));
9074
9075   /* Add null byte to terminate sibling list.  */
9076   if (die->die_child != NULL)
9077     dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
9078                          (unsigned long) die->die_offset);
9079 }
9080
9081 /* Output the compilation unit that appears at the beginning of the
9082    .debug_info section, and precedes the DIE descriptions.  */
9083
9084 static void
9085 output_compilation_unit_header (void)
9086 {
9087   /* We don't support actual DWARFv5 units yet, we just use some
9088      DWARFv5 draft DIE tags in DWARFv4 format.  */
9089   int ver = dwarf_version < 5 ? dwarf_version : 4;
9090
9091   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9092     dw2_asm_output_data (4, 0xffffffff,
9093       "Initial length escape value indicating 64-bit DWARF extension");
9094   dw2_asm_output_data (DWARF_OFFSET_SIZE,
9095                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
9096                        "Length of Compilation Unit Info");
9097   dw2_asm_output_data (2, ver, "DWARF version number");
9098   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
9099                          debug_abbrev_section,
9100                          "Offset Into Abbrev. Section");
9101   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9102 }
9103
9104 /* Output the compilation unit DIE and its children.  */
9105
9106 static void
9107 output_comp_unit (dw_die_ref die, int output_if_empty)
9108 {
9109   const char *secname, *oldsym;
9110   char *tmp;
9111
9112   /* Unless we are outputting main CU, we may throw away empty ones.  */
9113   if (!output_if_empty && die->die_child == NULL)
9114     return;
9115
9116   /* Even if there are no children of this DIE, we must output the information
9117      about the compilation unit.  Otherwise, on an empty translation unit, we
9118      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
9119      will then complain when examining the file.  First mark all the DIEs in
9120      this CU so we know which get local refs.  */
9121   mark_dies (die);
9122
9123   external_ref_hash_type *extern_map = optimize_external_refs (die);
9124
9125   build_abbrev_table (die, extern_map);
9126
9127   delete extern_map;
9128
9129   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
9130   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
9131   calc_die_sizes (die);
9132
9133   oldsym = die->die_id.die_symbol;
9134   if (oldsym)
9135     {
9136       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
9137
9138       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
9139       secname = tmp;
9140       die->die_id.die_symbol = NULL;
9141       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9142     }
9143   else
9144     {
9145       switch_to_section (debug_info_section);
9146       ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
9147       info_section_emitted = true;
9148     }
9149
9150   /* Output debugging information.  */
9151   output_compilation_unit_header ();
9152   output_die (die);
9153
9154   /* Leave the marks on the main CU, so we can check them in
9155      output_pubnames.  */
9156   if (oldsym)
9157     {
9158       unmark_dies (die);
9159       die->die_id.die_symbol = oldsym;
9160     }
9161 }
9162
9163 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
9164    and .debug_pubtypes.  This is configured per-target, but can be
9165    overridden by the -gpubnames or -gno-pubnames options.  */
9166
9167 static inline bool
9168 want_pubnames (void)
9169 {
9170   if (debug_info_level <= DINFO_LEVEL_TERSE)
9171     return false;
9172   if (debug_generate_pub_sections != -1)
9173     return debug_generate_pub_sections;
9174   return targetm.want_debug_pub_sections;
9175 }
9176
9177 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes.  */
9178
9179 static void
9180 add_AT_pubnames (dw_die_ref die)
9181 {
9182   if (want_pubnames ())
9183     add_AT_flag (die, DW_AT_GNU_pubnames, 1);
9184 }
9185
9186 /* Add a string attribute value to a skeleton DIE.  */
9187
9188 static inline void
9189 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
9190                         const char *str)
9191 {
9192   dw_attr_node attr;
9193   struct indirect_string_node *node;
9194
9195   if (! skeleton_debug_str_hash)
9196     skeleton_debug_str_hash
9197       = hash_table<indirect_string_hasher>::create_ggc (10);
9198
9199   node = find_AT_string_in_table (str, skeleton_debug_str_hash);
9200   find_string_form (node);
9201   if (node->form == DW_FORM_GNU_str_index)
9202     node->form = DW_FORM_strp;
9203
9204   attr.dw_attr = attr_kind;
9205   attr.dw_attr_val.val_class = dw_val_class_str;
9206   attr.dw_attr_val.val_entry = NULL;
9207   attr.dw_attr_val.v.val_str = node;
9208   add_dwarf_attr (die, &attr);
9209 }
9210
9211 /* Helper function to generate top-level dies for skeleton debug_info and
9212    debug_types.  */
9213
9214 static void
9215 add_top_level_skeleton_die_attrs (dw_die_ref die)
9216 {
9217   const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
9218   const char *comp_dir = comp_dir_string ();
9219
9220   add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
9221   if (comp_dir != NULL)
9222     add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
9223   add_AT_pubnames (die);
9224   add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
9225 }
9226
9227 /* Output skeleton debug sections that point to the dwo file.  */
9228
9229 static void
9230 output_skeleton_debug_sections (dw_die_ref comp_unit)
9231 {
9232   /* We don't support actual DWARFv5 units yet, we just use some
9233      DWARFv5 draft DIE tags in DWARFv4 format.  */
9234   int ver = dwarf_version < 5 ? dwarf_version : 4;
9235
9236   /* These attributes will be found in the full debug_info section.  */
9237   remove_AT (comp_unit, DW_AT_producer);
9238   remove_AT (comp_unit, DW_AT_language);
9239
9240   switch_to_section (debug_skeleton_info_section);
9241   ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
9242
9243   /* Produce the skeleton compilation-unit header.  This one differs enough from
9244      a normal CU header that it's better not to call output_compilation_unit
9245      header.  */
9246   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9247     dw2_asm_output_data (4, 0xffffffff,
9248       "Initial length escape value indicating 64-bit DWARF extension");
9249
9250   dw2_asm_output_data (DWARF_OFFSET_SIZE,
9251                        DWARF_COMPILE_UNIT_HEADER_SIZE
9252                        - DWARF_INITIAL_LENGTH_SIZE
9253                        + size_of_die (comp_unit),
9254                       "Length of Compilation Unit Info");
9255   dw2_asm_output_data (2, ver, "DWARF version number");
9256   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
9257                          debug_abbrev_section,
9258                          "Offset Into Abbrev. Section");
9259   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9260
9261   comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
9262   output_die (comp_unit);
9263
9264   /* Build the skeleton debug_abbrev section.  */
9265   switch_to_section (debug_skeleton_abbrev_section);
9266   ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
9267
9268   output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
9269
9270   dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
9271 }
9272
9273 /* Output a comdat type unit DIE and its children.  */
9274
9275 static void
9276 output_comdat_type_unit (comdat_type_node *node)
9277 {
9278   const char *secname;
9279   char *tmp;
9280   int i;
9281 #if defined (OBJECT_FORMAT_ELF)
9282   tree comdat_key;
9283 #endif
9284
9285   /* First mark all the DIEs in this CU so we know which get local refs.  */
9286   mark_dies (node->root_die);
9287
9288   external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
9289
9290   build_abbrev_table (node->root_die, extern_map);
9291
9292   delete extern_map;
9293   extern_map = NULL;
9294
9295   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
9296   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
9297   calc_die_sizes (node->root_die);
9298
9299 #if defined (OBJECT_FORMAT_ELF)
9300   if (!dwarf_split_debug_info)
9301     secname = ".debug_types";
9302   else
9303     secname = ".debug_types.dwo";
9304
9305   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9306   sprintf (tmp, "wt.");
9307   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9308     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9309   comdat_key = get_identifier (tmp);
9310   targetm.asm_out.named_section (secname,
9311                                  SECTION_DEBUG | SECTION_LINKONCE,
9312                                  comdat_key);
9313 #else
9314   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9315   sprintf (tmp, ".gnu.linkonce.wt.");
9316   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9317     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9318   secname = tmp;
9319   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9320 #endif
9321
9322   /* Output debugging information.  */
9323   output_compilation_unit_header ();
9324   output_signature (node->signature, "Type Signature");
9325   dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9326                        "Offset to Type DIE");
9327   output_die (node->root_die);
9328
9329   unmark_dies (node->root_die);
9330 }
9331
9332 /* Return the DWARF2/3 pubname associated with a decl.  */
9333
9334 static const char *
9335 dwarf2_name (tree decl, int scope)
9336 {
9337   if (DECL_NAMELESS (decl))
9338     return NULL;
9339   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9340 }
9341
9342 /* Add a new entry to .debug_pubnames if appropriate.  */
9343
9344 static void
9345 add_pubname_string (const char *str, dw_die_ref die)
9346 {
9347   pubname_entry e;
9348
9349   e.die = die;
9350   e.name = xstrdup (str);
9351   vec_safe_push (pubname_table, e);
9352 }
9353
9354 static void
9355 add_pubname (tree decl, dw_die_ref die)
9356 {
9357   if (!want_pubnames ())
9358     return;
9359
9360   /* Don't add items to the table when we expect that the consumer will have
9361      just read the enclosing die.  For example, if the consumer is looking at a
9362      class_member, it will either be inside the class already, or will have just
9363      looked up the class to find the member.  Either way, searching the class is
9364      faster than searching the index.  */
9365   if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9366       || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9367     {
9368       const char *name = dwarf2_name (decl, 1);
9369
9370       if (name)
9371         add_pubname_string (name, die);
9372     }
9373 }
9374
9375 /* Add an enumerator to the pubnames section.  */
9376
9377 static void
9378 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9379 {
9380   pubname_entry e;
9381
9382   gcc_assert (scope_name);
9383   e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9384   e.die = die;
9385   vec_safe_push (pubname_table, e);
9386 }
9387
9388 /* Add a new entry to .debug_pubtypes if appropriate.  */
9389
9390 static void
9391 add_pubtype (tree decl, dw_die_ref die)
9392 {
9393   pubname_entry e;
9394
9395   if (!want_pubnames ())
9396     return;
9397
9398   if ((TREE_PUBLIC (decl)
9399        || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9400       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9401     {
9402       tree scope = NULL;
9403       const char *scope_name = "";
9404       const char *sep = is_cxx () ? "::" : ".";
9405       const char *name;
9406
9407       scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9408       if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9409         {
9410           scope_name = lang_hooks.dwarf_name (scope, 1);
9411           if (scope_name != NULL && scope_name[0] != '\0')
9412             scope_name = concat (scope_name, sep, NULL);
9413           else
9414             scope_name = "";
9415         }
9416
9417       if (TYPE_P (decl))
9418         name = type_tag (decl);
9419       else
9420         name = lang_hooks.dwarf_name (decl, 1);
9421
9422       /* If we don't have a name for the type, there's no point in adding
9423          it to the table.  */
9424       if (name != NULL && name[0] != '\0')
9425         {
9426           e.die = die;
9427           e.name = concat (scope_name, name, NULL);
9428           vec_safe_push (pubtype_table, e);
9429         }
9430
9431       /* Although it might be more consistent to add the pubinfo for the
9432          enumerators as their dies are created, they should only be added if the
9433          enum type meets the criteria above.  So rather than re-check the parent
9434          enum type whenever an enumerator die is created, just output them all
9435          here.  This isn't protected by the name conditional because anonymous
9436          enums don't have names.  */
9437       if (die->die_tag == DW_TAG_enumeration_type)
9438         {
9439           dw_die_ref c;
9440
9441           FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9442         }
9443     }
9444 }
9445
9446 /* Output a single entry in the pubnames table.  */
9447
9448 static void
9449 output_pubname (dw_offset die_offset, pubname_entry *entry)
9450 {
9451   dw_die_ref die = entry->die;
9452   int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9453
9454   dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9455
9456   if (debug_generate_pub_sections == 2)
9457     {
9458       /* This logic follows gdb's method for determining the value of the flag
9459          byte.  */
9460       uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9461       switch (die->die_tag)
9462       {
9463         case DW_TAG_typedef:
9464         case DW_TAG_base_type:
9465         case DW_TAG_subrange_type:
9466           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9467           GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9468           break;
9469         case DW_TAG_enumerator:
9470           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9471                                           GDB_INDEX_SYMBOL_KIND_VARIABLE);
9472           if (!is_cxx () && !is_java ())
9473             GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9474           break;
9475         case DW_TAG_subprogram:
9476           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9477                                           GDB_INDEX_SYMBOL_KIND_FUNCTION);
9478           if (!is_ada ())
9479             GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9480           break;
9481         case DW_TAG_constant:
9482           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9483                                           GDB_INDEX_SYMBOL_KIND_VARIABLE);
9484           GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9485           break;
9486         case DW_TAG_variable:
9487           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9488                                           GDB_INDEX_SYMBOL_KIND_VARIABLE);
9489           GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9490           break;
9491         case DW_TAG_namespace:
9492         case DW_TAG_imported_declaration:
9493           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9494           break;
9495         case DW_TAG_class_type:
9496         case DW_TAG_interface_type:
9497         case DW_TAG_structure_type:
9498         case DW_TAG_union_type:
9499         case DW_TAG_enumeration_type:
9500           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9501           if (!is_cxx () && !is_java ())
9502             GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9503           break;
9504         default:
9505           /* An unusual tag.  Leave the flag-byte empty.  */
9506           break;
9507       }
9508       dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9509                            "GDB-index flags");
9510     }
9511
9512   dw2_asm_output_nstring (entry->name, -1, "external name");
9513 }
9514
9515
9516 /* Output the public names table used to speed up access to externally
9517    visible names; or the public types table used to find type definitions.  */
9518
9519 static void
9520 output_pubnames (vec<pubname_entry, va_gc> *names)
9521 {
9522   unsigned i;
9523   unsigned long pubnames_length = size_of_pubnames (names);
9524   pubname_ref pub;
9525
9526   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9527     dw2_asm_output_data (4, 0xffffffff,
9528       "Initial length escape value indicating 64-bit DWARF extension");
9529   dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length, "Pub Info Length");
9530
9531   /* Version number for pubnames/pubtypes is independent of dwarf version.  */
9532   dw2_asm_output_data (2, 2, "DWARF Version");
9533
9534   if (dwarf_split_debug_info)
9535     dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9536                            debug_skeleton_info_section,
9537                            "Offset of Compilation Unit Info");
9538   else
9539     dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9540                            debug_info_section,
9541                            "Offset of Compilation Unit Info");
9542   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9543                        "Compilation Unit Length");
9544
9545   FOR_EACH_VEC_ELT (*names, i, pub)
9546     {
9547       if (include_pubname_in_output (names, pub))
9548         {
9549           dw_offset die_offset = pub->die->die_offset;
9550
9551           /* We shouldn't see pubnames for DIEs outside of the main CU.  */
9552           if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9553             gcc_assert (pub->die->die_mark);
9554
9555           /* If we're putting types in their own .debug_types sections,
9556              the .debug_pubtypes table will still point to the compile
9557              unit (not the type unit), so we want to use the offset of
9558              the skeleton DIE (if there is one).  */
9559           if (pub->die->comdat_type_p && names == pubtype_table)
9560             {
9561               comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9562
9563               if (type_node != NULL)
9564                 die_offset = (type_node->skeleton_die != NULL
9565                               ? type_node->skeleton_die->die_offset
9566                               : comp_unit_die ()->die_offset);
9567             }
9568
9569           output_pubname (die_offset, pub);
9570         }
9571     }
9572
9573   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9574 }
9575
9576 /* Output public names and types tables if necessary.  */
9577
9578 static void
9579 output_pubtables (void)
9580 {
9581   if (!want_pubnames () || !info_section_emitted)
9582     return;
9583
9584   switch_to_section (debug_pubnames_section);
9585   output_pubnames (pubname_table);
9586   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
9587      It shouldn't hurt to emit it always, since pure DWARF2 consumers
9588      simply won't look for the section.  */
9589   switch_to_section (debug_pubtypes_section);
9590   output_pubnames (pubtype_table);
9591 }
9592
9593
9594 /* Output the information that goes into the .debug_aranges table.
9595    Namely, define the beginning and ending address range of the
9596    text section generated for this compilation unit.  */
9597
9598 static void
9599 output_aranges (unsigned long aranges_length)
9600 {
9601   unsigned i;
9602
9603   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9604     dw2_asm_output_data (4, 0xffffffff,
9605       "Initial length escape value indicating 64-bit DWARF extension");
9606   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9607                        "Length of Address Ranges Info");
9608   /* Version number for aranges is still 2, even up to DWARF5.  */
9609   dw2_asm_output_data (2, 2, "DWARF Version");
9610   if (dwarf_split_debug_info)
9611     dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9612                            debug_skeleton_info_section,
9613                            "Offset of Compilation Unit Info");
9614   else
9615     dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9616                            debug_info_section,
9617                            "Offset of Compilation Unit Info");
9618   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9619   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9620
9621   /* We need to align to twice the pointer size here.  */
9622   if (DWARF_ARANGES_PAD_SIZE)
9623     {
9624       /* Pad using a 2 byte words so that padding is correct for any
9625          pointer size.  */
9626       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9627                            2 * DWARF2_ADDR_SIZE);
9628       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9629         dw2_asm_output_data (2, 0, NULL);
9630     }
9631
9632   /* It is necessary not to output these entries if the sections were
9633      not used; if the sections were not used, the length will be 0 and
9634      the address may end up as 0 if the section is discarded by ld
9635      --gc-sections, leaving an invalid (0, 0) entry that can be
9636      confused with the terminator.  */
9637   if (text_section_used)
9638     {
9639       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9640       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9641                             text_section_label, "Length");
9642     }
9643   if (cold_text_section_used)
9644     {
9645       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9646                            "Address");
9647       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9648                             cold_text_section_label, "Length");
9649     }
9650
9651   if (have_multiple_function_sections)
9652     {
9653       unsigned fde_idx;
9654       dw_fde_ref fde;
9655
9656       FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9657         {
9658           if (DECL_IGNORED_P (fde->decl))
9659             continue;
9660           if (!fde->in_std_section)
9661             {
9662               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9663                                    "Address");
9664               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9665                                     fde->dw_fde_begin, "Length");
9666             }
9667           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9668             {
9669               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9670                                    "Address");
9671               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9672                                     fde->dw_fde_second_begin, "Length");
9673             }
9674         }
9675     }
9676
9677   /* Output the terminator words.  */
9678   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9679   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9680 }
9681
9682 /* Add a new entry to .debug_ranges.  Return the offset at which it
9683    was placed.  */
9684
9685 static unsigned int
9686 add_ranges_num (int num)
9687 {
9688   unsigned int in_use = ranges_table_in_use;
9689
9690   if (in_use == ranges_table_allocated)
9691     {
9692       ranges_table_allocated += RANGES_TABLE_INCREMENT;
9693       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9694                                     ranges_table_allocated);
9695       memset (ranges_table + ranges_table_in_use, 0,
9696               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9697     }
9698
9699   ranges_table[in_use].num = num;
9700   ranges_table_in_use = in_use + 1;
9701
9702   return in_use * 2 * DWARF2_ADDR_SIZE;
9703 }
9704
9705 /* Add a new entry to .debug_ranges corresponding to a block, or a
9706    range terminator if BLOCK is NULL.  */
9707
9708 static unsigned int
9709 add_ranges (const_tree block)
9710 {
9711   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9712 }
9713
9714 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9715    When using dwarf_split_debug_info, address attributes in dies destined
9716    for the final executable should be direct references--setting the
9717    parameter force_direct ensures this behavior.  */
9718
9719 static void
9720 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9721                       bool *added, bool force_direct)
9722 {
9723   unsigned int in_use = ranges_by_label_in_use;
9724   unsigned int offset;
9725
9726   if (in_use == ranges_by_label_allocated)
9727     {
9728       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9729       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9730                                        ranges_by_label,
9731                                        ranges_by_label_allocated);
9732       memset (ranges_by_label + ranges_by_label_in_use, 0,
9733               RANGES_TABLE_INCREMENT
9734               * sizeof (struct dw_ranges_by_label_struct));
9735     }
9736
9737   ranges_by_label[in_use].begin = begin;
9738   ranges_by_label[in_use].end = end;
9739   ranges_by_label_in_use = in_use + 1;
9740
9741   offset = add_ranges_num (-(int)in_use - 1);
9742   if (!*added)
9743     {
9744       add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9745       *added = true;
9746     }
9747 }
9748
9749 static void
9750 output_ranges (void)
9751 {
9752   unsigned i;
9753   static const char *const start_fmt = "Offset %#x";
9754   const char *fmt = start_fmt;
9755
9756   for (i = 0; i < ranges_table_in_use; i++)
9757     {
9758       int block_num = ranges_table[i].num;
9759
9760       if (block_num > 0)
9761         {
9762           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9763           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9764
9765           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9766           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9767
9768           /* If all code is in the text section, then the compilation
9769              unit base address defaults to DW_AT_low_pc, which is the
9770              base of the text section.  */
9771           if (!have_multiple_function_sections)
9772             {
9773               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9774                                     text_section_label,
9775                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
9776               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9777                                     text_section_label, NULL);
9778             }
9779
9780           /* Otherwise, the compilation unit base address is zero,
9781              which allows us to use absolute addresses, and not worry
9782              about whether the target supports cross-section
9783              arithmetic.  */
9784           else
9785             {
9786               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9787                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
9788               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9789             }
9790
9791           fmt = NULL;
9792         }
9793
9794       /* Negative block_num stands for an index into ranges_by_label.  */
9795       else if (block_num < 0)
9796         {
9797           int lab_idx = - block_num - 1;
9798
9799           if (!have_multiple_function_sections)
9800             {
9801               gcc_unreachable ();
9802 #if 0
9803               /* If we ever use add_ranges_by_labels () for a single
9804                  function section, all we have to do is to take out
9805                  the #if 0 above.  */
9806               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9807                                     ranges_by_label[lab_idx].begin,
9808                                     text_section_label,
9809                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
9810               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9811                                     ranges_by_label[lab_idx].end,
9812                                     text_section_label, NULL);
9813 #endif
9814             }
9815           else
9816             {
9817               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9818                                    ranges_by_label[lab_idx].begin,
9819                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
9820               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9821                                    ranges_by_label[lab_idx].end,
9822                                    NULL);
9823             }
9824         }
9825       else
9826         {
9827           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9828           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9829           fmt = start_fmt;
9830         }
9831     }
9832 }
9833
9834 /* Data structure containing information about input files.  */
9835 struct file_info
9836 {
9837   const char *path;     /* Complete file name.  */
9838   const char *fname;    /* File name part.  */
9839   int length;           /* Length of entire string.  */
9840   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
9841   int dir_idx;          /* Index in directory table.  */
9842 };
9843
9844 /* Data structure containing information about directories with source
9845    files.  */
9846 struct dir_info
9847 {
9848   const char *path;     /* Path including directory name.  */
9849   int length;           /* Path length.  */
9850   int prefix;           /* Index of directory entry which is a prefix.  */
9851   int count;            /* Number of files in this directory.  */
9852   int dir_idx;          /* Index of directory used as base.  */
9853 };
9854
9855 /* Callback function for file_info comparison.  We sort by looking at
9856    the directories in the path.  */
9857
9858 static int
9859 file_info_cmp (const void *p1, const void *p2)
9860 {
9861   const struct file_info *const s1 = (const struct file_info *) p1;
9862   const struct file_info *const s2 = (const struct file_info *) p2;
9863   const unsigned char *cp1;
9864   const unsigned char *cp2;
9865
9866   /* Take care of file names without directories.  We need to make sure that
9867      we return consistent values to qsort since some will get confused if
9868      we return the same value when identical operands are passed in opposite
9869      orders.  So if neither has a directory, return 0 and otherwise return
9870      1 or -1 depending on which one has the directory.  */
9871   if ((s1->path == s1->fname || s2->path == s2->fname))
9872     return (s2->path == s2->fname) - (s1->path == s1->fname);
9873
9874   cp1 = (const unsigned char *) s1->path;
9875   cp2 = (const unsigned char *) s2->path;
9876
9877   while (1)
9878     {
9879       ++cp1;
9880       ++cp2;
9881       /* Reached the end of the first path?  If so, handle like above.  */
9882       if ((cp1 == (const unsigned char *) s1->fname)
9883           || (cp2 == (const unsigned char *) s2->fname))
9884         return ((cp2 == (const unsigned char *) s2->fname)
9885                 - (cp1 == (const unsigned char *) s1->fname));
9886
9887       /* Character of current path component the same?  */
9888       else if (*cp1 != *cp2)
9889         return *cp1 - *cp2;
9890     }
9891 }
9892
9893 struct file_name_acquire_data
9894 {
9895   struct file_info *files;
9896   int used_files;
9897   int max_files;
9898 };
9899
9900 /* Traversal function for the hash table.  */
9901
9902 int
9903 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
9904 {
9905   struct dwarf_file_data *d = *slot;
9906   struct file_info *fi;
9907   const char *f;
9908
9909   gcc_assert (fnad->max_files >= d->emitted_number);
9910
9911   if (! d->emitted_number)
9912     return 1;
9913
9914   gcc_assert (fnad->max_files != fnad->used_files);
9915
9916   fi = fnad->files + fnad->used_files++;
9917
9918   /* Skip all leading "./".  */
9919   f = d->filename;
9920   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9921     f += 2;
9922
9923   /* Create a new array entry.  */
9924   fi->path = f;
9925   fi->length = strlen (f);
9926   fi->file_idx = d;
9927
9928   /* Search for the file name part.  */
9929   f = strrchr (f, DIR_SEPARATOR);
9930 #if defined (DIR_SEPARATOR_2)
9931   {
9932     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9933
9934     if (g != NULL)
9935       {
9936         if (f == NULL || f < g)
9937           f = g;
9938       }
9939   }
9940 #endif
9941
9942   fi->fname = f == NULL ? fi->path : f + 1;
9943   return 1;
9944 }
9945
9946 /* Output the directory table and the file name table.  We try to minimize
9947    the total amount of memory needed.  A heuristic is used to avoid large
9948    slowdowns with many input files.  */
9949
9950 static void
9951 output_file_names (void)
9952 {
9953   struct file_name_acquire_data fnad;
9954   int numfiles;
9955   struct file_info *files;
9956   struct dir_info *dirs;
9957   int *saved;
9958   int *savehere;
9959   int *backmap;
9960   int ndirs;
9961   int idx_offset;
9962   int i;
9963
9964   if (!last_emitted_file)
9965     {
9966       dw2_asm_output_data (1, 0, "End directory table");
9967       dw2_asm_output_data (1, 0, "End file name table");
9968       return;
9969     }
9970
9971   numfiles = last_emitted_file->emitted_number;
9972
9973   /* Allocate the various arrays we need.  */
9974   files = XALLOCAVEC (struct file_info, numfiles);
9975   dirs = XALLOCAVEC (struct dir_info, numfiles);
9976
9977   fnad.files = files;
9978   fnad.used_files = 0;
9979   fnad.max_files = numfiles;
9980   file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
9981   gcc_assert (fnad.used_files == fnad.max_files);
9982
9983   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9984
9985   /* Find all the different directories used.  */
9986   dirs[0].path = files[0].path;
9987   dirs[0].length = files[0].fname - files[0].path;
9988   dirs[0].prefix = -1;
9989   dirs[0].count = 1;
9990   dirs[0].dir_idx = 0;
9991   files[0].dir_idx = 0;
9992   ndirs = 1;
9993
9994   for (i = 1; i < numfiles; i++)
9995     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9996         && memcmp (dirs[ndirs - 1].path, files[i].path,
9997                    dirs[ndirs - 1].length) == 0)
9998       {
9999         /* Same directory as last entry.  */
10000         files[i].dir_idx = ndirs - 1;
10001         ++dirs[ndirs - 1].count;
10002       }
10003     else
10004       {
10005         int j;
10006
10007         /* This is a new directory.  */
10008         dirs[ndirs].path = files[i].path;
10009         dirs[ndirs].length = files[i].fname - files[i].path;
10010         dirs[ndirs].count = 1;
10011         dirs[ndirs].dir_idx = ndirs;
10012         files[i].dir_idx = ndirs;
10013
10014         /* Search for a prefix.  */
10015         dirs[ndirs].prefix = -1;
10016         for (j = 0; j < ndirs; j++)
10017           if (dirs[j].length < dirs[ndirs].length
10018               && dirs[j].length > 1
10019               && (dirs[ndirs].prefix == -1
10020                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
10021               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
10022             dirs[ndirs].prefix = j;
10023
10024         ++ndirs;
10025       }
10026
10027   /* Now to the actual work.  We have to find a subset of the directories which
10028      allow expressing the file name using references to the directory table
10029      with the least amount of characters.  We do not do an exhaustive search
10030      where we would have to check out every combination of every single
10031      possible prefix.  Instead we use a heuristic which provides nearly optimal
10032      results in most cases and never is much off.  */
10033   saved = XALLOCAVEC (int, ndirs);
10034   savehere = XALLOCAVEC (int, ndirs);
10035
10036   memset (saved, '\0', ndirs * sizeof (saved[0]));
10037   for (i = 0; i < ndirs; i++)
10038     {
10039       int j;
10040       int total;
10041
10042       /* We can always save some space for the current directory.  But this
10043          does not mean it will be enough to justify adding the directory.  */
10044       savehere[i] = dirs[i].length;
10045       total = (savehere[i] - saved[i]) * dirs[i].count;
10046
10047       for (j = i + 1; j < ndirs; j++)
10048         {
10049           savehere[j] = 0;
10050           if (saved[j] < dirs[i].length)
10051             {
10052               /* Determine whether the dirs[i] path is a prefix of the
10053                  dirs[j] path.  */
10054               int k;
10055
10056               k = dirs[j].prefix;
10057               while (k != -1 && k != (int) i)
10058                 k = dirs[k].prefix;
10059
10060               if (k == (int) i)
10061                 {
10062                   /* Yes it is.  We can possibly save some memory by
10063                      writing the filenames in dirs[j] relative to
10064                      dirs[i].  */
10065                   savehere[j] = dirs[i].length;
10066                   total += (savehere[j] - saved[j]) * dirs[j].count;
10067                 }
10068             }
10069         }
10070
10071       /* Check whether we can save enough to justify adding the dirs[i]
10072          directory.  */
10073       if (total > dirs[i].length + 1)
10074         {
10075           /* It's worthwhile adding.  */
10076           for (j = i; j < ndirs; j++)
10077             if (savehere[j] > 0)
10078               {
10079                 /* Remember how much we saved for this directory so far.  */
10080                 saved[j] = savehere[j];
10081
10082                 /* Remember the prefix directory.  */
10083                 dirs[j].dir_idx = i;
10084               }
10085         }
10086     }
10087
10088   /* Emit the directory name table.  */
10089   idx_offset = dirs[0].length > 0 ? 1 : 0;
10090   for (i = 1 - idx_offset; i < ndirs; i++)
10091     dw2_asm_output_nstring (dirs[i].path,
10092                             dirs[i].length
10093                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
10094                             "Directory Entry: %#x", i + idx_offset);
10095
10096   dw2_asm_output_data (1, 0, "End directory table");
10097
10098   /* We have to emit them in the order of emitted_number since that's
10099      used in the debug info generation.  To do this efficiently we
10100      generate a back-mapping of the indices first.  */
10101   backmap = XALLOCAVEC (int, numfiles);
10102   for (i = 0; i < numfiles; i++)
10103     backmap[files[i].file_idx->emitted_number - 1] = i;
10104
10105   /* Now write all the file names.  */
10106   for (i = 0; i < numfiles; i++)
10107     {
10108       int file_idx = backmap[i];
10109       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
10110
10111 #ifdef VMS_DEBUGGING_INFO
10112 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
10113
10114       /* Setting these fields can lead to debugger miscomparisons,
10115          but VMS Debug requires them to be set correctly.  */
10116
10117       int ver;
10118       long long cdt;
10119       long siz;
10120       int maxfilelen = strlen (files[file_idx].path)
10121                                + dirs[dir_idx].length
10122                                + MAX_VMS_VERSION_LEN + 1;
10123       char *filebuf = XALLOCAVEC (char, maxfilelen);
10124
10125       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
10126       snprintf (filebuf, maxfilelen, "%s;%d",
10127                 files[file_idx].path + dirs[dir_idx].length, ver);
10128
10129       dw2_asm_output_nstring
10130         (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
10131
10132       /* Include directory index.  */
10133       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10134
10135       /* Modification time.  */
10136       dw2_asm_output_data_uleb128
10137         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
10138           ? cdt : 0,
10139          NULL);
10140
10141       /* File length in bytes.  */
10142       dw2_asm_output_data_uleb128
10143         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
10144           ? siz : 0,
10145          NULL);
10146 #else
10147       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
10148                               "File Entry: %#x", (unsigned) i + 1);
10149
10150       /* Include directory index.  */
10151       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10152
10153       /* Modification time.  */
10154       dw2_asm_output_data_uleb128 (0, NULL);
10155
10156       /* File length in bytes.  */
10157       dw2_asm_output_data_uleb128 (0, NULL);
10158 #endif /* VMS_DEBUGGING_INFO */
10159     }
10160
10161   dw2_asm_output_data (1, 0, "End file name table");
10162 }
10163
10164
10165 /* Output one line number table into the .debug_line section.  */
10166
10167 static void
10168 output_one_line_info_table (dw_line_info_table *table)
10169 {
10170   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
10171   unsigned int current_line = 1;
10172   bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
10173   dw_line_info_entry *ent;
10174   size_t i;
10175
10176   FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
10177     {
10178       switch (ent->opcode)
10179         {
10180         case LI_set_address:
10181           /* ??? Unfortunately, we have little choice here currently, and
10182              must always use the most general form.  GCC does not know the
10183              address delta itself, so we can't use DW_LNS_advance_pc.  Many
10184              ports do have length attributes which will give an upper bound
10185              on the address range.  We could perhaps use length attributes
10186              to determine when it is safe to use DW_LNS_fixed_advance_pc.  */
10187           ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
10188
10189           /* This can handle any delta.  This takes
10190              4+DWARF2_ADDR_SIZE bytes.  */
10191           dw2_asm_output_data (1, 0, "set address %s", line_label);
10192           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10193           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10194           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10195           break;
10196
10197         case LI_set_line:
10198           if (ent->val == current_line)
10199             {
10200               /* We still need to start a new row, so output a copy insn.  */
10201               dw2_asm_output_data (1, DW_LNS_copy,
10202                                    "copy line %u", current_line);
10203             }
10204           else
10205             {
10206               int line_offset = ent->val - current_line;
10207               int line_delta = line_offset - DWARF_LINE_BASE;
10208
10209               current_line = ent->val;
10210               if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10211                 {
10212                   /* This can handle deltas from -10 to 234, using the current
10213                      definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
10214                      This takes 1 byte.  */
10215                   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10216                                        "line %u", current_line);
10217                 }
10218               else
10219                 {
10220                   /* This can handle any delta.  This takes at least 4 bytes,
10221                      depending on the value being encoded.  */
10222                   dw2_asm_output_data (1, DW_LNS_advance_line,
10223                                        "advance to line %u", current_line);
10224                   dw2_asm_output_data_sleb128 (line_offset, NULL);
10225                   dw2_asm_output_data (1, DW_LNS_copy, NULL);
10226                 }
10227             }
10228           break;
10229
10230         case LI_set_file:
10231           dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
10232           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10233           break;
10234
10235         case LI_set_column:
10236           dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
10237           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10238           break;
10239
10240         case LI_negate_stmt:
10241           current_is_stmt = !current_is_stmt;
10242           dw2_asm_output_data (1, DW_LNS_negate_stmt,
10243                                "is_stmt %d", current_is_stmt);
10244           break;
10245
10246         case LI_set_prologue_end:
10247           dw2_asm_output_data (1, DW_LNS_set_prologue_end,
10248                                "set prologue end");
10249           break;
10250           
10251         case LI_set_epilogue_begin:
10252           dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
10253                                "set epilogue begin");
10254           break;
10255
10256         case LI_set_discriminator:
10257           dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
10258           dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
10259           dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
10260           dw2_asm_output_data_uleb128 (ent->val, NULL);
10261           break;
10262         }
10263     }
10264
10265   /* Emit debug info for the address of the end of the table.  */
10266   dw2_asm_output_data (1, 0, "set address %s", table->end_label);
10267   dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10268   dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10269   dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10270
10271   dw2_asm_output_data (1, 0, "end sequence");
10272   dw2_asm_output_data_uleb128 (1, NULL);
10273   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10274 }
10275
10276 /* Output the source line number correspondence information.  This
10277    information goes into the .debug_line section.  */
10278
10279 static void
10280 output_line_info (bool prologue_only)
10281 {
10282   char l1[20], l2[20], p1[20], p2[20];
10283   /* We don't support DWARFv5 line tables yet.  */
10284   int ver = dwarf_version < 5 ? dwarf_version : 4;
10285   bool saw_one = false;
10286   int opc;
10287
10288   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10289   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10290   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10291   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10292
10293   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10294     dw2_asm_output_data (4, 0xffffffff,
10295       "Initial length escape value indicating 64-bit DWARF extension");
10296   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10297                         "Length of Source Line Info");
10298   ASM_OUTPUT_LABEL (asm_out_file, l1);
10299
10300   dw2_asm_output_data (2, ver, "DWARF Version");
10301   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10302   ASM_OUTPUT_LABEL (asm_out_file, p1);
10303
10304   /* Define the architecture-dependent minimum instruction length (in bytes).
10305      In this implementation of DWARF, this field is used for information
10306      purposes only.  Since GCC generates assembly language, we have no
10307      a priori knowledge of how many instruction bytes are generated for each
10308      source line, and therefore can use only the DW_LNE_set_address and
10309      DW_LNS_fixed_advance_pc line information commands.  Accordingly, we fix
10310      this as '1', which is "correct enough" for all architectures,
10311      and don't let the target override.  */
10312   dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10313
10314   if (ver >= 4)
10315     dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10316                          "Maximum Operations Per Instruction");
10317   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10318                        "Default is_stmt_start flag");
10319   dw2_asm_output_data (1, DWARF_LINE_BASE,
10320                        "Line Base Value (Special Opcodes)");
10321   dw2_asm_output_data (1, DWARF_LINE_RANGE,
10322                        "Line Range Value (Special Opcodes)");
10323   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10324                        "Special Opcode Base");
10325
10326   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10327     {
10328       int n_op_args;
10329       switch (opc)
10330         {
10331         case DW_LNS_advance_pc:
10332         case DW_LNS_advance_line:
10333         case DW_LNS_set_file:
10334         case DW_LNS_set_column:
10335         case DW_LNS_fixed_advance_pc:
10336         case DW_LNS_set_isa:
10337           n_op_args = 1;
10338           break;
10339         default:
10340           n_op_args = 0;
10341           break;
10342         }
10343
10344       dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10345                            opc, n_op_args);
10346     }
10347
10348   /* Write out the information about the files we use.  */
10349   output_file_names ();
10350   ASM_OUTPUT_LABEL (asm_out_file, p2);
10351   if (prologue_only)
10352     {
10353       /* Output the marker for the end of the line number info.  */
10354       ASM_OUTPUT_LABEL (asm_out_file, l2);
10355       return;
10356     }
10357
10358   if (separate_line_info)
10359     {
10360       dw_line_info_table *table;
10361       size_t i;
10362
10363       FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10364         if (table->in_use)
10365           {
10366             output_one_line_info_table (table);
10367             saw_one = true;
10368           }
10369     }
10370   if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10371     {
10372       output_one_line_info_table (cold_text_section_line_info);
10373       saw_one = true;
10374     }
10375
10376   /* ??? Some Darwin linkers crash on a .debug_line section with no
10377      sequences.  Further, merely a DW_LNE_end_sequence entry is not
10378      sufficient -- the address column must also be initialized.
10379      Make sure to output at least one set_address/end_sequence pair,
10380      choosing .text since that section is always present.  */
10381   if (text_section_line_info->in_use || !saw_one)
10382     output_one_line_info_table (text_section_line_info);
10383
10384   /* Output the marker for the end of the line number info.  */
10385   ASM_OUTPUT_LABEL (asm_out_file, l2);
10386 }
10387 \f
10388 /* Given a pointer to a tree node for some base type, return a pointer to
10389    a DIE that describes the given type.
10390
10391    This routine must only be called for GCC type nodes that correspond to
10392    Dwarf base (fundamental) types.  */
10393
10394 static dw_die_ref
10395 base_type_die (tree type)
10396 {
10397   dw_die_ref base_type_result;
10398   enum dwarf_type encoding;
10399
10400   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10401     return 0;
10402
10403   /* If this is a subtype that should not be emitted as a subrange type,
10404      use the base type.  See subrange_type_for_debug_p.  */
10405   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10406     type = TREE_TYPE (type);
10407
10408   switch (TREE_CODE (type))
10409     {
10410     case INTEGER_TYPE:
10411       if ((dwarf_version >= 4 || !dwarf_strict)
10412           && TYPE_NAME (type)
10413           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10414           && DECL_IS_BUILTIN (TYPE_NAME (type))
10415           && DECL_NAME (TYPE_NAME (type)))
10416         {
10417           const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10418           if (strcmp (name, "char16_t") == 0
10419               || strcmp (name, "char32_t") == 0)
10420             {
10421               encoding = DW_ATE_UTF;
10422               break;
10423             }
10424         }
10425       if (TYPE_STRING_FLAG (type))
10426         {
10427           if (TYPE_UNSIGNED (type))
10428             encoding = DW_ATE_unsigned_char;
10429           else
10430             encoding = DW_ATE_signed_char;
10431         }
10432       else if (TYPE_UNSIGNED (type))
10433         encoding = DW_ATE_unsigned;
10434       else
10435         encoding = DW_ATE_signed;
10436       break;
10437
10438     case REAL_TYPE:
10439       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10440         {
10441           if (dwarf_version >= 3 || !dwarf_strict)
10442             encoding = DW_ATE_decimal_float;
10443           else
10444             encoding = DW_ATE_lo_user;
10445         }
10446       else
10447         encoding = DW_ATE_float;
10448       break;
10449
10450     case FIXED_POINT_TYPE:
10451       if (!(dwarf_version >= 3 || !dwarf_strict))
10452         encoding = DW_ATE_lo_user;
10453       else if (TYPE_UNSIGNED (type))
10454         encoding = DW_ATE_unsigned_fixed;
10455       else
10456         encoding = DW_ATE_signed_fixed;
10457       break;
10458
10459       /* Dwarf2 doesn't know anything about complex ints, so use
10460          a user defined type for it.  */
10461     case COMPLEX_TYPE:
10462       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10463         encoding = DW_ATE_complex_float;
10464       else
10465         encoding = DW_ATE_lo_user;
10466       break;
10467
10468     case BOOLEAN_TYPE:
10469       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
10470       encoding = DW_ATE_boolean;
10471       break;
10472
10473     default:
10474       /* No other TREE_CODEs are Dwarf fundamental types.  */
10475       gcc_unreachable ();
10476     }
10477
10478   base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10479
10480   add_AT_unsigned (base_type_result, DW_AT_byte_size,
10481                    int_size_in_bytes (type));
10482   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10483   add_pubtype (type, base_type_result);
10484
10485   return base_type_result;
10486 }
10487
10488 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
10489    named 'auto' in its type: return true for it, false otherwise.  */
10490
10491 static inline bool
10492 is_cxx_auto (tree type)
10493 {
10494   if (is_cxx ())
10495     {
10496       tree name = TYPE_IDENTIFIER (type);
10497       if (name == get_identifier ("auto")
10498           || name == get_identifier ("decltype(auto)"))
10499         return true;
10500     }
10501   return false;
10502 }
10503
10504 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10505    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
10506
10507 static inline int
10508 is_base_type (tree type)
10509 {
10510   switch (TREE_CODE (type))
10511     {
10512     case ERROR_MARK:
10513     case VOID_TYPE:
10514     case INTEGER_TYPE:
10515     case REAL_TYPE:
10516     case FIXED_POINT_TYPE:
10517     case COMPLEX_TYPE:
10518     case BOOLEAN_TYPE:
10519     case POINTER_BOUNDS_TYPE:
10520       return 1;
10521
10522     case ARRAY_TYPE:
10523     case RECORD_TYPE:
10524     case UNION_TYPE:
10525     case QUAL_UNION_TYPE:
10526     case ENUMERAL_TYPE:
10527     case FUNCTION_TYPE:
10528     case METHOD_TYPE:
10529     case POINTER_TYPE:
10530     case REFERENCE_TYPE:
10531     case NULLPTR_TYPE:
10532     case OFFSET_TYPE:
10533     case LANG_TYPE:
10534     case VECTOR_TYPE:
10535       return 0;
10536
10537     default:
10538       if (is_cxx_auto (type))
10539         return 0;
10540       gcc_unreachable ();
10541     }
10542
10543   return 0;
10544 }
10545
10546 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10547    node, return the size in bits for the type if it is a constant, or else
10548    return the alignment for the type if the type's size is not constant, or
10549    else return BITS_PER_WORD if the type actually turns out to be an
10550    ERROR_MARK node.  */
10551
10552 static inline unsigned HOST_WIDE_INT
10553 simple_type_size_in_bits (const_tree type)
10554 {
10555   if (TREE_CODE (type) == ERROR_MARK)
10556     return BITS_PER_WORD;
10557   else if (TYPE_SIZE (type) == NULL_TREE)
10558     return 0;
10559   else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
10560     return tree_to_uhwi (TYPE_SIZE (type));
10561   else
10562     return TYPE_ALIGN (type);
10563 }
10564
10565 /* Similarly, but return an offset_int instead of UHWI.  */
10566
10567 static inline offset_int
10568 offset_int_type_size_in_bits (const_tree type)
10569 {
10570   if (TREE_CODE (type) == ERROR_MARK)
10571     return BITS_PER_WORD;
10572   else if (TYPE_SIZE (type) == NULL_TREE)
10573     return 0;
10574   else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10575     return wi::to_offset (TYPE_SIZE (type));
10576   else
10577     return TYPE_ALIGN (type);
10578 }
10579
10580 /*  Given a pointer to a tree node for a subrange type, return a pointer
10581     to a DIE that describes the given type.  */
10582
10583 static dw_die_ref
10584 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10585 {
10586   dw_die_ref subrange_die;
10587   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10588
10589   if (context_die == NULL)
10590     context_die = comp_unit_die ();
10591
10592   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10593
10594   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10595     {
10596       /* The size of the subrange type and its base type do not match,
10597          so we need to generate a size attribute for the subrange type.  */
10598       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10599     }
10600
10601   if (low)
10602     add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
10603   if (high)
10604     add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
10605
10606   return subrange_die;
10607 }
10608
10609 /* Returns the (const and/or volatile) cv_qualifiers associated with
10610    the decl node.  This will normally be augmented with the
10611    cv_qualifiers of the underlying type in add_type_attribute.  */
10612
10613 static int
10614 decl_quals (const_tree decl)
10615 {
10616   return ((TREE_READONLY (decl)
10617            ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
10618           | (TREE_THIS_VOLATILE (decl)
10619              ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
10620 }
10621
10622 /* Determine the TYPE whose qualifiers match the largest strict subset
10623    of the given TYPE_QUALS, and return its qualifiers.  Ignore all
10624    qualifiers outside QUAL_MASK.  */
10625
10626 static int
10627 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
10628 {
10629   tree t;
10630   int best_rank = 0, best_qual = 0, max_rank;
10631
10632   type_quals &= qual_mask;
10633   max_rank = popcount_hwi (type_quals) - 1;
10634
10635   for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
10636        t = TYPE_NEXT_VARIANT (t))
10637     {
10638       int q = TYPE_QUALS (t) & qual_mask;
10639
10640       if ((q & type_quals) == q && q != type_quals
10641           && check_base_type (t, type))
10642         {
10643           int rank = popcount_hwi (q);
10644
10645           if (rank > best_rank)
10646             {
10647               best_rank = rank;
10648               best_qual = q;
10649             }
10650         }
10651     }
10652
10653   return best_qual;
10654 }
10655
10656 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10657    entry that chains various modifiers in front of the given type.  */
10658
10659 static dw_die_ref
10660 modified_type_die (tree type, int cv_quals, dw_die_ref context_die)
10661 {
10662   enum tree_code code = TREE_CODE (type);
10663   dw_die_ref mod_type_die;
10664   dw_die_ref sub_die = NULL;
10665   tree item_type = NULL;
10666   tree qualified_type;
10667   tree name, low, high;
10668   dw_die_ref mod_scope;
10669   /* Only these cv-qualifiers are currently handled.  */
10670   const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
10671                             | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
10672
10673   if (code == ERROR_MARK)
10674     return NULL;
10675
10676   cv_quals &= cv_qual_mask;
10677
10678   /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
10679      tag modifier (and not an attribute) old consumers won't be able
10680      to handle it.  */
10681   if (dwarf_version < 3)
10682     cv_quals &= ~TYPE_QUAL_RESTRICT;
10683
10684   /* Likewise for DW_TAG_atomic_type for DWARFv5.  */
10685   if (dwarf_version < 5)
10686     cv_quals &= ~TYPE_QUAL_ATOMIC;
10687
10688   /* See if we already have the appropriately qualified variant of
10689      this type.  */
10690   qualified_type = get_qualified_type (type, cv_quals);
10691
10692   if (qualified_type == sizetype
10693       && TYPE_NAME (qualified_type)
10694       && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10695     {
10696       tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10697
10698       gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10699                            && TYPE_PRECISION (t)
10700                            == TYPE_PRECISION (qualified_type)
10701                            && TYPE_UNSIGNED (t)
10702                            == TYPE_UNSIGNED (qualified_type));
10703       qualified_type = t;
10704     }
10705
10706   /* If we do, then we can just use its DIE, if it exists.  */
10707   if (qualified_type)
10708     {
10709       mod_type_die = lookup_type_die (qualified_type);
10710       if (mod_type_die)
10711         return mod_type_die;
10712     }
10713
10714   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10715
10716   /* Handle C typedef types.  */
10717   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10718       && !DECL_ARTIFICIAL (name))
10719     {
10720       tree dtype = TREE_TYPE (name);
10721
10722       if (qualified_type == dtype)
10723         {
10724           /* For a named type, use the typedef.  */
10725           gen_type_die (qualified_type, context_die);
10726           return lookup_type_die (qualified_type);
10727         }
10728       else
10729         {
10730           int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
10731           dquals &= cv_qual_mask;
10732           if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
10733               || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
10734             /* cv-unqualified version of named type.  Just use
10735                the unnamed type to which it refers.  */
10736             return modified_type_die (DECL_ORIGINAL_TYPE (name),
10737                                       cv_quals, context_die);
10738           /* Else cv-qualified version of named type; fall through.  */
10739         }
10740     }
10741
10742   mod_scope = scope_die_for (type, context_die);
10743
10744   if (cv_quals)
10745     {
10746       struct qual_info { int q; enum dwarf_tag t; };
10747       static const struct qual_info qual_info[] =
10748         {
10749           { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type },
10750           { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
10751           { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
10752           { TYPE_QUAL_CONST, DW_TAG_const_type },
10753         };
10754       int sub_quals;
10755       unsigned i;
10756
10757       /* Determine a lesser qualified type that most closely matches
10758          this one.  Then generate DW_TAG_* entries for the remaining
10759          qualifiers.  */
10760       sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
10761                                                   cv_qual_mask);
10762       mod_type_die = modified_type_die (type, sub_quals, context_die);
10763
10764       for (i = 0; i < sizeof (qual_info) / sizeof (qual_info[0]); i++)
10765         if (qual_info[i].q & cv_quals & ~sub_quals)
10766           {
10767             dw_die_ref d = new_die (qual_info[i].t, mod_scope, type);
10768             if (mod_type_die)
10769               add_AT_die_ref (d, DW_AT_type, mod_type_die);
10770             mod_type_die = d;
10771           }
10772     }
10773   else if (code == POINTER_TYPE)
10774     {
10775       mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10776       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10777                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
10778       item_type = TREE_TYPE (type);
10779       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10780         add_AT_unsigned (mod_type_die, DW_AT_address_class,
10781                          TYPE_ADDR_SPACE (item_type));
10782     }
10783   else if (code == REFERENCE_TYPE)
10784     {
10785       if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10786         mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10787                                 type);
10788       else
10789         mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10790       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10791                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
10792       item_type = TREE_TYPE (type);
10793       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10794         add_AT_unsigned (mod_type_die, DW_AT_address_class,
10795                          TYPE_ADDR_SPACE (item_type));
10796     }
10797   else if (code == INTEGER_TYPE
10798            && TREE_TYPE (type) != NULL_TREE
10799            && subrange_type_for_debug_p (type, &low, &high))
10800     {
10801       mod_type_die = subrange_type_die (type, low, high, context_die);
10802       item_type = TREE_TYPE (type);
10803     }
10804   else if (is_base_type (type))
10805     mod_type_die = base_type_die (type);
10806   else
10807     {
10808       gen_type_die (type, context_die);
10809
10810       /* We have to get the type_main_variant here (and pass that to the
10811          `lookup_type_die' routine) because the ..._TYPE node we have
10812          might simply be a *copy* of some original type node (where the
10813          copy was created to help us keep track of typedef names) and
10814          that copy might have a different TYPE_UID from the original
10815          ..._TYPE node.  */
10816       if (TREE_CODE (type) != VECTOR_TYPE)
10817         return lookup_type_die (type_main_variant (type));
10818       else
10819         /* Vectors have the debugging information in the type,
10820            not the main variant.  */
10821         return lookup_type_die (type);
10822     }
10823
10824   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
10825      don't output a DW_TAG_typedef, since there isn't one in the
10826      user's program; just attach a DW_AT_name to the type.
10827      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10828      if the base type already has the same name.  */
10829   if (name
10830       && ((TREE_CODE (name) != TYPE_DECL
10831            && (qualified_type == TYPE_MAIN_VARIANT (type)
10832                || (cv_quals == TYPE_UNQUALIFIED)))
10833           || (TREE_CODE (name) == TYPE_DECL
10834               && TREE_TYPE (name) == qualified_type
10835               && DECL_NAME (name))))
10836     {
10837       if (TREE_CODE (name) == TYPE_DECL)
10838         /* Could just call add_name_and_src_coords_attributes here,
10839            but since this is a builtin type it doesn't have any
10840            useful source coordinates anyway.  */
10841         name = DECL_NAME (name);
10842       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10843     }
10844   /* This probably indicates a bug.  */
10845   else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10846     {
10847       name = TYPE_IDENTIFIER (type);
10848       add_name_attribute (mod_type_die,
10849                           name ? IDENTIFIER_POINTER (name) : "__unknown__");
10850     }
10851
10852   if (qualified_type)
10853     equate_type_number_to_die (qualified_type, mod_type_die);
10854
10855   if (item_type)
10856     /* We must do this after the equate_type_number_to_die call, in case
10857        this is a recursive type.  This ensures that the modified_type_die
10858        recursion will terminate even if the type is recursive.  Recursive
10859        types are possible in Ada.  */
10860     sub_die = modified_type_die (item_type,
10861                                  TYPE_QUALS_NO_ADDR_SPACE (item_type),
10862                                  context_die);
10863
10864   if (sub_die != NULL)
10865     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10866
10867   add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10868   if (TYPE_ARTIFICIAL (type))
10869     add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10870
10871   return mod_type_die;
10872 }
10873
10874 /* Generate DIEs for the generic parameters of T.
10875    T must be either a generic type or a generic function.
10876    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
10877
10878 static void
10879 gen_generic_params_dies (tree t)
10880 {
10881   tree parms, args;
10882   int parms_num, i;
10883   dw_die_ref die = NULL;
10884   int non_default;
10885
10886   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10887     return;
10888
10889   if (TYPE_P (t))
10890     die = lookup_type_die (t);
10891   else if (DECL_P (t))
10892     die = lookup_decl_die (t);
10893
10894   gcc_assert (die);
10895
10896   parms = lang_hooks.get_innermost_generic_parms (t);
10897   if (!parms)
10898     /* T has no generic parameter. It means T is neither a generic type
10899        or function. End of story.  */
10900     return;
10901
10902   parms_num = TREE_VEC_LENGTH (parms);
10903   args = lang_hooks.get_innermost_generic_args (t);
10904   if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
10905     non_default = int_cst_value (TREE_CHAIN (args));
10906   else
10907     non_default = TREE_VEC_LENGTH (args);
10908   for (i = 0; i < parms_num; i++)
10909     {
10910       tree parm, arg, arg_pack_elems;
10911       dw_die_ref parm_die;
10912
10913       parm = TREE_VEC_ELT (parms, i);
10914       arg = TREE_VEC_ELT (args, i);
10915       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10916       gcc_assert (parm && TREE_VALUE (parm) && arg);
10917
10918       if (parm && TREE_VALUE (parm) && arg)
10919         {
10920           /* If PARM represents a template parameter pack,
10921              emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10922              by DW_TAG_template_*_parameter DIEs for the argument
10923              pack elements of ARG. Note that ARG would then be
10924              an argument pack.  */
10925           if (arg_pack_elems)
10926             parm_die = template_parameter_pack_die (TREE_VALUE (parm),
10927                                                     arg_pack_elems,
10928                                                     die);
10929           else
10930             parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
10931                                               true /* emit name */, die);
10932           if (i >= non_default)
10933             add_AT_flag (parm_die, DW_AT_default_value, 1);
10934         }
10935     }
10936 }
10937
10938 /* Create and return a DIE for PARM which should be
10939    the representation of a generic type parameter.
10940    For instance, in the C++ front end, PARM would be a template parameter.
10941    ARG is the argument to PARM.
10942    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
10943    name of the PARM.
10944    PARENT_DIE is the parent DIE which the new created DIE should be added to,
10945    as a child node.  */
10946
10947 static dw_die_ref
10948 generic_parameter_die (tree parm, tree arg,
10949                        bool emit_name_p,
10950                        dw_die_ref parent_die)
10951 {
10952   dw_die_ref tmpl_die = NULL;
10953   const char *name = NULL;
10954
10955   if (!parm || !DECL_NAME (parm) || !arg)
10956     return NULL;
10957
10958   /* We support non-type generic parameters and arguments,
10959      type generic parameters and arguments, as well as
10960      generic generic parameters (a.k.a. template template parameters in C++)
10961      and arguments.  */
10962   if (TREE_CODE (parm) == PARM_DECL)
10963     /* PARM is a nontype generic parameter  */
10964     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10965   else if (TREE_CODE (parm) == TYPE_DECL)
10966     /* PARM is a type generic parameter.  */
10967     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10968   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10969     /* PARM is a generic generic parameter.
10970        Its DIE is a GNU extension. It shall have a
10971        DW_AT_name attribute to represent the name of the template template
10972        parameter, and a DW_AT_GNU_template_name attribute to represent the
10973        name of the template template argument.  */
10974     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10975                         parent_die, parm);
10976   else
10977     gcc_unreachable ();
10978
10979   if (tmpl_die)
10980     {
10981       tree tmpl_type;
10982
10983       /* If PARM is a generic parameter pack, it means we are
10984          emitting debug info for a template argument pack element.
10985          In other terms, ARG is a template argument pack element.
10986          In that case, we don't emit any DW_AT_name attribute for
10987          the die.  */
10988       if (emit_name_p)
10989         {
10990           name = IDENTIFIER_POINTER (DECL_NAME (parm));
10991           gcc_assert (name);
10992           add_AT_string (tmpl_die, DW_AT_name, name);
10993         }
10994
10995       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10996         {
10997           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10998              TMPL_DIE should have a child DW_AT_type attribute that is set
10999              to the type of the argument to PARM, which is ARG.
11000              If PARM is a type generic parameter, TMPL_DIE should have a
11001              child DW_AT_type that is set to ARG.  */
11002           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
11003           add_type_attribute (tmpl_die, tmpl_type,
11004                               (TREE_THIS_VOLATILE (tmpl_type)
11005                                ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
11006                               parent_die);
11007         }
11008       else
11009         {
11010           /* So TMPL_DIE is a DIE representing a
11011              a generic generic template parameter, a.k.a template template
11012              parameter in C++ and arg is a template.  */
11013
11014           /* The DW_AT_GNU_template_name attribute of the DIE must be set
11015              to the name of the argument.  */
11016           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
11017           if (name)
11018             add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
11019         }
11020
11021       if (TREE_CODE (parm) == PARM_DECL)
11022         /* So PARM is a non-type generic parameter.
11023            DWARF3 5.6.8 says we must set a DW_AT_const_value child
11024            attribute of TMPL_DIE which value represents the value
11025            of ARG.
11026            We must be careful here:
11027            The value of ARG might reference some function decls.
11028            We might currently be emitting debug info for a generic
11029            type and types are emitted before function decls, we don't
11030            know if the function decls referenced by ARG will actually be
11031            emitted after cgraph computations.
11032            So must defer the generation of the DW_AT_const_value to
11033            after cgraph is ready.  */
11034         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
11035     }
11036
11037   return tmpl_die;
11038 }
11039
11040 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
11041    PARM_PACK must be a template parameter pack. The returned DIE
11042    will be child DIE of PARENT_DIE.  */
11043
11044 static dw_die_ref
11045 template_parameter_pack_die (tree parm_pack,
11046                              tree parm_pack_args,
11047                              dw_die_ref parent_die)
11048 {
11049   dw_die_ref die;
11050   int j;
11051
11052   gcc_assert (parent_die && parm_pack);
11053
11054   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
11055   add_name_and_src_coords_attributes (die, parm_pack);
11056   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
11057     generic_parameter_die (parm_pack,
11058                            TREE_VEC_ELT (parm_pack_args, j),
11059                            false /* Don't emit DW_AT_name */,
11060                            die);
11061   return die;
11062 }
11063
11064 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
11065    an enumerated type.  */
11066
11067 static inline int
11068 type_is_enum (const_tree type)
11069 {
11070   return TREE_CODE (type) == ENUMERAL_TYPE;
11071 }
11072
11073 /* Return the DBX register number described by a given RTL node.  */
11074
11075 static unsigned int
11076 dbx_reg_number (const_rtx rtl)
11077 {
11078   unsigned regno = REGNO (rtl);
11079
11080   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
11081
11082 #ifdef LEAF_REG_REMAP
11083   if (crtl->uses_only_leaf_regs)
11084     {
11085       int leaf_reg = LEAF_REG_REMAP (regno);
11086       if (leaf_reg != -1)
11087         regno = (unsigned) leaf_reg;
11088     }
11089 #endif
11090
11091   regno = DBX_REGISTER_NUMBER (regno);
11092   gcc_assert (regno != INVALID_REGNUM);
11093   return regno;
11094 }
11095
11096 /* Optionally add a DW_OP_piece term to a location description expression.
11097    DW_OP_piece is only added if the location description expression already
11098    doesn't end with DW_OP_piece.  */
11099
11100 static void
11101 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
11102 {
11103   dw_loc_descr_ref loc;
11104
11105   if (*list_head != NULL)
11106     {
11107       /* Find the end of the chain.  */
11108       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
11109         ;
11110
11111       if (loc->dw_loc_opc != DW_OP_piece)
11112         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
11113     }
11114 }
11115
11116 /* Return a location descriptor that designates a machine register or
11117    zero if there is none.  */
11118
11119 static dw_loc_descr_ref
11120 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
11121 {
11122   rtx regs;
11123
11124   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
11125     return 0;
11126
11127   /* We only use "frame base" when we're sure we're talking about the
11128      post-prologue local stack frame.  We do this by *not* running
11129      register elimination until this point, and recognizing the special
11130      argument pointer and soft frame pointer rtx's.
11131      Use DW_OP_fbreg offset DW_OP_stack_value in this case.  */
11132   if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
11133       && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
11134     {
11135       dw_loc_descr_ref result = NULL;
11136
11137       if (dwarf_version >= 4 || !dwarf_strict)
11138         {
11139           result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
11140                                        initialized);
11141           if (result)
11142             add_loc_descr (&result,
11143                            new_loc_descr (DW_OP_stack_value, 0, 0));
11144         }
11145       return result;
11146     }
11147
11148   regs = targetm.dwarf_register_span (rtl);
11149
11150   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
11151     return multiple_reg_loc_descriptor (rtl, regs, initialized);
11152   else
11153     {
11154       unsigned int dbx_regnum = dbx_reg_number (rtl);
11155       if (dbx_regnum == IGNORED_DWARF_REGNUM)
11156         return 0;
11157       return one_reg_loc_descriptor (dbx_regnum, initialized);
11158     }
11159 }
11160
11161 /* Return a location descriptor that designates a machine register for
11162    a given hard register number.  */
11163
11164 static dw_loc_descr_ref
11165 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
11166 {
11167   dw_loc_descr_ref reg_loc_descr;
11168
11169   if (regno <= 31)
11170     reg_loc_descr
11171       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
11172   else
11173     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
11174
11175   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11176     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11177
11178   return reg_loc_descr;
11179 }
11180
11181 /* Given an RTL of a register, return a location descriptor that
11182    designates a value that spans more than one register.  */
11183
11184 static dw_loc_descr_ref
11185 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
11186                              enum var_init_status initialized)
11187 {
11188   int size, i;
11189   dw_loc_descr_ref loc_result = NULL;
11190
11191   /* Simple, contiguous registers.  */
11192   if (regs == NULL_RTX)
11193     {
11194       unsigned reg = REGNO (rtl);
11195       int nregs;
11196
11197 #ifdef LEAF_REG_REMAP
11198       if (crtl->uses_only_leaf_regs)
11199         {
11200           int leaf_reg = LEAF_REG_REMAP (reg);
11201           if (leaf_reg != -1)
11202             reg = (unsigned) leaf_reg;
11203         }
11204 #endif
11205
11206       gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
11207       nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
11208
11209       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
11210
11211       loc_result = NULL;
11212       while (nregs--)
11213         {
11214           dw_loc_descr_ref t;
11215
11216           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
11217                                       VAR_INIT_STATUS_INITIALIZED);
11218           add_loc_descr (&loc_result, t);
11219           add_loc_descr_op_piece (&loc_result, size);
11220           ++reg;
11221         }
11222       return loc_result;
11223     }
11224
11225   /* Now onto stupid register sets in non contiguous locations.  */
11226
11227   gcc_assert (GET_CODE (regs) == PARALLEL);
11228
11229   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
11230   loc_result = NULL;
11231
11232   for (i = 0; i < XVECLEN (regs, 0); ++i)
11233     {
11234       dw_loc_descr_ref t;
11235
11236       t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
11237                                   VAR_INIT_STATUS_INITIALIZED);
11238       add_loc_descr (&loc_result, t);
11239       add_loc_descr_op_piece (&loc_result, size);
11240     }
11241
11242   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11243     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11244   return loc_result;
11245 }
11246
11247 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
11248
11249 /* Return a location descriptor that designates a constant i,
11250    as a compound operation from constant (i >> shift), constant shift
11251    and DW_OP_shl.  */
11252
11253 static dw_loc_descr_ref
11254 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11255 {
11256   dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
11257   add_loc_descr (&ret, int_loc_descriptor (shift));
11258   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11259   return ret;
11260 }
11261
11262 /* Return a location descriptor that designates a constant.  */
11263
11264 static dw_loc_descr_ref
11265 int_loc_descriptor (HOST_WIDE_INT i)
11266 {
11267   enum dwarf_location_atom op;
11268
11269   /* Pick the smallest representation of a constant, rather than just
11270      defaulting to the LEB encoding.  */
11271   if (i >= 0)
11272     {
11273       int clz = clz_hwi (i);
11274       int ctz = ctz_hwi (i);
11275       if (i <= 31)
11276         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
11277       else if (i <= 0xff)
11278         op = DW_OP_const1u;
11279       else if (i <= 0xffff)
11280         op = DW_OP_const2u;
11281       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11282                && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11283         /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
11284            DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
11285            while DW_OP_const4u is 5 bytes.  */
11286         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
11287       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11288                && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11289         /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
11290            while DW_OP_const4u is 5 bytes.  */
11291         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11292       else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11293         op = DW_OP_const4u;
11294       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11295                && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11296         /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
11297            while DW_OP_constu of constant >= 0x100000000 takes at least
11298            6 bytes.  */
11299         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11300       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11301                && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
11302                   >= HOST_BITS_PER_WIDE_INT)
11303         /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
11304            DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
11305            while DW_OP_constu takes in this case at least 6 bytes.  */
11306         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
11307       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11308                && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11309                && size_of_uleb128 (i) > 6)
11310         /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes.  */
11311         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
11312       else
11313         op = DW_OP_constu;
11314     }
11315   else
11316     {
11317       if (i >= -0x80)
11318         op = DW_OP_const1s;
11319       else if (i >= -0x8000)
11320         op = DW_OP_const2s;
11321       else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11322         {
11323           if (size_of_int_loc_descriptor (i) < 5)
11324             {
11325               dw_loc_descr_ref ret = int_loc_descriptor (-i);
11326               add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11327               return ret;
11328             }
11329           op = DW_OP_const4s;
11330         }
11331       else
11332         {
11333           if (size_of_int_loc_descriptor (i)
11334               < (unsigned long) 1 + size_of_sleb128 (i))
11335             {
11336               dw_loc_descr_ref ret = int_loc_descriptor (-i);
11337               add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11338               return ret;
11339             }
11340           op = DW_OP_consts;
11341         }
11342     }
11343
11344   return new_loc_descr (op, i, 0);
11345 }
11346
11347 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
11348    without actually allocating it.  */
11349
11350 static unsigned long
11351 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11352 {
11353   return size_of_int_loc_descriptor (i >> shift)
11354          + size_of_int_loc_descriptor (shift)
11355          + 1;
11356 }
11357
11358 /* Return size_of_locs (int_loc_descriptor (i)) without
11359    actually allocating it.  */
11360
11361 static unsigned long
11362 size_of_int_loc_descriptor (HOST_WIDE_INT i)
11363 {
11364   unsigned long s;
11365
11366   if (i >= 0)
11367     {
11368       int clz, ctz;
11369       if (i <= 31)
11370         return 1;
11371       else if (i <= 0xff)
11372         return 2;
11373       else if (i <= 0xffff)
11374         return 3;
11375       clz = clz_hwi (i);
11376       ctz = ctz_hwi (i);
11377       if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11378           && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11379         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11380                                                     - clz - 5);
11381       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11382                && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11383         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11384                                                     - clz - 8);
11385       else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11386         return 5;
11387       s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
11388       if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11389           && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11390         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11391                                                     - clz - 8);
11392       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11393                && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
11394         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11395                                                     - clz - 16);
11396       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11397                && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11398                && s > 6)
11399         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11400                                                     - clz - 32);
11401       else
11402         return 1 + s;
11403     }
11404   else
11405     {
11406       if (i >= -0x80)
11407         return 2;
11408       else if (i >= -0x8000)
11409         return 3;
11410       else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11411         {
11412           if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11413             {
11414               s = size_of_int_loc_descriptor (-i) + 1;
11415               if (s < 5)
11416                 return s;
11417             }
11418           return 5;
11419         }
11420       else
11421         {
11422           unsigned long r = 1 + size_of_sleb128 (i);
11423           if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11424             {
11425               s = size_of_int_loc_descriptor (-i) + 1;
11426               if (s < r)
11427                 return s;
11428             }
11429           return r;
11430         }
11431     }
11432 }
11433
11434 /* Return loc description representing "address" of integer value.
11435    This can appear only as toplevel expression.  */
11436
11437 static dw_loc_descr_ref
11438 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
11439 {
11440   int litsize;
11441   dw_loc_descr_ref loc_result = NULL;
11442
11443   if (!(dwarf_version >= 4 || !dwarf_strict))
11444     return NULL;
11445
11446   litsize = size_of_int_loc_descriptor (i);
11447   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
11448      is more compact.  For DW_OP_stack_value we need:
11449      litsize + 1 (DW_OP_stack_value)
11450      and for DW_OP_implicit_value:
11451      1 (DW_OP_implicit_value) + 1 (length) + size.  */
11452   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
11453     {
11454       loc_result = int_loc_descriptor (i);
11455       add_loc_descr (&loc_result,
11456                      new_loc_descr (DW_OP_stack_value, 0, 0));
11457       return loc_result;
11458     }
11459
11460   loc_result = new_loc_descr (DW_OP_implicit_value,
11461                               size, 0);
11462   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11463   loc_result->dw_loc_oprnd2.v.val_int = i;
11464   return loc_result;
11465 }
11466
11467 /* Return a location descriptor that designates a base+offset location.  */
11468
11469 static dw_loc_descr_ref
11470 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
11471                  enum var_init_status initialized)
11472 {
11473   unsigned int regno;
11474   dw_loc_descr_ref result;
11475   dw_fde_ref fde = cfun->fde;
11476
11477   /* We only use "frame base" when we're sure we're talking about the
11478      post-prologue local stack frame.  We do this by *not* running
11479      register elimination until this point, and recognizing the special
11480      argument pointer and soft frame pointer rtx's.  */
11481   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
11482     {
11483       rtx elim = (ira_use_lra_p
11484                   ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
11485                   : eliminate_regs (reg, VOIDmode, NULL_RTX));
11486
11487       if (elim != reg)
11488         {
11489           if (GET_CODE (elim) == PLUS)
11490             {
11491               offset += INTVAL (XEXP (elim, 1));
11492               elim = XEXP (elim, 0);
11493             }
11494           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11495                        && (elim == hard_frame_pointer_rtx
11496                            || elim == stack_pointer_rtx))
11497                       || elim == (frame_pointer_needed
11498                                   ? hard_frame_pointer_rtx
11499                                   : stack_pointer_rtx));
11500
11501           /* If drap register is used to align stack, use frame
11502              pointer + offset to access stack variables.  If stack
11503              is aligned without drap, use stack pointer + offset to
11504              access stack variables.  */
11505           if (crtl->stack_realign_tried
11506               && reg == frame_pointer_rtx)
11507             {
11508               int base_reg
11509                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
11510                                       ? HARD_FRAME_POINTER_REGNUM
11511                                       : REGNO (elim));
11512               return new_reg_loc_descr (base_reg, offset);
11513             }
11514
11515           gcc_assert (frame_pointer_fb_offset_valid);
11516           offset += frame_pointer_fb_offset;
11517           return new_loc_descr (DW_OP_fbreg, offset, 0);
11518         }
11519     }
11520
11521   regno = REGNO (reg);
11522 #ifdef LEAF_REG_REMAP
11523   if (crtl->uses_only_leaf_regs)
11524     {
11525       int leaf_reg = LEAF_REG_REMAP (regno);
11526       if (leaf_reg != -1)
11527         regno = (unsigned) leaf_reg;
11528     }
11529 #endif
11530   regno = DWARF_FRAME_REGNUM (regno);
11531
11532   if (!optimize && fde
11533       && (fde->drap_reg == regno || fde->vdrap_reg == regno))
11534     {
11535       /* Use cfa+offset to represent the location of arguments passed
11536          on the stack when drap is used to align stack.
11537          Only do this when not optimizing, for optimized code var-tracking
11538          is supposed to track where the arguments live and the register
11539          used as vdrap or drap in some spot might be used for something
11540          else in other part of the routine.  */
11541       return new_loc_descr (DW_OP_fbreg, offset, 0);
11542     }
11543
11544   if (regno <= 31)
11545     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11546                             offset, 0);
11547   else
11548     result = new_loc_descr (DW_OP_bregx, regno, offset);
11549
11550   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11551     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11552
11553   return result;
11554 }
11555
11556 /* Return true if this RTL expression describes a base+offset calculation.  */
11557
11558 static inline int
11559 is_based_loc (const_rtx rtl)
11560 {
11561   return (GET_CODE (rtl) == PLUS
11562           && ((REG_P (XEXP (rtl, 0))
11563                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11564                && CONST_INT_P (XEXP (rtl, 1)))));
11565 }
11566
11567 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11568    failed.  */
11569
11570 static dw_loc_descr_ref
11571 tls_mem_loc_descriptor (rtx mem)
11572 {
11573   tree base;
11574   dw_loc_descr_ref loc_result;
11575
11576   if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11577     return NULL;
11578
11579   base = get_base_address (MEM_EXPR (mem));
11580   if (base == NULL
11581       || TREE_CODE (base) != VAR_DECL
11582       || !DECL_THREAD_LOCAL_P (base))
11583     return NULL;
11584
11585   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
11586   if (loc_result == NULL)
11587     return NULL;
11588
11589   if (MEM_OFFSET (mem))
11590     loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11591
11592   return loc_result;
11593 }
11594
11595 /* Output debug info about reason why we failed to expand expression as dwarf
11596    expression.  */
11597
11598 static void
11599 expansion_failed (tree expr, rtx rtl, char const *reason)
11600 {
11601   if (dump_file && (dump_flags & TDF_DETAILS))
11602     {
11603       fprintf (dump_file, "Failed to expand as dwarf: ");
11604       if (expr)
11605         print_generic_expr (dump_file, expr, dump_flags);
11606       if (rtl)
11607         {
11608           fprintf (dump_file, "\n");
11609           print_rtl (dump_file, rtl);
11610         }
11611       fprintf (dump_file, "\nReason: %s\n", reason);
11612     }
11613 }
11614
11615 /* Helper function for const_ok_for_output.  */
11616
11617 static bool
11618 const_ok_for_output_1 (rtx rtl)
11619 {
11620   if (GET_CODE (rtl) == UNSPEC)
11621     {
11622       /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11623          we can't express it in the debug info.  */
11624 #ifdef ENABLE_CHECKING
11625       /* Don't complain about TLS UNSPECs, those are just too hard to
11626          delegitimize.  Note this could be a non-decl SYMBOL_REF such as
11627          one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11628          rather than DECL_THREAD_LOCAL_P is not just an optimization.  */
11629       if (XVECLEN (rtl, 0) == 0
11630           || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11631           || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11632         inform (current_function_decl
11633                 ? DECL_SOURCE_LOCATION (current_function_decl)
11634                 : UNKNOWN_LOCATION,
11635 #if NUM_UNSPEC_VALUES > 0
11636                 "non-delegitimized UNSPEC %s (%d) found in variable location",
11637                 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11638                  ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11639                 XINT (rtl, 1));
11640 #else
11641                 "non-delegitimized UNSPEC %d found in variable location",
11642                 XINT (rtl, 1));
11643 #endif
11644 #endif
11645       expansion_failed (NULL_TREE, rtl,
11646                         "UNSPEC hasn't been delegitimized.\n");
11647       return false;
11648     }
11649
11650   if (targetm.const_not_ok_for_debug_p (rtl))
11651     {
11652       expansion_failed (NULL_TREE, rtl,
11653                         "Expression rejected for debug by the backend.\n");
11654       return false;
11655     }
11656
11657   /* FIXME: Refer to PR60655. It is possible for simplification
11658      of rtl expressions in var tracking to produce such expressions.
11659      We should really identify / validate expressions
11660      enclosed in CONST that can be handled by assemblers on various
11661      targets and only handle legitimate cases here.  */
11662   if (GET_CODE (rtl) != SYMBOL_REF)
11663     {
11664       if (GET_CODE (rtl) == NOT)
11665         return false;
11666       return true;
11667     }
11668
11669   if (CONSTANT_POOL_ADDRESS_P (rtl))
11670     {
11671       bool marked;
11672       get_pool_constant_mark (rtl, &marked);
11673       /* If all references to this pool constant were optimized away,
11674          it was not output and thus we can't represent it.  */
11675       if (!marked)
11676         {
11677           expansion_failed (NULL_TREE, rtl,
11678                             "Constant was removed from constant pool.\n");
11679           return false;
11680         }
11681     }
11682
11683   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11684     return false;
11685
11686   /* Avoid references to external symbols in debug info, on several targets
11687      the linker might even refuse to link when linking a shared library,
11688      and in many other cases the relocations for .debug_info/.debug_loc are
11689      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
11690      to be defined within the same shared library or executable are fine.  */
11691   if (SYMBOL_REF_EXTERNAL_P (rtl))
11692     {
11693       tree decl = SYMBOL_REF_DECL (rtl);
11694
11695       if (decl == NULL || !targetm.binds_local_p (decl))
11696         {
11697           expansion_failed (NULL_TREE, rtl,
11698                             "Symbol not defined in current TU.\n");
11699           return false;
11700         }
11701     }
11702
11703   return true;
11704 }
11705
11706 /* Return true if constant RTL can be emitted in DW_OP_addr or
11707    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
11708    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
11709
11710 static bool
11711 const_ok_for_output (rtx rtl)
11712 {
11713   if (GET_CODE (rtl) == SYMBOL_REF)
11714     return const_ok_for_output_1 (rtl);
11715
11716   if (GET_CODE (rtl) == CONST)
11717     {
11718       subrtx_var_iterator::array_type array;
11719       FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
11720         if (!const_ok_for_output_1 (*iter))
11721           return false;
11722       return true;
11723     }
11724
11725   return true;
11726 }
11727
11728 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11729    if possible, NULL otherwise.  */
11730
11731 static dw_die_ref
11732 base_type_for_mode (machine_mode mode, bool unsignedp)
11733 {
11734   dw_die_ref type_die;
11735   tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11736
11737   if (type == NULL)
11738     return NULL;
11739   switch (TREE_CODE (type))
11740     {
11741     case INTEGER_TYPE:
11742     case REAL_TYPE:
11743       break;
11744     default:
11745       return NULL;
11746     }
11747   type_die = lookup_type_die (type);
11748   if (!type_die)
11749     type_die = modified_type_die (type, TYPE_UNQUALIFIED, comp_unit_die ());
11750   if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11751     return NULL;
11752   return type_die;
11753 }
11754
11755 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11756    type matching MODE, or, if MODE is narrower than or as wide as
11757    DWARF2_ADDR_SIZE, untyped.  Return NULL if the conversion is not
11758    possible.  */
11759
11760 static dw_loc_descr_ref
11761 convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
11762 {
11763   machine_mode outer_mode = mode;
11764   dw_die_ref type_die;
11765   dw_loc_descr_ref cvt;
11766
11767   if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11768     {
11769       add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11770       return op;
11771     }
11772   type_die = base_type_for_mode (outer_mode, 1);
11773   if (type_die == NULL)
11774     return NULL;
11775   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11776   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11777   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11778   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11779   add_loc_descr (&op, cvt);
11780   return op;
11781 }
11782
11783 /* Return location descriptor for comparison OP with operands OP0 and OP1.  */
11784
11785 static dw_loc_descr_ref
11786 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11787                         dw_loc_descr_ref op1)
11788 {
11789   dw_loc_descr_ref ret = op0;
11790   add_loc_descr (&ret, op1);
11791   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11792   if (STORE_FLAG_VALUE != 1)
11793     {
11794       add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11795       add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11796     }
11797   return ret;
11798 }
11799
11800 /* Return location descriptor for signed comparison OP RTL.  */
11801
11802 static dw_loc_descr_ref
11803 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11804                          machine_mode mem_mode)
11805 {
11806   machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11807   dw_loc_descr_ref op0, op1;
11808   int shift;
11809
11810   if (op_mode == VOIDmode)
11811     op_mode = GET_MODE (XEXP (rtl, 1));
11812   if (op_mode == VOIDmode)
11813     return NULL;
11814
11815   if (dwarf_strict
11816       && (GET_MODE_CLASS (op_mode) != MODE_INT
11817           || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11818     return NULL;
11819
11820   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11821                             VAR_INIT_STATUS_INITIALIZED);
11822   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11823                             VAR_INIT_STATUS_INITIALIZED);
11824
11825   if (op0 == NULL || op1 == NULL)
11826     return NULL;
11827
11828   if (GET_MODE_CLASS (op_mode) != MODE_INT
11829       || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11830     return compare_loc_descriptor (op, op0, op1);
11831
11832   if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11833     {
11834       dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11835       dw_loc_descr_ref cvt;
11836
11837       if (type_die == NULL)
11838         return NULL;
11839       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11840       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11841       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11842       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11843       add_loc_descr (&op0, cvt);
11844       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11845       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11846       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11847       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11848       add_loc_descr (&op1, cvt);
11849       return compare_loc_descriptor (op, op0, op1);
11850     }
11851
11852   shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11853   /* For eq/ne, if the operands are known to be zero-extended,
11854      there is no need to do the fancy shifting up.  */
11855   if (op == DW_OP_eq || op == DW_OP_ne)
11856     {
11857       dw_loc_descr_ref last0, last1;
11858       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11859         ;
11860       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11861         ;
11862       /* deref_size zero extends, and for constants we can check
11863          whether they are zero extended or not.  */
11864       if (((last0->dw_loc_opc == DW_OP_deref_size
11865             && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11866            || (CONST_INT_P (XEXP (rtl, 0))
11867                && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11868                   == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11869           && ((last1->dw_loc_opc == DW_OP_deref_size
11870                && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11871               || (CONST_INT_P (XEXP (rtl, 1))
11872                   && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11873                      == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11874         return compare_loc_descriptor (op, op0, op1);
11875
11876       /* EQ/NE comparison against constant in narrower type than
11877          DWARF2_ADDR_SIZE can be performed either as
11878          DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11879          DW_OP_{eq,ne}
11880          or
11881          DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11882          DW_OP_{eq,ne}.  Pick whatever is shorter.  */
11883       if (CONST_INT_P (XEXP (rtl, 1))
11884           && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11885           && (size_of_int_loc_descriptor (shift) + 1
11886               + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11887               >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11888                  + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11889                                                & GET_MODE_MASK (op_mode))))
11890         {
11891           add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11892           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11893           op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11894                                     & GET_MODE_MASK (op_mode));
11895           return compare_loc_descriptor (op, op0, op1);
11896         }
11897     }
11898   add_loc_descr (&op0, int_loc_descriptor (shift));
11899   add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11900   if (CONST_INT_P (XEXP (rtl, 1)))
11901     op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11902   else
11903     {
11904       add_loc_descr (&op1, int_loc_descriptor (shift));
11905       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11906     }
11907   return compare_loc_descriptor (op, op0, op1);
11908 }
11909
11910 /* Return location descriptor for unsigned comparison OP RTL.  */
11911
11912 static dw_loc_descr_ref
11913 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11914                          machine_mode mem_mode)
11915 {
11916   machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11917   dw_loc_descr_ref op0, op1;
11918
11919   if (op_mode == VOIDmode)
11920     op_mode = GET_MODE (XEXP (rtl, 1));
11921   if (op_mode == VOIDmode)
11922     return NULL;
11923   if (GET_MODE_CLASS (op_mode) != MODE_INT)
11924     return NULL;
11925
11926   if (dwarf_strict && 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_SIZE (op_mode) < DWARF2_ADDR_SIZE)
11938     {
11939       HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
11940       dw_loc_descr_ref last0, last1;
11941       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11942         ;
11943       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11944         ;
11945       if (CONST_INT_P (XEXP (rtl, 0)))
11946         op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
11947       /* deref_size zero extends, so no need to mask it again.  */
11948       else if (last0->dw_loc_opc != DW_OP_deref_size
11949                || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11950         {
11951           add_loc_descr (&op0, int_loc_descriptor (mask));
11952           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11953         }
11954       if (CONST_INT_P (XEXP (rtl, 1)))
11955         op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11956       /* deref_size zero extends, so no need to mask it again.  */
11957       else if (last1->dw_loc_opc != DW_OP_deref_size
11958                || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11959         {
11960           add_loc_descr (&op1, int_loc_descriptor (mask));
11961           add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11962         }
11963     }
11964   else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11965     {
11966       HOST_WIDE_INT bias = 1;
11967       bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11968       add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11969       if (CONST_INT_P (XEXP (rtl, 1)))
11970         op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11971                                   + INTVAL (XEXP (rtl, 1)));
11972       else
11973         add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
11974                                             bias, 0));
11975     }
11976   return compare_loc_descriptor (op, op0, op1);
11977 }
11978
11979 /* Return location descriptor for {U,S}{MIN,MAX}.  */
11980
11981 static dw_loc_descr_ref
11982 minmax_loc_descriptor (rtx rtl, machine_mode mode,
11983                        machine_mode mem_mode)
11984 {
11985   enum dwarf_location_atom op;
11986   dw_loc_descr_ref op0, op1, ret;
11987   dw_loc_descr_ref bra_node, drop_node;
11988
11989   if (dwarf_strict
11990       && (GET_MODE_CLASS (mode) != MODE_INT
11991           || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
11992     return NULL;
11993
11994   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11995                             VAR_INIT_STATUS_INITIALIZED);
11996   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11997                             VAR_INIT_STATUS_INITIALIZED);
11998
11999   if (op0 == NULL || op1 == NULL)
12000     return NULL;
12001
12002   add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
12003   add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
12004   add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
12005   if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
12006     {
12007       if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12008         {
12009           HOST_WIDE_INT mask = GET_MODE_MASK (mode);
12010           add_loc_descr (&op0, int_loc_descriptor (mask));
12011           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12012           add_loc_descr (&op1, int_loc_descriptor (mask));
12013           add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12014         }
12015       else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12016         {
12017           HOST_WIDE_INT bias = 1;
12018           bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12019           add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12020           add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12021         }
12022     }
12023   else if (GET_MODE_CLASS (mode) == MODE_INT
12024            && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12025     {
12026       int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
12027       add_loc_descr (&op0, int_loc_descriptor (shift));
12028       add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
12029       add_loc_descr (&op1, int_loc_descriptor (shift));
12030       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
12031     }
12032   else if (GET_MODE_CLASS (mode) == MODE_INT
12033            && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12034     {
12035       dw_die_ref type_die = base_type_for_mode (mode, 0);
12036       dw_loc_descr_ref cvt;
12037       if (type_die == NULL)
12038         return NULL;
12039       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12040       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12041       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12042       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12043       add_loc_descr (&op0, cvt);
12044       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12045       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12046       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12047       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12048       add_loc_descr (&op1, cvt);
12049     }
12050
12051   if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
12052     op = DW_OP_lt;
12053   else
12054     op = DW_OP_gt;
12055   ret = op0;
12056   add_loc_descr (&ret, op1);
12057   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12058   bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12059   add_loc_descr (&ret, bra_node);
12060   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12061   drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12062   add_loc_descr (&ret, drop_node);
12063   bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12064   bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12065   if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
12066       && GET_MODE_CLASS (mode) == MODE_INT
12067       && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12068     ret = convert_descriptor_to_mode (mode, ret);
12069   return ret;
12070 }
12071
12072 /* Helper function for mem_loc_descriptor.  Perform OP binary op,
12073    but after converting arguments to type_die, afterwards
12074    convert back to unsigned.  */
12075
12076 static dw_loc_descr_ref
12077 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
12078              machine_mode mode, machine_mode mem_mode)
12079 {
12080   dw_loc_descr_ref cvt, op0, op1;
12081
12082   if (type_die == NULL)
12083     return NULL;
12084   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12085                             VAR_INIT_STATUS_INITIALIZED);
12086   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12087                             VAR_INIT_STATUS_INITIALIZED);
12088   if (op0 == NULL || op1 == NULL)
12089     return NULL;
12090   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12091   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12092   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12093   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12094   add_loc_descr (&op0, cvt);
12095   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12096   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12097   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12098   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12099   add_loc_descr (&op1, cvt);
12100   add_loc_descr (&op0, op1);
12101   add_loc_descr (&op0, new_loc_descr (op, 0, 0));
12102   return convert_descriptor_to_mode (mode, op0);
12103 }
12104
12105 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
12106    const0 is DW_OP_lit0 or corresponding typed constant,
12107    const1 is DW_OP_lit1 or corresponding typed constant
12108    and constMSB is constant with just the MSB bit set
12109    for the mode):
12110        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12111    L1: const0 DW_OP_swap
12112    L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
12113        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12114    L3: DW_OP_drop
12115    L4: DW_OP_nop
12116
12117    CTZ is similar:
12118        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12119    L1: const0 DW_OP_swap
12120    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12121        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12122    L3: DW_OP_drop
12123    L4: DW_OP_nop
12124
12125    FFS is similar:
12126        DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
12127    L1: const1 DW_OP_swap
12128    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12129        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12130    L3: DW_OP_drop
12131    L4: DW_OP_nop  */
12132
12133 static dw_loc_descr_ref
12134 clz_loc_descriptor (rtx rtl, machine_mode mode,
12135                     machine_mode mem_mode)
12136 {
12137   dw_loc_descr_ref op0, ret, tmp;
12138   HOST_WIDE_INT valv;
12139   dw_loc_descr_ref l1jump, l1label;
12140   dw_loc_descr_ref l2jump, l2label;
12141   dw_loc_descr_ref l3jump, l3label;
12142   dw_loc_descr_ref l4jump, l4label;
12143   rtx msb;
12144
12145   if (GET_MODE_CLASS (mode) != MODE_INT
12146       || GET_MODE (XEXP (rtl, 0)) != mode)
12147     return NULL;
12148
12149   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12150                             VAR_INIT_STATUS_INITIALIZED);
12151   if (op0 == NULL)
12152     return NULL;
12153   ret = op0;
12154   if (GET_CODE (rtl) == CLZ)
12155     {
12156       if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12157         valv = GET_MODE_BITSIZE (mode);
12158     }
12159   else if (GET_CODE (rtl) == FFS)
12160     valv = 0;
12161   else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12162     valv = GET_MODE_BITSIZE (mode);
12163   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12164   l1jump = new_loc_descr (DW_OP_bra, 0, 0);
12165   add_loc_descr (&ret, l1jump);
12166   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12167   tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
12168                             VAR_INIT_STATUS_INITIALIZED);
12169   if (tmp == NULL)
12170     return NULL;
12171   add_loc_descr (&ret, tmp);
12172   l4jump = new_loc_descr (DW_OP_skip, 0, 0);
12173   add_loc_descr (&ret, l4jump);
12174   l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
12175                                 ? const1_rtx : const0_rtx,
12176                                 mode, mem_mode,
12177                                 VAR_INIT_STATUS_INITIALIZED);
12178   if (l1label == NULL)
12179     return NULL;
12180   add_loc_descr (&ret, l1label);
12181   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12182   l2label = new_loc_descr (DW_OP_dup, 0, 0);
12183   add_loc_descr (&ret, l2label);
12184   if (GET_CODE (rtl) != CLZ)
12185     msb = const1_rtx;
12186   else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
12187     msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
12188                    << (GET_MODE_BITSIZE (mode) - 1));
12189   else
12190     msb = immed_wide_int_const
12191       (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
12192                             GET_MODE_PRECISION (mode)), mode);
12193   if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
12194     tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12195                          ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
12196                          ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
12197   else
12198     tmp = mem_loc_descriptor (msb, mode, mem_mode,
12199                               VAR_INIT_STATUS_INITIALIZED);
12200   if (tmp == NULL)
12201     return NULL;
12202   add_loc_descr (&ret, tmp);
12203   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12204   l3jump = new_loc_descr (DW_OP_bra, 0, 0);
12205   add_loc_descr (&ret, l3jump);
12206   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12207                             VAR_INIT_STATUS_INITIALIZED);
12208   if (tmp == NULL)
12209     return NULL;
12210   add_loc_descr (&ret, tmp);
12211   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
12212                                       ? DW_OP_shl : DW_OP_shr, 0, 0));
12213   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12214   add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
12215   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12216   l2jump = new_loc_descr (DW_OP_skip, 0, 0);
12217   add_loc_descr (&ret, l2jump);
12218   l3label = new_loc_descr (DW_OP_drop, 0, 0);
12219   add_loc_descr (&ret, l3label);
12220   l4label = new_loc_descr (DW_OP_nop, 0, 0);
12221   add_loc_descr (&ret, l4label);
12222   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12223   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12224   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12225   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12226   l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12227   l3jump->dw_loc_oprnd1.v.val_loc = l3label;
12228   l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12229   l4jump->dw_loc_oprnd1.v.val_loc = l4label;
12230   return ret;
12231 }
12232
12233 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
12234    const1 is DW_OP_lit1 or corresponding typed constant):
12235        const0 DW_OP_swap
12236    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12237        DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12238    L2: DW_OP_drop
12239
12240    PARITY is similar:
12241    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12242        DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12243    L2: DW_OP_drop  */
12244
12245 static dw_loc_descr_ref
12246 popcount_loc_descriptor (rtx rtl, machine_mode mode,
12247                          machine_mode mem_mode)
12248 {
12249   dw_loc_descr_ref op0, ret, tmp;
12250   dw_loc_descr_ref l1jump, l1label;
12251   dw_loc_descr_ref l2jump, l2label;
12252
12253   if (GET_MODE_CLASS (mode) != MODE_INT
12254       || GET_MODE (XEXP (rtl, 0)) != mode)
12255     return NULL;
12256
12257   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12258                             VAR_INIT_STATUS_INITIALIZED);
12259   if (op0 == NULL)
12260     return NULL;
12261   ret = op0;
12262   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12263                             VAR_INIT_STATUS_INITIALIZED);
12264   if (tmp == NULL)
12265     return NULL;
12266   add_loc_descr (&ret, tmp);
12267   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12268   l1label = new_loc_descr (DW_OP_dup, 0, 0);
12269   add_loc_descr (&ret, l1label);
12270   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12271   add_loc_descr (&ret, l2jump);
12272   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12273   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12274   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12275                             VAR_INIT_STATUS_INITIALIZED);
12276   if (tmp == NULL)
12277     return NULL;
12278   add_loc_descr (&ret, tmp);
12279   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12280   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
12281                                       ? DW_OP_plus : DW_OP_xor, 0, 0));
12282   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12283   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12284                             VAR_INIT_STATUS_INITIALIZED);
12285   add_loc_descr (&ret, tmp);
12286   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12287   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12288   add_loc_descr (&ret, l1jump);
12289   l2label = new_loc_descr (DW_OP_drop, 0, 0);
12290   add_loc_descr (&ret, l2label);
12291   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12292   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12293   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12294   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12295   return ret;
12296 }
12297
12298 /* BSWAP (constS is initial shift count, either 56 or 24):
12299        constS const0
12300    L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
12301        const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
12302        DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
12303        DW_OP_minus DW_OP_swap DW_OP_skip <L1>
12304    L2: DW_OP_drop DW_OP_swap DW_OP_drop  */
12305
12306 static dw_loc_descr_ref
12307 bswap_loc_descriptor (rtx rtl, machine_mode mode,
12308                       machine_mode mem_mode)
12309 {
12310   dw_loc_descr_ref op0, ret, tmp;
12311   dw_loc_descr_ref l1jump, l1label;
12312   dw_loc_descr_ref l2jump, l2label;
12313
12314   if (GET_MODE_CLASS (mode) != MODE_INT
12315       || BITS_PER_UNIT != 8
12316       || (GET_MODE_BITSIZE (mode) != 32
12317           &&  GET_MODE_BITSIZE (mode) != 64))
12318     return NULL;
12319
12320   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12321                             VAR_INIT_STATUS_INITIALIZED);
12322   if (op0 == NULL)
12323     return NULL;
12324
12325   ret = op0;
12326   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12327                             mode, mem_mode,
12328                             VAR_INIT_STATUS_INITIALIZED);
12329   if (tmp == NULL)
12330     return NULL;
12331   add_loc_descr (&ret, tmp);
12332   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12333                             VAR_INIT_STATUS_INITIALIZED);
12334   if (tmp == NULL)
12335     return NULL;
12336   add_loc_descr (&ret, tmp);
12337   l1label = new_loc_descr (DW_OP_pick, 2, 0);
12338   add_loc_descr (&ret, l1label);
12339   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12340                             mode, mem_mode,
12341                             VAR_INIT_STATUS_INITIALIZED);
12342   add_loc_descr (&ret, tmp);
12343   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
12344   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12345   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12346   tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
12347                             VAR_INIT_STATUS_INITIALIZED);
12348   if (tmp == NULL)
12349     return NULL;
12350   add_loc_descr (&ret, tmp);
12351   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12352   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
12353   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12354   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12355   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12356   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12357   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12358                             VAR_INIT_STATUS_INITIALIZED);
12359   add_loc_descr (&ret, tmp);
12360   add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
12361   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12362   add_loc_descr (&ret, l2jump);
12363   tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
12364                             VAR_INIT_STATUS_INITIALIZED);
12365   add_loc_descr (&ret, tmp);
12366   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12367   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12368   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12369   add_loc_descr (&ret, l1jump);
12370   l2label = new_loc_descr (DW_OP_drop, 0, 0);
12371   add_loc_descr (&ret, l2label);
12372   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12373   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12374   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12375   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12376   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12377   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12378   return ret;
12379 }
12380
12381 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
12382    DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12383    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
12384    DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
12385
12386    ROTATERT is similar:
12387    DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
12388    DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12389    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or  */
12390
12391 static dw_loc_descr_ref
12392 rotate_loc_descriptor (rtx rtl, machine_mode mode,
12393                        machine_mode mem_mode)
12394 {
12395   rtx rtlop1 = XEXP (rtl, 1);
12396   dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
12397   int i;
12398
12399   if (GET_MODE_CLASS (mode) != MODE_INT)
12400     return NULL;
12401
12402   if (GET_MODE (rtlop1) != VOIDmode
12403       && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
12404     rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12405   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12406                             VAR_INIT_STATUS_INITIALIZED);
12407   op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12408                             VAR_INIT_STATUS_INITIALIZED);
12409   if (op0 == NULL || op1 == NULL)
12410     return NULL;
12411   if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12412     for (i = 0; i < 2; i++)
12413       {
12414         if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
12415           mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
12416                                         mode, mem_mode,
12417                                         VAR_INIT_STATUS_INITIALIZED);
12418         else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12419           mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12420                                    ? DW_OP_const4u
12421                                    : HOST_BITS_PER_WIDE_INT == 64
12422                                    ? DW_OP_const8u : DW_OP_constu,
12423                                    GET_MODE_MASK (mode), 0);
12424         else
12425           mask[i] = NULL;
12426         if (mask[i] == NULL)
12427           return NULL;
12428         add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
12429       }
12430   ret = op0;
12431   add_loc_descr (&ret, op1);
12432   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12433   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12434   if (GET_CODE (rtl) == ROTATERT)
12435     {
12436       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12437       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12438                                           GET_MODE_BITSIZE (mode), 0));
12439     }
12440   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12441   if (mask[0] != NULL)
12442     add_loc_descr (&ret, mask[0]);
12443   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12444   if (mask[1] != NULL)
12445     {
12446       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12447       add_loc_descr (&ret, mask[1]);
12448       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12449     }
12450   if (GET_CODE (rtl) == ROTATE)
12451     {
12452       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12453       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12454                                           GET_MODE_BITSIZE (mode), 0));
12455     }
12456   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12457   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12458   return ret;
12459 }
12460
12461 /* Helper function for mem_loc_descriptor.  Return DW_OP_GNU_parameter_ref
12462    for DEBUG_PARAMETER_REF RTL.  */
12463
12464 static dw_loc_descr_ref
12465 parameter_ref_descriptor (rtx rtl)
12466 {
12467   dw_loc_descr_ref ret;
12468   dw_die_ref ref;
12469
12470   if (dwarf_strict)
12471     return NULL;
12472   gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
12473   ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
12474   ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
12475   if (ref)
12476     {
12477       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12478       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12479       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12480     }
12481   else
12482     {
12483       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12484       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
12485     }
12486   return ret;
12487 }
12488
12489 /* The following routine converts the RTL for a variable or parameter
12490    (resident in memory) into an equivalent Dwarf representation of a
12491    mechanism for getting the address of that same variable onto the top of a
12492    hypothetical "address evaluation" stack.
12493
12494    When creating memory location descriptors, we are effectively transforming
12495    the RTL for a memory-resident object into its Dwarf postfix expression
12496    equivalent.  This routine recursively descends an RTL tree, turning
12497    it into Dwarf postfix code as it goes.
12498
12499    MODE is the mode that should be assumed for the rtl if it is VOIDmode.
12500
12501    MEM_MODE is the mode of the memory reference, needed to handle some
12502    autoincrement addressing modes.
12503
12504    Return 0 if we can't represent the location.  */
12505
12506 dw_loc_descr_ref
12507 mem_loc_descriptor (rtx rtl, machine_mode mode,
12508                     machine_mode mem_mode,
12509                     enum var_init_status initialized)
12510 {
12511   dw_loc_descr_ref mem_loc_result = NULL;
12512   enum dwarf_location_atom op;
12513   dw_loc_descr_ref op0, op1;
12514   rtx inner = NULL_RTX;
12515
12516   if (mode == VOIDmode)
12517     mode = GET_MODE (rtl);
12518
12519   /* Note that for a dynamically sized array, the location we will generate a
12520      description of here will be the lowest numbered location which is
12521      actually within the array.  That's *not* necessarily the same as the
12522      zeroth element of the array.  */
12523
12524   rtl = targetm.delegitimize_address (rtl);
12525
12526   if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
12527     return NULL;
12528
12529   switch (GET_CODE (rtl))
12530     {
12531     case POST_INC:
12532     case POST_DEC:
12533     case POST_MODIFY:
12534       return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
12535
12536     case SUBREG:
12537       /* The case of a subreg may arise when we have a local (register)
12538          variable or a formal (register) parameter which doesn't quite fill
12539          up an entire register.  For now, just assume that it is
12540          legitimate to make the Dwarf info refer to the whole register which
12541          contains the given subreg.  */
12542       if (!subreg_lowpart_p (rtl))
12543         break;
12544       inner = SUBREG_REG (rtl);
12545     case TRUNCATE:
12546       if (inner == NULL_RTX)
12547         inner = XEXP (rtl, 0);
12548       if (GET_MODE_CLASS (mode) == MODE_INT
12549           && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
12550           && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12551 #ifdef POINTERS_EXTEND_UNSIGNED
12552               || (mode == Pmode && mem_mode != VOIDmode)
12553 #endif
12554              )
12555           && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
12556         {
12557           mem_loc_result = mem_loc_descriptor (inner,
12558                                                GET_MODE (inner),
12559                                                mem_mode, initialized);
12560           break;
12561         }
12562       if (dwarf_strict)
12563         break;
12564       if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
12565         break;
12566       if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
12567           && (GET_MODE_CLASS (mode) != MODE_INT
12568               || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
12569         break;
12570       else
12571         {
12572           dw_die_ref type_die;
12573           dw_loc_descr_ref cvt;
12574
12575           mem_loc_result = mem_loc_descriptor (inner,
12576                                                GET_MODE (inner),
12577                                                mem_mode, initialized);
12578           if (mem_loc_result == NULL)
12579             break;
12580           type_die = base_type_for_mode (mode,
12581                                          GET_MODE_CLASS (mode) == MODE_INT);
12582           if (type_die == NULL)
12583             {
12584               mem_loc_result = NULL;
12585               break;
12586             }
12587           if (GET_MODE_SIZE (mode)
12588               != GET_MODE_SIZE (GET_MODE (inner)))
12589             cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12590           else
12591             cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12592           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12593           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12594           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12595           add_loc_descr (&mem_loc_result, cvt);
12596         }
12597       break;
12598
12599     case REG:
12600       if (GET_MODE_CLASS (mode) != MODE_INT
12601           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12602               && rtl != arg_pointer_rtx
12603               && rtl != frame_pointer_rtx
12604 #ifdef POINTERS_EXTEND_UNSIGNED
12605               && (mode != Pmode || mem_mode == VOIDmode)
12606 #endif
12607               ))
12608         {
12609           dw_die_ref type_die;
12610           unsigned int dbx_regnum;
12611
12612           if (dwarf_strict)
12613             break;
12614           if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12615             break;
12616           type_die = base_type_for_mode (mode,
12617                                          GET_MODE_CLASS (mode) == MODE_INT);
12618           if (type_die == NULL)
12619             break;
12620
12621           dbx_regnum = dbx_reg_number (rtl);
12622           if (dbx_regnum == IGNORED_DWARF_REGNUM)
12623             break;
12624           mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12625                                           dbx_regnum, 0);
12626           mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12627           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12628           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12629           break;
12630         }
12631       /* Whenever a register number forms a part of the description of the
12632          method for calculating the (dynamic) address of a memory resident
12633          object, DWARF rules require the register number be referred to as
12634          a "base register".  This distinction is not based in any way upon
12635          what category of register the hardware believes the given register
12636          belongs to.  This is strictly DWARF terminology we're dealing with
12637          here. Note that in cases where the location of a memory-resident
12638          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12639          OP_CONST (0)) the actual DWARF location descriptor that we generate
12640          may just be OP_BASEREG (basereg).  This may look deceptively like
12641          the object in question was allocated to a register (rather than in
12642          memory) so DWARF consumers need to be aware of the subtle
12643          distinction between OP_REG and OP_BASEREG.  */
12644       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12645         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12646       else if (stack_realign_drap
12647                && crtl->drap_reg
12648                && crtl->args.internal_arg_pointer == rtl
12649                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12650         {
12651           /* If RTL is internal_arg_pointer, which has been optimized
12652              out, use DRAP instead.  */
12653           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12654                                             VAR_INIT_STATUS_INITIALIZED);
12655         }
12656       break;
12657
12658     case SIGN_EXTEND:
12659     case ZERO_EXTEND:
12660       if (GET_MODE_CLASS (mode) != MODE_INT)
12661         break;
12662       op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12663                                 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12664       if (op0 == 0)
12665         break;
12666       else if (GET_CODE (rtl) == ZERO_EXTEND
12667                && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12668                && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12669                   < HOST_BITS_PER_WIDE_INT
12670                /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12671                   to expand zero extend as two shifts instead of
12672                   masking.  */
12673                && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12674         {
12675           machine_mode imode = GET_MODE (XEXP (rtl, 0));
12676           mem_loc_result = op0;
12677           add_loc_descr (&mem_loc_result,
12678                          int_loc_descriptor (GET_MODE_MASK (imode)));
12679           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12680         }
12681       else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12682         {
12683           int shift = DWARF2_ADDR_SIZE
12684                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12685           shift *= BITS_PER_UNIT;
12686           if (GET_CODE (rtl) == SIGN_EXTEND)
12687             op = DW_OP_shra;
12688           else
12689             op = DW_OP_shr;
12690           mem_loc_result = op0;
12691           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12692           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12693           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12694           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12695         }
12696       else if (!dwarf_strict)
12697         {
12698           dw_die_ref type_die1, type_die2;
12699           dw_loc_descr_ref cvt;
12700
12701           type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12702                                           GET_CODE (rtl) == ZERO_EXTEND);
12703           if (type_die1 == NULL)
12704             break;
12705           type_die2 = base_type_for_mode (mode, 1);
12706           if (type_die2 == NULL)
12707             break;
12708           mem_loc_result = op0;
12709           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12710           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12711           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12712           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12713           add_loc_descr (&mem_loc_result, cvt);
12714           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12715           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12716           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12717           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12718           add_loc_descr (&mem_loc_result, cvt);
12719         }
12720       break;
12721
12722     case MEM:
12723       {
12724         rtx new_rtl = avoid_constant_pool_reference (rtl);
12725         if (new_rtl != rtl)
12726           {
12727             mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12728                                                  initialized);
12729             if (mem_loc_result != NULL)
12730               return mem_loc_result;
12731           }
12732       }
12733       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12734                                            get_address_mode (rtl), mode,
12735                                            VAR_INIT_STATUS_INITIALIZED);
12736       if (mem_loc_result == NULL)
12737         mem_loc_result = tls_mem_loc_descriptor (rtl);
12738       if (mem_loc_result != NULL)
12739         {
12740           if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12741               || GET_MODE_CLASS (mode) != MODE_INT)
12742             {
12743               dw_die_ref type_die;
12744               dw_loc_descr_ref deref;
12745
12746               if (dwarf_strict)
12747                 return NULL;
12748               type_die
12749                 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12750               if (type_die == NULL)
12751                 return NULL;
12752               deref = new_loc_descr (DW_OP_GNU_deref_type,
12753                                      GET_MODE_SIZE (mode), 0);
12754               deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12755               deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12756               deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12757               add_loc_descr (&mem_loc_result, deref);
12758             }
12759           else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12760             add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12761           else
12762             add_loc_descr (&mem_loc_result,
12763                            new_loc_descr (DW_OP_deref_size,
12764                                           GET_MODE_SIZE (mode), 0));
12765         }
12766       break;
12767
12768     case LO_SUM:
12769       return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12770
12771     case LABEL_REF:
12772       /* Some ports can transform a symbol ref into a label ref, because
12773          the symbol ref is too far away and has to be dumped into a constant
12774          pool.  */
12775     case CONST:
12776     case SYMBOL_REF:
12777       if ((GET_MODE_CLASS (mode) != MODE_INT
12778            && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
12779           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12780 #ifdef POINTERS_EXTEND_UNSIGNED
12781               && (mode != Pmode || mem_mode == VOIDmode)
12782 #endif
12783               ))
12784         break;
12785       if (GET_CODE (rtl) == SYMBOL_REF
12786           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12787         {
12788           dw_loc_descr_ref temp;
12789
12790           /* If this is not defined, we have no way to emit the data.  */
12791           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12792             break;
12793
12794           temp = new_addr_loc_descr (rtl, dtprel_true);
12795
12796           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12797           add_loc_descr (&mem_loc_result, temp);
12798
12799           break;
12800         }
12801
12802       if (!const_ok_for_output (rtl))
12803         {
12804           if (GET_CODE (rtl) == CONST)
12805             mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12806                                                  initialized);
12807           break;
12808         }
12809
12810     symref:
12811       mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12812       vec_safe_push (used_rtx_array, rtl);
12813       break;
12814
12815     case CONCAT:
12816     case CONCATN:
12817     case VAR_LOCATION:
12818     case DEBUG_IMPLICIT_PTR:
12819       expansion_failed (NULL_TREE, rtl,
12820                         "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12821       return 0;
12822
12823     case ENTRY_VALUE:
12824       if (dwarf_strict)
12825         return NULL;
12826       if (REG_P (ENTRY_VALUE_EXP (rtl)))
12827         {
12828           if (GET_MODE_CLASS (mode) != MODE_INT
12829               || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12830             op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12831                                       VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12832           else
12833             {
12834               unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12835               if (dbx_regnum == IGNORED_DWARF_REGNUM)
12836                 return NULL;
12837               op0 = one_reg_loc_descriptor (dbx_regnum,
12838                                             VAR_INIT_STATUS_INITIALIZED);
12839             }
12840         }
12841       else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12842                && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12843         {
12844           op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12845                                     VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12846           if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12847             return NULL;
12848         }
12849       else
12850         gcc_unreachable ();
12851       if (op0 == NULL)
12852         return NULL;
12853       mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12854       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12855       mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12856       break;
12857
12858     case DEBUG_PARAMETER_REF:
12859       mem_loc_result = parameter_ref_descriptor (rtl);
12860       break;
12861
12862     case PRE_MODIFY:
12863       /* Extract the PLUS expression nested inside and fall into
12864          PLUS code below.  */
12865       rtl = XEXP (rtl, 1);
12866       goto plus;
12867
12868     case PRE_INC:
12869     case PRE_DEC:
12870       /* Turn these into a PLUS expression and fall into the PLUS code
12871          below.  */
12872       rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12873                           gen_int_mode (GET_CODE (rtl) == PRE_INC
12874                                         ? GET_MODE_UNIT_SIZE (mem_mode)
12875                                         : -GET_MODE_UNIT_SIZE (mem_mode),
12876                                         mode));
12877
12878       /* ... fall through ...  */
12879
12880     case PLUS:
12881     plus:
12882       if (is_based_loc (rtl)
12883           && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12884               || XEXP (rtl, 0) == arg_pointer_rtx
12885               || XEXP (rtl, 0) == frame_pointer_rtx)
12886           && GET_MODE_CLASS (mode) == MODE_INT)
12887         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12888                                           INTVAL (XEXP (rtl, 1)),
12889                                           VAR_INIT_STATUS_INITIALIZED);
12890       else
12891         {
12892           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12893                                                VAR_INIT_STATUS_INITIALIZED);
12894           if (mem_loc_result == 0)
12895             break;
12896
12897           if (CONST_INT_P (XEXP (rtl, 1))
12898               && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12899             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12900           else
12901             {
12902               op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12903                                         VAR_INIT_STATUS_INITIALIZED);
12904               if (op1 == 0)
12905                 return NULL;
12906               add_loc_descr (&mem_loc_result, op1);
12907               add_loc_descr (&mem_loc_result,
12908                              new_loc_descr (DW_OP_plus, 0, 0));
12909             }
12910         }
12911       break;
12912
12913     /* If a pseudo-reg is optimized away, it is possible for it to
12914        be replaced with a MEM containing a multiply or shift.  */
12915     case MINUS:
12916       op = DW_OP_minus;
12917       goto do_binop;
12918
12919     case MULT:
12920       op = DW_OP_mul;
12921       goto do_binop;
12922
12923     case DIV:
12924       if (!dwarf_strict
12925           && GET_MODE_CLASS (mode) == MODE_INT
12926           && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12927         {
12928           mem_loc_result = typed_binop (DW_OP_div, rtl,
12929                                         base_type_for_mode (mode, 0),
12930                                         mode, mem_mode);
12931           break;
12932         }
12933       op = DW_OP_div;
12934       goto do_binop;
12935
12936     case UMOD:
12937       op = DW_OP_mod;
12938       goto do_binop;
12939
12940     case ASHIFT:
12941       op = DW_OP_shl;
12942       goto do_shift;
12943
12944     case ASHIFTRT:
12945       op = DW_OP_shra;
12946       goto do_shift;
12947
12948     case LSHIFTRT:
12949       op = DW_OP_shr;
12950       goto do_shift;
12951
12952     do_shift:
12953       if (GET_MODE_CLASS (mode) != MODE_INT)
12954         break;
12955       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12956                                 VAR_INIT_STATUS_INITIALIZED);
12957       {
12958         rtx rtlop1 = XEXP (rtl, 1);
12959         if (GET_MODE (rtlop1) != VOIDmode
12960             && GET_MODE_BITSIZE (GET_MODE (rtlop1))
12961                < GET_MODE_BITSIZE (mode))
12962           rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12963         op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12964                                   VAR_INIT_STATUS_INITIALIZED);
12965       }
12966
12967       if (op0 == 0 || op1 == 0)
12968         break;
12969
12970       mem_loc_result = op0;
12971       add_loc_descr (&mem_loc_result, op1);
12972       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12973       break;
12974
12975     case AND:
12976       op = DW_OP_and;
12977       goto do_binop;
12978
12979     case IOR:
12980       op = DW_OP_or;
12981       goto do_binop;
12982
12983     case XOR:
12984       op = DW_OP_xor;
12985       goto do_binop;
12986
12987     do_binop:
12988       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12989                                 VAR_INIT_STATUS_INITIALIZED);
12990       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12991                                 VAR_INIT_STATUS_INITIALIZED);
12992
12993       if (op0 == 0 || op1 == 0)
12994         break;
12995
12996       mem_loc_result = op0;
12997       add_loc_descr (&mem_loc_result, op1);
12998       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12999       break;
13000
13001     case MOD:
13002       if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
13003         {
13004           mem_loc_result = typed_binop (DW_OP_mod, rtl,
13005                                         base_type_for_mode (mode, 0),
13006                                         mode, mem_mode);
13007           break;
13008         }
13009
13010       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13011                                 VAR_INIT_STATUS_INITIALIZED);
13012       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13013                                 VAR_INIT_STATUS_INITIALIZED);
13014
13015       if (op0 == 0 || op1 == 0)
13016         break;
13017
13018       mem_loc_result = op0;
13019       add_loc_descr (&mem_loc_result, op1);
13020       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13021       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13022       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
13023       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13024       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
13025       break;
13026
13027     case UDIV:
13028       if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
13029         {
13030           if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
13031             {
13032               op = DW_OP_div;
13033               goto do_binop;
13034             }
13035           mem_loc_result = typed_binop (DW_OP_div, rtl,
13036                                         base_type_for_mode (mode, 1),
13037                                         mode, mem_mode);
13038         }
13039       break;
13040
13041     case NOT:
13042       op = DW_OP_not;
13043       goto do_unop;
13044
13045     case ABS:
13046       op = DW_OP_abs;
13047       goto do_unop;
13048
13049     case NEG:
13050       op = DW_OP_neg;
13051       goto do_unop;
13052
13053     do_unop:
13054       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13055                                 VAR_INIT_STATUS_INITIALIZED);
13056
13057       if (op0 == 0)
13058         break;
13059
13060       mem_loc_result = op0;
13061       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13062       break;
13063
13064     case CONST_INT:
13065       if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13066 #ifdef POINTERS_EXTEND_UNSIGNED
13067           || (mode == Pmode
13068               && mem_mode != VOIDmode
13069               && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
13070 #endif
13071           )
13072         {
13073           mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13074           break;
13075         }
13076       if (!dwarf_strict
13077           && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
13078               || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
13079         {
13080           dw_die_ref type_die = base_type_for_mode (mode, 1);
13081           machine_mode amode;
13082           if (type_die == NULL)
13083             return NULL;
13084           amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
13085                                  MODE_INT, 0);
13086           if (INTVAL (rtl) >= 0
13087               && amode != BLKmode
13088               && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
13089               /* const DW_OP_GNU_convert <XXX> vs.
13090                  DW_OP_GNU_const_type <XXX, 1, const>.  */
13091               && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
13092                  < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
13093             {
13094               mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13095               op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13096               op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13097               op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13098               op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
13099               add_loc_descr (&mem_loc_result, op0);
13100               return mem_loc_result;
13101             }
13102           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
13103                                           INTVAL (rtl));
13104           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13105           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13106           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13107           if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
13108             mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13109           else
13110             {
13111               mem_loc_result->dw_loc_oprnd2.val_class
13112                 = dw_val_class_const_double;
13113               mem_loc_result->dw_loc_oprnd2.v.val_double
13114                 = double_int::from_shwi (INTVAL (rtl));
13115             }
13116         }
13117       break;
13118
13119     case CONST_DOUBLE:
13120       if (!dwarf_strict)
13121         {
13122           dw_die_ref type_die;
13123
13124           /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
13125              CONST_DOUBLE rtx could represent either a large integer
13126              or a floating-point constant.  If TARGET_SUPPORTS_WIDE_INT != 0,
13127              the value is always a floating point constant.
13128
13129              When it is an integer, a CONST_DOUBLE is used whenever
13130              the constant requires 2 HWIs to be adequately represented.
13131              We output CONST_DOUBLEs as blocks.  */
13132           if (mode == VOIDmode
13133               || (GET_MODE (rtl) == VOIDmode
13134                   && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
13135             break;
13136           type_die = base_type_for_mode (mode,
13137                                          GET_MODE_CLASS (mode) == MODE_INT);
13138           if (type_die == NULL)
13139             return NULL;
13140           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13141           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13142           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13143           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13144 #if TARGET_SUPPORTS_WIDE_INT == 0
13145           if (!SCALAR_FLOAT_MODE_P (mode))
13146             {
13147               mem_loc_result->dw_loc_oprnd2.val_class
13148                 = dw_val_class_const_double;
13149               mem_loc_result->dw_loc_oprnd2.v.val_double
13150                 = rtx_to_double_int (rtl);
13151             }
13152           else
13153 #endif
13154             {
13155               unsigned int length = GET_MODE_SIZE (mode);
13156               unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13157
13158               insert_float (rtl, array);
13159               mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13160               mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13161               mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13162               mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13163             }
13164         }
13165       break;
13166
13167     case CONST_WIDE_INT:
13168       if (!dwarf_strict)
13169         {
13170           dw_die_ref type_die;
13171
13172           type_die = base_type_for_mode (mode,
13173                                          GET_MODE_CLASS (mode) == MODE_INT);
13174           if (type_die == NULL)
13175             return NULL;
13176           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13177           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13178           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13179           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13180           mem_loc_result->dw_loc_oprnd2.val_class
13181             = dw_val_class_wide_int;
13182           mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
13183           *mem_loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13184         }
13185       break;
13186
13187     case EQ:
13188       mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
13189       break;
13190
13191     case GE:
13192       mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13193       break;
13194
13195     case GT:
13196       mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13197       break;
13198
13199     case LE:
13200       mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13201       break;
13202
13203     case LT:
13204       mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13205       break;
13206
13207     case NE:
13208       mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
13209       break;
13210
13211     case GEU:
13212       mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13213       break;
13214
13215     case GTU:
13216       mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13217       break;
13218
13219     case LEU:
13220       mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13221       break;
13222
13223     case LTU:
13224       mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13225       break;
13226
13227     case UMIN:
13228     case UMAX:
13229       if (GET_MODE_CLASS (mode) != MODE_INT)
13230         break;
13231       /* FALLTHRU */
13232     case SMIN:
13233     case SMAX:
13234       mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
13235       break;
13236
13237     case ZERO_EXTRACT:
13238     case SIGN_EXTRACT:
13239       if (CONST_INT_P (XEXP (rtl, 1))
13240           && CONST_INT_P (XEXP (rtl, 2))
13241           && ((unsigned) INTVAL (XEXP (rtl, 1))
13242               + (unsigned) INTVAL (XEXP (rtl, 2))
13243               <= GET_MODE_BITSIZE (mode))
13244           && GET_MODE_CLASS (mode) == MODE_INT
13245           && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13246           && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
13247         {
13248           int shift, size;
13249           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13250                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
13251           if (op0 == 0)
13252             break;
13253           if (GET_CODE (rtl) == SIGN_EXTRACT)
13254             op = DW_OP_shra;
13255           else
13256             op = DW_OP_shr;
13257           mem_loc_result = op0;
13258           size = INTVAL (XEXP (rtl, 1));
13259           shift = INTVAL (XEXP (rtl, 2));
13260           if (BITS_BIG_ENDIAN)
13261             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
13262                     - shift - size;
13263           if (shift + size != (int) DWARF2_ADDR_SIZE)
13264             {
13265               add_loc_descr (&mem_loc_result,
13266                              int_loc_descriptor (DWARF2_ADDR_SIZE
13267                                                  - shift - size));
13268               add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13269             }
13270           if (size != (int) DWARF2_ADDR_SIZE)
13271             {
13272               add_loc_descr (&mem_loc_result,
13273                              int_loc_descriptor (DWARF2_ADDR_SIZE - size));
13274               add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13275             }
13276         }
13277       break;
13278
13279     case IF_THEN_ELSE:
13280       {
13281         dw_loc_descr_ref op2, bra_node, drop_node;
13282         op0 = mem_loc_descriptor (XEXP (rtl, 0),
13283                                   GET_MODE (XEXP (rtl, 0)) == VOIDmode
13284                                   ? word_mode : GET_MODE (XEXP (rtl, 0)),
13285                                   mem_mode, VAR_INIT_STATUS_INITIALIZED);
13286         op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13287                                   VAR_INIT_STATUS_INITIALIZED);
13288         op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
13289                                   VAR_INIT_STATUS_INITIALIZED);
13290         if (op0 == NULL || op1 == NULL || op2 == NULL)
13291           break;
13292
13293         mem_loc_result = op1;
13294         add_loc_descr (&mem_loc_result, op2);
13295         add_loc_descr (&mem_loc_result, op0);
13296         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13297         add_loc_descr (&mem_loc_result, bra_node);
13298         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
13299         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
13300         add_loc_descr (&mem_loc_result, drop_node);
13301         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13302         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
13303       }
13304       break;
13305
13306     case FLOAT_EXTEND:
13307     case FLOAT_TRUNCATE:
13308     case FLOAT:
13309     case UNSIGNED_FLOAT:
13310     case FIX:
13311     case UNSIGNED_FIX:
13312       if (!dwarf_strict)
13313         {
13314           dw_die_ref type_die;
13315           dw_loc_descr_ref cvt;
13316
13317           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13318                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
13319           if (op0 == NULL)
13320             break;
13321           if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
13322               && (GET_CODE (rtl) == FLOAT
13323                   || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
13324                      <= DWARF2_ADDR_SIZE))
13325             {
13326               type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
13327                                              GET_CODE (rtl) == UNSIGNED_FLOAT);
13328               if (type_die == NULL)
13329                 break;
13330               cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13331               cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13332               cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13333               cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13334               add_loc_descr (&op0, cvt);
13335             }
13336           type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
13337           if (type_die == NULL)
13338             break;
13339           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13340           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13341           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13342           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13343           add_loc_descr (&op0, cvt);
13344           if (GET_MODE_CLASS (mode) == MODE_INT
13345               && (GET_CODE (rtl) == FIX
13346                   || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
13347             {
13348               op0 = convert_descriptor_to_mode (mode, op0);
13349               if (op0 == NULL)
13350                 break;
13351             }
13352           mem_loc_result = op0;
13353         }
13354       break;
13355
13356     case CLZ:
13357     case CTZ:
13358     case FFS:
13359       mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
13360       break;
13361
13362     case POPCOUNT:
13363     case PARITY:
13364       mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
13365       break;
13366
13367     case BSWAP:
13368       mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
13369       break;
13370
13371     case ROTATE:
13372     case ROTATERT:
13373       mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
13374       break;
13375
13376     case COMPARE:
13377       /* In theory, we could implement the above.  */
13378       /* DWARF cannot represent the unsigned compare operations
13379          natively.  */
13380     case SS_MULT:
13381     case US_MULT:
13382     case SS_DIV:
13383     case US_DIV:
13384     case SS_PLUS:
13385     case US_PLUS:
13386     case SS_MINUS:
13387     case US_MINUS:
13388     case SS_NEG:
13389     case US_NEG:
13390     case SS_ABS:
13391     case SS_ASHIFT:
13392     case US_ASHIFT:
13393     case SS_TRUNCATE:
13394     case US_TRUNCATE:
13395     case UNORDERED:
13396     case ORDERED:
13397     case UNEQ:
13398     case UNGE:
13399     case UNGT:
13400     case UNLE:
13401     case UNLT:
13402     case LTGT:
13403     case FRACT_CONVERT:
13404     case UNSIGNED_FRACT_CONVERT:
13405     case SAT_FRACT:
13406     case UNSIGNED_SAT_FRACT:
13407     case SQRT:
13408     case ASM_OPERANDS:
13409     case VEC_MERGE:
13410     case VEC_SELECT:
13411     case VEC_CONCAT:
13412     case VEC_DUPLICATE:
13413     case UNSPEC:
13414     case HIGH:
13415     case FMA:
13416     case STRICT_LOW_PART:
13417     case CONST_VECTOR:
13418     case CONST_FIXED:
13419     case CLRSB:
13420     case CLOBBER:
13421       /* If delegitimize_address couldn't do anything with the UNSPEC, we
13422          can't express it in the debug info.  This can happen e.g. with some
13423          TLS UNSPECs.  */
13424       break;
13425
13426     case CONST_STRING:
13427       resolve_one_addr (&rtl);
13428       goto symref;
13429
13430     default:
13431 #ifdef ENABLE_CHECKING
13432       print_rtl (stderr, rtl);
13433       gcc_unreachable ();
13434 #else
13435       break;
13436 #endif
13437     }
13438
13439   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13440     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13441
13442   return mem_loc_result;
13443 }
13444
13445 /* Return a descriptor that describes the concatenation of two locations.
13446    This is typically a complex variable.  */
13447
13448 static dw_loc_descr_ref
13449 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
13450 {
13451   dw_loc_descr_ref cc_loc_result = NULL;
13452   dw_loc_descr_ref x0_ref
13453     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13454   dw_loc_descr_ref x1_ref
13455     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13456
13457   if (x0_ref == 0 || x1_ref == 0)
13458     return 0;
13459
13460   cc_loc_result = x0_ref;
13461   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
13462
13463   add_loc_descr (&cc_loc_result, x1_ref);
13464   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
13465
13466   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13467     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13468
13469   return cc_loc_result;
13470 }
13471
13472 /* Return a descriptor that describes the concatenation of N
13473    locations.  */
13474
13475 static dw_loc_descr_ref
13476 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
13477 {
13478   unsigned int i;
13479   dw_loc_descr_ref cc_loc_result = NULL;
13480   unsigned int n = XVECLEN (concatn, 0);
13481
13482   for (i = 0; i < n; ++i)
13483     {
13484       dw_loc_descr_ref ref;
13485       rtx x = XVECEXP (concatn, 0, i);
13486
13487       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13488       if (ref == NULL)
13489         return NULL;
13490
13491       add_loc_descr (&cc_loc_result, ref);
13492       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
13493     }
13494
13495   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13496     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13497
13498   return cc_loc_result;
13499 }
13500
13501 /* Helper function for loc_descriptor.  Return DW_OP_GNU_implicit_pointer
13502    for DEBUG_IMPLICIT_PTR RTL.  */
13503
13504 static dw_loc_descr_ref
13505 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
13506 {
13507   dw_loc_descr_ref ret;
13508   dw_die_ref ref;
13509
13510   if (dwarf_strict)
13511     return NULL;
13512   gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
13513               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
13514               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
13515   ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
13516   ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
13517   ret->dw_loc_oprnd2.val_class = dw_val_class_const;
13518   if (ref)
13519     {
13520       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13521       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13522       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13523     }
13524   else
13525     {
13526       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13527       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
13528     }
13529   return ret;
13530 }
13531
13532 /* Output a proper Dwarf location descriptor for a variable or parameter
13533    which is either allocated in a register or in a memory location.  For a
13534    register, we just generate an OP_REG and the register number.  For a
13535    memory location we provide a Dwarf postfix expression describing how to
13536    generate the (dynamic) address of the object onto the address stack.
13537
13538    MODE is mode of the decl if this loc_descriptor is going to be used in
13539    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13540    allowed, VOIDmode otherwise.
13541
13542    If we don't know how to describe it, return 0.  */
13543
13544 static dw_loc_descr_ref
13545 loc_descriptor (rtx rtl, machine_mode mode,
13546                 enum var_init_status initialized)
13547 {
13548   dw_loc_descr_ref loc_result = NULL;
13549
13550   switch (GET_CODE (rtl))
13551     {
13552     case SUBREG:
13553       /* The case of a subreg may arise when we have a local (register)
13554          variable or a formal (register) parameter which doesn't quite fill
13555          up an entire register.  For now, just assume that it is
13556          legitimate to make the Dwarf info refer to the whole register which
13557          contains the given subreg.  */
13558       if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
13559         loc_result = loc_descriptor (SUBREG_REG (rtl),
13560                                      GET_MODE (SUBREG_REG (rtl)), initialized);
13561       else
13562         goto do_default;
13563       break;
13564
13565     case REG:
13566       loc_result = reg_loc_descriptor (rtl, initialized);
13567       break;
13568
13569     case MEM:
13570       loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13571                                        GET_MODE (rtl), initialized);
13572       if (loc_result == NULL)
13573         loc_result = tls_mem_loc_descriptor (rtl);
13574       if (loc_result == NULL)
13575         {
13576           rtx new_rtl = avoid_constant_pool_reference (rtl);
13577           if (new_rtl != rtl)
13578             loc_result = loc_descriptor (new_rtl, mode, initialized);
13579         }
13580       break;
13581
13582     case CONCAT:
13583       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13584                                           initialized);
13585       break;
13586
13587     case CONCATN:
13588       loc_result = concatn_loc_descriptor (rtl, initialized);
13589       break;
13590
13591     case VAR_LOCATION:
13592       /* Single part.  */
13593       if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
13594         {
13595           rtx loc = PAT_VAR_LOCATION_LOC (rtl);
13596           if (GET_CODE (loc) == EXPR_LIST)
13597             loc = XEXP (loc, 0);
13598           loc_result = loc_descriptor (loc, mode, initialized);
13599           break;
13600         }
13601
13602       rtl = XEXP (rtl, 1);
13603       /* FALLTHRU */
13604
13605     case PARALLEL:
13606       {
13607         rtvec par_elems = XVEC (rtl, 0);
13608         int num_elem = GET_NUM_ELEM (par_elems);
13609         machine_mode mode;
13610         int i;
13611
13612         /* Create the first one, so we have something to add to.  */
13613         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13614                                      VOIDmode, initialized);
13615         if (loc_result == NULL)
13616           return NULL;
13617         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13618         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13619         for (i = 1; i < num_elem; i++)
13620           {
13621             dw_loc_descr_ref temp;
13622
13623             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13624                                    VOIDmode, initialized);
13625             if (temp == NULL)
13626               return NULL;
13627             add_loc_descr (&loc_result, temp);
13628             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13629             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13630           }
13631       }
13632       break;
13633
13634     case CONST_INT:
13635       if (mode != VOIDmode && mode != BLKmode)
13636         loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13637                                                     INTVAL (rtl));
13638       break;
13639
13640     case CONST_DOUBLE:
13641       if (mode == VOIDmode)
13642         mode = GET_MODE (rtl);
13643
13644       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13645         {
13646           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13647
13648           /* Note that a CONST_DOUBLE rtx could represent either an integer
13649              or a floating-point constant.  A CONST_DOUBLE is used whenever
13650              the constant requires more than one word in order to be
13651              adequately represented.  We output CONST_DOUBLEs as blocks.  */
13652           loc_result = new_loc_descr (DW_OP_implicit_value,
13653                                       GET_MODE_SIZE (mode), 0);
13654 #if TARGET_SUPPORTS_WIDE_INT == 0
13655           if (!SCALAR_FLOAT_MODE_P (mode))
13656             {
13657               loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13658               loc_result->dw_loc_oprnd2.v.val_double
13659                 = rtx_to_double_int (rtl);
13660             }
13661           else
13662 #endif
13663             {
13664               unsigned int length = GET_MODE_SIZE (mode);
13665               unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13666
13667               insert_float (rtl, array);
13668               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13669               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13670               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13671               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13672             }
13673         }
13674       break;
13675
13676     case CONST_WIDE_INT:
13677       if (mode == VOIDmode)
13678         mode = GET_MODE (rtl);
13679
13680       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13681         {
13682           loc_result = new_loc_descr (DW_OP_implicit_value,
13683                                       GET_MODE_SIZE (mode), 0);
13684           loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
13685           loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
13686           *loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13687         }
13688       break;
13689
13690     case CONST_VECTOR:
13691       if (mode == VOIDmode)
13692         mode = GET_MODE (rtl);
13693
13694       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13695         {
13696           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13697           unsigned int length = CONST_VECTOR_NUNITS (rtl);
13698           unsigned char *array
13699             = ggc_vec_alloc<unsigned char> (length * elt_size);
13700           unsigned int i;
13701           unsigned char *p;
13702           machine_mode imode = GET_MODE_INNER (mode);
13703
13704           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13705           switch (GET_MODE_CLASS (mode))
13706             {
13707             case MODE_VECTOR_INT:
13708               for (i = 0, p = array; i < length; i++, p += elt_size)
13709                 {
13710                   rtx elt = CONST_VECTOR_ELT (rtl, i);
13711                   insert_wide_int (std::make_pair (elt, imode), p, elt_size);
13712                 }
13713               break;
13714
13715             case MODE_VECTOR_FLOAT:
13716               for (i = 0, p = array; i < length; i++, p += elt_size)
13717                 {
13718                   rtx elt = CONST_VECTOR_ELT (rtl, i);
13719                   insert_float (elt, p);
13720                 }
13721               break;
13722
13723             default:
13724               gcc_unreachable ();
13725             }
13726
13727           loc_result = new_loc_descr (DW_OP_implicit_value,
13728                                       length * elt_size, 0);
13729           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13730           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13731           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13732           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13733         }
13734       break;
13735
13736     case CONST:
13737       if (mode == VOIDmode
13738           || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13739           || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13740           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13741         {
13742           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13743           break;
13744         }
13745       /* FALLTHROUGH */
13746     case SYMBOL_REF:
13747       if (!const_ok_for_output (rtl))
13748         break;
13749     case LABEL_REF:
13750       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13751           && (dwarf_version >= 4 || !dwarf_strict))
13752         {
13753          loc_result = new_addr_loc_descr (rtl, dtprel_false);
13754           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13755           vec_safe_push (used_rtx_array, rtl);
13756         }
13757       break;
13758
13759     case DEBUG_IMPLICIT_PTR:
13760       loc_result = implicit_ptr_descriptor (rtl, 0);
13761       break;
13762
13763     case PLUS:
13764       if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13765           && CONST_INT_P (XEXP (rtl, 1)))
13766         {
13767           loc_result
13768             = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13769           break;
13770         }
13771       /* FALLTHRU */
13772     do_default:
13773     default:
13774       if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13775            && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13776            && dwarf_version >= 4)
13777           || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13778         {
13779           /* Value expression.  */
13780           loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13781           if (loc_result)
13782             add_loc_descr (&loc_result,
13783                            new_loc_descr (DW_OP_stack_value, 0, 0));
13784         }
13785       break;
13786     }
13787
13788   return loc_result;
13789 }
13790
13791 /* We need to figure out what section we should use as the base for the
13792    address ranges where a given location is valid.
13793    1. If this particular DECL has a section associated with it, use that.
13794    2. If this function has a section associated with it, use that.
13795    3. Otherwise, use the text section.
13796    XXX: If you split a variable across multiple sections, we won't notice.  */
13797
13798 static const char *
13799 secname_for_decl (const_tree decl)
13800 {
13801   const char *secname;
13802
13803   if (VAR_OR_FUNCTION_DECL_P (decl)
13804       && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
13805       && DECL_SECTION_NAME (decl))
13806     secname = DECL_SECTION_NAME (decl);
13807   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13808     secname = DECL_SECTION_NAME (current_function_decl);
13809   else if (cfun && in_cold_section_p)
13810     secname = crtl->subsections.cold_section_label;
13811   else
13812     secname = text_section_label;
13813
13814   return secname;
13815 }
13816
13817 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
13818
13819 static bool
13820 decl_by_reference_p (tree decl)
13821 {
13822   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13823            || TREE_CODE (decl) == VAR_DECL)
13824           && DECL_BY_REFERENCE (decl));
13825 }
13826
13827 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
13828    for VARLOC.  */
13829
13830 static dw_loc_descr_ref
13831 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13832                enum var_init_status initialized)
13833 {
13834   int have_address = 0;
13835   dw_loc_descr_ref descr;
13836   machine_mode mode;
13837
13838   if (want_address != 2)
13839     {
13840       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13841       /* Single part.  */
13842       if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13843         {
13844           varloc = PAT_VAR_LOCATION_LOC (varloc);
13845           if (GET_CODE (varloc) == EXPR_LIST)
13846             varloc = XEXP (varloc, 0);
13847           mode = GET_MODE (varloc);
13848           if (MEM_P (varloc))
13849             {
13850               rtx addr = XEXP (varloc, 0);
13851               descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13852                                           mode, initialized);
13853               if (descr)
13854                 have_address = 1;
13855               else
13856                 {
13857                   rtx x = avoid_constant_pool_reference (varloc);
13858                   if (x != varloc)
13859                     descr = mem_loc_descriptor (x, mode, VOIDmode,
13860                                                 initialized);
13861                 }
13862             }
13863           else
13864             descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13865         }
13866       else
13867         return 0;
13868     }
13869   else
13870     {
13871       if (GET_CODE (varloc) == VAR_LOCATION)
13872         mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13873       else
13874         mode = DECL_MODE (loc);
13875       descr = loc_descriptor (varloc, mode, initialized);
13876       have_address = 1;
13877     }
13878
13879   if (!descr)
13880     return 0;
13881
13882   if (want_address == 2 && !have_address
13883       && (dwarf_version >= 4 || !dwarf_strict))
13884     {
13885       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13886         {
13887           expansion_failed (loc, NULL_RTX,
13888                             "DWARF address size mismatch");
13889           return 0;
13890         }
13891       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13892       have_address = 1;
13893     }
13894   /* Show if we can't fill the request for an address.  */
13895   if (want_address && !have_address)
13896     {
13897       expansion_failed (loc, NULL_RTX,
13898                         "Want address and only have value");
13899       return 0;
13900     }
13901
13902   /* If we've got an address and don't want one, dereference.  */
13903   if (!want_address && have_address)
13904     {
13905       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13906       enum dwarf_location_atom op;
13907
13908       if (size > DWARF2_ADDR_SIZE || size == -1)
13909         {
13910           expansion_failed (loc, NULL_RTX,
13911                             "DWARF address size mismatch");
13912           return 0;
13913         }
13914       else if (size == DWARF2_ADDR_SIZE)
13915         op = DW_OP_deref;
13916       else
13917         op = DW_OP_deref_size;
13918
13919       add_loc_descr (&descr, new_loc_descr (op, size, 0));
13920     }
13921
13922   return descr;
13923 }
13924
13925 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
13926    if it is not possible.  */
13927
13928 static dw_loc_descr_ref
13929 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
13930 {
13931   if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
13932     return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
13933   else if (dwarf_version >= 3 || !dwarf_strict)
13934     return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
13935   else
13936     return NULL;
13937 }
13938
13939 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
13940    for VAR_LOC_NOTE for variable DECL that has been optimized by SRA.  */
13941
13942 static dw_loc_descr_ref
13943 dw_sra_loc_expr (tree decl, rtx loc)
13944 {
13945   rtx p;
13946   unsigned HOST_WIDE_INT padsize = 0;
13947   dw_loc_descr_ref descr, *descr_tail;
13948   unsigned HOST_WIDE_INT decl_size;
13949   rtx varloc;
13950   enum var_init_status initialized;
13951
13952   if (DECL_SIZE (decl) == NULL
13953       || !tree_fits_uhwi_p (DECL_SIZE (decl)))
13954     return NULL;
13955
13956   decl_size = tree_to_uhwi (DECL_SIZE (decl));
13957   descr = NULL;
13958   descr_tail = &descr;
13959
13960   for (p = loc; p; p = XEXP (p, 1))
13961     {
13962       unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
13963       rtx loc_note = *decl_piece_varloc_ptr (p);
13964       dw_loc_descr_ref cur_descr;
13965       dw_loc_descr_ref *tail, last = NULL;
13966       unsigned HOST_WIDE_INT opsize = 0;
13967
13968       if (loc_note == NULL_RTX
13969           || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
13970         {
13971           padsize += bitsize;
13972           continue;
13973         }
13974       initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
13975       varloc = NOTE_VAR_LOCATION (loc_note);
13976       cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
13977       if (cur_descr == NULL)
13978         {
13979           padsize += bitsize;
13980           continue;
13981         }
13982
13983       /* Check that cur_descr either doesn't use
13984          DW_OP_*piece operations, or their sum is equal
13985          to bitsize.  Otherwise we can't embed it.  */
13986       for (tail = &cur_descr; *tail != NULL;
13987            tail = &(*tail)->dw_loc_next)
13988         if ((*tail)->dw_loc_opc == DW_OP_piece)
13989           {
13990             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
13991                       * BITS_PER_UNIT;
13992             last = *tail;
13993           }
13994         else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
13995           {
13996             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
13997             last = *tail;
13998           }
13999
14000       if (last != NULL && opsize != bitsize)
14001         {
14002           padsize += bitsize;
14003           /* Discard the current piece of the descriptor and release any
14004              addr_table entries it uses.  */
14005           remove_loc_list_addr_table_entries (cur_descr);
14006           continue;
14007         }
14008
14009       /* If there is a hole, add DW_OP_*piece after empty DWARF
14010          expression, which means that those bits are optimized out.  */
14011       if (padsize)
14012         {
14013           if (padsize > decl_size)
14014             {
14015               remove_loc_list_addr_table_entries (cur_descr);
14016               goto discard_descr;
14017             }
14018           decl_size -= padsize;
14019           *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
14020           if (*descr_tail == NULL)
14021             {
14022               remove_loc_list_addr_table_entries (cur_descr);
14023               goto discard_descr;
14024             }
14025           descr_tail = &(*descr_tail)->dw_loc_next;
14026           padsize = 0;
14027         }
14028       *descr_tail = cur_descr;
14029       descr_tail = tail;
14030       if (bitsize > decl_size)
14031         goto discard_descr;
14032       decl_size -= bitsize;
14033       if (last == NULL)
14034         {
14035           HOST_WIDE_INT offset = 0;
14036           if (GET_CODE (varloc) == VAR_LOCATION
14037               && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14038             {
14039               varloc = PAT_VAR_LOCATION_LOC (varloc);
14040               if (GET_CODE (varloc) == EXPR_LIST)
14041                 varloc = XEXP (varloc, 0);
14042             }
14043           do 
14044             {
14045               if (GET_CODE (varloc) == CONST
14046                   || GET_CODE (varloc) == SIGN_EXTEND
14047                   || GET_CODE (varloc) == ZERO_EXTEND)
14048                 varloc = XEXP (varloc, 0);
14049               else if (GET_CODE (varloc) == SUBREG)
14050                 varloc = SUBREG_REG (varloc);
14051               else
14052                 break;
14053             }
14054           while (1);
14055           /* DW_OP_bit_size offset should be zero for register
14056              or implicit location descriptions and empty location
14057              descriptions, but for memory addresses needs big endian
14058              adjustment.  */
14059           if (MEM_P (varloc))
14060             {
14061               unsigned HOST_WIDE_INT memsize
14062                 = MEM_SIZE (varloc) * BITS_PER_UNIT;
14063               if (memsize != bitsize)
14064                 {
14065                   if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
14066                       && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
14067                     goto discard_descr;
14068                   if (memsize < bitsize)
14069                     goto discard_descr;
14070                   if (BITS_BIG_ENDIAN)
14071                     offset = memsize - bitsize;
14072                 }
14073             }
14074
14075           *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
14076           if (*descr_tail == NULL)
14077             goto discard_descr;
14078           descr_tail = &(*descr_tail)->dw_loc_next;
14079         }
14080     }
14081
14082   /* If there were any non-empty expressions, add padding till the end of
14083      the decl.  */
14084   if (descr != NULL && decl_size != 0)
14085     {
14086       *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
14087       if (*descr_tail == NULL)
14088         goto discard_descr;
14089     }
14090   return descr;
14091
14092 discard_descr:
14093   /* Discard the descriptor and release any addr_table entries it uses.  */
14094   remove_loc_list_addr_table_entries (descr);
14095   return NULL;
14096 }
14097
14098 /* Return the dwarf representation of the location list LOC_LIST of
14099    DECL.  WANT_ADDRESS has the same meaning as in loc_list_from_tree
14100    function.  */
14101
14102 static dw_loc_list_ref
14103 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
14104 {
14105   const char *endname, *secname;
14106   rtx varloc;
14107   enum var_init_status initialized;
14108   struct var_loc_node *node;
14109   dw_loc_descr_ref descr;
14110   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
14111   dw_loc_list_ref list = NULL;
14112   dw_loc_list_ref *listp = &list;
14113
14114   /* Now that we know what section we are using for a base,
14115      actually construct the list of locations.
14116      The first location information is what is passed to the
14117      function that creates the location list, and the remaining
14118      locations just get added on to that list.
14119      Note that we only know the start address for a location
14120      (IE location changes), so to build the range, we use
14121      the range [current location start, next location start].
14122      This means we have to special case the last node, and generate
14123      a range of [last location start, end of function label].  */
14124
14125   secname = secname_for_decl (decl);
14126
14127   for (node = loc_list->first; node; node = node->next)
14128     if (GET_CODE (node->loc) == EXPR_LIST
14129         || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
14130       {
14131         if (GET_CODE (node->loc) == EXPR_LIST)
14132           {
14133             /* This requires DW_OP_{,bit_}piece, which is not usable
14134                inside DWARF expressions.  */
14135             if (want_address != 2)
14136               continue;
14137             descr = dw_sra_loc_expr (decl, node->loc);
14138             if (descr == NULL)
14139               continue;
14140           }
14141         else
14142           {
14143             initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14144             varloc = NOTE_VAR_LOCATION (node->loc);
14145             descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
14146           }
14147         if (descr)
14148           {
14149             bool range_across_switch = false;
14150             /* If section switch happens in between node->label
14151                and node->next->label (or end of function) and
14152                we can't emit it as a single entry list,
14153                emit two ranges, first one ending at the end
14154                of first partition and second one starting at the
14155                beginning of second partition.  */
14156             if (node == loc_list->last_before_switch
14157                 && (node != loc_list->first || loc_list->first->next)
14158                 && current_function_decl)
14159               {
14160                 endname = cfun->fde->dw_fde_end;
14161                 range_across_switch = true;
14162               }
14163             /* The variable has a location between NODE->LABEL and
14164                NODE->NEXT->LABEL.  */
14165             else if (node->next)
14166               endname = node->next->label;
14167             /* If the variable has a location at the last label
14168                it keeps its location until the end of function.  */
14169             else if (!current_function_decl)
14170               endname = text_end_label;
14171             else
14172               {
14173                 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14174                                              current_function_funcdef_no);
14175                 endname = ggc_strdup (label_id);
14176               }
14177
14178             *listp = new_loc_list (descr, node->label, endname, secname);
14179             if (TREE_CODE (decl) == PARM_DECL
14180                 && node == loc_list->first
14181                 && NOTE_P (node->loc)
14182                 && strcmp (node->label, endname) == 0)
14183               (*listp)->force = true;
14184             listp = &(*listp)->dw_loc_next;
14185
14186             if (range_across_switch)
14187               {
14188                 if (GET_CODE (node->loc) == EXPR_LIST)
14189                   descr = dw_sra_loc_expr (decl, node->loc);
14190                 else
14191                   {
14192                     initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14193                     varloc = NOTE_VAR_LOCATION (node->loc);
14194                     descr = dw_loc_list_1 (decl, varloc, want_address,
14195                                            initialized);
14196                   }
14197                 gcc_assert (descr);
14198                 /* The variable has a location between NODE->LABEL and
14199                    NODE->NEXT->LABEL.  */
14200                 if (node->next)
14201                   endname = node->next->label;
14202                 else
14203                   endname = cfun->fde->dw_fde_second_end;
14204                 *listp = new_loc_list (descr,
14205                                        cfun->fde->dw_fde_second_begin,
14206                                        endname, secname);
14207                 listp = &(*listp)->dw_loc_next;
14208               }
14209           }
14210       }
14211
14212   /* Try to avoid the overhead of a location list emitting a location
14213      expression instead, but only if we didn't have more than one
14214      location entry in the first place.  If some entries were not
14215      representable, we don't want to pretend a single entry that was
14216      applies to the entire scope in which the variable is
14217      available.  */
14218   if (list && loc_list->first->next)
14219     gen_llsym (list);
14220
14221   return list;
14222 }
14223
14224 /* Return if the loc_list has only single element and thus can be represented
14225    as location description.   */
14226
14227 static bool
14228 single_element_loc_list_p (dw_loc_list_ref list)
14229 {
14230   gcc_assert (!list->dw_loc_next || list->ll_symbol);
14231   return !list->ll_symbol;
14232 }
14233
14234 /* To each location in list LIST add loc descr REF.  */
14235
14236 static void
14237 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
14238 {
14239   dw_loc_descr_ref copy;
14240   add_loc_descr (&list->expr, ref);
14241   list = list->dw_loc_next;
14242   while (list)
14243     {
14244       copy = ggc_alloc<dw_loc_descr_node> ();
14245       memcpy (copy, ref, sizeof (dw_loc_descr_node));
14246       add_loc_descr (&list->expr, copy);
14247       while (copy->dw_loc_next)
14248         {
14249           dw_loc_descr_ref new_copy = ggc_alloc<dw_loc_descr_node> ();
14250           memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
14251           copy->dw_loc_next = new_copy;
14252           copy = new_copy;
14253         }
14254       list = list->dw_loc_next;
14255     }
14256 }
14257
14258 /* Given two lists RET and LIST
14259    produce location list that is result of adding expression in LIST
14260    to expression in RET on each position in program.
14261    Might be destructive on both RET and LIST.
14262
14263    TODO: We handle only simple cases of RET or LIST having at most one
14264    element. General case would inolve sorting the lists in program order
14265    and merging them that will need some additional work.
14266    Adding that will improve quality of debug info especially for SRA-ed
14267    structures.  */
14268
14269 static void
14270 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
14271 {
14272   if (!list)
14273     return;
14274   if (!*ret)
14275     {
14276       *ret = list;
14277       return;
14278     }
14279   if (!list->dw_loc_next)
14280     {
14281       add_loc_descr_to_each (*ret, list->expr);
14282       return;
14283     }
14284   if (!(*ret)->dw_loc_next)
14285     {
14286       add_loc_descr_to_each (list, (*ret)->expr);
14287       *ret = list;
14288       return;
14289     }
14290   expansion_failed (NULL_TREE, NULL_RTX,
14291                     "Don't know how to merge two non-trivial"
14292                     " location lists.\n");
14293   *ret = NULL;
14294   return;
14295 }
14296
14297 /* LOC is constant expression.  Try a luck, look it up in constant
14298    pool and return its loc_descr of its address.  */
14299
14300 static dw_loc_descr_ref
14301 cst_pool_loc_descr (tree loc)
14302 {
14303   /* Get an RTL for this, if something has been emitted.  */
14304   rtx rtl = lookup_constant_def (loc);
14305
14306   if (!rtl || !MEM_P (rtl))
14307     {
14308       gcc_assert (!rtl);
14309       return 0;
14310     }
14311   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
14312
14313   /* TODO: We might get more coverage if we was actually delaying expansion
14314      of all expressions till end of compilation when constant pools are fully
14315      populated.  */
14316   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
14317     {
14318       expansion_failed (loc, NULL_RTX,
14319                         "CST value in contant pool but not marked.");
14320       return 0;
14321     }
14322   return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
14323                              GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
14324 }
14325
14326 /* Return dw_loc_list representing address of addr_expr LOC
14327    by looking for inner INDIRECT_REF expression and turning
14328    it into simple arithmetics.
14329
14330    See loc_list_from_tree for the meaning of CONTEXT.  */
14331
14332 static dw_loc_list_ref
14333 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
14334                                                    const loc_descr_context *context)
14335 {
14336   tree obj, offset;
14337   HOST_WIDE_INT bitsize, bitpos, bytepos;
14338   machine_mode mode;
14339   int unsignedp, volatilep = 0;
14340   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14341
14342   obj = get_inner_reference (TREE_OPERAND (loc, 0),
14343                              &bitsize, &bitpos, &offset, &mode,
14344                              &unsignedp, &volatilep, false);
14345   STRIP_NOPS (obj);
14346   if (bitpos % BITS_PER_UNIT)
14347     {
14348       expansion_failed (loc, NULL_RTX, "bitfield access");
14349       return 0;
14350     }
14351   if (!INDIRECT_REF_P (obj))
14352     {
14353       expansion_failed (obj,
14354                         NULL_RTX, "no indirect ref in inner refrence");
14355       return 0;
14356     }
14357   if (!offset && !bitpos)
14358     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
14359                                    context);
14360   else if (toplev
14361            && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
14362            && (dwarf_version >= 4 || !dwarf_strict))
14363     {
14364       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
14365       if (!list_ret)
14366         return 0;
14367       if (offset)
14368         {
14369           /* Variable offset.  */
14370           list_ret1 = loc_list_from_tree (offset, 0, context);
14371           if (list_ret1 == 0)
14372             return 0;
14373           add_loc_list (&list_ret, list_ret1);
14374           if (!list_ret)
14375             return 0;
14376           add_loc_descr_to_each (list_ret,
14377                                  new_loc_descr (DW_OP_plus, 0, 0));
14378         }
14379       bytepos = bitpos / BITS_PER_UNIT;
14380       if (bytepos > 0)
14381         add_loc_descr_to_each (list_ret,
14382                                new_loc_descr (DW_OP_plus_uconst,
14383                                               bytepos, 0));
14384       else if (bytepos < 0)
14385         loc_list_plus_const (list_ret, bytepos);
14386       add_loc_descr_to_each (list_ret,
14387                              new_loc_descr (DW_OP_stack_value, 0, 0));
14388     }
14389   return list_ret;
14390 }
14391
14392
14393 /* Helper structure for location descriptions generation.  */
14394 struct loc_descr_context
14395 {
14396   /* The type that is implicitly referenced by DW_OP_push_object_address, or
14397      NULL_TREE if DW_OP_push_object_address in invalid for this location
14398      description.  This is used when processing PLACEHOLDER_EXPR nodes.  */
14399   tree context_type;
14400   /* The ..._DECL node that should be translated as a
14401      DW_OP_push_object_address operation.  */
14402   tree base_decl;
14403 };
14404
14405 /* Generate Dwarf location list representing LOC.
14406    If WANT_ADDRESS is false, expression computing LOC will be computed
14407    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
14408    if WANT_ADDRESS is 2, expression computing address useable in location
14409      will be returned (i.e. DW_OP_reg can be used
14410      to refer to register values).
14411
14412    CONTEXT provides information to customize the location descriptions
14413    generation.  Its context_type field specifies what type is implicitly
14414    referenced by DW_OP_push_object_address.  If it is NULL_TREE, this operation
14415    will not be generated.
14416
14417    If CONTEXT is NULL, the behavior is the same as if both context_type and
14418    base_decl fields were NULL_TREE.  */
14419
14420 static dw_loc_list_ref
14421 loc_list_from_tree (tree loc, int want_address,
14422                     const struct loc_descr_context *context)
14423 {
14424   dw_loc_descr_ref ret = NULL, ret1 = NULL;
14425   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14426   int have_address = 0;
14427   enum dwarf_location_atom op;
14428
14429   /* ??? Most of the time we do not take proper care for sign/zero
14430      extending the values properly.  Hopefully this won't be a real
14431      problem...  */
14432
14433   if (context != NULL
14434       && context->base_decl == loc
14435       && want_address == 0)
14436     {
14437       if (dwarf_version >= 3 || !dwarf_strict)
14438         return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
14439                              NULL, NULL, NULL);
14440       else
14441         return NULL;
14442     }
14443
14444   switch (TREE_CODE (loc))
14445     {
14446     case ERROR_MARK:
14447       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
14448       return 0;
14449
14450     case PLACEHOLDER_EXPR:
14451       /* This case involves extracting fields from an object to determine the
14452          position of other fields. It is supposed to appear only as the first
14453          operand of COMPONENT_REF nodes and to reference precisely the type
14454          that the context allows.  */
14455       if (context != NULL
14456           && TREE_TYPE (loc) == context->context_type
14457           && want_address >= 1)
14458         {
14459           if (dwarf_version >= 3 || !dwarf_strict)
14460             {
14461               ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
14462               have_address = 1;
14463               break;
14464             }
14465           else
14466             return NULL;
14467         }
14468       else
14469         expansion_failed (loc, NULL_RTX,
14470                           "PLACEHOLDER_EXPR for an unexpected type");
14471       break;
14472
14473     case CALL_EXPR:
14474       expansion_failed (loc, NULL_RTX, "CALL_EXPR");
14475       /* There are no opcodes for these operations.  */
14476       return 0;
14477
14478     case PREINCREMENT_EXPR:
14479     case PREDECREMENT_EXPR:
14480     case POSTINCREMENT_EXPR:
14481     case POSTDECREMENT_EXPR:
14482       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
14483       /* There are no opcodes for these operations.  */
14484       return 0;
14485
14486     case ADDR_EXPR:
14487       /* If we already want an address, see if there is INDIRECT_REF inside
14488          e.g. for &this->field.  */
14489       if (want_address)
14490         {
14491           list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
14492                        (loc, want_address == 2, context);
14493           if (list_ret)
14494             have_address = 1;
14495           else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
14496                    && (ret = cst_pool_loc_descr (loc)))
14497             have_address = 1;
14498         }
14499         /* Otherwise, process the argument and look for the address.  */
14500       if (!list_ret && !ret)
14501         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1, context);
14502       else
14503         {
14504           if (want_address)
14505             expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
14506           return NULL;
14507         }
14508       break;
14509
14510     case VAR_DECL:
14511       if (DECL_THREAD_LOCAL_P (loc))
14512         {
14513           rtx rtl;
14514          enum dwarf_location_atom tls_op;
14515          enum dtprel_bool dtprel = dtprel_false;
14516
14517           if (targetm.have_tls)
14518             {
14519               /* If this is not defined, we have no way to emit the
14520                  data.  */
14521               if (!targetm.asm_out.output_dwarf_dtprel)
14522                 return 0;
14523
14524                /* The way DW_OP_GNU_push_tls_address is specified, we
14525                   can only look up addresses of objects in the current
14526                   module.  We used DW_OP_addr as first op, but that's
14527                   wrong, because DW_OP_addr is relocated by the debug
14528                   info consumer, while DW_OP_GNU_push_tls_address
14529                   operand shouldn't be.  */
14530               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
14531                 return 0;
14532              dtprel = dtprel_true;
14533              tls_op = DW_OP_GNU_push_tls_address;
14534             }
14535           else
14536             {
14537               if (!targetm.emutls.debug_form_tls_address
14538                   || !(dwarf_version >= 3 || !dwarf_strict))
14539                 return 0;
14540               /* We stuffed the control variable into the DECL_VALUE_EXPR
14541                  to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
14542                  no longer appear in gimple code.  We used the control
14543                  variable in specific so that we could pick it up here.  */
14544               loc = DECL_VALUE_EXPR (loc);
14545               tls_op = DW_OP_form_tls_address;
14546             }
14547
14548           rtl = rtl_for_decl_location (loc);
14549           if (rtl == NULL_RTX)
14550             return 0;
14551
14552           if (!MEM_P (rtl))
14553             return 0;
14554           rtl = XEXP (rtl, 0);
14555           if (! CONSTANT_P (rtl))
14556             return 0;
14557
14558           ret = new_addr_loc_descr (rtl, dtprel);
14559           ret1 = new_loc_descr (tls_op, 0, 0);
14560           add_loc_descr (&ret, ret1);
14561
14562           have_address = 1;
14563           break;
14564         }
14565       /* FALLTHRU */
14566
14567     case PARM_DECL:
14568     case RESULT_DECL:
14569       if (DECL_HAS_VALUE_EXPR_P (loc))
14570         return loc_list_from_tree (DECL_VALUE_EXPR (loc),
14571                                    want_address, context);
14572       /* FALLTHRU */
14573
14574     case FUNCTION_DECL:
14575       {
14576         rtx rtl;
14577         var_loc_list *loc_list = lookup_decl_loc (loc);
14578
14579         if (loc_list && loc_list->first)
14580           {
14581             list_ret = dw_loc_list (loc_list, loc, want_address);
14582             have_address = want_address != 0;
14583             break;
14584           }
14585         rtl = rtl_for_decl_location (loc);
14586         if (rtl == NULL_RTX)
14587           {
14588             expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14589             return 0;
14590           }
14591         else if (CONST_INT_P (rtl))
14592           {
14593             HOST_WIDE_INT val = INTVAL (rtl);
14594             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14595               val &= GET_MODE_MASK (DECL_MODE (loc));
14596             ret = int_loc_descriptor (val);
14597           }
14598         else if (GET_CODE (rtl) == CONST_STRING)
14599           {
14600             expansion_failed (loc, NULL_RTX, "CONST_STRING");
14601             return 0;
14602           }
14603         else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14604           ret = new_addr_loc_descr (rtl, dtprel_false);
14605         else
14606           {
14607             machine_mode mode, mem_mode;
14608
14609             /* Certain constructs can only be represented at top-level.  */
14610             if (want_address == 2)
14611               {
14612                 ret = loc_descriptor (rtl, VOIDmode,
14613                                       VAR_INIT_STATUS_INITIALIZED);
14614                 have_address = 1;
14615               }
14616             else
14617               {
14618                 mode = GET_MODE (rtl);
14619                 mem_mode = VOIDmode;
14620                 if (MEM_P (rtl))
14621                   {
14622                     mem_mode = mode;
14623                     mode = get_address_mode (rtl);
14624                     rtl = XEXP (rtl, 0);
14625                     have_address = 1;
14626                   }
14627                 ret = mem_loc_descriptor (rtl, mode, mem_mode,
14628                                           VAR_INIT_STATUS_INITIALIZED);
14629               }
14630             if (!ret)
14631               expansion_failed (loc, rtl,
14632                                 "failed to produce loc descriptor for rtl");
14633           }
14634       }
14635       break;
14636
14637     case MEM_REF:
14638       if (!integer_zerop (TREE_OPERAND (loc, 1)))
14639         {
14640           have_address = 1;
14641           goto do_plus;
14642         }
14643       /* Fallthru.  */
14644     case INDIRECT_REF:
14645       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14646       have_address = 1;
14647       break;
14648
14649     case TARGET_MEM_REF:
14650     case SSA_NAME:
14651     case DEBUG_EXPR_DECL:
14652       return NULL;
14653
14654     case COMPOUND_EXPR:
14655       return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address, context);
14656
14657     CASE_CONVERT:
14658     case VIEW_CONVERT_EXPR:
14659     case SAVE_EXPR:
14660     case MODIFY_EXPR:
14661       return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address, context);
14662
14663     case COMPONENT_REF:
14664     case BIT_FIELD_REF:
14665     case ARRAY_REF:
14666     case ARRAY_RANGE_REF:
14667     case REALPART_EXPR:
14668     case IMAGPART_EXPR:
14669       {
14670         tree obj, offset;
14671         HOST_WIDE_INT bitsize, bitpos, bytepos;
14672         machine_mode mode;
14673         int unsignedp, volatilep = 0;
14674
14675         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14676                                    &unsignedp, &volatilep, false);
14677
14678         gcc_assert (obj != loc);
14679
14680         list_ret = loc_list_from_tree (obj,
14681                                        want_address == 2
14682                                        && !bitpos && !offset ? 2 : 1,
14683                                        context);
14684         /* TODO: We can extract value of the small expression via shifting even
14685            for nonzero bitpos.  */
14686         if (list_ret == 0)
14687           return 0;
14688         if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14689           {
14690             expansion_failed (loc, NULL_RTX,
14691                               "bitfield access");
14692             return 0;
14693           }
14694
14695         if (offset != NULL_TREE)
14696           {
14697             /* Variable offset.  */
14698             list_ret1 = loc_list_from_tree (offset, 0, context);
14699             if (list_ret1 == 0)
14700               return 0;
14701             add_loc_list (&list_ret, list_ret1);
14702             if (!list_ret)
14703               return 0;
14704             add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14705           }
14706
14707         bytepos = bitpos / BITS_PER_UNIT;
14708         if (bytepos > 0)
14709           add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14710         else if (bytepos < 0)
14711           loc_list_plus_const (list_ret, bytepos);
14712
14713         have_address = 1;
14714         break;
14715       }
14716
14717     case INTEGER_CST:
14718       if ((want_address || !tree_fits_shwi_p (loc))
14719           && (ret = cst_pool_loc_descr (loc)))
14720         have_address = 1;
14721       else if (want_address == 2
14722                && tree_fits_shwi_p (loc)
14723                && (ret = address_of_int_loc_descriptor
14724                            (int_size_in_bytes (TREE_TYPE (loc)),
14725                             tree_to_shwi (loc))))
14726         have_address = 1;
14727       else if (tree_fits_shwi_p (loc))
14728         ret = int_loc_descriptor (tree_to_shwi (loc));
14729       else
14730         {
14731           expansion_failed (loc, NULL_RTX,
14732                             "Integer operand is not host integer");
14733           return 0;
14734         }
14735       break;
14736
14737     case CONSTRUCTOR:
14738     case REAL_CST:
14739     case STRING_CST:
14740     case COMPLEX_CST:
14741       if ((ret = cst_pool_loc_descr (loc)))
14742         have_address = 1;
14743       else
14744       /* We can construct small constants here using int_loc_descriptor.  */
14745         expansion_failed (loc, NULL_RTX,
14746                           "constructor or constant not in constant pool");
14747       break;
14748
14749     case TRUTH_AND_EXPR:
14750     case TRUTH_ANDIF_EXPR:
14751     case BIT_AND_EXPR:
14752       op = DW_OP_and;
14753       goto do_binop;
14754
14755     case TRUTH_XOR_EXPR:
14756     case BIT_XOR_EXPR:
14757       op = DW_OP_xor;
14758       goto do_binop;
14759
14760     case TRUTH_OR_EXPR:
14761     case TRUTH_ORIF_EXPR:
14762     case BIT_IOR_EXPR:
14763       op = DW_OP_or;
14764       goto do_binop;
14765
14766     case FLOOR_DIV_EXPR:
14767     case CEIL_DIV_EXPR:
14768     case ROUND_DIV_EXPR:
14769     case TRUNC_DIV_EXPR:
14770       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14771         return 0;
14772       op = DW_OP_div;
14773       goto do_binop;
14774
14775     case MINUS_EXPR:
14776       op = DW_OP_minus;
14777       goto do_binop;
14778
14779     case FLOOR_MOD_EXPR:
14780     case CEIL_MOD_EXPR:
14781     case ROUND_MOD_EXPR:
14782     case TRUNC_MOD_EXPR:
14783       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14784         {
14785           op = DW_OP_mod;
14786           goto do_binop;
14787         }
14788       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14789       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
14790       if (list_ret == 0 || list_ret1 == 0)
14791         return 0;
14792
14793       add_loc_list (&list_ret, list_ret1);
14794       if (list_ret == 0)
14795         return 0;
14796       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14797       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14798       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14799       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14800       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14801       break;
14802
14803     case MULT_EXPR:
14804       op = DW_OP_mul;
14805       goto do_binop;
14806
14807     case LSHIFT_EXPR:
14808       op = DW_OP_shl;
14809       goto do_binop;
14810
14811     case RSHIFT_EXPR:
14812       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14813       goto do_binop;
14814
14815     case POINTER_PLUS_EXPR:
14816     case PLUS_EXPR:
14817     do_plus:
14818       if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
14819         {
14820           list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14821           if (list_ret == 0)
14822             return 0;
14823
14824           loc_list_plus_const (list_ret, tree_to_shwi (TREE_OPERAND (loc, 1)));
14825           break;
14826         }
14827
14828       op = DW_OP_plus;
14829       goto do_binop;
14830
14831     case LE_EXPR:
14832       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14833         return 0;
14834
14835       op = DW_OP_le;
14836       goto do_binop;
14837
14838     case GE_EXPR:
14839       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14840         return 0;
14841
14842       op = DW_OP_ge;
14843       goto do_binop;
14844
14845     case LT_EXPR:
14846       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14847         return 0;
14848
14849       op = DW_OP_lt;
14850       goto do_binop;
14851
14852     case GT_EXPR:
14853       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14854         return 0;
14855
14856       op = DW_OP_gt;
14857       goto do_binop;
14858
14859     case EQ_EXPR:
14860       op = DW_OP_eq;
14861       goto do_binop;
14862
14863     case NE_EXPR:
14864       op = DW_OP_ne;
14865       goto do_binop;
14866
14867     do_binop:
14868       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14869       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
14870       if (list_ret == 0 || list_ret1 == 0)
14871         return 0;
14872
14873       add_loc_list (&list_ret, list_ret1);
14874       if (list_ret == 0)
14875         return 0;
14876       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14877       break;
14878
14879     case TRUTH_NOT_EXPR:
14880     case BIT_NOT_EXPR:
14881       op = DW_OP_not;
14882       goto do_unop;
14883
14884     case ABS_EXPR:
14885       op = DW_OP_abs;
14886       goto do_unop;
14887
14888     case NEGATE_EXPR:
14889       op = DW_OP_neg;
14890       goto do_unop;
14891
14892     do_unop:
14893       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14894       if (list_ret == 0)
14895         return 0;
14896
14897       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14898       break;
14899
14900     case MIN_EXPR:
14901     case MAX_EXPR:
14902       {
14903         const enum tree_code code =
14904           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14905
14906         loc = build3 (COND_EXPR, TREE_TYPE (loc),
14907                       build2 (code, integer_type_node,
14908                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14909                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14910       }
14911
14912       /* ... fall through ...  */
14913
14914     case COND_EXPR:
14915       {
14916         dw_loc_descr_ref lhs
14917           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
14918         dw_loc_list_ref rhs
14919           = loc_list_from_tree (TREE_OPERAND (loc, 2), 0, context);
14920         dw_loc_descr_ref bra_node, jump_node, tmp;
14921
14922         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14923         if (list_ret == 0 || lhs == 0 || rhs == 0)
14924           return 0;
14925
14926         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14927         add_loc_descr_to_each (list_ret, bra_node);
14928
14929         add_loc_list (&list_ret, rhs);
14930         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14931         add_loc_descr_to_each (list_ret, jump_node);
14932
14933         add_loc_descr_to_each (list_ret, lhs);
14934         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14935         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14936
14937         /* ??? Need a node to point the skip at.  Use a nop.  */
14938         tmp = new_loc_descr (DW_OP_nop, 0, 0);
14939         add_loc_descr_to_each (list_ret, tmp);
14940         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14941         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14942       }
14943       break;
14944
14945     case FIX_TRUNC_EXPR:
14946       return 0;
14947
14948     default:
14949       /* Leave front-end specific codes as simply unknown.  This comes
14950          up, for instance, with the C STMT_EXPR.  */
14951       if ((unsigned int) TREE_CODE (loc)
14952           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14953         {
14954           expansion_failed (loc, NULL_RTX,
14955                             "language specific tree node");
14956           return 0;
14957         }
14958
14959 #ifdef ENABLE_CHECKING
14960       /* Otherwise this is a generic code; we should just lists all of
14961          these explicitly.  We forgot one.  */
14962       gcc_unreachable ();
14963 #else
14964       /* In a release build, we want to degrade gracefully: better to
14965          generate incomplete debugging information than to crash.  */
14966       return NULL;
14967 #endif
14968     }
14969
14970   if (!ret && !list_ret)
14971     return 0;
14972
14973   if (want_address == 2 && !have_address
14974       && (dwarf_version >= 4 || !dwarf_strict))
14975     {
14976       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14977         {
14978           expansion_failed (loc, NULL_RTX,
14979                             "DWARF address size mismatch");
14980           return 0;
14981         }
14982       if (ret)
14983         add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
14984       else
14985         add_loc_descr_to_each (list_ret,
14986                                new_loc_descr (DW_OP_stack_value, 0, 0));
14987       have_address = 1;
14988     }
14989   /* Show if we can't fill the request for an address.  */
14990   if (want_address && !have_address)
14991     {
14992       expansion_failed (loc, NULL_RTX,
14993                         "Want address and only have value");
14994       return 0;
14995     }
14996
14997   gcc_assert (!ret || !list_ret);
14998
14999   /* If we've got an address and don't want one, dereference.  */
15000   if (!want_address && have_address)
15001     {
15002       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15003
15004       if (size > DWARF2_ADDR_SIZE || size == -1)
15005         {
15006           expansion_failed (loc, NULL_RTX,
15007                             "DWARF address size mismatch");
15008           return 0;
15009         }
15010       else if (size == DWARF2_ADDR_SIZE)
15011         op = DW_OP_deref;
15012       else
15013         op = DW_OP_deref_size;
15014
15015       if (ret)
15016         add_loc_descr (&ret, new_loc_descr (op, size, 0));
15017       else
15018         add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
15019     }
15020   if (ret)
15021     list_ret = new_loc_list (ret, NULL, NULL, NULL);
15022
15023   return list_ret;
15024 }
15025
15026 /* Same as above but return only single location expression.  */
15027 static dw_loc_descr_ref
15028 loc_descriptor_from_tree (tree loc, int want_address,
15029                           const struct loc_descr_context *context)
15030 {
15031   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
15032   if (!ret)
15033     return NULL;
15034   if (ret->dw_loc_next)
15035     {
15036       expansion_failed (loc, NULL_RTX,
15037                         "Location list where only loc descriptor needed");
15038       return NULL;
15039     }
15040   return ret->expr;
15041 }
15042
15043 /* Given a value, round it up to the lowest multiple of `boundary'
15044    which is not less than the value itself.  */
15045
15046 static inline HOST_WIDE_INT
15047 ceiling (HOST_WIDE_INT value, unsigned int boundary)
15048 {
15049   return (((value + boundary - 1) / boundary) * boundary);
15050 }
15051
15052 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
15053    pointer to the declared type for the relevant field variable, or return
15054    `integer_type_node' if the given node turns out to be an
15055    ERROR_MARK node.  */
15056
15057 static inline tree
15058 field_type (const_tree decl)
15059 {
15060   tree type;
15061
15062   if (TREE_CODE (decl) == ERROR_MARK)
15063     return integer_type_node;
15064
15065   type = DECL_BIT_FIELD_TYPE (decl);
15066   if (type == NULL_TREE)
15067     type = TREE_TYPE (decl);
15068
15069   return type;
15070 }
15071
15072 /* Given a pointer to a tree node, return the alignment in bits for
15073    it, or else return BITS_PER_WORD if the node actually turns out to
15074    be an ERROR_MARK node.  */
15075
15076 static inline unsigned
15077 simple_type_align_in_bits (const_tree type)
15078 {
15079   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
15080 }
15081
15082 static inline unsigned
15083 simple_decl_align_in_bits (const_tree decl)
15084 {
15085   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
15086 }
15087
15088 /* Return the result of rounding T up to ALIGN.  */
15089
15090 static inline offset_int
15091 round_up_to_align (const offset_int &t, unsigned int align)
15092 {
15093   return wi::udiv_trunc (t + align - 1, align) * align;
15094 }
15095
15096 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
15097    lowest addressed byte of the "containing object" for the given FIELD_DECL,
15098    or return 0 if we are unable to determine what that offset is, either
15099    because the argument turns out to be a pointer to an ERROR_MARK node, or
15100    because the offset is actually variable.  (We can't handle the latter case
15101    just yet).  */
15102
15103 static HOST_WIDE_INT
15104 field_byte_offset (const_tree decl)
15105 {
15106   offset_int object_offset_in_bits;
15107   offset_int object_offset_in_bytes;
15108   offset_int bitpos_int;
15109
15110   if (TREE_CODE (decl) == ERROR_MARK)
15111     return 0;
15112
15113   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
15114
15115   /* We cannot yet cope with fields whose positions are variable, so
15116      for now, when we see such things, we simply return 0.  Someday, we may
15117      be able to handle such cases, but it will be damn difficult.  */
15118   if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
15119     return 0;
15120
15121   bitpos_int = wi::to_offset (bit_position (decl));
15122
15123   if (PCC_BITFIELD_TYPE_MATTERS)
15124     {
15125       tree type;
15126       tree field_size_tree;
15127       offset_int deepest_bitpos;
15128       offset_int field_size_in_bits;
15129       unsigned int type_align_in_bits;
15130       unsigned int decl_align_in_bits;
15131       offset_int type_size_in_bits;
15132
15133       type = field_type (decl);
15134       type_size_in_bits = offset_int_type_size_in_bits (type);
15135       type_align_in_bits = simple_type_align_in_bits (type);
15136
15137       field_size_tree = DECL_SIZE (decl);
15138
15139       /* The size could be unspecified if there was an error, or for
15140          a flexible array member.  */
15141       if (!field_size_tree)
15142         field_size_tree = bitsize_zero_node;
15143
15144       /* If the size of the field is not constant, use the type size.  */
15145       if (TREE_CODE (field_size_tree) == INTEGER_CST)
15146         field_size_in_bits = wi::to_offset (field_size_tree);
15147       else
15148         field_size_in_bits = type_size_in_bits;
15149
15150       decl_align_in_bits = simple_decl_align_in_bits (decl);
15151
15152       /* The GCC front-end doesn't make any attempt to keep track of the
15153          starting bit offset (relative to the start of the containing
15154          structure type) of the hypothetical "containing object" for a
15155          bit-field.  Thus, when computing the byte offset value for the
15156          start of the "containing object" of a bit-field, we must deduce
15157          this information on our own. This can be rather tricky to do in
15158          some cases.  For example, handling the following structure type
15159          definition when compiling for an i386/i486 target (which only
15160          aligns long long's to 32-bit boundaries) can be very tricky:
15161
15162          struct S { int field1; long long field2:31; };
15163
15164          Fortunately, there is a simple rule-of-thumb which can be used
15165          in such cases.  When compiling for an i386/i486, GCC will
15166          allocate 8 bytes for the structure shown above.  It decides to
15167          do this based upon one simple rule for bit-field allocation.
15168          GCC allocates each "containing object" for each bit-field at
15169          the first (i.e. lowest addressed) legitimate alignment boundary
15170          (based upon the required minimum alignment for the declared
15171          type of the field) which it can possibly use, subject to the
15172          condition that there is still enough available space remaining
15173          in the containing object (when allocated at the selected point)
15174          to fully accommodate all of the bits of the bit-field itself.
15175
15176          This simple rule makes it obvious why GCC allocates 8 bytes for
15177          each object of the structure type shown above.  When looking
15178          for a place to allocate the "containing object" for `field2',
15179          the compiler simply tries to allocate a 64-bit "containing
15180          object" at each successive 32-bit boundary (starting at zero)
15181          until it finds a place to allocate that 64- bit field such that
15182          at least 31 contiguous (and previously unallocated) bits remain
15183          within that selected 64 bit field.  (As it turns out, for the
15184          example above, the compiler finds it is OK to allocate the
15185          "containing object" 64-bit field at bit-offset zero within the
15186          structure type.)
15187
15188          Here we attempt to work backwards from the limited set of facts
15189          we're given, and we try to deduce from those facts, where GCC
15190          must have believed that the containing object started (within
15191          the structure type). The value we deduce is then used (by the
15192          callers of this routine) to generate DW_AT_location and
15193          DW_AT_bit_offset attributes for fields (both bit-fields and, in
15194          the case of DW_AT_location, regular fields as well).  */
15195
15196       /* Figure out the bit-distance from the start of the structure to
15197          the "deepest" bit of the bit-field.  */
15198       deepest_bitpos = bitpos_int + field_size_in_bits;
15199
15200       /* This is the tricky part.  Use some fancy footwork to deduce
15201          where the lowest addressed bit of the containing object must
15202          be.  */
15203       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15204
15205       /* Round up to type_align by default.  This works best for
15206          bitfields.  */
15207       object_offset_in_bits
15208         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
15209
15210       if (wi::gtu_p (object_offset_in_bits, bitpos_int))
15211         {
15212           object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15213
15214           /* Round up to decl_align instead.  */
15215           object_offset_in_bits
15216             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
15217         }
15218     }
15219   else
15220     object_offset_in_bits = bitpos_int;
15221
15222   object_offset_in_bytes
15223     = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
15224   return object_offset_in_bytes.to_shwi ();
15225 }
15226 \f
15227 /* The following routines define various Dwarf attributes and any data
15228    associated with them.  */
15229
15230 /* Add a location description attribute value to a DIE.
15231
15232    This emits location attributes suitable for whole variables and
15233    whole parameters.  Note that the location attributes for struct fields are
15234    generated by the routine `data_member_location_attribute' below.  */
15235
15236 static inline void
15237 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
15238                              dw_loc_list_ref descr)
15239 {
15240   if (descr == 0)
15241     return;
15242   if (single_element_loc_list_p (descr))
15243     add_AT_loc (die, attr_kind, descr->expr);
15244   else
15245     add_AT_loc_list (die, attr_kind, descr);
15246 }
15247
15248 /* Add DW_AT_accessibility attribute to DIE if needed.  */
15249
15250 static void
15251 add_accessibility_attribute (dw_die_ref die, tree decl)
15252 {
15253   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
15254      children, otherwise the default is DW_ACCESS_public.  In DWARF2
15255      the default has always been DW_ACCESS_public.  */
15256   if (TREE_PROTECTED (decl))
15257     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
15258   else if (TREE_PRIVATE (decl))
15259     {
15260       if (dwarf_version == 2
15261           || die->die_parent == NULL
15262           || die->die_parent->die_tag != DW_TAG_class_type)
15263         add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
15264     }
15265   else if (dwarf_version > 2
15266            && die->die_parent
15267            && die->die_parent->die_tag == DW_TAG_class_type)
15268     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
15269 }
15270
15271 /* Attach the specialized form of location attribute used for data members of
15272    struct and union types.  In the special case of a FIELD_DECL node which
15273    represents a bit-field, the "offset" part of this special location
15274    descriptor must indicate the distance in bytes from the lowest-addressed
15275    byte of the containing struct or union type to the lowest-addressed byte of
15276    the "containing object" for the bit-field.  (See the `field_byte_offset'
15277    function above).
15278
15279    For any given bit-field, the "containing object" is a hypothetical object
15280    (of some integral or enum type) within which the given bit-field lives.  The
15281    type of this hypothetical "containing object" is always the same as the
15282    declared type of the individual bit-field itself (for GCC anyway... the
15283    DWARF spec doesn't actually mandate this).  Note that it is the size (in
15284    bytes) of the hypothetical "containing object" which will be given in the
15285    DW_AT_byte_size attribute for this bit-field.  (See the
15286    `byte_size_attribute' function below.)  It is also used when calculating the
15287    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
15288    function below.)  */
15289
15290 static void
15291 add_data_member_location_attribute (dw_die_ref die, tree decl)
15292 {
15293   HOST_WIDE_INT offset;
15294   dw_loc_descr_ref loc_descr = 0;
15295
15296   if (TREE_CODE (decl) == TREE_BINFO)
15297     {
15298       /* We're working on the TAG_inheritance for a base class.  */
15299       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
15300         {
15301           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
15302              aren't at a fixed offset from all (sub)objects of the same
15303              type.  We need to extract the appropriate offset from our
15304              vtable.  The following dwarf expression means
15305
15306                BaseAddr = ObAddr + *((*ObAddr) - Offset)
15307
15308              This is specific to the V3 ABI, of course.  */
15309
15310           dw_loc_descr_ref tmp;
15311
15312           /* Make a copy of the object address.  */
15313           tmp = new_loc_descr (DW_OP_dup, 0, 0);
15314           add_loc_descr (&loc_descr, tmp);
15315
15316           /* Extract the vtable address.  */
15317           tmp = new_loc_descr (DW_OP_deref, 0, 0);
15318           add_loc_descr (&loc_descr, tmp);
15319
15320           /* Calculate the address of the offset.  */
15321           offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
15322           gcc_assert (offset < 0);
15323
15324           tmp = int_loc_descriptor (-offset);
15325           add_loc_descr (&loc_descr, tmp);
15326           tmp = new_loc_descr (DW_OP_minus, 0, 0);
15327           add_loc_descr (&loc_descr, tmp);
15328
15329           /* Extract the offset.  */
15330           tmp = new_loc_descr (DW_OP_deref, 0, 0);
15331           add_loc_descr (&loc_descr, tmp);
15332
15333           /* Add it to the object address.  */
15334           tmp = new_loc_descr (DW_OP_plus, 0, 0);
15335           add_loc_descr (&loc_descr, tmp);
15336         }
15337       else
15338         offset = tree_to_shwi (BINFO_OFFSET (decl));
15339     }
15340   else
15341     offset = field_byte_offset (decl);
15342
15343   if (! loc_descr)
15344     {
15345       if (dwarf_version > 2)
15346         {
15347           /* Don't need to output a location expression, just the constant. */
15348           if (offset < 0)
15349             add_AT_int (die, DW_AT_data_member_location, offset);
15350           else
15351             add_AT_unsigned (die, DW_AT_data_member_location, offset);
15352           return;
15353         }
15354       else
15355         {
15356           enum dwarf_location_atom op;
15357
15358           /* The DWARF2 standard says that we should assume that the structure
15359              address is already on the stack, so we can specify a structure
15360              field address by using DW_OP_plus_uconst.  */
15361           op = DW_OP_plus_uconst;
15362           loc_descr = new_loc_descr (op, offset, 0);
15363         }
15364     }
15365
15366   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
15367 }
15368
15369 /* Writes integer values to dw_vec_const array.  */
15370
15371 static void
15372 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
15373 {
15374   while (size != 0)
15375     {
15376       *dest++ = val & 0xff;
15377       val >>= 8;
15378       --size;
15379     }
15380 }
15381
15382 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
15383
15384 static HOST_WIDE_INT
15385 extract_int (const unsigned char *src, unsigned int size)
15386 {
15387   HOST_WIDE_INT val = 0;
15388
15389   src += size;
15390   while (size != 0)
15391     {
15392       val <<= 8;
15393       val |= *--src & 0xff;
15394       --size;
15395     }
15396   return val;
15397 }
15398
15399 /* Writes wide_int values to dw_vec_const array.  */
15400
15401 static void
15402 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
15403 {
15404   int i;
15405
15406   if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
15407     {
15408       insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
15409       return;
15410     }
15411
15412   /* We'd have to extend this code to support odd sizes.  */
15413   gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
15414
15415   int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
15416
15417   if (WORDS_BIG_ENDIAN)
15418     for (i = n - 1; i >= 0; i--)
15419       {
15420         insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15421         dest += sizeof (HOST_WIDE_INT);
15422       }
15423   else
15424     for (i = 0; i < n; i++)
15425       {
15426         insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15427         dest += sizeof (HOST_WIDE_INT);
15428       }
15429 }
15430
15431 /* Writes floating point values to dw_vec_const array.  */
15432
15433 static void
15434 insert_float (const_rtx rtl, unsigned char *array)
15435 {
15436   REAL_VALUE_TYPE rv;
15437   long val[4];
15438   int i;
15439
15440   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
15441   real_to_target (val, &rv, GET_MODE (rtl));
15442
15443   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
15444   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
15445     {
15446       insert_int (val[i], 4, array);
15447       array += 4;
15448     }
15449 }
15450
15451 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
15452    does not have a "location" either in memory or in a register.  These
15453    things can arise in GNU C when a constant is passed as an actual parameter
15454    to an inlined function.  They can also arise in C++ where declared
15455    constants do not necessarily get memory "homes".  */
15456
15457 static bool
15458 add_const_value_attribute (dw_die_ref die, rtx rtl)
15459 {
15460   switch (GET_CODE (rtl))
15461     {
15462     case CONST_INT:
15463       {
15464         HOST_WIDE_INT val = INTVAL (rtl);
15465
15466         if (val < 0)
15467           add_AT_int (die, DW_AT_const_value, val);
15468         else
15469           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
15470       }
15471       return true;
15472
15473     case CONST_WIDE_INT:
15474       add_AT_wide (die, DW_AT_const_value,
15475                    std::make_pair (rtl, GET_MODE (rtl)));
15476       return true;
15477
15478     case CONST_DOUBLE:
15479       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
15480          floating-point constant.  A CONST_DOUBLE is used whenever the
15481          constant requires more than one word in order to be adequately
15482          represented.  */
15483       {
15484         machine_mode mode = GET_MODE (rtl);
15485
15486         if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
15487           add_AT_double (die, DW_AT_const_value,
15488                          CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
15489         else
15490           {
15491             unsigned int length = GET_MODE_SIZE (mode);
15492             unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15493
15494             insert_float (rtl, array);
15495             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
15496           }
15497       }
15498       return true;
15499
15500     case CONST_VECTOR:
15501       {
15502         machine_mode mode = GET_MODE (rtl);
15503         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
15504         unsigned int length = CONST_VECTOR_NUNITS (rtl);
15505         unsigned char *array
15506           = ggc_vec_alloc<unsigned char> (length * elt_size);
15507         unsigned int i;
15508         unsigned char *p;
15509         machine_mode imode = GET_MODE_INNER (mode);
15510
15511         switch (GET_MODE_CLASS (mode))
15512           {
15513           case MODE_VECTOR_INT:
15514             for (i = 0, p = array; i < length; i++, p += elt_size)
15515               {
15516                 rtx elt = CONST_VECTOR_ELT (rtl, i);
15517                 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
15518               }
15519             break;
15520
15521           case MODE_VECTOR_FLOAT:
15522             for (i = 0, p = array; i < length; i++, p += elt_size)
15523               {
15524                 rtx elt = CONST_VECTOR_ELT (rtl, i);
15525                 insert_float (elt, p);
15526               }
15527             break;
15528
15529           default:
15530             gcc_unreachable ();
15531           }
15532
15533         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
15534       }
15535       return true;
15536
15537     case CONST_STRING:
15538       if (dwarf_version >= 4 || !dwarf_strict)
15539         {
15540           dw_loc_descr_ref loc_result;
15541           resolve_one_addr (&rtl);
15542         rtl_addr:
15543           loc_result = new_addr_loc_descr (rtl, dtprel_false);
15544           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15545           add_AT_loc (die, DW_AT_location, loc_result);
15546           vec_safe_push (used_rtx_array, rtl);
15547           return true;
15548         }
15549       return false;
15550
15551     case CONST:
15552       if (CONSTANT_P (XEXP (rtl, 0)))
15553         return add_const_value_attribute (die, XEXP (rtl, 0));
15554       /* FALLTHROUGH */
15555     case SYMBOL_REF:
15556       if (!const_ok_for_output (rtl))
15557         return false;
15558     case LABEL_REF:
15559       if (dwarf_version >= 4 || !dwarf_strict)
15560         goto rtl_addr;
15561       return false;
15562
15563     case PLUS:
15564       /* In cases where an inlined instance of an inline function is passed
15565          the address of an `auto' variable (which is local to the caller) we
15566          can get a situation where the DECL_RTL of the artificial local
15567          variable (for the inlining) which acts as a stand-in for the
15568          corresponding formal parameter (of the inline function) will look
15569          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
15570          exactly a compile-time constant expression, but it isn't the address
15571          of the (artificial) local variable either.  Rather, it represents the
15572          *value* which the artificial local variable always has during its
15573          lifetime.  We currently have no way to represent such quasi-constant
15574          values in Dwarf, so for now we just punt and generate nothing.  */
15575       return false;
15576
15577     case HIGH:
15578     case CONST_FIXED:
15579       return false;
15580
15581     case MEM:
15582       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
15583           && MEM_READONLY_P (rtl)
15584           && GET_MODE (rtl) == BLKmode)
15585         {
15586           add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
15587           return true;
15588         }
15589       return false;
15590
15591     default:
15592       /* No other kinds of rtx should be possible here.  */
15593       gcc_unreachable ();
15594     }
15595   return false;
15596 }
15597
15598 /* Determine whether the evaluation of EXPR references any variables
15599    or functions which aren't otherwise used (and therefore may not be
15600    output).  */
15601 static tree
15602 reference_to_unused (tree * tp, int * walk_subtrees,
15603                      void * data ATTRIBUTE_UNUSED)
15604 {
15605   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15606     *walk_subtrees = 0;
15607
15608   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15609       && ! TREE_ASM_WRITTEN (*tp))
15610     return *tp;
15611   /* ???  The C++ FE emits debug information for using decls, so
15612      putting gcc_unreachable here falls over.  See PR31899.  For now
15613      be conservative.  */
15614   else if (!symtab->global_info_ready
15615            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15616     return *tp;
15617   else if (TREE_CODE (*tp) == VAR_DECL)
15618     {
15619       varpool_node *node = varpool_node::get (*tp);
15620       if (!node || !node->definition)
15621         return *tp;
15622     }
15623   else if (TREE_CODE (*tp) == FUNCTION_DECL
15624            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15625     {
15626       /* The call graph machinery must have finished analyzing,
15627          optimizing and gimplifying the CU by now.
15628          So if *TP has no call graph node associated
15629          to it, it means *TP will not be emitted.  */
15630       if (!cgraph_node::get (*tp))
15631         return *tp;
15632     }
15633   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15634     return *tp;
15635
15636   return NULL_TREE;
15637 }
15638
15639 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15640    for use in a later add_const_value_attribute call.  */
15641
15642 static rtx
15643 rtl_for_decl_init (tree init, tree type)
15644 {
15645   rtx rtl = NULL_RTX;
15646
15647   STRIP_NOPS (init);
15648
15649   /* If a variable is initialized with a string constant without embedded
15650      zeros, build CONST_STRING.  */
15651   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15652     {
15653       tree enttype = TREE_TYPE (type);
15654       tree domain = TYPE_DOMAIN (type);
15655       machine_mode mode = TYPE_MODE (enttype);
15656
15657       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15658           && domain
15659           && integer_zerop (TYPE_MIN_VALUE (domain))
15660           && compare_tree_int (TYPE_MAX_VALUE (domain),
15661                                TREE_STRING_LENGTH (init) - 1) == 0
15662           && ((size_t) TREE_STRING_LENGTH (init)
15663               == strlen (TREE_STRING_POINTER (init)) + 1))
15664         {
15665           rtl = gen_rtx_CONST_STRING (VOIDmode,
15666                                       ggc_strdup (TREE_STRING_POINTER (init)));
15667           rtl = gen_rtx_MEM (BLKmode, rtl);
15668           MEM_READONLY_P (rtl) = 1;
15669         }
15670     }
15671   /* Other aggregates, and complex values, could be represented using
15672      CONCAT: FIXME!  */
15673   else if (AGGREGATE_TYPE_P (type)
15674            || (TREE_CODE (init) == VIEW_CONVERT_EXPR
15675                && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
15676            || TREE_CODE (type) == COMPLEX_TYPE)
15677     ;
15678   /* Vectors only work if their mode is supported by the target.
15679      FIXME: generic vectors ought to work too.  */
15680   else if (TREE_CODE (type) == VECTOR_TYPE
15681            && !VECTOR_MODE_P (TYPE_MODE (type)))
15682     ;
15683   /* If the initializer is something that we know will expand into an
15684      immediate RTL constant, expand it now.  We must be careful not to
15685      reference variables which won't be output.  */
15686   else if (initializer_constant_valid_p (init, type)
15687            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15688     {
15689       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15690          possible.  */
15691       if (TREE_CODE (type) == VECTOR_TYPE)
15692         switch (TREE_CODE (init))
15693           {
15694           case VECTOR_CST:
15695             break;
15696           case CONSTRUCTOR:
15697             if (TREE_CONSTANT (init))
15698               {
15699                 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15700                 bool constant_p = true;
15701                 tree value;
15702                 unsigned HOST_WIDE_INT ix;
15703
15704                 /* Even when ctor is constant, it might contain non-*_CST
15705                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15706                    belong into VECTOR_CST nodes.  */
15707                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15708                   if (!CONSTANT_CLASS_P (value))
15709                     {
15710                       constant_p = false;
15711                       break;
15712                     }
15713
15714                 if (constant_p)
15715                   {
15716                     init = build_vector_from_ctor (type, elts);
15717                     break;
15718                   }
15719               }
15720             /* FALLTHRU */
15721
15722           default:
15723             return NULL;
15724           }
15725
15726       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15727
15728       /* If expand_expr returns a MEM, it wasn't immediate.  */
15729       gcc_assert (!rtl || !MEM_P (rtl));
15730     }
15731
15732   return rtl;
15733 }
15734
15735 /* Generate RTL for the variable DECL to represent its location.  */
15736
15737 static rtx
15738 rtl_for_decl_location (tree decl)
15739 {
15740   rtx rtl;
15741
15742   /* Here we have to decide where we are going to say the parameter "lives"
15743      (as far as the debugger is concerned).  We only have a couple of
15744      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15745
15746      DECL_RTL normally indicates where the parameter lives during most of the
15747      activation of the function.  If optimization is enabled however, this
15748      could be either NULL or else a pseudo-reg.  Both of those cases indicate
15749      that the parameter doesn't really live anywhere (as far as the code
15750      generation parts of GCC are concerned) during most of the function's
15751      activation.  That will happen (for example) if the parameter is never
15752      referenced within the function.
15753
15754      We could just generate a location descriptor here for all non-NULL
15755      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15756      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15757      where DECL_RTL is NULL or is a pseudo-reg.
15758
15759      Note however that we can only get away with using DECL_INCOMING_RTL as
15760      a backup substitute for DECL_RTL in certain limited cases.  In cases
15761      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15762      we can be sure that the parameter was passed using the same type as it is
15763      declared to have within the function, and that its DECL_INCOMING_RTL
15764      points us to a place where a value of that type is passed.
15765
15766      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15767      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15768      because in these cases DECL_INCOMING_RTL points us to a value of some
15769      type which is *different* from the type of the parameter itself.  Thus,
15770      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15771      such cases, the debugger would end up (for example) trying to fetch a
15772      `float' from a place which actually contains the first part of a
15773      `double'.  That would lead to really incorrect and confusing
15774      output at debug-time.
15775
15776      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15777      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
15778      are a couple of exceptions however.  On little-endian machines we can
15779      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15780      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15781      an integral type that is smaller than TREE_TYPE (decl). These cases arise
15782      when (on a little-endian machine) a non-prototyped function has a
15783      parameter declared to be of type `short' or `char'.  In such cases,
15784      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15785      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15786      passed `int' value.  If the debugger then uses that address to fetch
15787      a `short' or a `char' (on a little-endian machine) the result will be
15788      the correct data, so we allow for such exceptional cases below.
15789
15790      Note that our goal here is to describe the place where the given formal
15791      parameter lives during most of the function's activation (i.e. between the
15792      end of the prologue and the start of the epilogue).  We'll do that as best
15793      as we can. Note however that if the given formal parameter is modified
15794      sometime during the execution of the function, then a stack backtrace (at
15795      debug-time) will show the function as having been called with the *new*
15796      value rather than the value which was originally passed in.  This happens
15797      rarely enough that it is not a major problem, but it *is* a problem, and
15798      I'd like to fix it.
15799
15800      A future version of dwarf2out.c may generate two additional attributes for
15801      any given DW_TAG_formal_parameter DIE which will describe the "passed
15802      type" and the "passed location" for the given formal parameter in addition
15803      to the attributes we now generate to indicate the "declared type" and the
15804      "active location" for each parameter.  This additional set of attributes
15805      could be used by debuggers for stack backtraces. Separately, note that
15806      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15807      This happens (for example) for inlined-instances of inline function formal
15808      parameters which are never referenced.  This really shouldn't be
15809      happening.  All PARM_DECL nodes should get valid non-NULL
15810      DECL_INCOMING_RTL values.  FIXME.  */
15811
15812   /* Use DECL_RTL as the "location" unless we find something better.  */
15813   rtl = DECL_RTL_IF_SET (decl);
15814
15815   /* When generating abstract instances, ignore everything except
15816      constants, symbols living in memory, and symbols living in
15817      fixed registers.  */
15818   if (! reload_completed)
15819     {
15820       if (rtl
15821           && (CONSTANT_P (rtl)
15822               || (MEM_P (rtl)
15823                   && CONSTANT_P (XEXP (rtl, 0)))
15824               || (REG_P (rtl)
15825                   && TREE_CODE (decl) == VAR_DECL
15826                   && TREE_STATIC (decl))))
15827         {
15828           rtl = targetm.delegitimize_address (rtl);
15829           return rtl;
15830         }
15831       rtl = NULL_RTX;
15832     }
15833   else if (TREE_CODE (decl) == PARM_DECL)
15834     {
15835       if (rtl == NULL_RTX
15836           || is_pseudo_reg (rtl)
15837           || (MEM_P (rtl)
15838               && is_pseudo_reg (XEXP (rtl, 0))
15839               && DECL_INCOMING_RTL (decl)
15840               && MEM_P (DECL_INCOMING_RTL (decl))
15841               && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15842         {
15843           tree declared_type = TREE_TYPE (decl);
15844           tree passed_type = DECL_ARG_TYPE (decl);
15845           machine_mode dmode = TYPE_MODE (declared_type);
15846           machine_mode pmode = TYPE_MODE (passed_type);
15847
15848           /* This decl represents a formal parameter which was optimized out.
15849              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15850              all cases where (rtl == NULL_RTX) just below.  */
15851           if (dmode == pmode)
15852             rtl = DECL_INCOMING_RTL (decl);
15853           else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15854                    && SCALAR_INT_MODE_P (dmode)
15855                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15856                    && DECL_INCOMING_RTL (decl))
15857             {
15858               rtx inc = DECL_INCOMING_RTL (decl);
15859               if (REG_P (inc))
15860                 rtl = inc;
15861               else if (MEM_P (inc))
15862                 {
15863                   if (BYTES_BIG_ENDIAN)
15864                     rtl = adjust_address_nv (inc, dmode,
15865                                              GET_MODE_SIZE (pmode)
15866                                              - GET_MODE_SIZE (dmode));
15867                   else
15868                     rtl = inc;
15869                 }
15870             }
15871         }
15872
15873       /* If the parm was passed in registers, but lives on the stack, then
15874          make a big endian correction if the mode of the type of the
15875          parameter is not the same as the mode of the rtl.  */
15876       /* ??? This is the same series of checks that are made in dbxout.c before
15877          we reach the big endian correction code there.  It isn't clear if all
15878          of these checks are necessary here, but keeping them all is the safe
15879          thing to do.  */
15880       else if (MEM_P (rtl)
15881                && XEXP (rtl, 0) != const0_rtx
15882                && ! CONSTANT_P (XEXP (rtl, 0))
15883                /* Not passed in memory.  */
15884                && !MEM_P (DECL_INCOMING_RTL (decl))
15885                /* Not passed by invisible reference.  */
15886                && (!REG_P (XEXP (rtl, 0))
15887                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15888                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15889 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15890                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15891 #endif
15892                      )
15893                /* Big endian correction check.  */
15894                && BYTES_BIG_ENDIAN
15895                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15896                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15897                    < UNITS_PER_WORD))
15898         {
15899           machine_mode addr_mode = get_address_mode (rtl);
15900           int offset = (UNITS_PER_WORD
15901                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15902
15903           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15904                              plus_constant (addr_mode, XEXP (rtl, 0), offset));
15905         }
15906     }
15907   else if (TREE_CODE (decl) == VAR_DECL
15908            && rtl
15909            && MEM_P (rtl)
15910            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15911            && BYTES_BIG_ENDIAN)
15912     {
15913       machine_mode addr_mode = get_address_mode (rtl);
15914       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15915       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15916
15917       /* If a variable is declared "register" yet is smaller than
15918          a register, then if we store the variable to memory, it
15919          looks like we're storing a register-sized value, when in
15920          fact we are not.  We need to adjust the offset of the
15921          storage location to reflect the actual value's bytes,
15922          else gdb will not be able to display it.  */
15923       if (rsize > dsize)
15924         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15925                            plus_constant (addr_mode, XEXP (rtl, 0),
15926                                           rsize - dsize));
15927     }
15928
15929   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15930      and will have been substituted directly into all expressions that use it.
15931      C does not have such a concept, but C++ and other languages do.  */
15932   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15933     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15934
15935   if (rtl)
15936     rtl = targetm.delegitimize_address (rtl);
15937
15938   /* If we don't look past the constant pool, we risk emitting a
15939      reference to a constant pool entry that isn't referenced from
15940      code, and thus is not emitted.  */
15941   if (rtl)
15942     rtl = avoid_constant_pool_reference (rtl);
15943
15944   /* Try harder to get a rtl.  If this symbol ends up not being emitted
15945      in the current CU, resolve_addr will remove the expression referencing
15946      it.  */
15947   if (rtl == NULL_RTX
15948       && TREE_CODE (decl) == VAR_DECL
15949       && !DECL_EXTERNAL (decl)
15950       && TREE_STATIC (decl)
15951       && DECL_NAME (decl)
15952       && !DECL_HARD_REGISTER (decl)
15953       && DECL_MODE (decl) != VOIDmode)
15954     {
15955       rtl = make_decl_rtl_for_debug (decl);
15956       if (!MEM_P (rtl)
15957           || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
15958           || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
15959         rtl = NULL_RTX;
15960     }
15961
15962   return rtl;
15963 }
15964
15965 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
15966    returned.  If so, the decl for the COMMON block is returned, and the
15967    value is the offset into the common block for the symbol.  */
15968
15969 static tree
15970 fortran_common (tree decl, HOST_WIDE_INT *value)
15971 {
15972   tree val_expr, cvar;
15973   machine_mode mode;
15974   HOST_WIDE_INT bitsize, bitpos;
15975   tree offset;
15976   int unsignedp, volatilep = 0;
15977
15978   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
15979      it does not have a value (the offset into the common area), or if it
15980      is thread local (as opposed to global) then it isn't common, and shouldn't
15981      be handled as such.  */
15982   if (TREE_CODE (decl) != VAR_DECL
15983       || !TREE_STATIC (decl)
15984       || !DECL_HAS_VALUE_EXPR_P (decl)
15985       || !is_fortran ())
15986     return NULL_TREE;
15987
15988   val_expr = DECL_VALUE_EXPR (decl);
15989   if (TREE_CODE (val_expr) != COMPONENT_REF)
15990     return NULL_TREE;
15991
15992   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
15993                               &mode, &unsignedp, &volatilep, true);
15994
15995   if (cvar == NULL_TREE
15996       || TREE_CODE (cvar) != VAR_DECL
15997       || DECL_ARTIFICIAL (cvar)
15998       || !TREE_PUBLIC (cvar))
15999     return NULL_TREE;
16000
16001   *value = 0;
16002   if (offset != NULL)
16003     {
16004       if (!tree_fits_shwi_p (offset))
16005         return NULL_TREE;
16006       *value = tree_to_shwi (offset);
16007     }
16008   if (bitpos != 0)
16009     *value += bitpos / BITS_PER_UNIT;
16010
16011   return cvar;
16012 }
16013
16014 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
16015    data attribute for a variable or a parameter.  We generate the
16016    DW_AT_const_value attribute only in those cases where the given variable
16017    or parameter does not have a true "location" either in memory or in a
16018    register.  This can happen (for example) when a constant is passed as an
16019    actual argument in a call to an inline function.  (It's possible that
16020    these things can crop up in other ways also.)  Note that one type of
16021    constant value which can be passed into an inlined function is a constant
16022    pointer.  This can happen for example if an actual argument in an inlined
16023    function call evaluates to a compile-time constant address.
16024
16025    CACHE_P is true if it is worth caching the location list for DECL,
16026    so that future calls can reuse it rather than regenerate it from scratch.
16027    This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
16028    since we will need to refer to them each time the function is inlined.  */
16029
16030 static bool
16031 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
16032                                        enum dwarf_attribute attr)
16033 {
16034   rtx rtl;
16035   dw_loc_list_ref list;
16036   var_loc_list *loc_list;
16037   cached_dw_loc_list *cache;
16038
16039   if (TREE_CODE (decl) == ERROR_MARK)
16040     return false;
16041
16042   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
16043               || TREE_CODE (decl) == RESULT_DECL);
16044
16045   /* Try to get some constant RTL for this decl, and use that as the value of
16046      the location.  */
16047
16048   rtl = rtl_for_decl_location (decl);
16049   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16050       && add_const_value_attribute (die, rtl))
16051     return true;
16052
16053   /* See if we have single element location list that is equivalent to
16054      a constant value.  That way we are better to use add_const_value_attribute
16055      rather than expanding constant value equivalent.  */
16056   loc_list = lookup_decl_loc (decl);
16057   if (loc_list
16058       && loc_list->first
16059       && loc_list->first->next == NULL
16060       && NOTE_P (loc_list->first->loc)
16061       && NOTE_VAR_LOCATION (loc_list->first->loc)
16062       && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
16063     {
16064       struct var_loc_node *node;
16065
16066       node = loc_list->first;
16067       rtl = NOTE_VAR_LOCATION_LOC (node->loc);
16068       if (GET_CODE (rtl) == EXPR_LIST)
16069         rtl = XEXP (rtl, 0);
16070       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16071           && add_const_value_attribute (die, rtl))
16072          return true;
16073     }
16074   /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
16075      list several times.  See if we've already cached the contents.  */
16076   list = NULL;
16077   if (loc_list == NULL || cached_dw_loc_list_table == NULL)
16078     cache_p = false;
16079   if (cache_p)
16080     {
16081       cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
16082       if (cache)
16083         list = cache->loc_list;
16084     }
16085   if (list == NULL)
16086     {
16087       list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
16088                                  NULL);
16089       /* It is usually worth caching this result if the decl is from
16090          BLOCK_NONLOCALIZED_VARS and if the list has at least two elements.  */
16091       if (cache_p && list && list->dw_loc_next)
16092         {
16093           cached_dw_loc_list **slot
16094             = cached_dw_loc_list_table->find_slot_with_hash (decl,
16095                                                              DECL_UID (decl),
16096                                                              INSERT);
16097           cache = ggc_cleared_alloc<cached_dw_loc_list> ();
16098           cache->decl_id = DECL_UID (decl);
16099           cache->loc_list = list;
16100           *slot = cache;
16101         }
16102     }
16103   if (list)
16104     {
16105       add_AT_location_description (die, attr, list);
16106       return true;
16107     }
16108   /* None of that worked, so it must not really have a location;
16109      try adding a constant value attribute from the DECL_INITIAL.  */
16110   return tree_add_const_value_attribute_for_decl (die, decl);
16111 }
16112
16113 /* Add VARIABLE and DIE into deferred locations list.  */
16114
16115 static void
16116 defer_location (tree variable, dw_die_ref die)
16117 {
16118   deferred_locations entry;
16119   entry.variable = variable;
16120   entry.die = die;
16121   vec_safe_push (deferred_locations_list, entry);
16122 }
16123
16124 /* Helper function for tree_add_const_value_attribute.  Natively encode
16125    initializer INIT into an array.  Return true if successful.  */
16126
16127 static bool
16128 native_encode_initializer (tree init, unsigned char *array, int size)
16129 {
16130   tree type;
16131
16132   if (init == NULL_TREE)
16133     return false;
16134
16135   STRIP_NOPS (init);
16136   switch (TREE_CODE (init))
16137     {
16138     case STRING_CST:
16139       type = TREE_TYPE (init);
16140       if (TREE_CODE (type) == ARRAY_TYPE)
16141         {
16142           tree enttype = TREE_TYPE (type);
16143           machine_mode mode = TYPE_MODE (enttype);
16144
16145           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
16146             return false;
16147           if (int_size_in_bytes (type) != size)
16148             return false;
16149           if (size > TREE_STRING_LENGTH (init))
16150             {
16151               memcpy (array, TREE_STRING_POINTER (init),
16152                       TREE_STRING_LENGTH (init));
16153               memset (array + TREE_STRING_LENGTH (init),
16154                       '\0', size - TREE_STRING_LENGTH (init));
16155             }
16156           else
16157             memcpy (array, TREE_STRING_POINTER (init), size);
16158           return true;
16159         }
16160       return false;
16161     case CONSTRUCTOR:
16162       type = TREE_TYPE (init);
16163       if (int_size_in_bytes (type) != size)
16164         return false;
16165       if (TREE_CODE (type) == ARRAY_TYPE)
16166         {
16167           HOST_WIDE_INT min_index;
16168           unsigned HOST_WIDE_INT cnt;
16169           int curpos = 0, fieldsize;
16170           constructor_elt *ce;
16171
16172           if (TYPE_DOMAIN (type) == NULL_TREE
16173               || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
16174             return false;
16175
16176           fieldsize = int_size_in_bytes (TREE_TYPE (type));
16177           if (fieldsize <= 0)
16178             return false;
16179
16180           min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
16181           memset (array, '\0', size);
16182           FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16183             {
16184               tree val = ce->value;
16185               tree index = ce->index;
16186               int pos = curpos;
16187               if (index && TREE_CODE (index) == RANGE_EXPR)
16188                 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
16189                       * fieldsize;
16190               else if (index)
16191                 pos = (tree_to_shwi (index) - min_index) * fieldsize;
16192
16193               if (val)
16194                 {
16195                   STRIP_NOPS (val);
16196                   if (!native_encode_initializer (val, array + pos, fieldsize))
16197                     return false;
16198                 }
16199               curpos = pos + fieldsize;
16200               if (index && TREE_CODE (index) == RANGE_EXPR)
16201                 {
16202                   int count = tree_to_shwi (TREE_OPERAND (index, 1))
16203                               - tree_to_shwi (TREE_OPERAND (index, 0));
16204                   while (count-- > 0)
16205                     {
16206                       if (val)
16207                         memcpy (array + curpos, array + pos, fieldsize);
16208                       curpos += fieldsize;
16209                     }
16210                 }
16211               gcc_assert (curpos <= size);
16212             }
16213           return true;
16214         }
16215       else if (TREE_CODE (type) == RECORD_TYPE
16216                || TREE_CODE (type) == UNION_TYPE)
16217         {
16218           tree field = NULL_TREE;
16219           unsigned HOST_WIDE_INT cnt;
16220           constructor_elt *ce;
16221
16222           if (int_size_in_bytes (type) != size)
16223             return false;
16224
16225           if (TREE_CODE (type) == RECORD_TYPE)
16226             field = TYPE_FIELDS (type);
16227
16228           FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16229             {
16230               tree val = ce->value;
16231               int pos, fieldsize;
16232
16233               if (ce->index != 0)
16234                 field = ce->index;
16235
16236               if (val)
16237                 STRIP_NOPS (val);
16238
16239               if (field == NULL_TREE || DECL_BIT_FIELD (field))
16240                 return false;
16241
16242               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
16243                   && TYPE_DOMAIN (TREE_TYPE (field))
16244                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
16245                 return false;
16246               else if (DECL_SIZE_UNIT (field) == NULL_TREE
16247                        || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
16248                 return false;
16249               fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
16250               pos = int_byte_position (field);
16251               gcc_assert (pos + fieldsize <= size);
16252               if (val
16253                   && !native_encode_initializer (val, array + pos, fieldsize))
16254                 return false;
16255             }
16256           return true;
16257         }
16258       return false;
16259     case VIEW_CONVERT_EXPR:
16260     case NON_LVALUE_EXPR:
16261       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
16262     default:
16263       return native_encode_expr (init, array, size) == size;
16264     }
16265 }
16266
16267 /* Attach a DW_AT_const_value attribute to DIE. The value of the
16268    attribute is the const value T.  */
16269
16270 static bool
16271 tree_add_const_value_attribute (dw_die_ref die, tree t)
16272 {
16273   tree init;
16274   tree type = TREE_TYPE (t);
16275   rtx rtl;
16276
16277   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
16278     return false;
16279
16280   init = t;
16281   gcc_assert (!DECL_P (init));
16282
16283   rtl = rtl_for_decl_init (init, type);
16284   if (rtl)
16285     return add_const_value_attribute (die, rtl);
16286   /* If the host and target are sane, try harder.  */
16287   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
16288            && initializer_constant_valid_p (init, type))
16289     {
16290       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
16291       if (size > 0 && (int) size == size)
16292         {
16293           unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
16294
16295           if (native_encode_initializer (init, array, size))
16296             {
16297               add_AT_vec (die, DW_AT_const_value, size, 1, array);
16298               return true;
16299             }
16300           ggc_free (array);
16301         }
16302     }
16303   return false;
16304 }
16305
16306 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
16307    attribute is the const value of T, where T is an integral constant
16308    variable with static storage duration
16309    (so it can't be a PARM_DECL or a RESULT_DECL).  */
16310
16311 static bool
16312 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
16313 {
16314
16315   if (!decl
16316       || (TREE_CODE (decl) != VAR_DECL
16317           && TREE_CODE (decl) != CONST_DECL)
16318       || (TREE_CODE (decl) == VAR_DECL
16319           && !TREE_STATIC (decl)))
16320     return false;
16321
16322     if (TREE_READONLY (decl)
16323         && ! TREE_THIS_VOLATILE (decl)
16324         && DECL_INITIAL (decl))
16325       /* OK */;
16326     else
16327       return false;
16328
16329   /* Don't add DW_AT_const_value if abstract origin already has one.  */
16330   if (get_AT (var_die, DW_AT_const_value))
16331     return false;
16332
16333   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
16334 }
16335
16336 /* Convert the CFI instructions for the current function into a
16337    location list.  This is used for DW_AT_frame_base when we targeting
16338    a dwarf2 consumer that does not support the dwarf3
16339    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
16340    expressions.  */
16341
16342 static dw_loc_list_ref
16343 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
16344 {
16345   int ix;
16346   dw_fde_ref fde;
16347   dw_loc_list_ref list, *list_tail;
16348   dw_cfi_ref cfi;
16349   dw_cfa_location last_cfa, next_cfa;
16350   const char *start_label, *last_label, *section;
16351   dw_cfa_location remember;
16352
16353   fde = cfun->fde;
16354   gcc_assert (fde != NULL);
16355
16356   section = secname_for_decl (current_function_decl);
16357   list_tail = &list;
16358   list = NULL;
16359
16360   memset (&next_cfa, 0, sizeof (next_cfa));
16361   next_cfa.reg = INVALID_REGNUM;
16362   remember = next_cfa;
16363
16364   start_label = fde->dw_fde_begin;
16365
16366   /* ??? Bald assumption that the CIE opcode list does not contain
16367      advance opcodes.  */
16368   FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
16369     lookup_cfa_1 (cfi, &next_cfa, &remember);
16370
16371   last_cfa = next_cfa;
16372   last_label = start_label;
16373
16374   if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
16375     {
16376       /* If the first partition contained no CFI adjustments, the
16377          CIE opcodes apply to the whole first partition.  */
16378       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16379                                  fde->dw_fde_begin, fde->dw_fde_end, section);
16380       list_tail =&(*list_tail)->dw_loc_next;
16381       start_label = last_label = fde->dw_fde_second_begin;
16382     }
16383
16384   FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
16385     {
16386       switch (cfi->dw_cfi_opc)
16387         {
16388         case DW_CFA_set_loc:
16389         case DW_CFA_advance_loc1:
16390         case DW_CFA_advance_loc2:
16391         case DW_CFA_advance_loc4:
16392           if (!cfa_equal_p (&last_cfa, &next_cfa))
16393             {
16394               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16395                                          start_label, last_label, section);
16396
16397               list_tail = &(*list_tail)->dw_loc_next;
16398               last_cfa = next_cfa;
16399               start_label = last_label;
16400             }
16401           last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
16402           break;
16403
16404         case DW_CFA_advance_loc:
16405           /* The encoding is complex enough that we should never emit this.  */
16406           gcc_unreachable ();
16407
16408         default:
16409           lookup_cfa_1 (cfi, &next_cfa, &remember);
16410           break;
16411         }
16412       if (ix + 1 == fde->dw_fde_switch_cfi_index)
16413         {
16414           if (!cfa_equal_p (&last_cfa, &next_cfa))
16415             {
16416               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16417                                          start_label, last_label, section);
16418
16419               list_tail = &(*list_tail)->dw_loc_next;
16420               last_cfa = next_cfa;
16421               start_label = last_label;
16422             }
16423           *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16424                                      start_label, fde->dw_fde_end, section);
16425           list_tail = &(*list_tail)->dw_loc_next;
16426           start_label = last_label = fde->dw_fde_second_begin;
16427         }
16428     }
16429
16430   if (!cfa_equal_p (&last_cfa, &next_cfa))
16431     {
16432       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16433                                  start_label, last_label, section);
16434       list_tail = &(*list_tail)->dw_loc_next;
16435       start_label = last_label;
16436     }
16437
16438   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
16439                              start_label,
16440                              fde->dw_fde_second_begin
16441                              ? fde->dw_fde_second_end : fde->dw_fde_end,
16442                              section);
16443
16444   if (list && list->dw_loc_next)
16445     gen_llsym (list);
16446
16447   return list;
16448 }
16449
16450 /* Compute a displacement from the "steady-state frame pointer" to the
16451    frame base (often the same as the CFA), and store it in
16452    frame_pointer_fb_offset.  OFFSET is added to the displacement
16453    before the latter is negated.  */
16454
16455 static void
16456 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
16457 {
16458   rtx reg, elim;
16459
16460 #ifdef FRAME_POINTER_CFA_OFFSET
16461   reg = frame_pointer_rtx;
16462   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
16463 #else
16464   reg = arg_pointer_rtx;
16465   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
16466 #endif
16467
16468   elim = (ira_use_lra_p
16469           ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
16470           : eliminate_regs (reg, VOIDmode, NULL_RTX));
16471   if (GET_CODE (elim) == PLUS)
16472     {
16473       offset += INTVAL (XEXP (elim, 1));
16474       elim = XEXP (elim, 0);
16475     }
16476
16477   frame_pointer_fb_offset = -offset;
16478
16479   /* ??? AVR doesn't set up valid eliminations when there is no stack frame
16480      in which to eliminate.  This is because it's stack pointer isn't 
16481      directly accessible as a register within the ISA.  To work around
16482      this, assume that while we cannot provide a proper value for
16483      frame_pointer_fb_offset, we won't need one either.  */
16484   frame_pointer_fb_offset_valid
16485     = ((SUPPORTS_STACK_ALIGNMENT
16486         && (elim == hard_frame_pointer_rtx
16487             || elim == stack_pointer_rtx))
16488        || elim == (frame_pointer_needed
16489                    ? hard_frame_pointer_rtx
16490                    : stack_pointer_rtx));
16491 }
16492
16493 /* Generate a DW_AT_name attribute given some string value to be included as
16494    the value of the attribute.  */
16495
16496 static void
16497 add_name_attribute (dw_die_ref die, const char *name_string)
16498 {
16499   if (name_string != NULL && *name_string != 0)
16500     {
16501       if (demangle_name_func)
16502         name_string = (*demangle_name_func) (name_string);
16503
16504       add_AT_string (die, DW_AT_name, name_string);
16505     }
16506 }
16507
16508 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
16509    DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
16510    of TYPE accordingly.
16511
16512    ??? This is a temporary measure until after we're able to generate
16513    regular DWARF for the complex Ada type system.  */
16514
16515 static void 
16516 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
16517                                      dw_die_ref context_die)
16518 {
16519   tree dtype;
16520   dw_die_ref dtype_die;
16521
16522   if (!lang_hooks.types.descriptive_type)
16523     return;
16524
16525   dtype = lang_hooks.types.descriptive_type (type);
16526   if (!dtype)
16527     return;
16528
16529   dtype_die = lookup_type_die (dtype);
16530   if (!dtype_die)
16531     {
16532       gen_type_die (dtype, context_die);
16533       dtype_die = lookup_type_die (dtype);
16534       gcc_assert (dtype_die);
16535     }
16536
16537   add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
16538 }
16539
16540 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir.  */
16541
16542 static const char *
16543 comp_dir_string (void)
16544 {
16545   const char *wd;
16546   char *wd1;
16547   static const char *cached_wd = NULL;
16548
16549   if (cached_wd != NULL)
16550     return cached_wd;
16551
16552   wd = get_src_pwd ();
16553   if (wd == NULL)
16554     return NULL;
16555
16556   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16557     {
16558       int wdlen;
16559
16560       wdlen = strlen (wd);
16561       wd1 = ggc_vec_alloc<char> (wdlen + 2);
16562       strcpy (wd1, wd);
16563       wd1 [wdlen] = DIR_SEPARATOR;
16564       wd1 [wdlen + 1] = 0;
16565       wd = wd1;
16566     }
16567
16568   cached_wd = remap_debug_filename (wd);
16569   return cached_wd;
16570 }
16571
16572 /* Generate a DW_AT_comp_dir attribute for DIE.  */
16573
16574 static void
16575 add_comp_dir_attribute (dw_die_ref die)
16576 {
16577   const char * wd = comp_dir_string ();
16578   if (wd != NULL)
16579     add_AT_string (die, DW_AT_comp_dir, wd);
16580 }
16581
16582 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
16583    pointer computation, ...), output a representation for that bound according
16584    to the accepted FORMS (see enum dw_scalar_form) and add it to DIE.  See
16585    loc_list_from_tree for the meaning of CONTEXT.  */
16586
16587 static void
16588 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
16589                  int forms, const struct loc_descr_context *context)
16590 {
16591   dw_die_ref ctx, decl_die;
16592   dw_loc_list_ref list;
16593
16594   bool strip_conversions = true;
16595
16596   while (strip_conversions)
16597     switch (TREE_CODE (value))
16598       {
16599       case ERROR_MARK:
16600       case SAVE_EXPR:
16601         return;
16602
16603       CASE_CONVERT:
16604       case VIEW_CONVERT_EXPR:
16605         value = TREE_OPERAND (value, 0);
16606         break;
16607
16608       default:
16609         strip_conversions = false;
16610         break;
16611       }
16612
16613   /* If possible and permitted, output the attribute as a constant.  */
16614   if ((forms & dw_scalar_form_constant) != 0
16615       && TREE_CODE (value) == INTEGER_CST)
16616     {
16617       unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
16618
16619       /* If HOST_WIDE_INT is big enough then represent the bound as
16620          a constant value.  We need to choose a form based on
16621          whether the type is signed or unsigned.  We cannot just
16622          call add_AT_unsigned if the value itself is positive
16623          (add_AT_unsigned might add the unsigned value encoded as
16624          DW_FORM_data[1248]).  Some DWARF consumers will lookup the
16625          bounds type and then sign extend any unsigned values found
16626          for signed types.  This is needed only for
16627          DW_AT_{lower,upper}_bound, since for most other attributes,
16628          consumers will treat DW_FORM_data[1248] as unsigned values,
16629          regardless of the underlying type.  */
16630       if (prec <= HOST_BITS_PER_WIDE_INT
16631           || tree_fits_uhwi_p (value))
16632         {
16633           if (TYPE_UNSIGNED (TREE_TYPE (value)))
16634             add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
16635           else
16636             add_AT_int (die, attr, TREE_INT_CST_LOW (value));
16637         }
16638       else
16639         /* Otherwise represent the bound as an unsigned value with
16640            the precision of its type.  The precision and signedness
16641            of the type will be necessary to re-interpret it
16642            unambiguously.  */
16643         add_AT_wide (die, attr, value);
16644       return;
16645     }
16646
16647   /* Otherwise, if it's possible and permitted too, output a reference to
16648      another DIE.  */
16649   if ((forms & dw_scalar_form_reference) != 0)
16650     {
16651       tree decl = NULL_TREE;
16652
16653       /* Some type attributes reference an outer type.  For instance, the upper
16654          bound of an array may reference an embedding record (this happens in
16655          Ada).  */
16656       if (TREE_CODE (value) == COMPONENT_REF
16657           && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
16658           && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
16659         decl = TREE_OPERAND (value, 1);
16660
16661       else if (TREE_CODE (value) == VAR_DECL
16662                || TREE_CODE (value) == PARM_DECL
16663                || TREE_CODE (value) == RESULT_DECL)
16664         decl = value;
16665
16666       if (decl != NULL_TREE)
16667         {
16668           dw_die_ref decl_die = lookup_decl_die (decl);
16669
16670           /* ??? Can this happen, or should the variable have been bound
16671              first?  Probably it can, since I imagine that we try to create
16672              the types of parameters in the order in which they exist in
16673              the list, and won't have created a forward reference to a
16674              later parameter.  */
16675           if (decl_die != NULL)
16676             {
16677               add_AT_die_ref (die, attr, decl_die);
16678               return;
16679             }
16680         }
16681     }
16682
16683   /* Last chance: try to create a stack operation procedure to evaluate the
16684      value.  Do nothing if even that is not possible or permitted.  */
16685   if ((forms & dw_scalar_form_exprloc) == 0)
16686     return;
16687
16688   list = loc_list_from_tree (value, 2, context);
16689   if (list == NULL || single_element_loc_list_p (list))
16690     {
16691       /* If this attribute is not a reference nor constant, it is
16692          a DWARF expression rather than location description.  For that
16693          loc_list_from_tree (value, 0, &context) is needed.  */
16694       dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
16695       if (list2 && single_element_loc_list_p (list2))
16696         {
16697           add_AT_loc (die, attr, list2->expr);
16698           return;
16699         }
16700     }
16701
16702   /* If that failed to give a single element location list, fall back to
16703      outputting this as a reference... still if permitted.  */
16704   if (list == NULL || (forms & dw_scalar_form_reference) == 0)
16705     return;
16706
16707   if (current_function_decl == 0)
16708     ctx = comp_unit_die ();
16709   else
16710     ctx = lookup_decl_die (current_function_decl);
16711
16712   decl_die = new_die (DW_TAG_variable, ctx, value);
16713   add_AT_flag (decl_die, DW_AT_artificial, 1);
16714   add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, ctx);
16715   add_AT_location_description (decl_die, DW_AT_location, list);
16716   add_AT_die_ref (die, attr, decl_die);
16717 }
16718
16719 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
16720    default.  */
16721
16722 static int
16723 lower_bound_default (void)
16724 {
16725   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16726     {
16727     case DW_LANG_C:
16728     case DW_LANG_C89:
16729     case DW_LANG_C99:
16730     case DW_LANG_C11:
16731     case DW_LANG_C_plus_plus:
16732     case DW_LANG_C_plus_plus_11:
16733     case DW_LANG_C_plus_plus_14:
16734     case DW_LANG_ObjC:
16735     case DW_LANG_ObjC_plus_plus:
16736     case DW_LANG_Java:
16737       return 0;
16738     case DW_LANG_Fortran77:
16739     case DW_LANG_Fortran90:
16740     case DW_LANG_Fortran95:
16741     case DW_LANG_Fortran03:
16742     case DW_LANG_Fortran08:
16743       return 1;
16744     case DW_LANG_UPC:
16745     case DW_LANG_D:
16746     case DW_LANG_Python:
16747       return dwarf_version >= 4 ? 0 : -1;
16748     case DW_LANG_Ada95:
16749     case DW_LANG_Ada83:
16750     case DW_LANG_Cobol74:
16751     case DW_LANG_Cobol85:
16752     case DW_LANG_Pascal83:
16753     case DW_LANG_Modula2:
16754     case DW_LANG_PLI:
16755       return dwarf_version >= 4 ? 1 : -1;
16756     default:
16757       return -1;
16758     }
16759 }
16760
16761 /* Given a tree node describing an array bound (either lower or upper) output
16762    a representation for that bound.  */
16763
16764 static void
16765 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
16766                 tree bound, const struct loc_descr_context *context)
16767 {
16768   int dflt;
16769
16770   while (1)
16771     switch (TREE_CODE (bound))
16772       {
16773       /* Strip all conversions.  */
16774       CASE_CONVERT:
16775       case VIEW_CONVERT_EXPR:
16776         bound = TREE_OPERAND (bound, 0);
16777         break;
16778
16779       /* All fixed-bounds are represented by INTEGER_CST nodes.  Lower bounds
16780          are even omitted when they are the default.  */
16781       case INTEGER_CST:
16782         /* If the value for this bound is the default one, we can even omit the
16783            attribute.  */
16784         if (bound_attr == DW_AT_lower_bound
16785             && tree_fits_shwi_p (bound)
16786             && (dflt = lower_bound_default ()) != -1
16787             && tree_to_shwi (bound) == dflt)
16788           return;
16789
16790         /* FALLTHRU */
16791
16792       default:
16793         add_scalar_info (subrange_die, bound_attr, bound,
16794                          dw_scalar_form_constant
16795                          | dw_scalar_form_exprloc
16796                          | dw_scalar_form_reference,
16797                          context);
16798         return;
16799       }
16800 }
16801
16802 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16803    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16804    Note that the block of subscript information for an array type also
16805    includes information about the element type of the given array type.  */
16806
16807 static void
16808 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16809 {
16810   unsigned dimension_number;
16811   tree lower, upper;
16812   dw_die_ref subrange_die;
16813
16814   for (dimension_number = 0;
16815        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16816        type = TREE_TYPE (type), dimension_number++)
16817     {
16818       tree domain = TYPE_DOMAIN (type);
16819
16820       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16821         break;
16822
16823       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16824          and (in GNU C only) variable bounds.  Handle all three forms
16825          here.  */
16826       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16827       if (domain)
16828         {
16829           /* We have an array type with specified bounds.  */
16830           lower = TYPE_MIN_VALUE (domain);
16831           upper = TYPE_MAX_VALUE (domain);
16832
16833           /* Define the index type.  */
16834           if (TREE_TYPE (domain))
16835             {
16836               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
16837                  TREE_TYPE field.  We can't emit debug info for this
16838                  because it is an unnamed integral type.  */
16839               if (TREE_CODE (domain) == INTEGER_TYPE
16840                   && TYPE_NAME (domain) == NULL_TREE
16841                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16842                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16843                 ;
16844               else
16845                 add_type_attribute (subrange_die, TREE_TYPE (domain),
16846                                     TYPE_UNQUALIFIED, type_die);
16847             }
16848
16849           /* ??? If upper is NULL, the array has unspecified length,
16850              but it does have a lower bound.  This happens with Fortran
16851                dimension arr(N:*)
16852              Since the debugger is definitely going to need to know N
16853              to produce useful results, go ahead and output the lower
16854              bound solo, and hope the debugger can cope.  */
16855
16856           add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
16857           if (upper)
16858             add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
16859         }
16860
16861       /* Otherwise we have an array type with an unspecified length.  The
16862          DWARF-2 spec does not say how to handle this; let's just leave out the
16863          bounds.  */
16864     }
16865 }
16866
16867 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size.  */
16868
16869 static void
16870 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16871 {
16872   dw_die_ref decl_die;
16873   HOST_WIDE_INT size;
16874
16875   switch (TREE_CODE (tree_node))
16876     {
16877     case ERROR_MARK:
16878       size = 0;
16879       break;
16880     case ENUMERAL_TYPE:
16881     case RECORD_TYPE:
16882     case UNION_TYPE:
16883     case QUAL_UNION_TYPE:
16884       if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
16885           && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
16886         {
16887           add_AT_die_ref (die, DW_AT_byte_size, decl_die);
16888           return;
16889         }
16890       size = int_size_in_bytes (tree_node);
16891       break;
16892     case FIELD_DECL:
16893       /* For a data member of a struct or union, the DW_AT_byte_size is
16894          generally given as the number of bytes normally allocated for an
16895          object of the *declared* type of the member itself.  This is true
16896          even for bit-fields.  */
16897       size = int_size_in_bytes (field_type (tree_node));
16898       break;
16899     default:
16900       gcc_unreachable ();
16901     }
16902
16903   /* Note that `size' might be -1 when we get to this point.  If it is, that
16904      indicates that the byte size of the entity in question is variable.  We
16905      have no good way of expressing this fact in Dwarf at the present time,
16906      when location description was not used by the caller code instead.  */
16907   if (size >= 0)
16908     add_AT_unsigned (die, DW_AT_byte_size, size);
16909 }
16910
16911 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16912    which specifies the distance in bits from the highest order bit of the
16913    "containing object" for the bit-field to the highest order bit of the
16914    bit-field itself.
16915
16916    For any given bit-field, the "containing object" is a hypothetical object
16917    (of some integral or enum type) within which the given bit-field lives.  The
16918    type of this hypothetical "containing object" is always the same as the
16919    declared type of the individual bit-field itself.  The determination of the
16920    exact location of the "containing object" for a bit-field is rather
16921    complicated.  It's handled by the `field_byte_offset' function (above).
16922
16923    Note that it is the size (in bytes) of the hypothetical "containing object"
16924    which will be given in the DW_AT_byte_size attribute for this bit-field.
16925    (See `byte_size_attribute' above).  */
16926
16927 static inline void
16928 add_bit_offset_attribute (dw_die_ref die, tree decl)
16929 {
16930   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16931   tree type = DECL_BIT_FIELD_TYPE (decl);
16932   HOST_WIDE_INT bitpos_int;
16933   HOST_WIDE_INT highest_order_object_bit_offset;
16934   HOST_WIDE_INT highest_order_field_bit_offset;
16935   HOST_WIDE_INT bit_offset;
16936
16937   /* Must be a field and a bit field.  */
16938   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16939
16940   /* We can't yet handle bit-fields whose offsets are variable, so if we
16941      encounter such things, just return without generating any attribute
16942      whatsoever.  Likewise for variable or too large size.  */
16943   if (! tree_fits_shwi_p (bit_position (decl))
16944       || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
16945     return;
16946
16947   bitpos_int = int_bit_position (decl);
16948
16949   /* Note that the bit offset is always the distance (in bits) from the
16950      highest-order bit of the "containing object" to the highest-order bit of
16951      the bit-field itself.  Since the "high-order end" of any object or field
16952      is different on big-endian and little-endian machines, the computation
16953      below must take account of these differences.  */
16954   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
16955   highest_order_field_bit_offset = bitpos_int;
16956
16957   if (! BYTES_BIG_ENDIAN)
16958     {
16959       highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
16960       highest_order_object_bit_offset += simple_type_size_in_bits (type);
16961     }
16962
16963   bit_offset
16964     = (! BYTES_BIG_ENDIAN
16965        ? highest_order_object_bit_offset - highest_order_field_bit_offset
16966        : highest_order_field_bit_offset - highest_order_object_bit_offset);
16967
16968   if (bit_offset < 0)
16969     add_AT_int (die, DW_AT_bit_offset, bit_offset);
16970   else
16971     add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
16972 }
16973
16974 /* For a FIELD_DECL node which represents a bit field, output an attribute
16975    which specifies the length in bits of the given field.  */
16976
16977 static inline void
16978 add_bit_size_attribute (dw_die_ref die, tree decl)
16979 {
16980   /* Must be a field and a bit field.  */
16981   gcc_assert (TREE_CODE (decl) == FIELD_DECL
16982               && DECL_BIT_FIELD_TYPE (decl));
16983
16984   if (tree_fits_uhwi_p (DECL_SIZE (decl)))
16985     add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
16986 }
16987
16988 /* If the compiled language is ANSI C, then add a 'prototyped'
16989    attribute, if arg types are given for the parameters of a function.  */
16990
16991 static inline void
16992 add_prototyped_attribute (dw_die_ref die, tree func_type)
16993 {
16994   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16995     {
16996     case DW_LANG_C:
16997     case DW_LANG_C89:
16998     case DW_LANG_C99:
16999     case DW_LANG_C11:
17000     case DW_LANG_ObjC:
17001       if (prototype_p (func_type))
17002         add_AT_flag (die, DW_AT_prototyped, 1);
17003       break;
17004     default:
17005       break;
17006     }
17007 }
17008
17009 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
17010    by looking in either the type declaration or object declaration
17011    equate table.  */
17012
17013 static inline dw_die_ref
17014 add_abstract_origin_attribute (dw_die_ref die, tree origin)
17015 {
17016   dw_die_ref origin_die = NULL;
17017
17018   if (TREE_CODE (origin) != FUNCTION_DECL)
17019     {
17020       /* We may have gotten separated from the block for the inlined
17021          function, if we're in an exception handler or some such; make
17022          sure that the abstract function has been written out.
17023
17024          Doing this for nested functions is wrong, however; functions are
17025          distinct units, and our context might not even be inline.  */
17026       tree fn = origin;
17027
17028       if (TYPE_P (fn))
17029         fn = TYPE_STUB_DECL (fn);
17030
17031       fn = decl_function_context (fn);
17032       if (fn)
17033         dwarf2out_abstract_function (fn);
17034     }
17035
17036   if (DECL_P (origin))
17037     origin_die = lookup_decl_die (origin);
17038   else if (TYPE_P (origin))
17039     origin_die = lookup_type_die (origin);
17040
17041   /* XXX: Functions that are never lowered don't always have correct block
17042      trees (in the case of java, they simply have no block tree, in some other
17043      languages).  For these functions, there is nothing we can really do to
17044      output correct debug info for inlined functions in all cases.  Rather
17045      than die, we'll just produce deficient debug info now, in that we will
17046      have variables without a proper abstract origin.  In the future, when all
17047      functions are lowered, we should re-add a gcc_assert (origin_die)
17048      here.  */
17049
17050   if (origin_die)
17051     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
17052   return origin_die;
17053 }
17054
17055 /* We do not currently support the pure_virtual attribute.  */
17056
17057 static inline void
17058 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
17059 {
17060   if (DECL_VINDEX (func_decl))
17061     {
17062       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
17063
17064       if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
17065         add_AT_loc (die, DW_AT_vtable_elem_location,
17066                     new_loc_descr (DW_OP_constu,
17067                                    tree_to_shwi (DECL_VINDEX (func_decl)),
17068                                    0));
17069
17070       /* GNU extension: Record what type this method came from originally.  */
17071       if (debug_info_level > DINFO_LEVEL_TERSE
17072           && DECL_CONTEXT (func_decl))
17073         add_AT_die_ref (die, DW_AT_containing_type,
17074                         lookup_type_die (DECL_CONTEXT (func_decl)));
17075     }
17076 }
17077 \f
17078 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
17079    given decl.  This used to be a vendor extension until after DWARF 4
17080    standardized it.  */
17081
17082 static void
17083 add_linkage_attr (dw_die_ref die, tree decl)
17084 {
17085   const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
17086
17087   /* Mimic what assemble_name_raw does with a leading '*'.  */
17088   if (name[0] == '*')
17089     name = &name[1];
17090
17091   if (dwarf_version >= 4)
17092     add_AT_string (die, DW_AT_linkage_name, name);
17093   else
17094     add_AT_string (die, DW_AT_MIPS_linkage_name, name);
17095 }
17096
17097 /* Add source coordinate attributes for the given decl.  */
17098
17099 static void
17100 add_src_coords_attributes (dw_die_ref die, tree decl)
17101 {
17102   expanded_location s;
17103
17104   if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
17105     return;
17106   s = expand_location (DECL_SOURCE_LOCATION (decl));
17107   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
17108   add_AT_unsigned (die, DW_AT_decl_line, s.line);
17109 }
17110
17111 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl.  */
17112
17113 static void
17114 add_linkage_name (dw_die_ref die, tree decl)
17115 {
17116   if (debug_info_level > DINFO_LEVEL_NONE
17117       && (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
17118       && TREE_PUBLIC (decl)
17119       && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
17120       && die->die_tag != DW_TAG_member)
17121     {
17122       /* Defer until we have an assembler name set.  */
17123       if (!DECL_ASSEMBLER_NAME_SET_P (decl))
17124         {
17125           limbo_die_node *asm_name;
17126
17127           asm_name = ggc_cleared_alloc<limbo_die_node> ();
17128           asm_name->die = die;
17129           asm_name->created_for = decl;
17130           asm_name->next = deferred_asm_name;
17131           deferred_asm_name = asm_name;
17132         }
17133       else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
17134         add_linkage_attr (die, decl);
17135     }
17136 }
17137
17138 /* Add a DW_AT_name attribute and source coordinate attribute for the
17139    given decl, but only if it actually has a name.  */
17140
17141 static void
17142 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
17143 {
17144   tree decl_name;
17145
17146   decl_name = DECL_NAME (decl);
17147   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
17148     {
17149       const char *name = dwarf2_name (decl, 0);
17150       if (name)
17151         add_name_attribute (die, name);
17152       if (! DECL_ARTIFICIAL (decl))
17153         add_src_coords_attributes (die, decl);
17154
17155       add_linkage_name (die, decl);
17156     }
17157
17158 #ifdef VMS_DEBUGGING_INFO
17159   /* Get the function's name, as described by its RTL.  This may be different
17160      from the DECL_NAME name used in the source file.  */
17161   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
17162     {
17163       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
17164                   XEXP (DECL_RTL (decl), 0), false);
17165       vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
17166     }
17167 #endif /* VMS_DEBUGGING_INFO */
17168 }
17169
17170 #ifdef VMS_DEBUGGING_INFO
17171 /* Output the debug main pointer die for VMS */
17172
17173 void
17174 dwarf2out_vms_debug_main_pointer (void)
17175 {
17176   char label[MAX_ARTIFICIAL_LABEL_BYTES];
17177   dw_die_ref die;
17178
17179   /* Allocate the VMS debug main subprogram die.  */
17180   die = ggc_cleared_alloc<die_node> ();
17181   die->die_tag = DW_TAG_subprogram;
17182   add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
17183   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
17184                                current_function_funcdef_no);
17185   add_AT_lbl_id (die, DW_AT_entry_pc, label);
17186
17187   /* Make it the first child of comp_unit_die ().  */
17188   die->die_parent = comp_unit_die ();
17189   if (comp_unit_die ()->die_child)
17190     {
17191       die->die_sib = comp_unit_die ()->die_child->die_sib;
17192       comp_unit_die ()->die_child->die_sib = die;
17193     }
17194   else
17195     {
17196       die->die_sib = die;
17197       comp_unit_die ()->die_child = die;
17198     }
17199 }
17200 #endif /* VMS_DEBUGGING_INFO */
17201
17202 /* Push a new declaration scope.  */
17203
17204 static void
17205 push_decl_scope (tree scope)
17206 {
17207   vec_safe_push (decl_scope_table, scope);
17208 }
17209
17210 /* Pop a declaration scope.  */
17211
17212 static inline void
17213 pop_decl_scope (void)
17214 {
17215   decl_scope_table->pop ();
17216 }
17217
17218 /* walk_tree helper function for uses_local_type, below.  */
17219
17220 static tree
17221 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
17222 {
17223   if (!TYPE_P (*tp))
17224     *walk_subtrees = 0;
17225   else
17226     {
17227       tree name = TYPE_NAME (*tp);
17228       if (name && DECL_P (name) && decl_function_context (name))
17229         return *tp;
17230     }
17231   return NULL_TREE;
17232 }
17233
17234 /* If TYPE involves a function-local type (including a local typedef to a
17235    non-local type), returns that type; otherwise returns NULL_TREE.  */
17236
17237 static tree
17238 uses_local_type (tree type)
17239 {
17240   tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
17241   return used;
17242 }
17243
17244 /* Return the DIE for the scope that immediately contains this type.
17245    Non-named types that do not involve a function-local type get global
17246    scope.  Named types nested in namespaces or other types get their
17247    containing scope.  All other types (i.e. function-local named types) get
17248    the current active scope.  */
17249
17250 static dw_die_ref
17251 scope_die_for (tree t, dw_die_ref context_die)
17252 {
17253   dw_die_ref scope_die = NULL;
17254   tree containing_scope;
17255
17256   /* Non-types always go in the current scope.  */
17257   gcc_assert (TYPE_P (t));
17258
17259   /* Use the scope of the typedef, rather than the scope of the type
17260      it refers to.  */
17261   if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
17262     containing_scope = DECL_CONTEXT (TYPE_NAME (t));
17263   else
17264     containing_scope = TYPE_CONTEXT (t);
17265
17266   /* Use the containing namespace if there is one.  */
17267   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
17268     {
17269       if (context_die == lookup_decl_die (containing_scope))
17270         /* OK */;
17271       else if (debug_info_level > DINFO_LEVEL_TERSE)
17272         context_die = get_context_die (containing_scope);
17273       else
17274         containing_scope = NULL_TREE;
17275     }
17276
17277   /* Ignore function type "scopes" from the C frontend.  They mean that
17278      a tagged type is local to a parmlist of a function declarator, but
17279      that isn't useful to DWARF.  */
17280   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
17281     containing_scope = NULL_TREE;
17282
17283   if (SCOPE_FILE_SCOPE_P (containing_scope))
17284     {
17285       /* If T uses a local type keep it local as well, to avoid references
17286          to function-local DIEs from outside the function.  */
17287       if (current_function_decl && uses_local_type (t))
17288         scope_die = context_die;
17289       else
17290         scope_die = comp_unit_die ();
17291     }
17292   else if (TYPE_P (containing_scope))
17293     {
17294       /* For types, we can just look up the appropriate DIE.  */
17295       if (debug_info_level > DINFO_LEVEL_TERSE)
17296         scope_die = get_context_die (containing_scope);
17297       else
17298         {
17299           scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
17300           if (scope_die == NULL)
17301             scope_die = comp_unit_die ();
17302         }
17303     }
17304   else
17305     scope_die = context_die;
17306
17307   return scope_die;
17308 }
17309
17310 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
17311
17312 static inline int
17313 local_scope_p (dw_die_ref context_die)
17314 {
17315   for (; context_die; context_die = context_die->die_parent)
17316     if (context_die->die_tag == DW_TAG_inlined_subroutine
17317         || context_die->die_tag == DW_TAG_subprogram)
17318       return 1;
17319
17320   return 0;
17321 }
17322
17323 /* Returns nonzero if CONTEXT_DIE is a class.  */
17324
17325 static inline int
17326 class_scope_p (dw_die_ref context_die)
17327 {
17328   return (context_die
17329           && (context_die->die_tag == DW_TAG_structure_type
17330               || context_die->die_tag == DW_TAG_class_type
17331               || context_die->die_tag == DW_TAG_interface_type
17332               || context_die->die_tag == DW_TAG_union_type));
17333 }
17334
17335 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17336    whether or not to treat a DIE in this context as a declaration.  */
17337
17338 static inline int
17339 class_or_namespace_scope_p (dw_die_ref context_die)
17340 {
17341   return (class_scope_p (context_die)
17342           || (context_die && context_die->die_tag == DW_TAG_namespace));
17343 }
17344
17345 /* Many forms of DIEs require a "type description" attribute.  This
17346    routine locates the proper "type descriptor" die for the type given
17347    by 'type' plus any additional qualifiers given by 'cv_quals', and
17348    adds a DW_AT_type attribute below the given die.  */
17349
17350 static void
17351 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
17352                     dw_die_ref context_die)
17353 {
17354   enum tree_code code  = TREE_CODE (type);
17355   dw_die_ref type_die  = NULL;
17356
17357   /* ??? If this type is an unnamed subrange type of an integral, floating-point
17358      or fixed-point type, use the inner type.  This is because we have no
17359      support for unnamed types in base_type_die.  This can happen if this is
17360      an Ada subrange type.  Correct solution is emit a subrange type die.  */
17361   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
17362       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
17363     type = TREE_TYPE (type), code = TREE_CODE (type);
17364
17365   if (code == ERROR_MARK
17366       /* Handle a special case.  For functions whose return type is void, we
17367          generate *no* type attribute.  (Note that no object may have type
17368          `void', so this only applies to function return types).  */
17369       || code == VOID_TYPE)
17370     return;
17371
17372   type_die = modified_type_die (type,
17373                                 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
17374                                 context_die);
17375
17376   if (type_die != NULL)
17377     add_AT_die_ref (object_die, DW_AT_type, type_die);
17378 }
17379
17380 /* Given an object die, add the calling convention attribute for the
17381    function call type.  */
17382 static void
17383 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
17384 {
17385   enum dwarf_calling_convention value = DW_CC_normal;
17386
17387   value = ((enum dwarf_calling_convention)
17388            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
17389
17390   if (is_fortran ()
17391       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
17392     {
17393       /* DWARF 2 doesn't provide a way to identify a program's source-level
17394         entry point.  DW_AT_calling_convention attributes are only meant
17395         to describe functions' calling conventions.  However, lacking a
17396         better way to signal the Fortran main program, we used this for 
17397         a long time, following existing custom.  Now, DWARF 4 has 
17398         DW_AT_main_subprogram, which we add below, but some tools still
17399         rely on the old way, which we thus keep.  */
17400       value = DW_CC_program;
17401
17402       if (dwarf_version >= 4 || !dwarf_strict)
17403         add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
17404     }
17405
17406   /* Only add the attribute if the backend requests it, and
17407      is not DW_CC_normal.  */
17408   if (value && (value != DW_CC_normal))
17409     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
17410 }
17411
17412 /* Given a tree pointer to a struct, class, union, or enum type node, return
17413    a pointer to the (string) tag name for the given type, or zero if the type
17414    was declared without a tag.  */
17415
17416 static const char *
17417 type_tag (const_tree type)
17418 {
17419   const char *name = 0;
17420
17421   if (TYPE_NAME (type) != 0)
17422     {
17423       tree t = 0;
17424
17425       /* Find the IDENTIFIER_NODE for the type name.  */
17426       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
17427           && !TYPE_NAMELESS (type))
17428         t = TYPE_NAME (type);
17429
17430       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
17431          a TYPE_DECL node, regardless of whether or not a `typedef' was
17432          involved.  */
17433       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
17434                && ! DECL_IGNORED_P (TYPE_NAME (type)))
17435         {
17436           /* We want to be extra verbose.  Don't call dwarf_name if
17437              DECL_NAME isn't set.  The default hook for decl_printable_name
17438              doesn't like that, and in this context it's correct to return
17439              0, instead of "<anonymous>" or the like.  */
17440           if (DECL_NAME (TYPE_NAME (type))
17441               && !DECL_NAMELESS (TYPE_NAME (type)))
17442             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
17443         }
17444
17445       /* Now get the name as a string, or invent one.  */
17446       if (!name && t != 0)
17447         name = IDENTIFIER_POINTER (t);
17448     }
17449
17450   return (name == 0 || *name == '\0') ? 0 : name;
17451 }
17452
17453 /* Return the type associated with a data member, make a special check
17454    for bit field types.  */
17455
17456 static inline tree
17457 member_declared_type (const_tree member)
17458 {
17459   return (DECL_BIT_FIELD_TYPE (member)
17460           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
17461 }
17462
17463 /* Get the decl's label, as described by its RTL. This may be different
17464    from the DECL_NAME name used in the source file.  */
17465
17466 #if 0
17467 static const char *
17468 decl_start_label (tree decl)
17469 {
17470   rtx x;
17471   const char *fnname;
17472
17473   x = DECL_RTL (decl);
17474   gcc_assert (MEM_P (x));
17475
17476   x = XEXP (x, 0);
17477   gcc_assert (GET_CODE (x) == SYMBOL_REF);
17478
17479   fnname = XSTR (x, 0);
17480   return fnname;
17481 }
17482 #endif
17483 \f
17484 /* These routines generate the internal representation of the DIE's for
17485    the compilation unit.  Debugging information is collected by walking
17486    the declaration trees passed in from dwarf2out_decl().  */
17487
17488 static void
17489 gen_array_type_die (tree type, dw_die_ref context_die)
17490 {
17491   dw_die_ref scope_die = scope_die_for (type, context_die);
17492   dw_die_ref array_die;
17493
17494   /* GNU compilers represent multidimensional array types as sequences of one
17495      dimensional array types whose element types are themselves array types.
17496      We sometimes squish that down to a single array_type DIE with multiple
17497      subscripts in the Dwarf debugging info.  The draft Dwarf specification
17498      say that we are allowed to do this kind of compression in C, because
17499      there is no difference between an array of arrays and a multidimensional
17500      array.  We don't do this for Ada to remain as close as possible to the
17501      actual representation, which is especially important against the language
17502      flexibilty wrt arrays of variable size.  */
17503
17504   bool collapse_nested_arrays = !is_ada ();
17505   tree element_type;
17506
17507   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
17508      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
17509   if (TYPE_STRING_FLAG (type)
17510       && TREE_CODE (type) == ARRAY_TYPE
17511       && is_fortran ()
17512       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
17513     {
17514       HOST_WIDE_INT size;
17515
17516       array_die = new_die (DW_TAG_string_type, scope_die, type);
17517       add_name_attribute (array_die, type_tag (type));
17518       equate_type_number_to_die (type, array_die);
17519       size = int_size_in_bytes (type);
17520       if (size >= 0)
17521         add_AT_unsigned (array_die, DW_AT_byte_size, size);
17522       else if (TYPE_DOMAIN (type) != NULL_TREE
17523                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
17524                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17525         {
17526           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17527           dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2, NULL);
17528
17529           size = int_size_in_bytes (TREE_TYPE (szdecl));
17530           if (loc && size > 0)
17531             {
17532               add_AT_location_description (array_die, DW_AT_string_length, loc);
17533               if (size != DWARF2_ADDR_SIZE)
17534                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17535             }
17536         }
17537       return;
17538     }
17539
17540   array_die = new_die (DW_TAG_array_type, scope_die, type);
17541   add_name_attribute (array_die, type_tag (type));
17542   equate_type_number_to_die (type, array_die);
17543
17544   if (TREE_CODE (type) == VECTOR_TYPE)
17545     add_AT_flag (array_die, DW_AT_GNU_vector, 1);
17546
17547   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
17548   if (is_fortran ()
17549       && TREE_CODE (type) == ARRAY_TYPE
17550       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17551       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17552     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17553
17554 #if 0
17555   /* We default the array ordering.  SDB will probably do
17556      the right things even if DW_AT_ordering is not present.  It's not even
17557      an issue until we start to get into multidimensional arrays anyway.  If
17558      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17559      then we'll have to put the DW_AT_ordering attribute back in.  (But if
17560      and when we find out that we need to put these in, we will only do so
17561      for multidimensional arrays.  */
17562   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17563 #endif
17564
17565   if (TREE_CODE (type) == VECTOR_TYPE)
17566     {
17567       /* For VECTOR_TYPEs we use an array die with appropriate bounds.  */
17568       dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
17569       add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
17570       add_bound_info (subrange_die, DW_AT_upper_bound,
17571                       size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
17572     }
17573   else
17574     add_subscript_info (array_die, type, collapse_nested_arrays);
17575
17576   /* Add representation of the type of the elements of this array type and
17577      emit the corresponding DIE if we haven't done it already.  */
17578   element_type = TREE_TYPE (type);
17579   if (collapse_nested_arrays)
17580     while (TREE_CODE (element_type) == ARRAY_TYPE)
17581       {
17582         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17583           break;
17584         element_type = TREE_TYPE (element_type);
17585       }
17586
17587   add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED, context_die);
17588
17589   add_gnat_descriptive_type_attribute (array_die, type, context_die);
17590   if (TYPE_ARTIFICIAL (type))
17591     add_AT_flag (array_die, DW_AT_artificial, 1);
17592
17593   if (get_AT (array_die, DW_AT_name))
17594     add_pubtype (type, array_die);
17595 }
17596
17597 /* This routine generates DIE for array with hidden descriptor, details
17598    are filled into *info by a langhook.  */
17599
17600 static void
17601 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17602                           dw_die_ref context_die)
17603 {
17604   const dw_die_ref scope_die = scope_die_for (type, context_die);
17605   const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
17606   const struct loc_descr_context context = { type, info->base_decl };
17607   int dim;
17608
17609   add_name_attribute (array_die, type_tag (type));
17610   equate_type_number_to_die (type, array_die);
17611
17612   if (info->ndimensions > 1)
17613     switch (info->ordering)
17614       {
17615       case array_descr_ordering_row_major:
17616         add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17617         break;
17618       case array_descr_ordering_column_major:
17619         add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17620         break;
17621       default:
17622         break;
17623       }
17624
17625   if (dwarf_version >= 3 || !dwarf_strict)
17626     {
17627       if (info->data_location)
17628         add_scalar_info (array_die, DW_AT_data_location, info->data_location,
17629                          dw_scalar_form_exprloc, &context);
17630       if (info->associated)
17631         add_scalar_info (array_die, DW_AT_associated, info->associated,
17632                          dw_scalar_form_constant
17633                          | dw_scalar_form_exprloc
17634                          | dw_scalar_form_reference, &context);
17635       if (info->allocated)
17636         add_scalar_info (array_die, DW_AT_allocated, info->allocated,
17637                          dw_scalar_form_constant
17638                          | dw_scalar_form_exprloc
17639                          | dw_scalar_form_reference, &context);
17640     }
17641
17642   add_gnat_descriptive_type_attribute (array_die, type, context_die);
17643
17644   for (dim = 0; dim < info->ndimensions; dim++)
17645     {
17646       dw_die_ref subrange_die
17647         = new_die (DW_TAG_subrange_type, array_die, NULL);
17648
17649       if (info->dimen[dim].bounds_type)
17650         add_type_attribute (subrange_die,
17651                             info->dimen[dim].bounds_type, 0,
17652                             context_die);
17653       if (info->dimen[dim].lower_bound)
17654         add_bound_info (subrange_die, DW_AT_lower_bound,
17655                         info->dimen[dim].lower_bound, &context);
17656       if (info->dimen[dim].upper_bound)
17657         add_bound_info (subrange_die, DW_AT_upper_bound,
17658                         info->dimen[dim].upper_bound, &context);
17659       if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
17660         add_scalar_info (subrange_die, DW_AT_byte_stride,
17661                          info->dimen[dim].stride,
17662                          dw_scalar_form_constant
17663                          | dw_scalar_form_exprloc
17664                          | dw_scalar_form_reference,
17665                          &context);
17666     }
17667
17668   gen_type_die (info->element_type, context_die);
17669   add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
17670                       context_die);
17671
17672   if (get_AT (array_die, DW_AT_name))
17673     add_pubtype (type, array_die);
17674 }
17675
17676 #if 0
17677 static void
17678 gen_entry_point_die (tree decl, dw_die_ref context_die)
17679 {
17680   tree origin = decl_ultimate_origin (decl);
17681   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17682
17683   if (origin != NULL)
17684     add_abstract_origin_attribute (decl_die, origin);
17685   else
17686     {
17687       add_name_and_src_coords_attributes (decl_die, decl);
17688       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17689                           TYPE_UNQUALIFIED, context_die);
17690     }
17691
17692   if (DECL_ABSTRACT_P (decl))
17693     equate_decl_number_to_die (decl, decl_die);
17694   else
17695     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17696 }
17697 #endif
17698
17699 /* Walk through the list of incomplete types again, trying once more to
17700    emit full debugging info for them.  */
17701
17702 static void
17703 retry_incomplete_types (void)
17704 {
17705   int i;
17706
17707   for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17708     if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17709       gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17710 }
17711
17712 /* Determine what tag to use for a record type.  */
17713
17714 static enum dwarf_tag
17715 record_type_tag (tree type)
17716 {
17717   if (! lang_hooks.types.classify_record)
17718     return DW_TAG_structure_type;
17719
17720   switch (lang_hooks.types.classify_record (type))
17721     {
17722     case RECORD_IS_STRUCT:
17723       return DW_TAG_structure_type;
17724
17725     case RECORD_IS_CLASS:
17726       return DW_TAG_class_type;
17727
17728     case RECORD_IS_INTERFACE:
17729       if (dwarf_version >= 3 || !dwarf_strict)
17730         return DW_TAG_interface_type;
17731       return DW_TAG_structure_type;
17732
17733     default:
17734       gcc_unreachable ();
17735     }
17736 }
17737
17738 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
17739    include all of the information about the enumeration values also. Each
17740    enumerated type name/value is listed as a child of the enumerated type
17741    DIE.  */
17742
17743 static dw_die_ref
17744 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17745 {
17746   dw_die_ref type_die = lookup_type_die (type);
17747
17748   if (type_die == NULL)
17749     {
17750       type_die = new_die (DW_TAG_enumeration_type,
17751                           scope_die_for (type, context_die), type);
17752       equate_type_number_to_die (type, type_die);
17753       add_name_attribute (type_die, type_tag (type));
17754       if (dwarf_version >= 4 || !dwarf_strict)
17755         {
17756           if (ENUM_IS_SCOPED (type))
17757             add_AT_flag (type_die, DW_AT_enum_class, 1);
17758           if (ENUM_IS_OPAQUE (type))
17759             add_AT_flag (type_die, DW_AT_declaration, 1);
17760         }
17761     }
17762   else if (! TYPE_SIZE (type))
17763     return type_die;
17764   else
17765     remove_AT (type_die, DW_AT_declaration);
17766
17767   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
17768      given enum type is incomplete, do not generate the DW_AT_byte_size
17769      attribute or the DW_AT_element_list attribute.  */
17770   if (TYPE_SIZE (type))
17771     {
17772       tree link;
17773
17774       TREE_ASM_WRITTEN (type) = 1;
17775       add_byte_size_attribute (type_die, type);
17776       if (dwarf_version >= 3 || !dwarf_strict)
17777         {
17778           tree underlying = lang_hooks.types.enum_underlying_base_type (type);
17779           add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED,
17780                               context_die);
17781         }
17782       if (TYPE_STUB_DECL (type) != NULL_TREE)
17783         {
17784           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17785           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17786         }
17787
17788       /* If the first reference to this type was as the return type of an
17789          inline function, then it may not have a parent.  Fix this now.  */
17790       if (type_die->die_parent == NULL)
17791         add_child_die (scope_die_for (type, context_die), type_die);
17792
17793       for (link = TYPE_VALUES (type);
17794            link != NULL; link = TREE_CHAIN (link))
17795         {
17796           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17797           tree value = TREE_VALUE (link);
17798
17799           add_name_attribute (enum_die,
17800                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17801
17802           if (TREE_CODE (value) == CONST_DECL)
17803             value = DECL_INITIAL (value);
17804
17805           if (simple_type_size_in_bits (TREE_TYPE (value))
17806               <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
17807             {
17808               /* For constant forms created by add_AT_unsigned DWARF
17809                  consumers (GDB, elfutils, etc.) always zero extend
17810                  the value.  Only when the actual value is negative
17811                  do we need to use add_AT_int to generate a constant
17812                  form that can represent negative values.  */
17813               HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
17814               if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
17815                 add_AT_unsigned (enum_die, DW_AT_const_value,
17816                                  (unsigned HOST_WIDE_INT) val);
17817               else
17818                 add_AT_int (enum_die, DW_AT_const_value, val);
17819             }
17820           else
17821             /* Enumeration constants may be wider than HOST_WIDE_INT.  Handle
17822                that here.  TODO: This should be re-worked to use correct
17823                signed/unsigned double tags for all cases.  */
17824             add_AT_wide (enum_die, DW_AT_const_value, value);
17825         }
17826
17827       add_gnat_descriptive_type_attribute (type_die, type, context_die);
17828       if (TYPE_ARTIFICIAL (type))
17829         add_AT_flag (type_die, DW_AT_artificial, 1);
17830     }
17831   else
17832     add_AT_flag (type_die, DW_AT_declaration, 1);
17833
17834   add_pubtype (type, type_die);
17835
17836   return type_die;
17837 }
17838
17839 /* Generate a DIE to represent either a real live formal parameter decl or to
17840    represent just the type of some formal parameter position in some function
17841    type.
17842
17843    Note that this routine is a bit unusual because its argument may be a
17844    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17845    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17846    node.  If it's the former then this function is being called to output a
17847    DIE to represent a formal parameter object (or some inlining thereof).  If
17848    it's the latter, then this function is only being called to output a
17849    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17850    argument type of some subprogram type.
17851    If EMIT_NAME_P is true, name and source coordinate attributes
17852    are emitted.  */
17853
17854 static dw_die_ref
17855 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17856                           dw_die_ref context_die)
17857 {
17858   tree node_or_origin = node ? node : origin;
17859   tree ultimate_origin;
17860   dw_die_ref parm_die
17861     = new_die (DW_TAG_formal_parameter, context_die, node);
17862
17863   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
17864     {
17865     case tcc_declaration:
17866       ultimate_origin = decl_ultimate_origin (node_or_origin);
17867       if (node || ultimate_origin)
17868         origin = ultimate_origin;
17869       if (origin != NULL)
17870         add_abstract_origin_attribute (parm_die, origin);
17871       else if (emit_name_p)
17872         add_name_and_src_coords_attributes (parm_die, node);
17873       if (origin == NULL
17874           || (! DECL_ABSTRACT_P (node_or_origin)
17875               && variably_modified_type_p (TREE_TYPE (node_or_origin),
17876                                            decl_function_context
17877                                                             (node_or_origin))))
17878         {
17879           tree type = TREE_TYPE (node_or_origin);
17880           if (decl_by_reference_p (node_or_origin))
17881             add_type_attribute (parm_die, TREE_TYPE (type),
17882                                 TYPE_UNQUALIFIED, context_die);
17883           else
17884             add_type_attribute (parm_die, type,
17885                                 decl_quals (node_or_origin),
17886                                 context_die);
17887         }
17888       if (origin == NULL && DECL_ARTIFICIAL (node))
17889         add_AT_flag (parm_die, DW_AT_artificial, 1);
17890
17891       if (node && node != origin)
17892         equate_decl_number_to_die (node, parm_die);
17893       if (! DECL_ABSTRACT_P (node_or_origin))
17894         add_location_or_const_value_attribute (parm_die, node_or_origin,
17895                                                node == NULL, DW_AT_location);
17896
17897       break;
17898
17899     case tcc_type:
17900       /* We were called with some kind of a ..._TYPE node.  */
17901       add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED,
17902                           context_die);
17903       break;
17904
17905     default:
17906       gcc_unreachable ();
17907     }
17908
17909   return parm_die;
17910 }
17911
17912 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17913    children DW_TAG_formal_parameter DIEs representing the arguments of the
17914    parameter pack.
17915
17916    PARM_PACK must be a function parameter pack.
17917    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17918    must point to the subsequent arguments of the function PACK_ARG belongs to.
17919    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17920    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17921    following the last one for which a DIE was generated.  */
17922
17923 static dw_die_ref
17924 gen_formal_parameter_pack_die  (tree parm_pack,
17925                                 tree pack_arg,
17926                                 dw_die_ref subr_die,
17927                                 tree *next_arg)
17928 {
17929   tree arg;
17930   dw_die_ref parm_pack_die;
17931
17932   gcc_assert (parm_pack
17933               && lang_hooks.function_parameter_pack_p (parm_pack)
17934               && subr_die);
17935
17936   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17937   add_src_coords_attributes (parm_pack_die, parm_pack);
17938
17939   for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
17940     {
17941       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17942                                                                  parm_pack))
17943         break;
17944       gen_formal_parameter_die (arg, NULL,
17945                                 false /* Don't emit name attribute.  */,
17946                                 parm_pack_die);
17947     }
17948   if (next_arg)
17949     *next_arg = arg;
17950   return parm_pack_die;
17951 }
17952
17953 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17954    at the end of an (ANSI prototyped) formal parameters list.  */
17955
17956 static void
17957 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
17958 {
17959   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
17960 }
17961
17962 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
17963    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
17964    parameters as specified in some function type specification (except for
17965    those which appear as part of a function *definition*).  */
17966
17967 static void
17968 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
17969 {
17970   tree link;
17971   tree formal_type = NULL;
17972   tree first_parm_type;
17973   tree arg;
17974
17975   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
17976     {
17977       arg = DECL_ARGUMENTS (function_or_method_type);
17978       function_or_method_type = TREE_TYPE (function_or_method_type);
17979     }
17980   else
17981     arg = NULL_TREE;
17982
17983   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
17984
17985   /* Make our first pass over the list of formal parameter types and output a
17986      DW_TAG_formal_parameter DIE for each one.  */
17987   for (link = first_parm_type; link; )
17988     {
17989       dw_die_ref parm_die;
17990
17991       formal_type = TREE_VALUE (link);
17992       if (formal_type == void_type_node)
17993         break;
17994
17995       /* Output a (nameless) DIE to represent the formal parameter itself.  */
17996       if (!POINTER_BOUNDS_TYPE_P (formal_type))
17997         {
17998           parm_die = gen_formal_parameter_die (formal_type, NULL,
17999                                                true /* Emit name attribute.  */,
18000                                                context_die);
18001           if (TREE_CODE (function_or_method_type) == METHOD_TYPE
18002               && link == first_parm_type)
18003             {
18004               add_AT_flag (parm_die, DW_AT_artificial, 1);
18005               if (dwarf_version >= 3 || !dwarf_strict)
18006                 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
18007             }
18008           else if (arg && DECL_ARTIFICIAL (arg))
18009             add_AT_flag (parm_die, DW_AT_artificial, 1);
18010         }
18011
18012       link = TREE_CHAIN (link);
18013       if (arg)
18014         arg = DECL_CHAIN (arg);
18015     }
18016
18017   /* If this function type has an ellipsis, add a
18018      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
18019   if (formal_type != void_type_node)
18020     gen_unspecified_parameters_die (function_or_method_type, context_die);
18021
18022   /* Make our second (and final) pass over the list of formal parameter types
18023      and output DIEs to represent those types (as necessary).  */
18024   for (link = TYPE_ARG_TYPES (function_or_method_type);
18025        link && TREE_VALUE (link);
18026        link = TREE_CHAIN (link))
18027     gen_type_die (TREE_VALUE (link), context_die);
18028 }
18029
18030 /* We want to generate the DIE for TYPE so that we can generate the
18031    die for MEMBER, which has been defined; we will need to refer back
18032    to the member declaration nested within TYPE.  If we're trying to
18033    generate minimal debug info for TYPE, processing TYPE won't do the
18034    trick; we need to attach the member declaration by hand.  */
18035
18036 static void
18037 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
18038 {
18039   gen_type_die (type, context_die);
18040
18041   /* If we're trying to avoid duplicate debug info, we may not have
18042      emitted the member decl for this function.  Emit it now.  */
18043   if (TYPE_STUB_DECL (type)
18044       && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
18045       && ! lookup_decl_die (member))
18046     {
18047       dw_die_ref type_die;
18048       gcc_assert (!decl_ultimate_origin (member));
18049
18050       push_decl_scope (type);
18051       type_die = lookup_type_die_strip_naming_typedef (type);
18052       if (TREE_CODE (member) == FUNCTION_DECL)
18053         gen_subprogram_die (member, type_die);
18054       else if (TREE_CODE (member) == FIELD_DECL)
18055         {
18056           /* Ignore the nameless fields that are used to skip bits but handle
18057              C++ anonymous unions and structs.  */
18058           if (DECL_NAME (member) != NULL_TREE
18059               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
18060               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
18061             {
18062               gen_type_die (member_declared_type (member), type_die);
18063               gen_field_die (member, type_die);
18064             }
18065         }
18066       else
18067         gen_variable_die (member, NULL_TREE, type_die);
18068
18069       pop_decl_scope ();
18070     }
18071 }
18072 \f
18073 /* Forward declare these functions, because they are mutually recursive
18074   with their set_block_* pairing functions.  */
18075 static void set_decl_origin_self (tree);
18076 static void set_decl_abstract_flags (tree, vec<tree> &);
18077
18078 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
18079    given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
18080    that it points to the node itself, thus indicating that the node is its
18081    own (abstract) origin.  Additionally, if the BLOCK_ABSTRACT_ORIGIN for
18082    the given node is NULL, recursively descend the decl/block tree which
18083    it is the root of, and for each other ..._DECL or BLOCK node contained
18084    therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
18085    still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
18086    values to point to themselves.  */
18087
18088 static void
18089 set_block_origin_self (tree stmt)
18090 {
18091   if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
18092     {
18093       BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
18094
18095       {
18096         tree local_decl;
18097
18098         for (local_decl = BLOCK_VARS (stmt);
18099              local_decl != NULL_TREE;
18100              local_decl = DECL_CHAIN (local_decl))
18101           /* Do not recurse on nested functions since the inlining status
18102              of parent and child can be different as per the DWARF spec.  */
18103           if (TREE_CODE (local_decl) != FUNCTION_DECL
18104               && !DECL_EXTERNAL (local_decl))
18105             set_decl_origin_self (local_decl);
18106       }
18107
18108       {
18109         tree subblock;
18110
18111         for (subblock = BLOCK_SUBBLOCKS (stmt);
18112              subblock != NULL_TREE;
18113              subblock = BLOCK_CHAIN (subblock))
18114           set_block_origin_self (subblock);     /* Recurse.  */
18115       }
18116     }
18117 }
18118
18119 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
18120    the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
18121    node to so that it points to the node itself, thus indicating that the
18122    node represents its own (abstract) origin.  Additionally, if the
18123    DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
18124    the decl/block tree of which the given node is the root of, and for
18125    each other ..._DECL or BLOCK node contained therein whose
18126    DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
18127    set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
18128    point to themselves.  */
18129
18130 static void
18131 set_decl_origin_self (tree decl)
18132 {
18133   if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
18134     {
18135       DECL_ABSTRACT_ORIGIN (decl) = decl;
18136       if (TREE_CODE (decl) == FUNCTION_DECL)
18137         {
18138           tree arg;
18139
18140           for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
18141             DECL_ABSTRACT_ORIGIN (arg) = arg;
18142           if (DECL_INITIAL (decl) != NULL_TREE
18143               && DECL_INITIAL (decl) != error_mark_node)
18144             set_block_origin_self (DECL_INITIAL (decl));
18145         }
18146     }
18147 }
18148 \f
18149 /* Given a pointer to some BLOCK node, set the BLOCK_ABSTRACT flag to 1
18150    and if it wasn't 1 before, push it to abstract_vec vector.
18151    For all local decls and all local sub-blocks (recursively) do it
18152    too.  */
18153
18154 static void
18155 set_block_abstract_flags (tree stmt, vec<tree> &abstract_vec)
18156 {
18157   tree local_decl;
18158   tree subblock;
18159   unsigned int i;
18160
18161   if (!BLOCK_ABSTRACT (stmt))
18162     {
18163       abstract_vec.safe_push (stmt);
18164       BLOCK_ABSTRACT (stmt) = 1;
18165     }
18166
18167   for (local_decl = BLOCK_VARS (stmt);
18168        local_decl != NULL_TREE;
18169        local_decl = DECL_CHAIN (local_decl))
18170     if (! DECL_EXTERNAL (local_decl))
18171       set_decl_abstract_flags (local_decl, abstract_vec);
18172
18173   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
18174     {
18175       local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
18176       if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
18177           || TREE_CODE (local_decl) == PARM_DECL)
18178         set_decl_abstract_flags (local_decl, abstract_vec);
18179     }
18180
18181   for (subblock = BLOCK_SUBBLOCKS (stmt);
18182        subblock != NULL_TREE;
18183        subblock = BLOCK_CHAIN (subblock))
18184     set_block_abstract_flags (subblock, abstract_vec);
18185 }
18186
18187 /* Given a pointer to some ..._DECL node, set DECL_ABSTRACT_P flag on it
18188    to 1 and if it wasn't 1 before, push to abstract_vec vector.
18189    In the case where the decl is a FUNCTION_DECL also set the abstract
18190    flags for all of the parameters, local vars, local
18191    blocks and sub-blocks (recursively).  */
18192
18193 static void
18194 set_decl_abstract_flags (tree decl, vec<tree> &abstract_vec)
18195 {
18196   if (!DECL_ABSTRACT_P (decl))
18197     {
18198       abstract_vec.safe_push (decl);
18199       DECL_ABSTRACT_P (decl) = 1;
18200     }
18201
18202   if (TREE_CODE (decl) == FUNCTION_DECL)
18203     {
18204       tree arg;
18205
18206       for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
18207         if (!DECL_ABSTRACT_P (arg))
18208           {
18209             abstract_vec.safe_push (arg);
18210             DECL_ABSTRACT_P (arg) = 1;
18211           }
18212       if (DECL_INITIAL (decl) != NULL_TREE
18213           && DECL_INITIAL (decl) != error_mark_node)
18214         set_block_abstract_flags (DECL_INITIAL (decl), abstract_vec);
18215     }
18216 }
18217
18218 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18219    may later generate inlined and/or out-of-line instances of.  */
18220
18221 static void
18222 dwarf2out_abstract_function (tree decl)
18223 {
18224   dw_die_ref old_die;
18225   tree save_fn;
18226   tree context;
18227   hash_table<decl_loc_hasher> *old_decl_loc_table;
18228   hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
18229   int old_call_site_count, old_tail_call_site_count;
18230   struct call_arg_loc_node *old_call_arg_locations;
18231
18232   /* Make sure we have the actual abstract inline, not a clone.  */
18233   decl = DECL_ORIGIN (decl);
18234
18235   old_die = lookup_decl_die (decl);
18236   if (old_die && get_AT (old_die, DW_AT_inline))
18237     /* We've already generated the abstract instance.  */
18238     return;
18239
18240   /* We can be called while recursively when seeing block defining inlined subroutine
18241      DIE.  Be sure to not clobber the outer location table nor use it or we would
18242      get locations in abstract instantces.  */
18243   old_decl_loc_table = decl_loc_table;
18244   decl_loc_table = NULL;
18245   old_cached_dw_loc_list_table = cached_dw_loc_list_table;
18246   cached_dw_loc_list_table = NULL;
18247   old_call_arg_locations = call_arg_locations;
18248   call_arg_locations = NULL;
18249   old_call_site_count = call_site_count;
18250   call_site_count = -1;
18251   old_tail_call_site_count = tail_call_site_count;
18252   tail_call_site_count = -1;
18253
18254   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18255      we don't get confused by DECL_ABSTRACT_P.  */
18256   if (debug_info_level > DINFO_LEVEL_TERSE)
18257     {
18258       context = decl_class_context (decl);
18259       if (context)
18260         gen_type_die_for_member
18261           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
18262     }
18263
18264   /* Pretend we've just finished compiling this function.  */
18265   save_fn = current_function_decl;
18266   current_function_decl = decl;
18267
18268   auto_vec<tree, 64> abstract_vec;
18269   set_decl_abstract_flags (decl, abstract_vec);
18270   dwarf2out_decl (decl);
18271   unsigned int i;
18272   tree t;
18273   FOR_EACH_VEC_ELT (abstract_vec, i, t)
18274     if (TREE_CODE (t) == BLOCK)
18275       BLOCK_ABSTRACT (t) = 0;
18276     else
18277       DECL_ABSTRACT_P (t) = 0;
18278
18279   current_function_decl = save_fn;
18280   decl_loc_table = old_decl_loc_table;
18281   cached_dw_loc_list_table = old_cached_dw_loc_list_table;
18282   call_arg_locations = old_call_arg_locations;
18283   call_site_count = old_call_site_count;
18284   tail_call_site_count = old_tail_call_site_count;
18285 }
18286
18287 /* Helper function of premark_used_types() which gets called through
18288    htab_traverse.
18289
18290    Marks the DIE of a given type in *SLOT as perennial, so it never gets
18291    marked as unused by prune_unused_types.  */
18292
18293 bool
18294 premark_used_types_helper (tree const &type, void *)
18295 {
18296   dw_die_ref die;
18297
18298   die = lookup_type_die (type);
18299   if (die != NULL)
18300     die->die_perennial_p = 1;
18301   return true;
18302 }
18303
18304 /* Helper function of premark_types_used_by_global_vars which gets called
18305    through htab_traverse.
18306
18307    Marks the DIE of a given type in *SLOT as perennial, so it never gets
18308    marked as unused by prune_unused_types. The DIE of the type is marked
18309    only if the global variable using the type will actually be emitted.  */
18310
18311 int
18312 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
18313                                           void *)
18314 {
18315   struct types_used_by_vars_entry *entry;
18316   dw_die_ref die;
18317
18318   entry = (struct types_used_by_vars_entry *) *slot;
18319   gcc_assert (entry->type != NULL
18320               && entry->var_decl != NULL);
18321   die = lookup_type_die (entry->type);
18322   if (die)
18323     {
18324       /* Ask cgraph if the global variable really is to be emitted.
18325          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
18326       varpool_node *node = varpool_node::get (entry->var_decl);
18327       if (node && node->definition)
18328         {
18329           die->die_perennial_p = 1;
18330           /* Keep the parent DIEs as well.  */
18331           while ((die = die->die_parent) && die->die_perennial_p == 0)
18332             die->die_perennial_p = 1;
18333         }
18334     }
18335   return 1;
18336 }
18337
18338 /* Mark all members of used_types_hash as perennial.  */
18339
18340 static void
18341 premark_used_types (struct function *fun)
18342 {
18343   if (fun && fun->used_types_hash)
18344     fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
18345 }
18346
18347 /* Mark all members of types_used_by_vars_entry as perennial.  */
18348
18349 static void
18350 premark_types_used_by_global_vars (void)
18351 {
18352   if (types_used_by_vars_hash)
18353     types_used_by_vars_hash
18354       ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
18355 }
18356
18357 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
18358    for CA_LOC call arg loc node.  */
18359
18360 static dw_die_ref
18361 gen_call_site_die (tree decl, dw_die_ref subr_die,
18362                    struct call_arg_loc_node *ca_loc)
18363 {
18364   dw_die_ref stmt_die = NULL, die;
18365   tree block = ca_loc->block;
18366
18367   while (block
18368          && block != DECL_INITIAL (decl)
18369          && TREE_CODE (block) == BLOCK)
18370     {
18371       if (block_map.length () > BLOCK_NUMBER (block))
18372         stmt_die = block_map[BLOCK_NUMBER (block)];
18373       if (stmt_die)
18374         break;
18375       block = BLOCK_SUPERCONTEXT (block);
18376     }
18377   if (stmt_die == NULL)
18378     stmt_die = subr_die;
18379   die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
18380   add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
18381   if (ca_loc->tail_call_p)
18382     add_AT_flag (die, DW_AT_GNU_tail_call, 1);
18383   if (ca_loc->symbol_ref)
18384     {
18385       dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
18386       if (tdie)
18387         add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
18388       else
18389         add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
18390     }
18391   return die;
18392 }
18393
18394 /* Generate a DIE to represent a declared function (either file-scope or
18395    block-local).  */
18396
18397 static void
18398 gen_subprogram_die (tree decl, dw_die_ref context_die)
18399 {
18400   tree origin = decl_ultimate_origin (decl);
18401   dw_die_ref subr_die;
18402   tree outer_scope;
18403   dw_die_ref old_die = lookup_decl_die (decl);
18404   int declaration = (current_function_decl != decl
18405                      || class_or_namespace_scope_p (context_die));
18406
18407   premark_used_types (DECL_STRUCT_FUNCTION (decl));
18408
18409   /* It is possible to have both DECL_ABSTRACT_P and DECLARATION be true if we
18410      started to generate the abstract instance of an inline, decided to output
18411      its containing class, and proceeded to emit the declaration of the inline
18412      from the member list for the class.  If so, DECLARATION takes priority;
18413      we'll get back to the abstract instance when done with the class.  */
18414
18415   /* The class-scope declaration DIE must be the primary DIE.  */
18416   if (origin && declaration && class_or_namespace_scope_p (context_die))
18417     {
18418       origin = NULL;
18419       gcc_assert (!old_die);
18420     }
18421
18422   /* Now that the C++ front end lazily declares artificial member fns, we
18423      might need to retrofit the declaration into its class.  */
18424   if (!declaration && !origin && !old_die
18425       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18426       && !class_or_namespace_scope_p (context_die)
18427       && debug_info_level > DINFO_LEVEL_TERSE)
18428     old_die = force_decl_die (decl);
18429
18430   if (origin != NULL)
18431     {
18432       gcc_assert (!declaration || local_scope_p (context_die));
18433
18434       /* Fixup die_parent for the abstract instance of a nested
18435          inline function.  */
18436       if (old_die && old_die->die_parent == NULL)
18437         add_child_die (context_die, old_die);
18438
18439       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18440       add_abstract_origin_attribute (subr_die, origin);
18441       /*  This is where the actual code for a cloned function is.
18442           Let's emit linkage name attribute for it.  This helps
18443           debuggers to e.g, set breakpoints into
18444           constructors/destructors when the user asks "break
18445           K::K".  */
18446       add_linkage_name (subr_die, decl);
18447     }
18448   else if (old_die)
18449     {
18450       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18451       struct dwarf_file_data * file_index = lookup_filename (s.file);
18452
18453       if (!get_AT_flag (old_die, DW_AT_declaration)
18454           /* We can have a normal definition following an inline one in the
18455              case of redefinition of GNU C extern inlines.
18456              It seems reasonable to use AT_specification in this case.  */
18457           && !get_AT (old_die, DW_AT_inline))
18458         {
18459           /* Detect and ignore this case, where we are trying to output
18460              something we have already output.  */
18461           return;
18462         }
18463
18464       /* If the definition comes from the same place as the declaration,
18465          maybe use the old DIE.  We always want the DIE for this function
18466          that has the *_pc attributes to be under comp_unit_die so the
18467          debugger can find it.  We also need to do this for abstract
18468          instances of inlines, since the spec requires the out-of-line copy
18469          to have the same parent.  For local class methods, this doesn't
18470          apply; we just use the old DIE.  */
18471       if ((is_cu_die (old_die->die_parent) || context_die == NULL)
18472           && (DECL_ARTIFICIAL (decl)
18473               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18474                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
18475                       == (unsigned) s.line))))
18476         {
18477           subr_die = old_die;
18478
18479           /* Clear out the declaration attribute and the formal parameters.
18480              Do not remove all children, because it is possible that this
18481              declaration die was forced using force_decl_die(). In such
18482              cases die that forced declaration die (e.g. TAG_imported_module)
18483              is one of the children that we do not want to remove.  */
18484           remove_AT (subr_die, DW_AT_declaration);
18485           remove_AT (subr_die, DW_AT_object_pointer);
18486           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18487         }
18488       else
18489         {
18490           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18491           add_AT_specification (subr_die, old_die);
18492           add_pubname (decl, subr_die);
18493           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18494             add_AT_file (subr_die, DW_AT_decl_file, file_index);
18495           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18496             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18497
18498           /* If the prototype had an 'auto' or 'decltype(auto)' return type,
18499              emit the real type on the definition die.  */
18500           if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
18501             {
18502               dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
18503               if (die == auto_die || die == decltype_auto_die)
18504                 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18505                                     TYPE_UNQUALIFIED, context_die);
18506             }
18507         }
18508     }
18509   else
18510     {
18511       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18512
18513       if (TREE_PUBLIC (decl))
18514         add_AT_flag (subr_die, DW_AT_external, 1);
18515
18516       add_name_and_src_coords_attributes (subr_die, decl);
18517       add_pubname (decl, subr_die);
18518       if (debug_info_level > DINFO_LEVEL_TERSE)
18519         {
18520           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18521           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18522                               TYPE_UNQUALIFIED, context_die);
18523         }
18524
18525       add_pure_or_virtual_attribute (subr_die, decl);
18526       if (DECL_ARTIFICIAL (decl))
18527         add_AT_flag (subr_die, DW_AT_artificial, 1);
18528
18529       if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
18530         add_AT_flag (subr_die, DW_AT_noreturn, 1);
18531
18532       add_accessibility_attribute (subr_die, decl);
18533     }
18534
18535   if (declaration)
18536     {
18537       if (!old_die || !get_AT (old_die, DW_AT_inline))
18538         {
18539           add_AT_flag (subr_die, DW_AT_declaration, 1);
18540
18541           /* If this is an explicit function declaration then generate
18542              a DW_AT_explicit attribute.  */
18543           if (lang_hooks.decls.function_decl_explicit_p (decl)
18544               && (dwarf_version >= 3 || !dwarf_strict))
18545             add_AT_flag (subr_die, DW_AT_explicit, 1);
18546
18547           /* If this is a C++11 deleted special function member then generate
18548              a DW_AT_GNU_deleted attribute.  */
18549           if (lang_hooks.decls.function_decl_deleted_p (decl)
18550               && (! dwarf_strict))
18551             add_AT_flag (subr_die, DW_AT_GNU_deleted, 1);
18552
18553           /* The first time we see a member function, it is in the context of
18554              the class to which it belongs.  We make sure of this by emitting
18555              the class first.  The next time is the definition, which is
18556              handled above.  The two may come from the same source text.
18557
18558              Note that force_decl_die() forces function declaration die. It is
18559              later reused to represent definition.  */
18560           equate_decl_number_to_die (decl, subr_die);
18561         }
18562     }
18563   else if (DECL_ABSTRACT_P (decl))
18564     {
18565       if (DECL_DECLARED_INLINE_P (decl))
18566         {
18567           if (cgraph_function_possibly_inlined_p (decl))
18568             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18569           else
18570             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18571         }
18572       else
18573         {
18574           if (cgraph_function_possibly_inlined_p (decl))
18575             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18576           else
18577             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18578         }
18579
18580       if (DECL_DECLARED_INLINE_P (decl)
18581           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18582         add_AT_flag (subr_die, DW_AT_artificial, 1);
18583
18584       equate_decl_number_to_die (decl, subr_die);
18585     }
18586   else if (!DECL_EXTERNAL (decl))
18587     {
18588       HOST_WIDE_INT cfa_fb_offset;
18589       struct function *fun = DECL_STRUCT_FUNCTION (decl);
18590
18591       if (!old_die || !get_AT (old_die, DW_AT_inline))
18592         equate_decl_number_to_die (decl, subr_die);
18593
18594       gcc_checking_assert (fun);
18595       if (!flag_reorder_blocks_and_partition)
18596         {
18597           dw_fde_ref fde = fun->fde;
18598           if (fde->dw_fde_begin)
18599             {
18600               /* We have already generated the labels.  */
18601              add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18602                                  fde->dw_fde_end, false);
18603             }
18604           else
18605             {
18606               /* Create start/end labels and add the range.  */
18607               char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
18608               char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
18609               ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
18610                                            current_function_funcdef_no);
18611               ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
18612                                            current_function_funcdef_no);
18613              add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
18614                                  false);
18615             }
18616
18617 #if VMS_DEBUGGING_INFO
18618       /* HP OpenVMS Industry Standard 64: DWARF Extensions
18619          Section 2.3 Prologue and Epilogue Attributes:
18620          When a breakpoint is set on entry to a function, it is generally
18621          desirable for execution to be suspended, not on the very first
18622          instruction of the function, but rather at a point after the
18623          function's frame has been set up, after any language defined local
18624          declaration processing has been completed, and before execution of
18625          the first statement of the function begins. Debuggers generally
18626          cannot properly determine where this point is.  Similarly for a
18627          breakpoint set on exit from a function. The prologue and epilogue
18628          attributes allow a compiler to communicate the location(s) to use.  */
18629
18630       {
18631         if (fde->dw_fde_vms_end_prologue)
18632           add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18633             fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18634
18635         if (fde->dw_fde_vms_begin_epilogue)
18636           add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18637             fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18638       }
18639 #endif
18640
18641         }
18642       else
18643         {
18644           /* Generate pubnames entries for the split function code ranges.  */
18645           dw_fde_ref fde = fun->fde;
18646
18647           if (fde->dw_fde_second_begin)
18648             {
18649               if (dwarf_version >= 3 || !dwarf_strict)
18650                 {
18651                   /* We should use ranges for non-contiguous code section 
18652                      addresses.  Use the actual code range for the initial
18653                      section, since the HOT/COLD labels might precede an 
18654                      alignment offset.  */
18655                   bool range_list_added = false;
18656                   add_ranges_by_labels (subr_die, fde->dw_fde_begin,
18657                                         fde->dw_fde_end, &range_list_added,
18658                                         false);
18659                   add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
18660                                         fde->dw_fde_second_end,
18661                                        &range_list_added, false);
18662                   if (range_list_added)
18663                     add_ranges (NULL);
18664                 }
18665               else
18666                 {
18667                   /* There is no real support in DW2 for this .. so we make
18668                      a work-around.  First, emit the pub name for the segment
18669                      containing the function label.  Then make and emit a
18670                      simplified subprogram DIE for the second segment with the
18671                      name pre-fixed by __hot/cold_sect_of_.  We use the same
18672                      linkage name for the second die so that gdb will find both
18673                      sections when given "b foo".  */
18674                   const char *name = NULL;
18675                   tree decl_name = DECL_NAME (decl);
18676                   dw_die_ref seg_die;
18677
18678                   /* Do the 'primary' section.   */
18679                   add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18680                                       fde->dw_fde_end, false);
18681
18682                   /* Build a minimal DIE for the secondary section.  */
18683                   seg_die = new_die (DW_TAG_subprogram,
18684                                      subr_die->die_parent, decl);
18685
18686                   if (TREE_PUBLIC (decl))
18687                     add_AT_flag (seg_die, DW_AT_external, 1);
18688
18689                   if (decl_name != NULL 
18690                       && IDENTIFIER_POINTER (decl_name) != NULL)
18691                     {
18692                       name = dwarf2_name (decl, 1);
18693                       if (! DECL_ARTIFICIAL (decl))
18694                         add_src_coords_attributes (seg_die, decl);
18695
18696                       add_linkage_name (seg_die, decl);
18697                     }
18698                   gcc_assert (name != NULL);
18699                   add_pure_or_virtual_attribute (seg_die, decl);
18700                   if (DECL_ARTIFICIAL (decl))
18701                     add_AT_flag (seg_die, DW_AT_artificial, 1);
18702
18703                   name = concat ("__second_sect_of_", name, NULL); 
18704                   add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
18705                                       fde->dw_fde_second_end, false);
18706                   add_name_attribute (seg_die, name);
18707                   if (want_pubnames ())
18708                     add_pubname_string (name, seg_die);
18709                 }
18710             }
18711           else
18712            add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
18713                                false);
18714         }
18715
18716       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
18717
18718       /* We define the "frame base" as the function's CFA.  This is more
18719          convenient for several reasons: (1) It's stable across the prologue
18720          and epilogue, which makes it better than just a frame pointer,
18721          (2) With dwarf3, there exists a one-byte encoding that allows us
18722          to reference the .debug_frame data by proxy, but failing that,
18723          (3) We can at least reuse the code inspection and interpretation
18724          code that determines the CFA position at various points in the
18725          function.  */
18726       if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
18727         {
18728           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
18729           add_AT_loc (subr_die, DW_AT_frame_base, op);
18730         }
18731       else
18732         {
18733           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
18734           if (list->dw_loc_next)
18735             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
18736           else
18737             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
18738         }
18739
18740       /* Compute a displacement from the "steady-state frame pointer" to
18741          the CFA.  The former is what all stack slots and argument slots
18742          will reference in the rtl; the latter is what we've told the
18743          debugger about.  We'll need to adjust all frame_base references
18744          by this displacement.  */
18745       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
18746
18747       if (fun->static_chain_decl)
18748         add_AT_location_description (subr_die, DW_AT_static_link,
18749                  loc_list_from_tree (fun->static_chain_decl, 2, NULL));
18750     }
18751
18752   /* Generate child dies for template paramaters.  */
18753   if (debug_info_level > DINFO_LEVEL_TERSE)
18754     gen_generic_params_dies (decl);
18755
18756   /* Now output descriptions of the arguments for this function. This gets
18757      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
18758      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
18759      `...' at the end of the formal parameter list.  In order to find out if
18760      there was a trailing ellipsis or not, we must instead look at the type
18761      associated with the FUNCTION_DECL.  This will be a node of type
18762      FUNCTION_TYPE. If the chain of type nodes hanging off of this
18763      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
18764      an ellipsis at the end.  */
18765
18766   /* In the case where we are describing a mere function declaration, all we
18767      need to do here (and all we *can* do here) is to describe the *types* of
18768      its formal parameters.  */
18769   if (debug_info_level <= DINFO_LEVEL_TERSE)
18770     ;
18771   else if (declaration)
18772     gen_formal_types_die (decl, subr_die);
18773   else
18774     {
18775       /* Generate DIEs to represent all known formal parameters.  */
18776       tree parm = DECL_ARGUMENTS (decl);
18777       tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
18778       tree generic_decl_parm = generic_decl
18779                                 ? DECL_ARGUMENTS (generic_decl)
18780                                 : NULL;
18781
18782       /* Now we want to walk the list of parameters of the function and
18783          emit their relevant DIEs.
18784
18785          We consider the case of DECL being an instance of a generic function
18786          as well as it being a normal function.
18787
18788          If DECL is an instance of a generic function we walk the
18789          parameters of the generic function declaration _and_ the parameters of
18790          DECL itself. This is useful because we want to emit specific DIEs for
18791          function parameter packs and those are declared as part of the
18792          generic function declaration. In that particular case,
18793          the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
18794          That DIE has children DIEs representing the set of arguments
18795          of the pack. Note that the set of pack arguments can be empty.
18796          In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
18797          children DIE.
18798
18799          Otherwise, we just consider the parameters of DECL.  */
18800       while (generic_decl_parm || parm)
18801         {
18802           if (generic_decl_parm
18803               && lang_hooks.function_parameter_pack_p (generic_decl_parm))
18804             gen_formal_parameter_pack_die (generic_decl_parm,
18805                                            parm, subr_die,
18806                                            &parm);
18807           else if (parm && !POINTER_BOUNDS_P (parm))
18808             {
18809               dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
18810
18811               if (parm == DECL_ARGUMENTS (decl)
18812                   && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
18813                   && parm_die
18814                   && (dwarf_version >= 3 || !dwarf_strict))
18815                 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
18816
18817               parm = DECL_CHAIN (parm);
18818             }
18819           else if (parm)
18820             parm = DECL_CHAIN (parm);
18821
18822           if (generic_decl_parm)
18823             generic_decl_parm = DECL_CHAIN (generic_decl_parm);
18824         }
18825
18826       /* Decide whether we need an unspecified_parameters DIE at the end.
18827          There are 2 more cases to do this for: 1) the ansi ... declaration -
18828          this is detectable when the end of the arg list is not a
18829          void_type_node 2) an unprototyped function declaration (not a
18830          definition).  This just means that we have no info about the
18831          parameters at all.  */
18832       if (prototype_p (TREE_TYPE (decl)))
18833         {
18834           /* This is the prototyped case, check for....  */
18835           if (stdarg_p (TREE_TYPE (decl)))
18836             gen_unspecified_parameters_die (decl, subr_die);
18837         }
18838       else if (DECL_INITIAL (decl) == NULL_TREE)
18839         gen_unspecified_parameters_die (decl, subr_die);
18840     }
18841
18842   /* Output Dwarf info for all of the stuff within the body of the function
18843      (if it has one - it may be just a declaration).  */
18844   outer_scope = DECL_INITIAL (decl);
18845
18846   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
18847      a function.  This BLOCK actually represents the outermost binding contour
18848      for the function, i.e. the contour in which the function's formal
18849      parameters and labels get declared. Curiously, it appears that the front
18850      end doesn't actually put the PARM_DECL nodes for the current function onto
18851      the BLOCK_VARS list for this outer scope, but are strung off of the
18852      DECL_ARGUMENTS list for the function instead.
18853
18854      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
18855      the LABEL_DECL nodes for the function however, and we output DWARF info
18856      for those in decls_for_scope.  Just within the `outer_scope' there will be
18857      a BLOCK node representing the function's outermost pair of curly braces,
18858      and any blocks used for the base and member initializers of a C++
18859      constructor function.  */
18860   if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
18861     {
18862       int call_site_note_count = 0;
18863       int tail_call_site_note_count = 0;
18864
18865       /* Emit a DW_TAG_variable DIE for a named return value.  */
18866       if (DECL_NAME (DECL_RESULT (decl)))
18867         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18868
18869       decls_for_scope (outer_scope, subr_die);
18870
18871       if (call_arg_locations && !dwarf_strict)
18872         {
18873           struct call_arg_loc_node *ca_loc;
18874           for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
18875             {
18876               dw_die_ref die = NULL;
18877               rtx tloc = NULL_RTX, tlocc = NULL_RTX;
18878               rtx arg, next_arg;
18879
18880               for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
18881                    arg; arg = next_arg)
18882                 {
18883                   dw_loc_descr_ref reg, val;
18884                   machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
18885                   dw_die_ref cdie, tdie = NULL;
18886
18887                   next_arg = XEXP (arg, 1);
18888                   if (REG_P (XEXP (XEXP (arg, 0), 0))
18889                       && next_arg
18890                       && MEM_P (XEXP (XEXP (next_arg, 0), 0))
18891                       && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
18892                       && REGNO (XEXP (XEXP (arg, 0), 0))
18893                          == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
18894                     next_arg = XEXP (next_arg, 1);
18895                   if (mode == VOIDmode)
18896                     {
18897                       mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
18898                       if (mode == VOIDmode)
18899                         mode = GET_MODE (XEXP (arg, 0));
18900                     }
18901                   if (mode == VOIDmode || mode == BLKmode)
18902                     continue;
18903                   if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
18904                     {
18905                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18906                       tloc = XEXP (XEXP (arg, 0), 1);
18907                       continue;
18908                     }
18909                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
18910                            && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
18911                     {
18912                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18913                       tlocc = XEXP (XEXP (arg, 0), 1);
18914                       continue;
18915                     }
18916                   reg = NULL;
18917                   if (REG_P (XEXP (XEXP (arg, 0), 0)))
18918                     reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
18919                                               VAR_INIT_STATUS_INITIALIZED);
18920                   else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
18921                     {
18922                       rtx mem = XEXP (XEXP (arg, 0), 0);
18923                       reg = mem_loc_descriptor (XEXP (mem, 0),
18924                                                 get_address_mode (mem),
18925                                                 GET_MODE (mem),
18926                                                 VAR_INIT_STATUS_INITIALIZED);
18927                     }
18928                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
18929                            == DEBUG_PARAMETER_REF)
18930                     {
18931                       tree tdecl
18932                         = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
18933                       tdie = lookup_decl_die (tdecl);
18934                       if (tdie == NULL)
18935                         continue;
18936                     }
18937                   else
18938                     continue;
18939                   if (reg == NULL
18940                       && GET_CODE (XEXP (XEXP (arg, 0), 0))
18941                          != DEBUG_PARAMETER_REF)
18942                     continue;
18943                   val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
18944                                             VOIDmode,
18945                                             VAR_INIT_STATUS_INITIALIZED);
18946                   if (val == NULL)
18947                     continue;
18948                   if (die == NULL)
18949                     die = gen_call_site_die (decl, subr_die, ca_loc);
18950                   cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
18951                                   NULL_TREE);
18952                   if (reg != NULL)
18953                     add_AT_loc (cdie, DW_AT_location, reg);
18954                   else if (tdie != NULL)
18955                     add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
18956                   add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
18957                   if (next_arg != XEXP (arg, 1))
18958                     {
18959                       mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
18960                       if (mode == VOIDmode)
18961                         mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
18962                       val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
18963                                                             0), 1),
18964                                                 mode, VOIDmode,
18965                                                 VAR_INIT_STATUS_INITIALIZED);
18966                       if (val != NULL)
18967                         add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
18968                     }
18969                 }
18970               if (die == NULL
18971                   && (ca_loc->symbol_ref || tloc))
18972                 die = gen_call_site_die (decl, subr_die, ca_loc);
18973               if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
18974                 {
18975                   dw_loc_descr_ref tval = NULL;
18976
18977                   if (tloc != NULL_RTX)
18978                     tval = mem_loc_descriptor (tloc,
18979                                                GET_MODE (tloc) == VOIDmode
18980                                                ? Pmode : GET_MODE (tloc),
18981                                                VOIDmode,
18982                                                VAR_INIT_STATUS_INITIALIZED);
18983                   if (tval)
18984                     add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
18985                   else if (tlocc != NULL_RTX)
18986                     {
18987                       tval = mem_loc_descriptor (tlocc,
18988                                                  GET_MODE (tlocc) == VOIDmode
18989                                                  ? Pmode : GET_MODE (tlocc),
18990                                                  VOIDmode,
18991                                                  VAR_INIT_STATUS_INITIALIZED);
18992                       if (tval)
18993                         add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
18994                                     tval);
18995                     }
18996                 }
18997               if (die != NULL)
18998                 {
18999                   call_site_note_count++;
19000                   if (ca_loc->tail_call_p)
19001                     tail_call_site_note_count++;
19002                 }
19003             }
19004         }
19005       call_arg_locations = NULL;
19006       call_arg_loc_last = NULL;
19007       if (tail_call_site_count >= 0
19008           && tail_call_site_count == tail_call_site_note_count
19009           && !dwarf_strict)
19010         {
19011           if (call_site_count >= 0
19012               && call_site_count == call_site_note_count)
19013             add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
19014           else
19015             add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
19016         }
19017       call_site_count = -1;
19018       tail_call_site_count = -1;
19019     }
19020
19021   if (subr_die != old_die)
19022     /* Add the calling convention attribute if requested.  */
19023     add_calling_convention_attribute (subr_die, decl);
19024 }
19025
19026 /* Returns a hash value for X (which really is a die_struct).  */
19027
19028 hashval_t
19029 block_die_hasher::hash (die_struct *d)
19030 {
19031   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
19032 }
19033
19034 /* Return nonzero if decl_id and die_parent of die_struct X is the same
19035    as decl_id and die_parent of die_struct Y.  */
19036
19037 bool
19038 block_die_hasher::equal (die_struct *x, die_struct *y)
19039 {
19040   return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
19041 }
19042
19043 /* Generate a DIE to represent a declared data object.
19044    Either DECL or ORIGIN must be non-null.  */
19045
19046 static void
19047 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
19048 {
19049   HOST_WIDE_INT off = 0;
19050   tree com_decl;
19051   tree decl_or_origin = decl ? decl : origin;
19052   tree ultimate_origin;
19053   dw_die_ref var_die;
19054   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
19055   dw_die_ref origin_die;
19056   bool declaration = (DECL_EXTERNAL (decl_or_origin)
19057                       || class_or_namespace_scope_p (context_die));
19058   bool specialization_p = false;
19059
19060   ultimate_origin = decl_ultimate_origin (decl_or_origin);
19061   if (decl || ultimate_origin)
19062     origin = ultimate_origin;
19063   com_decl = fortran_common (decl_or_origin, &off);
19064
19065   /* Symbol in common gets emitted as a child of the common block, in the form
19066      of a data member.  */
19067   if (com_decl)
19068     {
19069       dw_die_ref com_die;
19070       dw_loc_list_ref loc;
19071       die_node com_die_arg;
19072
19073       var_die = lookup_decl_die (decl_or_origin);
19074       if (var_die)
19075         {
19076           if (get_AT (var_die, DW_AT_location) == NULL)
19077             {
19078               loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
19079               if (loc)
19080                 {
19081                   if (off)
19082                     {
19083                       /* Optimize the common case.  */
19084                       if (single_element_loc_list_p (loc)
19085                           && loc->expr->dw_loc_opc == DW_OP_addr
19086                           && loc->expr->dw_loc_next == NULL
19087                           && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
19088                              == SYMBOL_REF)
19089                         {
19090                           rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
19091                           loc->expr->dw_loc_oprnd1.v.val_addr
19092                             = plus_constant (GET_MODE (x), x , off);
19093                         }
19094                       else
19095                         loc_list_plus_const (loc, off);
19096                     }
19097                   add_AT_location_description (var_die, DW_AT_location, loc);
19098                   remove_AT (var_die, DW_AT_declaration);
19099                 }
19100             }
19101           return;
19102         }
19103
19104       if (common_block_die_table == NULL)
19105         common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
19106
19107       com_die_arg.decl_id = DECL_UID (com_decl);
19108       com_die_arg.die_parent = context_die;
19109       com_die = common_block_die_table->find (&com_die_arg);
19110       loc = loc_list_from_tree (com_decl, 2, NULL);
19111       if (com_die == NULL)
19112         {
19113           const char *cnam
19114             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
19115           die_node **slot;
19116
19117           com_die = new_die (DW_TAG_common_block, context_die, decl);
19118           add_name_and_src_coords_attributes (com_die, com_decl);
19119           if (loc)
19120             {
19121               add_AT_location_description (com_die, DW_AT_location, loc);
19122               /* Avoid sharing the same loc descriptor between
19123                  DW_TAG_common_block and DW_TAG_variable.  */
19124               loc = loc_list_from_tree (com_decl, 2, NULL);
19125             }
19126           else if (DECL_EXTERNAL (decl))
19127             add_AT_flag (com_die, DW_AT_declaration, 1);
19128           if (want_pubnames ())
19129             add_pubname_string (cnam, com_die); /* ??? needed? */
19130           com_die->decl_id = DECL_UID (com_decl);
19131           slot = common_block_die_table->find_slot (com_die, INSERT);
19132           *slot = com_die;
19133         }
19134       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
19135         {
19136           add_AT_location_description (com_die, DW_AT_location, loc);
19137           loc = loc_list_from_tree (com_decl, 2, NULL);
19138           remove_AT (com_die, DW_AT_declaration);
19139         }
19140       var_die = new_die (DW_TAG_variable, com_die, decl);
19141       add_name_and_src_coords_attributes (var_die, decl);
19142       add_type_attribute (var_die, TREE_TYPE (decl), decl_quals (decl),
19143                           context_die);
19144       add_AT_flag (var_die, DW_AT_external, 1);
19145       if (loc)
19146         {
19147           if (off)
19148             {
19149               /* Optimize the common case.  */
19150               if (single_element_loc_list_p (loc)
19151                   && loc->expr->dw_loc_opc == DW_OP_addr
19152                   && loc->expr->dw_loc_next == NULL
19153                   && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
19154                 {
19155                   rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
19156                   loc->expr->dw_loc_oprnd1.v.val_addr
19157                     = plus_constant (GET_MODE (x), x, off);
19158                 }
19159               else
19160                 loc_list_plus_const (loc, off);
19161             }
19162           add_AT_location_description (var_die, DW_AT_location, loc);
19163         }
19164       else if (DECL_EXTERNAL (decl))
19165         add_AT_flag (var_die, DW_AT_declaration, 1);
19166       equate_decl_number_to_die (decl, var_die);
19167       return;
19168     }
19169
19170   /* If the compiler emitted a definition for the DECL declaration
19171      and if we already emitted a DIE for it, don't emit a second
19172      DIE for it again. Allow re-declarations of DECLs that are
19173      inside functions, though.  */
19174   if (old_die && declaration && !local_scope_p (context_die))
19175     return;
19176
19177   /* For static data members, the declaration in the class is supposed
19178      to have DW_TAG_member tag; the specification should still be
19179      DW_TAG_variable referencing the DW_TAG_member DIE.  */
19180   if (declaration && class_scope_p (context_die))
19181     var_die = new_die (DW_TAG_member, context_die, decl);
19182   else
19183     var_die = new_die (DW_TAG_variable, context_die, decl);
19184
19185   origin_die = NULL;
19186   if (origin != NULL)
19187     origin_die = add_abstract_origin_attribute (var_die, origin);
19188
19189   /* Loop unrolling can create multiple blocks that refer to the same
19190      static variable, so we must test for the DW_AT_declaration flag.
19191
19192      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
19193      copy decls and set the DECL_ABSTRACT_P flag on them instead of
19194      sharing them.
19195
19196      ??? Duplicated blocks have been rewritten to use .debug_ranges.
19197
19198      ??? The declare_in_namespace support causes us to get two DIEs for one
19199      variable, both of which are declarations.  We want to avoid considering
19200      one to be a specification, so we must test that this DIE is not a
19201      declaration.  */
19202   else if (old_die && TREE_STATIC (decl) && ! declaration
19203            && get_AT_flag (old_die, DW_AT_declaration) == 1)
19204     {
19205       /* This is a definition of a C++ class level static.  */
19206       add_AT_specification (var_die, old_die);
19207       specialization_p = true;
19208       if (DECL_NAME (decl))
19209         {
19210           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19211           struct dwarf_file_data * file_index = lookup_filename (s.file);
19212
19213           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19214             add_AT_file (var_die, DW_AT_decl_file, file_index);
19215
19216           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19217             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19218
19219           if (old_die->die_tag == DW_TAG_member)
19220             add_linkage_name (var_die, decl);
19221         }
19222     }
19223   else
19224     add_name_and_src_coords_attributes (var_die, decl);
19225
19226   if ((origin == NULL && !specialization_p)
19227       || (origin != NULL
19228           && !DECL_ABSTRACT_P (decl_or_origin)
19229           && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19230                                        decl_function_context
19231                                                         (decl_or_origin))))
19232     {
19233       tree type = TREE_TYPE (decl_or_origin);
19234
19235       if (decl_by_reference_p (decl_or_origin))
19236         add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19237                             context_die);
19238       else
19239         add_type_attribute (var_die, type, decl_quals (decl_or_origin),
19240                             context_die);
19241     }
19242
19243   if (origin == NULL && !specialization_p)
19244     {
19245       if (TREE_PUBLIC (decl))
19246         add_AT_flag (var_die, DW_AT_external, 1);
19247
19248       if (DECL_ARTIFICIAL (decl))
19249         add_AT_flag (var_die, DW_AT_artificial, 1);
19250
19251       add_accessibility_attribute (var_die, decl);
19252     }
19253
19254   if (declaration)
19255     add_AT_flag (var_die, DW_AT_declaration, 1);
19256
19257   if (decl && (DECL_ABSTRACT_P (decl) || declaration || old_die == NULL))
19258     equate_decl_number_to_die (decl, var_die);
19259
19260   if (! declaration
19261       && (! DECL_ABSTRACT_P (decl_or_origin)
19262           /* Local static vars are shared between all clones/inlines,
19263              so emit DW_AT_location on the abstract DIE if DECL_RTL is
19264              already set.  */
19265           || (TREE_CODE (decl_or_origin) == VAR_DECL
19266               && TREE_STATIC (decl_or_origin)
19267               && DECL_RTL_SET_P (decl_or_origin)))
19268       /* When abstract origin already has DW_AT_location attribute, no need
19269          to add it again.  */
19270       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19271     {
19272       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
19273           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
19274         defer_location (decl_or_origin, var_die);
19275       else
19276         add_location_or_const_value_attribute (var_die, decl_or_origin,
19277                                                decl == NULL, DW_AT_location);
19278       add_pubname (decl_or_origin, var_die);
19279     }
19280   else
19281     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19282 }
19283
19284 /* Generate a DIE to represent a named constant.  */
19285
19286 static void
19287 gen_const_die (tree decl, dw_die_ref context_die)
19288 {
19289   dw_die_ref const_die;
19290   tree type = TREE_TYPE (decl);
19291
19292   const_die = new_die (DW_TAG_constant, context_die, decl);
19293   add_name_and_src_coords_attributes (const_die, decl);
19294   add_type_attribute (const_die, type, TYPE_QUAL_CONST, context_die);
19295   if (TREE_PUBLIC (decl))
19296     add_AT_flag (const_die, DW_AT_external, 1);
19297   if (DECL_ARTIFICIAL (decl))
19298     add_AT_flag (const_die, DW_AT_artificial, 1);
19299   tree_add_const_value_attribute_for_decl (const_die, decl);
19300 }
19301
19302 /* Generate a DIE to represent a label identifier.  */
19303
19304 static void
19305 gen_label_die (tree decl, dw_die_ref context_die)
19306 {
19307   tree origin = decl_ultimate_origin (decl);
19308   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
19309   rtx insn;
19310   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19311
19312   if (origin != NULL)
19313     add_abstract_origin_attribute (lbl_die, origin);
19314   else
19315     add_name_and_src_coords_attributes (lbl_die, decl);
19316
19317   if (DECL_ABSTRACT_P (decl))
19318     equate_decl_number_to_die (decl, lbl_die);
19319   else
19320     {
19321       insn = DECL_RTL_IF_SET (decl);
19322
19323       /* Deleted labels are programmer specified labels which have been
19324          eliminated because of various optimizations.  We still emit them
19325          here so that it is possible to put breakpoints on them.  */
19326       if (insn
19327           && (LABEL_P (insn)
19328               || ((NOTE_P (insn)
19329                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19330         {
19331           /* When optimization is enabled (via -O) some parts of the compiler
19332              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19333              represent source-level labels which were explicitly declared by
19334              the user.  This really shouldn't be happening though, so catch
19335              it if it ever does happen.  */
19336           gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
19337
19338           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19339           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19340         }
19341       else if (insn
19342                && NOTE_P (insn)
19343                && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
19344                && CODE_LABEL_NUMBER (insn) != -1)
19345         {
19346           ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
19347           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19348         }
19349     }
19350 }
19351
19352 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
19353    attributes to the DIE for a block STMT, to describe where the inlined
19354    function was called from.  This is similar to add_src_coords_attributes.  */
19355
19356 static inline void
19357 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19358 {
19359   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19360
19361   if (dwarf_version >= 3 || !dwarf_strict)
19362     {
19363       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19364       add_AT_unsigned (die, DW_AT_call_line, s.line);
19365     }
19366 }
19367
19368
19369 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19370    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
19371
19372 static inline void
19373 add_high_low_attributes (tree stmt, dw_die_ref die)
19374 {
19375   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19376
19377   if (BLOCK_FRAGMENT_CHAIN (stmt)
19378       && (dwarf_version >= 3 || !dwarf_strict))
19379     {
19380       tree chain, superblock = NULL_TREE;
19381       dw_die_ref pdie;
19382       dw_attr_ref attr = NULL;
19383
19384       if (inlined_function_outer_scope_p (stmt))
19385         {
19386           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19387                                        BLOCK_NUMBER (stmt));
19388           add_AT_lbl_id (die, DW_AT_entry_pc, label);
19389         }
19390
19391       /* Optimize duplicate .debug_ranges lists or even tails of
19392          lists.  If this BLOCK has same ranges as its supercontext,
19393          lookup DW_AT_ranges attribute in the supercontext (and
19394          recursively so), verify that the ranges_table contains the
19395          right values and use it instead of adding a new .debug_range.  */
19396       for (chain = stmt, pdie = die;
19397            BLOCK_SAME_RANGE (chain);
19398            chain = BLOCK_SUPERCONTEXT (chain))
19399         {
19400           dw_attr_ref new_attr;
19401
19402           pdie = pdie->die_parent;
19403           if (pdie == NULL)
19404             break;
19405           if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
19406             break;
19407           new_attr = get_AT (pdie, DW_AT_ranges);
19408           if (new_attr == NULL
19409               || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
19410             break;
19411           attr = new_attr;
19412           superblock = BLOCK_SUPERCONTEXT (chain);
19413         }
19414       if (attr != NULL
19415           && (ranges_table[attr->dw_attr_val.v.val_offset
19416                            / 2 / DWARF2_ADDR_SIZE].num
19417               == BLOCK_NUMBER (superblock))
19418           && BLOCK_FRAGMENT_CHAIN (superblock))
19419         {
19420           unsigned long off = attr->dw_attr_val.v.val_offset
19421                               / 2 / DWARF2_ADDR_SIZE;
19422           unsigned long supercnt = 0, thiscnt = 0;
19423           for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
19424                chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19425             {
19426               ++supercnt;
19427               gcc_checking_assert (ranges_table[off + supercnt].num
19428                                    == BLOCK_NUMBER (chain));
19429             }
19430           gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
19431           for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
19432                chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19433             ++thiscnt;
19434           gcc_assert (supercnt >= thiscnt);
19435           add_AT_range_list (die, DW_AT_ranges,
19436                              ((off + supercnt - thiscnt)
19437                               * 2 * DWARF2_ADDR_SIZE),
19438                              false);
19439           return;
19440         }
19441
19442       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
19443
19444       chain = BLOCK_FRAGMENT_CHAIN (stmt);
19445       do
19446         {
19447           add_ranges (chain);
19448           chain = BLOCK_FRAGMENT_CHAIN (chain);
19449         }
19450       while (chain);
19451       add_ranges (NULL);
19452     }
19453   else
19454     {
19455       char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
19456       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19457                                    BLOCK_NUMBER (stmt));
19458       ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
19459                                    BLOCK_NUMBER (stmt));
19460       add_AT_low_high_pc (die, label, label_high, false);
19461     }
19462 }
19463
19464 /* Generate a DIE for a lexical block.  */
19465
19466 static void
19467 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
19468 {
19469   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19470
19471   if (call_arg_locations)
19472     {
19473       if (block_map.length () <= BLOCK_NUMBER (stmt))
19474         block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19475       block_map[BLOCK_NUMBER (stmt)] = stmt_die;
19476     }
19477
19478   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19479     add_high_low_attributes (stmt, stmt_die);
19480
19481   decls_for_scope (stmt, stmt_die);
19482 }
19483
19484 /* Generate a DIE for an inlined subprogram.  */
19485
19486 static void
19487 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
19488 {
19489   tree decl;
19490
19491   /* The instance of function that is effectively being inlined shall not
19492      be abstract.  */
19493   gcc_assert (! BLOCK_ABSTRACT (stmt));
19494
19495   decl = block_ultimate_origin (stmt);
19496
19497   /* Emit info for the abstract instance first, if we haven't yet.  We
19498      must emit this even if the block is abstract, otherwise when we
19499      emit the block below (or elsewhere), we may end up trying to emit
19500      a die whose origin die hasn't been emitted, and crashing.  */
19501   dwarf2out_abstract_function (decl);
19502
19503   if (! BLOCK_ABSTRACT (stmt))
19504     {
19505       dw_die_ref subr_die
19506         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19507
19508       if (call_arg_locations)
19509         {
19510           if (block_map.length () <= BLOCK_NUMBER (stmt))
19511             block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19512           block_map[BLOCK_NUMBER (stmt)] = subr_die;
19513         }
19514       add_abstract_origin_attribute (subr_die, decl);
19515       if (TREE_ASM_WRITTEN (stmt))
19516         add_high_low_attributes (stmt, subr_die);
19517       add_call_src_coords_attributes (stmt, subr_die);
19518
19519       decls_for_scope (stmt, subr_die);
19520     }
19521 }
19522
19523 /* Generate a DIE for a field in a record, or structure.  */
19524
19525 static void
19526 gen_field_die (tree decl, dw_die_ref context_die)
19527 {
19528   dw_die_ref decl_die;
19529
19530   if (TREE_TYPE (decl) == error_mark_node)
19531     return;
19532
19533   decl_die = new_die (DW_TAG_member, context_die, decl);
19534   add_name_and_src_coords_attributes (decl_die, decl);
19535   add_type_attribute (decl_die, member_declared_type (decl),
19536                       decl_quals (decl), context_die);
19537
19538   if (DECL_BIT_FIELD_TYPE (decl))
19539     {
19540       add_byte_size_attribute (decl_die, decl);
19541       add_bit_size_attribute (decl_die, decl);
19542       add_bit_offset_attribute (decl_die, decl);
19543     }
19544
19545   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19546     add_data_member_location_attribute (decl_die, decl);
19547
19548   if (DECL_ARTIFICIAL (decl))
19549     add_AT_flag (decl_die, DW_AT_artificial, 1);
19550
19551   add_accessibility_attribute (decl_die, decl);
19552
19553   /* Equate decl number to die, so that we can look up this decl later on.  */
19554   equate_decl_number_to_die (decl, decl_die);
19555 }
19556
19557 #if 0
19558 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19559    Use modified_type_die instead.
19560    We keep this code here just in case these types of DIEs may be needed to
19561    represent certain things in other languages (e.g. Pascal) someday.  */
19562
19563 static void
19564 gen_pointer_type_die (tree type, dw_die_ref context_die)
19565 {
19566   dw_die_ref ptr_die
19567     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19568
19569   equate_type_number_to_die (type, ptr_die);
19570   add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19571                       context_die);
19572   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19573 }
19574
19575 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19576    Use modified_type_die instead.
19577    We keep this code here just in case these types of DIEs may be needed to
19578    represent certain things in other languages (e.g. Pascal) someday.  */
19579
19580 static void
19581 gen_reference_type_die (tree type, dw_die_ref context_die)
19582 {
19583   dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19584
19585   if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19586     ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19587   else
19588     ref_die = new_die (DW_TAG_reference_type, scope_die, type);
19589
19590   equate_type_number_to_die (type, ref_die);
19591   add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19592                       context_die);
19593   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19594 }
19595 #endif
19596
19597 /* Generate a DIE for a pointer to a member type.  */
19598
19599 static void
19600 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
19601 {
19602   dw_die_ref ptr_die
19603     = new_die (DW_TAG_ptr_to_member_type,
19604                scope_die_for (type, context_die), type);
19605
19606   equate_type_number_to_die (type, ptr_die);
19607   add_AT_die_ref (ptr_die, DW_AT_containing_type,
19608                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
19609   add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19610                       context_die);
19611 }
19612
19613 typedef const char *dchar_p; /* For DEF_VEC_P.  */
19614
19615 static char *producer_string;
19616
19617 /* Return a heap allocated producer string including command line options
19618    if -grecord-gcc-switches.  */
19619
19620 static char *
19621 gen_producer_string (void)
19622 {
19623   size_t j;
19624   auto_vec<dchar_p> switches;
19625   const char *language_string = lang_hooks.name;
19626   char *producer, *tail;
19627   const char *p;
19628   size_t len = dwarf_record_gcc_switches ? 0 : 3;
19629   size_t plen = strlen (language_string) + 1 + strlen (version_string);
19630
19631   for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
19632     switch (save_decoded_options[j].opt_index)
19633       {
19634       case OPT_o:
19635       case OPT_d:
19636       case OPT_dumpbase:
19637       case OPT_dumpdir:
19638       case OPT_auxbase:
19639       case OPT_auxbase_strip:
19640       case OPT_quiet:
19641       case OPT_version:
19642       case OPT_v:
19643       case OPT_w:
19644       case OPT_L:
19645       case OPT_D:
19646       case OPT_I:
19647       case OPT_U:
19648       case OPT_SPECIAL_unknown:
19649       case OPT_SPECIAL_ignore:
19650       case OPT_SPECIAL_program_name:
19651       case OPT_SPECIAL_input_file:
19652       case OPT_grecord_gcc_switches:
19653       case OPT_gno_record_gcc_switches:
19654       case OPT__output_pch_:
19655       case OPT_fdiagnostics_show_location_:
19656       case OPT_fdiagnostics_show_option:
19657       case OPT_fdiagnostics_show_caret:
19658       case OPT_fdiagnostics_color_:
19659       case OPT_fverbose_asm:
19660       case OPT____:
19661       case OPT__sysroot_:
19662       case OPT_nostdinc:
19663       case OPT_nostdinc__:
19664       case OPT_fpreprocessed:
19665       case OPT_fltrans_output_list_:
19666       case OPT_fresolution_:
19667         /* Ignore these.  */
19668         continue;
19669       default:
19670         if (cl_options[save_decoded_options[j].opt_index].flags
19671             & CL_NO_DWARF_RECORD)
19672           continue;
19673         gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
19674                              == '-');
19675         switch (save_decoded_options[j].canonical_option[0][1])
19676           {
19677           case 'M':
19678           case 'i':
19679           case 'W':
19680             continue;
19681           case 'f':
19682             if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
19683                          "dump", 4) == 0)
19684               continue;
19685             break;
19686           default:
19687             break;
19688           }
19689         switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
19690         len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
19691         break;
19692       }
19693
19694   producer = XNEWVEC (char, plen + 1 + len + 1);
19695   tail = producer;
19696   sprintf (tail, "%s %s", language_string, version_string);
19697   tail += plen;
19698
19699   FOR_EACH_VEC_ELT (switches, j, p)
19700     {
19701       len = strlen (p);
19702       *tail = ' ';
19703       memcpy (tail + 1, p, len);
19704       tail += len + 1;
19705     }
19706
19707   *tail = '\0';
19708   return producer;
19709 }
19710
19711 /* Given a C and/or C++ language/version string return the "highest".
19712    C++ is assumed to be "higher" than C in this case.  Used for merging
19713    LTO translation unit languages.  */
19714 static const char *
19715 highest_c_language (const char *lang1, const char *lang2)
19716 {
19717   if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
19718     return "GNU C++14";
19719   if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
19720     return "GNU C++11";
19721   if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
19722     return "GNU C++98";
19723
19724   if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
19725     return "GNU C11";
19726   if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
19727     return "GNU C99";
19728   if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
19729     return "GNU C89";
19730
19731   gcc_unreachable ();
19732 }
19733
19734
19735 /* Generate the DIE for the compilation unit.  */
19736
19737 static dw_die_ref
19738 gen_compile_unit_die (const char *filename)
19739 {
19740   dw_die_ref die;
19741   const char *language_string = lang_hooks.name;
19742   int language;
19743
19744   die = new_die (DW_TAG_compile_unit, NULL, NULL);
19745
19746   if (filename)
19747     {
19748       add_name_attribute (die, filename);
19749       /* Don't add cwd for <built-in>.  */
19750       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19751         add_comp_dir_attribute (die);
19752     }
19753
19754   add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
19755
19756   /* If our producer is LTO try to figure out a common language to use
19757      from the global list of translation units.  */
19758   if (strcmp (language_string, "GNU GIMPLE") == 0)
19759     {
19760       unsigned i;
19761       tree t;
19762       const char *common_lang = NULL;
19763
19764       FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
19765         {
19766           if (!TRANSLATION_UNIT_LANGUAGE (t))
19767             continue;
19768           if (!common_lang)
19769             common_lang = TRANSLATION_UNIT_LANGUAGE (t);
19770           else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
19771             ;
19772           else if (strncmp (common_lang, "GNU C", 5) == 0
19773                     && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
19774             /* Mixing C and C++ is ok, use C++ in that case.  */
19775             common_lang = highest_c_language (common_lang,
19776                                               TRANSLATION_UNIT_LANGUAGE (t));
19777           else
19778             {
19779               /* Fall back to C.  */
19780               common_lang = NULL;
19781               break;
19782             }
19783         }
19784
19785       if (common_lang)
19786         language_string = common_lang;
19787     }
19788
19789   language = DW_LANG_C;
19790   if (strncmp (language_string, "GNU C", 5) == 0
19791       && ISDIGIT (language_string[5]))
19792     {
19793       language = DW_LANG_C89;
19794       if (dwarf_version >= 3 || !dwarf_strict)
19795         {
19796           if (strcmp (language_string, "GNU C89") != 0)
19797             language = DW_LANG_C99;
19798
19799           if (dwarf_version >= 5 /* || !dwarf_strict */)
19800             if (strcmp (language_string, "GNU C11") == 0)
19801               language = DW_LANG_C11;
19802         }
19803     }
19804   else if (strncmp (language_string, "GNU C++", 7) == 0)
19805     {
19806       language = DW_LANG_C_plus_plus;
19807       if (dwarf_version >= 5 /* || !dwarf_strict */)
19808         {
19809           if (strcmp (language_string, "GNU C++11") == 0)
19810             language = DW_LANG_C_plus_plus_11;
19811           else if (strcmp (language_string, "GNU C++14") == 0)
19812             language = DW_LANG_C_plus_plus_14;
19813         }
19814     }
19815   else if (strcmp (language_string, "GNU F77") == 0)
19816     language = DW_LANG_Fortran77;
19817   else if (strcmp (language_string, "GNU Pascal") == 0)
19818     language = DW_LANG_Pascal83;
19819   else if (dwarf_version >= 3 || !dwarf_strict)
19820     {
19821       if (strcmp (language_string, "GNU Ada") == 0)
19822         language = DW_LANG_Ada95;
19823       else if (strncmp (language_string, "GNU Fortran", 11) == 0)
19824         {
19825           language = DW_LANG_Fortran95;
19826           if (dwarf_version >= 5 /* || !dwarf_strict */)
19827             {
19828               if (strcmp (language_string, "GNU Fortran2003") == 0)
19829                 language = DW_LANG_Fortran03;
19830               else if (strcmp (language_string, "GNU Fortran2008") == 0)
19831                 language = DW_LANG_Fortran08;
19832             }
19833         }
19834       else if (strcmp (language_string, "GNU Java") == 0)
19835         language = DW_LANG_Java;
19836       else if (strcmp (language_string, "GNU Objective-C") == 0)
19837         language = DW_LANG_ObjC;
19838       else if (strcmp (language_string, "GNU Objective-C++") == 0)
19839         language = DW_LANG_ObjC_plus_plus;
19840       else if (dwarf_version >= 5 || !dwarf_strict)
19841         {
19842           if (strcmp (language_string, "GNU Go") == 0)
19843             language = DW_LANG_Go;
19844         }
19845     }
19846   /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works.  */
19847   else if (strncmp (language_string, "GNU Fortran", 11) == 0)
19848     language = DW_LANG_Fortran90;
19849
19850   add_AT_unsigned (die, DW_AT_language, language);
19851
19852   switch (language)
19853     {
19854     case DW_LANG_Fortran77:
19855     case DW_LANG_Fortran90:
19856     case DW_LANG_Fortran95:
19857     case DW_LANG_Fortran03:
19858     case DW_LANG_Fortran08:
19859       /* Fortran has case insensitive identifiers and the front-end
19860          lowercases everything.  */
19861       add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19862       break;
19863     default:
19864       /* The default DW_ID_case_sensitive doesn't need to be specified.  */
19865       break;
19866     }
19867   return die;
19868 }
19869
19870 /* Generate the DIE for a base class.  */
19871
19872 static void
19873 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19874 {
19875   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19876
19877   add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, context_die);
19878   add_data_member_location_attribute (die, binfo);
19879
19880   if (BINFO_VIRTUAL_P (binfo))
19881     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19882
19883   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19884      children, otherwise the default is DW_ACCESS_public.  In DWARF2
19885      the default has always been DW_ACCESS_private.  */
19886   if (access == access_public_node)
19887     {
19888       if (dwarf_version == 2
19889           || context_die->die_tag == DW_TAG_class_type)
19890       add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19891     }
19892   else if (access == access_protected_node)
19893     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19894   else if (dwarf_version > 2
19895            && context_die->die_tag != DW_TAG_class_type)
19896     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19897 }
19898
19899 /* Generate a DIE for a class member.  */
19900
19901 static void
19902 gen_member_die (tree type, dw_die_ref context_die)
19903 {
19904   tree member;
19905   tree binfo = TYPE_BINFO (type);
19906   dw_die_ref child;
19907
19908   /* If this is not an incomplete type, output descriptions of each of its
19909      members. Note that as we output the DIEs necessary to represent the
19910      members of this record or union type, we will also be trying to output
19911      DIEs to represent the *types* of those members. However the `type'
19912      function (above) will specifically avoid generating type DIEs for member
19913      types *within* the list of member DIEs for this (containing) type except
19914      for those types (of members) which are explicitly marked as also being
19915      members of this (containing) type themselves.  The g++ front- end can
19916      force any given type to be treated as a member of some other (containing)
19917      type by setting the TYPE_CONTEXT of the given (member) type to point to
19918      the TREE node representing the appropriate (containing) type.  */
19919
19920   /* First output info about the base classes.  */
19921   if (binfo)
19922     {
19923       vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
19924       int i;
19925       tree base;
19926
19927       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19928         gen_inheritance_die (base,
19929                              (accesses ? (*accesses)[i] : access_public_node),
19930                              context_die);
19931     }
19932
19933   /* Now output info about the data members and type members.  */
19934   for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19935     {
19936       /* If we thought we were generating minimal debug info for TYPE
19937          and then changed our minds, some of the member declarations
19938          may have already been defined.  Don't define them again, but
19939          do put them in the right order.  */
19940
19941       child = lookup_decl_die (member);
19942       if (child)
19943         splice_child_die (context_die, child);
19944       else
19945         gen_decl_die (member, NULL, context_die);
19946     }
19947
19948   /* Now output info about the function members (if any).  */
19949   for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19950     {
19951       /* Don't include clones in the member list.  */
19952       if (DECL_ABSTRACT_ORIGIN (member))
19953         continue;
19954
19955       child = lookup_decl_die (member);
19956       if (child)
19957         splice_child_die (context_die, child);
19958       else
19959         gen_decl_die (member, NULL, context_die);
19960     }
19961 }
19962
19963 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
19964    is set, we pretend that the type was never defined, so we only get the
19965    member DIEs needed by later specification DIEs.  */
19966
19967 static void
19968 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19969                                 enum debug_info_usage usage)
19970 {
19971   dw_die_ref type_die = lookup_type_die (type);
19972   dw_die_ref scope_die = 0;
19973   int nested = 0;
19974   int complete = (TYPE_SIZE (type)
19975                   && (! TYPE_STUB_DECL (type)
19976                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19977   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19978   complete = complete && should_emit_struct_debug (type, usage);
19979
19980   if (type_die && ! complete)
19981     return;
19982
19983   if (TYPE_CONTEXT (type) != NULL_TREE
19984       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19985           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19986     nested = 1;
19987
19988   scope_die = scope_die_for (type, context_die);
19989
19990   /* Generate child dies for template paramaters.  */
19991   if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
19992     schedule_generic_params_dies_gen (type);
19993
19994   if (! type_die || (nested && is_cu_die (scope_die)))
19995     /* First occurrence of type or toplevel definition of nested class.  */
19996     {
19997       dw_die_ref old_die = type_die;
19998
19999       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
20000                           ? record_type_tag (type) : DW_TAG_union_type,
20001                           scope_die, type);
20002       equate_type_number_to_die (type, type_die);
20003       if (old_die)
20004         add_AT_specification (type_die, old_die);
20005       else
20006         add_name_attribute (type_die, type_tag (type));
20007     }
20008   else
20009     remove_AT (type_die, DW_AT_declaration);
20010
20011   /* If this type has been completed, then give it a byte_size attribute and
20012      then give a list of members.  */
20013   if (complete && !ns_decl)
20014     {
20015       /* Prevent infinite recursion in cases where the type of some member of
20016          this type is expressed in terms of this type itself.  */
20017       TREE_ASM_WRITTEN (type) = 1;
20018       add_byte_size_attribute (type_die, type);
20019       if (TYPE_STUB_DECL (type) != NULL_TREE)
20020         {
20021           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
20022           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
20023         }
20024
20025       /* If the first reference to this type was as the return type of an
20026          inline function, then it may not have a parent.  Fix this now.  */
20027       if (type_die->die_parent == NULL)
20028         add_child_die (scope_die, type_die);
20029
20030       push_decl_scope (type);
20031       gen_member_die (type, type_die);
20032       pop_decl_scope ();
20033
20034       add_gnat_descriptive_type_attribute (type_die, type, context_die);
20035       if (TYPE_ARTIFICIAL (type))
20036         add_AT_flag (type_die, DW_AT_artificial, 1);
20037
20038       /* GNU extension: Record what type our vtable lives in.  */
20039       if (TYPE_VFIELD (type))
20040         {
20041           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
20042
20043           gen_type_die (vtype, context_die);
20044           add_AT_die_ref (type_die, DW_AT_containing_type,
20045                           lookup_type_die (vtype));
20046         }
20047     }
20048   else
20049     {
20050       add_AT_flag (type_die, DW_AT_declaration, 1);
20051
20052       /* We don't need to do this for function-local types.  */
20053       if (TYPE_STUB_DECL (type)
20054           && ! decl_function_context (TYPE_STUB_DECL (type)))
20055         vec_safe_push (incomplete_types, type);
20056     }
20057
20058   if (get_AT (type_die, DW_AT_name))
20059     add_pubtype (type, type_die);
20060 }
20061
20062 /* Generate a DIE for a subroutine _type_.  */
20063
20064 static void
20065 gen_subroutine_type_die (tree type, dw_die_ref context_die)
20066 {
20067   tree return_type = TREE_TYPE (type);
20068   dw_die_ref subr_die
20069     = new_die (DW_TAG_subroutine_type,
20070                scope_die_for (type, context_die), type);
20071
20072   equate_type_number_to_die (type, subr_die);
20073   add_prototyped_attribute (subr_die, type);
20074   add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, context_die);
20075   gen_formal_types_die (type, subr_die);
20076
20077   if (get_AT (subr_die, DW_AT_name))
20078     add_pubtype (type, subr_die);
20079 }
20080
20081 /* Generate a DIE for a type definition.  */
20082
20083 static void
20084 gen_typedef_die (tree decl, dw_die_ref context_die)
20085 {
20086   dw_die_ref type_die;
20087   tree origin;
20088
20089   if (TREE_ASM_WRITTEN (decl))
20090     return;
20091
20092   TREE_ASM_WRITTEN (decl) = 1;
20093   type_die = new_die (DW_TAG_typedef, context_die, decl);
20094   origin = decl_ultimate_origin (decl);
20095   if (origin != NULL)
20096     add_abstract_origin_attribute (type_die, origin);
20097   else
20098     {
20099       tree type;
20100
20101       add_name_and_src_coords_attributes (type_die, decl);
20102       if (DECL_ORIGINAL_TYPE (decl))
20103         {
20104           type = DECL_ORIGINAL_TYPE (decl);
20105
20106           gcc_assert (type != TREE_TYPE (decl));
20107           equate_type_number_to_die (TREE_TYPE (decl), type_die);
20108         }
20109       else
20110         {
20111           type = TREE_TYPE (decl);
20112
20113           if (is_naming_typedef_decl (TYPE_NAME (type)))
20114             {
20115               /* Here, we are in the case of decl being a typedef naming
20116                  an anonymous type, e.g:
20117                      typedef struct {...} foo;
20118                  In that case TREE_TYPE (decl) is not a typedef variant
20119                  type and TYPE_NAME of the anonymous type is set to the
20120                  TYPE_DECL of the typedef. This construct is emitted by
20121                  the C++ FE.
20122
20123                  TYPE is the anonymous struct named by the typedef
20124                  DECL. As we need the DW_AT_type attribute of the
20125                  DW_TAG_typedef to point to the DIE of TYPE, let's
20126                  generate that DIE right away. add_type_attribute
20127                  called below will then pick (via lookup_type_die) that
20128                  anonymous struct DIE.  */
20129               if (!TREE_ASM_WRITTEN (type))
20130                 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
20131
20132               /* This is a GNU Extension.  We are adding a
20133                  DW_AT_linkage_name attribute to the DIE of the
20134                  anonymous struct TYPE.  The value of that attribute
20135                  is the name of the typedef decl naming the anonymous
20136                  struct.  This greatly eases the work of consumers of
20137                  this debug info.  */
20138               add_linkage_attr (lookup_type_die (type), decl);
20139             }
20140         }
20141
20142       add_type_attribute (type_die, type, decl_quals (decl), context_die);
20143
20144       if (is_naming_typedef_decl (decl))
20145         /* We want that all subsequent calls to lookup_type_die with
20146            TYPE in argument yield the DW_TAG_typedef we have just
20147            created.  */
20148         equate_type_number_to_die (type, type_die);
20149
20150       add_accessibility_attribute (type_die, decl);
20151     }
20152
20153   if (DECL_ABSTRACT_P (decl))
20154     equate_decl_number_to_die (decl, type_die);
20155
20156   if (get_AT (type_die, DW_AT_name))
20157     add_pubtype (decl, type_die);
20158 }
20159
20160 /* Generate a DIE for a struct, class, enum or union type.  */
20161
20162 static void
20163 gen_tagged_type_die (tree type,
20164                      dw_die_ref context_die,
20165                      enum debug_info_usage usage)
20166 {
20167   int need_pop;
20168
20169   if (type == NULL_TREE
20170       || !is_tagged_type (type))
20171     return;
20172
20173   /* If this is a nested type whose containing class hasn't been written
20174      out yet, writing it out will cover this one, too.  This does not apply
20175      to instantiations of member class templates; they need to be added to
20176      the containing class as they are generated.  FIXME: This hurts the
20177      idea of combining type decls from multiple TUs, since we can't predict
20178      what set of template instantiations we'll get.  */
20179   if (TYPE_CONTEXT (type)
20180       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20181       && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
20182     {
20183       gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
20184
20185       if (TREE_ASM_WRITTEN (type))
20186         return;
20187
20188       /* If that failed, attach ourselves to the stub.  */
20189       push_decl_scope (TYPE_CONTEXT (type));
20190       context_die = lookup_type_die (TYPE_CONTEXT (type));
20191       need_pop = 1;
20192     }
20193   else if (TYPE_CONTEXT (type) != NULL_TREE
20194            && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
20195     {
20196       /* If this type is local to a function that hasn't been written
20197          out yet, use a NULL context for now; it will be fixed up in
20198          decls_for_scope.  */
20199       context_die = lookup_decl_die (TYPE_CONTEXT (type));
20200       /* A declaration DIE doesn't count; nested types need to go in the
20201          specification.  */
20202       if (context_die && is_declaration_die (context_die))
20203         context_die = NULL;
20204       need_pop = 0;
20205     }
20206   else
20207     {
20208       context_die = declare_in_namespace (type, context_die);
20209       need_pop = 0;
20210     }
20211
20212   if (TREE_CODE (type) == ENUMERAL_TYPE)
20213     {
20214       /* This might have been written out by the call to
20215          declare_in_namespace.  */
20216       if (!TREE_ASM_WRITTEN (type))
20217         gen_enumeration_type_die (type, context_die);
20218     }
20219   else
20220     gen_struct_or_union_type_die (type, context_die, usage);
20221
20222   if (need_pop)
20223     pop_decl_scope ();
20224
20225   /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
20226      it up if it is ever completed.  gen_*_type_die will set it for us
20227      when appropriate.  */
20228 }
20229
20230 /* Generate a type description DIE.  */
20231
20232 static void
20233 gen_type_die_with_usage (tree type, dw_die_ref context_die,
20234                          enum debug_info_usage usage)
20235 {
20236   struct array_descr_info info;
20237
20238   if (type == NULL_TREE || type == error_mark_node)
20239     return;
20240
20241 #ifdef ENABLE_CHECKING
20242   if (type)
20243      verify_type (type);
20244 #endif
20245
20246   if (TYPE_NAME (type) != NULL_TREE
20247       && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20248       && is_redundant_typedef (TYPE_NAME (type))
20249       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
20250     /* The DECL of this type is a typedef we don't want to emit debug
20251        info for but we want debug info for its underlying typedef.
20252        This can happen for e.g, the injected-class-name of a C++
20253        type.  */
20254     type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
20255
20256   /* If TYPE is a typedef type variant, let's generate debug info
20257      for the parent typedef which TYPE is a type of.  */
20258   if (typedef_variant_p (type))
20259     {
20260       if (TREE_ASM_WRITTEN (type))
20261         return;
20262
20263       /* Prevent broken recursion; we can't hand off to the same type.  */
20264       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
20265
20266       /* Give typedefs the right scope.  */
20267       context_die = scope_die_for (type, context_die);
20268
20269       TREE_ASM_WRITTEN (type) = 1;
20270
20271       gen_decl_die (TYPE_NAME (type), NULL, context_die);
20272       return;
20273     }
20274
20275   /* If type is an anonymous tagged type named by a typedef, let's
20276      generate debug info for the typedef.  */
20277   if (is_naming_typedef_decl (TYPE_NAME (type)))
20278     {
20279       /* Use the DIE of the containing namespace as the parent DIE of
20280          the type description DIE we want to generate.  */
20281       if (DECL_CONTEXT (TYPE_NAME (type))
20282           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20283         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20284       
20285       gen_decl_die (TYPE_NAME (type), NULL, context_die);
20286       return;
20287     }
20288
20289   /* We are going to output a DIE to represent the unqualified version
20290      of this type (i.e. without any const or volatile qualifiers) so
20291      get the main variant (i.e. the unqualified version) of this type
20292      now.  (Vectors are special because the debugging info is in the
20293      cloned type itself).  */
20294   if (TREE_CODE (type) != VECTOR_TYPE)
20295     type = type_main_variant (type);
20296
20297   /* If this is an array type with hidden descriptor, handle it first.  */
20298   if (!TREE_ASM_WRITTEN (type)
20299       && lang_hooks.types.get_array_descr_info)
20300     {
20301       memset (&info, 0, sizeof (info));
20302       if (lang_hooks.types.get_array_descr_info (type, &info))
20303         {
20304           gen_descr_array_type_die (type, &info, context_die);
20305           TREE_ASM_WRITTEN (type) = 1;
20306           return;
20307         }
20308     }
20309
20310   if (TREE_ASM_WRITTEN (type))
20311     return;
20312
20313   switch (TREE_CODE (type))
20314     {
20315     case ERROR_MARK:
20316       break;
20317
20318     case POINTER_TYPE:
20319     case REFERENCE_TYPE:
20320       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
20321          ensures that the gen_type_die recursion will terminate even if the
20322          type is recursive.  Recursive types are possible in Ada.  */
20323       /* ??? We could perhaps do this for all types before the switch
20324          statement.  */
20325       TREE_ASM_WRITTEN (type) = 1;
20326
20327       /* For these types, all that is required is that we output a DIE (or a
20328          set of DIEs) to represent the "basis" type.  */
20329       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20330                                 DINFO_USAGE_IND_USE);
20331       break;
20332
20333     case OFFSET_TYPE:
20334       /* This code is used for C++ pointer-to-data-member types.
20335          Output a description of the relevant class type.  */
20336       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
20337                                         DINFO_USAGE_IND_USE);
20338
20339       /* Output a description of the type of the object pointed to.  */
20340       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20341                                         DINFO_USAGE_IND_USE);
20342
20343       /* Now output a DIE to represent this pointer-to-data-member type
20344          itself.  */
20345       gen_ptr_to_mbr_type_die (type, context_die);
20346       break;
20347
20348     case FUNCTION_TYPE:
20349       /* Force out return type (in case it wasn't forced out already).  */
20350       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20351                                         DINFO_USAGE_DIR_USE);
20352       gen_subroutine_type_die (type, context_die);
20353       break;
20354
20355     case METHOD_TYPE:
20356       /* Force out return type (in case it wasn't forced out already).  */
20357       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20358                                         DINFO_USAGE_DIR_USE);
20359       gen_subroutine_type_die (type, context_die);
20360       break;
20361
20362     case ARRAY_TYPE:
20363       gen_array_type_die (type, context_die);
20364       break;
20365
20366     case VECTOR_TYPE:
20367       gen_array_type_die (type, context_die);
20368       break;
20369
20370     case ENUMERAL_TYPE:
20371     case RECORD_TYPE:
20372     case UNION_TYPE:
20373     case QUAL_UNION_TYPE:
20374       gen_tagged_type_die (type, context_die, usage);
20375       return;
20376
20377     case VOID_TYPE:
20378     case INTEGER_TYPE:
20379     case REAL_TYPE:
20380     case FIXED_POINT_TYPE:
20381     case COMPLEX_TYPE:
20382     case BOOLEAN_TYPE:
20383     case POINTER_BOUNDS_TYPE:
20384       /* No DIEs needed for fundamental types.  */
20385       break;
20386
20387     case NULLPTR_TYPE:
20388     case LANG_TYPE:
20389       /* Just use DW_TAG_unspecified_type.  */
20390       {
20391         dw_die_ref type_die = lookup_type_die (type);
20392         if (type_die == NULL)
20393           {
20394             tree name = TYPE_IDENTIFIER (type);
20395             type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
20396                                 type);
20397             add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20398             equate_type_number_to_die (type, type_die);
20399           }
20400       }
20401       break;
20402
20403     default:
20404       if (is_cxx_auto (type))
20405         {
20406           tree name = TYPE_IDENTIFIER (type);
20407           dw_die_ref *die = (name == get_identifier ("auto")
20408                              ? &auto_die : &decltype_auto_die);
20409           if (!*die)
20410             {
20411               *die = new_die (DW_TAG_unspecified_type,
20412                               comp_unit_die (), NULL_TREE);
20413               add_name_attribute (*die, IDENTIFIER_POINTER (name));
20414             }
20415           equate_type_number_to_die (type, *die);
20416           break;
20417         }
20418       gcc_unreachable ();
20419     }
20420
20421   TREE_ASM_WRITTEN (type) = 1;
20422 }
20423
20424 static void
20425 gen_type_die (tree type, dw_die_ref context_die)
20426 {
20427   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20428 }
20429
20430 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20431    things which are local to the given block.  */
20432
20433 static void
20434 gen_block_die (tree stmt, dw_die_ref context_die)
20435 {
20436   int must_output_die = 0;
20437   bool inlined_func;
20438
20439   /* Ignore blocks that are NULL.  */
20440   if (stmt == NULL_TREE)
20441     return;
20442
20443   inlined_func = inlined_function_outer_scope_p (stmt);
20444
20445   /* If the block is one fragment of a non-contiguous block, do not
20446      process the variables, since they will have been done by the
20447      origin block.  Do process subblocks.  */
20448   if (BLOCK_FRAGMENT_ORIGIN (stmt))
20449     {
20450       tree sub;
20451
20452       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20453         gen_block_die (sub, context_die);
20454
20455       return;
20456     }
20457
20458   /* Determine if we need to output any Dwarf DIEs at all to represent this
20459      block.  */
20460   if (inlined_func)
20461     /* The outer scopes for inlinings *must* always be represented.  We
20462        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
20463     must_output_die = 1;
20464   else
20465     {
20466       /* Determine if this block directly contains any "significant"
20467          local declarations which we will need to output DIEs for.  */
20468       if (debug_info_level > DINFO_LEVEL_TERSE)
20469         /* We are not in terse mode so *any* local declaration counts
20470            as being a "significant" one.  */
20471         must_output_die = ((BLOCK_VARS (stmt) != NULL
20472                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20473                            && (TREE_USED (stmt)
20474                                || TREE_ASM_WRITTEN (stmt)
20475                                || BLOCK_ABSTRACT (stmt)));
20476       else if ((TREE_USED (stmt)
20477                 || TREE_ASM_WRITTEN (stmt)
20478                 || BLOCK_ABSTRACT (stmt))
20479                && !dwarf2out_ignore_block (stmt))
20480         must_output_die = 1;
20481     }
20482
20483   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20484      DIE for any block which contains no significant local declarations at
20485      all.  Rather, in such cases we just call `decls_for_scope' so that any
20486      needed Dwarf info for any sub-blocks will get properly generated. Note
20487      that in terse mode, our definition of what constitutes a "significant"
20488      local declaration gets restricted to include only inlined function
20489      instances and local (nested) function definitions.  */
20490   if (must_output_die)
20491     {
20492       if (inlined_func)
20493         {
20494           /* If STMT block is abstract, that means we have been called
20495              indirectly from dwarf2out_abstract_function.
20496              That function rightfully marks the descendent blocks (of
20497              the abstract function it is dealing with) as being abstract,
20498              precisely to prevent us from emitting any
20499              DW_TAG_inlined_subroutine DIE as a descendent
20500              of an abstract function instance. So in that case, we should
20501              not call gen_inlined_subroutine_die.
20502
20503              Later though, when cgraph asks dwarf2out to emit info
20504              for the concrete instance of the function decl into which
20505              the concrete instance of STMT got inlined, the later will lead
20506              to the generation of a DW_TAG_inlined_subroutine DIE.  */
20507           if (! BLOCK_ABSTRACT (stmt))
20508             gen_inlined_subroutine_die (stmt, context_die);
20509         }
20510       else
20511         gen_lexical_block_die (stmt, context_die);
20512     }
20513   else
20514     decls_for_scope (stmt, context_die);
20515 }
20516
20517 /* Process variable DECL (or variable with origin ORIGIN) within
20518    block STMT and add it to CONTEXT_DIE.  */
20519 static void
20520 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20521 {
20522   dw_die_ref die;
20523   tree decl_or_origin = decl ? decl : origin;
20524
20525   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20526     die = lookup_decl_die (decl_or_origin);
20527   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20528            && TYPE_DECL_IS_STUB (decl_or_origin))
20529     die = lookup_type_die (TREE_TYPE (decl_or_origin));
20530   else
20531     die = NULL;
20532
20533   if (die != NULL && die->die_parent == NULL)
20534     add_child_die (context_die, die);
20535   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20536     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20537                                          stmt, context_die);
20538   else
20539     gen_decl_die (decl, origin, context_die);
20540 }
20541
20542 /* Generate all of the decls declared within a given scope and (recursively)
20543    all of its sub-blocks.  */
20544
20545 static void
20546 decls_for_scope (tree stmt, dw_die_ref context_die)
20547 {
20548   tree decl;
20549   unsigned int i;
20550   tree subblocks;
20551
20552   /* Ignore NULL blocks.  */
20553   if (stmt == NULL_TREE)
20554     return;
20555
20556   /* Output the DIEs to represent all of the data objects and typedefs
20557      declared directly within this block but not within any nested
20558      sub-blocks.  Also, nested function and tag DIEs have been
20559      generated with a parent of NULL; fix that up now.  We don't
20560      have to do this if we're at -g1.  */
20561   if (debug_info_level > DINFO_LEVEL_TERSE)
20562     {
20563       for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20564         process_scope_var (stmt, decl, NULL_TREE, context_die);
20565       for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20566         process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20567                            context_die);
20568     }
20569
20570   /* Even if we're at -g1, we need to process the subblocks in order to get
20571      inlined call information.  */
20572
20573   /* Output the DIEs to represent all sub-blocks (and the items declared
20574      therein) of this block.  */
20575   for (subblocks = BLOCK_SUBBLOCKS (stmt);
20576        subblocks != NULL;
20577        subblocks = BLOCK_CHAIN (subblocks))
20578     gen_block_die (subblocks, context_die);
20579 }
20580
20581 /* Is this a typedef we can avoid emitting?  */
20582
20583 static inline int
20584 is_redundant_typedef (const_tree decl)
20585 {
20586   if (TYPE_DECL_IS_STUB (decl))
20587     return 1;
20588
20589   if (DECL_ARTIFICIAL (decl)
20590       && DECL_CONTEXT (decl)
20591       && is_tagged_type (DECL_CONTEXT (decl))
20592       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20593       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20594     /* Also ignore the artificial member typedef for the class name.  */
20595     return 1;
20596
20597   return 0;
20598 }
20599
20600 /* Return TRUE if TYPE is a typedef that names a type for linkage
20601    purposes. This kind of typedefs is produced by the C++ FE for
20602    constructs like:
20603
20604    typedef struct {...} foo;
20605
20606    In that case, there is no typedef variant type produced for foo.
20607    Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20608    struct type.  */
20609
20610 static bool
20611 is_naming_typedef_decl (const_tree decl)
20612 {
20613   if (decl == NULL_TREE
20614       || TREE_CODE (decl) != TYPE_DECL
20615       || !is_tagged_type (TREE_TYPE (decl))
20616       || DECL_IS_BUILTIN (decl)
20617       || is_redundant_typedef (decl)
20618       /* It looks like Ada produces TYPE_DECLs that are very similar
20619          to C++ naming typedefs but that have different
20620          semantics. Let's be specific to c++ for now.  */
20621       || !is_cxx ())
20622     return FALSE;
20623
20624   return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20625           && TYPE_NAME (TREE_TYPE (decl)) == decl
20626           && (TYPE_STUB_DECL (TREE_TYPE (decl))
20627               != TYPE_NAME (TREE_TYPE (decl))));
20628 }
20629
20630 /* Returns the DIE for a context.  */
20631
20632 static inline dw_die_ref
20633 get_context_die (tree context)
20634 {
20635   if (context)
20636     {
20637       /* Find die that represents this context.  */
20638       if (TYPE_P (context))
20639         {
20640           context = TYPE_MAIN_VARIANT (context);
20641           return strip_naming_typedef (context, force_type_die (context));
20642         }
20643       else
20644         return force_decl_die (context);
20645     }
20646   return comp_unit_die ();
20647 }
20648
20649 /* Returns the DIE for decl.  A DIE will always be returned.  */
20650
20651 static dw_die_ref
20652 force_decl_die (tree decl)
20653 {
20654   dw_die_ref decl_die;
20655   unsigned saved_external_flag;
20656   tree save_fn = NULL_TREE;
20657   decl_die = lookup_decl_die (decl);
20658   if (!decl_die)
20659     {
20660       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20661
20662       decl_die = lookup_decl_die (decl);
20663       if (decl_die)
20664         return decl_die;
20665
20666       switch (TREE_CODE (decl))
20667         {
20668         case FUNCTION_DECL:
20669           /* Clear current_function_decl, so that gen_subprogram_die thinks
20670              that this is a declaration. At this point, we just want to force
20671              declaration die.  */
20672           save_fn = current_function_decl;
20673           current_function_decl = NULL_TREE;
20674           gen_subprogram_die (decl, context_die);
20675           current_function_decl = save_fn;
20676           break;
20677
20678         case VAR_DECL:
20679           /* Set external flag to force declaration die. Restore it after
20680            gen_decl_die() call.  */
20681           saved_external_flag = DECL_EXTERNAL (decl);
20682           DECL_EXTERNAL (decl) = 1;
20683           gen_decl_die (decl, NULL, context_die);
20684           DECL_EXTERNAL (decl) = saved_external_flag;
20685           break;
20686
20687         case NAMESPACE_DECL:
20688           if (dwarf_version >= 3 || !dwarf_strict)
20689             dwarf2out_decl (decl);
20690           else
20691             /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
20692             decl_die = comp_unit_die ();
20693           break;
20694
20695         case TRANSLATION_UNIT_DECL:
20696           decl_die = comp_unit_die ();
20697           break;
20698
20699         default:
20700           gcc_unreachable ();
20701         }
20702
20703       /* We should be able to find the DIE now.  */
20704       if (!decl_die)
20705         decl_die = lookup_decl_die (decl);
20706       gcc_assert (decl_die);
20707     }
20708
20709   return decl_die;
20710 }
20711
20712 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
20713    always returned.  */
20714
20715 static dw_die_ref
20716 force_type_die (tree type)
20717 {
20718   dw_die_ref type_die;
20719
20720   type_die = lookup_type_die (type);
20721   if (!type_die)
20722     {
20723       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
20724
20725       type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
20726                                     context_die);
20727       gcc_assert (type_die);
20728     }
20729   return type_die;
20730 }
20731
20732 /* Force out any required namespaces to be able to output DECL,
20733    and return the new context_die for it, if it's changed.  */
20734
20735 static dw_die_ref
20736 setup_namespace_context (tree thing, dw_die_ref context_die)
20737 {
20738   tree context = (DECL_P (thing)
20739                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
20740   if (context && TREE_CODE (context) == NAMESPACE_DECL)
20741     /* Force out the namespace.  */
20742     context_die = force_decl_die (context);
20743
20744   return context_die;
20745 }
20746
20747 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
20748    type) within its namespace, if appropriate.
20749
20750    For compatibility with older debuggers, namespace DIEs only contain
20751    declarations; all definitions are emitted at CU scope.  */
20752
20753 static dw_die_ref
20754 declare_in_namespace (tree thing, dw_die_ref context_die)
20755 {
20756   dw_die_ref ns_context;
20757
20758   if (debug_info_level <= DINFO_LEVEL_TERSE)
20759     return context_die;
20760
20761   /* External declarations in the local scope only need to be emitted
20762      once, not once in the namespace and once in the scope.
20763
20764      This avoids declaring the `extern' below in the
20765      namespace DIE as well as in the innermost scope:
20766
20767           namespace S
20768           {
20769             int i=5;
20770             int foo()
20771             {
20772               int i=8;
20773               extern int i;
20774               return i;
20775             }
20776           }
20777   */
20778   if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
20779     return context_die;
20780
20781   /* If this decl is from an inlined function, then don't try to emit it in its
20782      namespace, as we will get confused.  It would have already been emitted
20783      when the abstract instance of the inline function was emitted anyways.  */
20784   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
20785     return context_die;
20786
20787   ns_context = setup_namespace_context (thing, context_die);
20788
20789   if (ns_context != context_die)
20790     {
20791       if (is_fortran ())
20792         return ns_context;
20793       if (DECL_P (thing))
20794         gen_decl_die (thing, NULL, ns_context);
20795       else
20796         gen_type_die (thing, ns_context);
20797     }
20798   return context_die;
20799 }
20800
20801 /* Generate a DIE for a namespace or namespace alias.  */
20802
20803 static void
20804 gen_namespace_die (tree decl, dw_die_ref context_die)
20805 {
20806   dw_die_ref namespace_die;
20807
20808   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
20809      they are an alias of.  */
20810   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
20811     {
20812       /* Output a real namespace or module.  */
20813       context_die = setup_namespace_context (decl, comp_unit_die ());
20814       namespace_die = new_die (is_fortran ()
20815                                ? DW_TAG_module : DW_TAG_namespace,
20816                                context_die, decl);
20817       /* For Fortran modules defined in different CU don't add src coords.  */
20818       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20819         {
20820           const char *name = dwarf2_name (decl, 0);
20821           if (name)
20822             add_name_attribute (namespace_die, name);
20823         }
20824       else
20825         add_name_and_src_coords_attributes (namespace_die, decl);
20826       if (DECL_EXTERNAL (decl))
20827         add_AT_flag (namespace_die, DW_AT_declaration, 1);
20828       equate_decl_number_to_die (decl, namespace_die);
20829     }
20830   else
20831     {
20832       /* Output a namespace alias.  */
20833
20834       /* Force out the namespace we are an alias of, if necessary.  */
20835       dw_die_ref origin_die
20836         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20837
20838       if (DECL_FILE_SCOPE_P (decl)
20839           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20840         context_die = setup_namespace_context (decl, comp_unit_die ());
20841       /* Now create the namespace alias DIE.  */
20842       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20843       add_name_and_src_coords_attributes (namespace_die, decl);
20844       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20845       equate_decl_number_to_die (decl, namespace_die);
20846     }
20847   /* Bypass dwarf2_name's check for DECL_NAMELESS.  */
20848   if (want_pubnames ())
20849     add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
20850 }
20851
20852 /* Generate Dwarf debug information for a decl described by DECL.
20853    The return value is currently only meaningful for PARM_DECLs,
20854    for all other decls it returns NULL.  */
20855
20856 static dw_die_ref
20857 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20858 {
20859   tree decl_or_origin = decl ? decl : origin;
20860   tree class_origin = NULL, ultimate_origin;
20861
20862   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20863     return NULL;
20864
20865   /* Ignore pointer bounds decls.  */
20866   if (DECL_P (decl_or_origin)
20867       && TREE_TYPE (decl_or_origin)
20868       && POINTER_BOUNDS_P (decl_or_origin))
20869     return NULL;
20870
20871   switch (TREE_CODE (decl_or_origin))
20872     {
20873     case ERROR_MARK:
20874       break;
20875
20876     case CONST_DECL:
20877       if (!is_fortran () && !is_ada ())
20878         {
20879           /* The individual enumerators of an enum type get output when we output
20880              the Dwarf representation of the relevant enum type itself.  */
20881           break;
20882         }
20883
20884       /* Emit its type.  */
20885       gen_type_die (TREE_TYPE (decl), context_die);
20886
20887       /* And its containing namespace.  */
20888       context_die = declare_in_namespace (decl, context_die);
20889
20890       gen_const_die (decl, context_die);
20891       break;
20892
20893     case FUNCTION_DECL:
20894       /* Don't output any DIEs to represent mere function declarations,
20895          unless they are class members or explicit block externs.  */
20896       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20897           && DECL_FILE_SCOPE_P (decl_or_origin)
20898           && (current_function_decl == NULL_TREE
20899               || DECL_ARTIFICIAL (decl_or_origin)))
20900         break;
20901
20902 #if 0
20903       /* FIXME */
20904       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20905          on local redeclarations of global functions.  That seems broken.  */
20906       if (current_function_decl != decl)
20907         /* This is only a declaration.  */;
20908 #endif
20909
20910       /* If we're emitting a clone, emit info for the abstract instance.  */
20911       if (origin || DECL_ORIGIN (decl) != decl)
20912         dwarf2out_abstract_function (origin
20913                                      ? DECL_ORIGIN (origin)
20914                                      : DECL_ABSTRACT_ORIGIN (decl));
20915
20916       /* If we're emitting an out-of-line copy of an inline function,
20917          emit info for the abstract instance and set up to refer to it.  */
20918       else if (cgraph_function_possibly_inlined_p (decl)
20919                && ! DECL_ABSTRACT_P (decl)
20920                && ! class_or_namespace_scope_p (context_die)
20921                /* dwarf2out_abstract_function won't emit a die if this is just
20922                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
20923                   that case, because that works only if we have a die.  */
20924                && DECL_INITIAL (decl) != NULL_TREE)
20925         {
20926           dwarf2out_abstract_function (decl);
20927           set_decl_origin_self (decl);
20928         }
20929
20930       /* Otherwise we're emitting the primary DIE for this decl.  */
20931       else if (debug_info_level > DINFO_LEVEL_TERSE)
20932         {
20933           /* Before we describe the FUNCTION_DECL itself, make sure that we
20934              have its containing type.  */
20935           if (!origin)
20936             origin = decl_class_context (decl);
20937           if (origin != NULL_TREE)
20938             gen_type_die (origin, context_die);
20939
20940           /* And its return type.  */
20941           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20942
20943           /* And its virtual context.  */
20944           if (DECL_VINDEX (decl) != NULL_TREE)
20945             gen_type_die (DECL_CONTEXT (decl), context_die);
20946
20947           /* Make sure we have a member DIE for decl.  */
20948           if (origin != NULL_TREE)
20949             gen_type_die_for_member (origin, decl, context_die);
20950
20951           /* And its containing namespace.  */
20952           context_die = declare_in_namespace (decl, context_die);
20953         }
20954
20955       /* Now output a DIE to represent the function itself.  */
20956       if (decl)
20957         gen_subprogram_die (decl, context_die);
20958       break;
20959
20960     case TYPE_DECL:
20961       /* If we are in terse mode, don't generate any DIEs to represent any
20962          actual typedefs.  */
20963       if (debug_info_level <= DINFO_LEVEL_TERSE)
20964         break;
20965
20966       /* In the special case of a TYPE_DECL node representing the declaration
20967          of some type tag, if the given TYPE_DECL is marked as having been
20968          instantiated from some other (original) TYPE_DECL node (e.g. one which
20969          was generated within the original definition of an inline function) we
20970          used to generate a special (abbreviated) DW_TAG_structure_type,
20971          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
20972          should be actually referencing those DIEs, as variable DIEs with that
20973          type would be emitted already in the abstract origin, so it was always
20974          removed during unused type prunning.  Don't add anything in this
20975          case.  */
20976       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20977         break;
20978
20979       if (is_redundant_typedef (decl))
20980         gen_type_die (TREE_TYPE (decl), context_die);
20981       else
20982         /* Output a DIE to represent the typedef itself.  */
20983         gen_typedef_die (decl, context_die);
20984       break;
20985
20986     case LABEL_DECL:
20987       if (debug_info_level >= DINFO_LEVEL_NORMAL)
20988         gen_label_die (decl, context_die);
20989       break;
20990
20991     case VAR_DECL:
20992     case RESULT_DECL:
20993       /* If we are in terse mode, don't generate any DIEs to represent any
20994          variable declarations or definitions.  */
20995       if (debug_info_level <= DINFO_LEVEL_TERSE)
20996         break;
20997
20998       /* Output any DIEs that are needed to specify the type of this data
20999          object.  */
21000       if (decl_by_reference_p (decl_or_origin))
21001         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21002       else
21003         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21004
21005       /* And its containing type.  */
21006       class_origin = decl_class_context (decl_or_origin);
21007       if (class_origin != NULL_TREE)
21008         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
21009
21010       /* And its containing namespace.  */
21011       context_die = declare_in_namespace (decl_or_origin, context_die);
21012
21013       /* Now output the DIE to represent the data object itself.  This gets
21014          complicated because of the possibility that the VAR_DECL really
21015          represents an inlined instance of a formal parameter for an inline
21016          function.  */
21017       ultimate_origin = decl_ultimate_origin (decl_or_origin);
21018       if (ultimate_origin != NULL_TREE
21019           && TREE_CODE (ultimate_origin) == PARM_DECL)
21020         gen_formal_parameter_die (decl, origin,
21021                                   true /* Emit name attribute.  */,
21022                                   context_die);
21023       else
21024         gen_variable_die (decl, origin, context_die);
21025       break;
21026
21027     case FIELD_DECL:
21028       /* Ignore the nameless fields that are used to skip bits but handle C++
21029          anonymous unions and structs.  */
21030       if (DECL_NAME (decl) != NULL_TREE
21031           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
21032           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
21033         {
21034           gen_type_die (member_declared_type (decl), context_die);
21035           gen_field_die (decl, context_die);
21036         }
21037       break;
21038
21039     case PARM_DECL:
21040       if (DECL_BY_REFERENCE (decl_or_origin))
21041         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21042       else
21043         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21044       return gen_formal_parameter_die (decl, origin,
21045                                        true /* Emit name attribute.  */,
21046                                        context_die);
21047
21048     case NAMESPACE_DECL:
21049     case IMPORTED_DECL:
21050       if (dwarf_version >= 3 || !dwarf_strict)
21051         gen_namespace_die (decl, context_die);
21052       break;
21053
21054     case NAMELIST_DECL:
21055       gen_namelist_decl (DECL_NAME (decl), context_die,
21056                          NAMELIST_DECL_ASSOCIATED_DECL (decl));
21057       break;
21058
21059     default:
21060       /* Probably some frontend-internal decl.  Assume we don't care.  */
21061       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
21062       break;
21063     }
21064
21065   return NULL;
21066 }
21067 \f
21068 /* Output debug information for global decl DECL.  Called from toplev.c after
21069    compilation proper has finished.  */
21070
21071 static void
21072 dwarf2out_global_decl (tree decl)
21073 {
21074   /* Output DWARF2 information for file-scope tentative data object
21075      declarations, file-scope (extern) function declarations (which
21076      had no corresponding body) and file-scope tagged type declarations
21077      and definitions which have not yet been forced out.  */
21078   if ((TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
21079       && !POINTER_BOUNDS_P (decl))
21080     dwarf2out_decl (decl);
21081 }
21082
21083 /* Output debug information for type decl DECL.  Called from toplev.c
21084    and from language front ends (to record built-in types).  */
21085 static void
21086 dwarf2out_type_decl (tree decl, int local)
21087 {
21088   if (!local)
21089     dwarf2out_decl (decl);
21090 }
21091
21092 /* Output debug information for imported module or decl DECL.
21093    NAME is non-NULL name in the lexical block if the decl has been renamed.
21094    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
21095    that DECL belongs to.
21096    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
21097 static void
21098 dwarf2out_imported_module_or_decl_1 (tree decl,
21099                                      tree name,
21100                                      tree lexical_block,
21101                                      dw_die_ref lexical_block_die)
21102 {
21103   expanded_location xloc;
21104   dw_die_ref imported_die = NULL;
21105   dw_die_ref at_import_die;
21106
21107   if (TREE_CODE (decl) == IMPORTED_DECL)
21108     {
21109       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
21110       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
21111       gcc_assert (decl);
21112     }
21113   else
21114     xloc = expand_location (input_location);
21115
21116   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
21117     {
21118       at_import_die = force_type_die (TREE_TYPE (decl));
21119       /* For namespace N { typedef void T; } using N::T; base_type_die
21120          returns NULL, but DW_TAG_imported_declaration requires
21121          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
21122       if (!at_import_die)
21123         {
21124           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
21125           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
21126           at_import_die = lookup_type_die (TREE_TYPE (decl));
21127           gcc_assert (at_import_die);
21128         }
21129     }
21130   else
21131     {
21132       at_import_die = lookup_decl_die (decl);
21133       if (!at_import_die)
21134         {
21135           /* If we're trying to avoid duplicate debug info, we may not have
21136              emitted the member decl for this field.  Emit it now.  */
21137           if (TREE_CODE (decl) == FIELD_DECL)
21138             {
21139               tree type = DECL_CONTEXT (decl);
21140
21141               if (TYPE_CONTEXT (type)
21142                   && TYPE_P (TYPE_CONTEXT (type))
21143                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
21144                                                 DINFO_USAGE_DIR_USE))
21145                 return;
21146               gen_type_die_for_member (type, decl,
21147                                        get_context_die (TYPE_CONTEXT (type)));
21148             }
21149           if (TREE_CODE (decl) == NAMELIST_DECL)
21150             at_import_die = gen_namelist_decl (DECL_NAME (decl),
21151                                          get_context_die (DECL_CONTEXT (decl)),
21152                                          NULL_TREE);
21153           else
21154             at_import_die = force_decl_die (decl);
21155         }
21156     }
21157
21158   if (TREE_CODE (decl) == NAMESPACE_DECL)
21159     {
21160       if (dwarf_version >= 3 || !dwarf_strict)
21161         imported_die = new_die (DW_TAG_imported_module,
21162                                 lexical_block_die,
21163                                 lexical_block);
21164       else
21165         return;
21166     }
21167   else
21168     imported_die = new_die (DW_TAG_imported_declaration,
21169                             lexical_block_die,
21170                             lexical_block);
21171
21172   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
21173   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
21174   if (name)
21175     add_AT_string (imported_die, DW_AT_name,
21176                    IDENTIFIER_POINTER (name));
21177   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
21178 }
21179
21180 /* Output debug information for imported module or decl DECL.
21181    NAME is non-NULL name in context if the decl has been renamed.
21182    CHILD is true if decl is one of the renamed decls as part of
21183    importing whole module.  */
21184
21185 static void
21186 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
21187                                    bool child)
21188 {
21189   /* dw_die_ref at_import_die;  */
21190   dw_die_ref scope_die;
21191
21192   if (debug_info_level <= DINFO_LEVEL_TERSE)
21193     return;
21194
21195   gcc_assert (decl);
21196
21197   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
21198      We need decl DIE for reference and scope die. First, get DIE for the decl
21199      itself.  */
21200
21201   /* Get the scope die for decl context. Use comp_unit_die for global module
21202      or decl. If die is not found for non globals, force new die.  */
21203   if (context
21204       && TYPE_P (context)
21205       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
21206     return;
21207
21208   if (!(dwarf_version >= 3 || !dwarf_strict))
21209     return;
21210
21211   scope_die = get_context_die (context);
21212
21213   if (child)
21214     {
21215       gcc_assert (scope_die->die_child);
21216       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
21217       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
21218       scope_die = scope_die->die_child;
21219     }
21220
21221   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
21222   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
21223
21224 }
21225
21226 /* Output debug information for namelists.   */
21227
21228 static dw_die_ref
21229 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
21230 {
21231   dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
21232   tree value;
21233   unsigned i;
21234
21235   if (debug_info_level <= DINFO_LEVEL_TERSE)
21236     return NULL;
21237
21238   gcc_assert (scope_die != NULL);
21239   nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
21240   add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
21241
21242   /* If there are no item_decls, we have a nondefining namelist, e.g.
21243      with USE association; hence, set DW_AT_declaration.  */
21244   if (item_decls == NULL_TREE)
21245     {
21246       add_AT_flag (nml_die, DW_AT_declaration, 1);
21247       return nml_die;
21248     }
21249
21250   FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
21251     {
21252       nml_item_ref_die = lookup_decl_die (value);
21253       if (!nml_item_ref_die)
21254         nml_item_ref_die = force_decl_die (value);
21255
21256       nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
21257       add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
21258     }
21259   return nml_die;
21260 }
21261
21262
21263 /* Write the debugging output for DECL.  */
21264
21265 static void
21266 dwarf2out_decl (tree decl)
21267 {
21268   dw_die_ref context_die = comp_unit_die ();
21269
21270   switch (TREE_CODE (decl))
21271     {
21272     case ERROR_MARK:
21273       return;
21274
21275     case FUNCTION_DECL:
21276       /* What we would really like to do here is to filter out all mere
21277          file-scope declarations of file-scope functions which are never
21278          referenced later within this translation unit (and keep all of ones
21279          that *are* referenced later on) but we aren't clairvoyant, so we have
21280          no idea which functions will be referenced in the future (i.e. later
21281          on within the current translation unit). So here we just ignore all
21282          file-scope function declarations which are not also definitions.  If
21283          and when the debugger needs to know something about these functions,
21284          it will have to hunt around and find the DWARF information associated
21285          with the definition of the function.
21286
21287          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
21288          nodes represent definitions and which ones represent mere
21289          declarations.  We have to check DECL_INITIAL instead. That's because
21290          the C front-end supports some weird semantics for "extern inline"
21291          function definitions.  These can get inlined within the current
21292          translation unit (and thus, we need to generate Dwarf info for their
21293          abstract instances so that the Dwarf info for the concrete inlined
21294          instances can have something to refer to) but the compiler never
21295          generates any out-of-lines instances of such things (despite the fact
21296          that they *are* definitions).
21297
21298          The important point is that the C front-end marks these "extern
21299          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
21300          them anyway. Note that the C++ front-end also plays some similar games
21301          for inline function definitions appearing within include files which
21302          also contain `#pragma interface' pragmas.
21303
21304          If we are called from dwarf2out_abstract_function output a DIE
21305          anyway.  We can end up here this way with early inlining and LTO
21306          where the inlined function is output in a different LTRANS unit
21307          or not at all.  */
21308       if (DECL_INITIAL (decl) == NULL_TREE
21309           && ! DECL_ABSTRACT_P (decl))
21310         return;
21311
21312       /* If we're a nested function, initially use a parent of NULL; if we're
21313          a plain function, this will be fixed up in decls_for_scope.  If
21314          we're a method, it will be ignored, since we already have a DIE.  */
21315       if (decl_function_context (decl)
21316           /* But if we're in terse mode, we don't care about scope.  */
21317           && debug_info_level > DINFO_LEVEL_TERSE)
21318         context_die = NULL;
21319       break;
21320
21321     case VAR_DECL:
21322       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
21323          declaration and if the declaration was never even referenced from
21324          within this entire compilation unit.  We suppress these DIEs in
21325          order to save space in the .debug section (by eliminating entries
21326          which are probably useless).  Note that we must not suppress
21327          block-local extern declarations (whether used or not) because that
21328          would screw-up the debugger's name lookup mechanism and cause it to
21329          miss things which really ought to be in scope at a given point.  */
21330       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
21331         return;
21332
21333       /* For local statics lookup proper context die.  */
21334       if (TREE_STATIC (decl)
21335           && DECL_CONTEXT (decl)
21336           && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
21337         context_die = lookup_decl_die (DECL_CONTEXT (decl));
21338
21339       /* If we are in terse mode, don't generate any DIEs to represent any
21340          variable declarations or definitions.  */
21341       if (debug_info_level <= DINFO_LEVEL_TERSE)
21342         return;
21343       break;
21344
21345     case CONST_DECL:
21346       if (debug_info_level <= DINFO_LEVEL_TERSE)
21347         return;
21348       if (!is_fortran () && !is_ada ())
21349         return;
21350       if (TREE_STATIC (decl) && decl_function_context (decl))
21351         context_die = lookup_decl_die (DECL_CONTEXT (decl));
21352       break;
21353
21354     case NAMESPACE_DECL:
21355     case IMPORTED_DECL:
21356       if (debug_info_level <= DINFO_LEVEL_TERSE)
21357         return;
21358       if (lookup_decl_die (decl) != NULL)
21359         return;
21360       break;
21361
21362     case TYPE_DECL:
21363       /* Don't emit stubs for types unless they are needed by other DIEs.  */
21364       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
21365         return;
21366
21367       /* Don't bother trying to generate any DIEs to represent any of the
21368          normal built-in types for the language we are compiling.  */
21369       if (DECL_IS_BUILTIN (decl))
21370         return;
21371
21372       /* If we are in terse mode, don't generate any DIEs for types.  */
21373       if (debug_info_level <= DINFO_LEVEL_TERSE)
21374         return;
21375
21376       /* If we're a function-scope tag, initially use a parent of NULL;
21377          this will be fixed up in decls_for_scope.  */
21378       if (decl_function_context (decl))
21379         context_die = NULL;
21380
21381       break;
21382
21383     case NAMELIST_DECL:
21384       break;
21385
21386     default:
21387       return;
21388     }
21389
21390   gen_decl_die (decl, NULL, context_die);
21391 }
21392
21393 /* Write the debugging output for DECL.  */
21394
21395 static void
21396 dwarf2out_function_decl (tree decl)
21397 {
21398   dwarf2out_decl (decl);
21399   call_arg_locations = NULL;
21400   call_arg_loc_last = NULL;
21401   call_site_count = -1;
21402   tail_call_site_count = -1;
21403   block_map.release ();
21404   decl_loc_table->empty ();
21405   cached_dw_loc_list_table->empty ();
21406 }
21407
21408 /* Output a marker (i.e. a label) for the beginning of the generated code for
21409    a lexical block.  */
21410
21411 static void
21412 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21413                        unsigned int blocknum)
21414 {
21415   switch_to_section (current_function_section ());
21416   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
21417 }
21418
21419 /* Output a marker (i.e. a label) for the end of the generated code for a
21420    lexical block.  */
21421
21422 static void
21423 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
21424 {
21425   switch_to_section (current_function_section ());
21426   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
21427 }
21428
21429 /* Returns nonzero if it is appropriate not to emit any debugging
21430    information for BLOCK, because it doesn't contain any instructions.
21431
21432    Don't allow this for blocks with nested functions or local classes
21433    as we would end up with orphans, and in the presence of scheduling
21434    we may end up calling them anyway.  */
21435
21436 static bool
21437 dwarf2out_ignore_block (const_tree block)
21438 {
21439   tree decl;
21440   unsigned int i;
21441
21442   for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
21443     if (TREE_CODE (decl) == FUNCTION_DECL
21444         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21445       return 0;
21446   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
21447     {
21448       decl = BLOCK_NONLOCALIZED_VAR (block, i);
21449       if (TREE_CODE (decl) == FUNCTION_DECL
21450           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21451       return 0;
21452     }
21453
21454   return 1;
21455 }
21456
21457 /* Hash table routines for file_hash.  */
21458
21459 bool
21460 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
21461 {
21462   return filename_cmp (p1->filename, p2) == 0;
21463 }
21464
21465 hashval_t
21466 dwarf_file_hasher::hash (dwarf_file_data *p)
21467 {
21468   return htab_hash_string (p->filename);
21469 }
21470
21471 /* Lookup FILE_NAME (in the list of filenames that we know about here in
21472    dwarf2out.c) and return its "index".  The index of each (known) filename is
21473    just a unique number which is associated with only that one filename.  We
21474    need such numbers for the sake of generating labels (in the .debug_sfnames
21475    section) and references to those files numbers (in the .debug_srcinfo
21476    and.debug_macinfo sections).  If the filename given as an argument is not
21477    found in our current list, add it to the list and assign it the next
21478    available unique index number.  In order to speed up searches, we remember
21479    the index of the filename was looked up last.  This handles the majority of
21480    all searches.  */
21481
21482 static struct dwarf_file_data *
21483 lookup_filename (const char *file_name)
21484 {
21485   struct dwarf_file_data * created;
21486
21487   /* Check to see if the file name that was searched on the previous
21488      call matches this file name.  If so, return the index.  */
21489   if (file_table_last_lookup
21490       && (file_name == file_table_last_lookup->filename
21491           || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
21492     return file_table_last_lookup;
21493
21494   /* Didn't match the previous lookup, search the table.  */
21495   dwarf_file_data **slot
21496     = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
21497                                        INSERT);
21498   if (*slot)
21499     return *slot;
21500
21501   created = ggc_alloc<dwarf_file_data> ();
21502   created->filename = file_name;
21503   created->emitted_number = 0;
21504   *slot = created;
21505   return created;
21506 }
21507
21508 /* If the assembler will construct the file table, then translate the compiler
21509    internal file table number into the assembler file table number, and emit
21510    a .file directive if we haven't already emitted one yet.  The file table
21511    numbers are different because we prune debug info for unused variables and
21512    types, which may include filenames.  */
21513
21514 static int
21515 maybe_emit_file (struct dwarf_file_data * fd)
21516 {
21517   if (! fd->emitted_number)
21518     {
21519       if (last_emitted_file)
21520         fd->emitted_number = last_emitted_file->emitted_number + 1;
21521       else
21522         fd->emitted_number = 1;
21523       last_emitted_file = fd;
21524
21525       if (DWARF2_ASM_LINE_DEBUG_INFO)
21526         {
21527           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
21528           output_quoted_string (asm_out_file,
21529                                 remap_debug_filename (fd->filename));
21530           fputc ('\n', asm_out_file);
21531         }
21532     }
21533
21534   return fd->emitted_number;
21535 }
21536
21537 /* Schedule generation of a DW_AT_const_value attribute to DIE.
21538    That generation should happen after function debug info has been
21539    generated. The value of the attribute is the constant value of ARG.  */
21540
21541 static void
21542 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
21543 {
21544   die_arg_entry entry;
21545
21546   if (!die || !arg)
21547     return;
21548
21549   if (!tmpl_value_parm_die_table)
21550     vec_alloc (tmpl_value_parm_die_table, 32);
21551
21552   entry.die = die;
21553   entry.arg = arg;
21554   vec_safe_push (tmpl_value_parm_die_table, entry);
21555 }
21556
21557 /* Return TRUE if T is an instance of generic type, FALSE
21558    otherwise.  */
21559
21560 static bool
21561 generic_type_p (tree t)
21562 {
21563   if (t == NULL_TREE || !TYPE_P (t))
21564     return false;
21565   return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
21566 }
21567
21568 /* Schedule the generation of the generic parameter dies for the
21569   instance of generic type T. The proper generation itself is later
21570   done by gen_scheduled_generic_parms_dies. */
21571
21572 static void
21573 schedule_generic_params_dies_gen (tree t)
21574 {
21575   if (!generic_type_p (t))
21576     return;
21577
21578   if (!generic_type_instances)
21579     vec_alloc (generic_type_instances, 256);
21580
21581   vec_safe_push (generic_type_instances, t);
21582 }
21583
21584 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21585    by append_entry_to_tmpl_value_parm_die_table. This function must
21586    be called after function DIEs have been generated.  */
21587
21588 static void
21589 gen_remaining_tmpl_value_param_die_attribute (void)
21590 {
21591   if (tmpl_value_parm_die_table)
21592     {
21593       unsigned i;
21594       die_arg_entry *e;
21595
21596       FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
21597         tree_add_const_value_attribute (e->die, e->arg);
21598     }
21599 }
21600
21601 /* Generate generic parameters DIEs for instances of generic types
21602    that have been previously scheduled by
21603    schedule_generic_params_dies_gen. This function must be called
21604    after all the types of the CU have been laid out.  */
21605
21606 static void
21607 gen_scheduled_generic_parms_dies (void)
21608 {
21609   unsigned i;
21610   tree t;
21611
21612   if (!generic_type_instances)
21613     return;
21614   
21615   FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
21616     if (COMPLETE_TYPE_P (t))
21617       gen_generic_params_dies (t);
21618 }
21619
21620
21621 /* Replace DW_AT_name for the decl with name.  */
21622
21623 static void
21624 dwarf2out_set_name (tree decl, tree name)
21625 {
21626   dw_die_ref die;
21627   dw_attr_ref attr;
21628   const char *dname;
21629
21630   die = TYPE_SYMTAB_DIE (decl);
21631   if (!die)
21632     return;
21633
21634   dname = dwarf2_name (name, 0);
21635   if (!dname)
21636     return;
21637
21638   attr = get_AT (die, DW_AT_name);
21639   if (attr)
21640     {
21641       struct indirect_string_node *node;
21642
21643       node = find_AT_string (dname);
21644       /* replace the string.  */
21645       attr->dw_attr_val.v.val_str = node;
21646     }
21647
21648   else
21649     add_name_attribute (die, dname);
21650 }
21651
21652 /* True if before or during processing of the first function being emitted.  */
21653 static bool in_first_function_p = true;
21654 /* True if loc_note during dwarf2out_var_location call might still be
21655    before first real instruction at address equal to .Ltext0.  */
21656 static bool maybe_at_text_label_p = true;
21657 /* One above highest N where .LVLN label might be equal to .Ltext0 label.  */
21658 static unsigned int first_loclabel_num_not_at_text_label;
21659
21660 /* Called by the final INSN scan whenever we see a var location.  We
21661    use it to drop labels in the right places, and throw the location in
21662    our lookup table.  */
21663
21664 static void
21665 dwarf2out_var_location (rtx_insn *loc_note)
21666 {
21667   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21668   struct var_loc_node *newloc;
21669   rtx_insn *next_real, *next_note;
21670   static const char *last_label;
21671   static const char *last_postcall_label;
21672   static bool last_in_cold_section_p;
21673   static rtx_insn *expected_next_loc_note;
21674   tree decl;
21675   bool var_loc_p;
21676
21677   if (!NOTE_P (loc_note))
21678     {
21679       if (CALL_P (loc_note))
21680         {
21681           call_site_count++;
21682           if (SIBLING_CALL_P (loc_note))
21683             tail_call_site_count++;
21684         }
21685       return;
21686     }
21687
21688   var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
21689   if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21690     return;
21691
21692   /* Optimize processing a large consecutive sequence of location
21693      notes so we don't spend too much time in next_real_insn.  If the
21694      next insn is another location note, remember the next_real_insn
21695      calculation for next time.  */
21696   next_real = cached_next_real_insn;
21697   if (next_real)
21698     {
21699       if (expected_next_loc_note != loc_note)
21700         next_real = NULL;
21701     }
21702
21703   next_note = NEXT_INSN (loc_note);
21704   if (! next_note
21705       || next_note->deleted ()
21706       || ! NOTE_P (next_note)
21707       || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
21708           && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
21709     next_note = NULL;
21710
21711   if (! next_real)
21712     next_real = next_real_insn (loc_note);
21713
21714   if (next_note)
21715     {
21716       expected_next_loc_note = next_note;
21717       cached_next_real_insn = next_real;
21718     }
21719   else
21720     cached_next_real_insn = NULL;
21721
21722   /* If there are no instructions which would be affected by this note,
21723      don't do anything.  */
21724   if (var_loc_p
21725       && next_real == NULL_RTX
21726       && !NOTE_DURING_CALL_P (loc_note))
21727     return;
21728
21729   if (next_real == NULL_RTX)
21730     next_real = get_last_insn ();
21731
21732   /* If there were any real insns between note we processed last time
21733      and this note (or if it is the first note), clear
21734      last_{,postcall_}label so that they are not reused this time.  */
21735   if (last_var_location_insn == NULL_RTX
21736       || last_var_location_insn != next_real
21737       || last_in_cold_section_p != in_cold_section_p)
21738     {
21739       last_label = NULL;
21740       last_postcall_label = NULL;
21741     }
21742
21743   if (var_loc_p)
21744     {
21745       decl = NOTE_VAR_LOCATION_DECL (loc_note);
21746       newloc = add_var_loc_to_decl (decl, loc_note,
21747                                     NOTE_DURING_CALL_P (loc_note)
21748                                     ? last_postcall_label : last_label);
21749       if (newloc == NULL)
21750         return;
21751     }
21752   else
21753     {
21754       decl = NULL_TREE;
21755       newloc = NULL;
21756     }
21757
21758   /* If there were no real insns between note we processed last time
21759      and this note, use the label we emitted last time.  Otherwise
21760      create a new label and emit it.  */
21761   if (last_label == NULL)
21762     {
21763       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21764       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21765       loclabel_num++;
21766       last_label = ggc_strdup (loclabel);
21767       /* See if loclabel might be equal to .Ltext0.  If yes,
21768          bump first_loclabel_num_not_at_text_label.  */
21769       if (!have_multiple_function_sections
21770           && in_first_function_p
21771           && maybe_at_text_label_p)
21772         {
21773           static rtx_insn *last_start;
21774           rtx_insn *insn;
21775           for (insn = loc_note; insn; insn = previous_insn (insn))
21776             if (insn == last_start)
21777               break;
21778             else if (!NONDEBUG_INSN_P (insn))
21779               continue;
21780             else
21781               {
21782                 rtx body = PATTERN (insn);
21783                 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
21784                   continue;
21785                 /* Inline asm could occupy zero bytes.  */
21786                 else if (GET_CODE (body) == ASM_INPUT
21787                          || asm_noperands (body) >= 0)
21788                   continue;
21789 #ifdef HAVE_attr_length
21790                 else if (get_attr_min_length (insn) == 0)
21791                   continue;
21792 #endif
21793                 else
21794                   {
21795                     /* Assume insn has non-zero length.  */
21796                     maybe_at_text_label_p = false;
21797                     break;
21798                   }
21799               }
21800           if (maybe_at_text_label_p)
21801             {
21802               last_start = loc_note;
21803               first_loclabel_num_not_at_text_label = loclabel_num;
21804             }
21805         }
21806     }
21807
21808   if (!var_loc_p)
21809     {
21810       struct call_arg_loc_node *ca_loc
21811         = ggc_cleared_alloc<call_arg_loc_node> ();
21812       rtx_insn *prev = prev_real_insn (loc_note);
21813       rtx x;
21814       ca_loc->call_arg_loc_note = loc_note;
21815       ca_loc->next = NULL;
21816       ca_loc->label = last_label;
21817       gcc_assert (prev
21818                   && (CALL_P (prev)
21819                       || (NONJUMP_INSN_P (prev)
21820                           && GET_CODE (PATTERN (prev)) == SEQUENCE
21821                           && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
21822       if (!CALL_P (prev))
21823         prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
21824       ca_loc->tail_call_p = SIBLING_CALL_P (prev);
21825       x = get_call_rtx_from (PATTERN (prev));
21826       if (x)
21827         {
21828           x = XEXP (XEXP (x, 0), 0);
21829           if (GET_CODE (x) == SYMBOL_REF
21830               && SYMBOL_REF_DECL (x)
21831               && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
21832             ca_loc->symbol_ref = x;
21833         }
21834       ca_loc->block = insn_scope (prev);
21835       if (call_arg_locations)
21836         call_arg_loc_last->next = ca_loc;
21837       else
21838         call_arg_locations = ca_loc;
21839       call_arg_loc_last = ca_loc;
21840     }
21841   else if (!NOTE_DURING_CALL_P (loc_note))
21842     newloc->label = last_label;
21843   else
21844     {
21845       if (!last_postcall_label)
21846         {
21847           sprintf (loclabel, "%s-1", last_label);
21848           last_postcall_label = ggc_strdup (loclabel);
21849         }
21850       newloc->label = last_postcall_label;
21851     }
21852
21853   last_var_location_insn = next_real;
21854   last_in_cold_section_p = in_cold_section_p;
21855 }
21856
21857 /* Note in one location list that text section has changed.  */
21858
21859 int
21860 var_location_switch_text_section_1 (var_loc_list **slot, void *)
21861 {
21862   var_loc_list *list = *slot;
21863   if (list->first)
21864     list->last_before_switch
21865       = list->last->next ? list->last->next : list->last;
21866   return 1;
21867 }
21868
21869 /* Note in all location lists that text section has changed.  */
21870
21871 static void
21872 var_location_switch_text_section (void)
21873 {
21874   if (decl_loc_table == NULL)
21875     return;
21876
21877   decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
21878 }
21879
21880 /* Create a new line number table.  */
21881
21882 static dw_line_info_table *
21883 new_line_info_table (void)
21884 {
21885   dw_line_info_table *table;
21886
21887   table = ggc_cleared_alloc<dw_line_info_table_struct> ();
21888   table->file_num = 1;
21889   table->line_num = 1;
21890   table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
21891
21892   return table;
21893 }
21894
21895 /* Lookup the "current" table into which we emit line info, so
21896    that we don't have to do it for every source line.  */
21897
21898 static void
21899 set_cur_line_info_table (section *sec)
21900 {
21901   dw_line_info_table *table;
21902
21903   if (sec == text_section)
21904     table = text_section_line_info;
21905   else if (sec == cold_text_section)
21906     {
21907       table = cold_text_section_line_info;
21908       if (!table)
21909         {
21910           cold_text_section_line_info = table = new_line_info_table ();
21911           table->end_label = cold_end_label;
21912         }
21913     }
21914   else
21915     {
21916       const char *end_label;
21917
21918       if (flag_reorder_blocks_and_partition)
21919         {
21920           if (in_cold_section_p)
21921             end_label = crtl->subsections.cold_section_end_label;
21922           else
21923             end_label = crtl->subsections.hot_section_end_label;
21924         }
21925       else
21926         {
21927           char label[MAX_ARTIFICIAL_LABEL_BYTES];
21928           ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
21929                                        current_function_funcdef_no);
21930           end_label = ggc_strdup (label);
21931         }
21932
21933       table = new_line_info_table ();
21934       table->end_label = end_label;
21935
21936       vec_safe_push (separate_line_info, table);
21937     }
21938
21939   if (DWARF2_ASM_LINE_DEBUG_INFO)
21940     table->is_stmt = (cur_line_info_table
21941                       ? cur_line_info_table->is_stmt
21942                       : DWARF_LINE_DEFAULT_IS_STMT_START);
21943   cur_line_info_table = table;
21944 }
21945
21946
21947 /* We need to reset the locations at the beginning of each
21948    function. We can't do this in the end_function hook, because the
21949    declarations that use the locations won't have been output when
21950    that hook is called.  Also compute have_multiple_function_sections here.  */
21951
21952 static void
21953 dwarf2out_begin_function (tree fun)
21954 {
21955   section *sec = function_section (fun);
21956
21957   if (sec != text_section)
21958     have_multiple_function_sections = true;
21959
21960   if (flag_reorder_blocks_and_partition && !cold_text_section)
21961     {
21962       gcc_assert (current_function_decl == fun);
21963       cold_text_section = unlikely_text_section ();
21964       switch_to_section (cold_text_section);
21965       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21966       switch_to_section (sec);
21967     }
21968
21969   dwarf2out_note_section_used ();
21970   call_site_count = 0;
21971   tail_call_site_count = 0;
21972
21973   set_cur_line_info_table (sec);
21974 }
21975
21976 /* Helper function of dwarf2out_end_function, called only after emitting
21977    the very first function into assembly.  Check if some .debug_loc range
21978    might end with a .LVL* label that could be equal to .Ltext0.
21979    In that case we must force using absolute addresses in .debug_loc ranges,
21980    because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
21981    .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
21982    list terminator.
21983    Set have_multiple_function_sections to true in that case and
21984    terminate htab traversal.  */
21985
21986 int
21987 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
21988 {
21989   var_loc_list *entry = *slot;
21990   struct var_loc_node *node;
21991
21992   node = entry->first;
21993   if (node && node->next && node->next->label)
21994     {
21995       unsigned int i;
21996       const char *label = node->next->label;
21997       char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
21998
21999       for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
22000         {
22001           ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
22002           if (strcmp (label, loclabel) == 0)
22003             {
22004               have_multiple_function_sections = true;
22005               return 0;
22006             }
22007         }
22008     }
22009   return 1;
22010 }
22011
22012 /* Hook called after emitting a function into assembly.
22013    This does something only for the very first function emitted.  */
22014
22015 static void
22016 dwarf2out_end_function (unsigned int)
22017 {
22018   if (in_first_function_p
22019       && !have_multiple_function_sections
22020       && first_loclabel_num_not_at_text_label
22021       && decl_loc_table)
22022     decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
22023   in_first_function_p = false;
22024   maybe_at_text_label_p = false;
22025 }
22026
22027 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE.  */
22028
22029 static void
22030 push_dw_line_info_entry (dw_line_info_table *table,
22031                          enum dw_line_info_opcode opcode, unsigned int val)
22032 {
22033   dw_line_info_entry e;
22034   e.opcode = opcode;
22035   e.val = val;
22036   vec_safe_push (table->entries, e);
22037 }
22038
22039 /* Output a label to mark the beginning of a source code line entry
22040    and record information relating to this source line, in
22041    'line_info_table' for later output of the .debug_line section.  */
22042 /* ??? The discriminator parameter ought to be unsigned.  */
22043
22044 static void
22045 dwarf2out_source_line (unsigned int line, const char *filename,
22046                        int discriminator, bool is_stmt)
22047 {
22048   unsigned int file_num;
22049   dw_line_info_table *table;
22050
22051   if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
22052     return;
22053
22054   /* The discriminator column was added in dwarf4.  Simplify the below
22055      by simply removing it if we're not supposed to output it.  */
22056   if (dwarf_version < 4 && dwarf_strict)
22057     discriminator = 0;
22058
22059   table = cur_line_info_table;
22060   file_num = maybe_emit_file (lookup_filename (filename));
22061
22062   /* ??? TODO: Elide duplicate line number entries.  Traditionally,
22063      the debugger has used the second (possibly duplicate) line number
22064      at the beginning of the function to mark the end of the prologue.
22065      We could eliminate any other duplicates within the function.  For
22066      Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
22067      that second line number entry.  */
22068   /* Recall that this end-of-prologue indication is *not* the same thing
22069      as the end_prologue debug hook.  The NOTE_INSN_PROLOGUE_END note,
22070      to which the hook corresponds, follows the last insn that was 
22071      emitted by gen_prologue.  What we need is to precede the first insn
22072      that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
22073      insn that corresponds to something the user wrote.  These may be
22074      very different locations once scheduling is enabled.  */
22075
22076   if (0 && file_num == table->file_num
22077       && line == table->line_num
22078       && discriminator == table->discrim_num
22079       && is_stmt == table->is_stmt)
22080     return;
22081
22082   switch_to_section (current_function_section ());
22083
22084   /* If requested, emit something human-readable.  */
22085   if (flag_debug_asm)
22086     fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
22087
22088   if (DWARF2_ASM_LINE_DEBUG_INFO)
22089     {
22090       /* Emit the .loc directive understood by GNU as.  */
22091       /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
22092          file_num, line, is_stmt, discriminator */
22093       fputs ("\t.loc ", asm_out_file);
22094       fprint_ul (asm_out_file, file_num);
22095       putc (' ', asm_out_file);
22096       fprint_ul (asm_out_file, line);
22097       putc (' ', asm_out_file);
22098       putc ('0', asm_out_file);
22099
22100       if (is_stmt != table->is_stmt)
22101         {
22102           fputs (" is_stmt ", asm_out_file);
22103           putc (is_stmt ? '1' : '0', asm_out_file);
22104         }
22105       if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
22106         {
22107           gcc_assert (discriminator > 0);
22108           fputs (" discriminator ", asm_out_file);
22109           fprint_ul (asm_out_file, (unsigned long) discriminator);
22110         }
22111       putc ('\n', asm_out_file);
22112     }
22113   else
22114     {
22115       unsigned int label_num = ++line_info_label_num;
22116
22117       targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
22118
22119       push_dw_line_info_entry (table, LI_set_address, label_num);
22120       if (file_num != table->file_num)
22121         push_dw_line_info_entry (table, LI_set_file, file_num);
22122       if (discriminator != table->discrim_num)
22123         push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
22124       if (is_stmt != table->is_stmt)
22125         push_dw_line_info_entry (table, LI_negate_stmt, 0);
22126       push_dw_line_info_entry (table, LI_set_line, line);
22127     }
22128
22129   table->file_num = file_num;
22130   table->line_num = line;
22131   table->discrim_num = discriminator;
22132   table->is_stmt = is_stmt;
22133   table->in_use = true;
22134 }
22135
22136 /* Record the beginning of a new source file.  */
22137
22138 static void
22139 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
22140 {
22141   if (flag_eliminate_dwarf2_dups)
22142     {
22143       /* Record the beginning of the file for break_out_includes.  */
22144       dw_die_ref bincl_die;
22145
22146       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
22147       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
22148     }
22149
22150   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22151     {
22152       macinfo_entry e;
22153       e.code = DW_MACINFO_start_file;
22154       e.lineno = lineno;
22155       e.info = ggc_strdup (filename);
22156       vec_safe_push (macinfo_table, e);
22157     }
22158 }
22159
22160 /* Record the end of a source file.  */
22161
22162 static void
22163 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
22164 {
22165   if (flag_eliminate_dwarf2_dups)
22166     /* Record the end of the file for break_out_includes.  */
22167     new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
22168
22169   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22170     {
22171       macinfo_entry e;
22172       e.code = DW_MACINFO_end_file;
22173       e.lineno = lineno;
22174       e.info = NULL;
22175       vec_safe_push (macinfo_table, e);
22176     }
22177 }
22178
22179 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
22180    the tail part of the directive line, i.e. the part which is past the
22181    initial whitespace, #, whitespace, directive-name, whitespace part.  */
22182
22183 static void
22184 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
22185                   const char *buffer ATTRIBUTE_UNUSED)
22186 {
22187   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22188     {
22189       macinfo_entry e;
22190       /* Insert a dummy first entry to be able to optimize the whole
22191          predefined macro block using DW_MACRO_GNU_transparent_include.  */
22192       if (macinfo_table->is_empty () && lineno <= 1)
22193         {
22194           e.code = 0;
22195           e.lineno = 0;
22196           e.info = NULL;
22197           vec_safe_push (macinfo_table, e);
22198         }
22199       e.code = DW_MACINFO_define;
22200       e.lineno = lineno;
22201       e.info = ggc_strdup (buffer);
22202       vec_safe_push (macinfo_table, e);
22203     }
22204 }
22205
22206 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
22207    the tail part of the directive line, i.e. the part which is past the
22208    initial whitespace, #, whitespace, directive-name, whitespace part.  */
22209
22210 static void
22211 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
22212                  const char *buffer ATTRIBUTE_UNUSED)
22213 {
22214   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22215     {
22216       macinfo_entry e;
22217       /* Insert a dummy first entry to be able to optimize the whole
22218          predefined macro block using DW_MACRO_GNU_transparent_include.  */
22219       if (macinfo_table->is_empty () && lineno <= 1)
22220         {
22221           e.code = 0;
22222           e.lineno = 0;
22223           e.info = NULL;
22224           vec_safe_push (macinfo_table, e);
22225         }
22226       e.code = DW_MACINFO_undef;
22227       e.lineno = lineno;
22228       e.info = ggc_strdup (buffer);
22229       vec_safe_push (macinfo_table, e);
22230     }
22231 }
22232
22233 /* Helpers to manipulate hash table of CUs.  */
22234
22235 struct macinfo_entry_hasher : typed_noop_remove <macinfo_entry>
22236 {
22237   typedef macinfo_entry *value_type;
22238   typedef macinfo_entry *compare_type;
22239   static inline hashval_t hash (const macinfo_entry *);
22240   static inline bool equal (const macinfo_entry *, const macinfo_entry *);
22241 };
22242
22243 inline hashval_t
22244 macinfo_entry_hasher::hash (const macinfo_entry *entry)
22245 {
22246   return htab_hash_string (entry->info);
22247 }
22248
22249 inline bool
22250 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
22251                              const macinfo_entry *entry2)
22252 {
22253   return !strcmp (entry1->info, entry2->info);
22254 }
22255
22256 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
22257
22258 /* Output a single .debug_macinfo entry.  */
22259
22260 static void
22261 output_macinfo_op (macinfo_entry *ref)
22262 {
22263   int file_num;
22264   size_t len;
22265   struct indirect_string_node *node;
22266   char label[MAX_ARTIFICIAL_LABEL_BYTES];
22267   struct dwarf_file_data *fd;
22268
22269   switch (ref->code)
22270     {
22271     case DW_MACINFO_start_file:
22272       fd = lookup_filename (ref->info);
22273       file_num = maybe_emit_file (fd);
22274       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
22275       dw2_asm_output_data_uleb128 (ref->lineno,
22276                                    "Included from line number %lu", 
22277                                    (unsigned long) ref->lineno);
22278       dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
22279       break;
22280     case DW_MACINFO_end_file:
22281       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
22282       break;
22283     case DW_MACINFO_define:
22284     case DW_MACINFO_undef:
22285       len = strlen (ref->info) + 1;
22286       if (!dwarf_strict
22287           && len > DWARF_OFFSET_SIZE
22288           && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
22289           && (debug_str_section->common.flags & SECTION_MERGE) != 0)
22290         {
22291           ref->code = ref->code == DW_MACINFO_define
22292                       ? DW_MACRO_GNU_define_indirect
22293                       : DW_MACRO_GNU_undef_indirect;
22294           output_macinfo_op (ref);
22295           return;
22296         }
22297       dw2_asm_output_data (1, ref->code,
22298                            ref->code == DW_MACINFO_define
22299                            ? "Define macro" : "Undefine macro");
22300       dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu", 
22301                                    (unsigned long) ref->lineno);
22302       dw2_asm_output_nstring (ref->info, -1, "The macro");
22303       break;
22304     case DW_MACRO_GNU_define_indirect:
22305     case DW_MACRO_GNU_undef_indirect:
22306       node = find_AT_string (ref->info);
22307       gcc_assert (node
22308                   && ((node->form == DW_FORM_strp)
22309                       || (node->form == DW_FORM_GNU_str_index)));
22310       dw2_asm_output_data (1, ref->code,
22311                            ref->code == DW_MACRO_GNU_define_indirect
22312                            ? "Define macro indirect"
22313                            : "Undefine macro indirect");
22314       dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22315                                    (unsigned long) ref->lineno);
22316       if (node->form == DW_FORM_strp)
22317         dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
22318                                debug_str_section, "The macro: \"%s\"",
22319                                ref->info);
22320       else
22321         dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
22322                                      ref->info);
22323       break;
22324     case DW_MACRO_GNU_transparent_include:
22325       dw2_asm_output_data (1, ref->code, "Transparent include");
22326       ASM_GENERATE_INTERNAL_LABEL (label,
22327                                    DEBUG_MACRO_SECTION_LABEL, ref->lineno);
22328       dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
22329       break;
22330     default:
22331       fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
22332                ASM_COMMENT_START, (unsigned long) ref->code);
22333       break;
22334     }
22335 }
22336
22337 /* Attempt to make a sequence of define/undef macinfo ops shareable with
22338    other compilation unit .debug_macinfo sections.  IDX is the first
22339    index of a define/undef, return the number of ops that should be
22340    emitted in a comdat .debug_macinfo section and emit
22341    a DW_MACRO_GNU_transparent_include entry referencing it.
22342    If the define/undef entry should be emitted normally, return 0.  */
22343
22344 static unsigned
22345 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
22346                         macinfo_hash_type **macinfo_htab)
22347 {
22348   macinfo_entry *first, *second, *cur, *inc;
22349   char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
22350   unsigned char checksum[16];
22351   struct md5_ctx ctx;
22352   char *grp_name, *tail;
22353   const char *base;
22354   unsigned int i, count, encoded_filename_len, linebuf_len;
22355   macinfo_entry **slot;
22356
22357   first = &(*macinfo_table)[idx];
22358   second = &(*macinfo_table)[idx + 1];
22359
22360   /* Optimize only if there are at least two consecutive define/undef ops,
22361      and either all of them are before first DW_MACINFO_start_file
22362      with lineno {0,1} (i.e. predefined macro block), or all of them are
22363      in some included header file.  */
22364   if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
22365     return 0;
22366   if (vec_safe_is_empty (files))
22367     {
22368       if (first->lineno > 1 || second->lineno > 1)
22369         return 0;
22370     }
22371   else if (first->lineno == 0)
22372     return 0;
22373
22374   /* Find the last define/undef entry that can be grouped together
22375      with first and at the same time compute md5 checksum of their
22376      codes, linenumbers and strings.  */
22377   md5_init_ctx (&ctx);
22378   for (i = idx; macinfo_table->iterate (i, &cur); i++)
22379     if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
22380       break;
22381     else if (vec_safe_is_empty (files) && cur->lineno > 1)
22382       break;
22383     else
22384       {
22385         unsigned char code = cur->code;
22386         md5_process_bytes (&code, 1, &ctx);
22387         checksum_uleb128 (cur->lineno, &ctx);
22388         md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
22389       }
22390   md5_finish_ctx (&ctx, checksum);
22391   count = i - idx;
22392
22393   /* From the containing include filename (if any) pick up just
22394      usable characters from its basename.  */
22395   if (vec_safe_is_empty (files))
22396     base = "";
22397   else
22398     base = lbasename (files->last ().info);
22399   for (encoded_filename_len = 0, i = 0; base[i]; i++)
22400     if (ISIDNUM (base[i]) || base[i] == '.')
22401       encoded_filename_len++;
22402   /* Count . at the end.  */
22403   if (encoded_filename_len)
22404     encoded_filename_len++;
22405
22406   sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
22407   linebuf_len = strlen (linebuf);
22408
22409   /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum>  */
22410   grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
22411                          + 16 * 2 + 1);
22412   memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
22413   tail = grp_name + 4;
22414   if (encoded_filename_len)
22415     {
22416       for (i = 0; base[i]; i++)
22417         if (ISIDNUM (base[i]) || base[i] == '.')
22418           *tail++ = base[i];
22419       *tail++ = '.';
22420     }
22421   memcpy (tail, linebuf, linebuf_len);
22422   tail += linebuf_len;
22423   *tail++ = '.';
22424   for (i = 0; i < 16; i++)
22425     sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
22426
22427   /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
22428      in the empty vector entry before the first define/undef.  */
22429   inc = &(*macinfo_table)[idx - 1];
22430   inc->code = DW_MACRO_GNU_transparent_include;
22431   inc->lineno = 0;
22432   inc->info = ggc_strdup (grp_name);
22433   if (!*macinfo_htab)
22434     *macinfo_htab = new macinfo_hash_type (10);
22435   /* Avoid emitting duplicates.  */
22436   slot = (*macinfo_htab)->find_slot (inc, INSERT);
22437   if (*slot != NULL)
22438     {
22439       inc->code = 0;
22440       inc->info = NULL;
22441       /* If such an entry has been used before, just emit
22442          a DW_MACRO_GNU_transparent_include op.  */
22443       inc = *slot;
22444       output_macinfo_op (inc);
22445       /* And clear all macinfo_entry in the range to avoid emitting them
22446          in the second pass.  */
22447       for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
22448         {
22449           cur->code = 0;
22450           cur->info = NULL;
22451         }
22452     }
22453   else
22454     {
22455       *slot = inc;
22456       inc->lineno = (*macinfo_htab)->elements ();
22457       output_macinfo_op (inc);
22458     }
22459   return count;
22460 }
22461
22462 /* Save any strings needed by the macinfo table in the debug str
22463    table.  All strings must be collected into the table by the time
22464    index_string is called.  */
22465
22466 static void
22467 save_macinfo_strings (void)
22468 {
22469   unsigned len;
22470   unsigned i;
22471   macinfo_entry *ref;
22472
22473   for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
22474     {
22475       switch (ref->code)
22476         {
22477           /* Match the logic in output_macinfo_op to decide on
22478              indirect strings.  */
22479           case DW_MACINFO_define:
22480           case DW_MACINFO_undef:
22481             len = strlen (ref->info) + 1;
22482             if (!dwarf_strict
22483                 && len > DWARF_OFFSET_SIZE
22484                 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
22485                 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
22486               set_indirect_string (find_AT_string (ref->info));
22487             break;
22488           case DW_MACRO_GNU_define_indirect:
22489           case DW_MACRO_GNU_undef_indirect:
22490             set_indirect_string (find_AT_string (ref->info));
22491             break;
22492           default:
22493             break;
22494         }
22495     }
22496 }
22497
22498 /* Output macinfo section(s).  */
22499
22500 static void
22501 output_macinfo (void)
22502 {
22503   unsigned i;
22504   unsigned long length = vec_safe_length (macinfo_table);
22505   macinfo_entry *ref;
22506   vec<macinfo_entry, va_gc> *files = NULL;
22507   macinfo_hash_type *macinfo_htab = NULL;
22508
22509   if (! length)
22510     return;
22511
22512   /* output_macinfo* uses these interchangeably.  */
22513   gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
22514               && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
22515               && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
22516               && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
22517
22518   /* For .debug_macro emit the section header.  */
22519   if (!dwarf_strict)
22520     {
22521       dw2_asm_output_data (2, 4, "DWARF macro version number");
22522       if (DWARF_OFFSET_SIZE == 8)
22523         dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
22524       else
22525         dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
22526       dw2_asm_output_offset (DWARF_OFFSET_SIZE,
22527                              (!dwarf_split_debug_info ? debug_line_section_label
22528                               : debug_skeleton_line_section_label),
22529                              debug_line_section, NULL);
22530     }
22531
22532   /* In the first loop, it emits the primary .debug_macinfo section
22533      and after each emitted op the macinfo_entry is cleared.
22534      If a longer range of define/undef ops can be optimized using
22535      DW_MACRO_GNU_transparent_include, the
22536      DW_MACRO_GNU_transparent_include op is emitted and kept in
22537      the vector before the first define/undef in the range and the
22538      whole range of define/undef ops is not emitted and kept.  */
22539   for (i = 0; macinfo_table->iterate (i, &ref); i++)
22540     {
22541       switch (ref->code)
22542         {
22543         case DW_MACINFO_start_file:
22544           vec_safe_push (files, *ref);
22545           break;
22546         case DW_MACINFO_end_file:
22547           if (!vec_safe_is_empty (files))
22548             files->pop ();
22549           break;
22550         case DW_MACINFO_define:
22551         case DW_MACINFO_undef:
22552           if (!dwarf_strict
22553               && HAVE_COMDAT_GROUP
22554               && vec_safe_length (files) != 1
22555               && i > 0
22556               && i + 1 < length
22557               && (*macinfo_table)[i - 1].code == 0)
22558             {
22559               unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
22560               if (count)
22561                 {
22562                   i += count - 1;
22563                   continue;
22564                 }
22565             }
22566           break;
22567         case 0:
22568           /* A dummy entry may be inserted at the beginning to be able
22569              to optimize the whole block of predefined macros.  */
22570           if (i == 0)
22571             continue;
22572         default:
22573           break;
22574         }
22575       output_macinfo_op (ref);
22576       ref->info = NULL;
22577       ref->code = 0;
22578     }
22579
22580   if (!macinfo_htab)
22581     return;
22582
22583   delete macinfo_htab;
22584   macinfo_htab = NULL;
22585
22586   /* If any DW_MACRO_GNU_transparent_include were used, on those
22587      DW_MACRO_GNU_transparent_include entries terminate the
22588      current chain and switch to a new comdat .debug_macinfo
22589      section and emit the define/undef entries within it.  */
22590   for (i = 0; macinfo_table->iterate (i, &ref); i++)
22591     switch (ref->code)
22592       {
22593       case 0:
22594         continue;
22595       case DW_MACRO_GNU_transparent_include:
22596         {
22597           char label[MAX_ARTIFICIAL_LABEL_BYTES];
22598           tree comdat_key = get_identifier (ref->info);
22599           /* Terminate the previous .debug_macinfo section.  */
22600           dw2_asm_output_data (1, 0, "End compilation unit");
22601           targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
22602                                          SECTION_DEBUG
22603                                          | SECTION_LINKONCE,
22604                                          comdat_key);
22605           ASM_GENERATE_INTERNAL_LABEL (label,
22606                                        DEBUG_MACRO_SECTION_LABEL,
22607                                        ref->lineno);
22608           ASM_OUTPUT_LABEL (asm_out_file, label);
22609           ref->code = 0;
22610           ref->info = NULL;
22611           dw2_asm_output_data (2, 4, "DWARF macro version number");
22612           if (DWARF_OFFSET_SIZE == 8)
22613             dw2_asm_output_data (1, 1, "Flags: 64-bit");
22614           else
22615             dw2_asm_output_data (1, 0, "Flags: 32-bit");
22616         }
22617         break;
22618       case DW_MACINFO_define:
22619       case DW_MACINFO_undef:
22620         output_macinfo_op (ref);
22621         ref->code = 0;
22622         ref->info = NULL;
22623         break;
22624       default:
22625         gcc_unreachable ();
22626       }
22627 }
22628
22629 /* Set up for Dwarf output at the start of compilation.  */
22630
22631 static void
22632 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
22633 {
22634   /* This option is currently broken, see (PR53118 and PR46102).  */
22635   if (flag_eliminate_dwarf2_dups
22636       && strstr (lang_hooks.name, "C++"))
22637     {
22638       warning (0, "-feliminate-dwarf2-dups is broken for C++, ignoring");
22639       flag_eliminate_dwarf2_dups = 0;
22640     }
22641
22642   /* Allocate the file_table.  */
22643   file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
22644
22645   /* Allocate the decl_die_table.  */
22646   decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
22647
22648   /* Allocate the decl_loc_table.  */
22649   decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
22650
22651   /* Allocate the cached_dw_loc_list_table.  */
22652   cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
22653
22654   /* Allocate the initial hunk of the decl_scope_table.  */
22655   vec_alloc (decl_scope_table, 256);
22656
22657   /* Allocate the initial hunk of the abbrev_die_table.  */
22658   abbrev_die_table = ggc_cleared_vec_alloc<dw_die_ref>
22659     (ABBREV_DIE_TABLE_INCREMENT);
22660   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
22661   /* Zero-th entry is allocated, but unused.  */
22662   abbrev_die_table_in_use = 1;
22663
22664   /* Allocate the pubtypes and pubnames vectors.  */
22665   vec_alloc (pubname_table, 32);
22666   vec_alloc (pubtype_table, 32);
22667
22668   vec_alloc (incomplete_types, 64);
22669
22670   vec_alloc (used_rtx_array, 32);
22671
22672   if (!dwarf_split_debug_info)
22673     {
22674       debug_info_section = get_section (DEBUG_INFO_SECTION,
22675                                         SECTION_DEBUG, NULL);
22676       debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22677                                           SECTION_DEBUG, NULL);
22678       debug_loc_section = get_section (DEBUG_LOC_SECTION,
22679                                        SECTION_DEBUG, NULL);
22680     }
22681   else
22682     {
22683       debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
22684                                         SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22685       debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
22686                                           SECTION_DEBUG | SECTION_EXCLUDE,
22687                                           NULL);
22688       debug_addr_section = get_section (DEBUG_ADDR_SECTION,
22689                                         SECTION_DEBUG, NULL);
22690       debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
22691                                                  SECTION_DEBUG, NULL);
22692       debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22693                                                    SECTION_DEBUG, NULL);
22694       ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
22695                                   DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
22696
22697       /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
22698          the main .o, but the skeleton_line goes into the split off dwo.  */
22699       debug_skeleton_line_section
22700           = get_section (DEBUG_DWO_LINE_SECTION,
22701                          SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22702       ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
22703                                    DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
22704       debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
22705                                                SECTION_DEBUG | SECTION_EXCLUDE,
22706                                                NULL);
22707       ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
22708                                    DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
22709       debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
22710                                        SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22711       debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
22712                                            DEBUG_STR_DWO_SECTION_FLAGS, NULL);
22713     }
22714   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
22715                                        SECTION_DEBUG, NULL);
22716   debug_macinfo_section = get_section (dwarf_strict
22717                                        ? DEBUG_MACINFO_SECTION
22718                                        : DEBUG_MACRO_SECTION,
22719                                        DEBUG_MACRO_SECTION_FLAGS, NULL);
22720   debug_line_section = get_section (DEBUG_LINE_SECTION,
22721                                     SECTION_DEBUG, NULL);
22722   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
22723                                         SECTION_DEBUG, NULL);
22724   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
22725                                         SECTION_DEBUG, NULL);
22726   debug_str_section = get_section (DEBUG_STR_SECTION,
22727                                    DEBUG_STR_SECTION_FLAGS, NULL);
22728   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
22729                                       SECTION_DEBUG, NULL);
22730   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
22731                                      SECTION_DEBUG, NULL);
22732
22733   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
22734   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
22735                                DEBUG_ABBREV_SECTION_LABEL, 0);
22736   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
22737   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
22738                                COLD_TEXT_SECTION_LABEL, 0);
22739   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
22740
22741   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
22742                                DEBUG_INFO_SECTION_LABEL, 0);
22743   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
22744                                DEBUG_LINE_SECTION_LABEL, 0);
22745   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
22746                                DEBUG_RANGES_SECTION_LABEL, 0);
22747   ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
22748                                DEBUG_ADDR_SECTION_LABEL, 0);
22749   ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
22750                                dwarf_strict
22751                                ? DEBUG_MACINFO_SECTION_LABEL
22752                                : DEBUG_MACRO_SECTION_LABEL, 0);
22753   ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
22754
22755   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22756     vec_alloc (macinfo_table, 64);
22757
22758   switch_to_section (text_section);
22759   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
22760
22761   /* Make sure the line number table for .text always exists.  */
22762   text_section_line_info = new_line_info_table ();
22763   text_section_line_info->end_label = text_end_label;
22764 }
22765
22766 /* Called before compile () starts outputtting functions, variables
22767    and toplevel asms into assembly.  */
22768
22769 static void
22770 dwarf2out_assembly_start (void)
22771 {
22772   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
22773       && dwarf2out_do_cfi_asm ()
22774       && (!(flag_unwind_tables || flag_exceptions)
22775           || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
22776     fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
22777 }
22778
22779 /* A helper function for dwarf2out_finish called through
22780    htab_traverse.  Assign a string its index.  All strings must be
22781    collected into the table by the time index_string is called,
22782    because the indexing code relies on htab_traverse to traverse nodes
22783    in the same order for each run. */
22784
22785 int
22786 index_string (indirect_string_node **h, unsigned int *index)
22787 {
22788   indirect_string_node *node = *h;
22789
22790   find_string_form (node);
22791   if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22792     {
22793       gcc_assert (node->index == NO_INDEX_ASSIGNED);
22794       node->index = *index;
22795       *index += 1;
22796     }
22797   return 1;
22798 }
22799
22800 /* A helper function for output_indirect_strings called through
22801    htab_traverse.  Output the offset to a string and update the
22802    current offset.  */
22803
22804 int
22805 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
22806 {
22807   indirect_string_node *node = *h;
22808
22809   if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22810     {
22811       /* Assert that this node has been assigned an index.  */
22812       gcc_assert (node->index != NO_INDEX_ASSIGNED
22813                   && node->index != NOT_INDEXED);
22814       dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
22815                            "indexed string 0x%x: %s", node->index, node->str);
22816       *offset += strlen (node->str) + 1;
22817     }
22818   return 1;
22819 }
22820
22821 /* A helper function for dwarf2out_finish called through
22822    htab_traverse.  Output the indexed string.  */
22823
22824 int
22825 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
22826 {
22827   struct indirect_string_node *node = *h;
22828
22829   if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22830     {
22831       /* Assert that the strings are output in the same order as their
22832          indexes were assigned.  */
22833       gcc_assert (*cur_idx == node->index);
22834       assemble_string (node->str, strlen (node->str) + 1);
22835       *cur_idx += 1;
22836     }
22837   return 1;
22838 }
22839
22840 /* A helper function for dwarf2out_finish called through
22841    htab_traverse.  Emit one queued .debug_str string.  */
22842
22843 int
22844 output_indirect_string (indirect_string_node **h, void *)
22845 {
22846   struct indirect_string_node *node = *h;
22847
22848   node->form = find_string_form (node);
22849   if (node->form == DW_FORM_strp && node->refcount > 0)
22850     {
22851       ASM_OUTPUT_LABEL (asm_out_file, node->label);
22852       assemble_string (node->str, strlen (node->str) + 1);
22853     }
22854
22855   return 1;
22856 }
22857
22858 /* Output the indexed string table.  */
22859
22860 static void
22861 output_indirect_strings (void)
22862 {
22863   switch_to_section (debug_str_section);
22864   if (!dwarf_split_debug_info)
22865     debug_str_hash->traverse<void *, output_indirect_string> (NULL);
22866   else
22867     {
22868       unsigned int offset = 0;
22869       unsigned int cur_idx = 0;
22870
22871       skeleton_debug_str_hash->traverse<void *, output_indirect_string> (NULL);
22872
22873       switch_to_section (debug_str_offsets_section);
22874       debug_str_hash->traverse_noresize
22875         <unsigned int *, output_index_string_offset> (&offset);
22876       switch_to_section (debug_str_dwo_section);
22877       debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
22878         (&cur_idx);
22879     }
22880 }
22881
22882 /* Callback for htab_traverse to assign an index to an entry in the
22883    table, and to write that entry to the .debug_addr section.  */
22884
22885 int
22886 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
22887 {
22888   addr_table_entry *entry = *slot;
22889
22890   if (entry->refcount == 0)
22891     {
22892       gcc_assert (entry->index == NO_INDEX_ASSIGNED
22893                   || entry->index == NOT_INDEXED);
22894       return 1;
22895     }
22896
22897   gcc_assert (entry->index == *cur_index);
22898   (*cur_index)++;
22899
22900   switch (entry->kind)
22901     {
22902       case ate_kind_rtx:
22903         dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
22904                                  "0x%x", entry->index);
22905         break;
22906       case ate_kind_rtx_dtprel:
22907         gcc_assert (targetm.asm_out.output_dwarf_dtprel);
22908         targetm.asm_out.output_dwarf_dtprel (asm_out_file,
22909                                              DWARF2_ADDR_SIZE,
22910                                              entry->addr.rtl);
22911         fputc ('\n', asm_out_file);
22912         break;
22913       case ate_kind_label:
22914         dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
22915                                  "0x%x", entry->index);
22916         break;
22917       default:
22918         gcc_unreachable ();
22919     }
22920   return 1;
22921 }
22922
22923 /* Produce the .debug_addr section.  */
22924
22925 static void
22926 output_addr_table (void)
22927 {
22928   unsigned int index = 0;
22929   if (addr_index_table == NULL || addr_index_table->size () == 0)
22930     return;
22931
22932   switch_to_section (debug_addr_section);
22933   addr_index_table
22934     ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
22935 }
22936
22937 #if ENABLE_ASSERT_CHECKING
22938 /* Verify that all marks are clear.  */
22939
22940 static void
22941 verify_marks_clear (dw_die_ref die)
22942 {
22943   dw_die_ref c;
22944
22945   gcc_assert (! die->die_mark);
22946   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22947 }
22948 #endif /* ENABLE_ASSERT_CHECKING */
22949
22950 /* Clear the marks for a die and its children.
22951    Be cool if the mark isn't set.  */
22952
22953 static void
22954 prune_unmark_dies (dw_die_ref die)
22955 {
22956   dw_die_ref c;
22957
22958   if (die->die_mark)
22959     die->die_mark = 0;
22960   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22961 }
22962
22963 /* Given DIE that we're marking as used, find any other dies
22964    it references as attributes and mark them as used.  */
22965
22966 static void
22967 prune_unused_types_walk_attribs (dw_die_ref die)
22968 {
22969   dw_attr_ref a;
22970   unsigned ix;
22971
22972   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22973     {
22974       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22975         {
22976           /* A reference to another DIE.
22977              Make sure that it will get emitted.
22978              If it was broken out into a comdat group, don't follow it.  */
22979           if (! AT_ref (a)->comdat_type_p
22980               || a->dw_attr == DW_AT_specification)
22981             prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22982         }
22983       /* Set the string's refcount to 0 so that prune_unused_types_mark
22984          accounts properly for it.  */
22985       if (AT_class (a) == dw_val_class_str)
22986         a->dw_attr_val.v.val_str->refcount = 0;
22987     }
22988 }
22989
22990 /* Mark the generic parameters and arguments children DIEs of DIE.  */
22991
22992 static void
22993 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
22994 {
22995   dw_die_ref c;
22996
22997   if (die == NULL || die->die_child == NULL)
22998     return;
22999   c = die->die_child;
23000   do
23001     {
23002       if (is_template_parameter (c))
23003         prune_unused_types_mark (c, 1);
23004       c = c->die_sib;
23005     } while (c && c != die->die_child);
23006 }
23007
23008 /* Mark DIE as being used.  If DOKIDS is true, then walk down
23009    to DIE's children.  */
23010
23011 static void
23012 prune_unused_types_mark (dw_die_ref die, int dokids)
23013 {
23014   dw_die_ref c;
23015
23016   if (die->die_mark == 0)
23017     {
23018       /* We haven't done this node yet.  Mark it as used.  */
23019       die->die_mark = 1;
23020       /* If this is the DIE of a generic type instantiation,
23021          mark the children DIEs that describe its generic parms and
23022          args.  */
23023       prune_unused_types_mark_generic_parms_dies (die);
23024
23025       /* We also have to mark its parents as used.
23026          (But we don't want to mark our parent's kids due to this,
23027          unless it is a class.)  */
23028       if (die->die_parent)
23029         prune_unused_types_mark (die->die_parent,
23030                                  class_scope_p (die->die_parent));
23031
23032       /* Mark any referenced nodes.  */
23033       prune_unused_types_walk_attribs (die);
23034
23035       /* If this node is a specification,
23036          also mark the definition, if it exists.  */
23037       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
23038         prune_unused_types_mark (die->die_definition, 1);
23039     }
23040
23041   if (dokids && die->die_mark != 2)
23042     {
23043       /* We need to walk the children, but haven't done so yet.
23044          Remember that we've walked the kids.  */
23045       die->die_mark = 2;
23046
23047       /* If this is an array type, we need to make sure our
23048          kids get marked, even if they're types.  If we're
23049          breaking out types into comdat sections, do this
23050          for all type definitions.  */
23051       if (die->die_tag == DW_TAG_array_type
23052           || (use_debug_types
23053               && is_type_die (die) && ! is_declaration_die (die)))
23054         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
23055       else
23056         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
23057     }
23058 }
23059
23060 /* For local classes, look if any static member functions were emitted
23061    and if so, mark them.  */
23062
23063 static void
23064 prune_unused_types_walk_local_classes (dw_die_ref die)
23065 {
23066   dw_die_ref c;
23067
23068   if (die->die_mark == 2)
23069     return;
23070
23071   switch (die->die_tag)
23072     {
23073     case DW_TAG_structure_type:
23074     case DW_TAG_union_type:
23075     case DW_TAG_class_type:
23076       break;
23077
23078     case DW_TAG_subprogram:
23079       if (!get_AT_flag (die, DW_AT_declaration)
23080           || die->die_definition != NULL)
23081         prune_unused_types_mark (die, 1);
23082       return;
23083
23084     default:
23085       return;
23086     }
23087
23088   /* Mark children.  */
23089   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
23090 }
23091
23092 /* Walk the tree DIE and mark types that we actually use.  */
23093
23094 static void
23095 prune_unused_types_walk (dw_die_ref die)
23096 {
23097   dw_die_ref c;
23098
23099   /* Don't do anything if this node is already marked and
23100      children have been marked as well.  */
23101   if (die->die_mark == 2)
23102     return;
23103
23104   switch (die->die_tag)
23105     {
23106     case DW_TAG_structure_type:
23107     case DW_TAG_union_type:
23108     case DW_TAG_class_type:
23109       if (die->die_perennial_p)
23110         break;
23111
23112       for (c = die->die_parent; c; c = c->die_parent)
23113         if (c->die_tag == DW_TAG_subprogram)
23114           break;
23115
23116       /* Finding used static member functions inside of classes
23117          is needed just for local classes, because for other classes
23118          static member function DIEs with DW_AT_specification
23119          are emitted outside of the DW_TAG_*_type.  If we ever change
23120          it, we'd need to call this even for non-local classes.  */
23121       if (c)
23122         prune_unused_types_walk_local_classes (die);
23123
23124       /* It's a type node --- don't mark it.  */
23125       return;
23126
23127     case DW_TAG_const_type:
23128     case DW_TAG_packed_type:
23129     case DW_TAG_pointer_type:
23130     case DW_TAG_reference_type:
23131     case DW_TAG_rvalue_reference_type:
23132     case DW_TAG_volatile_type:
23133     case DW_TAG_typedef:
23134     case DW_TAG_array_type:
23135     case DW_TAG_interface_type:
23136     case DW_TAG_friend:
23137     case DW_TAG_variant_part:
23138     case DW_TAG_enumeration_type:
23139     case DW_TAG_subroutine_type:
23140     case DW_TAG_string_type:
23141     case DW_TAG_set_type:
23142     case DW_TAG_subrange_type:
23143     case DW_TAG_ptr_to_member_type:
23144     case DW_TAG_file_type:
23145       if (die->die_perennial_p)
23146         break;
23147
23148       /* It's a type node --- don't mark it.  */
23149       return;
23150
23151     default:
23152       /* Mark everything else.  */
23153       break;
23154   }
23155
23156   if (die->die_mark == 0)
23157     {
23158       die->die_mark = 1;
23159
23160       /* Now, mark any dies referenced from here.  */
23161       prune_unused_types_walk_attribs (die);
23162     }
23163
23164   die->die_mark = 2;
23165
23166   /* Mark children.  */
23167   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
23168 }
23169
23170 /* Increment the string counts on strings referred to from DIE's
23171    attributes.  */
23172
23173 static void
23174 prune_unused_types_update_strings (dw_die_ref die)
23175 {
23176   dw_attr_ref a;
23177   unsigned ix;
23178
23179   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23180     if (AT_class (a) == dw_val_class_str)
23181       {
23182         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
23183         s->refcount++;
23184         /* Avoid unnecessarily putting strings that are used less than
23185            twice in the hash table.  */
23186         if (s->refcount
23187             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
23188           {
23189             indirect_string_node **slot
23190               = debug_str_hash->find_slot_with_hash (s->str,
23191                                                      htab_hash_string (s->str),
23192                                                      INSERT);
23193             gcc_assert (*slot == NULL);
23194             *slot = s;
23195           }
23196       }
23197 }
23198
23199 /* Remove from the tree DIE any dies that aren't marked.  */
23200
23201 static void
23202 prune_unused_types_prune (dw_die_ref die)
23203 {
23204   dw_die_ref c;
23205
23206   gcc_assert (die->die_mark);
23207   prune_unused_types_update_strings (die);
23208
23209   if (! die->die_child)
23210     return;
23211
23212   c = die->die_child;
23213   do {
23214     dw_die_ref prev = c;
23215     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
23216       if (c == die->die_child)
23217         {
23218           /* No marked children between 'prev' and the end of the list.  */
23219           if (prev == c)
23220             /* No marked children at all.  */
23221             die->die_child = NULL;
23222           else
23223             {
23224               prev->die_sib = c->die_sib;
23225               die->die_child = prev;
23226             }
23227           return;
23228         }
23229
23230     if (c != prev->die_sib)
23231       prev->die_sib = c;
23232     prune_unused_types_prune (c);
23233   } while (c != die->die_child);
23234 }
23235
23236 /* Remove dies representing declarations that we never use.  */
23237
23238 static void
23239 prune_unused_types (void)
23240 {
23241   unsigned int i;
23242   limbo_die_node *node;
23243   comdat_type_node *ctnode;
23244   pubname_ref pub;
23245   dw_die_ref base_type;
23246
23247 #if ENABLE_ASSERT_CHECKING
23248   /* All the marks should already be clear.  */
23249   verify_marks_clear (comp_unit_die ());
23250   for (node = limbo_die_list; node; node = node->next)
23251     verify_marks_clear (node->die);
23252   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23253     verify_marks_clear (ctnode->root_die);
23254 #endif /* ENABLE_ASSERT_CHECKING */
23255
23256   /* Mark types that are used in global variables.  */
23257   premark_types_used_by_global_vars ();
23258
23259   /* Set the mark on nodes that are actually used.  */
23260   prune_unused_types_walk (comp_unit_die ());
23261   for (node = limbo_die_list; node; node = node->next)
23262     prune_unused_types_walk (node->die);
23263   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23264     {
23265       prune_unused_types_walk (ctnode->root_die);
23266       prune_unused_types_mark (ctnode->type_die, 1);
23267     }
23268
23269   /* Also set the mark on nodes referenced from the pubname_table.  Enumerators
23270      are unusual in that they are pubnames that are the children of pubtypes.
23271      They should only be marked via their parent DW_TAG_enumeration_type die,
23272      not as roots in themselves.  */
23273   FOR_EACH_VEC_ELT (*pubname_table, i, pub)
23274     if (pub->die->die_tag != DW_TAG_enumerator)
23275       prune_unused_types_mark (pub->die, 1);
23276   for (i = 0; base_types.iterate (i, &base_type); i++)
23277     prune_unused_types_mark (base_type, 1);
23278
23279   if (debug_str_hash)
23280     debug_str_hash->empty ();
23281   if (skeleton_debug_str_hash)
23282     skeleton_debug_str_hash->empty ();
23283   prune_unused_types_prune (comp_unit_die ());
23284   for (node = limbo_die_list; node; node = node->next)
23285     prune_unused_types_prune (node->die);
23286   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23287     prune_unused_types_prune (ctnode->root_die);
23288
23289   /* Leave the marks clear.  */
23290   prune_unmark_dies (comp_unit_die ());
23291   for (node = limbo_die_list; node; node = node->next)
23292     prune_unmark_dies (node->die);
23293   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23294     prune_unmark_dies (ctnode->root_die);
23295 }
23296
23297 /* Set the parameter to true if there are any relative pathnames in
23298    the file table.  */
23299 int
23300 file_table_relative_p (dwarf_file_data **slot, bool *p)
23301 {
23302   struct dwarf_file_data *d = *slot;
23303   if (!IS_ABSOLUTE_PATH (d->filename))
23304     {
23305       *p = true;
23306       return 0;
23307     }
23308   return 1;
23309 }
23310
23311 /* Helpers to manipulate hash table of comdat type units.  */
23312
23313 struct comdat_type_hasher : typed_noop_remove <comdat_type_node>
23314 {
23315   typedef comdat_type_node *value_type;
23316   typedef comdat_type_node *compare_type;
23317   static inline hashval_t hash (const comdat_type_node *);
23318   static inline bool equal (const comdat_type_node *, const comdat_type_node *);
23319 };
23320
23321 inline hashval_t
23322 comdat_type_hasher::hash (const comdat_type_node *type_node)
23323 {
23324   hashval_t h;
23325   memcpy (&h, type_node->signature, sizeof (h));
23326   return h;
23327 }
23328
23329 inline bool
23330 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
23331                            const comdat_type_node *type_node_2)
23332 {
23333   return (! memcmp (type_node_1->signature, type_node_2->signature,
23334                     DWARF_TYPE_SIGNATURE_SIZE));
23335 }
23336
23337 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
23338    to the location it would have been added, should we know its
23339    DECL_ASSEMBLER_NAME when we added other attributes.  This will
23340    probably improve compactness of debug info, removing equivalent
23341    abbrevs, and hide any differences caused by deferring the
23342    computation of the assembler name, triggered by e.g. PCH.  */
23343
23344 static inline void
23345 move_linkage_attr (dw_die_ref die)
23346 {
23347   unsigned ix = vec_safe_length (die->die_attr);
23348   dw_attr_node linkage = (*die->die_attr)[ix - 1];
23349
23350   gcc_assert (linkage.dw_attr == DW_AT_linkage_name
23351               || linkage.dw_attr == DW_AT_MIPS_linkage_name);
23352
23353   while (--ix > 0)
23354     {
23355       dw_attr_node *prev = &(*die->die_attr)[ix - 1];
23356
23357       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
23358         break;
23359     }
23360
23361   if (ix != vec_safe_length (die->die_attr) - 1)
23362     {
23363       die->die_attr->pop ();
23364       die->die_attr->quick_insert (ix, linkage);
23365     }
23366 }
23367
23368 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
23369    referenced from typed stack ops and count how often they are used.  */
23370
23371 static void
23372 mark_base_types (dw_loc_descr_ref loc)
23373 {
23374   dw_die_ref base_type = NULL;
23375
23376   for (; loc; loc = loc->dw_loc_next)
23377     {
23378       switch (loc->dw_loc_opc)
23379         {
23380         case DW_OP_GNU_regval_type:
23381         case DW_OP_GNU_deref_type:
23382           base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
23383           break;
23384         case DW_OP_GNU_convert:
23385         case DW_OP_GNU_reinterpret:
23386           if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
23387             continue;
23388           /* FALLTHRU */
23389         case DW_OP_GNU_const_type:
23390           base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
23391           break;
23392         case DW_OP_GNU_entry_value:
23393           mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
23394           continue;
23395         default:
23396           continue;
23397         }
23398       gcc_assert (base_type->die_parent == comp_unit_die ());
23399       if (base_type->die_mark)
23400         base_type->die_mark++;
23401       else
23402         {
23403           base_types.safe_push (base_type);
23404           base_type->die_mark = 1;
23405         }
23406     }
23407 }
23408
23409 /* Comparison function for sorting marked base types.  */
23410
23411 static int
23412 base_type_cmp (const void *x, const void *y)
23413 {
23414   dw_die_ref dx = *(const dw_die_ref *) x;
23415   dw_die_ref dy = *(const dw_die_ref *) y;
23416   unsigned int byte_size1, byte_size2;
23417   unsigned int encoding1, encoding2;
23418   if (dx->die_mark > dy->die_mark)
23419     return -1;
23420   if (dx->die_mark < dy->die_mark)
23421     return 1;
23422   byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
23423   byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
23424   if (byte_size1 < byte_size2)
23425     return 1;
23426   if (byte_size1 > byte_size2)
23427     return -1;
23428   encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
23429   encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
23430   if (encoding1 < encoding2)
23431     return 1;
23432   if (encoding1 > encoding2)
23433     return -1;
23434   return 0;
23435 }
23436
23437 /* Move base types marked by mark_base_types as early as possible
23438    in the CU, sorted by decreasing usage count both to make the
23439    uleb128 references as small as possible and to make sure they
23440    will have die_offset already computed by calc_die_sizes when
23441    sizes of typed stack loc ops is computed.  */
23442
23443 static void
23444 move_marked_base_types (void)
23445 {
23446   unsigned int i;
23447   dw_die_ref base_type, die, c;
23448
23449   if (base_types.is_empty ())
23450     return;
23451
23452   /* Sort by decreasing usage count, they will be added again in that
23453      order later on.  */
23454   base_types.qsort (base_type_cmp);
23455   die = comp_unit_die ();
23456   c = die->die_child;
23457   do
23458     {
23459       dw_die_ref prev = c;
23460       c = c->die_sib;
23461       while (c->die_mark)
23462         {
23463           remove_child_with_prev (c, prev);
23464           /* As base types got marked, there must be at least
23465              one node other than DW_TAG_base_type.  */
23466           gcc_assert (c != c->die_sib);
23467           c = c->die_sib;
23468         }
23469     }
23470   while (c != die->die_child);
23471   gcc_assert (die->die_child);
23472   c = die->die_child;
23473   for (i = 0; base_types.iterate (i, &base_type); i++)
23474     {
23475       base_type->die_mark = 0;
23476       base_type->die_sib = c->die_sib;
23477       c->die_sib = base_type;
23478       c = base_type;
23479     }
23480 }
23481
23482 /* Helper function for resolve_addr, attempt to resolve
23483    one CONST_STRING, return true if successful.  Similarly verify that
23484    SYMBOL_REFs refer to variables emitted in the current CU.  */
23485
23486 static bool
23487 resolve_one_addr (rtx *addr)
23488 {
23489   rtx rtl = *addr;
23490
23491   if (GET_CODE (rtl) == CONST_STRING)
23492     {
23493       size_t len = strlen (XSTR (rtl, 0)) + 1;
23494       tree t = build_string (len, XSTR (rtl, 0));
23495       tree tlen = size_int (len - 1);
23496       TREE_TYPE (t)
23497         = build_array_type (char_type_node, build_index_type (tlen));
23498       rtl = lookup_constant_def (t);
23499       if (!rtl || !MEM_P (rtl))
23500         return false;
23501       rtl = XEXP (rtl, 0);
23502       if (GET_CODE (rtl) == SYMBOL_REF
23503           && SYMBOL_REF_DECL (rtl)
23504           && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23505         return false;
23506       vec_safe_push (used_rtx_array, rtl);
23507       *addr = rtl;
23508       return true;
23509     }
23510
23511   if (GET_CODE (rtl) == SYMBOL_REF
23512       && SYMBOL_REF_DECL (rtl))
23513     {
23514       if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
23515         {
23516           if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
23517             return false;
23518         }
23519       else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23520         return false;
23521     }
23522
23523   if (GET_CODE (rtl) == CONST)
23524     {
23525       subrtx_ptr_iterator::array_type array;
23526       FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
23527         if (!resolve_one_addr (*iter))
23528           return false;
23529     }
23530
23531   return true;
23532 }
23533
23534 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
23535    if possible, and create DW_TAG_dwarf_procedure that can be referenced
23536    from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet.  */
23537
23538 static rtx
23539 string_cst_pool_decl (tree t)
23540 {
23541   rtx rtl = output_constant_def (t, 1);
23542   unsigned char *array;
23543   dw_loc_descr_ref l;
23544   tree decl;
23545   size_t len;
23546   dw_die_ref ref;
23547
23548   if (!rtl || !MEM_P (rtl))
23549     return NULL_RTX;
23550   rtl = XEXP (rtl, 0);
23551   if (GET_CODE (rtl) != SYMBOL_REF
23552       || SYMBOL_REF_DECL (rtl) == NULL_TREE)
23553     return NULL_RTX;
23554
23555   decl = SYMBOL_REF_DECL (rtl);
23556   if (!lookup_decl_die (decl))
23557     {
23558       len = TREE_STRING_LENGTH (t);
23559       vec_safe_push (used_rtx_array, rtl);
23560       ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
23561       array = ggc_vec_alloc<unsigned char> (len);
23562       memcpy (array, TREE_STRING_POINTER (t), len);
23563       l = new_loc_descr (DW_OP_implicit_value, len, 0);
23564       l->dw_loc_oprnd2.val_class = dw_val_class_vec;
23565       l->dw_loc_oprnd2.v.val_vec.length = len;
23566       l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
23567       l->dw_loc_oprnd2.v.val_vec.array = array;
23568       add_AT_loc (ref, DW_AT_location, l);
23569       equate_decl_number_to_die (decl, ref);
23570     }
23571   return rtl;
23572 }
23573
23574 /* Helper function of resolve_addr_in_expr.  LOC is
23575    a DW_OP_addr followed by DW_OP_stack_value, either at the start
23576    of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
23577    resolved.  Replace it (both DW_OP_addr and DW_OP_stack_value)
23578    with DW_OP_GNU_implicit_pointer if possible
23579    and return true, if unsuccessful, return false.  */
23580
23581 static bool
23582 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
23583 {
23584   rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
23585   HOST_WIDE_INT offset = 0;
23586   dw_die_ref ref = NULL;
23587   tree decl;
23588
23589   if (GET_CODE (rtl) == CONST
23590       && GET_CODE (XEXP (rtl, 0)) == PLUS
23591       && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
23592     {
23593       offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
23594       rtl = XEXP (XEXP (rtl, 0), 0);
23595     }
23596   if (GET_CODE (rtl) == CONST_STRING)
23597     {
23598       size_t len = strlen (XSTR (rtl, 0)) + 1;
23599       tree t = build_string (len, XSTR (rtl, 0));
23600       tree tlen = size_int (len - 1);
23601
23602       TREE_TYPE (t)
23603         = build_array_type (char_type_node, build_index_type (tlen));
23604       rtl = string_cst_pool_decl (t);
23605       if (!rtl)
23606         return false;
23607     }
23608   if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
23609     {
23610       decl = SYMBOL_REF_DECL (rtl);
23611       if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
23612         {
23613           ref = lookup_decl_die (decl);
23614           if (ref && (get_AT (ref, DW_AT_location)
23615                       || get_AT (ref, DW_AT_const_value)))
23616             {
23617               loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
23618               loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23619               loc->dw_loc_oprnd1.val_entry = NULL;
23620               loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23621               loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23622               loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23623               loc->dw_loc_oprnd2.v.val_int = offset;
23624               return true;
23625             }
23626         }
23627     }
23628   return false;
23629 }
23630
23631 /* Helper function for resolve_addr, handle one location
23632    expression, return false if at least one CONST_STRING or SYMBOL_REF in
23633    the location list couldn't be resolved.  */
23634
23635 static bool
23636 resolve_addr_in_expr (dw_loc_descr_ref loc)
23637 {
23638   dw_loc_descr_ref keep = NULL;
23639   for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
23640     switch (loc->dw_loc_opc)
23641       {
23642       case DW_OP_addr:
23643         if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
23644           {
23645             if ((prev == NULL
23646                  || prev->dw_loc_opc == DW_OP_piece
23647                  || prev->dw_loc_opc == DW_OP_bit_piece)
23648                 && loc->dw_loc_next
23649                 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
23650                 && !dwarf_strict
23651                 && optimize_one_addr_into_implicit_ptr (loc))
23652               break;
23653             return false;
23654           }
23655         break;
23656       case DW_OP_GNU_addr_index:
23657       case DW_OP_GNU_const_index:
23658         if (loc->dw_loc_opc == DW_OP_GNU_addr_index
23659             || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
23660           {
23661             rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
23662             if (!resolve_one_addr (&rtl))
23663               return false;
23664             remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
23665             loc->dw_loc_oprnd1.val_entry =
23666                 add_addr_table_entry (rtl, ate_kind_rtx);
23667           }
23668         break;
23669       case DW_OP_const4u:
23670       case DW_OP_const8u:
23671         if (loc->dtprel
23672             && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
23673           return false;
23674         break;
23675       case DW_OP_plus_uconst:
23676         if (size_of_loc_descr (loc)
23677             > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
23678               + 1
23679             && loc->dw_loc_oprnd1.v.val_unsigned > 0)
23680           {
23681             dw_loc_descr_ref repl
23682               = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
23683             add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
23684             add_loc_descr (&repl, loc->dw_loc_next);
23685             *loc = *repl;
23686           }
23687         break;
23688       case DW_OP_implicit_value:
23689         if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
23690             && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
23691           return false;
23692         break;
23693       case DW_OP_GNU_implicit_pointer:
23694       case DW_OP_GNU_parameter_ref:
23695         if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
23696           {
23697             dw_die_ref ref
23698               = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
23699             if (ref == NULL)
23700               return false;
23701             loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23702             loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23703             loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23704           }
23705         break;
23706       case DW_OP_GNU_const_type:
23707       case DW_OP_GNU_regval_type:
23708       case DW_OP_GNU_deref_type:
23709       case DW_OP_GNU_convert:
23710       case DW_OP_GNU_reinterpret:
23711         while (loc->dw_loc_next
23712                && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
23713           {
23714             dw_die_ref base1, base2;
23715             unsigned enc1, enc2, size1, size2;
23716             if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23717                 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23718               base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
23719             else if (loc->dw_loc_oprnd1.val_class
23720                      == dw_val_class_unsigned_const)
23721               break;
23722             else
23723               base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
23724             if (loc->dw_loc_next->dw_loc_oprnd1.val_class
23725                 == dw_val_class_unsigned_const)
23726               break;
23727             base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
23728             gcc_assert (base1->die_tag == DW_TAG_base_type
23729                         && base2->die_tag == DW_TAG_base_type);
23730             enc1 = get_AT_unsigned (base1, DW_AT_encoding);
23731             enc2 = get_AT_unsigned (base2, DW_AT_encoding);
23732             size1 = get_AT_unsigned (base1, DW_AT_byte_size);
23733             size2 = get_AT_unsigned (base2, DW_AT_byte_size);
23734             if (size1 == size2
23735                 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
23736                      && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
23737                      && loc != keep)
23738                     || enc1 == enc2))
23739               {
23740                 /* Optimize away next DW_OP_GNU_convert after
23741                    adjusting LOC's base type die reference.  */
23742                 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23743                     || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23744                   loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
23745                 else
23746                   loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
23747                 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23748                 continue;
23749               }
23750             /* Don't change integer DW_OP_GNU_convert after e.g. floating
23751                point typed stack entry.  */
23752             else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
23753               keep = loc->dw_loc_next;
23754             break;
23755           }
23756         break;
23757       default:
23758         break;
23759       }
23760   return true;
23761 }
23762
23763 /* Helper function of resolve_addr.  DIE had DW_AT_location of
23764    DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
23765    and DW_OP_addr couldn't be resolved.  resolve_addr has already
23766    removed the DW_AT_location attribute.  This function attempts to
23767    add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
23768    to it or DW_AT_const_value attribute, if possible.  */
23769
23770 static void
23771 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
23772 {
23773   if (TREE_CODE (decl) != VAR_DECL
23774       || lookup_decl_die (decl) != die
23775       || DECL_EXTERNAL (decl)
23776       || !TREE_STATIC (decl)
23777       || DECL_INITIAL (decl) == NULL_TREE
23778       || DECL_P (DECL_INITIAL (decl))
23779       || get_AT (die, DW_AT_const_value))
23780     return;
23781
23782   tree init = DECL_INITIAL (decl);
23783   HOST_WIDE_INT offset = 0;
23784   /* For variables that have been optimized away and thus
23785      don't have a memory location, see if we can emit
23786      DW_AT_const_value instead.  */
23787   if (tree_add_const_value_attribute (die, init))
23788     return;
23789   if (dwarf_strict)
23790     return;
23791   /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
23792      and ADDR_EXPR refers to a decl that has DW_AT_location or
23793      DW_AT_const_value (but isn't addressable, otherwise
23794      resolving the original DW_OP_addr wouldn't fail), see if
23795      we can add DW_OP_GNU_implicit_pointer.  */
23796   STRIP_NOPS (init);
23797   if (TREE_CODE (init) == POINTER_PLUS_EXPR
23798       && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
23799     {
23800       offset = tree_to_shwi (TREE_OPERAND (init, 1));
23801       init = TREE_OPERAND (init, 0);
23802       STRIP_NOPS (init);
23803     }
23804   if (TREE_CODE (init) != ADDR_EXPR)
23805     return;
23806   if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
23807        && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
23808       || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
23809           && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
23810           && TREE_OPERAND (init, 0) != decl))
23811     {
23812       dw_die_ref ref;
23813       dw_loc_descr_ref l;
23814
23815       if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
23816         {
23817           rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
23818           if (!rtl)
23819             return;
23820           decl = SYMBOL_REF_DECL (rtl);
23821         }
23822       else
23823         decl = TREE_OPERAND (init, 0);
23824       ref = lookup_decl_die (decl);
23825       if (ref == NULL
23826           || (!get_AT (ref, DW_AT_location)
23827               && !get_AT (ref, DW_AT_const_value)))
23828         return;
23829       l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
23830       l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23831       l->dw_loc_oprnd1.v.val_die_ref.die = ref;
23832       l->dw_loc_oprnd1.v.val_die_ref.external = 0;
23833       add_AT_loc (die, DW_AT_location, l);
23834     }
23835 }
23836
23837 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
23838    an address in .rodata section if the string literal is emitted there,
23839    or remove the containing location list or replace DW_AT_const_value
23840    with DW_AT_location and empty location expression, if it isn't found
23841    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
23842    to something that has been emitted in the current CU.  */
23843
23844 static void
23845 resolve_addr (dw_die_ref die)
23846 {
23847   dw_die_ref c;
23848   dw_attr_ref a;
23849   dw_loc_list_ref *curr, *start, loc;
23850   unsigned ix;
23851
23852   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23853     switch (AT_class (a))
23854       {
23855       case dw_val_class_loc_list:
23856         start = curr = AT_loc_list_ptr (a);
23857         loc = *curr;
23858         gcc_assert (loc);
23859         /* The same list can be referenced more than once.  See if we have
23860            already recorded the result from a previous pass.  */
23861         if (loc->replaced)
23862           *curr = loc->dw_loc_next;
23863         else if (!loc->resolved_addr)
23864           {
23865             /* As things stand, we do not expect or allow one die to
23866                reference a suffix of another die's location list chain.
23867                References must be identical or completely separate.
23868                There is therefore no need to cache the result of this
23869                pass on any list other than the first; doing so
23870                would lead to unnecessary writes.  */
23871             while (*curr)
23872               {
23873                 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
23874                 if (!resolve_addr_in_expr ((*curr)->expr))
23875                   {
23876                     dw_loc_list_ref next = (*curr)->dw_loc_next;
23877                     dw_loc_descr_ref l = (*curr)->expr;
23878
23879                     if (next && (*curr)->ll_symbol)
23880                       {
23881                         gcc_assert (!next->ll_symbol);
23882                         next->ll_symbol = (*curr)->ll_symbol;
23883                       }
23884                     if (dwarf_split_debug_info)
23885                       remove_loc_list_addr_table_entries (l);
23886                     *curr = next;
23887                   }
23888                 else
23889                   {
23890                     mark_base_types ((*curr)->expr);
23891                     curr = &(*curr)->dw_loc_next;
23892                   }
23893               }
23894             if (loc == *start)
23895               loc->resolved_addr = 1;
23896             else
23897               {
23898                 loc->replaced = 1;
23899                 loc->dw_loc_next = *start;
23900               }
23901           }
23902         if (!*start)
23903           {
23904             remove_AT (die, a->dw_attr);
23905             ix--;
23906           }
23907         break;
23908       case dw_val_class_loc:
23909         {
23910           dw_loc_descr_ref l = AT_loc (a);
23911           /* For -gdwarf-2 don't attempt to optimize
23912              DW_AT_data_member_location containing
23913              DW_OP_plus_uconst - older consumers might
23914              rely on it being that op instead of a more complex,
23915              but shorter, location description.  */
23916           if ((dwarf_version > 2
23917                || a->dw_attr != DW_AT_data_member_location
23918                || l == NULL
23919                || l->dw_loc_opc != DW_OP_plus_uconst
23920                || l->dw_loc_next != NULL)
23921               && !resolve_addr_in_expr (l))
23922             {
23923               if (dwarf_split_debug_info)
23924                 remove_loc_list_addr_table_entries (l);
23925               if (l != NULL
23926                   && l->dw_loc_next == NULL
23927                   && l->dw_loc_opc == DW_OP_addr
23928                   && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
23929                   && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
23930                   && a->dw_attr == DW_AT_location)
23931                 {
23932                   tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
23933                   remove_AT (die, a->dw_attr);
23934                   ix--;
23935                   optimize_location_into_implicit_ptr (die, decl);
23936                   break;
23937                 }
23938               remove_AT (die, a->dw_attr);
23939               ix--;
23940             }
23941           else
23942             mark_base_types (l);
23943         }
23944         break;
23945       case dw_val_class_addr:
23946         if (a->dw_attr == DW_AT_const_value
23947             && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
23948           {
23949             if (AT_index (a) != NOT_INDEXED)
23950               remove_addr_table_entry (a->dw_attr_val.val_entry);
23951             remove_AT (die, a->dw_attr);
23952             ix--;
23953           }
23954         if (die->die_tag == DW_TAG_GNU_call_site
23955             && a->dw_attr == DW_AT_abstract_origin)
23956           {
23957             tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
23958             dw_die_ref tdie = lookup_decl_die (tdecl);
23959             if (tdie == NULL
23960                 && DECL_EXTERNAL (tdecl)
23961                 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
23962               {
23963                 force_decl_die (tdecl);
23964                 tdie = lookup_decl_die (tdecl);
23965               }
23966             if (tdie)
23967               {
23968                 a->dw_attr_val.val_class = dw_val_class_die_ref;
23969                 a->dw_attr_val.v.val_die_ref.die = tdie;
23970                 a->dw_attr_val.v.val_die_ref.external = 0;
23971               }
23972             else
23973               {
23974                 if (AT_index (a) != NOT_INDEXED)
23975                   remove_addr_table_entry (a->dw_attr_val.val_entry);
23976                 remove_AT (die, a->dw_attr);
23977                 ix--;
23978               }
23979           }
23980         break;
23981       default:
23982         break;
23983       }
23984
23985   FOR_EACH_CHILD (die, c, resolve_addr (c));
23986 }
23987 \f
23988 /* Helper routines for optimize_location_lists.
23989    This pass tries to share identical local lists in .debug_loc
23990    section.  */
23991
23992 /* Iteratively hash operands of LOC opcode into HSTATE.  */
23993
23994 static void
23995 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
23996 {
23997   dw_val_ref val1 = &loc->dw_loc_oprnd1;
23998   dw_val_ref val2 = &loc->dw_loc_oprnd2;
23999
24000   switch (loc->dw_loc_opc)
24001     {
24002     case DW_OP_const4u:
24003     case DW_OP_const8u:
24004       if (loc->dtprel)
24005         goto hash_addr;
24006       /* FALLTHRU */
24007     case DW_OP_const1u:
24008     case DW_OP_const1s:
24009     case DW_OP_const2u:
24010     case DW_OP_const2s:
24011     case DW_OP_const4s:
24012     case DW_OP_const8s:
24013     case DW_OP_constu:
24014     case DW_OP_consts:
24015     case DW_OP_pick:
24016     case DW_OP_plus_uconst:
24017     case DW_OP_breg0:
24018     case DW_OP_breg1:
24019     case DW_OP_breg2:
24020     case DW_OP_breg3:
24021     case DW_OP_breg4:
24022     case DW_OP_breg5:
24023     case DW_OP_breg6:
24024     case DW_OP_breg7:
24025     case DW_OP_breg8:
24026     case DW_OP_breg9:
24027     case DW_OP_breg10:
24028     case DW_OP_breg11:
24029     case DW_OP_breg12:
24030     case DW_OP_breg13:
24031     case DW_OP_breg14:
24032     case DW_OP_breg15:
24033     case DW_OP_breg16:
24034     case DW_OP_breg17:
24035     case DW_OP_breg18:
24036     case DW_OP_breg19:
24037     case DW_OP_breg20:
24038     case DW_OP_breg21:
24039     case DW_OP_breg22:
24040     case DW_OP_breg23:
24041     case DW_OP_breg24:
24042     case DW_OP_breg25:
24043     case DW_OP_breg26:
24044     case DW_OP_breg27:
24045     case DW_OP_breg28:
24046     case DW_OP_breg29:
24047     case DW_OP_breg30:
24048     case DW_OP_breg31:
24049     case DW_OP_regx:
24050     case DW_OP_fbreg:
24051     case DW_OP_piece:
24052     case DW_OP_deref_size:
24053     case DW_OP_xderef_size:
24054       hstate.add_object (val1->v.val_int);
24055       break;
24056     case DW_OP_skip:
24057     case DW_OP_bra:
24058       {
24059         int offset;
24060
24061         gcc_assert (val1->val_class == dw_val_class_loc);
24062         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
24063         hstate.add_object (offset);
24064       }
24065       break;
24066     case DW_OP_implicit_value:
24067       hstate.add_object (val1->v.val_unsigned);
24068       switch (val2->val_class)
24069         {
24070         case dw_val_class_const:
24071           hstate.add_object (val2->v.val_int);
24072           break;
24073         case dw_val_class_vec:
24074           {
24075             unsigned int elt_size = val2->v.val_vec.elt_size;
24076             unsigned int len = val2->v.val_vec.length;
24077
24078             hstate.add_int (elt_size);
24079             hstate.add_int (len);
24080             hstate.add (val2->v.val_vec.array, len * elt_size);
24081           }
24082           break;
24083         case dw_val_class_const_double:
24084           hstate.add_object (val2->v.val_double.low);
24085           hstate.add_object (val2->v.val_double.high);
24086           break;
24087         case dw_val_class_wide_int:
24088           hstate.add (val2->v.val_wide->get_val (),
24089                       get_full_len (*val2->v.val_wide)
24090                       * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
24091           break;
24092         case dw_val_class_addr: 
24093           inchash::add_rtx (val2->v.val_addr, hstate);
24094           break;
24095         default:
24096           gcc_unreachable ();
24097         }
24098       break;
24099     case DW_OP_bregx:
24100     case DW_OP_bit_piece:
24101       hstate.add_object (val1->v.val_int);
24102       hstate.add_object (val2->v.val_int);
24103       break;
24104     case DW_OP_addr:
24105     hash_addr:
24106       if (loc->dtprel)
24107         {
24108           unsigned char dtprel = 0xd1;
24109           hstate.add_object (dtprel);
24110         }
24111       inchash::add_rtx (val1->v.val_addr, hstate);
24112       break;
24113     case DW_OP_GNU_addr_index:
24114     case DW_OP_GNU_const_index:
24115       {
24116         if (loc->dtprel)
24117           {
24118             unsigned char dtprel = 0xd1;
24119             hstate.add_object (dtprel);
24120           }
24121         inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
24122       }
24123       break;
24124     case DW_OP_GNU_implicit_pointer:
24125       hstate.add_int (val2->v.val_int);
24126       break;
24127     case DW_OP_GNU_entry_value:
24128       hstate.add_object (val1->v.val_loc);
24129       break;
24130     case DW_OP_GNU_regval_type:
24131     case DW_OP_GNU_deref_type:
24132       {
24133         unsigned int byte_size
24134           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
24135         unsigned int encoding
24136           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
24137         hstate.add_object (val1->v.val_int);
24138         hstate.add_object (byte_size);
24139         hstate.add_object (encoding);
24140       }
24141       break;
24142     case DW_OP_GNU_convert:
24143     case DW_OP_GNU_reinterpret:
24144       if (val1->val_class == dw_val_class_unsigned_const)
24145         {
24146           hstate.add_object (val1->v.val_unsigned);
24147           break;
24148         }
24149       /* FALLTHRU */
24150     case DW_OP_GNU_const_type:
24151       {
24152         unsigned int byte_size
24153           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
24154         unsigned int encoding
24155           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
24156         hstate.add_object (byte_size);
24157         hstate.add_object (encoding);
24158         if (loc->dw_loc_opc != DW_OP_GNU_const_type)
24159           break;
24160         hstate.add_object (val2->val_class);
24161         switch (val2->val_class)
24162           {
24163           case dw_val_class_const:
24164             hstate.add_object (val2->v.val_int);
24165             break;
24166           case dw_val_class_vec:
24167             {
24168               unsigned int elt_size = val2->v.val_vec.elt_size;
24169               unsigned int len = val2->v.val_vec.length;
24170
24171               hstate.add_object (elt_size);
24172               hstate.add_object (len);
24173               hstate.add (val2->v.val_vec.array, len * elt_size);
24174             }
24175             break;
24176           case dw_val_class_const_double:
24177             hstate.add_object (val2->v.val_double.low);
24178             hstate.add_object (val2->v.val_double.high);
24179             break;
24180           case dw_val_class_wide_int:
24181             hstate.add (val2->v.val_wide->get_val (),
24182                         get_full_len (*val2->v.val_wide)
24183                         * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
24184             break;
24185           default:
24186             gcc_unreachable ();
24187           }
24188       }
24189       break;
24190
24191     default:
24192       /* Other codes have no operands.  */
24193       break;
24194     }
24195 }
24196
24197 /* Iteratively hash the whole DWARF location expression LOC into HSTATE.  */
24198
24199 static inline void
24200 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
24201 {
24202   dw_loc_descr_ref l;
24203   bool sizes_computed = false;
24204   /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed.  */
24205   size_of_locs (loc);
24206
24207   for (l = loc; l != NULL; l = l->dw_loc_next)
24208     {
24209       enum dwarf_location_atom opc = l->dw_loc_opc;
24210       hstate.add_object (opc);
24211       if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
24212         {
24213           size_of_locs (loc);
24214           sizes_computed = true;
24215         }
24216       hash_loc_operands (l, hstate);
24217     }
24218 }
24219
24220 /* Compute hash of the whole location list LIST_HEAD.  */
24221
24222 static inline void
24223 hash_loc_list (dw_loc_list_ref list_head)
24224 {
24225   dw_loc_list_ref curr = list_head;
24226   inchash::hash hstate;
24227
24228   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
24229     {
24230       hstate.add (curr->begin, strlen (curr->begin) + 1);
24231       hstate.add (curr->end, strlen (curr->end) + 1);
24232       if (curr->section)
24233         hstate.add (curr->section, strlen (curr->section) + 1);
24234       hash_locs (curr->expr, hstate);
24235     }
24236   list_head->hash = hstate.end ();
24237 }
24238
24239 /* Return true if X and Y opcodes have the same operands.  */
24240
24241 static inline bool
24242 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
24243 {
24244   dw_val_ref valx1 = &x->dw_loc_oprnd1;
24245   dw_val_ref valx2 = &x->dw_loc_oprnd2;
24246   dw_val_ref valy1 = &y->dw_loc_oprnd1;
24247   dw_val_ref valy2 = &y->dw_loc_oprnd2;
24248
24249   switch (x->dw_loc_opc)
24250     {
24251     case DW_OP_const4u:
24252     case DW_OP_const8u:
24253       if (x->dtprel)
24254         goto hash_addr;
24255       /* FALLTHRU */
24256     case DW_OP_const1u:
24257     case DW_OP_const1s:
24258     case DW_OP_const2u:
24259     case DW_OP_const2s:
24260     case DW_OP_const4s:
24261     case DW_OP_const8s:
24262     case DW_OP_constu:
24263     case DW_OP_consts:
24264     case DW_OP_pick:
24265     case DW_OP_plus_uconst:
24266     case DW_OP_breg0:
24267     case DW_OP_breg1:
24268     case DW_OP_breg2:
24269     case DW_OP_breg3:
24270     case DW_OP_breg4:
24271     case DW_OP_breg5:
24272     case DW_OP_breg6:
24273     case DW_OP_breg7:
24274     case DW_OP_breg8:
24275     case DW_OP_breg9:
24276     case DW_OP_breg10:
24277     case DW_OP_breg11:
24278     case DW_OP_breg12:
24279     case DW_OP_breg13:
24280     case DW_OP_breg14:
24281     case DW_OP_breg15:
24282     case DW_OP_breg16:
24283     case DW_OP_breg17:
24284     case DW_OP_breg18:
24285     case DW_OP_breg19:
24286     case DW_OP_breg20:
24287     case DW_OP_breg21:
24288     case DW_OP_breg22:
24289     case DW_OP_breg23:
24290     case DW_OP_breg24:
24291     case DW_OP_breg25:
24292     case DW_OP_breg26:
24293     case DW_OP_breg27:
24294     case DW_OP_breg28:
24295     case DW_OP_breg29:
24296     case DW_OP_breg30:
24297     case DW_OP_breg31:
24298     case DW_OP_regx:
24299     case DW_OP_fbreg:
24300     case DW_OP_piece:
24301     case DW_OP_deref_size:
24302     case DW_OP_xderef_size:
24303       return valx1->v.val_int == valy1->v.val_int;
24304     case DW_OP_skip:
24305     case DW_OP_bra:
24306       /* If splitting debug info, the use of DW_OP_GNU_addr_index
24307         can cause irrelevant differences in dw_loc_addr.  */
24308       gcc_assert (valx1->val_class == dw_val_class_loc
24309                   && valy1->val_class == dw_val_class_loc
24310                   && (dwarf_split_debug_info
24311                       || x->dw_loc_addr == y->dw_loc_addr));
24312       return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
24313     case DW_OP_implicit_value:
24314       if (valx1->v.val_unsigned != valy1->v.val_unsigned
24315           || valx2->val_class != valy2->val_class)
24316         return false;
24317       switch (valx2->val_class)
24318         {
24319         case dw_val_class_const:
24320           return valx2->v.val_int == valy2->v.val_int;
24321         case dw_val_class_vec:
24322           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24323                  && valx2->v.val_vec.length == valy2->v.val_vec.length
24324                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24325                             valx2->v.val_vec.elt_size
24326                             * valx2->v.val_vec.length) == 0;
24327         case dw_val_class_const_double:
24328           return valx2->v.val_double.low == valy2->v.val_double.low
24329                  && valx2->v.val_double.high == valy2->v.val_double.high;
24330         case dw_val_class_wide_int:
24331           return *valx2->v.val_wide == *valy2->v.val_wide;
24332         case dw_val_class_addr:
24333           return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
24334         default:
24335           gcc_unreachable ();
24336         }
24337     case DW_OP_bregx:
24338     case DW_OP_bit_piece:
24339       return valx1->v.val_int == valy1->v.val_int
24340              && valx2->v.val_int == valy2->v.val_int;
24341     case DW_OP_addr:
24342     hash_addr:
24343       return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
24344     case DW_OP_GNU_addr_index:
24345     case DW_OP_GNU_const_index:
24346       {
24347         rtx ax1 = valx1->val_entry->addr.rtl;
24348         rtx ay1 = valy1->val_entry->addr.rtl;
24349         return rtx_equal_p (ax1, ay1);
24350       }
24351     case DW_OP_GNU_implicit_pointer:
24352       return valx1->val_class == dw_val_class_die_ref
24353              && valx1->val_class == valy1->val_class
24354              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
24355              && valx2->v.val_int == valy2->v.val_int;
24356     case DW_OP_GNU_entry_value:
24357       return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
24358     case DW_OP_GNU_const_type:
24359       if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
24360           || valx2->val_class != valy2->val_class)
24361         return false;
24362       switch (valx2->val_class)
24363         {
24364         case dw_val_class_const:
24365           return valx2->v.val_int == valy2->v.val_int;
24366         case dw_val_class_vec:
24367           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24368                  && valx2->v.val_vec.length == valy2->v.val_vec.length
24369                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24370                             valx2->v.val_vec.elt_size
24371                             * valx2->v.val_vec.length) == 0;
24372         case dw_val_class_const_double:
24373           return valx2->v.val_double.low == valy2->v.val_double.low
24374                  && valx2->v.val_double.high == valy2->v.val_double.high;
24375         case dw_val_class_wide_int:
24376           return *valx2->v.val_wide == *valy2->v.val_wide;
24377         default:
24378           gcc_unreachable ();
24379         }
24380     case DW_OP_GNU_regval_type:
24381     case DW_OP_GNU_deref_type:
24382       return valx1->v.val_int == valy1->v.val_int
24383              && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
24384     case DW_OP_GNU_convert:
24385     case DW_OP_GNU_reinterpret:
24386       if (valx1->val_class != valy1->val_class)
24387         return false;
24388       if (valx1->val_class == dw_val_class_unsigned_const)
24389         return valx1->v.val_unsigned == valy1->v.val_unsigned;
24390       return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24391     case DW_OP_GNU_parameter_ref:
24392       return valx1->val_class == dw_val_class_die_ref
24393              && valx1->val_class == valy1->val_class
24394              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24395     default:
24396       /* Other codes have no operands.  */
24397       return true;
24398     }
24399 }
24400
24401 /* Return true if DWARF location expressions X and Y are the same.  */
24402
24403 static inline bool
24404 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
24405 {
24406   for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
24407     if (x->dw_loc_opc != y->dw_loc_opc
24408         || x->dtprel != y->dtprel
24409         || !compare_loc_operands (x, y))
24410       break;
24411   return x == NULL && y == NULL;
24412 }
24413
24414 /* Hashtable helpers.  */
24415
24416 struct loc_list_hasher : typed_noop_remove <dw_loc_list_struct>
24417 {
24418   typedef dw_loc_list_struct *value_type;
24419   typedef dw_loc_list_struct *compare_type;
24420   static inline hashval_t hash (const dw_loc_list_struct *);
24421   static inline bool equal (const dw_loc_list_struct *,
24422                             const dw_loc_list_struct *);
24423 };
24424
24425 /* Return precomputed hash of location list X.  */
24426
24427 inline hashval_t
24428 loc_list_hasher::hash (const dw_loc_list_struct *x)
24429 {
24430   return x->hash;
24431 }
24432
24433 /* Return true if location lists A and B are the same.  */
24434
24435 inline bool
24436 loc_list_hasher::equal (const dw_loc_list_struct *a,
24437                         const dw_loc_list_struct *b)
24438 {
24439   if (a == b)
24440     return 1;
24441   if (a->hash != b->hash)
24442     return 0;
24443   for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
24444     if (strcmp (a->begin, b->begin) != 0
24445         || strcmp (a->end, b->end) != 0
24446         || (a->section == NULL) != (b->section == NULL)
24447         || (a->section && strcmp (a->section, b->section) != 0)
24448         || !compare_locs (a->expr, b->expr))
24449       break;
24450   return a == NULL && b == NULL;
24451 }
24452
24453 typedef hash_table<loc_list_hasher> loc_list_hash_type;
24454
24455
24456 /* Recursively optimize location lists referenced from DIE
24457    children and share them whenever possible.  */
24458
24459 static void
24460 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
24461 {
24462   dw_die_ref c;
24463   dw_attr_ref a;
24464   unsigned ix;
24465   dw_loc_list_struct **slot;
24466
24467   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24468     if (AT_class (a) == dw_val_class_loc_list)
24469       {
24470         dw_loc_list_ref list = AT_loc_list (a);
24471         /* TODO: perform some optimizations here, before hashing
24472            it and storing into the hash table.  */
24473         hash_loc_list (list);
24474         slot = htab->find_slot_with_hash (list, list->hash, INSERT);
24475         if (*slot == NULL)
24476           *slot = list;
24477         else
24478           a->dw_attr_val.v.val_loc_list = *slot;
24479       }
24480
24481   FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
24482 }
24483
24484
24485 /* Recursively assign each location list a unique index into the debug_addr
24486    section.  */
24487
24488 static void
24489 index_location_lists (dw_die_ref die)
24490 {
24491   dw_die_ref c;
24492   dw_attr_ref a;
24493   unsigned ix;
24494
24495   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24496     if (AT_class (a) == dw_val_class_loc_list)
24497       {
24498         dw_loc_list_ref list = AT_loc_list (a);
24499         dw_loc_list_ref curr;
24500         for (curr = list; curr != NULL; curr = curr->dw_loc_next)
24501           {
24502             /* Don't index an entry that has already been indexed
24503                or won't be output.  */
24504             if (curr->begin_entry != NULL
24505                 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
24506               continue;
24507
24508             curr->begin_entry
24509                 = add_addr_table_entry (xstrdup (curr->begin),
24510                                         ate_kind_label);
24511           }
24512       }
24513
24514   FOR_EACH_CHILD (die, c, index_location_lists (c));
24515 }
24516
24517 /* Optimize location lists referenced from DIE
24518    children and share them whenever possible.  */
24519
24520 static void
24521 optimize_location_lists (dw_die_ref die)
24522 {
24523   loc_list_hash_type htab (500);
24524   optimize_location_lists_1 (die, &htab);
24525 }
24526 \f
24527 /* Output stuff that dwarf requires at the end of every file,
24528    and generate the DWARF-2 debugging info.  */
24529
24530 static void
24531 dwarf2out_finish (const char *filename)
24532 {
24533   limbo_die_node *node, *next_node;
24534   comdat_type_node *ctnode;
24535   unsigned int i;
24536   dw_die_ref main_comp_unit_die;
24537
24538   /* PCH might result in DW_AT_producer string being restored from the
24539      header compilation, so always fill it with empty string initially
24540      and overwrite only here.  */
24541   dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
24542   producer_string = gen_producer_string ();
24543   producer->dw_attr_val.v.val_str->refcount--;
24544   producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
24545
24546   gen_scheduled_generic_parms_dies ();
24547   gen_remaining_tmpl_value_param_die_attribute ();
24548
24549   /* Add the name for the main input file now.  We delayed this from
24550      dwarf2out_init to avoid complications with PCH.
24551      For LTO produced units use a fixed artificial name to avoid
24552      leaking tempfile names into the dwarf.  */
24553   if (!in_lto_p)
24554     add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
24555   else
24556     add_name_attribute (comp_unit_die (), "<artificial>");
24557   if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
24558     add_comp_dir_attribute (comp_unit_die ());
24559   else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
24560     {
24561       bool p = false;
24562       file_table->traverse<bool *, file_table_relative_p> (&p);
24563       if (p)
24564         add_comp_dir_attribute (comp_unit_die ());
24565     }
24566
24567   if (deferred_locations_list)
24568     for (i = 0; i < deferred_locations_list->length (); i++)
24569       {
24570         add_location_or_const_value_attribute (
24571             (*deferred_locations_list)[i].die,
24572             (*deferred_locations_list)[i].variable,
24573             false,
24574             DW_AT_location);
24575       }
24576
24577   /* Traverse the limbo die list, and add parent/child links.  The only
24578      dies without parents that should be here are concrete instances of
24579      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
24580      For concrete instances, we can get the parent die from the abstract
24581      instance.  */
24582   for (node = limbo_die_list; node; node = next_node)
24583     {
24584       dw_die_ref die = node->die;
24585       next_node = node->next;
24586
24587       if (die->die_parent == NULL)
24588         {
24589           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
24590
24591           if (origin && origin->die_parent)
24592             add_child_die (origin->die_parent, die);
24593           else if (is_cu_die (die))
24594             ;
24595           else if (seen_error ())
24596             /* It's OK to be confused by errors in the input.  */
24597             add_child_die (comp_unit_die (), die);
24598           else
24599             {
24600               /* In certain situations, the lexical block containing a
24601                  nested function can be optimized away, which results
24602                  in the nested function die being orphaned.  Likewise
24603                  with the return type of that nested function.  Force
24604                  this to be a child of the containing function.
24605
24606                  It may happen that even the containing function got fully
24607                  inlined and optimized out.  In that case we are lost and
24608                  assign the empty child.  This should not be big issue as
24609                  the function is likely unreachable too.  */
24610               gcc_assert (node->created_for);
24611
24612               if (DECL_P (node->created_for))
24613                 origin = get_context_die (DECL_CONTEXT (node->created_for));
24614               else if (TYPE_P (node->created_for))
24615                 origin = scope_die_for (node->created_for, comp_unit_die ());
24616               else
24617                 origin = comp_unit_die ();
24618
24619               add_child_die (origin, die);
24620             }
24621         }
24622     }
24623
24624   limbo_die_list = NULL;
24625
24626 #if ENABLE_ASSERT_CHECKING
24627   {
24628     dw_die_ref die = comp_unit_die (), c;
24629     FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
24630   }
24631 #endif
24632   resolve_addr (comp_unit_die ());
24633   move_marked_base_types ();
24634
24635   for (node = deferred_asm_name; node; node = node->next)
24636     {
24637       tree decl = node->created_for;
24638       /* When generating LTO bytecode we can not generate new assembler
24639          names at this point and all important decls got theirs via
24640          free-lang-data.  */
24641       if (((!flag_generate_lto && !flag_generate_offload)
24642            || DECL_ASSEMBLER_NAME_SET_P (decl))
24643           && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
24644         {
24645           add_linkage_attr (node->die, decl);
24646           move_linkage_attr (node->die);
24647         }
24648     }
24649
24650   deferred_asm_name = NULL;
24651
24652   /* Walk through the list of incomplete types again, trying once more to
24653      emit full debugging info for them.  */
24654   retry_incomplete_types ();
24655
24656   if (flag_eliminate_unused_debug_types)
24657     prune_unused_types ();
24658
24659   /* Generate separate COMDAT sections for type DIEs. */
24660   if (use_debug_types)
24661     {
24662       break_out_comdat_types (comp_unit_die ());
24663
24664       /* Each new type_unit DIE was added to the limbo die list when created.
24665          Since these have all been added to comdat_type_list, clear the
24666          limbo die list.  */
24667       limbo_die_list = NULL;
24668
24669       /* For each new comdat type unit, copy declarations for incomplete
24670          types to make the new unit self-contained (i.e., no direct
24671          references to the main compile unit).  */
24672       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24673         copy_decls_for_unworthy_types (ctnode->root_die);
24674       copy_decls_for_unworthy_types (comp_unit_die ());
24675
24676       /* In the process of copying declarations from one unit to another,
24677          we may have left some declarations behind that are no longer
24678          referenced.  Prune them.  */
24679       prune_unused_types ();
24680     }
24681
24682   /* Generate separate CUs for each of the include files we've seen.
24683      They will go into limbo_die_list.  */
24684   if (flag_eliminate_dwarf2_dups)
24685     break_out_includes (comp_unit_die ());
24686
24687   /* Traverse the DIE's and add add sibling attributes to those DIE's
24688      that have children.  */
24689   add_sibling_attributes (comp_unit_die ());
24690   for (node = limbo_die_list; node; node = node->next)
24691     add_sibling_attributes (node->die);
24692   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24693     add_sibling_attributes (ctnode->root_die);
24694
24695   /* When splitting DWARF info, we put some attributes in the
24696      skeleton compile_unit DIE that remains in the .o, while
24697      most attributes go in the DWO compile_unit_die.  */
24698   if (dwarf_split_debug_info)
24699     main_comp_unit_die = gen_compile_unit_die (NULL);
24700   else
24701     main_comp_unit_die = comp_unit_die ();
24702
24703   /* Output a terminator label for the .text section.  */
24704   switch_to_section (text_section);
24705   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
24706   if (cold_text_section)
24707     {
24708       switch_to_section (cold_text_section);
24709       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
24710     }
24711
24712   /* We can only use the low/high_pc attributes if all of the code was
24713      in .text.  */
24714   if (!have_multiple_function_sections 
24715       || (dwarf_version < 3 && dwarf_strict))
24716     {
24717       /* Don't add if the CU has no associated code.  */
24718       if (text_section_used)
24719         add_AT_low_high_pc (main_comp_unit_die, text_section_label,
24720                             text_end_label, true);
24721     }
24722   else
24723     {
24724       unsigned fde_idx;
24725       dw_fde_ref fde;
24726       bool range_list_added = false;
24727
24728       if (text_section_used)
24729         add_ranges_by_labels (main_comp_unit_die, text_section_label,
24730                               text_end_label, &range_list_added, true);
24731       if (cold_text_section_used)
24732         add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
24733                               cold_end_label, &range_list_added, true);
24734
24735       FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
24736         {
24737           if (DECL_IGNORED_P (fde->decl))
24738             continue;
24739           if (!fde->in_std_section)
24740             add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
24741                                   fde->dw_fde_end, &range_list_added,
24742                                   true);
24743           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
24744             add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
24745                                   fde->dw_fde_second_end, &range_list_added,
24746                                   true);
24747         }
24748
24749       if (range_list_added)
24750         {
24751           /* We need to give .debug_loc and .debug_ranges an appropriate
24752              "base address".  Use zero so that these addresses become
24753              absolute.  Historically, we've emitted the unexpected
24754              DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
24755              Emit both to give time for other tools to adapt.  */
24756           add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
24757           if (! dwarf_strict && dwarf_version < 4)
24758             add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
24759
24760           add_ranges (NULL);
24761         }
24762     }
24763
24764   if (debug_info_level >= DINFO_LEVEL_TERSE)
24765     add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
24766                     debug_line_section_label);
24767
24768   if (have_macinfo)
24769     add_AT_macptr (comp_unit_die (),
24770                    dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
24771                    macinfo_section_label);
24772
24773   if (dwarf_split_debug_info)
24774     {
24775       /* optimize_location_lists calculates the size of the lists,
24776          so index them first, and assign indices to the entries.
24777          Although optimize_location_lists will remove entries from
24778          the table, it only does so for duplicates, and therefore
24779          only reduces ref_counts to 1.  */
24780       index_location_lists (comp_unit_die ());
24781
24782       if (addr_index_table != NULL)
24783         {
24784           unsigned int index = 0;
24785           addr_index_table
24786             ->traverse_noresize<unsigned int *, index_addr_table_entry>
24787             (&index);
24788         }
24789     }
24790
24791   if (have_location_lists)
24792     optimize_location_lists (comp_unit_die ());
24793
24794   save_macinfo_strings ();
24795
24796   if (dwarf_split_debug_info)
24797     {
24798       unsigned int index = 0;
24799
24800       /* Add attributes common to skeleton compile_units and
24801          type_units.  Because these attributes include strings, it
24802          must be done before freezing the string table.  Top-level
24803          skeleton die attrs are added when the skeleton type unit is
24804          created, so ensure it is created by this point.  */
24805       add_top_level_skeleton_die_attrs (main_comp_unit_die);
24806       debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
24807     }
24808
24809   /* Output all of the compilation units.  We put the main one last so that
24810      the offsets are available to output_pubnames.  */
24811   for (node = limbo_die_list; node; node = node->next)
24812     output_comp_unit (node->die, 0);
24813
24814   hash_table<comdat_type_hasher> comdat_type_table (100);
24815   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24816     {
24817       comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
24818
24819       /* Don't output duplicate types.  */
24820       if (*slot != HTAB_EMPTY_ENTRY)
24821         continue;
24822
24823       /* Add a pointer to the line table for the main compilation unit
24824          so that the debugger can make sense of DW_AT_decl_file
24825          attributes.  */
24826       if (debug_info_level >= DINFO_LEVEL_TERSE)
24827         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
24828                         (!dwarf_split_debug_info
24829                          ? debug_line_section_label
24830                          : debug_skeleton_line_section_label));
24831
24832       output_comdat_type_unit (ctnode);
24833       *slot = ctnode;
24834     }
24835
24836   /* The AT_pubnames attribute needs to go in all skeleton dies, including
24837      both the main_cu and all skeleton TUs.  Making this call unconditional
24838      would end up either adding a second copy of the AT_pubnames attribute, or
24839      requiring a special case in add_top_level_skeleton_die_attrs.  */
24840   if (!dwarf_split_debug_info)
24841     add_AT_pubnames (comp_unit_die ());
24842
24843   if (dwarf_split_debug_info)
24844     {
24845       int mark;
24846       unsigned char checksum[16];
24847       struct md5_ctx ctx;
24848
24849       /* Compute a checksum of the comp_unit to use as the dwo_id.  */
24850       md5_init_ctx (&ctx);
24851       mark = 0;
24852       die_checksum (comp_unit_die (), &ctx, &mark);
24853       unmark_all_dies (comp_unit_die ());
24854       md5_finish_ctx (&ctx, checksum);
24855
24856       /* Use the first 8 bytes of the checksum as the dwo_id,
24857         and add it to both comp-unit DIEs.  */
24858       add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
24859       add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
24860
24861       /* Add the base offset of the ranges table to the skeleton
24862         comp-unit DIE.  */
24863       if (ranges_table_in_use)
24864         add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
24865                         ranges_section_label);
24866
24867       switch_to_section (debug_addr_section);
24868       ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
24869       output_addr_table ();
24870     }
24871
24872   /* Output the main compilation unit if non-empty or if .debug_macinfo
24873      or .debug_macro will be emitted.  */
24874   output_comp_unit (comp_unit_die (), have_macinfo);
24875
24876   if (dwarf_split_debug_info && info_section_emitted)
24877     output_skeleton_debug_sections (main_comp_unit_die);
24878
24879   /* Output the abbreviation table.  */
24880   if (abbrev_die_table_in_use != 1)
24881     {
24882       switch_to_section (debug_abbrev_section);
24883       ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
24884       output_abbrev_section ();
24885     }
24886
24887   /* Output location list section if necessary.  */
24888   if (have_location_lists)
24889     {
24890       /* Output the location lists info.  */
24891       switch_to_section (debug_loc_section);
24892       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
24893       output_location_lists (comp_unit_die ());
24894     }
24895
24896   output_pubtables ();
24897
24898   /* Output the address range information if a CU (.debug_info section)
24899      was emitted.  We output an empty table even if we had no functions
24900      to put in it.  This because the consumer has no way to tell the
24901      difference between an empty table that we omitted and failure to
24902      generate a table that would have contained data.  */
24903   if (info_section_emitted)
24904     {
24905       unsigned long aranges_length = size_of_aranges ();
24906
24907       switch_to_section (debug_aranges_section);
24908       output_aranges (aranges_length);
24909     }
24910
24911   /* Output ranges section if necessary.  */
24912   if (ranges_table_in_use)
24913     {
24914       switch_to_section (debug_ranges_section);
24915       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
24916       output_ranges ();
24917     }
24918
24919   /* Have to end the macro section.  */
24920   if (have_macinfo)
24921     {
24922       switch_to_section (debug_macinfo_section);
24923       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
24924       output_macinfo ();
24925       dw2_asm_output_data (1, 0, "End compilation unit");
24926     }
24927
24928   /* Output the source line correspondence table.  We must do this
24929      even if there is no line information.  Otherwise, on an empty
24930      translation unit, we will generate a present, but empty,
24931      .debug_info section.  IRIX 6.5 `nm' will then complain when
24932      examining the file.  This is done late so that any filenames
24933      used by the debug_info section are marked as 'used'.  */
24934   switch_to_section (debug_line_section);
24935   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
24936   if (! DWARF2_ASM_LINE_DEBUG_INFO)
24937     output_line_info (false);
24938
24939   if (dwarf_split_debug_info && info_section_emitted)
24940     {
24941       switch_to_section (debug_skeleton_line_section);
24942       ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
24943       output_line_info (true);
24944     }
24945
24946   /* If we emitted any indirect strings, output the string table too.  */
24947   if (debug_str_hash || skeleton_debug_str_hash)
24948     output_indirect_strings ();
24949 }
24950
24951 /* Reset all state within dwarf2out.c so that we can rerun the compiler
24952    within the same process.  For use by toplev::finalize.  */
24953
24954 void
24955 dwarf2out_c_finalize (void)
24956 {
24957   last_var_location_insn = NULL;
24958   cached_next_real_insn = NULL;
24959   used_rtx_array = NULL;
24960   incomplete_types = NULL;
24961   decl_scope_table = NULL;
24962   debug_info_section = NULL;
24963   debug_skeleton_info_section = NULL;
24964   debug_abbrev_section = NULL;
24965   debug_skeleton_abbrev_section = NULL;
24966   debug_aranges_section = NULL;
24967   debug_addr_section = NULL;
24968   debug_macinfo_section = NULL;
24969   debug_line_section = NULL;
24970   debug_skeleton_line_section = NULL;
24971   debug_loc_section = NULL;
24972   debug_pubnames_section = NULL;
24973   debug_pubtypes_section = NULL;
24974   debug_str_section = NULL;
24975   debug_str_dwo_section = NULL;
24976   debug_str_offsets_section = NULL;
24977   debug_ranges_section = NULL;
24978   debug_frame_section = NULL;
24979   fde_vec = NULL;
24980   debug_str_hash = NULL;
24981   skeleton_debug_str_hash = NULL;
24982   dw2_string_counter = 0;
24983   have_multiple_function_sections = false;
24984   text_section_used = false;
24985   cold_text_section_used = false;
24986   cold_text_section = NULL;
24987   current_unit_personality = NULL;
24988
24989   deferred_locations_list = NULL;
24990
24991   next_die_offset = 0;
24992   single_comp_unit_die = NULL;
24993   comdat_type_list = NULL;
24994   limbo_die_list = NULL;
24995   deferred_asm_name = NULL;
24996   file_table = NULL;
24997   decl_die_table = NULL;
24998   common_block_die_table = NULL;
24999   decl_loc_table = NULL;
25000   call_arg_locations = NULL;
25001   call_arg_loc_last = NULL;
25002   call_site_count = -1;
25003   tail_call_site_count = -1;
25004   //block_map = NULL;
25005   cached_dw_loc_list_table = NULL;
25006   abbrev_die_table = NULL;
25007   abbrev_die_table_allocated = 0;
25008   abbrev_die_table_in_use = 0;
25009   line_info_label_num = 0;
25010   cur_line_info_table = NULL;
25011   text_section_line_info = NULL;
25012   cold_text_section_line_info = NULL;
25013   separate_line_info = NULL;
25014   info_section_emitted = false;
25015   pubname_table = NULL;
25016   pubtype_table = NULL;
25017   macinfo_table = NULL;
25018   ranges_table = NULL;
25019   ranges_table_allocated = 0;
25020   ranges_table_in_use = 0;
25021   ranges_by_label = 0;
25022   ranges_by_label_allocated = 0;
25023   ranges_by_label_in_use = 0;
25024   have_location_lists = false;
25025   loclabel_num = 0;
25026   poc_label_num = 0;
25027   last_emitted_file = NULL;
25028   label_num = 0;
25029   file_table_last_lookup = NULL;
25030   tmpl_value_parm_die_table = NULL;
25031   generic_type_instances = NULL;
25032   frame_pointer_fb_offset = 0;
25033   frame_pointer_fb_offset_valid = false;
25034   base_types.release ();
25035   XDELETEVEC (producer_string);
25036   producer_string = NULL;
25037 }
25038
25039 #include "gt-dwarf2out.h"