Import pre-release gcc-5.0 to new vendor branch
[dragonfly.git] / contrib / gcc-5.0 / gcc / dwarf2out.c
1 /* Output Dwarf2 format symbol table information from GCC.
2    Copyright (C) 1992-2015 Free Software Foundation, Inc.
3    Contributed by Gary Funck (gary@intrepid.com).
4    Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
5    Extensively modified by Jason Merrill (jason@cygnus.com).
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
13
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
17 for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3.  If not see
21 <http://www.gnu.org/licenses/>.  */
22
23 /* TODO: Emit .debug_line header even when there are no functions, since
24            the file numbers are used by .debug_info.  Alternately, leave
25            out locations for types and decls.
26          Avoid talking about ctors and op= for PODs.
27          Factor out common prologue sequences into multiple CIEs.  */
28
29 /* The first part of this file deals with the DWARF 2 frame unwind
30    information, which is also used by the GCC efficient exception handling
31    mechanism.  The second part, controlled only by an #ifdef
32    DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
33    information.  */
34
35 /* DWARF2 Abbreviation Glossary:
36
37    CFA = Canonical Frame Address
38            a fixed address on the stack which identifies a call frame.
39            We define it to be the value of SP just before the call insn.
40            The CFA register and offset, which may change during the course
41            of the function, are used to calculate its value at runtime.
42
43    CFI = Call Frame Instruction
44            an instruction for the DWARF2 abstract machine
45
46    CIE = Common Information Entry
47            information describing information common to one or more FDEs
48
49    DIE = Debugging Information Entry
50
51    FDE = Frame Description Entry
52            information describing the stack call frame, in particular,
53            how to restore registers
54
55    DW_CFA_... = DWARF2 CFA call frame instruction
56    DW_TAG_... = DWARF2 DIE tag */
57
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "tm.h"
62 #include "rtl.h"
63 #include "hash-set.h"
64 #include "machmode.h"
65 #include "vec.h"
66 #include "double-int.h"
67 #include "input.h"
68 #include "alias.h"
69 #include "symtab.h"
70 #include "wide-int.h"
71 #include "inchash.h"
72 #include "real.h"
73 #include "tree.h"
74 #include "fold-const.h"
75 #include "stringpool.h"
76 #include "stor-layout.h"
77 #include "varasm.h"
78 #include "hashtab.h"
79 #include "hard-reg-set.h"
80 #include "function.h"
81 #include "emit-rtl.h"
82 #include "hash-table.h"
83 #include "version.h"
84 #include "flags.h"
85 #include "regs.h"
86 #include "rtlhash.h"
87 #include "insn-config.h"
88 #include "reload.h"
89 #include "output.h"
90 #include "statistics.h"
91 #include "fixed-value.h"
92 #include "expmed.h"
93 #include "dojump.h"
94 #include "explow.h"
95 #include "calls.h"
96 #include "stmt.h"
97 #include "expr.h"
98 #include "except.h"
99 #include "dwarf2.h"
100 #include "dwarf2out.h"
101 #include "dwarf2asm.h"
102 #include "toplev.h"
103 #include "md5.h"
104 #include "tm_p.h"
105 #include "diagnostic.h"
106 #include "tree-pretty-print.h"
107 #include "debug.h"
108 #include "target.h"
109 #include "common/common-target.h"
110 #include "langhooks.h"
111 #include "hash-map.h"
112 #include "is-a.h"
113 #include "plugin-api.h"
114 #include "ipa-ref.h"
115 #include "cgraph.h"
116 #include "ira.h"
117 #include "lra.h"
118 #include "dumpfile.h"
119 #include "opts.h"
120 #include "tree-dfa.h"
121 #include "gdb/gdb-index.h"
122 #include "rtl-iter.h"
123
124 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
125 static rtx_insn *last_var_location_insn;
126 static rtx_insn *cached_next_real_insn;
127 static void dwarf2out_decl (tree);
128
129 #ifdef VMS_DEBUGGING_INFO
130 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
131
132 /* Define this macro to be a nonzero value if the directory specifications
133     which are output in the debug info should end with a separator.  */
134 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
135 /* Define this macro to evaluate to a nonzero value if GCC should refrain
136    from generating indirect strings in DWARF2 debug information, for instance
137    if your target is stuck with an old version of GDB that is unable to
138    process them properly or uses VMS Debug.  */
139 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
140 #else
141 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
142 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
143 #endif
144
145 /* ??? Poison these here until it can be done generically.  They've been
146    totally replaced in this file; make sure it stays that way.  */
147 #undef DWARF2_UNWIND_INFO
148 #undef DWARF2_FRAME_INFO
149 #if (GCC_VERSION >= 3000)
150  #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
151 #endif
152
153 /* The size of the target's pointer type.  */
154 #ifndef PTR_SIZE
155 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
156 #endif
157
158 /* Array of RTXes referenced by the debugging information, which therefore
159    must be kept around forever.  */
160 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
161
162 /* A pointer to the base of a list of incomplete types which might be
163    completed at some later time.  incomplete_types_list needs to be a
164    vec<tree, va_gc> *because we want to tell the garbage collector about
165    it.  */
166 static GTY(()) vec<tree, va_gc> *incomplete_types;
167
168 /* A pointer to the base of a table of references to declaration
169    scopes.  This table is a display which tracks the nesting
170    of declaration scopes at the current scope and containing
171    scopes.  This table is used to find the proper place to
172    define type declaration DIE's.  */
173 static GTY(()) vec<tree, va_gc> *decl_scope_table;
174
175 /* Pointers to various DWARF2 sections.  */
176 static GTY(()) section *debug_info_section;
177 static GTY(()) section *debug_skeleton_info_section;
178 static GTY(()) section *debug_abbrev_section;
179 static GTY(()) section *debug_skeleton_abbrev_section;
180 static GTY(()) section *debug_aranges_section;
181 static GTY(()) section *debug_addr_section;
182 static GTY(()) section *debug_macinfo_section;
183 static GTY(()) section *debug_line_section;
184 static GTY(()) section *debug_skeleton_line_section;
185 static GTY(()) section *debug_loc_section;
186 static GTY(()) section *debug_pubnames_section;
187 static GTY(()) section *debug_pubtypes_section;
188 static GTY(()) section *debug_str_section;
189 static GTY(()) section *debug_str_dwo_section;
190 static GTY(()) section *debug_str_offsets_section;
191 static GTY(()) section *debug_ranges_section;
192 static GTY(()) section *debug_frame_section;
193
194 /* Maximum size (in bytes) of an artificially generated label.  */
195 #define MAX_ARTIFICIAL_LABEL_BYTES      30
196
197 /* According to the (draft) DWARF 3 specification, the initial length
198    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
199    bytes are 0xffffffff, followed by the length stored in the next 8
200    bytes.
201
202    However, the SGI/MIPS ABI uses an initial length which is equal to
203    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
204
205 #ifndef DWARF_INITIAL_LENGTH_SIZE
206 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
207 #endif
208
209 /* Round SIZE up to the nearest BOUNDARY.  */
210 #define DWARF_ROUND(SIZE,BOUNDARY) \
211   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
212
213 /* CIE identifier.  */
214 #if HOST_BITS_PER_WIDE_INT >= 64
215 #define DWARF_CIE_ID \
216   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
217 #else
218 #define DWARF_CIE_ID DW_CIE_ID
219 #endif
220
221
222 /* A vector for a table that contains frame description
223    information for each routine.  */
224 #define NOT_INDEXED (-1U)
225 #define NO_INDEX_ASSIGNED (-2U)
226
227 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
228
229 struct GTY((for_user)) indirect_string_node {
230   const char *str;
231   unsigned int refcount;
232   enum dwarf_form form;
233   char *label;
234   unsigned int index;
235 };
236
237 struct indirect_string_hasher : ggc_hasher<indirect_string_node *>
238 {
239   typedef const char *compare_type;
240
241   static hashval_t hash (indirect_string_node *);
242   static bool equal (indirect_string_node *, const char *);
243 };
244
245 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
246
247 /* With split_debug_info, both the comp_dir and dwo_name go in the
248    main object file, rather than the dwo, similar to the force_direct
249    parameter elsewhere but with additional complications:
250
251    1) The string is needed in both the main object file and the dwo.
252    That is, the comp_dir and dwo_name will appear in both places.
253
254    2) Strings can use three forms: DW_FORM_string, DW_FORM_strp or
255    DW_FORM_GNU_str_index.
256
257    3) GCC chooses the form to use late, depending on the size and
258    reference count.
259
260    Rather than forcing the all debug string handling functions and
261    callers to deal with these complications, simply use a separate,
262    special-cased string table for any attribute that should go in the
263    main object file.  This limits the complexity to just the places
264    that need it.  */
265
266 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
267
268 static GTY(()) int dw2_string_counter;
269
270 /* True if the compilation unit places functions in more than one section.  */
271 static GTY(()) bool have_multiple_function_sections = false;
272
273 /* Whether the default text and cold text sections have been used at all.  */
274
275 static GTY(()) bool text_section_used = false;
276 static GTY(()) bool cold_text_section_used = false;
277
278 /* The default cold text section.  */
279 static GTY(()) section *cold_text_section;
280
281 /* The DIE for C++14 'auto' in a function return type.  */
282 static GTY(()) dw_die_ref auto_die;
283
284 /* The DIE for C++14 'decltype(auto)' in a function return type.  */
285 static GTY(()) dw_die_ref decltype_auto_die;
286
287 /* Forward declarations for functions defined in this file.  */
288
289 static char *stripattributes (const char *);
290 static void output_call_frame_info (int);
291 static void dwarf2out_note_section_used (void);
292
293 /* Personality decl of current unit.  Used only when assembler does not support
294    personality CFI.  */
295 static GTY(()) rtx current_unit_personality;
296
297 /* Data and reference forms for relocatable data.  */
298 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
299 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
300
301 #ifndef DEBUG_FRAME_SECTION
302 #define DEBUG_FRAME_SECTION     ".debug_frame"
303 #endif
304
305 #ifndef FUNC_BEGIN_LABEL
306 #define FUNC_BEGIN_LABEL        "LFB"
307 #endif
308
309 #ifndef FUNC_END_LABEL
310 #define FUNC_END_LABEL          "LFE"
311 #endif
312
313 #ifndef PROLOGUE_END_LABEL
314 #define PROLOGUE_END_LABEL      "LPE"
315 #endif
316
317 #ifndef EPILOGUE_BEGIN_LABEL
318 #define EPILOGUE_BEGIN_LABEL    "LEB"
319 #endif
320
321 #ifndef FRAME_BEGIN_LABEL
322 #define FRAME_BEGIN_LABEL       "Lframe"
323 #endif
324 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
325 #define CIE_END_LABEL           "LECIE"
326 #define FDE_LABEL               "LSFDE"
327 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
328 #define FDE_END_LABEL           "LEFDE"
329 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
330 #define LINE_NUMBER_END_LABEL   "LELT"
331 #define LN_PROLOG_AS_LABEL      "LASLTP"
332 #define LN_PROLOG_END_LABEL     "LELTP"
333 #define DIE_LABEL_PREFIX        "DW"
334 \f
335 /* Match the base name of a file to the base name of a compilation unit. */
336
337 static int
338 matches_main_base (const char *path)
339 {
340   /* Cache the last query. */
341   static const char *last_path = NULL;
342   static int last_match = 0;
343   if (path != last_path)
344     {
345       const char *base;
346       int length = base_of_path (path, &base);
347       last_path = path;
348       last_match = (length == main_input_baselength
349                     && memcmp (base, main_input_basename, length) == 0);
350     }
351   return last_match;
352 }
353
354 #ifdef DEBUG_DEBUG_STRUCT
355
356 static int
357 dump_struct_debug (tree type, enum debug_info_usage usage,
358                    enum debug_struct_file criterion, int generic,
359                    int matches, int result)
360 {
361   /* Find the type name. */
362   tree type_decl = TYPE_STUB_DECL (type);
363   tree t = type_decl;
364   const char *name = 0;
365   if (TREE_CODE (t) == TYPE_DECL)
366     t = DECL_NAME (t);
367   if (t)
368     name = IDENTIFIER_POINTER (t);
369
370   fprintf (stderr, "    struct %d %s %s %s %s %d %p %s\n",
371            criterion,
372            DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
373            matches ? "bas" : "hdr",
374            generic ? "gen" : "ord",
375            usage == DINFO_USAGE_DFN ? ";" :
376              usage == DINFO_USAGE_DIR_USE ? "." : "*",
377            result,
378            (void*) type_decl, name);
379   return result;
380 }
381 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
382   dump_struct_debug (type, usage, criterion, generic, matches, result)
383
384 #else
385
386 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
387   (result)
388
389 #endif
390
391 /* Get the number of HOST_WIDE_INTs needed to represent the precision
392    of the number.  */
393
394 static unsigned int
395 get_full_len (const wide_int &op)
396 {
397   return ((op.get_precision () + HOST_BITS_PER_WIDE_INT - 1)
398           / HOST_BITS_PER_WIDE_INT);
399 }
400
401 static bool
402 should_emit_struct_debug (tree type, enum debug_info_usage usage)
403 {
404   enum debug_struct_file criterion;
405   tree type_decl;
406   bool generic = lang_hooks.types.generic_p (type);
407
408   if (generic)
409     criterion = debug_struct_generic[usage];
410   else
411     criterion = debug_struct_ordinary[usage];
412
413   if (criterion == DINFO_STRUCT_FILE_NONE)
414     return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
415   if (criterion == DINFO_STRUCT_FILE_ANY)
416     return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
417
418   type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
419
420   if (type_decl != NULL)
421     {
422      if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
423         return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
424
425       if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
426         return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
427     }
428
429   return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
430 }
431 \f
432 /* Return a pointer to a copy of the section string name S with all
433    attributes stripped off, and an asterisk prepended (for assemble_name).  */
434
435 static inline char *
436 stripattributes (const char *s)
437 {
438   char *stripped = XNEWVEC (char, strlen (s) + 2);
439   char *p = stripped;
440
441   *p++ = '*';
442
443   while (*s && *s != ',')
444     *p++ = *s++;
445
446   *p = '\0';
447   return stripped;
448 }
449
450 /* Switch [BACK] to eh_frame_section.  If we don't have an eh_frame_section,
451    switch to the data section instead, and write out a synthetic start label
452    for collect2 the first time around.  */
453
454 static void
455 switch_to_eh_frame_section (bool back)
456 {
457   tree label;
458
459 #ifdef EH_FRAME_SECTION_NAME
460   if (eh_frame_section == 0)
461     {
462       int flags;
463
464       if (EH_TABLES_CAN_BE_READ_ONLY)
465         {
466           int fde_encoding;
467           int per_encoding;
468           int lsda_encoding;
469
470           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
471                                                        /*global=*/0);
472           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
473                                                        /*global=*/1);
474           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
475                                                         /*global=*/0);
476           flags = ((! flag_pic
477                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
478                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
479                         && (per_encoding & 0x70) != DW_EH_PE_absptr
480                         && (per_encoding & 0x70) != DW_EH_PE_aligned
481                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
482                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
483                    ? 0 : SECTION_WRITE);
484         }
485       else
486         flags = SECTION_WRITE;
487       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
488     }
489 #endif /* EH_FRAME_SECTION_NAME */
490
491   if (eh_frame_section)
492     switch_to_section (eh_frame_section);
493   else
494     {
495       /* We have no special eh_frame section.  Put the information in
496          the data section and emit special labels to guide collect2.  */
497       switch_to_section (data_section);
498
499       if (!back)
500         {
501           label = get_file_function_name ("F");
502           ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
503           targetm.asm_out.globalize_label (asm_out_file,
504                                            IDENTIFIER_POINTER (label));
505           ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
506         }
507     }
508 }
509
510 /* Switch [BACK] to the eh or debug frame table section, depending on
511    FOR_EH.  */
512
513 static void
514 switch_to_frame_table_section (int for_eh, bool back)
515 {
516   if (for_eh)
517     switch_to_eh_frame_section (back);
518   else
519     {
520       if (!debug_frame_section)
521         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
522                                            SECTION_DEBUG, NULL);
523       switch_to_section (debug_frame_section);
524     }
525 }
526
527 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
528
529 enum dw_cfi_oprnd_type
530 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
531 {
532   switch (cfi)
533     {
534     case DW_CFA_nop:
535     case DW_CFA_GNU_window_save:
536     case DW_CFA_remember_state:
537     case DW_CFA_restore_state:
538       return dw_cfi_oprnd_unused;
539
540     case DW_CFA_set_loc:
541     case DW_CFA_advance_loc1:
542     case DW_CFA_advance_loc2:
543     case DW_CFA_advance_loc4:
544     case DW_CFA_MIPS_advance_loc8:
545       return dw_cfi_oprnd_addr;
546
547     case DW_CFA_offset:
548     case DW_CFA_offset_extended:
549     case DW_CFA_def_cfa:
550     case DW_CFA_offset_extended_sf:
551     case DW_CFA_def_cfa_sf:
552     case DW_CFA_restore:
553     case DW_CFA_restore_extended:
554     case DW_CFA_undefined:
555     case DW_CFA_same_value:
556     case DW_CFA_def_cfa_register:
557     case DW_CFA_register:
558     case DW_CFA_expression:
559       return dw_cfi_oprnd_reg_num;
560
561     case DW_CFA_def_cfa_offset:
562     case DW_CFA_GNU_args_size:
563     case DW_CFA_def_cfa_offset_sf:
564       return dw_cfi_oprnd_offset;
565
566     case DW_CFA_def_cfa_expression:
567       return dw_cfi_oprnd_loc;
568
569     default:
570       gcc_unreachable ();
571     }
572 }
573
574 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
575
576 enum dw_cfi_oprnd_type
577 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
578 {
579   switch (cfi)
580     {
581     case DW_CFA_def_cfa:
582     case DW_CFA_def_cfa_sf:
583     case DW_CFA_offset:
584     case DW_CFA_offset_extended_sf:
585     case DW_CFA_offset_extended:
586       return dw_cfi_oprnd_offset;
587
588     case DW_CFA_register:
589       return dw_cfi_oprnd_reg_num;
590
591     case DW_CFA_expression:
592       return dw_cfi_oprnd_loc;
593
594     default:
595       return dw_cfi_oprnd_unused;
596     }
597 }
598
599 /* Output one FDE.  */
600
601 static void
602 output_fde (dw_fde_ref fde, bool for_eh, bool second,
603             char *section_start_label, int fde_encoding, char *augmentation,
604             bool any_lsda_needed, int lsda_encoding)
605 {
606   const char *begin, *end;
607   static unsigned int j;
608   char l1[20], l2[20];
609
610   targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
611                                      /* empty */ 0);
612   targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
613                                   for_eh + j);
614   ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
615   ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
616   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
617     dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
618                          " indicating 64-bit DWARF extension");
619   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
620                         "FDE Length");
621   ASM_OUTPUT_LABEL (asm_out_file, l1);
622
623   if (for_eh)
624     dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
625   else
626     dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
627                            debug_frame_section, "FDE CIE offset");
628
629   begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
630   end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
631
632   if (for_eh)
633     {
634       rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
635       SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
636       dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
637                                        "FDE initial location");
638       dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
639                             end, begin, "FDE address range");
640     }
641   else
642     {
643       dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
644       dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
645     }
646
647   if (augmentation[0])
648     {
649       if (any_lsda_needed)
650         {
651           int size = size_of_encoded_value (lsda_encoding);
652
653           if (lsda_encoding == DW_EH_PE_aligned)
654             {
655               int offset = (  4         /* Length */
656                             + 4         /* CIE offset */
657                             + 2 * size_of_encoded_value (fde_encoding)
658                             + 1         /* Augmentation size */ );
659               int pad = -offset & (PTR_SIZE - 1);
660
661               size += pad;
662               gcc_assert (size_of_uleb128 (size) == 1);
663             }
664
665           dw2_asm_output_data_uleb128 (size, "Augmentation size");
666
667           if (fde->uses_eh_lsda)
668             {
669               ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
670                                            fde->funcdef_number);
671               dw2_asm_output_encoded_addr_rtx (lsda_encoding,
672                                                gen_rtx_SYMBOL_REF (Pmode, l1),
673                                                false,
674                                                "Language Specific Data Area");
675             }
676           else
677             {
678               if (lsda_encoding == DW_EH_PE_aligned)
679                 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
680               dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
681                                    "Language Specific Data Area (none)");
682             }
683         }
684       else
685         dw2_asm_output_data_uleb128 (0, "Augmentation size");
686     }
687
688   /* Loop through the Call Frame Instructions associated with this FDE.  */
689   fde->dw_fde_current_label = begin;
690   {
691     size_t from, until, i;
692
693     from = 0;
694     until = vec_safe_length (fde->dw_fde_cfi);
695
696     if (fde->dw_fde_second_begin == NULL)
697       ;
698     else if (!second)
699       until = fde->dw_fde_switch_cfi_index;
700     else
701       from = fde->dw_fde_switch_cfi_index;
702
703     for (i = from; i < until; i++)
704       output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
705   }
706
707   /* If we are to emit a ref/link from function bodies to their frame tables,
708      do it now.  This is typically performed to make sure that tables
709      associated with functions are dragged with them and not discarded in
710      garbage collecting links. We need to do this on a per function basis to
711      cope with -ffunction-sections.  */
712
713 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
714   /* Switch to the function section, emit the ref to the tables, and
715      switch *back* into the table section.  */
716   switch_to_section (function_section (fde->decl));
717   ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
718   switch_to_frame_table_section (for_eh, true);
719 #endif
720
721   /* Pad the FDE out to an address sized boundary.  */
722   ASM_OUTPUT_ALIGN (asm_out_file,
723                     floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
724   ASM_OUTPUT_LABEL (asm_out_file, l2);
725
726   j += 2;
727 }
728
729 /* Return true if frame description entry FDE is needed for EH.  */
730
731 static bool
732 fde_needed_for_eh_p (dw_fde_ref fde)
733 {
734   if (flag_asynchronous_unwind_tables)
735     return true;
736
737   if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
738     return true;
739
740   if (fde->uses_eh_lsda)
741     return true;
742
743   /* If exceptions are enabled, we have collected nothrow info.  */
744   if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
745     return false;
746
747   return true;
748 }
749
750 /* Output the call frame information used to record information
751    that relates to calculating the frame pointer, and records the
752    location of saved registers.  */
753
754 static void
755 output_call_frame_info (int for_eh)
756 {
757   unsigned int i;
758   dw_fde_ref fde;
759   dw_cfi_ref cfi;
760   char l1[20], l2[20], section_start_label[20];
761   bool any_lsda_needed = false;
762   char augmentation[6];
763   int augmentation_size;
764   int fde_encoding = DW_EH_PE_absptr;
765   int per_encoding = DW_EH_PE_absptr;
766   int lsda_encoding = DW_EH_PE_absptr;
767   int return_reg;
768   rtx personality = NULL;
769   int dw_cie_version;
770
771   /* Don't emit a CIE if there won't be any FDEs.  */
772   if (!fde_vec)
773     return;
774
775   /* Nothing to do if the assembler's doing it all.  */
776   if (dwarf2out_do_cfi_asm ())
777     return;
778
779   /* If we don't have any functions we'll want to unwind out of, don't emit
780      any EH unwind information.  If we make FDEs linkonce, we may have to
781      emit an empty label for an FDE that wouldn't otherwise be emitted.  We
782      want to avoid having an FDE kept around when the function it refers to
783      is discarded.  Example where this matters: a primary function template
784      in C++ requires EH information, an explicit specialization doesn't.  */
785   if (for_eh)
786     {
787       bool any_eh_needed = false;
788
789       FOR_EACH_VEC_ELT (*fde_vec, i, fde)
790         {
791           if (fde->uses_eh_lsda)
792             any_eh_needed = any_lsda_needed = true;
793           else if (fde_needed_for_eh_p (fde))
794             any_eh_needed = true;
795           else if (TARGET_USES_WEAK_UNWIND_INFO)
796             targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
797         }
798
799       if (!any_eh_needed)
800         return;
801     }
802
803   /* We're going to be generating comments, so turn on app.  */
804   if (flag_debug_asm)
805     app_enable ();
806
807   /* Switch to the proper frame section, first time.  */
808   switch_to_frame_table_section (for_eh, false);
809
810   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
811   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
812
813   /* Output the CIE.  */
814   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
815   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
816   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
817     dw2_asm_output_data (4, 0xffffffff,
818       "Initial length escape value indicating 64-bit DWARF extension");
819   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
820                         "Length of Common Information Entry");
821   ASM_OUTPUT_LABEL (asm_out_file, l1);
822
823   /* Now that the CIE pointer is PC-relative for EH,
824      use 0 to identify the CIE.  */
825   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
826                        (for_eh ? 0 : DWARF_CIE_ID),
827                        "CIE Identifier Tag");
828
829   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
830      use CIE version 1, unless that would produce incorrect results
831      due to overflowing the return register column.  */
832   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
833   dw_cie_version = 1;
834   if (return_reg >= 256 || dwarf_version > 2)
835     dw_cie_version = 3;
836   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
837
838   augmentation[0] = 0;
839   augmentation_size = 0;
840
841   personality = current_unit_personality;
842   if (for_eh)
843     {
844       char *p;
845
846       /* Augmentation:
847          z      Indicates that a uleb128 is present to size the
848                 augmentation section.
849          L      Indicates the encoding (and thus presence) of
850                 an LSDA pointer in the FDE augmentation.
851          R      Indicates a non-default pointer encoding for
852                 FDE code pointers.
853          P      Indicates the presence of an encoding + language
854                 personality routine in the CIE augmentation.  */
855
856       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
857       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
858       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
859
860       p = augmentation + 1;
861       if (personality)
862         {
863           *p++ = 'P';
864           augmentation_size += 1 + size_of_encoded_value (per_encoding);
865           assemble_external_libcall (personality);
866         }
867       if (any_lsda_needed)
868         {
869           *p++ = 'L';
870           augmentation_size += 1;
871         }
872       if (fde_encoding != DW_EH_PE_absptr)
873         {
874           *p++ = 'R';
875           augmentation_size += 1;
876         }
877       if (p > augmentation + 1)
878         {
879           augmentation[0] = 'z';
880           *p = '\0';
881         }
882
883       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
884       if (personality && per_encoding == DW_EH_PE_aligned)
885         {
886           int offset = (  4             /* Length */
887                         + 4             /* CIE Id */
888                         + 1             /* CIE version */
889                         + strlen (augmentation) + 1     /* Augmentation */
890                         + size_of_uleb128 (1)           /* Code alignment */
891                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
892                         + 1             /* RA column */
893                         + 1             /* Augmentation size */
894                         + 1             /* Personality encoding */ );
895           int pad = -offset & (PTR_SIZE - 1);
896
897           augmentation_size += pad;
898
899           /* Augmentations should be small, so there's scarce need to
900              iterate for a solution.  Die if we exceed one uleb128 byte.  */
901           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
902         }
903     }
904
905   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
906   if (dw_cie_version >= 4)
907     {
908       dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
909       dw2_asm_output_data (1, 0, "CIE Segment Size");
910     }
911   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
912   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
913                                "CIE Data Alignment Factor");
914
915   if (dw_cie_version == 1)
916     dw2_asm_output_data (1, return_reg, "CIE RA Column");
917   else
918     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
919
920   if (augmentation[0])
921     {
922       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
923       if (personality)
924         {
925           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
926                                eh_data_format_name (per_encoding));
927           dw2_asm_output_encoded_addr_rtx (per_encoding,
928                                            personality,
929                                            true, NULL);
930         }
931
932       if (any_lsda_needed)
933         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
934                              eh_data_format_name (lsda_encoding));
935
936       if (fde_encoding != DW_EH_PE_absptr)
937         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
938                              eh_data_format_name (fde_encoding));
939     }
940
941   FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
942     output_cfi (cfi, NULL, for_eh);
943
944   /* Pad the CIE out to an address sized boundary.  */
945   ASM_OUTPUT_ALIGN (asm_out_file,
946                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
947   ASM_OUTPUT_LABEL (asm_out_file, l2);
948
949   /* Loop through all of the FDE's.  */
950   FOR_EACH_VEC_ELT (*fde_vec, i, fde)
951     {
952       unsigned int k;
953
954       /* Don't emit EH unwind info for leaf functions that don't need it.  */
955       if (for_eh && !fde_needed_for_eh_p (fde))
956         continue;
957
958       for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
959         output_fde (fde, for_eh, k, section_start_label, fde_encoding,
960                     augmentation, any_lsda_needed, lsda_encoding);
961     }
962
963   if (for_eh && targetm.terminate_dw2_eh_frame_info)
964     dw2_asm_output_data (4, 0, "End of Table");
965
966   /* Turn off app to make assembly quicker.  */
967   if (flag_debug_asm)
968     app_disable ();
969 }
970
971 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed.  */
972
973 static void
974 dwarf2out_do_cfi_startproc (bool second)
975 {
976   int enc;
977   rtx ref;
978   rtx personality = get_personality_function (current_function_decl);
979
980   fprintf (asm_out_file, "\t.cfi_startproc\n");
981
982   if (personality)
983     {
984       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
985       ref = personality;
986
987       /* ??? The GAS support isn't entirely consistent.  We have to
988          handle indirect support ourselves, but PC-relative is done
989          in the assembler.  Further, the assembler can't handle any
990          of the weirder relocation types.  */
991       if (enc & DW_EH_PE_indirect)
992         ref = dw2_force_const_mem (ref, true);
993
994       fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
995       output_addr_const (asm_out_file, ref);
996       fputc ('\n', asm_out_file);
997     }
998
999   if (crtl->uses_eh_lsda)
1000     {
1001       char lab[20];
1002
1003       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
1004       ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
1005                                    current_function_funcdef_no);
1006       ref = gen_rtx_SYMBOL_REF (Pmode, lab);
1007       SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
1008
1009       if (enc & DW_EH_PE_indirect)
1010         ref = dw2_force_const_mem (ref, true);
1011
1012       fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
1013       output_addr_const (asm_out_file, ref);
1014       fputc ('\n', asm_out_file);
1015     }
1016 }
1017
1018 /* Allocate CURRENT_FDE.  Immediately initialize all we can, noting that
1019    this allocation may be done before pass_final.  */
1020
1021 dw_fde_ref
1022 dwarf2out_alloc_current_fde (void)
1023 {
1024   dw_fde_ref fde;
1025
1026   fde = ggc_cleared_alloc<dw_fde_node> ();
1027   fde->decl = current_function_decl;
1028   fde->funcdef_number = current_function_funcdef_no;
1029   fde->fde_index = vec_safe_length (fde_vec);
1030   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1031   fde->uses_eh_lsda = crtl->uses_eh_lsda;
1032   fde->nothrow = crtl->nothrow;
1033   fde->drap_reg = INVALID_REGNUM;
1034   fde->vdrap_reg = INVALID_REGNUM;
1035
1036   /* Record the FDE associated with this function.  */
1037   cfun->fde = fde;
1038   vec_safe_push (fde_vec, fde);
1039
1040   return fde;
1041 }
1042
1043 /* Output a marker (i.e. a label) for the beginning of a function, before
1044    the prologue.  */
1045
1046 void
1047 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1048                           const char *file ATTRIBUTE_UNUSED)
1049 {
1050   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1051   char * dup_label;
1052   dw_fde_ref fde;
1053   section *fnsec;
1054   bool do_frame;
1055
1056   current_function_func_begin_label = NULL;
1057
1058   do_frame = dwarf2out_do_frame ();
1059
1060   /* ??? current_function_func_begin_label is also used by except.c for
1061      call-site information.  We must emit this label if it might be used.  */
1062   if (!do_frame
1063       && (!flag_exceptions
1064           || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1065     return;
1066
1067   fnsec = function_section (current_function_decl);
1068   switch_to_section (fnsec);
1069   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1070                                current_function_funcdef_no);
1071   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1072                           current_function_funcdef_no);
1073   dup_label = xstrdup (label);
1074   current_function_func_begin_label = dup_label;
1075
1076   /* We can elide the fde allocation if we're not emitting debug info.  */
1077   if (!do_frame)
1078     return;
1079
1080   /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1081      emit insns as rtx but bypass the bulk of rest_of_compilation, which
1082      would include pass_dwarf2_frame.  If we've not created the FDE yet,
1083      do so now.  */
1084   fde = cfun->fde;
1085   if (fde == NULL)
1086     fde = dwarf2out_alloc_current_fde ();
1087
1088   /* Initialize the bits of CURRENT_FDE that were not available earlier.  */
1089   fde->dw_fde_begin = dup_label;
1090   fde->dw_fde_current_label = dup_label;
1091   fde->in_std_section = (fnsec == text_section
1092                          || (cold_text_section && fnsec == cold_text_section));
1093
1094   /* We only want to output line number information for the genuine dwarf2
1095      prologue case, not the eh frame case.  */
1096 #ifdef DWARF2_DEBUGGING_INFO
1097   if (file)
1098     dwarf2out_source_line (line, file, 0, true);
1099 #endif
1100
1101   if (dwarf2out_do_cfi_asm ())
1102     dwarf2out_do_cfi_startproc (false);
1103   else
1104     {
1105       rtx personality = get_personality_function (current_function_decl);
1106       if (!current_unit_personality)
1107         current_unit_personality = personality;
1108
1109       /* We cannot keep a current personality per function as without CFI
1110          asm, at the point where we emit the CFI data, there is no current
1111          function anymore.  */
1112       if (personality && current_unit_personality != personality)
1113         sorry ("multiple EH personalities are supported only with assemblers "
1114                "supporting .cfi_personality directive");
1115     }
1116 }
1117
1118 /* Output a marker (i.e. a label) for the end of the generated code
1119    for a function prologue.  This gets called *after* the prologue code has
1120    been generated.  */
1121
1122 void
1123 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1124                         const char *file ATTRIBUTE_UNUSED)
1125 {
1126   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1127
1128   /* Output a label to mark the endpoint of the code generated for this
1129      function.  */
1130   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1131                                current_function_funcdef_no);
1132   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1133                           current_function_funcdef_no);
1134   cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1135 }
1136
1137 /* Output a marker (i.e. a label) for the beginning of the generated code
1138    for a function epilogue.  This gets called *before* the prologue code has
1139    been generated.  */
1140
1141 void
1142 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1143                           const char *file ATTRIBUTE_UNUSED)
1144 {
1145   dw_fde_ref fde = cfun->fde;
1146   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1147
1148   if (fde->dw_fde_vms_begin_epilogue)
1149     return;
1150
1151   /* Output a label to mark the endpoint of the code generated for this
1152      function.  */
1153   ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1154                                current_function_funcdef_no);
1155   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1156                           current_function_funcdef_no);
1157   fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1158 }
1159
1160 /* Output a marker (i.e. a label) for the absolute end of the generated code
1161    for a function definition.  This gets called *after* the epilogue code has
1162    been generated.  */
1163
1164 void
1165 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1166                         const char *file ATTRIBUTE_UNUSED)
1167 {
1168   dw_fde_ref fde;
1169   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1170
1171   last_var_location_insn = NULL;
1172   cached_next_real_insn = NULL;
1173
1174   if (dwarf2out_do_cfi_asm ())
1175     fprintf (asm_out_file, "\t.cfi_endproc\n");
1176
1177   /* Output a label to mark the endpoint of the code generated for this
1178      function.  */
1179   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1180                                current_function_funcdef_no);
1181   ASM_OUTPUT_LABEL (asm_out_file, label);
1182   fde = cfun->fde;
1183   gcc_assert (fde != NULL);
1184   if (fde->dw_fde_second_begin == NULL)
1185     fde->dw_fde_end = xstrdup (label);
1186 }
1187
1188 void
1189 dwarf2out_frame_finish (void)
1190 {
1191   /* Output call frame information.  */
1192   if (targetm.debug_unwind_info () == UI_DWARF2)
1193     output_call_frame_info (0);
1194
1195   /* Output another copy for the unwinder.  */
1196   if ((flag_unwind_tables || flag_exceptions)
1197       && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1198     output_call_frame_info (1);
1199 }
1200
1201 /* Note that the current function section is being used for code.  */
1202
1203 static void
1204 dwarf2out_note_section_used (void)
1205 {
1206   section *sec = current_function_section ();
1207   if (sec == text_section)
1208     text_section_used = true;
1209   else if (sec == cold_text_section)
1210     cold_text_section_used = true;
1211 }
1212
1213 static void var_location_switch_text_section (void);
1214 static void set_cur_line_info_table (section *);
1215
1216 void
1217 dwarf2out_switch_text_section (void)
1218 {
1219   section *sect;
1220   dw_fde_ref fde = cfun->fde;
1221
1222   gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1223
1224   if (!in_cold_section_p)
1225     {
1226       fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1227       fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1228       fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1229     }
1230   else
1231     {
1232       fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1233       fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1234       fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1235     }
1236   have_multiple_function_sections = true;
1237
1238   /* There is no need to mark used sections when not debugging.  */
1239   if (cold_text_section != NULL)
1240     dwarf2out_note_section_used ();
1241
1242   if (dwarf2out_do_cfi_asm ())
1243     fprintf (asm_out_file, "\t.cfi_endproc\n");
1244
1245   /* Now do the real section switch.  */
1246   sect = current_function_section ();
1247   switch_to_section (sect);
1248
1249   fde->second_in_std_section
1250     = (sect == text_section
1251        || (cold_text_section && sect == cold_text_section));
1252
1253   if (dwarf2out_do_cfi_asm ())
1254     dwarf2out_do_cfi_startproc (true);
1255
1256   var_location_switch_text_section ();
1257
1258   if (cold_text_section != NULL)
1259     set_cur_line_info_table (sect);
1260 }
1261 \f
1262 /* And now, the subset of the debugging information support code necessary
1263    for emitting location expressions.  */
1264
1265 /* Data about a single source file.  */
1266 struct GTY((for_user)) dwarf_file_data {
1267   const char * filename;
1268   int emitted_number;
1269 };
1270
1271 typedef struct GTY(()) deferred_locations_struct
1272 {
1273   tree variable;
1274   dw_die_ref die;
1275 } deferred_locations;
1276
1277
1278 static GTY(()) vec<deferred_locations, va_gc> *deferred_locations_list;
1279
1280
1281 /* Describe an entry into the .debug_addr section.  */
1282
1283 enum ate_kind {
1284   ate_kind_rtx,
1285   ate_kind_rtx_dtprel,
1286   ate_kind_label
1287 };
1288
1289 typedef struct GTY((for_user)) addr_table_entry_struct {
1290   enum ate_kind kind;
1291   unsigned int refcount;
1292   unsigned int index;
1293   union addr_table_entry_struct_union
1294     {
1295       rtx GTY ((tag ("0"))) rtl;
1296       char * GTY ((tag ("1"))) label;
1297     }
1298   GTY ((desc ("%1.kind"))) addr;
1299 }
1300 addr_table_entry;
1301
1302 /* Location lists are ranges + location descriptions for that range,
1303    so you can track variables that are in different places over
1304    their entire life.  */
1305 typedef struct GTY(()) dw_loc_list_struct {
1306   dw_loc_list_ref dw_loc_next;
1307   const char *begin; /* Label and addr_entry for start of range */
1308   addr_table_entry *begin_entry;
1309   const char *end;  /* Label for end of range */
1310   char *ll_symbol; /* Label for beginning of location list.
1311                       Only on head of list */
1312   const char *section; /* Section this loclist is relative to */
1313   dw_loc_descr_ref expr;
1314   hashval_t hash;
1315   /* True if all addresses in this and subsequent lists are known to be
1316      resolved.  */
1317   bool resolved_addr;
1318   /* True if this list has been replaced by dw_loc_next.  */
1319   bool replaced;
1320   bool emitted;
1321   /* True if the range should be emitted even if begin and end
1322      are the same.  */
1323   bool force;
1324 } dw_loc_list_node;
1325
1326 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1327
1328 /* Convert a DWARF stack opcode into its string name.  */
1329
1330 static const char *
1331 dwarf_stack_op_name (unsigned int op)
1332 {
1333   const char *name = get_DW_OP_name (op);
1334
1335   if (name != NULL)
1336     return name;
1337
1338   return "OP_<unknown>";
1339 }
1340
1341 /* Return a pointer to a newly allocated location description.  Location
1342    descriptions are simple expression terms that can be strung
1343    together to form more complicated location (address) descriptions.  */
1344
1345 static inline dw_loc_descr_ref
1346 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1347                unsigned HOST_WIDE_INT oprnd2)
1348 {
1349   dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1350
1351   descr->dw_loc_opc = op;
1352   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1353   descr->dw_loc_oprnd1.val_entry = NULL;
1354   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1355   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1356   descr->dw_loc_oprnd2.val_entry = NULL;
1357   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1358
1359   return descr;
1360 }
1361
1362 /* Return a pointer to a newly allocated location description for
1363    REG and OFFSET.  */
1364
1365 static inline dw_loc_descr_ref
1366 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
1367 {
1368   if (reg <= 31)
1369     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1370                           offset, 0);
1371   else
1372     return new_loc_descr (DW_OP_bregx, reg, offset);
1373 }
1374
1375 /* Add a location description term to a location description expression.  */
1376
1377 static inline void
1378 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1379 {
1380   dw_loc_descr_ref *d;
1381
1382   /* Find the end of the chain.  */
1383   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1384     ;
1385
1386   *d = descr;
1387 }
1388
1389 /* Compare two location operands for exact equality.  */
1390
1391 static bool
1392 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1393 {
1394   if (a->val_class != b->val_class)
1395     return false;
1396   switch (a->val_class)
1397     {
1398     case dw_val_class_none:
1399       return true;
1400     case dw_val_class_addr:
1401       return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1402
1403     case dw_val_class_offset:
1404     case dw_val_class_unsigned_const:
1405     case dw_val_class_const:
1406     case dw_val_class_range_list:
1407     case dw_val_class_lineptr:
1408     case dw_val_class_macptr:
1409       /* These are all HOST_WIDE_INT, signed or unsigned.  */
1410       return a->v.val_unsigned == b->v.val_unsigned;
1411
1412     case dw_val_class_loc:
1413       return a->v.val_loc == b->v.val_loc;
1414     case dw_val_class_loc_list:
1415       return a->v.val_loc_list == b->v.val_loc_list;
1416     case dw_val_class_die_ref:
1417       return a->v.val_die_ref.die == b->v.val_die_ref.die;
1418     case dw_val_class_fde_ref:
1419       return a->v.val_fde_index == b->v.val_fde_index;
1420     case dw_val_class_lbl_id:
1421     case dw_val_class_high_pc:
1422       return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1423     case dw_val_class_str:
1424       return a->v.val_str == b->v.val_str;
1425     case dw_val_class_flag:
1426       return a->v.val_flag == b->v.val_flag;
1427     case dw_val_class_file:
1428       return a->v.val_file == b->v.val_file;
1429     case dw_val_class_decl_ref:
1430       return a->v.val_decl_ref == b->v.val_decl_ref;
1431     
1432     case dw_val_class_const_double:
1433       return (a->v.val_double.high == b->v.val_double.high
1434               && a->v.val_double.low == b->v.val_double.low);
1435
1436     case dw_val_class_wide_int:
1437       return *a->v.val_wide == *b->v.val_wide;
1438
1439     case dw_val_class_vec:
1440       {
1441         size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1442         size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1443
1444         return (a_len == b_len
1445                 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1446       }
1447
1448     case dw_val_class_data8:
1449       return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1450
1451     case dw_val_class_vms_delta:
1452       return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1453               && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1454     }
1455   gcc_unreachable ();
1456 }
1457
1458 /* Compare two location atoms for exact equality.  */
1459
1460 static bool
1461 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1462 {
1463   if (a->dw_loc_opc != b->dw_loc_opc)
1464     return false;
1465
1466   /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1467      address size, but since we always allocate cleared storage it
1468      should be zero for other types of locations.  */
1469   if (a->dtprel != b->dtprel)
1470     return false;
1471
1472   return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1473           && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1474 }
1475
1476 /* Compare two complete location expressions for exact equality.  */
1477
1478 bool
1479 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1480 {
1481   while (1)
1482     {
1483       if (a == b)
1484         return true;
1485       if (a == NULL || b == NULL)
1486         return false;
1487       if (!loc_descr_equal_p_1 (a, b))
1488         return false;
1489
1490       a = a->dw_loc_next;
1491       b = b->dw_loc_next;
1492     }
1493 }
1494
1495
1496 /* Add a constant OFFSET to a location expression.  */
1497
1498 static void
1499 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1500 {
1501   dw_loc_descr_ref loc;
1502   HOST_WIDE_INT *p;
1503
1504   gcc_assert (*list_head != NULL);
1505
1506   if (!offset)
1507     return;
1508
1509   /* Find the end of the chain.  */
1510   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1511     ;
1512
1513   p = NULL;
1514   if (loc->dw_loc_opc == DW_OP_fbreg
1515       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1516     p = &loc->dw_loc_oprnd1.v.val_int;
1517   else if (loc->dw_loc_opc == DW_OP_bregx)
1518     p = &loc->dw_loc_oprnd2.v.val_int;
1519
1520   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1521      offset.  Don't optimize if an signed integer overflow would happen.  */
1522   if (p != NULL
1523       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1524           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1525     *p += offset;
1526
1527   else if (offset > 0)
1528     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1529
1530   else
1531     {
1532       loc->dw_loc_next = int_loc_descriptor (-offset);
1533       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1534     }
1535 }
1536
1537 /* Add a constant OFFSET to a location list.  */
1538
1539 static void
1540 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1541 {
1542   dw_loc_list_ref d;
1543   for (d = list_head; d != NULL; d = d->dw_loc_next)
1544     loc_descr_plus_const (&d->expr, offset);
1545 }
1546
1547 #define DWARF_REF_SIZE  \
1548   (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1549
1550 static unsigned long int get_base_type_offset (dw_die_ref);
1551
1552 /* Return the size of a location descriptor.  */
1553
1554 static unsigned long
1555 size_of_loc_descr (dw_loc_descr_ref loc)
1556 {
1557   unsigned long size = 1;
1558
1559   switch (loc->dw_loc_opc)
1560     {
1561     case DW_OP_addr:
1562       size += DWARF2_ADDR_SIZE;
1563       break;
1564     case DW_OP_GNU_addr_index:
1565     case DW_OP_GNU_const_index:
1566       gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1567       size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1568       break;
1569     case DW_OP_const1u:
1570     case DW_OP_const1s:
1571       size += 1;
1572       break;
1573     case DW_OP_const2u:
1574     case DW_OP_const2s:
1575       size += 2;
1576       break;
1577     case DW_OP_const4u:
1578     case DW_OP_const4s:
1579       size += 4;
1580       break;
1581     case DW_OP_const8u:
1582     case DW_OP_const8s:
1583       size += 8;
1584       break;
1585     case DW_OP_constu:
1586       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1587       break;
1588     case DW_OP_consts:
1589       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1590       break;
1591     case DW_OP_pick:
1592       size += 1;
1593       break;
1594     case DW_OP_plus_uconst:
1595       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1596       break;
1597     case DW_OP_skip:
1598     case DW_OP_bra:
1599       size += 2;
1600       break;
1601     case DW_OP_breg0:
1602     case DW_OP_breg1:
1603     case DW_OP_breg2:
1604     case DW_OP_breg3:
1605     case DW_OP_breg4:
1606     case DW_OP_breg5:
1607     case DW_OP_breg6:
1608     case DW_OP_breg7:
1609     case DW_OP_breg8:
1610     case DW_OP_breg9:
1611     case DW_OP_breg10:
1612     case DW_OP_breg11:
1613     case DW_OP_breg12:
1614     case DW_OP_breg13:
1615     case DW_OP_breg14:
1616     case DW_OP_breg15:
1617     case DW_OP_breg16:
1618     case DW_OP_breg17:
1619     case DW_OP_breg18:
1620     case DW_OP_breg19:
1621     case DW_OP_breg20:
1622     case DW_OP_breg21:
1623     case DW_OP_breg22:
1624     case DW_OP_breg23:
1625     case DW_OP_breg24:
1626     case DW_OP_breg25:
1627     case DW_OP_breg26:
1628     case DW_OP_breg27:
1629     case DW_OP_breg28:
1630     case DW_OP_breg29:
1631     case DW_OP_breg30:
1632     case DW_OP_breg31:
1633       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1634       break;
1635     case DW_OP_regx:
1636       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1637       break;
1638     case DW_OP_fbreg:
1639       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1640       break;
1641     case DW_OP_bregx:
1642       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1643       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1644       break;
1645     case DW_OP_piece:
1646       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1647       break;
1648     case DW_OP_bit_piece:
1649       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1650       size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1651       break;
1652     case DW_OP_deref_size:
1653     case DW_OP_xderef_size:
1654       size += 1;
1655       break;
1656     case DW_OP_call2:
1657       size += 2;
1658       break;
1659     case DW_OP_call4:
1660       size += 4;
1661       break;
1662     case DW_OP_call_ref:
1663       size += DWARF_REF_SIZE;
1664       break;
1665     case DW_OP_implicit_value:
1666       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1667               + loc->dw_loc_oprnd1.v.val_unsigned;
1668       break;
1669     case DW_OP_GNU_implicit_pointer:
1670       size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1671       break;
1672     case DW_OP_GNU_entry_value:
1673       {
1674         unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1675         size += size_of_uleb128 (op_size) + op_size;
1676         break;
1677       }
1678     case DW_OP_GNU_const_type:
1679       {
1680         unsigned long o
1681           = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1682         size += size_of_uleb128 (o) + 1;
1683         switch (loc->dw_loc_oprnd2.val_class)
1684           {
1685           case dw_val_class_vec:
1686             size += loc->dw_loc_oprnd2.v.val_vec.length
1687                     * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1688             break;
1689           case dw_val_class_const:
1690             size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1691             break;
1692           case dw_val_class_const_double:
1693             size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1694             break;
1695           case dw_val_class_wide_int:
1696             size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1697                      * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1698             break;
1699           default:
1700             gcc_unreachable ();
1701           }
1702         break;
1703       }
1704     case DW_OP_GNU_regval_type:
1705       {
1706         unsigned long o
1707           = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1708         size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1709                 + size_of_uleb128 (o);
1710       }
1711       break;
1712     case DW_OP_GNU_deref_type:
1713       {
1714         unsigned long o
1715           = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1716         size += 1 + size_of_uleb128 (o);
1717       }
1718       break;
1719     case DW_OP_GNU_convert:
1720     case DW_OP_GNU_reinterpret:
1721       if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1722         size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1723       else
1724         {
1725           unsigned long o
1726             = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1727           size += size_of_uleb128 (o);
1728         }
1729       break;
1730     case DW_OP_GNU_parameter_ref:
1731       size += 4;
1732       break;
1733     default:
1734       break;
1735     }
1736
1737   return size;
1738 }
1739
1740 /* Return the size of a series of location descriptors.  */
1741
1742 unsigned long
1743 size_of_locs (dw_loc_descr_ref loc)
1744 {
1745   dw_loc_descr_ref l;
1746   unsigned long size;
1747
1748   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1749      field, to avoid writing to a PCH file.  */
1750   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1751     {
1752       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1753         break;
1754       size += size_of_loc_descr (l);
1755     }
1756   if (! l)
1757     return size;
1758
1759   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1760     {
1761       l->dw_loc_addr = size;
1762       size += size_of_loc_descr (l);
1763     }
1764
1765   return size;
1766 }
1767
1768 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1769 static void get_ref_die_offset_label (char *, dw_die_ref);
1770 static unsigned long int get_ref_die_offset (dw_die_ref);
1771
1772 /* Output location description stack opcode's operands (if any).
1773    The for_eh_or_skip parameter controls whether register numbers are
1774    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1775    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1776    info).  This should be suppressed for the cases that have not been converted
1777    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
1778
1779 static void
1780 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1781 {
1782   dw_val_ref val1 = &loc->dw_loc_oprnd1;
1783   dw_val_ref val2 = &loc->dw_loc_oprnd2;
1784
1785   switch (loc->dw_loc_opc)
1786     {
1787 #ifdef DWARF2_DEBUGGING_INFO
1788     case DW_OP_const2u:
1789     case DW_OP_const2s:
1790       dw2_asm_output_data (2, val1->v.val_int, NULL);
1791       break;
1792     case DW_OP_const4u:
1793       if (loc->dtprel)
1794         {
1795           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1796           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1797                                                val1->v.val_addr);
1798           fputc ('\n', asm_out_file);
1799           break;
1800         }
1801       /* FALLTHRU */
1802     case DW_OP_const4s:
1803       dw2_asm_output_data (4, val1->v.val_int, NULL);
1804       break;
1805     case DW_OP_const8u:
1806       if (loc->dtprel)
1807         {
1808           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1809           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1810                                                val1->v.val_addr);
1811           fputc ('\n', asm_out_file);
1812           break;
1813         }
1814       /* FALLTHRU */
1815     case DW_OP_const8s:
1816       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1817       dw2_asm_output_data (8, val1->v.val_int, NULL);
1818       break;
1819     case DW_OP_skip:
1820     case DW_OP_bra:
1821       {
1822         int offset;
1823
1824         gcc_assert (val1->val_class == dw_val_class_loc);
1825         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1826
1827         dw2_asm_output_data (2, offset, NULL);
1828       }
1829       break;
1830     case DW_OP_implicit_value:
1831       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1832       switch (val2->val_class)
1833         {
1834         case dw_val_class_const:
1835           dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1836           break;
1837         case dw_val_class_vec:
1838           {
1839             unsigned int elt_size = val2->v.val_vec.elt_size;
1840             unsigned int len = val2->v.val_vec.length;
1841             unsigned int i;
1842             unsigned char *p;
1843
1844             if (elt_size > sizeof (HOST_WIDE_INT))
1845               {
1846                 elt_size /= 2;
1847                 len *= 2;
1848               }
1849             for (i = 0, p = val2->v.val_vec.array;
1850                  i < len;
1851                  i++, p += elt_size)
1852               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1853                                    "fp or vector constant word %u", i);
1854           }
1855           break;
1856         case dw_val_class_const_double:
1857           {
1858             unsigned HOST_WIDE_INT first, second;
1859
1860             if (WORDS_BIG_ENDIAN)
1861               {
1862                 first = val2->v.val_double.high;
1863                 second = val2->v.val_double.low;
1864               }
1865             else
1866               {
1867                 first = val2->v.val_double.low;
1868                 second = val2->v.val_double.high;
1869               }
1870             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1871                                  first, NULL);
1872             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1873                                  second, NULL);
1874           }
1875           break;
1876         case dw_val_class_wide_int:
1877           {
1878             int i;
1879             int len = get_full_len (*val2->v.val_wide);
1880             if (WORDS_BIG_ENDIAN)
1881               for (i = len - 1; i >= 0; --i)
1882                 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1883                                      val2->v.val_wide->elt (i), NULL);
1884             else
1885               for (i = 0; i < len; ++i)
1886                 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1887                                      val2->v.val_wide->elt (i), NULL);
1888           }
1889           break;
1890         case dw_val_class_addr:
1891           gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1892           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1893           break;
1894         default:
1895           gcc_unreachable ();
1896         }
1897       break;
1898 #else
1899     case DW_OP_const2u:
1900     case DW_OP_const2s:
1901     case DW_OP_const4u:
1902     case DW_OP_const4s:
1903     case DW_OP_const8u:
1904     case DW_OP_const8s:
1905     case DW_OP_skip:
1906     case DW_OP_bra:
1907     case DW_OP_implicit_value:
1908       /* We currently don't make any attempt to make sure these are
1909          aligned properly like we do for the main unwind info, so
1910          don't support emitting things larger than a byte if we're
1911          only doing unwinding.  */
1912       gcc_unreachable ();
1913 #endif
1914     case DW_OP_const1u:
1915     case DW_OP_const1s:
1916       dw2_asm_output_data (1, val1->v.val_int, NULL);
1917       break;
1918     case DW_OP_constu:
1919       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1920       break;
1921     case DW_OP_consts:
1922       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1923       break;
1924     case DW_OP_pick:
1925       dw2_asm_output_data (1, val1->v.val_int, NULL);
1926       break;
1927     case DW_OP_plus_uconst:
1928       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1929       break;
1930     case DW_OP_breg0:
1931     case DW_OP_breg1:
1932     case DW_OP_breg2:
1933     case DW_OP_breg3:
1934     case DW_OP_breg4:
1935     case DW_OP_breg5:
1936     case DW_OP_breg6:
1937     case DW_OP_breg7:
1938     case DW_OP_breg8:
1939     case DW_OP_breg9:
1940     case DW_OP_breg10:
1941     case DW_OP_breg11:
1942     case DW_OP_breg12:
1943     case DW_OP_breg13:
1944     case DW_OP_breg14:
1945     case DW_OP_breg15:
1946     case DW_OP_breg16:
1947     case DW_OP_breg17:
1948     case DW_OP_breg18:
1949     case DW_OP_breg19:
1950     case DW_OP_breg20:
1951     case DW_OP_breg21:
1952     case DW_OP_breg22:
1953     case DW_OP_breg23:
1954     case DW_OP_breg24:
1955     case DW_OP_breg25:
1956     case DW_OP_breg26:
1957     case DW_OP_breg27:
1958     case DW_OP_breg28:
1959     case DW_OP_breg29:
1960     case DW_OP_breg30:
1961     case DW_OP_breg31:
1962       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1963       break;
1964     case DW_OP_regx:
1965       {
1966         unsigned r = val1->v.val_unsigned;
1967         if (for_eh_or_skip >= 0)
1968           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1969         gcc_assert (size_of_uleb128 (r) 
1970                     == size_of_uleb128 (val1->v.val_unsigned));
1971         dw2_asm_output_data_uleb128 (r, NULL);  
1972       }
1973       break;
1974     case DW_OP_fbreg:
1975       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1976       break;
1977     case DW_OP_bregx:
1978       {
1979         unsigned r = val1->v.val_unsigned;
1980         if (for_eh_or_skip >= 0)
1981           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1982         gcc_assert (size_of_uleb128 (r) 
1983                     == size_of_uleb128 (val1->v.val_unsigned));
1984         dw2_asm_output_data_uleb128 (r, NULL);  
1985         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1986       }
1987       break;
1988     case DW_OP_piece:
1989       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1990       break;
1991     case DW_OP_bit_piece:
1992       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1993       dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1994       break;
1995     case DW_OP_deref_size:
1996     case DW_OP_xderef_size:
1997       dw2_asm_output_data (1, val1->v.val_int, NULL);
1998       break;
1999
2000     case DW_OP_addr:
2001       if (loc->dtprel)
2002         {
2003           if (targetm.asm_out.output_dwarf_dtprel)
2004             {
2005               targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2006                                                    DWARF2_ADDR_SIZE,
2007                                                    val1->v.val_addr);
2008               fputc ('\n', asm_out_file);
2009             }
2010           else
2011             gcc_unreachable ();
2012         }
2013       else
2014         {
2015 #ifdef DWARF2_DEBUGGING_INFO
2016           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2017 #else
2018           gcc_unreachable ();
2019 #endif
2020         }
2021       break;
2022
2023     case DW_OP_GNU_addr_index:
2024     case DW_OP_GNU_const_index:
2025       gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2026       dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2027                                    "(index into .debug_addr)");
2028       break;
2029
2030     case DW_OP_GNU_implicit_pointer:
2031       {
2032         char label[MAX_ARTIFICIAL_LABEL_BYTES
2033                    + HOST_BITS_PER_WIDE_INT / 2 + 2];
2034         gcc_assert (val1->val_class == dw_val_class_die_ref);
2035         get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2036         dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2037         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2038       }
2039       break;
2040
2041     case DW_OP_GNU_entry_value:
2042       dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2043       output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2044       break;
2045
2046     case DW_OP_GNU_const_type:
2047       {
2048         unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2049         gcc_assert (o);
2050         dw2_asm_output_data_uleb128 (o, NULL);
2051         switch (val2->val_class)
2052           {
2053           case dw_val_class_const:
2054             l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2055             dw2_asm_output_data (1, l, NULL);
2056             dw2_asm_output_data (l, val2->v.val_int, NULL);
2057             break;
2058           case dw_val_class_vec:
2059             {
2060               unsigned int elt_size = val2->v.val_vec.elt_size;
2061               unsigned int len = val2->v.val_vec.length;
2062               unsigned int i;
2063               unsigned char *p;
2064
2065               l = len * elt_size;
2066               dw2_asm_output_data (1, l, NULL);
2067               if (elt_size > sizeof (HOST_WIDE_INT))
2068                 {
2069                   elt_size /= 2;
2070                   len *= 2;
2071                 }
2072               for (i = 0, p = val2->v.val_vec.array;
2073                    i < len;
2074                    i++, p += elt_size)
2075                 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2076                                      "fp or vector constant word %u", i);
2077             }
2078             break;
2079           case dw_val_class_const_double:
2080             {
2081               unsigned HOST_WIDE_INT first, second;
2082               l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2083
2084               dw2_asm_output_data (1, 2 * l, NULL);
2085               if (WORDS_BIG_ENDIAN)
2086                 {
2087                   first = val2->v.val_double.high;
2088                   second = val2->v.val_double.low;
2089                 }
2090               else
2091                 {
2092                   first = val2->v.val_double.low;
2093                   second = val2->v.val_double.high;
2094                 }
2095               dw2_asm_output_data (l, first, NULL);
2096               dw2_asm_output_data (l, second, NULL);
2097             }
2098             break;
2099           case dw_val_class_wide_int:
2100             {
2101               int i;
2102               int len = get_full_len (*val2->v.val_wide);
2103               l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2104
2105               dw2_asm_output_data (1, len * l, NULL);
2106               if (WORDS_BIG_ENDIAN)
2107                 for (i = len - 1; i >= 0; --i)
2108                   dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2109               else
2110                 for (i = 0; i < len; ++i)
2111                   dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2112             }
2113             break;
2114           default:
2115             gcc_unreachable ();
2116           }
2117       }
2118       break;
2119     case DW_OP_GNU_regval_type:
2120       {
2121         unsigned r = val1->v.val_unsigned;
2122         unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2123         gcc_assert (o);
2124         if (for_eh_or_skip >= 0)
2125           {
2126             r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2127             gcc_assert (size_of_uleb128 (r)
2128                         == size_of_uleb128 (val1->v.val_unsigned));
2129           }
2130         dw2_asm_output_data_uleb128 (r, NULL);
2131         dw2_asm_output_data_uleb128 (o, NULL);
2132       }
2133       break;
2134     case DW_OP_GNU_deref_type:
2135       {
2136         unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2137         gcc_assert (o);
2138         dw2_asm_output_data (1, val1->v.val_int, NULL);
2139         dw2_asm_output_data_uleb128 (o, NULL);
2140       }
2141       break;
2142     case DW_OP_GNU_convert:
2143     case DW_OP_GNU_reinterpret:
2144       if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2145         dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2146       else
2147         {
2148           unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2149           gcc_assert (o);
2150           dw2_asm_output_data_uleb128 (o, NULL);
2151         }
2152       break;
2153
2154     case DW_OP_GNU_parameter_ref:
2155       {
2156         unsigned long o;
2157         gcc_assert (val1->val_class == dw_val_class_die_ref);
2158         o = get_ref_die_offset (val1->v.val_die_ref.die);
2159         dw2_asm_output_data (4, o, NULL);
2160       }
2161       break;
2162
2163     default:
2164       /* Other codes have no operands.  */
2165       break;
2166     }
2167 }
2168
2169 /* Output a sequence of location operations.  
2170    The for_eh_or_skip parameter controls whether register numbers are
2171    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2172    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2173    info).  This should be suppressed for the cases that have not been converted
2174    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
2175
2176 void
2177 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2178 {
2179   for (; loc != NULL; loc = loc->dw_loc_next)
2180     {
2181       enum dwarf_location_atom opc = loc->dw_loc_opc;
2182       /* Output the opcode.  */
2183       if (for_eh_or_skip >= 0 
2184           && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2185         {
2186           unsigned r = (opc - DW_OP_breg0);
2187           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2188           gcc_assert (r <= 31);
2189           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2190         }
2191       else if (for_eh_or_skip >= 0 
2192                && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2193         {
2194           unsigned r = (opc - DW_OP_reg0);
2195           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2196           gcc_assert (r <= 31);
2197           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2198         }
2199
2200       dw2_asm_output_data (1, opc,
2201                              "%s", dwarf_stack_op_name (opc));
2202
2203       /* Output the operand(s) (if any).  */
2204       output_loc_operands (loc, for_eh_or_skip);
2205     }
2206 }
2207
2208 /* Output location description stack opcode's operands (if any).
2209    The output is single bytes on a line, suitable for .cfi_escape.  */
2210
2211 static void
2212 output_loc_operands_raw (dw_loc_descr_ref loc)
2213 {
2214   dw_val_ref val1 = &loc->dw_loc_oprnd1;
2215   dw_val_ref val2 = &loc->dw_loc_oprnd2;
2216
2217   switch (loc->dw_loc_opc)
2218     {
2219     case DW_OP_addr:
2220     case DW_OP_GNU_addr_index:
2221     case DW_OP_GNU_const_index:
2222     case DW_OP_implicit_value:
2223       /* We cannot output addresses in .cfi_escape, only bytes.  */
2224       gcc_unreachable ();
2225
2226     case DW_OP_const1u:
2227     case DW_OP_const1s:
2228     case DW_OP_pick:
2229     case DW_OP_deref_size:
2230     case DW_OP_xderef_size:
2231       fputc (',', asm_out_file);
2232       dw2_asm_output_data_raw (1, val1->v.val_int);
2233       break;
2234
2235     case DW_OP_const2u:
2236     case DW_OP_const2s:
2237       fputc (',', asm_out_file);
2238       dw2_asm_output_data_raw (2, val1->v.val_int);
2239       break;
2240
2241     case DW_OP_const4u:
2242     case DW_OP_const4s:
2243       fputc (',', asm_out_file);
2244       dw2_asm_output_data_raw (4, val1->v.val_int);
2245       break;
2246
2247     case DW_OP_const8u:
2248     case DW_OP_const8s:
2249       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2250       fputc (',', asm_out_file);
2251       dw2_asm_output_data_raw (8, val1->v.val_int);
2252       break;
2253
2254     case DW_OP_skip:
2255     case DW_OP_bra:
2256       {
2257         int offset;
2258
2259         gcc_assert (val1->val_class == dw_val_class_loc);
2260         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2261
2262         fputc (',', asm_out_file);
2263         dw2_asm_output_data_raw (2, offset);
2264       }
2265       break;
2266
2267     case DW_OP_regx:
2268       {
2269         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2270         gcc_assert (size_of_uleb128 (r) 
2271                     == size_of_uleb128 (val1->v.val_unsigned));
2272         fputc (',', asm_out_file);
2273         dw2_asm_output_data_uleb128_raw (r);
2274       }
2275       break;
2276       
2277     case DW_OP_constu:
2278     case DW_OP_plus_uconst:
2279     case DW_OP_piece:
2280       fputc (',', asm_out_file);
2281       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2282       break;
2283
2284     case DW_OP_bit_piece:
2285       fputc (',', asm_out_file);
2286       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2287       dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2288       break;
2289
2290     case DW_OP_consts:
2291     case DW_OP_breg0:
2292     case DW_OP_breg1:
2293     case DW_OP_breg2:
2294     case DW_OP_breg3:
2295     case DW_OP_breg4:
2296     case DW_OP_breg5:
2297     case DW_OP_breg6:
2298     case DW_OP_breg7:
2299     case DW_OP_breg8:
2300     case DW_OP_breg9:
2301     case DW_OP_breg10:
2302     case DW_OP_breg11:
2303     case DW_OP_breg12:
2304     case DW_OP_breg13:
2305     case DW_OP_breg14:
2306     case DW_OP_breg15:
2307     case DW_OP_breg16:
2308     case DW_OP_breg17:
2309     case DW_OP_breg18:
2310     case DW_OP_breg19:
2311     case DW_OP_breg20:
2312     case DW_OP_breg21:
2313     case DW_OP_breg22:
2314     case DW_OP_breg23:
2315     case DW_OP_breg24:
2316     case DW_OP_breg25:
2317     case DW_OP_breg26:
2318     case DW_OP_breg27:
2319     case DW_OP_breg28:
2320     case DW_OP_breg29:
2321     case DW_OP_breg30:
2322     case DW_OP_breg31:
2323     case DW_OP_fbreg:
2324       fputc (',', asm_out_file);
2325       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2326       break;
2327
2328     case DW_OP_bregx:
2329       {
2330         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2331         gcc_assert (size_of_uleb128 (r) 
2332                     == size_of_uleb128 (val1->v.val_unsigned));
2333         fputc (',', asm_out_file);
2334         dw2_asm_output_data_uleb128_raw (r);
2335         fputc (',', asm_out_file);
2336         dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2337       }
2338       break;
2339
2340     case DW_OP_GNU_implicit_pointer:
2341     case DW_OP_GNU_entry_value:
2342     case DW_OP_GNU_const_type:
2343     case DW_OP_GNU_regval_type:
2344     case DW_OP_GNU_deref_type:
2345     case DW_OP_GNU_convert:
2346     case DW_OP_GNU_reinterpret:
2347     case DW_OP_GNU_parameter_ref:
2348       gcc_unreachable ();
2349       break;
2350
2351     default:
2352       /* Other codes have no operands.  */
2353       break;
2354     }
2355 }
2356
2357 void
2358 output_loc_sequence_raw (dw_loc_descr_ref loc)
2359 {
2360   while (1)
2361     {
2362       enum dwarf_location_atom opc = loc->dw_loc_opc;
2363       /* Output the opcode.  */
2364       if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2365         {
2366           unsigned r = (opc - DW_OP_breg0);
2367           r = DWARF2_FRAME_REG_OUT (r, 1);
2368           gcc_assert (r <= 31);
2369           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2370         }
2371       else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2372         {
2373           unsigned r = (opc - DW_OP_reg0);
2374           r = DWARF2_FRAME_REG_OUT (r, 1);
2375           gcc_assert (r <= 31);
2376           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2377         }
2378       /* Output the opcode.  */
2379       fprintf (asm_out_file, "%#x", opc);
2380       output_loc_operands_raw (loc);
2381
2382       if (!loc->dw_loc_next)
2383         break;
2384       loc = loc->dw_loc_next;
2385
2386       fputc (',', asm_out_file);
2387     }
2388 }
2389
2390 /* This function builds a dwarf location descriptor sequence from a
2391    dw_cfa_location, adding the given OFFSET to the result of the
2392    expression.  */
2393
2394 struct dw_loc_descr_node *
2395 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2396 {
2397   struct dw_loc_descr_node *head, *tmp;
2398
2399   offset += cfa->offset;
2400
2401   if (cfa->indirect)
2402     {
2403       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2404       head->dw_loc_oprnd1.val_class = dw_val_class_const;
2405       head->dw_loc_oprnd1.val_entry = NULL;
2406       tmp = new_loc_descr (DW_OP_deref, 0, 0);
2407       add_loc_descr (&head, tmp);
2408       if (offset != 0)
2409         {
2410           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2411           add_loc_descr (&head, tmp);
2412         }
2413     }
2414   else
2415     head = new_reg_loc_descr (cfa->reg, offset);
2416
2417   return head;
2418 }
2419
2420 /* This function builds a dwarf location descriptor sequence for
2421    the address at OFFSET from the CFA when stack is aligned to
2422    ALIGNMENT byte.  */
2423
2424 struct dw_loc_descr_node *
2425 build_cfa_aligned_loc (dw_cfa_location *cfa,
2426                        HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2427 {
2428   struct dw_loc_descr_node *head;
2429   unsigned int dwarf_fp
2430     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2431
2432   /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
2433   if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2434     {
2435       head = new_reg_loc_descr (dwarf_fp, 0);
2436       add_loc_descr (&head, int_loc_descriptor (alignment));
2437       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2438       loc_descr_plus_const (&head, offset);
2439     }
2440   else
2441     head = new_reg_loc_descr (dwarf_fp, offset);
2442   return head;
2443 }
2444 \f
2445 /* And now, the support for symbolic debugging information.  */
2446
2447 /* .debug_str support.  */
2448
2449 static void dwarf2out_init (const char *);
2450 static void dwarf2out_finish (const char *);
2451 static void dwarf2out_assembly_start (void);
2452 static void dwarf2out_define (unsigned int, const char *);
2453 static void dwarf2out_undef (unsigned int, const char *);
2454 static void dwarf2out_start_source_file (unsigned, const char *);
2455 static void dwarf2out_end_source_file (unsigned);
2456 static void dwarf2out_function_decl (tree);
2457 static void dwarf2out_begin_block (unsigned, unsigned);
2458 static void dwarf2out_end_block (unsigned, unsigned);
2459 static bool dwarf2out_ignore_block (const_tree);
2460 static void dwarf2out_global_decl (tree);
2461 static void dwarf2out_type_decl (tree, int);
2462 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2463 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2464                                                  dw_die_ref);
2465 static void dwarf2out_abstract_function (tree);
2466 static void dwarf2out_var_location (rtx_insn *);
2467 static void dwarf2out_begin_function (tree);
2468 static void dwarf2out_end_function (unsigned int);
2469 static void dwarf2out_set_name (tree, tree);
2470
2471 /* The debug hooks structure.  */
2472
2473 const struct gcc_debug_hooks dwarf2_debug_hooks =
2474 {
2475   dwarf2out_init,
2476   dwarf2out_finish,
2477   dwarf2out_assembly_start,
2478   dwarf2out_define,
2479   dwarf2out_undef,
2480   dwarf2out_start_source_file,
2481   dwarf2out_end_source_file,
2482   dwarf2out_begin_block,
2483   dwarf2out_end_block,
2484   dwarf2out_ignore_block,
2485   dwarf2out_source_line,
2486   dwarf2out_begin_prologue,
2487 #if VMS_DEBUGGING_INFO
2488   dwarf2out_vms_end_prologue,
2489   dwarf2out_vms_begin_epilogue,
2490 #else
2491   debug_nothing_int_charstar,
2492   debug_nothing_int_charstar,
2493 #endif
2494   dwarf2out_end_epilogue,
2495   dwarf2out_begin_function,
2496   dwarf2out_end_function,       /* end_function */
2497   dwarf2out_function_decl,      /* function_decl */
2498   dwarf2out_global_decl,
2499   dwarf2out_type_decl,          /* type_decl */
2500   dwarf2out_imported_module_or_decl,
2501   debug_nothing_tree,           /* deferred_inline_function */
2502   /* The DWARF 2 backend tries to reduce debugging bloat by not
2503      emitting the abstract description of inline functions until
2504      something tries to reference them.  */
2505   dwarf2out_abstract_function,  /* outlining_inline_function */
2506   debug_nothing_rtx_code_label, /* label */
2507   debug_nothing_int,            /* handle_pch */
2508   dwarf2out_var_location,
2509   dwarf2out_switch_text_section,
2510   dwarf2out_set_name,
2511   1,                            /* start_end_main_source_file */
2512   TYPE_SYMTAB_IS_DIE            /* tree_type_symtab_field */
2513 };
2514 \f
2515 /* NOTE: In the comments in this file, many references are made to
2516    "Debugging Information Entries".  This term is abbreviated as `DIE'
2517    throughout the remainder of this file.  */
2518
2519 /* An internal representation of the DWARF output is built, and then
2520    walked to generate the DWARF debugging info.  The walk of the internal
2521    representation is done after the entire program has been compiled.
2522    The types below are used to describe the internal representation.  */
2523
2524 /* Whether to put type DIEs into their own section .debug_types instead
2525    of making them part of the .debug_info section.  Only supported for
2526    Dwarf V4 or higher and the user didn't disable them through
2527    -fno-debug-types-section.  It is more efficient to put them in a
2528    separate comdat sections since the linker will then be able to
2529    remove duplicates.  But not all tools support .debug_types sections
2530    yet.  */
2531
2532 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2533
2534 /* Various DIE's use offsets relative to the beginning of the
2535    .debug_info section to refer to each other.  */
2536
2537 typedef long int dw_offset;
2538
2539 /* Define typedefs here to avoid circular dependencies.  */
2540
2541 typedef struct dw_attr_struct *dw_attr_ref;
2542 typedef struct dw_line_info_struct *dw_line_info_ref;
2543 typedef struct pubname_struct *pubname_ref;
2544 typedef struct dw_ranges_struct *dw_ranges_ref;
2545 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2546 typedef struct comdat_type_struct *comdat_type_node_ref;
2547
2548 /* The entries in the line_info table more-or-less mirror the opcodes
2549    that are used in the real dwarf line table.  Arrays of these entries
2550    are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2551    supported.  */
2552
2553 enum dw_line_info_opcode {
2554   /* Emit DW_LNE_set_address; the operand is the label index.  */
2555   LI_set_address,
2556
2557   /* Emit a row to the matrix with the given line.  This may be done
2558      via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2559      special opcodes.  */
2560   LI_set_line,
2561
2562   /* Emit a DW_LNS_set_file.  */
2563   LI_set_file,
2564
2565   /* Emit a DW_LNS_set_column.  */
2566   LI_set_column,
2567
2568   /* Emit a DW_LNS_negate_stmt; the operand is ignored.  */
2569   LI_negate_stmt,
2570
2571   /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored.  */
2572   LI_set_prologue_end,
2573   LI_set_epilogue_begin,
2574
2575   /* Emit a DW_LNE_set_discriminator.  */
2576   LI_set_discriminator
2577 };
2578
2579 typedef struct GTY(()) dw_line_info_struct {
2580   enum dw_line_info_opcode opcode;
2581   unsigned int val;
2582 } dw_line_info_entry;
2583
2584
2585 typedef struct GTY(()) dw_line_info_table_struct {
2586   /* The label that marks the end of this section.  */
2587   const char *end_label;
2588
2589   /* The values for the last row of the matrix, as collected in the table.
2590      These are used to minimize the changes to the next row.  */
2591   unsigned int file_num;
2592   unsigned int line_num;
2593   unsigned int column_num;
2594   int discrim_num;
2595   bool is_stmt;
2596   bool in_use;
2597
2598   vec<dw_line_info_entry, va_gc> *entries;
2599 } dw_line_info_table;
2600
2601 typedef dw_line_info_table *dw_line_info_table_p;
2602
2603
2604 /* Each DIE attribute has a field specifying the attribute kind,
2605    a link to the next attribute in the chain, and an attribute value.
2606    Attributes are typically linked below the DIE they modify.  */
2607
2608 typedef struct GTY(()) dw_attr_struct {
2609   enum dwarf_attribute dw_attr;
2610   dw_val_node dw_attr_val;
2611 }
2612 dw_attr_node;
2613
2614
2615 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
2616    The children of each node form a circular list linked by
2617    die_sib.  die_child points to the node *before* the "first" child node.  */
2618
2619 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
2620   union die_symbol_or_type_node
2621     {
2622       const char * GTY ((tag ("0"))) die_symbol;
2623       comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2624     }
2625   GTY ((desc ("%0.comdat_type_p"))) die_id;
2626   vec<dw_attr_node, va_gc> *die_attr;
2627   dw_die_ref die_parent;
2628   dw_die_ref die_child;
2629   dw_die_ref die_sib;
2630   dw_die_ref die_definition; /* ref from a specification to its definition */
2631   dw_offset die_offset;
2632   unsigned long die_abbrev;
2633   int die_mark;
2634   unsigned int decl_id;
2635   enum dwarf_tag die_tag;
2636   /* Die is used and must not be pruned as unused.  */
2637   BOOL_BITFIELD die_perennial_p : 1;
2638   BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2639   /* Lots of spare bits.  */
2640 }
2641 die_node;
2642
2643 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
2644 #define FOR_EACH_CHILD(die, c, expr) do {       \
2645   c = die->die_child;                           \
2646   if (c) do {                                   \
2647     c = c->die_sib;                             \
2648     expr;                                       \
2649   } while (c != die->die_child);                \
2650 } while (0)
2651
2652 /* The pubname structure */
2653
2654 typedef struct GTY(()) pubname_struct {
2655   dw_die_ref die;
2656   const char *name;
2657 }
2658 pubname_entry;
2659
2660
2661 struct GTY(()) dw_ranges_struct {
2662   /* If this is positive, it's a block number, otherwise it's a
2663      bitwise-negated index into dw_ranges_by_label.  */
2664   int num;
2665 };
2666
2667 /* A structure to hold a macinfo entry.  */
2668
2669 typedef struct GTY(()) macinfo_struct {
2670   unsigned char code;
2671   unsigned HOST_WIDE_INT lineno;
2672   const char *info;
2673 }
2674 macinfo_entry;
2675
2676
2677 struct GTY(()) dw_ranges_by_label_struct {
2678   const char *begin;
2679   const char *end;
2680 };
2681
2682 /* The comdat type node structure.  */
2683 typedef struct GTY(()) comdat_type_struct
2684 {
2685   dw_die_ref root_die;
2686   dw_die_ref type_die;
2687   dw_die_ref skeleton_die;
2688   char signature[DWARF_TYPE_SIGNATURE_SIZE];
2689   struct comdat_type_struct *next;
2690 }
2691 comdat_type_node;
2692
2693 /* The limbo die list structure.  */
2694 typedef struct GTY(()) limbo_die_struct {
2695   dw_die_ref die;
2696   tree created_for;
2697   struct limbo_die_struct *next;
2698 }
2699 limbo_die_node;
2700
2701 typedef struct skeleton_chain_struct
2702 {
2703   dw_die_ref old_die;
2704   dw_die_ref new_die;
2705   struct skeleton_chain_struct *parent;
2706 }
2707 skeleton_chain_node;
2708
2709 /* Define a macro which returns nonzero for a TYPE_DECL which was
2710    implicitly generated for a type.
2711
2712    Note that, unlike the C front-end (which generates a NULL named
2713    TYPE_DECL node for each complete tagged type, each array type,
2714    and each function type node created) the C++ front-end generates
2715    a _named_ TYPE_DECL node for each tagged type node created.
2716    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2717    generate a DW_TAG_typedef DIE for them.  Likewise with the Ada
2718    front-end, but for each type, tagged or not.  */
2719
2720 #define TYPE_DECL_IS_STUB(decl)                         \
2721   (DECL_NAME (decl) == NULL_TREE                        \
2722    || (DECL_ARTIFICIAL (decl)                           \
2723        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
2724            /* This is necessary for stub decls that     \
2725               appear in nested inline functions.  */    \
2726            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2727                && (decl_ultimate_origin (decl)          \
2728                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2729
2730 /* Information concerning the compilation unit's programming
2731    language, and compiler version.  */
2732
2733 /* Fixed size portion of the DWARF compilation unit header.  */
2734 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2735   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2736
2737 /* Fixed size portion of the DWARF comdat type unit header.  */
2738 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2739   (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2740    + DWARF_OFFSET_SIZE)
2741
2742 /* Fixed size portion of public names info.  */
2743 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2744
2745 /* Fixed size portion of the address range info.  */
2746 #define DWARF_ARANGES_HEADER_SIZE                                       \
2747   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
2748                 DWARF2_ADDR_SIZE * 2)                                   \
2749    - DWARF_INITIAL_LENGTH_SIZE)
2750
2751 /* Size of padding portion in the address range info.  It must be
2752    aligned to twice the pointer size.  */
2753 #define DWARF_ARANGES_PAD_SIZE \
2754   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2755                 DWARF2_ADDR_SIZE * 2)                              \
2756    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2757
2758 /* Use assembler line directives if available.  */
2759 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2760 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2761 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2762 #else
2763 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2764 #endif
2765 #endif
2766
2767 /* Minimum line offset in a special line info. opcode.
2768    This value was chosen to give a reasonable range of values.  */
2769 #define DWARF_LINE_BASE  -10
2770
2771 /* First special line opcode - leave room for the standard opcodes.  */
2772 #define DWARF_LINE_OPCODE_BASE  ((int)DW_LNS_set_isa + 1)
2773
2774 /* Range of line offsets in a special line info. opcode.  */
2775 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
2776
2777 /* Flag that indicates the initial value of the is_stmt_start flag.
2778    In the present implementation, we do not mark any lines as
2779    the beginning of a source statement, because that information
2780    is not made available by the GCC front-end.  */
2781 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2782
2783 /* Maximum number of operations per instruction bundle.  */
2784 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2785 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2786 #endif
2787
2788 /* This location is used by calc_die_sizes() to keep track
2789    the offset of each DIE within the .debug_info section.  */
2790 static unsigned long next_die_offset;
2791
2792 /* Record the root of the DIE's built for the current compilation unit.  */
2793 static GTY(()) dw_die_ref single_comp_unit_die;
2794
2795 /* A list of type DIEs that have been separated into comdat sections.  */
2796 static GTY(()) comdat_type_node *comdat_type_list;
2797
2798 /* A list of DIEs with a NULL parent waiting to be relocated.  */
2799 static GTY(()) limbo_die_node *limbo_die_list;
2800
2801 /* A list of DIEs for which we may have to generate
2802    DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set.  */
2803 static GTY(()) limbo_die_node *deferred_asm_name;
2804
2805 struct dwarf_file_hasher : ggc_hasher<dwarf_file_data *>
2806 {
2807   typedef const char *compare_type;
2808
2809   static hashval_t hash (dwarf_file_data *);
2810   static bool equal (dwarf_file_data *, const char *);
2811 };
2812
2813 /* Filenames referenced by this compilation unit.  */
2814 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
2815
2816 struct decl_die_hasher : ggc_hasher<die_node *>
2817 {
2818   typedef tree compare_type;
2819
2820   static hashval_t hash (die_node *);
2821   static bool equal (die_node *, tree);
2822 };
2823 /* A hash table of references to DIE's that describe declarations.
2824    The key is a DECL_UID() which is a unique number identifying each decl.  */
2825 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
2826
2827 struct block_die_hasher : ggc_hasher<die_struct *>
2828 {
2829   static hashval_t hash (die_struct *);
2830   static bool equal (die_struct *, die_struct *);
2831 };
2832
2833 /* A hash table of references to DIE's that describe COMMON blocks.
2834    The key is DECL_UID() ^ die_parent.  */
2835 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
2836
2837 typedef struct GTY(()) die_arg_entry_struct {
2838     dw_die_ref die;
2839     tree arg;
2840 } die_arg_entry;
2841
2842
2843 /* Node of the variable location list.  */
2844 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2845   /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2846      EXPR_LIST chain.  For small bitsizes, bitsize is encoded
2847      in mode of the EXPR_LIST node and first EXPR_LIST operand
2848      is either NOTE_INSN_VAR_LOCATION for a piece with a known
2849      location or NULL for padding.  For larger bitsizes,
2850      mode is 0 and first operand is a CONCAT with bitsize
2851      as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2852      NULL as second operand.  */
2853   rtx GTY (()) loc;
2854   const char * GTY (()) label;
2855   struct var_loc_node * GTY (()) next;
2856 };
2857
2858 /* Variable location list.  */
2859 struct GTY ((for_user)) var_loc_list_def {
2860   struct var_loc_node * GTY (()) first;
2861
2862   /* Pointer to the last but one or last element of the
2863      chained list.  If the list is empty, both first and
2864      last are NULL, if the list contains just one node
2865      or the last node certainly is not redundant, it points
2866      to the last node, otherwise points to the last but one.
2867      Do not mark it for GC because it is marked through the chain.  */
2868   struct var_loc_node * GTY ((skip ("%h"))) last;
2869
2870   /* Pointer to the last element before section switch,
2871      if NULL, either sections weren't switched or first
2872      is after section switch.  */
2873   struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2874
2875   /* DECL_UID of the variable decl.  */
2876   unsigned int decl_id;
2877 };
2878 typedef struct var_loc_list_def var_loc_list;
2879
2880 /* Call argument location list.  */
2881 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2882   rtx GTY (()) call_arg_loc_note;
2883   const char * GTY (()) label;
2884   tree GTY (()) block;
2885   bool tail_call_p;
2886   rtx GTY (()) symbol_ref;
2887   struct call_arg_loc_node * GTY (()) next;
2888 };
2889
2890
2891 struct decl_loc_hasher : ggc_hasher<var_loc_list *>
2892 {
2893   typedef const_tree compare_type;
2894
2895   static hashval_t hash (var_loc_list *);
2896   static bool equal (var_loc_list *, const_tree);
2897 };
2898
2899 /* Table of decl location linked lists.  */
2900 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
2901
2902 /* Head and tail of call_arg_loc chain.  */
2903 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2904 static struct call_arg_loc_node *call_arg_loc_last;
2905
2906 /* Number of call sites in the current function.  */
2907 static int call_site_count = -1;
2908 /* Number of tail call sites in the current function.  */
2909 static int tail_call_site_count = -1;
2910
2911 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
2912    DIEs.  */
2913 static vec<dw_die_ref> block_map;
2914
2915 /* A cached location list.  */
2916 struct GTY ((for_user)) cached_dw_loc_list_def {
2917   /* The DECL_UID of the decl that this entry describes.  */
2918   unsigned int decl_id;
2919
2920   /* The cached location list.  */
2921   dw_loc_list_ref loc_list;
2922 };
2923 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2924
2925 struct dw_loc_list_hasher : ggc_hasher<cached_dw_loc_list *>
2926 {
2927
2928   typedef const_tree compare_type;
2929   
2930   static hashval_t hash (cached_dw_loc_list *);
2931   static bool equal (cached_dw_loc_list *, const_tree);
2932 };
2933
2934 /* Table of cached location lists.  */
2935 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
2936
2937 /* A pointer to the base of a list of references to DIE's that
2938    are uniquely identified by their tag, presence/absence of
2939    children DIE's, and list of attribute/value pairs.  */
2940 static GTY((length ("abbrev_die_table_allocated")))
2941   dw_die_ref *abbrev_die_table;
2942
2943 /* Number of elements currently allocated for abbrev_die_table.  */
2944 static GTY(()) unsigned abbrev_die_table_allocated;
2945
2946 /* Number of elements in type_die_table currently in use.  */
2947 static GTY(()) unsigned abbrev_die_table_in_use;
2948
2949 /* Size (in elements) of increments by which we may expand the
2950    abbrev_die_table.  */
2951 #define ABBREV_DIE_TABLE_INCREMENT 256
2952
2953 /* A global counter for generating labels for line number data.  */
2954 static unsigned int line_info_label_num;
2955
2956 /* The current table to which we should emit line number information
2957    for the current function.  This will be set up at the beginning of
2958    assembly for the function.  */
2959 static dw_line_info_table *cur_line_info_table;
2960
2961 /* The two default tables of line number info.  */
2962 static GTY(()) dw_line_info_table *text_section_line_info;
2963 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2964
2965 /* The set of all non-default tables of line number info.  */
2966 static GTY(()) vec<dw_line_info_table_p, va_gc> *separate_line_info;
2967
2968 /* A flag to tell pubnames/types export if there is an info section to
2969    refer to.  */
2970 static bool info_section_emitted;
2971
2972 /* A pointer to the base of a table that contains a list of publicly
2973    accessible names.  */
2974 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
2975
2976 /* A pointer to the base of a table that contains a list of publicly
2977    accessible types.  */
2978 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
2979
2980 /* A pointer to the base of a table that contains a list of macro
2981    defines/undefines (and file start/end markers).  */
2982 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
2983
2984 /* True if .debug_macinfo or .debug_macros section is going to be
2985    emitted.  */
2986 #define have_macinfo \
2987   (debug_info_level >= DINFO_LEVEL_VERBOSE \
2988    && !macinfo_table->is_empty ())
2989
2990 /* Array of dies for which we should generate .debug_ranges info.  */
2991 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
2992
2993 /* Number of elements currently allocated for ranges_table.  */
2994 static GTY(()) unsigned ranges_table_allocated;
2995
2996 /* Number of elements in ranges_table currently in use.  */
2997 static GTY(()) unsigned ranges_table_in_use;
2998
2999 /* Array of pairs of labels referenced in ranges_table.  */
3000 static GTY ((length ("ranges_by_label_allocated")))
3001      dw_ranges_by_label_ref ranges_by_label;
3002
3003 /* Number of elements currently allocated for ranges_by_label.  */
3004 static GTY(()) unsigned ranges_by_label_allocated;
3005
3006 /* Number of elements in ranges_by_label currently in use.  */
3007 static GTY(()) unsigned ranges_by_label_in_use;
3008
3009 /* Size (in elements) of increments by which we may expand the
3010    ranges_table.  */
3011 #define RANGES_TABLE_INCREMENT 64
3012
3013 /* Whether we have location lists that need outputting */
3014 static GTY(()) bool have_location_lists;
3015
3016 /* Unique label counter.  */
3017 static GTY(()) unsigned int loclabel_num;
3018
3019 /* Unique label counter for point-of-call tables.  */
3020 static GTY(()) unsigned int poc_label_num;
3021
3022 /* The last file entry emitted by maybe_emit_file().  */
3023 static GTY(()) struct dwarf_file_data * last_emitted_file;
3024
3025 /* Number of internal labels generated by gen_internal_sym().  */
3026 static GTY(()) int label_num;
3027
3028 /* Cached result of previous call to lookup_filename.  */
3029 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
3030
3031 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3032
3033 /* Instances of generic types for which we need to generate debug
3034    info that describe their generic parameters and arguments. That
3035    generation needs to happen once all types are properly laid out so
3036    we do it at the end of compilation.  */
3037 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3038
3039 /* Offset from the "steady-state frame pointer" to the frame base,
3040    within the current function.  */
3041 static HOST_WIDE_INT frame_pointer_fb_offset;
3042 static bool frame_pointer_fb_offset_valid;
3043
3044 static vec<dw_die_ref> base_types;
3045
3046 /* Flags to represent a set of attribute classes for attributes that represent
3047    a scalar value (bounds, pointers, ...).  */
3048 enum dw_scalar_form
3049 {
3050   dw_scalar_form_constant = 0x01,
3051   dw_scalar_form_exprloc = 0x02,
3052   dw_scalar_form_reference = 0x04
3053 };
3054
3055 /* Forward declarations for functions defined in this file.  */
3056
3057 static int is_pseudo_reg (const_rtx);
3058 static tree type_main_variant (tree);
3059 static int is_tagged_type (const_tree);
3060 static const char *dwarf_tag_name (unsigned);
3061 static const char *dwarf_attr_name (unsigned);
3062 static const char *dwarf_form_name (unsigned);
3063 static tree decl_ultimate_origin (const_tree);
3064 static tree decl_class_context (tree);
3065 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
3066 static inline enum dw_val_class AT_class (dw_attr_ref);
3067 static inline unsigned int AT_index (dw_attr_ref);
3068 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3069 static inline unsigned AT_flag (dw_attr_ref);
3070 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3071 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
3072 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3073 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
3074 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3075                            HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3076 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3077                                unsigned int, unsigned char *);
3078 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3079 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3080 static inline const char *AT_string (dw_attr_ref);
3081 static enum dwarf_form AT_string_form (dw_attr_ref);
3082 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3083 static void add_AT_specification (dw_die_ref, dw_die_ref);
3084 static inline dw_die_ref AT_ref (dw_attr_ref);
3085 static inline int AT_ref_external (dw_attr_ref);
3086 static inline void set_AT_ref_external (dw_attr_ref, int);
3087 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3088 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3089 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
3090 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3091                              dw_loc_list_ref);
3092 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
3093 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3094 static void remove_addr_table_entry (addr_table_entry *);
3095 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3096 static inline rtx AT_addr (dw_attr_ref);
3097 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3098 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3099 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3100 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3101                            unsigned HOST_WIDE_INT);
3102 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3103                                unsigned long, bool);
3104 static inline const char *AT_lbl (dw_attr_ref);
3105 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
3106 static const char *get_AT_low_pc (dw_die_ref);
3107 static const char *get_AT_hi_pc (dw_die_ref);
3108 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3109 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3110 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3111 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3112 static bool is_cxx (void);
3113 static bool is_fortran (void);
3114 static bool is_ada (void);
3115 static void remove_AT (dw_die_ref, enum dwarf_attribute);
3116 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3117 static void add_child_die (dw_die_ref, dw_die_ref);
3118 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3119 static dw_die_ref lookup_type_die (tree);
3120 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3121 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3122 static void equate_type_number_to_die (tree, dw_die_ref);
3123 static dw_die_ref lookup_decl_die (tree);
3124 static var_loc_list *lookup_decl_loc (const_tree);
3125 static void equate_decl_number_to_die (tree, dw_die_ref);
3126 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3127 static void print_spaces (FILE *);
3128 static void print_die (dw_die_ref, FILE *);
3129 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3130 static dw_die_ref pop_compile_unit (dw_die_ref);
3131 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3132 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3133 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3134 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3135 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3136 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3137 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
3138                                    struct md5_ctx *, int *);
3139 struct checksum_attributes;
3140 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3141 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3142 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3143 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3144 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3145 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3146 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3147 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3148 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3149 static void compute_section_prefix (dw_die_ref);
3150 static int is_type_die (dw_die_ref);
3151 static int is_comdat_die (dw_die_ref);
3152 static int is_symbol_die (dw_die_ref);
3153 static inline bool is_template_instantiation (dw_die_ref);
3154 static void assign_symbol_names (dw_die_ref);
3155 static void break_out_includes (dw_die_ref);
3156 static int is_declaration_die (dw_die_ref);
3157 static int should_move_die_to_comdat (dw_die_ref);
3158 static dw_die_ref clone_as_declaration (dw_die_ref);
3159 static dw_die_ref clone_die (dw_die_ref);
3160 static dw_die_ref clone_tree (dw_die_ref);
3161 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3162 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3163 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3164 static dw_die_ref generate_skeleton (dw_die_ref);
3165 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3166                                                          dw_die_ref,
3167                                                          dw_die_ref);
3168 static void break_out_comdat_types (dw_die_ref);
3169 static void copy_decls_for_unworthy_types (dw_die_ref);
3170
3171 static void add_sibling_attributes (dw_die_ref);
3172 static void output_location_lists (dw_die_ref);
3173 static int constant_size (unsigned HOST_WIDE_INT);
3174 static unsigned long size_of_die (dw_die_ref);
3175 static void calc_die_sizes (dw_die_ref);
3176 static void calc_base_type_die_sizes (void);
3177 static void mark_dies (dw_die_ref);
3178 static void unmark_dies (dw_die_ref);
3179 static void unmark_all_dies (dw_die_ref);
3180 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3181 static unsigned long size_of_aranges (void);
3182 static enum dwarf_form value_format (dw_attr_ref);
3183 static void output_value_format (dw_attr_ref);
3184 static void output_abbrev_section (void);
3185 static void output_die_abbrevs (unsigned long, dw_die_ref);
3186 static void output_die_symbol (dw_die_ref);
3187 static void output_die (dw_die_ref);
3188 static void output_compilation_unit_header (void);
3189 static void output_comp_unit (dw_die_ref, int);
3190 static void output_comdat_type_unit (comdat_type_node *);
3191 static const char *dwarf2_name (tree, int);
3192 static void add_pubname (tree, dw_die_ref);
3193 static void add_enumerator_pubname (const char *, dw_die_ref);
3194 static void add_pubname_string (const char *, dw_die_ref);
3195 static void add_pubtype (tree, dw_die_ref);
3196 static void output_pubnames (vec<pubname_entry, va_gc> *);
3197 static void output_aranges (unsigned long);
3198 static unsigned int add_ranges_num (int);
3199 static unsigned int add_ranges (const_tree);
3200 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3201                                   bool *, bool);
3202 static void output_ranges (void);
3203 static dw_line_info_table *new_line_info_table (void);
3204 static void output_line_info (bool);
3205 static void output_file_names (void);
3206 static dw_die_ref base_type_die (tree);
3207 static int is_base_type (tree);
3208 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3209 static int decl_quals (const_tree);
3210 static dw_die_ref modified_type_die (tree, int, dw_die_ref);
3211 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3212 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3213 static int type_is_enum (const_tree);
3214 static unsigned int dbx_reg_number (const_rtx);
3215 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3216 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3217 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3218                                                 enum var_init_status);
3219 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3220                                                      enum var_init_status);
3221 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3222                                          enum var_init_status);
3223 static int is_based_loc (const_rtx);
3224 static bool resolve_one_addr (rtx *);
3225 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3226                                                enum var_init_status);
3227 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3228                                         enum var_init_status);
3229 struct loc_descr_context;
3230 static dw_loc_list_ref loc_list_from_tree (tree, int,
3231                                            const struct loc_descr_context *);
3232 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3233                                                   const struct loc_descr_context *);
3234 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3235 static tree field_type (const_tree);
3236 static unsigned int simple_type_align_in_bits (const_tree);
3237 static unsigned int simple_decl_align_in_bits (const_tree);
3238 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3239 static HOST_WIDE_INT field_byte_offset (const_tree);
3240 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3241                                          dw_loc_list_ref);
3242 static void add_data_member_location_attribute (dw_die_ref, tree);
3243 static bool add_const_value_attribute (dw_die_ref, rtx);
3244 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3245 static void insert_wide_int (const wide_int &, unsigned char *, int);
3246 static void insert_float (const_rtx, unsigned char *);
3247 static rtx rtl_for_decl_location (tree);
3248 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3249                                                    enum dwarf_attribute);
3250 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3251 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3252 static void add_name_attribute (dw_die_ref, const char *);
3253 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3254 static void add_comp_dir_attribute (dw_die_ref);
3255 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3256                              const struct loc_descr_context *);
3257 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3258                             const struct loc_descr_context *);
3259 static void add_subscript_info (dw_die_ref, tree, bool);
3260 static void add_byte_size_attribute (dw_die_ref, tree);
3261 static void add_bit_offset_attribute (dw_die_ref, tree);
3262 static void add_bit_size_attribute (dw_die_ref, tree);
3263 static void add_prototyped_attribute (dw_die_ref, tree);
3264 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3265 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3266 static void add_src_coords_attributes (dw_die_ref, tree);
3267 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3268 static void push_decl_scope (tree);
3269 static void pop_decl_scope (void);
3270 static dw_die_ref scope_die_for (tree, dw_die_ref);
3271 static inline int local_scope_p (dw_die_ref);
3272 static inline int class_scope_p (dw_die_ref);
3273 static inline int class_or_namespace_scope_p (dw_die_ref);
3274 static void add_type_attribute (dw_die_ref, tree, int, dw_die_ref);
3275 static void add_calling_convention_attribute (dw_die_ref, tree);
3276 static const char *type_tag (const_tree);
3277 static tree member_declared_type (const_tree);
3278 #if 0
3279 static const char *decl_start_label (tree);
3280 #endif
3281 static void gen_array_type_die (tree, dw_die_ref);
3282 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3283 #if 0
3284 static void gen_entry_point_die (tree, dw_die_ref);
3285 #endif
3286 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3287 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3288 static dw_die_ref gen_formal_parameter_pack_die  (tree, tree, dw_die_ref, tree*);
3289 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3290 static void gen_formal_types_die (tree, dw_die_ref);
3291 static void gen_subprogram_die (tree, dw_die_ref);
3292 static void gen_variable_die (tree, tree, dw_die_ref);
3293 static void gen_const_die (tree, dw_die_ref);
3294 static void gen_label_die (tree, dw_die_ref);
3295 static void gen_lexical_block_die (tree, dw_die_ref);
3296 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3297 static void gen_field_die (tree, dw_die_ref);
3298 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3299 static dw_die_ref gen_compile_unit_die (const char *);
3300 static void gen_inheritance_die (tree, tree, dw_die_ref);
3301 static void gen_member_die (tree, dw_die_ref);
3302 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3303                                                 enum debug_info_usage);
3304 static void gen_subroutine_type_die (tree, dw_die_ref);
3305 static void gen_typedef_die (tree, dw_die_ref);
3306 static void gen_type_die (tree, dw_die_ref);
3307 static void gen_block_die (tree, dw_die_ref);
3308 static void decls_for_scope (tree, dw_die_ref);
3309 static inline int is_redundant_typedef (const_tree);
3310 static bool is_naming_typedef_decl (const_tree);
3311 static inline dw_die_ref get_context_die (tree);
3312 static void gen_namespace_die (tree, dw_die_ref);
3313 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3314 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3315 static dw_die_ref force_decl_die (tree);
3316 static dw_die_ref force_type_die (tree);
3317 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3318 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3319 static struct dwarf_file_data * lookup_filename (const char *);
3320 static void retry_incomplete_types (void);
3321 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3322 static void gen_generic_params_dies (tree);
3323 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3324 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3325 static void splice_child_die (dw_die_ref, dw_die_ref);
3326 static int file_info_cmp (const void *, const void *);
3327 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3328                                      const char *, const char *);
3329 static void output_loc_list (dw_loc_list_ref);
3330 static char *gen_internal_sym (const char *);
3331 static bool want_pubnames (void);
3332
3333 static void prune_unmark_dies (dw_die_ref);
3334 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3335 static void prune_unused_types_mark (dw_die_ref, int);
3336 static void prune_unused_types_walk (dw_die_ref);
3337 static void prune_unused_types_walk_attribs (dw_die_ref);
3338 static void prune_unused_types_prune (dw_die_ref);
3339 static void prune_unused_types (void);
3340 static int maybe_emit_file (struct dwarf_file_data *fd);
3341 static inline const char *AT_vms_delta1 (dw_attr_ref);
3342 static inline const char *AT_vms_delta2 (dw_attr_ref);
3343 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3344                                      const char *, const char *);
3345 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3346 static void gen_remaining_tmpl_value_param_die_attribute (void);
3347 static bool generic_type_p (tree);
3348 static void schedule_generic_params_dies_gen (tree t);
3349 static void gen_scheduled_generic_parms_dies (void);
3350
3351 static const char *comp_dir_string (void);
3352
3353 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3354
3355 /* enum for tracking thread-local variables whose address is really an offset
3356    relative to the TLS pointer, which will need link-time relocation, but will
3357    not need relocation by the DWARF consumer.  */
3358
3359 enum dtprel_bool
3360 {
3361   dtprel_false = 0,
3362   dtprel_true = 1
3363 };
3364
3365 /* Return the operator to use for an address of a variable.  For dtprel_true, we
3366    use DW_OP_const*.  For regular variables, which need both link-time
3367    relocation and consumer-level relocation (e.g., to account for shared objects
3368    loaded at a random address), we use DW_OP_addr*.  */
3369
3370 static inline enum dwarf_location_atom
3371 dw_addr_op (enum dtprel_bool dtprel)
3372 {
3373   if (dtprel == dtprel_true)
3374     return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3375             : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3376   else
3377     return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3378 }
3379
3380 /* Return a pointer to a newly allocated address location description.  If
3381    dwarf_split_debug_info is true, then record the address with the appropriate
3382    relocation.  */
3383 static inline dw_loc_descr_ref
3384 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3385 {
3386   dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3387
3388   ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3389   ref->dw_loc_oprnd1.v.val_addr = addr;
3390   ref->dtprel = dtprel;
3391   if (dwarf_split_debug_info)
3392     ref->dw_loc_oprnd1.val_entry
3393         = add_addr_table_entry (addr,
3394                                 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3395   else
3396     ref->dw_loc_oprnd1.val_entry = NULL;
3397
3398   return ref;
3399 }
3400
3401 /* Section names used to hold DWARF debugging information.  */
3402
3403 #ifndef DEBUG_INFO_SECTION
3404 #define DEBUG_INFO_SECTION      ".debug_info"
3405 #endif
3406 #ifndef DEBUG_DWO_INFO_SECTION
3407 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3408 #endif
3409 #ifndef DEBUG_ABBREV_SECTION
3410 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
3411 #endif
3412 #ifndef DEBUG_DWO_ABBREV_SECTION
3413 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3414 #endif
3415 #ifndef DEBUG_ARANGES_SECTION
3416 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
3417 #endif
3418 #ifndef DEBUG_ADDR_SECTION
3419 #define DEBUG_ADDR_SECTION     ".debug_addr"
3420 #endif
3421 #ifndef DEBUG_NORM_MACINFO_SECTION
3422 #define DEBUG_NORM_MACINFO_SECTION     ".debug_macinfo"
3423 #endif
3424 #ifndef DEBUG_DWO_MACINFO_SECTION
3425 #define DEBUG_DWO_MACINFO_SECTION      ".debug_macinfo.dwo"
3426 #endif
3427 #ifndef DEBUG_MACINFO_SECTION
3428 #define DEBUG_MACINFO_SECTION                                           \
3429   (!dwarf_split_debug_info                                              \
3430    ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3431 #endif
3432 #ifndef DEBUG_NORM_MACRO_SECTION
3433 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3434 #endif
3435 #ifndef DEBUG_DWO_MACRO_SECTION
3436 #define DEBUG_DWO_MACRO_SECTION        ".debug_macro.dwo"
3437 #endif
3438 #ifndef DEBUG_MACRO_SECTION
3439 #define DEBUG_MACRO_SECTION                                             \
3440   (!dwarf_split_debug_info                                              \
3441    ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3442 #endif
3443 #ifndef DEBUG_LINE_SECTION
3444 #define DEBUG_LINE_SECTION      ".debug_line"
3445 #endif
3446 #ifndef DEBUG_DWO_LINE_SECTION
3447 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3448 #endif
3449 #ifndef DEBUG_LOC_SECTION
3450 #define DEBUG_LOC_SECTION       ".debug_loc"
3451 #endif
3452 #ifndef DEBUG_DWO_LOC_SECTION
3453 #define DEBUG_DWO_LOC_SECTION  ".debug_loc.dwo"
3454 #endif
3455 #ifndef DEBUG_PUBNAMES_SECTION
3456 #define DEBUG_PUBNAMES_SECTION  \
3457   ((debug_generate_pub_sections == 2) \
3458    ? ".debug_gnu_pubnames" : ".debug_pubnames")
3459 #endif
3460 #ifndef DEBUG_PUBTYPES_SECTION
3461 #define DEBUG_PUBTYPES_SECTION  \
3462   ((debug_generate_pub_sections == 2) \
3463    ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3464 #endif
3465 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3466 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3467 #ifndef DEBUG_STR_OFFSETS_SECTION
3468 #define DEBUG_STR_OFFSETS_SECTION                                       \
3469   (!dwarf_split_debug_info                                              \
3470    ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3471 #endif
3472 #ifndef DEBUG_STR_DWO_SECTION
3473 #define DEBUG_STR_DWO_SECTION   ".debug_str.dwo"
3474 #endif
3475 #ifndef DEBUG_STR_SECTION
3476 #define DEBUG_STR_SECTION  ".debug_str"
3477 #endif
3478 #ifndef DEBUG_RANGES_SECTION
3479 #define DEBUG_RANGES_SECTION    ".debug_ranges"
3480 #endif
3481
3482 /* Standard ELF section names for compiled code and data.  */
3483 #ifndef TEXT_SECTION_NAME
3484 #define TEXT_SECTION_NAME       ".text"
3485 #endif
3486
3487 /* Section flags for .debug_macinfo/.debug_macro section.  */
3488 #define DEBUG_MACRO_SECTION_FLAGS                                       \
3489   (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3490
3491 /* Section flags for .debug_str section.  */
3492 #define DEBUG_STR_SECTION_FLAGS                                 \
3493   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
3494    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
3495    : SECTION_DEBUG)
3496
3497 /* Section flags for .debug_str.dwo section.  */
3498 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3499
3500 /* Labels we insert at beginning sections we can reference instead of
3501    the section names themselves.  */
3502
3503 #ifndef TEXT_SECTION_LABEL
3504 #define TEXT_SECTION_LABEL                 "Ltext"
3505 #endif
3506 #ifndef COLD_TEXT_SECTION_LABEL
3507 #define COLD_TEXT_SECTION_LABEL             "Ltext_cold"
3508 #endif
3509 #ifndef DEBUG_LINE_SECTION_LABEL
3510 #define DEBUG_LINE_SECTION_LABEL           "Ldebug_line"
3511 #endif
3512 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3513 #define DEBUG_SKELETON_LINE_SECTION_LABEL   "Lskeleton_debug_line"
3514 #endif
3515 #ifndef DEBUG_INFO_SECTION_LABEL
3516 #define DEBUG_INFO_SECTION_LABEL           "Ldebug_info"
3517 #endif
3518 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3519 #define DEBUG_SKELETON_INFO_SECTION_LABEL   "Lskeleton_debug_info"
3520 #endif
3521 #ifndef DEBUG_ABBREV_SECTION_LABEL
3522 #define DEBUG_ABBREV_SECTION_LABEL         "Ldebug_abbrev"
3523 #endif
3524 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3525 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3526 #endif
3527 #ifndef DEBUG_ADDR_SECTION_LABEL
3528 #define DEBUG_ADDR_SECTION_LABEL           "Ldebug_addr"
3529 #endif
3530 #ifndef DEBUG_LOC_SECTION_LABEL
3531 #define DEBUG_LOC_SECTION_LABEL                    "Ldebug_loc"
3532 #endif
3533 #ifndef DEBUG_RANGES_SECTION_LABEL
3534 #define DEBUG_RANGES_SECTION_LABEL         "Ldebug_ranges"
3535 #endif
3536 #ifndef DEBUG_MACINFO_SECTION_LABEL
3537 #define DEBUG_MACINFO_SECTION_LABEL         "Ldebug_macinfo"
3538 #endif
3539 #ifndef DEBUG_MACRO_SECTION_LABEL
3540 #define DEBUG_MACRO_SECTION_LABEL          "Ldebug_macro"
3541 #endif
3542 #define SKELETON_COMP_DIE_ABBREV 1
3543 #define SKELETON_TYPE_DIE_ABBREV 2
3544
3545 /* Definitions of defaults for formats and names of various special
3546    (artificial) labels which may be generated within this file (when the -g
3547    options is used and DWARF2_DEBUGGING_INFO is in effect.
3548    If necessary, these may be overridden from within the tm.h file, but
3549    typically, overriding these defaults is unnecessary.  */
3550
3551 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3552 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3553 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3554 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3555 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3556 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3557 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3558 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3559 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3560 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3561 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3562 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3563 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3564 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3565
3566 #ifndef TEXT_END_LABEL
3567 #define TEXT_END_LABEL          "Letext"
3568 #endif
3569 #ifndef COLD_END_LABEL
3570 #define COLD_END_LABEL          "Letext_cold"
3571 #endif
3572 #ifndef BLOCK_BEGIN_LABEL
3573 #define BLOCK_BEGIN_LABEL       "LBB"
3574 #endif
3575 #ifndef BLOCK_END_LABEL
3576 #define BLOCK_END_LABEL         "LBE"
3577 #endif
3578 #ifndef LINE_CODE_LABEL
3579 #define LINE_CODE_LABEL         "LM"
3580 #endif
3581
3582 \f
3583 /* Return the root of the DIE's built for the current compilation unit.  */
3584 static dw_die_ref
3585 comp_unit_die (void)
3586 {
3587   if (!single_comp_unit_die)
3588     single_comp_unit_die = gen_compile_unit_die (NULL);
3589   return single_comp_unit_die;
3590 }
3591
3592 /* We allow a language front-end to designate a function that is to be
3593    called to "demangle" any name before it is put into a DIE.  */
3594
3595 static const char *(*demangle_name_func) (const char *);
3596
3597 void
3598 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3599 {
3600   demangle_name_func = func;
3601 }
3602
3603 /* Test if rtl node points to a pseudo register.  */
3604
3605 static inline int
3606 is_pseudo_reg (const_rtx rtl)
3607 {
3608   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3609           || (GET_CODE (rtl) == SUBREG
3610               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3611 }
3612
3613 /* Return a reference to a type, with its const and volatile qualifiers
3614    removed.  */
3615
3616 static inline tree
3617 type_main_variant (tree type)
3618 {
3619   type = TYPE_MAIN_VARIANT (type);
3620
3621   /* ??? There really should be only one main variant among any group of
3622      variants of a given type (and all of the MAIN_VARIANT values for all
3623      members of the group should point to that one type) but sometimes the C
3624      front-end messes this up for array types, so we work around that bug
3625      here.  */
3626   if (TREE_CODE (type) == ARRAY_TYPE)
3627     while (type != TYPE_MAIN_VARIANT (type))
3628       type = TYPE_MAIN_VARIANT (type);
3629
3630   return type;
3631 }
3632
3633 /* Return nonzero if the given type node represents a tagged type.  */
3634
3635 static inline int
3636 is_tagged_type (const_tree type)
3637 {
3638   enum tree_code code = TREE_CODE (type);
3639
3640   return (code == RECORD_TYPE || code == UNION_TYPE
3641           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3642 }
3643
3644 /* Set label to debug_info_section_label + die_offset of a DIE reference.  */
3645
3646 static void
3647 get_ref_die_offset_label (char *label, dw_die_ref ref)
3648 {
3649   sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3650 }
3651
3652 /* Return die_offset of a DIE reference to a base type.  */
3653
3654 static unsigned long int
3655 get_base_type_offset (dw_die_ref ref)
3656 {
3657   if (ref->die_offset)
3658     return ref->die_offset;
3659   if (comp_unit_die ()->die_abbrev)
3660     {
3661       calc_base_type_die_sizes ();
3662       gcc_assert (ref->die_offset);
3663     }
3664   return ref->die_offset;
3665 }
3666
3667 /* Return die_offset of a DIE reference other than base type.  */
3668
3669 static unsigned long int
3670 get_ref_die_offset (dw_die_ref ref)
3671 {
3672   gcc_assert (ref->die_offset);
3673   return ref->die_offset;
3674 }
3675
3676 /* Convert a DIE tag into its string name.  */
3677
3678 static const char *
3679 dwarf_tag_name (unsigned int tag)
3680 {
3681   const char *name = get_DW_TAG_name (tag);
3682
3683   if (name != NULL)
3684     return name;
3685
3686   return "DW_TAG_<unknown>";
3687 }
3688
3689 /* Convert a DWARF attribute code into its string name.  */
3690
3691 static const char *
3692 dwarf_attr_name (unsigned int attr)
3693 {
3694   const char *name;
3695
3696   switch (attr)
3697     {
3698 #if VMS_DEBUGGING_INFO
3699     case DW_AT_HP_prologue:
3700       return "DW_AT_HP_prologue";
3701 #else
3702     case DW_AT_MIPS_loop_unroll_factor:
3703       return "DW_AT_MIPS_loop_unroll_factor";
3704 #endif
3705
3706 #if VMS_DEBUGGING_INFO
3707     case DW_AT_HP_epilogue:
3708       return "DW_AT_HP_epilogue";
3709 #else
3710     case DW_AT_MIPS_stride:
3711       return "DW_AT_MIPS_stride";
3712 #endif
3713     }
3714
3715   name = get_DW_AT_name (attr);
3716
3717   if (name != NULL)
3718     return name;
3719
3720   return "DW_AT_<unknown>";
3721 }
3722
3723 /* Convert a DWARF value form code into its string name.  */
3724
3725 static const char *
3726 dwarf_form_name (unsigned int form)
3727 {
3728   const char *name = get_DW_FORM_name (form);
3729
3730   if (name != NULL)
3731     return name;
3732
3733   return "DW_FORM_<unknown>";
3734 }
3735 \f
3736 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
3737    instance of an inlined instance of a decl which is local to an inline
3738    function, so we have to trace all of the way back through the origin chain
3739    to find out what sort of node actually served as the original seed for the
3740    given block.  */
3741
3742 static tree
3743 decl_ultimate_origin (const_tree decl)
3744 {
3745   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3746     return NULL_TREE;
3747
3748   /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
3749      we're trying to output the abstract instance of this function.  */
3750   if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3751     return NULL_TREE;
3752
3753   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3754      most distant ancestor, this should never happen.  */
3755   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3756
3757   return DECL_ABSTRACT_ORIGIN (decl);
3758 }
3759
3760 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
3761    of a virtual function may refer to a base class, so we check the 'this'
3762    parameter.  */
3763
3764 static tree
3765 decl_class_context (tree decl)
3766 {
3767   tree context = NULL_TREE;
3768
3769   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3770     context = DECL_CONTEXT (decl);
3771   else
3772     context = TYPE_MAIN_VARIANT
3773       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3774
3775   if (context && !TYPE_P (context))
3776     context = NULL_TREE;
3777
3778   return context;
3779 }
3780 \f
3781 /* Add an attribute/value pair to a DIE.  */
3782
3783 static inline void
3784 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
3785 {
3786   /* Maybe this should be an assert?  */
3787   if (die == NULL)
3788     return;
3789
3790   vec_safe_reserve (die->die_attr, 1);
3791   vec_safe_push (die->die_attr, *attr);
3792 }
3793
3794 static inline enum dw_val_class
3795 AT_class (dw_attr_ref a)
3796 {
3797   return a->dw_attr_val.val_class;
3798 }
3799
3800 /* Return the index for any attribute that will be referenced with a
3801    DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index.  String indices
3802    are stored in dw_attr_val.v.val_str for reference counting
3803    pruning.  */
3804
3805 static inline unsigned int
3806 AT_index (dw_attr_ref a)
3807 {
3808   if (AT_class (a) == dw_val_class_str)
3809     return a->dw_attr_val.v.val_str->index;
3810   else if (a->dw_attr_val.val_entry != NULL)
3811     return a->dw_attr_val.val_entry->index;
3812   return NOT_INDEXED;
3813 }
3814
3815 /* Add a flag value attribute to a DIE.  */
3816
3817 static inline void
3818 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3819 {
3820   dw_attr_node attr;
3821
3822   attr.dw_attr = attr_kind;
3823   attr.dw_attr_val.val_class = dw_val_class_flag;
3824   attr.dw_attr_val.val_entry = NULL;
3825   attr.dw_attr_val.v.val_flag = flag;
3826   add_dwarf_attr (die, &attr);
3827 }
3828
3829 static inline unsigned
3830 AT_flag (dw_attr_ref a)
3831 {
3832   gcc_assert (a && AT_class (a) == dw_val_class_flag);
3833   return a->dw_attr_val.v.val_flag;
3834 }
3835
3836 /* Add a signed integer attribute value to a DIE.  */
3837
3838 static inline void
3839 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3840 {
3841   dw_attr_node attr;
3842
3843   attr.dw_attr = attr_kind;
3844   attr.dw_attr_val.val_class = dw_val_class_const;
3845   attr.dw_attr_val.val_entry = NULL;
3846   attr.dw_attr_val.v.val_int = int_val;
3847   add_dwarf_attr (die, &attr);
3848 }
3849
3850 static inline HOST_WIDE_INT
3851 AT_int (dw_attr_ref a)
3852 {
3853   gcc_assert (a && AT_class (a) == dw_val_class_const);
3854   return a->dw_attr_val.v.val_int;
3855 }
3856
3857 /* Add an unsigned integer attribute value to a DIE.  */
3858
3859 static inline void
3860 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3861                  unsigned HOST_WIDE_INT unsigned_val)
3862 {
3863   dw_attr_node attr;
3864
3865   attr.dw_attr = attr_kind;
3866   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3867   attr.dw_attr_val.val_entry = NULL;
3868   attr.dw_attr_val.v.val_unsigned = unsigned_val;
3869   add_dwarf_attr (die, &attr);
3870 }
3871
3872 static inline unsigned HOST_WIDE_INT
3873 AT_unsigned (dw_attr_ref a)
3874 {
3875   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3876   return a->dw_attr_val.v.val_unsigned;
3877 }
3878
3879 /* Add an unsigned wide integer attribute value to a DIE.  */
3880
3881 static inline void
3882 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
3883              const wide_int& w)
3884 {
3885   dw_attr_node attr;
3886
3887   attr.dw_attr = attr_kind;
3888   attr.dw_attr_val.val_class = dw_val_class_wide_int;
3889   attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
3890   *attr.dw_attr_val.v.val_wide = w;
3891   add_dwarf_attr (die, &attr);
3892 }
3893
3894 /* Add an unsigned double integer attribute value to a DIE.  */
3895
3896 static inline void
3897 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3898                HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3899 {
3900   dw_attr_node attr;
3901
3902   attr.dw_attr = attr_kind;
3903   attr.dw_attr_val.val_class = dw_val_class_const_double;
3904   attr.dw_attr_val.val_entry = NULL;
3905   attr.dw_attr_val.v.val_double.high = high;
3906   attr.dw_attr_val.v.val_double.low = low;
3907   add_dwarf_attr (die, &attr);
3908 }
3909
3910 /* Add a floating point attribute value to a DIE and return it.  */
3911
3912 static inline void
3913 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3914             unsigned int length, unsigned int elt_size, unsigned char *array)
3915 {
3916   dw_attr_node attr;
3917
3918   attr.dw_attr = attr_kind;
3919   attr.dw_attr_val.val_class = dw_val_class_vec;
3920   attr.dw_attr_val.val_entry = NULL;
3921   attr.dw_attr_val.v.val_vec.length = length;
3922   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3923   attr.dw_attr_val.v.val_vec.array = array;
3924   add_dwarf_attr (die, &attr);
3925 }
3926
3927 /* Add an 8-byte data attribute value to a DIE.  */
3928
3929 static inline void
3930 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3931               unsigned char data8[8])
3932 {
3933   dw_attr_node attr;
3934
3935   attr.dw_attr = attr_kind;
3936   attr.dw_attr_val.val_class = dw_val_class_data8;
3937   attr.dw_attr_val.val_entry = NULL;
3938   memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3939   add_dwarf_attr (die, &attr);
3940 }
3941
3942 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE.  When using
3943    dwarf_split_debug_info, address attributes in dies destined for the
3944    final executable have force_direct set to avoid using indexed
3945    references.  */
3946
3947 static inline void
3948 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3949                     bool force_direct)
3950 {
3951   dw_attr_node attr;
3952   char * lbl_id;
3953
3954   lbl_id = xstrdup (lbl_low);
3955   attr.dw_attr = DW_AT_low_pc;
3956   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3957   attr.dw_attr_val.v.val_lbl_id = lbl_id;
3958   if (dwarf_split_debug_info && !force_direct)
3959     attr.dw_attr_val.val_entry
3960         = add_addr_table_entry (lbl_id, ate_kind_label);
3961   else
3962     attr.dw_attr_val.val_entry = NULL;
3963   add_dwarf_attr (die, &attr);
3964
3965   attr.dw_attr = DW_AT_high_pc;
3966   if (dwarf_version < 4)
3967     attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3968   else
3969     attr.dw_attr_val.val_class = dw_val_class_high_pc;
3970   lbl_id = xstrdup (lbl_high);
3971   attr.dw_attr_val.v.val_lbl_id = lbl_id;
3972   if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3973       && dwarf_split_debug_info && !force_direct)
3974     attr.dw_attr_val.val_entry
3975         = add_addr_table_entry (lbl_id, ate_kind_label);
3976   else
3977     attr.dw_attr_val.val_entry = NULL;
3978   add_dwarf_attr (die, &attr);
3979 }
3980
3981 /* Hash and equality functions for debug_str_hash.  */
3982
3983 hashval_t
3984 indirect_string_hasher::hash (indirect_string_node *x)
3985 {
3986   return htab_hash_string (x->str);
3987 }
3988
3989 bool
3990 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
3991 {
3992   return strcmp (x1->str, x2) == 0;
3993 }
3994
3995 /* Add STR to the given string hash table.  */
3996
3997 static struct indirect_string_node *
3998 find_AT_string_in_table (const char *str,
3999                          hash_table<indirect_string_hasher> *table)
4000 {
4001   struct indirect_string_node *node;
4002
4003   indirect_string_node **slot
4004     = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
4005   if (*slot == NULL)
4006     {
4007       node = ggc_cleared_alloc<indirect_string_node> ();
4008       node->str = ggc_strdup (str);
4009       *slot = node;
4010     }
4011   else
4012     node = *slot;
4013
4014   node->refcount++;
4015   return node;
4016 }
4017
4018 /* Add STR to the indirect string hash table.  */
4019
4020 static struct indirect_string_node *
4021 find_AT_string (const char *str)
4022 {
4023   if (! debug_str_hash)
4024     debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4025
4026   return find_AT_string_in_table (str, debug_str_hash);
4027 }
4028
4029 /* Add a string attribute value to a DIE.  */
4030
4031 static inline void
4032 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4033 {
4034   dw_attr_node attr;
4035   struct indirect_string_node *node;
4036
4037   node = find_AT_string (str);
4038
4039   attr.dw_attr = attr_kind;
4040   attr.dw_attr_val.val_class = dw_val_class_str;
4041   attr.dw_attr_val.val_entry = NULL;
4042   attr.dw_attr_val.v.val_str = node;
4043   add_dwarf_attr (die, &attr);
4044 }
4045
4046 static inline const char *
4047 AT_string (dw_attr_ref a)
4048 {
4049   gcc_assert (a && AT_class (a) == dw_val_class_str);
4050   return a->dw_attr_val.v.val_str->str;
4051 }
4052
4053 /* Call this function directly to bypass AT_string_form's logic to put
4054    the string inline in the die. */
4055
4056 static void
4057 set_indirect_string (struct indirect_string_node *node)
4058 {
4059   char label[32];
4060   /* Already indirect is a no op.  */
4061   if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
4062     {
4063       gcc_assert (node->label);
4064       return;
4065     }
4066   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4067   ++dw2_string_counter;
4068   node->label = xstrdup (label);
4069
4070   if (!dwarf_split_debug_info)
4071     {
4072       node->form = DW_FORM_strp;
4073       node->index = NOT_INDEXED;
4074     }
4075   else
4076     {
4077       node->form = DW_FORM_GNU_str_index;
4078       node->index = NO_INDEX_ASSIGNED;
4079     }
4080 }
4081
4082 /* Find out whether a string should be output inline in DIE
4083    or out-of-line in .debug_str section.  */
4084
4085 static enum dwarf_form
4086 find_string_form (struct indirect_string_node *node)
4087 {
4088   unsigned int len;
4089
4090   if (node->form)
4091     return node->form;
4092
4093   len = strlen (node->str) + 1;
4094
4095   /* If the string is shorter or equal to the size of the reference, it is
4096      always better to put it inline.  */
4097   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4098     return node->form = DW_FORM_string;
4099
4100   /* If we cannot expect the linker to merge strings in .debug_str
4101      section, only put it into .debug_str if it is worth even in this
4102      single module.  */
4103   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4104       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4105       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4106     return node->form = DW_FORM_string;
4107
4108   set_indirect_string (node);
4109
4110   return node->form;
4111 }
4112
4113 /* Find out whether the string referenced from the attribute should be
4114    output inline in DIE or out-of-line in .debug_str section.  */
4115
4116 static enum dwarf_form
4117 AT_string_form (dw_attr_ref a)
4118 {
4119   gcc_assert (a && AT_class (a) == dw_val_class_str);
4120   return find_string_form (a->dw_attr_val.v.val_str);
4121 }
4122
4123 /* Add a DIE reference attribute value to a DIE.  */
4124
4125 static inline void
4126 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4127 {
4128   dw_attr_node attr;
4129
4130 #ifdef ENABLE_CHECKING
4131   gcc_assert (targ_die != NULL);
4132 #else
4133   /* With LTO we can end up trying to reference something we didn't create
4134      a DIE for.  Avoid crashing later on a NULL referenced DIE.  */
4135   if (targ_die == NULL)
4136     return;
4137 #endif
4138
4139   attr.dw_attr = attr_kind;
4140   attr.dw_attr_val.val_class = dw_val_class_die_ref;
4141   attr.dw_attr_val.val_entry = NULL;
4142   attr.dw_attr_val.v.val_die_ref.die = targ_die;
4143   attr.dw_attr_val.v.val_die_ref.external = 0;
4144   add_dwarf_attr (die, &attr);
4145 }
4146
4147 /* Change DIE reference REF to point to NEW_DIE instead.  */
4148
4149 static inline void
4150 change_AT_die_ref (dw_attr_ref ref, dw_die_ref new_die)
4151 {
4152   gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4153   ref->dw_attr_val.v.val_die_ref.die = new_die;
4154   ref->dw_attr_val.v.val_die_ref.external = 0;
4155 }
4156
4157 /* Add an AT_specification attribute to a DIE, and also make the back
4158    pointer from the specification to the definition.  */
4159
4160 static inline void
4161 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4162 {
4163   add_AT_die_ref (die, DW_AT_specification, targ_die);
4164   gcc_assert (!targ_die->die_definition);
4165   targ_die->die_definition = die;
4166 }
4167
4168 static inline dw_die_ref
4169 AT_ref (dw_attr_ref a)
4170 {
4171   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4172   return a->dw_attr_val.v.val_die_ref.die;
4173 }
4174
4175 static inline int
4176 AT_ref_external (dw_attr_ref a)
4177 {
4178   if (a && AT_class (a) == dw_val_class_die_ref)
4179     return a->dw_attr_val.v.val_die_ref.external;
4180
4181   return 0;
4182 }
4183
4184 static inline void
4185 set_AT_ref_external (dw_attr_ref a, int i)
4186 {
4187   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4188   a->dw_attr_val.v.val_die_ref.external = i;
4189 }
4190
4191 /* Add an FDE reference attribute value to a DIE.  */
4192
4193 static inline void
4194 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4195 {
4196   dw_attr_node attr;
4197
4198   attr.dw_attr = attr_kind;
4199   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4200   attr.dw_attr_val.val_entry = NULL;
4201   attr.dw_attr_val.v.val_fde_index = targ_fde;
4202   add_dwarf_attr (die, &attr);
4203 }
4204
4205 /* Add a location description attribute value to a DIE.  */
4206
4207 static inline void
4208 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4209 {
4210   dw_attr_node attr;
4211
4212   attr.dw_attr = attr_kind;
4213   attr.dw_attr_val.val_class = dw_val_class_loc;
4214   attr.dw_attr_val.val_entry = NULL;
4215   attr.dw_attr_val.v.val_loc = loc;
4216   add_dwarf_attr (die, &attr);
4217 }
4218
4219 static inline dw_loc_descr_ref
4220 AT_loc (dw_attr_ref a)
4221 {
4222   gcc_assert (a && AT_class (a) == dw_val_class_loc);
4223   return a->dw_attr_val.v.val_loc;
4224 }
4225
4226 static inline void
4227 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4228 {
4229   dw_attr_node attr;
4230
4231   attr.dw_attr = attr_kind;
4232   attr.dw_attr_val.val_class = dw_val_class_loc_list;
4233   attr.dw_attr_val.val_entry = NULL;
4234   attr.dw_attr_val.v.val_loc_list = loc_list;
4235   add_dwarf_attr (die, &attr);
4236   have_location_lists = true;
4237 }
4238
4239 static inline dw_loc_list_ref
4240 AT_loc_list (dw_attr_ref a)
4241 {
4242   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4243   return a->dw_attr_val.v.val_loc_list;
4244 }
4245
4246 static inline dw_loc_list_ref *
4247 AT_loc_list_ptr (dw_attr_ref a)
4248 {
4249   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4250   return &a->dw_attr_val.v.val_loc_list;
4251 }
4252
4253 struct addr_hasher : ggc_hasher<addr_table_entry *>
4254 {
4255   static hashval_t hash (addr_table_entry *);
4256   static bool equal (addr_table_entry *, addr_table_entry *);
4257 };
4258
4259 /* Table of entries into the .debug_addr section.  */
4260
4261 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4262
4263 /* Hash an address_table_entry.  */
4264
4265 hashval_t
4266 addr_hasher::hash (addr_table_entry *a)
4267 {
4268   inchash::hash hstate;
4269   switch (a->kind)
4270     {
4271       case ate_kind_rtx:
4272         hstate.add_int (0);
4273         break;
4274       case ate_kind_rtx_dtprel:
4275         hstate.add_int (1);
4276         break;
4277       case ate_kind_label:
4278         return htab_hash_string (a->addr.label);
4279       default:
4280         gcc_unreachable ();
4281     }
4282   inchash::add_rtx (a->addr.rtl, hstate);
4283   return hstate.end ();
4284 }
4285
4286 /* Determine equality for two address_table_entries.  */
4287
4288 bool
4289 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4290 {
4291   if (a1->kind != a2->kind)
4292     return 0;
4293   switch (a1->kind)
4294     {
4295       case ate_kind_rtx:
4296       case ate_kind_rtx_dtprel:
4297         return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4298       case ate_kind_label:
4299         return strcmp (a1->addr.label, a2->addr.label) == 0;
4300       default:
4301         gcc_unreachable ();
4302     }
4303 }
4304
4305 /* Initialize an addr_table_entry.  */
4306
4307 void
4308 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4309 {
4310   e->kind = kind;
4311   switch (kind)
4312     {
4313       case ate_kind_rtx:
4314       case ate_kind_rtx_dtprel:
4315         e->addr.rtl = (rtx) addr;
4316         break;
4317       case ate_kind_label:
4318         e->addr.label = (char *) addr;
4319         break;
4320     }
4321   e->refcount = 0;
4322   e->index = NO_INDEX_ASSIGNED;
4323 }
4324
4325 /* Add attr to the address table entry to the table.  Defer setting an
4326    index until output time.  */
4327
4328 static addr_table_entry *
4329 add_addr_table_entry (void *addr, enum ate_kind kind)
4330 {
4331   addr_table_entry *node;
4332   addr_table_entry finder;
4333
4334   gcc_assert (dwarf_split_debug_info);
4335   if (! addr_index_table)
4336     addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4337   init_addr_table_entry (&finder, kind, addr);
4338   addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4339
4340   if (*slot == HTAB_EMPTY_ENTRY)
4341     {
4342       node = ggc_cleared_alloc<addr_table_entry> ();
4343       init_addr_table_entry (node, kind, addr);
4344       *slot = node;
4345     }
4346   else
4347     node = *slot;
4348
4349   node->refcount++;
4350   return node;
4351 }
4352
4353 /* Remove an entry from the addr table by decrementing its refcount.
4354    Strictly, decrementing the refcount would be enough, but the
4355    assertion that the entry is actually in the table has found
4356    bugs.  */
4357
4358 static void
4359 remove_addr_table_entry (addr_table_entry *entry)
4360 {
4361   gcc_assert (dwarf_split_debug_info && addr_index_table);
4362   /* After an index is assigned, the table is frozen.  */
4363   gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4364   entry->refcount--;
4365 }
4366
4367 /* Given a location list, remove all addresses it refers to from the
4368    address_table.  */
4369
4370 static void
4371 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4372 {
4373   for (; descr; descr = descr->dw_loc_next)
4374     if (descr->dw_loc_oprnd1.val_entry != NULL)
4375       {
4376         gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4377         remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4378       }
4379 }
4380
4381 /* A helper function for dwarf2out_finish called through
4382    htab_traverse.  Assign an addr_table_entry its index.  All entries
4383    must be collected into the table when this function is called,
4384    because the indexing code relies on htab_traverse to traverse nodes
4385    in the same order for each run. */
4386
4387 int
4388 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4389 {
4390   addr_table_entry *node = *h;
4391
4392   /* Don't index unreferenced nodes.  */
4393   if (node->refcount == 0)
4394     return 1;
4395
4396   gcc_assert (node->index == NO_INDEX_ASSIGNED);
4397   node->index = *index;
4398   *index += 1;
4399
4400   return 1;
4401 }
4402
4403 /* Add an address constant attribute value to a DIE.  When using
4404    dwarf_split_debug_info, address attributes in dies destined for the
4405    final executable should be direct references--setting the parameter
4406    force_direct ensures this behavior.  */
4407
4408 static inline void
4409 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4410              bool force_direct)
4411 {
4412   dw_attr_node attr;
4413
4414   attr.dw_attr = attr_kind;
4415   attr.dw_attr_val.val_class = dw_val_class_addr;
4416   attr.dw_attr_val.v.val_addr = addr;
4417   if (dwarf_split_debug_info && !force_direct)
4418     attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4419   else
4420     attr.dw_attr_val.val_entry = NULL;
4421   add_dwarf_attr (die, &attr);
4422 }
4423
4424 /* Get the RTX from to an address DIE attribute.  */
4425
4426 static inline rtx
4427 AT_addr (dw_attr_ref a)
4428 {
4429   gcc_assert (a && AT_class (a) == dw_val_class_addr);
4430   return a->dw_attr_val.v.val_addr;
4431 }
4432
4433 /* Add a file attribute value to a DIE.  */
4434
4435 static inline void
4436 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4437              struct dwarf_file_data *fd)
4438 {
4439   dw_attr_node attr;
4440
4441   attr.dw_attr = attr_kind;
4442   attr.dw_attr_val.val_class = dw_val_class_file;
4443   attr.dw_attr_val.val_entry = NULL;
4444   attr.dw_attr_val.v.val_file = fd;
4445   add_dwarf_attr (die, &attr);
4446 }
4447
4448 /* Get the dwarf_file_data from a file DIE attribute.  */
4449
4450 static inline struct dwarf_file_data *
4451 AT_file (dw_attr_ref a)
4452 {
4453   gcc_assert (a && AT_class (a) == dw_val_class_file);
4454   return a->dw_attr_val.v.val_file;
4455 }
4456
4457 /* Add a vms delta attribute value to a DIE.  */
4458
4459 static inline void
4460 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4461                   const char *lbl1, const char *lbl2)
4462 {
4463   dw_attr_node attr;
4464
4465   attr.dw_attr = attr_kind;
4466   attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4467   attr.dw_attr_val.val_entry = NULL;
4468   attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4469   attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4470   add_dwarf_attr (die, &attr);
4471 }
4472
4473 /* Add a label identifier attribute value to a DIE.  */
4474
4475 static inline void
4476 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4477                const char *lbl_id)
4478 {
4479   dw_attr_node attr;
4480
4481   attr.dw_attr = attr_kind;
4482   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4483   attr.dw_attr_val.val_entry = NULL;
4484   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4485   if (dwarf_split_debug_info)
4486     attr.dw_attr_val.val_entry
4487         = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4488                                 ate_kind_label);
4489   add_dwarf_attr (die, &attr);
4490 }
4491
4492 /* Add a section offset attribute value to a DIE, an offset into the
4493    debug_line section.  */
4494
4495 static inline void
4496 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4497                 const char *label)
4498 {
4499   dw_attr_node attr;
4500
4501   attr.dw_attr = attr_kind;
4502   attr.dw_attr_val.val_class = dw_val_class_lineptr;
4503   attr.dw_attr_val.val_entry = NULL;
4504   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4505   add_dwarf_attr (die, &attr);
4506 }
4507
4508 /* Add a section offset attribute value to a DIE, an offset into the
4509    debug_macinfo section.  */
4510
4511 static inline void
4512 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4513                const char *label)
4514 {
4515   dw_attr_node attr;
4516
4517   attr.dw_attr = attr_kind;
4518   attr.dw_attr_val.val_class = dw_val_class_macptr;
4519   attr.dw_attr_val.val_entry = NULL;
4520   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4521   add_dwarf_attr (die, &attr);
4522 }
4523
4524 /* Add an offset attribute value to a DIE.  */
4525
4526 static inline void
4527 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4528                unsigned HOST_WIDE_INT offset)
4529 {
4530   dw_attr_node attr;
4531
4532   attr.dw_attr = attr_kind;
4533   attr.dw_attr_val.val_class = dw_val_class_offset;
4534   attr.dw_attr_val.val_entry = NULL;
4535   attr.dw_attr_val.v.val_offset = offset;
4536   add_dwarf_attr (die, &attr);
4537 }
4538
4539 /* Add a range_list attribute value to a DIE.  When using
4540    dwarf_split_debug_info, address attributes in dies destined for the
4541    final executable should be direct references--setting the parameter
4542    force_direct ensures this behavior.  */
4543
4544 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4545 #define RELOCATED_OFFSET (NULL)
4546
4547 static void
4548 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4549                    long unsigned int offset, bool force_direct)
4550 {
4551   dw_attr_node attr;
4552
4553   attr.dw_attr = attr_kind;
4554   attr.dw_attr_val.val_class = dw_val_class_range_list;
4555   /* For the range_list attribute, use val_entry to store whether the
4556      offset should follow split-debug-info or normal semantics.  This
4557      value is read in output_range_list_offset.  */
4558   if (dwarf_split_debug_info && !force_direct)
4559     attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4560   else
4561     attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4562   attr.dw_attr_val.v.val_offset = offset;
4563   add_dwarf_attr (die, &attr);
4564 }
4565
4566 /* Return the start label of a delta attribute.  */
4567
4568 static inline const char *
4569 AT_vms_delta1 (dw_attr_ref a)
4570 {
4571   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4572   return a->dw_attr_val.v.val_vms_delta.lbl1;
4573 }
4574
4575 /* Return the end label of a delta attribute.  */
4576
4577 static inline const char *
4578 AT_vms_delta2 (dw_attr_ref a)
4579 {
4580   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4581   return a->dw_attr_val.v.val_vms_delta.lbl2;
4582 }
4583
4584 static inline const char *
4585 AT_lbl (dw_attr_ref a)
4586 {
4587   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4588                     || AT_class (a) == dw_val_class_lineptr
4589                     || AT_class (a) == dw_val_class_macptr
4590                     || AT_class (a) == dw_val_class_high_pc));
4591   return a->dw_attr_val.v.val_lbl_id;
4592 }
4593
4594 /* Get the attribute of type attr_kind.  */
4595
4596 static dw_attr_ref
4597 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4598 {
4599   dw_attr_ref a;
4600   unsigned ix;
4601   dw_die_ref spec = NULL;
4602
4603   if (! die)
4604     return NULL;
4605
4606   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4607     if (a->dw_attr == attr_kind)
4608       return a;
4609     else if (a->dw_attr == DW_AT_specification
4610              || a->dw_attr == DW_AT_abstract_origin)
4611       spec = AT_ref (a);
4612
4613   if (spec)
4614     return get_AT (spec, attr_kind);
4615
4616   return NULL;
4617 }
4618
4619 /* Returns the parent of the declaration of DIE.  */
4620
4621 static dw_die_ref
4622 get_die_parent (dw_die_ref die)
4623 {
4624   dw_die_ref t;
4625
4626   if (!die)
4627     return NULL;
4628
4629   if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4630       || (t = get_AT_ref (die, DW_AT_specification)))
4631     die = t;
4632
4633   return die->die_parent;
4634 }
4635
4636 /* Return the "low pc" attribute value, typically associated with a subprogram
4637    DIE.  Return null if the "low pc" attribute is either not present, or if it
4638    cannot be represented as an assembler label identifier.  */
4639
4640 static inline const char *
4641 get_AT_low_pc (dw_die_ref die)
4642 {
4643   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4644
4645   return a ? AT_lbl (a) : NULL;
4646 }
4647
4648 /* Return the "high pc" attribute value, typically associated with a subprogram
4649    DIE.  Return null if the "high pc" attribute is either not present, or if it
4650    cannot be represented as an assembler label identifier.  */
4651
4652 static inline const char *
4653 get_AT_hi_pc (dw_die_ref die)
4654 {
4655   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4656
4657   return a ? AT_lbl (a) : NULL;
4658 }
4659
4660 /* Return the value of the string attribute designated by ATTR_KIND, or
4661    NULL if it is not present.  */
4662
4663 static inline const char *
4664 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4665 {
4666   dw_attr_ref a = get_AT (die, attr_kind);
4667
4668   return a ? AT_string (a) : NULL;
4669 }
4670
4671 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4672    if it is not present.  */
4673
4674 static inline int
4675 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4676 {
4677   dw_attr_ref a = get_AT (die, attr_kind);
4678
4679   return a ? AT_flag (a) : 0;
4680 }
4681
4682 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4683    if it is not present.  */
4684
4685 static inline unsigned
4686 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4687 {
4688   dw_attr_ref a = get_AT (die, attr_kind);
4689
4690   return a ? AT_unsigned (a) : 0;
4691 }
4692
4693 static inline dw_die_ref
4694 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4695 {
4696   dw_attr_ref a = get_AT (die, attr_kind);
4697
4698   return a ? AT_ref (a) : NULL;
4699 }
4700
4701 static inline struct dwarf_file_data *
4702 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4703 {
4704   dw_attr_ref a = get_AT (die, attr_kind);
4705
4706   return a ? AT_file (a) : NULL;
4707 }
4708
4709 /* Return TRUE if the language is C++.  */
4710
4711 static inline bool
4712 is_cxx (void)
4713 {
4714   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4715
4716   return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
4717           || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
4718 }
4719
4720 /* Return TRUE if the language is Java.  */
4721
4722 static inline bool
4723 is_java (void)
4724 {
4725   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4726
4727   return lang == DW_LANG_Java;
4728 }
4729
4730 /* Return TRUE if the language is Fortran.  */
4731
4732 static inline bool
4733 is_fortran (void)
4734 {
4735   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4736
4737   return (lang == DW_LANG_Fortran77
4738           || lang == DW_LANG_Fortran90
4739           || lang == DW_LANG_Fortran95);
4740 }
4741
4742 /* Return TRUE if the language is Ada.  */
4743
4744 static inline bool
4745 is_ada (void)
4746 {
4747   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4748
4749   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4750 }
4751
4752 /* Remove the specified attribute if present.  */
4753
4754 static void
4755 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4756 {
4757   dw_attr_ref a;
4758   unsigned ix;
4759
4760   if (! die)
4761     return;
4762
4763   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4764     if (a->dw_attr == attr_kind)
4765       {
4766         if (AT_class (a) == dw_val_class_str)
4767           if (a->dw_attr_val.v.val_str->refcount)
4768             a->dw_attr_val.v.val_str->refcount--;
4769
4770         /* vec::ordered_remove should help reduce the number of abbrevs
4771            that are needed.  */
4772         die->die_attr->ordered_remove (ix);
4773         return;
4774       }
4775 }
4776
4777 /* Remove CHILD from its parent.  PREV must have the property that
4778    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
4779
4780 static void
4781 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4782 {
4783   gcc_assert (child->die_parent == prev->die_parent);
4784   gcc_assert (prev->die_sib == child);
4785   if (prev == child)
4786     {
4787       gcc_assert (child->die_parent->die_child == child);
4788       prev = NULL;
4789     }
4790   else
4791     prev->die_sib = child->die_sib;
4792   if (child->die_parent->die_child == child)
4793     child->die_parent->die_child = prev;
4794 }
4795
4796 /* Replace OLD_CHILD with NEW_CHILD.  PREV must have the property that
4797    PREV->DIE_SIB == OLD_CHILD.  Does not alter OLD_CHILD.  */
4798
4799 static void
4800 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4801 {
4802   dw_die_ref parent = old_child->die_parent;
4803
4804   gcc_assert (parent == prev->die_parent);
4805   gcc_assert (prev->die_sib == old_child);
4806
4807   new_child->die_parent = parent;
4808   if (prev == old_child)
4809     {
4810       gcc_assert (parent->die_child == old_child);
4811       new_child->die_sib = new_child;
4812     }
4813   else
4814     {
4815       prev->die_sib = new_child;
4816       new_child->die_sib = old_child->die_sib;
4817     }
4818   if (old_child->die_parent->die_child == old_child)
4819     old_child->die_parent->die_child = new_child;
4820 }
4821
4822 /* Move all children from OLD_PARENT to NEW_PARENT.  */
4823
4824 static void
4825 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4826 {
4827   dw_die_ref c;
4828   new_parent->die_child = old_parent->die_child;
4829   old_parent->die_child = NULL;
4830   FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4831 }
4832
4833 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
4834    matches TAG.  */
4835
4836 static void
4837 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4838 {
4839   dw_die_ref c;
4840
4841   c = die->die_child;
4842   if (c) do {
4843     dw_die_ref prev = c;
4844     c = c->die_sib;
4845     while (c->die_tag == tag)
4846       {
4847         remove_child_with_prev (c, prev);
4848         /* Might have removed every child.  */
4849         if (c == c->die_sib)
4850           return;
4851         c = c->die_sib;
4852       }
4853   } while (c != die->die_child);
4854 }
4855
4856 /* Add a CHILD_DIE as the last child of DIE.  */
4857
4858 static void
4859 add_child_die (dw_die_ref die, dw_die_ref child_die)
4860 {
4861   /* FIXME this should probably be an assert.  */
4862   if (! die || ! child_die)
4863     return;
4864   gcc_assert (die != child_die);
4865
4866   child_die->die_parent = die;
4867   if (die->die_child)
4868     {
4869       child_die->die_sib = die->die_child->die_sib;
4870       die->die_child->die_sib = child_die;
4871     }
4872   else
4873     child_die->die_sib = child_die;
4874   die->die_child = child_die;
4875 }
4876
4877 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4878    is the specification, to the end of PARENT's list of children.
4879    This is done by removing and re-adding it.  */
4880
4881 static void
4882 splice_child_die (dw_die_ref parent, dw_die_ref child)
4883 {
4884   dw_die_ref p;
4885
4886   /* We want the declaration DIE from inside the class, not the
4887      specification DIE at toplevel.  */
4888   if (child->die_parent != parent)
4889     {
4890       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4891
4892       if (tmp)
4893         child = tmp;
4894     }
4895
4896   gcc_assert (child->die_parent == parent
4897               || (child->die_parent
4898                   == get_AT_ref (parent, DW_AT_specification)));
4899
4900   for (p = child->die_parent->die_child; ; p = p->die_sib)
4901     if (p->die_sib == child)
4902       {
4903         remove_child_with_prev (child, p);
4904         break;
4905       }
4906
4907   add_child_die (parent, child);
4908 }
4909
4910 /* Return a pointer to a newly created DIE node.  */
4911
4912 static inline dw_die_ref
4913 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4914 {
4915   dw_die_ref die = ggc_cleared_alloc<die_node> ();
4916
4917   die->die_tag = tag_value;
4918
4919   if (parent_die != NULL)
4920     add_child_die (parent_die, die);
4921   else
4922     {
4923       limbo_die_node *limbo_node;
4924
4925       limbo_node = ggc_cleared_alloc<limbo_die_node> ();
4926       limbo_node->die = die;
4927       limbo_node->created_for = t;
4928       limbo_node->next = limbo_die_list;
4929       limbo_die_list = limbo_node;
4930     }
4931
4932   return die;
4933 }
4934
4935 /* Return the DIE associated with the given type specifier.  */
4936
4937 static inline dw_die_ref
4938 lookup_type_die (tree type)
4939 {
4940   return TYPE_SYMTAB_DIE (type);
4941 }
4942
4943 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4944    anonymous type named by the typedef TYPE_DIE, return the DIE of the
4945    anonymous type instead the one of the naming typedef.  */
4946
4947 static inline dw_die_ref
4948 strip_naming_typedef (tree type, dw_die_ref type_die)
4949 {
4950   if (type
4951       && TREE_CODE (type) == RECORD_TYPE
4952       && type_die
4953       && type_die->die_tag == DW_TAG_typedef
4954       && is_naming_typedef_decl (TYPE_NAME (type)))
4955     type_die = get_AT_ref (type_die, DW_AT_type);
4956   return type_die;
4957 }
4958
4959 /* Like lookup_type_die, but if type is an anonymous type named by a
4960    typedef[1], return the DIE of the anonymous type instead the one of
4961    the naming typedef.  This is because in gen_typedef_die, we did
4962    equate the anonymous struct named by the typedef with the DIE of
4963    the naming typedef. So by default, lookup_type_die on an anonymous
4964    struct yields the DIE of the naming typedef.
4965
4966    [1]: Read the comment of is_naming_typedef_decl to learn about what
4967    a naming typedef is.  */
4968
4969 static inline dw_die_ref
4970 lookup_type_die_strip_naming_typedef (tree type)
4971 {
4972   dw_die_ref die = lookup_type_die (type);
4973   return strip_naming_typedef (type, die);
4974 }
4975
4976 /* Equate a DIE to a given type specifier.  */
4977
4978 static inline void
4979 equate_type_number_to_die (tree type, dw_die_ref type_die)
4980 {
4981   TYPE_SYMTAB_DIE (type) = type_die;
4982 }
4983
4984 /* Returns a hash value for X (which really is a die_struct).  */
4985
4986 inline hashval_t
4987 decl_die_hasher::hash (die_node *x)
4988 {
4989   return (hashval_t) x->decl_id;
4990 }
4991
4992 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
4993
4994 inline bool
4995 decl_die_hasher::equal (die_node *x, tree y)
4996 {
4997   return (x->decl_id == DECL_UID (y));
4998 }
4999
5000 /* Return the DIE associated with a given declaration.  */
5001
5002 static inline dw_die_ref
5003 lookup_decl_die (tree decl)
5004 {
5005   return decl_die_table->find_with_hash (decl, DECL_UID (decl));
5006 }
5007
5008 /* Returns a hash value for X (which really is a var_loc_list).  */
5009
5010 inline hashval_t
5011 decl_loc_hasher::hash (var_loc_list *x)
5012 {
5013   return (hashval_t) x->decl_id;
5014 }
5015
5016 /* Return nonzero if decl_id of var_loc_list X is the same as
5017    UID of decl *Y.  */
5018
5019 inline bool
5020 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5021 {
5022   return (x->decl_id == DECL_UID (y));
5023 }
5024
5025 /* Return the var_loc list associated with a given declaration.  */
5026
5027 static inline var_loc_list *
5028 lookup_decl_loc (const_tree decl)
5029 {
5030   if (!decl_loc_table)
5031     return NULL;
5032   return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5033 }
5034
5035 /* Returns a hash value for X (which really is a cached_dw_loc_list_list).  */
5036
5037 inline hashval_t
5038 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5039 {
5040   return (hashval_t) x->decl_id;
5041 }
5042
5043 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5044    UID of decl *Y.  */
5045
5046 inline bool
5047 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5048 {
5049   return (x->decl_id == DECL_UID (y));
5050 }
5051
5052 /* Equate a DIE to a particular declaration.  */
5053
5054 static void
5055 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5056 {
5057   unsigned int decl_id = DECL_UID (decl);
5058
5059   *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5060   decl_die->decl_id = decl_id;
5061 }
5062
5063 /* Return how many bits covers PIECE EXPR_LIST.  */
5064
5065 static HOST_WIDE_INT
5066 decl_piece_bitsize (rtx piece)
5067 {
5068   int ret = (int) GET_MODE (piece);
5069   if (ret)
5070     return ret;
5071   gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5072               && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5073   return INTVAL (XEXP (XEXP (piece, 0), 0));
5074 }
5075
5076 /* Return pointer to the location of location note in PIECE EXPR_LIST.  */
5077
5078 static rtx *
5079 decl_piece_varloc_ptr (rtx piece)
5080 {
5081   if ((int) GET_MODE (piece))
5082     return &XEXP (piece, 0);
5083   else
5084     return &XEXP (XEXP (piece, 0), 1);
5085 }
5086
5087 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5088    Next is the chain of following piece nodes.  */
5089
5090 static rtx_expr_list *
5091 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5092 {
5093   if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
5094     return alloc_EXPR_LIST (bitsize, loc_note, next);
5095   else
5096     return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5097                                                GEN_INT (bitsize),
5098                                                loc_note), next);
5099 }
5100
5101 /* Return rtx that should be stored into loc field for
5102    LOC_NOTE and BITPOS/BITSIZE.  */
5103
5104 static rtx
5105 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5106                       HOST_WIDE_INT bitsize)
5107 {
5108   if (bitsize != -1)
5109     {
5110       loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5111       if (bitpos != 0)
5112         loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5113     }
5114   return loc_note;
5115 }
5116
5117 /* This function either modifies location piece list *DEST in
5118    place (if SRC and INNER is NULL), or copies location piece list
5119    *SRC to *DEST while modifying it.  Location BITPOS is modified
5120    to contain LOC_NOTE, any pieces overlapping it are removed resp.
5121    not copied and if needed some padding around it is added.
5122    When modifying in place, DEST should point to EXPR_LIST where
5123    earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5124    to the start of the whole list and INNER points to the EXPR_LIST
5125    where earlier pieces cover PIECE_BITPOS bits.  */
5126
5127 static void
5128 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5129                    HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5130                    HOST_WIDE_INT bitsize, rtx loc_note)
5131 {
5132   HOST_WIDE_INT diff;
5133   bool copy = inner != NULL;
5134
5135   if (copy)
5136     {
5137       /* First copy all nodes preceding the current bitpos.  */
5138       while (src != inner)
5139         {
5140           *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5141                                    decl_piece_bitsize (*src), NULL_RTX);
5142           dest = &XEXP (*dest, 1);
5143           src = &XEXP (*src, 1);
5144         }
5145     }
5146   /* Add padding if needed.  */
5147   if (bitpos != piece_bitpos)
5148     {
5149       *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5150                                copy ? NULL_RTX : *dest);
5151       dest = &XEXP (*dest, 1);
5152     }
5153   else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5154     {
5155       gcc_assert (!copy);
5156       /* A piece with correct bitpos and bitsize already exist,
5157          just update the location for it and return.  */
5158       *decl_piece_varloc_ptr (*dest) = loc_note;
5159       return;
5160     }
5161   /* Add the piece that changed.  */
5162   *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5163   dest = &XEXP (*dest, 1);
5164   /* Skip over pieces that overlap it.  */
5165   diff = bitpos - piece_bitpos + bitsize;
5166   if (!copy)
5167     src = dest;
5168   while (diff > 0 && *src)
5169     {
5170       rtx piece = *src;
5171       diff -= decl_piece_bitsize (piece);
5172       if (copy)
5173         src = &XEXP (piece, 1);
5174       else
5175         {
5176           *src = XEXP (piece, 1);
5177           free_EXPR_LIST_node (piece);
5178         }
5179     }
5180   /* Add padding if needed.  */
5181   if (diff < 0 && *src)
5182     {
5183       if (!copy)
5184         dest = src;
5185       *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5186       dest = &XEXP (*dest, 1);
5187     }
5188   if (!copy)
5189     return;
5190   /* Finally copy all nodes following it.  */
5191   while (*src)
5192     {
5193       *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5194                                decl_piece_bitsize (*src), NULL_RTX);
5195       dest = &XEXP (*dest, 1);
5196       src = &XEXP (*src, 1);
5197     }
5198 }
5199
5200 /* Add a variable location node to the linked list for DECL.  */
5201
5202 static struct var_loc_node *
5203 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5204 {
5205   unsigned int decl_id;
5206   var_loc_list *temp;
5207   struct var_loc_node *loc = NULL;
5208   HOST_WIDE_INT bitsize = -1, bitpos = -1;
5209
5210   if (TREE_CODE (decl) == VAR_DECL
5211       && DECL_HAS_DEBUG_EXPR_P (decl))
5212     {
5213       tree realdecl = DECL_DEBUG_EXPR (decl);
5214       if (handled_component_p (realdecl)
5215           || (TREE_CODE (realdecl) == MEM_REF
5216               && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5217         {
5218           HOST_WIDE_INT maxsize;
5219           tree innerdecl;
5220           innerdecl
5221             = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5222           if (!DECL_P (innerdecl)
5223               || DECL_IGNORED_P (innerdecl)
5224               || TREE_STATIC (innerdecl)
5225               || bitsize <= 0
5226               || bitpos + bitsize > 256
5227               || bitsize != maxsize)
5228             return NULL;
5229           decl = innerdecl;
5230         }
5231     }
5232
5233   decl_id = DECL_UID (decl);
5234   var_loc_list **slot
5235     = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5236   if (*slot == NULL)
5237     {
5238       temp = ggc_cleared_alloc<var_loc_list> ();
5239       temp->decl_id = decl_id;
5240       *slot = temp;
5241     }
5242   else
5243     temp = *slot;
5244
5245   /* For PARM_DECLs try to keep around the original incoming value,
5246      even if that means we'll emit a zero-range .debug_loc entry.  */
5247   if (temp->last
5248       && temp->first == temp->last
5249       && TREE_CODE (decl) == PARM_DECL
5250       && NOTE_P (temp->first->loc)
5251       && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5252       && DECL_INCOMING_RTL (decl)
5253       && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5254       && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5255          == GET_CODE (DECL_INCOMING_RTL (decl))
5256       && prev_real_insn (temp->first->loc) == NULL_RTX
5257       && (bitsize != -1
5258           || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5259                            NOTE_VAR_LOCATION_LOC (loc_note))
5260           || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5261               != NOTE_VAR_LOCATION_STATUS (loc_note))))
5262     {
5263       loc = ggc_cleared_alloc<var_loc_node> ();
5264       temp->first->next = loc;
5265       temp->last = loc;
5266       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5267     }
5268   else if (temp->last)
5269     {
5270       struct var_loc_node *last = temp->last, *unused = NULL;
5271       rtx *piece_loc = NULL, last_loc_note;
5272       HOST_WIDE_INT piece_bitpos = 0;
5273       if (last->next)
5274         {
5275           last = last->next;
5276           gcc_assert (last->next == NULL);
5277         }
5278       if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5279         {
5280           piece_loc = &last->loc;
5281           do
5282             {
5283               HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5284               if (piece_bitpos + cur_bitsize > bitpos)
5285                 break;
5286               piece_bitpos += cur_bitsize;
5287               piece_loc = &XEXP (*piece_loc, 1);
5288             }
5289           while (*piece_loc);
5290         }
5291       /* TEMP->LAST here is either pointer to the last but one or
5292          last element in the chained list, LAST is pointer to the
5293          last element.  */
5294       if (label && strcmp (last->label, label) == 0)
5295         {
5296           /* For SRA optimized variables if there weren't any real
5297              insns since last note, just modify the last node.  */
5298           if (piece_loc != NULL)
5299             {
5300               adjust_piece_list (piece_loc, NULL, NULL,
5301                                  bitpos, piece_bitpos, bitsize, loc_note);
5302               return NULL;
5303             }
5304           /* If the last note doesn't cover any instructions, remove it.  */
5305           if (temp->last != last)
5306             {
5307               temp->last->next = NULL;
5308               unused = last;
5309               last = temp->last;
5310               gcc_assert (strcmp (last->label, label) != 0);
5311             }
5312           else
5313             {
5314               gcc_assert (temp->first == temp->last
5315                           || (temp->first->next == temp->last
5316                               && TREE_CODE (decl) == PARM_DECL));
5317               memset (temp->last, '\0', sizeof (*temp->last));
5318               temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5319               return temp->last;
5320             }
5321         }
5322       if (bitsize == -1 && NOTE_P (last->loc))
5323         last_loc_note = last->loc;
5324       else if (piece_loc != NULL
5325                && *piece_loc != NULL_RTX
5326                && piece_bitpos == bitpos
5327                && decl_piece_bitsize (*piece_loc) == bitsize)
5328         last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5329       else
5330         last_loc_note = NULL_RTX;
5331       /* If the current location is the same as the end of the list,
5332          and either both or neither of the locations is uninitialized,
5333          we have nothing to do.  */
5334       if (last_loc_note == NULL_RTX
5335           || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5336                             NOTE_VAR_LOCATION_LOC (loc_note)))
5337           || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5338                != NOTE_VAR_LOCATION_STATUS (loc_note))
5339               && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5340                    == VAR_INIT_STATUS_UNINITIALIZED)
5341                   || (NOTE_VAR_LOCATION_STATUS (loc_note)
5342                       == VAR_INIT_STATUS_UNINITIALIZED))))
5343         {
5344           /* Add LOC to the end of list and update LAST.  If the last
5345              element of the list has been removed above, reuse its
5346              memory for the new node, otherwise allocate a new one.  */
5347           if (unused)
5348             {
5349               loc = unused;
5350               memset (loc, '\0', sizeof (*loc));
5351             }
5352           else
5353             loc = ggc_cleared_alloc<var_loc_node> ();
5354           if (bitsize == -1 || piece_loc == NULL)
5355             loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5356           else
5357             adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5358                                bitpos, piece_bitpos, bitsize, loc_note);
5359           last->next = loc;
5360           /* Ensure TEMP->LAST will point either to the new last but one
5361              element of the chain, or to the last element in it.  */
5362           if (last != temp->last)
5363             temp->last = last;
5364         }
5365       else if (unused)
5366         ggc_free (unused);
5367     }
5368   else
5369     {
5370       loc = ggc_cleared_alloc<var_loc_node> ();
5371       temp->first = loc;
5372       temp->last = loc;
5373       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5374     }
5375   return loc;
5376 }
5377 \f
5378 /* Keep track of the number of spaces used to indent the
5379    output of the debugging routines that print the structure of
5380    the DIE internal representation.  */
5381 static int print_indent;
5382
5383 /* Indent the line the number of spaces given by print_indent.  */
5384
5385 static inline void
5386 print_spaces (FILE *outfile)
5387 {
5388   fprintf (outfile, "%*s", print_indent, "");
5389 }
5390
5391 /* Print a type signature in hex.  */
5392
5393 static inline void
5394 print_signature (FILE *outfile, char *sig)
5395 {
5396   int i;
5397
5398   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5399     fprintf (outfile, "%02x", sig[i] & 0xff);
5400 }
5401
5402 static void print_loc_descr (dw_loc_descr_ref, FILE *);
5403
5404 /* Print the value associated to the VAL DWARF value node to OUTFILE.  If
5405    RECURSE, output location descriptor operations.  */
5406
5407 static void
5408 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
5409 {
5410   switch (val->val_class)
5411     {
5412     case dw_val_class_addr:
5413       fprintf (outfile, "address");
5414       break;
5415     case dw_val_class_offset:
5416       fprintf (outfile, "offset");
5417       break;
5418     case dw_val_class_loc:
5419       fprintf (outfile, "location descriptor");
5420       if (val->v.val_loc == NULL)
5421         fprintf (outfile, " -> <null>\n");
5422       else if (recurse)
5423         {
5424           fprintf (outfile, ":\n");
5425           print_indent += 4;
5426           print_loc_descr (val->v.val_loc, outfile);
5427           print_indent -= 4;
5428         }
5429       else
5430         fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
5431       break;
5432     case dw_val_class_loc_list:
5433       fprintf (outfile, "location list -> label:%s",
5434                val->v.val_loc_list->ll_symbol);
5435       break;
5436     case dw_val_class_range_list:
5437       fprintf (outfile, "range list");
5438       break;
5439     case dw_val_class_const:
5440       fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
5441       break;
5442     case dw_val_class_unsigned_const:
5443       fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
5444       break;
5445     case dw_val_class_const_double:
5446       fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
5447                         HOST_WIDE_INT_PRINT_UNSIGNED")",
5448                val->v.val_double.high,
5449                val->v.val_double.low);
5450       break;
5451     case dw_val_class_wide_int:
5452       {
5453         int i = val->v.val_wide->get_len ();
5454         fprintf (outfile, "constant (");
5455         gcc_assert (i > 0);
5456         if (val->v.val_wide->elt (i - 1) == 0)
5457           fprintf (outfile, "0x");
5458         fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5459                  val->v.val_wide->elt (--i));
5460         while (--i >= 0)
5461           fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5462                    val->v.val_wide->elt (i));
5463         fprintf (outfile, ")");
5464         break;
5465       }
5466     case dw_val_class_vec:
5467       fprintf (outfile, "floating-point or vector constant");
5468       break;
5469     case dw_val_class_flag:
5470       fprintf (outfile, "%u", val->v.val_flag);
5471       break;
5472     case dw_val_class_die_ref:
5473       if (val->v.val_die_ref.die != NULL)
5474         {
5475           dw_die_ref die = val->v.val_die_ref.die;
5476
5477           if (die->comdat_type_p)
5478             {
5479               fprintf (outfile, "die -> signature: ");
5480               print_signature (outfile,
5481                                die->die_id.die_type_node->signature);
5482             }
5483           else if (die->die_id.die_symbol)
5484             fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
5485           else
5486             fprintf (outfile, "die -> %ld", die->die_offset);
5487           fprintf (outfile, " (%p)", (void *) die);
5488         }
5489       else
5490         fprintf (outfile, "die -> <null>");
5491       break;
5492     case dw_val_class_vms_delta:
5493       fprintf (outfile, "delta: @slotcount(%s-%s)",
5494                val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
5495       break;
5496     case dw_val_class_lbl_id:
5497     case dw_val_class_lineptr:
5498     case dw_val_class_macptr:
5499     case dw_val_class_high_pc:
5500       fprintf (outfile, "label: %s", val->v.val_lbl_id);
5501       break;
5502     case dw_val_class_str:
5503       if (val->v.val_str->str != NULL)
5504         fprintf (outfile, "\"%s\"", val->v.val_str->str);
5505       else
5506         fprintf (outfile, "<null>");
5507       break;
5508     case dw_val_class_file:
5509       fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
5510                val->v.val_file->emitted_number);
5511       break;
5512     case dw_val_class_data8:
5513       {
5514         int i;
5515
5516         for (i = 0; i < 8; i++)
5517           fprintf (outfile, "%02x", val->v.val_data8[i]);
5518         break;
5519       }
5520     default:
5521       break;
5522     }
5523 }
5524
5525 /* Likewise, for a DIE attribute.  */
5526
5527 static void
5528 print_attribute (dw_attr_ref a, bool recurse, FILE *outfile)
5529 {
5530   print_dw_val (&a->dw_attr_val, recurse, outfile);
5531 }
5532
5533
5534 /* Print the list of operands in the LOC location description to OUTFILE.  This
5535    routine is a debugging aid only.  */
5536
5537 static void
5538 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
5539 {
5540   dw_loc_descr_ref l = loc;
5541
5542   if (loc == NULL)
5543     {
5544       print_spaces (outfile);
5545       fprintf (outfile, "<null>\n");
5546       return;
5547     }
5548
5549   for (l = loc; l != NULL; l = l->dw_loc_next)
5550     {
5551       print_spaces (outfile);
5552       fprintf (outfile, "(%p) %s",
5553                (void *) l,
5554                dwarf_stack_op_name (l->dw_loc_opc));
5555       if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
5556         {
5557           fprintf (outfile, " ");
5558           print_dw_val (&l->dw_loc_oprnd1, false, outfile);
5559         }
5560       if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
5561         {
5562           fprintf (outfile, ", ");
5563           print_dw_val (&l->dw_loc_oprnd2, false, outfile);
5564         }
5565       fprintf (outfile, "\n");
5566     }
5567 }
5568
5569 /* Print the information associated with a given DIE, and its children.
5570    This routine is a debugging aid only.  */
5571
5572 static void
5573 print_die (dw_die_ref die, FILE *outfile)
5574 {
5575   dw_attr_ref a;
5576   dw_die_ref c;
5577   unsigned ix;
5578
5579   print_spaces (outfile);
5580   fprintf (outfile, "DIE %4ld: %s (%p)\n",
5581            die->die_offset, dwarf_tag_name (die->die_tag),
5582            (void*) die);
5583   print_spaces (outfile);
5584   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
5585   fprintf (outfile, " offset: %ld", die->die_offset);
5586   fprintf (outfile, " mark: %d\n", die->die_mark);
5587
5588   if (die->comdat_type_p)
5589     {
5590       print_spaces (outfile);
5591       fprintf (outfile, "  signature: ");
5592       print_signature (outfile, die->die_id.die_type_node->signature);
5593       fprintf (outfile, "\n");
5594     }
5595
5596   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5597     {
5598       print_spaces (outfile);
5599       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
5600
5601       print_attribute (a, true, outfile);
5602       fprintf (outfile, "\n");
5603     }
5604
5605   if (die->die_child != NULL)
5606     {
5607       print_indent += 4;
5608       FOR_EACH_CHILD (die, c, print_die (c, outfile));
5609       print_indent -= 4;
5610     }
5611   if (print_indent == 0)
5612     fprintf (outfile, "\n");
5613 }
5614
5615 /* Print the list of operations in the LOC location description.  */
5616
5617 DEBUG_FUNCTION void
5618 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
5619 {
5620   print_loc_descr (loc, stderr);
5621 }
5622
5623 /* Print the information collected for a given DIE.  */
5624
5625 DEBUG_FUNCTION void
5626 debug_dwarf_die (dw_die_ref die)
5627 {
5628   print_die (die, stderr);
5629 }
5630
5631 DEBUG_FUNCTION void
5632 debug (die_struct &ref)
5633 {
5634   print_die (&ref, stderr);
5635 }
5636
5637 DEBUG_FUNCTION void
5638 debug (die_struct *ptr)
5639 {
5640   if (ptr)
5641     debug (*ptr);
5642   else
5643     fprintf (stderr, "<nil>\n");
5644 }
5645
5646
5647 /* Print all DWARF information collected for the compilation unit.
5648    This routine is a debugging aid only.  */
5649
5650 DEBUG_FUNCTION void
5651 debug_dwarf (void)
5652 {
5653   print_indent = 0;
5654   print_die (comp_unit_die (), stderr);
5655 }
5656 \f
5657 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
5658    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
5659    DIE that marks the start of the DIEs for this include file.  */
5660
5661 static dw_die_ref
5662 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5663 {
5664   const char *filename = get_AT_string (bincl_die, DW_AT_name);
5665   dw_die_ref new_unit = gen_compile_unit_die (filename);
5666
5667   new_unit->die_sib = old_unit;
5668   return new_unit;
5669 }
5670
5671 /* Close an include-file CU and reopen the enclosing one.  */
5672
5673 static dw_die_ref
5674 pop_compile_unit (dw_die_ref old_unit)
5675 {
5676   dw_die_ref new_unit = old_unit->die_sib;
5677
5678   old_unit->die_sib = NULL;
5679   return new_unit;
5680 }
5681
5682 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5683 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5684 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5685
5686 /* Calculate the checksum of a location expression.  */
5687
5688 static inline void
5689 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5690 {
5691   int tem;
5692   inchash::hash hstate;
5693   hashval_t hash;
5694
5695   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5696   CHECKSUM (tem);
5697   hash_loc_operands (loc, hstate);
5698   hash = hstate.end();
5699   CHECKSUM (hash);
5700 }
5701
5702 /* Calculate the checksum of an attribute.  */
5703
5704 static void
5705 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5706 {
5707   dw_loc_descr_ref loc;
5708   rtx r;
5709
5710   CHECKSUM (at->dw_attr);
5711
5712   /* We don't care that this was compiled with a different compiler
5713      snapshot; if the output is the same, that's what matters.  */
5714   if (at->dw_attr == DW_AT_producer)
5715     return;
5716
5717   switch (AT_class (at))
5718     {
5719     case dw_val_class_const:
5720       CHECKSUM (at->dw_attr_val.v.val_int);
5721       break;
5722     case dw_val_class_unsigned_const:
5723       CHECKSUM (at->dw_attr_val.v.val_unsigned);
5724       break;
5725     case dw_val_class_const_double:
5726       CHECKSUM (at->dw_attr_val.v.val_double);
5727       break;
5728     case dw_val_class_wide_int:
5729       CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
5730                       get_full_len (*at->dw_attr_val.v.val_wide)
5731                       * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
5732       break;
5733     case dw_val_class_vec:
5734       CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5735                       (at->dw_attr_val.v.val_vec.length
5736                        * at->dw_attr_val.v.val_vec.elt_size));
5737       break;
5738     case dw_val_class_flag:
5739       CHECKSUM (at->dw_attr_val.v.val_flag);
5740       break;
5741     case dw_val_class_str:
5742       CHECKSUM_STRING (AT_string (at));
5743       break;
5744
5745     case dw_val_class_addr:
5746       r = AT_addr (at);
5747       gcc_assert (GET_CODE (r) == SYMBOL_REF);
5748       CHECKSUM_STRING (XSTR (r, 0));
5749       break;
5750
5751     case dw_val_class_offset:
5752       CHECKSUM (at->dw_attr_val.v.val_offset);
5753       break;
5754
5755     case dw_val_class_loc:
5756       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5757         loc_checksum (loc, ctx);
5758       break;
5759
5760     case dw_val_class_die_ref:
5761       die_checksum (AT_ref (at), ctx, mark);
5762       break;
5763
5764     case dw_val_class_fde_ref:
5765     case dw_val_class_vms_delta:
5766     case dw_val_class_lbl_id:
5767     case dw_val_class_lineptr:
5768     case dw_val_class_macptr:
5769     case dw_val_class_high_pc:
5770       break;
5771
5772     case dw_val_class_file:
5773       CHECKSUM_STRING (AT_file (at)->filename);
5774       break;
5775
5776     case dw_val_class_data8:
5777       CHECKSUM (at->dw_attr_val.v.val_data8);
5778       break;
5779
5780     default:
5781       break;
5782     }
5783 }
5784
5785 /* Calculate the checksum of a DIE.  */
5786
5787 static void
5788 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5789 {
5790   dw_die_ref c;
5791   dw_attr_ref a;
5792   unsigned ix;
5793
5794   /* To avoid infinite recursion.  */
5795   if (die->die_mark)
5796     {
5797       CHECKSUM (die->die_mark);
5798       return;
5799     }
5800   die->die_mark = ++(*mark);
5801
5802   CHECKSUM (die->die_tag);
5803
5804   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5805     attr_checksum (a, ctx, mark);
5806
5807   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5808 }
5809
5810 #undef CHECKSUM
5811 #undef CHECKSUM_BLOCK
5812 #undef CHECKSUM_STRING
5813
5814 /* For DWARF-4 types, include the trailing NULL when checksumming strings.  */
5815 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5816 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5817 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5818 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5819 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5820 #define CHECKSUM_ATTR(FOO) \
5821   if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5822
5823 /* Calculate the checksum of a number in signed LEB128 format.  */
5824
5825 static void
5826 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5827 {
5828   unsigned char byte;
5829   bool more;
5830
5831   while (1)
5832     {
5833       byte = (value & 0x7f);
5834       value >>= 7;
5835       more = !((value == 0 && (byte & 0x40) == 0)
5836                 || (value == -1 && (byte & 0x40) != 0));
5837       if (more)
5838         byte |= 0x80;
5839       CHECKSUM (byte);
5840       if (!more)
5841         break;
5842     }
5843 }
5844
5845 /* Calculate the checksum of a number in unsigned LEB128 format.  */
5846
5847 static void
5848 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5849 {
5850   while (1)
5851     {
5852       unsigned char byte = (value & 0x7f);
5853       value >>= 7;
5854       if (value != 0)
5855         /* More bytes to follow.  */
5856         byte |= 0x80;
5857       CHECKSUM (byte);
5858       if (value == 0)
5859         break;
5860     }
5861 }
5862
5863 /* Checksum the context of the DIE.  This adds the names of any
5864    surrounding namespaces or structures to the checksum.  */
5865
5866 static void
5867 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5868 {
5869   const char *name;
5870   dw_die_ref spec;
5871   int tag = die->die_tag;
5872
5873   if (tag != DW_TAG_namespace
5874       && tag != DW_TAG_structure_type
5875       && tag != DW_TAG_class_type)
5876     return;
5877
5878   name = get_AT_string (die, DW_AT_name);
5879
5880   spec = get_AT_ref (die, DW_AT_specification);
5881   if (spec != NULL)
5882     die = spec;
5883
5884   if (die->die_parent != NULL)
5885     checksum_die_context (die->die_parent, ctx);
5886
5887   CHECKSUM_ULEB128 ('C');
5888   CHECKSUM_ULEB128 (tag);
5889   if (name != NULL)
5890     CHECKSUM_STRING (name);
5891 }
5892
5893 /* Calculate the checksum of a location expression.  */
5894
5895 static inline void
5896 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5897 {
5898   /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5899      were emitted as a DW_FORM_sdata instead of a location expression.  */
5900   if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5901     {
5902       CHECKSUM_ULEB128 (DW_FORM_sdata);
5903       CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5904       return;
5905     }
5906
5907   /* Otherwise, just checksum the raw location expression.  */
5908   while (loc != NULL)
5909     {
5910       inchash::hash hstate;
5911       hashval_t hash;
5912
5913       CHECKSUM_ULEB128 (loc->dtprel);
5914       CHECKSUM_ULEB128 (loc->dw_loc_opc);
5915       hash_loc_operands (loc, hstate);
5916       hash = hstate.end ();
5917       CHECKSUM (hash);
5918       loc = loc->dw_loc_next;
5919     }
5920 }
5921
5922 /* Calculate the checksum of an attribute.  */
5923
5924 static void
5925 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5926                        struct md5_ctx *ctx, int *mark)
5927 {
5928   dw_loc_descr_ref loc;
5929   rtx r;
5930
5931   if (AT_class (at) == dw_val_class_die_ref)
5932     {
5933       dw_die_ref target_die = AT_ref (at);
5934
5935       /* For pointer and reference types, we checksum only the (qualified)
5936          name of the target type (if there is a name).  For friend entries,
5937          we checksum only the (qualified) name of the target type or function.
5938          This allows the checksum to remain the same whether the target type
5939          is complete or not.  */
5940       if ((at->dw_attr == DW_AT_type
5941            && (tag == DW_TAG_pointer_type
5942                || tag == DW_TAG_reference_type
5943                || tag == DW_TAG_rvalue_reference_type
5944                || tag == DW_TAG_ptr_to_member_type))
5945           || (at->dw_attr == DW_AT_friend
5946               && tag == DW_TAG_friend))
5947         {
5948           dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5949
5950           if (name_attr != NULL)
5951             {
5952               dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5953
5954               if (decl == NULL)
5955                 decl = target_die;
5956               CHECKSUM_ULEB128 ('N');
5957               CHECKSUM_ULEB128 (at->dw_attr);
5958               if (decl->die_parent != NULL)
5959                 checksum_die_context (decl->die_parent, ctx);
5960               CHECKSUM_ULEB128 ('E');
5961               CHECKSUM_STRING (AT_string (name_attr));
5962               return;
5963             }
5964         }
5965
5966       /* For all other references to another DIE, we check to see if the
5967          target DIE has already been visited.  If it has, we emit a
5968          backward reference; if not, we descend recursively.  */
5969       if (target_die->die_mark > 0)
5970         {
5971           CHECKSUM_ULEB128 ('R');
5972           CHECKSUM_ULEB128 (at->dw_attr);
5973           CHECKSUM_ULEB128 (target_die->die_mark);
5974         }
5975       else
5976         {
5977           dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5978
5979           if (decl == NULL)
5980             decl = target_die;
5981           target_die->die_mark = ++(*mark);
5982           CHECKSUM_ULEB128 ('T');
5983           CHECKSUM_ULEB128 (at->dw_attr);
5984           if (decl->die_parent != NULL)
5985             checksum_die_context (decl->die_parent, ctx);
5986           die_checksum_ordered (target_die, ctx, mark);
5987         }
5988       return;
5989     }
5990
5991   CHECKSUM_ULEB128 ('A');
5992   CHECKSUM_ULEB128 (at->dw_attr);
5993
5994   switch (AT_class (at))
5995     {
5996     case dw_val_class_const:
5997       CHECKSUM_ULEB128 (DW_FORM_sdata);
5998       CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5999       break;
6000
6001     case dw_val_class_unsigned_const:
6002       CHECKSUM_ULEB128 (DW_FORM_sdata);
6003       CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6004       break;
6005
6006     case dw_val_class_const_double:
6007       CHECKSUM_ULEB128 (DW_FORM_block);
6008       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6009       CHECKSUM (at->dw_attr_val.v.val_double);
6010       break;
6011
6012     case dw_val_class_wide_int:
6013       CHECKSUM_ULEB128 (DW_FORM_block);
6014       CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
6015                         * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
6016       CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6017                       get_full_len (*at->dw_attr_val.v.val_wide)
6018                       * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6019       break;
6020
6021     case dw_val_class_vec:
6022       CHECKSUM_ULEB128 (DW_FORM_block);
6023       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6024                         * at->dw_attr_val.v.val_vec.elt_size);
6025       CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6026                       (at->dw_attr_val.v.val_vec.length
6027                        * at->dw_attr_val.v.val_vec.elt_size));
6028       break;
6029
6030     case dw_val_class_flag:
6031       CHECKSUM_ULEB128 (DW_FORM_flag);
6032       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6033       break;
6034
6035     case dw_val_class_str:
6036       CHECKSUM_ULEB128 (DW_FORM_string);
6037       CHECKSUM_STRING (AT_string (at));
6038       break;
6039
6040     case dw_val_class_addr:
6041       r = AT_addr (at);
6042       gcc_assert (GET_CODE (r) == SYMBOL_REF);
6043       CHECKSUM_ULEB128 (DW_FORM_string);
6044       CHECKSUM_STRING (XSTR (r, 0));
6045       break;
6046
6047     case dw_val_class_offset:
6048       CHECKSUM_ULEB128 (DW_FORM_sdata);
6049       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6050       break;
6051
6052     case dw_val_class_loc:
6053       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6054         loc_checksum_ordered (loc, ctx);
6055       break;
6056
6057     case dw_val_class_fde_ref:
6058     case dw_val_class_lbl_id:
6059     case dw_val_class_lineptr:
6060     case dw_val_class_macptr:
6061     case dw_val_class_high_pc:
6062       break;
6063
6064     case dw_val_class_file:
6065       CHECKSUM_ULEB128 (DW_FORM_string);
6066       CHECKSUM_STRING (AT_file (at)->filename);
6067       break;
6068
6069     case dw_val_class_data8:
6070       CHECKSUM (at->dw_attr_val.v.val_data8);
6071       break;
6072
6073     default:
6074       break;
6075     }
6076 }
6077
6078 struct checksum_attributes
6079 {
6080   dw_attr_ref at_name;
6081   dw_attr_ref at_type;
6082   dw_attr_ref at_friend;
6083   dw_attr_ref at_accessibility;
6084   dw_attr_ref at_address_class;
6085   dw_attr_ref at_allocated;
6086   dw_attr_ref at_artificial;
6087   dw_attr_ref at_associated;
6088   dw_attr_ref at_binary_scale;
6089   dw_attr_ref at_bit_offset;
6090   dw_attr_ref at_bit_size;
6091   dw_attr_ref at_bit_stride;
6092   dw_attr_ref at_byte_size;
6093   dw_attr_ref at_byte_stride;
6094   dw_attr_ref at_const_value;
6095   dw_attr_ref at_containing_type;
6096   dw_attr_ref at_count;
6097   dw_attr_ref at_data_location;
6098   dw_attr_ref at_data_member_location;
6099   dw_attr_ref at_decimal_scale;
6100   dw_attr_ref at_decimal_sign;
6101   dw_attr_ref at_default_value;
6102   dw_attr_ref at_digit_count;
6103   dw_attr_ref at_discr;
6104   dw_attr_ref at_discr_list;
6105   dw_attr_ref at_discr_value;
6106   dw_attr_ref at_encoding;
6107   dw_attr_ref at_endianity;
6108   dw_attr_ref at_explicit;
6109   dw_attr_ref at_is_optional;
6110   dw_attr_ref at_location;
6111   dw_attr_ref at_lower_bound;
6112   dw_attr_ref at_mutable;
6113   dw_attr_ref at_ordering;
6114   dw_attr_ref at_picture_string;
6115   dw_attr_ref at_prototyped;
6116   dw_attr_ref at_small;
6117   dw_attr_ref at_segment;
6118   dw_attr_ref at_string_length;
6119   dw_attr_ref at_threads_scaled;
6120   dw_attr_ref at_upper_bound;
6121   dw_attr_ref at_use_location;
6122   dw_attr_ref at_use_UTF8;
6123   dw_attr_ref at_variable_parameter;
6124   dw_attr_ref at_virtuality;
6125   dw_attr_ref at_visibility;
6126   dw_attr_ref at_vtable_elem_location;
6127 };
6128
6129 /* Collect the attributes that we will want to use for the checksum.  */
6130
6131 static void
6132 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6133 {
6134   dw_attr_ref a;
6135   unsigned ix;
6136
6137   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6138     {
6139       switch (a->dw_attr)
6140         {
6141         case DW_AT_name:
6142           attrs->at_name = a;
6143           break;
6144         case DW_AT_type:
6145           attrs->at_type = a;
6146           break;
6147         case DW_AT_friend:
6148           attrs->at_friend = a;
6149           break;
6150         case DW_AT_accessibility:
6151           attrs->at_accessibility = a;
6152           break;
6153         case DW_AT_address_class:
6154           attrs->at_address_class = a;
6155           break;
6156         case DW_AT_allocated:
6157           attrs->at_allocated = a;
6158           break;
6159         case DW_AT_artificial:
6160           attrs->at_artificial = a;
6161           break;
6162         case DW_AT_associated:
6163           attrs->at_associated = a;
6164           break;
6165         case DW_AT_binary_scale:
6166           attrs->at_binary_scale = a;
6167           break;
6168         case DW_AT_bit_offset:
6169           attrs->at_bit_offset = a;
6170           break;
6171         case DW_AT_bit_size:
6172           attrs->at_bit_size = a;
6173           break;
6174         case DW_AT_bit_stride:
6175           attrs->at_bit_stride = a;
6176           break;
6177         case DW_AT_byte_size:
6178           attrs->at_byte_size = a;
6179           break;
6180         case DW_AT_byte_stride:
6181           attrs->at_byte_stride = a;
6182           break;
6183         case DW_AT_const_value:
6184           attrs->at_const_value = a;
6185           break;
6186         case DW_AT_containing_type:
6187           attrs->at_containing_type = a;
6188           break;
6189         case DW_AT_count:
6190           attrs->at_count = a;
6191           break;
6192         case DW_AT_data_location:
6193           attrs->at_data_location = a;
6194           break;
6195         case DW_AT_data_member_location:
6196           attrs->at_data_member_location = a;
6197           break;
6198         case DW_AT_decimal_scale:
6199           attrs->at_decimal_scale = a;
6200           break;
6201         case DW_AT_decimal_sign:
6202           attrs->at_decimal_sign = a;
6203           break;
6204         case DW_AT_default_value:
6205           attrs->at_default_value = a;
6206           break;
6207         case DW_AT_digit_count:
6208           attrs->at_digit_count = a;
6209           break;
6210         case DW_AT_discr:
6211           attrs->at_discr = a;
6212           break;
6213         case DW_AT_discr_list:
6214           attrs->at_discr_list = a;
6215           break;
6216         case DW_AT_discr_value:
6217           attrs->at_discr_value = a;
6218           break;
6219         case DW_AT_encoding:
6220           attrs->at_encoding = a;
6221           break;
6222         case DW_AT_endianity:
6223           attrs->at_endianity = a;
6224           break;
6225         case DW_AT_explicit:
6226           attrs->at_explicit = a;
6227           break;
6228         case DW_AT_is_optional:
6229           attrs->at_is_optional = a;
6230           break;
6231         case DW_AT_location:
6232           attrs->at_location = a;
6233           break;
6234         case DW_AT_lower_bound:
6235           attrs->at_lower_bound = a;
6236           break;
6237         case DW_AT_mutable:
6238           attrs->at_mutable = a;
6239           break;
6240         case DW_AT_ordering:
6241           attrs->at_ordering = a;
6242           break;
6243         case DW_AT_picture_string:
6244           attrs->at_picture_string = a;
6245           break;
6246         case DW_AT_prototyped:
6247           attrs->at_prototyped = a;
6248           break;
6249         case DW_AT_small:
6250           attrs->at_small = a;
6251           break;
6252         case DW_AT_segment:
6253           attrs->at_segment = a;
6254           break;
6255         case DW_AT_string_length:
6256           attrs->at_string_length = a;
6257           break;
6258         case DW_AT_threads_scaled:
6259           attrs->at_threads_scaled = a;
6260           break;
6261         case DW_AT_upper_bound:
6262           attrs->at_upper_bound = a;
6263           break;
6264         case DW_AT_use_location:
6265           attrs->at_use_location = a;
6266           break;
6267         case DW_AT_use_UTF8:
6268           attrs->at_use_UTF8 = a;
6269           break;
6270         case DW_AT_variable_parameter:
6271           attrs->at_variable_parameter = a;
6272           break;
6273         case DW_AT_virtuality:
6274           attrs->at_virtuality = a;
6275           break;
6276         case DW_AT_visibility:
6277           attrs->at_visibility = a;
6278           break;
6279         case DW_AT_vtable_elem_location:
6280           attrs->at_vtable_elem_location = a;
6281           break;
6282         default:
6283           break;
6284         }
6285     }
6286 }
6287
6288 /* Calculate the checksum of a DIE, using an ordered subset of attributes.  */
6289
6290 static void
6291 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6292 {
6293   dw_die_ref c;
6294   dw_die_ref decl;
6295   struct checksum_attributes attrs;
6296
6297   CHECKSUM_ULEB128 ('D');
6298   CHECKSUM_ULEB128 (die->die_tag);
6299
6300   memset (&attrs, 0, sizeof (attrs));
6301
6302   decl = get_AT_ref (die, DW_AT_specification);
6303   if (decl != NULL)
6304     collect_checksum_attributes (&attrs, decl);
6305   collect_checksum_attributes (&attrs, die);
6306
6307   CHECKSUM_ATTR (attrs.at_name);
6308   CHECKSUM_ATTR (attrs.at_accessibility);
6309   CHECKSUM_ATTR (attrs.at_address_class);
6310   CHECKSUM_ATTR (attrs.at_allocated);
6311   CHECKSUM_ATTR (attrs.at_artificial);
6312   CHECKSUM_ATTR (attrs.at_associated);
6313   CHECKSUM_ATTR (attrs.at_binary_scale);
6314   CHECKSUM_ATTR (attrs.at_bit_offset);
6315   CHECKSUM_ATTR (attrs.at_bit_size);
6316   CHECKSUM_ATTR (attrs.at_bit_stride);
6317   CHECKSUM_ATTR (attrs.at_byte_size);
6318   CHECKSUM_ATTR (attrs.at_byte_stride);
6319   CHECKSUM_ATTR (attrs.at_const_value);
6320   CHECKSUM_ATTR (attrs.at_containing_type);
6321   CHECKSUM_ATTR (attrs.at_count);
6322   CHECKSUM_ATTR (attrs.at_data_location);
6323   CHECKSUM_ATTR (attrs.at_data_member_location);
6324   CHECKSUM_ATTR (attrs.at_decimal_scale);
6325   CHECKSUM_ATTR (attrs.at_decimal_sign);
6326   CHECKSUM_ATTR (attrs.at_default_value);
6327   CHECKSUM_ATTR (attrs.at_digit_count);
6328   CHECKSUM_ATTR (attrs.at_discr);
6329   CHECKSUM_ATTR (attrs.at_discr_list);
6330   CHECKSUM_ATTR (attrs.at_discr_value);
6331   CHECKSUM_ATTR (attrs.at_encoding);
6332   CHECKSUM_ATTR (attrs.at_endianity);
6333   CHECKSUM_ATTR (attrs.at_explicit);
6334   CHECKSUM_ATTR (attrs.at_is_optional);
6335   CHECKSUM_ATTR (attrs.at_location);
6336   CHECKSUM_ATTR (attrs.at_lower_bound);
6337   CHECKSUM_ATTR (attrs.at_mutable);
6338   CHECKSUM_ATTR (attrs.at_ordering);
6339   CHECKSUM_ATTR (attrs.at_picture_string);
6340   CHECKSUM_ATTR (attrs.at_prototyped);
6341   CHECKSUM_ATTR (attrs.at_small);
6342   CHECKSUM_ATTR (attrs.at_segment);
6343   CHECKSUM_ATTR (attrs.at_string_length);
6344   CHECKSUM_ATTR (attrs.at_threads_scaled);
6345   CHECKSUM_ATTR (attrs.at_upper_bound);
6346   CHECKSUM_ATTR (attrs.at_use_location);
6347   CHECKSUM_ATTR (attrs.at_use_UTF8);
6348   CHECKSUM_ATTR (attrs.at_variable_parameter);
6349   CHECKSUM_ATTR (attrs.at_virtuality);
6350   CHECKSUM_ATTR (attrs.at_visibility);
6351   CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6352   CHECKSUM_ATTR (attrs.at_type);
6353   CHECKSUM_ATTR (attrs.at_friend);
6354
6355   /* Checksum the child DIEs.  */
6356   c = die->die_child;
6357   if (c) do {
6358     dw_attr_ref name_attr;
6359
6360     c = c->die_sib;
6361     name_attr = get_AT (c, DW_AT_name);
6362     if (is_template_instantiation (c))
6363       {
6364         /* Ignore instantiations of member type and function templates.  */
6365       }
6366     else if (name_attr != NULL
6367              && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6368       {
6369         /* Use a shallow checksum for named nested types and member
6370            functions.  */
6371         CHECKSUM_ULEB128 ('S');
6372         CHECKSUM_ULEB128 (c->die_tag);
6373         CHECKSUM_STRING (AT_string (name_attr));
6374       }
6375     else
6376       {
6377         /* Use a deep checksum for other children.  */
6378         /* Mark this DIE so it gets processed when unmarking.  */
6379         if (c->die_mark == 0)
6380           c->die_mark = -1;
6381         die_checksum_ordered (c, ctx, mark);
6382       }
6383   } while (c != die->die_child);
6384
6385   CHECKSUM_ULEB128 (0);
6386 }
6387
6388 /* Add a type name and tag to a hash.  */
6389 static void
6390 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6391 {
6392   CHECKSUM_ULEB128 (tag);
6393   CHECKSUM_STRING (name);
6394 }
6395
6396 #undef CHECKSUM
6397 #undef CHECKSUM_STRING
6398 #undef CHECKSUM_ATTR
6399 #undef CHECKSUM_LEB128
6400 #undef CHECKSUM_ULEB128
6401
6402 /* Generate the type signature for DIE.  This is computed by generating an
6403    MD5 checksum over the DIE's tag, its relevant attributes, and its
6404    children.  Attributes that are references to other DIEs are processed
6405    by recursion, using the MARK field to prevent infinite recursion.
6406    If the DIE is nested inside a namespace or another type, we also
6407    need to include that context in the signature.  The lower 64 bits
6408    of the resulting MD5 checksum comprise the signature.  */
6409
6410 static void
6411 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6412 {
6413   int mark;
6414   const char *name;
6415   unsigned char checksum[16];
6416   struct md5_ctx ctx;
6417   dw_die_ref decl;
6418   dw_die_ref parent;
6419
6420   name = get_AT_string (die, DW_AT_name);
6421   decl = get_AT_ref (die, DW_AT_specification);
6422   parent = get_die_parent (die);
6423
6424   /* First, compute a signature for just the type name (and its surrounding
6425      context, if any.  This is stored in the type unit DIE for link-time
6426      ODR (one-definition rule) checking.  */
6427
6428   if (is_cxx () && name != NULL)
6429     {
6430       md5_init_ctx (&ctx);
6431
6432       /* Checksum the names of surrounding namespaces and structures.  */
6433       if (parent != NULL)
6434         checksum_die_context (parent, &ctx);
6435
6436       /* Checksum the current DIE. */
6437       die_odr_checksum (die->die_tag, name, &ctx);
6438       md5_finish_ctx (&ctx, checksum);
6439
6440       add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6441     }
6442
6443   /* Next, compute the complete type signature.  */
6444
6445   md5_init_ctx (&ctx);
6446   mark = 1;
6447   die->die_mark = mark;
6448
6449   /* Checksum the names of surrounding namespaces and structures.  */
6450   if (parent != NULL)
6451     checksum_die_context (parent, &ctx);
6452
6453   /* Checksum the DIE and its children.  */
6454   die_checksum_ordered (die, &ctx, &mark);
6455   unmark_all_dies (die);
6456   md5_finish_ctx (&ctx, checksum);
6457
6458   /* Store the signature in the type node and link the type DIE and the
6459      type node together.  */
6460   memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6461           DWARF_TYPE_SIGNATURE_SIZE);
6462   die->comdat_type_p = true;
6463   die->die_id.die_type_node = type_node;
6464   type_node->type_die = die;
6465
6466   /* If the DIE is a specification, link its declaration to the type node
6467      as well.  */
6468   if (decl != NULL)
6469     {
6470       decl->comdat_type_p = true;
6471       decl->die_id.die_type_node = type_node;
6472     }
6473 }
6474
6475 /* Do the location expressions look same?  */
6476 static inline int
6477 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6478 {
6479   return loc1->dw_loc_opc == loc2->dw_loc_opc
6480          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6481          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6482 }
6483
6484 /* Do the values look the same?  */
6485 static int
6486 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6487 {
6488   dw_loc_descr_ref loc1, loc2;
6489   rtx r1, r2;
6490
6491   if (v1->val_class != v2->val_class)
6492     return 0;
6493
6494   switch (v1->val_class)
6495     {
6496     case dw_val_class_const:
6497       return v1->v.val_int == v2->v.val_int;
6498     case dw_val_class_unsigned_const:
6499       return v1->v.val_unsigned == v2->v.val_unsigned;
6500     case dw_val_class_const_double:
6501       return v1->v.val_double.high == v2->v.val_double.high
6502              && v1->v.val_double.low == v2->v.val_double.low;
6503     case dw_val_class_wide_int:
6504       return *v1->v.val_wide == *v2->v.val_wide;
6505     case dw_val_class_vec:
6506       if (v1->v.val_vec.length != v2->v.val_vec.length
6507           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6508         return 0;
6509       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6510                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
6511         return 0;
6512       return 1;
6513     case dw_val_class_flag:
6514       return v1->v.val_flag == v2->v.val_flag;
6515     case dw_val_class_str:
6516       return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6517
6518     case dw_val_class_addr:
6519       r1 = v1->v.val_addr;
6520       r2 = v2->v.val_addr;
6521       if (GET_CODE (r1) != GET_CODE (r2))
6522         return 0;
6523       return !rtx_equal_p (r1, r2);
6524
6525     case dw_val_class_offset:
6526       return v1->v.val_offset == v2->v.val_offset;
6527
6528     case dw_val_class_loc:
6529       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6530            loc1 && loc2;
6531            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6532         if (!same_loc_p (loc1, loc2, mark))
6533           return 0;
6534       return !loc1 && !loc2;
6535
6536     case dw_val_class_die_ref:
6537       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6538
6539     case dw_val_class_fde_ref:
6540     case dw_val_class_vms_delta:
6541     case dw_val_class_lbl_id:
6542     case dw_val_class_lineptr:
6543     case dw_val_class_macptr:
6544     case dw_val_class_high_pc:
6545       return 1;
6546
6547     case dw_val_class_file:
6548       return v1->v.val_file == v2->v.val_file;
6549
6550     case dw_val_class_data8:
6551       return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6552
6553     default:
6554       return 1;
6555     }
6556 }
6557
6558 /* Do the attributes look the same?  */
6559
6560 static int
6561 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6562 {
6563   if (at1->dw_attr != at2->dw_attr)
6564     return 0;
6565
6566   /* We don't care that this was compiled with a different compiler
6567      snapshot; if the output is the same, that's what matters. */
6568   if (at1->dw_attr == DW_AT_producer)
6569     return 1;
6570
6571   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6572 }
6573
6574 /* Do the dies look the same?  */
6575
6576 static int
6577 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6578 {
6579   dw_die_ref c1, c2;
6580   dw_attr_ref a1;
6581   unsigned ix;
6582
6583   /* To avoid infinite recursion.  */
6584   if (die1->die_mark)
6585     return die1->die_mark == die2->die_mark;
6586   die1->die_mark = die2->die_mark = ++(*mark);
6587
6588   if (die1->die_tag != die2->die_tag)
6589     return 0;
6590
6591   if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6592     return 0;
6593
6594   FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6595     if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6596       return 0;
6597
6598   c1 = die1->die_child;
6599   c2 = die2->die_child;
6600   if (! c1)
6601     {
6602       if (c2)
6603         return 0;
6604     }
6605   else
6606     for (;;)
6607       {
6608         if (!same_die_p (c1, c2, mark))
6609           return 0;
6610         c1 = c1->die_sib;
6611         c2 = c2->die_sib;
6612         if (c1 == die1->die_child)
6613           {
6614             if (c2 == die2->die_child)
6615               break;
6616             else
6617               return 0;
6618           }
6619     }
6620
6621   return 1;
6622 }
6623
6624 /* Do the dies look the same?  Wrapper around same_die_p.  */
6625
6626 static int
6627 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6628 {
6629   int mark = 0;
6630   int ret = same_die_p (die1, die2, &mark);
6631
6632   unmark_all_dies (die1);
6633   unmark_all_dies (die2);
6634
6635   return ret;
6636 }
6637
6638 /* The prefix to attach to symbols on DIEs in the current comdat debug
6639    info section.  */
6640 static const char *comdat_symbol_id;
6641
6642 /* The index of the current symbol within the current comdat CU.  */
6643 static unsigned int comdat_symbol_number;
6644
6645 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6646    children, and set comdat_symbol_id accordingly.  */
6647
6648 static void
6649 compute_section_prefix (dw_die_ref unit_die)
6650 {
6651   const char *die_name = get_AT_string (unit_die, DW_AT_name);
6652   const char *base = die_name ? lbasename (die_name) : "anonymous";
6653   char *name = XALLOCAVEC (char, strlen (base) + 64);
6654   char *p;
6655   int i, mark;
6656   unsigned char checksum[16];
6657   struct md5_ctx ctx;
6658
6659   /* Compute the checksum of the DIE, then append part of it as hex digits to
6660      the name filename of the unit.  */
6661
6662   md5_init_ctx (&ctx);
6663   mark = 0;
6664   die_checksum (unit_die, &ctx, &mark);
6665   unmark_all_dies (unit_die);
6666   md5_finish_ctx (&ctx, checksum);
6667
6668   sprintf (name, "%s.", base);
6669   clean_symbol_name (name);
6670
6671   p = name + strlen (name);
6672   for (i = 0; i < 4; i++)
6673     {
6674       sprintf (p, "%.2x", checksum[i]);
6675       p += 2;
6676     }
6677
6678   comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6679   comdat_symbol_number = 0;
6680 }
6681
6682 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
6683
6684 static int
6685 is_type_die (dw_die_ref die)
6686 {
6687   switch (die->die_tag)
6688     {
6689     case DW_TAG_array_type:
6690     case DW_TAG_class_type:
6691     case DW_TAG_interface_type:
6692     case DW_TAG_enumeration_type:
6693     case DW_TAG_pointer_type:
6694     case DW_TAG_reference_type:
6695     case DW_TAG_rvalue_reference_type:
6696     case DW_TAG_string_type:
6697     case DW_TAG_structure_type:
6698     case DW_TAG_subroutine_type:
6699     case DW_TAG_union_type:
6700     case DW_TAG_ptr_to_member_type:
6701     case DW_TAG_set_type:
6702     case DW_TAG_subrange_type:
6703     case DW_TAG_base_type:
6704     case DW_TAG_const_type:
6705     case DW_TAG_file_type:
6706     case DW_TAG_packed_type:
6707     case DW_TAG_volatile_type:
6708     case DW_TAG_typedef:
6709       return 1;
6710     default:
6711       return 0;
6712     }
6713 }
6714
6715 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6716    Basically, we want to choose the bits that are likely to be shared between
6717    compilations (types) and leave out the bits that are specific to individual
6718    compilations (functions).  */
6719
6720 static int
6721 is_comdat_die (dw_die_ref c)
6722 {
6723   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6724      we do for stabs.  The advantage is a greater likelihood of sharing between
6725      objects that don't include headers in the same order (and therefore would
6726      put the base types in a different comdat).  jason 8/28/00 */
6727
6728   if (c->die_tag == DW_TAG_base_type)
6729     return 0;
6730
6731   if (c->die_tag == DW_TAG_pointer_type
6732       || c->die_tag == DW_TAG_reference_type
6733       || c->die_tag == DW_TAG_rvalue_reference_type
6734       || c->die_tag == DW_TAG_const_type
6735       || c->die_tag == DW_TAG_volatile_type)
6736     {
6737       dw_die_ref t = get_AT_ref (c, DW_AT_type);
6738
6739       return t ? is_comdat_die (t) : 0;
6740     }
6741
6742   return is_type_die (c);
6743 }
6744
6745 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6746    compilation unit.  */
6747
6748 static int
6749 is_symbol_die (dw_die_ref c)
6750 {
6751   return (is_type_die (c)
6752           || is_declaration_die (c)
6753           || c->die_tag == DW_TAG_namespace
6754           || c->die_tag == DW_TAG_module);
6755 }
6756
6757 /* Returns true iff C is a compile-unit DIE.  */
6758
6759 static inline bool
6760 is_cu_die (dw_die_ref c)
6761 {
6762   return c && c->die_tag == DW_TAG_compile_unit;
6763 }
6764
6765 /* Returns true iff C is a unit DIE of some sort.  */
6766
6767 static inline bool
6768 is_unit_die (dw_die_ref c)
6769 {
6770   return c && (c->die_tag == DW_TAG_compile_unit
6771                || c->die_tag == DW_TAG_partial_unit
6772                || c->die_tag == DW_TAG_type_unit);
6773 }
6774
6775 /* Returns true iff C is a namespace DIE.  */
6776
6777 static inline bool
6778 is_namespace_die (dw_die_ref c)
6779 {
6780   return c && c->die_tag == DW_TAG_namespace;
6781 }
6782
6783 /* Returns true iff C is a class or structure DIE.  */
6784
6785 static inline bool
6786 is_class_die (dw_die_ref c)
6787 {
6788   return c && (c->die_tag == DW_TAG_class_type
6789                || c->die_tag == DW_TAG_structure_type);
6790 }
6791
6792 /* Return non-zero if this DIE is a template parameter.  */
6793
6794 static inline bool
6795 is_template_parameter (dw_die_ref die)
6796 {
6797   switch (die->die_tag)
6798     {
6799     case DW_TAG_template_type_param:
6800     case DW_TAG_template_value_param:
6801     case DW_TAG_GNU_template_template_param:
6802     case DW_TAG_GNU_template_parameter_pack:
6803       return true;
6804     default:
6805       return false;
6806     }
6807 }
6808
6809 /* Return non-zero if this DIE represents a template instantiation.  */
6810
6811 static inline bool
6812 is_template_instantiation (dw_die_ref die)
6813 {
6814   dw_die_ref c;
6815
6816   if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
6817     return false;
6818   FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
6819   return false;
6820 }
6821
6822 static char *
6823 gen_internal_sym (const char *prefix)
6824 {
6825   char buf[256];
6826
6827   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6828   return xstrdup (buf);
6829 }
6830
6831 /* Assign symbols to all worthy DIEs under DIE.  */
6832
6833 static void
6834 assign_symbol_names (dw_die_ref die)
6835 {
6836   dw_die_ref c;
6837
6838   if (is_symbol_die (die) && !die->comdat_type_p)
6839     {
6840       if (comdat_symbol_id)
6841         {
6842           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6843
6844           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6845                    comdat_symbol_id, comdat_symbol_number++);
6846           die->die_id.die_symbol = xstrdup (p);
6847         }
6848       else
6849         die->die_id.die_symbol = gen_internal_sym ("LDIE");
6850     }
6851
6852   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6853 }
6854
6855 struct cu_hash_table_entry
6856 {
6857   dw_die_ref cu;
6858   unsigned min_comdat_num, max_comdat_num;
6859   struct cu_hash_table_entry *next;
6860 };
6861
6862 /* Helpers to manipulate hash table of CUs.  */
6863
6864 struct cu_hash_table_entry_hasher
6865 {
6866   typedef cu_hash_table_entry value_type;
6867   typedef die_struct compare_type;
6868   static inline hashval_t hash (const value_type *);
6869   static inline bool equal (const value_type *, const compare_type *);
6870   static inline void remove (value_type *);
6871 };
6872
6873 inline hashval_t
6874 cu_hash_table_entry_hasher::hash (const value_type *entry)
6875 {
6876   return htab_hash_string (entry->cu->die_id.die_symbol);
6877 }
6878
6879 inline bool
6880 cu_hash_table_entry_hasher::equal (const value_type *entry1,
6881                                    const compare_type *entry2)
6882 {
6883   return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6884 }
6885
6886 inline void
6887 cu_hash_table_entry_hasher::remove (value_type *entry)
6888 {
6889   struct cu_hash_table_entry *next;
6890
6891   while (entry)
6892     {
6893       next = entry->next;
6894       free (entry);
6895       entry = next;
6896     }
6897 }
6898
6899 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
6900
6901 /* Check whether we have already seen this CU and set up SYM_NUM
6902    accordingly.  */
6903 static int
6904 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
6905 {
6906   struct cu_hash_table_entry dummy;
6907   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6908
6909   dummy.max_comdat_num = 0;
6910
6911   slot = htable->find_slot_with_hash (cu,
6912                                       htab_hash_string (cu->die_id.die_symbol),
6913                                       INSERT);
6914   entry = *slot;
6915
6916   for (; entry; last = entry, entry = entry->next)
6917     {
6918       if (same_die_p_wrap (cu, entry->cu))
6919         break;
6920     }
6921
6922   if (entry)
6923     {
6924       *sym_num = entry->min_comdat_num;
6925       return 1;
6926     }
6927
6928   entry = XCNEW (struct cu_hash_table_entry);
6929   entry->cu = cu;
6930   entry->min_comdat_num = *sym_num = last->max_comdat_num;
6931   entry->next = *slot;
6932   *slot = entry;
6933
6934   return 0;
6935 }
6936
6937 /* Record SYM_NUM to record of CU in HTABLE.  */
6938 static void
6939 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
6940                              unsigned int sym_num)
6941 {
6942   struct cu_hash_table_entry **slot, *entry;
6943
6944   slot = htable->find_slot_with_hash (cu,
6945                                       htab_hash_string (cu->die_id.die_symbol),
6946                                       NO_INSERT);
6947   entry = *slot;
6948
6949   entry->max_comdat_num = sym_num;
6950 }
6951
6952 /* Traverse the DIE (which is always comp_unit_die), and set up
6953    additional compilation units for each of the include files we see
6954    bracketed by BINCL/EINCL.  */
6955
6956 static void
6957 break_out_includes (dw_die_ref die)
6958 {
6959   dw_die_ref c;
6960   dw_die_ref unit = NULL;
6961   limbo_die_node *node, **pnode;
6962
6963   c = die->die_child;
6964   if (c) do {
6965     dw_die_ref prev = c;
6966     c = c->die_sib;
6967     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6968            || (unit && is_comdat_die (c)))
6969       {
6970         dw_die_ref next = c->die_sib;
6971
6972         /* This DIE is for a secondary CU; remove it from the main one.  */
6973         remove_child_with_prev (c, prev);
6974
6975         if (c->die_tag == DW_TAG_GNU_BINCL)
6976           unit = push_new_compile_unit (unit, c);
6977         else if (c->die_tag == DW_TAG_GNU_EINCL)
6978           unit = pop_compile_unit (unit);
6979         else
6980           add_child_die (unit, c);
6981         c = next;
6982         if (c == die->die_child)
6983           break;
6984       }
6985   } while (c != die->die_child);
6986
6987 #if 0
6988   /* We can only use this in debugging, since the frontend doesn't check
6989      to make sure that we leave every include file we enter.  */
6990   gcc_assert (!unit);
6991 #endif
6992
6993   assign_symbol_names (die);
6994   cu_hash_type cu_hash_table (10);
6995   for (node = limbo_die_list, pnode = &limbo_die_list;
6996        node;
6997        node = node->next)
6998     {
6999       int is_dupl;
7000
7001       compute_section_prefix (node->die);
7002       is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
7003                         &comdat_symbol_number);
7004       assign_symbol_names (node->die);
7005       if (is_dupl)
7006         *pnode = node->next;
7007       else
7008         {
7009           pnode = &node->next;
7010           record_comdat_symbol_number (node->die, &cu_hash_table,
7011                 comdat_symbol_number);
7012         }
7013     }
7014 }
7015
7016 /* Return non-zero if this DIE is a declaration.  */
7017
7018 static int
7019 is_declaration_die (dw_die_ref die)
7020 {
7021   dw_attr_ref a;
7022   unsigned ix;
7023
7024   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7025     if (a->dw_attr == DW_AT_declaration)
7026       return 1;
7027
7028   return 0;
7029 }
7030
7031 /* Return non-zero if this DIE is nested inside a subprogram.  */
7032
7033 static int
7034 is_nested_in_subprogram (dw_die_ref die)
7035 {
7036   dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7037
7038   if (decl == NULL)
7039     decl = die;
7040   return local_scope_p (decl);
7041 }
7042
7043 /* Return non-zero if this DIE contains a defining declaration of a
7044    subprogram.  */
7045
7046 static int
7047 contains_subprogram_definition (dw_die_ref die)
7048 {
7049   dw_die_ref c;
7050
7051   if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7052     return 1;
7053   FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7054   return 0;
7055 }
7056
7057 /* Return non-zero if this is a type DIE that should be moved to a
7058    COMDAT .debug_types section.  */
7059
7060 static int
7061 should_move_die_to_comdat (dw_die_ref die)
7062 {
7063   switch (die->die_tag)
7064     {
7065     case DW_TAG_class_type:
7066     case DW_TAG_structure_type:
7067     case DW_TAG_enumeration_type:
7068     case DW_TAG_union_type:
7069       /* Don't move declarations, inlined instances, types nested in a
7070          subprogram, or types that contain subprogram definitions.  */
7071       if (is_declaration_die (die)
7072           || get_AT (die, DW_AT_abstract_origin)
7073           || is_nested_in_subprogram (die)
7074           || contains_subprogram_definition (die))
7075         return 0;
7076       return 1;
7077     case DW_TAG_array_type:
7078     case DW_TAG_interface_type:
7079     case DW_TAG_pointer_type:
7080     case DW_TAG_reference_type:
7081     case DW_TAG_rvalue_reference_type:
7082     case DW_TAG_string_type:
7083     case DW_TAG_subroutine_type:
7084     case DW_TAG_ptr_to_member_type:
7085     case DW_TAG_set_type:
7086     case DW_TAG_subrange_type:
7087     case DW_TAG_base_type:
7088     case DW_TAG_const_type:
7089     case DW_TAG_file_type:
7090     case DW_TAG_packed_type:
7091     case DW_TAG_volatile_type:
7092     case DW_TAG_typedef:
7093     default:
7094       return 0;
7095     }
7096 }
7097
7098 /* Make a clone of DIE.  */
7099
7100 static dw_die_ref
7101 clone_die (dw_die_ref die)
7102 {
7103   dw_die_ref clone;
7104   dw_attr_ref a;
7105   unsigned ix;
7106
7107   clone = ggc_cleared_alloc<die_node> ();
7108   clone->die_tag = die->die_tag;
7109
7110   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7111     add_dwarf_attr (clone, a);
7112
7113   return clone;
7114 }
7115
7116 /* Make a clone of the tree rooted at DIE.  */
7117
7118 static dw_die_ref
7119 clone_tree (dw_die_ref die)
7120 {
7121   dw_die_ref c;
7122   dw_die_ref clone = clone_die (die);
7123
7124   FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7125
7126   return clone;
7127 }
7128
7129 /* Make a clone of DIE as a declaration.  */
7130
7131 static dw_die_ref
7132 clone_as_declaration (dw_die_ref die)
7133 {
7134   dw_die_ref clone;
7135   dw_die_ref decl;
7136   dw_attr_ref a;
7137   unsigned ix;
7138
7139   /* If the DIE is already a declaration, just clone it.  */
7140   if (is_declaration_die (die))
7141     return clone_die (die);
7142
7143   /* If the DIE is a specification, just clone its declaration DIE.  */
7144   decl = get_AT_ref (die, DW_AT_specification);
7145   if (decl != NULL)
7146     {
7147       clone = clone_die (decl);
7148       if (die->comdat_type_p)
7149         add_AT_die_ref (clone, DW_AT_signature, die);
7150       return clone;
7151     }
7152
7153   clone = ggc_cleared_alloc<die_node> ();
7154   clone->die_tag = die->die_tag;
7155
7156   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7157     {
7158       /* We don't want to copy over all attributes.
7159          For example we don't want DW_AT_byte_size because otherwise we will no
7160          longer have a declaration and GDB will treat it as a definition.  */
7161
7162       switch (a->dw_attr)
7163         {
7164         case DW_AT_abstract_origin:
7165         case DW_AT_artificial:
7166         case DW_AT_containing_type:
7167         case DW_AT_external:
7168         case DW_AT_name:
7169         case DW_AT_type:
7170         case DW_AT_virtuality:
7171         case DW_AT_linkage_name:
7172         case DW_AT_MIPS_linkage_name:
7173           add_dwarf_attr (clone, a);
7174           break;
7175         case DW_AT_byte_size:
7176         default:
7177           break;
7178         }
7179     }
7180
7181   if (die->comdat_type_p)
7182     add_AT_die_ref (clone, DW_AT_signature, die);
7183
7184   add_AT_flag (clone, DW_AT_declaration, 1);
7185   return clone;
7186 }
7187
7188
7189 /* Structure to map a DIE in one CU to its copy in a comdat type unit.  */
7190
7191 struct decl_table_entry
7192 {
7193   dw_die_ref orig;
7194   dw_die_ref copy;
7195 };
7196
7197 /* Helpers to manipulate hash table of copied declarations.  */
7198
7199 /* Hashtable helpers.  */
7200
7201 struct decl_table_entry_hasher : typed_free_remove <decl_table_entry>
7202 {
7203   typedef decl_table_entry value_type;
7204   typedef die_struct compare_type;
7205   static inline hashval_t hash (const value_type *);
7206   static inline bool equal (const value_type *, const compare_type *);
7207 };
7208
7209 inline hashval_t
7210 decl_table_entry_hasher::hash (const value_type *entry)
7211 {
7212   return htab_hash_pointer (entry->orig);
7213 }
7214
7215 inline bool
7216 decl_table_entry_hasher::equal (const value_type *entry1,
7217                                 const compare_type *entry2)
7218 {
7219   return entry1->orig == entry2;
7220 }
7221
7222 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7223
7224 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7225    or type unit entry, to a new tree.  Adds the new tree to UNIT and returns
7226    a pointer to the copy of DIE.  If DECL_TABLE is provided, it is used
7227    to check if the ancestor has already been copied into UNIT.  */
7228
7229 static dw_die_ref
7230 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7231                     decl_hash_type *decl_table)
7232 {
7233   dw_die_ref parent = die->die_parent;
7234   dw_die_ref new_parent = unit;
7235   dw_die_ref copy;
7236   decl_table_entry **slot = NULL;
7237   struct decl_table_entry *entry = NULL;
7238
7239   if (decl_table)
7240     {
7241       /* Check if the entry has already been copied to UNIT.  */
7242       slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7243                                               INSERT);
7244       if (*slot != HTAB_EMPTY_ENTRY)
7245         {
7246           entry = *slot;
7247           return entry->copy;
7248         }
7249
7250       /* Record in DECL_TABLE that DIE has been copied to UNIT.  */
7251       entry = XCNEW (struct decl_table_entry);
7252       entry->orig = die;
7253       entry->copy = NULL;
7254       *slot = entry;
7255     }
7256
7257   if (parent != NULL)
7258     {
7259       dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7260       if (spec != NULL)
7261         parent = spec;
7262       if (!is_unit_die (parent))
7263         new_parent = copy_ancestor_tree (unit, parent, decl_table);
7264     }
7265
7266   copy = clone_as_declaration (die);
7267   add_child_die (new_parent, copy);
7268
7269   if (decl_table)
7270     {
7271       /* Record the pointer to the copy.  */
7272       entry->copy = copy;
7273     }
7274
7275   return copy;
7276 }
7277 /* Copy the declaration context to the new type unit DIE.  This includes
7278    any surrounding namespace or type declarations.  If the DIE has an
7279    AT_specification attribute, it also includes attributes and children
7280    attached to the specification, and returns a pointer to the original
7281    parent of the declaration DIE.  Returns NULL otherwise.  */
7282
7283 static dw_die_ref
7284 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7285 {
7286   dw_die_ref decl;
7287   dw_die_ref new_decl;
7288   dw_die_ref orig_parent = NULL;
7289
7290   decl = get_AT_ref (die, DW_AT_specification);
7291   if (decl == NULL)
7292     decl = die;
7293   else
7294     {
7295       unsigned ix;
7296       dw_die_ref c;
7297       dw_attr_ref a;
7298
7299       /* The original DIE will be changed to a declaration, and must
7300          be moved to be a child of the original declaration DIE.  */
7301       orig_parent = decl->die_parent;
7302
7303       /* Copy the type node pointer from the new DIE to the original
7304          declaration DIE so we can forward references later.  */
7305       decl->comdat_type_p = true;
7306       decl->die_id.die_type_node = die->die_id.die_type_node;
7307
7308       remove_AT (die, DW_AT_specification);
7309
7310       FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7311         {
7312           if (a->dw_attr != DW_AT_name
7313               && a->dw_attr != DW_AT_declaration
7314               && a->dw_attr != DW_AT_external)
7315             add_dwarf_attr (die, a);
7316         }
7317
7318       FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7319     }
7320
7321   if (decl->die_parent != NULL
7322       && !is_unit_die (decl->die_parent))
7323     {
7324       new_decl = copy_ancestor_tree (unit, decl, NULL);
7325       if (new_decl != NULL)
7326         {
7327           remove_AT (new_decl, DW_AT_signature);
7328           add_AT_specification (die, new_decl);
7329         }
7330     }
7331
7332   return orig_parent;
7333 }
7334
7335 /* Generate the skeleton ancestor tree for the given NODE, then clone
7336    the DIE and add the clone into the tree.  */
7337
7338 static void
7339 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7340 {
7341   if (node->new_die != NULL)
7342     return;
7343
7344   node->new_die = clone_as_declaration (node->old_die);
7345
7346   if (node->parent != NULL)
7347     {
7348       generate_skeleton_ancestor_tree (node->parent);
7349       add_child_die (node->parent->new_die, node->new_die);
7350     }
7351 }
7352
7353 /* Generate a skeleton tree of DIEs containing any declarations that are
7354    found in the original tree.  We traverse the tree looking for declaration
7355    DIEs, and construct the skeleton from the bottom up whenever we find one.  */
7356
7357 static void
7358 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7359 {
7360   skeleton_chain_node node;
7361   dw_die_ref c;
7362   dw_die_ref first;
7363   dw_die_ref prev = NULL;
7364   dw_die_ref next = NULL;
7365
7366   node.parent = parent;
7367
7368   first = c = parent->old_die->die_child;
7369   if (c)
7370     next = c->die_sib;
7371   if (c) do {
7372     if (prev == NULL || prev->die_sib == c)
7373       prev = c;
7374     c = next;
7375     next = (c == first ? NULL : c->die_sib);
7376     node.old_die = c;
7377     node.new_die = NULL;
7378     if (is_declaration_die (c))
7379       {
7380         if (is_template_instantiation (c))
7381           {
7382             /* Instantiated templates do not need to be cloned into the
7383                type unit.  Just move the DIE and its children back to
7384                the skeleton tree (in the main CU).  */
7385             remove_child_with_prev (c, prev);
7386             add_child_die (parent->new_die, c);
7387             c = prev;
7388           }
7389         else
7390           {
7391             /* Clone the existing DIE, move the original to the skeleton
7392                tree (which is in the main CU), and put the clone, with
7393                all the original's children, where the original came from
7394                (which is about to be moved to the type unit).  */
7395             dw_die_ref clone = clone_die (c);
7396             move_all_children (c, clone);
7397
7398             /* If the original has a DW_AT_object_pointer attribute,
7399                it would now point to a child DIE just moved to the
7400                cloned tree, so we need to remove that attribute from
7401                the original.  */
7402             remove_AT (c, DW_AT_object_pointer);
7403
7404             replace_child (c, clone, prev);
7405             generate_skeleton_ancestor_tree (parent);
7406             add_child_die (parent->new_die, c);
7407             node.new_die = c;
7408             c = clone;
7409           }
7410       }
7411     generate_skeleton_bottom_up (&node);
7412   } while (next != NULL);
7413 }
7414
7415 /* Wrapper function for generate_skeleton_bottom_up.  */
7416
7417 static dw_die_ref
7418 generate_skeleton (dw_die_ref die)
7419 {
7420   skeleton_chain_node node;
7421
7422   node.old_die = die;
7423   node.new_die = NULL;
7424   node.parent = NULL;
7425
7426   /* If this type definition is nested inside another type,
7427      and is not an instantiation of a template, always leave
7428      at least a declaration in its place.  */
7429   if (die->die_parent != NULL
7430       && is_type_die (die->die_parent)
7431       && !is_template_instantiation (die))
7432     node.new_die = clone_as_declaration (die);
7433
7434   generate_skeleton_bottom_up (&node);
7435   return node.new_die;
7436 }
7437
7438 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7439    declaration.  The original DIE is moved to a new compile unit so that
7440    existing references to it follow it to the new location.  If any of the
7441    original DIE's descendants is a declaration, we need to replace the
7442    original DIE with a skeleton tree and move the declarations back into the
7443    skeleton tree.  */
7444
7445 static dw_die_ref
7446 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7447                                        dw_die_ref prev)
7448 {
7449   dw_die_ref skeleton, orig_parent;
7450
7451   /* Copy the declaration context to the type unit DIE.  If the returned
7452      ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7453      that DIE.  */
7454   orig_parent = copy_declaration_context (unit, child);
7455
7456   skeleton = generate_skeleton (child);
7457   if (skeleton == NULL)
7458     remove_child_with_prev (child, prev);
7459   else
7460     {
7461       skeleton->comdat_type_p = true;
7462       skeleton->die_id.die_type_node = child->die_id.die_type_node;
7463
7464       /* If the original DIE was a specification, we need to put
7465          the skeleton under the parent DIE of the declaration.
7466          This leaves the original declaration in the tree, but
7467          it will be pruned later since there are no longer any
7468          references to it.  */
7469       if (orig_parent != NULL)
7470         {
7471           remove_child_with_prev (child, prev);
7472           add_child_die (orig_parent, skeleton);
7473         }
7474       else
7475         replace_child (child, skeleton, prev);
7476     }
7477
7478   return skeleton;
7479 }
7480
7481 /* Traverse the DIE and set up additional .debug_types sections for each
7482    type worthy of being placed in a COMDAT section.  */
7483
7484 static void
7485 break_out_comdat_types (dw_die_ref die)
7486 {
7487   dw_die_ref c;
7488   dw_die_ref first;
7489   dw_die_ref prev = NULL;
7490   dw_die_ref next = NULL;
7491   dw_die_ref unit = NULL;
7492
7493   first = c = die->die_child;
7494   if (c)
7495     next = c->die_sib;
7496   if (c) do {
7497     if (prev == NULL || prev->die_sib == c)
7498       prev = c;
7499     c = next;
7500     next = (c == first ? NULL : c->die_sib);
7501     if (should_move_die_to_comdat (c))
7502       {
7503         dw_die_ref replacement;
7504         comdat_type_node_ref type_node;
7505
7506         /* Break out nested types into their own type units.  */
7507         break_out_comdat_types (c);
7508
7509         /* Create a new type unit DIE as the root for the new tree, and
7510            add it to the list of comdat types.  */
7511         unit = new_die (DW_TAG_type_unit, NULL, NULL);
7512         add_AT_unsigned (unit, DW_AT_language,
7513                          get_AT_unsigned (comp_unit_die (), DW_AT_language));
7514         type_node = ggc_cleared_alloc<comdat_type_node> ();
7515         type_node->root_die = unit;
7516         type_node->next = comdat_type_list;
7517         comdat_type_list = type_node;
7518
7519         /* Generate the type signature.  */
7520         generate_type_signature (c, type_node);
7521
7522         /* Copy the declaration context, attributes, and children of the
7523            declaration into the new type unit DIE, then remove this DIE
7524            from the main CU (or replace it with a skeleton if necessary).  */
7525         replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7526         type_node->skeleton_die = replacement;
7527
7528         /* Add the DIE to the new compunit.  */
7529         add_child_die (unit, c);
7530
7531         if (replacement != NULL)
7532           c = replacement;
7533       }
7534     else if (c->die_tag == DW_TAG_namespace
7535              || c->die_tag == DW_TAG_class_type
7536              || c->die_tag == DW_TAG_structure_type
7537              || c->die_tag == DW_TAG_union_type)
7538       {
7539         /* Look for nested types that can be broken out.  */
7540         break_out_comdat_types (c);
7541       }
7542   } while (next != NULL);
7543 }
7544
7545 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
7546    Enter all the cloned children into the hash table decl_table.  */
7547
7548 static dw_die_ref
7549 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
7550 {
7551   dw_die_ref c;
7552   dw_die_ref clone;
7553   struct decl_table_entry *entry;
7554   decl_table_entry **slot;
7555
7556   if (die->die_tag == DW_TAG_subprogram)
7557     clone = clone_as_declaration (die);
7558   else
7559     clone = clone_die (die);
7560
7561   slot = decl_table->find_slot_with_hash (die,
7562                                           htab_hash_pointer (die), INSERT);
7563
7564   /* Assert that DIE isn't in the hash table yet.  If it would be there
7565      before, the ancestors would be necessarily there as well, therefore
7566      clone_tree_partial wouldn't be called.  */
7567   gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7568
7569   entry = XCNEW (struct decl_table_entry);
7570   entry->orig = die;
7571   entry->copy = clone;
7572   *slot = entry;
7573
7574   if (die->die_tag != DW_TAG_subprogram)
7575     FOR_EACH_CHILD (die, c,
7576                     add_child_die (clone, clone_tree_partial (c, decl_table)));
7577
7578   return clone;
7579 }
7580
7581 /* Walk the DIE and its children, looking for references to incomplete
7582    or trivial types that are unmarked (i.e., that are not in the current
7583    type_unit).  */
7584
7585 static void
7586 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
7587 {
7588   dw_die_ref c;
7589   dw_attr_ref a;
7590   unsigned ix;
7591
7592   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7593     {
7594       if (AT_class (a) == dw_val_class_die_ref)
7595         {
7596           dw_die_ref targ = AT_ref (a);
7597           decl_table_entry **slot;
7598           struct decl_table_entry *entry;
7599
7600           if (targ->die_mark != 0 || targ->comdat_type_p)
7601             continue;
7602
7603           slot = decl_table->find_slot_with_hash (targ,
7604                                                   htab_hash_pointer (targ),
7605                                                   INSERT);
7606
7607           if (*slot != HTAB_EMPTY_ENTRY)
7608             {
7609               /* TARG has already been copied, so we just need to
7610                  modify the reference to point to the copy.  */
7611               entry = *slot;
7612               a->dw_attr_val.v.val_die_ref.die = entry->copy;
7613             }
7614           else
7615             {
7616               dw_die_ref parent = unit;
7617               dw_die_ref copy = clone_die (targ);
7618
7619               /* Record in DECL_TABLE that TARG has been copied.
7620                  Need to do this now, before the recursive call,
7621                  because DECL_TABLE may be expanded and SLOT
7622                  would no longer be a valid pointer.  */
7623               entry = XCNEW (struct decl_table_entry);
7624               entry->orig = targ;
7625               entry->copy = copy;
7626               *slot = entry;
7627
7628               /* If TARG is not a declaration DIE, we need to copy its
7629                  children.  */
7630               if (!is_declaration_die (targ))
7631                 {
7632                   FOR_EACH_CHILD (
7633                       targ, c,
7634                       add_child_die (copy,
7635                                      clone_tree_partial (c, decl_table)));
7636                 }
7637
7638               /* Make sure the cloned tree is marked as part of the
7639                  type unit.  */
7640               mark_dies (copy);
7641
7642               /* If TARG has surrounding context, copy its ancestor tree
7643                  into the new type unit.  */
7644               if (targ->die_parent != NULL
7645                   && !is_unit_die (targ->die_parent))
7646                 parent = copy_ancestor_tree (unit, targ->die_parent,
7647                                              decl_table);
7648
7649               add_child_die (parent, copy);
7650               a->dw_attr_val.v.val_die_ref.die = copy;
7651
7652               /* Make sure the newly-copied DIE is walked.  If it was
7653                  installed in a previously-added context, it won't
7654                  get visited otherwise.  */
7655               if (parent != unit)
7656                 {
7657                   /* Find the highest point of the newly-added tree,
7658                      mark each node along the way, and walk from there.  */
7659                   parent->die_mark = 1;
7660                   while (parent->die_parent
7661                          && parent->die_parent->die_mark == 0)
7662                     {
7663                       parent = parent->die_parent;
7664                       parent->die_mark = 1;
7665                     }
7666                   copy_decls_walk (unit, parent, decl_table);
7667                 }
7668             }
7669         }
7670     }
7671
7672   FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7673 }
7674
7675 /* Copy declarations for "unworthy" types into the new comdat section.
7676    Incomplete types, modified types, and certain other types aren't broken
7677    out into comdat sections of their own, so they don't have a signature,
7678    and we need to copy the declaration into the same section so that we
7679    don't have an external reference.  */
7680
7681 static void
7682 copy_decls_for_unworthy_types (dw_die_ref unit)
7683 {
7684   mark_dies (unit);
7685   decl_hash_type decl_table (10);
7686   copy_decls_walk (unit, unit, &decl_table);
7687   unmark_dies (unit);
7688 }
7689
7690 /* Traverse the DIE and add a sibling attribute if it may have the
7691    effect of speeding up access to siblings.  To save some space,
7692    avoid generating sibling attributes for DIE's without children.  */
7693
7694 static void
7695 add_sibling_attributes (dw_die_ref die)
7696 {
7697   dw_die_ref c;
7698
7699   if (! die->die_child)
7700     return;
7701
7702   if (die->die_parent && die != die->die_parent->die_child)
7703     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7704
7705   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7706 }
7707
7708 /* Output all location lists for the DIE and its children.  */
7709
7710 static void
7711 output_location_lists (dw_die_ref die)
7712 {
7713   dw_die_ref c;
7714   dw_attr_ref a;
7715   unsigned ix;
7716
7717   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7718     if (AT_class (a) == dw_val_class_loc_list)
7719       output_loc_list (AT_loc_list (a));
7720
7721   FOR_EACH_CHILD (die, c, output_location_lists (c));
7722 }
7723
7724 /* We want to limit the number of external references, because they are
7725    larger than local references: a relocation takes multiple words, and
7726    even a sig8 reference is always eight bytes, whereas a local reference
7727    can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7728    So if we encounter multiple external references to the same type DIE, we
7729    make a local typedef stub for it and redirect all references there.
7730
7731    This is the element of the hash table for keeping track of these
7732    references.  */
7733
7734 struct external_ref
7735 {
7736   dw_die_ref type;
7737   dw_die_ref stub;
7738   unsigned n_refs;
7739 };
7740
7741 /* Hashtable helpers.  */
7742
7743 struct external_ref_hasher : typed_free_remove <external_ref>
7744 {
7745   typedef external_ref value_type;
7746   typedef external_ref compare_type;
7747   static inline hashval_t hash (const value_type *);
7748   static inline bool equal (const value_type *, const compare_type *);
7749 };
7750
7751 inline hashval_t
7752 external_ref_hasher::hash (const value_type *r)
7753 {
7754   dw_die_ref die = r->type;
7755   hashval_t h = 0;
7756
7757   /* We can't use the address of the DIE for hashing, because
7758      that will make the order of the stub DIEs non-deterministic.  */
7759   if (! die->comdat_type_p)
7760     /* We have a symbol; use it to compute a hash.  */
7761     h = htab_hash_string (die->die_id.die_symbol);
7762   else
7763     {
7764       /* We have a type signature; use a subset of the bits as the hash.
7765          The 8-byte signature is at least as large as hashval_t.  */
7766       comdat_type_node_ref type_node = die->die_id.die_type_node;
7767       memcpy (&h, type_node->signature, sizeof (h));
7768     }
7769   return h;
7770 }
7771
7772 inline bool
7773 external_ref_hasher::equal (const value_type *r1, const compare_type *r2)
7774 {
7775   return r1->type == r2->type;
7776 }
7777
7778 typedef hash_table<external_ref_hasher> external_ref_hash_type;
7779
7780 /* Return a pointer to the external_ref for references to DIE.  */
7781
7782 static struct external_ref *
7783 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
7784 {
7785   struct external_ref ref, *ref_p;
7786   external_ref **slot;
7787
7788   ref.type = die;
7789   slot = map->find_slot (&ref, INSERT);
7790   if (*slot != HTAB_EMPTY_ENTRY)
7791     return *slot;
7792
7793   ref_p = XCNEW (struct external_ref);
7794   ref_p->type = die;
7795   *slot = ref_p;
7796   return ref_p;
7797 }
7798
7799 /* Subroutine of optimize_external_refs, below.
7800
7801    If we see a type skeleton, record it as our stub.  If we see external
7802    references, remember how many we've seen.  */
7803
7804 static void
7805 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
7806 {
7807   dw_die_ref c;
7808   dw_attr_ref a;
7809   unsigned ix;
7810   struct external_ref *ref_p;
7811
7812   if (is_type_die (die)
7813       && (c = get_AT_ref (die, DW_AT_signature)))
7814     {
7815       /* This is a local skeleton; use it for local references.  */
7816       ref_p = lookup_external_ref (map, c);
7817       ref_p->stub = die;
7818     }
7819
7820   /* Scan the DIE references, and remember any that refer to DIEs from
7821      other CUs (i.e. those which are not marked).  */
7822   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7823     if (AT_class (a) == dw_val_class_die_ref
7824         && (c = AT_ref (a))->die_mark == 0
7825         && is_type_die (c))
7826       {
7827         ref_p = lookup_external_ref (map, c);
7828         ref_p->n_refs++;
7829       }
7830
7831   FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7832 }
7833
7834 /* htab_traverse callback function for optimize_external_refs, below.  SLOT
7835    points to an external_ref, DATA is the CU we're processing.  If we don't
7836    already have a local stub, and we have multiple refs, build a stub.  */
7837
7838 int
7839 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
7840 {
7841   struct external_ref *ref_p = *slot;
7842
7843   if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7844     {
7845       /* We have multiple references to this type, so build a small stub.
7846          Both of these forms are a bit dodgy from the perspective of the
7847          DWARF standard, since technically they should have names.  */
7848       dw_die_ref cu = data;
7849       dw_die_ref type = ref_p->type;
7850       dw_die_ref stub = NULL;
7851
7852       if (type->comdat_type_p)
7853         {
7854           /* If we refer to this type via sig8, use AT_signature.  */
7855           stub = new_die (type->die_tag, cu, NULL_TREE);
7856           add_AT_die_ref (stub, DW_AT_signature, type);
7857         }
7858       else
7859         {
7860           /* Otherwise, use a typedef with no name.  */
7861           stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7862           add_AT_die_ref (stub, DW_AT_type, type);
7863         }
7864
7865       stub->die_mark++;
7866       ref_p->stub = stub;
7867     }
7868   return 1;
7869 }
7870
7871 /* DIE is a unit; look through all the DIE references to see if there are
7872    any external references to types, and if so, create local stubs for
7873    them which will be applied in build_abbrev_table.  This is useful because
7874    references to local DIEs are smaller.  */
7875
7876 static external_ref_hash_type *
7877 optimize_external_refs (dw_die_ref die)
7878 {
7879   external_ref_hash_type *map = new external_ref_hash_type (10);
7880   optimize_external_refs_1 (die, map);
7881   map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
7882   return map;
7883 }
7884
7885 /* The format of each DIE (and its attribute value pairs) is encoded in an
7886    abbreviation table.  This routine builds the abbreviation table and assigns
7887    a unique abbreviation id for each abbreviation entry.  The children of each
7888    die are visited recursively.  */
7889
7890 static void
7891 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
7892 {
7893   unsigned long abbrev_id;
7894   unsigned int n_alloc;
7895   dw_die_ref c;
7896   dw_attr_ref a;
7897   unsigned ix;
7898
7899   /* Scan the DIE references, and replace any that refer to
7900      DIEs from other CUs (i.e. those which are not marked) with
7901      the local stubs we built in optimize_external_refs.  */
7902   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7903     if (AT_class (a) == dw_val_class_die_ref
7904         && (c = AT_ref (a))->die_mark == 0)
7905       {
7906         struct external_ref *ref_p;
7907         gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
7908
7909         ref_p = lookup_external_ref (extern_map, c);
7910         if (ref_p->stub && ref_p->stub != die)
7911           change_AT_die_ref (a, ref_p->stub);
7912         else
7913           /* We aren't changing this reference, so mark it external.  */
7914           set_AT_ref_external (a, 1);
7915       }
7916
7917   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7918     {
7919       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7920       dw_attr_ref die_a, abbrev_a;
7921       unsigned ix;
7922       bool ok = true;
7923
7924       if (abbrev->die_tag != die->die_tag)
7925         continue;
7926       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7927         continue;
7928
7929       if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
7930         continue;
7931
7932       FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
7933         {
7934           abbrev_a = &(*abbrev->die_attr)[ix];
7935           if ((abbrev_a->dw_attr != die_a->dw_attr)
7936               || (value_format (abbrev_a) != value_format (die_a)))
7937             {
7938               ok = false;
7939               break;
7940             }
7941         }
7942       if (ok)
7943         break;
7944     }
7945
7946   if (abbrev_id >= abbrev_die_table_in_use)
7947     {
7948       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7949         {
7950           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7951           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7952                                             n_alloc);
7953
7954           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7955                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7956           abbrev_die_table_allocated = n_alloc;
7957         }
7958
7959       ++abbrev_die_table_in_use;
7960       abbrev_die_table[abbrev_id] = die;
7961     }
7962
7963   die->die_abbrev = abbrev_id;
7964   FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
7965 }
7966 \f
7967 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
7968
7969 static int
7970 constant_size (unsigned HOST_WIDE_INT value)
7971 {
7972   int log;
7973
7974   if (value == 0)
7975     log = 0;
7976   else
7977     log = floor_log2 (value);
7978
7979   log = log / 8;
7980   log = 1 << (floor_log2 (log) + 1);
7981
7982   return log;
7983 }
7984
7985 /* Return the size of a DIE as it is represented in the
7986    .debug_info section.  */
7987
7988 static unsigned long
7989 size_of_die (dw_die_ref die)
7990 {
7991   unsigned long size = 0;
7992   dw_attr_ref a;
7993   unsigned ix;
7994   enum dwarf_form form;
7995
7996   size += size_of_uleb128 (die->die_abbrev);
7997   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7998     {
7999       switch (AT_class (a))
8000         {
8001         case dw_val_class_addr:
8002           if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8003             {
8004               gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8005               size += size_of_uleb128 (AT_index (a));
8006             }
8007           else
8008             size += DWARF2_ADDR_SIZE;
8009           break;
8010         case dw_val_class_offset:
8011           size += DWARF_OFFSET_SIZE;
8012           break;
8013         case dw_val_class_loc:
8014           {
8015             unsigned long lsize = size_of_locs (AT_loc (a));
8016
8017             /* Block length.  */
8018             if (dwarf_version >= 4)
8019               size += size_of_uleb128 (lsize);
8020             else
8021               size += constant_size (lsize);
8022             size += lsize;
8023           }
8024           break;
8025         case dw_val_class_loc_list:
8026           if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8027             {
8028               gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8029               size += size_of_uleb128 (AT_index (a));
8030             }
8031           else
8032             size += DWARF_OFFSET_SIZE;
8033           break;
8034         case dw_val_class_range_list:
8035           size += DWARF_OFFSET_SIZE;
8036           break;
8037         case dw_val_class_const:
8038           size += size_of_sleb128 (AT_int (a));
8039           break;
8040         case dw_val_class_unsigned_const:
8041           {
8042             int csize = constant_size (AT_unsigned (a));
8043             if (dwarf_version == 3
8044                 && a->dw_attr == DW_AT_data_member_location
8045                 && csize >= 4)
8046               size += size_of_uleb128 (AT_unsigned (a));
8047             else
8048               size += csize;
8049           }
8050           break;
8051         case dw_val_class_const_double:
8052           size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
8053           if (HOST_BITS_PER_WIDE_INT >= 64)
8054             size++; /* block */
8055           break;
8056         case dw_val_class_wide_int:
8057           size += (get_full_len (*a->dw_attr_val.v.val_wide)
8058                    * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
8059           if (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT
8060               > 64)
8061             size++; /* block */
8062           break;
8063         case dw_val_class_vec:
8064           size += constant_size (a->dw_attr_val.v.val_vec.length
8065                                  * a->dw_attr_val.v.val_vec.elt_size)
8066                   + a->dw_attr_val.v.val_vec.length
8067                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
8068           break;
8069         case dw_val_class_flag:
8070           if (dwarf_version >= 4)
8071             /* Currently all add_AT_flag calls pass in 1 as last argument,
8072                so DW_FORM_flag_present can be used.  If that ever changes,
8073                we'll need to use DW_FORM_flag and have some optimization
8074                in build_abbrev_table that will change those to
8075                DW_FORM_flag_present if it is set to 1 in all DIEs using
8076                the same abbrev entry.  */
8077             gcc_assert (a->dw_attr_val.v.val_flag == 1);
8078           else
8079             size += 1;
8080           break;
8081         case dw_val_class_die_ref:
8082           if (AT_ref_external (a))
8083             {
8084               /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
8085                  we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
8086                  is sized by target address length, whereas in DWARF3
8087                  it's always sized as an offset.  */
8088               if (use_debug_types)
8089                 size += DWARF_TYPE_SIGNATURE_SIZE;
8090               else if (dwarf_version == 2)
8091                 size += DWARF2_ADDR_SIZE;
8092               else
8093                 size += DWARF_OFFSET_SIZE;
8094             }
8095           else
8096             size += DWARF_OFFSET_SIZE;
8097           break;
8098         case dw_val_class_fde_ref:
8099           size += DWARF_OFFSET_SIZE;
8100           break;
8101         case dw_val_class_lbl_id:
8102           if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8103             {
8104               gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8105               size += size_of_uleb128 (AT_index (a));
8106             }
8107           else
8108             size += DWARF2_ADDR_SIZE;
8109           break;
8110         case dw_val_class_lineptr:
8111         case dw_val_class_macptr:
8112           size += DWARF_OFFSET_SIZE;
8113           break;
8114         case dw_val_class_str:
8115           form = AT_string_form (a);
8116           if (form == DW_FORM_strp)
8117             size += DWARF_OFFSET_SIZE;
8118          else if (form == DW_FORM_GNU_str_index)
8119             size += size_of_uleb128 (AT_index (a));
8120           else
8121             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8122           break;
8123         case dw_val_class_file:
8124           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8125           break;
8126         case dw_val_class_data8:
8127           size += 8;
8128           break;
8129         case dw_val_class_vms_delta:
8130           size += DWARF_OFFSET_SIZE;
8131           break;
8132         case dw_val_class_high_pc:
8133           size += DWARF2_ADDR_SIZE;
8134           break;
8135         default:
8136           gcc_unreachable ();
8137         }
8138     }
8139
8140   return size;
8141 }
8142
8143 /* Size the debugging information associated with a given DIE.  Visits the
8144    DIE's children recursively.  Updates the global variable next_die_offset, on
8145    each time through.  Uses the current value of next_die_offset to update the
8146    die_offset field in each DIE.  */
8147
8148 static void
8149 calc_die_sizes (dw_die_ref die)
8150 {
8151   dw_die_ref c;
8152
8153   gcc_assert (die->die_offset == 0
8154               || (unsigned long int) die->die_offset == next_die_offset);
8155   die->die_offset = next_die_offset;
8156   next_die_offset += size_of_die (die);
8157
8158   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8159
8160   if (die->die_child != NULL)
8161     /* Count the null byte used to terminate sibling lists.  */
8162     next_die_offset += 1;
8163 }
8164
8165 /* Size just the base type children at the start of the CU.
8166    This is needed because build_abbrev needs to size locs
8167    and sizing of type based stack ops needs to know die_offset
8168    values for the base types.  */
8169
8170 static void
8171 calc_base_type_die_sizes (void)
8172 {
8173   unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8174   unsigned int i;
8175   dw_die_ref base_type;
8176 #if ENABLE_ASSERT_CHECKING
8177   dw_die_ref prev = comp_unit_die ()->die_child;
8178 #endif
8179
8180   die_offset += size_of_die (comp_unit_die ());
8181   for (i = 0; base_types.iterate (i, &base_type); i++)
8182     {
8183 #if ENABLE_ASSERT_CHECKING
8184       gcc_assert (base_type->die_offset == 0
8185                   && prev->die_sib == base_type
8186                   && base_type->die_child == NULL
8187                   && base_type->die_abbrev);
8188       prev = base_type;
8189 #endif
8190       base_type->die_offset = die_offset;
8191       die_offset += size_of_die (base_type);
8192     }
8193 }
8194
8195 /* Set the marks for a die and its children.  We do this so
8196    that we know whether or not a reference needs to use FORM_ref_addr; only
8197    DIEs in the same CU will be marked.  We used to clear out the offset
8198    and use that as the flag, but ran into ordering problems.  */
8199
8200 static void
8201 mark_dies (dw_die_ref die)
8202 {
8203   dw_die_ref c;
8204
8205   gcc_assert (!die->die_mark);
8206
8207   die->die_mark = 1;
8208   FOR_EACH_CHILD (die, c, mark_dies (c));
8209 }
8210
8211 /* Clear the marks for a die and its children.  */
8212
8213 static void
8214 unmark_dies (dw_die_ref die)
8215 {
8216   dw_die_ref c;
8217
8218   if (! use_debug_types)
8219     gcc_assert (die->die_mark);
8220
8221   die->die_mark = 0;
8222   FOR_EACH_CHILD (die, c, unmark_dies (c));
8223 }
8224
8225 /* Clear the marks for a die, its children and referred dies.  */
8226
8227 static void
8228 unmark_all_dies (dw_die_ref die)
8229 {
8230   dw_die_ref c;
8231   dw_attr_ref a;
8232   unsigned ix;
8233
8234   if (!die->die_mark)
8235     return;
8236   die->die_mark = 0;
8237
8238   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8239
8240   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8241     if (AT_class (a) == dw_val_class_die_ref)
8242       unmark_all_dies (AT_ref (a));
8243 }
8244
8245 /* Calculate if the entry should appear in the final output file.  It may be
8246    from a pruned a type.  */
8247
8248 static bool
8249 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
8250 {
8251   /* By limiting gnu pubnames to definitions only, gold can generate a
8252      gdb index without entries for declarations, which don't include
8253      enough information to be useful.  */
8254   if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
8255     return false;
8256
8257   if (table == pubname_table)
8258     {
8259       /* Enumerator names are part of the pubname table, but the
8260          parent DW_TAG_enumeration_type die may have been pruned.
8261          Don't output them if that is the case.  */
8262       if (p->die->die_tag == DW_TAG_enumerator &&
8263           (p->die->die_parent == NULL
8264            || !p->die->die_parent->die_perennial_p))
8265         return false;
8266
8267       /* Everything else in the pubname table is included.  */
8268       return true;
8269     }
8270
8271   /* The pubtypes table shouldn't include types that have been
8272      pruned.  */
8273   return (p->die->die_offset != 0
8274           || !flag_eliminate_unused_debug_types);
8275 }
8276
8277 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8278    generated for the compilation unit.  */
8279
8280 static unsigned long
8281 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8282 {
8283   unsigned long size;
8284   unsigned i;
8285   pubname_ref p;
8286   int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8287
8288   size = DWARF_PUBNAMES_HEADER_SIZE;
8289   FOR_EACH_VEC_ELT (*names, i, p)
8290     if (include_pubname_in_output (names, p))
8291       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8292
8293   size += DWARF_OFFSET_SIZE;
8294   return size;
8295 }
8296
8297 /* Return the size of the information in the .debug_aranges section.  */
8298
8299 static unsigned long
8300 size_of_aranges (void)
8301 {
8302   unsigned long size;
8303
8304   size = DWARF_ARANGES_HEADER_SIZE;
8305
8306   /* Count the address/length pair for this compilation unit.  */
8307   if (text_section_used)
8308     size += 2 * DWARF2_ADDR_SIZE;
8309   if (cold_text_section_used)
8310     size += 2 * DWARF2_ADDR_SIZE;
8311   if (have_multiple_function_sections)
8312     {
8313       unsigned fde_idx;
8314       dw_fde_ref fde;
8315
8316       FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8317         {
8318           if (DECL_IGNORED_P (fde->decl))
8319             continue;
8320           if (!fde->in_std_section)
8321             size += 2 * DWARF2_ADDR_SIZE;
8322           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8323             size += 2 * DWARF2_ADDR_SIZE;
8324         }
8325     }
8326
8327   /* Count the two zero words used to terminated the address range table.  */
8328   size += 2 * DWARF2_ADDR_SIZE;
8329   return size;
8330 }
8331 \f
8332 /* Select the encoding of an attribute value.  */
8333
8334 static enum dwarf_form
8335 value_format (dw_attr_ref a)
8336 {
8337   switch (AT_class (a))
8338     {
8339     case dw_val_class_addr:
8340       /* Only very few attributes allow DW_FORM_addr.  */
8341       switch (a->dw_attr)
8342         {
8343         case DW_AT_low_pc:
8344         case DW_AT_high_pc:
8345         case DW_AT_entry_pc:
8346         case DW_AT_trampoline:
8347           return (AT_index (a) == NOT_INDEXED
8348                   ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8349         default:
8350           break;
8351         }
8352       switch (DWARF2_ADDR_SIZE)
8353         {
8354         case 1:
8355           return DW_FORM_data1;
8356         case 2:
8357           return DW_FORM_data2;
8358         case 4:
8359           return DW_FORM_data4;
8360         case 8:
8361           return DW_FORM_data8;
8362         default:
8363           gcc_unreachable ();
8364         }
8365     case dw_val_class_range_list:
8366     case dw_val_class_loc_list:
8367       if (dwarf_version >= 4)
8368         return DW_FORM_sec_offset;
8369       /* FALLTHRU */
8370     case dw_val_class_vms_delta:
8371     case dw_val_class_offset:
8372       switch (DWARF_OFFSET_SIZE)
8373         {
8374         case 4:
8375           return DW_FORM_data4;
8376         case 8:
8377           return DW_FORM_data8;
8378         default:
8379           gcc_unreachable ();
8380         }
8381     case dw_val_class_loc:
8382       if (dwarf_version >= 4)
8383         return DW_FORM_exprloc;
8384       switch (constant_size (size_of_locs (AT_loc (a))))
8385         {
8386         case 1:
8387           return DW_FORM_block1;
8388         case 2:
8389           return DW_FORM_block2;
8390         case 4:
8391           return DW_FORM_block4;
8392         default:
8393           gcc_unreachable ();
8394         }
8395     case dw_val_class_const:
8396       return DW_FORM_sdata;
8397     case dw_val_class_unsigned_const:
8398       switch (constant_size (AT_unsigned (a)))
8399         {
8400         case 1:
8401           return DW_FORM_data1;
8402         case 2:
8403           return DW_FORM_data2;
8404         case 4:
8405           /* In DWARF3 DW_AT_data_member_location with
8406              DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8407              constant, so we need to use DW_FORM_udata if we need
8408              a large constant.  */
8409           if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8410             return DW_FORM_udata;
8411           return DW_FORM_data4;
8412         case 8:
8413           if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8414             return DW_FORM_udata;
8415           return DW_FORM_data8;
8416         default:
8417           gcc_unreachable ();
8418         }
8419     case dw_val_class_const_double:
8420       switch (HOST_BITS_PER_WIDE_INT)
8421         {
8422         case 8:
8423           return DW_FORM_data2;
8424         case 16:
8425           return DW_FORM_data4;
8426         case 32:
8427           return DW_FORM_data8;
8428         case 64:
8429         default:
8430           return DW_FORM_block1;
8431         }
8432     case dw_val_class_wide_int:
8433       switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
8434         {
8435         case 8:
8436           return DW_FORM_data1;
8437         case 16:
8438           return DW_FORM_data2;
8439         case 32:
8440           return DW_FORM_data4;
8441         case 64:
8442           return DW_FORM_data8;
8443         default:
8444           return DW_FORM_block1;
8445         }
8446     case dw_val_class_vec:
8447       switch (constant_size (a->dw_attr_val.v.val_vec.length
8448                              * a->dw_attr_val.v.val_vec.elt_size))
8449         {
8450         case 1:
8451           return DW_FORM_block1;
8452         case 2:
8453           return DW_FORM_block2;
8454         case 4:
8455           return DW_FORM_block4;
8456         default:
8457           gcc_unreachable ();
8458         }
8459     case dw_val_class_flag:
8460       if (dwarf_version >= 4)
8461         {
8462           /* Currently all add_AT_flag calls pass in 1 as last argument,
8463              so DW_FORM_flag_present can be used.  If that ever changes,
8464              we'll need to use DW_FORM_flag and have some optimization
8465              in build_abbrev_table that will change those to
8466              DW_FORM_flag_present if it is set to 1 in all DIEs using
8467              the same abbrev entry.  */
8468           gcc_assert (a->dw_attr_val.v.val_flag == 1);
8469           return DW_FORM_flag_present;
8470         }
8471       return DW_FORM_flag;
8472     case dw_val_class_die_ref:
8473       if (AT_ref_external (a))
8474         return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8475       else
8476         return DW_FORM_ref;
8477     case dw_val_class_fde_ref:
8478       return DW_FORM_data;
8479     case dw_val_class_lbl_id:
8480       return (AT_index (a) == NOT_INDEXED
8481               ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8482     case dw_val_class_lineptr:
8483     case dw_val_class_macptr:
8484       return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8485     case dw_val_class_str:
8486       return AT_string_form (a);
8487     case dw_val_class_file:
8488       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8489         {
8490         case 1:
8491           return DW_FORM_data1;
8492         case 2:
8493           return DW_FORM_data2;
8494         case 4:
8495           return DW_FORM_data4;
8496         default:
8497           gcc_unreachable ();
8498         }
8499
8500     case dw_val_class_data8:
8501       return DW_FORM_data8;
8502
8503     case dw_val_class_high_pc:
8504       switch (DWARF2_ADDR_SIZE)
8505         {
8506         case 1:
8507           return DW_FORM_data1;
8508         case 2:
8509           return DW_FORM_data2;
8510         case 4:
8511           return DW_FORM_data4;
8512         case 8:
8513           return DW_FORM_data8;
8514         default:
8515           gcc_unreachable ();
8516         }
8517
8518     default:
8519       gcc_unreachable ();
8520     }
8521 }
8522
8523 /* Output the encoding of an attribute value.  */
8524
8525 static void
8526 output_value_format (dw_attr_ref a)
8527 {
8528   enum dwarf_form form = value_format (a);
8529
8530   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8531 }
8532
8533 /* Given a die and id, produce the appropriate abbreviations.  */
8534
8535 static void
8536 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8537 {
8538   unsigned ix;
8539   dw_attr_ref a_attr;
8540
8541   dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8542   dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8543                                dwarf_tag_name (abbrev->die_tag));
8544
8545   if (abbrev->die_child != NULL)
8546     dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8547   else
8548     dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8549
8550   for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8551     {
8552       dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8553                                    dwarf_attr_name (a_attr->dw_attr));
8554       output_value_format (a_attr);
8555     }
8556
8557   dw2_asm_output_data (1, 0, NULL);
8558   dw2_asm_output_data (1, 0, NULL);
8559 }
8560
8561
8562 /* Output the .debug_abbrev section which defines the DIE abbreviation
8563    table.  */
8564
8565 static void
8566 output_abbrev_section (void)
8567 {
8568   unsigned long abbrev_id;
8569
8570   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8571     output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8572
8573   /* Terminate the table.  */
8574   dw2_asm_output_data (1, 0, NULL);
8575 }
8576
8577 /* Output a symbol we can use to refer to this DIE from another CU.  */
8578
8579 static inline void
8580 output_die_symbol (dw_die_ref die)
8581 {
8582   const char *sym = die->die_id.die_symbol;
8583
8584   gcc_assert (!die->comdat_type_p);
8585
8586   if (sym == 0)
8587     return;
8588
8589   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8590     /* We make these global, not weak; if the target doesn't support
8591        .linkonce, it doesn't support combining the sections, so debugging
8592        will break.  */
8593     targetm.asm_out.globalize_label (asm_out_file, sym);
8594
8595   ASM_OUTPUT_LABEL (asm_out_file, sym);
8596 }
8597
8598 /* Return a new location list, given the begin and end range, and the
8599    expression.  */
8600
8601 static inline dw_loc_list_ref
8602 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8603               const char *section)
8604 {
8605   dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
8606
8607   retlist->begin = begin;
8608   retlist->begin_entry = NULL;
8609   retlist->end = end;
8610   retlist->expr = expr;
8611   retlist->section = section;
8612
8613   return retlist;
8614 }
8615
8616 /* Generate a new internal symbol for this location list node, if it
8617    hasn't got one yet.  */
8618
8619 static inline void
8620 gen_llsym (dw_loc_list_ref list)
8621 {
8622   gcc_assert (!list->ll_symbol);
8623   list->ll_symbol = gen_internal_sym ("LLST");
8624 }
8625
8626 /* Output the location list given to us.  */
8627
8628 static void
8629 output_loc_list (dw_loc_list_ref list_head)
8630 {
8631   dw_loc_list_ref curr = list_head;
8632
8633   if (list_head->emitted)
8634     return;
8635   list_head->emitted = true;
8636
8637   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8638
8639   /* Walk the location list, and output each range + expression.  */
8640   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8641     {
8642       unsigned long size;
8643       /* Don't output an entry that starts and ends at the same address.  */
8644       if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8645         continue;
8646       size = size_of_locs (curr->expr);
8647       /* If the expression is too large, drop it on the floor.  We could
8648          perhaps put it into DW_TAG_dwarf_procedure and refer to that
8649          in the expression, but >= 64KB expressions for a single value
8650          in a single range are unlikely very useful.  */
8651       if (size > 0xffff)
8652         continue;
8653       if (dwarf_split_debug_info)
8654         {
8655           dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8656                                "Location list start/length entry (%s)",
8657                                list_head->ll_symbol);
8658           dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8659                                        "Location list range start index (%s)",
8660                                        curr->begin);
8661           /* The length field is 4 bytes.  If we ever need to support
8662             an 8-byte length, we can add a new DW_LLE code or fall back
8663             to DW_LLE_GNU_start_end_entry.  */
8664           dw2_asm_output_delta (4, curr->end, curr->begin,
8665                                 "Location list range length (%s)",
8666                                 list_head->ll_symbol);
8667         }
8668       else if (!have_multiple_function_sections)
8669         {
8670           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8671                                 "Location list begin address (%s)",
8672                                 list_head->ll_symbol);
8673           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8674                                 "Location list end address (%s)",
8675                                 list_head->ll_symbol);
8676         }
8677       else
8678         {
8679           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8680                                "Location list begin address (%s)",
8681                                list_head->ll_symbol);
8682           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8683                                "Location list end address (%s)",
8684                                list_head->ll_symbol);
8685         }
8686
8687       /* Output the block length for this list of location operations.  */
8688       gcc_assert (size <= 0xffff);
8689       dw2_asm_output_data (2, size, "%s", "Location expression size");
8690
8691       output_loc_sequence (curr->expr, -1);
8692     }
8693
8694   if (dwarf_split_debug_info)
8695     dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8696                          "Location list terminator (%s)",
8697                          list_head->ll_symbol);
8698   else
8699     {
8700       dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8701                            "Location list terminator begin (%s)",
8702                            list_head->ll_symbol);
8703       dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8704                            "Location list terminator end (%s)",
8705                            list_head->ll_symbol);
8706     }
8707 }
8708
8709 /* Output a range_list offset into the debug_range section.  Emit a
8710    relocated reference if val_entry is NULL, otherwise, emit an
8711    indirect reference.  */
8712
8713 static void
8714 output_range_list_offset (dw_attr_ref a)
8715 {
8716   const char *name = dwarf_attr_name (a->dw_attr);
8717
8718   if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8719     {
8720       char *p = strchr (ranges_section_label, '\0');
8721       sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8722       dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8723                              debug_ranges_section, "%s", name);
8724       *p = '\0';
8725     }
8726   else
8727     dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8728                          "%s (offset from %s)", name, ranges_section_label);
8729 }
8730
8731 /* Output the offset into the debug_loc section.  */
8732
8733 static void
8734 output_loc_list_offset (dw_attr_ref a)
8735 {
8736   char *sym = AT_loc_list (a)->ll_symbol;
8737
8738   gcc_assert (sym);
8739   if (dwarf_split_debug_info)
8740     dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8741                           "%s", dwarf_attr_name (a->dw_attr));
8742   else
8743     dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8744                            "%s", dwarf_attr_name (a->dw_attr));
8745 }
8746
8747 /* Output an attribute's index or value appropriately.  */
8748
8749 static void
8750 output_attr_index_or_value (dw_attr_ref a)
8751 {
8752   const char *name = dwarf_attr_name (a->dw_attr);
8753
8754   if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8755     {
8756       dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8757       return;
8758     }
8759   switch (AT_class (a))
8760     {
8761       case dw_val_class_addr:
8762         dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8763         break;
8764       case dw_val_class_high_pc:
8765       case dw_val_class_lbl_id:
8766         dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8767         break;
8768       case dw_val_class_loc_list:
8769         output_loc_list_offset (a);
8770         break;
8771       default:
8772         gcc_unreachable ();
8773     }
8774 }
8775
8776 /* Output a type signature.  */
8777
8778 static inline void
8779 output_signature (const char *sig, const char *name)
8780 {
8781   int i;
8782
8783   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8784     dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8785 }
8786
8787 /* Output the DIE and its attributes.  Called recursively to generate
8788    the definitions of each child DIE.  */
8789
8790 static void
8791 output_die (dw_die_ref die)
8792 {
8793   dw_attr_ref a;
8794   dw_die_ref c;
8795   unsigned long size;
8796   unsigned ix;
8797
8798   /* If someone in another CU might refer to us, set up a symbol for
8799      them to point to.  */
8800   if (! die->comdat_type_p && die->die_id.die_symbol)
8801     output_die_symbol (die);
8802
8803   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8804                                (unsigned long)die->die_offset,
8805                                dwarf_tag_name (die->die_tag));
8806
8807   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8808     {
8809       const char *name = dwarf_attr_name (a->dw_attr);
8810
8811       switch (AT_class (a))
8812         {
8813         case dw_val_class_addr:
8814           output_attr_index_or_value (a);
8815           break;
8816
8817         case dw_val_class_offset:
8818           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8819                                "%s", name);
8820           break;
8821
8822         case dw_val_class_range_list:
8823           output_range_list_offset (a);
8824           break;
8825
8826         case dw_val_class_loc:
8827           size = size_of_locs (AT_loc (a));
8828
8829           /* Output the block length for this list of location operations.  */
8830           if (dwarf_version >= 4)
8831             dw2_asm_output_data_uleb128 (size, "%s", name);
8832           else
8833             dw2_asm_output_data (constant_size (size), size, "%s", name);
8834
8835           output_loc_sequence (AT_loc (a), -1);
8836           break;
8837
8838         case dw_val_class_const:
8839           /* ??? It would be slightly more efficient to use a scheme like is
8840              used for unsigned constants below, but gdb 4.x does not sign
8841              extend.  Gdb 5.x does sign extend.  */
8842           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8843           break;
8844
8845         case dw_val_class_unsigned_const:
8846           {
8847             int csize = constant_size (AT_unsigned (a));
8848             if (dwarf_version == 3
8849                 && a->dw_attr == DW_AT_data_member_location
8850                 && csize >= 4)
8851               dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8852             else
8853               dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8854           }
8855           break;
8856
8857         case dw_val_class_const_double:
8858           {
8859             unsigned HOST_WIDE_INT first, second;
8860
8861             if (HOST_BITS_PER_WIDE_INT >= 64)
8862               dw2_asm_output_data (1,
8863                                    HOST_BITS_PER_DOUBLE_INT
8864                                    / HOST_BITS_PER_CHAR,
8865                                    NULL);
8866
8867             if (WORDS_BIG_ENDIAN)
8868               {
8869                 first = a->dw_attr_val.v.val_double.high;
8870                 second = a->dw_attr_val.v.val_double.low;
8871               }
8872             else
8873               {
8874                 first = a->dw_attr_val.v.val_double.low;
8875                 second = a->dw_attr_val.v.val_double.high;
8876               }
8877
8878             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8879                                  first, "%s", name);
8880             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8881                                  second, NULL);
8882           }
8883           break;
8884
8885         case dw_val_class_wide_int:
8886           {
8887             int i;
8888             int len = get_full_len (*a->dw_attr_val.v.val_wide);
8889             int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
8890             if (len * HOST_BITS_PER_WIDE_INT > 64)
8891               dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide) * l,
8892                                    NULL);
8893
8894             if (WORDS_BIG_ENDIAN)
8895               for (i = len - 1; i >= 0; --i)
8896                 {
8897                   dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8898                                        name);
8899                   name = NULL;
8900                 }
8901             else
8902               for (i = 0; i < len; ++i)
8903                 {
8904                   dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8905                                        name);
8906                   name = NULL;
8907                 }
8908           }
8909           break;
8910
8911         case dw_val_class_vec:
8912           {
8913             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8914             unsigned int len = a->dw_attr_val.v.val_vec.length;
8915             unsigned int i;
8916             unsigned char *p;
8917
8918             dw2_asm_output_data (constant_size (len * elt_size),
8919                                  len * elt_size, "%s", name);
8920             if (elt_size > sizeof (HOST_WIDE_INT))
8921               {
8922                 elt_size /= 2;
8923                 len *= 2;
8924               }
8925             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8926                  i < len;
8927                  i++, p += elt_size)
8928               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8929                                    "fp or vector constant word %u", i);
8930             break;
8931           }
8932
8933         case dw_val_class_flag:
8934           if (dwarf_version >= 4)
8935             {
8936               /* Currently all add_AT_flag calls pass in 1 as last argument,
8937                  so DW_FORM_flag_present can be used.  If that ever changes,
8938                  we'll need to use DW_FORM_flag and have some optimization
8939                  in build_abbrev_table that will change those to
8940                  DW_FORM_flag_present if it is set to 1 in all DIEs using
8941                  the same abbrev entry.  */
8942               gcc_assert (AT_flag (a) == 1);
8943               if (flag_debug_asm)
8944                 fprintf (asm_out_file, "\t\t\t%s %s\n",
8945                          ASM_COMMENT_START, name);
8946               break;
8947             }
8948           dw2_asm_output_data (1, AT_flag (a), "%s", name);
8949           break;
8950
8951         case dw_val_class_loc_list:
8952           output_attr_index_or_value (a);
8953           break;
8954
8955         case dw_val_class_die_ref:
8956           if (AT_ref_external (a))
8957             {
8958               if (AT_ref (a)->comdat_type_p)
8959                 {
8960                   comdat_type_node_ref type_node =
8961                     AT_ref (a)->die_id.die_type_node;
8962
8963                   gcc_assert (type_node);
8964                   output_signature (type_node->signature, name);
8965                 }
8966               else
8967                 {
8968                   const char *sym = AT_ref (a)->die_id.die_symbol;
8969                   int size;
8970
8971                   gcc_assert (sym);
8972                   /* In DWARF2, DW_FORM_ref_addr is sized by target address
8973                      length, whereas in DWARF3 it's always sized as an
8974                      offset.  */
8975                   if (dwarf_version == 2)
8976                     size = DWARF2_ADDR_SIZE;
8977                   else
8978                     size = DWARF_OFFSET_SIZE;
8979                   dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8980                                          name);
8981                 }
8982             }
8983           else
8984             {
8985               gcc_assert (AT_ref (a)->die_offset);
8986               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8987                                    "%s", name);
8988             }
8989           break;
8990
8991         case dw_val_class_fde_ref:
8992           {
8993             char l1[20];
8994
8995             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8996                                          a->dw_attr_val.v.val_fde_index * 2);
8997             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8998                                    "%s", name);
8999           }
9000           break;
9001
9002         case dw_val_class_vms_delta:
9003           dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
9004                                     AT_vms_delta2 (a), AT_vms_delta1 (a),
9005                                     "%s", name);
9006           break;
9007
9008         case dw_val_class_lbl_id:
9009           output_attr_index_or_value (a);
9010           break;
9011
9012         case dw_val_class_lineptr:
9013           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9014                                  debug_line_section, "%s", name);
9015           break;
9016
9017         case dw_val_class_macptr:
9018           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9019                                  debug_macinfo_section, "%s", name);
9020           break;
9021
9022         case dw_val_class_str:
9023           if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
9024             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9025                                    a->dw_attr_val.v.val_str->label,
9026                                    debug_str_section,
9027                                    "%s: \"%s\"", name, AT_string (a));
9028           else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
9029             dw2_asm_output_data_uleb128 (AT_index (a),
9030                                          "%s: \"%s\"", name, AT_string (a));
9031           else
9032             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
9033           break;
9034
9035         case dw_val_class_file:
9036           {
9037             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
9038
9039             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
9040                                  a->dw_attr_val.v.val_file->filename);
9041             break;
9042           }
9043
9044         case dw_val_class_data8:
9045           {
9046             int i;
9047
9048             for (i = 0; i < 8; i++)
9049               dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
9050                                    i == 0 ? "%s" : NULL, name);
9051             break;
9052           }
9053
9054         case dw_val_class_high_pc:
9055           dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
9056                                 get_AT_low_pc (die), "DW_AT_high_pc");
9057           break;
9058
9059         default:
9060           gcc_unreachable ();
9061         }
9062     }
9063
9064   FOR_EACH_CHILD (die, c, output_die (c));
9065
9066   /* Add null byte to terminate sibling list.  */
9067   if (die->die_child != NULL)
9068     dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
9069                          (unsigned long) die->die_offset);
9070 }
9071
9072 /* Output the compilation unit that appears at the beginning of the
9073    .debug_info section, and precedes the DIE descriptions.  */
9074
9075 static void
9076 output_compilation_unit_header (void)
9077 {
9078   /* We don't support actual DWARFv5 units yet, we just use some
9079      DWARFv5 draft DIE tags in DWARFv4 format.  */
9080   int ver = dwarf_version < 5 ? dwarf_version : 4;
9081
9082   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9083     dw2_asm_output_data (4, 0xffffffff,
9084       "Initial length escape value indicating 64-bit DWARF extension");
9085   dw2_asm_output_data (DWARF_OFFSET_SIZE,
9086                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
9087                        "Length of Compilation Unit Info");
9088   dw2_asm_output_data (2, ver, "DWARF version number");
9089   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
9090                          debug_abbrev_section,
9091                          "Offset Into Abbrev. Section");
9092   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9093 }
9094
9095 /* Output the compilation unit DIE and its children.  */
9096
9097 static void
9098 output_comp_unit (dw_die_ref die, int output_if_empty)
9099 {
9100   const char *secname, *oldsym;
9101   char *tmp;
9102
9103   /* Unless we are outputting main CU, we may throw away empty ones.  */
9104   if (!output_if_empty && die->die_child == NULL)
9105     return;
9106
9107   /* Even if there are no children of this DIE, we must output the information
9108      about the compilation unit.  Otherwise, on an empty translation unit, we
9109      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
9110      will then complain when examining the file.  First mark all the DIEs in
9111      this CU so we know which get local refs.  */
9112   mark_dies (die);
9113
9114   external_ref_hash_type *extern_map = optimize_external_refs (die);
9115
9116   build_abbrev_table (die, extern_map);
9117
9118   delete extern_map;
9119
9120   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
9121   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
9122   calc_die_sizes (die);
9123
9124   oldsym = die->die_id.die_symbol;
9125   if (oldsym)
9126     {
9127       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
9128
9129       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
9130       secname = tmp;
9131       die->die_id.die_symbol = NULL;
9132       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9133     }
9134   else
9135     {
9136       switch_to_section (debug_info_section);
9137       ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
9138       info_section_emitted = true;
9139     }
9140
9141   /* Output debugging information.  */
9142   output_compilation_unit_header ();
9143   output_die (die);
9144
9145   /* Leave the marks on the main CU, so we can check them in
9146      output_pubnames.  */
9147   if (oldsym)
9148     {
9149       unmark_dies (die);
9150       die->die_id.die_symbol = oldsym;
9151     }
9152 }
9153
9154 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
9155    and .debug_pubtypes.  This is configured per-target, but can be
9156    overridden by the -gpubnames or -gno-pubnames options.  */
9157
9158 static inline bool
9159 want_pubnames (void)
9160 {
9161   if (debug_info_level <= DINFO_LEVEL_TERSE)
9162     return false;
9163   if (debug_generate_pub_sections != -1)
9164     return debug_generate_pub_sections;
9165   return targetm.want_debug_pub_sections;
9166 }
9167
9168 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes.  */
9169
9170 static void
9171 add_AT_pubnames (dw_die_ref die)
9172 {
9173   if (want_pubnames ())
9174     add_AT_flag (die, DW_AT_GNU_pubnames, 1);
9175 }
9176
9177 /* Add a string attribute value to a skeleton DIE.  */
9178
9179 static inline void
9180 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
9181                         const char *str)
9182 {
9183   dw_attr_node attr;
9184   struct indirect_string_node *node;
9185
9186   if (! skeleton_debug_str_hash)
9187     skeleton_debug_str_hash
9188       = hash_table<indirect_string_hasher>::create_ggc (10);
9189
9190   node = find_AT_string_in_table (str, skeleton_debug_str_hash);
9191   find_string_form (node);
9192   if (node->form == DW_FORM_GNU_str_index)
9193     node->form = DW_FORM_strp;
9194
9195   attr.dw_attr = attr_kind;
9196   attr.dw_attr_val.val_class = dw_val_class_str;
9197   attr.dw_attr_val.val_entry = NULL;
9198   attr.dw_attr_val.v.val_str = node;
9199   add_dwarf_attr (die, &attr);
9200 }
9201
9202 /* Helper function to generate top-level dies for skeleton debug_info and
9203    debug_types.  */
9204
9205 static void
9206 add_top_level_skeleton_die_attrs (dw_die_ref die)
9207 {
9208   const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
9209   const char *comp_dir = comp_dir_string ();
9210
9211   add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
9212   if (comp_dir != NULL)
9213     add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
9214   add_AT_pubnames (die);
9215   add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
9216 }
9217
9218 /* Output skeleton debug sections that point to the dwo file.  */
9219
9220 static void
9221 output_skeleton_debug_sections (dw_die_ref comp_unit)
9222 {
9223   /* We don't support actual DWARFv5 units yet, we just use some
9224      DWARFv5 draft DIE tags in DWARFv4 format.  */
9225   int ver = dwarf_version < 5 ? dwarf_version : 4;
9226
9227   /* These attributes will be found in the full debug_info section.  */
9228   remove_AT (comp_unit, DW_AT_producer);
9229   remove_AT (comp_unit, DW_AT_language);
9230
9231   switch_to_section (debug_skeleton_info_section);
9232   ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
9233
9234   /* Produce the skeleton compilation-unit header.  This one differs enough from
9235      a normal CU header that it's better not to call output_compilation_unit
9236      header.  */
9237   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9238     dw2_asm_output_data (4, 0xffffffff,
9239       "Initial length escape value indicating 64-bit DWARF extension");
9240
9241   dw2_asm_output_data (DWARF_OFFSET_SIZE,
9242                        DWARF_COMPILE_UNIT_HEADER_SIZE
9243                        - DWARF_INITIAL_LENGTH_SIZE
9244                        + size_of_die (comp_unit),
9245                       "Length of Compilation Unit Info");
9246   dw2_asm_output_data (2, ver, "DWARF version number");
9247   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
9248                          debug_abbrev_section,
9249                          "Offset Into Abbrev. Section");
9250   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9251
9252   comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
9253   output_die (comp_unit);
9254
9255   /* Build the skeleton debug_abbrev section.  */
9256   switch_to_section (debug_skeleton_abbrev_section);
9257   ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
9258
9259   output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
9260
9261   dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
9262 }
9263
9264 /* Output a comdat type unit DIE and its children.  */
9265
9266 static void
9267 output_comdat_type_unit (comdat_type_node *node)
9268 {
9269   const char *secname;
9270   char *tmp;
9271   int i;
9272 #if defined (OBJECT_FORMAT_ELF)
9273   tree comdat_key;
9274 #endif
9275
9276   /* First mark all the DIEs in this CU so we know which get local refs.  */
9277   mark_dies (node->root_die);
9278
9279   external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
9280
9281   build_abbrev_table (node->root_die, extern_map);
9282
9283   delete extern_map;
9284   extern_map = NULL;
9285
9286   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
9287   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
9288   calc_die_sizes (node->root_die);
9289
9290 #if defined (OBJECT_FORMAT_ELF)
9291   if (!dwarf_split_debug_info)
9292     secname = ".debug_types";
9293   else
9294     secname = ".debug_types.dwo";
9295
9296   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9297   sprintf (tmp, "wt.");
9298   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9299     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9300   comdat_key = get_identifier (tmp);
9301   targetm.asm_out.named_section (secname,
9302                                  SECTION_DEBUG | SECTION_LINKONCE,
9303                                  comdat_key);
9304 #else
9305   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9306   sprintf (tmp, ".gnu.linkonce.wt.");
9307   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9308     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9309   secname = tmp;
9310   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9311 #endif
9312
9313   /* Output debugging information.  */
9314   output_compilation_unit_header ();
9315   output_signature (node->signature, "Type Signature");
9316   dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9317                        "Offset to Type DIE");
9318   output_die (node->root_die);
9319
9320   unmark_dies (node->root_die);
9321 }
9322
9323 /* Return the DWARF2/3 pubname associated with a decl.  */
9324
9325 static const char *
9326 dwarf2_name (tree decl, int scope)
9327 {
9328   if (DECL_NAMELESS (decl))
9329     return NULL;
9330   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9331 }
9332
9333 /* Add a new entry to .debug_pubnames if appropriate.  */
9334
9335 static void
9336 add_pubname_string (const char *str, dw_die_ref die)
9337 {
9338   pubname_entry e;
9339
9340   e.die = die;
9341   e.name = xstrdup (str);
9342   vec_safe_push (pubname_table, e);
9343 }
9344
9345 static void
9346 add_pubname (tree decl, dw_die_ref die)
9347 {
9348   if (!want_pubnames ())
9349     return;
9350
9351   /* Don't add items to the table when we expect that the consumer will have
9352      just read the enclosing die.  For example, if the consumer is looking at a
9353      class_member, it will either be inside the class already, or will have just
9354      looked up the class to find the member.  Either way, searching the class is
9355      faster than searching the index.  */
9356   if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9357       || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9358     {
9359       const char *name = dwarf2_name (decl, 1);
9360
9361       if (name)
9362         add_pubname_string (name, die);
9363     }
9364 }
9365
9366 /* Add an enumerator to the pubnames section.  */
9367
9368 static void
9369 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9370 {
9371   pubname_entry e;
9372
9373   gcc_assert (scope_name);
9374   e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9375   e.die = die;
9376   vec_safe_push (pubname_table, e);
9377 }
9378
9379 /* Add a new entry to .debug_pubtypes if appropriate.  */
9380
9381 static void
9382 add_pubtype (tree decl, dw_die_ref die)
9383 {
9384   pubname_entry e;
9385
9386   if (!want_pubnames ())
9387     return;
9388
9389   if ((TREE_PUBLIC (decl)
9390        || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9391       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9392     {
9393       tree scope = NULL;
9394       const char *scope_name = "";
9395       const char *sep = is_cxx () ? "::" : ".";
9396       const char *name;
9397
9398       scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9399       if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9400         {
9401           scope_name = lang_hooks.dwarf_name (scope, 1);
9402           if (scope_name != NULL && scope_name[0] != '\0')
9403             scope_name = concat (scope_name, sep, NULL);
9404           else
9405             scope_name = "";
9406         }
9407
9408       if (TYPE_P (decl))
9409         name = type_tag (decl);
9410       else
9411         name = lang_hooks.dwarf_name (decl, 1);
9412
9413       /* If we don't have a name for the type, there's no point in adding
9414          it to the table.  */
9415       if (name != NULL && name[0] != '\0')
9416         {
9417           e.die = die;
9418           e.name = concat (scope_name, name, NULL);
9419           vec_safe_push (pubtype_table, e);
9420         }
9421
9422       /* Although it might be more consistent to add the pubinfo for the
9423          enumerators as their dies are created, they should only be added if the
9424          enum type meets the criteria above.  So rather than re-check the parent
9425          enum type whenever an enumerator die is created, just output them all
9426          here.  This isn't protected by the name conditional because anonymous
9427          enums don't have names.  */
9428       if (die->die_tag == DW_TAG_enumeration_type)
9429         {
9430           dw_die_ref c;
9431
9432           FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9433         }
9434     }
9435 }
9436
9437 /* Output a single entry in the pubnames table.  */
9438
9439 static void
9440 output_pubname (dw_offset die_offset, pubname_entry *entry)
9441 {
9442   dw_die_ref die = entry->die;
9443   int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9444
9445   dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9446
9447   if (debug_generate_pub_sections == 2)
9448     {
9449       /* This logic follows gdb's method for determining the value of the flag
9450          byte.  */
9451       uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9452       switch (die->die_tag)
9453       {
9454         case DW_TAG_typedef:
9455         case DW_TAG_base_type:
9456         case DW_TAG_subrange_type:
9457           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9458           GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9459           break;
9460         case DW_TAG_enumerator:
9461           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9462                                           GDB_INDEX_SYMBOL_KIND_VARIABLE);
9463           if (!is_cxx () && !is_java ())
9464             GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9465           break;
9466         case DW_TAG_subprogram:
9467           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9468                                           GDB_INDEX_SYMBOL_KIND_FUNCTION);
9469           if (!is_ada ())
9470             GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9471           break;
9472         case DW_TAG_constant:
9473           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9474                                           GDB_INDEX_SYMBOL_KIND_VARIABLE);
9475           GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9476           break;
9477         case DW_TAG_variable:
9478           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9479                                           GDB_INDEX_SYMBOL_KIND_VARIABLE);
9480           GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9481           break;
9482         case DW_TAG_namespace:
9483         case DW_TAG_imported_declaration:
9484           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9485           break;
9486         case DW_TAG_class_type:
9487         case DW_TAG_interface_type:
9488         case DW_TAG_structure_type:
9489         case DW_TAG_union_type:
9490         case DW_TAG_enumeration_type:
9491           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9492           if (!is_cxx () && !is_java ())
9493             GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9494           break;
9495         default:
9496           /* An unusual tag.  Leave the flag-byte empty.  */
9497           break;
9498       }
9499       dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9500                            "GDB-index flags");
9501     }
9502
9503   dw2_asm_output_nstring (entry->name, -1, "external name");
9504 }
9505
9506
9507 /* Output the public names table used to speed up access to externally
9508    visible names; or the public types table used to find type definitions.  */
9509
9510 static void
9511 output_pubnames (vec<pubname_entry, va_gc> *names)
9512 {
9513   unsigned i;
9514   unsigned long pubnames_length = size_of_pubnames (names);
9515   pubname_ref pub;
9516
9517   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9518     dw2_asm_output_data (4, 0xffffffff,
9519       "Initial length escape value indicating 64-bit DWARF extension");
9520   dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length, "Pub Info Length");
9521
9522   /* Version number for pubnames/pubtypes is independent of dwarf version.  */
9523   dw2_asm_output_data (2, 2, "DWARF Version");
9524
9525   if (dwarf_split_debug_info)
9526     dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9527                            debug_skeleton_info_section,
9528                            "Offset of Compilation Unit Info");
9529   else
9530     dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9531                            debug_info_section,
9532                            "Offset of Compilation Unit Info");
9533   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9534                        "Compilation Unit Length");
9535
9536   FOR_EACH_VEC_ELT (*names, i, pub)
9537     {
9538       if (include_pubname_in_output (names, pub))
9539         {
9540           dw_offset die_offset = pub->die->die_offset;
9541
9542           /* We shouldn't see pubnames for DIEs outside of the main CU.  */
9543           if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9544             gcc_assert (pub->die->die_mark);
9545
9546           /* If we're putting types in their own .debug_types sections,
9547              the .debug_pubtypes table will still point to the compile
9548              unit (not the type unit), so we want to use the offset of
9549              the skeleton DIE (if there is one).  */
9550           if (pub->die->comdat_type_p && names == pubtype_table)
9551             {
9552               comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9553
9554               if (type_node != NULL)
9555                 die_offset = (type_node->skeleton_die != NULL
9556                               ? type_node->skeleton_die->die_offset
9557                               : comp_unit_die ()->die_offset);
9558             }
9559
9560           output_pubname (die_offset, pub);
9561         }
9562     }
9563
9564   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9565 }
9566
9567 /* Output public names and types tables if necessary.  */
9568
9569 static void
9570 output_pubtables (void)
9571 {
9572   if (!want_pubnames () || !info_section_emitted)
9573     return;
9574
9575   switch_to_section (debug_pubnames_section);
9576   output_pubnames (pubname_table);
9577   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
9578      It shouldn't hurt to emit it always, since pure DWARF2 consumers
9579      simply won't look for the section.  */
9580   switch_to_section (debug_pubtypes_section);
9581   output_pubnames (pubtype_table);
9582 }
9583
9584
9585 /* Output the information that goes into the .debug_aranges table.
9586    Namely, define the beginning and ending address range of the
9587    text section generated for this compilation unit.  */
9588
9589 static void
9590 output_aranges (unsigned long aranges_length)
9591 {
9592   unsigned i;
9593
9594   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9595     dw2_asm_output_data (4, 0xffffffff,
9596       "Initial length escape value indicating 64-bit DWARF extension");
9597   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9598                        "Length of Address Ranges Info");
9599   /* Version number for aranges is still 2, even up to DWARF5.  */
9600   dw2_asm_output_data (2, 2, "DWARF Version");
9601   if (dwarf_split_debug_info)
9602     dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9603                            debug_skeleton_info_section,
9604                            "Offset of Compilation Unit Info");
9605   else
9606     dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9607                            debug_info_section,
9608                            "Offset of Compilation Unit Info");
9609   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9610   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9611
9612   /* We need to align to twice the pointer size here.  */
9613   if (DWARF_ARANGES_PAD_SIZE)
9614     {
9615       /* Pad using a 2 byte words so that padding is correct for any
9616          pointer size.  */
9617       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9618                            2 * DWARF2_ADDR_SIZE);
9619       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9620         dw2_asm_output_data (2, 0, NULL);
9621     }
9622
9623   /* It is necessary not to output these entries if the sections were
9624      not used; if the sections were not used, the length will be 0 and
9625      the address may end up as 0 if the section is discarded by ld
9626      --gc-sections, leaving an invalid (0, 0) entry that can be
9627      confused with the terminator.  */
9628   if (text_section_used)
9629     {
9630       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9631       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9632                             text_section_label, "Length");
9633     }
9634   if (cold_text_section_used)
9635     {
9636       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9637                            "Address");
9638       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9639                             cold_text_section_label, "Length");
9640     }
9641
9642   if (have_multiple_function_sections)
9643     {
9644       unsigned fde_idx;
9645       dw_fde_ref fde;
9646
9647       FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9648         {
9649           if (DECL_IGNORED_P (fde->decl))
9650             continue;
9651           if (!fde->in_std_section)
9652             {
9653               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9654                                    "Address");
9655               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9656                                     fde->dw_fde_begin, "Length");
9657             }
9658           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9659             {
9660               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9661                                    "Address");
9662               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9663                                     fde->dw_fde_second_begin, "Length");
9664             }
9665         }
9666     }
9667
9668   /* Output the terminator words.  */
9669   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9670   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9671 }
9672
9673 /* Add a new entry to .debug_ranges.  Return the offset at which it
9674    was placed.  */
9675
9676 static unsigned int
9677 add_ranges_num (int num)
9678 {
9679   unsigned int in_use = ranges_table_in_use;
9680
9681   if (in_use == ranges_table_allocated)
9682     {
9683       ranges_table_allocated += RANGES_TABLE_INCREMENT;
9684       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9685                                     ranges_table_allocated);
9686       memset (ranges_table + ranges_table_in_use, 0,
9687               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9688     }
9689
9690   ranges_table[in_use].num = num;
9691   ranges_table_in_use = in_use + 1;
9692
9693   return in_use * 2 * DWARF2_ADDR_SIZE;
9694 }
9695
9696 /* Add a new entry to .debug_ranges corresponding to a block, or a
9697    range terminator if BLOCK is NULL.  */
9698
9699 static unsigned int
9700 add_ranges (const_tree block)
9701 {
9702   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9703 }
9704
9705 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9706    When using dwarf_split_debug_info, address attributes in dies destined
9707    for the final executable should be direct references--setting the
9708    parameter force_direct ensures this behavior.  */
9709
9710 static void
9711 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9712                       bool *added, bool force_direct)
9713 {
9714   unsigned int in_use = ranges_by_label_in_use;
9715   unsigned int offset;
9716
9717   if (in_use == ranges_by_label_allocated)
9718     {
9719       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9720       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9721                                        ranges_by_label,
9722                                        ranges_by_label_allocated);
9723       memset (ranges_by_label + ranges_by_label_in_use, 0,
9724               RANGES_TABLE_INCREMENT
9725               * sizeof (struct dw_ranges_by_label_struct));
9726     }
9727
9728   ranges_by_label[in_use].begin = begin;
9729   ranges_by_label[in_use].end = end;
9730   ranges_by_label_in_use = in_use + 1;
9731
9732   offset = add_ranges_num (-(int)in_use - 1);
9733   if (!*added)
9734     {
9735       add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9736       *added = true;
9737     }
9738 }
9739
9740 static void
9741 output_ranges (void)
9742 {
9743   unsigned i;
9744   static const char *const start_fmt = "Offset %#x";
9745   const char *fmt = start_fmt;
9746
9747   for (i = 0; i < ranges_table_in_use; i++)
9748     {
9749       int block_num = ranges_table[i].num;
9750
9751       if (block_num > 0)
9752         {
9753           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9754           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9755
9756           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9757           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9758
9759           /* If all code is in the text section, then the compilation
9760              unit base address defaults to DW_AT_low_pc, which is the
9761              base of the text section.  */
9762           if (!have_multiple_function_sections)
9763             {
9764               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9765                                     text_section_label,
9766                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
9767               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9768                                     text_section_label, NULL);
9769             }
9770
9771           /* Otherwise, the compilation unit base address is zero,
9772              which allows us to use absolute addresses, and not worry
9773              about whether the target supports cross-section
9774              arithmetic.  */
9775           else
9776             {
9777               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9778                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
9779               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9780             }
9781
9782           fmt = NULL;
9783         }
9784
9785       /* Negative block_num stands for an index into ranges_by_label.  */
9786       else if (block_num < 0)
9787         {
9788           int lab_idx = - block_num - 1;
9789
9790           if (!have_multiple_function_sections)
9791             {
9792               gcc_unreachable ();
9793 #if 0
9794               /* If we ever use add_ranges_by_labels () for a single
9795                  function section, all we have to do is to take out
9796                  the #if 0 above.  */
9797               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9798                                     ranges_by_label[lab_idx].begin,
9799                                     text_section_label,
9800                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
9801               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9802                                     ranges_by_label[lab_idx].end,
9803                                     text_section_label, NULL);
9804 #endif
9805             }
9806           else
9807             {
9808               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9809                                    ranges_by_label[lab_idx].begin,
9810                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
9811               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9812                                    ranges_by_label[lab_idx].end,
9813                                    NULL);
9814             }
9815         }
9816       else
9817         {
9818           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9819           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9820           fmt = start_fmt;
9821         }
9822     }
9823 }
9824
9825 /* Data structure containing information about input files.  */
9826 struct file_info
9827 {
9828   const char *path;     /* Complete file name.  */
9829   const char *fname;    /* File name part.  */
9830   int length;           /* Length of entire string.  */
9831   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
9832   int dir_idx;          /* Index in directory table.  */
9833 };
9834
9835 /* Data structure containing information about directories with source
9836    files.  */
9837 struct dir_info
9838 {
9839   const char *path;     /* Path including directory name.  */
9840   int length;           /* Path length.  */
9841   int prefix;           /* Index of directory entry which is a prefix.  */
9842   int count;            /* Number of files in this directory.  */
9843   int dir_idx;          /* Index of directory used as base.  */
9844 };
9845
9846 /* Callback function for file_info comparison.  We sort by looking at
9847    the directories in the path.  */
9848
9849 static int
9850 file_info_cmp (const void *p1, const void *p2)
9851 {
9852   const struct file_info *const s1 = (const struct file_info *) p1;
9853   const struct file_info *const s2 = (const struct file_info *) p2;
9854   const unsigned char *cp1;
9855   const unsigned char *cp2;
9856
9857   /* Take care of file names without directories.  We need to make sure that
9858      we return consistent values to qsort since some will get confused if
9859      we return the same value when identical operands are passed in opposite
9860      orders.  So if neither has a directory, return 0 and otherwise return
9861      1 or -1 depending on which one has the directory.  */
9862   if ((s1->path == s1->fname || s2->path == s2->fname))
9863     return (s2->path == s2->fname) - (s1->path == s1->fname);
9864
9865   cp1 = (const unsigned char *) s1->path;
9866   cp2 = (const unsigned char *) s2->path;
9867
9868   while (1)
9869     {
9870       ++cp1;
9871       ++cp2;
9872       /* Reached the end of the first path?  If so, handle like above.  */
9873       if ((cp1 == (const unsigned char *) s1->fname)
9874           || (cp2 == (const unsigned char *) s2->fname))
9875         return ((cp2 == (const unsigned char *) s2->fname)
9876                 - (cp1 == (const unsigned char *) s1->fname));
9877
9878       /* Character of current path component the same?  */
9879       else if (*cp1 != *cp2)
9880         return *cp1 - *cp2;
9881     }
9882 }
9883
9884 struct file_name_acquire_data
9885 {
9886   struct file_info *files;
9887   int used_files;
9888   int max_files;
9889 };
9890
9891 /* Traversal function for the hash table.  */
9892
9893 int
9894 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
9895 {
9896   struct dwarf_file_data *d = *slot;
9897   struct file_info *fi;
9898   const char *f;
9899
9900   gcc_assert (fnad->max_files >= d->emitted_number);
9901
9902   if (! d->emitted_number)
9903     return 1;
9904
9905   gcc_assert (fnad->max_files != fnad->used_files);
9906
9907   fi = fnad->files + fnad->used_files++;
9908
9909   /* Skip all leading "./".  */
9910   f = d->filename;
9911   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9912     f += 2;
9913
9914   /* Create a new array entry.  */
9915   fi->path = f;
9916   fi->length = strlen (f);
9917   fi->file_idx = d;
9918
9919   /* Search for the file name part.  */
9920   f = strrchr (f, DIR_SEPARATOR);
9921 #if defined (DIR_SEPARATOR_2)
9922   {
9923     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9924
9925     if (g != NULL)
9926       {
9927         if (f == NULL || f < g)
9928           f = g;
9929       }
9930   }
9931 #endif
9932
9933   fi->fname = f == NULL ? fi->path : f + 1;
9934   return 1;
9935 }
9936
9937 /* Output the directory table and the file name table.  We try to minimize
9938    the total amount of memory needed.  A heuristic is used to avoid large
9939    slowdowns with many input files.  */
9940
9941 static void
9942 output_file_names (void)
9943 {
9944   struct file_name_acquire_data fnad;
9945   int numfiles;
9946   struct file_info *files;
9947   struct dir_info *dirs;
9948   int *saved;
9949   int *savehere;
9950   int *backmap;
9951   int ndirs;
9952   int idx_offset;
9953   int i;
9954
9955   if (!last_emitted_file)
9956     {
9957       dw2_asm_output_data (1, 0, "End directory table");
9958       dw2_asm_output_data (1, 0, "End file name table");
9959       return;
9960     }
9961
9962   numfiles = last_emitted_file->emitted_number;
9963
9964   /* Allocate the various arrays we need.  */
9965   files = XALLOCAVEC (struct file_info, numfiles);
9966   dirs = XALLOCAVEC (struct dir_info, numfiles);
9967
9968   fnad.files = files;
9969   fnad.used_files = 0;
9970   fnad.max_files = numfiles;
9971   file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
9972   gcc_assert (fnad.used_files == fnad.max_files);
9973
9974   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9975
9976   /* Find all the different directories used.  */
9977   dirs[0].path = files[0].path;
9978   dirs[0].length = files[0].fname - files[0].path;
9979   dirs[0].prefix = -1;
9980   dirs[0].count = 1;
9981   dirs[0].dir_idx = 0;
9982   files[0].dir_idx = 0;
9983   ndirs = 1;
9984
9985   for (i = 1; i < numfiles; i++)
9986     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9987         && memcmp (dirs[ndirs - 1].path, files[i].path,
9988                    dirs[ndirs - 1].length) == 0)
9989       {
9990         /* Same directory as last entry.  */
9991         files[i].dir_idx = ndirs - 1;
9992         ++dirs[ndirs - 1].count;
9993       }
9994     else
9995       {
9996         int j;
9997
9998         /* This is a new directory.  */
9999         dirs[ndirs].path = files[i].path;
10000         dirs[ndirs].length = files[i].fname - files[i].path;
10001         dirs[ndirs].count = 1;
10002         dirs[ndirs].dir_idx = ndirs;
10003         files[i].dir_idx = ndirs;
10004
10005         /* Search for a prefix.  */
10006         dirs[ndirs].prefix = -1;
10007         for (j = 0; j < ndirs; j++)
10008           if (dirs[j].length < dirs[ndirs].length
10009               && dirs[j].length > 1
10010               && (dirs[ndirs].prefix == -1
10011                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
10012               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
10013             dirs[ndirs].prefix = j;
10014
10015         ++ndirs;
10016       }
10017
10018   /* Now to the actual work.  We have to find a subset of the directories which
10019      allow expressing the file name using references to the directory table
10020      with the least amount of characters.  We do not do an exhaustive search
10021      where we would have to check out every combination of every single
10022      possible prefix.  Instead we use a heuristic which provides nearly optimal
10023      results in most cases and never is much off.  */
10024   saved = XALLOCAVEC (int, ndirs);
10025   savehere = XALLOCAVEC (int, ndirs);
10026
10027   memset (saved, '\0', ndirs * sizeof (saved[0]));
10028   for (i = 0; i < ndirs; i++)
10029     {
10030       int j;
10031       int total;
10032
10033       /* We can always save some space for the current directory.  But this
10034          does not mean it will be enough to justify adding the directory.  */
10035       savehere[i] = dirs[i].length;
10036       total = (savehere[i] - saved[i]) * dirs[i].count;
10037
10038       for (j = i + 1; j < ndirs; j++)
10039         {
10040           savehere[j] = 0;
10041           if (saved[j] < dirs[i].length)
10042             {
10043               /* Determine whether the dirs[i] path is a prefix of the
10044                  dirs[j] path.  */
10045               int k;
10046
10047               k = dirs[j].prefix;
10048               while (k != -1 && k != (int) i)
10049                 k = dirs[k].prefix;
10050
10051               if (k == (int) i)
10052                 {
10053                   /* Yes it is.  We can possibly save some memory by
10054                      writing the filenames in dirs[j] relative to
10055                      dirs[i].  */
10056                   savehere[j] = dirs[i].length;
10057                   total += (savehere[j] - saved[j]) * dirs[j].count;
10058                 }
10059             }
10060         }
10061
10062       /* Check whether we can save enough to justify adding the dirs[i]
10063          directory.  */
10064       if (total > dirs[i].length + 1)
10065         {
10066           /* It's worthwhile adding.  */
10067           for (j = i; j < ndirs; j++)
10068             if (savehere[j] > 0)
10069               {
10070                 /* Remember how much we saved for this directory so far.  */
10071                 saved[j] = savehere[j];
10072
10073                 /* Remember the prefix directory.  */
10074                 dirs[j].dir_idx = i;
10075               }
10076         }
10077     }
10078
10079   /* Emit the directory name table.  */
10080   idx_offset = dirs[0].length > 0 ? 1 : 0;
10081   for (i = 1 - idx_offset; i < ndirs; i++)
10082     dw2_asm_output_nstring (dirs[i].path,
10083                             dirs[i].length
10084                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
10085                             "Directory Entry: %#x", i + idx_offset);
10086
10087   dw2_asm_output_data (1, 0, "End directory table");
10088
10089   /* We have to emit them in the order of emitted_number since that's
10090      used in the debug info generation.  To do this efficiently we
10091      generate a back-mapping of the indices first.  */
10092   backmap = XALLOCAVEC (int, numfiles);
10093   for (i = 0; i < numfiles; i++)
10094     backmap[files[i].file_idx->emitted_number - 1] = i;
10095
10096   /* Now write all the file names.  */
10097   for (i = 0; i < numfiles; i++)
10098     {
10099       int file_idx = backmap[i];
10100       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
10101
10102 #ifdef VMS_DEBUGGING_INFO
10103 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
10104
10105       /* Setting these fields can lead to debugger miscomparisons,
10106          but VMS Debug requires them to be set correctly.  */
10107
10108       int ver;
10109       long long cdt;
10110       long siz;
10111       int maxfilelen = strlen (files[file_idx].path)
10112                                + dirs[dir_idx].length
10113                                + MAX_VMS_VERSION_LEN + 1;
10114       char *filebuf = XALLOCAVEC (char, maxfilelen);
10115
10116       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
10117       snprintf (filebuf, maxfilelen, "%s;%d",
10118                 files[file_idx].path + dirs[dir_idx].length, ver);
10119
10120       dw2_asm_output_nstring
10121         (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
10122
10123       /* Include directory index.  */
10124       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10125
10126       /* Modification time.  */
10127       dw2_asm_output_data_uleb128
10128         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
10129           ? cdt : 0,
10130          NULL);
10131
10132       /* File length in bytes.  */
10133       dw2_asm_output_data_uleb128
10134         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
10135           ? siz : 0,
10136          NULL);
10137 #else
10138       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
10139                               "File Entry: %#x", (unsigned) i + 1);
10140
10141       /* Include directory index.  */
10142       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10143
10144       /* Modification time.  */
10145       dw2_asm_output_data_uleb128 (0, NULL);
10146
10147       /* File length in bytes.  */
10148       dw2_asm_output_data_uleb128 (0, NULL);
10149 #endif /* VMS_DEBUGGING_INFO */
10150     }
10151
10152   dw2_asm_output_data (1, 0, "End file name table");
10153 }
10154
10155
10156 /* Output one line number table into the .debug_line section.  */
10157
10158 static void
10159 output_one_line_info_table (dw_line_info_table *table)
10160 {
10161   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
10162   unsigned int current_line = 1;
10163   bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
10164   dw_line_info_entry *ent;
10165   size_t i;
10166
10167   FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
10168     {
10169       switch (ent->opcode)
10170         {
10171         case LI_set_address:
10172           /* ??? Unfortunately, we have little choice here currently, and
10173              must always use the most general form.  GCC does not know the
10174              address delta itself, so we can't use DW_LNS_advance_pc.  Many
10175              ports do have length attributes which will give an upper bound
10176              on the address range.  We could perhaps use length attributes
10177              to determine when it is safe to use DW_LNS_fixed_advance_pc.  */
10178           ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
10179
10180           /* This can handle any delta.  This takes
10181              4+DWARF2_ADDR_SIZE bytes.  */
10182           dw2_asm_output_data (1, 0, "set address %s", line_label);
10183           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10184           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10185           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10186           break;
10187
10188         case LI_set_line:
10189           if (ent->val == current_line)
10190             {
10191               /* We still need to start a new row, so output a copy insn.  */
10192               dw2_asm_output_data (1, DW_LNS_copy,
10193                                    "copy line %u", current_line);
10194             }
10195           else
10196             {
10197               int line_offset = ent->val - current_line;
10198               int line_delta = line_offset - DWARF_LINE_BASE;
10199
10200               current_line = ent->val;
10201               if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10202                 {
10203                   /* This can handle deltas from -10 to 234, using the current
10204                      definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
10205                      This takes 1 byte.  */
10206                   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10207                                        "line %u", current_line);
10208                 }
10209               else
10210                 {
10211                   /* This can handle any delta.  This takes at least 4 bytes,
10212                      depending on the value being encoded.  */
10213                   dw2_asm_output_data (1, DW_LNS_advance_line,
10214                                        "advance to line %u", current_line);
10215                   dw2_asm_output_data_sleb128 (line_offset, NULL);
10216                   dw2_asm_output_data (1, DW_LNS_copy, NULL);
10217                 }
10218             }
10219           break;
10220
10221         case LI_set_file:
10222           dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
10223           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10224           break;
10225
10226         case LI_set_column:
10227           dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
10228           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10229           break;
10230
10231         case LI_negate_stmt:
10232           current_is_stmt = !current_is_stmt;
10233           dw2_asm_output_data (1, DW_LNS_negate_stmt,
10234                                "is_stmt %d", current_is_stmt);
10235           break;
10236
10237         case LI_set_prologue_end:
10238           dw2_asm_output_data (1, DW_LNS_set_prologue_end,
10239                                "set prologue end");
10240           break;
10241           
10242         case LI_set_epilogue_begin:
10243           dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
10244                                "set epilogue begin");
10245           break;
10246
10247         case LI_set_discriminator:
10248           dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
10249           dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
10250           dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
10251           dw2_asm_output_data_uleb128 (ent->val, NULL);
10252           break;
10253         }
10254     }
10255
10256   /* Emit debug info for the address of the end of the table.  */
10257   dw2_asm_output_data (1, 0, "set address %s", table->end_label);
10258   dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10259   dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10260   dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10261
10262   dw2_asm_output_data (1, 0, "end sequence");
10263   dw2_asm_output_data_uleb128 (1, NULL);
10264   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10265 }
10266
10267 /* Output the source line number correspondence information.  This
10268    information goes into the .debug_line section.  */
10269
10270 static void
10271 output_line_info (bool prologue_only)
10272 {
10273   char l1[20], l2[20], p1[20], p2[20];
10274   /* We don't support DWARFv5 line tables yet.  */
10275   int ver = dwarf_version < 5 ? dwarf_version : 4;
10276   bool saw_one = false;
10277   int opc;
10278
10279   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10280   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10281   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10282   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10283
10284   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10285     dw2_asm_output_data (4, 0xffffffff,
10286       "Initial length escape value indicating 64-bit DWARF extension");
10287   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10288                         "Length of Source Line Info");
10289   ASM_OUTPUT_LABEL (asm_out_file, l1);
10290
10291   dw2_asm_output_data (2, ver, "DWARF Version");
10292   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10293   ASM_OUTPUT_LABEL (asm_out_file, p1);
10294
10295   /* Define the architecture-dependent minimum instruction length (in bytes).
10296      In this implementation of DWARF, this field is used for information
10297      purposes only.  Since GCC generates assembly language, we have no
10298      a priori knowledge of how many instruction bytes are generated for each
10299      source line, and therefore can use only the DW_LNE_set_address and
10300      DW_LNS_fixed_advance_pc line information commands.  Accordingly, we fix
10301      this as '1', which is "correct enough" for all architectures,
10302      and don't let the target override.  */
10303   dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10304
10305   if (ver >= 4)
10306     dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10307                          "Maximum Operations Per Instruction");
10308   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10309                        "Default is_stmt_start flag");
10310   dw2_asm_output_data (1, DWARF_LINE_BASE,
10311                        "Line Base Value (Special Opcodes)");
10312   dw2_asm_output_data (1, DWARF_LINE_RANGE,
10313                        "Line Range Value (Special Opcodes)");
10314   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10315                        "Special Opcode Base");
10316
10317   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10318     {
10319       int n_op_args;
10320       switch (opc)
10321         {
10322         case DW_LNS_advance_pc:
10323         case DW_LNS_advance_line:
10324         case DW_LNS_set_file:
10325         case DW_LNS_set_column:
10326         case DW_LNS_fixed_advance_pc:
10327         case DW_LNS_set_isa:
10328           n_op_args = 1;
10329           break;
10330         default:
10331           n_op_args = 0;
10332           break;
10333         }
10334
10335       dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10336                            opc, n_op_args);
10337     }
10338
10339   /* Write out the information about the files we use.  */
10340   output_file_names ();
10341   ASM_OUTPUT_LABEL (asm_out_file, p2);
10342   if (prologue_only)
10343     {
10344       /* Output the marker for the end of the line number info.  */
10345       ASM_OUTPUT_LABEL (asm_out_file, l2);
10346       return;
10347     }
10348
10349   if (separate_line_info)
10350     {
10351       dw_line_info_table *table;
10352       size_t i;
10353
10354       FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10355         if (table->in_use)
10356           {
10357             output_one_line_info_table (table);
10358             saw_one = true;
10359           }
10360     }
10361   if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10362     {
10363       output_one_line_info_table (cold_text_section_line_info);
10364       saw_one = true;
10365     }
10366
10367   /* ??? Some Darwin linkers crash on a .debug_line section with no
10368      sequences.  Further, merely a DW_LNE_end_sequence entry is not
10369      sufficient -- the address column must also be initialized.
10370      Make sure to output at least one set_address/end_sequence pair,
10371      choosing .text since that section is always present.  */
10372   if (text_section_line_info->in_use || !saw_one)
10373     output_one_line_info_table (text_section_line_info);
10374
10375   /* Output the marker for the end of the line number info.  */
10376   ASM_OUTPUT_LABEL (asm_out_file, l2);
10377 }
10378 \f
10379 /* Given a pointer to a tree node for some base type, return a pointer to
10380    a DIE that describes the given type.
10381
10382    This routine must only be called for GCC type nodes that correspond to
10383    Dwarf base (fundamental) types.  */
10384
10385 static dw_die_ref
10386 base_type_die (tree type)
10387 {
10388   dw_die_ref base_type_result;
10389   enum dwarf_type encoding;
10390
10391   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10392     return 0;
10393
10394   /* If this is a subtype that should not be emitted as a subrange type,
10395      use the base type.  See subrange_type_for_debug_p.  */
10396   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10397     type = TREE_TYPE (type);
10398
10399   switch (TREE_CODE (type))
10400     {
10401     case INTEGER_TYPE:
10402       if ((dwarf_version >= 4 || !dwarf_strict)
10403           && TYPE_NAME (type)
10404           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10405           && DECL_IS_BUILTIN (TYPE_NAME (type))
10406           && DECL_NAME (TYPE_NAME (type)))
10407         {
10408           const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10409           if (strcmp (name, "char16_t") == 0
10410               || strcmp (name, "char32_t") == 0)
10411             {
10412               encoding = DW_ATE_UTF;
10413               break;
10414             }
10415         }
10416       if (TYPE_STRING_FLAG (type))
10417         {
10418           if (TYPE_UNSIGNED (type))
10419             encoding = DW_ATE_unsigned_char;
10420           else
10421             encoding = DW_ATE_signed_char;
10422         }
10423       else if (TYPE_UNSIGNED (type))
10424         encoding = DW_ATE_unsigned;
10425       else
10426         encoding = DW_ATE_signed;
10427       break;
10428
10429     case REAL_TYPE:
10430       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10431         {
10432           if (dwarf_version >= 3 || !dwarf_strict)
10433             encoding = DW_ATE_decimal_float;
10434           else
10435             encoding = DW_ATE_lo_user;
10436         }
10437       else
10438         encoding = DW_ATE_float;
10439       break;
10440
10441     case FIXED_POINT_TYPE:
10442       if (!(dwarf_version >= 3 || !dwarf_strict))
10443         encoding = DW_ATE_lo_user;
10444       else if (TYPE_UNSIGNED (type))
10445         encoding = DW_ATE_unsigned_fixed;
10446       else
10447         encoding = DW_ATE_signed_fixed;
10448       break;
10449
10450       /* Dwarf2 doesn't know anything about complex ints, so use
10451          a user defined type for it.  */
10452     case COMPLEX_TYPE:
10453       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10454         encoding = DW_ATE_complex_float;
10455       else
10456         encoding = DW_ATE_lo_user;
10457       break;
10458
10459     case BOOLEAN_TYPE:
10460       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
10461       encoding = DW_ATE_boolean;
10462       break;
10463
10464     default:
10465       /* No other TREE_CODEs are Dwarf fundamental types.  */
10466       gcc_unreachable ();
10467     }
10468
10469   base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10470
10471   add_AT_unsigned (base_type_result, DW_AT_byte_size,
10472                    int_size_in_bytes (type));
10473   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10474   add_pubtype (type, base_type_result);
10475
10476   return base_type_result;
10477 }
10478
10479 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
10480    named 'auto' in its type: return true for it, false otherwise.  */
10481
10482 static inline bool
10483 is_cxx_auto (tree type)
10484 {
10485   if (is_cxx ())
10486     {
10487       tree name = TYPE_IDENTIFIER (type);
10488       if (name == get_identifier ("auto")
10489           || name == get_identifier ("decltype(auto)"))
10490         return true;
10491     }
10492   return false;
10493 }
10494
10495 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10496    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
10497
10498 static inline int
10499 is_base_type (tree type)
10500 {
10501   switch (TREE_CODE (type))
10502     {
10503     case ERROR_MARK:
10504     case VOID_TYPE:
10505     case INTEGER_TYPE:
10506     case REAL_TYPE:
10507     case FIXED_POINT_TYPE:
10508     case COMPLEX_TYPE:
10509     case BOOLEAN_TYPE:
10510     case POINTER_BOUNDS_TYPE:
10511       return 1;
10512
10513     case ARRAY_TYPE:
10514     case RECORD_TYPE:
10515     case UNION_TYPE:
10516     case QUAL_UNION_TYPE:
10517     case ENUMERAL_TYPE:
10518     case FUNCTION_TYPE:
10519     case METHOD_TYPE:
10520     case POINTER_TYPE:
10521     case REFERENCE_TYPE:
10522     case NULLPTR_TYPE:
10523     case OFFSET_TYPE:
10524     case LANG_TYPE:
10525     case VECTOR_TYPE:
10526       return 0;
10527
10528     default:
10529       if (is_cxx_auto (type))
10530         return 0;
10531       gcc_unreachable ();
10532     }
10533
10534   return 0;
10535 }
10536
10537 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10538    node, return the size in bits for the type if it is a constant, or else
10539    return the alignment for the type if the type's size is not constant, or
10540    else return BITS_PER_WORD if the type actually turns out to be an
10541    ERROR_MARK node.  */
10542
10543 static inline unsigned HOST_WIDE_INT
10544 simple_type_size_in_bits (const_tree type)
10545 {
10546   if (TREE_CODE (type) == ERROR_MARK)
10547     return BITS_PER_WORD;
10548   else if (TYPE_SIZE (type) == NULL_TREE)
10549     return 0;
10550   else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
10551     return tree_to_uhwi (TYPE_SIZE (type));
10552   else
10553     return TYPE_ALIGN (type);
10554 }
10555
10556 /* Similarly, but return an offset_int instead of UHWI.  */
10557
10558 static inline offset_int
10559 offset_int_type_size_in_bits (const_tree type)
10560 {
10561   if (TREE_CODE (type) == ERROR_MARK)
10562     return BITS_PER_WORD;
10563   else if (TYPE_SIZE (type) == NULL_TREE)
10564     return 0;
10565   else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10566     return wi::to_offset (TYPE_SIZE (type));
10567   else
10568     return TYPE_ALIGN (type);
10569 }
10570
10571 /*  Given a pointer to a tree node for a subrange type, return a pointer
10572     to a DIE that describes the given type.  */
10573
10574 static dw_die_ref
10575 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10576 {
10577   dw_die_ref subrange_die;
10578   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10579
10580   if (context_die == NULL)
10581     context_die = comp_unit_die ();
10582
10583   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10584
10585   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10586     {
10587       /* The size of the subrange type and its base type do not match,
10588          so we need to generate a size attribute for the subrange type.  */
10589       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10590     }
10591
10592   if (low)
10593     add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
10594   if (high)
10595     add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
10596
10597   return subrange_die;
10598 }
10599
10600 /* Returns the (const and/or volatile) cv_qualifiers associated with
10601    the decl node.  This will normally be augmented with the
10602    cv_qualifiers of the underlying type in add_type_attribute.  */
10603
10604 static int
10605 decl_quals (const_tree decl)
10606 {
10607   return ((TREE_READONLY (decl)
10608            ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
10609           | (TREE_THIS_VOLATILE (decl)
10610              ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
10611 }
10612
10613 /* Determine the TYPE whose qualifiers match the largest strict subset
10614    of the given TYPE_QUALS, and return its qualifiers.  Ignore all
10615    qualifiers outside QUAL_MASK.  */
10616
10617 static int
10618 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
10619 {
10620   tree t;
10621   int best_rank = 0, best_qual = 0, max_rank;
10622
10623   type_quals &= qual_mask;
10624   max_rank = popcount_hwi (type_quals) - 1;
10625
10626   for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
10627        t = TYPE_NEXT_VARIANT (t))
10628     {
10629       int q = TYPE_QUALS (t) & qual_mask;
10630
10631       if ((q & type_quals) == q && q != type_quals
10632           && check_base_type (t, type))
10633         {
10634           int rank = popcount_hwi (q);
10635
10636           if (rank > best_rank)
10637             {
10638               best_rank = rank;
10639               best_qual = q;
10640             }
10641         }
10642     }
10643
10644   return best_qual;
10645 }
10646
10647 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10648    entry that chains various modifiers in front of the given type.  */
10649
10650 static dw_die_ref
10651 modified_type_die (tree type, int cv_quals, dw_die_ref context_die)
10652 {
10653   enum tree_code code = TREE_CODE (type);
10654   dw_die_ref mod_type_die;
10655   dw_die_ref sub_die = NULL;
10656   tree item_type = NULL;
10657   tree qualified_type;
10658   tree name, low, high;
10659   dw_die_ref mod_scope;
10660   /* Only these cv-qualifiers are currently handled.  */
10661   const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
10662                             | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
10663
10664   if (code == ERROR_MARK)
10665     return NULL;
10666
10667   cv_quals &= cv_qual_mask;
10668
10669   /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
10670      tag modifier (and not an attribute) old consumers won't be able
10671      to handle it.  */
10672   if (dwarf_version < 3)
10673     cv_quals &= ~TYPE_QUAL_RESTRICT;
10674
10675   /* Likewise for DW_TAG_atomic_type for DWARFv5.  */
10676   if (dwarf_version < 5)
10677     cv_quals &= ~TYPE_QUAL_ATOMIC;
10678
10679   /* See if we already have the appropriately qualified variant of
10680      this type.  */
10681   qualified_type = get_qualified_type (type, cv_quals);
10682
10683   if (qualified_type == sizetype
10684       && TYPE_NAME (qualified_type)
10685       && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10686     {
10687       tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10688
10689       gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10690                            && TYPE_PRECISION (t)
10691                            == TYPE_PRECISION (qualified_type)
10692                            && TYPE_UNSIGNED (t)
10693                            == TYPE_UNSIGNED (qualified_type));
10694       qualified_type = t;
10695     }
10696
10697   /* If we do, then we can just use its DIE, if it exists.  */
10698   if (qualified_type)
10699     {
10700       mod_type_die = lookup_type_die (qualified_type);
10701       if (mod_type_die)
10702         return mod_type_die;
10703     }
10704
10705   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10706
10707   /* Handle C typedef types.  */
10708   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10709       && !DECL_ARTIFICIAL (name))
10710     {
10711       tree dtype = TREE_TYPE (name);
10712
10713       if (qualified_type == dtype)
10714         {
10715           /* For a named type, use the typedef.  */
10716           gen_type_die (qualified_type, context_die);
10717           return lookup_type_die (qualified_type);
10718         }
10719       else
10720         {
10721           int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
10722           dquals &= cv_qual_mask;
10723           if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
10724               || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
10725             /* cv-unqualified version of named type.  Just use
10726                the unnamed type to which it refers.  */
10727             return modified_type_die (DECL_ORIGINAL_TYPE (name),
10728                                       cv_quals, context_die);
10729           /* Else cv-qualified version of named type; fall through.  */
10730         }
10731     }
10732
10733   mod_scope = scope_die_for (type, context_die);
10734
10735   if (cv_quals)
10736     {
10737       struct qual_info { int q; enum dwarf_tag t; };
10738       static const struct qual_info qual_info[] =
10739         {
10740           { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type },
10741           { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
10742           { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
10743           { TYPE_QUAL_CONST, DW_TAG_const_type },
10744         };
10745       int sub_quals;
10746       unsigned i;
10747
10748       /* Determine a lesser qualified type that most closely matches
10749          this one.  Then generate DW_TAG_* entries for the remaining
10750          qualifiers.  */
10751       sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
10752                                                   cv_qual_mask);
10753       mod_type_die = modified_type_die (type, sub_quals, context_die);
10754
10755       for (i = 0; i < sizeof (qual_info) / sizeof (qual_info[0]); i++)
10756         if (qual_info[i].q & cv_quals & ~sub_quals)
10757           {
10758             dw_die_ref d = new_die (qual_info[i].t, mod_scope, type);
10759             if (mod_type_die)
10760               add_AT_die_ref (d, DW_AT_type, mod_type_die);
10761             mod_type_die = d;
10762           }
10763     }
10764   else if (code == POINTER_TYPE)
10765     {
10766       mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10767       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10768                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
10769       item_type = TREE_TYPE (type);
10770       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10771         add_AT_unsigned (mod_type_die, DW_AT_address_class,
10772                          TYPE_ADDR_SPACE (item_type));
10773     }
10774   else if (code == REFERENCE_TYPE)
10775     {
10776       if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10777         mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10778                                 type);
10779       else
10780         mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10781       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10782                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
10783       item_type = TREE_TYPE (type);
10784       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10785         add_AT_unsigned (mod_type_die, DW_AT_address_class,
10786                          TYPE_ADDR_SPACE (item_type));
10787     }
10788   else if (code == INTEGER_TYPE
10789            && TREE_TYPE (type) != NULL_TREE
10790            && subrange_type_for_debug_p (type, &low, &high))
10791     {
10792       mod_type_die = subrange_type_die (type, low, high, context_die);
10793       item_type = TREE_TYPE (type);
10794     }
10795   else if (is_base_type (type))
10796     mod_type_die = base_type_die (type);
10797   else
10798     {
10799       gen_type_die (type, context_die);
10800
10801       /* We have to get the type_main_variant here (and pass that to the
10802          `lookup_type_die' routine) because the ..._TYPE node we have
10803          might simply be a *copy* of some original type node (where the
10804          copy was created to help us keep track of typedef names) and
10805          that copy might have a different TYPE_UID from the original
10806          ..._TYPE node.  */
10807       if (TREE_CODE (type) != VECTOR_TYPE)
10808         return lookup_type_die (type_main_variant (type));
10809       else
10810         /* Vectors have the debugging information in the type,
10811            not the main variant.  */
10812         return lookup_type_die (type);
10813     }
10814
10815   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
10816      don't output a DW_TAG_typedef, since there isn't one in the
10817      user's program; just attach a DW_AT_name to the type.
10818      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10819      if the base type already has the same name.  */
10820   if (name
10821       && ((TREE_CODE (name) != TYPE_DECL
10822            && (qualified_type == TYPE_MAIN_VARIANT (type)
10823                || (cv_quals == TYPE_UNQUALIFIED)))
10824           || (TREE_CODE (name) == TYPE_DECL
10825               && TREE_TYPE (name) == qualified_type
10826               && DECL_NAME (name))))
10827     {
10828       if (TREE_CODE (name) == TYPE_DECL)
10829         /* Could just call add_name_and_src_coords_attributes here,
10830            but since this is a builtin type it doesn't have any
10831            useful source coordinates anyway.  */
10832         name = DECL_NAME (name);
10833       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10834     }
10835   /* This probably indicates a bug.  */
10836   else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10837     {
10838       name = TYPE_IDENTIFIER (type);
10839       add_name_attribute (mod_type_die,
10840                           name ? IDENTIFIER_POINTER (name) : "__unknown__");
10841     }
10842
10843   if (qualified_type)
10844     equate_type_number_to_die (qualified_type, mod_type_die);
10845
10846   if (item_type)
10847     /* We must do this after the equate_type_number_to_die call, in case
10848        this is a recursive type.  This ensures that the modified_type_die
10849        recursion will terminate even if the type is recursive.  Recursive
10850        types are possible in Ada.  */
10851     sub_die = modified_type_die (item_type,
10852                                  TYPE_QUALS_NO_ADDR_SPACE (item_type),
10853                                  context_die);
10854
10855   if (sub_die != NULL)
10856     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10857
10858   add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10859   if (TYPE_ARTIFICIAL (type))
10860     add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10861
10862   return mod_type_die;
10863 }
10864
10865 /* Generate DIEs for the generic parameters of T.
10866    T must be either a generic type or a generic function.
10867    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
10868
10869 static void
10870 gen_generic_params_dies (tree t)
10871 {
10872   tree parms, args;
10873   int parms_num, i;
10874   dw_die_ref die = NULL;
10875   int non_default;
10876
10877   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10878     return;
10879
10880   if (TYPE_P (t))
10881     die = lookup_type_die (t);
10882   else if (DECL_P (t))
10883     die = lookup_decl_die (t);
10884
10885   gcc_assert (die);
10886
10887   parms = lang_hooks.get_innermost_generic_parms (t);
10888   if (!parms)
10889     /* T has no generic parameter. It means T is neither a generic type
10890        or function. End of story.  */
10891     return;
10892
10893   parms_num = TREE_VEC_LENGTH (parms);
10894   args = lang_hooks.get_innermost_generic_args (t);
10895   if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
10896     non_default = int_cst_value (TREE_CHAIN (args));
10897   else
10898     non_default = TREE_VEC_LENGTH (args);
10899   for (i = 0; i < parms_num; i++)
10900     {
10901       tree parm, arg, arg_pack_elems;
10902       dw_die_ref parm_die;
10903
10904       parm = TREE_VEC_ELT (parms, i);
10905       arg = TREE_VEC_ELT (args, i);
10906       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10907       gcc_assert (parm && TREE_VALUE (parm) && arg);
10908
10909       if (parm && TREE_VALUE (parm) && arg)
10910         {
10911           /* If PARM represents a template parameter pack,
10912              emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10913              by DW_TAG_template_*_parameter DIEs for the argument
10914              pack elements of ARG. Note that ARG would then be
10915              an argument pack.  */
10916           if (arg_pack_elems)
10917             parm_die = template_parameter_pack_die (TREE_VALUE (parm),
10918                                                     arg_pack_elems,
10919                                                     die);
10920           else
10921             parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
10922                                               true /* emit name */, die);
10923           if (i >= non_default)
10924             add_AT_flag (parm_die, DW_AT_default_value, 1);
10925         }
10926     }
10927 }
10928
10929 /* Create and return a DIE for PARM which should be
10930    the representation of a generic type parameter.
10931    For instance, in the C++ front end, PARM would be a template parameter.
10932    ARG is the argument to PARM.
10933    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
10934    name of the PARM.
10935    PARENT_DIE is the parent DIE which the new created DIE should be added to,
10936    as a child node.  */
10937
10938 static dw_die_ref
10939 generic_parameter_die (tree parm, tree arg,
10940                        bool emit_name_p,
10941                        dw_die_ref parent_die)
10942 {
10943   dw_die_ref tmpl_die = NULL;
10944   const char *name = NULL;
10945
10946   if (!parm || !DECL_NAME (parm) || !arg)
10947     return NULL;
10948
10949   /* We support non-type generic parameters and arguments,
10950      type generic parameters and arguments, as well as
10951      generic generic parameters (a.k.a. template template parameters in C++)
10952      and arguments.  */
10953   if (TREE_CODE (parm) == PARM_DECL)
10954     /* PARM is a nontype generic parameter  */
10955     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10956   else if (TREE_CODE (parm) == TYPE_DECL)
10957     /* PARM is a type generic parameter.  */
10958     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10959   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10960     /* PARM is a generic generic parameter.
10961        Its DIE is a GNU extension. It shall have a
10962        DW_AT_name attribute to represent the name of the template template
10963        parameter, and a DW_AT_GNU_template_name attribute to represent the
10964        name of the template template argument.  */
10965     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10966                         parent_die, parm);
10967   else
10968     gcc_unreachable ();
10969
10970   if (tmpl_die)
10971     {
10972       tree tmpl_type;
10973
10974       /* If PARM is a generic parameter pack, it means we are
10975          emitting debug info for a template argument pack element.
10976          In other terms, ARG is a template argument pack element.
10977          In that case, we don't emit any DW_AT_name attribute for
10978          the die.  */
10979       if (emit_name_p)
10980         {
10981           name = IDENTIFIER_POINTER (DECL_NAME (parm));
10982           gcc_assert (name);
10983           add_AT_string (tmpl_die, DW_AT_name, name);
10984         }
10985
10986       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10987         {
10988           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10989              TMPL_DIE should have a child DW_AT_type attribute that is set
10990              to the type of the argument to PARM, which is ARG.
10991              If PARM is a type generic parameter, TMPL_DIE should have a
10992              child DW_AT_type that is set to ARG.  */
10993           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10994           add_type_attribute (tmpl_die, tmpl_type,
10995                               (TREE_THIS_VOLATILE (tmpl_type)
10996                                ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
10997                               parent_die);
10998         }
10999       else
11000         {
11001           /* So TMPL_DIE is a DIE representing a
11002              a generic generic template parameter, a.k.a template template
11003              parameter in C++ and arg is a template.  */
11004
11005           /* The DW_AT_GNU_template_name attribute of the DIE must be set
11006              to the name of the argument.  */
11007           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
11008           if (name)
11009             add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
11010         }
11011
11012       if (TREE_CODE (parm) == PARM_DECL)
11013         /* So PARM is a non-type generic parameter.
11014            DWARF3 5.6.8 says we must set a DW_AT_const_value child
11015            attribute of TMPL_DIE which value represents the value
11016            of ARG.
11017            We must be careful here:
11018            The value of ARG might reference some function decls.
11019            We might currently be emitting debug info for a generic
11020            type and types are emitted before function decls, we don't
11021            know if the function decls referenced by ARG will actually be
11022            emitted after cgraph computations.
11023            So must defer the generation of the DW_AT_const_value to
11024            after cgraph is ready.  */
11025         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
11026     }
11027
11028   return tmpl_die;
11029 }
11030
11031 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
11032    PARM_PACK must be a template parameter pack. The returned DIE
11033    will be child DIE of PARENT_DIE.  */
11034
11035 static dw_die_ref
11036 template_parameter_pack_die (tree parm_pack,
11037                              tree parm_pack_args,
11038                              dw_die_ref parent_die)
11039 {
11040   dw_die_ref die;
11041   int j;
11042
11043   gcc_assert (parent_die && parm_pack);
11044
11045   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
11046   add_name_and_src_coords_attributes (die, parm_pack);
11047   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
11048     generic_parameter_die (parm_pack,
11049                            TREE_VEC_ELT (parm_pack_args, j),
11050                            false /* Don't emit DW_AT_name */,
11051                            die);
11052   return die;
11053 }
11054
11055 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
11056    an enumerated type.  */
11057
11058 static inline int
11059 type_is_enum (const_tree type)
11060 {
11061   return TREE_CODE (type) == ENUMERAL_TYPE;
11062 }
11063
11064 /* Return the DBX register number described by a given RTL node.  */
11065
11066 static unsigned int
11067 dbx_reg_number (const_rtx rtl)
11068 {
11069   unsigned regno = REGNO (rtl);
11070
11071   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
11072
11073 #ifdef LEAF_REG_REMAP
11074   if (crtl->uses_only_leaf_regs)
11075     {
11076       int leaf_reg = LEAF_REG_REMAP (regno);
11077       if (leaf_reg != -1)
11078         regno = (unsigned) leaf_reg;
11079     }
11080 #endif
11081
11082   regno = DBX_REGISTER_NUMBER (regno);
11083   gcc_assert (regno != INVALID_REGNUM);
11084   return regno;
11085 }
11086
11087 /* Optionally add a DW_OP_piece term to a location description expression.
11088    DW_OP_piece is only added if the location description expression already
11089    doesn't end with DW_OP_piece.  */
11090
11091 static void
11092 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
11093 {
11094   dw_loc_descr_ref loc;
11095
11096   if (*list_head != NULL)
11097     {
11098       /* Find the end of the chain.  */
11099       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
11100         ;
11101
11102       if (loc->dw_loc_opc != DW_OP_piece)
11103         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
11104     }
11105 }
11106
11107 /* Return a location descriptor that designates a machine register or
11108    zero if there is none.  */
11109
11110 static dw_loc_descr_ref
11111 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
11112 {
11113   rtx regs;
11114
11115   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
11116     return 0;
11117
11118   /* We only use "frame base" when we're sure we're talking about the
11119      post-prologue local stack frame.  We do this by *not* running
11120      register elimination until this point, and recognizing the special
11121      argument pointer and soft frame pointer rtx's.
11122      Use DW_OP_fbreg offset DW_OP_stack_value in this case.  */
11123   if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
11124       && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
11125     {
11126       dw_loc_descr_ref result = NULL;
11127
11128       if (dwarf_version >= 4 || !dwarf_strict)
11129         {
11130           result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
11131                                        initialized);
11132           if (result)
11133             add_loc_descr (&result,
11134                            new_loc_descr (DW_OP_stack_value, 0, 0));
11135         }
11136       return result;
11137     }
11138
11139   regs = targetm.dwarf_register_span (rtl);
11140
11141   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
11142     return multiple_reg_loc_descriptor (rtl, regs, initialized);
11143   else
11144     {
11145       unsigned int dbx_regnum = dbx_reg_number (rtl);
11146       if (dbx_regnum == IGNORED_DWARF_REGNUM)
11147         return 0;
11148       return one_reg_loc_descriptor (dbx_regnum, initialized);
11149     }
11150 }
11151
11152 /* Return a location descriptor that designates a machine register for
11153    a given hard register number.  */
11154
11155 static dw_loc_descr_ref
11156 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
11157 {
11158   dw_loc_descr_ref reg_loc_descr;
11159
11160   if (regno <= 31)
11161     reg_loc_descr
11162       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
11163   else
11164     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
11165
11166   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11167     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11168
11169   return reg_loc_descr;
11170 }
11171
11172 /* Given an RTL of a register, return a location descriptor that
11173    designates a value that spans more than one register.  */
11174
11175 static dw_loc_descr_ref
11176 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
11177                              enum var_init_status initialized)
11178 {
11179   int size, i;
11180   dw_loc_descr_ref loc_result = NULL;
11181
11182   /* Simple, contiguous registers.  */
11183   if (regs == NULL_RTX)
11184     {
11185       unsigned reg = REGNO (rtl);
11186       int nregs;
11187
11188 #ifdef LEAF_REG_REMAP
11189       if (crtl->uses_only_leaf_regs)
11190         {
11191           int leaf_reg = LEAF_REG_REMAP (reg);
11192           if (leaf_reg != -1)
11193             reg = (unsigned) leaf_reg;
11194         }
11195 #endif
11196
11197       gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
11198       nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
11199
11200       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
11201
11202       loc_result = NULL;
11203       while (nregs--)
11204         {
11205           dw_loc_descr_ref t;
11206
11207           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
11208                                       VAR_INIT_STATUS_INITIALIZED);
11209           add_loc_descr (&loc_result, t);
11210           add_loc_descr_op_piece (&loc_result, size);
11211           ++reg;
11212         }
11213       return loc_result;
11214     }
11215
11216   /* Now onto stupid register sets in non contiguous locations.  */
11217
11218   gcc_assert (GET_CODE (regs) == PARALLEL);
11219
11220   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
11221   loc_result = NULL;
11222
11223   for (i = 0; i < XVECLEN (regs, 0); ++i)
11224     {
11225       dw_loc_descr_ref t;
11226
11227       t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
11228                                   VAR_INIT_STATUS_INITIALIZED);
11229       add_loc_descr (&loc_result, t);
11230       add_loc_descr_op_piece (&loc_result, size);
11231     }
11232
11233   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11234     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11235   return loc_result;
11236 }
11237
11238 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
11239
11240 /* Return a location descriptor that designates a constant i,
11241    as a compound operation from constant (i >> shift), constant shift
11242    and DW_OP_shl.  */
11243
11244 static dw_loc_descr_ref
11245 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11246 {
11247   dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
11248   add_loc_descr (&ret, int_loc_descriptor (shift));
11249   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11250   return ret;
11251 }
11252
11253 /* Return a location descriptor that designates a constant.  */
11254
11255 static dw_loc_descr_ref
11256 int_loc_descriptor (HOST_WIDE_INT i)
11257 {
11258   enum dwarf_location_atom op;
11259
11260   /* Pick the smallest representation of a constant, rather than just
11261      defaulting to the LEB encoding.  */
11262   if (i >= 0)
11263     {
11264       int clz = clz_hwi (i);
11265       int ctz = ctz_hwi (i);
11266       if (i <= 31)
11267         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
11268       else if (i <= 0xff)
11269         op = DW_OP_const1u;
11270       else if (i <= 0xffff)
11271         op = DW_OP_const2u;
11272       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11273                && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11274         /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
11275            DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
11276            while DW_OP_const4u is 5 bytes.  */
11277         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
11278       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11279                && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11280         /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
11281            while DW_OP_const4u is 5 bytes.  */
11282         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11283       else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11284         op = DW_OP_const4u;
11285       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11286                && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11287         /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
11288            while DW_OP_constu of constant >= 0x100000000 takes at least
11289            6 bytes.  */
11290         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11291       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11292                && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
11293                   >= HOST_BITS_PER_WIDE_INT)
11294         /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
11295            DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
11296            while DW_OP_constu takes in this case at least 6 bytes.  */
11297         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
11298       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11299                && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11300                && size_of_uleb128 (i) > 6)
11301         /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes.  */
11302         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
11303       else
11304         op = DW_OP_constu;
11305     }
11306   else
11307     {
11308       if (i >= -0x80)
11309         op = DW_OP_const1s;
11310       else if (i >= -0x8000)
11311         op = DW_OP_const2s;
11312       else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11313         {
11314           if (size_of_int_loc_descriptor (i) < 5)
11315             {
11316               dw_loc_descr_ref ret = int_loc_descriptor (-i);
11317               add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11318               return ret;
11319             }
11320           op = DW_OP_const4s;
11321         }
11322       else
11323         {
11324           if (size_of_int_loc_descriptor (i)
11325               < (unsigned long) 1 + size_of_sleb128 (i))
11326             {
11327               dw_loc_descr_ref ret = int_loc_descriptor (-i);
11328               add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11329               return ret;
11330             }
11331           op = DW_OP_consts;
11332         }
11333     }
11334
11335   return new_loc_descr (op, i, 0);
11336 }
11337
11338 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
11339    without actually allocating it.  */
11340
11341 static unsigned long
11342 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11343 {
11344   return size_of_int_loc_descriptor (i >> shift)
11345          + size_of_int_loc_descriptor (shift)
11346          + 1;
11347 }
11348
11349 /* Return size_of_locs (int_loc_descriptor (i)) without
11350    actually allocating it.  */
11351
11352 static unsigned long
11353 size_of_int_loc_descriptor (HOST_WIDE_INT i)
11354 {
11355   unsigned long s;
11356
11357   if (i >= 0)
11358     {
11359       int clz, ctz;
11360       if (i <= 31)
11361         return 1;
11362       else if (i <= 0xff)
11363         return 2;
11364       else if (i <= 0xffff)
11365         return 3;
11366       clz = clz_hwi (i);
11367       ctz = ctz_hwi (i);
11368       if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11369           && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11370         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11371                                                     - clz - 5);
11372       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11373                && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11374         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11375                                                     - clz - 8);
11376       else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11377         return 5;
11378       s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
11379       if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11380           && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11381         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11382                                                     - clz - 8);
11383       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11384                && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
11385         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11386                                                     - clz - 16);
11387       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11388                && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11389                && s > 6)
11390         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11391                                                     - clz - 32);
11392       else
11393         return 1 + s;
11394     }
11395   else
11396     {
11397       if (i >= -0x80)
11398         return 2;
11399       else if (i >= -0x8000)
11400         return 3;
11401       else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11402         {
11403           if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11404             {
11405               s = size_of_int_loc_descriptor (-i) + 1;
11406               if (s < 5)
11407                 return s;
11408             }
11409           return 5;
11410         }
11411       else
11412         {
11413           unsigned long r = 1 + size_of_sleb128 (i);
11414           if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11415             {
11416               s = size_of_int_loc_descriptor (-i) + 1;
11417               if (s < r)
11418                 return s;
11419             }
11420           return r;
11421         }
11422     }
11423 }
11424
11425 /* Return loc description representing "address" of integer value.
11426    This can appear only as toplevel expression.  */
11427
11428 static dw_loc_descr_ref
11429 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
11430 {
11431   int litsize;
11432   dw_loc_descr_ref loc_result = NULL;
11433
11434   if (!(dwarf_version >= 4 || !dwarf_strict))
11435     return NULL;
11436
11437   litsize = size_of_int_loc_descriptor (i);
11438   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
11439      is more compact.  For DW_OP_stack_value we need:
11440      litsize + 1 (DW_OP_stack_value)
11441      and for DW_OP_implicit_value:
11442      1 (DW_OP_implicit_value) + 1 (length) + size.  */
11443   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
11444     {
11445       loc_result = int_loc_descriptor (i);
11446       add_loc_descr (&loc_result,
11447                      new_loc_descr (DW_OP_stack_value, 0, 0));
11448       return loc_result;
11449     }
11450
11451   loc_result = new_loc_descr (DW_OP_implicit_value,
11452                               size, 0);
11453   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11454   loc_result->dw_loc_oprnd2.v.val_int = i;
11455   return loc_result;
11456 }
11457
11458 /* Return a location descriptor that designates a base+offset location.  */
11459
11460 static dw_loc_descr_ref
11461 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
11462                  enum var_init_status initialized)
11463 {
11464   unsigned int regno;
11465   dw_loc_descr_ref result;
11466   dw_fde_ref fde = cfun->fde;
11467
11468   /* We only use "frame base" when we're sure we're talking about the
11469      post-prologue local stack frame.  We do this by *not* running
11470      register elimination until this point, and recognizing the special
11471      argument pointer and soft frame pointer rtx's.  */
11472   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
11473     {
11474       rtx elim = (ira_use_lra_p
11475                   ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
11476                   : eliminate_regs (reg, VOIDmode, NULL_RTX));
11477
11478       if (elim != reg)
11479         {
11480           if (GET_CODE (elim) == PLUS)
11481             {
11482               offset += INTVAL (XEXP (elim, 1));
11483               elim = XEXP (elim, 0);
11484             }
11485           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11486                        && (elim == hard_frame_pointer_rtx
11487                            || elim == stack_pointer_rtx))
11488                       || elim == (frame_pointer_needed
11489                                   ? hard_frame_pointer_rtx
11490                                   : stack_pointer_rtx));
11491
11492           /* If drap register is used to align stack, use frame
11493              pointer + offset to access stack variables.  If stack
11494              is aligned without drap, use stack pointer + offset to
11495              access stack variables.  */
11496           if (crtl->stack_realign_tried
11497               && reg == frame_pointer_rtx)
11498             {
11499               int base_reg
11500                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
11501                                       ? HARD_FRAME_POINTER_REGNUM
11502                                       : REGNO (elim));
11503               return new_reg_loc_descr (base_reg, offset);
11504             }
11505
11506           gcc_assert (frame_pointer_fb_offset_valid);
11507           offset += frame_pointer_fb_offset;
11508           return new_loc_descr (DW_OP_fbreg, offset, 0);
11509         }
11510     }
11511
11512   regno = REGNO (reg);
11513 #ifdef LEAF_REG_REMAP
11514   if (crtl->uses_only_leaf_regs)
11515     {
11516       int leaf_reg = LEAF_REG_REMAP (regno);
11517       if (leaf_reg != -1)
11518         regno = (unsigned) leaf_reg;
11519     }
11520 #endif
11521   regno = DWARF_FRAME_REGNUM (regno);
11522
11523   if (!optimize && fde
11524       && (fde->drap_reg == regno || fde->vdrap_reg == regno))
11525     {
11526       /* Use cfa+offset to represent the location of arguments passed
11527          on the stack when drap is used to align stack.
11528          Only do this when not optimizing, for optimized code var-tracking
11529          is supposed to track where the arguments live and the register
11530          used as vdrap or drap in some spot might be used for something
11531          else in other part of the routine.  */
11532       return new_loc_descr (DW_OP_fbreg, offset, 0);
11533     }
11534
11535   if (regno <= 31)
11536     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11537                             offset, 0);
11538   else
11539     result = new_loc_descr (DW_OP_bregx, regno, offset);
11540
11541   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11542     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11543
11544   return result;
11545 }
11546
11547 /* Return true if this RTL expression describes a base+offset calculation.  */
11548
11549 static inline int
11550 is_based_loc (const_rtx rtl)
11551 {
11552   return (GET_CODE (rtl) == PLUS
11553           && ((REG_P (XEXP (rtl, 0))
11554                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11555                && CONST_INT_P (XEXP (rtl, 1)))));
11556 }
11557
11558 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11559    failed.  */
11560
11561 static dw_loc_descr_ref
11562 tls_mem_loc_descriptor (rtx mem)
11563 {
11564   tree base;
11565   dw_loc_descr_ref loc_result;
11566
11567   if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11568     return NULL;
11569
11570   base = get_base_address (MEM_EXPR (mem));
11571   if (base == NULL
11572       || TREE_CODE (base) != VAR_DECL
11573       || !DECL_THREAD_LOCAL_P (base))
11574     return NULL;
11575
11576   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
11577   if (loc_result == NULL)
11578     return NULL;
11579
11580   if (MEM_OFFSET (mem))
11581     loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11582
11583   return loc_result;
11584 }
11585
11586 /* Output debug info about reason why we failed to expand expression as dwarf
11587    expression.  */
11588
11589 static void
11590 expansion_failed (tree expr, rtx rtl, char const *reason)
11591 {
11592   if (dump_file && (dump_flags & TDF_DETAILS))
11593     {
11594       fprintf (dump_file, "Failed to expand as dwarf: ");
11595       if (expr)
11596         print_generic_expr (dump_file, expr, dump_flags);
11597       if (rtl)
11598         {
11599           fprintf (dump_file, "\n");
11600           print_rtl (dump_file, rtl);
11601         }
11602       fprintf (dump_file, "\nReason: %s\n", reason);
11603     }
11604 }
11605
11606 /* Helper function for const_ok_for_output.  */
11607
11608 static bool
11609 const_ok_for_output_1 (rtx rtl)
11610 {
11611   if (GET_CODE (rtl) == UNSPEC)
11612     {
11613       /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11614          we can't express it in the debug info.  */
11615 #ifdef ENABLE_CHECKING
11616       /* Don't complain about TLS UNSPECs, those are just too hard to
11617          delegitimize.  Note this could be a non-decl SYMBOL_REF such as
11618          one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11619          rather than DECL_THREAD_LOCAL_P is not just an optimization.  */
11620       if (XVECLEN (rtl, 0) == 0
11621           || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11622           || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11623         inform (current_function_decl
11624                 ? DECL_SOURCE_LOCATION (current_function_decl)
11625                 : UNKNOWN_LOCATION,
11626 #if NUM_UNSPEC_VALUES > 0
11627                 "non-delegitimized UNSPEC %s (%d) found in variable location",
11628                 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11629                  ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11630                 XINT (rtl, 1));
11631 #else
11632                 "non-delegitimized UNSPEC %d found in variable location",
11633                 XINT (rtl, 1));
11634 #endif
11635 #endif
11636       expansion_failed (NULL_TREE, rtl,
11637                         "UNSPEC hasn't been delegitimized.\n");
11638       return false;
11639     }
11640
11641   if (targetm.const_not_ok_for_debug_p (rtl))
11642     {
11643       expansion_failed (NULL_TREE, rtl,
11644                         "Expression rejected for debug by the backend.\n");
11645       return false;
11646     }
11647
11648   /* FIXME: Refer to PR60655. It is possible for simplification
11649      of rtl expressions in var tracking to produce such expressions.
11650      We should really identify / validate expressions
11651      enclosed in CONST that can be handled by assemblers on various
11652      targets and only handle legitimate cases here.  */
11653   if (GET_CODE (rtl) != SYMBOL_REF)
11654     {
11655       if (GET_CODE (rtl) == NOT)
11656         return false;
11657       return true;
11658     }
11659
11660   if (CONSTANT_POOL_ADDRESS_P (rtl))
11661     {
11662       bool marked;
11663       get_pool_constant_mark (rtl, &marked);
11664       /* If all references to this pool constant were optimized away,
11665          it was not output and thus we can't represent it.  */
11666       if (!marked)
11667         {
11668           expansion_failed (NULL_TREE, rtl,
11669                             "Constant was removed from constant pool.\n");
11670           return false;
11671         }
11672     }
11673
11674   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11675     return false;
11676
11677   /* Avoid references to external symbols in debug info, on several targets
11678      the linker might even refuse to link when linking a shared library,
11679      and in many other cases the relocations for .debug_info/.debug_loc are
11680      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
11681      to be defined within the same shared library or executable are fine.  */
11682   if (SYMBOL_REF_EXTERNAL_P (rtl))
11683     {
11684       tree decl = SYMBOL_REF_DECL (rtl);
11685
11686       if (decl == NULL || !targetm.binds_local_p (decl))
11687         {
11688           expansion_failed (NULL_TREE, rtl,
11689                             "Symbol not defined in current TU.\n");
11690           return false;
11691         }
11692     }
11693
11694   return true;
11695 }
11696
11697 /* Return true if constant RTL can be emitted in DW_OP_addr or
11698    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
11699    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
11700
11701 static bool
11702 const_ok_for_output (rtx rtl)
11703 {
11704   if (GET_CODE (rtl) == SYMBOL_REF)
11705     return const_ok_for_output_1 (rtl);
11706
11707   if (GET_CODE (rtl) == CONST)
11708     {
11709       subrtx_var_iterator::array_type array;
11710       FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
11711         if (!const_ok_for_output_1 (*iter))
11712           return false;
11713       return true;
11714     }
11715
11716   return true;
11717 }
11718
11719 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11720    if possible, NULL otherwise.  */
11721
11722 static dw_die_ref
11723 base_type_for_mode (machine_mode mode, bool unsignedp)
11724 {
11725   dw_die_ref type_die;
11726   tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11727
11728   if (type == NULL)
11729     return NULL;
11730   switch (TREE_CODE (type))
11731     {
11732     case INTEGER_TYPE:
11733     case REAL_TYPE:
11734       break;
11735     default:
11736       return NULL;
11737     }
11738   type_die = lookup_type_die (type);
11739   if (!type_die)
11740     type_die = modified_type_die (type, TYPE_UNQUALIFIED, comp_unit_die ());
11741   if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11742     return NULL;
11743   return type_die;
11744 }
11745
11746 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11747    type matching MODE, or, if MODE is narrower than or as wide as
11748    DWARF2_ADDR_SIZE, untyped.  Return NULL if the conversion is not
11749    possible.  */
11750
11751 static dw_loc_descr_ref
11752 convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
11753 {
11754   machine_mode outer_mode = mode;
11755   dw_die_ref type_die;
11756   dw_loc_descr_ref cvt;
11757
11758   if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11759     {
11760       add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11761       return op;
11762     }
11763   type_die = base_type_for_mode (outer_mode, 1);
11764   if (type_die == NULL)
11765     return NULL;
11766   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11767   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11768   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11769   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11770   add_loc_descr (&op, cvt);
11771   return op;
11772 }
11773
11774 /* Return location descriptor for comparison OP with operands OP0 and OP1.  */
11775
11776 static dw_loc_descr_ref
11777 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11778                         dw_loc_descr_ref op1)
11779 {
11780   dw_loc_descr_ref ret = op0;
11781   add_loc_descr (&ret, op1);
11782   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11783   if (STORE_FLAG_VALUE != 1)
11784     {
11785       add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11786       add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11787     }
11788   return ret;
11789 }
11790
11791 /* Return location descriptor for signed comparison OP RTL.  */
11792
11793 static dw_loc_descr_ref
11794 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11795                          machine_mode mem_mode)
11796 {
11797   machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11798   dw_loc_descr_ref op0, op1;
11799   int shift;
11800
11801   if (op_mode == VOIDmode)
11802     op_mode = GET_MODE (XEXP (rtl, 1));
11803   if (op_mode == VOIDmode)
11804     return NULL;
11805
11806   if (dwarf_strict
11807       && (GET_MODE_CLASS (op_mode) != MODE_INT
11808           || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11809     return NULL;
11810
11811   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11812                             VAR_INIT_STATUS_INITIALIZED);
11813   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11814                             VAR_INIT_STATUS_INITIALIZED);
11815
11816   if (op0 == NULL || op1 == NULL)
11817     return NULL;
11818
11819   if (GET_MODE_CLASS (op_mode) != MODE_INT
11820       || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11821     return compare_loc_descriptor (op, op0, op1);
11822
11823   if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11824     {
11825       dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11826       dw_loc_descr_ref cvt;
11827
11828       if (type_die == NULL)
11829         return NULL;
11830       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11831       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11832       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11833       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11834       add_loc_descr (&op0, cvt);
11835       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11836       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11837       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11838       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11839       add_loc_descr (&op1, cvt);
11840       return compare_loc_descriptor (op, op0, op1);
11841     }
11842
11843   shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11844   /* For eq/ne, if the operands are known to be zero-extended,
11845      there is no need to do the fancy shifting up.  */
11846   if (op == DW_OP_eq || op == DW_OP_ne)
11847     {
11848       dw_loc_descr_ref last0, last1;
11849       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11850         ;
11851       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11852         ;
11853       /* deref_size zero extends, and for constants we can check
11854          whether they are zero extended or not.  */
11855       if (((last0->dw_loc_opc == DW_OP_deref_size
11856             && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11857            || (CONST_INT_P (XEXP (rtl, 0))
11858                && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11859                   == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11860           && ((last1->dw_loc_opc == DW_OP_deref_size
11861                && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11862               || (CONST_INT_P (XEXP (rtl, 1))
11863                   && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11864                      == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11865         return compare_loc_descriptor (op, op0, op1);
11866
11867       /* EQ/NE comparison against constant in narrower type than
11868          DWARF2_ADDR_SIZE can be performed either as
11869          DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11870          DW_OP_{eq,ne}
11871          or
11872          DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11873          DW_OP_{eq,ne}.  Pick whatever is shorter.  */
11874       if (CONST_INT_P (XEXP (rtl, 1))
11875           && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11876           && (size_of_int_loc_descriptor (shift) + 1
11877               + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11878               >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11879                  + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11880                                                & GET_MODE_MASK (op_mode))))
11881         {
11882           add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11883           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11884           op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11885                                     & GET_MODE_MASK (op_mode));
11886           return compare_loc_descriptor (op, op0, op1);
11887         }
11888     }
11889   add_loc_descr (&op0, int_loc_descriptor (shift));
11890   add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11891   if (CONST_INT_P (XEXP (rtl, 1)))
11892     op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11893   else
11894     {
11895       add_loc_descr (&op1, int_loc_descriptor (shift));
11896       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11897     }
11898   return compare_loc_descriptor (op, op0, op1);
11899 }
11900
11901 /* Return location descriptor for unsigned comparison OP RTL.  */
11902
11903 static dw_loc_descr_ref
11904 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11905                          machine_mode mem_mode)
11906 {
11907   machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11908   dw_loc_descr_ref op0, op1;
11909
11910   if (op_mode == VOIDmode)
11911     op_mode = GET_MODE (XEXP (rtl, 1));
11912   if (op_mode == VOIDmode)
11913     return NULL;
11914   if (GET_MODE_CLASS (op_mode) != MODE_INT)
11915     return NULL;
11916
11917   if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11918     return NULL;
11919
11920   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11921                             VAR_INIT_STATUS_INITIALIZED);
11922   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11923                             VAR_INIT_STATUS_INITIALIZED);
11924
11925   if (op0 == NULL || op1 == NULL)
11926     return NULL;
11927
11928   if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
11929     {
11930       HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
11931       dw_loc_descr_ref last0, last1;
11932       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11933         ;
11934       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11935         ;
11936       if (CONST_INT_P (XEXP (rtl, 0)))
11937         op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
11938       /* deref_size zero extends, so no need to mask it again.  */
11939       else if (last0->dw_loc_opc != DW_OP_deref_size
11940                || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11941         {
11942           add_loc_descr (&op0, int_loc_descriptor (mask));
11943           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11944         }
11945       if (CONST_INT_P (XEXP (rtl, 1)))
11946         op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11947       /* deref_size zero extends, so no need to mask it again.  */
11948       else if (last1->dw_loc_opc != DW_OP_deref_size
11949                || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11950         {
11951           add_loc_descr (&op1, int_loc_descriptor (mask));
11952           add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11953         }
11954     }
11955   else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11956     {
11957       HOST_WIDE_INT bias = 1;
11958       bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11959       add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11960       if (CONST_INT_P (XEXP (rtl, 1)))
11961         op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11962                                   + INTVAL (XEXP (rtl, 1)));
11963       else
11964         add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
11965                                             bias, 0));
11966     }
11967   return compare_loc_descriptor (op, op0, op1);
11968 }
11969
11970 /* Return location descriptor for {U,S}{MIN,MAX}.  */
11971
11972 static dw_loc_descr_ref
11973 minmax_loc_descriptor (rtx rtl, machine_mode mode,
11974                        machine_mode mem_mode)
11975 {
11976   enum dwarf_location_atom op;
11977   dw_loc_descr_ref op0, op1, ret;
11978   dw_loc_descr_ref bra_node, drop_node;
11979
11980   if (dwarf_strict
11981       && (GET_MODE_CLASS (mode) != MODE_INT
11982           || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
11983     return NULL;
11984
11985   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11986                             VAR_INIT_STATUS_INITIALIZED);
11987   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11988                             VAR_INIT_STATUS_INITIALIZED);
11989
11990   if (op0 == NULL || op1 == NULL)
11991     return NULL;
11992
11993   add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11994   add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11995   add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11996   if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
11997     {
11998       if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11999         {
12000           HOST_WIDE_INT mask = GET_MODE_MASK (mode);
12001           add_loc_descr (&op0, int_loc_descriptor (mask));
12002           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12003           add_loc_descr (&op1, int_loc_descriptor (mask));
12004           add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12005         }
12006       else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12007         {
12008           HOST_WIDE_INT bias = 1;
12009           bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12010           add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12011           add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12012         }
12013     }
12014   else if (GET_MODE_CLASS (mode) == MODE_INT
12015            && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12016     {
12017       int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
12018       add_loc_descr (&op0, int_loc_descriptor (shift));
12019       add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
12020       add_loc_descr (&op1, int_loc_descriptor (shift));
12021       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
12022     }
12023   else if (GET_MODE_CLASS (mode) == MODE_INT
12024            && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12025     {
12026       dw_die_ref type_die = base_type_for_mode (mode, 0);
12027       dw_loc_descr_ref cvt;
12028       if (type_die == NULL)
12029         return NULL;
12030       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12031       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12032       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12033       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12034       add_loc_descr (&op0, cvt);
12035       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12036       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12037       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12038       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12039       add_loc_descr (&op1, cvt);
12040     }
12041
12042   if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
12043     op = DW_OP_lt;
12044   else
12045     op = DW_OP_gt;
12046   ret = op0;
12047   add_loc_descr (&ret, op1);
12048   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12049   bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12050   add_loc_descr (&ret, bra_node);
12051   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12052   drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12053   add_loc_descr (&ret, drop_node);
12054   bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12055   bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12056   if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
12057       && GET_MODE_CLASS (mode) == MODE_INT
12058       && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12059     ret = convert_descriptor_to_mode (mode, ret);
12060   return ret;
12061 }
12062
12063 /* Helper function for mem_loc_descriptor.  Perform OP binary op,
12064    but after converting arguments to type_die, afterwards
12065    convert back to unsigned.  */
12066
12067 static dw_loc_descr_ref
12068 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
12069              machine_mode mode, machine_mode mem_mode)
12070 {
12071   dw_loc_descr_ref cvt, op0, op1;
12072
12073   if (type_die == NULL)
12074     return NULL;
12075   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12076                             VAR_INIT_STATUS_INITIALIZED);
12077   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12078                             VAR_INIT_STATUS_INITIALIZED);
12079   if (op0 == NULL || op1 == NULL)
12080     return NULL;
12081   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12082   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12083   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12084   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12085   add_loc_descr (&op0, cvt);
12086   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12087   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12088   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12089   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12090   add_loc_descr (&op1, cvt);
12091   add_loc_descr (&op0, op1);
12092   add_loc_descr (&op0, new_loc_descr (op, 0, 0));
12093   return convert_descriptor_to_mode (mode, op0);
12094 }
12095
12096 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
12097    const0 is DW_OP_lit0 or corresponding typed constant,
12098    const1 is DW_OP_lit1 or corresponding typed constant
12099    and constMSB is constant with just the MSB bit set
12100    for the mode):
12101        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12102    L1: const0 DW_OP_swap
12103    L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
12104        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12105    L3: DW_OP_drop
12106    L4: DW_OP_nop
12107
12108    CTZ is similar:
12109        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12110    L1: const0 DW_OP_swap
12111    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12112        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12113    L3: DW_OP_drop
12114    L4: DW_OP_nop
12115
12116    FFS is similar:
12117        DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
12118    L1: const1 DW_OP_swap
12119    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12120        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12121    L3: DW_OP_drop
12122    L4: DW_OP_nop  */
12123
12124 static dw_loc_descr_ref
12125 clz_loc_descriptor (rtx rtl, machine_mode mode,
12126                     machine_mode mem_mode)
12127 {
12128   dw_loc_descr_ref op0, ret, tmp;
12129   HOST_WIDE_INT valv;
12130   dw_loc_descr_ref l1jump, l1label;
12131   dw_loc_descr_ref l2jump, l2label;
12132   dw_loc_descr_ref l3jump, l3label;
12133   dw_loc_descr_ref l4jump, l4label;
12134   rtx msb;
12135
12136   if (GET_MODE_CLASS (mode) != MODE_INT
12137       || GET_MODE (XEXP (rtl, 0)) != mode)
12138     return NULL;
12139
12140   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12141                             VAR_INIT_STATUS_INITIALIZED);
12142   if (op0 == NULL)
12143     return NULL;
12144   ret = op0;
12145   if (GET_CODE (rtl) == CLZ)
12146     {
12147       if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12148         valv = GET_MODE_BITSIZE (mode);
12149     }
12150   else if (GET_CODE (rtl) == FFS)
12151     valv = 0;
12152   else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12153     valv = GET_MODE_BITSIZE (mode);
12154   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12155   l1jump = new_loc_descr (DW_OP_bra, 0, 0);
12156   add_loc_descr (&ret, l1jump);
12157   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12158   tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
12159                             VAR_INIT_STATUS_INITIALIZED);
12160   if (tmp == NULL)
12161     return NULL;
12162   add_loc_descr (&ret, tmp);
12163   l4jump = new_loc_descr (DW_OP_skip, 0, 0);
12164   add_loc_descr (&ret, l4jump);
12165   l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
12166                                 ? const1_rtx : const0_rtx,
12167                                 mode, mem_mode,
12168                                 VAR_INIT_STATUS_INITIALIZED);
12169   if (l1label == NULL)
12170     return NULL;
12171   add_loc_descr (&ret, l1label);
12172   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12173   l2label = new_loc_descr (DW_OP_dup, 0, 0);
12174   add_loc_descr (&ret, l2label);
12175   if (GET_CODE (rtl) != CLZ)
12176     msb = const1_rtx;
12177   else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
12178     msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
12179                    << (GET_MODE_BITSIZE (mode) - 1));
12180   else
12181     msb = immed_wide_int_const
12182       (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
12183                             GET_MODE_PRECISION (mode)), mode);
12184   if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
12185     tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12186                          ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
12187                          ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
12188   else
12189     tmp = mem_loc_descriptor (msb, mode, mem_mode,
12190                               VAR_INIT_STATUS_INITIALIZED);
12191   if (tmp == NULL)
12192     return NULL;
12193   add_loc_descr (&ret, tmp);
12194   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12195   l3jump = new_loc_descr (DW_OP_bra, 0, 0);
12196   add_loc_descr (&ret, l3jump);
12197   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12198                             VAR_INIT_STATUS_INITIALIZED);
12199   if (tmp == NULL)
12200     return NULL;
12201   add_loc_descr (&ret, tmp);
12202   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
12203                                       ? DW_OP_shl : DW_OP_shr, 0, 0));
12204   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12205   add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
12206   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12207   l2jump = new_loc_descr (DW_OP_skip, 0, 0);
12208   add_loc_descr (&ret, l2jump);
12209   l3label = new_loc_descr (DW_OP_drop, 0, 0);
12210   add_loc_descr (&ret, l3label);
12211   l4label = new_loc_descr (DW_OP_nop, 0, 0);
12212   add_loc_descr (&ret, l4label);
12213   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12214   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12215   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12216   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12217   l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12218   l3jump->dw_loc_oprnd1.v.val_loc = l3label;
12219   l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12220   l4jump->dw_loc_oprnd1.v.val_loc = l4label;
12221   return ret;
12222 }
12223
12224 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
12225    const1 is DW_OP_lit1 or corresponding typed constant):
12226        const0 DW_OP_swap
12227    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12228        DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12229    L2: DW_OP_drop
12230
12231    PARITY is similar:
12232    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12233        DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12234    L2: DW_OP_drop  */
12235
12236 static dw_loc_descr_ref
12237 popcount_loc_descriptor (rtx rtl, machine_mode mode,
12238                          machine_mode mem_mode)
12239 {
12240   dw_loc_descr_ref op0, ret, tmp;
12241   dw_loc_descr_ref l1jump, l1label;
12242   dw_loc_descr_ref l2jump, l2label;
12243
12244   if (GET_MODE_CLASS (mode) != MODE_INT
12245       || GET_MODE (XEXP (rtl, 0)) != mode)
12246     return NULL;
12247
12248   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12249                             VAR_INIT_STATUS_INITIALIZED);
12250   if (op0 == NULL)
12251     return NULL;
12252   ret = op0;
12253   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12254                             VAR_INIT_STATUS_INITIALIZED);
12255   if (tmp == NULL)
12256     return NULL;
12257   add_loc_descr (&ret, tmp);
12258   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12259   l1label = new_loc_descr (DW_OP_dup, 0, 0);
12260   add_loc_descr (&ret, l1label);
12261   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12262   add_loc_descr (&ret, l2jump);
12263   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12264   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12265   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12266                             VAR_INIT_STATUS_INITIALIZED);
12267   if (tmp == NULL)
12268     return NULL;
12269   add_loc_descr (&ret, tmp);
12270   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12271   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
12272                                       ? DW_OP_plus : DW_OP_xor, 0, 0));
12273   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12274   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12275                             VAR_INIT_STATUS_INITIALIZED);
12276   add_loc_descr (&ret, tmp);
12277   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12278   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12279   add_loc_descr (&ret, l1jump);
12280   l2label = new_loc_descr (DW_OP_drop, 0, 0);
12281   add_loc_descr (&ret, l2label);
12282   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12283   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12284   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12285   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12286   return ret;
12287 }
12288
12289 /* BSWAP (constS is initial shift count, either 56 or 24):
12290        constS const0
12291    L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
12292        const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
12293        DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
12294        DW_OP_minus DW_OP_swap DW_OP_skip <L1>
12295    L2: DW_OP_drop DW_OP_swap DW_OP_drop  */
12296
12297 static dw_loc_descr_ref
12298 bswap_loc_descriptor (rtx rtl, machine_mode mode,
12299                       machine_mode mem_mode)
12300 {
12301   dw_loc_descr_ref op0, ret, tmp;
12302   dw_loc_descr_ref l1jump, l1label;
12303   dw_loc_descr_ref l2jump, l2label;
12304
12305   if (GET_MODE_CLASS (mode) != MODE_INT
12306       || BITS_PER_UNIT != 8
12307       || (GET_MODE_BITSIZE (mode) != 32
12308           &&  GET_MODE_BITSIZE (mode) != 64))
12309     return NULL;
12310
12311   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12312                             VAR_INIT_STATUS_INITIALIZED);
12313   if (op0 == NULL)
12314     return NULL;
12315
12316   ret = op0;
12317   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12318                             mode, mem_mode,
12319                             VAR_INIT_STATUS_INITIALIZED);
12320   if (tmp == NULL)
12321     return NULL;
12322   add_loc_descr (&ret, tmp);
12323   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12324                             VAR_INIT_STATUS_INITIALIZED);
12325   if (tmp == NULL)
12326     return NULL;
12327   add_loc_descr (&ret, tmp);
12328   l1label = new_loc_descr (DW_OP_pick, 2, 0);
12329   add_loc_descr (&ret, l1label);
12330   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12331                             mode, mem_mode,
12332                             VAR_INIT_STATUS_INITIALIZED);
12333   add_loc_descr (&ret, tmp);
12334   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
12335   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12336   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12337   tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
12338                             VAR_INIT_STATUS_INITIALIZED);
12339   if (tmp == NULL)
12340     return NULL;
12341   add_loc_descr (&ret, tmp);
12342   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12343   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
12344   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12345   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12346   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12347   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12348   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12349                             VAR_INIT_STATUS_INITIALIZED);
12350   add_loc_descr (&ret, tmp);
12351   add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
12352   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12353   add_loc_descr (&ret, l2jump);
12354   tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
12355                             VAR_INIT_STATUS_INITIALIZED);
12356   add_loc_descr (&ret, tmp);
12357   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12358   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12359   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12360   add_loc_descr (&ret, l1jump);
12361   l2label = new_loc_descr (DW_OP_drop, 0, 0);
12362   add_loc_descr (&ret, l2label);
12363   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12364   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12365   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12366   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12367   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12368   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12369   return ret;
12370 }
12371
12372 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
12373    DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12374    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
12375    DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
12376
12377    ROTATERT is similar:
12378    DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
12379    DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12380    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or  */
12381
12382 static dw_loc_descr_ref
12383 rotate_loc_descriptor (rtx rtl, machine_mode mode,
12384                        machine_mode mem_mode)
12385 {
12386   rtx rtlop1 = XEXP (rtl, 1);
12387   dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
12388   int i;
12389
12390   if (GET_MODE_CLASS (mode) != MODE_INT)
12391     return NULL;
12392
12393   if (GET_MODE (rtlop1) != VOIDmode
12394       && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
12395     rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12396   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12397                             VAR_INIT_STATUS_INITIALIZED);
12398   op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12399                             VAR_INIT_STATUS_INITIALIZED);
12400   if (op0 == NULL || op1 == NULL)
12401     return NULL;
12402   if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12403     for (i = 0; i < 2; i++)
12404       {
12405         if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
12406           mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
12407                                         mode, mem_mode,
12408                                         VAR_INIT_STATUS_INITIALIZED);
12409         else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12410           mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12411                                    ? DW_OP_const4u
12412                                    : HOST_BITS_PER_WIDE_INT == 64
12413                                    ? DW_OP_const8u : DW_OP_constu,
12414                                    GET_MODE_MASK (mode), 0);
12415         else
12416           mask[i] = NULL;
12417         if (mask[i] == NULL)
12418           return NULL;
12419         add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
12420       }
12421   ret = op0;
12422   add_loc_descr (&ret, op1);
12423   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12424   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12425   if (GET_CODE (rtl) == ROTATERT)
12426     {
12427       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12428       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12429                                           GET_MODE_BITSIZE (mode), 0));
12430     }
12431   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12432   if (mask[0] != NULL)
12433     add_loc_descr (&ret, mask[0]);
12434   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12435   if (mask[1] != NULL)
12436     {
12437       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12438       add_loc_descr (&ret, mask[1]);
12439       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12440     }
12441   if (GET_CODE (rtl) == ROTATE)
12442     {
12443       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12444       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12445                                           GET_MODE_BITSIZE (mode), 0));
12446     }
12447   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12448   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12449   return ret;
12450 }
12451
12452 /* Helper function for mem_loc_descriptor.  Return DW_OP_GNU_parameter_ref
12453    for DEBUG_PARAMETER_REF RTL.  */
12454
12455 static dw_loc_descr_ref
12456 parameter_ref_descriptor (rtx rtl)
12457 {
12458   dw_loc_descr_ref ret;
12459   dw_die_ref ref;
12460
12461   if (dwarf_strict)
12462     return NULL;
12463   gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
12464   ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
12465   ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
12466   if (ref)
12467     {
12468       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12469       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12470       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12471     }
12472   else
12473     {
12474       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12475       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
12476     }
12477   return ret;
12478 }
12479
12480 /* The following routine converts the RTL for a variable or parameter
12481    (resident in memory) into an equivalent Dwarf representation of a
12482    mechanism for getting the address of that same variable onto the top of a
12483    hypothetical "address evaluation" stack.
12484
12485    When creating memory location descriptors, we are effectively transforming
12486    the RTL for a memory-resident object into its Dwarf postfix expression
12487    equivalent.  This routine recursively descends an RTL tree, turning
12488    it into Dwarf postfix code as it goes.
12489
12490    MODE is the mode that should be assumed for the rtl if it is VOIDmode.
12491
12492    MEM_MODE is the mode of the memory reference, needed to handle some
12493    autoincrement addressing modes.
12494
12495    Return 0 if we can't represent the location.  */
12496
12497 dw_loc_descr_ref
12498 mem_loc_descriptor (rtx rtl, machine_mode mode,
12499                     machine_mode mem_mode,
12500                     enum var_init_status initialized)
12501 {
12502   dw_loc_descr_ref mem_loc_result = NULL;
12503   enum dwarf_location_atom op;
12504   dw_loc_descr_ref op0, op1;
12505   rtx inner = NULL_RTX;
12506
12507   if (mode == VOIDmode)
12508     mode = GET_MODE (rtl);
12509
12510   /* Note that for a dynamically sized array, the location we will generate a
12511      description of here will be the lowest numbered location which is
12512      actually within the array.  That's *not* necessarily the same as the
12513      zeroth element of the array.  */
12514
12515   rtl = targetm.delegitimize_address (rtl);
12516
12517   if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
12518     return NULL;
12519
12520   switch (GET_CODE (rtl))
12521     {
12522     case POST_INC:
12523     case POST_DEC:
12524     case POST_MODIFY:
12525       return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
12526
12527     case SUBREG:
12528       /* The case of a subreg may arise when we have a local (register)
12529          variable or a formal (register) parameter which doesn't quite fill
12530          up an entire register.  For now, just assume that it is
12531          legitimate to make the Dwarf info refer to the whole register which
12532          contains the given subreg.  */
12533       if (!subreg_lowpart_p (rtl))
12534         break;
12535       inner = SUBREG_REG (rtl);
12536     case TRUNCATE:
12537       if (inner == NULL_RTX)
12538         inner = XEXP (rtl, 0);
12539       if (GET_MODE_CLASS (mode) == MODE_INT
12540           && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
12541           && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12542 #ifdef POINTERS_EXTEND_UNSIGNED
12543               || (mode == Pmode && mem_mode != VOIDmode)
12544 #endif
12545              )
12546           && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
12547         {
12548           mem_loc_result = mem_loc_descriptor (inner,
12549                                                GET_MODE (inner),
12550                                                mem_mode, initialized);
12551           break;
12552         }
12553       if (dwarf_strict)
12554         break;
12555       if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
12556         break;
12557       if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
12558           && (GET_MODE_CLASS (mode) != MODE_INT
12559               || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
12560         break;
12561       else
12562         {
12563           dw_die_ref type_die;
12564           dw_loc_descr_ref cvt;
12565
12566           mem_loc_result = mem_loc_descriptor (inner,
12567                                                GET_MODE (inner),
12568                                                mem_mode, initialized);
12569           if (mem_loc_result == NULL)
12570             break;
12571           type_die = base_type_for_mode (mode,
12572                                          GET_MODE_CLASS (mode) == MODE_INT);
12573           if (type_die == NULL)
12574             {
12575               mem_loc_result = NULL;
12576               break;
12577             }
12578           if (GET_MODE_SIZE (mode)
12579               != GET_MODE_SIZE (GET_MODE (inner)))
12580             cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12581           else
12582             cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12583           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12584           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12585           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12586           add_loc_descr (&mem_loc_result, cvt);
12587         }
12588       break;
12589
12590     case REG:
12591       if (GET_MODE_CLASS (mode) != MODE_INT
12592           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12593               && rtl != arg_pointer_rtx
12594               && rtl != frame_pointer_rtx
12595 #ifdef POINTERS_EXTEND_UNSIGNED
12596               && (mode != Pmode || mem_mode == VOIDmode)
12597 #endif
12598               ))
12599         {
12600           dw_die_ref type_die;
12601           unsigned int dbx_regnum;
12602
12603           if (dwarf_strict)
12604             break;
12605           if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12606             break;
12607           type_die = base_type_for_mode (mode,
12608                                          GET_MODE_CLASS (mode) == MODE_INT);
12609           if (type_die == NULL)
12610             break;
12611
12612           dbx_regnum = dbx_reg_number (rtl);
12613           if (dbx_regnum == IGNORED_DWARF_REGNUM)
12614             break;
12615           mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12616                                           dbx_regnum, 0);
12617           mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12618           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12619           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12620           break;
12621         }
12622       /* Whenever a register number forms a part of the description of the
12623          method for calculating the (dynamic) address of a memory resident
12624          object, DWARF rules require the register number be referred to as
12625          a "base register".  This distinction is not based in any way upon
12626          what category of register the hardware believes the given register
12627          belongs to.  This is strictly DWARF terminology we're dealing with
12628          here. Note that in cases where the location of a memory-resident
12629          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12630          OP_CONST (0)) the actual DWARF location descriptor that we generate
12631          may just be OP_BASEREG (basereg).  This may look deceptively like
12632          the object in question was allocated to a register (rather than in
12633          memory) so DWARF consumers need to be aware of the subtle
12634          distinction between OP_REG and OP_BASEREG.  */
12635       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12636         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12637       else if (stack_realign_drap
12638                && crtl->drap_reg
12639                && crtl->args.internal_arg_pointer == rtl
12640                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12641         {
12642           /* If RTL is internal_arg_pointer, which has been optimized
12643              out, use DRAP instead.  */
12644           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12645                                             VAR_INIT_STATUS_INITIALIZED);
12646         }
12647       break;
12648
12649     case SIGN_EXTEND:
12650     case ZERO_EXTEND:
12651       if (GET_MODE_CLASS (mode) != MODE_INT)
12652         break;
12653       op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12654                                 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12655       if (op0 == 0)
12656         break;
12657       else if (GET_CODE (rtl) == ZERO_EXTEND
12658                && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12659                && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12660                   < HOST_BITS_PER_WIDE_INT
12661                /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12662                   to expand zero extend as two shifts instead of
12663                   masking.  */
12664                && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12665         {
12666           machine_mode imode = GET_MODE (XEXP (rtl, 0));
12667           mem_loc_result = op0;
12668           add_loc_descr (&mem_loc_result,
12669                          int_loc_descriptor (GET_MODE_MASK (imode)));
12670           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12671         }
12672       else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12673         {
12674           int shift = DWARF2_ADDR_SIZE
12675                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12676           shift *= BITS_PER_UNIT;
12677           if (GET_CODE (rtl) == SIGN_EXTEND)
12678             op = DW_OP_shra;
12679           else
12680             op = DW_OP_shr;
12681           mem_loc_result = op0;
12682           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12683           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12684           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12685           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12686         }
12687       else if (!dwarf_strict)
12688         {
12689           dw_die_ref type_die1, type_die2;
12690           dw_loc_descr_ref cvt;
12691
12692           type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12693                                           GET_CODE (rtl) == ZERO_EXTEND);
12694           if (type_die1 == NULL)
12695             break;
12696           type_die2 = base_type_for_mode (mode, 1);
12697           if (type_die2 == NULL)
12698             break;
12699           mem_loc_result = op0;
12700           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12701           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12702           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12703           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12704           add_loc_descr (&mem_loc_result, cvt);
12705           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12706           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12707           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12708           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12709           add_loc_descr (&mem_loc_result, cvt);
12710         }
12711       break;
12712
12713     case MEM:
12714       {
12715         rtx new_rtl = avoid_constant_pool_reference (rtl);
12716         if (new_rtl != rtl)
12717           {
12718             mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12719                                                  initialized);
12720             if (mem_loc_result != NULL)
12721               return mem_loc_result;
12722           }
12723       }
12724       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12725                                            get_address_mode (rtl), mode,
12726                                            VAR_INIT_STATUS_INITIALIZED);
12727       if (mem_loc_result == NULL)
12728         mem_loc_result = tls_mem_loc_descriptor (rtl);
12729       if (mem_loc_result != NULL)
12730         {
12731           if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12732               || GET_MODE_CLASS (mode) != MODE_INT)
12733             {
12734               dw_die_ref type_die;
12735               dw_loc_descr_ref deref;
12736
12737               if (dwarf_strict)
12738                 return NULL;
12739               type_die
12740                 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12741               if (type_die == NULL)
12742                 return NULL;
12743               deref = new_loc_descr (DW_OP_GNU_deref_type,
12744                                      GET_MODE_SIZE (mode), 0);
12745               deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12746               deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12747               deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12748               add_loc_descr (&mem_loc_result, deref);
12749             }
12750           else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12751             add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12752           else
12753             add_loc_descr (&mem_loc_result,
12754                            new_loc_descr (DW_OP_deref_size,
12755                                           GET_MODE_SIZE (mode), 0));
12756         }
12757       break;
12758
12759     case LO_SUM:
12760       return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12761
12762     case LABEL_REF:
12763       /* Some ports can transform a symbol ref into a label ref, because
12764          the symbol ref is too far away and has to be dumped into a constant
12765          pool.  */
12766     case CONST:
12767     case SYMBOL_REF:
12768       if ((GET_MODE_CLASS (mode) != MODE_INT
12769            && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
12770           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12771 #ifdef POINTERS_EXTEND_UNSIGNED
12772               && (mode != Pmode || mem_mode == VOIDmode)
12773 #endif
12774               ))
12775         break;
12776       if (GET_CODE (rtl) == SYMBOL_REF
12777           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12778         {
12779           dw_loc_descr_ref temp;
12780
12781           /* If this is not defined, we have no way to emit the data.  */
12782           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12783             break;
12784
12785           temp = new_addr_loc_descr (rtl, dtprel_true);
12786
12787           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12788           add_loc_descr (&mem_loc_result, temp);
12789
12790           break;
12791         }
12792
12793       if (!const_ok_for_output (rtl))
12794         break;
12795
12796     symref:
12797       mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12798       vec_safe_push (used_rtx_array, rtl);
12799       break;
12800
12801     case CONCAT:
12802     case CONCATN:
12803     case VAR_LOCATION:
12804     case DEBUG_IMPLICIT_PTR:
12805       expansion_failed (NULL_TREE, rtl,
12806                         "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12807       return 0;
12808
12809     case ENTRY_VALUE:
12810       if (dwarf_strict)
12811         return NULL;
12812       if (REG_P (ENTRY_VALUE_EXP (rtl)))
12813         {
12814           if (GET_MODE_CLASS (mode) != MODE_INT
12815               || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12816             op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12817                                       VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12818           else
12819             {
12820               unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12821               if (dbx_regnum == IGNORED_DWARF_REGNUM)
12822                 return NULL;
12823               op0 = one_reg_loc_descriptor (dbx_regnum,
12824                                             VAR_INIT_STATUS_INITIALIZED);
12825             }
12826         }
12827       else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12828                && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12829         {
12830           op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12831                                     VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12832           if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12833             return NULL;
12834         }
12835       else
12836         gcc_unreachable ();
12837       if (op0 == NULL)
12838         return NULL;
12839       mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12840       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12841       mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12842       break;
12843
12844     case DEBUG_PARAMETER_REF:
12845       mem_loc_result = parameter_ref_descriptor (rtl);
12846       break;
12847
12848     case PRE_MODIFY:
12849       /* Extract the PLUS expression nested inside and fall into
12850          PLUS code below.  */
12851       rtl = XEXP (rtl, 1);
12852       goto plus;
12853
12854     case PRE_INC:
12855     case PRE_DEC:
12856       /* Turn these into a PLUS expression and fall into the PLUS code
12857          below.  */
12858       rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12859                           gen_int_mode (GET_CODE (rtl) == PRE_INC
12860                                         ? GET_MODE_UNIT_SIZE (mem_mode)
12861                                         : -GET_MODE_UNIT_SIZE (mem_mode),
12862                                         mode));
12863
12864       /* ... fall through ...  */
12865
12866     case PLUS:
12867     plus:
12868       if (is_based_loc (rtl)
12869           && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12870               || XEXP (rtl, 0) == arg_pointer_rtx
12871               || XEXP (rtl, 0) == frame_pointer_rtx)
12872           && GET_MODE_CLASS (mode) == MODE_INT)
12873         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12874                                           INTVAL (XEXP (rtl, 1)),
12875                                           VAR_INIT_STATUS_INITIALIZED);
12876       else
12877         {
12878           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12879                                                VAR_INIT_STATUS_INITIALIZED);
12880           if (mem_loc_result == 0)
12881             break;
12882
12883           if (CONST_INT_P (XEXP (rtl, 1))
12884               && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12885             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12886           else
12887             {
12888               op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12889                                         VAR_INIT_STATUS_INITIALIZED);
12890               if (op1 == 0)
12891                 return NULL;
12892               add_loc_descr (&mem_loc_result, op1);
12893               add_loc_descr (&mem_loc_result,
12894                              new_loc_descr (DW_OP_plus, 0, 0));
12895             }
12896         }
12897       break;
12898
12899     /* If a pseudo-reg is optimized away, it is possible for it to
12900        be replaced with a MEM containing a multiply or shift.  */
12901     case MINUS:
12902       op = DW_OP_minus;
12903       goto do_binop;
12904
12905     case MULT:
12906       op = DW_OP_mul;
12907       goto do_binop;
12908
12909     case DIV:
12910       if (!dwarf_strict
12911           && GET_MODE_CLASS (mode) == MODE_INT
12912           && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12913         {
12914           mem_loc_result = typed_binop (DW_OP_div, rtl,
12915                                         base_type_for_mode (mode, 0),
12916                                         mode, mem_mode);
12917           break;
12918         }
12919       op = DW_OP_div;
12920       goto do_binop;
12921
12922     case UMOD:
12923       op = DW_OP_mod;
12924       goto do_binop;
12925
12926     case ASHIFT:
12927       op = DW_OP_shl;
12928       goto do_shift;
12929
12930     case ASHIFTRT:
12931       op = DW_OP_shra;
12932       goto do_shift;
12933
12934     case LSHIFTRT:
12935       op = DW_OP_shr;
12936       goto do_shift;
12937
12938     do_shift:
12939       if (GET_MODE_CLASS (mode) != MODE_INT)
12940         break;
12941       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12942                                 VAR_INIT_STATUS_INITIALIZED);
12943       {
12944         rtx rtlop1 = XEXP (rtl, 1);
12945         if (GET_MODE (rtlop1) != VOIDmode
12946             && GET_MODE_BITSIZE (GET_MODE (rtlop1))
12947                < GET_MODE_BITSIZE (mode))
12948           rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12949         op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12950                                   VAR_INIT_STATUS_INITIALIZED);
12951       }
12952
12953       if (op0 == 0 || op1 == 0)
12954         break;
12955
12956       mem_loc_result = op0;
12957       add_loc_descr (&mem_loc_result, op1);
12958       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12959       break;
12960
12961     case AND:
12962       op = DW_OP_and;
12963       goto do_binop;
12964
12965     case IOR:
12966       op = DW_OP_or;
12967       goto do_binop;
12968
12969     case XOR:
12970       op = DW_OP_xor;
12971       goto do_binop;
12972
12973     do_binop:
12974       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12975                                 VAR_INIT_STATUS_INITIALIZED);
12976       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12977                                 VAR_INIT_STATUS_INITIALIZED);
12978
12979       if (op0 == 0 || op1 == 0)
12980         break;
12981
12982       mem_loc_result = op0;
12983       add_loc_descr (&mem_loc_result, op1);
12984       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12985       break;
12986
12987     case MOD:
12988       if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
12989         {
12990           mem_loc_result = typed_binop (DW_OP_mod, rtl,
12991                                         base_type_for_mode (mode, 0),
12992                                         mode, mem_mode);
12993           break;
12994         }
12995
12996       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12997                                 VAR_INIT_STATUS_INITIALIZED);
12998       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12999                                 VAR_INIT_STATUS_INITIALIZED);
13000
13001       if (op0 == 0 || op1 == 0)
13002         break;
13003
13004       mem_loc_result = op0;
13005       add_loc_descr (&mem_loc_result, op1);
13006       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13007       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13008       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
13009       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13010       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
13011       break;
13012
13013     case UDIV:
13014       if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
13015         {
13016           if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
13017             {
13018               op = DW_OP_div;
13019               goto do_binop;
13020             }
13021           mem_loc_result = typed_binop (DW_OP_div, rtl,
13022                                         base_type_for_mode (mode, 1),
13023                                         mode, mem_mode);
13024         }
13025       break;
13026
13027     case NOT:
13028       op = DW_OP_not;
13029       goto do_unop;
13030
13031     case ABS:
13032       op = DW_OP_abs;
13033       goto do_unop;
13034
13035     case NEG:
13036       op = DW_OP_neg;
13037       goto do_unop;
13038
13039     do_unop:
13040       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13041                                 VAR_INIT_STATUS_INITIALIZED);
13042
13043       if (op0 == 0)
13044         break;
13045
13046       mem_loc_result = op0;
13047       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13048       break;
13049
13050     case CONST_INT:
13051       if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13052 #ifdef POINTERS_EXTEND_UNSIGNED
13053           || (mode == Pmode
13054               && mem_mode != VOIDmode
13055               && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
13056 #endif
13057           )
13058         {
13059           mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13060           break;
13061         }
13062       if (!dwarf_strict
13063           && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
13064               || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
13065         {
13066           dw_die_ref type_die = base_type_for_mode (mode, 1);
13067           machine_mode amode;
13068           if (type_die == NULL)
13069             return NULL;
13070           amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
13071                                  MODE_INT, 0);
13072           if (INTVAL (rtl) >= 0
13073               && amode != BLKmode
13074               && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
13075               /* const DW_OP_GNU_convert <XXX> vs.
13076                  DW_OP_GNU_const_type <XXX, 1, const>.  */
13077               && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
13078                  < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
13079             {
13080               mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13081               op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13082               op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13083               op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13084               op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
13085               add_loc_descr (&mem_loc_result, op0);
13086               return mem_loc_result;
13087             }
13088           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
13089                                           INTVAL (rtl));
13090           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13091           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13092           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13093           if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
13094             mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13095           else
13096             {
13097               mem_loc_result->dw_loc_oprnd2.val_class
13098                 = dw_val_class_const_double;
13099               mem_loc_result->dw_loc_oprnd2.v.val_double
13100                 = double_int::from_shwi (INTVAL (rtl));
13101             }
13102         }
13103       break;
13104
13105     case CONST_DOUBLE:
13106       if (!dwarf_strict)
13107         {
13108           dw_die_ref type_die;
13109
13110           /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
13111              CONST_DOUBLE rtx could represent either a large integer
13112              or a floating-point constant.  If TARGET_SUPPORTS_WIDE_INT != 0,
13113              the value is always a floating point constant.
13114
13115              When it is an integer, a CONST_DOUBLE is used whenever
13116              the constant requires 2 HWIs to be adequately represented.
13117              We output CONST_DOUBLEs as blocks.  */
13118           if (mode == VOIDmode
13119               || (GET_MODE (rtl) == VOIDmode
13120                   && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
13121             break;
13122           type_die = base_type_for_mode (mode,
13123                                          GET_MODE_CLASS (mode) == MODE_INT);
13124           if (type_die == NULL)
13125             return NULL;
13126           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13127           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13128           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13129           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13130 #if TARGET_SUPPORTS_WIDE_INT == 0
13131           if (!SCALAR_FLOAT_MODE_P (mode))
13132             {
13133               mem_loc_result->dw_loc_oprnd2.val_class
13134                 = dw_val_class_const_double;
13135               mem_loc_result->dw_loc_oprnd2.v.val_double
13136                 = rtx_to_double_int (rtl);
13137             }
13138           else
13139 #endif
13140             {
13141               unsigned int length = GET_MODE_SIZE (mode);
13142               unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13143
13144               insert_float (rtl, array);
13145               mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13146               mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13147               mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13148               mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13149             }
13150         }
13151       break;
13152
13153     case CONST_WIDE_INT:
13154       if (!dwarf_strict)
13155         {
13156           dw_die_ref type_die;
13157
13158           type_die = base_type_for_mode (mode,
13159                                          GET_MODE_CLASS (mode) == MODE_INT);
13160           if (type_die == NULL)
13161             return NULL;
13162           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13163           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13164           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13165           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13166           mem_loc_result->dw_loc_oprnd2.val_class
13167             = dw_val_class_wide_int;
13168           mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
13169           *mem_loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13170         }
13171       break;
13172
13173     case EQ:
13174       mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
13175       break;
13176
13177     case GE:
13178       mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13179       break;
13180
13181     case GT:
13182       mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13183       break;
13184
13185     case LE:
13186       mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13187       break;
13188
13189     case LT:
13190       mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13191       break;
13192
13193     case NE:
13194       mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
13195       break;
13196
13197     case GEU:
13198       mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13199       break;
13200
13201     case GTU:
13202       mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13203       break;
13204
13205     case LEU:
13206       mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13207       break;
13208
13209     case LTU:
13210       mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13211       break;
13212
13213     case UMIN:
13214     case UMAX:
13215       if (GET_MODE_CLASS (mode) != MODE_INT)
13216         break;
13217       /* FALLTHRU */
13218     case SMIN:
13219     case SMAX:
13220       mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
13221       break;
13222
13223     case ZERO_EXTRACT:
13224     case SIGN_EXTRACT:
13225       if (CONST_INT_P (XEXP (rtl, 1))
13226           && CONST_INT_P (XEXP (rtl, 2))
13227           && ((unsigned) INTVAL (XEXP (rtl, 1))
13228               + (unsigned) INTVAL (XEXP (rtl, 2))
13229               <= GET_MODE_BITSIZE (mode))
13230           && GET_MODE_CLASS (mode) == MODE_INT
13231           && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13232           && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
13233         {
13234           int shift, size;
13235           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13236                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
13237           if (op0 == 0)
13238             break;
13239           if (GET_CODE (rtl) == SIGN_EXTRACT)
13240             op = DW_OP_shra;
13241           else
13242             op = DW_OP_shr;
13243           mem_loc_result = op0;
13244           size = INTVAL (XEXP (rtl, 1));
13245           shift = INTVAL (XEXP (rtl, 2));
13246           if (BITS_BIG_ENDIAN)
13247             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
13248                     - shift - size;
13249           if (shift + size != (int) DWARF2_ADDR_SIZE)
13250             {
13251               add_loc_descr (&mem_loc_result,
13252                              int_loc_descriptor (DWARF2_ADDR_SIZE
13253                                                  - shift - size));
13254               add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13255             }
13256           if (size != (int) DWARF2_ADDR_SIZE)
13257             {
13258               add_loc_descr (&mem_loc_result,
13259                              int_loc_descriptor (DWARF2_ADDR_SIZE - size));
13260               add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13261             }
13262         }
13263       break;
13264
13265     case IF_THEN_ELSE:
13266       {
13267         dw_loc_descr_ref op2, bra_node, drop_node;
13268         op0 = mem_loc_descriptor (XEXP (rtl, 0),
13269                                   GET_MODE (XEXP (rtl, 0)) == VOIDmode
13270                                   ? word_mode : GET_MODE (XEXP (rtl, 0)),
13271                                   mem_mode, VAR_INIT_STATUS_INITIALIZED);
13272         op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13273                                   VAR_INIT_STATUS_INITIALIZED);
13274         op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
13275                                   VAR_INIT_STATUS_INITIALIZED);
13276         if (op0 == NULL || op1 == NULL || op2 == NULL)
13277           break;
13278
13279         mem_loc_result = op1;
13280         add_loc_descr (&mem_loc_result, op2);
13281         add_loc_descr (&mem_loc_result, op0);
13282         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13283         add_loc_descr (&mem_loc_result, bra_node);
13284         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
13285         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
13286         add_loc_descr (&mem_loc_result, drop_node);
13287         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13288         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
13289       }
13290       break;
13291
13292     case FLOAT_EXTEND:
13293     case FLOAT_TRUNCATE:
13294     case FLOAT:
13295     case UNSIGNED_FLOAT:
13296     case FIX:
13297     case UNSIGNED_FIX:
13298       if (!dwarf_strict)
13299         {
13300           dw_die_ref type_die;
13301           dw_loc_descr_ref cvt;
13302
13303           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13304                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
13305           if (op0 == NULL)
13306             break;
13307           if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
13308               && (GET_CODE (rtl) == FLOAT
13309                   || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
13310                      <= DWARF2_ADDR_SIZE))
13311             {
13312               type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
13313                                              GET_CODE (rtl) == UNSIGNED_FLOAT);
13314               if (type_die == NULL)
13315                 break;
13316               cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13317               cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13318               cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13319               cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13320               add_loc_descr (&op0, cvt);
13321             }
13322           type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
13323           if (type_die == NULL)
13324             break;
13325           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13326           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13327           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13328           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13329           add_loc_descr (&op0, cvt);
13330           if (GET_MODE_CLASS (mode) == MODE_INT
13331               && (GET_CODE (rtl) == FIX
13332                   || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
13333             {
13334               op0 = convert_descriptor_to_mode (mode, op0);
13335               if (op0 == NULL)
13336                 break;
13337             }
13338           mem_loc_result = op0;
13339         }
13340       break;
13341
13342     case CLZ:
13343     case CTZ:
13344     case FFS:
13345       mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
13346       break;
13347
13348     case POPCOUNT:
13349     case PARITY:
13350       mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
13351       break;
13352
13353     case BSWAP:
13354       mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
13355       break;
13356
13357     case ROTATE:
13358     case ROTATERT:
13359       mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
13360       break;
13361
13362     case COMPARE:
13363       /* In theory, we could implement the above.  */
13364       /* DWARF cannot represent the unsigned compare operations
13365          natively.  */
13366     case SS_MULT:
13367     case US_MULT:
13368     case SS_DIV:
13369     case US_DIV:
13370     case SS_PLUS:
13371     case US_PLUS:
13372     case SS_MINUS:
13373     case US_MINUS:
13374     case SS_NEG:
13375     case US_NEG:
13376     case SS_ABS:
13377     case SS_ASHIFT:
13378     case US_ASHIFT:
13379     case SS_TRUNCATE:
13380     case US_TRUNCATE:
13381     case UNORDERED:
13382     case ORDERED:
13383     case UNEQ:
13384     case UNGE:
13385     case UNGT:
13386     case UNLE:
13387     case UNLT:
13388     case LTGT:
13389     case FRACT_CONVERT:
13390     case UNSIGNED_FRACT_CONVERT:
13391     case SAT_FRACT:
13392     case UNSIGNED_SAT_FRACT:
13393     case SQRT:
13394     case ASM_OPERANDS:
13395     case VEC_MERGE:
13396     case VEC_SELECT:
13397     case VEC_CONCAT:
13398     case VEC_DUPLICATE:
13399     case UNSPEC:
13400     case HIGH:
13401     case FMA:
13402     case STRICT_LOW_PART:
13403     case CONST_VECTOR:
13404     case CONST_FIXED:
13405     case CLRSB:
13406     case CLOBBER:
13407       /* If delegitimize_address couldn't do anything with the UNSPEC, we
13408          can't express it in the debug info.  This can happen e.g. with some
13409          TLS UNSPECs.  */
13410       break;
13411
13412     case CONST_STRING:
13413       resolve_one_addr (&rtl);
13414       goto symref;
13415
13416     default:
13417 #ifdef ENABLE_CHECKING
13418       print_rtl (stderr, rtl);
13419       gcc_unreachable ();
13420 #else
13421       break;
13422 #endif
13423     }
13424
13425   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13426     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13427
13428   return mem_loc_result;
13429 }
13430
13431 /* Return a descriptor that describes the concatenation of two locations.
13432    This is typically a complex variable.  */
13433
13434 static dw_loc_descr_ref
13435 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
13436 {
13437   dw_loc_descr_ref cc_loc_result = NULL;
13438   dw_loc_descr_ref x0_ref
13439     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13440   dw_loc_descr_ref x1_ref
13441     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13442
13443   if (x0_ref == 0 || x1_ref == 0)
13444     return 0;
13445
13446   cc_loc_result = x0_ref;
13447   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
13448
13449   add_loc_descr (&cc_loc_result, x1_ref);
13450   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
13451
13452   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13453     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13454
13455   return cc_loc_result;
13456 }
13457
13458 /* Return a descriptor that describes the concatenation of N
13459    locations.  */
13460
13461 static dw_loc_descr_ref
13462 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
13463 {
13464   unsigned int i;
13465   dw_loc_descr_ref cc_loc_result = NULL;
13466   unsigned int n = XVECLEN (concatn, 0);
13467
13468   for (i = 0; i < n; ++i)
13469     {
13470       dw_loc_descr_ref ref;
13471       rtx x = XVECEXP (concatn, 0, i);
13472
13473       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13474       if (ref == NULL)
13475         return NULL;
13476
13477       add_loc_descr (&cc_loc_result, ref);
13478       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
13479     }
13480
13481   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13482     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13483
13484   return cc_loc_result;
13485 }
13486
13487 /* Helper function for loc_descriptor.  Return DW_OP_GNU_implicit_pointer
13488    for DEBUG_IMPLICIT_PTR RTL.  */
13489
13490 static dw_loc_descr_ref
13491 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
13492 {
13493   dw_loc_descr_ref ret;
13494   dw_die_ref ref;
13495
13496   if (dwarf_strict)
13497     return NULL;
13498   gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
13499               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
13500               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
13501   ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
13502   ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
13503   ret->dw_loc_oprnd2.val_class = dw_val_class_const;
13504   if (ref)
13505     {
13506       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13507       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13508       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13509     }
13510   else
13511     {
13512       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13513       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
13514     }
13515   return ret;
13516 }
13517
13518 /* Output a proper Dwarf location descriptor for a variable or parameter
13519    which is either allocated in a register or in a memory location.  For a
13520    register, we just generate an OP_REG and the register number.  For a
13521    memory location we provide a Dwarf postfix expression describing how to
13522    generate the (dynamic) address of the object onto the address stack.
13523
13524    MODE is mode of the decl if this loc_descriptor is going to be used in
13525    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13526    allowed, VOIDmode otherwise.
13527
13528    If we don't know how to describe it, return 0.  */
13529
13530 static dw_loc_descr_ref
13531 loc_descriptor (rtx rtl, machine_mode mode,
13532                 enum var_init_status initialized)
13533 {
13534   dw_loc_descr_ref loc_result = NULL;
13535
13536   switch (GET_CODE (rtl))
13537     {
13538     case SUBREG:
13539       /* The case of a subreg may arise when we have a local (register)
13540          variable or a formal (register) parameter which doesn't quite fill
13541          up an entire register.  For now, just assume that it is
13542          legitimate to make the Dwarf info refer to the whole register which
13543          contains the given subreg.  */
13544       if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
13545         loc_result = loc_descriptor (SUBREG_REG (rtl),
13546                                      GET_MODE (SUBREG_REG (rtl)), initialized);
13547       else
13548         goto do_default;
13549       break;
13550
13551     case REG:
13552       loc_result = reg_loc_descriptor (rtl, initialized);
13553       break;
13554
13555     case MEM:
13556       loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13557                                        GET_MODE (rtl), initialized);
13558       if (loc_result == NULL)
13559         loc_result = tls_mem_loc_descriptor (rtl);
13560       if (loc_result == NULL)
13561         {
13562           rtx new_rtl = avoid_constant_pool_reference (rtl);
13563           if (new_rtl != rtl)
13564             loc_result = loc_descriptor (new_rtl, mode, initialized);
13565         }
13566       break;
13567
13568     case CONCAT:
13569       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13570                                           initialized);
13571       break;
13572
13573     case CONCATN:
13574       loc_result = concatn_loc_descriptor (rtl, initialized);
13575       break;
13576
13577     case VAR_LOCATION:
13578       /* Single part.  */
13579       if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
13580         {
13581           rtx loc = PAT_VAR_LOCATION_LOC (rtl);
13582           if (GET_CODE (loc) == EXPR_LIST)
13583             loc = XEXP (loc, 0);
13584           loc_result = loc_descriptor (loc, mode, initialized);
13585           break;
13586         }
13587
13588       rtl = XEXP (rtl, 1);
13589       /* FALLTHRU */
13590
13591     case PARALLEL:
13592       {
13593         rtvec par_elems = XVEC (rtl, 0);
13594         int num_elem = GET_NUM_ELEM (par_elems);
13595         machine_mode mode;
13596         int i;
13597
13598         /* Create the first one, so we have something to add to.  */
13599         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13600                                      VOIDmode, initialized);
13601         if (loc_result == NULL)
13602           return NULL;
13603         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13604         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13605         for (i = 1; i < num_elem; i++)
13606           {
13607             dw_loc_descr_ref temp;
13608
13609             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13610                                    VOIDmode, initialized);
13611             if (temp == NULL)
13612               return NULL;
13613             add_loc_descr (&loc_result, temp);
13614             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13615             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13616           }
13617       }
13618       break;
13619
13620     case CONST_INT:
13621       if (mode != VOIDmode && mode != BLKmode)
13622         loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13623                                                     INTVAL (rtl));
13624       break;
13625
13626     case CONST_DOUBLE:
13627       if (mode == VOIDmode)
13628         mode = GET_MODE (rtl);
13629
13630       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13631         {
13632           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13633
13634           /* Note that a CONST_DOUBLE rtx could represent either an integer
13635              or a floating-point constant.  A CONST_DOUBLE is used whenever
13636              the constant requires more than one word in order to be
13637              adequately represented.  We output CONST_DOUBLEs as blocks.  */
13638           loc_result = new_loc_descr (DW_OP_implicit_value,
13639                                       GET_MODE_SIZE (mode), 0);
13640 #if TARGET_SUPPORTS_WIDE_INT == 0
13641           if (!SCALAR_FLOAT_MODE_P (mode))
13642             {
13643               loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13644               loc_result->dw_loc_oprnd2.v.val_double
13645                 = rtx_to_double_int (rtl);
13646             }
13647           else
13648 #endif
13649             {
13650               unsigned int length = GET_MODE_SIZE (mode);
13651               unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13652
13653               insert_float (rtl, array);
13654               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13655               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13656               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13657               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13658             }
13659         }
13660       break;
13661
13662     case CONST_WIDE_INT:
13663       if (mode == VOIDmode)
13664         mode = GET_MODE (rtl);
13665
13666       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13667         {
13668           loc_result = new_loc_descr (DW_OP_implicit_value,
13669                                       GET_MODE_SIZE (mode), 0);
13670           loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
13671           loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
13672           *loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13673         }
13674       break;
13675
13676     case CONST_VECTOR:
13677       if (mode == VOIDmode)
13678         mode = GET_MODE (rtl);
13679
13680       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13681         {
13682           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13683           unsigned int length = CONST_VECTOR_NUNITS (rtl);
13684           unsigned char *array
13685             = ggc_vec_alloc<unsigned char> (length * elt_size);
13686           unsigned int i;
13687           unsigned char *p;
13688           machine_mode imode = GET_MODE_INNER (mode);
13689
13690           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13691           switch (GET_MODE_CLASS (mode))
13692             {
13693             case MODE_VECTOR_INT:
13694               for (i = 0, p = array; i < length; i++, p += elt_size)
13695                 {
13696                   rtx elt = CONST_VECTOR_ELT (rtl, i);
13697                   insert_wide_int (std::make_pair (elt, imode), p, elt_size);
13698                 }
13699               break;
13700
13701             case MODE_VECTOR_FLOAT:
13702               for (i = 0, p = array; i < length; i++, p += elt_size)
13703                 {
13704                   rtx elt = CONST_VECTOR_ELT (rtl, i);
13705                   insert_float (elt, p);
13706                 }
13707               break;
13708
13709             default:
13710               gcc_unreachable ();
13711             }
13712
13713           loc_result = new_loc_descr (DW_OP_implicit_value,
13714                                       length * elt_size, 0);
13715           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13716           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13717           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13718           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13719         }
13720       break;
13721
13722     case CONST:
13723       if (mode == VOIDmode
13724           || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13725           || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13726           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13727         {
13728           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13729           break;
13730         }
13731       /* FALLTHROUGH */
13732     case SYMBOL_REF:
13733       if (!const_ok_for_output (rtl))
13734         break;
13735     case LABEL_REF:
13736       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13737           && (dwarf_version >= 4 || !dwarf_strict))
13738         {
13739          loc_result = new_addr_loc_descr (rtl, dtprel_false);
13740           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13741           vec_safe_push (used_rtx_array, rtl);
13742         }
13743       break;
13744
13745     case DEBUG_IMPLICIT_PTR:
13746       loc_result = implicit_ptr_descriptor (rtl, 0);
13747       break;
13748
13749     case PLUS:
13750       if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13751           && CONST_INT_P (XEXP (rtl, 1)))
13752         {
13753           loc_result
13754             = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13755           break;
13756         }
13757       /* FALLTHRU */
13758     do_default:
13759     default:
13760       if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13761            && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13762            && dwarf_version >= 4)
13763           || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13764         {
13765           /* Value expression.  */
13766           loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13767           if (loc_result)
13768             add_loc_descr (&loc_result,
13769                            new_loc_descr (DW_OP_stack_value, 0, 0));
13770         }
13771       break;
13772     }
13773
13774   return loc_result;
13775 }
13776
13777 /* We need to figure out what section we should use as the base for the
13778    address ranges where a given location is valid.
13779    1. If this particular DECL has a section associated with it, use that.
13780    2. If this function has a section associated with it, use that.
13781    3. Otherwise, use the text section.
13782    XXX: If you split a variable across multiple sections, we won't notice.  */
13783
13784 static const char *
13785 secname_for_decl (const_tree decl)
13786 {
13787   const char *secname;
13788
13789   if (VAR_OR_FUNCTION_DECL_P (decl)
13790       && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
13791       && DECL_SECTION_NAME (decl))
13792     secname = DECL_SECTION_NAME (decl);
13793   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13794     secname = DECL_SECTION_NAME (current_function_decl);
13795   else if (cfun && in_cold_section_p)
13796     secname = crtl->subsections.cold_section_label;
13797   else
13798     secname = text_section_label;
13799
13800   return secname;
13801 }
13802
13803 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
13804
13805 static bool
13806 decl_by_reference_p (tree decl)
13807 {
13808   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13809            || TREE_CODE (decl) == VAR_DECL)
13810           && DECL_BY_REFERENCE (decl));
13811 }
13812
13813 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
13814    for VARLOC.  */
13815
13816 static dw_loc_descr_ref
13817 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13818                enum var_init_status initialized)
13819 {
13820   int have_address = 0;
13821   dw_loc_descr_ref descr;
13822   machine_mode mode;
13823
13824   if (want_address != 2)
13825     {
13826       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13827       /* Single part.  */
13828       if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13829         {
13830           varloc = PAT_VAR_LOCATION_LOC (varloc);
13831           if (GET_CODE (varloc) == EXPR_LIST)
13832             varloc = XEXP (varloc, 0);
13833           mode = GET_MODE (varloc);
13834           if (MEM_P (varloc))
13835             {
13836               rtx addr = XEXP (varloc, 0);
13837               descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13838                                           mode, initialized);
13839               if (descr)
13840                 have_address = 1;
13841               else
13842                 {
13843                   rtx x = avoid_constant_pool_reference (varloc);
13844                   if (x != varloc)
13845                     descr = mem_loc_descriptor (x, mode, VOIDmode,
13846                                                 initialized);
13847                 }
13848             }
13849           else
13850             descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13851         }
13852       else
13853         return 0;
13854     }
13855   else
13856     {
13857       if (GET_CODE (varloc) == VAR_LOCATION)
13858         mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13859       else
13860         mode = DECL_MODE (loc);
13861       descr = loc_descriptor (varloc, mode, initialized);
13862       have_address = 1;
13863     }
13864
13865   if (!descr)
13866     return 0;
13867
13868   if (want_address == 2 && !have_address
13869       && (dwarf_version >= 4 || !dwarf_strict))
13870     {
13871       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13872         {
13873           expansion_failed (loc, NULL_RTX,
13874                             "DWARF address size mismatch");
13875           return 0;
13876         }
13877       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13878       have_address = 1;
13879     }
13880   /* Show if we can't fill the request for an address.  */
13881   if (want_address && !have_address)
13882     {
13883       expansion_failed (loc, NULL_RTX,
13884                         "Want address and only have value");
13885       return 0;
13886     }
13887
13888   /* If we've got an address and don't want one, dereference.  */
13889   if (!want_address && have_address)
13890     {
13891       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13892       enum dwarf_location_atom op;
13893
13894       if (size > DWARF2_ADDR_SIZE || size == -1)
13895         {
13896           expansion_failed (loc, NULL_RTX,
13897                             "DWARF address size mismatch");
13898           return 0;
13899         }
13900       else if (size == DWARF2_ADDR_SIZE)
13901         op = DW_OP_deref;
13902       else
13903         op = DW_OP_deref_size;
13904
13905       add_loc_descr (&descr, new_loc_descr (op, size, 0));
13906     }
13907
13908   return descr;
13909 }
13910
13911 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
13912    if it is not possible.  */
13913
13914 static dw_loc_descr_ref
13915 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
13916 {
13917   if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
13918     return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
13919   else if (dwarf_version >= 3 || !dwarf_strict)
13920     return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
13921   else
13922     return NULL;
13923 }
13924
13925 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
13926    for VAR_LOC_NOTE for variable DECL that has been optimized by SRA.  */
13927
13928 static dw_loc_descr_ref
13929 dw_sra_loc_expr (tree decl, rtx loc)
13930 {
13931   rtx p;
13932   unsigned HOST_WIDE_INT padsize = 0;
13933   dw_loc_descr_ref descr, *descr_tail;
13934   unsigned HOST_WIDE_INT decl_size;
13935   rtx varloc;
13936   enum var_init_status initialized;
13937
13938   if (DECL_SIZE (decl) == NULL
13939       || !tree_fits_uhwi_p (DECL_SIZE (decl)))
13940     return NULL;
13941
13942   decl_size = tree_to_uhwi (DECL_SIZE (decl));
13943   descr = NULL;
13944   descr_tail = &descr;
13945
13946   for (p = loc; p; p = XEXP (p, 1))
13947     {
13948       unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
13949       rtx loc_note = *decl_piece_varloc_ptr (p);
13950       dw_loc_descr_ref cur_descr;
13951       dw_loc_descr_ref *tail, last = NULL;
13952       unsigned HOST_WIDE_INT opsize = 0;
13953
13954       if (loc_note == NULL_RTX
13955           || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
13956         {
13957           padsize += bitsize;
13958           continue;
13959         }
13960       initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
13961       varloc = NOTE_VAR_LOCATION (loc_note);
13962       cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
13963       if (cur_descr == NULL)
13964         {
13965           padsize += bitsize;
13966           continue;
13967         }
13968
13969       /* Check that cur_descr either doesn't use
13970          DW_OP_*piece operations, or their sum is equal
13971          to bitsize.  Otherwise we can't embed it.  */
13972       for (tail = &cur_descr; *tail != NULL;
13973            tail = &(*tail)->dw_loc_next)
13974         if ((*tail)->dw_loc_opc == DW_OP_piece)
13975           {
13976             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
13977                       * BITS_PER_UNIT;
13978             last = *tail;
13979           }
13980         else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
13981           {
13982             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
13983             last = *tail;
13984           }
13985
13986       if (last != NULL && opsize != bitsize)
13987         {
13988           padsize += bitsize;
13989           /* Discard the current piece of the descriptor and release any
13990              addr_table entries it uses.  */
13991           remove_loc_list_addr_table_entries (cur_descr);
13992           continue;
13993         }
13994
13995       /* If there is a hole, add DW_OP_*piece after empty DWARF
13996          expression, which means that those bits are optimized out.  */
13997       if (padsize)
13998         {
13999           if (padsize > decl_size)
14000             {
14001               remove_loc_list_addr_table_entries (cur_descr);
14002               goto discard_descr;
14003             }
14004           decl_size -= padsize;
14005           *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
14006           if (*descr_tail == NULL)
14007             {
14008               remove_loc_list_addr_table_entries (cur_descr);
14009               goto discard_descr;
14010             }
14011           descr_tail = &(*descr_tail)->dw_loc_next;
14012           padsize = 0;
14013         }
14014       *descr_tail = cur_descr;
14015       descr_tail = tail;
14016       if (bitsize > decl_size)
14017         goto discard_descr;
14018       decl_size -= bitsize;
14019       if (last == NULL)
14020         {
14021           HOST_WIDE_INT offset = 0;
14022           if (GET_CODE (varloc) == VAR_LOCATION
14023               && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14024             {
14025               varloc = PAT_VAR_LOCATION_LOC (varloc);
14026               if (GET_CODE (varloc) == EXPR_LIST)
14027                 varloc = XEXP (varloc, 0);
14028             }
14029           do 
14030             {
14031               if (GET_CODE (varloc) == CONST
14032                   || GET_CODE (varloc) == SIGN_EXTEND
14033                   || GET_CODE (varloc) == ZERO_EXTEND)
14034                 varloc = XEXP (varloc, 0);
14035               else if (GET_CODE (varloc) == SUBREG)
14036                 varloc = SUBREG_REG (varloc);
14037               else
14038                 break;
14039             }
14040           while (1);
14041           /* DW_OP_bit_size offset should be zero for register
14042              or implicit location descriptions and empty location
14043              descriptions, but for memory addresses needs big endian
14044              adjustment.  */
14045           if (MEM_P (varloc))
14046             {
14047               unsigned HOST_WIDE_INT memsize
14048                 = MEM_SIZE (varloc) * BITS_PER_UNIT;
14049               if (memsize != bitsize)
14050                 {
14051                   if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
14052                       && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
14053                     goto discard_descr;
14054                   if (memsize < bitsize)
14055                     goto discard_descr;
14056                   if (BITS_BIG_ENDIAN)
14057                     offset = memsize - bitsize;
14058                 }
14059             }
14060
14061           *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
14062           if (*descr_tail == NULL)
14063             goto discard_descr;
14064           descr_tail = &(*descr_tail)->dw_loc_next;
14065         }
14066     }
14067
14068   /* If there were any non-empty expressions, add padding till the end of
14069      the decl.  */
14070   if (descr != NULL && decl_size != 0)
14071     {
14072       *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
14073       if (*descr_tail == NULL)
14074         goto discard_descr;
14075     }
14076   return descr;
14077
14078 discard_descr:
14079   /* Discard the descriptor and release any addr_table entries it uses.  */
14080   remove_loc_list_addr_table_entries (descr);
14081   return NULL;
14082 }
14083
14084 /* Return the dwarf representation of the location list LOC_LIST of
14085    DECL.  WANT_ADDRESS has the same meaning as in loc_list_from_tree
14086    function.  */
14087
14088 static dw_loc_list_ref
14089 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
14090 {
14091   const char *endname, *secname;
14092   rtx varloc;
14093   enum var_init_status initialized;
14094   struct var_loc_node *node;
14095   dw_loc_descr_ref descr;
14096   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
14097   dw_loc_list_ref list = NULL;
14098   dw_loc_list_ref *listp = &list;
14099
14100   /* Now that we know what section we are using for a base,
14101      actually construct the list of locations.
14102      The first location information is what is passed to the
14103      function that creates the location list, and the remaining
14104      locations just get added on to that list.
14105      Note that we only know the start address for a location
14106      (IE location changes), so to build the range, we use
14107      the range [current location start, next location start].
14108      This means we have to special case the last node, and generate
14109      a range of [last location start, end of function label].  */
14110
14111   secname = secname_for_decl (decl);
14112
14113   for (node = loc_list->first; node; node = node->next)
14114     if (GET_CODE (node->loc) == EXPR_LIST
14115         || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
14116       {
14117         if (GET_CODE (node->loc) == EXPR_LIST)
14118           {
14119             /* This requires DW_OP_{,bit_}piece, which is not usable
14120                inside DWARF expressions.  */
14121             if (want_address != 2)
14122               continue;
14123             descr = dw_sra_loc_expr (decl, node->loc);
14124             if (descr == NULL)
14125               continue;
14126           }
14127         else
14128           {
14129             initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14130             varloc = NOTE_VAR_LOCATION (node->loc);
14131             descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
14132           }
14133         if (descr)
14134           {
14135             bool range_across_switch = false;
14136             /* If section switch happens in between node->label
14137                and node->next->label (or end of function) and
14138                we can't emit it as a single entry list,
14139                emit two ranges, first one ending at the end
14140                of first partition and second one starting at the
14141                beginning of second partition.  */
14142             if (node == loc_list->last_before_switch
14143                 && (node != loc_list->first || loc_list->first->next)
14144                 && current_function_decl)
14145               {
14146                 endname = cfun->fde->dw_fde_end;
14147                 range_across_switch = true;
14148               }
14149             /* The variable has a location between NODE->LABEL and
14150                NODE->NEXT->LABEL.  */
14151             else if (node->next)
14152               endname = node->next->label;
14153             /* If the variable has a location at the last label
14154                it keeps its location until the end of function.  */
14155             else if (!current_function_decl)
14156               endname = text_end_label;
14157             else
14158               {
14159                 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14160                                              current_function_funcdef_no);
14161                 endname = ggc_strdup (label_id);
14162               }
14163
14164             *listp = new_loc_list (descr, node->label, endname, secname);
14165             if (TREE_CODE (decl) == PARM_DECL
14166                 && node == loc_list->first
14167                 && NOTE_P (node->loc)
14168                 && strcmp (node->label, endname) == 0)
14169               (*listp)->force = true;
14170             listp = &(*listp)->dw_loc_next;
14171
14172             if (range_across_switch)
14173               {
14174                 if (GET_CODE (node->loc) == EXPR_LIST)
14175                   descr = dw_sra_loc_expr (decl, node->loc);
14176                 else
14177                   {
14178                     initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14179                     varloc = NOTE_VAR_LOCATION (node->loc);
14180                     descr = dw_loc_list_1 (decl, varloc, want_address,
14181                                            initialized);
14182                   }
14183                 gcc_assert (descr);
14184                 /* The variable has a location between NODE->LABEL and
14185                    NODE->NEXT->LABEL.  */
14186                 if (node->next)
14187                   endname = node->next->label;
14188                 else
14189                   endname = cfun->fde->dw_fde_second_end;
14190                 *listp = new_loc_list (descr,
14191                                        cfun->fde->dw_fde_second_begin,
14192                                        endname, secname);
14193                 listp = &(*listp)->dw_loc_next;
14194               }
14195           }
14196       }
14197
14198   /* Try to avoid the overhead of a location list emitting a location
14199      expression instead, but only if we didn't have more than one
14200      location entry in the first place.  If some entries were not
14201      representable, we don't want to pretend a single entry that was
14202      applies to the entire scope in which the variable is
14203      available.  */
14204   if (list && loc_list->first->next)
14205     gen_llsym (list);
14206
14207   return list;
14208 }
14209
14210 /* Return if the loc_list has only single element and thus can be represented
14211    as location description.   */
14212
14213 static bool
14214 single_element_loc_list_p (dw_loc_list_ref list)
14215 {
14216   gcc_assert (!list->dw_loc_next || list->ll_symbol);
14217   return !list->ll_symbol;
14218 }
14219
14220 /* To each location in list LIST add loc descr REF.  */
14221
14222 static void
14223 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
14224 {
14225   dw_loc_descr_ref copy;
14226   add_loc_descr (&list->expr, ref);
14227   list = list->dw_loc_next;
14228   while (list)
14229     {
14230       copy = ggc_alloc<dw_loc_descr_node> ();
14231       memcpy (copy, ref, sizeof (dw_loc_descr_node));
14232       add_loc_descr (&list->expr, copy);
14233       while (copy->dw_loc_next)
14234         {
14235           dw_loc_descr_ref new_copy = ggc_alloc<dw_loc_descr_node> ();
14236           memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
14237           copy->dw_loc_next = new_copy;
14238           copy = new_copy;
14239         }
14240       list = list->dw_loc_next;
14241     }
14242 }
14243
14244 /* Given two lists RET and LIST
14245    produce location list that is result of adding expression in LIST
14246    to expression in RET on each position in program.
14247    Might be destructive on both RET and LIST.
14248
14249    TODO: We handle only simple cases of RET or LIST having at most one
14250    element. General case would inolve sorting the lists in program order
14251    and merging them that will need some additional work.
14252    Adding that will improve quality of debug info especially for SRA-ed
14253    structures.  */
14254
14255 static void
14256 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
14257 {
14258   if (!list)
14259     return;
14260   if (!*ret)
14261     {
14262       *ret = list;
14263       return;
14264     }
14265   if (!list->dw_loc_next)
14266     {
14267       add_loc_descr_to_each (*ret, list->expr);
14268       return;
14269     }
14270   if (!(*ret)->dw_loc_next)
14271     {
14272       add_loc_descr_to_each (list, (*ret)->expr);
14273       *ret = list;
14274       return;
14275     }
14276   expansion_failed (NULL_TREE, NULL_RTX,
14277                     "Don't know how to merge two non-trivial"
14278                     " location lists.\n");
14279   *ret = NULL;
14280   return;
14281 }
14282
14283 /* LOC is constant expression.  Try a luck, look it up in constant
14284    pool and return its loc_descr of its address.  */
14285
14286 static dw_loc_descr_ref
14287 cst_pool_loc_descr (tree loc)
14288 {
14289   /* Get an RTL for this, if something has been emitted.  */
14290   rtx rtl = lookup_constant_def (loc);
14291
14292   if (!rtl || !MEM_P (rtl))
14293     {
14294       gcc_assert (!rtl);
14295       return 0;
14296     }
14297   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
14298
14299   /* TODO: We might get more coverage if we was actually delaying expansion
14300      of all expressions till end of compilation when constant pools are fully
14301      populated.  */
14302   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
14303     {
14304       expansion_failed (loc, NULL_RTX,
14305                         "CST value in contant pool but not marked.");
14306       return 0;
14307     }
14308   return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
14309                              GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
14310 }
14311
14312 /* Return dw_loc_list representing address of addr_expr LOC
14313    by looking for inner INDIRECT_REF expression and turning
14314    it into simple arithmetics.
14315
14316    See loc_list_from_tree for the meaning of CONTEXT.  */
14317
14318 static dw_loc_list_ref
14319 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
14320                                                    const loc_descr_context *context)
14321 {
14322   tree obj, offset;
14323   HOST_WIDE_INT bitsize, bitpos, bytepos;
14324   machine_mode mode;
14325   int unsignedp, volatilep = 0;
14326   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14327
14328   obj = get_inner_reference (TREE_OPERAND (loc, 0),
14329                              &bitsize, &bitpos, &offset, &mode,
14330                              &unsignedp, &volatilep, false);
14331   STRIP_NOPS (obj);
14332   if (bitpos % BITS_PER_UNIT)
14333     {
14334       expansion_failed (loc, NULL_RTX, "bitfield access");
14335       return 0;
14336     }
14337   if (!INDIRECT_REF_P (obj))
14338     {
14339       expansion_failed (obj,
14340                         NULL_RTX, "no indirect ref in inner refrence");
14341       return 0;
14342     }
14343   if (!offset && !bitpos)
14344     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
14345                                    context);
14346   else if (toplev
14347            && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
14348            && (dwarf_version >= 4 || !dwarf_strict))
14349     {
14350       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
14351       if (!list_ret)
14352         return 0;
14353       if (offset)
14354         {
14355           /* Variable offset.  */
14356           list_ret1 = loc_list_from_tree (offset, 0, context);
14357           if (list_ret1 == 0)
14358             return 0;
14359           add_loc_list (&list_ret, list_ret1);
14360           if (!list_ret)
14361             return 0;
14362           add_loc_descr_to_each (list_ret,
14363                                  new_loc_descr (DW_OP_plus, 0, 0));
14364         }
14365       bytepos = bitpos / BITS_PER_UNIT;
14366       if (bytepos > 0)
14367         add_loc_descr_to_each (list_ret,
14368                                new_loc_descr (DW_OP_plus_uconst,
14369                                               bytepos, 0));
14370       else if (bytepos < 0)
14371         loc_list_plus_const (list_ret, bytepos);
14372       add_loc_descr_to_each (list_ret,
14373                              new_loc_descr (DW_OP_stack_value, 0, 0));
14374     }
14375   return list_ret;
14376 }
14377
14378
14379 /* Helper structure for location descriptions generation.  */
14380 struct loc_descr_context
14381 {
14382   /* The type that is implicitly referenced by DW_OP_push_object_address, or
14383      NULL_TREE if DW_OP_push_object_address in invalid for this location
14384      description.  This is used when processing PLACEHOLDER_EXPR nodes.  */
14385   tree context_type;
14386   /* The ..._DECL node that should be translated as a
14387      DW_OP_push_object_address operation.  */
14388   tree base_decl;
14389 };
14390
14391 /* Generate Dwarf location list representing LOC.
14392    If WANT_ADDRESS is false, expression computing LOC will be computed
14393    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
14394    if WANT_ADDRESS is 2, expression computing address useable in location
14395      will be returned (i.e. DW_OP_reg can be used
14396      to refer to register values).
14397
14398    CONTEXT provides information to customize the location descriptions
14399    generation.  Its context_type field specifies what type is implicitly
14400    referenced by DW_OP_push_object_address.  If it is NULL_TREE, this operation
14401    will not be generated.
14402
14403    If CONTEXT is NULL, the behavior is the same as if both context_type and
14404    base_decl fields were NULL_TREE.  */
14405
14406 static dw_loc_list_ref
14407 loc_list_from_tree (tree loc, int want_address,
14408                     const struct loc_descr_context *context)
14409 {
14410   dw_loc_descr_ref ret = NULL, ret1 = NULL;
14411   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14412   int have_address = 0;
14413   enum dwarf_location_atom op;
14414
14415   /* ??? Most of the time we do not take proper care for sign/zero
14416      extending the values properly.  Hopefully this won't be a real
14417      problem...  */
14418
14419   if (context != NULL
14420       && context->base_decl == loc
14421       && want_address == 0)
14422     {
14423       if (dwarf_version >= 3 || !dwarf_strict)
14424         return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
14425                              NULL, NULL, NULL);
14426       else
14427         return NULL;
14428     }
14429
14430   switch (TREE_CODE (loc))
14431     {
14432     case ERROR_MARK:
14433       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
14434       return 0;
14435
14436     case PLACEHOLDER_EXPR:
14437       /* This case involves extracting fields from an object to determine the
14438          position of other fields. It is supposed to appear only as the first
14439          operand of COMPONENT_REF nodes and to reference precisely the type
14440          that the context allows.  */
14441       if (context != NULL
14442           && TREE_TYPE (loc) == context->context_type
14443           && want_address >= 1)
14444         {
14445           if (dwarf_version >= 3 || !dwarf_strict)
14446             {
14447               ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
14448               have_address = 1;
14449               break;
14450             }
14451           else
14452             return NULL;
14453         }
14454       else
14455         expansion_failed (loc, NULL_RTX,
14456                           "PLACEHOLDER_EXPR for an unexpected type");
14457       break;
14458
14459     case CALL_EXPR:
14460       expansion_failed (loc, NULL_RTX, "CALL_EXPR");
14461       /* There are no opcodes for these operations.  */
14462       return 0;
14463
14464     case PREINCREMENT_EXPR:
14465     case PREDECREMENT_EXPR:
14466     case POSTINCREMENT_EXPR:
14467     case POSTDECREMENT_EXPR:
14468       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
14469       /* There are no opcodes for these operations.  */
14470       return 0;
14471
14472     case ADDR_EXPR:
14473       /* If we already want an address, see if there is INDIRECT_REF inside
14474          e.g. for &this->field.  */
14475       if (want_address)
14476         {
14477           list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
14478                        (loc, want_address == 2, context);
14479           if (list_ret)
14480             have_address = 1;
14481           else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
14482                    && (ret = cst_pool_loc_descr (loc)))
14483             have_address = 1;
14484         }
14485         /* Otherwise, process the argument and look for the address.  */
14486       if (!list_ret && !ret)
14487         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1, context);
14488       else
14489         {
14490           if (want_address)
14491             expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
14492           return NULL;
14493         }
14494       break;
14495
14496     case VAR_DECL:
14497       if (DECL_THREAD_LOCAL_P (loc))
14498         {
14499           rtx rtl;
14500          enum dwarf_location_atom tls_op;
14501          enum dtprel_bool dtprel = dtprel_false;
14502
14503           if (targetm.have_tls)
14504             {
14505               /* If this is not defined, we have no way to emit the
14506                  data.  */
14507               if (!targetm.asm_out.output_dwarf_dtprel)
14508                 return 0;
14509
14510                /* The way DW_OP_GNU_push_tls_address is specified, we
14511                   can only look up addresses of objects in the current
14512                   module.  We used DW_OP_addr as first op, but that's
14513                   wrong, because DW_OP_addr is relocated by the debug
14514                   info consumer, while DW_OP_GNU_push_tls_address
14515                   operand shouldn't be.  */
14516               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
14517                 return 0;
14518              dtprel = dtprel_true;
14519              tls_op = DW_OP_GNU_push_tls_address;
14520             }
14521           else
14522             {
14523               if (!targetm.emutls.debug_form_tls_address
14524                   || !(dwarf_version >= 3 || !dwarf_strict))
14525                 return 0;
14526               /* We stuffed the control variable into the DECL_VALUE_EXPR
14527                  to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
14528                  no longer appear in gimple code.  We used the control
14529                  variable in specific so that we could pick it up here.  */
14530               loc = DECL_VALUE_EXPR (loc);
14531               tls_op = DW_OP_form_tls_address;
14532             }
14533
14534           rtl = rtl_for_decl_location (loc);
14535           if (rtl == NULL_RTX)
14536             return 0;
14537
14538           if (!MEM_P (rtl))
14539             return 0;
14540           rtl = XEXP (rtl, 0);
14541           if (! CONSTANT_P (rtl))
14542             return 0;
14543
14544           ret = new_addr_loc_descr (rtl, dtprel);
14545           ret1 = new_loc_descr (tls_op, 0, 0);
14546           add_loc_descr (&ret, ret1);
14547
14548           have_address = 1;
14549           break;
14550         }
14551       /* FALLTHRU */
14552
14553     case PARM_DECL:
14554     case RESULT_DECL:
14555       if (DECL_HAS_VALUE_EXPR_P (loc))
14556         return loc_list_from_tree (DECL_VALUE_EXPR (loc),
14557                                    want_address, context);
14558       /* FALLTHRU */
14559
14560     case FUNCTION_DECL:
14561       {
14562         rtx rtl;
14563         var_loc_list *loc_list = lookup_decl_loc (loc);
14564
14565         if (loc_list && loc_list->first)
14566           {
14567             list_ret = dw_loc_list (loc_list, loc, want_address);
14568             have_address = want_address != 0;
14569             break;
14570           }
14571         rtl = rtl_for_decl_location (loc);
14572         if (rtl == NULL_RTX)
14573           {
14574             expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14575             return 0;
14576           }
14577         else if (CONST_INT_P (rtl))
14578           {
14579             HOST_WIDE_INT val = INTVAL (rtl);
14580             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14581               val &= GET_MODE_MASK (DECL_MODE (loc));
14582             ret = int_loc_descriptor (val);
14583           }
14584         else if (GET_CODE (rtl) == CONST_STRING)
14585           {
14586             expansion_failed (loc, NULL_RTX, "CONST_STRING");
14587             return 0;
14588           }
14589         else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14590           ret = new_addr_loc_descr (rtl, dtprel_false);
14591         else
14592           {
14593             machine_mode mode, mem_mode;
14594
14595             /* Certain constructs can only be represented at top-level.  */
14596             if (want_address == 2)
14597               {
14598                 ret = loc_descriptor (rtl, VOIDmode,
14599                                       VAR_INIT_STATUS_INITIALIZED);
14600                 have_address = 1;
14601               }
14602             else
14603               {
14604                 mode = GET_MODE (rtl);
14605                 mem_mode = VOIDmode;
14606                 if (MEM_P (rtl))
14607                   {
14608                     mem_mode = mode;
14609                     mode = get_address_mode (rtl);
14610                     rtl = XEXP (rtl, 0);
14611                     have_address = 1;
14612                   }
14613                 ret = mem_loc_descriptor (rtl, mode, mem_mode,
14614                                           VAR_INIT_STATUS_INITIALIZED);
14615               }
14616             if (!ret)
14617               expansion_failed (loc, rtl,
14618                                 "failed to produce loc descriptor for rtl");
14619           }
14620       }
14621       break;
14622
14623     case MEM_REF:
14624       if (!integer_zerop (TREE_OPERAND (loc, 1)))
14625         {
14626           have_address = 1;
14627           goto do_plus;
14628         }
14629       /* Fallthru.  */
14630     case INDIRECT_REF:
14631       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14632       have_address = 1;
14633       break;
14634
14635     case TARGET_MEM_REF:
14636     case SSA_NAME:
14637       return NULL;
14638
14639     case COMPOUND_EXPR:
14640       return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address, context);
14641
14642     CASE_CONVERT:
14643     case VIEW_CONVERT_EXPR:
14644     case SAVE_EXPR:
14645     case MODIFY_EXPR:
14646       return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address, context);
14647
14648     case COMPONENT_REF:
14649     case BIT_FIELD_REF:
14650     case ARRAY_REF:
14651     case ARRAY_RANGE_REF:
14652     case REALPART_EXPR:
14653     case IMAGPART_EXPR:
14654       {
14655         tree obj, offset;
14656         HOST_WIDE_INT bitsize, bitpos, bytepos;
14657         machine_mode mode;
14658         int unsignedp, volatilep = 0;
14659
14660         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14661                                    &unsignedp, &volatilep, false);
14662
14663         gcc_assert (obj != loc);
14664
14665         list_ret = loc_list_from_tree (obj,
14666                                        want_address == 2
14667                                        && !bitpos && !offset ? 2 : 1,
14668                                        context);
14669         /* TODO: We can extract value of the small expression via shifting even
14670            for nonzero bitpos.  */
14671         if (list_ret == 0)
14672           return 0;
14673         if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14674           {
14675             expansion_failed (loc, NULL_RTX,
14676                               "bitfield access");
14677             return 0;
14678           }
14679
14680         if (offset != NULL_TREE)
14681           {
14682             /* Variable offset.  */
14683             list_ret1 = loc_list_from_tree (offset, 0, context);
14684             if (list_ret1 == 0)
14685               return 0;
14686             add_loc_list (&list_ret, list_ret1);
14687             if (!list_ret)
14688               return 0;
14689             add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14690           }
14691
14692         bytepos = bitpos / BITS_PER_UNIT;
14693         if (bytepos > 0)
14694           add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14695         else if (bytepos < 0)
14696           loc_list_plus_const (list_ret, bytepos);
14697
14698         have_address = 1;
14699         break;
14700       }
14701
14702     case INTEGER_CST:
14703       if ((want_address || !tree_fits_shwi_p (loc))
14704           && (ret = cst_pool_loc_descr (loc)))
14705         have_address = 1;
14706       else if (want_address == 2
14707                && tree_fits_shwi_p (loc)
14708                && (ret = address_of_int_loc_descriptor
14709                            (int_size_in_bytes (TREE_TYPE (loc)),
14710                             tree_to_shwi (loc))))
14711         have_address = 1;
14712       else if (tree_fits_shwi_p (loc))
14713         ret = int_loc_descriptor (tree_to_shwi (loc));
14714       else
14715         {
14716           expansion_failed (loc, NULL_RTX,
14717                             "Integer operand is not host integer");
14718           return 0;
14719         }
14720       break;
14721
14722     case CONSTRUCTOR:
14723     case REAL_CST:
14724     case STRING_CST:
14725     case COMPLEX_CST:
14726       if ((ret = cst_pool_loc_descr (loc)))
14727         have_address = 1;
14728       else
14729       /* We can construct small constants here using int_loc_descriptor.  */
14730         expansion_failed (loc, NULL_RTX,
14731                           "constructor or constant not in constant pool");
14732       break;
14733
14734     case TRUTH_AND_EXPR:
14735     case TRUTH_ANDIF_EXPR:
14736     case BIT_AND_EXPR:
14737       op = DW_OP_and;
14738       goto do_binop;
14739
14740     case TRUTH_XOR_EXPR:
14741     case BIT_XOR_EXPR:
14742       op = DW_OP_xor;
14743       goto do_binop;
14744
14745     case TRUTH_OR_EXPR:
14746     case TRUTH_ORIF_EXPR:
14747     case BIT_IOR_EXPR:
14748       op = DW_OP_or;
14749       goto do_binop;
14750
14751     case FLOOR_DIV_EXPR:
14752     case CEIL_DIV_EXPR:
14753     case ROUND_DIV_EXPR:
14754     case TRUNC_DIV_EXPR:
14755       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14756         return 0;
14757       op = DW_OP_div;
14758       goto do_binop;
14759
14760     case MINUS_EXPR:
14761       op = DW_OP_minus;
14762       goto do_binop;
14763
14764     case FLOOR_MOD_EXPR:
14765     case CEIL_MOD_EXPR:
14766     case ROUND_MOD_EXPR:
14767     case TRUNC_MOD_EXPR:
14768       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14769         {
14770           op = DW_OP_mod;
14771           goto do_binop;
14772         }
14773       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14774       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
14775       if (list_ret == 0 || list_ret1 == 0)
14776         return 0;
14777
14778       add_loc_list (&list_ret, list_ret1);
14779       if (list_ret == 0)
14780         return 0;
14781       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14782       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14783       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14784       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14785       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14786       break;
14787
14788     case MULT_EXPR:
14789       op = DW_OP_mul;
14790       goto do_binop;
14791
14792     case LSHIFT_EXPR:
14793       op = DW_OP_shl;
14794       goto do_binop;
14795
14796     case RSHIFT_EXPR:
14797       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14798       goto do_binop;
14799
14800     case POINTER_PLUS_EXPR:
14801     case PLUS_EXPR:
14802     do_plus:
14803       if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
14804         {
14805           list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14806           if (list_ret == 0)
14807             return 0;
14808
14809           loc_list_plus_const (list_ret, tree_to_shwi (TREE_OPERAND (loc, 1)));
14810           break;
14811         }
14812
14813       op = DW_OP_plus;
14814       goto do_binop;
14815
14816     case LE_EXPR:
14817       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14818         return 0;
14819
14820       op = DW_OP_le;
14821       goto do_binop;
14822
14823     case GE_EXPR:
14824       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14825         return 0;
14826
14827       op = DW_OP_ge;
14828       goto do_binop;
14829
14830     case LT_EXPR:
14831       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14832         return 0;
14833
14834       op = DW_OP_lt;
14835       goto do_binop;
14836
14837     case GT_EXPR:
14838       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14839         return 0;
14840
14841       op = DW_OP_gt;
14842       goto do_binop;
14843
14844     case EQ_EXPR:
14845       op = DW_OP_eq;
14846       goto do_binop;
14847
14848     case NE_EXPR:
14849       op = DW_OP_ne;
14850       goto do_binop;
14851
14852     do_binop:
14853       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14854       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
14855       if (list_ret == 0 || list_ret1 == 0)
14856         return 0;
14857
14858       add_loc_list (&list_ret, list_ret1);
14859       if (list_ret == 0)
14860         return 0;
14861       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14862       break;
14863
14864     case TRUTH_NOT_EXPR:
14865     case BIT_NOT_EXPR:
14866       op = DW_OP_not;
14867       goto do_unop;
14868
14869     case ABS_EXPR:
14870       op = DW_OP_abs;
14871       goto do_unop;
14872
14873     case NEGATE_EXPR:
14874       op = DW_OP_neg;
14875       goto do_unop;
14876
14877     do_unop:
14878       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14879       if (list_ret == 0)
14880         return 0;
14881
14882       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14883       break;
14884
14885     case MIN_EXPR:
14886     case MAX_EXPR:
14887       {
14888         const enum tree_code code =
14889           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14890
14891         loc = build3 (COND_EXPR, TREE_TYPE (loc),
14892                       build2 (code, integer_type_node,
14893                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14894                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14895       }
14896
14897       /* ... fall through ...  */
14898
14899     case COND_EXPR:
14900       {
14901         dw_loc_descr_ref lhs
14902           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
14903         dw_loc_list_ref rhs
14904           = loc_list_from_tree (TREE_OPERAND (loc, 2), 0, context);
14905         dw_loc_descr_ref bra_node, jump_node, tmp;
14906
14907         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14908         if (list_ret == 0 || lhs == 0 || rhs == 0)
14909           return 0;
14910
14911         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14912         add_loc_descr_to_each (list_ret, bra_node);
14913
14914         add_loc_list (&list_ret, rhs);
14915         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14916         add_loc_descr_to_each (list_ret, jump_node);
14917
14918         add_loc_descr_to_each (list_ret, lhs);
14919         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14920         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14921
14922         /* ??? Need a node to point the skip at.  Use a nop.  */
14923         tmp = new_loc_descr (DW_OP_nop, 0, 0);
14924         add_loc_descr_to_each (list_ret, tmp);
14925         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14926         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14927       }
14928       break;
14929
14930     case FIX_TRUNC_EXPR:
14931       return 0;
14932
14933     default:
14934       /* Leave front-end specific codes as simply unknown.  This comes
14935          up, for instance, with the C STMT_EXPR.  */
14936       if ((unsigned int) TREE_CODE (loc)
14937           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14938         {
14939           expansion_failed (loc, NULL_RTX,
14940                             "language specific tree node");
14941           return 0;
14942         }
14943
14944 #ifdef ENABLE_CHECKING
14945       /* Otherwise this is a generic code; we should just lists all of
14946          these explicitly.  We forgot one.  */
14947       gcc_unreachable ();
14948 #else
14949       /* In a release build, we want to degrade gracefully: better to
14950          generate incomplete debugging information than to crash.  */
14951       return NULL;
14952 #endif
14953     }
14954
14955   if (!ret && !list_ret)
14956     return 0;
14957
14958   if (want_address == 2 && !have_address
14959       && (dwarf_version >= 4 || !dwarf_strict))
14960     {
14961       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14962         {
14963           expansion_failed (loc, NULL_RTX,
14964                             "DWARF address size mismatch");
14965           return 0;
14966         }
14967       if (ret)
14968         add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
14969       else
14970         add_loc_descr_to_each (list_ret,
14971                                new_loc_descr (DW_OP_stack_value, 0, 0));
14972       have_address = 1;
14973     }
14974   /* Show if we can't fill the request for an address.  */
14975   if (want_address && !have_address)
14976     {
14977       expansion_failed (loc, NULL_RTX,
14978                         "Want address and only have value");
14979       return 0;
14980     }
14981
14982   gcc_assert (!ret || !list_ret);
14983
14984   /* If we've got an address and don't want one, dereference.  */
14985   if (!want_address && have_address)
14986     {
14987       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14988
14989       if (size > DWARF2_ADDR_SIZE || size == -1)
14990         {
14991           expansion_failed (loc, NULL_RTX,
14992                             "DWARF address size mismatch");
14993           return 0;
14994         }
14995       else if (size == DWARF2_ADDR_SIZE)
14996         op = DW_OP_deref;
14997       else
14998         op = DW_OP_deref_size;
14999
15000       if (ret)
15001         add_loc_descr (&ret, new_loc_descr (op, size, 0));
15002       else
15003         add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
15004     }
15005   if (ret)
15006     list_ret = new_loc_list (ret, NULL, NULL, NULL);
15007
15008   return list_ret;
15009 }
15010
15011 /* Same as above but return only single location expression.  */
15012 static dw_loc_descr_ref
15013 loc_descriptor_from_tree (tree loc, int want_address,
15014                           const struct loc_descr_context *context)
15015 {
15016   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
15017   if (!ret)
15018     return NULL;
15019   if (ret->dw_loc_next)
15020     {
15021       expansion_failed (loc, NULL_RTX,
15022                         "Location list where only loc descriptor needed");
15023       return NULL;
15024     }
15025   return ret->expr;
15026 }
15027
15028 /* Given a value, round it up to the lowest multiple of `boundary'
15029    which is not less than the value itself.  */
15030
15031 static inline HOST_WIDE_INT
15032 ceiling (HOST_WIDE_INT value, unsigned int boundary)
15033 {
15034   return (((value + boundary - 1) / boundary) * boundary);
15035 }
15036
15037 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
15038    pointer to the declared type for the relevant field variable, or return
15039    `integer_type_node' if the given node turns out to be an
15040    ERROR_MARK node.  */
15041
15042 static inline tree
15043 field_type (const_tree decl)
15044 {
15045   tree type;
15046
15047   if (TREE_CODE (decl) == ERROR_MARK)
15048     return integer_type_node;
15049
15050   type = DECL_BIT_FIELD_TYPE (decl);
15051   if (type == NULL_TREE)
15052     type = TREE_TYPE (decl);
15053
15054   return type;
15055 }
15056
15057 /* Given a pointer to a tree node, return the alignment in bits for
15058    it, or else return BITS_PER_WORD if the node actually turns out to
15059    be an ERROR_MARK node.  */
15060
15061 static inline unsigned
15062 simple_type_align_in_bits (const_tree type)
15063 {
15064   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
15065 }
15066
15067 static inline unsigned
15068 simple_decl_align_in_bits (const_tree decl)
15069 {
15070   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
15071 }
15072
15073 /* Return the result of rounding T up to ALIGN.  */
15074
15075 static inline offset_int
15076 round_up_to_align (const offset_int &t, unsigned int align)
15077 {
15078   return wi::udiv_trunc (t + align - 1, align) * align;
15079 }
15080
15081 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
15082    lowest addressed byte of the "containing object" for the given FIELD_DECL,
15083    or return 0 if we are unable to determine what that offset is, either
15084    because the argument turns out to be a pointer to an ERROR_MARK node, or
15085    because the offset is actually variable.  (We can't handle the latter case
15086    just yet).  */
15087
15088 static HOST_WIDE_INT
15089 field_byte_offset (const_tree decl)
15090 {
15091   offset_int object_offset_in_bits;
15092   offset_int object_offset_in_bytes;
15093   offset_int bitpos_int;
15094
15095   if (TREE_CODE (decl) == ERROR_MARK)
15096     return 0;
15097
15098   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
15099
15100   /* We cannot yet cope with fields whose positions are variable, so
15101      for now, when we see such things, we simply return 0.  Someday, we may
15102      be able to handle such cases, but it will be damn difficult.  */
15103   if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
15104     return 0;
15105
15106   bitpos_int = wi::to_offset (bit_position (decl));
15107
15108 #ifdef PCC_BITFIELD_TYPE_MATTERS
15109   if (PCC_BITFIELD_TYPE_MATTERS)
15110     {
15111       tree type;
15112       tree field_size_tree;
15113       offset_int deepest_bitpos;
15114       offset_int field_size_in_bits;
15115       unsigned int type_align_in_bits;
15116       unsigned int decl_align_in_bits;
15117       offset_int type_size_in_bits;
15118
15119       type = field_type (decl);
15120       type_size_in_bits = offset_int_type_size_in_bits (type);
15121       type_align_in_bits = simple_type_align_in_bits (type);
15122
15123       field_size_tree = DECL_SIZE (decl);
15124
15125       /* The size could be unspecified if there was an error, or for
15126          a flexible array member.  */
15127       if (!field_size_tree)
15128         field_size_tree = bitsize_zero_node;
15129
15130       /* If the size of the field is not constant, use the type size.  */
15131       if (TREE_CODE (field_size_tree) == INTEGER_CST)
15132         field_size_in_bits = wi::to_offset (field_size_tree);
15133       else
15134         field_size_in_bits = type_size_in_bits;
15135
15136       decl_align_in_bits = simple_decl_align_in_bits (decl);
15137
15138       /* The GCC front-end doesn't make any attempt to keep track of the
15139          starting bit offset (relative to the start of the containing
15140          structure type) of the hypothetical "containing object" for a
15141          bit-field.  Thus, when computing the byte offset value for the
15142          start of the "containing object" of a bit-field, we must deduce
15143          this information on our own. This can be rather tricky to do in
15144          some cases.  For example, handling the following structure type
15145          definition when compiling for an i386/i486 target (which only
15146          aligns long long's to 32-bit boundaries) can be very tricky:
15147
15148          struct S { int field1; long long field2:31; };
15149
15150          Fortunately, there is a simple rule-of-thumb which can be used
15151          in such cases.  When compiling for an i386/i486, GCC will
15152          allocate 8 bytes for the structure shown above.  It decides to
15153          do this based upon one simple rule for bit-field allocation.
15154          GCC allocates each "containing object" for each bit-field at
15155          the first (i.e. lowest addressed) legitimate alignment boundary
15156          (based upon the required minimum alignment for the declared
15157          type of the field) which it can possibly use, subject to the
15158          condition that there is still enough available space remaining
15159          in the containing object (when allocated at the selected point)
15160          to fully accommodate all of the bits of the bit-field itself.
15161
15162          This simple rule makes it obvious why GCC allocates 8 bytes for
15163          each object of the structure type shown above.  When looking
15164          for a place to allocate the "containing object" for `field2',
15165          the compiler simply tries to allocate a 64-bit "containing
15166          object" at each successive 32-bit boundary (starting at zero)
15167          until it finds a place to allocate that 64- bit field such that
15168          at least 31 contiguous (and previously unallocated) bits remain
15169          within that selected 64 bit field.  (As it turns out, for the
15170          example above, the compiler finds it is OK to allocate the
15171          "containing object" 64-bit field at bit-offset zero within the
15172          structure type.)
15173
15174          Here we attempt to work backwards from the limited set of facts
15175          we're given, and we try to deduce from those facts, where GCC
15176          must have believed that the containing object started (within
15177          the structure type). The value we deduce is then used (by the
15178          callers of this routine) to generate DW_AT_location and
15179          DW_AT_bit_offset attributes for fields (both bit-fields and, in
15180          the case of DW_AT_location, regular fields as well).  */
15181
15182       /* Figure out the bit-distance from the start of the structure to
15183          the "deepest" bit of the bit-field.  */
15184       deepest_bitpos = bitpos_int + field_size_in_bits;
15185
15186       /* This is the tricky part.  Use some fancy footwork to deduce
15187          where the lowest addressed bit of the containing object must
15188          be.  */
15189       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15190
15191       /* Round up to type_align by default.  This works best for
15192          bitfields.  */
15193       object_offset_in_bits
15194         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
15195
15196       if (wi::gtu_p (object_offset_in_bits, bitpos_int))
15197         {
15198           object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15199
15200           /* Round up to decl_align instead.  */
15201           object_offset_in_bits
15202             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
15203         }
15204     }
15205   else
15206 #endif /* PCC_BITFIELD_TYPE_MATTERS */
15207     object_offset_in_bits = bitpos_int;
15208
15209   object_offset_in_bytes
15210     = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
15211   return object_offset_in_bytes.to_shwi ();
15212 }
15213 \f
15214 /* The following routines define various Dwarf attributes and any data
15215    associated with them.  */
15216
15217 /* Add a location description attribute value to a DIE.
15218
15219    This emits location attributes suitable for whole variables and
15220    whole parameters.  Note that the location attributes for struct fields are
15221    generated by the routine `data_member_location_attribute' below.  */
15222
15223 static inline void
15224 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
15225                              dw_loc_list_ref descr)
15226 {
15227   if (descr == 0)
15228     return;
15229   if (single_element_loc_list_p (descr))
15230     add_AT_loc (die, attr_kind, descr->expr);
15231   else
15232     add_AT_loc_list (die, attr_kind, descr);
15233 }
15234
15235 /* Add DW_AT_accessibility attribute to DIE if needed.  */
15236
15237 static void
15238 add_accessibility_attribute (dw_die_ref die, tree decl)
15239 {
15240   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
15241      children, otherwise the default is DW_ACCESS_public.  In DWARF2
15242      the default has always been DW_ACCESS_public.  */
15243   if (TREE_PROTECTED (decl))
15244     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
15245   else if (TREE_PRIVATE (decl))
15246     {
15247       if (dwarf_version == 2
15248           || die->die_parent == NULL
15249           || die->die_parent->die_tag != DW_TAG_class_type)
15250         add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
15251     }
15252   else if (dwarf_version > 2
15253            && die->die_parent
15254            && die->die_parent->die_tag == DW_TAG_class_type)
15255     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
15256 }
15257
15258 /* Attach the specialized form of location attribute used for data members of
15259    struct and union types.  In the special case of a FIELD_DECL node which
15260    represents a bit-field, the "offset" part of this special location
15261    descriptor must indicate the distance in bytes from the lowest-addressed
15262    byte of the containing struct or union type to the lowest-addressed byte of
15263    the "containing object" for the bit-field.  (See the `field_byte_offset'
15264    function above).
15265
15266    For any given bit-field, the "containing object" is a hypothetical object
15267    (of some integral or enum type) within which the given bit-field lives.  The
15268    type of this hypothetical "containing object" is always the same as the
15269    declared type of the individual bit-field itself (for GCC anyway... the
15270    DWARF spec doesn't actually mandate this).  Note that it is the size (in
15271    bytes) of the hypothetical "containing object" which will be given in the
15272    DW_AT_byte_size attribute for this bit-field.  (See the
15273    `byte_size_attribute' function below.)  It is also used when calculating the
15274    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
15275    function below.)  */
15276
15277 static void
15278 add_data_member_location_attribute (dw_die_ref die, tree decl)
15279 {
15280   HOST_WIDE_INT offset;
15281   dw_loc_descr_ref loc_descr = 0;
15282
15283   if (TREE_CODE (decl) == TREE_BINFO)
15284     {
15285       /* We're working on the TAG_inheritance for a base class.  */
15286       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
15287         {
15288           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
15289              aren't at a fixed offset from all (sub)objects of the same
15290              type.  We need to extract the appropriate offset from our
15291              vtable.  The following dwarf expression means
15292
15293                BaseAddr = ObAddr + *((*ObAddr) - Offset)
15294
15295              This is specific to the V3 ABI, of course.  */
15296
15297           dw_loc_descr_ref tmp;
15298
15299           /* Make a copy of the object address.  */
15300           tmp = new_loc_descr (DW_OP_dup, 0, 0);
15301           add_loc_descr (&loc_descr, tmp);
15302
15303           /* Extract the vtable address.  */
15304           tmp = new_loc_descr (DW_OP_deref, 0, 0);
15305           add_loc_descr (&loc_descr, tmp);
15306
15307           /* Calculate the address of the offset.  */
15308           offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
15309           gcc_assert (offset < 0);
15310
15311           tmp = int_loc_descriptor (-offset);
15312           add_loc_descr (&loc_descr, tmp);
15313           tmp = new_loc_descr (DW_OP_minus, 0, 0);
15314           add_loc_descr (&loc_descr, tmp);
15315
15316           /* Extract the offset.  */
15317           tmp = new_loc_descr (DW_OP_deref, 0, 0);
15318           add_loc_descr (&loc_descr, tmp);
15319
15320           /* Add it to the object address.  */
15321           tmp = new_loc_descr (DW_OP_plus, 0, 0);
15322           add_loc_descr (&loc_descr, tmp);
15323         }
15324       else
15325         offset = tree_to_shwi (BINFO_OFFSET (decl));
15326     }
15327   else
15328     offset = field_byte_offset (decl);
15329
15330   if (! loc_descr)
15331     {
15332       if (dwarf_version > 2)
15333         {
15334           /* Don't need to output a location expression, just the constant. */
15335           if (offset < 0)
15336             add_AT_int (die, DW_AT_data_member_location, offset);
15337           else
15338             add_AT_unsigned (die, DW_AT_data_member_location, offset);
15339           return;
15340         }
15341       else
15342         {
15343           enum dwarf_location_atom op;
15344
15345           /* The DWARF2 standard says that we should assume that the structure
15346              address is already on the stack, so we can specify a structure
15347              field address by using DW_OP_plus_uconst.  */
15348           op = DW_OP_plus_uconst;
15349           loc_descr = new_loc_descr (op, offset, 0);
15350         }
15351     }
15352
15353   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
15354 }
15355
15356 /* Writes integer values to dw_vec_const array.  */
15357
15358 static void
15359 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
15360 {
15361   while (size != 0)
15362     {
15363       *dest++ = val & 0xff;
15364       val >>= 8;
15365       --size;
15366     }
15367 }
15368
15369 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
15370
15371 static HOST_WIDE_INT
15372 extract_int (const unsigned char *src, unsigned int size)
15373 {
15374   HOST_WIDE_INT val = 0;
15375
15376   src += size;
15377   while (size != 0)
15378     {
15379       val <<= 8;
15380       val |= *--src & 0xff;
15381       --size;
15382     }
15383   return val;
15384 }
15385
15386 /* Writes wide_int values to dw_vec_const array.  */
15387
15388 static void
15389 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
15390 {
15391   int i;
15392
15393   if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
15394     {
15395       insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
15396       return;
15397     }
15398
15399   /* We'd have to extend this code to support odd sizes.  */
15400   gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
15401
15402   int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
15403
15404   if (WORDS_BIG_ENDIAN)
15405     for (i = n - 1; i >= 0; i--)
15406       {
15407         insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15408         dest += sizeof (HOST_WIDE_INT);
15409       }
15410   else
15411     for (i = 0; i < n; i++)
15412       {
15413         insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15414         dest += sizeof (HOST_WIDE_INT);
15415       }
15416 }
15417
15418 /* Writes floating point values to dw_vec_const array.  */
15419
15420 static void
15421 insert_float (const_rtx rtl, unsigned char *array)
15422 {
15423   REAL_VALUE_TYPE rv;
15424   long val[4];
15425   int i;
15426
15427   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
15428   real_to_target (val, &rv, GET_MODE (rtl));
15429
15430   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
15431   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
15432     {
15433       insert_int (val[i], 4, array);
15434       array += 4;
15435     }
15436 }
15437
15438 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
15439    does not have a "location" either in memory or in a register.  These
15440    things can arise in GNU C when a constant is passed as an actual parameter
15441    to an inlined function.  They can also arise in C++ where declared
15442    constants do not necessarily get memory "homes".  */
15443
15444 static bool
15445 add_const_value_attribute (dw_die_ref die, rtx rtl)
15446 {
15447   switch (GET_CODE (rtl))
15448     {
15449     case CONST_INT:
15450       {
15451         HOST_WIDE_INT val = INTVAL (rtl);
15452
15453         if (val < 0)
15454           add_AT_int (die, DW_AT_const_value, val);
15455         else
15456           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
15457       }
15458       return true;
15459
15460     case CONST_WIDE_INT:
15461       add_AT_wide (die, DW_AT_const_value,
15462                    std::make_pair (rtl, GET_MODE (rtl)));
15463       return true;
15464
15465     case CONST_DOUBLE:
15466       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
15467          floating-point constant.  A CONST_DOUBLE is used whenever the
15468          constant requires more than one word in order to be adequately
15469          represented.  */
15470       {
15471         machine_mode mode = GET_MODE (rtl);
15472
15473         if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
15474           add_AT_double (die, DW_AT_const_value,
15475                          CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
15476         else
15477           {
15478             unsigned int length = GET_MODE_SIZE (mode);
15479             unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15480
15481             insert_float (rtl, array);
15482             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
15483           }
15484       }
15485       return true;
15486
15487     case CONST_VECTOR:
15488       {
15489         machine_mode mode = GET_MODE (rtl);
15490         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
15491         unsigned int length = CONST_VECTOR_NUNITS (rtl);
15492         unsigned char *array
15493           = ggc_vec_alloc<unsigned char> (length * elt_size);
15494         unsigned int i;
15495         unsigned char *p;
15496         machine_mode imode = GET_MODE_INNER (mode);
15497
15498         switch (GET_MODE_CLASS (mode))
15499           {
15500           case MODE_VECTOR_INT:
15501             for (i = 0, p = array; i < length; i++, p += elt_size)
15502               {
15503                 rtx elt = CONST_VECTOR_ELT (rtl, i);
15504                 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
15505               }
15506             break;
15507
15508           case MODE_VECTOR_FLOAT:
15509             for (i = 0, p = array; i < length; i++, p += elt_size)
15510               {
15511                 rtx elt = CONST_VECTOR_ELT (rtl, i);
15512                 insert_float (elt, p);
15513               }
15514             break;
15515
15516           default:
15517             gcc_unreachable ();
15518           }
15519
15520         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
15521       }
15522       return true;
15523
15524     case CONST_STRING:
15525       if (dwarf_version >= 4 || !dwarf_strict)
15526         {
15527           dw_loc_descr_ref loc_result;
15528           resolve_one_addr (&rtl);
15529         rtl_addr:
15530           loc_result = new_addr_loc_descr (rtl, dtprel_false);
15531           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15532           add_AT_loc (die, DW_AT_location, loc_result);
15533           vec_safe_push (used_rtx_array, rtl);
15534           return true;
15535         }
15536       return false;
15537
15538     case CONST:
15539       if (CONSTANT_P (XEXP (rtl, 0)))
15540         return add_const_value_attribute (die, XEXP (rtl, 0));
15541       /* FALLTHROUGH */
15542     case SYMBOL_REF:
15543       if (!const_ok_for_output (rtl))
15544         return false;
15545     case LABEL_REF:
15546       if (dwarf_version >= 4 || !dwarf_strict)
15547         goto rtl_addr;
15548       return false;
15549
15550     case PLUS:
15551       /* In cases where an inlined instance of an inline function is passed
15552          the address of an `auto' variable (which is local to the caller) we
15553          can get a situation where the DECL_RTL of the artificial local
15554          variable (for the inlining) which acts as a stand-in for the
15555          corresponding formal parameter (of the inline function) will look
15556          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
15557          exactly a compile-time constant expression, but it isn't the address
15558          of the (artificial) local variable either.  Rather, it represents the
15559          *value* which the artificial local variable always has during its
15560          lifetime.  We currently have no way to represent such quasi-constant
15561          values in Dwarf, so for now we just punt and generate nothing.  */
15562       return false;
15563
15564     case HIGH:
15565     case CONST_FIXED:
15566       return false;
15567
15568     case MEM:
15569       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
15570           && MEM_READONLY_P (rtl)
15571           && GET_MODE (rtl) == BLKmode)
15572         {
15573           add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
15574           return true;
15575         }
15576       return false;
15577
15578     default:
15579       /* No other kinds of rtx should be possible here.  */
15580       gcc_unreachable ();
15581     }
15582   return false;
15583 }
15584
15585 /* Determine whether the evaluation of EXPR references any variables
15586    or functions which aren't otherwise used (and therefore may not be
15587    output).  */
15588 static tree
15589 reference_to_unused (tree * tp, int * walk_subtrees,
15590                      void * data ATTRIBUTE_UNUSED)
15591 {
15592   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15593     *walk_subtrees = 0;
15594
15595   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15596       && ! TREE_ASM_WRITTEN (*tp))
15597     return *tp;
15598   /* ???  The C++ FE emits debug information for using decls, so
15599      putting gcc_unreachable here falls over.  See PR31899.  For now
15600      be conservative.  */
15601   else if (!symtab->global_info_ready
15602            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15603     return *tp;
15604   else if (TREE_CODE (*tp) == VAR_DECL)
15605     {
15606       varpool_node *node = varpool_node::get (*tp);
15607       if (!node || !node->definition)
15608         return *tp;
15609     }
15610   else if (TREE_CODE (*tp) == FUNCTION_DECL
15611            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15612     {
15613       /* The call graph machinery must have finished analyzing,
15614          optimizing and gimplifying the CU by now.
15615          So if *TP has no call graph node associated
15616          to it, it means *TP will not be emitted.  */
15617       if (!cgraph_node::get (*tp))
15618         return *tp;
15619     }
15620   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15621     return *tp;
15622
15623   return NULL_TREE;
15624 }
15625
15626 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15627    for use in a later add_const_value_attribute call.  */
15628
15629 static rtx
15630 rtl_for_decl_init (tree init, tree type)
15631 {
15632   rtx rtl = NULL_RTX;
15633
15634   STRIP_NOPS (init);
15635
15636   /* If a variable is initialized with a string constant without embedded
15637      zeros, build CONST_STRING.  */
15638   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15639     {
15640       tree enttype = TREE_TYPE (type);
15641       tree domain = TYPE_DOMAIN (type);
15642       machine_mode mode = TYPE_MODE (enttype);
15643
15644       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15645           && domain
15646           && integer_zerop (TYPE_MIN_VALUE (domain))
15647           && compare_tree_int (TYPE_MAX_VALUE (domain),
15648                                TREE_STRING_LENGTH (init) - 1) == 0
15649           && ((size_t) TREE_STRING_LENGTH (init)
15650               == strlen (TREE_STRING_POINTER (init)) + 1))
15651         {
15652           rtl = gen_rtx_CONST_STRING (VOIDmode,
15653                                       ggc_strdup (TREE_STRING_POINTER (init)));
15654           rtl = gen_rtx_MEM (BLKmode, rtl);
15655           MEM_READONLY_P (rtl) = 1;
15656         }
15657     }
15658   /* Other aggregates, and complex values, could be represented using
15659      CONCAT: FIXME!  */
15660   else if (AGGREGATE_TYPE_P (type)
15661            || (TREE_CODE (init) == VIEW_CONVERT_EXPR
15662                && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
15663            || TREE_CODE (type) == COMPLEX_TYPE)
15664     ;
15665   /* Vectors only work if their mode is supported by the target.
15666      FIXME: generic vectors ought to work too.  */
15667   else if (TREE_CODE (type) == VECTOR_TYPE
15668            && !VECTOR_MODE_P (TYPE_MODE (type)))
15669     ;
15670   /* If the initializer is something that we know will expand into an
15671      immediate RTL constant, expand it now.  We must be careful not to
15672      reference variables which won't be output.  */
15673   else if (initializer_constant_valid_p (init, type)
15674            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15675     {
15676       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15677          possible.  */
15678       if (TREE_CODE (type) == VECTOR_TYPE)
15679         switch (TREE_CODE (init))
15680           {
15681           case VECTOR_CST:
15682             break;
15683           case CONSTRUCTOR:
15684             if (TREE_CONSTANT (init))
15685               {
15686                 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15687                 bool constant_p = true;
15688                 tree value;
15689                 unsigned HOST_WIDE_INT ix;
15690
15691                 /* Even when ctor is constant, it might contain non-*_CST
15692                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15693                    belong into VECTOR_CST nodes.  */
15694                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15695                   if (!CONSTANT_CLASS_P (value))
15696                     {
15697                       constant_p = false;
15698                       break;
15699                     }
15700
15701                 if (constant_p)
15702                   {
15703                     init = build_vector_from_ctor (type, elts);
15704                     break;
15705                   }
15706               }
15707             /* FALLTHRU */
15708
15709           default:
15710             return NULL;
15711           }
15712
15713       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15714
15715       /* If expand_expr returns a MEM, it wasn't immediate.  */
15716       gcc_assert (!rtl || !MEM_P (rtl));
15717     }
15718
15719   return rtl;
15720 }
15721
15722 /* Generate RTL for the variable DECL to represent its location.  */
15723
15724 static rtx
15725 rtl_for_decl_location (tree decl)
15726 {
15727   rtx rtl;
15728
15729   /* Here we have to decide where we are going to say the parameter "lives"
15730      (as far as the debugger is concerned).  We only have a couple of
15731      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15732
15733      DECL_RTL normally indicates where the parameter lives during most of the
15734      activation of the function.  If optimization is enabled however, this
15735      could be either NULL or else a pseudo-reg.  Both of those cases indicate
15736      that the parameter doesn't really live anywhere (as far as the code
15737      generation parts of GCC are concerned) during most of the function's
15738      activation.  That will happen (for example) if the parameter is never
15739      referenced within the function.
15740
15741      We could just generate a location descriptor here for all non-NULL
15742      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15743      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15744      where DECL_RTL is NULL or is a pseudo-reg.
15745
15746      Note however that we can only get away with using DECL_INCOMING_RTL as
15747      a backup substitute for DECL_RTL in certain limited cases.  In cases
15748      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15749      we can be sure that the parameter was passed using the same type as it is
15750      declared to have within the function, and that its DECL_INCOMING_RTL
15751      points us to a place where a value of that type is passed.
15752
15753      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15754      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15755      because in these cases DECL_INCOMING_RTL points us to a value of some
15756      type which is *different* from the type of the parameter itself.  Thus,
15757      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15758      such cases, the debugger would end up (for example) trying to fetch a
15759      `float' from a place which actually contains the first part of a
15760      `double'.  That would lead to really incorrect and confusing
15761      output at debug-time.
15762
15763      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15764      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
15765      are a couple of exceptions however.  On little-endian machines we can
15766      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15767      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15768      an integral type that is smaller than TREE_TYPE (decl). These cases arise
15769      when (on a little-endian machine) a non-prototyped function has a
15770      parameter declared to be of type `short' or `char'.  In such cases,
15771      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15772      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15773      passed `int' value.  If the debugger then uses that address to fetch
15774      a `short' or a `char' (on a little-endian machine) the result will be
15775      the correct data, so we allow for such exceptional cases below.
15776
15777      Note that our goal here is to describe the place where the given formal
15778      parameter lives during most of the function's activation (i.e. between the
15779      end of the prologue and the start of the epilogue).  We'll do that as best
15780      as we can. Note however that if the given formal parameter is modified
15781      sometime during the execution of the function, then a stack backtrace (at
15782      debug-time) will show the function as having been called with the *new*
15783      value rather than the value which was originally passed in.  This happens
15784      rarely enough that it is not a major problem, but it *is* a problem, and
15785      I'd like to fix it.
15786
15787      A future version of dwarf2out.c may generate two additional attributes for
15788      any given DW_TAG_formal_parameter DIE which will describe the "passed
15789      type" and the "passed location" for the given formal parameter in addition
15790      to the attributes we now generate to indicate the "declared type" and the
15791      "active location" for each parameter.  This additional set of attributes
15792      could be used by debuggers for stack backtraces. Separately, note that
15793      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15794      This happens (for example) for inlined-instances of inline function formal
15795      parameters which are never referenced.  This really shouldn't be
15796      happening.  All PARM_DECL nodes should get valid non-NULL
15797      DECL_INCOMING_RTL values.  FIXME.  */
15798
15799   /* Use DECL_RTL as the "location" unless we find something better.  */
15800   rtl = DECL_RTL_IF_SET (decl);
15801
15802   /* When generating abstract instances, ignore everything except
15803      constants, symbols living in memory, and symbols living in
15804      fixed registers.  */
15805   if (! reload_completed)
15806     {
15807       if (rtl
15808           && (CONSTANT_P (rtl)
15809               || (MEM_P (rtl)
15810                   && CONSTANT_P (XEXP (rtl, 0)))
15811               || (REG_P (rtl)
15812                   && TREE_CODE (decl) == VAR_DECL
15813                   && TREE_STATIC (decl))))
15814         {
15815           rtl = targetm.delegitimize_address (rtl);
15816           return rtl;
15817         }
15818       rtl = NULL_RTX;
15819     }
15820   else if (TREE_CODE (decl) == PARM_DECL)
15821     {
15822       if (rtl == NULL_RTX
15823           || is_pseudo_reg (rtl)
15824           || (MEM_P (rtl)
15825               && is_pseudo_reg (XEXP (rtl, 0))
15826               && DECL_INCOMING_RTL (decl)
15827               && MEM_P (DECL_INCOMING_RTL (decl))
15828               && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15829         {
15830           tree declared_type = TREE_TYPE (decl);
15831           tree passed_type = DECL_ARG_TYPE (decl);
15832           machine_mode dmode = TYPE_MODE (declared_type);
15833           machine_mode pmode = TYPE_MODE (passed_type);
15834
15835           /* This decl represents a formal parameter which was optimized out.
15836              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15837              all cases where (rtl == NULL_RTX) just below.  */
15838           if (dmode == pmode)
15839             rtl = DECL_INCOMING_RTL (decl);
15840           else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15841                    && SCALAR_INT_MODE_P (dmode)
15842                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15843                    && DECL_INCOMING_RTL (decl))
15844             {
15845               rtx inc = DECL_INCOMING_RTL (decl);
15846               if (REG_P (inc))
15847                 rtl = inc;
15848               else if (MEM_P (inc))
15849                 {
15850                   if (BYTES_BIG_ENDIAN)
15851                     rtl = adjust_address_nv (inc, dmode,
15852                                              GET_MODE_SIZE (pmode)
15853                                              - GET_MODE_SIZE (dmode));
15854                   else
15855                     rtl = inc;
15856                 }
15857             }
15858         }
15859
15860       /* If the parm was passed in registers, but lives on the stack, then
15861          make a big endian correction if the mode of the type of the
15862          parameter is not the same as the mode of the rtl.  */
15863       /* ??? This is the same series of checks that are made in dbxout.c before
15864          we reach the big endian correction code there.  It isn't clear if all
15865          of these checks are necessary here, but keeping them all is the safe
15866          thing to do.  */
15867       else if (MEM_P (rtl)
15868                && XEXP (rtl, 0) != const0_rtx
15869                && ! CONSTANT_P (XEXP (rtl, 0))
15870                /* Not passed in memory.  */
15871                && !MEM_P (DECL_INCOMING_RTL (decl))
15872                /* Not passed by invisible reference.  */
15873                && (!REG_P (XEXP (rtl, 0))
15874                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15875                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15876 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15877                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15878 #endif
15879                      )
15880                /* Big endian correction check.  */
15881                && BYTES_BIG_ENDIAN
15882                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15883                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15884                    < UNITS_PER_WORD))
15885         {
15886           machine_mode addr_mode = get_address_mode (rtl);
15887           int offset = (UNITS_PER_WORD
15888                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15889
15890           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15891                              plus_constant (addr_mode, XEXP (rtl, 0), offset));
15892         }
15893     }
15894   else if (TREE_CODE (decl) == VAR_DECL
15895            && rtl
15896            && MEM_P (rtl)
15897            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15898            && BYTES_BIG_ENDIAN)
15899     {
15900       machine_mode addr_mode = get_address_mode (rtl);
15901       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15902       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15903
15904       /* If a variable is declared "register" yet is smaller than
15905          a register, then if we store the variable to memory, it
15906          looks like we're storing a register-sized value, when in
15907          fact we are not.  We need to adjust the offset of the
15908          storage location to reflect the actual value's bytes,
15909          else gdb will not be able to display it.  */
15910       if (rsize > dsize)
15911         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15912                            plus_constant (addr_mode, XEXP (rtl, 0),
15913                                           rsize - dsize));
15914     }
15915
15916   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15917      and will have been substituted directly into all expressions that use it.
15918      C does not have such a concept, but C++ and other languages do.  */
15919   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15920     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15921
15922   if (rtl)
15923     rtl = targetm.delegitimize_address (rtl);
15924
15925   /* If we don't look past the constant pool, we risk emitting a
15926      reference to a constant pool entry that isn't referenced from
15927      code, and thus is not emitted.  */
15928   if (rtl)
15929     rtl = avoid_constant_pool_reference (rtl);
15930
15931   /* Try harder to get a rtl.  If this symbol ends up not being emitted
15932      in the current CU, resolve_addr will remove the expression referencing
15933      it.  */
15934   if (rtl == NULL_RTX
15935       && TREE_CODE (decl) == VAR_DECL
15936       && !DECL_EXTERNAL (decl)
15937       && TREE_STATIC (decl)
15938       && DECL_NAME (decl)
15939       && !DECL_HARD_REGISTER (decl)
15940       && DECL_MODE (decl) != VOIDmode)
15941     {
15942       rtl = make_decl_rtl_for_debug (decl);
15943       if (!MEM_P (rtl)
15944           || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
15945           || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
15946         rtl = NULL_RTX;
15947     }
15948
15949   return rtl;
15950 }
15951
15952 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
15953    returned.  If so, the decl for the COMMON block is returned, and the
15954    value is the offset into the common block for the symbol.  */
15955
15956 static tree
15957 fortran_common (tree decl, HOST_WIDE_INT *value)
15958 {
15959   tree val_expr, cvar;
15960   machine_mode mode;
15961   HOST_WIDE_INT bitsize, bitpos;
15962   tree offset;
15963   int unsignedp, volatilep = 0;
15964
15965   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
15966      it does not have a value (the offset into the common area), or if it
15967      is thread local (as opposed to global) then it isn't common, and shouldn't
15968      be handled as such.  */
15969   if (TREE_CODE (decl) != VAR_DECL
15970       || !TREE_STATIC (decl)
15971       || !DECL_HAS_VALUE_EXPR_P (decl)
15972       || !is_fortran ())
15973     return NULL_TREE;
15974
15975   val_expr = DECL_VALUE_EXPR (decl);
15976   if (TREE_CODE (val_expr) != COMPONENT_REF)
15977     return NULL_TREE;
15978
15979   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
15980                               &mode, &unsignedp, &volatilep, true);
15981
15982   if (cvar == NULL_TREE
15983       || TREE_CODE (cvar) != VAR_DECL
15984       || DECL_ARTIFICIAL (cvar)
15985       || !TREE_PUBLIC (cvar))
15986     return NULL_TREE;
15987
15988   *value = 0;
15989   if (offset != NULL)
15990     {
15991       if (!tree_fits_shwi_p (offset))
15992         return NULL_TREE;
15993       *value = tree_to_shwi (offset);
15994     }
15995   if (bitpos != 0)
15996     *value += bitpos / BITS_PER_UNIT;
15997
15998   return cvar;
15999 }
16000
16001 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
16002    data attribute for a variable or a parameter.  We generate the
16003    DW_AT_const_value attribute only in those cases where the given variable
16004    or parameter does not have a true "location" either in memory or in a
16005    register.  This can happen (for example) when a constant is passed as an
16006    actual argument in a call to an inline function.  (It's possible that
16007    these things can crop up in other ways also.)  Note that one type of
16008    constant value which can be passed into an inlined function is a constant
16009    pointer.  This can happen for example if an actual argument in an inlined
16010    function call evaluates to a compile-time constant address.
16011
16012    CACHE_P is true if it is worth caching the location list for DECL,
16013    so that future calls can reuse it rather than regenerate it from scratch.
16014    This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
16015    since we will need to refer to them each time the function is inlined.  */
16016
16017 static bool
16018 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
16019                                        enum dwarf_attribute attr)
16020 {
16021   rtx rtl;
16022   dw_loc_list_ref list;
16023   var_loc_list *loc_list;
16024   cached_dw_loc_list *cache;
16025
16026   if (TREE_CODE (decl) == ERROR_MARK)
16027     return false;
16028
16029   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
16030               || TREE_CODE (decl) == RESULT_DECL);
16031
16032   /* Try to get some constant RTL for this decl, and use that as the value of
16033      the location.  */
16034
16035   rtl = rtl_for_decl_location (decl);
16036   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16037       && add_const_value_attribute (die, rtl))
16038     return true;
16039
16040   /* See if we have single element location list that is equivalent to
16041      a constant value.  That way we are better to use add_const_value_attribute
16042      rather than expanding constant value equivalent.  */
16043   loc_list = lookup_decl_loc (decl);
16044   if (loc_list
16045       && loc_list->first
16046       && loc_list->first->next == NULL
16047       && NOTE_P (loc_list->first->loc)
16048       && NOTE_VAR_LOCATION (loc_list->first->loc)
16049       && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
16050     {
16051       struct var_loc_node *node;
16052
16053       node = loc_list->first;
16054       rtl = NOTE_VAR_LOCATION_LOC (node->loc);
16055       if (GET_CODE (rtl) == EXPR_LIST)
16056         rtl = XEXP (rtl, 0);
16057       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16058           && add_const_value_attribute (die, rtl))
16059          return true;
16060     }
16061   /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
16062      list several times.  See if we've already cached the contents.  */
16063   list = NULL;
16064   if (loc_list == NULL || cached_dw_loc_list_table == NULL)
16065     cache_p = false;
16066   if (cache_p)
16067     {
16068       cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
16069       if (cache)
16070         list = cache->loc_list;
16071     }
16072   if (list == NULL)
16073     {
16074       list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
16075                                  NULL);
16076       /* It is usually worth caching this result if the decl is from
16077          BLOCK_NONLOCALIZED_VARS and if the list has at least two elements.  */
16078       if (cache_p && list && list->dw_loc_next)
16079         {
16080           cached_dw_loc_list **slot
16081             = cached_dw_loc_list_table->find_slot_with_hash (decl,
16082                                                              DECL_UID (decl),
16083                                                              INSERT);
16084           cache = ggc_cleared_alloc<cached_dw_loc_list> ();
16085           cache->decl_id = DECL_UID (decl);
16086           cache->loc_list = list;
16087           *slot = cache;
16088         }
16089     }
16090   if (list)
16091     {
16092       add_AT_location_description (die, attr, list);
16093       return true;
16094     }
16095   /* None of that worked, so it must not really have a location;
16096      try adding a constant value attribute from the DECL_INITIAL.  */
16097   return tree_add_const_value_attribute_for_decl (die, decl);
16098 }
16099
16100 /* Add VARIABLE and DIE into deferred locations list.  */
16101
16102 static void
16103 defer_location (tree variable, dw_die_ref die)
16104 {
16105   deferred_locations entry;
16106   entry.variable = variable;
16107   entry.die = die;
16108   vec_safe_push (deferred_locations_list, entry);
16109 }
16110
16111 /* Helper function for tree_add_const_value_attribute.  Natively encode
16112    initializer INIT into an array.  Return true if successful.  */
16113
16114 static bool
16115 native_encode_initializer (tree init, unsigned char *array, int size)
16116 {
16117   tree type;
16118
16119   if (init == NULL_TREE)
16120     return false;
16121
16122   STRIP_NOPS (init);
16123   switch (TREE_CODE (init))
16124     {
16125     case STRING_CST:
16126       type = TREE_TYPE (init);
16127       if (TREE_CODE (type) == ARRAY_TYPE)
16128         {
16129           tree enttype = TREE_TYPE (type);
16130           machine_mode mode = TYPE_MODE (enttype);
16131
16132           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
16133             return false;
16134           if (int_size_in_bytes (type) != size)
16135             return false;
16136           if (size > TREE_STRING_LENGTH (init))
16137             {
16138               memcpy (array, TREE_STRING_POINTER (init),
16139                       TREE_STRING_LENGTH (init));
16140               memset (array + TREE_STRING_LENGTH (init),
16141                       '\0', size - TREE_STRING_LENGTH (init));
16142             }
16143           else
16144             memcpy (array, TREE_STRING_POINTER (init), size);
16145           return true;
16146         }
16147       return false;
16148     case CONSTRUCTOR:
16149       type = TREE_TYPE (init);
16150       if (int_size_in_bytes (type) != size)
16151         return false;
16152       if (TREE_CODE (type) == ARRAY_TYPE)
16153         {
16154           HOST_WIDE_INT min_index;
16155           unsigned HOST_WIDE_INT cnt;
16156           int curpos = 0, fieldsize;
16157           constructor_elt *ce;
16158
16159           if (TYPE_DOMAIN (type) == NULL_TREE
16160               || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
16161             return false;
16162
16163           fieldsize = int_size_in_bytes (TREE_TYPE (type));
16164           if (fieldsize <= 0)
16165             return false;
16166
16167           min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
16168           memset (array, '\0', size);
16169           FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16170             {
16171               tree val = ce->value;
16172               tree index = ce->index;
16173               int pos = curpos;
16174               if (index && TREE_CODE (index) == RANGE_EXPR)
16175                 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
16176                       * fieldsize;
16177               else if (index)
16178                 pos = (tree_to_shwi (index) - min_index) * fieldsize;
16179
16180               if (val)
16181                 {
16182                   STRIP_NOPS (val);
16183                   if (!native_encode_initializer (val, array + pos, fieldsize))
16184                     return false;
16185                 }
16186               curpos = pos + fieldsize;
16187               if (index && TREE_CODE (index) == RANGE_EXPR)
16188                 {
16189                   int count = tree_to_shwi (TREE_OPERAND (index, 1))
16190                               - tree_to_shwi (TREE_OPERAND (index, 0));
16191                   while (count-- > 0)
16192                     {
16193                       if (val)
16194                         memcpy (array + curpos, array + pos, fieldsize);
16195                       curpos += fieldsize;
16196                     }
16197                 }
16198               gcc_assert (curpos <= size);
16199             }
16200           return true;
16201         }
16202       else if (TREE_CODE (type) == RECORD_TYPE
16203                || TREE_CODE (type) == UNION_TYPE)
16204         {
16205           tree field = NULL_TREE;
16206           unsigned HOST_WIDE_INT cnt;
16207           constructor_elt *ce;
16208
16209           if (int_size_in_bytes (type) != size)
16210             return false;
16211
16212           if (TREE_CODE (type) == RECORD_TYPE)
16213             field = TYPE_FIELDS (type);
16214
16215           FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16216             {
16217               tree val = ce->value;
16218               int pos, fieldsize;
16219
16220               if (ce->index != 0)
16221                 field = ce->index;
16222
16223               if (val)
16224                 STRIP_NOPS (val);
16225
16226               if (field == NULL_TREE || DECL_BIT_FIELD (field))
16227                 return false;
16228
16229               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
16230                   && TYPE_DOMAIN (TREE_TYPE (field))
16231                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
16232                 return false;
16233               else if (DECL_SIZE_UNIT (field) == NULL_TREE
16234                        || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
16235                 return false;
16236               fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
16237               pos = int_byte_position (field);
16238               gcc_assert (pos + fieldsize <= size);
16239               if (val
16240                   && !native_encode_initializer (val, array + pos, fieldsize))
16241                 return false;
16242             }
16243           return true;
16244         }
16245       return false;
16246     case VIEW_CONVERT_EXPR:
16247     case NON_LVALUE_EXPR:
16248       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
16249     default:
16250       return native_encode_expr (init, array, size) == size;
16251     }
16252 }
16253
16254 /* Attach a DW_AT_const_value attribute to DIE. The value of the
16255    attribute is the const value T.  */
16256
16257 static bool
16258 tree_add_const_value_attribute (dw_die_ref die, tree t)
16259 {
16260   tree init;
16261   tree type = TREE_TYPE (t);
16262   rtx rtl;
16263
16264   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
16265     return false;
16266
16267   init = t;
16268   gcc_assert (!DECL_P (init));
16269
16270   rtl = rtl_for_decl_init (init, type);
16271   if (rtl)
16272     return add_const_value_attribute (die, rtl);
16273   /* If the host and target are sane, try harder.  */
16274   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
16275            && initializer_constant_valid_p (init, type))
16276     {
16277       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
16278       if (size > 0 && (int) size == size)
16279         {
16280           unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
16281
16282           if (native_encode_initializer (init, array, size))
16283             {
16284               add_AT_vec (die, DW_AT_const_value, size, 1, array);
16285               return true;
16286             }
16287           ggc_free (array);
16288         }
16289     }
16290   return false;
16291 }
16292
16293 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
16294    attribute is the const value of T, where T is an integral constant
16295    variable with static storage duration
16296    (so it can't be a PARM_DECL or a RESULT_DECL).  */
16297
16298 static bool
16299 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
16300 {
16301
16302   if (!decl
16303       || (TREE_CODE (decl) != VAR_DECL
16304           && TREE_CODE (decl) != CONST_DECL)
16305       || (TREE_CODE (decl) == VAR_DECL
16306           && !TREE_STATIC (decl)))
16307     return false;
16308
16309     if (TREE_READONLY (decl)
16310         && ! TREE_THIS_VOLATILE (decl)
16311         && DECL_INITIAL (decl))
16312       /* OK */;
16313     else
16314       return false;
16315
16316   /* Don't add DW_AT_const_value if abstract origin already has one.  */
16317   if (get_AT (var_die, DW_AT_const_value))
16318     return false;
16319
16320   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
16321 }
16322
16323 /* Convert the CFI instructions for the current function into a
16324    location list.  This is used for DW_AT_frame_base when we targeting
16325    a dwarf2 consumer that does not support the dwarf3
16326    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
16327    expressions.  */
16328
16329 static dw_loc_list_ref
16330 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
16331 {
16332   int ix;
16333   dw_fde_ref fde;
16334   dw_loc_list_ref list, *list_tail;
16335   dw_cfi_ref cfi;
16336   dw_cfa_location last_cfa, next_cfa;
16337   const char *start_label, *last_label, *section;
16338   dw_cfa_location remember;
16339
16340   fde = cfun->fde;
16341   gcc_assert (fde != NULL);
16342
16343   section = secname_for_decl (current_function_decl);
16344   list_tail = &list;
16345   list = NULL;
16346
16347   memset (&next_cfa, 0, sizeof (next_cfa));
16348   next_cfa.reg = INVALID_REGNUM;
16349   remember = next_cfa;
16350
16351   start_label = fde->dw_fde_begin;
16352
16353   /* ??? Bald assumption that the CIE opcode list does not contain
16354      advance opcodes.  */
16355   FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
16356     lookup_cfa_1 (cfi, &next_cfa, &remember);
16357
16358   last_cfa = next_cfa;
16359   last_label = start_label;
16360
16361   if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
16362     {
16363       /* If the first partition contained no CFI adjustments, the
16364          CIE opcodes apply to the whole first partition.  */
16365       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16366                                  fde->dw_fde_begin, fde->dw_fde_end, section);
16367       list_tail =&(*list_tail)->dw_loc_next;
16368       start_label = last_label = fde->dw_fde_second_begin;
16369     }
16370
16371   FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
16372     {
16373       switch (cfi->dw_cfi_opc)
16374         {
16375         case DW_CFA_set_loc:
16376         case DW_CFA_advance_loc1:
16377         case DW_CFA_advance_loc2:
16378         case DW_CFA_advance_loc4:
16379           if (!cfa_equal_p (&last_cfa, &next_cfa))
16380             {
16381               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16382                                          start_label, last_label, section);
16383
16384               list_tail = &(*list_tail)->dw_loc_next;
16385               last_cfa = next_cfa;
16386               start_label = last_label;
16387             }
16388           last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
16389           break;
16390
16391         case DW_CFA_advance_loc:
16392           /* The encoding is complex enough that we should never emit this.  */
16393           gcc_unreachable ();
16394
16395         default:
16396           lookup_cfa_1 (cfi, &next_cfa, &remember);
16397           break;
16398         }
16399       if (ix + 1 == fde->dw_fde_switch_cfi_index)
16400         {
16401           if (!cfa_equal_p (&last_cfa, &next_cfa))
16402             {
16403               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16404                                          start_label, last_label, section);
16405
16406               list_tail = &(*list_tail)->dw_loc_next;
16407               last_cfa = next_cfa;
16408               start_label = last_label;
16409             }
16410           *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16411                                      start_label, fde->dw_fde_end, section);
16412           list_tail = &(*list_tail)->dw_loc_next;
16413           start_label = last_label = fde->dw_fde_second_begin;
16414         }
16415     }
16416
16417   if (!cfa_equal_p (&last_cfa, &next_cfa))
16418     {
16419       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16420                                  start_label, last_label, section);
16421       list_tail = &(*list_tail)->dw_loc_next;
16422       start_label = last_label;
16423     }
16424
16425   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
16426                              start_label,
16427                              fde->dw_fde_second_begin
16428                              ? fde->dw_fde_second_end : fde->dw_fde_end,
16429                              section);
16430
16431   if (list && list->dw_loc_next)
16432     gen_llsym (list);
16433
16434   return list;
16435 }
16436
16437 /* Compute a displacement from the "steady-state frame pointer" to the
16438    frame base (often the same as the CFA), and store it in
16439    frame_pointer_fb_offset.  OFFSET is added to the displacement
16440    before the latter is negated.  */
16441
16442 static void
16443 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
16444 {
16445   rtx reg, elim;
16446
16447 #ifdef FRAME_POINTER_CFA_OFFSET
16448   reg = frame_pointer_rtx;
16449   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
16450 #else
16451   reg = arg_pointer_rtx;
16452   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
16453 #endif
16454
16455   elim = (ira_use_lra_p
16456           ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
16457           : eliminate_regs (reg, VOIDmode, NULL_RTX));
16458   if (GET_CODE (elim) == PLUS)
16459     {
16460       offset += INTVAL (XEXP (elim, 1));
16461       elim = XEXP (elim, 0);
16462     }
16463
16464   frame_pointer_fb_offset = -offset;
16465
16466   /* ??? AVR doesn't set up valid eliminations when there is no stack frame
16467      in which to eliminate.  This is because it's stack pointer isn't 
16468      directly accessible as a register within the ISA.  To work around
16469      this, assume that while we cannot provide a proper value for
16470      frame_pointer_fb_offset, we won't need one either.  */
16471   frame_pointer_fb_offset_valid
16472     = ((SUPPORTS_STACK_ALIGNMENT
16473         && (elim == hard_frame_pointer_rtx
16474             || elim == stack_pointer_rtx))
16475        || elim == (frame_pointer_needed
16476                    ? hard_frame_pointer_rtx
16477                    : stack_pointer_rtx));
16478 }
16479
16480 /* Generate a DW_AT_name attribute given some string value to be included as
16481    the value of the attribute.  */
16482
16483 static void
16484 add_name_attribute (dw_die_ref die, const char *name_string)
16485 {
16486   if (name_string != NULL && *name_string != 0)
16487     {
16488       if (demangle_name_func)
16489         name_string = (*demangle_name_func) (name_string);
16490
16491       add_AT_string (die, DW_AT_name, name_string);
16492     }
16493 }
16494
16495 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
16496    DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
16497    of TYPE accordingly.
16498
16499    ??? This is a temporary measure until after we're able to generate
16500    regular DWARF for the complex Ada type system.  */
16501
16502 static void 
16503 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
16504                                      dw_die_ref context_die)
16505 {
16506   tree dtype;
16507   dw_die_ref dtype_die;
16508
16509   if (!lang_hooks.types.descriptive_type)
16510     return;
16511
16512   dtype = lang_hooks.types.descriptive_type (type);
16513   if (!dtype)
16514     return;
16515
16516   dtype_die = lookup_type_die (dtype);
16517   if (!dtype_die)
16518     {
16519       gen_type_die (dtype, context_die);
16520       dtype_die = lookup_type_die (dtype);
16521       gcc_assert (dtype_die);
16522     }
16523
16524   add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
16525 }
16526
16527 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir.  */
16528
16529 static const char *
16530 comp_dir_string (void)
16531 {
16532   const char *wd;
16533   char *wd1;
16534   static const char *cached_wd = NULL;
16535
16536   if (cached_wd != NULL)
16537     return cached_wd;
16538
16539   wd = get_src_pwd ();
16540   if (wd == NULL)
16541     return NULL;
16542
16543   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16544     {
16545       int wdlen;
16546
16547       wdlen = strlen (wd);
16548       wd1 = ggc_vec_alloc<char> (wdlen + 2);
16549       strcpy (wd1, wd);
16550       wd1 [wdlen] = DIR_SEPARATOR;
16551       wd1 [wdlen + 1] = 0;
16552       wd = wd1;
16553     }
16554
16555   cached_wd = remap_debug_filename (wd);
16556   return cached_wd;
16557 }
16558
16559 /* Generate a DW_AT_comp_dir attribute for DIE.  */
16560
16561 static void
16562 add_comp_dir_attribute (dw_die_ref die)
16563 {
16564   const char * wd = comp_dir_string ();
16565   if (wd != NULL)
16566     add_AT_string (die, DW_AT_comp_dir, wd);
16567 }
16568
16569 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
16570    pointer computation, ...), output a representation for that bound according
16571    to the accepted FORMS (see enum dw_scalar_form) and add it to DIE.  See
16572    loc_list_from_tree for the meaning of CONTEXT.  */
16573
16574 static void
16575 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
16576                  int forms, const struct loc_descr_context *context)
16577 {
16578   dw_die_ref ctx, decl_die;
16579   dw_loc_list_ref list;
16580
16581   bool strip_conversions = true;
16582
16583   while (strip_conversions)
16584     switch (TREE_CODE (value))
16585       {
16586       case ERROR_MARK:
16587       case SAVE_EXPR:
16588         return;
16589
16590       CASE_CONVERT:
16591       case VIEW_CONVERT_EXPR:
16592         value = TREE_OPERAND (value, 0);
16593         break;
16594
16595       default:
16596         strip_conversions = false;
16597         break;
16598       }
16599
16600   /* If possible and permitted, output the attribute as a constant.  */
16601   if ((forms & dw_scalar_form_constant) != 0
16602       && TREE_CODE (value) == INTEGER_CST)
16603     {
16604       unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
16605
16606       /* If HOST_WIDE_INT is big enough then represent the bound as
16607          a constant value.  We need to choose a form based on
16608          whether the type is signed or unsigned.  We cannot just
16609          call add_AT_unsigned if the value itself is positive
16610          (add_AT_unsigned might add the unsigned value encoded as
16611          DW_FORM_data[1248]).  Some DWARF consumers will lookup the
16612          bounds type and then sign extend any unsigned values found
16613          for signed types.  This is needed only for
16614          DW_AT_{lower,upper}_bound, since for most other attributes,
16615          consumers will treat DW_FORM_data[1248] as unsigned values,
16616          regardless of the underlying type.  */
16617       if (prec <= HOST_BITS_PER_WIDE_INT
16618           || tree_fits_uhwi_p (value))
16619         {
16620           if (TYPE_UNSIGNED (TREE_TYPE (value)))
16621             add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
16622           else
16623             add_AT_int (die, attr, TREE_INT_CST_LOW (value));
16624         }
16625       else
16626         /* Otherwise represent the bound as an unsigned value with
16627            the precision of its type.  The precision and signedness
16628            of the type will be necessary to re-interpret it
16629            unambiguously.  */
16630         add_AT_wide (die, attr, value);
16631       return;
16632     }
16633
16634   /* Otherwise, if it's possible and permitted too, output a reference to
16635      another DIE.  */
16636   if ((forms & dw_scalar_form_reference) != 0)
16637     {
16638       tree decl = NULL_TREE;
16639
16640       /* Some type attributes reference an outer type.  For instance, the upper
16641          bound of an array may reference an embedding record (this happens in
16642          Ada).  */
16643       if (TREE_CODE (value) == COMPONENT_REF
16644           && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
16645           && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
16646         decl = TREE_OPERAND (value, 1);
16647
16648       else if (TREE_CODE (value) == VAR_DECL
16649                || TREE_CODE (value) == PARM_DECL
16650                || TREE_CODE (value) == RESULT_DECL)
16651         decl = value;
16652
16653       if (decl != NULL_TREE)
16654         {
16655           dw_die_ref decl_die = lookup_decl_die (decl);
16656
16657           /* ??? Can this happen, or should the variable have been bound
16658              first?  Probably it can, since I imagine that we try to create
16659              the types of parameters in the order in which they exist in
16660              the list, and won't have created a forward reference to a
16661              later parameter.  */
16662           if (decl_die != NULL)
16663             {
16664               add_AT_die_ref (die, attr, decl_die);
16665               return;
16666             }
16667         }
16668     }
16669
16670   /* Last chance: try to create a stack operation procedure to evaluate the
16671      value.  Do nothing if even that is not possible or permitted.  */
16672   if ((forms & dw_scalar_form_exprloc) == 0)
16673     return;
16674
16675   list = loc_list_from_tree (value, 2, context);
16676   if (list == NULL || single_element_loc_list_p (list))
16677     {
16678       /* If this attribute is not a reference nor constant, it is
16679          a DWARF expression rather than location description.  For that
16680          loc_list_from_tree (value, 0, &context) is needed.  */
16681       dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
16682       if (list2 && single_element_loc_list_p (list2))
16683         {
16684           add_AT_loc (die, attr, list2->expr);
16685           return;
16686         }
16687     }
16688
16689   /* If that failed to give a single element location list, fall back to
16690      outputting this as a reference... still if permitted.  */
16691   if (list == NULL || (forms & dw_scalar_form_reference) == 0)
16692     return;
16693
16694   if (current_function_decl == 0)
16695     ctx = comp_unit_die ();
16696   else
16697     ctx = lookup_decl_die (current_function_decl);
16698
16699   decl_die = new_die (DW_TAG_variable, ctx, value);
16700   add_AT_flag (decl_die, DW_AT_artificial, 1);
16701   add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, ctx);
16702   add_AT_location_description (decl_die, DW_AT_location, list);
16703   add_AT_die_ref (die, attr, decl_die);
16704 }
16705
16706 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
16707    default.  */
16708
16709 static int
16710 lower_bound_default (void)
16711 {
16712   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16713     {
16714     case DW_LANG_C:
16715     case DW_LANG_C89:
16716     case DW_LANG_C99:
16717     case DW_LANG_C11:
16718     case DW_LANG_C_plus_plus:
16719     case DW_LANG_C_plus_plus_11:
16720     case DW_LANG_C_plus_plus_14:
16721     case DW_LANG_ObjC:
16722     case DW_LANG_ObjC_plus_plus:
16723     case DW_LANG_Java:
16724       return 0;
16725     case DW_LANG_Fortran77:
16726     case DW_LANG_Fortran90:
16727     case DW_LANG_Fortran95:
16728       return 1;
16729     case DW_LANG_UPC:
16730     case DW_LANG_D:
16731     case DW_LANG_Python:
16732       return dwarf_version >= 4 ? 0 : -1;
16733     case DW_LANG_Ada95:
16734     case DW_LANG_Ada83:
16735     case DW_LANG_Cobol74:
16736     case DW_LANG_Cobol85:
16737     case DW_LANG_Pascal83:
16738     case DW_LANG_Modula2:
16739     case DW_LANG_PLI:
16740       return dwarf_version >= 4 ? 1 : -1;
16741     default:
16742       return -1;
16743     }
16744 }
16745
16746 /* Given a tree node describing an array bound (either lower or upper) output
16747    a representation for that bound.  */
16748
16749 static void
16750 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
16751                 tree bound, const struct loc_descr_context *context)
16752 {
16753   int dflt;
16754
16755   while (1)
16756     switch (TREE_CODE (bound))
16757       {
16758       /* Strip all conversions.  */
16759       CASE_CONVERT:
16760       case VIEW_CONVERT_EXPR:
16761         bound = TREE_OPERAND (bound, 0);
16762         break;
16763
16764       /* All fixed-bounds are represented by INTEGER_CST nodes.  Lower bounds
16765          are even omitted when they are the default.  */
16766       case INTEGER_CST:
16767         /* If the value for this bound is the default one, we can even omit the
16768            attribute.  */
16769         if (bound_attr == DW_AT_lower_bound
16770             && tree_fits_shwi_p (bound)
16771             && (dflt = lower_bound_default ()) != -1
16772             && tree_to_shwi (bound) == dflt)
16773           return;
16774
16775         /* FALLTHRU */
16776
16777       default:
16778         add_scalar_info (subrange_die, bound_attr, bound,
16779                          dw_scalar_form_constant
16780                          | dw_scalar_form_exprloc
16781                          | dw_scalar_form_reference,
16782                          context);
16783         return;
16784       }
16785 }
16786
16787 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16788    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16789    Note that the block of subscript information for an array type also
16790    includes information about the element type of the given array type.  */
16791
16792 static void
16793 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16794 {
16795   unsigned dimension_number;
16796   tree lower, upper;
16797   dw_die_ref subrange_die;
16798
16799   for (dimension_number = 0;
16800        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16801        type = TREE_TYPE (type), dimension_number++)
16802     {
16803       tree domain = TYPE_DOMAIN (type);
16804
16805       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16806         break;
16807
16808       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16809          and (in GNU C only) variable bounds.  Handle all three forms
16810          here.  */
16811       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16812       if (domain)
16813         {
16814           /* We have an array type with specified bounds.  */
16815           lower = TYPE_MIN_VALUE (domain);
16816           upper = TYPE_MAX_VALUE (domain);
16817
16818           /* Define the index type.  */
16819           if (TREE_TYPE (domain))
16820             {
16821               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
16822                  TREE_TYPE field.  We can't emit debug info for this
16823                  because it is an unnamed integral type.  */
16824               if (TREE_CODE (domain) == INTEGER_TYPE
16825                   && TYPE_NAME (domain) == NULL_TREE
16826                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16827                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16828                 ;
16829               else
16830                 add_type_attribute (subrange_die, TREE_TYPE (domain),
16831                                     TYPE_UNQUALIFIED, type_die);
16832             }
16833
16834           /* ??? If upper is NULL, the array has unspecified length,
16835              but it does have a lower bound.  This happens with Fortran
16836                dimension arr(N:*)
16837              Since the debugger is definitely going to need to know N
16838              to produce useful results, go ahead and output the lower
16839              bound solo, and hope the debugger can cope.  */
16840
16841           add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
16842           if (upper)
16843             add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
16844         }
16845
16846       /* Otherwise we have an array type with an unspecified length.  The
16847          DWARF-2 spec does not say how to handle this; let's just leave out the
16848          bounds.  */
16849     }
16850 }
16851
16852 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size.  */
16853
16854 static void
16855 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16856 {
16857   dw_die_ref decl_die;
16858   HOST_WIDE_INT size;
16859
16860   switch (TREE_CODE (tree_node))
16861     {
16862     case ERROR_MARK:
16863       size = 0;
16864       break;
16865     case ENUMERAL_TYPE:
16866     case RECORD_TYPE:
16867     case UNION_TYPE:
16868     case QUAL_UNION_TYPE:
16869       if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
16870           && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
16871         {
16872           add_AT_die_ref (die, DW_AT_byte_size, decl_die);
16873           return;
16874         }
16875       size = int_size_in_bytes (tree_node);
16876       break;
16877     case FIELD_DECL:
16878       /* For a data member of a struct or union, the DW_AT_byte_size is
16879          generally given as the number of bytes normally allocated for an
16880          object of the *declared* type of the member itself.  This is true
16881          even for bit-fields.  */
16882       size = int_size_in_bytes (field_type (tree_node));
16883       break;
16884     default:
16885       gcc_unreachable ();
16886     }
16887
16888   /* Note that `size' might be -1 when we get to this point.  If it is, that
16889      indicates that the byte size of the entity in question is variable.  We
16890      have no good way of expressing this fact in Dwarf at the present time,
16891      when location description was not used by the caller code instead.  */
16892   if (size >= 0)
16893     add_AT_unsigned (die, DW_AT_byte_size, size);
16894 }
16895
16896 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16897    which specifies the distance in bits from the highest order bit of the
16898    "containing object" for the bit-field to the highest order bit of the
16899    bit-field itself.
16900
16901    For any given bit-field, the "containing object" is a hypothetical object
16902    (of some integral or enum type) within which the given bit-field lives.  The
16903    type of this hypothetical "containing object" is always the same as the
16904    declared type of the individual bit-field itself.  The determination of the
16905    exact location of the "containing object" for a bit-field is rather
16906    complicated.  It's handled by the `field_byte_offset' function (above).
16907
16908    Note that it is the size (in bytes) of the hypothetical "containing object"
16909    which will be given in the DW_AT_byte_size attribute for this bit-field.
16910    (See `byte_size_attribute' above).  */
16911
16912 static inline void
16913 add_bit_offset_attribute (dw_die_ref die, tree decl)
16914 {
16915   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16916   tree type = DECL_BIT_FIELD_TYPE (decl);
16917   HOST_WIDE_INT bitpos_int;
16918   HOST_WIDE_INT highest_order_object_bit_offset;
16919   HOST_WIDE_INT highest_order_field_bit_offset;
16920   HOST_WIDE_INT bit_offset;
16921
16922   /* Must be a field and a bit field.  */
16923   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16924
16925   /* We can't yet handle bit-fields whose offsets are variable, so if we
16926      encounter such things, just return without generating any attribute
16927      whatsoever.  Likewise for variable or too large size.  */
16928   if (! tree_fits_shwi_p (bit_position (decl))
16929       || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
16930     return;
16931
16932   bitpos_int = int_bit_position (decl);
16933
16934   /* Note that the bit offset is always the distance (in bits) from the
16935      highest-order bit of the "containing object" to the highest-order bit of
16936      the bit-field itself.  Since the "high-order end" of any object or field
16937      is different on big-endian and little-endian machines, the computation
16938      below must take account of these differences.  */
16939   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
16940   highest_order_field_bit_offset = bitpos_int;
16941
16942   if (! BYTES_BIG_ENDIAN)
16943     {
16944       highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
16945       highest_order_object_bit_offset += simple_type_size_in_bits (type);
16946     }
16947
16948   bit_offset
16949     = (! BYTES_BIG_ENDIAN
16950        ? highest_order_object_bit_offset - highest_order_field_bit_offset
16951        : highest_order_field_bit_offset - highest_order_object_bit_offset);
16952
16953   if (bit_offset < 0)
16954     add_AT_int (die, DW_AT_bit_offset, bit_offset);
16955   else
16956     add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
16957 }
16958
16959 /* For a FIELD_DECL node which represents a bit field, output an attribute
16960    which specifies the length in bits of the given field.  */
16961
16962 static inline void
16963 add_bit_size_attribute (dw_die_ref die, tree decl)
16964 {
16965   /* Must be a field and a bit field.  */
16966   gcc_assert (TREE_CODE (decl) == FIELD_DECL
16967               && DECL_BIT_FIELD_TYPE (decl));
16968
16969   if (tree_fits_uhwi_p (DECL_SIZE (decl)))
16970     add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
16971 }
16972
16973 /* If the compiled language is ANSI C, then add a 'prototyped'
16974    attribute, if arg types are given for the parameters of a function.  */
16975
16976 static inline void
16977 add_prototyped_attribute (dw_die_ref die, tree func_type)
16978 {
16979   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16980     {
16981     case DW_LANG_C:
16982     case DW_LANG_C89:
16983     case DW_LANG_C99:
16984     case DW_LANG_C11:
16985     case DW_LANG_ObjC:
16986       if (prototype_p (func_type))
16987         add_AT_flag (die, DW_AT_prototyped, 1);
16988       break;
16989     default:
16990       break;
16991     }
16992 }
16993
16994 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
16995    by looking in either the type declaration or object declaration
16996    equate table.  */
16997
16998 static inline dw_die_ref
16999 add_abstract_origin_attribute (dw_die_ref die, tree origin)
17000 {
17001   dw_die_ref origin_die = NULL;
17002
17003   if (TREE_CODE (origin) != FUNCTION_DECL)
17004     {
17005       /* We may have gotten separated from the block for the inlined
17006          function, if we're in an exception handler or some such; make
17007          sure that the abstract function has been written out.
17008
17009          Doing this for nested functions is wrong, however; functions are
17010          distinct units, and our context might not even be inline.  */
17011       tree fn = origin;
17012
17013       if (TYPE_P (fn))
17014         fn = TYPE_STUB_DECL (fn);
17015
17016       fn = decl_function_context (fn);
17017       if (fn)
17018         dwarf2out_abstract_function (fn);
17019     }
17020
17021   if (DECL_P (origin))
17022     origin_die = lookup_decl_die (origin);
17023   else if (TYPE_P (origin))
17024     origin_die = lookup_type_die (origin);
17025
17026   /* XXX: Functions that are never lowered don't always have correct block
17027      trees (in the case of java, they simply have no block tree, in some other
17028      languages).  For these functions, there is nothing we can really do to
17029      output correct debug info for inlined functions in all cases.  Rather
17030      than die, we'll just produce deficient debug info now, in that we will
17031      have variables without a proper abstract origin.  In the future, when all
17032      functions are lowered, we should re-add a gcc_assert (origin_die)
17033      here.  */
17034
17035   if (origin_die)
17036     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
17037   return origin_die;
17038 }
17039
17040 /* We do not currently support the pure_virtual attribute.  */
17041
17042 static inline void
17043 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
17044 {
17045   if (DECL_VINDEX (func_decl))
17046     {
17047       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
17048
17049       if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
17050         add_AT_loc (die, DW_AT_vtable_elem_location,
17051                     new_loc_descr (DW_OP_constu,
17052                                    tree_to_shwi (DECL_VINDEX (func_decl)),
17053                                    0));
17054
17055       /* GNU extension: Record what type this method came from originally.  */
17056       if (debug_info_level > DINFO_LEVEL_TERSE
17057           && DECL_CONTEXT (func_decl))
17058         add_AT_die_ref (die, DW_AT_containing_type,
17059                         lookup_type_die (DECL_CONTEXT (func_decl)));
17060     }
17061 }
17062 \f
17063 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
17064    given decl.  This used to be a vendor extension until after DWARF 4
17065    standardized it.  */
17066
17067 static void
17068 add_linkage_attr (dw_die_ref die, tree decl)
17069 {
17070   const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
17071
17072   /* Mimic what assemble_name_raw does with a leading '*'.  */
17073   if (name[0] == '*')
17074     name = &name[1];
17075
17076   if (dwarf_version >= 4)
17077     add_AT_string (die, DW_AT_linkage_name, name);
17078   else
17079     add_AT_string (die, DW_AT_MIPS_linkage_name, name);
17080 }
17081
17082 /* Add source coordinate attributes for the given decl.  */
17083
17084 static void
17085 add_src_coords_attributes (dw_die_ref die, tree decl)
17086 {
17087   expanded_location s;
17088
17089   if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
17090     return;
17091   s = expand_location (DECL_SOURCE_LOCATION (decl));
17092   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
17093   add_AT_unsigned (die, DW_AT_decl_line, s.line);
17094 }
17095
17096 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl.  */
17097
17098 static void
17099 add_linkage_name (dw_die_ref die, tree decl)
17100 {
17101   if (debug_info_level > DINFO_LEVEL_NONE
17102       && (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
17103       && TREE_PUBLIC (decl)
17104       && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
17105       && die->die_tag != DW_TAG_member)
17106     {
17107       /* Defer until we have an assembler name set.  */
17108       if (!DECL_ASSEMBLER_NAME_SET_P (decl))
17109         {
17110           limbo_die_node *asm_name;
17111
17112           asm_name = ggc_cleared_alloc<limbo_die_node> ();
17113           asm_name->die = die;
17114           asm_name->created_for = decl;
17115           asm_name->next = deferred_asm_name;
17116           deferred_asm_name = asm_name;
17117         }
17118       else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
17119         add_linkage_attr (die, decl);
17120     }
17121 }
17122
17123 /* Add a DW_AT_name attribute and source coordinate attribute for the
17124    given decl, but only if it actually has a name.  */
17125
17126 static void
17127 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
17128 {
17129   tree decl_name;
17130
17131   decl_name = DECL_NAME (decl);
17132   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
17133     {
17134       const char *name = dwarf2_name (decl, 0);
17135       if (name)
17136         add_name_attribute (die, name);
17137       if (! DECL_ARTIFICIAL (decl))
17138         add_src_coords_attributes (die, decl);
17139
17140       add_linkage_name (die, decl);
17141     }
17142
17143 #ifdef VMS_DEBUGGING_INFO
17144   /* Get the function's name, as described by its RTL.  This may be different
17145      from the DECL_NAME name used in the source file.  */
17146   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
17147     {
17148       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
17149                   XEXP (DECL_RTL (decl), 0), false);
17150       vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
17151     }
17152 #endif /* VMS_DEBUGGING_INFO */
17153 }
17154
17155 #ifdef VMS_DEBUGGING_INFO
17156 /* Output the debug main pointer die for VMS */
17157
17158 void
17159 dwarf2out_vms_debug_main_pointer (void)
17160 {
17161   char label[MAX_ARTIFICIAL_LABEL_BYTES];
17162   dw_die_ref die;
17163
17164   /* Allocate the VMS debug main subprogram die.  */
17165   die = ggc_cleared_alloc<die_node> ();
17166   die->die_tag = DW_TAG_subprogram;
17167   add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
17168   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
17169                                current_function_funcdef_no);
17170   add_AT_lbl_id (die, DW_AT_entry_pc, label);
17171
17172   /* Make it the first child of comp_unit_die ().  */
17173   die->die_parent = comp_unit_die ();
17174   if (comp_unit_die ()->die_child)
17175     {
17176       die->die_sib = comp_unit_die ()->die_child->die_sib;
17177       comp_unit_die ()->die_child->die_sib = die;
17178     }
17179   else
17180     {
17181       die->die_sib = die;
17182       comp_unit_die ()->die_child = die;
17183     }
17184 }
17185 #endif /* VMS_DEBUGGING_INFO */
17186
17187 /* Push a new declaration scope.  */
17188
17189 static void
17190 push_decl_scope (tree scope)
17191 {
17192   vec_safe_push (decl_scope_table, scope);
17193 }
17194
17195 /* Pop a declaration scope.  */
17196
17197 static inline void
17198 pop_decl_scope (void)
17199 {
17200   decl_scope_table->pop ();
17201 }
17202
17203 /* walk_tree helper function for uses_local_type, below.  */
17204
17205 static tree
17206 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
17207 {
17208   if (!TYPE_P (*tp))
17209     *walk_subtrees = 0;
17210   else
17211     {
17212       tree name = TYPE_NAME (*tp);
17213       if (name && DECL_P (name) && decl_function_context (name))
17214         return *tp;
17215     }
17216   return NULL_TREE;
17217 }
17218
17219 /* If TYPE involves a function-local type (including a local typedef to a
17220    non-local type), returns that type; otherwise returns NULL_TREE.  */
17221
17222 static tree
17223 uses_local_type (tree type)
17224 {
17225   tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
17226   return used;
17227 }
17228
17229 /* Return the DIE for the scope that immediately contains this type.
17230    Non-named types that do not involve a function-local type get global
17231    scope.  Named types nested in namespaces or other types get their
17232    containing scope.  All other types (i.e. function-local named types) get
17233    the current active scope.  */
17234
17235 static dw_die_ref
17236 scope_die_for (tree t, dw_die_ref context_die)
17237 {
17238   dw_die_ref scope_die = NULL;
17239   tree containing_scope;
17240
17241   /* Non-types always go in the current scope.  */
17242   gcc_assert (TYPE_P (t));
17243
17244   /* Use the scope of the typedef, rather than the scope of the type
17245      it refers to.  */
17246   if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
17247     containing_scope = DECL_CONTEXT (TYPE_NAME (t));
17248   else
17249     containing_scope = TYPE_CONTEXT (t);
17250
17251   /* Use the containing namespace if there is one.  */
17252   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
17253     {
17254       if (context_die == lookup_decl_die (containing_scope))
17255         /* OK */;
17256       else if (debug_info_level > DINFO_LEVEL_TERSE)
17257         context_die = get_context_die (containing_scope);
17258       else
17259         containing_scope = NULL_TREE;
17260     }
17261
17262   /* Ignore function type "scopes" from the C frontend.  They mean that
17263      a tagged type is local to a parmlist of a function declarator, but
17264      that isn't useful to DWARF.  */
17265   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
17266     containing_scope = NULL_TREE;
17267
17268   if (SCOPE_FILE_SCOPE_P (containing_scope))
17269     {
17270       /* If T uses a local type keep it local as well, to avoid references
17271          to function-local DIEs from outside the function.  */
17272       if (current_function_decl && uses_local_type (t))
17273         scope_die = context_die;
17274       else
17275         scope_die = comp_unit_die ();
17276     }
17277   else if (TYPE_P (containing_scope))
17278     {
17279       /* For types, we can just look up the appropriate DIE.  */
17280       if (debug_info_level > DINFO_LEVEL_TERSE)
17281         scope_die = get_context_die (containing_scope);
17282       else
17283         {
17284           scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
17285           if (scope_die == NULL)
17286             scope_die = comp_unit_die ();
17287         }
17288     }
17289   else
17290     scope_die = context_die;
17291
17292   return scope_die;
17293 }
17294
17295 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
17296
17297 static inline int
17298 local_scope_p (dw_die_ref context_die)
17299 {
17300   for (; context_die; context_die = context_die->die_parent)
17301     if (context_die->die_tag == DW_TAG_inlined_subroutine
17302         || context_die->die_tag == DW_TAG_subprogram)
17303       return 1;
17304
17305   return 0;
17306 }
17307
17308 /* Returns nonzero if CONTEXT_DIE is a class.  */
17309
17310 static inline int
17311 class_scope_p (dw_die_ref context_die)
17312 {
17313   return (context_die
17314           && (context_die->die_tag == DW_TAG_structure_type
17315               || context_die->die_tag == DW_TAG_class_type
17316               || context_die->die_tag == DW_TAG_interface_type
17317               || context_die->die_tag == DW_TAG_union_type));
17318 }
17319
17320 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17321    whether or not to treat a DIE in this context as a declaration.  */
17322
17323 static inline int
17324 class_or_namespace_scope_p (dw_die_ref context_die)
17325 {
17326   return (class_scope_p (context_die)
17327           || (context_die && context_die->die_tag == DW_TAG_namespace));
17328 }
17329
17330 /* Many forms of DIEs require a "type description" attribute.  This
17331    routine locates the proper "type descriptor" die for the type given
17332    by 'type' plus any additional qualifiers given by 'cv_quals', and
17333    adds a DW_AT_type attribute below the given die.  */
17334
17335 static void
17336 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
17337                     dw_die_ref context_die)
17338 {
17339   enum tree_code code  = TREE_CODE (type);
17340   dw_die_ref type_die  = NULL;
17341
17342   /* ??? If this type is an unnamed subrange type of an integral, floating-point
17343      or fixed-point type, use the inner type.  This is because we have no
17344      support for unnamed types in base_type_die.  This can happen if this is
17345      an Ada subrange type.  Correct solution is emit a subrange type die.  */
17346   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
17347       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
17348     type = TREE_TYPE (type), code = TREE_CODE (type);
17349
17350   if (code == ERROR_MARK
17351       /* Handle a special case.  For functions whose return type is void, we
17352          generate *no* type attribute.  (Note that no object may have type
17353          `void', so this only applies to function return types).  */
17354       || code == VOID_TYPE)
17355     return;
17356
17357   type_die = modified_type_die (type,
17358                                 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
17359                                 context_die);
17360
17361   if (type_die != NULL)
17362     add_AT_die_ref (object_die, DW_AT_type, type_die);
17363 }
17364
17365 /* Given an object die, add the calling convention attribute for the
17366    function call type.  */
17367 static void
17368 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
17369 {
17370   enum dwarf_calling_convention value = DW_CC_normal;
17371
17372   value = ((enum dwarf_calling_convention)
17373            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
17374
17375   if (is_fortran ()
17376       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
17377     {
17378       /* DWARF 2 doesn't provide a way to identify a program's source-level
17379         entry point.  DW_AT_calling_convention attributes are only meant
17380         to describe functions' calling conventions.  However, lacking a
17381         better way to signal the Fortran main program, we used this for 
17382         a long time, following existing custom.  Now, DWARF 4 has 
17383         DW_AT_main_subprogram, which we add below, but some tools still
17384         rely on the old way, which we thus keep.  */
17385       value = DW_CC_program;
17386
17387       if (dwarf_version >= 4 || !dwarf_strict)
17388         add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
17389     }
17390
17391   /* Only add the attribute if the backend requests it, and
17392      is not DW_CC_normal.  */
17393   if (value && (value != DW_CC_normal))
17394     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
17395 }
17396
17397 /* Given a tree pointer to a struct, class, union, or enum type node, return
17398    a pointer to the (string) tag name for the given type, or zero if the type
17399    was declared without a tag.  */
17400
17401 static const char *
17402 type_tag (const_tree type)
17403 {
17404   const char *name = 0;
17405
17406   if (TYPE_NAME (type) != 0)
17407     {
17408       tree t = 0;
17409
17410       /* Find the IDENTIFIER_NODE for the type name.  */
17411       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
17412           && !TYPE_NAMELESS (type))
17413         t = TYPE_NAME (type);
17414
17415       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
17416          a TYPE_DECL node, regardless of whether or not a `typedef' was
17417          involved.  */
17418       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
17419                && ! DECL_IGNORED_P (TYPE_NAME (type)))
17420         {
17421           /* We want to be extra verbose.  Don't call dwarf_name if
17422              DECL_NAME isn't set.  The default hook for decl_printable_name
17423              doesn't like that, and in this context it's correct to return
17424              0, instead of "<anonymous>" or the like.  */
17425           if (DECL_NAME (TYPE_NAME (type))
17426               && !DECL_NAMELESS (TYPE_NAME (type)))
17427             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
17428         }
17429
17430       /* Now get the name as a string, or invent one.  */
17431       if (!name && t != 0)
17432         name = IDENTIFIER_POINTER (t);
17433     }
17434
17435   return (name == 0 || *name == '\0') ? 0 : name;
17436 }
17437
17438 /* Return the type associated with a data member, make a special check
17439    for bit field types.  */
17440
17441 static inline tree
17442 member_declared_type (const_tree member)
17443 {
17444   return (DECL_BIT_FIELD_TYPE (member)
17445           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
17446 }
17447
17448 /* Get the decl's label, as described by its RTL. This may be different
17449    from the DECL_NAME name used in the source file.  */
17450
17451 #if 0
17452 static const char *
17453 decl_start_label (tree decl)
17454 {
17455   rtx x;
17456   const char *fnname;
17457
17458   x = DECL_RTL (decl);
17459   gcc_assert (MEM_P (x));
17460
17461   x = XEXP (x, 0);
17462   gcc_assert (GET_CODE (x) == SYMBOL_REF);
17463
17464   fnname = XSTR (x, 0);
17465   return fnname;
17466 }
17467 #endif
17468 \f
17469 /* These routines generate the internal representation of the DIE's for
17470    the compilation unit.  Debugging information is collected by walking
17471    the declaration trees passed in from dwarf2out_decl().  */
17472
17473 static void
17474 gen_array_type_die (tree type, dw_die_ref context_die)
17475 {
17476   dw_die_ref scope_die = scope_die_for (type, context_die);
17477   dw_die_ref array_die;
17478
17479   /* GNU compilers represent multidimensional array types as sequences of one
17480      dimensional array types whose element types are themselves array types.
17481      We sometimes squish that down to a single array_type DIE with multiple
17482      subscripts in the Dwarf debugging info.  The draft Dwarf specification
17483      say that we are allowed to do this kind of compression in C, because
17484      there is no difference between an array of arrays and a multidimensional
17485      array.  We don't do this for Ada to remain as close as possible to the
17486      actual representation, which is especially important against the language
17487      flexibilty wrt arrays of variable size.  */
17488
17489   bool collapse_nested_arrays = !is_ada ();
17490   tree element_type;
17491
17492   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
17493      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
17494   if (TYPE_STRING_FLAG (type)
17495       && TREE_CODE (type) == ARRAY_TYPE
17496       && is_fortran ()
17497       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
17498     {
17499       HOST_WIDE_INT size;
17500
17501       array_die = new_die (DW_TAG_string_type, scope_die, type);
17502       add_name_attribute (array_die, type_tag (type));
17503       equate_type_number_to_die (type, array_die);
17504       size = int_size_in_bytes (type);
17505       if (size >= 0)
17506         add_AT_unsigned (array_die, DW_AT_byte_size, size);
17507       else if (TYPE_DOMAIN (type) != NULL_TREE
17508                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
17509                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17510         {
17511           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17512           dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2, NULL);
17513
17514           size = int_size_in_bytes (TREE_TYPE (szdecl));
17515           if (loc && size > 0)
17516             {
17517               add_AT_location_description (array_die, DW_AT_string_length, loc);
17518               if (size != DWARF2_ADDR_SIZE)
17519                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17520             }
17521         }
17522       return;
17523     }
17524
17525   array_die = new_die (DW_TAG_array_type, scope_die, type);
17526   add_name_attribute (array_die, type_tag (type));
17527   equate_type_number_to_die (type, array_die);
17528
17529   if (TREE_CODE (type) == VECTOR_TYPE)
17530     add_AT_flag (array_die, DW_AT_GNU_vector, 1);
17531
17532   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
17533   if (is_fortran ()
17534       && TREE_CODE (type) == ARRAY_TYPE
17535       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17536       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17537     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17538
17539 #if 0
17540   /* We default the array ordering.  SDB will probably do
17541      the right things even if DW_AT_ordering is not present.  It's not even
17542      an issue until we start to get into multidimensional arrays anyway.  If
17543      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17544      then we'll have to put the DW_AT_ordering attribute back in.  (But if
17545      and when we find out that we need to put these in, we will only do so
17546      for multidimensional arrays.  */
17547   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17548 #endif
17549
17550   if (TREE_CODE (type) == VECTOR_TYPE)
17551     {
17552       /* For VECTOR_TYPEs we use an array die with appropriate bounds.  */
17553       dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
17554       add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
17555       add_bound_info (subrange_die, DW_AT_upper_bound,
17556                       size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
17557     }
17558   else
17559     add_subscript_info (array_die, type, collapse_nested_arrays);
17560
17561   /* Add representation of the type of the elements of this array type and
17562      emit the corresponding DIE if we haven't done it already.  */
17563   element_type = TREE_TYPE (type);
17564   if (collapse_nested_arrays)
17565     while (TREE_CODE (element_type) == ARRAY_TYPE)
17566       {
17567         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17568           break;
17569         element_type = TREE_TYPE (element_type);
17570       }
17571
17572   add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED, context_die);
17573
17574   add_gnat_descriptive_type_attribute (array_die, type, context_die);
17575   if (TYPE_ARTIFICIAL (type))
17576     add_AT_flag (array_die, DW_AT_artificial, 1);
17577
17578   if (get_AT (array_die, DW_AT_name))
17579     add_pubtype (type, array_die);
17580 }
17581
17582 /* This routine generates DIE for array with hidden descriptor, details
17583    are filled into *info by a langhook.  */
17584
17585 static void
17586 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17587                           dw_die_ref context_die)
17588 {
17589   const dw_die_ref scope_die = scope_die_for (type, context_die);
17590   const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
17591   const struct loc_descr_context context = { type, info->base_decl };
17592   int dim;
17593
17594   add_name_attribute (array_die, type_tag (type));
17595   equate_type_number_to_die (type, array_die);
17596
17597   if (info->ndimensions > 1)
17598     switch (info->ordering)
17599       {
17600       case array_descr_ordering_row_major:
17601         add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17602         break;
17603       case array_descr_ordering_column_major:
17604         add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17605         break;
17606       default:
17607         break;
17608       }
17609
17610   if (dwarf_version >= 3 || !dwarf_strict)
17611     {
17612       if (info->data_location)
17613         add_scalar_info (array_die, DW_AT_data_location, info->data_location,
17614                          dw_scalar_form_exprloc, &context);
17615       if (info->associated)
17616         add_scalar_info (array_die, DW_AT_associated, info->associated,
17617                          dw_scalar_form_constant
17618                          | dw_scalar_form_exprloc
17619                          | dw_scalar_form_reference, &context);
17620       if (info->allocated)
17621         add_scalar_info (array_die, DW_AT_allocated, info->allocated,
17622                          dw_scalar_form_constant
17623                          | dw_scalar_form_exprloc
17624                          | dw_scalar_form_reference, &context);
17625     }
17626
17627   add_gnat_descriptive_type_attribute (array_die, type, context_die);
17628
17629   for (dim = 0; dim < info->ndimensions; dim++)
17630     {
17631       dw_die_ref subrange_die
17632         = new_die (DW_TAG_subrange_type, array_die, NULL);
17633
17634       if (info->dimen[dim].bounds_type)
17635         add_type_attribute (subrange_die,
17636                             info->dimen[dim].bounds_type, 0,
17637                             context_die);
17638       if (info->dimen[dim].lower_bound)
17639         add_bound_info (subrange_die, DW_AT_lower_bound,
17640                         info->dimen[dim].lower_bound, &context);
17641       if (info->dimen[dim].upper_bound)
17642         add_bound_info (subrange_die, DW_AT_upper_bound,
17643                         info->dimen[dim].upper_bound, &context);
17644       if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
17645         add_scalar_info (subrange_die, DW_AT_byte_stride,
17646                          info->dimen[dim].stride,
17647                          dw_scalar_form_constant
17648                          | dw_scalar_form_exprloc
17649                          | dw_scalar_form_reference,
17650                          &context);
17651     }
17652
17653   gen_type_die (info->element_type, context_die);
17654   add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
17655                       context_die);
17656
17657   if (get_AT (array_die, DW_AT_name))
17658     add_pubtype (type, array_die);
17659 }
17660
17661 #if 0
17662 static void
17663 gen_entry_point_die (tree decl, dw_die_ref context_die)
17664 {
17665   tree origin = decl_ultimate_origin (decl);
17666   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17667
17668   if (origin != NULL)
17669     add_abstract_origin_attribute (decl_die, origin);
17670   else
17671     {
17672       add_name_and_src_coords_attributes (decl_die, decl);
17673       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17674                           TYPE_UNQUALIFIED, context_die);
17675     }
17676
17677   if (DECL_ABSTRACT_P (decl))
17678     equate_decl_number_to_die (decl, decl_die);
17679   else
17680     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17681 }
17682 #endif
17683
17684 /* Walk through the list of incomplete types again, trying once more to
17685    emit full debugging info for them.  */
17686
17687 static void
17688 retry_incomplete_types (void)
17689 {
17690   int i;
17691
17692   for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17693     if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17694       gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17695 }
17696
17697 /* Determine what tag to use for a record type.  */
17698
17699 static enum dwarf_tag
17700 record_type_tag (tree type)
17701 {
17702   if (! lang_hooks.types.classify_record)
17703     return DW_TAG_structure_type;
17704
17705   switch (lang_hooks.types.classify_record (type))
17706     {
17707     case RECORD_IS_STRUCT:
17708       return DW_TAG_structure_type;
17709
17710     case RECORD_IS_CLASS:
17711       return DW_TAG_class_type;
17712
17713     case RECORD_IS_INTERFACE:
17714       if (dwarf_version >= 3 || !dwarf_strict)
17715         return DW_TAG_interface_type;
17716       return DW_TAG_structure_type;
17717
17718     default:
17719       gcc_unreachable ();
17720     }
17721 }
17722
17723 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
17724    include all of the information about the enumeration values also. Each
17725    enumerated type name/value is listed as a child of the enumerated type
17726    DIE.  */
17727
17728 static dw_die_ref
17729 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17730 {
17731   dw_die_ref type_die = lookup_type_die (type);
17732
17733   if (type_die == NULL)
17734     {
17735       type_die = new_die (DW_TAG_enumeration_type,
17736                           scope_die_for (type, context_die), type);
17737       equate_type_number_to_die (type, type_die);
17738       add_name_attribute (type_die, type_tag (type));
17739       if (dwarf_version >= 4 || !dwarf_strict)
17740         {
17741           if (ENUM_IS_SCOPED (type))
17742             add_AT_flag (type_die, DW_AT_enum_class, 1);
17743           if (ENUM_IS_OPAQUE (type))
17744             add_AT_flag (type_die, DW_AT_declaration, 1);
17745         }
17746     }
17747   else if (! TYPE_SIZE (type))
17748     return type_die;
17749   else
17750     remove_AT (type_die, DW_AT_declaration);
17751
17752   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
17753      given enum type is incomplete, do not generate the DW_AT_byte_size
17754      attribute or the DW_AT_element_list attribute.  */
17755   if (TYPE_SIZE (type))
17756     {
17757       tree link;
17758
17759       TREE_ASM_WRITTEN (type) = 1;
17760       add_byte_size_attribute (type_die, type);
17761       if (dwarf_version >= 3 || !dwarf_strict)
17762         {
17763           tree underlying = lang_hooks.types.enum_underlying_base_type (type);
17764           add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED,
17765                               context_die);
17766         }
17767       if (TYPE_STUB_DECL (type) != NULL_TREE)
17768         {
17769           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17770           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17771         }
17772
17773       /* If the first reference to this type was as the return type of an
17774          inline function, then it may not have a parent.  Fix this now.  */
17775       if (type_die->die_parent == NULL)
17776         add_child_die (scope_die_for (type, context_die), type_die);
17777
17778       for (link = TYPE_VALUES (type);
17779            link != NULL; link = TREE_CHAIN (link))
17780         {
17781           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17782           tree value = TREE_VALUE (link);
17783
17784           add_name_attribute (enum_die,
17785                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17786
17787           if (TREE_CODE (value) == CONST_DECL)
17788             value = DECL_INITIAL (value);
17789
17790           if (simple_type_size_in_bits (TREE_TYPE (value))
17791               <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
17792             {
17793               /* For constant forms created by add_AT_unsigned DWARF
17794                  consumers (GDB, elfutils, etc.) always zero extend
17795                  the value.  Only when the actual value is negative
17796                  do we need to use add_AT_int to generate a constant
17797                  form that can represent negative values.  */
17798               HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
17799               if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
17800                 add_AT_unsigned (enum_die, DW_AT_const_value,
17801                                  (unsigned HOST_WIDE_INT) val);
17802               else
17803                 add_AT_int (enum_die, DW_AT_const_value, val);
17804             }
17805           else
17806             /* Enumeration constants may be wider than HOST_WIDE_INT.  Handle
17807                that here.  TODO: This should be re-worked to use correct
17808                signed/unsigned double tags for all cases.  */
17809             add_AT_wide (enum_die, DW_AT_const_value, value);
17810         }
17811
17812       add_gnat_descriptive_type_attribute (type_die, type, context_die);
17813       if (TYPE_ARTIFICIAL (type))
17814         add_AT_flag (type_die, DW_AT_artificial, 1);
17815     }
17816   else
17817     add_AT_flag (type_die, DW_AT_declaration, 1);
17818
17819   add_pubtype (type, type_die);
17820
17821   return type_die;
17822 }
17823
17824 /* Generate a DIE to represent either a real live formal parameter decl or to
17825    represent just the type of some formal parameter position in some function
17826    type.
17827
17828    Note that this routine is a bit unusual because its argument may be a
17829    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17830    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17831    node.  If it's the former then this function is being called to output a
17832    DIE to represent a formal parameter object (or some inlining thereof).  If
17833    it's the latter, then this function is only being called to output a
17834    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17835    argument type of some subprogram type.
17836    If EMIT_NAME_P is true, name and source coordinate attributes
17837    are emitted.  */
17838
17839 static dw_die_ref
17840 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17841                           dw_die_ref context_die)
17842 {
17843   tree node_or_origin = node ? node : origin;
17844   tree ultimate_origin;
17845   dw_die_ref parm_die
17846     = new_die (DW_TAG_formal_parameter, context_die, node);
17847
17848   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
17849     {
17850     case tcc_declaration:
17851       ultimate_origin = decl_ultimate_origin (node_or_origin);
17852       if (node || ultimate_origin)
17853         origin = ultimate_origin;
17854       if (origin != NULL)
17855         add_abstract_origin_attribute (parm_die, origin);
17856       else if (emit_name_p)
17857         add_name_and_src_coords_attributes (parm_die, node);
17858       if (origin == NULL
17859           || (! DECL_ABSTRACT_P (node_or_origin)
17860               && variably_modified_type_p (TREE_TYPE (node_or_origin),
17861                                            decl_function_context
17862                                                             (node_or_origin))))
17863         {
17864           tree type = TREE_TYPE (node_or_origin);
17865           if (decl_by_reference_p (node_or_origin))
17866             add_type_attribute (parm_die, TREE_TYPE (type),
17867                                 TYPE_UNQUALIFIED, context_die);
17868           else
17869             add_type_attribute (parm_die, type,
17870                                 decl_quals (node_or_origin),
17871                                 context_die);
17872         }
17873       if (origin == NULL && DECL_ARTIFICIAL (node))
17874         add_AT_flag (parm_die, DW_AT_artificial, 1);
17875
17876       if (node && node != origin)
17877         equate_decl_number_to_die (node, parm_die);
17878       if (! DECL_ABSTRACT_P (node_or_origin))
17879         add_location_or_const_value_attribute (parm_die, node_or_origin,
17880                                                node == NULL, DW_AT_location);
17881
17882       break;
17883
17884     case tcc_type:
17885       /* We were called with some kind of a ..._TYPE node.  */
17886       add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED,
17887                           context_die);
17888       break;
17889
17890     default:
17891       gcc_unreachable ();
17892     }
17893
17894   return parm_die;
17895 }
17896
17897 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17898    children DW_TAG_formal_parameter DIEs representing the arguments of the
17899    parameter pack.
17900
17901    PARM_PACK must be a function parameter pack.
17902    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17903    must point to the subsequent arguments of the function PACK_ARG belongs to.
17904    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17905    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17906    following the last one for which a DIE was generated.  */
17907
17908 static dw_die_ref
17909 gen_formal_parameter_pack_die  (tree parm_pack,
17910                                 tree pack_arg,
17911                                 dw_die_ref subr_die,
17912                                 tree *next_arg)
17913 {
17914   tree arg;
17915   dw_die_ref parm_pack_die;
17916
17917   gcc_assert (parm_pack
17918               && lang_hooks.function_parameter_pack_p (parm_pack)
17919               && subr_die);
17920
17921   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17922   add_src_coords_attributes (parm_pack_die, parm_pack);
17923
17924   for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
17925     {
17926       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17927                                                                  parm_pack))
17928         break;
17929       gen_formal_parameter_die (arg, NULL,
17930                                 false /* Don't emit name attribute.  */,
17931                                 parm_pack_die);
17932     }
17933   if (next_arg)
17934     *next_arg = arg;
17935   return parm_pack_die;
17936 }
17937
17938 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17939    at the end of an (ANSI prototyped) formal parameters list.  */
17940
17941 static void
17942 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
17943 {
17944   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
17945 }
17946
17947 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
17948    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
17949    parameters as specified in some function type specification (except for
17950    those which appear as part of a function *definition*).  */
17951
17952 static void
17953 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
17954 {
17955   tree link;
17956   tree formal_type = NULL;
17957   tree first_parm_type;
17958   tree arg;
17959
17960   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
17961     {
17962       arg = DECL_ARGUMENTS (function_or_method_type);
17963       function_or_method_type = TREE_TYPE (function_or_method_type);
17964     }
17965   else
17966     arg = NULL_TREE;
17967
17968   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
17969
17970   /* Make our first pass over the list of formal parameter types and output a
17971      DW_TAG_formal_parameter DIE for each one.  */
17972   for (link = first_parm_type; link; )
17973     {
17974       dw_die_ref parm_die;
17975
17976       formal_type = TREE_VALUE (link);
17977       if (formal_type == void_type_node)
17978         break;
17979
17980       /* Output a (nameless) DIE to represent the formal parameter itself.  */
17981       if (!POINTER_BOUNDS_TYPE_P (formal_type))
17982         {
17983           parm_die = gen_formal_parameter_die (formal_type, NULL,
17984                                                true /* Emit name attribute.  */,
17985                                                context_die);
17986           if (TREE_CODE (function_or_method_type) == METHOD_TYPE
17987               && link == first_parm_type)
17988             {
17989               add_AT_flag (parm_die, DW_AT_artificial, 1);
17990               if (dwarf_version >= 3 || !dwarf_strict)
17991                 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
17992             }
17993           else if (arg && DECL_ARTIFICIAL (arg))
17994             add_AT_flag (parm_die, DW_AT_artificial, 1);
17995         }
17996
17997       link = TREE_CHAIN (link);
17998       if (arg)
17999         arg = DECL_CHAIN (arg);
18000     }
18001
18002   /* If this function type has an ellipsis, add a
18003      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
18004   if (formal_type != void_type_node)
18005     gen_unspecified_parameters_die (function_or_method_type, context_die);
18006
18007   /* Make our second (and final) pass over the list of formal parameter types
18008      and output DIEs to represent those types (as necessary).  */
18009   for (link = TYPE_ARG_TYPES (function_or_method_type);
18010        link && TREE_VALUE (link);
18011        link = TREE_CHAIN (link))
18012     gen_type_die (TREE_VALUE (link), context_die);
18013 }
18014
18015 /* We want to generate the DIE for TYPE so that we can generate the
18016    die for MEMBER, which has been defined; we will need to refer back
18017    to the member declaration nested within TYPE.  If we're trying to
18018    generate minimal debug info for TYPE, processing TYPE won't do the
18019    trick; we need to attach the member declaration by hand.  */
18020
18021 static void
18022 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
18023 {
18024   gen_type_die (type, context_die);
18025
18026   /* If we're trying to avoid duplicate debug info, we may not have
18027      emitted the member decl for this function.  Emit it now.  */
18028   if (TYPE_STUB_DECL (type)
18029       && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
18030       && ! lookup_decl_die (member))
18031     {
18032       dw_die_ref type_die;
18033       gcc_assert (!decl_ultimate_origin (member));
18034
18035       push_decl_scope (type);
18036       type_die = lookup_type_die_strip_naming_typedef (type);
18037       if (TREE_CODE (member) == FUNCTION_DECL)
18038         gen_subprogram_die (member, type_die);
18039       else if (TREE_CODE (member) == FIELD_DECL)
18040         {
18041           /* Ignore the nameless fields that are used to skip bits but handle
18042              C++ anonymous unions and structs.  */
18043           if (DECL_NAME (member) != NULL_TREE
18044               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
18045               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
18046             {
18047               gen_type_die (member_declared_type (member), type_die);
18048               gen_field_die (member, type_die);
18049             }
18050         }
18051       else
18052         gen_variable_die (member, NULL_TREE, type_die);
18053
18054       pop_decl_scope ();
18055     }
18056 }
18057 \f
18058 /* Forward declare these functions, because they are mutually recursive
18059   with their set_block_* pairing functions.  */
18060 static void set_decl_origin_self (tree);
18061 static void set_decl_abstract_flags (tree, int);
18062
18063 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
18064    given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
18065    that it points to the node itself, thus indicating that the node is its
18066    own (abstract) origin.  Additionally, if the BLOCK_ABSTRACT_ORIGIN for
18067    the given node is NULL, recursively descend the decl/block tree which
18068    it is the root of, and for each other ..._DECL or BLOCK node contained
18069    therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
18070    still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
18071    values to point to themselves.  */
18072
18073 static void
18074 set_block_origin_self (tree stmt)
18075 {
18076   if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
18077     {
18078       BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
18079
18080       {
18081         tree local_decl;
18082
18083         for (local_decl = BLOCK_VARS (stmt);
18084              local_decl != NULL_TREE;
18085              local_decl = DECL_CHAIN (local_decl))
18086           /* Do not recurse on nested functions since the inlining status
18087              of parent and child can be different as per the DWARF spec.  */
18088           if (TREE_CODE (local_decl) != FUNCTION_DECL
18089               && !DECL_EXTERNAL (local_decl))
18090             set_decl_origin_self (local_decl);
18091       }
18092
18093       {
18094         tree subblock;
18095
18096         for (subblock = BLOCK_SUBBLOCKS (stmt);
18097              subblock != NULL_TREE;
18098              subblock = BLOCK_CHAIN (subblock))
18099           set_block_origin_self (subblock);     /* Recurse.  */
18100       }
18101     }
18102 }
18103
18104 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
18105    the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
18106    node to so that it points to the node itself, thus indicating that the
18107    node represents its own (abstract) origin.  Additionally, if the
18108    DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
18109    the decl/block tree of which the given node is the root of, and for
18110    each other ..._DECL or BLOCK node contained therein whose
18111    DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
18112    set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
18113    point to themselves.  */
18114
18115 static void
18116 set_decl_origin_self (tree decl)
18117 {
18118   if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
18119     {
18120       DECL_ABSTRACT_ORIGIN (decl) = decl;
18121       if (TREE_CODE (decl) == FUNCTION_DECL)
18122         {
18123           tree arg;
18124
18125           for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
18126             DECL_ABSTRACT_ORIGIN (arg) = arg;
18127           if (DECL_INITIAL (decl) != NULL_TREE
18128               && DECL_INITIAL (decl) != error_mark_node)
18129             set_block_origin_self (DECL_INITIAL (decl));
18130         }
18131     }
18132 }
18133 \f
18134 /* Given a pointer to some BLOCK node, and a boolean value to set the
18135    "abstract" flags to, set that value into the BLOCK_ABSTRACT flag for
18136    the given block, and for all local decls and all local sub-blocks
18137    (recursively) which are contained therein.  */
18138
18139 static void
18140 set_block_abstract_flags (tree stmt, int setting)
18141 {
18142   tree local_decl;
18143   tree subblock;
18144   unsigned int i;
18145
18146   BLOCK_ABSTRACT (stmt) = setting;
18147
18148   for (local_decl = BLOCK_VARS (stmt);
18149        local_decl != NULL_TREE;
18150        local_decl = DECL_CHAIN (local_decl))
18151     if (! DECL_EXTERNAL (local_decl))
18152       set_decl_abstract_flags (local_decl, setting);
18153
18154   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
18155     {
18156       local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
18157       if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
18158           || TREE_CODE (local_decl) == PARM_DECL)
18159         set_decl_abstract_flags (local_decl, setting);
18160     }
18161
18162   for (subblock = BLOCK_SUBBLOCKS (stmt);
18163        subblock != NULL_TREE;
18164        subblock = BLOCK_CHAIN (subblock))
18165     set_block_abstract_flags (subblock, setting);
18166 }
18167
18168 /* Given a pointer to some ..._DECL node, and a boolean value to set the
18169    "abstract" flags to, set that value into the DECL_ABSTRACT_P flag for the
18170    given decl, and (in the case where the decl is a FUNCTION_DECL) also
18171    set the abstract flags for all of the parameters, local vars, local
18172    blocks and sub-blocks (recursively) to the same setting.  */
18173
18174 static void
18175 set_decl_abstract_flags (tree decl, int setting)
18176 {
18177   DECL_ABSTRACT_P (decl) = setting;
18178   if (TREE_CODE (decl) == FUNCTION_DECL)
18179     {
18180       tree arg;
18181
18182       for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
18183         DECL_ABSTRACT_P (arg) = setting;
18184       if (DECL_INITIAL (decl) != NULL_TREE
18185           && DECL_INITIAL (decl) != error_mark_node)
18186         set_block_abstract_flags (DECL_INITIAL (decl), setting);
18187     }
18188 }
18189
18190 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18191    may later generate inlined and/or out-of-line instances of.  */
18192
18193 static void
18194 dwarf2out_abstract_function (tree decl)
18195 {
18196   dw_die_ref old_die;
18197   tree save_fn;
18198   tree context;
18199   int was_abstract;
18200   hash_table<decl_loc_hasher> *old_decl_loc_table;
18201   hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
18202   int old_call_site_count, old_tail_call_site_count;
18203   struct call_arg_loc_node *old_call_arg_locations;
18204
18205   /* Make sure we have the actual abstract inline, not a clone.  */
18206   decl = DECL_ORIGIN (decl);
18207
18208   old_die = lookup_decl_die (decl);
18209   if (old_die && get_AT (old_die, DW_AT_inline))
18210     /* We've already generated the abstract instance.  */
18211     return;
18212
18213   /* We can be called while recursively when seeing block defining inlined subroutine
18214      DIE.  Be sure to not clobber the outer location table nor use it or we would
18215      get locations in abstract instantces.  */
18216   old_decl_loc_table = decl_loc_table;
18217   decl_loc_table = NULL;
18218   old_cached_dw_loc_list_table = cached_dw_loc_list_table;
18219   cached_dw_loc_list_table = NULL;
18220   old_call_arg_locations = call_arg_locations;
18221   call_arg_locations = NULL;
18222   old_call_site_count = call_site_count;
18223   call_site_count = -1;
18224   old_tail_call_site_count = tail_call_site_count;
18225   tail_call_site_count = -1;
18226
18227   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18228      we don't get confused by DECL_ABSTRACT_P.  */
18229   if (debug_info_level > DINFO_LEVEL_TERSE)
18230     {
18231       context = decl_class_context (decl);
18232       if (context)
18233         gen_type_die_for_member
18234           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
18235     }
18236
18237   /* Pretend we've just finished compiling this function.  */
18238   save_fn = current_function_decl;
18239   current_function_decl = decl;
18240
18241   was_abstract = DECL_ABSTRACT_P (decl);
18242   set_decl_abstract_flags (decl, 1);
18243   dwarf2out_decl (decl);
18244   if (! was_abstract)
18245     set_decl_abstract_flags (decl, 0);
18246
18247   current_function_decl = save_fn;
18248   decl_loc_table = old_decl_loc_table;
18249   cached_dw_loc_list_table = old_cached_dw_loc_list_table;
18250   call_arg_locations = old_call_arg_locations;
18251   call_site_count = old_call_site_count;
18252   tail_call_site_count = old_tail_call_site_count;
18253 }
18254
18255 /* Helper function of premark_used_types() which gets called through
18256    htab_traverse.
18257
18258    Marks the DIE of a given type in *SLOT as perennial, so it never gets
18259    marked as unused by prune_unused_types.  */
18260
18261 bool
18262 premark_used_types_helper (tree const &type, void *)
18263 {
18264   dw_die_ref die;
18265
18266   die = lookup_type_die (type);
18267   if (die != NULL)
18268     die->die_perennial_p = 1;
18269   return true;
18270 }
18271
18272 /* Helper function of premark_types_used_by_global_vars which gets called
18273    through htab_traverse.
18274
18275    Marks the DIE of a given type in *SLOT as perennial, so it never gets
18276    marked as unused by prune_unused_types. The DIE of the type is marked
18277    only if the global variable using the type will actually be emitted.  */
18278
18279 int
18280 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
18281                                           void *)
18282 {
18283   struct types_used_by_vars_entry *entry;
18284   dw_die_ref die;
18285
18286   entry = (struct types_used_by_vars_entry *) *slot;
18287   gcc_assert (entry->type != NULL
18288               && entry->var_decl != NULL);
18289   die = lookup_type_die (entry->type);
18290   if (die)
18291     {
18292       /* Ask cgraph if the global variable really is to be emitted.
18293          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
18294       varpool_node *node = varpool_node::get (entry->var_decl);
18295       if (node && node->definition)
18296         {
18297           die->die_perennial_p = 1;
18298           /* Keep the parent DIEs as well.  */
18299           while ((die = die->die_parent) && die->die_perennial_p == 0)
18300             die->die_perennial_p = 1;
18301         }
18302     }
18303   return 1;
18304 }
18305
18306 /* Mark all members of used_types_hash as perennial.  */
18307
18308 static void
18309 premark_used_types (struct function *fun)
18310 {
18311   if (fun && fun->used_types_hash)
18312     fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
18313 }
18314
18315 /* Mark all members of types_used_by_vars_entry as perennial.  */
18316
18317 static void
18318 premark_types_used_by_global_vars (void)
18319 {
18320   if (types_used_by_vars_hash)
18321     types_used_by_vars_hash
18322       ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
18323 }
18324
18325 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
18326    for CA_LOC call arg loc node.  */
18327
18328 static dw_die_ref
18329 gen_call_site_die (tree decl, dw_die_ref subr_die,
18330                    struct call_arg_loc_node *ca_loc)
18331 {
18332   dw_die_ref stmt_die = NULL, die;
18333   tree block = ca_loc->block;
18334
18335   while (block
18336          && block != DECL_INITIAL (decl)
18337          && TREE_CODE (block) == BLOCK)
18338     {
18339       if (block_map.length () > BLOCK_NUMBER (block))
18340         stmt_die = block_map[BLOCK_NUMBER (block)];
18341       if (stmt_die)
18342         break;
18343       block = BLOCK_SUPERCONTEXT (block);
18344     }
18345   if (stmt_die == NULL)
18346     stmt_die = subr_die;
18347   die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
18348   add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
18349   if (ca_loc->tail_call_p)
18350     add_AT_flag (die, DW_AT_GNU_tail_call, 1);
18351   if (ca_loc->symbol_ref)
18352     {
18353       dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
18354       if (tdie)
18355         add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
18356       else
18357         add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
18358     }
18359   return die;
18360 }
18361
18362 /* Generate a DIE to represent a declared function (either file-scope or
18363    block-local).  */
18364
18365 static void
18366 gen_subprogram_die (tree decl, dw_die_ref context_die)
18367 {
18368   tree origin = decl_ultimate_origin (decl);
18369   dw_die_ref subr_die;
18370   tree outer_scope;
18371   dw_die_ref old_die = lookup_decl_die (decl);
18372   int declaration = (current_function_decl != decl
18373                      || class_or_namespace_scope_p (context_die));
18374
18375   premark_used_types (DECL_STRUCT_FUNCTION (decl));
18376
18377   /* It is possible to have both DECL_ABSTRACT_P and DECLARATION be true if we
18378      started to generate the abstract instance of an inline, decided to output
18379      its containing class, and proceeded to emit the declaration of the inline
18380      from the member list for the class.  If so, DECLARATION takes priority;
18381      we'll get back to the abstract instance when done with the class.  */
18382
18383   /* The class-scope declaration DIE must be the primary DIE.  */
18384   if (origin && declaration && class_or_namespace_scope_p (context_die))
18385     {
18386       origin = NULL;
18387       gcc_assert (!old_die);
18388     }
18389
18390   /* Now that the C++ front end lazily declares artificial member fns, we
18391      might need to retrofit the declaration into its class.  */
18392   if (!declaration && !origin && !old_die
18393       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18394       && !class_or_namespace_scope_p (context_die)
18395       && debug_info_level > DINFO_LEVEL_TERSE)
18396     old_die = force_decl_die (decl);
18397
18398   if (origin != NULL)
18399     {
18400       gcc_assert (!declaration || local_scope_p (context_die));
18401
18402       /* Fixup die_parent for the abstract instance of a nested
18403          inline function.  */
18404       if (old_die && old_die->die_parent == NULL)
18405         add_child_die (context_die, old_die);
18406
18407       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18408       add_abstract_origin_attribute (subr_die, origin);
18409       /*  This is where the actual code for a cloned function is.
18410           Let's emit linkage name attribute for it.  This helps
18411           debuggers to e.g, set breakpoints into
18412           constructors/destructors when the user asks "break
18413           K::K".  */
18414       add_linkage_name (subr_die, decl);
18415     }
18416   else if (old_die)
18417     {
18418       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18419       struct dwarf_file_data * file_index = lookup_filename (s.file);
18420
18421       if (!get_AT_flag (old_die, DW_AT_declaration)
18422           /* We can have a normal definition following an inline one in the
18423              case of redefinition of GNU C extern inlines.
18424              It seems reasonable to use AT_specification in this case.  */
18425           && !get_AT (old_die, DW_AT_inline))
18426         {
18427           /* Detect and ignore this case, where we are trying to output
18428              something we have already output.  */
18429           return;
18430         }
18431
18432       /* If the definition comes from the same place as the declaration,
18433          maybe use the old DIE.  We always want the DIE for this function
18434          that has the *_pc attributes to be under comp_unit_die so the
18435          debugger can find it.  We also need to do this for abstract
18436          instances of inlines, since the spec requires the out-of-line copy
18437          to have the same parent.  For local class methods, this doesn't
18438          apply; we just use the old DIE.  */
18439       if ((is_cu_die (old_die->die_parent) || context_die == NULL)
18440           && (DECL_ARTIFICIAL (decl)
18441               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18442                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
18443                       == (unsigned) s.line))))
18444         {
18445           subr_die = old_die;
18446
18447           /* Clear out the declaration attribute and the formal parameters.
18448              Do not remove all children, because it is possible that this
18449              declaration die was forced using force_decl_die(). In such
18450              cases die that forced declaration die (e.g. TAG_imported_module)
18451              is one of the children that we do not want to remove.  */
18452           remove_AT (subr_die, DW_AT_declaration);
18453           remove_AT (subr_die, DW_AT_object_pointer);
18454           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18455         }
18456       else
18457         {
18458           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18459           add_AT_specification (subr_die, old_die);
18460           add_pubname (decl, subr_die);
18461           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18462             add_AT_file (subr_die, DW_AT_decl_file, file_index);
18463           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18464             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18465
18466           /* If the prototype had an 'auto' or 'decltype(auto)' return type,
18467              emit the real type on the definition die.  */
18468           if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
18469             {
18470               dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
18471               if (die == auto_die || die == decltype_auto_die)
18472                 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18473                                     TYPE_UNQUALIFIED, context_die);
18474             }
18475         }
18476     }
18477   else
18478     {
18479       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18480
18481       if (TREE_PUBLIC (decl))
18482         add_AT_flag (subr_die, DW_AT_external, 1);
18483
18484       add_name_and_src_coords_attributes (subr_die, decl);
18485       add_pubname (decl, subr_die);
18486       if (debug_info_level > DINFO_LEVEL_TERSE)
18487         {
18488           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18489           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18490                               TYPE_UNQUALIFIED, context_die);
18491         }
18492
18493       add_pure_or_virtual_attribute (subr_die, decl);
18494       if (DECL_ARTIFICIAL (decl))
18495         add_AT_flag (subr_die, DW_AT_artificial, 1);
18496
18497       if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
18498         add_AT_flag (subr_die, DW_AT_noreturn, 1);
18499
18500       add_accessibility_attribute (subr_die, decl);
18501     }
18502
18503   if (declaration)
18504     {
18505       if (!old_die || !get_AT (old_die, DW_AT_inline))
18506         {
18507           add_AT_flag (subr_die, DW_AT_declaration, 1);
18508
18509           /* If this is an explicit function declaration then generate
18510              a DW_AT_explicit attribute.  */
18511           if (lang_hooks.decls.function_decl_explicit_p (decl)
18512               && (dwarf_version >= 3 || !dwarf_strict))
18513             add_AT_flag (subr_die, DW_AT_explicit, 1);
18514
18515           /* If this is a C++11 deleted special function member then generate
18516              a DW_AT_GNU_deleted attribute.  */
18517           if (lang_hooks.decls.function_decl_deleted_p (decl)
18518               && (! dwarf_strict))
18519             add_AT_flag (subr_die, DW_AT_GNU_deleted, 1);
18520
18521           /* The first time we see a member function, it is in the context of
18522              the class to which it belongs.  We make sure of this by emitting
18523              the class first.  The next time is the definition, which is
18524              handled above.  The two may come from the same source text.
18525
18526              Note that force_decl_die() forces function declaration die. It is
18527              later reused to represent definition.  */
18528           equate_decl_number_to_die (decl, subr_die);
18529         }
18530     }
18531   else if (DECL_ABSTRACT_P (decl))
18532     {
18533       if (DECL_DECLARED_INLINE_P (decl))
18534         {
18535           if (cgraph_function_possibly_inlined_p (decl))
18536             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18537           else
18538             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18539         }
18540       else
18541         {
18542           if (cgraph_function_possibly_inlined_p (decl))
18543             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18544           else
18545             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18546         }
18547
18548       if (DECL_DECLARED_INLINE_P (decl)
18549           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18550         add_AT_flag (subr_die, DW_AT_artificial, 1);
18551
18552       equate_decl_number_to_die (decl, subr_die);
18553     }
18554   else if (!DECL_EXTERNAL (decl))
18555     {
18556       HOST_WIDE_INT cfa_fb_offset;
18557       struct function *fun = DECL_STRUCT_FUNCTION (decl);
18558
18559       if (!old_die || !get_AT (old_die, DW_AT_inline))
18560         equate_decl_number_to_die (decl, subr_die);
18561
18562       gcc_checking_assert (fun);
18563       if (!flag_reorder_blocks_and_partition)
18564         {
18565           dw_fde_ref fde = fun->fde;
18566           if (fde->dw_fde_begin)
18567             {
18568               /* We have already generated the labels.  */
18569              add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18570                                  fde->dw_fde_end, false);
18571             }
18572           else
18573             {
18574               /* Create start/end labels and add the range.  */
18575               char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
18576               char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
18577               ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
18578                                            current_function_funcdef_no);
18579               ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
18580                                            current_function_funcdef_no);
18581              add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
18582                                  false);
18583             }
18584
18585 #if VMS_DEBUGGING_INFO
18586       /* HP OpenVMS Industry Standard 64: DWARF Extensions
18587          Section 2.3 Prologue and Epilogue Attributes:
18588          When a breakpoint is set on entry to a function, it is generally
18589          desirable for execution to be suspended, not on the very first
18590          instruction of the function, but rather at a point after the
18591          function's frame has been set up, after any language defined local
18592          declaration processing has been completed, and before execution of
18593          the first statement of the function begins. Debuggers generally
18594          cannot properly determine where this point is.  Similarly for a
18595          breakpoint set on exit from a function. The prologue and epilogue
18596          attributes allow a compiler to communicate the location(s) to use.  */
18597
18598       {
18599         if (fde->dw_fde_vms_end_prologue)
18600           add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18601             fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18602
18603         if (fde->dw_fde_vms_begin_epilogue)
18604           add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18605             fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18606       }
18607 #endif
18608
18609         }
18610       else
18611         {
18612           /* Generate pubnames entries for the split function code ranges.  */
18613           dw_fde_ref fde = fun->fde;
18614
18615           if (fde->dw_fde_second_begin)
18616             {
18617               if (dwarf_version >= 3 || !dwarf_strict)
18618                 {
18619                   /* We should use ranges for non-contiguous code section 
18620                      addresses.  Use the actual code range for the initial
18621                      section, since the HOT/COLD labels might precede an 
18622                      alignment offset.  */
18623                   bool range_list_added = false;
18624                   add_ranges_by_labels (subr_die, fde->dw_fde_begin,
18625                                         fde->dw_fde_end, &range_list_added,
18626                                         false);
18627                   add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
18628                                         fde->dw_fde_second_end,
18629                                        &range_list_added, false);
18630                   if (range_list_added)
18631                     add_ranges (NULL);
18632                 }
18633               else
18634                 {
18635                   /* There is no real support in DW2 for this .. so we make
18636                      a work-around.  First, emit the pub name for the segment
18637                      containing the function label.  Then make and emit a
18638                      simplified subprogram DIE for the second segment with the
18639                      name pre-fixed by __hot/cold_sect_of_.  We use the same
18640                      linkage name for the second die so that gdb will find both
18641                      sections when given "b foo".  */
18642                   const char *name = NULL;
18643                   tree decl_name = DECL_NAME (decl);
18644                   dw_die_ref seg_die;
18645
18646                   /* Do the 'primary' section.   */
18647                   add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18648                                       fde->dw_fde_end, false);
18649
18650                   /* Build a minimal DIE for the secondary section.  */
18651                   seg_die = new_die (DW_TAG_subprogram,
18652                                      subr_die->die_parent, decl);
18653
18654                   if (TREE_PUBLIC (decl))
18655                     add_AT_flag (seg_die, DW_AT_external, 1);
18656
18657                   if (decl_name != NULL 
18658                       && IDENTIFIER_POINTER (decl_name) != NULL)
18659                     {
18660                       name = dwarf2_name (decl, 1);
18661                       if (! DECL_ARTIFICIAL (decl))
18662                         add_src_coords_attributes (seg_die, decl);
18663
18664                       add_linkage_name (seg_die, decl);
18665                     }
18666                   gcc_assert (name != NULL);
18667                   add_pure_or_virtual_attribute (seg_die, decl);
18668                   if (DECL_ARTIFICIAL (decl))
18669                     add_AT_flag (seg_die, DW_AT_artificial, 1);
18670
18671                   name = concat ("__second_sect_of_", name, NULL); 
18672                   add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
18673                                       fde->dw_fde_second_end, false);
18674                   add_name_attribute (seg_die, name);
18675                   if (want_pubnames ())
18676                     add_pubname_string (name, seg_die);
18677                 }
18678             }
18679           else
18680            add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
18681                                false);
18682         }
18683
18684       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
18685
18686       /* We define the "frame base" as the function's CFA.  This is more
18687          convenient for several reasons: (1) It's stable across the prologue
18688          and epilogue, which makes it better than just a frame pointer,
18689          (2) With dwarf3, there exists a one-byte encoding that allows us
18690          to reference the .debug_frame data by proxy, but failing that,
18691          (3) We can at least reuse the code inspection and interpretation
18692          code that determines the CFA position at various points in the
18693          function.  */
18694       if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
18695         {
18696           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
18697           add_AT_loc (subr_die, DW_AT_frame_base, op);
18698         }
18699       else
18700         {
18701           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
18702           if (list->dw_loc_next)
18703             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
18704           else
18705             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
18706         }
18707
18708       /* Compute a displacement from the "steady-state frame pointer" to
18709          the CFA.  The former is what all stack slots and argument slots
18710          will reference in the rtl; the latter is what we've told the
18711          debugger about.  We'll need to adjust all frame_base references
18712          by this displacement.  */
18713       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
18714
18715       if (fun->static_chain_decl)
18716         add_AT_location_description (subr_die, DW_AT_static_link,
18717                  loc_list_from_tree (fun->static_chain_decl, 2, NULL));
18718     }
18719
18720   /* Generate child dies for template paramaters.  */
18721   if (debug_info_level > DINFO_LEVEL_TERSE)
18722     gen_generic_params_dies (decl);
18723
18724   /* Now output descriptions of the arguments for this function. This gets
18725      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
18726      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
18727      `...' at the end of the formal parameter list.  In order to find out if
18728      there was a trailing ellipsis or not, we must instead look at the type
18729      associated with the FUNCTION_DECL.  This will be a node of type
18730      FUNCTION_TYPE. If the chain of type nodes hanging off of this
18731      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
18732      an ellipsis at the end.  */
18733
18734   /* In the case where we are describing a mere function declaration, all we
18735      need to do here (and all we *can* do here) is to describe the *types* of
18736      its formal parameters.  */
18737   if (debug_info_level <= DINFO_LEVEL_TERSE)
18738     ;
18739   else if (declaration)
18740     gen_formal_types_die (decl, subr_die);
18741   else
18742     {
18743       /* Generate DIEs to represent all known formal parameters.  */
18744       tree parm = DECL_ARGUMENTS (decl);
18745       tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
18746       tree generic_decl_parm = generic_decl
18747                                 ? DECL_ARGUMENTS (generic_decl)
18748                                 : NULL;
18749
18750       /* Now we want to walk the list of parameters of the function and
18751          emit their relevant DIEs.
18752
18753          We consider the case of DECL being an instance of a generic function
18754          as well as it being a normal function.
18755
18756          If DECL is an instance of a generic function we walk the
18757          parameters of the generic function declaration _and_ the parameters of
18758          DECL itself. This is useful because we want to emit specific DIEs for
18759          function parameter packs and those are declared as part of the
18760          generic function declaration. In that particular case,
18761          the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
18762          That DIE has children DIEs representing the set of arguments
18763          of the pack. Note that the set of pack arguments can be empty.
18764          In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
18765          children DIE.
18766
18767          Otherwise, we just consider the parameters of DECL.  */
18768       while (generic_decl_parm || parm)
18769         {
18770           if (generic_decl_parm
18771               && lang_hooks.function_parameter_pack_p (generic_decl_parm))
18772             gen_formal_parameter_pack_die (generic_decl_parm,
18773                                            parm, subr_die,
18774                                            &parm);
18775           else if (parm && !POINTER_BOUNDS_P (parm))
18776             {
18777               dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
18778
18779               if (parm == DECL_ARGUMENTS (decl)
18780                   && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
18781                   && parm_die
18782                   && (dwarf_version >= 3 || !dwarf_strict))
18783                 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
18784
18785               parm = DECL_CHAIN (parm);
18786             }
18787           else if (parm)
18788             parm = DECL_CHAIN (parm);
18789
18790           if (generic_decl_parm)
18791             generic_decl_parm = DECL_CHAIN (generic_decl_parm);
18792         }
18793
18794       /* Decide whether we need an unspecified_parameters DIE at the end.
18795          There are 2 more cases to do this for: 1) the ansi ... declaration -
18796          this is detectable when the end of the arg list is not a
18797          void_type_node 2) an unprototyped function declaration (not a
18798          definition).  This just means that we have no info about the
18799          parameters at all.  */
18800       if (prototype_p (TREE_TYPE (decl)))
18801         {
18802           /* This is the prototyped case, check for....  */
18803           if (stdarg_p (TREE_TYPE (decl)))
18804             gen_unspecified_parameters_die (decl, subr_die);
18805         }
18806       else if (DECL_INITIAL (decl) == NULL_TREE)
18807         gen_unspecified_parameters_die (decl, subr_die);
18808     }
18809
18810   /* Output Dwarf info for all of the stuff within the body of the function
18811      (if it has one - it may be just a declaration).  */
18812   outer_scope = DECL_INITIAL (decl);
18813
18814   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
18815      a function.  This BLOCK actually represents the outermost binding contour
18816      for the function, i.e. the contour in which the function's formal
18817      parameters and labels get declared. Curiously, it appears that the front
18818      end doesn't actually put the PARM_DECL nodes for the current function onto
18819      the BLOCK_VARS list for this outer scope, but are strung off of the
18820      DECL_ARGUMENTS list for the function instead.
18821
18822      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
18823      the LABEL_DECL nodes for the function however, and we output DWARF info
18824      for those in decls_for_scope.  Just within the `outer_scope' there will be
18825      a BLOCK node representing the function's outermost pair of curly braces,
18826      and any blocks used for the base and member initializers of a C++
18827      constructor function.  */
18828   if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
18829     {
18830       int call_site_note_count = 0;
18831       int tail_call_site_note_count = 0;
18832
18833       /* Emit a DW_TAG_variable DIE for a named return value.  */
18834       if (DECL_NAME (DECL_RESULT (decl)))
18835         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18836
18837       decls_for_scope (outer_scope, subr_die);
18838
18839       if (call_arg_locations && !dwarf_strict)
18840         {
18841           struct call_arg_loc_node *ca_loc;
18842           for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
18843             {
18844               dw_die_ref die = NULL;
18845               rtx tloc = NULL_RTX, tlocc = NULL_RTX;
18846               rtx arg, next_arg;
18847
18848               for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
18849                    arg; arg = next_arg)
18850                 {
18851                   dw_loc_descr_ref reg, val;
18852                   machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
18853                   dw_die_ref cdie, tdie = NULL;
18854
18855                   next_arg = XEXP (arg, 1);
18856                   if (REG_P (XEXP (XEXP (arg, 0), 0))
18857                       && next_arg
18858                       && MEM_P (XEXP (XEXP (next_arg, 0), 0))
18859                       && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
18860                       && REGNO (XEXP (XEXP (arg, 0), 0))
18861                          == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
18862                     next_arg = XEXP (next_arg, 1);
18863                   if (mode == VOIDmode)
18864                     {
18865                       mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
18866                       if (mode == VOIDmode)
18867                         mode = GET_MODE (XEXP (arg, 0));
18868                     }
18869                   if (mode == VOIDmode || mode == BLKmode)
18870                     continue;
18871                   if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
18872                     {
18873                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18874                       tloc = XEXP (XEXP (arg, 0), 1);
18875                       continue;
18876                     }
18877                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
18878                            && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
18879                     {
18880                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18881                       tlocc = XEXP (XEXP (arg, 0), 1);
18882                       continue;
18883                     }
18884                   reg = NULL;
18885                   if (REG_P (XEXP (XEXP (arg, 0), 0)))
18886                     reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
18887                                               VAR_INIT_STATUS_INITIALIZED);
18888                   else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
18889                     {
18890                       rtx mem = XEXP (XEXP (arg, 0), 0);
18891                       reg = mem_loc_descriptor (XEXP (mem, 0),
18892                                                 get_address_mode (mem),
18893                                                 GET_MODE (mem),
18894                                                 VAR_INIT_STATUS_INITIALIZED);
18895                     }
18896                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
18897                            == DEBUG_PARAMETER_REF)
18898                     {
18899                       tree tdecl
18900                         = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
18901                       tdie = lookup_decl_die (tdecl);
18902                       if (tdie == NULL)
18903                         continue;
18904                     }
18905                   else
18906                     continue;
18907                   if (reg == NULL
18908                       && GET_CODE (XEXP (XEXP (arg, 0), 0))
18909                          != DEBUG_PARAMETER_REF)
18910                     continue;
18911                   val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
18912                                             VOIDmode,
18913                                             VAR_INIT_STATUS_INITIALIZED);
18914                   if (val == NULL)
18915                     continue;
18916                   if (die == NULL)
18917                     die = gen_call_site_die (decl, subr_die, ca_loc);
18918                   cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
18919                                   NULL_TREE);
18920                   if (reg != NULL)
18921                     add_AT_loc (cdie, DW_AT_location, reg);
18922                   else if (tdie != NULL)
18923                     add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
18924                   add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
18925                   if (next_arg != XEXP (arg, 1))
18926                     {
18927                       mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
18928                       if (mode == VOIDmode)
18929                         mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
18930                       val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
18931                                                             0), 1),
18932                                                 mode, VOIDmode,
18933                                                 VAR_INIT_STATUS_INITIALIZED);
18934                       if (val != NULL)
18935                         add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
18936                     }
18937                 }
18938               if (die == NULL
18939                   && (ca_loc->symbol_ref || tloc))
18940                 die = gen_call_site_die (decl, subr_die, ca_loc);
18941               if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
18942                 {
18943                   dw_loc_descr_ref tval = NULL;
18944
18945                   if (tloc != NULL_RTX)
18946                     tval = mem_loc_descriptor (tloc,
18947                                                GET_MODE (tloc) == VOIDmode
18948                                                ? Pmode : GET_MODE (tloc),
18949                                                VOIDmode,
18950                                                VAR_INIT_STATUS_INITIALIZED);
18951                   if (tval)
18952                     add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
18953                   else if (tlocc != NULL_RTX)
18954                     {
18955                       tval = mem_loc_descriptor (tlocc,
18956                                                  GET_MODE (tlocc) == VOIDmode
18957                                                  ? Pmode : GET_MODE (tlocc),
18958                                                  VOIDmode,
18959                                                  VAR_INIT_STATUS_INITIALIZED);
18960                       if (tval)
18961                         add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
18962                                     tval);
18963                     }
18964                 }
18965               if (die != NULL)
18966                 {
18967                   call_site_note_count++;
18968                   if (ca_loc->tail_call_p)
18969                     tail_call_site_note_count++;
18970                 }
18971             }
18972         }
18973       call_arg_locations = NULL;
18974       call_arg_loc_last = NULL;
18975       if (tail_call_site_count >= 0
18976           && tail_call_site_count == tail_call_site_note_count
18977           && !dwarf_strict)
18978         {
18979           if (call_site_count >= 0
18980               && call_site_count == call_site_note_count)
18981             add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
18982           else
18983             add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
18984         }
18985       call_site_count = -1;
18986       tail_call_site_count = -1;
18987     }
18988
18989   if (subr_die != old_die)
18990     /* Add the calling convention attribute if requested.  */
18991     add_calling_convention_attribute (subr_die, decl);
18992 }
18993
18994 /* Returns a hash value for X (which really is a die_struct).  */
18995
18996 hashval_t
18997 block_die_hasher::hash (die_struct *d)
18998 {
18999   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
19000 }
19001
19002 /* Return nonzero if decl_id and die_parent of die_struct X is the same
19003    as decl_id and die_parent of die_struct Y.  */
19004
19005 bool
19006 block_die_hasher::equal (die_struct *x, die_struct *y)
19007 {
19008   return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
19009 }
19010
19011 /* Generate a DIE to represent a declared data object.
19012    Either DECL or ORIGIN must be non-null.  */
19013
19014 static void
19015 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
19016 {
19017   HOST_WIDE_INT off = 0;
19018   tree com_decl;
19019   tree decl_or_origin = decl ? decl : origin;
19020   tree ultimate_origin;
19021   dw_die_ref var_die;
19022   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
19023   dw_die_ref origin_die;
19024   bool declaration = (DECL_EXTERNAL (decl_or_origin)
19025                       || class_or_namespace_scope_p (context_die));
19026   bool specialization_p = false;
19027
19028   ultimate_origin = decl_ultimate_origin (decl_or_origin);
19029   if (decl || ultimate_origin)
19030     origin = ultimate_origin;
19031   com_decl = fortran_common (decl_or_origin, &off);
19032
19033   /* Symbol in common gets emitted as a child of the common block, in the form
19034      of a data member.  */
19035   if (com_decl)
19036     {
19037       dw_die_ref com_die;
19038       dw_loc_list_ref loc;
19039       die_node com_die_arg;
19040
19041       var_die = lookup_decl_die (decl_or_origin);
19042       if (var_die)
19043         {
19044           if (get_AT (var_die, DW_AT_location) == NULL)
19045             {
19046               loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
19047               if (loc)
19048                 {
19049                   if (off)
19050                     {
19051                       /* Optimize the common case.  */
19052                       if (single_element_loc_list_p (loc)
19053                           && loc->expr->dw_loc_opc == DW_OP_addr
19054                           && loc->expr->dw_loc_next == NULL
19055                           && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
19056                              == SYMBOL_REF)
19057                         {
19058                           rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
19059                           loc->expr->dw_loc_oprnd1.v.val_addr
19060                             = plus_constant (GET_MODE (x), x , off);
19061                         }
19062                       else
19063                         loc_list_plus_const (loc, off);
19064                     }
19065                   add_AT_location_description (var_die, DW_AT_location, loc);
19066                   remove_AT (var_die, DW_AT_declaration);
19067                 }
19068             }
19069           return;
19070         }
19071
19072       if (common_block_die_table == NULL)
19073         common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
19074
19075       com_die_arg.decl_id = DECL_UID (com_decl);
19076       com_die_arg.die_parent = context_die;
19077       com_die = common_block_die_table->find (&com_die_arg);
19078       loc = loc_list_from_tree (com_decl, 2, NULL);
19079       if (com_die == NULL)
19080         {
19081           const char *cnam
19082             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
19083           die_node **slot;
19084
19085           com_die = new_die (DW_TAG_common_block, context_die, decl);
19086           add_name_and_src_coords_attributes (com_die, com_decl);
19087           if (loc)
19088             {
19089               add_AT_location_description (com_die, DW_AT_location, loc);
19090               /* Avoid sharing the same loc descriptor between
19091                  DW_TAG_common_block and DW_TAG_variable.  */
19092               loc = loc_list_from_tree (com_decl, 2, NULL);
19093             }
19094           else if (DECL_EXTERNAL (decl))
19095             add_AT_flag (com_die, DW_AT_declaration, 1);
19096           if (want_pubnames ())
19097             add_pubname_string (cnam, com_die); /* ??? needed? */
19098           com_die->decl_id = DECL_UID (com_decl);
19099           slot = common_block_die_table->find_slot (com_die, INSERT);
19100           *slot = com_die;
19101         }
19102       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
19103         {
19104           add_AT_location_description (com_die, DW_AT_location, loc);
19105           loc = loc_list_from_tree (com_decl, 2, NULL);
19106           remove_AT (com_die, DW_AT_declaration);
19107         }
19108       var_die = new_die (DW_TAG_variable, com_die, decl);
19109       add_name_and_src_coords_attributes (var_die, decl);
19110       add_type_attribute (var_die, TREE_TYPE (decl), decl_quals (decl),
19111                           context_die);
19112       add_AT_flag (var_die, DW_AT_external, 1);
19113       if (loc)
19114         {
19115           if (off)
19116             {
19117               /* Optimize the common case.  */
19118               if (single_element_loc_list_p (loc)
19119                   && loc->expr->dw_loc_opc == DW_OP_addr
19120                   && loc->expr->dw_loc_next == NULL
19121                   && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
19122                 {
19123                   rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
19124                   loc->expr->dw_loc_oprnd1.v.val_addr
19125                     = plus_constant (GET_MODE (x), x, off);
19126                 }
19127               else
19128                 loc_list_plus_const (loc, off);
19129             }
19130           add_AT_location_description (var_die, DW_AT_location, loc);
19131         }
19132       else if (DECL_EXTERNAL (decl))
19133         add_AT_flag (var_die, DW_AT_declaration, 1);
19134       equate_decl_number_to_die (decl, var_die);
19135       return;
19136     }
19137
19138   /* If the compiler emitted a definition for the DECL declaration
19139      and if we already emitted a DIE for it, don't emit a second
19140      DIE for it again. Allow re-declarations of DECLs that are
19141      inside functions, though.  */
19142   if (old_die && declaration && !local_scope_p (context_die))
19143     return;
19144
19145   /* For static data members, the declaration in the class is supposed
19146      to have DW_TAG_member tag; the specification should still be
19147      DW_TAG_variable referencing the DW_TAG_member DIE.  */
19148   if (declaration && class_scope_p (context_die))
19149     var_die = new_die (DW_TAG_member, context_die, decl);
19150   else
19151     var_die = new_die (DW_TAG_variable, context_die, decl);
19152
19153   origin_die = NULL;
19154   if (origin != NULL)
19155     origin_die = add_abstract_origin_attribute (var_die, origin);
19156
19157   /* Loop unrolling can create multiple blocks that refer to the same
19158      static variable, so we must test for the DW_AT_declaration flag.
19159
19160      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
19161      copy decls and set the DECL_ABSTRACT_P flag on them instead of
19162      sharing them.
19163
19164      ??? Duplicated blocks have been rewritten to use .debug_ranges.
19165
19166      ??? The declare_in_namespace support causes us to get two DIEs for one
19167      variable, both of which are declarations.  We want to avoid considering
19168      one to be a specification, so we must test that this DIE is not a
19169      declaration.  */
19170   else if (old_die && TREE_STATIC (decl) && ! declaration
19171            && get_AT_flag (old_die, DW_AT_declaration) == 1)
19172     {
19173       /* This is a definition of a C++ class level static.  */
19174       add_AT_specification (var_die, old_die);
19175       specialization_p = true;
19176       if (DECL_NAME (decl))
19177         {
19178           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19179           struct dwarf_file_data * file_index = lookup_filename (s.file);
19180
19181           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19182             add_AT_file (var_die, DW_AT_decl_file, file_index);
19183
19184           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19185             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19186
19187           if (old_die->die_tag == DW_TAG_member)
19188             add_linkage_name (var_die, decl);
19189         }
19190     }
19191   else
19192     add_name_and_src_coords_attributes (var_die, decl);
19193
19194   if ((origin == NULL && !specialization_p)
19195       || (origin != NULL
19196           && !DECL_ABSTRACT_P (decl_or_origin)
19197           && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19198                                        decl_function_context
19199                                                         (decl_or_origin))))
19200     {
19201       tree type = TREE_TYPE (decl_or_origin);
19202
19203       if (decl_by_reference_p (decl_or_origin))
19204         add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19205                             context_die);
19206       else
19207         add_type_attribute (var_die, type, decl_quals (decl_or_origin),
19208                             context_die);
19209     }
19210
19211   if (origin == NULL && !specialization_p)
19212     {
19213       if (TREE_PUBLIC (decl))
19214         add_AT_flag (var_die, DW_AT_external, 1);
19215
19216       if (DECL_ARTIFICIAL (decl))
19217         add_AT_flag (var_die, DW_AT_artificial, 1);
19218
19219       add_accessibility_attribute (var_die, decl);
19220     }
19221
19222   if (declaration)
19223     add_AT_flag (var_die, DW_AT_declaration, 1);
19224
19225   if (decl && (DECL_ABSTRACT_P (decl) || declaration || old_die == NULL))
19226     equate_decl_number_to_die (decl, var_die);
19227
19228   if (! declaration
19229       && (! DECL_ABSTRACT_P (decl_or_origin)
19230           /* Local static vars are shared between all clones/inlines,
19231              so emit DW_AT_location on the abstract DIE if DECL_RTL is
19232              already set.  */
19233           || (TREE_CODE (decl_or_origin) == VAR_DECL
19234               && TREE_STATIC (decl_or_origin)
19235               && DECL_RTL_SET_P (decl_or_origin)))
19236       /* When abstract origin already has DW_AT_location attribute, no need
19237          to add it again.  */
19238       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19239     {
19240       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
19241           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
19242         defer_location (decl_or_origin, var_die);
19243       else
19244         add_location_or_const_value_attribute (var_die, decl_or_origin,
19245                                                decl == NULL, DW_AT_location);
19246       add_pubname (decl_or_origin, var_die);
19247     }
19248   else
19249     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19250 }
19251
19252 /* Generate a DIE to represent a named constant.  */
19253
19254 static void
19255 gen_const_die (tree decl, dw_die_ref context_die)
19256 {
19257   dw_die_ref const_die;
19258   tree type = TREE_TYPE (decl);
19259
19260   const_die = new_die (DW_TAG_constant, context_die, decl);
19261   add_name_and_src_coords_attributes (const_die, decl);
19262   add_type_attribute (const_die, type, TYPE_QUAL_CONST, context_die);
19263   if (TREE_PUBLIC (decl))
19264     add_AT_flag (const_die, DW_AT_external, 1);
19265   if (DECL_ARTIFICIAL (decl))
19266     add_AT_flag (const_die, DW_AT_artificial, 1);
19267   tree_add_const_value_attribute_for_decl (const_die, decl);
19268 }
19269
19270 /* Generate a DIE to represent a label identifier.  */
19271
19272 static void
19273 gen_label_die (tree decl, dw_die_ref context_die)
19274 {
19275   tree origin = decl_ultimate_origin (decl);
19276   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
19277   rtx insn;
19278   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19279
19280   if (origin != NULL)
19281     add_abstract_origin_attribute (lbl_die, origin);
19282   else
19283     add_name_and_src_coords_attributes (lbl_die, decl);
19284
19285   if (DECL_ABSTRACT_P (decl))
19286     equate_decl_number_to_die (decl, lbl_die);
19287   else
19288     {
19289       insn = DECL_RTL_IF_SET (decl);
19290
19291       /* Deleted labels are programmer specified labels which have been
19292          eliminated because of various optimizations.  We still emit them
19293          here so that it is possible to put breakpoints on them.  */
19294       if (insn
19295           && (LABEL_P (insn)
19296               || ((NOTE_P (insn)
19297                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19298         {
19299           /* When optimization is enabled (via -O) some parts of the compiler
19300              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19301              represent source-level labels which were explicitly declared by
19302              the user.  This really shouldn't be happening though, so catch
19303              it if it ever does happen.  */
19304           gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
19305
19306           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19307           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19308         }
19309       else if (insn
19310                && NOTE_P (insn)
19311                && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
19312                && CODE_LABEL_NUMBER (insn) != -1)
19313         {
19314           ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
19315           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19316         }
19317     }
19318 }
19319
19320 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
19321    attributes to the DIE for a block STMT, to describe where the inlined
19322    function was called from.  This is similar to add_src_coords_attributes.  */
19323
19324 static inline void
19325 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19326 {
19327   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19328
19329   if (dwarf_version >= 3 || !dwarf_strict)
19330     {
19331       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19332       add_AT_unsigned (die, DW_AT_call_line, s.line);
19333     }
19334 }
19335
19336
19337 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19338    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
19339
19340 static inline void
19341 add_high_low_attributes (tree stmt, dw_die_ref die)
19342 {
19343   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19344
19345   if (BLOCK_FRAGMENT_CHAIN (stmt)
19346       && (dwarf_version >= 3 || !dwarf_strict))
19347     {
19348       tree chain, superblock = NULL_TREE;
19349       dw_die_ref pdie;
19350       dw_attr_ref attr = NULL;
19351
19352       if (inlined_function_outer_scope_p (stmt))
19353         {
19354           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19355                                        BLOCK_NUMBER (stmt));
19356           add_AT_lbl_id (die, DW_AT_entry_pc, label);
19357         }
19358
19359       /* Optimize duplicate .debug_ranges lists or even tails of
19360          lists.  If this BLOCK has same ranges as its supercontext,
19361          lookup DW_AT_ranges attribute in the supercontext (and
19362          recursively so), verify that the ranges_table contains the
19363          right values and use it instead of adding a new .debug_range.  */
19364       for (chain = stmt, pdie = die;
19365            BLOCK_SAME_RANGE (chain);
19366            chain = BLOCK_SUPERCONTEXT (chain))
19367         {
19368           dw_attr_ref new_attr;
19369
19370           pdie = pdie->die_parent;
19371           if (pdie == NULL)
19372             break;
19373           if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
19374             break;
19375           new_attr = get_AT (pdie, DW_AT_ranges);
19376           if (new_attr == NULL
19377               || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
19378             break;
19379           attr = new_attr;
19380           superblock = BLOCK_SUPERCONTEXT (chain);
19381         }
19382       if (attr != NULL
19383           && (ranges_table[attr->dw_attr_val.v.val_offset
19384                            / 2 / DWARF2_ADDR_SIZE].num
19385               == BLOCK_NUMBER (superblock))
19386           && BLOCK_FRAGMENT_CHAIN (superblock))
19387         {
19388           unsigned long off = attr->dw_attr_val.v.val_offset
19389                               / 2 / DWARF2_ADDR_SIZE;
19390           unsigned long supercnt = 0, thiscnt = 0;
19391           for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
19392                chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19393             {
19394               ++supercnt;
19395               gcc_checking_assert (ranges_table[off + supercnt].num
19396                                    == BLOCK_NUMBER (chain));
19397             }
19398           gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
19399           for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
19400                chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19401             ++thiscnt;
19402           gcc_assert (supercnt >= thiscnt);
19403           add_AT_range_list (die, DW_AT_ranges,
19404                              ((off + supercnt - thiscnt)
19405                               * 2 * DWARF2_ADDR_SIZE),
19406                              false);
19407           return;
19408         }
19409
19410       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
19411
19412       chain = BLOCK_FRAGMENT_CHAIN (stmt);
19413       do
19414         {
19415           add_ranges (chain);
19416           chain = BLOCK_FRAGMENT_CHAIN (chain);
19417         }
19418       while (chain);
19419       add_ranges (NULL);
19420     }
19421   else
19422     {
19423       char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
19424       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19425                                    BLOCK_NUMBER (stmt));
19426       ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
19427                                    BLOCK_NUMBER (stmt));
19428       add_AT_low_high_pc (die, label, label_high, false);
19429     }
19430 }
19431
19432 /* Generate a DIE for a lexical block.  */
19433
19434 static void
19435 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
19436 {
19437   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19438
19439   if (call_arg_locations)
19440     {
19441       if (block_map.length () <= BLOCK_NUMBER (stmt))
19442         block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19443       block_map[BLOCK_NUMBER (stmt)] = stmt_die;
19444     }
19445
19446   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19447     add_high_low_attributes (stmt, stmt_die);
19448
19449   decls_for_scope (stmt, stmt_die);
19450 }
19451
19452 /* Generate a DIE for an inlined subprogram.  */
19453
19454 static void
19455 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
19456 {
19457   tree decl;
19458
19459   /* The instance of function that is effectively being inlined shall not
19460      be abstract.  */
19461   gcc_assert (! BLOCK_ABSTRACT (stmt));
19462
19463   decl = block_ultimate_origin (stmt);
19464
19465   /* Emit info for the abstract instance first, if we haven't yet.  We
19466      must emit this even if the block is abstract, otherwise when we
19467      emit the block below (or elsewhere), we may end up trying to emit
19468      a die whose origin die hasn't been emitted, and crashing.  */
19469   dwarf2out_abstract_function (decl);
19470
19471   if (! BLOCK_ABSTRACT (stmt))
19472     {
19473       dw_die_ref subr_die
19474         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19475
19476       if (call_arg_locations)
19477         {
19478           if (block_map.length () <= BLOCK_NUMBER (stmt))
19479             block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19480           block_map[BLOCK_NUMBER (stmt)] = subr_die;
19481         }
19482       add_abstract_origin_attribute (subr_die, decl);
19483       if (TREE_ASM_WRITTEN (stmt))
19484         add_high_low_attributes (stmt, subr_die);
19485       add_call_src_coords_attributes (stmt, subr_die);
19486
19487       decls_for_scope (stmt, subr_die);
19488     }
19489 }
19490
19491 /* Generate a DIE for a field in a record, or structure.  */
19492
19493 static void
19494 gen_field_die (tree decl, dw_die_ref context_die)
19495 {
19496   dw_die_ref decl_die;
19497
19498   if (TREE_TYPE (decl) == error_mark_node)
19499     return;
19500
19501   decl_die = new_die (DW_TAG_member, context_die, decl);
19502   add_name_and_src_coords_attributes (decl_die, decl);
19503   add_type_attribute (decl_die, member_declared_type (decl),
19504                       decl_quals (decl), context_die);
19505
19506   if (DECL_BIT_FIELD_TYPE (decl))
19507     {
19508       add_byte_size_attribute (decl_die, decl);
19509       add_bit_size_attribute (decl_die, decl);
19510       add_bit_offset_attribute (decl_die, decl);
19511     }
19512
19513   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19514     add_data_member_location_attribute (decl_die, decl);
19515
19516   if (DECL_ARTIFICIAL (decl))
19517     add_AT_flag (decl_die, DW_AT_artificial, 1);
19518
19519   add_accessibility_attribute (decl_die, decl);
19520
19521   /* Equate decl number to die, so that we can look up this decl later on.  */
19522   equate_decl_number_to_die (decl, decl_die);
19523 }
19524
19525 #if 0
19526 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19527    Use modified_type_die instead.
19528    We keep this code here just in case these types of DIEs may be needed to
19529    represent certain things in other languages (e.g. Pascal) someday.  */
19530
19531 static void
19532 gen_pointer_type_die (tree type, dw_die_ref context_die)
19533 {
19534   dw_die_ref ptr_die
19535     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19536
19537   equate_type_number_to_die (type, ptr_die);
19538   add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19539                       context_die);
19540   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19541 }
19542
19543 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19544    Use modified_type_die instead.
19545    We keep this code here just in case these types of DIEs may be needed to
19546    represent certain things in other languages (e.g. Pascal) someday.  */
19547
19548 static void
19549 gen_reference_type_die (tree type, dw_die_ref context_die)
19550 {
19551   dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19552
19553   if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19554     ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19555   else
19556     ref_die = new_die (DW_TAG_reference_type, scope_die, type);
19557
19558   equate_type_number_to_die (type, ref_die);
19559   add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19560                       context_die);
19561   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19562 }
19563 #endif
19564
19565 /* Generate a DIE for a pointer to a member type.  */
19566
19567 static void
19568 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
19569 {
19570   dw_die_ref ptr_die
19571     = new_die (DW_TAG_ptr_to_member_type,
19572                scope_die_for (type, context_die), type);
19573
19574   equate_type_number_to_die (type, ptr_die);
19575   add_AT_die_ref (ptr_die, DW_AT_containing_type,
19576                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
19577   add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19578                       context_die);
19579 }
19580
19581 typedef const char *dchar_p; /* For DEF_VEC_P.  */
19582
19583 static char *producer_string;
19584
19585 /* Return a heap allocated producer string including command line options
19586    if -grecord-gcc-switches.  */
19587
19588 static char *
19589 gen_producer_string (void)
19590 {
19591   size_t j;
19592   auto_vec<dchar_p> switches;
19593   const char *language_string = lang_hooks.name;
19594   char *producer, *tail;
19595   const char *p;
19596   size_t len = dwarf_record_gcc_switches ? 0 : 3;
19597   size_t plen = strlen (language_string) + 1 + strlen (version_string);
19598
19599   for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
19600     switch (save_decoded_options[j].opt_index)
19601       {
19602       case OPT_o:
19603       case OPT_d:
19604       case OPT_dumpbase:
19605       case OPT_dumpdir:
19606       case OPT_auxbase:
19607       case OPT_auxbase_strip:
19608       case OPT_quiet:
19609       case OPT_version:
19610       case OPT_v:
19611       case OPT_w:
19612       case OPT_L:
19613       case OPT_D:
19614       case OPT_I:
19615       case OPT_U:
19616       case OPT_SPECIAL_unknown:
19617       case OPT_SPECIAL_ignore:
19618       case OPT_SPECIAL_program_name:
19619       case OPT_SPECIAL_input_file:
19620       case OPT_grecord_gcc_switches:
19621       case OPT_gno_record_gcc_switches:
19622       case OPT__output_pch_:
19623       case OPT_fdiagnostics_show_location_:
19624       case OPT_fdiagnostics_show_option:
19625       case OPT_fdiagnostics_show_caret:
19626       case OPT_fdiagnostics_color_:
19627       case OPT_fverbose_asm:
19628       case OPT____:
19629       case OPT__sysroot_:
19630       case OPT_nostdinc:
19631       case OPT_nostdinc__:
19632       case OPT_fpreprocessed:
19633         /* Ignore these.  */
19634         continue;
19635       default:
19636         if (cl_options[save_decoded_options[j].opt_index].flags
19637             & CL_NO_DWARF_RECORD)
19638           continue;
19639         gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
19640                              == '-');
19641         switch (save_decoded_options[j].canonical_option[0][1])
19642           {
19643           case 'M':
19644           case 'i':
19645           case 'W':
19646             continue;
19647           case 'f':
19648             if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
19649                          "dump", 4) == 0)
19650               continue;
19651             break;
19652           default:
19653             break;
19654           }
19655         switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
19656         len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
19657         break;
19658       }
19659
19660   producer = XNEWVEC (char, plen + 1 + len + 1);
19661   tail = producer;
19662   sprintf (tail, "%s %s", language_string, version_string);
19663   tail += plen;
19664
19665   FOR_EACH_VEC_ELT (switches, j, p)
19666     {
19667       len = strlen (p);
19668       *tail = ' ';
19669       memcpy (tail + 1, p, len);
19670       tail += len + 1;
19671     }
19672
19673   *tail = '\0';
19674   return producer;
19675 }
19676
19677 /* Given a C and/or C++ language/version string return the "highest".
19678    C++ is assumed to be "higher" than C in this case.  Used for merging
19679    LTO translation unit languages.  */
19680 static const char *
19681 highest_c_language (const char *lang1, const char *lang2)
19682 {
19683   if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
19684     return "GNU C++14";
19685   if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
19686     return "GNU C++11";
19687   if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
19688     return "GNU C++98";
19689
19690   if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
19691     return "GNU C11";
19692   if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
19693     return "GNU C99";
19694   if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
19695     return "GNU C89";
19696
19697   gcc_unreachable ();
19698 }
19699
19700
19701 /* Generate the DIE for the compilation unit.  */
19702
19703 static dw_die_ref
19704 gen_compile_unit_die (const char *filename)
19705 {
19706   dw_die_ref die;
19707   const char *language_string = lang_hooks.name;
19708   int language;
19709
19710   die = new_die (DW_TAG_compile_unit, NULL, NULL);
19711
19712   if (filename)
19713     {
19714       add_name_attribute (die, filename);
19715       /* Don't add cwd for <built-in>.  */
19716       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19717         add_comp_dir_attribute (die);
19718     }
19719
19720   add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
19721
19722   /* If our producer is LTO try to figure out a common language to use
19723      from the global list of translation units.  */
19724   if (strcmp (language_string, "GNU GIMPLE") == 0)
19725     {
19726       unsigned i;
19727       tree t;
19728       const char *common_lang = NULL;
19729
19730       FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
19731         {
19732           if (!TRANSLATION_UNIT_LANGUAGE (t))
19733             continue;
19734           if (!common_lang)
19735             common_lang = TRANSLATION_UNIT_LANGUAGE (t);
19736           else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
19737             ;
19738           else if (strncmp (common_lang, "GNU C", 5) == 0
19739                     && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
19740             /* Mixing C and C++ is ok, use C++ in that case.  */
19741             common_lang = highest_c_language (common_lang,
19742                                               TRANSLATION_UNIT_LANGUAGE (t));
19743           else
19744             {
19745               /* Fall back to C.  */
19746               common_lang = NULL;
19747               break;
19748             }
19749         }
19750
19751       if (common_lang)
19752         language_string = common_lang;
19753     }
19754
19755   language = DW_LANG_C;
19756   if (strncmp (language_string, "GNU C", 5) == 0
19757       && ISDIGIT (language_string[5]))
19758     {
19759       language = DW_LANG_C89;
19760       if (dwarf_version >= 3 || !dwarf_strict)
19761         {
19762           if (strcmp (language_string, "GNU C89") != 0)
19763             language = DW_LANG_C99;
19764
19765           if (dwarf_version >= 5 /* || !dwarf_strict */)
19766             if (strcmp (language_string, "GNU C11") == 0)
19767               language = DW_LANG_C11;
19768         }
19769     }
19770   else if (strncmp (language_string, "GNU C++", 7) == 0)
19771     {
19772       language = DW_LANG_C_plus_plus;
19773       if (dwarf_version >= 5 /* || !dwarf_strict */)
19774         {
19775           if (strcmp (language_string, "GNU C++11") == 0)
19776             language = DW_LANG_C_plus_plus_11;
19777           else if (strcmp (language_string, "GNU C++14") == 0)
19778             language = DW_LANG_C_plus_plus_14;
19779         }
19780     }
19781   else if (strcmp (language_string, "GNU F77") == 0)
19782     language = DW_LANG_Fortran77;
19783   else if (strcmp (language_string, "GNU Pascal") == 0)
19784     language = DW_LANG_Pascal83;
19785   else if (dwarf_version >= 3 || !dwarf_strict)
19786     {
19787       if (strcmp (language_string, "GNU Ada") == 0)
19788         language = DW_LANG_Ada95;
19789       else if (strcmp (language_string, "GNU Fortran") == 0)
19790         language = DW_LANG_Fortran95;
19791       else if (strcmp (language_string, "GNU Java") == 0)
19792         language = DW_LANG_Java;
19793       else if (strcmp (language_string, "GNU Objective-C") == 0)
19794         language = DW_LANG_ObjC;
19795       else if (strcmp (language_string, "GNU Objective-C++") == 0)
19796         language = DW_LANG_ObjC_plus_plus;
19797       else if (dwarf_version >= 5 || !dwarf_strict)
19798         {
19799           if (strcmp (language_string, "GNU Go") == 0)
19800             language = DW_LANG_Go;
19801         }
19802     }
19803   /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works.  */
19804   else if (strcmp (language_string, "GNU Fortran") == 0)
19805     language = DW_LANG_Fortran90;
19806
19807   add_AT_unsigned (die, DW_AT_language, language);
19808
19809   switch (language)
19810     {
19811     case DW_LANG_Fortran77:
19812     case DW_LANG_Fortran90:
19813     case DW_LANG_Fortran95:
19814       /* Fortran has case insensitive identifiers and the front-end
19815          lowercases everything.  */
19816       add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19817       break;
19818     default:
19819       /* The default DW_ID_case_sensitive doesn't need to be specified.  */
19820       break;
19821     }
19822   return die;
19823 }
19824
19825 /* Generate the DIE for a base class.  */
19826
19827 static void
19828 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19829 {
19830   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19831
19832   add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, context_die);
19833   add_data_member_location_attribute (die, binfo);
19834
19835   if (BINFO_VIRTUAL_P (binfo))
19836     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19837
19838   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19839      children, otherwise the default is DW_ACCESS_public.  In DWARF2
19840      the default has always been DW_ACCESS_private.  */
19841   if (access == access_public_node)
19842     {
19843       if (dwarf_version == 2
19844           || context_die->die_tag == DW_TAG_class_type)
19845       add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19846     }
19847   else if (access == access_protected_node)
19848     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19849   else if (dwarf_version > 2
19850            && context_die->die_tag != DW_TAG_class_type)
19851     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19852 }
19853
19854 /* Generate a DIE for a class member.  */
19855
19856 static void
19857 gen_member_die (tree type, dw_die_ref context_die)
19858 {
19859   tree member;
19860   tree binfo = TYPE_BINFO (type);
19861   dw_die_ref child;
19862
19863   /* If this is not an incomplete type, output descriptions of each of its
19864      members. Note that as we output the DIEs necessary to represent the
19865      members of this record or union type, we will also be trying to output
19866      DIEs to represent the *types* of those members. However the `type'
19867      function (above) will specifically avoid generating type DIEs for member
19868      types *within* the list of member DIEs for this (containing) type except
19869      for those types (of members) which are explicitly marked as also being
19870      members of this (containing) type themselves.  The g++ front- end can
19871      force any given type to be treated as a member of some other (containing)
19872      type by setting the TYPE_CONTEXT of the given (member) type to point to
19873      the TREE node representing the appropriate (containing) type.  */
19874
19875   /* First output info about the base classes.  */
19876   if (binfo)
19877     {
19878       vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
19879       int i;
19880       tree base;
19881
19882       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19883         gen_inheritance_die (base,
19884                              (accesses ? (*accesses)[i] : access_public_node),
19885                              context_die);
19886     }
19887
19888   /* Now output info about the data members and type members.  */
19889   for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19890     {
19891       /* If we thought we were generating minimal debug info for TYPE
19892          and then changed our minds, some of the member declarations
19893          may have already been defined.  Don't define them again, but
19894          do put them in the right order.  */
19895
19896       child = lookup_decl_die (member);
19897       if (child)
19898         splice_child_die (context_die, child);
19899       else
19900         gen_decl_die (member, NULL, context_die);
19901     }
19902
19903   /* Now output info about the function members (if any).  */
19904   for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19905     {
19906       /* Don't include clones in the member list.  */
19907       if (DECL_ABSTRACT_ORIGIN (member))
19908         continue;
19909
19910       child = lookup_decl_die (member);
19911       if (child)
19912         splice_child_die (context_die, child);
19913       else
19914         gen_decl_die (member, NULL, context_die);
19915     }
19916 }
19917
19918 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
19919    is set, we pretend that the type was never defined, so we only get the
19920    member DIEs needed by later specification DIEs.  */
19921
19922 static void
19923 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19924                                 enum debug_info_usage usage)
19925 {
19926   dw_die_ref type_die = lookup_type_die (type);
19927   dw_die_ref scope_die = 0;
19928   int nested = 0;
19929   int complete = (TYPE_SIZE (type)
19930                   && (! TYPE_STUB_DECL (type)
19931                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19932   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19933   complete = complete && should_emit_struct_debug (type, usage);
19934
19935   if (type_die && ! complete)
19936     return;
19937
19938   if (TYPE_CONTEXT (type) != NULL_TREE
19939       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19940           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19941     nested = 1;
19942
19943   scope_die = scope_die_for (type, context_die);
19944
19945   /* Generate child dies for template paramaters.  */
19946   if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
19947     schedule_generic_params_dies_gen (type);
19948
19949   if (! type_die || (nested && is_cu_die (scope_die)))
19950     /* First occurrence of type or toplevel definition of nested class.  */
19951     {
19952       dw_die_ref old_die = type_die;
19953
19954       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19955                           ? record_type_tag (type) : DW_TAG_union_type,
19956                           scope_die, type);
19957       equate_type_number_to_die (type, type_die);
19958       if (old_die)
19959         add_AT_specification (type_die, old_die);
19960       else
19961         add_name_attribute (type_die, type_tag (type));
19962     }
19963   else
19964     remove_AT (type_die, DW_AT_declaration);
19965
19966   /* If this type has been completed, then give it a byte_size attribute and
19967      then give a list of members.  */
19968   if (complete && !ns_decl)
19969     {
19970       /* Prevent infinite recursion in cases where the type of some member of
19971          this type is expressed in terms of this type itself.  */
19972       TREE_ASM_WRITTEN (type) = 1;
19973       add_byte_size_attribute (type_die, type);
19974       if (TYPE_STUB_DECL (type) != NULL_TREE)
19975         {
19976           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19977           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19978         }
19979
19980       /* If the first reference to this type was as the return type of an
19981          inline function, then it may not have a parent.  Fix this now.  */
19982       if (type_die->die_parent == NULL)
19983         add_child_die (scope_die, type_die);
19984
19985       push_decl_scope (type);
19986       gen_member_die (type, type_die);
19987       pop_decl_scope ();
19988
19989       add_gnat_descriptive_type_attribute (type_die, type, context_die);
19990       if (TYPE_ARTIFICIAL (type))
19991         add_AT_flag (type_die, DW_AT_artificial, 1);
19992
19993       /* GNU extension: Record what type our vtable lives in.  */
19994       if (TYPE_VFIELD (type))
19995         {
19996           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
19997
19998           gen_type_die (vtype, context_die);
19999           add_AT_die_ref (type_die, DW_AT_containing_type,
20000                           lookup_type_die (vtype));
20001         }
20002     }
20003   else
20004     {
20005       add_AT_flag (type_die, DW_AT_declaration, 1);
20006
20007       /* We don't need to do this for function-local types.  */
20008       if (TYPE_STUB_DECL (type)
20009           && ! decl_function_context (TYPE_STUB_DECL (type)))
20010         vec_safe_push (incomplete_types, type);
20011     }
20012
20013   if (get_AT (type_die, DW_AT_name))
20014     add_pubtype (type, type_die);
20015 }
20016
20017 /* Generate a DIE for a subroutine _type_.  */
20018
20019 static void
20020 gen_subroutine_type_die (tree type, dw_die_ref context_die)
20021 {
20022   tree return_type = TREE_TYPE (type);
20023   dw_die_ref subr_die
20024     = new_die (DW_TAG_subroutine_type,
20025                scope_die_for (type, context_die), type);
20026
20027   equate_type_number_to_die (type, subr_die);
20028   add_prototyped_attribute (subr_die, type);
20029   add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, context_die);
20030   gen_formal_types_die (type, subr_die);
20031
20032   if (get_AT (subr_die, DW_AT_name))
20033     add_pubtype (type, subr_die);
20034 }
20035
20036 /* Generate a DIE for a type definition.  */
20037
20038 static void
20039 gen_typedef_die (tree decl, dw_die_ref context_die)
20040 {
20041   dw_die_ref type_die;
20042   tree origin;
20043
20044   if (TREE_ASM_WRITTEN (decl))
20045     return;
20046
20047   TREE_ASM_WRITTEN (decl) = 1;
20048   type_die = new_die (DW_TAG_typedef, context_die, decl);
20049   origin = decl_ultimate_origin (decl);
20050   if (origin != NULL)
20051     add_abstract_origin_attribute (type_die, origin);
20052   else
20053     {
20054       tree type;
20055
20056       add_name_and_src_coords_attributes (type_die, decl);
20057       if (DECL_ORIGINAL_TYPE (decl))
20058         {
20059           type = DECL_ORIGINAL_TYPE (decl);
20060
20061           gcc_assert (type != TREE_TYPE (decl));
20062           equate_type_number_to_die (TREE_TYPE (decl), type_die);
20063         }
20064       else
20065         {
20066           type = TREE_TYPE (decl);
20067
20068           if (is_naming_typedef_decl (TYPE_NAME (type)))
20069             {
20070               /* Here, we are in the case of decl being a typedef naming
20071                  an anonymous type, e.g:
20072                      typedef struct {...} foo;
20073                  In that case TREE_TYPE (decl) is not a typedef variant
20074                  type and TYPE_NAME of the anonymous type is set to the
20075                  TYPE_DECL of the typedef. This construct is emitted by
20076                  the C++ FE.
20077
20078                  TYPE is the anonymous struct named by the typedef
20079                  DECL. As we need the DW_AT_type attribute of the
20080                  DW_TAG_typedef to point to the DIE of TYPE, let's
20081                  generate that DIE right away. add_type_attribute
20082                  called below will then pick (via lookup_type_die) that
20083                  anonymous struct DIE.  */
20084               if (!TREE_ASM_WRITTEN (type))
20085                 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
20086
20087               /* This is a GNU Extension.  We are adding a
20088                  DW_AT_linkage_name attribute to the DIE of the
20089                  anonymous struct TYPE.  The value of that attribute
20090                  is the name of the typedef decl naming the anonymous
20091                  struct.  This greatly eases the work of consumers of
20092                  this debug info.  */
20093               add_linkage_attr (lookup_type_die (type), decl);
20094             }
20095         }
20096
20097       add_type_attribute (type_die, type, decl_quals (decl), context_die);
20098
20099       if (is_naming_typedef_decl (decl))
20100         /* We want that all subsequent calls to lookup_type_die with
20101            TYPE in argument yield the DW_TAG_typedef we have just
20102            created.  */
20103         equate_type_number_to_die (type, type_die);
20104
20105       add_accessibility_attribute (type_die, decl);
20106     }
20107
20108   if (DECL_ABSTRACT_P (decl))
20109     equate_decl_number_to_die (decl, type_die);
20110
20111   if (get_AT (type_die, DW_AT_name))
20112     add_pubtype (decl, type_die);
20113 }
20114
20115 /* Generate a DIE for a struct, class, enum or union type.  */
20116
20117 static void
20118 gen_tagged_type_die (tree type,
20119                      dw_die_ref context_die,
20120                      enum debug_info_usage usage)
20121 {
20122   int need_pop;
20123
20124   if (type == NULL_TREE
20125       || !is_tagged_type (type))
20126     return;
20127
20128   /* If this is a nested type whose containing class hasn't been written
20129      out yet, writing it out will cover this one, too.  This does not apply
20130      to instantiations of member class templates; they need to be added to
20131      the containing class as they are generated.  FIXME: This hurts the
20132      idea of combining type decls from multiple TUs, since we can't predict
20133      what set of template instantiations we'll get.  */
20134   if (TYPE_CONTEXT (type)
20135       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20136       && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
20137     {
20138       gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
20139
20140       if (TREE_ASM_WRITTEN (type))
20141         return;
20142
20143       /* If that failed, attach ourselves to the stub.  */
20144       push_decl_scope (TYPE_CONTEXT (type));
20145       context_die = lookup_type_die (TYPE_CONTEXT (type));
20146       need_pop = 1;
20147     }
20148   else if (TYPE_CONTEXT (type) != NULL_TREE
20149            && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
20150     {
20151       /* If this type is local to a function that hasn't been written
20152          out yet, use a NULL context for now; it will be fixed up in
20153          decls_for_scope.  */
20154       context_die = lookup_decl_die (TYPE_CONTEXT (type));
20155       /* A declaration DIE doesn't count; nested types need to go in the
20156          specification.  */
20157       if (context_die && is_declaration_die (context_die))
20158         context_die = NULL;
20159       need_pop = 0;
20160     }
20161   else
20162     {
20163       context_die = declare_in_namespace (type, context_die);
20164       need_pop = 0;
20165     }
20166
20167   if (TREE_CODE (type) == ENUMERAL_TYPE)
20168     {
20169       /* This might have been written out by the call to
20170          declare_in_namespace.  */
20171       if (!TREE_ASM_WRITTEN (type))
20172         gen_enumeration_type_die (type, context_die);
20173     }
20174   else
20175     gen_struct_or_union_type_die (type, context_die, usage);
20176
20177   if (need_pop)
20178     pop_decl_scope ();
20179
20180   /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
20181      it up if it is ever completed.  gen_*_type_die will set it for us
20182      when appropriate.  */
20183 }
20184
20185 /* Generate a type description DIE.  */
20186
20187 static void
20188 gen_type_die_with_usage (tree type, dw_die_ref context_die,
20189                          enum debug_info_usage usage)
20190 {
20191   struct array_descr_info info;
20192
20193   if (type == NULL_TREE || type == error_mark_node)
20194     return;
20195
20196   if (TYPE_NAME (type) != NULL_TREE
20197       && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20198       && is_redundant_typedef (TYPE_NAME (type))
20199       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
20200     /* The DECL of this type is a typedef we don't want to emit debug
20201        info for but we want debug info for its underlying typedef.
20202        This can happen for e.g, the injected-class-name of a C++
20203        type.  */
20204     type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
20205
20206   /* If TYPE is a typedef type variant, let's generate debug info
20207      for the parent typedef which TYPE is a type of.  */
20208   if (typedef_variant_p (type))
20209     {
20210       if (TREE_ASM_WRITTEN (type))
20211         return;
20212
20213       /* Prevent broken recursion; we can't hand off to the same type.  */
20214       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
20215
20216       /* Give typedefs the right scope.  */
20217       context_die = scope_die_for (type, context_die);
20218
20219       TREE_ASM_WRITTEN (type) = 1;
20220
20221       gen_decl_die (TYPE_NAME (type), NULL, context_die);
20222       return;
20223     }
20224
20225   /* If type is an anonymous tagged type named by a typedef, let's
20226      generate debug info for the typedef.  */
20227   if (is_naming_typedef_decl (TYPE_NAME (type)))
20228     {
20229       /* Use the DIE of the containing namespace as the parent DIE of
20230          the type description DIE we want to generate.  */
20231       if (DECL_CONTEXT (TYPE_NAME (type))
20232           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20233         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20234       
20235       gen_decl_die (TYPE_NAME (type), NULL, context_die);
20236       return;
20237     }
20238
20239   /* We are going to output a DIE to represent the unqualified version
20240      of this type (i.e. without any const or volatile qualifiers) so
20241      get the main variant (i.e. the unqualified version) of this type
20242      now.  (Vectors are special because the debugging info is in the
20243      cloned type itself).  */
20244   if (TREE_CODE (type) != VECTOR_TYPE)
20245     type = type_main_variant (type);
20246
20247   /* If this is an array type with hidden descriptor, handle it first.  */
20248   if (!TREE_ASM_WRITTEN (type)
20249       && lang_hooks.types.get_array_descr_info)
20250     {
20251       memset (&info, 0, sizeof (info));
20252       if (lang_hooks.types.get_array_descr_info (type, &info))
20253         {
20254           gen_descr_array_type_die (type, &info, context_die);
20255           TREE_ASM_WRITTEN (type) = 1;
20256           return;
20257         }
20258     }
20259
20260   if (TREE_ASM_WRITTEN (type))
20261     return;
20262
20263   switch (TREE_CODE (type))
20264     {
20265     case ERROR_MARK:
20266       break;
20267
20268     case POINTER_TYPE:
20269     case REFERENCE_TYPE:
20270       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
20271          ensures that the gen_type_die recursion will terminate even if the
20272          type is recursive.  Recursive types are possible in Ada.  */
20273       /* ??? We could perhaps do this for all types before the switch
20274          statement.  */
20275       TREE_ASM_WRITTEN (type) = 1;
20276
20277       /* For these types, all that is required is that we output a DIE (or a
20278          set of DIEs) to represent the "basis" type.  */
20279       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20280                                 DINFO_USAGE_IND_USE);
20281       break;
20282
20283     case OFFSET_TYPE:
20284       /* This code is used for C++ pointer-to-data-member types.
20285          Output a description of the relevant class type.  */
20286       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
20287                                         DINFO_USAGE_IND_USE);
20288
20289       /* Output a description of the type of the object pointed to.  */
20290       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20291                                         DINFO_USAGE_IND_USE);
20292
20293       /* Now output a DIE to represent this pointer-to-data-member type
20294          itself.  */
20295       gen_ptr_to_mbr_type_die (type, context_die);
20296       break;
20297
20298     case FUNCTION_TYPE:
20299       /* Force out return type (in case it wasn't forced out already).  */
20300       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20301                                         DINFO_USAGE_DIR_USE);
20302       gen_subroutine_type_die (type, context_die);
20303       break;
20304
20305     case METHOD_TYPE:
20306       /* Force out return type (in case it wasn't forced out already).  */
20307       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20308                                         DINFO_USAGE_DIR_USE);
20309       gen_subroutine_type_die (type, context_die);
20310       break;
20311
20312     case ARRAY_TYPE:
20313       gen_array_type_die (type, context_die);
20314       break;
20315
20316     case VECTOR_TYPE:
20317       gen_array_type_die (type, context_die);
20318       break;
20319
20320     case ENUMERAL_TYPE:
20321     case RECORD_TYPE:
20322     case UNION_TYPE:
20323     case QUAL_UNION_TYPE:
20324       gen_tagged_type_die (type, context_die, usage);
20325       return;
20326
20327     case VOID_TYPE:
20328     case INTEGER_TYPE:
20329     case REAL_TYPE:
20330     case FIXED_POINT_TYPE:
20331     case COMPLEX_TYPE:
20332     case BOOLEAN_TYPE:
20333     case POINTER_BOUNDS_TYPE:
20334       /* No DIEs needed for fundamental types.  */
20335       break;
20336
20337     case NULLPTR_TYPE:
20338     case LANG_TYPE:
20339       /* Just use DW_TAG_unspecified_type.  */
20340       {
20341         dw_die_ref type_die = lookup_type_die (type);
20342         if (type_die == NULL)
20343           {
20344             tree name = TYPE_IDENTIFIER (type);
20345             type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
20346                                 type);
20347             add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20348             equate_type_number_to_die (type, type_die);
20349           }
20350       }
20351       break;
20352
20353     default:
20354       if (is_cxx_auto (type))
20355         {
20356           tree name = TYPE_IDENTIFIER (type);
20357           dw_die_ref *die = (name == get_identifier ("auto")
20358                              ? &auto_die : &decltype_auto_die);
20359           if (!*die)
20360             {
20361               *die = new_die (DW_TAG_unspecified_type,
20362                               comp_unit_die (), NULL_TREE);
20363               add_name_attribute (*die, IDENTIFIER_POINTER (name));
20364             }
20365           equate_type_number_to_die (type, *die);
20366           break;
20367         }
20368       gcc_unreachable ();
20369     }
20370
20371   TREE_ASM_WRITTEN (type) = 1;
20372 }
20373
20374 static void
20375 gen_type_die (tree type, dw_die_ref context_die)
20376 {
20377   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20378 }
20379
20380 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20381    things which are local to the given block.  */
20382
20383 static void
20384 gen_block_die (tree stmt, dw_die_ref context_die)
20385 {
20386   int must_output_die = 0;
20387   bool inlined_func;
20388
20389   /* Ignore blocks that are NULL.  */
20390   if (stmt == NULL_TREE)
20391     return;
20392
20393   inlined_func = inlined_function_outer_scope_p (stmt);
20394
20395   /* If the block is one fragment of a non-contiguous block, do not
20396      process the variables, since they will have been done by the
20397      origin block.  Do process subblocks.  */
20398   if (BLOCK_FRAGMENT_ORIGIN (stmt))
20399     {
20400       tree sub;
20401
20402       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20403         gen_block_die (sub, context_die);
20404
20405       return;
20406     }
20407
20408   /* Determine if we need to output any Dwarf DIEs at all to represent this
20409      block.  */
20410   if (inlined_func)
20411     /* The outer scopes for inlinings *must* always be represented.  We
20412        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
20413     must_output_die = 1;
20414   else
20415     {
20416       /* Determine if this block directly contains any "significant"
20417          local declarations which we will need to output DIEs for.  */
20418       if (debug_info_level > DINFO_LEVEL_TERSE)
20419         /* We are not in terse mode so *any* local declaration counts
20420            as being a "significant" one.  */
20421         must_output_die = ((BLOCK_VARS (stmt) != NULL
20422                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20423                            && (TREE_USED (stmt)
20424                                || TREE_ASM_WRITTEN (stmt)
20425                                || BLOCK_ABSTRACT (stmt)));
20426       else if ((TREE_USED (stmt)
20427                 || TREE_ASM_WRITTEN (stmt)
20428                 || BLOCK_ABSTRACT (stmt))
20429                && !dwarf2out_ignore_block (stmt))
20430         must_output_die = 1;
20431     }
20432
20433   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20434      DIE for any block which contains no significant local declarations at
20435      all.  Rather, in such cases we just call `decls_for_scope' so that any
20436      needed Dwarf info for any sub-blocks will get properly generated. Note
20437      that in terse mode, our definition of what constitutes a "significant"
20438      local declaration gets restricted to include only inlined function
20439      instances and local (nested) function definitions.  */
20440   if (must_output_die)
20441     {
20442       if (inlined_func)
20443         {
20444           /* If STMT block is abstract, that means we have been called
20445              indirectly from dwarf2out_abstract_function.
20446              That function rightfully marks the descendent blocks (of
20447              the abstract function it is dealing with) as being abstract,
20448              precisely to prevent us from emitting any
20449              DW_TAG_inlined_subroutine DIE as a descendent
20450              of an abstract function instance. So in that case, we should
20451              not call gen_inlined_subroutine_die.
20452
20453              Later though, when cgraph asks dwarf2out to emit info
20454              for the concrete instance of the function decl into which
20455              the concrete instance of STMT got inlined, the later will lead
20456              to the generation of a DW_TAG_inlined_subroutine DIE.  */
20457           if (! BLOCK_ABSTRACT (stmt))
20458             gen_inlined_subroutine_die (stmt, context_die);
20459         }
20460       else
20461         gen_lexical_block_die (stmt, context_die);
20462     }
20463   else
20464     decls_for_scope (stmt, context_die);
20465 }
20466
20467 /* Process variable DECL (or variable with origin ORIGIN) within
20468    block STMT and add it to CONTEXT_DIE.  */
20469 static void
20470 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20471 {
20472   dw_die_ref die;
20473   tree decl_or_origin = decl ? decl : origin;
20474
20475   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20476     die = lookup_decl_die (decl_or_origin);
20477   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20478            && TYPE_DECL_IS_STUB (decl_or_origin))
20479     die = lookup_type_die (TREE_TYPE (decl_or_origin));
20480   else
20481     die = NULL;
20482
20483   if (die != NULL && die->die_parent == NULL)
20484     add_child_die (context_die, die);
20485   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20486     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20487                                          stmt, context_die);
20488   else
20489     gen_decl_die (decl, origin, context_die);
20490 }
20491
20492 /* Generate all of the decls declared within a given scope and (recursively)
20493    all of its sub-blocks.  */
20494
20495 static void
20496 decls_for_scope (tree stmt, dw_die_ref context_die)
20497 {
20498   tree decl;
20499   unsigned int i;
20500   tree subblocks;
20501
20502   /* Ignore NULL blocks.  */
20503   if (stmt == NULL_TREE)
20504     return;
20505
20506   /* Output the DIEs to represent all of the data objects and typedefs
20507      declared directly within this block but not within any nested
20508      sub-blocks.  Also, nested function and tag DIEs have been
20509      generated with a parent of NULL; fix that up now.  We don't
20510      have to do this if we're at -g1.  */
20511   if (debug_info_level > DINFO_LEVEL_TERSE)
20512     {
20513       for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20514         process_scope_var (stmt, decl, NULL_TREE, context_die);
20515       for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20516         process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20517                            context_die);
20518     }
20519
20520   /* Even if we're at -g1, we need to process the subblocks in order to get
20521      inlined call information.  */
20522
20523   /* Output the DIEs to represent all sub-blocks (and the items declared
20524      therein) of this block.  */
20525   for (subblocks = BLOCK_SUBBLOCKS (stmt);
20526        subblocks != NULL;
20527        subblocks = BLOCK_CHAIN (subblocks))
20528     gen_block_die (subblocks, context_die);
20529 }
20530
20531 /* Is this a typedef we can avoid emitting?  */
20532
20533 static inline int
20534 is_redundant_typedef (const_tree decl)
20535 {
20536   if (TYPE_DECL_IS_STUB (decl))
20537     return 1;
20538
20539   if (DECL_ARTIFICIAL (decl)
20540       && DECL_CONTEXT (decl)
20541       && is_tagged_type (DECL_CONTEXT (decl))
20542       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20543       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20544     /* Also ignore the artificial member typedef for the class name.  */
20545     return 1;
20546
20547   return 0;
20548 }
20549
20550 /* Return TRUE if TYPE is a typedef that names a type for linkage
20551    purposes. This kind of typedefs is produced by the C++ FE for
20552    constructs like:
20553
20554    typedef struct {...} foo;
20555
20556    In that case, there is no typedef variant type produced for foo.
20557    Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20558    struct type.  */
20559
20560 static bool
20561 is_naming_typedef_decl (const_tree decl)
20562 {
20563   if (decl == NULL_TREE
20564       || TREE_CODE (decl) != TYPE_DECL
20565       || !is_tagged_type (TREE_TYPE (decl))
20566       || DECL_IS_BUILTIN (decl)
20567       || is_redundant_typedef (decl)
20568       /* It looks like Ada produces TYPE_DECLs that are very similar
20569          to C++ naming typedefs but that have different
20570          semantics. Let's be specific to c++ for now.  */
20571       || !is_cxx ())
20572     return FALSE;
20573
20574   return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20575           && TYPE_NAME (TREE_TYPE (decl)) == decl
20576           && (TYPE_STUB_DECL (TREE_TYPE (decl))
20577               != TYPE_NAME (TREE_TYPE (decl))));
20578 }
20579
20580 /* Returns the DIE for a context.  */
20581
20582 static inline dw_die_ref
20583 get_context_die (tree context)
20584 {
20585   if (context)
20586     {
20587       /* Find die that represents this context.  */
20588       if (TYPE_P (context))
20589         {
20590           context = TYPE_MAIN_VARIANT (context);
20591           return strip_naming_typedef (context, force_type_die (context));
20592         }
20593       else
20594         return force_decl_die (context);
20595     }
20596   return comp_unit_die ();
20597 }
20598
20599 /* Returns the DIE for decl.  A DIE will always be returned.  */
20600
20601 static dw_die_ref
20602 force_decl_die (tree decl)
20603 {
20604   dw_die_ref decl_die;
20605   unsigned saved_external_flag;
20606   tree save_fn = NULL_TREE;
20607   decl_die = lookup_decl_die (decl);
20608   if (!decl_die)
20609     {
20610       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20611
20612       decl_die = lookup_decl_die (decl);
20613       if (decl_die)
20614         return decl_die;
20615
20616       switch (TREE_CODE (decl))
20617         {
20618         case FUNCTION_DECL:
20619           /* Clear current_function_decl, so that gen_subprogram_die thinks
20620              that this is a declaration. At this point, we just want to force
20621              declaration die.  */
20622           save_fn = current_function_decl;
20623           current_function_decl = NULL_TREE;
20624           gen_subprogram_die (decl, context_die);
20625           current_function_decl = save_fn;
20626           break;
20627
20628         case VAR_DECL:
20629           /* Set external flag to force declaration die. Restore it after
20630            gen_decl_die() call.  */
20631           saved_external_flag = DECL_EXTERNAL (decl);
20632           DECL_EXTERNAL (decl) = 1;
20633           gen_decl_die (decl, NULL, context_die);
20634           DECL_EXTERNAL (decl) = saved_external_flag;
20635           break;
20636
20637         case NAMESPACE_DECL:
20638           if (dwarf_version >= 3 || !dwarf_strict)
20639             dwarf2out_decl (decl);
20640           else
20641             /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
20642             decl_die = comp_unit_die ();
20643           break;
20644
20645         case TRANSLATION_UNIT_DECL:
20646           decl_die = comp_unit_die ();
20647           break;
20648
20649         default:
20650           gcc_unreachable ();
20651         }
20652
20653       /* We should be able to find the DIE now.  */
20654       if (!decl_die)
20655         decl_die = lookup_decl_die (decl);
20656       gcc_assert (decl_die);
20657     }
20658
20659   return decl_die;
20660 }
20661
20662 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
20663    always returned.  */
20664
20665 static dw_die_ref
20666 force_type_die (tree type)
20667 {
20668   dw_die_ref type_die;
20669
20670   type_die = lookup_type_die (type);
20671   if (!type_die)
20672     {
20673       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
20674
20675       type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
20676                                     context_die);
20677       gcc_assert (type_die);
20678     }
20679   return type_die;
20680 }
20681
20682 /* Force out any required namespaces to be able to output DECL,
20683    and return the new context_die for it, if it's changed.  */
20684
20685 static dw_die_ref
20686 setup_namespace_context (tree thing, dw_die_ref context_die)
20687 {
20688   tree context = (DECL_P (thing)
20689                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
20690   if (context && TREE_CODE (context) == NAMESPACE_DECL)
20691     /* Force out the namespace.  */
20692     context_die = force_decl_die (context);
20693
20694   return context_die;
20695 }
20696
20697 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
20698    type) within its namespace, if appropriate.
20699
20700    For compatibility with older debuggers, namespace DIEs only contain
20701    declarations; all definitions are emitted at CU scope.  */
20702
20703 static dw_die_ref
20704 declare_in_namespace (tree thing, dw_die_ref context_die)
20705 {
20706   dw_die_ref ns_context;
20707
20708   if (debug_info_level <= DINFO_LEVEL_TERSE)
20709     return context_die;
20710
20711   /* External declarations in the local scope only need to be emitted
20712      once, not once in the namespace and once in the scope.
20713
20714      This avoids declaring the `extern' below in the
20715      namespace DIE as well as in the innermost scope:
20716
20717           namespace S
20718           {
20719             int i=5;
20720             int foo()
20721             {
20722               int i=8;
20723               extern int i;
20724               return i;
20725             }
20726           }
20727   */
20728   if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
20729     return context_die;
20730
20731   /* If this decl is from an inlined function, then don't try to emit it in its
20732      namespace, as we will get confused.  It would have already been emitted
20733      when the abstract instance of the inline function was emitted anyways.  */
20734   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
20735     return context_die;
20736
20737   ns_context = setup_namespace_context (thing, context_die);
20738
20739   if (ns_context != context_die)
20740     {
20741       if (is_fortran ())
20742         return ns_context;
20743       if (DECL_P (thing))
20744         gen_decl_die (thing, NULL, ns_context);
20745       else
20746         gen_type_die (thing, ns_context);
20747     }
20748   return context_die;
20749 }
20750
20751 /* Generate a DIE for a namespace or namespace alias.  */
20752
20753 static void
20754 gen_namespace_die (tree decl, dw_die_ref context_die)
20755 {
20756   dw_die_ref namespace_die;
20757
20758   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
20759      they are an alias of.  */
20760   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
20761     {
20762       /* Output a real namespace or module.  */
20763       context_die = setup_namespace_context (decl, comp_unit_die ());
20764       namespace_die = new_die (is_fortran ()
20765                                ? DW_TAG_module : DW_TAG_namespace,
20766                                context_die, decl);
20767       /* For Fortran modules defined in different CU don't add src coords.  */
20768       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20769         {
20770           const char *name = dwarf2_name (decl, 0);
20771           if (name)
20772             add_name_attribute (namespace_die, name);
20773         }
20774       else
20775         add_name_and_src_coords_attributes (namespace_die, decl);
20776       if (DECL_EXTERNAL (decl))
20777         add_AT_flag (namespace_die, DW_AT_declaration, 1);
20778       equate_decl_number_to_die (decl, namespace_die);
20779     }
20780   else
20781     {
20782       /* Output a namespace alias.  */
20783
20784       /* Force out the namespace we are an alias of, if necessary.  */
20785       dw_die_ref origin_die
20786         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20787
20788       if (DECL_FILE_SCOPE_P (decl)
20789           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20790         context_die = setup_namespace_context (decl, comp_unit_die ());
20791       /* Now create the namespace alias DIE.  */
20792       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20793       add_name_and_src_coords_attributes (namespace_die, decl);
20794       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20795       equate_decl_number_to_die (decl, namespace_die);
20796     }
20797   /* Bypass dwarf2_name's check for DECL_NAMELESS.  */
20798   if (want_pubnames ())
20799     add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
20800 }
20801
20802 /* Generate Dwarf debug information for a decl described by DECL.
20803    The return value is currently only meaningful for PARM_DECLs,
20804    for all other decls it returns NULL.  */
20805
20806 static dw_die_ref
20807 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20808 {
20809   tree decl_or_origin = decl ? decl : origin;
20810   tree class_origin = NULL, ultimate_origin;
20811
20812   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20813     return NULL;
20814
20815   /* Ignore pointer bounds decls.  */
20816   if (DECL_P (decl_or_origin)
20817       && TREE_TYPE (decl_or_origin)
20818       && POINTER_BOUNDS_P (decl_or_origin))
20819     return NULL;
20820
20821   switch (TREE_CODE (decl_or_origin))
20822     {
20823     case ERROR_MARK:
20824       break;
20825
20826     case CONST_DECL:
20827       if (!is_fortran () && !is_ada ())
20828         {
20829           /* The individual enumerators of an enum type get output when we output
20830              the Dwarf representation of the relevant enum type itself.  */
20831           break;
20832         }
20833
20834       /* Emit its type.  */
20835       gen_type_die (TREE_TYPE (decl), context_die);
20836
20837       /* And its containing namespace.  */
20838       context_die = declare_in_namespace (decl, context_die);
20839
20840       gen_const_die (decl, context_die);
20841       break;
20842
20843     case FUNCTION_DECL:
20844       /* Don't output any DIEs to represent mere function declarations,
20845          unless they are class members or explicit block externs.  */
20846       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20847           && DECL_FILE_SCOPE_P (decl_or_origin)
20848           && (current_function_decl == NULL_TREE
20849               || DECL_ARTIFICIAL (decl_or_origin)))
20850         break;
20851
20852 #if 0
20853       /* FIXME */
20854       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20855          on local redeclarations of global functions.  That seems broken.  */
20856       if (current_function_decl != decl)
20857         /* This is only a declaration.  */;
20858 #endif
20859
20860       /* If we're emitting a clone, emit info for the abstract instance.  */
20861       if (origin || DECL_ORIGIN (decl) != decl)
20862         dwarf2out_abstract_function (origin
20863                                      ? DECL_ORIGIN (origin)
20864                                      : DECL_ABSTRACT_ORIGIN (decl));
20865
20866       /* If we're emitting an out-of-line copy of an inline function,
20867          emit info for the abstract instance and set up to refer to it.  */
20868       else if (cgraph_function_possibly_inlined_p (decl)
20869                && ! DECL_ABSTRACT_P (decl)
20870                && ! class_or_namespace_scope_p (context_die)
20871                /* dwarf2out_abstract_function won't emit a die if this is just
20872                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
20873                   that case, because that works only if we have a die.  */
20874                && DECL_INITIAL (decl) != NULL_TREE)
20875         {
20876           dwarf2out_abstract_function (decl);
20877           set_decl_origin_self (decl);
20878         }
20879
20880       /* Otherwise we're emitting the primary DIE for this decl.  */
20881       else if (debug_info_level > DINFO_LEVEL_TERSE)
20882         {
20883           /* Before we describe the FUNCTION_DECL itself, make sure that we
20884              have its containing type.  */
20885           if (!origin)
20886             origin = decl_class_context (decl);
20887           if (origin != NULL_TREE)
20888             gen_type_die (origin, context_die);
20889
20890           /* And its return type.  */
20891           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20892
20893           /* And its virtual context.  */
20894           if (DECL_VINDEX (decl) != NULL_TREE)
20895             gen_type_die (DECL_CONTEXT (decl), context_die);
20896
20897           /* Make sure we have a member DIE for decl.  */
20898           if (origin != NULL_TREE)
20899             gen_type_die_for_member (origin, decl, context_die);
20900
20901           /* And its containing namespace.  */
20902           context_die = declare_in_namespace (decl, context_die);
20903         }
20904
20905       /* Now output a DIE to represent the function itself.  */
20906       if (decl)
20907         gen_subprogram_die (decl, context_die);
20908       break;
20909
20910     case TYPE_DECL:
20911       /* If we are in terse mode, don't generate any DIEs to represent any
20912          actual typedefs.  */
20913       if (debug_info_level <= DINFO_LEVEL_TERSE)
20914         break;
20915
20916       /* In the special case of a TYPE_DECL node representing the declaration
20917          of some type tag, if the given TYPE_DECL is marked as having been
20918          instantiated from some other (original) TYPE_DECL node (e.g. one which
20919          was generated within the original definition of an inline function) we
20920          used to generate a special (abbreviated) DW_TAG_structure_type,
20921          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
20922          should be actually referencing those DIEs, as variable DIEs with that
20923          type would be emitted already in the abstract origin, so it was always
20924          removed during unused type prunning.  Don't add anything in this
20925          case.  */
20926       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20927         break;
20928
20929       if (is_redundant_typedef (decl))
20930         gen_type_die (TREE_TYPE (decl), context_die);
20931       else
20932         /* Output a DIE to represent the typedef itself.  */
20933         gen_typedef_die (decl, context_die);
20934       break;
20935
20936     case LABEL_DECL:
20937       if (debug_info_level >= DINFO_LEVEL_NORMAL)
20938         gen_label_die (decl, context_die);
20939       break;
20940
20941     case VAR_DECL:
20942     case RESULT_DECL:
20943       /* If we are in terse mode, don't generate any DIEs to represent any
20944          variable declarations or definitions.  */
20945       if (debug_info_level <= DINFO_LEVEL_TERSE)
20946         break;
20947
20948       /* Output any DIEs that are needed to specify the type of this data
20949          object.  */
20950       if (decl_by_reference_p (decl_or_origin))
20951         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20952       else
20953         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20954
20955       /* And its containing type.  */
20956       class_origin = decl_class_context (decl_or_origin);
20957       if (class_origin != NULL_TREE)
20958         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20959
20960       /* And its containing namespace.  */
20961       context_die = declare_in_namespace (decl_or_origin, context_die);
20962
20963       /* Now output the DIE to represent the data object itself.  This gets
20964          complicated because of the possibility that the VAR_DECL really
20965          represents an inlined instance of a formal parameter for an inline
20966          function.  */
20967       ultimate_origin = decl_ultimate_origin (decl_or_origin);
20968       if (ultimate_origin != NULL_TREE
20969           && TREE_CODE (ultimate_origin) == PARM_DECL)
20970         gen_formal_parameter_die (decl, origin,
20971                                   true /* Emit name attribute.  */,
20972                                   context_die);
20973       else
20974         gen_variable_die (decl, origin, context_die);
20975       break;
20976
20977     case FIELD_DECL:
20978       /* Ignore the nameless fields that are used to skip bits but handle C++
20979          anonymous unions and structs.  */
20980       if (DECL_NAME (decl) != NULL_TREE
20981           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20982           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20983         {
20984           gen_type_die (member_declared_type (decl), context_die);
20985           gen_field_die (decl, context_die);
20986         }
20987       break;
20988
20989     case PARM_DECL:
20990       if (DECL_BY_REFERENCE (decl_or_origin))
20991         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20992       else
20993         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20994       return gen_formal_parameter_die (decl, origin,
20995                                        true /* Emit name attribute.  */,
20996                                        context_die);
20997
20998     case NAMESPACE_DECL:
20999     case IMPORTED_DECL:
21000       if (dwarf_version >= 3 || !dwarf_strict)
21001         gen_namespace_die (decl, context_die);
21002       break;
21003
21004     case NAMELIST_DECL:
21005       gen_namelist_decl (DECL_NAME (decl), context_die,
21006                          NAMELIST_DECL_ASSOCIATED_DECL (decl));
21007       break;
21008
21009     default:
21010       /* Probably some frontend-internal decl.  Assume we don't care.  */
21011       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
21012       break;
21013     }
21014
21015   return NULL;
21016 }
21017 \f
21018 /* Output debug information for global decl DECL.  Called from toplev.c after
21019    compilation proper has finished.  */
21020
21021 static void
21022 dwarf2out_global_decl (tree decl)
21023 {
21024   /* Output DWARF2 information for file-scope tentative data object
21025      declarations, file-scope (extern) function declarations (which
21026      had no corresponding body) and file-scope tagged type declarations
21027      and definitions which have not yet been forced out.  */
21028   if ((TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
21029       && !POINTER_BOUNDS_P (decl))
21030     dwarf2out_decl (decl);
21031 }
21032
21033 /* Output debug information for type decl DECL.  Called from toplev.c
21034    and from language front ends (to record built-in types).  */
21035 static void
21036 dwarf2out_type_decl (tree decl, int local)
21037 {
21038   if (!local)
21039     dwarf2out_decl (decl);
21040 }
21041
21042 /* Output debug information for imported module or decl DECL.
21043    NAME is non-NULL name in the lexical block if the decl has been renamed.
21044    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
21045    that DECL belongs to.
21046    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
21047 static void
21048 dwarf2out_imported_module_or_decl_1 (tree decl,
21049                                      tree name,
21050                                      tree lexical_block,
21051                                      dw_die_ref lexical_block_die)
21052 {
21053   expanded_location xloc;
21054   dw_die_ref imported_die = NULL;
21055   dw_die_ref at_import_die;
21056
21057   if (TREE_CODE (decl) == IMPORTED_DECL)
21058     {
21059       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
21060       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
21061       gcc_assert (decl);
21062     }
21063   else
21064     xloc = expand_location (input_location);
21065
21066   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
21067     {
21068       at_import_die = force_type_die (TREE_TYPE (decl));
21069       /* For namespace N { typedef void T; } using N::T; base_type_die
21070          returns NULL, but DW_TAG_imported_declaration requires
21071          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
21072       if (!at_import_die)
21073         {
21074           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
21075           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
21076           at_import_die = lookup_type_die (TREE_TYPE (decl));
21077           gcc_assert (at_import_die);
21078         }
21079     }
21080   else
21081     {
21082       at_import_die = lookup_decl_die (decl);
21083       if (!at_import_die)
21084         {
21085           /* If we're trying to avoid duplicate debug info, we may not have
21086              emitted the member decl for this field.  Emit it now.  */
21087           if (TREE_CODE (decl) == FIELD_DECL)
21088             {
21089               tree type = DECL_CONTEXT (decl);
21090
21091               if (TYPE_CONTEXT (type)
21092                   && TYPE_P (TYPE_CONTEXT (type))
21093                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
21094                                                 DINFO_USAGE_DIR_USE))
21095                 return;
21096               gen_type_die_for_member (type, decl,
21097                                        get_context_die (TYPE_CONTEXT (type)));
21098             }
21099           if (TREE_CODE (decl) == NAMELIST_DECL)
21100             at_import_die = gen_namelist_decl (DECL_NAME (decl),
21101                                          get_context_die (DECL_CONTEXT (decl)),
21102                                          NULL_TREE);
21103           else
21104             at_import_die = force_decl_die (decl);
21105         }
21106     }
21107
21108   if (TREE_CODE (decl) == NAMESPACE_DECL)
21109     {
21110       if (dwarf_version >= 3 || !dwarf_strict)
21111         imported_die = new_die (DW_TAG_imported_module,
21112                                 lexical_block_die,
21113                                 lexical_block);
21114       else
21115         return;
21116     }
21117   else
21118     imported_die = new_die (DW_TAG_imported_declaration,
21119                             lexical_block_die,
21120                             lexical_block);
21121
21122   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
21123   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
21124   if (name)
21125     add_AT_string (imported_die, DW_AT_name,
21126                    IDENTIFIER_POINTER (name));
21127   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
21128 }
21129
21130 /* Output debug information for imported module or decl DECL.
21131    NAME is non-NULL name in context if the decl has been renamed.
21132    CHILD is true if decl is one of the renamed decls as part of
21133    importing whole module.  */
21134
21135 static void
21136 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
21137                                    bool child)
21138 {
21139   /* dw_die_ref at_import_die;  */
21140   dw_die_ref scope_die;
21141
21142   if (debug_info_level <= DINFO_LEVEL_TERSE)
21143     return;
21144
21145   gcc_assert (decl);
21146
21147   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
21148      We need decl DIE for reference and scope die. First, get DIE for the decl
21149      itself.  */
21150
21151   /* Get the scope die for decl context. Use comp_unit_die for global module
21152      or decl. If die is not found for non globals, force new die.  */
21153   if (context
21154       && TYPE_P (context)
21155       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
21156     return;
21157
21158   if (!(dwarf_version >= 3 || !dwarf_strict))
21159     return;
21160
21161   scope_die = get_context_die (context);
21162
21163   if (child)
21164     {
21165       gcc_assert (scope_die->die_child);
21166       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
21167       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
21168       scope_die = scope_die->die_child;
21169     }
21170
21171   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
21172   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
21173
21174 }
21175
21176 /* Output debug information for namelists.   */
21177
21178 static dw_die_ref
21179 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
21180 {
21181   dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
21182   tree value;
21183   unsigned i;
21184
21185   if (debug_info_level <= DINFO_LEVEL_TERSE)
21186     return NULL;
21187
21188   gcc_assert (scope_die != NULL);
21189   nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
21190   add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
21191
21192   /* If there are no item_decls, we have a nondefining namelist, e.g.
21193      with USE association; hence, set DW_AT_declaration.  */
21194   if (item_decls == NULL_TREE)
21195     {
21196       add_AT_flag (nml_die, DW_AT_declaration, 1);
21197       return nml_die;
21198     }
21199
21200   FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
21201     {
21202       nml_item_ref_die = lookup_decl_die (value);
21203       if (!nml_item_ref_die)
21204         nml_item_ref_die = force_decl_die (value);
21205
21206       nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
21207       add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
21208     }
21209   return nml_die;
21210 }
21211
21212
21213 /* Write the debugging output for DECL.  */
21214
21215 static void
21216 dwarf2out_decl (tree decl)
21217 {
21218   dw_die_ref context_die = comp_unit_die ();
21219
21220   switch (TREE_CODE (decl))
21221     {
21222     case ERROR_MARK:
21223       return;
21224
21225     case FUNCTION_DECL:
21226       /* What we would really like to do here is to filter out all mere
21227          file-scope declarations of file-scope functions which are never
21228          referenced later within this translation unit (and keep all of ones
21229          that *are* referenced later on) but we aren't clairvoyant, so we have
21230          no idea which functions will be referenced in the future (i.e. later
21231          on within the current translation unit). So here we just ignore all
21232          file-scope function declarations which are not also definitions.  If
21233          and when the debugger needs to know something about these functions,
21234          it will have to hunt around and find the DWARF information associated
21235          with the definition of the function.
21236
21237          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
21238          nodes represent definitions and which ones represent mere
21239          declarations.  We have to check DECL_INITIAL instead. That's because
21240          the C front-end supports some weird semantics for "extern inline"
21241          function definitions.  These can get inlined within the current
21242          translation unit (and thus, we need to generate Dwarf info for their
21243          abstract instances so that the Dwarf info for the concrete inlined
21244          instances can have something to refer to) but the compiler never
21245          generates any out-of-lines instances of such things (despite the fact
21246          that they *are* definitions).
21247
21248          The important point is that the C front-end marks these "extern
21249          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
21250          them anyway. Note that the C++ front-end also plays some similar games
21251          for inline function definitions appearing within include files which
21252          also contain `#pragma interface' pragmas.
21253
21254          If we are called from dwarf2out_abstract_function output a DIE
21255          anyway.  We can end up here this way with early inlining and LTO
21256          where the inlined function is output in a different LTRANS unit
21257          or not at all.  */
21258       if (DECL_INITIAL (decl) == NULL_TREE
21259           && ! DECL_ABSTRACT_P (decl))
21260         return;
21261
21262       /* If we're a nested function, initially use a parent of NULL; if we're
21263          a plain function, this will be fixed up in decls_for_scope.  If
21264          we're a method, it will be ignored, since we already have a DIE.  */
21265       if (decl_function_context (decl)
21266           /* But if we're in terse mode, we don't care about scope.  */
21267           && debug_info_level > DINFO_LEVEL_TERSE)
21268         context_die = NULL;
21269       break;
21270
21271     case VAR_DECL:
21272       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
21273          declaration and if the declaration was never even referenced from
21274          within this entire compilation unit.  We suppress these DIEs in
21275          order to save space in the .debug section (by eliminating entries
21276          which are probably useless).  Note that we must not suppress
21277          block-local extern declarations (whether used or not) because that
21278          would screw-up the debugger's name lookup mechanism and cause it to
21279          miss things which really ought to be in scope at a given point.  */
21280       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
21281         return;
21282
21283       /* For local statics lookup proper context die.  */
21284       if (TREE_STATIC (decl)
21285           && DECL_CONTEXT (decl)
21286           && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
21287         context_die = lookup_decl_die (DECL_CONTEXT (decl));
21288
21289       /* If we are in terse mode, don't generate any DIEs to represent any
21290          variable declarations or definitions.  */
21291       if (debug_info_level <= DINFO_LEVEL_TERSE)
21292         return;
21293       break;
21294
21295     case CONST_DECL:
21296       if (debug_info_level <= DINFO_LEVEL_TERSE)
21297         return;
21298       if (!is_fortran () && !is_ada ())
21299         return;
21300       if (TREE_STATIC (decl) && decl_function_context (decl))
21301         context_die = lookup_decl_die (DECL_CONTEXT (decl));
21302       break;
21303
21304     case NAMESPACE_DECL:
21305     case IMPORTED_DECL:
21306       if (debug_info_level <= DINFO_LEVEL_TERSE)
21307         return;
21308       if (lookup_decl_die (decl) != NULL)
21309         return;
21310       break;
21311
21312     case TYPE_DECL:
21313       /* Don't emit stubs for types unless they are needed by other DIEs.  */
21314       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
21315         return;
21316
21317       /* Don't bother trying to generate any DIEs to represent any of the
21318          normal built-in types for the language we are compiling.  */
21319       if (DECL_IS_BUILTIN (decl))
21320         return;
21321
21322       /* If we are in terse mode, don't generate any DIEs for types.  */
21323       if (debug_info_level <= DINFO_LEVEL_TERSE)
21324         return;
21325
21326       /* If we're a function-scope tag, initially use a parent of NULL;
21327          this will be fixed up in decls_for_scope.  */
21328       if (decl_function_context (decl))
21329         context_die = NULL;
21330
21331       break;
21332
21333     case NAMELIST_DECL:
21334       break;
21335
21336     default:
21337       return;
21338     }
21339
21340   gen_decl_die (decl, NULL, context_die);
21341 }
21342
21343 /* Write the debugging output for DECL.  */
21344
21345 static void
21346 dwarf2out_function_decl (tree decl)
21347 {
21348   dwarf2out_decl (decl);
21349   call_arg_locations = NULL;
21350   call_arg_loc_last = NULL;
21351   call_site_count = -1;
21352   tail_call_site_count = -1;
21353   block_map.release ();
21354   decl_loc_table->empty ();
21355   cached_dw_loc_list_table->empty ();
21356 }
21357
21358 /* Output a marker (i.e. a label) for the beginning of the generated code for
21359    a lexical block.  */
21360
21361 static void
21362 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21363                        unsigned int blocknum)
21364 {
21365   switch_to_section (current_function_section ());
21366   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
21367 }
21368
21369 /* Output a marker (i.e. a label) for the end of the generated code for a
21370    lexical block.  */
21371
21372 static void
21373 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
21374 {
21375   switch_to_section (current_function_section ());
21376   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
21377 }
21378
21379 /* Returns nonzero if it is appropriate not to emit any debugging
21380    information for BLOCK, because it doesn't contain any instructions.
21381
21382    Don't allow this for blocks with nested functions or local classes
21383    as we would end up with orphans, and in the presence of scheduling
21384    we may end up calling them anyway.  */
21385
21386 static bool
21387 dwarf2out_ignore_block (const_tree block)
21388 {
21389   tree decl;
21390   unsigned int i;
21391
21392   for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
21393     if (TREE_CODE (decl) == FUNCTION_DECL
21394         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21395       return 0;
21396   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
21397     {
21398       decl = BLOCK_NONLOCALIZED_VAR (block, i);
21399       if (TREE_CODE (decl) == FUNCTION_DECL
21400           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21401       return 0;
21402     }
21403
21404   return 1;
21405 }
21406
21407 /* Hash table routines for file_hash.  */
21408
21409 bool
21410 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
21411 {
21412   return filename_cmp (p1->filename, p2) == 0;
21413 }
21414
21415 hashval_t
21416 dwarf_file_hasher::hash (dwarf_file_data *p)
21417 {
21418   return htab_hash_string (p->filename);
21419 }
21420
21421 /* Lookup FILE_NAME (in the list of filenames that we know about here in
21422    dwarf2out.c) and return its "index".  The index of each (known) filename is
21423    just a unique number which is associated with only that one filename.  We
21424    need such numbers for the sake of generating labels (in the .debug_sfnames
21425    section) and references to those files numbers (in the .debug_srcinfo
21426    and.debug_macinfo sections).  If the filename given as an argument is not
21427    found in our current list, add it to the list and assign it the next
21428    available unique index number.  In order to speed up searches, we remember
21429    the index of the filename was looked up last.  This handles the majority of
21430    all searches.  */
21431
21432 static struct dwarf_file_data *
21433 lookup_filename (const char *file_name)
21434 {
21435   struct dwarf_file_data * created;
21436
21437   /* Check to see if the file name that was searched on the previous
21438      call matches this file name.  If so, return the index.  */
21439   if (file_table_last_lookup
21440       && (file_name == file_table_last_lookup->filename
21441           || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
21442     return file_table_last_lookup;
21443
21444   /* Didn't match the previous lookup, search the table.  */
21445   dwarf_file_data **slot
21446     = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
21447                                        INSERT);
21448   if (*slot)
21449     return *slot;
21450
21451   created = ggc_alloc<dwarf_file_data> ();
21452   created->filename = file_name;
21453   created->emitted_number = 0;
21454   *slot = created;
21455   return created;
21456 }
21457
21458 /* If the assembler will construct the file table, then translate the compiler
21459    internal file table number into the assembler file table number, and emit
21460    a .file directive if we haven't already emitted one yet.  The file table
21461    numbers are different because we prune debug info for unused variables and
21462    types, which may include filenames.  */
21463
21464 static int
21465 maybe_emit_file (struct dwarf_file_data * fd)
21466 {
21467   if (! fd->emitted_number)
21468     {
21469       if (last_emitted_file)
21470         fd->emitted_number = last_emitted_file->emitted_number + 1;
21471       else
21472         fd->emitted_number = 1;
21473       last_emitted_file = fd;
21474
21475       if (DWARF2_ASM_LINE_DEBUG_INFO)
21476         {
21477           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
21478           output_quoted_string (asm_out_file,
21479                                 remap_debug_filename (fd->filename));
21480           fputc ('\n', asm_out_file);
21481         }
21482     }
21483
21484   return fd->emitted_number;
21485 }
21486
21487 /* Schedule generation of a DW_AT_const_value attribute to DIE.
21488    That generation should happen after function debug info has been
21489    generated. The value of the attribute is the constant value of ARG.  */
21490
21491 static void
21492 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
21493 {
21494   die_arg_entry entry;
21495
21496   if (!die || !arg)
21497     return;
21498
21499   if (!tmpl_value_parm_die_table)
21500     vec_alloc (tmpl_value_parm_die_table, 32);
21501
21502   entry.die = die;
21503   entry.arg = arg;
21504   vec_safe_push (tmpl_value_parm_die_table, entry);
21505 }
21506
21507 /* Return TRUE if T is an instance of generic type, FALSE
21508    otherwise.  */
21509
21510 static bool
21511 generic_type_p (tree t)
21512 {
21513   if (t == NULL_TREE || !TYPE_P (t))
21514     return false;
21515   return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
21516 }
21517
21518 /* Schedule the generation of the generic parameter dies for the
21519   instance of generic type T. The proper generation itself is later
21520   done by gen_scheduled_generic_parms_dies. */
21521
21522 static void
21523 schedule_generic_params_dies_gen (tree t)
21524 {
21525   if (!generic_type_p (t))
21526     return;
21527
21528   if (!generic_type_instances)
21529     vec_alloc (generic_type_instances, 256);
21530
21531   vec_safe_push (generic_type_instances, t);
21532 }
21533
21534 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21535    by append_entry_to_tmpl_value_parm_die_table. This function must
21536    be called after function DIEs have been generated.  */
21537
21538 static void
21539 gen_remaining_tmpl_value_param_die_attribute (void)
21540 {
21541   if (tmpl_value_parm_die_table)
21542     {
21543       unsigned i;
21544       die_arg_entry *e;
21545
21546       FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
21547         tree_add_const_value_attribute (e->die, e->arg);
21548     }
21549 }
21550
21551 /* Generate generic parameters DIEs for instances of generic types
21552    that have been previously scheduled by
21553    schedule_generic_params_dies_gen. This function must be called
21554    after all the types of the CU have been laid out.  */
21555
21556 static void
21557 gen_scheduled_generic_parms_dies (void)
21558 {
21559   unsigned i;
21560   tree t;
21561
21562   if (!generic_type_instances)
21563     return;
21564   
21565   FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
21566     if (COMPLETE_TYPE_P (t))
21567       gen_generic_params_dies (t);
21568 }
21569
21570
21571 /* Replace DW_AT_name for the decl with name.  */
21572
21573 static void
21574 dwarf2out_set_name (tree decl, tree name)
21575 {
21576   dw_die_ref die;
21577   dw_attr_ref attr;
21578   const char *dname;
21579
21580   die = TYPE_SYMTAB_DIE (decl);
21581   if (!die)
21582     return;
21583
21584   dname = dwarf2_name (name, 0);
21585   if (!dname)
21586     return;
21587
21588   attr = get_AT (die, DW_AT_name);
21589   if (attr)
21590     {
21591       struct indirect_string_node *node;
21592
21593       node = find_AT_string (dname);
21594       /* replace the string.  */
21595       attr->dw_attr_val.v.val_str = node;
21596     }
21597
21598   else
21599     add_name_attribute (die, dname);
21600 }
21601
21602 /* True if before or during processing of the first function being emitted.  */
21603 static bool in_first_function_p = true;
21604 /* True if loc_note during dwarf2out_var_location call might still be
21605    before first real instruction at address equal to .Ltext0.  */
21606 static bool maybe_at_text_label_p = true;
21607 /* One above highest N where .LVLN label might be equal to .Ltext0 label.  */
21608 static unsigned int first_loclabel_num_not_at_text_label;
21609
21610 /* Called by the final INSN scan whenever we see a var location.  We
21611    use it to drop labels in the right places, and throw the location in
21612    our lookup table.  */
21613
21614 static void
21615 dwarf2out_var_location (rtx_insn *loc_note)
21616 {
21617   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21618   struct var_loc_node *newloc;
21619   rtx_insn *next_real, *next_note;
21620   static const char *last_label;
21621   static const char *last_postcall_label;
21622   static bool last_in_cold_section_p;
21623   static rtx_insn *expected_next_loc_note;
21624   tree decl;
21625   bool var_loc_p;
21626
21627   if (!NOTE_P (loc_note))
21628     {
21629       if (CALL_P (loc_note))
21630         {
21631           call_site_count++;
21632           if (SIBLING_CALL_P (loc_note))
21633             tail_call_site_count++;
21634         }
21635       return;
21636     }
21637
21638   var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
21639   if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21640     return;
21641
21642   /* Optimize processing a large consecutive sequence of location
21643      notes so we don't spend too much time in next_real_insn.  If the
21644      next insn is another location note, remember the next_real_insn
21645      calculation for next time.  */
21646   next_real = cached_next_real_insn;
21647   if (next_real)
21648     {
21649       if (expected_next_loc_note != loc_note)
21650         next_real = NULL;
21651     }
21652
21653   next_note = NEXT_INSN (loc_note);
21654   if (! next_note
21655       || next_note->deleted ()
21656       || ! NOTE_P (next_note)
21657       || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
21658           && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
21659     next_note = NULL;
21660
21661   if (! next_real)
21662     next_real = next_real_insn (loc_note);
21663
21664   if (next_note)
21665     {
21666       expected_next_loc_note = next_note;
21667       cached_next_real_insn = next_real;
21668     }
21669   else
21670     cached_next_real_insn = NULL;
21671
21672   /* If there are no instructions which would be affected by this note,
21673      don't do anything.  */
21674   if (var_loc_p
21675       && next_real == NULL_RTX
21676       && !NOTE_DURING_CALL_P (loc_note))
21677     return;
21678
21679   if (next_real == NULL_RTX)
21680     next_real = get_last_insn ();
21681
21682   /* If there were any real insns between note we processed last time
21683      and this note (or if it is the first note), clear
21684      last_{,postcall_}label so that they are not reused this time.  */
21685   if (last_var_location_insn == NULL_RTX
21686       || last_var_location_insn != next_real
21687       || last_in_cold_section_p != in_cold_section_p)
21688     {
21689       last_label = NULL;
21690       last_postcall_label = NULL;
21691     }
21692
21693   if (var_loc_p)
21694     {
21695       decl = NOTE_VAR_LOCATION_DECL (loc_note);
21696       newloc = add_var_loc_to_decl (decl, loc_note,
21697                                     NOTE_DURING_CALL_P (loc_note)
21698                                     ? last_postcall_label : last_label);
21699       if (newloc == NULL)
21700         return;
21701     }
21702   else
21703     {
21704       decl = NULL_TREE;
21705       newloc = NULL;
21706     }
21707
21708   /* If there were no real insns between note we processed last time
21709      and this note, use the label we emitted last time.  Otherwise
21710      create a new label and emit it.  */
21711   if (last_label == NULL)
21712     {
21713       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21714       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21715       loclabel_num++;
21716       last_label = ggc_strdup (loclabel);
21717       /* See if loclabel might be equal to .Ltext0.  If yes,
21718          bump first_loclabel_num_not_at_text_label.  */
21719       if (!have_multiple_function_sections
21720           && in_first_function_p
21721           && maybe_at_text_label_p)
21722         {
21723           static rtx_insn *last_start;
21724           rtx_insn *insn;
21725           for (insn = loc_note; insn; insn = previous_insn (insn))
21726             if (insn == last_start)
21727               break;
21728             else if (!NONDEBUG_INSN_P (insn))
21729               continue;
21730             else
21731               {
21732                 rtx body = PATTERN (insn);
21733                 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
21734                   continue;
21735                 /* Inline asm could occupy zero bytes.  */
21736                 else if (GET_CODE (body) == ASM_INPUT
21737                          || asm_noperands (body) >= 0)
21738                   continue;
21739 #ifdef HAVE_attr_length
21740                 else if (get_attr_min_length (insn) == 0)
21741                   continue;
21742 #endif
21743                 else
21744                   {
21745                     /* Assume insn has non-zero length.  */
21746                     maybe_at_text_label_p = false;
21747                     break;
21748                   }
21749               }
21750           if (maybe_at_text_label_p)
21751             {
21752               last_start = loc_note;
21753               first_loclabel_num_not_at_text_label = loclabel_num;
21754             }
21755         }
21756     }
21757
21758   if (!var_loc_p)
21759     {
21760       struct call_arg_loc_node *ca_loc
21761         = ggc_cleared_alloc<call_arg_loc_node> ();
21762       rtx_insn *prev = prev_real_insn (loc_note);
21763       rtx x;
21764       ca_loc->call_arg_loc_note = loc_note;
21765       ca_loc->next = NULL;
21766       ca_loc->label = last_label;
21767       gcc_assert (prev
21768                   && (CALL_P (prev)
21769                       || (NONJUMP_INSN_P (prev)
21770                           && GET_CODE (PATTERN (prev)) == SEQUENCE
21771                           && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
21772       if (!CALL_P (prev))
21773         prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
21774       ca_loc->tail_call_p = SIBLING_CALL_P (prev);
21775       x = get_call_rtx_from (PATTERN (prev));
21776       if (x)
21777         {
21778           x = XEXP (XEXP (x, 0), 0);
21779           if (GET_CODE (x) == SYMBOL_REF
21780               && SYMBOL_REF_DECL (x)
21781               && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
21782             ca_loc->symbol_ref = x;
21783         }
21784       ca_loc->block = insn_scope (prev);
21785       if (call_arg_locations)
21786         call_arg_loc_last->next = ca_loc;
21787       else
21788         call_arg_locations = ca_loc;
21789       call_arg_loc_last = ca_loc;
21790     }
21791   else if (!NOTE_DURING_CALL_P (loc_note))
21792     newloc->label = last_label;
21793   else
21794     {
21795       if (!last_postcall_label)
21796         {
21797           sprintf (loclabel, "%s-1", last_label);
21798           last_postcall_label = ggc_strdup (loclabel);
21799         }
21800       newloc->label = last_postcall_label;
21801     }
21802
21803   last_var_location_insn = next_real;
21804   last_in_cold_section_p = in_cold_section_p;
21805 }
21806
21807 /* Note in one location list that text section has changed.  */
21808
21809 int
21810 var_location_switch_text_section_1 (var_loc_list **slot, void *)
21811 {
21812   var_loc_list *list = *slot;
21813   if (list->first)
21814     list->last_before_switch
21815       = list->last->next ? list->last->next : list->last;
21816   return 1;
21817 }
21818
21819 /* Note in all location lists that text section has changed.  */
21820
21821 static void
21822 var_location_switch_text_section (void)
21823 {
21824   if (decl_loc_table == NULL)
21825     return;
21826
21827   decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
21828 }
21829
21830 /* Create a new line number table.  */
21831
21832 static dw_line_info_table *
21833 new_line_info_table (void)
21834 {
21835   dw_line_info_table *table;
21836
21837   table = ggc_cleared_alloc<dw_line_info_table_struct> ();
21838   table->file_num = 1;
21839   table->line_num = 1;
21840   table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
21841
21842   return table;
21843 }
21844
21845 /* Lookup the "current" table into which we emit line info, so
21846    that we don't have to do it for every source line.  */
21847
21848 static void
21849 set_cur_line_info_table (section *sec)
21850 {
21851   dw_line_info_table *table;
21852
21853   if (sec == text_section)
21854     table = text_section_line_info;
21855   else if (sec == cold_text_section)
21856     {
21857       table = cold_text_section_line_info;
21858       if (!table)
21859         {
21860           cold_text_section_line_info = table = new_line_info_table ();
21861           table->end_label = cold_end_label;
21862         }
21863     }
21864   else
21865     {
21866       const char *end_label;
21867
21868       if (flag_reorder_blocks_and_partition)
21869         {
21870           if (in_cold_section_p)
21871             end_label = crtl->subsections.cold_section_end_label;
21872           else
21873             end_label = crtl->subsections.hot_section_end_label;
21874         }
21875       else
21876         {
21877           char label[MAX_ARTIFICIAL_LABEL_BYTES];
21878           ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
21879                                        current_function_funcdef_no);
21880           end_label = ggc_strdup (label);
21881         }
21882
21883       table = new_line_info_table ();
21884       table->end_label = end_label;
21885
21886       vec_safe_push (separate_line_info, table);
21887     }
21888
21889   if (DWARF2_ASM_LINE_DEBUG_INFO)
21890     table->is_stmt = (cur_line_info_table
21891                       ? cur_line_info_table->is_stmt
21892                       : DWARF_LINE_DEFAULT_IS_STMT_START);
21893   cur_line_info_table = table;
21894 }
21895
21896
21897 /* We need to reset the locations at the beginning of each
21898    function. We can't do this in the end_function hook, because the
21899    declarations that use the locations won't have been output when
21900    that hook is called.  Also compute have_multiple_function_sections here.  */
21901
21902 static void
21903 dwarf2out_begin_function (tree fun)
21904 {
21905   section *sec = function_section (fun);
21906
21907   if (sec != text_section)
21908     have_multiple_function_sections = true;
21909
21910   if (flag_reorder_blocks_and_partition && !cold_text_section)
21911     {
21912       gcc_assert (current_function_decl == fun);
21913       cold_text_section = unlikely_text_section ();
21914       switch_to_section (cold_text_section);
21915       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21916       switch_to_section (sec);
21917     }
21918
21919   dwarf2out_note_section_used ();
21920   call_site_count = 0;
21921   tail_call_site_count = 0;
21922
21923   set_cur_line_info_table (sec);
21924 }
21925
21926 /* Helper function of dwarf2out_end_function, called only after emitting
21927    the very first function into assembly.  Check if some .debug_loc range
21928    might end with a .LVL* label that could be equal to .Ltext0.
21929    In that case we must force using absolute addresses in .debug_loc ranges,
21930    because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
21931    .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
21932    list terminator.
21933    Set have_multiple_function_sections to true in that case and
21934    terminate htab traversal.  */
21935
21936 int
21937 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
21938 {
21939   var_loc_list *entry = *slot;
21940   struct var_loc_node *node;
21941
21942   node = entry->first;
21943   if (node && node->next && node->next->label)
21944     {
21945       unsigned int i;
21946       const char *label = node->next->label;
21947       char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
21948
21949       for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
21950         {
21951           ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
21952           if (strcmp (label, loclabel) == 0)
21953             {
21954               have_multiple_function_sections = true;
21955               return 0;
21956             }
21957         }
21958     }
21959   return 1;
21960 }
21961
21962 /* Hook called after emitting a function into assembly.
21963    This does something only for the very first function emitted.  */
21964
21965 static void
21966 dwarf2out_end_function (unsigned int)
21967 {
21968   if (in_first_function_p
21969       && !have_multiple_function_sections
21970       && first_loclabel_num_not_at_text_label
21971       && decl_loc_table)
21972     decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
21973   in_first_function_p = false;
21974   maybe_at_text_label_p = false;
21975 }
21976
21977 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE.  */
21978
21979 static void
21980 push_dw_line_info_entry (dw_line_info_table *table,
21981                          enum dw_line_info_opcode opcode, unsigned int val)
21982 {
21983   dw_line_info_entry e;
21984   e.opcode = opcode;
21985   e.val = val;
21986   vec_safe_push (table->entries, e);
21987 }
21988
21989 /* Output a label to mark the beginning of a source code line entry
21990    and record information relating to this source line, in
21991    'line_info_table' for later output of the .debug_line section.  */
21992 /* ??? The discriminator parameter ought to be unsigned.  */
21993
21994 static void
21995 dwarf2out_source_line (unsigned int line, const char *filename,
21996                        int discriminator, bool is_stmt)
21997 {
21998   unsigned int file_num;
21999   dw_line_info_table *table;
22000
22001   if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
22002     return;
22003
22004   /* The discriminator column was added in dwarf4.  Simplify the below
22005      by simply removing it if we're not supposed to output it.  */
22006   if (dwarf_version < 4 && dwarf_strict)
22007     discriminator = 0;
22008
22009   table = cur_line_info_table;
22010   file_num = maybe_emit_file (lookup_filename (filename));
22011
22012   /* ??? TODO: Elide duplicate line number entries.  Traditionally,
22013      the debugger has used the second (possibly duplicate) line number
22014      at the beginning of the function to mark the end of the prologue.
22015      We could eliminate any other duplicates within the function.  For
22016      Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
22017      that second line number entry.  */
22018   /* Recall that this end-of-prologue indication is *not* the same thing
22019      as the end_prologue debug hook.  The NOTE_INSN_PROLOGUE_END note,
22020      to which the hook corresponds, follows the last insn that was 
22021      emitted by gen_prologue.  What we need is to precede the first insn
22022      that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
22023      insn that corresponds to something the user wrote.  These may be
22024      very different locations once scheduling is enabled.  */
22025
22026   if (0 && file_num == table->file_num
22027       && line == table->line_num
22028       && discriminator == table->discrim_num
22029       && is_stmt == table->is_stmt)
22030     return;
22031
22032   switch_to_section (current_function_section ());
22033
22034   /* If requested, emit something human-readable.  */
22035   if (flag_debug_asm)
22036     fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
22037
22038   if (DWARF2_ASM_LINE_DEBUG_INFO)
22039     {
22040       /* Emit the .loc directive understood by GNU as.  */
22041       /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
22042          file_num, line, is_stmt, discriminator */
22043       fputs ("\t.loc ", asm_out_file);
22044       fprint_ul (asm_out_file, file_num);
22045       putc (' ', asm_out_file);
22046       fprint_ul (asm_out_file, line);
22047       putc (' ', asm_out_file);
22048       putc ('0', asm_out_file);
22049
22050       if (is_stmt != table->is_stmt)
22051         {
22052           fputs (" is_stmt ", asm_out_file);
22053           putc (is_stmt ? '1' : '0', asm_out_file);
22054         }
22055       if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
22056         {
22057           gcc_assert (discriminator > 0);
22058           fputs (" discriminator ", asm_out_file);
22059           fprint_ul (asm_out_file, (unsigned long) discriminator);
22060         }
22061       putc ('\n', asm_out_file);
22062     }
22063   else
22064     {
22065       unsigned int label_num = ++line_info_label_num;
22066
22067       targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
22068
22069       push_dw_line_info_entry (table, LI_set_address, label_num);
22070       if (file_num != table->file_num)
22071         push_dw_line_info_entry (table, LI_set_file, file_num);
22072       if (discriminator != table->discrim_num)
22073         push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
22074       if (is_stmt != table->is_stmt)
22075         push_dw_line_info_entry (table, LI_negate_stmt, 0);
22076       push_dw_line_info_entry (table, LI_set_line, line);
22077     }
22078
22079   table->file_num = file_num;
22080   table->line_num = line;
22081   table->discrim_num = discriminator;
22082   table->is_stmt = is_stmt;
22083   table->in_use = true;
22084 }
22085
22086 /* Record the beginning of a new source file.  */
22087
22088 static void
22089 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
22090 {
22091   if (flag_eliminate_dwarf2_dups)
22092     {
22093       /* Record the beginning of the file for break_out_includes.  */
22094       dw_die_ref bincl_die;
22095
22096       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
22097       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
22098     }
22099
22100   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22101     {
22102       macinfo_entry e;
22103       e.code = DW_MACINFO_start_file;
22104       e.lineno = lineno;
22105       e.info = ggc_strdup (filename);
22106       vec_safe_push (macinfo_table, e);
22107     }
22108 }
22109
22110 /* Record the end of a source file.  */
22111
22112 static void
22113 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
22114 {
22115   if (flag_eliminate_dwarf2_dups)
22116     /* Record the end of the file for break_out_includes.  */
22117     new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
22118
22119   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22120     {
22121       macinfo_entry e;
22122       e.code = DW_MACINFO_end_file;
22123       e.lineno = lineno;
22124       e.info = NULL;
22125       vec_safe_push (macinfo_table, e);
22126     }
22127 }
22128
22129 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
22130    the tail part of the directive line, i.e. the part which is past the
22131    initial whitespace, #, whitespace, directive-name, whitespace part.  */
22132
22133 static void
22134 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
22135                   const char *buffer ATTRIBUTE_UNUSED)
22136 {
22137   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22138     {
22139       macinfo_entry e;
22140       /* Insert a dummy first entry to be able to optimize the whole
22141          predefined macro block using DW_MACRO_GNU_transparent_include.  */
22142       if (macinfo_table->is_empty () && lineno <= 1)
22143         {
22144           e.code = 0;
22145           e.lineno = 0;
22146           e.info = NULL;
22147           vec_safe_push (macinfo_table, e);
22148         }
22149       e.code = DW_MACINFO_define;
22150       e.lineno = lineno;
22151       e.info = ggc_strdup (buffer);
22152       vec_safe_push (macinfo_table, e);
22153     }
22154 }
22155
22156 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
22157    the tail part of the directive line, i.e. the part which is past the
22158    initial whitespace, #, whitespace, directive-name, whitespace part.  */
22159
22160 static void
22161 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
22162                  const char *buffer ATTRIBUTE_UNUSED)
22163 {
22164   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22165     {
22166       macinfo_entry e;
22167       /* Insert a dummy first entry to be able to optimize the whole
22168          predefined macro block using DW_MACRO_GNU_transparent_include.  */
22169       if (macinfo_table->is_empty () && lineno <= 1)
22170         {
22171           e.code = 0;
22172           e.lineno = 0;
22173           e.info = NULL;
22174           vec_safe_push (macinfo_table, e);
22175         }
22176       e.code = DW_MACINFO_undef;
22177       e.lineno = lineno;
22178       e.info = ggc_strdup (buffer);
22179       vec_safe_push (macinfo_table, e);
22180     }
22181 }
22182
22183 /* Helpers to manipulate hash table of CUs.  */
22184
22185 struct macinfo_entry_hasher : typed_noop_remove <macinfo_entry>
22186 {
22187   typedef macinfo_entry value_type;
22188   typedef macinfo_entry compare_type;
22189   static inline hashval_t hash (const value_type *);
22190   static inline bool equal (const value_type *, const compare_type *);
22191 };
22192
22193 inline hashval_t
22194 macinfo_entry_hasher::hash (const value_type *entry)
22195 {
22196   return htab_hash_string (entry->info);
22197 }
22198
22199 inline bool
22200 macinfo_entry_hasher::equal (const value_type *entry1,
22201                              const compare_type *entry2)
22202 {
22203   return !strcmp (entry1->info, entry2->info);
22204 }
22205
22206 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
22207
22208 /* Output a single .debug_macinfo entry.  */
22209
22210 static void
22211 output_macinfo_op (macinfo_entry *ref)
22212 {
22213   int file_num;
22214   size_t len;
22215   struct indirect_string_node *node;
22216   char label[MAX_ARTIFICIAL_LABEL_BYTES];
22217   struct dwarf_file_data *fd;
22218
22219   switch (ref->code)
22220     {
22221     case DW_MACINFO_start_file:
22222       fd = lookup_filename (ref->info);
22223       file_num = maybe_emit_file (fd);
22224       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
22225       dw2_asm_output_data_uleb128 (ref->lineno,
22226                                    "Included from line number %lu", 
22227                                    (unsigned long) ref->lineno);
22228       dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
22229       break;
22230     case DW_MACINFO_end_file:
22231       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
22232       break;
22233     case DW_MACINFO_define:
22234     case DW_MACINFO_undef:
22235       len = strlen (ref->info) + 1;
22236       if (!dwarf_strict
22237           && len > DWARF_OFFSET_SIZE
22238           && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
22239           && (debug_str_section->common.flags & SECTION_MERGE) != 0)
22240         {
22241           ref->code = ref->code == DW_MACINFO_define
22242                       ? DW_MACRO_GNU_define_indirect
22243                       : DW_MACRO_GNU_undef_indirect;
22244           output_macinfo_op (ref);
22245           return;
22246         }
22247       dw2_asm_output_data (1, ref->code,
22248                            ref->code == DW_MACINFO_define
22249                            ? "Define macro" : "Undefine macro");
22250       dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu", 
22251                                    (unsigned long) ref->lineno);
22252       dw2_asm_output_nstring (ref->info, -1, "The macro");
22253       break;
22254     case DW_MACRO_GNU_define_indirect:
22255     case DW_MACRO_GNU_undef_indirect:
22256       node = find_AT_string (ref->info);
22257       gcc_assert (node
22258                   && ((node->form == DW_FORM_strp)
22259                       || (node->form == DW_FORM_GNU_str_index)));
22260       dw2_asm_output_data (1, ref->code,
22261                            ref->code == DW_MACRO_GNU_define_indirect
22262                            ? "Define macro indirect"
22263                            : "Undefine macro indirect");
22264       dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22265                                    (unsigned long) ref->lineno);
22266       if (node->form == DW_FORM_strp)
22267         dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
22268                                debug_str_section, "The macro: \"%s\"",
22269                                ref->info);
22270       else
22271         dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
22272                                      ref->info);
22273       break;
22274     case DW_MACRO_GNU_transparent_include:
22275       dw2_asm_output_data (1, ref->code, "Transparent include");
22276       ASM_GENERATE_INTERNAL_LABEL (label,
22277                                    DEBUG_MACRO_SECTION_LABEL, ref->lineno);
22278       dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
22279       break;
22280     default:
22281       fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
22282                ASM_COMMENT_START, (unsigned long) ref->code);
22283       break;
22284     }
22285 }
22286
22287 /* Attempt to make a sequence of define/undef macinfo ops shareable with
22288    other compilation unit .debug_macinfo sections.  IDX is the first
22289    index of a define/undef, return the number of ops that should be
22290    emitted in a comdat .debug_macinfo section and emit
22291    a DW_MACRO_GNU_transparent_include entry referencing it.
22292    If the define/undef entry should be emitted normally, return 0.  */
22293
22294 static unsigned
22295 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
22296                         macinfo_hash_type **macinfo_htab)
22297 {
22298   macinfo_entry *first, *second, *cur, *inc;
22299   char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
22300   unsigned char checksum[16];
22301   struct md5_ctx ctx;
22302   char *grp_name, *tail;
22303   const char *base;
22304   unsigned int i, count, encoded_filename_len, linebuf_len;
22305   macinfo_entry **slot;
22306
22307   first = &(*macinfo_table)[idx];
22308   second = &(*macinfo_table)[idx + 1];
22309
22310   /* Optimize only if there are at least two consecutive define/undef ops,
22311      and either all of them are before first DW_MACINFO_start_file
22312      with lineno {0,1} (i.e. predefined macro block), or all of them are
22313      in some included header file.  */
22314   if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
22315     return 0;
22316   if (vec_safe_is_empty (files))
22317     {
22318       if (first->lineno > 1 || second->lineno > 1)
22319         return 0;
22320     }
22321   else if (first->lineno == 0)
22322     return 0;
22323
22324   /* Find the last define/undef entry that can be grouped together
22325      with first and at the same time compute md5 checksum of their
22326      codes, linenumbers and strings.  */
22327   md5_init_ctx (&ctx);
22328   for (i = idx; macinfo_table->iterate (i, &cur); i++)
22329     if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
22330       break;
22331     else if (vec_safe_is_empty (files) && cur->lineno > 1)
22332       break;
22333     else
22334       {
22335         unsigned char code = cur->code;
22336         md5_process_bytes (&code, 1, &ctx);
22337         checksum_uleb128 (cur->lineno, &ctx);
22338         md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
22339       }
22340   md5_finish_ctx (&ctx, checksum);
22341   count = i - idx;
22342
22343   /* From the containing include filename (if any) pick up just
22344      usable characters from its basename.  */
22345   if (vec_safe_is_empty (files))
22346     base = "";
22347   else
22348     base = lbasename (files->last ().info);
22349   for (encoded_filename_len = 0, i = 0; base[i]; i++)
22350     if (ISIDNUM (base[i]) || base[i] == '.')
22351       encoded_filename_len++;
22352   /* Count . at the end.  */
22353   if (encoded_filename_len)
22354     encoded_filename_len++;
22355
22356   sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
22357   linebuf_len = strlen (linebuf);
22358
22359   /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum>  */
22360   grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
22361                          + 16 * 2 + 1);
22362   memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
22363   tail = grp_name + 4;
22364   if (encoded_filename_len)
22365     {
22366       for (i = 0; base[i]; i++)
22367         if (ISIDNUM (base[i]) || base[i] == '.')
22368           *tail++ = base[i];
22369       *tail++ = '.';
22370     }
22371   memcpy (tail, linebuf, linebuf_len);
22372   tail += linebuf_len;
22373   *tail++ = '.';
22374   for (i = 0; i < 16; i++)
22375     sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
22376
22377   /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
22378      in the empty vector entry before the first define/undef.  */
22379   inc = &(*macinfo_table)[idx - 1];
22380   inc->code = DW_MACRO_GNU_transparent_include;
22381   inc->lineno = 0;
22382   inc->info = ggc_strdup (grp_name);
22383   if (!*macinfo_htab)
22384     *macinfo_htab = new macinfo_hash_type (10);
22385   /* Avoid emitting duplicates.  */
22386   slot = (*macinfo_htab)->find_slot (inc, INSERT);
22387   if (*slot != NULL)
22388     {
22389       inc->code = 0;
22390       inc->info = NULL;
22391       /* If such an entry has been used before, just emit
22392          a DW_MACRO_GNU_transparent_include op.  */
22393       inc = *slot;
22394       output_macinfo_op (inc);
22395       /* And clear all macinfo_entry in the range to avoid emitting them
22396          in the second pass.  */
22397       for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
22398         {
22399           cur->code = 0;
22400           cur->info = NULL;
22401         }
22402     }
22403   else
22404     {
22405       *slot = inc;
22406       inc->lineno = (*macinfo_htab)->elements ();
22407       output_macinfo_op (inc);
22408     }
22409   return count;
22410 }
22411
22412 /* Save any strings needed by the macinfo table in the debug str
22413    table.  All strings must be collected into the table by the time
22414    index_string is called.  */
22415
22416 static void
22417 save_macinfo_strings (void)
22418 {
22419   unsigned len;
22420   unsigned i;
22421   macinfo_entry *ref;
22422
22423   for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
22424     {
22425       switch (ref->code)
22426         {
22427           /* Match the logic in output_macinfo_op to decide on
22428              indirect strings.  */
22429           case DW_MACINFO_define:
22430           case DW_MACINFO_undef:
22431             len = strlen (ref->info) + 1;
22432             if (!dwarf_strict
22433                 && len > DWARF_OFFSET_SIZE
22434                 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
22435                 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
22436               set_indirect_string (find_AT_string (ref->info));
22437             break;
22438           case DW_MACRO_GNU_define_indirect:
22439           case DW_MACRO_GNU_undef_indirect:
22440             set_indirect_string (find_AT_string (ref->info));
22441             break;
22442           default:
22443             break;
22444         }
22445     }
22446 }
22447
22448 /* Output macinfo section(s).  */
22449
22450 static void
22451 output_macinfo (void)
22452 {
22453   unsigned i;
22454   unsigned long length = vec_safe_length (macinfo_table);
22455   macinfo_entry *ref;
22456   vec<macinfo_entry, va_gc> *files = NULL;
22457   macinfo_hash_type *macinfo_htab = NULL;
22458
22459   if (! length)
22460     return;
22461
22462   /* output_macinfo* uses these interchangeably.  */
22463   gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
22464               && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
22465               && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
22466               && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
22467
22468   /* For .debug_macro emit the section header.  */
22469   if (!dwarf_strict)
22470     {
22471       dw2_asm_output_data (2, 4, "DWARF macro version number");
22472       if (DWARF_OFFSET_SIZE == 8)
22473         dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
22474       else
22475         dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
22476       dw2_asm_output_offset (DWARF_OFFSET_SIZE,
22477                              (!dwarf_split_debug_info ? debug_line_section_label
22478                               : debug_skeleton_line_section_label),
22479                              debug_line_section, NULL);
22480     }
22481
22482   /* In the first loop, it emits the primary .debug_macinfo section
22483      and after each emitted op the macinfo_entry is cleared.
22484      If a longer range of define/undef ops can be optimized using
22485      DW_MACRO_GNU_transparent_include, the
22486      DW_MACRO_GNU_transparent_include op is emitted and kept in
22487      the vector before the first define/undef in the range and the
22488      whole range of define/undef ops is not emitted and kept.  */
22489   for (i = 0; macinfo_table->iterate (i, &ref); i++)
22490     {
22491       switch (ref->code)
22492         {
22493         case DW_MACINFO_start_file:
22494           vec_safe_push (files, *ref);
22495           break;
22496         case DW_MACINFO_end_file:
22497           if (!vec_safe_is_empty (files))
22498             files->pop ();
22499           break;
22500         case DW_MACINFO_define:
22501         case DW_MACINFO_undef:
22502           if (!dwarf_strict
22503               && HAVE_COMDAT_GROUP
22504               && vec_safe_length (files) != 1
22505               && i > 0
22506               && i + 1 < length
22507               && (*macinfo_table)[i - 1].code == 0)
22508             {
22509               unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
22510               if (count)
22511                 {
22512                   i += count - 1;
22513                   continue;
22514                 }
22515             }
22516           break;
22517         case 0:
22518           /* A dummy entry may be inserted at the beginning to be able
22519              to optimize the whole block of predefined macros.  */
22520           if (i == 0)
22521             continue;
22522         default:
22523           break;
22524         }
22525       output_macinfo_op (ref);
22526       ref->info = NULL;
22527       ref->code = 0;
22528     }
22529
22530   if (!macinfo_htab)
22531     return;
22532
22533   delete macinfo_htab;
22534   macinfo_htab = NULL;
22535
22536   /* If any DW_MACRO_GNU_transparent_include were used, on those
22537      DW_MACRO_GNU_transparent_include entries terminate the
22538      current chain and switch to a new comdat .debug_macinfo
22539      section and emit the define/undef entries within it.  */
22540   for (i = 0; macinfo_table->iterate (i, &ref); i++)
22541     switch (ref->code)
22542       {
22543       case 0:
22544         continue;
22545       case DW_MACRO_GNU_transparent_include:
22546         {
22547           char label[MAX_ARTIFICIAL_LABEL_BYTES];
22548           tree comdat_key = get_identifier (ref->info);
22549           /* Terminate the previous .debug_macinfo section.  */
22550           dw2_asm_output_data (1, 0, "End compilation unit");
22551           targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
22552                                          SECTION_DEBUG
22553                                          | SECTION_LINKONCE,
22554                                          comdat_key);
22555           ASM_GENERATE_INTERNAL_LABEL (label,
22556                                        DEBUG_MACRO_SECTION_LABEL,
22557                                        ref->lineno);
22558           ASM_OUTPUT_LABEL (asm_out_file, label);
22559           ref->code = 0;
22560           ref->info = NULL;
22561           dw2_asm_output_data (2, 4, "DWARF macro version number");
22562           if (DWARF_OFFSET_SIZE == 8)
22563             dw2_asm_output_data (1, 1, "Flags: 64-bit");
22564           else
22565             dw2_asm_output_data (1, 0, "Flags: 32-bit");
22566         }
22567         break;
22568       case DW_MACINFO_define:
22569       case DW_MACINFO_undef:
22570         output_macinfo_op (ref);
22571         ref->code = 0;
22572         ref->info = NULL;
22573         break;
22574       default:
22575         gcc_unreachable ();
22576       }
22577 }
22578
22579 /* Set up for Dwarf output at the start of compilation.  */
22580
22581 static void
22582 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
22583 {
22584   /* Allocate the file_table.  */
22585   file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
22586
22587   /* Allocate the decl_die_table.  */
22588   decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
22589
22590   /* Allocate the decl_loc_table.  */
22591   decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
22592
22593   /* Allocate the cached_dw_loc_list_table.  */
22594   cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
22595
22596   /* Allocate the initial hunk of the decl_scope_table.  */
22597   vec_alloc (decl_scope_table, 256);
22598
22599   /* Allocate the initial hunk of the abbrev_die_table.  */
22600   abbrev_die_table = ggc_cleared_vec_alloc<dw_die_ref>
22601     (ABBREV_DIE_TABLE_INCREMENT);
22602   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
22603   /* Zero-th entry is allocated, but unused.  */
22604   abbrev_die_table_in_use = 1;
22605
22606   /* Allocate the pubtypes and pubnames vectors.  */
22607   vec_alloc (pubname_table, 32);
22608   vec_alloc (pubtype_table, 32);
22609
22610   vec_alloc (incomplete_types, 64);
22611
22612   vec_alloc (used_rtx_array, 32);
22613
22614   if (!dwarf_split_debug_info)
22615     {
22616       debug_info_section = get_section (DEBUG_INFO_SECTION,
22617                                         SECTION_DEBUG, NULL);
22618       debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22619                                           SECTION_DEBUG, NULL);
22620       debug_loc_section = get_section (DEBUG_LOC_SECTION,
22621                                        SECTION_DEBUG, NULL);
22622     }
22623   else
22624     {
22625       debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
22626                                         SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22627       debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
22628                                           SECTION_DEBUG | SECTION_EXCLUDE,
22629                                           NULL);
22630       debug_addr_section = get_section (DEBUG_ADDR_SECTION,
22631                                         SECTION_DEBUG, NULL);
22632       debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
22633                                                  SECTION_DEBUG, NULL);
22634       debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22635                                                    SECTION_DEBUG, NULL);
22636       ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
22637                                   DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
22638
22639       /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
22640          the main .o, but the skeleton_line goes into the split off dwo.  */
22641       debug_skeleton_line_section
22642           = get_section (DEBUG_DWO_LINE_SECTION,
22643                          SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22644       ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
22645                                    DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
22646       debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
22647                                                SECTION_DEBUG | SECTION_EXCLUDE,
22648                                                NULL);
22649       ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
22650                                    DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
22651       debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
22652                                        SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22653       debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
22654                                            DEBUG_STR_DWO_SECTION_FLAGS, NULL);
22655     }
22656   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
22657                                        SECTION_DEBUG, NULL);
22658   debug_macinfo_section = get_section (dwarf_strict
22659                                        ? DEBUG_MACINFO_SECTION
22660                                        : DEBUG_MACRO_SECTION,
22661                                        DEBUG_MACRO_SECTION_FLAGS, NULL);
22662   debug_line_section = get_section (DEBUG_LINE_SECTION,
22663                                     SECTION_DEBUG, NULL);
22664   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
22665                                         SECTION_DEBUG, NULL);
22666   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
22667                                         SECTION_DEBUG, NULL);
22668   debug_str_section = get_section (DEBUG_STR_SECTION,
22669                                    DEBUG_STR_SECTION_FLAGS, NULL);
22670   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
22671                                       SECTION_DEBUG, NULL);
22672   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
22673                                      SECTION_DEBUG, NULL);
22674
22675   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
22676   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
22677                                DEBUG_ABBREV_SECTION_LABEL, 0);
22678   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
22679   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
22680                                COLD_TEXT_SECTION_LABEL, 0);
22681   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
22682
22683   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
22684                                DEBUG_INFO_SECTION_LABEL, 0);
22685   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
22686                                DEBUG_LINE_SECTION_LABEL, 0);
22687   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
22688                                DEBUG_RANGES_SECTION_LABEL, 0);
22689   ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
22690                                DEBUG_ADDR_SECTION_LABEL, 0);
22691   ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
22692                                dwarf_strict
22693                                ? DEBUG_MACINFO_SECTION_LABEL
22694                                : DEBUG_MACRO_SECTION_LABEL, 0);
22695   ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
22696
22697   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22698     vec_alloc (macinfo_table, 64);
22699
22700   switch_to_section (text_section);
22701   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
22702
22703   /* Make sure the line number table for .text always exists.  */
22704   text_section_line_info = new_line_info_table ();
22705   text_section_line_info->end_label = text_end_label;
22706 }
22707
22708 /* Called before compile () starts outputtting functions, variables
22709    and toplevel asms into assembly.  */
22710
22711 static void
22712 dwarf2out_assembly_start (void)
22713 {
22714   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
22715       && dwarf2out_do_cfi_asm ()
22716       && (!(flag_unwind_tables || flag_exceptions)
22717           || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
22718     fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
22719 }
22720
22721 /* A helper function for dwarf2out_finish called through
22722    htab_traverse.  Assign a string its index.  All strings must be
22723    collected into the table by the time index_string is called,
22724    because the indexing code relies on htab_traverse to traverse nodes
22725    in the same order for each run. */
22726
22727 int
22728 index_string (indirect_string_node **h, unsigned int *index)
22729 {
22730   indirect_string_node *node = *h;
22731
22732   find_string_form (node);
22733   if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22734     {
22735       gcc_assert (node->index == NO_INDEX_ASSIGNED);
22736       node->index = *index;
22737       *index += 1;
22738     }
22739   return 1;
22740 }
22741
22742 /* A helper function for output_indirect_strings called through
22743    htab_traverse.  Output the offset to a string and update the
22744    current offset.  */
22745
22746 int
22747 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
22748 {
22749   indirect_string_node *node = *h;
22750
22751   if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22752     {
22753       /* Assert that this node has been assigned an index.  */
22754       gcc_assert (node->index != NO_INDEX_ASSIGNED
22755                   && node->index != NOT_INDEXED);
22756       dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
22757                            "indexed string 0x%x: %s", node->index, node->str);
22758       *offset += strlen (node->str) + 1;
22759     }
22760   return 1;
22761 }
22762
22763 /* A helper function for dwarf2out_finish called through
22764    htab_traverse.  Output the indexed string.  */
22765
22766 int
22767 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
22768 {
22769   struct indirect_string_node *node = *h;
22770
22771   if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22772     {
22773       /* Assert that the strings are output in the same order as their
22774          indexes were assigned.  */
22775       gcc_assert (*cur_idx == node->index);
22776       assemble_string (node->str, strlen (node->str) + 1);
22777       *cur_idx += 1;
22778     }
22779   return 1;
22780 }
22781
22782 /* A helper function for dwarf2out_finish called through
22783    htab_traverse.  Emit one queued .debug_str string.  */
22784
22785 int
22786 output_indirect_string (indirect_string_node **h, void *)
22787 {
22788   struct indirect_string_node *node = *h;
22789
22790   node->form = find_string_form (node);
22791   if (node->form == DW_FORM_strp && node->refcount > 0)
22792     {
22793       ASM_OUTPUT_LABEL (asm_out_file, node->label);
22794       assemble_string (node->str, strlen (node->str) + 1);
22795     }
22796
22797   return 1;
22798 }
22799
22800 /* Output the indexed string table.  */
22801
22802 static void
22803 output_indirect_strings (void)
22804 {
22805   switch_to_section (debug_str_section);
22806   if (!dwarf_split_debug_info)
22807     debug_str_hash->traverse<void *, output_indirect_string> (NULL);
22808   else
22809     {
22810       unsigned int offset = 0;
22811       unsigned int cur_idx = 0;
22812
22813       skeleton_debug_str_hash->traverse<void *, output_indirect_string> (NULL);
22814
22815       switch_to_section (debug_str_offsets_section);
22816       debug_str_hash->traverse_noresize
22817         <unsigned int *, output_index_string_offset> (&offset);
22818       switch_to_section (debug_str_dwo_section);
22819       debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
22820         (&cur_idx);
22821     }
22822 }
22823
22824 /* Callback for htab_traverse to assign an index to an entry in the
22825    table, and to write that entry to the .debug_addr section.  */
22826
22827 int
22828 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
22829 {
22830   addr_table_entry *entry = *slot;
22831
22832   if (entry->refcount == 0)
22833     {
22834       gcc_assert (entry->index == NO_INDEX_ASSIGNED
22835                   || entry->index == NOT_INDEXED);
22836       return 1;
22837     }
22838
22839   gcc_assert (entry->index == *cur_index);
22840   (*cur_index)++;
22841
22842   switch (entry->kind)
22843     {
22844       case ate_kind_rtx:
22845         dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
22846                                  "0x%x", entry->index);
22847         break;
22848       case ate_kind_rtx_dtprel:
22849         gcc_assert (targetm.asm_out.output_dwarf_dtprel);
22850         targetm.asm_out.output_dwarf_dtprel (asm_out_file,
22851                                              DWARF2_ADDR_SIZE,
22852                                              entry->addr.rtl);
22853         fputc ('\n', asm_out_file);
22854         break;
22855       case ate_kind_label:
22856         dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
22857                                  "0x%x", entry->index);
22858         break;
22859       default:
22860         gcc_unreachable ();
22861     }
22862   return 1;
22863 }
22864
22865 /* Produce the .debug_addr section.  */
22866
22867 static void
22868 output_addr_table (void)
22869 {
22870   unsigned int index = 0;
22871   if (addr_index_table == NULL || addr_index_table->size () == 0)
22872     return;
22873
22874   switch_to_section (debug_addr_section);
22875   addr_index_table
22876     ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
22877 }
22878
22879 #if ENABLE_ASSERT_CHECKING
22880 /* Verify that all marks are clear.  */
22881
22882 static void
22883 verify_marks_clear (dw_die_ref die)
22884 {
22885   dw_die_ref c;
22886
22887   gcc_assert (! die->die_mark);
22888   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22889 }
22890 #endif /* ENABLE_ASSERT_CHECKING */
22891
22892 /* Clear the marks for a die and its children.
22893    Be cool if the mark isn't set.  */
22894
22895 static void
22896 prune_unmark_dies (dw_die_ref die)
22897 {
22898   dw_die_ref c;
22899
22900   if (die->die_mark)
22901     die->die_mark = 0;
22902   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22903 }
22904
22905 /* Given DIE that we're marking as used, find any other dies
22906    it references as attributes and mark them as used.  */
22907
22908 static void
22909 prune_unused_types_walk_attribs (dw_die_ref die)
22910 {
22911   dw_attr_ref a;
22912   unsigned ix;
22913
22914   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22915     {
22916       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22917         {
22918           /* A reference to another DIE.
22919              Make sure that it will get emitted.
22920              If it was broken out into a comdat group, don't follow it.  */
22921           if (! AT_ref (a)->comdat_type_p
22922               || a->dw_attr == DW_AT_specification)
22923             prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22924         }
22925       /* Set the string's refcount to 0 so that prune_unused_types_mark
22926          accounts properly for it.  */
22927       if (AT_class (a) == dw_val_class_str)
22928         a->dw_attr_val.v.val_str->refcount = 0;
22929     }
22930 }
22931
22932 /* Mark the generic parameters and arguments children DIEs of DIE.  */
22933
22934 static void
22935 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
22936 {
22937   dw_die_ref c;
22938
22939   if (die == NULL || die->die_child == NULL)
22940     return;
22941   c = die->die_child;
22942   do
22943     {
22944       if (is_template_parameter (c))
22945         prune_unused_types_mark (c, 1);
22946       c = c->die_sib;
22947     } while (c && c != die->die_child);
22948 }
22949
22950 /* Mark DIE as being used.  If DOKIDS is true, then walk down
22951    to DIE's children.  */
22952
22953 static void
22954 prune_unused_types_mark (dw_die_ref die, int dokids)
22955 {
22956   dw_die_ref c;
22957
22958   if (die->die_mark == 0)
22959     {
22960       /* We haven't done this node yet.  Mark it as used.  */
22961       die->die_mark = 1;
22962       /* If this is the DIE of a generic type instantiation,
22963          mark the children DIEs that describe its generic parms and
22964          args.  */
22965       prune_unused_types_mark_generic_parms_dies (die);
22966
22967       /* We also have to mark its parents as used.
22968          (But we don't want to mark our parent's kids due to this,
22969          unless it is a class.)  */
22970       if (die->die_parent)
22971         prune_unused_types_mark (die->die_parent,
22972                                  class_scope_p (die->die_parent));
22973
22974       /* Mark any referenced nodes.  */
22975       prune_unused_types_walk_attribs (die);
22976
22977       /* If this node is a specification,
22978          also mark the definition, if it exists.  */
22979       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22980         prune_unused_types_mark (die->die_definition, 1);
22981     }
22982
22983   if (dokids && die->die_mark != 2)
22984     {
22985       /* We need to walk the children, but haven't done so yet.
22986          Remember that we've walked the kids.  */
22987       die->die_mark = 2;
22988
22989       /* If this is an array type, we need to make sure our
22990          kids get marked, even if they're types.  If we're
22991          breaking out types into comdat sections, do this
22992          for all type definitions.  */
22993       if (die->die_tag == DW_TAG_array_type
22994           || (use_debug_types
22995               && is_type_die (die) && ! is_declaration_die (die)))
22996         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
22997       else
22998         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22999     }
23000 }
23001
23002 /* For local classes, look if any static member functions were emitted
23003    and if so, mark them.  */
23004
23005 static void
23006 prune_unused_types_walk_local_classes (dw_die_ref die)
23007 {
23008   dw_die_ref c;
23009
23010   if (die->die_mark == 2)
23011     return;
23012
23013   switch (die->die_tag)
23014     {
23015     case DW_TAG_structure_type:
23016     case DW_TAG_union_type:
23017     case DW_TAG_class_type:
23018       break;
23019
23020     case DW_TAG_subprogram:
23021       if (!get_AT_flag (die, DW_AT_declaration)
23022           || die->die_definition != NULL)
23023         prune_unused_types_mark (die, 1);
23024       return;
23025
23026     default:
23027       return;
23028     }
23029
23030   /* Mark children.  */
23031   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
23032 }
23033
23034 /* Walk the tree DIE and mark types that we actually use.  */
23035
23036 static void
23037 prune_unused_types_walk (dw_die_ref die)
23038 {
23039   dw_die_ref c;
23040
23041   /* Don't do anything if this node is already marked and
23042      children have been marked as well.  */
23043   if (die->die_mark == 2)
23044     return;
23045
23046   switch (die->die_tag)
23047     {
23048     case DW_TAG_structure_type:
23049     case DW_TAG_union_type:
23050     case DW_TAG_class_type:
23051       if (die->die_perennial_p)
23052         break;
23053
23054       for (c = die->die_parent; c; c = c->die_parent)
23055         if (c->die_tag == DW_TAG_subprogram)
23056           break;
23057
23058       /* Finding used static member functions inside of classes
23059          is needed just for local classes, because for other classes
23060          static member function DIEs with DW_AT_specification
23061          are emitted outside of the DW_TAG_*_type.  If we ever change
23062          it, we'd need to call this even for non-local classes.  */
23063       if (c)
23064         prune_unused_types_walk_local_classes (die);
23065
23066       /* It's a type node --- don't mark it.  */
23067       return;
23068
23069     case DW_TAG_const_type:
23070     case DW_TAG_packed_type:
23071     case DW_TAG_pointer_type:
23072     case DW_TAG_reference_type:
23073     case DW_TAG_rvalue_reference_type:
23074     case DW_TAG_volatile_type:
23075     case DW_TAG_typedef:
23076     case DW_TAG_array_type:
23077     case DW_TAG_interface_type:
23078     case DW_TAG_friend:
23079     case DW_TAG_variant_part:
23080     case DW_TAG_enumeration_type:
23081     case DW_TAG_subroutine_type:
23082     case DW_TAG_string_type:
23083     case DW_TAG_set_type:
23084     case DW_TAG_subrange_type:
23085     case DW_TAG_ptr_to_member_type:
23086     case DW_TAG_file_type:
23087       if (die->die_perennial_p)
23088         break;
23089
23090       /* It's a type node --- don't mark it.  */
23091       return;
23092
23093     default:
23094       /* Mark everything else.  */
23095       break;
23096   }
23097
23098   if (die->die_mark == 0)
23099     {
23100       die->die_mark = 1;
23101
23102       /* Now, mark any dies referenced from here.  */
23103       prune_unused_types_walk_attribs (die);
23104     }
23105
23106   die->die_mark = 2;
23107
23108   /* Mark children.  */
23109   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
23110 }
23111
23112 /* Increment the string counts on strings referred to from DIE's
23113    attributes.  */
23114
23115 static void
23116 prune_unused_types_update_strings (dw_die_ref die)
23117 {
23118   dw_attr_ref a;
23119   unsigned ix;
23120
23121   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23122     if (AT_class (a) == dw_val_class_str)
23123       {
23124         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
23125         s->refcount++;
23126         /* Avoid unnecessarily putting strings that are used less than
23127            twice in the hash table.  */
23128         if (s->refcount
23129             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
23130           {
23131             indirect_string_node **slot
23132               = debug_str_hash->find_slot_with_hash (s->str,
23133                                                      htab_hash_string (s->str),
23134                                                      INSERT);
23135             gcc_assert (*slot == NULL);
23136             *slot = s;
23137           }
23138       }
23139 }
23140
23141 /* Remove from the tree DIE any dies that aren't marked.  */
23142
23143 static void
23144 prune_unused_types_prune (dw_die_ref die)
23145 {
23146   dw_die_ref c;
23147
23148   gcc_assert (die->die_mark);
23149   prune_unused_types_update_strings (die);
23150
23151   if (! die->die_child)
23152     return;
23153
23154   c = die->die_child;
23155   do {
23156     dw_die_ref prev = c;
23157     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
23158       if (c == die->die_child)
23159         {
23160           /* No marked children between 'prev' and the end of the list.  */
23161           if (prev == c)
23162             /* No marked children at all.  */
23163             die->die_child = NULL;
23164           else
23165             {
23166               prev->die_sib = c->die_sib;
23167               die->die_child = prev;
23168             }
23169           return;
23170         }
23171
23172     if (c != prev->die_sib)
23173       prev->die_sib = c;
23174     prune_unused_types_prune (c);
23175   } while (c != die->die_child);
23176 }
23177
23178 /* Remove dies representing declarations that we never use.  */
23179
23180 static void
23181 prune_unused_types (void)
23182 {
23183   unsigned int i;
23184   limbo_die_node *node;
23185   comdat_type_node *ctnode;
23186   pubname_ref pub;
23187   dw_die_ref base_type;
23188
23189 #if ENABLE_ASSERT_CHECKING
23190   /* All the marks should already be clear.  */
23191   verify_marks_clear (comp_unit_die ());
23192   for (node = limbo_die_list; node; node = node->next)
23193     verify_marks_clear (node->die);
23194   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23195     verify_marks_clear (ctnode->root_die);
23196 #endif /* ENABLE_ASSERT_CHECKING */
23197
23198   /* Mark types that are used in global variables.  */
23199   premark_types_used_by_global_vars ();
23200
23201   /* Set the mark on nodes that are actually used.  */
23202   prune_unused_types_walk (comp_unit_die ());
23203   for (node = limbo_die_list; node; node = node->next)
23204     prune_unused_types_walk (node->die);
23205   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23206     {
23207       prune_unused_types_walk (ctnode->root_die);
23208       prune_unused_types_mark (ctnode->type_die, 1);
23209     }
23210
23211   /* Also set the mark on nodes referenced from the pubname_table.  Enumerators
23212      are unusual in that they are pubnames that are the children of pubtypes.
23213      They should only be marked via their parent DW_TAG_enumeration_type die,
23214      not as roots in themselves.  */
23215   FOR_EACH_VEC_ELT (*pubname_table, i, pub)
23216     if (pub->die->die_tag != DW_TAG_enumerator)
23217       prune_unused_types_mark (pub->die, 1);
23218   for (i = 0; base_types.iterate (i, &base_type); i++)
23219     prune_unused_types_mark (base_type, 1);
23220
23221   if (debug_str_hash)
23222     debug_str_hash->empty ();
23223   if (skeleton_debug_str_hash)
23224     skeleton_debug_str_hash->empty ();
23225   prune_unused_types_prune (comp_unit_die ());
23226   for (node = limbo_die_list; node; node = node->next)
23227     prune_unused_types_prune (node->die);
23228   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23229     prune_unused_types_prune (ctnode->root_die);
23230
23231   /* Leave the marks clear.  */
23232   prune_unmark_dies (comp_unit_die ());
23233   for (node = limbo_die_list; node; node = node->next)
23234     prune_unmark_dies (node->die);
23235   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23236     prune_unmark_dies (ctnode->root_die);
23237 }
23238
23239 /* Set the parameter to true if there are any relative pathnames in
23240    the file table.  */
23241 int
23242 file_table_relative_p (dwarf_file_data **slot, bool *p)
23243 {
23244   struct dwarf_file_data *d = *slot;
23245   if (!IS_ABSOLUTE_PATH (d->filename))
23246     {
23247       *p = true;
23248       return 0;
23249     }
23250   return 1;
23251 }
23252
23253 /* Helpers to manipulate hash table of comdat type units.  */
23254
23255 struct comdat_type_hasher : typed_noop_remove <comdat_type_node>
23256 {
23257   typedef comdat_type_node value_type;
23258   typedef comdat_type_node compare_type;
23259   static inline hashval_t hash (const value_type *);
23260   static inline bool equal (const value_type *, const compare_type *);
23261 };
23262
23263 inline hashval_t
23264 comdat_type_hasher::hash (const value_type *type_node)
23265 {
23266   hashval_t h;
23267   memcpy (&h, type_node->signature, sizeof (h));
23268   return h;
23269 }
23270
23271 inline bool
23272 comdat_type_hasher::equal (const value_type *type_node_1,
23273                            const compare_type *type_node_2)
23274 {
23275   return (! memcmp (type_node_1->signature, type_node_2->signature,
23276                     DWARF_TYPE_SIGNATURE_SIZE));
23277 }
23278
23279 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
23280    to the location it would have been added, should we know its
23281    DECL_ASSEMBLER_NAME when we added other attributes.  This will
23282    probably improve compactness of debug info, removing equivalent
23283    abbrevs, and hide any differences caused by deferring the
23284    computation of the assembler name, triggered by e.g. PCH.  */
23285
23286 static inline void
23287 move_linkage_attr (dw_die_ref die)
23288 {
23289   unsigned ix = vec_safe_length (die->die_attr);
23290   dw_attr_node linkage = (*die->die_attr)[ix - 1];
23291
23292   gcc_assert (linkage.dw_attr == DW_AT_linkage_name
23293               || linkage.dw_attr == DW_AT_MIPS_linkage_name);
23294
23295   while (--ix > 0)
23296     {
23297       dw_attr_node *prev = &(*die->die_attr)[ix - 1];
23298
23299       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
23300         break;
23301     }
23302
23303   if (ix != vec_safe_length (die->die_attr) - 1)
23304     {
23305       die->die_attr->pop ();
23306       die->die_attr->quick_insert (ix, linkage);
23307     }
23308 }
23309
23310 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
23311    referenced from typed stack ops and count how often they are used.  */
23312
23313 static void
23314 mark_base_types (dw_loc_descr_ref loc)
23315 {
23316   dw_die_ref base_type = NULL;
23317
23318   for (; loc; loc = loc->dw_loc_next)
23319     {
23320       switch (loc->dw_loc_opc)
23321         {
23322         case DW_OP_GNU_regval_type:
23323         case DW_OP_GNU_deref_type:
23324           base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
23325           break;
23326         case DW_OP_GNU_convert:
23327         case DW_OP_GNU_reinterpret:
23328           if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
23329             continue;
23330           /* FALLTHRU */
23331         case DW_OP_GNU_const_type:
23332           base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
23333           break;
23334         case DW_OP_GNU_entry_value:
23335           mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
23336           continue;
23337         default:
23338           continue;
23339         }
23340       gcc_assert (base_type->die_parent == comp_unit_die ());
23341       if (base_type->die_mark)
23342         base_type->die_mark++;
23343       else
23344         {
23345           base_types.safe_push (base_type);
23346           base_type->die_mark = 1;
23347         }
23348     }
23349 }
23350
23351 /* Comparison function for sorting marked base types.  */
23352
23353 static int
23354 base_type_cmp (const void *x, const void *y)
23355 {
23356   dw_die_ref dx = *(const dw_die_ref *) x;
23357   dw_die_ref dy = *(const dw_die_ref *) y;
23358   unsigned int byte_size1, byte_size2;
23359   unsigned int encoding1, encoding2;
23360   if (dx->die_mark > dy->die_mark)
23361     return -1;
23362   if (dx->die_mark < dy->die_mark)
23363     return 1;
23364   byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
23365   byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
23366   if (byte_size1 < byte_size2)
23367     return 1;
23368   if (byte_size1 > byte_size2)
23369     return -1;
23370   encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
23371   encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
23372   if (encoding1 < encoding2)
23373     return 1;
23374   if (encoding1 > encoding2)
23375     return -1;
23376   return 0;
23377 }
23378
23379 /* Move base types marked by mark_base_types as early as possible
23380    in the CU, sorted by decreasing usage count both to make the
23381    uleb128 references as small as possible and to make sure they
23382    will have die_offset already computed by calc_die_sizes when
23383    sizes of typed stack loc ops is computed.  */
23384
23385 static void
23386 move_marked_base_types (void)
23387 {
23388   unsigned int i;
23389   dw_die_ref base_type, die, c;
23390
23391   if (base_types.is_empty ())
23392     return;
23393
23394   /* Sort by decreasing usage count, they will be added again in that
23395      order later on.  */
23396   base_types.qsort (base_type_cmp);
23397   die = comp_unit_die ();
23398   c = die->die_child;
23399   do
23400     {
23401       dw_die_ref prev = c;
23402       c = c->die_sib;
23403       while (c->die_mark)
23404         {
23405           remove_child_with_prev (c, prev);
23406           /* As base types got marked, there must be at least
23407              one node other than DW_TAG_base_type.  */
23408           gcc_assert (c != c->die_sib);
23409           c = c->die_sib;
23410         }
23411     }
23412   while (c != die->die_child);
23413   gcc_assert (die->die_child);
23414   c = die->die_child;
23415   for (i = 0; base_types.iterate (i, &base_type); i++)
23416     {
23417       base_type->die_mark = 0;
23418       base_type->die_sib = c->die_sib;
23419       c->die_sib = base_type;
23420       c = base_type;
23421     }
23422 }
23423
23424 /* Helper function for resolve_addr, attempt to resolve
23425    one CONST_STRING, return true if successful.  Similarly verify that
23426    SYMBOL_REFs refer to variables emitted in the current CU.  */
23427
23428 static bool
23429 resolve_one_addr (rtx *addr)
23430 {
23431   rtx rtl = *addr;
23432
23433   if (GET_CODE (rtl) == CONST_STRING)
23434     {
23435       size_t len = strlen (XSTR (rtl, 0)) + 1;
23436       tree t = build_string (len, XSTR (rtl, 0));
23437       tree tlen = size_int (len - 1);
23438       TREE_TYPE (t)
23439         = build_array_type (char_type_node, build_index_type (tlen));
23440       rtl = lookup_constant_def (t);
23441       if (!rtl || !MEM_P (rtl))
23442         return false;
23443       rtl = XEXP (rtl, 0);
23444       if (GET_CODE (rtl) == SYMBOL_REF
23445           && SYMBOL_REF_DECL (rtl)
23446           && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23447         return false;
23448       vec_safe_push (used_rtx_array, rtl);
23449       *addr = rtl;
23450       return true;
23451     }
23452
23453   if (GET_CODE (rtl) == SYMBOL_REF
23454       && SYMBOL_REF_DECL (rtl))
23455     {
23456       if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
23457         {
23458           if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
23459             return false;
23460         }
23461       else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23462         return false;
23463     }
23464
23465   if (GET_CODE (rtl) == CONST)
23466     {
23467       subrtx_ptr_iterator::array_type array;
23468       FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
23469         if (!resolve_one_addr (*iter))
23470           return false;
23471     }
23472
23473   return true;
23474 }
23475
23476 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
23477    if possible, and create DW_TAG_dwarf_procedure that can be referenced
23478    from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet.  */
23479
23480 static rtx
23481 string_cst_pool_decl (tree t)
23482 {
23483   rtx rtl = output_constant_def (t, 1);
23484   unsigned char *array;
23485   dw_loc_descr_ref l;
23486   tree decl;
23487   size_t len;
23488   dw_die_ref ref;
23489
23490   if (!rtl || !MEM_P (rtl))
23491     return NULL_RTX;
23492   rtl = XEXP (rtl, 0);
23493   if (GET_CODE (rtl) != SYMBOL_REF
23494       || SYMBOL_REF_DECL (rtl) == NULL_TREE)
23495     return NULL_RTX;
23496
23497   decl = SYMBOL_REF_DECL (rtl);
23498   if (!lookup_decl_die (decl))
23499     {
23500       len = TREE_STRING_LENGTH (t);
23501       vec_safe_push (used_rtx_array, rtl);
23502       ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
23503       array = ggc_vec_alloc<unsigned char> (len);
23504       memcpy (array, TREE_STRING_POINTER (t), len);
23505       l = new_loc_descr (DW_OP_implicit_value, len, 0);
23506       l->dw_loc_oprnd2.val_class = dw_val_class_vec;
23507       l->dw_loc_oprnd2.v.val_vec.length = len;
23508       l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
23509       l->dw_loc_oprnd2.v.val_vec.array = array;
23510       add_AT_loc (ref, DW_AT_location, l);
23511       equate_decl_number_to_die (decl, ref);
23512     }
23513   return rtl;
23514 }
23515
23516 /* Helper function of resolve_addr_in_expr.  LOC is
23517    a DW_OP_addr followed by DW_OP_stack_value, either at the start
23518    of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
23519    resolved.  Replace it (both DW_OP_addr and DW_OP_stack_value)
23520    with DW_OP_GNU_implicit_pointer if possible
23521    and return true, if unsuccessful, return false.  */
23522
23523 static bool
23524 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
23525 {
23526   rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
23527   HOST_WIDE_INT offset = 0;
23528   dw_die_ref ref = NULL;
23529   tree decl;
23530
23531   if (GET_CODE (rtl) == CONST
23532       && GET_CODE (XEXP (rtl, 0)) == PLUS
23533       && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
23534     {
23535       offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
23536       rtl = XEXP (XEXP (rtl, 0), 0);
23537     }
23538   if (GET_CODE (rtl) == CONST_STRING)
23539     {
23540       size_t len = strlen (XSTR (rtl, 0)) + 1;
23541       tree t = build_string (len, XSTR (rtl, 0));
23542       tree tlen = size_int (len - 1);
23543
23544       TREE_TYPE (t)
23545         = build_array_type (char_type_node, build_index_type (tlen));
23546       rtl = string_cst_pool_decl (t);
23547       if (!rtl)
23548         return false;
23549     }
23550   if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
23551     {
23552       decl = SYMBOL_REF_DECL (rtl);
23553       if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
23554         {
23555           ref = lookup_decl_die (decl);
23556           if (ref && (get_AT (ref, DW_AT_location)
23557                       || get_AT (ref, DW_AT_const_value)))
23558             {
23559               loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
23560               loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23561               loc->dw_loc_oprnd1.val_entry = NULL;
23562               loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23563               loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23564               loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23565               loc->dw_loc_oprnd2.v.val_int = offset;
23566               return true;
23567             }
23568         }
23569     }
23570   return false;
23571 }
23572
23573 /* Helper function for resolve_addr, handle one location
23574    expression, return false if at least one CONST_STRING or SYMBOL_REF in
23575    the location list couldn't be resolved.  */
23576
23577 static bool
23578 resolve_addr_in_expr (dw_loc_descr_ref loc)
23579 {
23580   dw_loc_descr_ref keep = NULL;
23581   for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
23582     switch (loc->dw_loc_opc)
23583       {
23584       case DW_OP_addr:
23585         if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
23586           {
23587             if ((prev == NULL
23588                  || prev->dw_loc_opc == DW_OP_piece
23589                  || prev->dw_loc_opc == DW_OP_bit_piece)
23590                 && loc->dw_loc_next
23591                 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
23592                 && !dwarf_strict
23593                 && optimize_one_addr_into_implicit_ptr (loc))
23594               break;
23595             return false;
23596           }
23597         break;
23598       case DW_OP_GNU_addr_index:
23599       case DW_OP_GNU_const_index:
23600         if (loc->dw_loc_opc == DW_OP_GNU_addr_index
23601             || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
23602           {
23603             rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
23604             if (!resolve_one_addr (&rtl))
23605               return false;
23606             remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
23607             loc->dw_loc_oprnd1.val_entry =
23608                 add_addr_table_entry (rtl, ate_kind_rtx);
23609           }
23610         break;
23611       case DW_OP_const4u:
23612       case DW_OP_const8u:
23613         if (loc->dtprel
23614             && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
23615           return false;
23616         break;
23617       case DW_OP_plus_uconst:
23618         if (size_of_loc_descr (loc)
23619             > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
23620               + 1
23621             && loc->dw_loc_oprnd1.v.val_unsigned > 0)
23622           {
23623             dw_loc_descr_ref repl
23624               = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
23625             add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
23626             add_loc_descr (&repl, loc->dw_loc_next);
23627             *loc = *repl;
23628           }
23629         break;
23630       case DW_OP_implicit_value:
23631         if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
23632             && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
23633           return false;
23634         break;
23635       case DW_OP_GNU_implicit_pointer:
23636       case DW_OP_GNU_parameter_ref:
23637         if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
23638           {
23639             dw_die_ref ref
23640               = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
23641             if (ref == NULL)
23642               return false;
23643             loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23644             loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23645             loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23646           }
23647         break;
23648       case DW_OP_GNU_const_type:
23649       case DW_OP_GNU_regval_type:
23650       case DW_OP_GNU_deref_type:
23651       case DW_OP_GNU_convert:
23652       case DW_OP_GNU_reinterpret:
23653         while (loc->dw_loc_next
23654                && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
23655           {
23656             dw_die_ref base1, base2;
23657             unsigned enc1, enc2, size1, size2;
23658             if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23659                 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23660               base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
23661             else if (loc->dw_loc_oprnd1.val_class
23662                      == dw_val_class_unsigned_const)
23663               break;
23664             else
23665               base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
23666             if (loc->dw_loc_next->dw_loc_oprnd1.val_class
23667                 == dw_val_class_unsigned_const)
23668               break;
23669             base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
23670             gcc_assert (base1->die_tag == DW_TAG_base_type
23671                         && base2->die_tag == DW_TAG_base_type);
23672             enc1 = get_AT_unsigned (base1, DW_AT_encoding);
23673             enc2 = get_AT_unsigned (base2, DW_AT_encoding);
23674             size1 = get_AT_unsigned (base1, DW_AT_byte_size);
23675             size2 = get_AT_unsigned (base2, DW_AT_byte_size);
23676             if (size1 == size2
23677                 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
23678                      && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
23679                      && loc != keep)
23680                     || enc1 == enc2))
23681               {
23682                 /* Optimize away next DW_OP_GNU_convert after
23683                    adjusting LOC's base type die reference.  */
23684                 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23685                     || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23686                   loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
23687                 else
23688                   loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
23689                 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23690                 continue;
23691               }
23692             /* Don't change integer DW_OP_GNU_convert after e.g. floating
23693                point typed stack entry.  */
23694             else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
23695               keep = loc->dw_loc_next;
23696             break;
23697           }
23698         break;
23699       default:
23700         break;
23701       }
23702   return true;
23703 }
23704
23705 /* Helper function of resolve_addr.  DIE had DW_AT_location of
23706    DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
23707    and DW_OP_addr couldn't be resolved.  resolve_addr has already
23708    removed the DW_AT_location attribute.  This function attempts to
23709    add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
23710    to it or DW_AT_const_value attribute, if possible.  */
23711
23712 static void
23713 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
23714 {
23715   if (TREE_CODE (decl) != VAR_DECL
23716       || lookup_decl_die (decl) != die
23717       || DECL_EXTERNAL (decl)
23718       || !TREE_STATIC (decl)
23719       || DECL_INITIAL (decl) == NULL_TREE
23720       || DECL_P (DECL_INITIAL (decl))
23721       || get_AT (die, DW_AT_const_value))
23722     return;
23723
23724   tree init = DECL_INITIAL (decl);
23725   HOST_WIDE_INT offset = 0;
23726   /* For variables that have been optimized away and thus
23727      don't have a memory location, see if we can emit
23728      DW_AT_const_value instead.  */
23729   if (tree_add_const_value_attribute (die, init))
23730     return;
23731   if (dwarf_strict)
23732     return;
23733   /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
23734      and ADDR_EXPR refers to a decl that has DW_AT_location or
23735      DW_AT_const_value (but isn't addressable, otherwise
23736      resolving the original DW_OP_addr wouldn't fail), see if
23737      we can add DW_OP_GNU_implicit_pointer.  */
23738   STRIP_NOPS (init);
23739   if (TREE_CODE (init) == POINTER_PLUS_EXPR
23740       && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
23741     {
23742       offset = tree_to_shwi (TREE_OPERAND (init, 1));
23743       init = TREE_OPERAND (init, 0);
23744       STRIP_NOPS (init);
23745     }
23746   if (TREE_CODE (init) != ADDR_EXPR)
23747     return;
23748   if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
23749        && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
23750       || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
23751           && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
23752           && TREE_OPERAND (init, 0) != decl))
23753     {
23754       dw_die_ref ref;
23755       dw_loc_descr_ref l;
23756
23757       if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
23758         {
23759           rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
23760           if (!rtl)
23761             return;
23762           decl = SYMBOL_REF_DECL (rtl);
23763         }
23764       else
23765         decl = TREE_OPERAND (init, 0);
23766       ref = lookup_decl_die (decl);
23767       if (ref == NULL
23768           || (!get_AT (ref, DW_AT_location)
23769               && !get_AT (ref, DW_AT_const_value)))
23770         return;
23771       l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
23772       l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23773       l->dw_loc_oprnd1.v.val_die_ref.die = ref;
23774       l->dw_loc_oprnd1.v.val_die_ref.external = 0;
23775       add_AT_loc (die, DW_AT_location, l);
23776     }
23777 }
23778
23779 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
23780    an address in .rodata section if the string literal is emitted there,
23781    or remove the containing location list or replace DW_AT_const_value
23782    with DW_AT_location and empty location expression, if it isn't found
23783    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
23784    to something that has been emitted in the current CU.  */
23785
23786 static void
23787 resolve_addr (dw_die_ref die)
23788 {
23789   dw_die_ref c;
23790   dw_attr_ref a;
23791   dw_loc_list_ref *curr, *start, loc;
23792   unsigned ix;
23793
23794   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23795     switch (AT_class (a))
23796       {
23797       case dw_val_class_loc_list:
23798         start = curr = AT_loc_list_ptr (a);
23799         loc = *curr;
23800         gcc_assert (loc);
23801         /* The same list can be referenced more than once.  See if we have
23802            already recorded the result from a previous pass.  */
23803         if (loc->replaced)
23804           *curr = loc->dw_loc_next;
23805         else if (!loc->resolved_addr)
23806           {
23807             /* As things stand, we do not expect or allow one die to
23808                reference a suffix of another die's location list chain.
23809                References must be identical or completely separate.
23810                There is therefore no need to cache the result of this
23811                pass on any list other than the first; doing so
23812                would lead to unnecessary writes.  */
23813             while (*curr)
23814               {
23815                 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
23816                 if (!resolve_addr_in_expr ((*curr)->expr))
23817                   {
23818                     dw_loc_list_ref next = (*curr)->dw_loc_next;
23819                     dw_loc_descr_ref l = (*curr)->expr;
23820
23821                     if (next && (*curr)->ll_symbol)
23822                       {
23823                         gcc_assert (!next->ll_symbol);
23824                         next->ll_symbol = (*curr)->ll_symbol;
23825                       }
23826                     if (dwarf_split_debug_info)
23827                       remove_loc_list_addr_table_entries (l);
23828                     *curr = next;
23829                   }
23830                 else
23831                   {
23832                     mark_base_types ((*curr)->expr);
23833                     curr = &(*curr)->dw_loc_next;
23834                   }
23835               }
23836             if (loc == *start)
23837               loc->resolved_addr = 1;
23838             else
23839               {
23840                 loc->replaced = 1;
23841                 loc->dw_loc_next = *start;
23842               }
23843           }
23844         if (!*start)
23845           {
23846             remove_AT (die, a->dw_attr);
23847             ix--;
23848           }
23849         break;
23850       case dw_val_class_loc:
23851         {
23852           dw_loc_descr_ref l = AT_loc (a);
23853           /* For -gdwarf-2 don't attempt to optimize
23854              DW_AT_data_member_location containing
23855              DW_OP_plus_uconst - older consumers might
23856              rely on it being that op instead of a more complex,
23857              but shorter, location description.  */
23858           if ((dwarf_version > 2
23859                || a->dw_attr != DW_AT_data_member_location
23860                || l == NULL
23861                || l->dw_loc_opc != DW_OP_plus_uconst
23862                || l->dw_loc_next != NULL)
23863               && !resolve_addr_in_expr (l))
23864             {
23865               if (dwarf_split_debug_info)
23866                 remove_loc_list_addr_table_entries (l);
23867               if (l != NULL
23868                   && l->dw_loc_next == NULL
23869                   && l->dw_loc_opc == DW_OP_addr
23870                   && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
23871                   && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
23872                   && a->dw_attr == DW_AT_location)
23873                 {
23874                   tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
23875                   remove_AT (die, a->dw_attr);
23876                   ix--;
23877                   optimize_location_into_implicit_ptr (die, decl);
23878                   break;
23879                 }
23880               remove_AT (die, a->dw_attr);
23881               ix--;
23882             }
23883           else
23884             mark_base_types (l);
23885         }
23886         break;
23887       case dw_val_class_addr:
23888         if (a->dw_attr == DW_AT_const_value
23889             && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
23890           {
23891             if (AT_index (a) != NOT_INDEXED)
23892               remove_addr_table_entry (a->dw_attr_val.val_entry);
23893             remove_AT (die, a->dw_attr);
23894             ix--;
23895           }
23896         if (die->die_tag == DW_TAG_GNU_call_site
23897             && a->dw_attr == DW_AT_abstract_origin)
23898           {
23899             tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
23900             dw_die_ref tdie = lookup_decl_die (tdecl);
23901             if (tdie == NULL
23902                 && DECL_EXTERNAL (tdecl)
23903                 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
23904               {
23905                 force_decl_die (tdecl);
23906                 tdie = lookup_decl_die (tdecl);
23907               }
23908             if (tdie)
23909               {
23910                 a->dw_attr_val.val_class = dw_val_class_die_ref;
23911                 a->dw_attr_val.v.val_die_ref.die = tdie;
23912                 a->dw_attr_val.v.val_die_ref.external = 0;
23913               }
23914             else
23915               {
23916                 if (AT_index (a) != NOT_INDEXED)
23917                   remove_addr_table_entry (a->dw_attr_val.val_entry);
23918                 remove_AT (die, a->dw_attr);
23919                 ix--;
23920               }
23921           }
23922         break;
23923       default:
23924         break;
23925       }
23926
23927   FOR_EACH_CHILD (die, c, resolve_addr (c));
23928 }
23929 \f
23930 /* Helper routines for optimize_location_lists.
23931    This pass tries to share identical local lists in .debug_loc
23932    section.  */
23933
23934 /* Iteratively hash operands of LOC opcode into HSTATE.  */
23935
23936 static void
23937 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
23938 {
23939   dw_val_ref val1 = &loc->dw_loc_oprnd1;
23940   dw_val_ref val2 = &loc->dw_loc_oprnd2;
23941
23942   switch (loc->dw_loc_opc)
23943     {
23944     case DW_OP_const4u:
23945     case DW_OP_const8u:
23946       if (loc->dtprel)
23947         goto hash_addr;
23948       /* FALLTHRU */
23949     case DW_OP_const1u:
23950     case DW_OP_const1s:
23951     case DW_OP_const2u:
23952     case DW_OP_const2s:
23953     case DW_OP_const4s:
23954     case DW_OP_const8s:
23955     case DW_OP_constu:
23956     case DW_OP_consts:
23957     case DW_OP_pick:
23958     case DW_OP_plus_uconst:
23959     case DW_OP_breg0:
23960     case DW_OP_breg1:
23961     case DW_OP_breg2:
23962     case DW_OP_breg3:
23963     case DW_OP_breg4:
23964     case DW_OP_breg5:
23965     case DW_OP_breg6:
23966     case DW_OP_breg7:
23967     case DW_OP_breg8:
23968     case DW_OP_breg9:
23969     case DW_OP_breg10:
23970     case DW_OP_breg11:
23971     case DW_OP_breg12:
23972     case DW_OP_breg13:
23973     case DW_OP_breg14:
23974     case DW_OP_breg15:
23975     case DW_OP_breg16:
23976     case DW_OP_breg17:
23977     case DW_OP_breg18:
23978     case DW_OP_breg19:
23979     case DW_OP_breg20:
23980     case DW_OP_breg21:
23981     case DW_OP_breg22:
23982     case DW_OP_breg23:
23983     case DW_OP_breg24:
23984     case DW_OP_breg25:
23985     case DW_OP_breg26:
23986     case DW_OP_breg27:
23987     case DW_OP_breg28:
23988     case DW_OP_breg29:
23989     case DW_OP_breg30:
23990     case DW_OP_breg31:
23991     case DW_OP_regx:
23992     case DW_OP_fbreg:
23993     case DW_OP_piece:
23994     case DW_OP_deref_size:
23995     case DW_OP_xderef_size:
23996       hstate.add_object (val1->v.val_int);
23997       break;
23998     case DW_OP_skip:
23999     case DW_OP_bra:
24000       {
24001         int offset;
24002
24003         gcc_assert (val1->val_class == dw_val_class_loc);
24004         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
24005         hstate.add_object (offset);
24006       }
24007       break;
24008     case DW_OP_implicit_value:
24009       hstate.add_object (val1->v.val_unsigned);
24010       switch (val2->val_class)
24011         {
24012         case dw_val_class_const:
24013           hstate.add_object (val2->v.val_int);
24014           break;
24015         case dw_val_class_vec:
24016           {
24017             unsigned int elt_size = val2->v.val_vec.elt_size;
24018             unsigned int len = val2->v.val_vec.length;
24019
24020             hstate.add_int (elt_size);
24021             hstate.add_int (len);
24022             hstate.add (val2->v.val_vec.array, len * elt_size);
24023           }
24024           break;
24025         case dw_val_class_const_double:
24026           hstate.add_object (val2->v.val_double.low);
24027           hstate.add_object (val2->v.val_double.high);
24028           break;
24029         case dw_val_class_wide_int:
24030           hstate.add (val2->v.val_wide->get_val (),
24031                       get_full_len (*val2->v.val_wide)
24032                       * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
24033           break;
24034         case dw_val_class_addr: 
24035           inchash::add_rtx (val2->v.val_addr, hstate);
24036           break;
24037         default:
24038           gcc_unreachable ();
24039         }
24040       break;
24041     case DW_OP_bregx:
24042     case DW_OP_bit_piece:
24043       hstate.add_object (val1->v.val_int);
24044       hstate.add_object (val2->v.val_int);
24045       break;
24046     case DW_OP_addr:
24047     hash_addr:
24048       if (loc->dtprel)
24049         {
24050           unsigned char dtprel = 0xd1;
24051           hstate.add_object (dtprel);
24052         }
24053       inchash::add_rtx (val1->v.val_addr, hstate);
24054       break;
24055     case DW_OP_GNU_addr_index:
24056     case DW_OP_GNU_const_index:
24057       {
24058         if (loc->dtprel)
24059           {
24060             unsigned char dtprel = 0xd1;
24061             hstate.add_object (dtprel);
24062           }
24063         inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
24064       }
24065       break;
24066     case DW_OP_GNU_implicit_pointer:
24067       hstate.add_int (val2->v.val_int);
24068       break;
24069     case DW_OP_GNU_entry_value:
24070       hstate.add_object (val1->v.val_loc);
24071       break;
24072     case DW_OP_GNU_regval_type:
24073     case DW_OP_GNU_deref_type:
24074       {
24075         unsigned int byte_size
24076           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
24077         unsigned int encoding
24078           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
24079         hstate.add_object (val1->v.val_int);
24080         hstate.add_object (byte_size);
24081         hstate.add_object (encoding);
24082       }
24083       break;
24084     case DW_OP_GNU_convert:
24085     case DW_OP_GNU_reinterpret:
24086       if (val1->val_class == dw_val_class_unsigned_const)
24087         {
24088           hstate.add_object (val1->v.val_unsigned);
24089           break;
24090         }
24091       /* FALLTHRU */
24092     case DW_OP_GNU_const_type:
24093       {
24094         unsigned int byte_size
24095           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
24096         unsigned int encoding
24097           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
24098         hstate.add_object (byte_size);
24099         hstate.add_object (encoding);
24100         if (loc->dw_loc_opc != DW_OP_GNU_const_type)
24101           break;
24102         hstate.add_object (val2->val_class);
24103         switch (val2->val_class)
24104           {
24105           case dw_val_class_const:
24106             hstate.add_object (val2->v.val_int);
24107             break;
24108           case dw_val_class_vec:
24109             {
24110               unsigned int elt_size = val2->v.val_vec.elt_size;
24111               unsigned int len = val2->v.val_vec.length;
24112
24113               hstate.add_object (elt_size);
24114               hstate.add_object (len);
24115               hstate.add (val2->v.val_vec.array, len * elt_size);
24116             }
24117             break;
24118           case dw_val_class_const_double:
24119             hstate.add_object (val2->v.val_double.low);
24120             hstate.add_object (val2->v.val_double.high);
24121             break;
24122           case dw_val_class_wide_int:
24123             hstate.add (val2->v.val_wide->get_val (),
24124                         get_full_len (*val2->v.val_wide)
24125                         * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
24126             break;
24127           default:
24128             gcc_unreachable ();
24129           }
24130       }
24131       break;
24132
24133     default:
24134       /* Other codes have no operands.  */
24135       break;
24136     }
24137 }
24138
24139 /* Iteratively hash the whole DWARF location expression LOC into HSTATE.  */
24140
24141 static inline void
24142 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
24143 {
24144   dw_loc_descr_ref l;
24145   bool sizes_computed = false;
24146   /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed.  */
24147   size_of_locs (loc);
24148
24149   for (l = loc; l != NULL; l = l->dw_loc_next)
24150     {
24151       enum dwarf_location_atom opc = l->dw_loc_opc;
24152       hstate.add_object (opc);
24153       if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
24154         {
24155           size_of_locs (loc);
24156           sizes_computed = true;
24157         }
24158       hash_loc_operands (l, hstate);
24159     }
24160 }
24161
24162 /* Compute hash of the whole location list LIST_HEAD.  */
24163
24164 static inline void
24165 hash_loc_list (dw_loc_list_ref list_head)
24166 {
24167   dw_loc_list_ref curr = list_head;
24168   inchash::hash hstate;
24169
24170   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
24171     {
24172       hstate.add (curr->begin, strlen (curr->begin) + 1);
24173       hstate.add (curr->end, strlen (curr->end) + 1);
24174       if (curr->section)
24175         hstate.add (curr->section, strlen (curr->section) + 1);
24176       hash_locs (curr->expr, hstate);
24177     }
24178   list_head->hash = hstate.end ();
24179 }
24180
24181 /* Return true if X and Y opcodes have the same operands.  */
24182
24183 static inline bool
24184 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
24185 {
24186   dw_val_ref valx1 = &x->dw_loc_oprnd1;
24187   dw_val_ref valx2 = &x->dw_loc_oprnd2;
24188   dw_val_ref valy1 = &y->dw_loc_oprnd1;
24189   dw_val_ref valy2 = &y->dw_loc_oprnd2;
24190
24191   switch (x->dw_loc_opc)
24192     {
24193     case DW_OP_const4u:
24194     case DW_OP_const8u:
24195       if (x->dtprel)
24196         goto hash_addr;
24197       /* FALLTHRU */
24198     case DW_OP_const1u:
24199     case DW_OP_const1s:
24200     case DW_OP_const2u:
24201     case DW_OP_const2s:
24202     case DW_OP_const4s:
24203     case DW_OP_const8s:
24204     case DW_OP_constu:
24205     case DW_OP_consts:
24206     case DW_OP_pick:
24207     case DW_OP_plus_uconst:
24208     case DW_OP_breg0:
24209     case DW_OP_breg1:
24210     case DW_OP_breg2:
24211     case DW_OP_breg3:
24212     case DW_OP_breg4:
24213     case DW_OP_breg5:
24214     case DW_OP_breg6:
24215     case DW_OP_breg7:
24216     case DW_OP_breg8:
24217     case DW_OP_breg9:
24218     case DW_OP_breg10:
24219     case DW_OP_breg11:
24220     case DW_OP_breg12:
24221     case DW_OP_breg13:
24222     case DW_OP_breg14:
24223     case DW_OP_breg15:
24224     case DW_OP_breg16:
24225     case DW_OP_breg17:
24226     case DW_OP_breg18:
24227     case DW_OP_breg19:
24228     case DW_OP_breg20:
24229     case DW_OP_breg21:
24230     case DW_OP_breg22:
24231     case DW_OP_breg23:
24232     case DW_OP_breg24:
24233     case DW_OP_breg25:
24234     case DW_OP_breg26:
24235     case DW_OP_breg27:
24236     case DW_OP_breg28:
24237     case DW_OP_breg29:
24238     case DW_OP_breg30:
24239     case DW_OP_breg31:
24240     case DW_OP_regx:
24241     case DW_OP_fbreg:
24242     case DW_OP_piece:
24243     case DW_OP_deref_size:
24244     case DW_OP_xderef_size:
24245       return valx1->v.val_int == valy1->v.val_int;
24246     case DW_OP_skip:
24247     case DW_OP_bra:
24248       /* If splitting debug info, the use of DW_OP_GNU_addr_index
24249         can cause irrelevant differences in dw_loc_addr.  */
24250       gcc_assert (valx1->val_class == dw_val_class_loc
24251                   && valy1->val_class == dw_val_class_loc
24252                   && (dwarf_split_debug_info
24253                       || x->dw_loc_addr == y->dw_loc_addr));
24254       return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
24255     case DW_OP_implicit_value:
24256       if (valx1->v.val_unsigned != valy1->v.val_unsigned
24257           || valx2->val_class != valy2->val_class)
24258         return false;
24259       switch (valx2->val_class)
24260         {
24261         case dw_val_class_const:
24262           return valx2->v.val_int == valy2->v.val_int;
24263         case dw_val_class_vec:
24264           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24265                  && valx2->v.val_vec.length == valy2->v.val_vec.length
24266                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24267                             valx2->v.val_vec.elt_size
24268                             * valx2->v.val_vec.length) == 0;
24269         case dw_val_class_const_double:
24270           return valx2->v.val_double.low == valy2->v.val_double.low
24271                  && valx2->v.val_double.high == valy2->v.val_double.high;
24272         case dw_val_class_wide_int:
24273           return *valx2->v.val_wide == *valy2->v.val_wide;
24274         case dw_val_class_addr:
24275           return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
24276         default:
24277           gcc_unreachable ();
24278         }
24279     case DW_OP_bregx:
24280     case DW_OP_bit_piece:
24281       return valx1->v.val_int == valy1->v.val_int
24282              && valx2->v.val_int == valy2->v.val_int;
24283     case DW_OP_addr:
24284     hash_addr:
24285       return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
24286     case DW_OP_GNU_addr_index:
24287     case DW_OP_GNU_const_index:
24288       {
24289         rtx ax1 = valx1->val_entry->addr.rtl;
24290         rtx ay1 = valy1->val_entry->addr.rtl;
24291         return rtx_equal_p (ax1, ay1);
24292       }
24293     case DW_OP_GNU_implicit_pointer:
24294       return valx1->val_class == dw_val_class_die_ref
24295              && valx1->val_class == valy1->val_class
24296              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
24297              && valx2->v.val_int == valy2->v.val_int;
24298     case DW_OP_GNU_entry_value:
24299       return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
24300     case DW_OP_GNU_const_type:
24301       if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
24302           || valx2->val_class != valy2->val_class)
24303         return false;
24304       switch (valx2->val_class)
24305         {
24306         case dw_val_class_const:
24307           return valx2->v.val_int == valy2->v.val_int;
24308         case dw_val_class_vec:
24309           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24310                  && valx2->v.val_vec.length == valy2->v.val_vec.length
24311                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24312                             valx2->v.val_vec.elt_size
24313                             * valx2->v.val_vec.length) == 0;
24314         case dw_val_class_const_double:
24315           return valx2->v.val_double.low == valy2->v.val_double.low
24316                  && valx2->v.val_double.high == valy2->v.val_double.high;
24317         case dw_val_class_wide_int:
24318           return *valx2->v.val_wide == *valy2->v.val_wide;
24319         default:
24320           gcc_unreachable ();
24321         }
24322     case DW_OP_GNU_regval_type:
24323     case DW_OP_GNU_deref_type:
24324       return valx1->v.val_int == valy1->v.val_int
24325              && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
24326     case DW_OP_GNU_convert:
24327     case DW_OP_GNU_reinterpret:
24328       if (valx1->val_class != valy1->val_class)
24329         return false;
24330       if (valx1->val_class == dw_val_class_unsigned_const)
24331         return valx1->v.val_unsigned == valy1->v.val_unsigned;
24332       return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24333     case DW_OP_GNU_parameter_ref:
24334       return valx1->val_class == dw_val_class_die_ref
24335              && valx1->val_class == valy1->val_class
24336              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24337     default:
24338       /* Other codes have no operands.  */
24339       return true;
24340     }
24341 }
24342
24343 /* Return true if DWARF location expressions X and Y are the same.  */
24344
24345 static inline bool
24346 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
24347 {
24348   for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
24349     if (x->dw_loc_opc != y->dw_loc_opc
24350         || x->dtprel != y->dtprel
24351         || !compare_loc_operands (x, y))
24352       break;
24353   return x == NULL && y == NULL;
24354 }
24355
24356 /* Hashtable helpers.  */
24357
24358 struct loc_list_hasher : typed_noop_remove <dw_loc_list_struct>
24359 {
24360   typedef dw_loc_list_struct value_type;
24361   typedef dw_loc_list_struct compare_type;
24362   static inline hashval_t hash (const value_type *);
24363   static inline bool equal (const value_type *, const compare_type *);
24364 };
24365
24366 /* Return precomputed hash of location list X.  */
24367
24368 inline hashval_t
24369 loc_list_hasher::hash (const value_type *x)
24370 {
24371   return x->hash;
24372 }
24373
24374 /* Return true if location lists A and B are the same.  */
24375
24376 inline bool
24377 loc_list_hasher::equal (const value_type *a, const compare_type *b)
24378 {
24379   if (a == b)
24380     return 1;
24381   if (a->hash != b->hash)
24382     return 0;
24383   for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
24384     if (strcmp (a->begin, b->begin) != 0
24385         || strcmp (a->end, b->end) != 0
24386         || (a->section == NULL) != (b->section == NULL)
24387         || (a->section && strcmp (a->section, b->section) != 0)
24388         || !compare_locs (a->expr, b->expr))
24389       break;
24390   return a == NULL && b == NULL;
24391 }
24392
24393 typedef hash_table<loc_list_hasher> loc_list_hash_type;
24394
24395
24396 /* Recursively optimize location lists referenced from DIE
24397    children and share them whenever possible.  */
24398
24399 static void
24400 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
24401 {
24402   dw_die_ref c;
24403   dw_attr_ref a;
24404   unsigned ix;
24405   dw_loc_list_struct **slot;
24406
24407   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24408     if (AT_class (a) == dw_val_class_loc_list)
24409       {
24410         dw_loc_list_ref list = AT_loc_list (a);
24411         /* TODO: perform some optimizations here, before hashing
24412            it and storing into the hash table.  */
24413         hash_loc_list (list);
24414         slot = htab->find_slot_with_hash (list, list->hash, INSERT);
24415         if (*slot == NULL)
24416           *slot = list;
24417         else
24418           a->dw_attr_val.v.val_loc_list = *slot;
24419       }
24420
24421   FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
24422 }
24423
24424
24425 /* Recursively assign each location list a unique index into the debug_addr
24426    section.  */
24427
24428 static void
24429 index_location_lists (dw_die_ref die)
24430 {
24431   dw_die_ref c;
24432   dw_attr_ref a;
24433   unsigned ix;
24434
24435   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24436     if (AT_class (a) == dw_val_class_loc_list)
24437       {
24438         dw_loc_list_ref list = AT_loc_list (a);
24439         dw_loc_list_ref curr;
24440         for (curr = list; curr != NULL; curr = curr->dw_loc_next)
24441           {
24442             /* Don't index an entry that has already been indexed
24443                or won't be output.  */
24444             if (curr->begin_entry != NULL
24445                 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
24446               continue;
24447
24448             curr->begin_entry
24449                 = add_addr_table_entry (xstrdup (curr->begin),
24450                                         ate_kind_label);
24451           }
24452       }
24453
24454   FOR_EACH_CHILD (die, c, index_location_lists (c));
24455 }
24456
24457 /* Optimize location lists referenced from DIE
24458    children and share them whenever possible.  */
24459
24460 static void
24461 optimize_location_lists (dw_die_ref die)
24462 {
24463   loc_list_hash_type htab (500);
24464   optimize_location_lists_1 (die, &htab);
24465 }
24466 \f
24467 /* Output stuff that dwarf requires at the end of every file,
24468    and generate the DWARF-2 debugging info.  */
24469
24470 static void
24471 dwarf2out_finish (const char *filename)
24472 {
24473   limbo_die_node *node, *next_node;
24474   comdat_type_node *ctnode;
24475   unsigned int i;
24476   dw_die_ref main_comp_unit_die;
24477
24478   /* PCH might result in DW_AT_producer string being restored from the
24479      header compilation, so always fill it with empty string initially
24480      and overwrite only here.  */
24481   dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
24482   producer_string = gen_producer_string ();
24483   producer->dw_attr_val.v.val_str->refcount--;
24484   producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
24485
24486   gen_scheduled_generic_parms_dies ();
24487   gen_remaining_tmpl_value_param_die_attribute ();
24488
24489   /* Add the name for the main input file now.  We delayed this from
24490      dwarf2out_init to avoid complications with PCH.  */
24491   add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
24492   if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
24493     add_comp_dir_attribute (comp_unit_die ());
24494   else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
24495     {
24496       bool p = false;
24497       file_table->traverse<bool *, file_table_relative_p> (&p);
24498       if (p)
24499         add_comp_dir_attribute (comp_unit_die ());
24500     }
24501
24502   if (deferred_locations_list)
24503     for (i = 0; i < deferred_locations_list->length (); i++)
24504       {
24505         add_location_or_const_value_attribute (
24506             (*deferred_locations_list)[i].die,
24507             (*deferred_locations_list)[i].variable,
24508             false,
24509             DW_AT_location);
24510       }
24511
24512   /* Traverse the limbo die list, and add parent/child links.  The only
24513      dies without parents that should be here are concrete instances of
24514      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
24515      For concrete instances, we can get the parent die from the abstract
24516      instance.  */
24517   for (node = limbo_die_list; node; node = next_node)
24518     {
24519       dw_die_ref die = node->die;
24520       next_node = node->next;
24521
24522       if (die->die_parent == NULL)
24523         {
24524           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
24525
24526           if (origin && origin->die_parent)
24527             add_child_die (origin->die_parent, die);
24528           else if (is_cu_die (die))
24529             ;
24530           else if (seen_error ())
24531             /* It's OK to be confused by errors in the input.  */
24532             add_child_die (comp_unit_die (), die);
24533           else
24534             {
24535               /* In certain situations, the lexical block containing a
24536                  nested function can be optimized away, which results
24537                  in the nested function die being orphaned.  Likewise
24538                  with the return type of that nested function.  Force
24539                  this to be a child of the containing function.
24540
24541                  It may happen that even the containing function got fully
24542                  inlined and optimized out.  In that case we are lost and
24543                  assign the empty child.  This should not be big issue as
24544                  the function is likely unreachable too.  */
24545               gcc_assert (node->created_for);
24546
24547               if (DECL_P (node->created_for))
24548                 origin = get_context_die (DECL_CONTEXT (node->created_for));
24549               else if (TYPE_P (node->created_for))
24550                 origin = scope_die_for (node->created_for, comp_unit_die ());
24551               else
24552                 origin = comp_unit_die ();
24553
24554               add_child_die (origin, die);
24555             }
24556         }
24557     }
24558
24559   limbo_die_list = NULL;
24560
24561 #if ENABLE_ASSERT_CHECKING
24562   {
24563     dw_die_ref die = comp_unit_die (), c;
24564     FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
24565   }
24566 #endif
24567   resolve_addr (comp_unit_die ());
24568   move_marked_base_types ();
24569
24570   for (node = deferred_asm_name; node; node = node->next)
24571     {
24572       tree decl = node->created_for;
24573       /* When generating LTO bytecode we can not generate new assembler
24574          names at this point and all important decls got theirs via
24575          free-lang-data.  */
24576       if (((!flag_generate_lto && !flag_generate_offload)
24577            || DECL_ASSEMBLER_NAME_SET_P (decl))
24578           && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
24579         {
24580           add_linkage_attr (node->die, decl);
24581           move_linkage_attr (node->die);
24582         }
24583     }
24584
24585   deferred_asm_name = NULL;
24586
24587   /* Walk through the list of incomplete types again, trying once more to
24588      emit full debugging info for them.  */
24589   retry_incomplete_types ();
24590
24591   if (flag_eliminate_unused_debug_types)
24592     prune_unused_types ();
24593
24594   /* Generate separate COMDAT sections for type DIEs. */
24595   if (use_debug_types)
24596     {
24597       break_out_comdat_types (comp_unit_die ());
24598
24599       /* Each new type_unit DIE was added to the limbo die list when created.
24600          Since these have all been added to comdat_type_list, clear the
24601          limbo die list.  */
24602       limbo_die_list = NULL;
24603
24604       /* For each new comdat type unit, copy declarations for incomplete
24605          types to make the new unit self-contained (i.e., no direct
24606          references to the main compile unit).  */
24607       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24608         copy_decls_for_unworthy_types (ctnode->root_die);
24609       copy_decls_for_unworthy_types (comp_unit_die ());
24610
24611       /* In the process of copying declarations from one unit to another,
24612          we may have left some declarations behind that are no longer
24613          referenced.  Prune them.  */
24614       prune_unused_types ();
24615     }
24616
24617   /* Generate separate CUs for each of the include files we've seen.
24618      They will go into limbo_die_list.  */
24619   if (flag_eliminate_dwarf2_dups)
24620     break_out_includes (comp_unit_die ());
24621
24622   /* Traverse the DIE's and add add sibling attributes to those DIE's
24623      that have children.  */
24624   add_sibling_attributes (comp_unit_die ());
24625   for (node = limbo_die_list; node; node = node->next)
24626     add_sibling_attributes (node->die);
24627   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24628     add_sibling_attributes (ctnode->root_die);
24629
24630   /* When splitting DWARF info, we put some attributes in the
24631      skeleton compile_unit DIE that remains in the .o, while
24632      most attributes go in the DWO compile_unit_die.  */
24633   if (dwarf_split_debug_info)
24634     main_comp_unit_die = gen_compile_unit_die (NULL);
24635   else
24636     main_comp_unit_die = comp_unit_die ();
24637
24638   /* Output a terminator label for the .text section.  */
24639   switch_to_section (text_section);
24640   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
24641   if (cold_text_section)
24642     {
24643       switch_to_section (cold_text_section);
24644       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
24645     }
24646
24647   /* We can only use the low/high_pc attributes if all of the code was
24648      in .text.  */
24649   if (!have_multiple_function_sections 
24650       || (dwarf_version < 3 && dwarf_strict))
24651     {
24652       /* Don't add if the CU has no associated code.  */
24653       if (text_section_used)
24654         add_AT_low_high_pc (main_comp_unit_die, text_section_label,
24655                             text_end_label, true);
24656     }
24657   else
24658     {
24659       unsigned fde_idx;
24660       dw_fde_ref fde;
24661       bool range_list_added = false;
24662
24663       if (text_section_used)
24664         add_ranges_by_labels (main_comp_unit_die, text_section_label,
24665                               text_end_label, &range_list_added, true);
24666       if (cold_text_section_used)
24667         add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
24668                               cold_end_label, &range_list_added, true);
24669
24670       FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
24671         {
24672           if (DECL_IGNORED_P (fde->decl))
24673             continue;
24674           if (!fde->in_std_section)
24675             add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
24676                                   fde->dw_fde_end, &range_list_added,
24677                                   true);
24678           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
24679             add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
24680                                   fde->dw_fde_second_end, &range_list_added,
24681                                   true);
24682         }
24683
24684       if (range_list_added)
24685         {
24686           /* We need to give .debug_loc and .debug_ranges an appropriate
24687              "base address".  Use zero so that these addresses become
24688              absolute.  Historically, we've emitted the unexpected
24689              DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
24690              Emit both to give time for other tools to adapt.  */
24691           add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
24692           if (! dwarf_strict && dwarf_version < 4)
24693             add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
24694
24695           add_ranges (NULL);
24696         }
24697     }
24698
24699   if (debug_info_level >= DINFO_LEVEL_TERSE)
24700     add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
24701                     debug_line_section_label);
24702
24703   if (have_macinfo)
24704     add_AT_macptr (comp_unit_die (),
24705                    dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
24706                    macinfo_section_label);
24707
24708   if (dwarf_split_debug_info)
24709     {
24710       /* optimize_location_lists calculates the size of the lists,
24711          so index them first, and assign indices to the entries.
24712          Although optimize_location_lists will remove entries from
24713          the table, it only does so for duplicates, and therefore
24714          only reduces ref_counts to 1.  */
24715       index_location_lists (comp_unit_die ());
24716
24717       if (addr_index_table != NULL)
24718         {
24719           unsigned int index = 0;
24720           addr_index_table
24721             ->traverse_noresize<unsigned int *, index_addr_table_entry>
24722             (&index);
24723         }
24724     }
24725
24726   if (have_location_lists)
24727     optimize_location_lists (comp_unit_die ());
24728
24729   save_macinfo_strings ();
24730
24731   if (dwarf_split_debug_info)
24732     {
24733       unsigned int index = 0;
24734
24735       /* Add attributes common to skeleton compile_units and
24736          type_units.  Because these attributes include strings, it
24737          must be done before freezing the string table.  Top-level
24738          skeleton die attrs are added when the skeleton type unit is
24739          created, so ensure it is created by this point.  */
24740       add_top_level_skeleton_die_attrs (main_comp_unit_die);
24741       debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
24742     }
24743
24744   /* Output all of the compilation units.  We put the main one last so that
24745      the offsets are available to output_pubnames.  */
24746   for (node = limbo_die_list; node; node = node->next)
24747     output_comp_unit (node->die, 0);
24748
24749   hash_table<comdat_type_hasher> comdat_type_table (100);
24750   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24751     {
24752       comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
24753
24754       /* Don't output duplicate types.  */
24755       if (*slot != HTAB_EMPTY_ENTRY)
24756         continue;
24757
24758       /* Add a pointer to the line table for the main compilation unit
24759          so that the debugger can make sense of DW_AT_decl_file
24760          attributes.  */
24761       if (debug_info_level >= DINFO_LEVEL_TERSE)
24762         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
24763                         (!dwarf_split_debug_info
24764                          ? debug_line_section_label
24765                          : debug_skeleton_line_section_label));
24766
24767       output_comdat_type_unit (ctnode);
24768       *slot = ctnode;
24769     }
24770
24771   /* The AT_pubnames attribute needs to go in all skeleton dies, including
24772      both the main_cu and all skeleton TUs.  Making this call unconditional
24773      would end up either adding a second copy of the AT_pubnames attribute, or
24774      requiring a special case in add_top_level_skeleton_die_attrs.  */
24775   if (!dwarf_split_debug_info)
24776     add_AT_pubnames (comp_unit_die ());
24777
24778   if (dwarf_split_debug_info)
24779     {
24780       int mark;
24781       unsigned char checksum[16];
24782       struct md5_ctx ctx;
24783
24784       /* Compute a checksum of the comp_unit to use as the dwo_id.  */
24785       md5_init_ctx (&ctx);
24786       mark = 0;
24787       die_checksum (comp_unit_die (), &ctx, &mark);
24788       unmark_all_dies (comp_unit_die ());
24789       md5_finish_ctx (&ctx, checksum);
24790
24791       /* Use the first 8 bytes of the checksum as the dwo_id,
24792         and add it to both comp-unit DIEs.  */
24793       add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
24794       add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
24795
24796       /* Add the base offset of the ranges table to the skeleton
24797         comp-unit DIE.  */
24798       if (ranges_table_in_use)
24799         add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
24800                         ranges_section_label);
24801
24802       switch_to_section (debug_addr_section);
24803       ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
24804       output_addr_table ();
24805     }
24806
24807   /* Output the main compilation unit if non-empty or if .debug_macinfo
24808      or .debug_macro will be emitted.  */
24809   output_comp_unit (comp_unit_die (), have_macinfo);
24810
24811   if (dwarf_split_debug_info && info_section_emitted)
24812     output_skeleton_debug_sections (main_comp_unit_die);
24813
24814   /* Output the abbreviation table.  */
24815   if (abbrev_die_table_in_use != 1)
24816     {
24817       switch_to_section (debug_abbrev_section);
24818       ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
24819       output_abbrev_section ();
24820     }
24821
24822   /* Output location list section if necessary.  */
24823   if (have_location_lists)
24824     {
24825       /* Output the location lists info.  */
24826       switch_to_section (debug_loc_section);
24827       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
24828       output_location_lists (comp_unit_die ());
24829     }
24830
24831   output_pubtables ();
24832
24833   /* Output the address range information if a CU (.debug_info section)
24834      was emitted.  We output an empty table even if we had no functions
24835      to put in it.  This because the consumer has no way to tell the
24836      difference between an empty table that we omitted and failure to
24837      generate a table that would have contained data.  */
24838   if (info_section_emitted)
24839     {
24840       unsigned long aranges_length = size_of_aranges ();
24841
24842       switch_to_section (debug_aranges_section);
24843       output_aranges (aranges_length);
24844     }
24845
24846   /* Output ranges section if necessary.  */
24847   if (ranges_table_in_use)
24848     {
24849       switch_to_section (debug_ranges_section);
24850       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
24851       output_ranges ();
24852     }
24853
24854   /* Have to end the macro section.  */
24855   if (have_macinfo)
24856     {
24857       switch_to_section (debug_macinfo_section);
24858       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
24859       output_macinfo ();
24860       dw2_asm_output_data (1, 0, "End compilation unit");
24861     }
24862
24863   /* Output the source line correspondence table.  We must do this
24864      even if there is no line information.  Otherwise, on an empty
24865      translation unit, we will generate a present, but empty,
24866      .debug_info section.  IRIX 6.5 `nm' will then complain when
24867      examining the file.  This is done late so that any filenames
24868      used by the debug_info section are marked as 'used'.  */
24869   switch_to_section (debug_line_section);
24870   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
24871   if (! DWARF2_ASM_LINE_DEBUG_INFO)
24872     output_line_info (false);
24873
24874   if (dwarf_split_debug_info && info_section_emitted)
24875     {
24876       switch_to_section (debug_skeleton_line_section);
24877       ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
24878       output_line_info (true);
24879     }
24880
24881   /* If we emitted any indirect strings, output the string table too.  */
24882   if (debug_str_hash || skeleton_debug_str_hash)
24883     output_indirect_strings ();
24884 }
24885
24886 /* Reset all state within dwarf2out.c so that we can rerun the compiler
24887    within the same process.  For use by toplev::finalize.  */
24888
24889 void
24890 dwarf2out_c_finalize (void)
24891 {
24892   last_var_location_insn = NULL;
24893   cached_next_real_insn = NULL;
24894   used_rtx_array = NULL;
24895   incomplete_types = NULL;
24896   decl_scope_table = NULL;
24897   debug_info_section = NULL;
24898   debug_skeleton_info_section = NULL;
24899   debug_abbrev_section = NULL;
24900   debug_skeleton_abbrev_section = NULL;
24901   debug_aranges_section = NULL;
24902   debug_addr_section = NULL;
24903   debug_macinfo_section = NULL;
24904   debug_line_section = NULL;
24905   debug_skeleton_line_section = NULL;
24906   debug_loc_section = NULL;
24907   debug_pubnames_section = NULL;
24908   debug_pubtypes_section = NULL;
24909   debug_str_section = NULL;
24910   debug_str_dwo_section = NULL;
24911   debug_str_offsets_section = NULL;
24912   debug_ranges_section = NULL;
24913   debug_frame_section = NULL;
24914   fde_vec = NULL;
24915   debug_str_hash = NULL;
24916   skeleton_debug_str_hash = NULL;
24917   dw2_string_counter = 0;
24918   have_multiple_function_sections = false;
24919   text_section_used = false;
24920   cold_text_section_used = false;
24921   cold_text_section = NULL;
24922   current_unit_personality = NULL;
24923
24924   deferred_locations_list = NULL;
24925
24926   next_die_offset = 0;
24927   single_comp_unit_die = NULL;
24928   comdat_type_list = NULL;
24929   limbo_die_list = NULL;
24930   deferred_asm_name = NULL;
24931   file_table = NULL;
24932   decl_die_table = NULL;
24933   common_block_die_table = NULL;
24934   decl_loc_table = NULL;
24935   call_arg_locations = NULL;
24936   call_arg_loc_last = NULL;
24937   call_site_count = -1;
24938   tail_call_site_count = -1;
24939   //block_map = NULL;
24940   cached_dw_loc_list_table = NULL;
24941   abbrev_die_table = NULL;
24942   abbrev_die_table_allocated = 0;
24943   abbrev_die_table_in_use = 0;
24944   line_info_label_num = 0;
24945   cur_line_info_table = NULL;
24946   text_section_line_info = NULL;
24947   cold_text_section_line_info = NULL;
24948   separate_line_info = NULL;
24949   info_section_emitted = false;
24950   pubname_table = NULL;
24951   pubtype_table = NULL;
24952   macinfo_table = NULL;
24953   ranges_table = NULL;
24954   ranges_table_allocated = 0;
24955   ranges_table_in_use = 0;
24956   ranges_by_label = 0;
24957   ranges_by_label_allocated = 0;
24958   ranges_by_label_in_use = 0;
24959   have_location_lists = false;
24960   loclabel_num = 0;
24961   poc_label_num = 0;
24962   last_emitted_file = NULL;
24963   label_num = 0;
24964   file_table_last_lookup = NULL;
24965   tmpl_value_parm_die_table = NULL;
24966   generic_type_instances = NULL;
24967   frame_pointer_fb_offset = 0;
24968   frame_pointer_fb_offset_valid = false;
24969   base_types.release ();
24970   XDELETEVEC (producer_string);
24971   producer_string = NULL;
24972 }
24973
24974 #include "gt-dwarf2out.h"