Ignore -fdebug-prefix-map in producer string (by Daniel Kahn Gillmor)
[platform/upstream/linaro-gcc.git] / gcc / dwarf2out.c
1 /* Output Dwarf2 format symbol table information from GCC.
2    Copyright (C) 1992-2015 Free Software Foundation, Inc.
3    Contributed by Gary Funck (gary@intrepid.com).
4    Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
5    Extensively modified by Jason Merrill (jason@cygnus.com).
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
13
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
17 for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3.  If not see
21 <http://www.gnu.org/licenses/>.  */
22
23 /* TODO: Emit .debug_line header even when there are no functions, since
24            the file numbers are used by .debug_info.  Alternately, leave
25            out locations for types and decls.
26          Avoid talking about ctors and op= for PODs.
27          Factor out common prologue sequences into multiple CIEs.  */
28
29 /* The first part of this file deals with the DWARF 2 frame unwind
30    information, which is also used by the GCC efficient exception handling
31    mechanism.  The second part, controlled only by an #ifdef
32    DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
33    information.  */
34
35 /* DWARF2 Abbreviation Glossary:
36
37    CFA = Canonical Frame Address
38            a fixed address on the stack which identifies a call frame.
39            We define it to be the value of SP just before the call insn.
40            The CFA register and offset, which may change during the course
41            of the function, are used to calculate its value at runtime.
42
43    CFI = Call Frame Instruction
44            an instruction for the DWARF2 abstract machine
45
46    CIE = Common Information Entry
47            information describing information common to one or more FDEs
48
49    DIE = Debugging Information Entry
50
51    FDE = Frame Description Entry
52            information describing the stack call frame, in particular,
53            how to restore registers
54
55    DW_CFA_... = DWARF2 CFA call frame instruction
56    DW_TAG_... = DWARF2 DIE tag */
57
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "target.h"
62 #include "function.h"
63 #include "rtl.h"
64 #include "tree.h"
65 #include "tm_p.h"
66 #include "stringpool.h"
67 #include "insn-config.h"
68 #include "ira.h"
69 #include "cgraph.h"
70 #include "diagnostic.h"
71 #include "fold-const.h"
72 #include "stor-layout.h"
73 #include "varasm.h"
74 #include "version.h"
75 #include "flags.h"
76 #include "rtlhash.h"
77 #include "reload.h"
78 #include "output.h"
79 #include "expr.h"
80 #include "dwarf2out.h"
81 #include "dwarf2asm.h"
82 #include "toplev.h"
83 #include "md5.h"
84 #include "tree-pretty-print.h"
85 #include "debug.h"
86 #include "common/common-target.h"
87 #include "langhooks.h"
88 #include "lra.h"
89 #include "dumpfile.h"
90 #include "opts.h"
91 #include "tree-dfa.h"
92 #include "gdb/gdb-index.h"
93 #include "rtl-iter.h"
94
95 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
96 static rtx_insn *last_var_location_insn;
97 static rtx_insn *cached_next_real_insn;
98 static void dwarf2out_decl (tree);
99
100 #ifndef XCOFF_DEBUGGING_INFO
101 #define XCOFF_DEBUGGING_INFO 0
102 #endif
103
104 #ifndef HAVE_XCOFF_DWARF_EXTRAS
105 #define HAVE_XCOFF_DWARF_EXTRAS 0
106 #endif
107
108 #ifdef VMS_DEBUGGING_INFO
109 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
110
111 /* Define this macro to be a nonzero value if the directory specifications
112     which are output in the debug info should end with a separator.  */
113 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
114 /* Define this macro to evaluate to a nonzero value if GCC should refrain
115    from generating indirect strings in DWARF2 debug information, for instance
116    if your target is stuck with an old version of GDB that is unable to
117    process them properly or uses VMS Debug.  */
118 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
119 #else
120 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
121 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
122 #endif
123
124 /* ??? Poison these here until it can be done generically.  They've been
125    totally replaced in this file; make sure it stays that way.  */
126 #undef DWARF2_UNWIND_INFO
127 #undef DWARF2_FRAME_INFO
128 #if (GCC_VERSION >= 3000)
129  #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
130 #endif
131
132 /* The size of the target's pointer type.  */
133 #ifndef PTR_SIZE
134 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
135 #endif
136
137 /* Array of RTXes referenced by the debugging information, which therefore
138    must be kept around forever.  */
139 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
140
141 /* A pointer to the base of a list of incomplete types which might be
142    completed at some later time.  incomplete_types_list needs to be a
143    vec<tree, va_gc> *because we want to tell the garbage collector about
144    it.  */
145 static GTY(()) vec<tree, va_gc> *incomplete_types;
146
147 /* A pointer to the base of a table of references to declaration
148    scopes.  This table is a display which tracks the nesting
149    of declaration scopes at the current scope and containing
150    scopes.  This table is used to find the proper place to
151    define type declaration DIE's.  */
152 static GTY(()) vec<tree, va_gc> *decl_scope_table;
153
154 /* Pointers to various DWARF2 sections.  */
155 static GTY(()) section *debug_info_section;
156 static GTY(()) section *debug_skeleton_info_section;
157 static GTY(()) section *debug_abbrev_section;
158 static GTY(()) section *debug_skeleton_abbrev_section;
159 static GTY(()) section *debug_aranges_section;
160 static GTY(()) section *debug_addr_section;
161 static GTY(()) section *debug_macinfo_section;
162 static GTY(()) section *debug_line_section;
163 static GTY(()) section *debug_skeleton_line_section;
164 static GTY(()) section *debug_loc_section;
165 static GTY(()) section *debug_pubnames_section;
166 static GTY(()) section *debug_pubtypes_section;
167 static GTY(()) section *debug_str_section;
168 static GTY(()) section *debug_str_dwo_section;
169 static GTY(()) section *debug_str_offsets_section;
170 static GTY(()) section *debug_ranges_section;
171 static GTY(()) section *debug_frame_section;
172
173 /* Maximum size (in bytes) of an artificially generated label.  */
174 #define MAX_ARTIFICIAL_LABEL_BYTES      30
175
176 /* According to the (draft) DWARF 3 specification, the initial length
177    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
178    bytes are 0xffffffff, followed by the length stored in the next 8
179    bytes.
180
181    However, the SGI/MIPS ABI uses an initial length which is equal to
182    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
183
184 #ifndef DWARF_INITIAL_LENGTH_SIZE
185 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
186 #endif
187
188 /* Round SIZE up to the nearest BOUNDARY.  */
189 #define DWARF_ROUND(SIZE,BOUNDARY) \
190   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
191
192 /* CIE identifier.  */
193 #if HOST_BITS_PER_WIDE_INT >= 64
194 #define DWARF_CIE_ID \
195   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
196 #else
197 #define DWARF_CIE_ID DW_CIE_ID
198 #endif
199
200
201 /* A vector for a table that contains frame description
202    information for each routine.  */
203 #define NOT_INDEXED (-1U)
204 #define NO_INDEX_ASSIGNED (-2U)
205
206 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
207
208 struct GTY((for_user)) indirect_string_node {
209   const char *str;
210   unsigned int refcount;
211   enum dwarf_form form;
212   char *label;
213   unsigned int index;
214 };
215
216 struct indirect_string_hasher : ggc_ptr_hash<indirect_string_node>
217 {
218   typedef const char *compare_type;
219
220   static hashval_t hash (indirect_string_node *);
221   static bool equal (indirect_string_node *, const char *);
222 };
223
224 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
225
226 /* With split_debug_info, both the comp_dir and dwo_name go in the
227    main object file, rather than the dwo, similar to the force_direct
228    parameter elsewhere but with additional complications:
229
230    1) The string is needed in both the main object file and the dwo.
231    That is, the comp_dir and dwo_name will appear in both places.
232
233    2) Strings can use three forms: DW_FORM_string, DW_FORM_strp or
234    DW_FORM_GNU_str_index.
235
236    3) GCC chooses the form to use late, depending on the size and
237    reference count.
238
239    Rather than forcing the all debug string handling functions and
240    callers to deal with these complications, simply use a separate,
241    special-cased string table for any attribute that should go in the
242    main object file.  This limits the complexity to just the places
243    that need it.  */
244
245 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
246
247 static GTY(()) int dw2_string_counter;
248
249 /* True if the compilation unit places functions in more than one section.  */
250 static GTY(()) bool have_multiple_function_sections = false;
251
252 /* Whether the default text and cold text sections have been used at all.  */
253
254 static GTY(()) bool text_section_used = false;
255 static GTY(()) bool cold_text_section_used = false;
256
257 /* The default cold text section.  */
258 static GTY(()) section *cold_text_section;
259
260 /* The DIE for C++14 'auto' in a function return type.  */
261 static GTY(()) dw_die_ref auto_die;
262
263 /* The DIE for C++14 'decltype(auto)' in a function return type.  */
264 static GTY(()) dw_die_ref decltype_auto_die;
265
266 /* Forward declarations for functions defined in this file.  */
267
268 static char *stripattributes (const char *);
269 static void output_call_frame_info (int);
270 static void dwarf2out_note_section_used (void);
271
272 /* Personality decl of current unit.  Used only when assembler does not support
273    personality CFI.  */
274 static GTY(()) rtx current_unit_personality;
275
276 /* Data and reference forms for relocatable data.  */
277 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
278 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
279
280 #ifndef DEBUG_FRAME_SECTION
281 #define DEBUG_FRAME_SECTION     ".debug_frame"
282 #endif
283
284 #ifndef FUNC_BEGIN_LABEL
285 #define FUNC_BEGIN_LABEL        "LFB"
286 #endif
287
288 #ifndef FUNC_END_LABEL
289 #define FUNC_END_LABEL          "LFE"
290 #endif
291
292 #ifndef PROLOGUE_END_LABEL
293 #define PROLOGUE_END_LABEL      "LPE"
294 #endif
295
296 #ifndef EPILOGUE_BEGIN_LABEL
297 #define EPILOGUE_BEGIN_LABEL    "LEB"
298 #endif
299
300 #ifndef FRAME_BEGIN_LABEL
301 #define FRAME_BEGIN_LABEL       "Lframe"
302 #endif
303 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
304 #define CIE_END_LABEL           "LECIE"
305 #define FDE_LABEL               "LSFDE"
306 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
307 #define FDE_END_LABEL           "LEFDE"
308 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
309 #define LINE_NUMBER_END_LABEL   "LELT"
310 #define LN_PROLOG_AS_LABEL      "LASLTP"
311 #define LN_PROLOG_END_LABEL     "LELTP"
312 #define DIE_LABEL_PREFIX        "DW"
313 \f
314 /* Match the base name of a file to the base name of a compilation unit. */
315
316 static int
317 matches_main_base (const char *path)
318 {
319   /* Cache the last query. */
320   static const char *last_path = NULL;
321   static int last_match = 0;
322   if (path != last_path)
323     {
324       const char *base;
325       int length = base_of_path (path, &base);
326       last_path = path;
327       last_match = (length == main_input_baselength
328                     && memcmp (base, main_input_basename, length) == 0);
329     }
330   return last_match;
331 }
332
333 #ifdef DEBUG_DEBUG_STRUCT
334
335 static int
336 dump_struct_debug (tree type, enum debug_info_usage usage,
337                    enum debug_struct_file criterion, int generic,
338                    int matches, int result)
339 {
340   /* Find the type name. */
341   tree type_decl = TYPE_STUB_DECL (type);
342   tree t = type_decl;
343   const char *name = 0;
344   if (TREE_CODE (t) == TYPE_DECL)
345     t = DECL_NAME (t);
346   if (t)
347     name = IDENTIFIER_POINTER (t);
348
349   fprintf (stderr, "    struct %d %s %s %s %s %d %p %s\n",
350            criterion,
351            DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
352            matches ? "bas" : "hdr",
353            generic ? "gen" : "ord",
354            usage == DINFO_USAGE_DFN ? ";" :
355              usage == DINFO_USAGE_DIR_USE ? "." : "*",
356            result,
357            (void*) type_decl, name);
358   return result;
359 }
360 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
361   dump_struct_debug (type, usage, criterion, generic, matches, result)
362
363 #else
364
365 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
366   (result)
367
368 #endif
369
370 /* Get the number of HOST_WIDE_INTs needed to represent the precision
371    of the number.  Some constants have a large uniform precision, so
372    we get the precision needed for the actual value of the number.  */
373
374 static unsigned int
375 get_full_len (const wide_int &op)
376 {
377   int prec = wi::min_precision (op, UNSIGNED);
378   return ((prec + HOST_BITS_PER_WIDE_INT - 1)
379           / HOST_BITS_PER_WIDE_INT);
380 }
381
382 static bool
383 should_emit_struct_debug (tree type, enum debug_info_usage usage)
384 {
385   enum debug_struct_file criterion;
386   tree type_decl;
387   bool generic = lang_hooks.types.generic_p (type);
388
389   if (generic)
390     criterion = debug_struct_generic[usage];
391   else
392     criterion = debug_struct_ordinary[usage];
393
394   if (criterion == DINFO_STRUCT_FILE_NONE)
395     return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
396   if (criterion == DINFO_STRUCT_FILE_ANY)
397     return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
398
399   type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
400
401   if (type_decl != NULL)
402     {
403      if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
404         return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
405
406       if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
407         return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
408     }
409
410   return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
411 }
412 \f
413 /* Return a pointer to a copy of the section string name S with all
414    attributes stripped off, and an asterisk prepended (for assemble_name).  */
415
416 static inline char *
417 stripattributes (const char *s)
418 {
419   char *stripped = XNEWVEC (char, strlen (s) + 2);
420   char *p = stripped;
421
422   *p++ = '*';
423
424   while (*s && *s != ',')
425     *p++ = *s++;
426
427   *p = '\0';
428   return stripped;
429 }
430
431 /* Switch [BACK] to eh_frame_section.  If we don't have an eh_frame_section,
432    switch to the data section instead, and write out a synthetic start label
433    for collect2 the first time around.  */
434
435 static void
436 switch_to_eh_frame_section (bool back ATTRIBUTE_UNUSED)
437 {
438   if (eh_frame_section == 0)
439     {
440       int flags;
441
442       if (EH_TABLES_CAN_BE_READ_ONLY)
443         {
444           int fde_encoding;
445           int per_encoding;
446           int lsda_encoding;
447
448           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
449                                                        /*global=*/0);
450           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
451                                                        /*global=*/1);
452           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
453                                                         /*global=*/0);
454           flags = ((! flag_pic
455                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
456                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
457                         && (per_encoding & 0x70) != DW_EH_PE_absptr
458                         && (per_encoding & 0x70) != DW_EH_PE_aligned
459                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
460                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
461                    ? 0 : SECTION_WRITE);
462         }
463       else
464         flags = SECTION_WRITE;
465
466 #ifdef EH_FRAME_SECTION_NAME
467       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
468 #else
469       eh_frame_section = ((flags == SECTION_WRITE)
470                           ? data_section : readonly_data_section);
471 #endif /* EH_FRAME_SECTION_NAME */
472     }
473
474   switch_to_section (eh_frame_section);
475
476 #ifdef EH_FRAME_THROUGH_COLLECT2
477   /* We have no special eh_frame section.  Emit special labels to guide
478      collect2.  */
479   if (!back)
480     {
481       tree label = get_file_function_name ("F");
482       ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
483       targetm.asm_out.globalize_label (asm_out_file,
484                                         IDENTIFIER_POINTER (label));
485       ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
486     }
487 #endif
488 }
489
490 /* Switch [BACK] to the eh or debug frame table section, depending on
491    FOR_EH.  */
492
493 static void
494 switch_to_frame_table_section (int for_eh, bool back)
495 {
496   if (for_eh)
497     switch_to_eh_frame_section (back);
498   else
499     {
500       if (!debug_frame_section)
501         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
502                                            SECTION_DEBUG, NULL);
503       switch_to_section (debug_frame_section);
504     }
505 }
506
507 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
508
509 enum dw_cfi_oprnd_type
510 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
511 {
512   switch (cfi)
513     {
514     case DW_CFA_nop:
515     case DW_CFA_GNU_window_save:
516     case DW_CFA_remember_state:
517     case DW_CFA_restore_state:
518       return dw_cfi_oprnd_unused;
519
520     case DW_CFA_set_loc:
521     case DW_CFA_advance_loc1:
522     case DW_CFA_advance_loc2:
523     case DW_CFA_advance_loc4:
524     case DW_CFA_MIPS_advance_loc8:
525       return dw_cfi_oprnd_addr;
526
527     case DW_CFA_offset:
528     case DW_CFA_offset_extended:
529     case DW_CFA_def_cfa:
530     case DW_CFA_offset_extended_sf:
531     case DW_CFA_def_cfa_sf:
532     case DW_CFA_restore:
533     case DW_CFA_restore_extended:
534     case DW_CFA_undefined:
535     case DW_CFA_same_value:
536     case DW_CFA_def_cfa_register:
537     case DW_CFA_register:
538     case DW_CFA_expression:
539       return dw_cfi_oprnd_reg_num;
540
541     case DW_CFA_def_cfa_offset:
542     case DW_CFA_GNU_args_size:
543     case DW_CFA_def_cfa_offset_sf:
544       return dw_cfi_oprnd_offset;
545
546     case DW_CFA_def_cfa_expression:
547       return dw_cfi_oprnd_loc;
548
549     default:
550       gcc_unreachable ();
551     }
552 }
553
554 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
555
556 enum dw_cfi_oprnd_type
557 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
558 {
559   switch (cfi)
560     {
561     case DW_CFA_def_cfa:
562     case DW_CFA_def_cfa_sf:
563     case DW_CFA_offset:
564     case DW_CFA_offset_extended_sf:
565     case DW_CFA_offset_extended:
566       return dw_cfi_oprnd_offset;
567
568     case DW_CFA_register:
569       return dw_cfi_oprnd_reg_num;
570
571     case DW_CFA_expression:
572       return dw_cfi_oprnd_loc;
573
574     default:
575       return dw_cfi_oprnd_unused;
576     }
577 }
578
579 /* Output one FDE.  */
580
581 static void
582 output_fde (dw_fde_ref fde, bool for_eh, bool second,
583             char *section_start_label, int fde_encoding, char *augmentation,
584             bool any_lsda_needed, int lsda_encoding)
585 {
586   const char *begin, *end;
587   static unsigned int j;
588   char l1[20], l2[20];
589
590   targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
591                                      /* empty */ 0);
592   targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
593                                   for_eh + j);
594   ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
595   ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
596   if (!XCOFF_DEBUGGING_INFO || for_eh)
597     {
598       if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
599         dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
600                              " indicating 64-bit DWARF extension");
601       dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
602                             "FDE Length");
603     }
604   ASM_OUTPUT_LABEL (asm_out_file, l1);
605
606   if (for_eh)
607     dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
608   else
609     dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
610                            debug_frame_section, "FDE CIE offset");
611
612   begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
613   end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
614
615   if (for_eh)
616     {
617       rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
618       SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
619       dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
620                                        "FDE initial location");
621       dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
622                             end, begin, "FDE address range");
623     }
624   else
625     {
626       dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
627       dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
628     }
629
630   if (augmentation[0])
631     {
632       if (any_lsda_needed)
633         {
634           int size = size_of_encoded_value (lsda_encoding);
635
636           if (lsda_encoding == DW_EH_PE_aligned)
637             {
638               int offset = (  4         /* Length */
639                             + 4         /* CIE offset */
640                             + 2 * size_of_encoded_value (fde_encoding)
641                             + 1         /* Augmentation size */ );
642               int pad = -offset & (PTR_SIZE - 1);
643
644               size += pad;
645               gcc_assert (size_of_uleb128 (size) == 1);
646             }
647
648           dw2_asm_output_data_uleb128 (size, "Augmentation size");
649
650           if (fde->uses_eh_lsda)
651             {
652               ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
653                                            fde->funcdef_number);
654               dw2_asm_output_encoded_addr_rtx (lsda_encoding,
655                                                gen_rtx_SYMBOL_REF (Pmode, l1),
656                                                false,
657                                                "Language Specific Data Area");
658             }
659           else
660             {
661               if (lsda_encoding == DW_EH_PE_aligned)
662                 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
663               dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
664                                    "Language Specific Data Area (none)");
665             }
666         }
667       else
668         dw2_asm_output_data_uleb128 (0, "Augmentation size");
669     }
670
671   /* Loop through the Call Frame Instructions associated with this FDE.  */
672   fde->dw_fde_current_label = begin;
673   {
674     size_t from, until, i;
675
676     from = 0;
677     until = vec_safe_length (fde->dw_fde_cfi);
678
679     if (fde->dw_fde_second_begin == NULL)
680       ;
681     else if (!second)
682       until = fde->dw_fde_switch_cfi_index;
683     else
684       from = fde->dw_fde_switch_cfi_index;
685
686     for (i = from; i < until; i++)
687       output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
688   }
689
690   /* If we are to emit a ref/link from function bodies to their frame tables,
691      do it now.  This is typically performed to make sure that tables
692      associated with functions are dragged with them and not discarded in
693      garbage collecting links. We need to do this on a per function basis to
694      cope with -ffunction-sections.  */
695
696 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
697   /* Switch to the function section, emit the ref to the tables, and
698      switch *back* into the table section.  */
699   switch_to_section (function_section (fde->decl));
700   ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
701   switch_to_frame_table_section (for_eh, true);
702 #endif
703
704   /* Pad the FDE out to an address sized boundary.  */
705   ASM_OUTPUT_ALIGN (asm_out_file,
706                     floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
707   ASM_OUTPUT_LABEL (asm_out_file, l2);
708
709   j += 2;
710 }
711
712 /* Return true if frame description entry FDE is needed for EH.  */
713
714 static bool
715 fde_needed_for_eh_p (dw_fde_ref fde)
716 {
717   if (flag_asynchronous_unwind_tables)
718     return true;
719
720   if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
721     return true;
722
723   if (fde->uses_eh_lsda)
724     return true;
725
726   /* If exceptions are enabled, we have collected nothrow info.  */
727   if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
728     return false;
729
730   return true;
731 }
732
733 /* Output the call frame information used to record information
734    that relates to calculating the frame pointer, and records the
735    location of saved registers.  */
736
737 static void
738 output_call_frame_info (int for_eh)
739 {
740   unsigned int i;
741   dw_fde_ref fde;
742   dw_cfi_ref cfi;
743   char l1[20], l2[20], section_start_label[20];
744   bool any_lsda_needed = false;
745   char augmentation[6];
746   int augmentation_size;
747   int fde_encoding = DW_EH_PE_absptr;
748   int per_encoding = DW_EH_PE_absptr;
749   int lsda_encoding = DW_EH_PE_absptr;
750   int return_reg;
751   rtx personality = NULL;
752   int dw_cie_version;
753
754   /* Don't emit a CIE if there won't be any FDEs.  */
755   if (!fde_vec)
756     return;
757
758   /* Nothing to do if the assembler's doing it all.  */
759   if (dwarf2out_do_cfi_asm ())
760     return;
761
762   /* If we don't have any functions we'll want to unwind out of, don't emit
763      any EH unwind information.  If we make FDEs linkonce, we may have to
764      emit an empty label for an FDE that wouldn't otherwise be emitted.  We
765      want to avoid having an FDE kept around when the function it refers to
766      is discarded.  Example where this matters: a primary function template
767      in C++ requires EH information, an explicit specialization doesn't.  */
768   if (for_eh)
769     {
770       bool any_eh_needed = false;
771
772       FOR_EACH_VEC_ELT (*fde_vec, i, fde)
773         {
774           if (fde->uses_eh_lsda)
775             any_eh_needed = any_lsda_needed = true;
776           else if (fde_needed_for_eh_p (fde))
777             any_eh_needed = true;
778           else if (TARGET_USES_WEAK_UNWIND_INFO)
779             targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
780         }
781
782       if (!any_eh_needed)
783         return;
784     }
785
786   /* We're going to be generating comments, so turn on app.  */
787   if (flag_debug_asm)
788     app_enable ();
789
790   /* Switch to the proper frame section, first time.  */
791   switch_to_frame_table_section (for_eh, false);
792
793   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
794   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
795
796   /* Output the CIE.  */
797   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
798   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
799   if (!XCOFF_DEBUGGING_INFO || for_eh)
800     {
801       if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
802         dw2_asm_output_data (4, 0xffffffff,
803           "Initial length escape value indicating 64-bit DWARF extension");
804       dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
805                             "Length of Common Information Entry");
806     }
807   ASM_OUTPUT_LABEL (asm_out_file, l1);
808
809   /* Now that the CIE pointer is PC-relative for EH,
810      use 0 to identify the CIE.  */
811   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
812                        (for_eh ? 0 : DWARF_CIE_ID),
813                        "CIE Identifier Tag");
814
815   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
816      use CIE version 1, unless that would produce incorrect results
817      due to overflowing the return register column.  */
818   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
819   dw_cie_version = 1;
820   if (return_reg >= 256 || dwarf_version > 2)
821     dw_cie_version = 3;
822   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
823
824   augmentation[0] = 0;
825   augmentation_size = 0;
826
827   personality = current_unit_personality;
828   if (for_eh)
829     {
830       char *p;
831
832       /* Augmentation:
833          z      Indicates that a uleb128 is present to size the
834                 augmentation section.
835          L      Indicates the encoding (and thus presence) of
836                 an LSDA pointer in the FDE augmentation.
837          R      Indicates a non-default pointer encoding for
838                 FDE code pointers.
839          P      Indicates the presence of an encoding + language
840                 personality routine in the CIE augmentation.  */
841
842       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
843       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
844       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
845
846       p = augmentation + 1;
847       if (personality)
848         {
849           *p++ = 'P';
850           augmentation_size += 1 + size_of_encoded_value (per_encoding);
851           assemble_external_libcall (personality);
852         }
853       if (any_lsda_needed)
854         {
855           *p++ = 'L';
856           augmentation_size += 1;
857         }
858       if (fde_encoding != DW_EH_PE_absptr)
859         {
860           *p++ = 'R';
861           augmentation_size += 1;
862         }
863       if (p > augmentation + 1)
864         {
865           augmentation[0] = 'z';
866           *p = '\0';
867         }
868
869       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
870       if (personality && per_encoding == DW_EH_PE_aligned)
871         {
872           int offset = (  4             /* Length */
873                         + 4             /* CIE Id */
874                         + 1             /* CIE version */
875                         + strlen (augmentation) + 1     /* Augmentation */
876                         + size_of_uleb128 (1)           /* Code alignment */
877                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
878                         + 1             /* RA column */
879                         + 1             /* Augmentation size */
880                         + 1             /* Personality encoding */ );
881           int pad = -offset & (PTR_SIZE - 1);
882
883           augmentation_size += pad;
884
885           /* Augmentations should be small, so there's scarce need to
886              iterate for a solution.  Die if we exceed one uleb128 byte.  */
887           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
888         }
889     }
890
891   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
892   if (dw_cie_version >= 4)
893     {
894       dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
895       dw2_asm_output_data (1, 0, "CIE Segment Size");
896     }
897   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
898   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
899                                "CIE Data Alignment Factor");
900
901   if (dw_cie_version == 1)
902     dw2_asm_output_data (1, return_reg, "CIE RA Column");
903   else
904     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
905
906   if (augmentation[0])
907     {
908       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
909       if (personality)
910         {
911           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
912                                eh_data_format_name (per_encoding));
913           dw2_asm_output_encoded_addr_rtx (per_encoding,
914                                            personality,
915                                            true, NULL);
916         }
917
918       if (any_lsda_needed)
919         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
920                              eh_data_format_name (lsda_encoding));
921
922       if (fde_encoding != DW_EH_PE_absptr)
923         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
924                              eh_data_format_name (fde_encoding));
925     }
926
927   FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
928     output_cfi (cfi, NULL, for_eh);
929
930   /* Pad the CIE out to an address sized boundary.  */
931   ASM_OUTPUT_ALIGN (asm_out_file,
932                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
933   ASM_OUTPUT_LABEL (asm_out_file, l2);
934
935   /* Loop through all of the FDE's.  */
936   FOR_EACH_VEC_ELT (*fde_vec, i, fde)
937     {
938       unsigned int k;
939
940       /* Don't emit EH unwind info for leaf functions that don't need it.  */
941       if (for_eh && !fde_needed_for_eh_p (fde))
942         continue;
943
944       for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
945         output_fde (fde, for_eh, k, section_start_label, fde_encoding,
946                     augmentation, any_lsda_needed, lsda_encoding);
947     }
948
949   if (for_eh && targetm.terminate_dw2_eh_frame_info)
950     dw2_asm_output_data (4, 0, "End of Table");
951
952   /* Turn off app to make assembly quicker.  */
953   if (flag_debug_asm)
954     app_disable ();
955 }
956
957 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed.  */
958
959 static void
960 dwarf2out_do_cfi_startproc (bool second)
961 {
962   int enc;
963   rtx ref;
964   rtx personality = get_personality_function (current_function_decl);
965
966   fprintf (asm_out_file, "\t.cfi_startproc\n");
967
968   if (personality)
969     {
970       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
971       ref = personality;
972
973       /* ??? The GAS support isn't entirely consistent.  We have to
974          handle indirect support ourselves, but PC-relative is done
975          in the assembler.  Further, the assembler can't handle any
976          of the weirder relocation types.  */
977       if (enc & DW_EH_PE_indirect)
978         ref = dw2_force_const_mem (ref, true);
979
980       fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
981       output_addr_const (asm_out_file, ref);
982       fputc ('\n', asm_out_file);
983     }
984
985   if (crtl->uses_eh_lsda)
986     {
987       char lab[20];
988
989       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
990       ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
991                                    current_function_funcdef_no);
992       ref = gen_rtx_SYMBOL_REF (Pmode, lab);
993       SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
994
995       if (enc & DW_EH_PE_indirect)
996         ref = dw2_force_const_mem (ref, true);
997
998       fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
999       output_addr_const (asm_out_file, ref);
1000       fputc ('\n', asm_out_file);
1001     }
1002 }
1003
1004 /* Allocate CURRENT_FDE.  Immediately initialize all we can, noting that
1005    this allocation may be done before pass_final.  */
1006
1007 dw_fde_ref
1008 dwarf2out_alloc_current_fde (void)
1009 {
1010   dw_fde_ref fde;
1011
1012   fde = ggc_cleared_alloc<dw_fde_node> ();
1013   fde->decl = current_function_decl;
1014   fde->funcdef_number = current_function_funcdef_no;
1015   fde->fde_index = vec_safe_length (fde_vec);
1016   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1017   fde->uses_eh_lsda = crtl->uses_eh_lsda;
1018   fde->nothrow = crtl->nothrow;
1019   fde->drap_reg = INVALID_REGNUM;
1020   fde->vdrap_reg = INVALID_REGNUM;
1021
1022   /* Record the FDE associated with this function.  */
1023   cfun->fde = fde;
1024   vec_safe_push (fde_vec, fde);
1025
1026   return fde;
1027 }
1028
1029 /* Output a marker (i.e. a label) for the beginning of a function, before
1030    the prologue.  */
1031
1032 void
1033 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1034                           const char *file ATTRIBUTE_UNUSED)
1035 {
1036   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1037   char * dup_label;
1038   dw_fde_ref fde;
1039   section *fnsec;
1040   bool do_frame;
1041
1042   current_function_func_begin_label = NULL;
1043
1044   do_frame = dwarf2out_do_frame ();
1045
1046   /* ??? current_function_func_begin_label is also used by except.c for
1047      call-site information.  We must emit this label if it might be used.  */
1048   if (!do_frame
1049       && (!flag_exceptions
1050           || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1051     return;
1052
1053   fnsec = function_section (current_function_decl);
1054   switch_to_section (fnsec);
1055   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1056                                current_function_funcdef_no);
1057   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1058                           current_function_funcdef_no);
1059   dup_label = xstrdup (label);
1060   current_function_func_begin_label = dup_label;
1061
1062   /* We can elide the fde allocation if we're not emitting debug info.  */
1063   if (!do_frame)
1064     return;
1065
1066   /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1067      emit insns as rtx but bypass the bulk of rest_of_compilation, which
1068      would include pass_dwarf2_frame.  If we've not created the FDE yet,
1069      do so now.  */
1070   fde = cfun->fde;
1071   if (fde == NULL)
1072     fde = dwarf2out_alloc_current_fde ();
1073
1074   /* Initialize the bits of CURRENT_FDE that were not available earlier.  */
1075   fde->dw_fde_begin = dup_label;
1076   fde->dw_fde_current_label = dup_label;
1077   fde->in_std_section = (fnsec == text_section
1078                          || (cold_text_section && fnsec == cold_text_section));
1079
1080   /* We only want to output line number information for the genuine dwarf2
1081      prologue case, not the eh frame case.  */
1082 #ifdef DWARF2_DEBUGGING_INFO
1083   if (file)
1084     dwarf2out_source_line (line, file, 0, true);
1085 #endif
1086
1087   if (dwarf2out_do_cfi_asm ())
1088     dwarf2out_do_cfi_startproc (false);
1089   else
1090     {
1091       rtx personality = get_personality_function (current_function_decl);
1092       if (!current_unit_personality)
1093         current_unit_personality = personality;
1094
1095       /* We cannot keep a current personality per function as without CFI
1096          asm, at the point where we emit the CFI data, there is no current
1097          function anymore.  */
1098       if (personality && current_unit_personality != personality)
1099         sorry ("multiple EH personalities are supported only with assemblers "
1100                "supporting .cfi_personality directive");
1101     }
1102 }
1103
1104 /* Output a marker (i.e. a label) for the end of the generated code
1105    for a function prologue.  This gets called *after* the prologue code has
1106    been generated.  */
1107
1108 void
1109 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1110                         const char *file ATTRIBUTE_UNUSED)
1111 {
1112   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1113
1114   /* Output a label to mark the endpoint of the code generated for this
1115      function.  */
1116   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1117                                current_function_funcdef_no);
1118   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1119                           current_function_funcdef_no);
1120   cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1121 }
1122
1123 /* Output a marker (i.e. a label) for the beginning of the generated code
1124    for a function epilogue.  This gets called *before* the prologue code has
1125    been generated.  */
1126
1127 void
1128 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1129                           const char *file ATTRIBUTE_UNUSED)
1130 {
1131   dw_fde_ref fde = cfun->fde;
1132   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1133
1134   if (fde->dw_fde_vms_begin_epilogue)
1135     return;
1136
1137   /* Output a label to mark the endpoint of the code generated for this
1138      function.  */
1139   ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1140                                current_function_funcdef_no);
1141   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1142                           current_function_funcdef_no);
1143   fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1144 }
1145
1146 /* Output a marker (i.e. a label) for the absolute end of the generated code
1147    for a function definition.  This gets called *after* the epilogue code has
1148    been generated.  */
1149
1150 void
1151 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1152                         const char *file ATTRIBUTE_UNUSED)
1153 {
1154   dw_fde_ref fde;
1155   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1156
1157   last_var_location_insn = NULL;
1158   cached_next_real_insn = NULL;
1159
1160   if (dwarf2out_do_cfi_asm ())
1161     fprintf (asm_out_file, "\t.cfi_endproc\n");
1162
1163   /* Output a label to mark the endpoint of the code generated for this
1164      function.  */
1165   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1166                                current_function_funcdef_no);
1167   ASM_OUTPUT_LABEL (asm_out_file, label);
1168   fde = cfun->fde;
1169   gcc_assert (fde != NULL);
1170   if (fde->dw_fde_second_begin == NULL)
1171     fde->dw_fde_end = xstrdup (label);
1172 }
1173
1174 void
1175 dwarf2out_frame_finish (void)
1176 {
1177   /* Output call frame information.  */
1178   if (targetm.debug_unwind_info () == UI_DWARF2)
1179     output_call_frame_info (0);
1180
1181   /* Output another copy for the unwinder.  */
1182   if ((flag_unwind_tables || flag_exceptions)
1183       && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1184     output_call_frame_info (1);
1185 }
1186
1187 /* Note that the current function section is being used for code.  */
1188
1189 static void
1190 dwarf2out_note_section_used (void)
1191 {
1192   section *sec = current_function_section ();
1193   if (sec == text_section)
1194     text_section_used = true;
1195   else if (sec == cold_text_section)
1196     cold_text_section_used = true;
1197 }
1198
1199 static void var_location_switch_text_section (void);
1200 static void set_cur_line_info_table (section *);
1201
1202 void
1203 dwarf2out_switch_text_section (void)
1204 {
1205   section *sect;
1206   dw_fde_ref fde = cfun->fde;
1207
1208   gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1209
1210   if (!in_cold_section_p)
1211     {
1212       fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1213       fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1214       fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1215     }
1216   else
1217     {
1218       fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1219       fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1220       fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1221     }
1222   have_multiple_function_sections = true;
1223
1224   /* There is no need to mark used sections when not debugging.  */
1225   if (cold_text_section != NULL)
1226     dwarf2out_note_section_used ();
1227
1228   if (dwarf2out_do_cfi_asm ())
1229     fprintf (asm_out_file, "\t.cfi_endproc\n");
1230
1231   /* Now do the real section switch.  */
1232   sect = current_function_section ();
1233   switch_to_section (sect);
1234
1235   fde->second_in_std_section
1236     = (sect == text_section
1237        || (cold_text_section && sect == cold_text_section));
1238
1239   if (dwarf2out_do_cfi_asm ())
1240     dwarf2out_do_cfi_startproc (true);
1241
1242   var_location_switch_text_section ();
1243
1244   if (cold_text_section != NULL)
1245     set_cur_line_info_table (sect);
1246 }
1247 \f
1248 /* And now, the subset of the debugging information support code necessary
1249    for emitting location expressions.  */
1250
1251 /* Data about a single source file.  */
1252 struct GTY((for_user)) dwarf_file_data {
1253   const char * filename;
1254   int emitted_number;
1255 };
1256
1257 /* Describe an entry into the .debug_addr section.  */
1258
1259 enum ate_kind {
1260   ate_kind_rtx,
1261   ate_kind_rtx_dtprel,
1262   ate_kind_label
1263 };
1264
1265 struct GTY((for_user)) addr_table_entry {
1266   enum ate_kind kind;
1267   unsigned int refcount;
1268   unsigned int index;
1269   union addr_table_entry_struct_union
1270     {
1271       rtx GTY ((tag ("0"))) rtl;
1272       char * GTY ((tag ("1"))) label;
1273     }
1274   GTY ((desc ("%1.kind"))) addr;
1275 };
1276
1277 /* Location lists are ranges + location descriptions for that range,
1278    so you can track variables that are in different places over
1279    their entire life.  */
1280 typedef struct GTY(()) dw_loc_list_struct {
1281   dw_loc_list_ref dw_loc_next;
1282   const char *begin; /* Label and addr_entry for start of range */
1283   addr_table_entry *begin_entry;
1284   const char *end;  /* Label for end of range */
1285   char *ll_symbol; /* Label for beginning of location list.
1286                       Only on head of list */
1287   const char *section; /* Section this loclist is relative to */
1288   dw_loc_descr_ref expr;
1289   hashval_t hash;
1290   /* True if all addresses in this and subsequent lists are known to be
1291      resolved.  */
1292   bool resolved_addr;
1293   /* True if this list has been replaced by dw_loc_next.  */
1294   bool replaced;
1295   bool emitted;
1296   /* True if the range should be emitted even if begin and end
1297      are the same.  */
1298   bool force;
1299 } dw_loc_list_node;
1300
1301 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1302 static dw_loc_descr_ref uint_loc_descriptor (unsigned HOST_WIDE_INT);
1303
1304 /* Convert a DWARF stack opcode into its string name.  */
1305
1306 static const char *
1307 dwarf_stack_op_name (unsigned int op)
1308 {
1309   const char *name = get_DW_OP_name (op);
1310
1311   if (name != NULL)
1312     return name;
1313
1314   return "OP_<unknown>";
1315 }
1316
1317 /* Return a pointer to a newly allocated location description.  Location
1318    descriptions are simple expression terms that can be strung
1319    together to form more complicated location (address) descriptions.  */
1320
1321 static inline dw_loc_descr_ref
1322 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1323                unsigned HOST_WIDE_INT oprnd2)
1324 {
1325   dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1326
1327   descr->dw_loc_opc = op;
1328 #if ENABLE_CHECKING
1329   descr->dw_loc_frame_offset = -1;
1330 #endif
1331   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1332   descr->dw_loc_oprnd1.val_entry = NULL;
1333   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1334   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1335   descr->dw_loc_oprnd2.val_entry = NULL;
1336   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1337
1338   return descr;
1339 }
1340
1341 /* Return a pointer to a newly allocated location description for
1342    REG and OFFSET.  */
1343
1344 static inline dw_loc_descr_ref
1345 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
1346 {
1347   if (reg <= 31)
1348     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1349                           offset, 0);
1350   else
1351     return new_loc_descr (DW_OP_bregx, reg, offset);
1352 }
1353
1354 /* Add a location description term to a location description expression.  */
1355
1356 static inline void
1357 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1358 {
1359   dw_loc_descr_ref *d;
1360
1361   /* Find the end of the chain.  */
1362   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1363     ;
1364
1365   *d = descr;
1366 }
1367
1368 /* Compare two location operands for exact equality.  */
1369
1370 static bool
1371 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1372 {
1373   if (a->val_class != b->val_class)
1374     return false;
1375   switch (a->val_class)
1376     {
1377     case dw_val_class_none:
1378       return true;
1379     case dw_val_class_addr:
1380       return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1381
1382     case dw_val_class_offset:
1383     case dw_val_class_unsigned_const:
1384     case dw_val_class_const:
1385     case dw_val_class_range_list:
1386     case dw_val_class_lineptr:
1387     case dw_val_class_macptr:
1388       /* These are all HOST_WIDE_INT, signed or unsigned.  */
1389       return a->v.val_unsigned == b->v.val_unsigned;
1390
1391     case dw_val_class_loc:
1392       return a->v.val_loc == b->v.val_loc;
1393     case dw_val_class_loc_list:
1394       return a->v.val_loc_list == b->v.val_loc_list;
1395     case dw_val_class_die_ref:
1396       return a->v.val_die_ref.die == b->v.val_die_ref.die;
1397     case dw_val_class_fde_ref:
1398       return a->v.val_fde_index == b->v.val_fde_index;
1399     case dw_val_class_lbl_id:
1400     case dw_val_class_high_pc:
1401       return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1402     case dw_val_class_str:
1403       return a->v.val_str == b->v.val_str;
1404     case dw_val_class_flag:
1405       return a->v.val_flag == b->v.val_flag;
1406     case dw_val_class_file:
1407       return a->v.val_file == b->v.val_file;
1408     case dw_val_class_decl_ref:
1409       return a->v.val_decl_ref == b->v.val_decl_ref;
1410     
1411     case dw_val_class_const_double:
1412       return (a->v.val_double.high == b->v.val_double.high
1413               && a->v.val_double.low == b->v.val_double.low);
1414
1415     case dw_val_class_wide_int:
1416       return *a->v.val_wide == *b->v.val_wide;
1417
1418     case dw_val_class_vec:
1419       {
1420         size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1421         size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1422
1423         return (a_len == b_len
1424                 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1425       }
1426
1427     case dw_val_class_data8:
1428       return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1429
1430     case dw_val_class_vms_delta:
1431       return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1432               && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1433
1434     case dw_val_class_discr_value:
1435       return (a->v.val_discr_value.pos == b->v.val_discr_value.pos
1436               && a->v.val_discr_value.v.uval == b->v.val_discr_value.v.uval);
1437     case dw_val_class_discr_list:
1438       /* It makes no sense comparing two discriminant value lists.  */
1439       return false;
1440     }
1441   gcc_unreachable ();
1442 }
1443
1444 /* Compare two location atoms for exact equality.  */
1445
1446 static bool
1447 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1448 {
1449   if (a->dw_loc_opc != b->dw_loc_opc)
1450     return false;
1451
1452   /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1453      address size, but since we always allocate cleared storage it
1454      should be zero for other types of locations.  */
1455   if (a->dtprel != b->dtprel)
1456     return false;
1457
1458   return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1459           && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1460 }
1461
1462 /* Compare two complete location expressions for exact equality.  */
1463
1464 bool
1465 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1466 {
1467   while (1)
1468     {
1469       if (a == b)
1470         return true;
1471       if (a == NULL || b == NULL)
1472         return false;
1473       if (!loc_descr_equal_p_1 (a, b))
1474         return false;
1475
1476       a = a->dw_loc_next;
1477       b = b->dw_loc_next;
1478     }
1479 }
1480
1481
1482 /* Add a constant OFFSET to a location expression.  */
1483
1484 static void
1485 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1486 {
1487   dw_loc_descr_ref loc;
1488   HOST_WIDE_INT *p;
1489
1490   gcc_assert (*list_head != NULL);
1491
1492   if (!offset)
1493     return;
1494
1495   /* Find the end of the chain.  */
1496   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1497     ;
1498
1499   p = NULL;
1500   if (loc->dw_loc_opc == DW_OP_fbreg
1501       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1502     p = &loc->dw_loc_oprnd1.v.val_int;
1503   else if (loc->dw_loc_opc == DW_OP_bregx)
1504     p = &loc->dw_loc_oprnd2.v.val_int;
1505
1506   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1507      offset.  Don't optimize if an signed integer overflow would happen.  */
1508   if (p != NULL
1509       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1510           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1511     *p += offset;
1512
1513   else if (offset > 0)
1514     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1515
1516   else
1517     {
1518       loc->dw_loc_next = int_loc_descriptor (-offset);
1519       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1520     }
1521 }
1522
1523 /* Add a constant OFFSET to a location list.  */
1524
1525 static void
1526 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1527 {
1528   dw_loc_list_ref d;
1529   for (d = list_head; d != NULL; d = d->dw_loc_next)
1530     loc_descr_plus_const (&d->expr, offset);
1531 }
1532
1533 #define DWARF_REF_SIZE  \
1534   (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1535
1536 static unsigned long int get_base_type_offset (dw_die_ref);
1537
1538 /* Return the size of a location descriptor.  */
1539
1540 static unsigned long
1541 size_of_loc_descr (dw_loc_descr_ref loc)
1542 {
1543   unsigned long size = 1;
1544
1545   switch (loc->dw_loc_opc)
1546     {
1547     case DW_OP_addr:
1548       size += DWARF2_ADDR_SIZE;
1549       break;
1550     case DW_OP_GNU_addr_index:
1551     case DW_OP_GNU_const_index:
1552       gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1553       size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1554       break;
1555     case DW_OP_const1u:
1556     case DW_OP_const1s:
1557       size += 1;
1558       break;
1559     case DW_OP_const2u:
1560     case DW_OP_const2s:
1561       size += 2;
1562       break;
1563     case DW_OP_const4u:
1564     case DW_OP_const4s:
1565       size += 4;
1566       break;
1567     case DW_OP_const8u:
1568     case DW_OP_const8s:
1569       size += 8;
1570       break;
1571     case DW_OP_constu:
1572       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1573       break;
1574     case DW_OP_consts:
1575       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1576       break;
1577     case DW_OP_pick:
1578       size += 1;
1579       break;
1580     case DW_OP_plus_uconst:
1581       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1582       break;
1583     case DW_OP_skip:
1584     case DW_OP_bra:
1585       size += 2;
1586       break;
1587     case DW_OP_breg0:
1588     case DW_OP_breg1:
1589     case DW_OP_breg2:
1590     case DW_OP_breg3:
1591     case DW_OP_breg4:
1592     case DW_OP_breg5:
1593     case DW_OP_breg6:
1594     case DW_OP_breg7:
1595     case DW_OP_breg8:
1596     case DW_OP_breg9:
1597     case DW_OP_breg10:
1598     case DW_OP_breg11:
1599     case DW_OP_breg12:
1600     case DW_OP_breg13:
1601     case DW_OP_breg14:
1602     case DW_OP_breg15:
1603     case DW_OP_breg16:
1604     case DW_OP_breg17:
1605     case DW_OP_breg18:
1606     case DW_OP_breg19:
1607     case DW_OP_breg20:
1608     case DW_OP_breg21:
1609     case DW_OP_breg22:
1610     case DW_OP_breg23:
1611     case DW_OP_breg24:
1612     case DW_OP_breg25:
1613     case DW_OP_breg26:
1614     case DW_OP_breg27:
1615     case DW_OP_breg28:
1616     case DW_OP_breg29:
1617     case DW_OP_breg30:
1618     case DW_OP_breg31:
1619       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1620       break;
1621     case DW_OP_regx:
1622       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1623       break;
1624     case DW_OP_fbreg:
1625       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1626       break;
1627     case DW_OP_bregx:
1628       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1629       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1630       break;
1631     case DW_OP_piece:
1632       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1633       break;
1634     case DW_OP_bit_piece:
1635       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1636       size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1637       break;
1638     case DW_OP_deref_size:
1639     case DW_OP_xderef_size:
1640       size += 1;
1641       break;
1642     case DW_OP_call2:
1643       size += 2;
1644       break;
1645     case DW_OP_call4:
1646       size += 4;
1647       break;
1648     case DW_OP_call_ref:
1649       size += DWARF_REF_SIZE;
1650       break;
1651     case DW_OP_implicit_value:
1652       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1653               + loc->dw_loc_oprnd1.v.val_unsigned;
1654       break;
1655     case DW_OP_GNU_implicit_pointer:
1656       size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1657       break;
1658     case DW_OP_GNU_entry_value:
1659       {
1660         unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1661         size += size_of_uleb128 (op_size) + op_size;
1662         break;
1663       }
1664     case DW_OP_GNU_const_type:
1665       {
1666         unsigned long o
1667           = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1668         size += size_of_uleb128 (o) + 1;
1669         switch (loc->dw_loc_oprnd2.val_class)
1670           {
1671           case dw_val_class_vec:
1672             size += loc->dw_loc_oprnd2.v.val_vec.length
1673                     * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1674             break;
1675           case dw_val_class_const:
1676             size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1677             break;
1678           case dw_val_class_const_double:
1679             size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1680             break;
1681           case dw_val_class_wide_int:
1682             size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1683                      * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1684             break;
1685           default:
1686             gcc_unreachable ();
1687           }
1688         break;
1689       }
1690     case DW_OP_GNU_regval_type:
1691       {
1692         unsigned long o
1693           = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1694         size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1695                 + size_of_uleb128 (o);
1696       }
1697       break;
1698     case DW_OP_GNU_deref_type:
1699       {
1700         unsigned long o
1701           = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1702         size += 1 + size_of_uleb128 (o);
1703       }
1704       break;
1705     case DW_OP_GNU_convert:
1706     case DW_OP_GNU_reinterpret:
1707       if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1708         size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1709       else
1710         {
1711           unsigned long o
1712             = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1713           size += size_of_uleb128 (o);
1714         }
1715       break;
1716     case DW_OP_GNU_parameter_ref:
1717       size += 4;
1718       break;
1719     default:
1720       break;
1721     }
1722
1723   return size;
1724 }
1725
1726 /* Return the size of a series of location descriptors.  */
1727
1728 unsigned long
1729 size_of_locs (dw_loc_descr_ref loc)
1730 {
1731   dw_loc_descr_ref l;
1732   unsigned long size;
1733
1734   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1735      field, to avoid writing to a PCH file.  */
1736   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1737     {
1738       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1739         break;
1740       size += size_of_loc_descr (l);
1741     }
1742   if (! l)
1743     return size;
1744
1745   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1746     {
1747       l->dw_loc_addr = size;
1748       size += size_of_loc_descr (l);
1749     }
1750
1751   return size;
1752 }
1753
1754 /* Return the size of the value in a DW_AT_discr_value attribute.  */
1755
1756 static int
1757 size_of_discr_value (dw_discr_value *discr_value)
1758 {
1759   if (discr_value->pos)
1760     return size_of_uleb128 (discr_value->v.uval);
1761   else
1762     return size_of_sleb128 (discr_value->v.sval);
1763 }
1764
1765 /* Return the size of the value in a DW_discr_list attribute.  */
1766
1767 static int
1768 size_of_discr_list (dw_discr_list_ref discr_list)
1769 {
1770   int size = 0;
1771
1772   for (dw_discr_list_ref list = discr_list;
1773        list != NULL;
1774        list = list->dw_discr_next)
1775     {
1776       /* One byte for the discriminant value descriptor, and then one or two
1777          LEB128 numbers, depending on whether it's a single case label or a
1778          range label.  */
1779       size += 1;
1780       size += size_of_discr_value (&list->dw_discr_lower_bound);
1781       if (list->dw_discr_range != 0)
1782         size += size_of_discr_value (&list->dw_discr_upper_bound);
1783     }
1784   return size;
1785 }
1786
1787 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1788 static void get_ref_die_offset_label (char *, dw_die_ref);
1789 static unsigned long int get_ref_die_offset (dw_die_ref);
1790
1791 /* Output location description stack opcode's operands (if any).
1792    The for_eh_or_skip parameter controls whether register numbers are
1793    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1794    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1795    info).  This should be suppressed for the cases that have not been converted
1796    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
1797
1798 static void
1799 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1800 {
1801   dw_val_ref val1 = &loc->dw_loc_oprnd1;
1802   dw_val_ref val2 = &loc->dw_loc_oprnd2;
1803
1804   switch (loc->dw_loc_opc)
1805     {
1806 #ifdef DWARF2_DEBUGGING_INFO
1807     case DW_OP_const2u:
1808     case DW_OP_const2s:
1809       dw2_asm_output_data (2, val1->v.val_int, NULL);
1810       break;
1811     case DW_OP_const4u:
1812       if (loc->dtprel)
1813         {
1814           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1815           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1816                                                val1->v.val_addr);
1817           fputc ('\n', asm_out_file);
1818           break;
1819         }
1820       /* FALLTHRU */
1821     case DW_OP_const4s:
1822       dw2_asm_output_data (4, val1->v.val_int, NULL);
1823       break;
1824     case DW_OP_const8u:
1825       if (loc->dtprel)
1826         {
1827           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1828           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1829                                                val1->v.val_addr);
1830           fputc ('\n', asm_out_file);
1831           break;
1832         }
1833       /* FALLTHRU */
1834     case DW_OP_const8s:
1835       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1836       dw2_asm_output_data (8, val1->v.val_int, NULL);
1837       break;
1838     case DW_OP_skip:
1839     case DW_OP_bra:
1840       {
1841         int offset;
1842
1843         gcc_assert (val1->val_class == dw_val_class_loc);
1844         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1845
1846         dw2_asm_output_data (2, offset, NULL);
1847       }
1848       break;
1849     case DW_OP_implicit_value:
1850       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1851       switch (val2->val_class)
1852         {
1853         case dw_val_class_const:
1854           dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1855           break;
1856         case dw_val_class_vec:
1857           {
1858             unsigned int elt_size = val2->v.val_vec.elt_size;
1859             unsigned int len = val2->v.val_vec.length;
1860             unsigned int i;
1861             unsigned char *p;
1862
1863             if (elt_size > sizeof (HOST_WIDE_INT))
1864               {
1865                 elt_size /= 2;
1866                 len *= 2;
1867               }
1868             for (i = 0, p = val2->v.val_vec.array;
1869                  i < len;
1870                  i++, p += elt_size)
1871               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1872                                    "fp or vector constant word %u", i);
1873           }
1874           break;
1875         case dw_val_class_const_double:
1876           {
1877             unsigned HOST_WIDE_INT first, second;
1878
1879             if (WORDS_BIG_ENDIAN)
1880               {
1881                 first = val2->v.val_double.high;
1882                 second = val2->v.val_double.low;
1883               }
1884             else
1885               {
1886                 first = val2->v.val_double.low;
1887                 second = val2->v.val_double.high;
1888               }
1889             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1890                                  first, NULL);
1891             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1892                                  second, NULL);
1893           }
1894           break;
1895         case dw_val_class_wide_int:
1896           {
1897             int i;
1898             int len = get_full_len (*val2->v.val_wide);
1899             if (WORDS_BIG_ENDIAN)
1900               for (i = len - 1; i >= 0; --i)
1901                 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1902                                      val2->v.val_wide->elt (i), NULL);
1903             else
1904               for (i = 0; i < len; ++i)
1905                 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1906                                      val2->v.val_wide->elt (i), NULL);
1907           }
1908           break;
1909         case dw_val_class_addr:
1910           gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1911           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1912           break;
1913         default:
1914           gcc_unreachable ();
1915         }
1916       break;
1917 #else
1918     case DW_OP_const2u:
1919     case DW_OP_const2s:
1920     case DW_OP_const4u:
1921     case DW_OP_const4s:
1922     case DW_OP_const8u:
1923     case DW_OP_const8s:
1924     case DW_OP_skip:
1925     case DW_OP_bra:
1926     case DW_OP_implicit_value:
1927       /* We currently don't make any attempt to make sure these are
1928          aligned properly like we do for the main unwind info, so
1929          don't support emitting things larger than a byte if we're
1930          only doing unwinding.  */
1931       gcc_unreachable ();
1932 #endif
1933     case DW_OP_const1u:
1934     case DW_OP_const1s:
1935       dw2_asm_output_data (1, val1->v.val_int, NULL);
1936       break;
1937     case DW_OP_constu:
1938       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1939       break;
1940     case DW_OP_consts:
1941       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1942       break;
1943     case DW_OP_pick:
1944       dw2_asm_output_data (1, val1->v.val_int, NULL);
1945       break;
1946     case DW_OP_plus_uconst:
1947       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1948       break;
1949     case DW_OP_breg0:
1950     case DW_OP_breg1:
1951     case DW_OP_breg2:
1952     case DW_OP_breg3:
1953     case DW_OP_breg4:
1954     case DW_OP_breg5:
1955     case DW_OP_breg6:
1956     case DW_OP_breg7:
1957     case DW_OP_breg8:
1958     case DW_OP_breg9:
1959     case DW_OP_breg10:
1960     case DW_OP_breg11:
1961     case DW_OP_breg12:
1962     case DW_OP_breg13:
1963     case DW_OP_breg14:
1964     case DW_OP_breg15:
1965     case DW_OP_breg16:
1966     case DW_OP_breg17:
1967     case DW_OP_breg18:
1968     case DW_OP_breg19:
1969     case DW_OP_breg20:
1970     case DW_OP_breg21:
1971     case DW_OP_breg22:
1972     case DW_OP_breg23:
1973     case DW_OP_breg24:
1974     case DW_OP_breg25:
1975     case DW_OP_breg26:
1976     case DW_OP_breg27:
1977     case DW_OP_breg28:
1978     case DW_OP_breg29:
1979     case DW_OP_breg30:
1980     case DW_OP_breg31:
1981       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1982       break;
1983     case DW_OP_regx:
1984       {
1985         unsigned r = val1->v.val_unsigned;
1986         if (for_eh_or_skip >= 0)
1987           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1988         gcc_assert (size_of_uleb128 (r) 
1989                     == size_of_uleb128 (val1->v.val_unsigned));
1990         dw2_asm_output_data_uleb128 (r, NULL);  
1991       }
1992       break;
1993     case DW_OP_fbreg:
1994       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1995       break;
1996     case DW_OP_bregx:
1997       {
1998         unsigned r = val1->v.val_unsigned;
1999         if (for_eh_or_skip >= 0)
2000           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2001         gcc_assert (size_of_uleb128 (r) 
2002                     == size_of_uleb128 (val1->v.val_unsigned));
2003         dw2_asm_output_data_uleb128 (r, NULL);  
2004         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2005       }
2006       break;
2007     case DW_OP_piece:
2008       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2009       break;
2010     case DW_OP_bit_piece:
2011       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2012       dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
2013       break;
2014     case DW_OP_deref_size:
2015     case DW_OP_xderef_size:
2016       dw2_asm_output_data (1, val1->v.val_int, NULL);
2017       break;
2018
2019     case DW_OP_addr:
2020       if (loc->dtprel)
2021         {
2022           if (targetm.asm_out.output_dwarf_dtprel)
2023             {
2024               targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2025                                                    DWARF2_ADDR_SIZE,
2026                                                    val1->v.val_addr);
2027               fputc ('\n', asm_out_file);
2028             }
2029           else
2030             gcc_unreachable ();
2031         }
2032       else
2033         {
2034 #ifdef DWARF2_DEBUGGING_INFO
2035           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2036 #else
2037           gcc_unreachable ();
2038 #endif
2039         }
2040       break;
2041
2042     case DW_OP_GNU_addr_index:
2043     case DW_OP_GNU_const_index:
2044       gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2045       dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2046                                    "(index into .debug_addr)");
2047       break;
2048
2049     case DW_OP_call2:
2050     case DW_OP_call4:
2051       {
2052         unsigned long die_offset
2053           = get_ref_die_offset (val1->v.val_die_ref.die);
2054         /* Make sure the offset has been computed and that we can encode it as
2055            an operand.  */
2056         gcc_assert (die_offset > 0
2057                     && die_offset <= (loc->dw_loc_opc == DW_OP_call2)
2058                                      ? 0xffff
2059                                      : 0xffffffff);
2060         dw2_asm_output_data ((loc->dw_loc_opc == DW_OP_call2) ? 2 : 4,
2061                              die_offset, NULL);
2062       }
2063       break;
2064
2065     case DW_OP_GNU_implicit_pointer:
2066       {
2067         char label[MAX_ARTIFICIAL_LABEL_BYTES
2068                    + HOST_BITS_PER_WIDE_INT / 2 + 2];
2069         gcc_assert (val1->val_class == dw_val_class_die_ref);
2070         get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2071         dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2072         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2073       }
2074       break;
2075
2076     case DW_OP_GNU_entry_value:
2077       dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2078       output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2079       break;
2080
2081     case DW_OP_GNU_const_type:
2082       {
2083         unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2084         gcc_assert (o);
2085         dw2_asm_output_data_uleb128 (o, NULL);
2086         switch (val2->val_class)
2087           {
2088           case dw_val_class_const:
2089             l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2090             dw2_asm_output_data (1, l, NULL);
2091             dw2_asm_output_data (l, val2->v.val_int, NULL);
2092             break;
2093           case dw_val_class_vec:
2094             {
2095               unsigned int elt_size = val2->v.val_vec.elt_size;
2096               unsigned int len = val2->v.val_vec.length;
2097               unsigned int i;
2098               unsigned char *p;
2099
2100               l = len * elt_size;
2101               dw2_asm_output_data (1, l, NULL);
2102               if (elt_size > sizeof (HOST_WIDE_INT))
2103                 {
2104                   elt_size /= 2;
2105                   len *= 2;
2106                 }
2107               for (i = 0, p = val2->v.val_vec.array;
2108                    i < len;
2109                    i++, p += elt_size)
2110                 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2111                                      "fp or vector constant word %u", i);
2112             }
2113             break;
2114           case dw_val_class_const_double:
2115             {
2116               unsigned HOST_WIDE_INT first, second;
2117               l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2118
2119               dw2_asm_output_data (1, 2 * l, NULL);
2120               if (WORDS_BIG_ENDIAN)
2121                 {
2122                   first = val2->v.val_double.high;
2123                   second = val2->v.val_double.low;
2124                 }
2125               else
2126                 {
2127                   first = val2->v.val_double.low;
2128                   second = val2->v.val_double.high;
2129                 }
2130               dw2_asm_output_data (l, first, NULL);
2131               dw2_asm_output_data (l, second, NULL);
2132             }
2133             break;
2134           case dw_val_class_wide_int:
2135             {
2136               int i;
2137               int len = get_full_len (*val2->v.val_wide);
2138               l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2139
2140               dw2_asm_output_data (1, len * l, NULL);
2141               if (WORDS_BIG_ENDIAN)
2142                 for (i = len - 1; i >= 0; --i)
2143                   dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2144               else
2145                 for (i = 0; i < len; ++i)
2146                   dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2147             }
2148             break;
2149           default:
2150             gcc_unreachable ();
2151           }
2152       }
2153       break;
2154     case DW_OP_GNU_regval_type:
2155       {
2156         unsigned r = val1->v.val_unsigned;
2157         unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2158         gcc_assert (o);
2159         if (for_eh_or_skip >= 0)
2160           {
2161             r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2162             gcc_assert (size_of_uleb128 (r)
2163                         == size_of_uleb128 (val1->v.val_unsigned));
2164           }
2165         dw2_asm_output_data_uleb128 (r, NULL);
2166         dw2_asm_output_data_uleb128 (o, NULL);
2167       }
2168       break;
2169     case DW_OP_GNU_deref_type:
2170       {
2171         unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2172         gcc_assert (o);
2173         dw2_asm_output_data (1, val1->v.val_int, NULL);
2174         dw2_asm_output_data_uleb128 (o, NULL);
2175       }
2176       break;
2177     case DW_OP_GNU_convert:
2178     case DW_OP_GNU_reinterpret:
2179       if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2180         dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2181       else
2182         {
2183           unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2184           gcc_assert (o);
2185           dw2_asm_output_data_uleb128 (o, NULL);
2186         }
2187       break;
2188
2189     case DW_OP_GNU_parameter_ref:
2190       {
2191         unsigned long o;
2192         gcc_assert (val1->val_class == dw_val_class_die_ref);
2193         o = get_ref_die_offset (val1->v.val_die_ref.die);
2194         dw2_asm_output_data (4, o, NULL);
2195       }
2196       break;
2197
2198     default:
2199       /* Other codes have no operands.  */
2200       break;
2201     }
2202 }
2203
2204 /* Output a sequence of location operations.  
2205    The for_eh_or_skip parameter controls whether register numbers are
2206    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2207    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2208    info).  This should be suppressed for the cases that have not been converted
2209    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
2210
2211 void
2212 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2213 {
2214   for (; loc != NULL; loc = loc->dw_loc_next)
2215     {
2216       enum dwarf_location_atom opc = loc->dw_loc_opc;
2217       /* Output the opcode.  */
2218       if (for_eh_or_skip >= 0 
2219           && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2220         {
2221           unsigned r = (opc - DW_OP_breg0);
2222           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2223           gcc_assert (r <= 31);
2224           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2225         }
2226       else if (for_eh_or_skip >= 0 
2227                && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2228         {
2229           unsigned r = (opc - DW_OP_reg0);
2230           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2231           gcc_assert (r <= 31);
2232           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2233         }
2234
2235       dw2_asm_output_data (1, opc,
2236                              "%s", dwarf_stack_op_name (opc));
2237
2238       /* Output the operand(s) (if any).  */
2239       output_loc_operands (loc, for_eh_or_skip);
2240     }
2241 }
2242
2243 /* Output location description stack opcode's operands (if any).
2244    The output is single bytes on a line, suitable for .cfi_escape.  */
2245
2246 static void
2247 output_loc_operands_raw (dw_loc_descr_ref loc)
2248 {
2249   dw_val_ref val1 = &loc->dw_loc_oprnd1;
2250   dw_val_ref val2 = &loc->dw_loc_oprnd2;
2251
2252   switch (loc->dw_loc_opc)
2253     {
2254     case DW_OP_addr:
2255     case DW_OP_GNU_addr_index:
2256     case DW_OP_GNU_const_index:
2257     case DW_OP_implicit_value:
2258       /* We cannot output addresses in .cfi_escape, only bytes.  */
2259       gcc_unreachable ();
2260
2261     case DW_OP_const1u:
2262     case DW_OP_const1s:
2263     case DW_OP_pick:
2264     case DW_OP_deref_size:
2265     case DW_OP_xderef_size:
2266       fputc (',', asm_out_file);
2267       dw2_asm_output_data_raw (1, val1->v.val_int);
2268       break;
2269
2270     case DW_OP_const2u:
2271     case DW_OP_const2s:
2272       fputc (',', asm_out_file);
2273       dw2_asm_output_data_raw (2, val1->v.val_int);
2274       break;
2275
2276     case DW_OP_const4u:
2277     case DW_OP_const4s:
2278       fputc (',', asm_out_file);
2279       dw2_asm_output_data_raw (4, val1->v.val_int);
2280       break;
2281
2282     case DW_OP_const8u:
2283     case DW_OP_const8s:
2284       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2285       fputc (',', asm_out_file);
2286       dw2_asm_output_data_raw (8, val1->v.val_int);
2287       break;
2288
2289     case DW_OP_skip:
2290     case DW_OP_bra:
2291       {
2292         int offset;
2293
2294         gcc_assert (val1->val_class == dw_val_class_loc);
2295         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2296
2297         fputc (',', asm_out_file);
2298         dw2_asm_output_data_raw (2, offset);
2299       }
2300       break;
2301
2302     case DW_OP_regx:
2303       {
2304         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2305         gcc_assert (size_of_uleb128 (r) 
2306                     == size_of_uleb128 (val1->v.val_unsigned));
2307         fputc (',', asm_out_file);
2308         dw2_asm_output_data_uleb128_raw (r);
2309       }
2310       break;
2311       
2312     case DW_OP_constu:
2313     case DW_OP_plus_uconst:
2314     case DW_OP_piece:
2315       fputc (',', asm_out_file);
2316       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2317       break;
2318
2319     case DW_OP_bit_piece:
2320       fputc (',', asm_out_file);
2321       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2322       dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2323       break;
2324
2325     case DW_OP_consts:
2326     case DW_OP_breg0:
2327     case DW_OP_breg1:
2328     case DW_OP_breg2:
2329     case DW_OP_breg3:
2330     case DW_OP_breg4:
2331     case DW_OP_breg5:
2332     case DW_OP_breg6:
2333     case DW_OP_breg7:
2334     case DW_OP_breg8:
2335     case DW_OP_breg9:
2336     case DW_OP_breg10:
2337     case DW_OP_breg11:
2338     case DW_OP_breg12:
2339     case DW_OP_breg13:
2340     case DW_OP_breg14:
2341     case DW_OP_breg15:
2342     case DW_OP_breg16:
2343     case DW_OP_breg17:
2344     case DW_OP_breg18:
2345     case DW_OP_breg19:
2346     case DW_OP_breg20:
2347     case DW_OP_breg21:
2348     case DW_OP_breg22:
2349     case DW_OP_breg23:
2350     case DW_OP_breg24:
2351     case DW_OP_breg25:
2352     case DW_OP_breg26:
2353     case DW_OP_breg27:
2354     case DW_OP_breg28:
2355     case DW_OP_breg29:
2356     case DW_OP_breg30:
2357     case DW_OP_breg31:
2358     case DW_OP_fbreg:
2359       fputc (',', asm_out_file);
2360       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2361       break;
2362
2363     case DW_OP_bregx:
2364       {
2365         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2366         gcc_assert (size_of_uleb128 (r) 
2367                     == size_of_uleb128 (val1->v.val_unsigned));
2368         fputc (',', asm_out_file);
2369         dw2_asm_output_data_uleb128_raw (r);
2370         fputc (',', asm_out_file);
2371         dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2372       }
2373       break;
2374
2375     case DW_OP_GNU_implicit_pointer:
2376     case DW_OP_GNU_entry_value:
2377     case DW_OP_GNU_const_type:
2378     case DW_OP_GNU_regval_type:
2379     case DW_OP_GNU_deref_type:
2380     case DW_OP_GNU_convert:
2381     case DW_OP_GNU_reinterpret:
2382     case DW_OP_GNU_parameter_ref:
2383       gcc_unreachable ();
2384       break;
2385
2386     default:
2387       /* Other codes have no operands.  */
2388       break;
2389     }
2390 }
2391
2392 void
2393 output_loc_sequence_raw (dw_loc_descr_ref loc)
2394 {
2395   while (1)
2396     {
2397       enum dwarf_location_atom opc = loc->dw_loc_opc;
2398       /* Output the opcode.  */
2399       if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2400         {
2401           unsigned r = (opc - DW_OP_breg0);
2402           r = DWARF2_FRAME_REG_OUT (r, 1);
2403           gcc_assert (r <= 31);
2404           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2405         }
2406       else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2407         {
2408           unsigned r = (opc - DW_OP_reg0);
2409           r = DWARF2_FRAME_REG_OUT (r, 1);
2410           gcc_assert (r <= 31);
2411           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2412         }
2413       /* Output the opcode.  */
2414       fprintf (asm_out_file, "%#x", opc);
2415       output_loc_operands_raw (loc);
2416
2417       if (!loc->dw_loc_next)
2418         break;
2419       loc = loc->dw_loc_next;
2420
2421       fputc (',', asm_out_file);
2422     }
2423 }
2424
2425 /* This function builds a dwarf location descriptor sequence from a
2426    dw_cfa_location, adding the given OFFSET to the result of the
2427    expression.  */
2428
2429 struct dw_loc_descr_node *
2430 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2431 {
2432   struct dw_loc_descr_node *head, *tmp;
2433
2434   offset += cfa->offset;
2435
2436   if (cfa->indirect)
2437     {
2438       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2439       head->dw_loc_oprnd1.val_class = dw_val_class_const;
2440       head->dw_loc_oprnd1.val_entry = NULL;
2441       tmp = new_loc_descr (DW_OP_deref, 0, 0);
2442       add_loc_descr (&head, tmp);
2443       if (offset != 0)
2444         {
2445           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2446           add_loc_descr (&head, tmp);
2447         }
2448     }
2449   else
2450     head = new_reg_loc_descr (cfa->reg, offset);
2451
2452   return head;
2453 }
2454
2455 /* This function builds a dwarf location descriptor sequence for
2456    the address at OFFSET from the CFA when stack is aligned to
2457    ALIGNMENT byte.  */
2458
2459 struct dw_loc_descr_node *
2460 build_cfa_aligned_loc (dw_cfa_location *cfa,
2461                        HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2462 {
2463   struct dw_loc_descr_node *head;
2464   unsigned int dwarf_fp
2465     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2466
2467   /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
2468   if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2469     {
2470       head = new_reg_loc_descr (dwarf_fp, 0);
2471       add_loc_descr (&head, int_loc_descriptor (alignment));
2472       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2473       loc_descr_plus_const (&head, offset);
2474     }
2475   else
2476     head = new_reg_loc_descr (dwarf_fp, offset);
2477   return head;
2478 }
2479 \f
2480 /* And now, the support for symbolic debugging information.  */
2481
2482 /* .debug_str support.  */
2483
2484 static void dwarf2out_init (const char *);
2485 static void dwarf2out_finish (const char *);
2486 static void dwarf2out_early_finish (void);
2487 static void dwarf2out_assembly_start (void);
2488 static void dwarf2out_define (unsigned int, const char *);
2489 static void dwarf2out_undef (unsigned int, const char *);
2490 static void dwarf2out_start_source_file (unsigned, const char *);
2491 static void dwarf2out_end_source_file (unsigned);
2492 static void dwarf2out_function_decl (tree);
2493 static void dwarf2out_begin_block (unsigned, unsigned);
2494 static void dwarf2out_end_block (unsigned, unsigned);
2495 static bool dwarf2out_ignore_block (const_tree);
2496 static void dwarf2out_early_global_decl (tree);
2497 static void dwarf2out_late_global_decl (tree);
2498 static void dwarf2out_type_decl (tree, int);
2499 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2500 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2501                                                  dw_die_ref);
2502 static void dwarf2out_abstract_function (tree);
2503 static void dwarf2out_var_location (rtx_insn *);
2504 static void dwarf2out_size_function (tree);
2505 static void dwarf2out_begin_function (tree);
2506 static void dwarf2out_end_function (unsigned int);
2507 static void dwarf2out_register_main_translation_unit (tree unit);
2508 static void dwarf2out_set_name (tree, tree);
2509
2510 /* The debug hooks structure.  */
2511
2512 const struct gcc_debug_hooks dwarf2_debug_hooks =
2513 {
2514   dwarf2out_init,
2515   dwarf2out_finish,
2516   dwarf2out_early_finish,
2517   dwarf2out_assembly_start,
2518   dwarf2out_define,
2519   dwarf2out_undef,
2520   dwarf2out_start_source_file,
2521   dwarf2out_end_source_file,
2522   dwarf2out_begin_block,
2523   dwarf2out_end_block,
2524   dwarf2out_ignore_block,
2525   dwarf2out_source_line,
2526   dwarf2out_begin_prologue,
2527 #if VMS_DEBUGGING_INFO
2528   dwarf2out_vms_end_prologue,
2529   dwarf2out_vms_begin_epilogue,
2530 #else
2531   debug_nothing_int_charstar,
2532   debug_nothing_int_charstar,
2533 #endif
2534   dwarf2out_end_epilogue,
2535   dwarf2out_begin_function,
2536   dwarf2out_end_function,       /* end_function */
2537   dwarf2out_register_main_translation_unit,
2538   dwarf2out_function_decl,      /* function_decl */
2539   dwarf2out_early_global_decl,
2540   dwarf2out_late_global_decl,
2541   dwarf2out_type_decl,          /* type_decl */
2542   dwarf2out_imported_module_or_decl,
2543   debug_nothing_tree,           /* deferred_inline_function */
2544   /* The DWARF 2 backend tries to reduce debugging bloat by not
2545      emitting the abstract description of inline functions until
2546      something tries to reference them.  */
2547   dwarf2out_abstract_function,  /* outlining_inline_function */
2548   debug_nothing_rtx_code_label, /* label */
2549   debug_nothing_int,            /* handle_pch */
2550   dwarf2out_var_location,
2551   dwarf2out_size_function,      /* size_function */
2552   dwarf2out_switch_text_section,
2553   dwarf2out_set_name,
2554   1,                            /* start_end_main_source_file */
2555   TYPE_SYMTAB_IS_DIE            /* tree_type_symtab_field */
2556 };
2557
2558 const struct gcc_debug_hooks dwarf2_lineno_debug_hooks =
2559 {
2560   dwarf2out_init,
2561   debug_nothing_charstar,
2562   debug_nothing_void,
2563   debug_nothing_void,
2564   debug_nothing_int_charstar,
2565   debug_nothing_int_charstar,
2566   debug_nothing_int_charstar,
2567   debug_nothing_int,
2568   debug_nothing_int_int,                 /* begin_block */
2569   debug_nothing_int_int,                 /* end_block */
2570   debug_true_const_tree,                 /* ignore_block */
2571   dwarf2out_source_line,         /* source_line */
2572   debug_nothing_int_charstar,            /* begin_prologue */
2573   debug_nothing_int_charstar,            /* end_prologue */
2574   debug_nothing_int_charstar,            /* begin_epilogue */
2575   debug_nothing_int_charstar,            /* end_epilogue */
2576   debug_nothing_tree,                    /* begin_function */
2577   debug_nothing_int,                     /* end_function */
2578   debug_nothing_tree,                    /* register_main_translation_unit */
2579   debug_nothing_tree,                    /* function_decl */
2580   debug_nothing_tree,                    /* early_global_decl */
2581   debug_nothing_tree,                    /* late_global_decl */
2582   debug_nothing_tree_int,                /* type_decl */
2583   debug_nothing_tree_tree_tree_bool,     /* imported_module_or_decl */
2584   debug_nothing_tree,                    /* deferred_inline_function */
2585   debug_nothing_tree,                    /* outlining_inline_function */
2586   debug_nothing_rtx_code_label,          /* label */
2587   debug_nothing_int,                     /* handle_pch */
2588   debug_nothing_rtx_insn,                /* var_location */
2589   debug_nothing_tree,                    /* size_function */
2590   debug_nothing_void,                    /* switch_text_section */
2591   debug_nothing_tree_tree,               /* set_name */
2592   0,                                     /* start_end_main_source_file */
2593   TYPE_SYMTAB_IS_ADDRESS                 /* tree_type_symtab_field */
2594 };
2595 \f
2596 /* NOTE: In the comments in this file, many references are made to
2597    "Debugging Information Entries".  This term is abbreviated as `DIE'
2598    throughout the remainder of this file.  */
2599
2600 /* An internal representation of the DWARF output is built, and then
2601    walked to generate the DWARF debugging info.  The walk of the internal
2602    representation is done after the entire program has been compiled.
2603    The types below are used to describe the internal representation.  */
2604
2605 /* Whether to put type DIEs into their own section .debug_types instead
2606    of making them part of the .debug_info section.  Only supported for
2607    Dwarf V4 or higher and the user didn't disable them through
2608    -fno-debug-types-section.  It is more efficient to put them in a
2609    separate comdat sections since the linker will then be able to
2610    remove duplicates.  But not all tools support .debug_types sections
2611    yet.  */
2612
2613 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2614
2615 /* Various DIE's use offsets relative to the beginning of the
2616    .debug_info section to refer to each other.  */
2617
2618 typedef long int dw_offset;
2619
2620 struct comdat_type_node;
2621
2622 /* The entries in the line_info table more-or-less mirror the opcodes
2623    that are used in the real dwarf line table.  Arrays of these entries
2624    are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2625    supported.  */
2626
2627 enum dw_line_info_opcode {
2628   /* Emit DW_LNE_set_address; the operand is the label index.  */
2629   LI_set_address,
2630
2631   /* Emit a row to the matrix with the given line.  This may be done
2632      via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2633      special opcodes.  */
2634   LI_set_line,
2635
2636   /* Emit a DW_LNS_set_file.  */
2637   LI_set_file,
2638
2639   /* Emit a DW_LNS_set_column.  */
2640   LI_set_column,
2641
2642   /* Emit a DW_LNS_negate_stmt; the operand is ignored.  */
2643   LI_negate_stmt,
2644
2645   /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored.  */
2646   LI_set_prologue_end,
2647   LI_set_epilogue_begin,
2648
2649   /* Emit a DW_LNE_set_discriminator.  */
2650   LI_set_discriminator
2651 };
2652
2653 typedef struct GTY(()) dw_line_info_struct {
2654   enum dw_line_info_opcode opcode;
2655   unsigned int val;
2656 } dw_line_info_entry;
2657
2658
2659 struct GTY(()) dw_line_info_table {
2660   /* The label that marks the end of this section.  */
2661   const char *end_label;
2662
2663   /* The values for the last row of the matrix, as collected in the table.
2664      These are used to minimize the changes to the next row.  */
2665   unsigned int file_num;
2666   unsigned int line_num;
2667   unsigned int column_num;
2668   int discrim_num;
2669   bool is_stmt;
2670   bool in_use;
2671
2672   vec<dw_line_info_entry, va_gc> *entries;
2673 };
2674
2675
2676 /* Each DIE attribute has a field specifying the attribute kind,
2677    a link to the next attribute in the chain, and an attribute value.
2678    Attributes are typically linked below the DIE they modify.  */
2679
2680 typedef struct GTY(()) dw_attr_struct {
2681   enum dwarf_attribute dw_attr;
2682   dw_val_node dw_attr_val;
2683 }
2684 dw_attr_node;
2685
2686
2687 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
2688    The children of each node form a circular list linked by
2689    die_sib.  die_child points to the node *before* the "first" child node.  */
2690
2691 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
2692   union die_symbol_or_type_node
2693     {
2694       const char * GTY ((tag ("0"))) die_symbol;
2695       comdat_type_node *GTY ((tag ("1"))) die_type_node;
2696     }
2697   GTY ((desc ("%0.comdat_type_p"))) die_id;
2698   vec<dw_attr_node, va_gc> *die_attr;
2699   dw_die_ref die_parent;
2700   dw_die_ref die_child;
2701   dw_die_ref die_sib;
2702   dw_die_ref die_definition; /* ref from a specification to its definition */
2703   dw_offset die_offset;
2704   unsigned long die_abbrev;
2705   int die_mark;
2706   unsigned int decl_id;
2707   enum dwarf_tag die_tag;
2708   /* Die is used and must not be pruned as unused.  */
2709   BOOL_BITFIELD die_perennial_p : 1;
2710   BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2711   /* Lots of spare bits.  */
2712 }
2713 die_node;
2714
2715 /* Set to TRUE while dwarf2out_early_global_decl is running.  */
2716 static bool early_dwarf;
2717 struct set_early_dwarf {
2718   bool saved;
2719   set_early_dwarf () : saved(early_dwarf) { early_dwarf = true; }
2720   ~set_early_dwarf () { early_dwarf = saved; }
2721 };
2722
2723 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
2724 #define FOR_EACH_CHILD(die, c, expr) do {       \
2725   c = die->die_child;                           \
2726   if (c) do {                                   \
2727     c = c->die_sib;                             \
2728     expr;                                       \
2729   } while (c != die->die_child);                \
2730 } while (0)
2731
2732 /* The pubname structure */
2733
2734 typedef struct GTY(()) pubname_struct {
2735   dw_die_ref die;
2736   const char *name;
2737 }
2738 pubname_entry;
2739
2740
2741 struct GTY(()) dw_ranges {
2742   /* If this is positive, it's a block number, otherwise it's a
2743      bitwise-negated index into dw_ranges_by_label.  */
2744   int num;
2745 };
2746
2747 /* A structure to hold a macinfo entry.  */
2748
2749 typedef struct GTY(()) macinfo_struct {
2750   unsigned char code;
2751   unsigned HOST_WIDE_INT lineno;
2752   const char *info;
2753 }
2754 macinfo_entry;
2755
2756
2757 struct GTY(()) dw_ranges_by_label {
2758   const char *begin;
2759   const char *end;
2760 };
2761
2762 /* The comdat type node structure.  */
2763 struct GTY(()) comdat_type_node
2764 {
2765   dw_die_ref root_die;
2766   dw_die_ref type_die;
2767   dw_die_ref skeleton_die;
2768   char signature[DWARF_TYPE_SIGNATURE_SIZE];
2769   comdat_type_node *next;
2770 };
2771
2772 /* A list of DIEs for which we can't determine ancestry (parent_die
2773    field) just yet.  Later in dwarf2out_finish we will fill in the
2774    missing bits.  */
2775 typedef struct GTY(()) limbo_die_struct {
2776   dw_die_ref die;
2777   /* The tree for which this DIE was created.  We use this to
2778      determine ancestry later.  */
2779   tree created_for;
2780   struct limbo_die_struct *next;
2781 }
2782 limbo_die_node;
2783
2784 typedef struct skeleton_chain_struct
2785 {
2786   dw_die_ref old_die;
2787   dw_die_ref new_die;
2788   struct skeleton_chain_struct *parent;
2789 }
2790 skeleton_chain_node;
2791
2792 /* Define a macro which returns nonzero for a TYPE_DECL which was
2793    implicitly generated for a type.
2794
2795    Note that, unlike the C front-end (which generates a NULL named
2796    TYPE_DECL node for each complete tagged type, each array type,
2797    and each function type node created) the C++ front-end generates
2798    a _named_ TYPE_DECL node for each tagged type node created.
2799    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2800    generate a DW_TAG_typedef DIE for them.  Likewise with the Ada
2801    front-end, but for each type, tagged or not.  */
2802
2803 #define TYPE_DECL_IS_STUB(decl)                         \
2804   (DECL_NAME (decl) == NULL_TREE                        \
2805    || (DECL_ARTIFICIAL (decl)                           \
2806        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
2807            /* This is necessary for stub decls that     \
2808               appear in nested inline functions.  */    \
2809            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2810                && (decl_ultimate_origin (decl)          \
2811                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2812
2813 /* Information concerning the compilation unit's programming
2814    language, and compiler version.  */
2815
2816 /* Fixed size portion of the DWARF compilation unit header.  */
2817 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2818   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2819
2820 /* Fixed size portion of the DWARF comdat type unit header.  */
2821 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2822   (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2823    + DWARF_OFFSET_SIZE)
2824
2825 /* Fixed size portion of public names info.  */
2826 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2827
2828 /* Fixed size portion of the address range info.  */
2829 #define DWARF_ARANGES_HEADER_SIZE                                       \
2830   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
2831                 DWARF2_ADDR_SIZE * 2)                                   \
2832    - DWARF_INITIAL_LENGTH_SIZE)
2833
2834 /* Size of padding portion in the address range info.  It must be
2835    aligned to twice the pointer size.  */
2836 #define DWARF_ARANGES_PAD_SIZE \
2837   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2838                 DWARF2_ADDR_SIZE * 2)                              \
2839    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2840
2841 /* Use assembler line directives if available.  */
2842 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2843 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2844 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2845 #else
2846 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2847 #endif
2848 #endif
2849
2850 /* Minimum line offset in a special line info. opcode.
2851    This value was chosen to give a reasonable range of values.  */
2852 #define DWARF_LINE_BASE  -10
2853
2854 /* First special line opcode - leave room for the standard opcodes.  */
2855 #define DWARF_LINE_OPCODE_BASE  ((int)DW_LNS_set_isa + 1)
2856
2857 /* Range of line offsets in a special line info. opcode.  */
2858 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
2859
2860 /* Flag that indicates the initial value of the is_stmt_start flag.
2861    In the present implementation, we do not mark any lines as
2862    the beginning of a source statement, because that information
2863    is not made available by the GCC front-end.  */
2864 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2865
2866 /* Maximum number of operations per instruction bundle.  */
2867 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2868 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2869 #endif
2870
2871 /* This location is used by calc_die_sizes() to keep track
2872    the offset of each DIE within the .debug_info section.  */
2873 static unsigned long next_die_offset;
2874
2875 /* Record the root of the DIE's built for the current compilation unit.  */
2876 static GTY(()) dw_die_ref single_comp_unit_die;
2877
2878 /* A list of type DIEs that have been separated into comdat sections.  */
2879 static GTY(()) comdat_type_node *comdat_type_list;
2880
2881 /* A list of DIEs with a NULL parent waiting to be relocated.  */
2882 static GTY(()) limbo_die_node *limbo_die_list;
2883
2884 /* A list of DIEs for which we may have to generate
2885    DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set.  */
2886 static GTY(()) limbo_die_node *deferred_asm_name;
2887
2888 struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
2889 {
2890   typedef const char *compare_type;
2891
2892   static hashval_t hash (dwarf_file_data *);
2893   static bool equal (dwarf_file_data *, const char *);
2894 };
2895
2896 /* Filenames referenced by this compilation unit.  */
2897 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
2898
2899 struct decl_die_hasher : ggc_ptr_hash<die_node>
2900 {
2901   typedef tree compare_type;
2902
2903   static hashval_t hash (die_node *);
2904   static bool equal (die_node *, tree);
2905 };
2906 /* A hash table of references to DIE's that describe declarations.
2907    The key is a DECL_UID() which is a unique number identifying each decl.  */
2908 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
2909
2910 struct block_die_hasher : ggc_ptr_hash<die_struct>
2911 {
2912   static hashval_t hash (die_struct *);
2913   static bool equal (die_struct *, die_struct *);
2914 };
2915
2916 /* A hash table of references to DIE's that describe COMMON blocks.
2917    The key is DECL_UID() ^ die_parent.  */
2918 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
2919
2920 typedef struct GTY(()) die_arg_entry_struct {
2921     dw_die_ref die;
2922     tree arg;
2923 } die_arg_entry;
2924
2925
2926 /* Node of the variable location list.  */
2927 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2928   /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2929      EXPR_LIST chain.  For small bitsizes, bitsize is encoded
2930      in mode of the EXPR_LIST node and first EXPR_LIST operand
2931      is either NOTE_INSN_VAR_LOCATION for a piece with a known
2932      location or NULL for padding.  For larger bitsizes,
2933      mode is 0 and first operand is a CONCAT with bitsize
2934      as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2935      NULL as second operand.  */
2936   rtx GTY (()) loc;
2937   const char * GTY (()) label;
2938   struct var_loc_node * GTY (()) next;
2939 };
2940
2941 /* Variable location list.  */
2942 struct GTY ((for_user)) var_loc_list_def {
2943   struct var_loc_node * GTY (()) first;
2944
2945   /* Pointer to the last but one or last element of the
2946      chained list.  If the list is empty, both first and
2947      last are NULL, if the list contains just one node
2948      or the last node certainly is not redundant, it points
2949      to the last node, otherwise points to the last but one.
2950      Do not mark it for GC because it is marked through the chain.  */
2951   struct var_loc_node * GTY ((skip ("%h"))) last;
2952
2953   /* Pointer to the last element before section switch,
2954      if NULL, either sections weren't switched or first
2955      is after section switch.  */
2956   struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2957
2958   /* DECL_UID of the variable decl.  */
2959   unsigned int decl_id;
2960 };
2961 typedef struct var_loc_list_def var_loc_list;
2962
2963 /* Call argument location list.  */
2964 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2965   rtx GTY (()) call_arg_loc_note;
2966   const char * GTY (()) label;
2967   tree GTY (()) block;
2968   bool tail_call_p;
2969   rtx GTY (()) symbol_ref;
2970   struct call_arg_loc_node * GTY (()) next;
2971 };
2972
2973
2974 struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
2975 {
2976   typedef const_tree compare_type;
2977
2978   static hashval_t hash (var_loc_list *);
2979   static bool equal (var_loc_list *, const_tree);
2980 };
2981
2982 /* Table of decl location linked lists.  */
2983 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
2984
2985 /* Head and tail of call_arg_loc chain.  */
2986 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2987 static struct call_arg_loc_node *call_arg_loc_last;
2988
2989 /* Number of call sites in the current function.  */
2990 static int call_site_count = -1;
2991 /* Number of tail call sites in the current function.  */
2992 static int tail_call_site_count = -1;
2993
2994 /* A cached location list.  */
2995 struct GTY ((for_user)) cached_dw_loc_list_def {
2996   /* The DECL_UID of the decl that this entry describes.  */
2997   unsigned int decl_id;
2998
2999   /* The cached location list.  */
3000   dw_loc_list_ref loc_list;
3001 };
3002 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
3003
3004 struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
3005 {
3006
3007   typedef const_tree compare_type;
3008   
3009   static hashval_t hash (cached_dw_loc_list *);
3010   static bool equal (cached_dw_loc_list *, const_tree);
3011 };
3012
3013 /* Table of cached location lists.  */
3014 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
3015
3016 /* A pointer to the base of a list of references to DIE's that
3017    are uniquely identified by their tag, presence/absence of
3018    children DIE's, and list of attribute/value pairs.  */
3019 static GTY((length ("abbrev_die_table_allocated")))
3020   dw_die_ref *abbrev_die_table;
3021
3022 /* Number of elements currently allocated for abbrev_die_table.  */
3023 static GTY(()) unsigned abbrev_die_table_allocated;
3024
3025 /* Number of elements in abbrev_die_table currently in use.  */
3026 static GTY(()) unsigned abbrev_die_table_in_use;
3027
3028 /* A hash map to remember the stack usage for DWARF procedures.  The value
3029    stored is the stack size difference between before the DWARF procedure
3030    invokation and after it returned.  In other words, for a DWARF procedure
3031    that consumes N stack slots and that pushes M ones, this stores M - N.  */
3032 static hash_map<dw_die_ref, int> *dwarf_proc_stack_usage_map;
3033
3034 /* Size (in elements) of increments by which we may expand the
3035    abbrev_die_table.  */
3036 #define ABBREV_DIE_TABLE_INCREMENT 256
3037
3038 /* A global counter for generating labels for line number data.  */
3039 static unsigned int line_info_label_num;
3040
3041 /* The current table to which we should emit line number information
3042    for the current function.  This will be set up at the beginning of
3043    assembly for the function.  */
3044 static dw_line_info_table *cur_line_info_table;
3045
3046 /* The two default tables of line number info.  */
3047 static GTY(()) dw_line_info_table *text_section_line_info;
3048 static GTY(()) dw_line_info_table *cold_text_section_line_info;
3049
3050 /* The set of all non-default tables of line number info.  */
3051 static GTY(()) vec<dw_line_info_table *, va_gc> *separate_line_info;
3052
3053 /* A flag to tell pubnames/types export if there is an info section to
3054    refer to.  */
3055 static bool info_section_emitted;
3056
3057 /* A pointer to the base of a table that contains a list of publicly
3058    accessible names.  */
3059 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
3060
3061 /* A pointer to the base of a table that contains a list of publicly
3062    accessible types.  */
3063 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
3064
3065 /* A pointer to the base of a table that contains a list of macro
3066    defines/undefines (and file start/end markers).  */
3067 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
3068
3069 /* True if .debug_macinfo or .debug_macros section is going to be
3070    emitted.  */
3071 #define have_macinfo \
3072   ((!XCOFF_DEBUGGING_INFO || HAVE_XCOFF_DWARF_EXTRAS) \
3073    && debug_info_level >= DINFO_LEVEL_VERBOSE \
3074    && !macinfo_table->is_empty ())
3075
3076 /* Array of dies for which we should generate .debug_ranges info.  */
3077 static GTY ((length ("ranges_table_allocated"))) dw_ranges *ranges_table;
3078
3079 /* Number of elements currently allocated for ranges_table.  */
3080 static GTY(()) unsigned ranges_table_allocated;
3081
3082 /* Number of elements in ranges_table currently in use.  */
3083 static GTY(()) unsigned ranges_table_in_use;
3084
3085 /* Array of pairs of labels referenced in ranges_table.  */
3086 static GTY ((length ("ranges_by_label_allocated")))
3087      dw_ranges_by_label *ranges_by_label;
3088
3089 /* Number of elements currently allocated for ranges_by_label.  */
3090 static GTY(()) unsigned ranges_by_label_allocated;
3091
3092 /* Number of elements in ranges_by_label currently in use.  */
3093 static GTY(()) unsigned ranges_by_label_in_use;
3094
3095 /* Size (in elements) of increments by which we may expand the
3096    ranges_table.  */
3097 #define RANGES_TABLE_INCREMENT 64
3098
3099 /* Whether we have location lists that need outputting */
3100 static GTY(()) bool have_location_lists;
3101
3102 /* Unique label counter.  */
3103 static GTY(()) unsigned int loclabel_num;
3104
3105 /* Unique label counter for point-of-call tables.  */
3106 static GTY(()) unsigned int poc_label_num;
3107
3108 /* The last file entry emitted by maybe_emit_file().  */
3109 static GTY(()) struct dwarf_file_data * last_emitted_file;
3110
3111 /* Number of internal labels generated by gen_internal_sym().  */
3112 static GTY(()) int label_num;
3113
3114 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3115
3116 /* Instances of generic types for which we need to generate debug
3117    info that describe their generic parameters and arguments. That
3118    generation needs to happen once all types are properly laid out so
3119    we do it at the end of compilation.  */
3120 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3121
3122 /* Offset from the "steady-state frame pointer" to the frame base,
3123    within the current function.  */
3124 static HOST_WIDE_INT frame_pointer_fb_offset;
3125 static bool frame_pointer_fb_offset_valid;
3126
3127 static vec<dw_die_ref> base_types;
3128
3129 /* Flags to represent a set of attribute classes for attributes that represent
3130    a scalar value (bounds, pointers, ...).  */
3131 enum dw_scalar_form
3132 {
3133   dw_scalar_form_constant = 0x01,
3134   dw_scalar_form_exprloc = 0x02,
3135   dw_scalar_form_reference = 0x04
3136 };
3137
3138 /* Forward declarations for functions defined in this file.  */
3139
3140 static int is_pseudo_reg (const_rtx);
3141 static tree type_main_variant (tree);
3142 static int is_tagged_type (const_tree);
3143 static const char *dwarf_tag_name (unsigned);
3144 static const char *dwarf_attr_name (unsigned);
3145 static const char *dwarf_form_name (unsigned);
3146 static tree decl_ultimate_origin (const_tree);
3147 static tree decl_class_context (tree);
3148 static void add_dwarf_attr (dw_die_ref, dw_attr_node *);
3149 static inline enum dw_val_class AT_class (dw_attr_node *);
3150 static inline unsigned int AT_index (dw_attr_node *);
3151 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3152 static inline unsigned AT_flag (dw_attr_node *);
3153 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3154 static inline HOST_WIDE_INT AT_int (dw_attr_node *);
3155 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3156 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_node *);
3157 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3158                            HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3159 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3160                                unsigned int, unsigned char *);
3161 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3162 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3163 static inline const char *AT_string (dw_attr_node *);
3164 static enum dwarf_form AT_string_form (dw_attr_node *);
3165 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3166 static void add_AT_specification (dw_die_ref, dw_die_ref);
3167 static inline dw_die_ref AT_ref (dw_attr_node *);
3168 static inline int AT_ref_external (dw_attr_node *);
3169 static inline void set_AT_ref_external (dw_attr_node *, int);
3170 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3171 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3172 static inline dw_loc_descr_ref AT_loc (dw_attr_node *);
3173 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3174                              dw_loc_list_ref);
3175 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3176 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3177 static void remove_addr_table_entry (addr_table_entry *);
3178 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3179 static inline rtx AT_addr (dw_attr_node *);
3180 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3181 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3182 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3183 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3184                            unsigned HOST_WIDE_INT);
3185 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3186                                unsigned long, bool);
3187 static inline const char *AT_lbl (dw_attr_node *);
3188 static dw_attr_node *get_AT (dw_die_ref, enum dwarf_attribute);
3189 static const char *get_AT_low_pc (dw_die_ref);
3190 static const char *get_AT_hi_pc (dw_die_ref);
3191 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3192 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3193 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3194 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3195 static bool is_cxx (void);
3196 static bool is_fortran (void);
3197 static bool is_ada (void);
3198 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3199 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3200 static void add_child_die (dw_die_ref, dw_die_ref);
3201 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3202 static dw_die_ref lookup_type_die (tree);
3203 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3204 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3205 static void equate_type_number_to_die (tree, dw_die_ref);
3206 static dw_die_ref lookup_decl_die (tree);
3207 static var_loc_list *lookup_decl_loc (const_tree);
3208 static void equate_decl_number_to_die (tree, dw_die_ref);
3209 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3210 static void print_spaces (FILE *);
3211 static void print_die (dw_die_ref, FILE *);
3212 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3213 static dw_die_ref pop_compile_unit (dw_die_ref);
3214 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3215 static void attr_checksum (dw_attr_node *, struct md5_ctx *, int *);
3216 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3217 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3218 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3219 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3220 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_node *,
3221                                    struct md5_ctx *, int *);
3222 struct checksum_attributes;
3223 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3224 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3225 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3226 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3227 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3228 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3229 static int same_attr_p (dw_attr_node *, dw_attr_node *, int *);
3230 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3231 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3232 static void compute_section_prefix (dw_die_ref);
3233 static int is_type_die (dw_die_ref);
3234 static int is_comdat_die (dw_die_ref);
3235 static int is_symbol_die (dw_die_ref);
3236 static inline bool is_template_instantiation (dw_die_ref);
3237 static void assign_symbol_names (dw_die_ref);
3238 static void break_out_includes (dw_die_ref);
3239 static int is_declaration_die (dw_die_ref);
3240 static int should_move_die_to_comdat (dw_die_ref);
3241 static dw_die_ref clone_as_declaration (dw_die_ref);
3242 static dw_die_ref clone_die (dw_die_ref);
3243 static dw_die_ref clone_tree (dw_die_ref);
3244 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3245 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3246 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3247 static dw_die_ref generate_skeleton (dw_die_ref);
3248 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3249                                                          dw_die_ref,
3250                                                          dw_die_ref);
3251 static void break_out_comdat_types (dw_die_ref);
3252 static void copy_decls_for_unworthy_types (dw_die_ref);
3253
3254 static void add_sibling_attributes (dw_die_ref);
3255 static void output_location_lists (dw_die_ref);
3256 static int constant_size (unsigned HOST_WIDE_INT);
3257 static unsigned long size_of_die (dw_die_ref);
3258 static void calc_die_sizes (dw_die_ref);
3259 static void calc_base_type_die_sizes (void);
3260 static void mark_dies (dw_die_ref);
3261 static void unmark_dies (dw_die_ref);
3262 static void unmark_all_dies (dw_die_ref);
3263 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3264 static unsigned long size_of_aranges (void);
3265 static enum dwarf_form value_format (dw_attr_node *);
3266 static void output_value_format (dw_attr_node *);
3267 static void output_abbrev_section (void);
3268 static void output_die_abbrevs (unsigned long, dw_die_ref);
3269 static void output_die_symbol (dw_die_ref);
3270 static void output_die (dw_die_ref);
3271 static void output_compilation_unit_header (void);
3272 static void output_comp_unit (dw_die_ref, int);
3273 static void output_comdat_type_unit (comdat_type_node *);
3274 static const char *dwarf2_name (tree, int);
3275 static void add_pubname (tree, dw_die_ref);
3276 static void add_enumerator_pubname (const char *, dw_die_ref);
3277 static void add_pubname_string (const char *, dw_die_ref);
3278 static void add_pubtype (tree, dw_die_ref);
3279 static void output_pubnames (vec<pubname_entry, va_gc> *);
3280 static void output_aranges (void);
3281 static unsigned int add_ranges_num (int);
3282 static unsigned int add_ranges (const_tree);
3283 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3284                                   bool *, bool);
3285 static void output_ranges (void);
3286 static dw_line_info_table *new_line_info_table (void);
3287 static void output_line_info (bool);
3288 static void output_file_names (void);
3289 static dw_die_ref base_type_die (tree);
3290 static int is_base_type (tree);
3291 static dw_die_ref subrange_type_die (tree, tree, tree, tree, dw_die_ref);
3292 static int decl_quals (const_tree);
3293 static dw_die_ref modified_type_die (tree, int, dw_die_ref);
3294 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3295 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3296 static int type_is_enum (const_tree);
3297 static unsigned int dbx_reg_number (const_rtx);
3298 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3299 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3300 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3301                                                 enum var_init_status);
3302 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3303                                                      enum var_init_status);
3304 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3305                                          enum var_init_status);
3306 static int is_based_loc (const_rtx);
3307 static bool resolve_one_addr (rtx *);
3308 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3309                                                enum var_init_status);
3310 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3311                                         enum var_init_status);
3312 struct loc_descr_context;
3313 static void add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref);
3314 static void add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list);
3315 static dw_loc_list_ref loc_list_from_tree (tree, int,
3316                                            const struct loc_descr_context *);
3317 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3318                                                   const struct loc_descr_context *);
3319 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3320 static tree field_type (const_tree);
3321 static unsigned int simple_type_align_in_bits (const_tree);
3322 static unsigned int simple_decl_align_in_bits (const_tree);
3323 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3324 struct vlr_context;
3325 static dw_loc_descr_ref field_byte_offset (const_tree, struct vlr_context *,
3326                                            HOST_WIDE_INT *);
3327 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3328                                          dw_loc_list_ref);
3329 static void add_data_member_location_attribute (dw_die_ref, tree,
3330                                                 struct vlr_context *);
3331 static bool add_const_value_attribute (dw_die_ref, rtx);
3332 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3333 static void insert_wide_int (const wide_int &, unsigned char *, int);
3334 static void insert_float (const_rtx, unsigned char *);
3335 static rtx rtl_for_decl_location (tree);
3336 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool);
3337 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3338 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3339 static void add_name_attribute (dw_die_ref, const char *);
3340 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3341 static void add_comp_dir_attribute (dw_die_ref);
3342 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3343                              const struct loc_descr_context *);
3344 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3345                             const struct loc_descr_context *);
3346 static void add_subscript_info (dw_die_ref, tree, bool);
3347 static void add_byte_size_attribute (dw_die_ref, tree);
3348 static inline void add_bit_offset_attribute (dw_die_ref, tree,
3349                                              struct vlr_context *);
3350 static void add_bit_size_attribute (dw_die_ref, tree);
3351 static void add_prototyped_attribute (dw_die_ref, tree);
3352 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3353 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3354 static void add_src_coords_attributes (dw_die_ref, tree);
3355 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3356 static void add_discr_value (dw_die_ref, dw_discr_value *);
3357 static void add_discr_list (dw_die_ref, dw_discr_list_ref);
3358 static inline dw_discr_list_ref AT_discr_list (dw_attr_node *);
3359 static void push_decl_scope (tree);
3360 static void pop_decl_scope (void);
3361 static dw_die_ref scope_die_for (tree, dw_die_ref);
3362 static inline int local_scope_p (dw_die_ref);
3363 static inline int class_scope_p (dw_die_ref);
3364 static inline int class_or_namespace_scope_p (dw_die_ref);
3365 static void add_type_attribute (dw_die_ref, tree, int, dw_die_ref);
3366 static void add_calling_convention_attribute (dw_die_ref, tree);
3367 static const char *type_tag (const_tree);
3368 static tree member_declared_type (const_tree);
3369 #if 0
3370 static const char *decl_start_label (tree);
3371 #endif
3372 static void gen_array_type_die (tree, dw_die_ref);
3373 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3374 #if 0
3375 static void gen_entry_point_die (tree, dw_die_ref);
3376 #endif
3377 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3378 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3379 static dw_die_ref gen_formal_parameter_pack_die  (tree, tree, dw_die_ref, tree*);
3380 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3381 static void gen_formal_types_die (tree, dw_die_ref);
3382 static void gen_subprogram_die (tree, dw_die_ref);
3383 static void gen_variable_die (tree, tree, dw_die_ref);
3384 static void gen_const_die (tree, dw_die_ref);
3385 static void gen_label_die (tree, dw_die_ref);
3386 static void gen_lexical_block_die (tree, dw_die_ref);
3387 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3388 static void gen_field_die (tree, struct vlr_context *, dw_die_ref);
3389 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3390 static dw_die_ref gen_compile_unit_die (const char *);
3391 static void gen_inheritance_die (tree, tree, tree, dw_die_ref);
3392 static void gen_member_die (tree, dw_die_ref);
3393 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3394                                                 enum debug_info_usage);
3395 static void gen_subroutine_type_die (tree, dw_die_ref);
3396 static void gen_typedef_die (tree, dw_die_ref);
3397 static void gen_type_die (tree, dw_die_ref);
3398 static void gen_block_die (tree, dw_die_ref);
3399 static void decls_for_scope (tree, dw_die_ref);
3400 static bool is_naming_typedef_decl (const_tree);
3401 static inline dw_die_ref get_context_die (tree);
3402 static void gen_namespace_die (tree, dw_die_ref);
3403 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3404 static dw_die_ref gen_decl_die (tree, tree, struct vlr_context *, dw_die_ref);
3405 static dw_die_ref force_decl_die (tree);
3406 static dw_die_ref force_type_die (tree);
3407 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3408 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3409 static struct dwarf_file_data * lookup_filename (const char *);
3410 static void retry_incomplete_types (void);
3411 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3412 static void gen_generic_params_dies (tree);
3413 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3414 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3415 static void splice_child_die (dw_die_ref, dw_die_ref);
3416 static int file_info_cmp (const void *, const void *);
3417 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3418                                      const char *, const char *);
3419 static void output_loc_list (dw_loc_list_ref);
3420 static char *gen_internal_sym (const char *);
3421 static bool want_pubnames (void);
3422
3423 static void prune_unmark_dies (dw_die_ref);
3424 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3425 static void prune_unused_types_mark (dw_die_ref, int);
3426 static void prune_unused_types_walk (dw_die_ref);
3427 static void prune_unused_types_walk_attribs (dw_die_ref);
3428 static void prune_unused_types_prune (dw_die_ref);
3429 static void prune_unused_types (void);
3430 static int maybe_emit_file (struct dwarf_file_data *fd);
3431 static inline const char *AT_vms_delta1 (dw_attr_node *);
3432 static inline const char *AT_vms_delta2 (dw_attr_node *);
3433 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3434                                      const char *, const char *);
3435 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3436 static void gen_remaining_tmpl_value_param_die_attribute (void);
3437 static bool generic_type_p (tree);
3438 static void schedule_generic_params_dies_gen (tree t);
3439 static void gen_scheduled_generic_parms_dies (void);
3440
3441 static const char *comp_dir_string (void);
3442
3443 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3444
3445 /* enum for tracking thread-local variables whose address is really an offset
3446    relative to the TLS pointer, which will need link-time relocation, but will
3447    not need relocation by the DWARF consumer.  */
3448
3449 enum dtprel_bool
3450 {
3451   dtprel_false = 0,
3452   dtprel_true = 1
3453 };
3454
3455 /* Return the operator to use for an address of a variable.  For dtprel_true, we
3456    use DW_OP_const*.  For regular variables, which need both link-time
3457    relocation and consumer-level relocation (e.g., to account for shared objects
3458    loaded at a random address), we use DW_OP_addr*.  */
3459
3460 static inline enum dwarf_location_atom
3461 dw_addr_op (enum dtprel_bool dtprel)
3462 {
3463   if (dtprel == dtprel_true)
3464     return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3465             : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3466   else
3467     return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3468 }
3469
3470 /* Return a pointer to a newly allocated address location description.  If
3471    dwarf_split_debug_info is true, then record the address with the appropriate
3472    relocation.  */
3473 static inline dw_loc_descr_ref
3474 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3475 {
3476   dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3477
3478   ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3479   ref->dw_loc_oprnd1.v.val_addr = addr;
3480   ref->dtprel = dtprel;
3481   if (dwarf_split_debug_info)
3482     ref->dw_loc_oprnd1.val_entry
3483         = add_addr_table_entry (addr,
3484                                 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3485   else
3486     ref->dw_loc_oprnd1.val_entry = NULL;
3487
3488   return ref;
3489 }
3490
3491 /* Section names used to hold DWARF debugging information.  */
3492
3493 #ifndef DEBUG_INFO_SECTION
3494 #define DEBUG_INFO_SECTION      ".debug_info"
3495 #endif
3496 #ifndef DEBUG_DWO_INFO_SECTION
3497 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3498 #endif
3499 #ifndef DEBUG_ABBREV_SECTION
3500 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
3501 #endif
3502 #ifndef DEBUG_DWO_ABBREV_SECTION
3503 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3504 #endif
3505 #ifndef DEBUG_ARANGES_SECTION
3506 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
3507 #endif
3508 #ifndef DEBUG_ADDR_SECTION
3509 #define DEBUG_ADDR_SECTION     ".debug_addr"
3510 #endif
3511 #ifndef DEBUG_NORM_MACINFO_SECTION
3512 #define DEBUG_NORM_MACINFO_SECTION     ".debug_macinfo"
3513 #endif
3514 #ifndef DEBUG_DWO_MACINFO_SECTION
3515 #define DEBUG_DWO_MACINFO_SECTION      ".debug_macinfo.dwo"
3516 #endif
3517 #ifndef DEBUG_MACINFO_SECTION
3518 #define DEBUG_MACINFO_SECTION                                           \
3519   (!dwarf_split_debug_info                                              \
3520    ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3521 #endif
3522 #ifndef DEBUG_NORM_MACRO_SECTION
3523 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3524 #endif
3525 #ifndef DEBUG_DWO_MACRO_SECTION
3526 #define DEBUG_DWO_MACRO_SECTION        ".debug_macro.dwo"
3527 #endif
3528 #ifndef DEBUG_MACRO_SECTION
3529 #define DEBUG_MACRO_SECTION                                             \
3530   (!dwarf_split_debug_info                                              \
3531    ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3532 #endif
3533 #ifndef DEBUG_LINE_SECTION
3534 #define DEBUG_LINE_SECTION      ".debug_line"
3535 #endif
3536 #ifndef DEBUG_DWO_LINE_SECTION
3537 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3538 #endif
3539 #ifndef DEBUG_LOC_SECTION
3540 #define DEBUG_LOC_SECTION       ".debug_loc"
3541 #endif
3542 #ifndef DEBUG_DWO_LOC_SECTION
3543 #define DEBUG_DWO_LOC_SECTION  ".debug_loc.dwo"
3544 #endif
3545 #ifndef DEBUG_PUBNAMES_SECTION
3546 #define DEBUG_PUBNAMES_SECTION  \
3547   ((debug_generate_pub_sections == 2) \
3548    ? ".debug_gnu_pubnames" : ".debug_pubnames")
3549 #endif
3550 #ifndef DEBUG_PUBTYPES_SECTION
3551 #define DEBUG_PUBTYPES_SECTION  \
3552   ((debug_generate_pub_sections == 2) \
3553    ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3554 #endif
3555 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3556 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3557 #ifndef DEBUG_STR_OFFSETS_SECTION
3558 #define DEBUG_STR_OFFSETS_SECTION                                       \
3559   (!dwarf_split_debug_info                                              \
3560    ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3561 #endif
3562 #ifndef DEBUG_STR_DWO_SECTION
3563 #define DEBUG_STR_DWO_SECTION   ".debug_str.dwo"
3564 #endif
3565 #ifndef DEBUG_STR_SECTION
3566 #define DEBUG_STR_SECTION  ".debug_str"
3567 #endif
3568 #ifndef DEBUG_RANGES_SECTION
3569 #define DEBUG_RANGES_SECTION    ".debug_ranges"
3570 #endif
3571
3572 /* Standard ELF section names for compiled code and data.  */
3573 #ifndef TEXT_SECTION_NAME
3574 #define TEXT_SECTION_NAME       ".text"
3575 #endif
3576
3577 /* Section flags for .debug_macinfo/.debug_macro section.  */
3578 #define DEBUG_MACRO_SECTION_FLAGS                                       \
3579   (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3580
3581 /* Section flags for .debug_str section.  */
3582 #define DEBUG_STR_SECTION_FLAGS                                 \
3583   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
3584    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
3585    : SECTION_DEBUG)
3586
3587 /* Section flags for .debug_str.dwo section.  */
3588 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3589
3590 /* Labels we insert at beginning sections we can reference instead of
3591    the section names themselves.  */
3592
3593 #ifndef TEXT_SECTION_LABEL
3594 #define TEXT_SECTION_LABEL                 "Ltext"
3595 #endif
3596 #ifndef COLD_TEXT_SECTION_LABEL
3597 #define COLD_TEXT_SECTION_LABEL             "Ltext_cold"
3598 #endif
3599 #ifndef DEBUG_LINE_SECTION_LABEL
3600 #define DEBUG_LINE_SECTION_LABEL           "Ldebug_line"
3601 #endif
3602 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3603 #define DEBUG_SKELETON_LINE_SECTION_LABEL   "Lskeleton_debug_line"
3604 #endif
3605 #ifndef DEBUG_INFO_SECTION_LABEL
3606 #define DEBUG_INFO_SECTION_LABEL           "Ldebug_info"
3607 #endif
3608 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3609 #define DEBUG_SKELETON_INFO_SECTION_LABEL   "Lskeleton_debug_info"
3610 #endif
3611 #ifndef DEBUG_ABBREV_SECTION_LABEL
3612 #define DEBUG_ABBREV_SECTION_LABEL         "Ldebug_abbrev"
3613 #endif
3614 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3615 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3616 #endif
3617 #ifndef DEBUG_ADDR_SECTION_LABEL
3618 #define DEBUG_ADDR_SECTION_LABEL           "Ldebug_addr"
3619 #endif
3620 #ifndef DEBUG_LOC_SECTION_LABEL
3621 #define DEBUG_LOC_SECTION_LABEL                    "Ldebug_loc"
3622 #endif
3623 #ifndef DEBUG_RANGES_SECTION_LABEL
3624 #define DEBUG_RANGES_SECTION_LABEL         "Ldebug_ranges"
3625 #endif
3626 #ifndef DEBUG_MACINFO_SECTION_LABEL
3627 #define DEBUG_MACINFO_SECTION_LABEL         "Ldebug_macinfo"
3628 #endif
3629 #ifndef DEBUG_MACRO_SECTION_LABEL
3630 #define DEBUG_MACRO_SECTION_LABEL          "Ldebug_macro"
3631 #endif
3632 #define SKELETON_COMP_DIE_ABBREV 1
3633 #define SKELETON_TYPE_DIE_ABBREV 2
3634
3635 /* Definitions of defaults for formats and names of various special
3636    (artificial) labels which may be generated within this file (when the -g
3637    options is used and DWARF2_DEBUGGING_INFO is in effect.
3638    If necessary, these may be overridden from within the tm.h file, but
3639    typically, overriding these defaults is unnecessary.  */
3640
3641 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3642 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3643 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3644 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3645 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3646 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3647 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3648 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3649 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3650 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3651 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3652 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3653 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3654 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3655
3656 #ifndef TEXT_END_LABEL
3657 #define TEXT_END_LABEL          "Letext"
3658 #endif
3659 #ifndef COLD_END_LABEL
3660 #define COLD_END_LABEL          "Letext_cold"
3661 #endif
3662 #ifndef BLOCK_BEGIN_LABEL
3663 #define BLOCK_BEGIN_LABEL       "LBB"
3664 #endif
3665 #ifndef BLOCK_END_LABEL
3666 #define BLOCK_END_LABEL         "LBE"
3667 #endif
3668 #ifndef LINE_CODE_LABEL
3669 #define LINE_CODE_LABEL         "LM"
3670 #endif
3671
3672 \f
3673 /* Return the root of the DIE's built for the current compilation unit.  */
3674 static dw_die_ref
3675 comp_unit_die (void)
3676 {
3677   if (!single_comp_unit_die)
3678     single_comp_unit_die = gen_compile_unit_die (NULL);
3679   return single_comp_unit_die;
3680 }
3681
3682 /* We allow a language front-end to designate a function that is to be
3683    called to "demangle" any name before it is put into a DIE.  */
3684
3685 static const char *(*demangle_name_func) (const char *);
3686
3687 void
3688 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3689 {
3690   demangle_name_func = func;
3691 }
3692
3693 /* Test if rtl node points to a pseudo register.  */
3694
3695 static inline int
3696 is_pseudo_reg (const_rtx rtl)
3697 {
3698   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3699           || (GET_CODE (rtl) == SUBREG
3700               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3701 }
3702
3703 /* Return a reference to a type, with its const and volatile qualifiers
3704    removed.  */
3705
3706 static inline tree
3707 type_main_variant (tree type)
3708 {
3709   type = TYPE_MAIN_VARIANT (type);
3710
3711   /* ??? There really should be only one main variant among any group of
3712      variants of a given type (and all of the MAIN_VARIANT values for all
3713      members of the group should point to that one type) but sometimes the C
3714      front-end messes this up for array types, so we work around that bug
3715      here.  */
3716   if (TREE_CODE (type) == ARRAY_TYPE)
3717     while (type != TYPE_MAIN_VARIANT (type))
3718       type = TYPE_MAIN_VARIANT (type);
3719
3720   return type;
3721 }
3722
3723 /* Return nonzero if the given type node represents a tagged type.  */
3724
3725 static inline int
3726 is_tagged_type (const_tree type)
3727 {
3728   enum tree_code code = TREE_CODE (type);
3729
3730   return (code == RECORD_TYPE || code == UNION_TYPE
3731           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3732 }
3733
3734 /* Set label to debug_info_section_label + die_offset of a DIE reference.  */
3735
3736 static void
3737 get_ref_die_offset_label (char *label, dw_die_ref ref)
3738 {
3739   sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3740 }
3741
3742 /* Return die_offset of a DIE reference to a base type.  */
3743
3744 static unsigned long int
3745 get_base_type_offset (dw_die_ref ref)
3746 {
3747   if (ref->die_offset)
3748     return ref->die_offset;
3749   if (comp_unit_die ()->die_abbrev)
3750     {
3751       calc_base_type_die_sizes ();
3752       gcc_assert (ref->die_offset);
3753     }
3754   return ref->die_offset;
3755 }
3756
3757 /* Return die_offset of a DIE reference other than base type.  */
3758
3759 static unsigned long int
3760 get_ref_die_offset (dw_die_ref ref)
3761 {
3762   gcc_assert (ref->die_offset);
3763   return ref->die_offset;
3764 }
3765
3766 /* Convert a DIE tag into its string name.  */
3767
3768 static const char *
3769 dwarf_tag_name (unsigned int tag)
3770 {
3771   const char *name = get_DW_TAG_name (tag);
3772
3773   if (name != NULL)
3774     return name;
3775
3776   return "DW_TAG_<unknown>";
3777 }
3778
3779 /* Convert a DWARF attribute code into its string name.  */
3780
3781 static const char *
3782 dwarf_attr_name (unsigned int attr)
3783 {
3784   const char *name;
3785
3786   switch (attr)
3787     {
3788 #if VMS_DEBUGGING_INFO
3789     case DW_AT_HP_prologue:
3790       return "DW_AT_HP_prologue";
3791 #else
3792     case DW_AT_MIPS_loop_unroll_factor:
3793       return "DW_AT_MIPS_loop_unroll_factor";
3794 #endif
3795
3796 #if VMS_DEBUGGING_INFO
3797     case DW_AT_HP_epilogue:
3798       return "DW_AT_HP_epilogue";
3799 #else
3800     case DW_AT_MIPS_stride:
3801       return "DW_AT_MIPS_stride";
3802 #endif
3803     }
3804
3805   name = get_DW_AT_name (attr);
3806
3807   if (name != NULL)
3808     return name;
3809
3810   return "DW_AT_<unknown>";
3811 }
3812
3813 /* Convert a DWARF value form code into its string name.  */
3814
3815 static const char *
3816 dwarf_form_name (unsigned int form)
3817 {
3818   const char *name = get_DW_FORM_name (form);
3819
3820   if (name != NULL)
3821     return name;
3822
3823   return "DW_FORM_<unknown>";
3824 }
3825 \f
3826 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
3827    instance of an inlined instance of a decl which is local to an inline
3828    function, so we have to trace all of the way back through the origin chain
3829    to find out what sort of node actually served as the original seed for the
3830    given block.  */
3831
3832 static tree
3833 decl_ultimate_origin (const_tree decl)
3834 {
3835   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3836     return NULL_TREE;
3837
3838   /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
3839      we're trying to output the abstract instance of this function.  */
3840   if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3841     return NULL_TREE;
3842
3843   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3844      most distant ancestor, this should never happen.  */
3845   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3846
3847   return DECL_ABSTRACT_ORIGIN (decl);
3848 }
3849
3850 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
3851    of a virtual function may refer to a base class, so we check the 'this'
3852    parameter.  */
3853
3854 static tree
3855 decl_class_context (tree decl)
3856 {
3857   tree context = NULL_TREE;
3858
3859   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3860     context = DECL_CONTEXT (decl);
3861   else
3862     context = TYPE_MAIN_VARIANT
3863       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3864
3865   if (context && !TYPE_P (context))
3866     context = NULL_TREE;
3867
3868   return context;
3869 }
3870 \f
3871 /* Add an attribute/value pair to a DIE.  */
3872
3873 static inline void
3874 add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
3875 {
3876   /* Maybe this should be an assert?  */
3877   if (die == NULL)
3878     return;
3879
3880   vec_safe_reserve (die->die_attr, 1);
3881   vec_safe_push (die->die_attr, *attr);
3882 }
3883
3884 static inline enum dw_val_class
3885 AT_class (dw_attr_node *a)
3886 {
3887   return a->dw_attr_val.val_class;
3888 }
3889
3890 /* Return the index for any attribute that will be referenced with a
3891    DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index.  String indices
3892    are stored in dw_attr_val.v.val_str for reference counting
3893    pruning.  */
3894
3895 static inline unsigned int
3896 AT_index (dw_attr_node *a)
3897 {
3898   if (AT_class (a) == dw_val_class_str)
3899     return a->dw_attr_val.v.val_str->index;
3900   else if (a->dw_attr_val.val_entry != NULL)
3901     return a->dw_attr_val.val_entry->index;
3902   return NOT_INDEXED;
3903 }
3904
3905 /* Add a flag value attribute to a DIE.  */
3906
3907 static inline void
3908 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3909 {
3910   dw_attr_node attr;
3911
3912   attr.dw_attr = attr_kind;
3913   attr.dw_attr_val.val_class = dw_val_class_flag;
3914   attr.dw_attr_val.val_entry = NULL;
3915   attr.dw_attr_val.v.val_flag = flag;
3916   add_dwarf_attr (die, &attr);
3917 }
3918
3919 static inline unsigned
3920 AT_flag (dw_attr_node *a)
3921 {
3922   gcc_assert (a && AT_class (a) == dw_val_class_flag);
3923   return a->dw_attr_val.v.val_flag;
3924 }
3925
3926 /* Add a signed integer attribute value to a DIE.  */
3927
3928 static inline void
3929 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3930 {
3931   dw_attr_node attr;
3932
3933   attr.dw_attr = attr_kind;
3934   attr.dw_attr_val.val_class = dw_val_class_const;
3935   attr.dw_attr_val.val_entry = NULL;
3936   attr.dw_attr_val.v.val_int = int_val;
3937   add_dwarf_attr (die, &attr);
3938 }
3939
3940 static inline HOST_WIDE_INT
3941 AT_int (dw_attr_node *a)
3942 {
3943   gcc_assert (a && AT_class (a) == dw_val_class_const);
3944   return a->dw_attr_val.v.val_int;
3945 }
3946
3947 /* Add an unsigned integer attribute value to a DIE.  */
3948
3949 static inline void
3950 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3951                  unsigned HOST_WIDE_INT unsigned_val)
3952 {
3953   dw_attr_node attr;
3954
3955   attr.dw_attr = attr_kind;
3956   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3957   attr.dw_attr_val.val_entry = NULL;
3958   attr.dw_attr_val.v.val_unsigned = unsigned_val;
3959   add_dwarf_attr (die, &attr);
3960 }
3961
3962 static inline unsigned HOST_WIDE_INT
3963 AT_unsigned (dw_attr_node *a)
3964 {
3965   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3966   return a->dw_attr_val.v.val_unsigned;
3967 }
3968
3969 /* Add an unsigned wide integer attribute value to a DIE.  */
3970
3971 static inline void
3972 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
3973              const wide_int& w)
3974 {
3975   dw_attr_node attr;
3976
3977   attr.dw_attr = attr_kind;
3978   attr.dw_attr_val.val_class = dw_val_class_wide_int;
3979   attr.dw_attr_val.val_entry = NULL;
3980   attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
3981   *attr.dw_attr_val.v.val_wide = w;
3982   add_dwarf_attr (die, &attr);
3983 }
3984
3985 /* Add an unsigned double integer attribute value to a DIE.  */
3986
3987 static inline void
3988 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3989                HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3990 {
3991   dw_attr_node attr;
3992
3993   attr.dw_attr = attr_kind;
3994   attr.dw_attr_val.val_class = dw_val_class_const_double;
3995   attr.dw_attr_val.val_entry = NULL;
3996   attr.dw_attr_val.v.val_double.high = high;
3997   attr.dw_attr_val.v.val_double.low = low;
3998   add_dwarf_attr (die, &attr);
3999 }
4000
4001 /* Add a floating point attribute value to a DIE and return it.  */
4002
4003 static inline void
4004 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4005             unsigned int length, unsigned int elt_size, unsigned char *array)
4006 {
4007   dw_attr_node attr;
4008
4009   attr.dw_attr = attr_kind;
4010   attr.dw_attr_val.val_class = dw_val_class_vec;
4011   attr.dw_attr_val.val_entry = NULL;
4012   attr.dw_attr_val.v.val_vec.length = length;
4013   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4014   attr.dw_attr_val.v.val_vec.array = array;
4015   add_dwarf_attr (die, &attr);
4016 }
4017
4018 /* Add an 8-byte data attribute value to a DIE.  */
4019
4020 static inline void
4021 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4022               unsigned char data8[8])
4023 {
4024   dw_attr_node attr;
4025
4026   attr.dw_attr = attr_kind;
4027   attr.dw_attr_val.val_class = dw_val_class_data8;
4028   attr.dw_attr_val.val_entry = NULL;
4029   memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4030   add_dwarf_attr (die, &attr);
4031 }
4032
4033 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE.  When using
4034    dwarf_split_debug_info, address attributes in dies destined for the
4035    final executable have force_direct set to avoid using indexed
4036    references.  */
4037
4038 static inline void
4039 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
4040                     bool force_direct)
4041 {
4042   dw_attr_node attr;
4043   char * lbl_id;
4044
4045   lbl_id = xstrdup (lbl_low);
4046   attr.dw_attr = DW_AT_low_pc;
4047   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4048   attr.dw_attr_val.v.val_lbl_id = lbl_id;
4049   if (dwarf_split_debug_info && !force_direct)
4050     attr.dw_attr_val.val_entry
4051         = add_addr_table_entry (lbl_id, ate_kind_label);
4052   else
4053     attr.dw_attr_val.val_entry = NULL;
4054   add_dwarf_attr (die, &attr);
4055
4056   attr.dw_attr = DW_AT_high_pc;
4057   if (dwarf_version < 4)
4058     attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4059   else
4060     attr.dw_attr_val.val_class = dw_val_class_high_pc;
4061   lbl_id = xstrdup (lbl_high);
4062   attr.dw_attr_val.v.val_lbl_id = lbl_id;
4063   if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
4064       && dwarf_split_debug_info && !force_direct)
4065     attr.dw_attr_val.val_entry
4066         = add_addr_table_entry (lbl_id, ate_kind_label);
4067   else
4068     attr.dw_attr_val.val_entry = NULL;
4069   add_dwarf_attr (die, &attr);
4070 }
4071
4072 /* Hash and equality functions for debug_str_hash.  */
4073
4074 hashval_t
4075 indirect_string_hasher::hash (indirect_string_node *x)
4076 {
4077   return htab_hash_string (x->str);
4078 }
4079
4080 bool
4081 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4082 {
4083   return strcmp (x1->str, x2) == 0;
4084 }
4085
4086 /* Add STR to the given string hash table.  */
4087
4088 static struct indirect_string_node *
4089 find_AT_string_in_table (const char *str,
4090                          hash_table<indirect_string_hasher> *table)
4091 {
4092   struct indirect_string_node *node;
4093
4094   indirect_string_node **slot
4095     = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
4096   if (*slot == NULL)
4097     {
4098       node = ggc_cleared_alloc<indirect_string_node> ();
4099       node->str = ggc_strdup (str);
4100       *slot = node;
4101     }
4102   else
4103     node = *slot;
4104
4105   node->refcount++;
4106   return node;
4107 }
4108
4109 /* Add STR to the indirect string hash table.  */
4110
4111 static struct indirect_string_node *
4112 find_AT_string (const char *str)
4113 {
4114   if (! debug_str_hash)
4115     debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4116
4117   return find_AT_string_in_table (str, debug_str_hash);
4118 }
4119
4120 /* Add a string attribute value to a DIE.  */
4121
4122 static inline void
4123 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4124 {
4125   dw_attr_node attr;
4126   struct indirect_string_node *node;
4127
4128   node = find_AT_string (str);
4129
4130   attr.dw_attr = attr_kind;
4131   attr.dw_attr_val.val_class = dw_val_class_str;
4132   attr.dw_attr_val.val_entry = NULL;
4133   attr.dw_attr_val.v.val_str = node;
4134   add_dwarf_attr (die, &attr);
4135 }
4136
4137 static inline const char *
4138 AT_string (dw_attr_node *a)
4139 {
4140   gcc_assert (a && AT_class (a) == dw_val_class_str);
4141   return a->dw_attr_val.v.val_str->str;
4142 }
4143
4144 /* Call this function directly to bypass AT_string_form's logic to put
4145    the string inline in the die. */
4146
4147 static void
4148 set_indirect_string (struct indirect_string_node *node)
4149 {
4150   char label[32];
4151   /* Already indirect is a no op.  */
4152   if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
4153     {
4154       gcc_assert (node->label);
4155       return;
4156     }
4157   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4158   ++dw2_string_counter;
4159   node->label = xstrdup (label);
4160
4161   if (!dwarf_split_debug_info)
4162     {
4163       node->form = DW_FORM_strp;
4164       node->index = NOT_INDEXED;
4165     }
4166   else
4167     {
4168       node->form = DW_FORM_GNU_str_index;
4169       node->index = NO_INDEX_ASSIGNED;
4170     }
4171 }
4172
4173 /* Find out whether a string should be output inline in DIE
4174    or out-of-line in .debug_str section.  */
4175
4176 static enum dwarf_form
4177 find_string_form (struct indirect_string_node *node)
4178 {
4179   unsigned int len;
4180
4181   if (node->form)
4182     return node->form;
4183
4184   len = strlen (node->str) + 1;
4185
4186   /* If the string is shorter or equal to the size of the reference, it is
4187      always better to put it inline.  */
4188   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4189     return node->form = DW_FORM_string;
4190
4191   /* If we cannot expect the linker to merge strings in .debug_str
4192      section, only put it into .debug_str if it is worth even in this
4193      single module.  */
4194   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4195       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4196       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4197     return node->form = DW_FORM_string;
4198
4199   set_indirect_string (node);
4200
4201   return node->form;
4202 }
4203
4204 /* Find out whether the string referenced from the attribute should be
4205    output inline in DIE or out-of-line in .debug_str section.  */
4206
4207 static enum dwarf_form
4208 AT_string_form (dw_attr_node *a)
4209 {
4210   gcc_assert (a && AT_class (a) == dw_val_class_str);
4211   return find_string_form (a->dw_attr_val.v.val_str);
4212 }
4213
4214 /* Add a DIE reference attribute value to a DIE.  */
4215
4216 static inline void
4217 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4218 {
4219   dw_attr_node attr;
4220   gcc_checking_assert (targ_die != NULL);
4221
4222   /* With LTO we can end up trying to reference something we didn't create
4223      a DIE for.  Avoid crashing later on a NULL referenced DIE.  */
4224   if (targ_die == NULL)
4225     return;
4226
4227   attr.dw_attr = attr_kind;
4228   attr.dw_attr_val.val_class = dw_val_class_die_ref;
4229   attr.dw_attr_val.val_entry = NULL;
4230   attr.dw_attr_val.v.val_die_ref.die = targ_die;
4231   attr.dw_attr_val.v.val_die_ref.external = 0;
4232   add_dwarf_attr (die, &attr);
4233 }
4234
4235 /* Change DIE reference REF to point to NEW_DIE instead.  */
4236
4237 static inline void
4238 change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4239 {
4240   gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4241   ref->dw_attr_val.v.val_die_ref.die = new_die;
4242   ref->dw_attr_val.v.val_die_ref.external = 0;
4243 }
4244
4245 /* Add an AT_specification attribute to a DIE, and also make the back
4246    pointer from the specification to the definition.  */
4247
4248 static inline void
4249 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4250 {
4251   add_AT_die_ref (die, DW_AT_specification, targ_die);
4252   gcc_assert (!targ_die->die_definition);
4253   targ_die->die_definition = die;
4254 }
4255
4256 static inline dw_die_ref
4257 AT_ref (dw_attr_node *a)
4258 {
4259   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4260   return a->dw_attr_val.v.val_die_ref.die;
4261 }
4262
4263 static inline int
4264 AT_ref_external (dw_attr_node *a)
4265 {
4266   if (a && AT_class (a) == dw_val_class_die_ref)
4267     return a->dw_attr_val.v.val_die_ref.external;
4268
4269   return 0;
4270 }
4271
4272 static inline void
4273 set_AT_ref_external (dw_attr_node *a, int i)
4274 {
4275   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4276   a->dw_attr_val.v.val_die_ref.external = i;
4277 }
4278
4279 /* Add an FDE reference attribute value to a DIE.  */
4280
4281 static inline void
4282 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4283 {
4284   dw_attr_node attr;
4285
4286   attr.dw_attr = attr_kind;
4287   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4288   attr.dw_attr_val.val_entry = NULL;
4289   attr.dw_attr_val.v.val_fde_index = targ_fde;
4290   add_dwarf_attr (die, &attr);
4291 }
4292
4293 /* Add a location description attribute value to a DIE.  */
4294
4295 static inline void
4296 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4297 {
4298   dw_attr_node attr;
4299
4300   attr.dw_attr = attr_kind;
4301   attr.dw_attr_val.val_class = dw_val_class_loc;
4302   attr.dw_attr_val.val_entry = NULL;
4303   attr.dw_attr_val.v.val_loc = loc;
4304   add_dwarf_attr (die, &attr);
4305 }
4306
4307 static inline dw_loc_descr_ref
4308 AT_loc (dw_attr_node *a)
4309 {
4310   gcc_assert (a && AT_class (a) == dw_val_class_loc);
4311   return a->dw_attr_val.v.val_loc;
4312 }
4313
4314 static inline void
4315 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4316 {
4317   dw_attr_node attr;
4318
4319   if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4320     return;
4321
4322   attr.dw_attr = attr_kind;
4323   attr.dw_attr_val.val_class = dw_val_class_loc_list;
4324   attr.dw_attr_val.val_entry = NULL;
4325   attr.dw_attr_val.v.val_loc_list = loc_list;
4326   add_dwarf_attr (die, &attr);
4327   have_location_lists = true;
4328 }
4329
4330 static inline dw_loc_list_ref
4331 AT_loc_list (dw_attr_node *a)
4332 {
4333   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4334   return a->dw_attr_val.v.val_loc_list;
4335 }
4336
4337 static inline dw_loc_list_ref *
4338 AT_loc_list_ptr (dw_attr_node *a)
4339 {
4340   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4341   return &a->dw_attr_val.v.val_loc_list;
4342 }
4343
4344 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
4345 {
4346   static hashval_t hash (addr_table_entry *);
4347   static bool equal (addr_table_entry *, addr_table_entry *);
4348 };
4349
4350 /* Table of entries into the .debug_addr section.  */
4351
4352 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4353
4354 /* Hash an address_table_entry.  */
4355
4356 hashval_t
4357 addr_hasher::hash (addr_table_entry *a)
4358 {
4359   inchash::hash hstate;
4360   switch (a->kind)
4361     {
4362       case ate_kind_rtx:
4363         hstate.add_int (0);
4364         break;
4365       case ate_kind_rtx_dtprel:
4366         hstate.add_int (1);
4367         break;
4368       case ate_kind_label:
4369         return htab_hash_string (a->addr.label);
4370       default:
4371         gcc_unreachable ();
4372     }
4373   inchash::add_rtx (a->addr.rtl, hstate);
4374   return hstate.end ();
4375 }
4376
4377 /* Determine equality for two address_table_entries.  */
4378
4379 bool
4380 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4381 {
4382   if (a1->kind != a2->kind)
4383     return 0;
4384   switch (a1->kind)
4385     {
4386       case ate_kind_rtx:
4387       case ate_kind_rtx_dtprel:
4388         return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4389       case ate_kind_label:
4390         return strcmp (a1->addr.label, a2->addr.label) == 0;
4391       default:
4392         gcc_unreachable ();
4393     }
4394 }
4395
4396 /* Initialize an addr_table_entry.  */
4397
4398 void
4399 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4400 {
4401   e->kind = kind;
4402   switch (kind)
4403     {
4404       case ate_kind_rtx:
4405       case ate_kind_rtx_dtprel:
4406         e->addr.rtl = (rtx) addr;
4407         break;
4408       case ate_kind_label:
4409         e->addr.label = (char *) addr;
4410         break;
4411     }
4412   e->refcount = 0;
4413   e->index = NO_INDEX_ASSIGNED;
4414 }
4415
4416 /* Add attr to the address table entry to the table.  Defer setting an
4417    index until output time.  */
4418
4419 static addr_table_entry *
4420 add_addr_table_entry (void *addr, enum ate_kind kind)
4421 {
4422   addr_table_entry *node;
4423   addr_table_entry finder;
4424
4425   gcc_assert (dwarf_split_debug_info);
4426   if (! addr_index_table)
4427     addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4428   init_addr_table_entry (&finder, kind, addr);
4429   addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4430
4431   if (*slot == HTAB_EMPTY_ENTRY)
4432     {
4433       node = ggc_cleared_alloc<addr_table_entry> ();
4434       init_addr_table_entry (node, kind, addr);
4435       *slot = node;
4436     }
4437   else
4438     node = *slot;
4439
4440   node->refcount++;
4441   return node;
4442 }
4443
4444 /* Remove an entry from the addr table by decrementing its refcount.
4445    Strictly, decrementing the refcount would be enough, but the
4446    assertion that the entry is actually in the table has found
4447    bugs.  */
4448
4449 static void
4450 remove_addr_table_entry (addr_table_entry *entry)
4451 {
4452   gcc_assert (dwarf_split_debug_info && addr_index_table);
4453   /* After an index is assigned, the table is frozen.  */
4454   gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4455   entry->refcount--;
4456 }
4457
4458 /* Given a location list, remove all addresses it refers to from the
4459    address_table.  */
4460
4461 static void
4462 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4463 {
4464   for (; descr; descr = descr->dw_loc_next)
4465     if (descr->dw_loc_oprnd1.val_entry != NULL)
4466       {
4467         gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4468         remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4469       }
4470 }
4471
4472 /* A helper function for dwarf2out_finish called through
4473    htab_traverse.  Assign an addr_table_entry its index.  All entries
4474    must be collected into the table when this function is called,
4475    because the indexing code relies on htab_traverse to traverse nodes
4476    in the same order for each run. */
4477
4478 int
4479 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4480 {
4481   addr_table_entry *node = *h;
4482
4483   /* Don't index unreferenced nodes.  */
4484   if (node->refcount == 0)
4485     return 1;
4486
4487   gcc_assert (node->index == NO_INDEX_ASSIGNED);
4488   node->index = *index;
4489   *index += 1;
4490
4491   return 1;
4492 }
4493
4494 /* Add an address constant attribute value to a DIE.  When using
4495    dwarf_split_debug_info, address attributes in dies destined for the
4496    final executable should be direct references--setting the parameter
4497    force_direct ensures this behavior.  */
4498
4499 static inline void
4500 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4501              bool force_direct)
4502 {
4503   dw_attr_node attr;
4504
4505   attr.dw_attr = attr_kind;
4506   attr.dw_attr_val.val_class = dw_val_class_addr;
4507   attr.dw_attr_val.v.val_addr = addr;
4508   if (dwarf_split_debug_info && !force_direct)
4509     attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4510   else
4511     attr.dw_attr_val.val_entry = NULL;
4512   add_dwarf_attr (die, &attr);
4513 }
4514
4515 /* Get the RTX from to an address DIE attribute.  */
4516
4517 static inline rtx
4518 AT_addr (dw_attr_node *a)
4519 {
4520   gcc_assert (a && AT_class (a) == dw_val_class_addr);
4521   return a->dw_attr_val.v.val_addr;
4522 }
4523
4524 /* Add a file attribute value to a DIE.  */
4525
4526 static inline void
4527 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4528              struct dwarf_file_data *fd)
4529 {
4530   dw_attr_node attr;
4531
4532   attr.dw_attr = attr_kind;
4533   attr.dw_attr_val.val_class = dw_val_class_file;
4534   attr.dw_attr_val.val_entry = NULL;
4535   attr.dw_attr_val.v.val_file = fd;
4536   add_dwarf_attr (die, &attr);
4537 }
4538
4539 /* Get the dwarf_file_data from a file DIE attribute.  */
4540
4541 static inline struct dwarf_file_data *
4542 AT_file (dw_attr_node *a)
4543 {
4544   gcc_assert (a && AT_class (a) == dw_val_class_file);
4545   return a->dw_attr_val.v.val_file;
4546 }
4547
4548 /* Add a vms delta attribute value to a DIE.  */
4549
4550 static inline void
4551 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4552                   const char *lbl1, const char *lbl2)
4553 {
4554   dw_attr_node attr;
4555
4556   attr.dw_attr = attr_kind;
4557   attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4558   attr.dw_attr_val.val_entry = NULL;
4559   attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4560   attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4561   add_dwarf_attr (die, &attr);
4562 }
4563
4564 /* Add a label identifier attribute value to a DIE.  */
4565
4566 static inline void
4567 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4568                const char *lbl_id)
4569 {
4570   dw_attr_node attr;
4571
4572   attr.dw_attr = attr_kind;
4573   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4574   attr.dw_attr_val.val_entry = NULL;
4575   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4576   if (dwarf_split_debug_info)
4577     attr.dw_attr_val.val_entry
4578         = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4579                                 ate_kind_label);
4580   add_dwarf_attr (die, &attr);
4581 }
4582
4583 /* Add a section offset attribute value to a DIE, an offset into the
4584    debug_line section.  */
4585
4586 static inline void
4587 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4588                 const char *label)
4589 {
4590   dw_attr_node attr;
4591
4592   attr.dw_attr = attr_kind;
4593   attr.dw_attr_val.val_class = dw_val_class_lineptr;
4594   attr.dw_attr_val.val_entry = NULL;
4595   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4596   add_dwarf_attr (die, &attr);
4597 }
4598
4599 /* Add a section offset attribute value to a DIE, an offset into the
4600    debug_macinfo section.  */
4601
4602 static inline void
4603 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4604                const char *label)
4605 {
4606   dw_attr_node attr;
4607
4608   attr.dw_attr = attr_kind;
4609   attr.dw_attr_val.val_class = dw_val_class_macptr;
4610   attr.dw_attr_val.val_entry = NULL;
4611   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4612   add_dwarf_attr (die, &attr);
4613 }
4614
4615 /* Add an offset attribute value to a DIE.  */
4616
4617 static inline void
4618 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4619                unsigned HOST_WIDE_INT offset)
4620 {
4621   dw_attr_node attr;
4622
4623   attr.dw_attr = attr_kind;
4624   attr.dw_attr_val.val_class = dw_val_class_offset;
4625   attr.dw_attr_val.val_entry = NULL;
4626   attr.dw_attr_val.v.val_offset = offset;
4627   add_dwarf_attr (die, &attr);
4628 }
4629
4630 /* Add a range_list attribute value to a DIE.  When using
4631    dwarf_split_debug_info, address attributes in dies destined for the
4632    final executable should be direct references--setting the parameter
4633    force_direct ensures this behavior.  */
4634
4635 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4636 #define RELOCATED_OFFSET (NULL)
4637
4638 static void
4639 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4640                    long unsigned int offset, bool force_direct)
4641 {
4642   dw_attr_node attr;
4643
4644   attr.dw_attr = attr_kind;
4645   attr.dw_attr_val.val_class = dw_val_class_range_list;
4646   /* For the range_list attribute, use val_entry to store whether the
4647      offset should follow split-debug-info or normal semantics.  This
4648      value is read in output_range_list_offset.  */
4649   if (dwarf_split_debug_info && !force_direct)
4650     attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4651   else
4652     attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4653   attr.dw_attr_val.v.val_offset = offset;
4654   add_dwarf_attr (die, &attr);
4655 }
4656
4657 /* Return the start label of a delta attribute.  */
4658
4659 static inline const char *
4660 AT_vms_delta1 (dw_attr_node *a)
4661 {
4662   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4663   return a->dw_attr_val.v.val_vms_delta.lbl1;
4664 }
4665
4666 /* Return the end label of a delta attribute.  */
4667
4668 static inline const char *
4669 AT_vms_delta2 (dw_attr_node *a)
4670 {
4671   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4672   return a->dw_attr_val.v.val_vms_delta.lbl2;
4673 }
4674
4675 static inline const char *
4676 AT_lbl (dw_attr_node *a)
4677 {
4678   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4679                     || AT_class (a) == dw_val_class_lineptr
4680                     || AT_class (a) == dw_val_class_macptr
4681                     || AT_class (a) == dw_val_class_high_pc));
4682   return a->dw_attr_val.v.val_lbl_id;
4683 }
4684
4685 /* Get the attribute of type attr_kind.  */
4686
4687 static dw_attr_node *
4688 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4689 {
4690   dw_attr_node *a;
4691   unsigned ix;
4692   dw_die_ref spec = NULL;
4693
4694   if (! die)
4695     return NULL;
4696
4697   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4698     if (a->dw_attr == attr_kind)
4699       return a;
4700     else if (a->dw_attr == DW_AT_specification
4701              || a->dw_attr == DW_AT_abstract_origin)
4702       spec = AT_ref (a);
4703
4704   if (spec)
4705     return get_AT (spec, attr_kind);
4706
4707   return NULL;
4708 }
4709
4710 /* Returns the parent of the declaration of DIE.  */
4711
4712 static dw_die_ref
4713 get_die_parent (dw_die_ref die)
4714 {
4715   dw_die_ref t;
4716
4717   if (!die)
4718     return NULL;
4719
4720   if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4721       || (t = get_AT_ref (die, DW_AT_specification)))
4722     die = t;
4723
4724   return die->die_parent;
4725 }
4726
4727 /* Return the "low pc" attribute value, typically associated with a subprogram
4728    DIE.  Return null if the "low pc" attribute is either not present, or if it
4729    cannot be represented as an assembler label identifier.  */
4730
4731 static inline const char *
4732 get_AT_low_pc (dw_die_ref die)
4733 {
4734   dw_attr_node *a = get_AT (die, DW_AT_low_pc);
4735
4736   return a ? AT_lbl (a) : NULL;
4737 }
4738
4739 /* Return the "high pc" attribute value, typically associated with a subprogram
4740    DIE.  Return null if the "high pc" attribute is either not present, or if it
4741    cannot be represented as an assembler label identifier.  */
4742
4743 static inline const char *
4744 get_AT_hi_pc (dw_die_ref die)
4745 {
4746   dw_attr_node *a = get_AT (die, DW_AT_high_pc);
4747
4748   return a ? AT_lbl (a) : NULL;
4749 }
4750
4751 /* Return the value of the string attribute designated by ATTR_KIND, or
4752    NULL if it is not present.  */
4753
4754 static inline const char *
4755 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4756 {
4757   dw_attr_node *a = get_AT (die, attr_kind);
4758
4759   return a ? AT_string (a) : NULL;
4760 }
4761
4762 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4763    if it is not present.  */
4764
4765 static inline int
4766 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4767 {
4768   dw_attr_node *a = get_AT (die, attr_kind);
4769
4770   return a ? AT_flag (a) : 0;
4771 }
4772
4773 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4774    if it is not present.  */
4775
4776 static inline unsigned
4777 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4778 {
4779   dw_attr_node *a = get_AT (die, attr_kind);
4780
4781   return a ? AT_unsigned (a) : 0;
4782 }
4783
4784 static inline dw_die_ref
4785 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4786 {
4787   dw_attr_node *a = get_AT (die, attr_kind);
4788
4789   return a ? AT_ref (a) : NULL;
4790 }
4791
4792 static inline struct dwarf_file_data *
4793 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4794 {
4795   dw_attr_node *a = get_AT (die, attr_kind);
4796
4797   return a ? AT_file (a) : NULL;
4798 }
4799
4800 /* Return TRUE if the language is C++.  */
4801
4802 static inline bool
4803 is_cxx (void)
4804 {
4805   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4806
4807   return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
4808           || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
4809 }
4810
4811 /* Return TRUE if the language is Java.  */
4812
4813 static inline bool
4814 is_java (void)
4815 {
4816   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4817
4818   return lang == DW_LANG_Java;
4819 }
4820
4821 /* Return TRUE if the language is Fortran.  */
4822
4823 static inline bool
4824 is_fortran (void)
4825 {
4826   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4827
4828   return (lang == DW_LANG_Fortran77
4829           || lang == DW_LANG_Fortran90
4830           || lang == DW_LANG_Fortran95
4831           || lang == DW_LANG_Fortran03
4832           || lang == DW_LANG_Fortran08);
4833 }
4834
4835 /* Return TRUE if the language is Ada.  */
4836
4837 static inline bool
4838 is_ada (void)
4839 {
4840   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4841
4842   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4843 }
4844
4845 /* Remove the specified attribute if present.  Return TRUE if removal
4846    was successful.  */
4847
4848 static bool
4849 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4850 {
4851   dw_attr_node *a;
4852   unsigned ix;
4853
4854   if (! die)
4855     return false;
4856
4857   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4858     if (a->dw_attr == attr_kind)
4859       {
4860         if (AT_class (a) == dw_val_class_str)
4861           if (a->dw_attr_val.v.val_str->refcount)
4862             a->dw_attr_val.v.val_str->refcount--;
4863
4864         /* vec::ordered_remove should help reduce the number of abbrevs
4865            that are needed.  */
4866         die->die_attr->ordered_remove (ix);
4867         return true;
4868       }
4869   return false;
4870 }
4871
4872 /* Remove CHILD from its parent.  PREV must have the property that
4873    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
4874
4875 static void
4876 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4877 {
4878   gcc_assert (child->die_parent == prev->die_parent);
4879   gcc_assert (prev->die_sib == child);
4880   if (prev == child)
4881     {
4882       gcc_assert (child->die_parent->die_child == child);
4883       prev = NULL;
4884     }
4885   else
4886     prev->die_sib = child->die_sib;
4887   if (child->die_parent->die_child == child)
4888     child->die_parent->die_child = prev;
4889 }
4890
4891 /* Replace OLD_CHILD with NEW_CHILD.  PREV must have the property that
4892    PREV->DIE_SIB == OLD_CHILD.  Does not alter OLD_CHILD.  */
4893
4894 static void
4895 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4896 {
4897   dw_die_ref parent = old_child->die_parent;
4898
4899   gcc_assert (parent == prev->die_parent);
4900   gcc_assert (prev->die_sib == old_child);
4901
4902   new_child->die_parent = parent;
4903   if (prev == old_child)
4904     {
4905       gcc_assert (parent->die_child == old_child);
4906       new_child->die_sib = new_child;
4907     }
4908   else
4909     {
4910       prev->die_sib = new_child;
4911       new_child->die_sib = old_child->die_sib;
4912     }
4913   if (old_child->die_parent->die_child == old_child)
4914     old_child->die_parent->die_child = new_child;
4915 }
4916
4917 /* Move all children from OLD_PARENT to NEW_PARENT.  */
4918
4919 static void
4920 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4921 {
4922   dw_die_ref c;
4923   new_parent->die_child = old_parent->die_child;
4924   old_parent->die_child = NULL;
4925   FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4926 }
4927
4928 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
4929    matches TAG.  */
4930
4931 static void
4932 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4933 {
4934   dw_die_ref c;
4935
4936   c = die->die_child;
4937   if (c) do {
4938     dw_die_ref prev = c;
4939     c = c->die_sib;
4940     while (c->die_tag == tag)
4941       {
4942         remove_child_with_prev (c, prev);
4943         c->die_parent = NULL;
4944         /* Might have removed every child.  */
4945         if (c == c->die_sib)
4946           return;
4947         c = c->die_sib;
4948       }
4949   } while (c != die->die_child);
4950 }
4951
4952 /* Add a CHILD_DIE as the last child of DIE.  */
4953
4954 static void
4955 add_child_die (dw_die_ref die, dw_die_ref child_die)
4956 {
4957   /* FIXME this should probably be an assert.  */
4958   if (! die || ! child_die)
4959     return;
4960   gcc_assert (die != child_die);
4961
4962   child_die->die_parent = die;
4963   if (die->die_child)
4964     {
4965       child_die->die_sib = die->die_child->die_sib;
4966       die->die_child->die_sib = child_die;
4967     }
4968   else
4969     child_die->die_sib = child_die;
4970   die->die_child = child_die;
4971 }
4972
4973 /* Unassociate CHILD from its parent, and make its parent be
4974    NEW_PARENT.  */
4975
4976 static void
4977 reparent_child (dw_die_ref child, dw_die_ref new_parent)
4978 {
4979   for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
4980     if (p->die_sib == child)
4981       {
4982         remove_child_with_prev (child, p);
4983         break;
4984       }
4985   add_child_die (new_parent, child);
4986 }
4987
4988 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4989    is the specification, to the end of PARENT's list of children.
4990    This is done by removing and re-adding it.  */
4991
4992 static void
4993 splice_child_die (dw_die_ref parent, dw_die_ref child)
4994 {
4995   /* We want the declaration DIE from inside the class, not the
4996      specification DIE at toplevel.  */
4997   if (child->die_parent != parent)
4998     {
4999       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5000
5001       if (tmp)
5002         child = tmp;
5003     }
5004
5005   gcc_assert (child->die_parent == parent
5006               || (child->die_parent
5007                   == get_AT_ref (parent, DW_AT_specification)));
5008
5009   reparent_child (child, parent);
5010 }
5011
5012 /* Create and return a new die with a parent of PARENT_DIE.  If
5013    PARENT_DIE is NULL, the new DIE is placed in limbo and an
5014    associated tree T must be supplied to determine parenthood
5015    later.  */
5016
5017 static inline dw_die_ref
5018 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5019 {
5020   dw_die_ref die = ggc_cleared_alloc<die_node> ();
5021
5022   die->die_tag = tag_value;
5023
5024   if (parent_die != NULL)
5025     add_child_die (parent_die, die);
5026   else
5027     {
5028       limbo_die_node *limbo_node;
5029
5030       /* No DIEs created after early dwarf should end up in limbo,
5031          because the limbo list should not persist past LTO
5032          streaming.  */
5033       if (tag_value != DW_TAG_compile_unit
5034           /* These are allowed because they're generated while
5035              breaking out COMDAT units late.  */
5036           && tag_value != DW_TAG_type_unit
5037           && !early_dwarf
5038           /* Allow nested functions to live in limbo because they will
5039              only temporarily live there, as decls_for_scope will fix
5040              them up.  */
5041           && (TREE_CODE (t) != FUNCTION_DECL
5042               || !decl_function_context (t))
5043           /* Same as nested functions above but for types.  Types that
5044              are local to a function will be fixed in
5045              decls_for_scope.  */
5046           && (!RECORD_OR_UNION_TYPE_P (t)
5047               || !TYPE_CONTEXT (t)
5048               || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
5049           /* FIXME debug-early: Allow late limbo DIE creation for LTO,
5050              especially in the ltrans stage, but once we implement LTO
5051              dwarf streaming, we should remove this exception.  */
5052           && !in_lto_p)
5053         {
5054           fprintf (stderr, "symbol ended up in limbo too late:");
5055           debug_generic_stmt (t);
5056           gcc_unreachable ();
5057         }
5058
5059       limbo_node = ggc_cleared_alloc<limbo_die_node> ();
5060       limbo_node->die = die;
5061       limbo_node->created_for = t;
5062       limbo_node->next = limbo_die_list;
5063       limbo_die_list = limbo_node;
5064     }
5065
5066   return die;
5067 }
5068
5069 /* Return the DIE associated with the given type specifier.  */
5070
5071 static inline dw_die_ref
5072 lookup_type_die (tree type)
5073 {
5074   return TYPE_SYMTAB_DIE (type);
5075 }
5076
5077 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5078    anonymous type named by the typedef TYPE_DIE, return the DIE of the
5079    anonymous type instead the one of the naming typedef.  */
5080
5081 static inline dw_die_ref
5082 strip_naming_typedef (tree type, dw_die_ref type_die)
5083 {
5084   if (type
5085       && TREE_CODE (type) == RECORD_TYPE
5086       && type_die
5087       && type_die->die_tag == DW_TAG_typedef
5088       && is_naming_typedef_decl (TYPE_NAME (type)))
5089     type_die = get_AT_ref (type_die, DW_AT_type);
5090   return type_die;
5091 }
5092
5093 /* Like lookup_type_die, but if type is an anonymous type named by a
5094    typedef[1], return the DIE of the anonymous type instead the one of
5095    the naming typedef.  This is because in gen_typedef_die, we did
5096    equate the anonymous struct named by the typedef with the DIE of
5097    the naming typedef. So by default, lookup_type_die on an anonymous
5098    struct yields the DIE of the naming typedef.
5099
5100    [1]: Read the comment of is_naming_typedef_decl to learn about what
5101    a naming typedef is.  */
5102
5103 static inline dw_die_ref
5104 lookup_type_die_strip_naming_typedef (tree type)
5105 {
5106   dw_die_ref die = lookup_type_die (type);
5107   return strip_naming_typedef (type, die);
5108 }
5109
5110 /* Equate a DIE to a given type specifier.  */
5111
5112 static inline void
5113 equate_type_number_to_die (tree type, dw_die_ref type_die)
5114 {
5115   TYPE_SYMTAB_DIE (type) = type_die;
5116 }
5117
5118 /* Returns a hash value for X (which really is a die_struct).  */
5119
5120 inline hashval_t
5121 decl_die_hasher::hash (die_node *x)
5122 {
5123   return (hashval_t) x->decl_id;
5124 }
5125
5126 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
5127
5128 inline bool
5129 decl_die_hasher::equal (die_node *x, tree y)
5130 {
5131   return (x->decl_id == DECL_UID (y));
5132 }
5133
5134 /* Return the DIE associated with a given declaration.  */
5135
5136 static inline dw_die_ref
5137 lookup_decl_die (tree decl)
5138 {
5139   return decl_die_table->find_with_hash (decl, DECL_UID (decl));
5140 }
5141
5142 /* Returns a hash value for X (which really is a var_loc_list).  */
5143
5144 inline hashval_t
5145 decl_loc_hasher::hash (var_loc_list *x)
5146 {
5147   return (hashval_t) x->decl_id;
5148 }
5149
5150 /* Return nonzero if decl_id of var_loc_list X is the same as
5151    UID of decl *Y.  */
5152
5153 inline bool
5154 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5155 {
5156   return (x->decl_id == DECL_UID (y));
5157 }
5158
5159 /* Return the var_loc list associated with a given declaration.  */
5160
5161 static inline var_loc_list *
5162 lookup_decl_loc (const_tree decl)
5163 {
5164   if (!decl_loc_table)
5165     return NULL;
5166   return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5167 }
5168
5169 /* Returns a hash value for X (which really is a cached_dw_loc_list_list).  */
5170
5171 inline hashval_t
5172 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5173 {
5174   return (hashval_t) x->decl_id;
5175 }
5176
5177 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5178    UID of decl *Y.  */
5179
5180 inline bool
5181 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5182 {
5183   return (x->decl_id == DECL_UID (y));
5184 }
5185
5186 /* Equate a DIE to a particular declaration.  */
5187
5188 static void
5189 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5190 {
5191   unsigned int decl_id = DECL_UID (decl);
5192
5193   *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5194   decl_die->decl_id = decl_id;
5195 }
5196
5197 /* Return how many bits covers PIECE EXPR_LIST.  */
5198
5199 static HOST_WIDE_INT
5200 decl_piece_bitsize (rtx piece)
5201 {
5202   int ret = (int) GET_MODE (piece);
5203   if (ret)
5204     return ret;
5205   gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5206               && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5207   return INTVAL (XEXP (XEXP (piece, 0), 0));
5208 }
5209
5210 /* Return pointer to the location of location note in PIECE EXPR_LIST.  */
5211
5212 static rtx *
5213 decl_piece_varloc_ptr (rtx piece)
5214 {
5215   if ((int) GET_MODE (piece))
5216     return &XEXP (piece, 0);
5217   else
5218     return &XEXP (XEXP (piece, 0), 1);
5219 }
5220
5221 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5222    Next is the chain of following piece nodes.  */
5223
5224 static rtx_expr_list *
5225 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5226 {
5227   if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
5228     return alloc_EXPR_LIST (bitsize, loc_note, next);
5229   else
5230     return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5231                                                GEN_INT (bitsize),
5232                                                loc_note), next);
5233 }
5234
5235 /* Return rtx that should be stored into loc field for
5236    LOC_NOTE and BITPOS/BITSIZE.  */
5237
5238 static rtx
5239 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5240                       HOST_WIDE_INT bitsize)
5241 {
5242   if (bitsize != -1)
5243     {
5244       loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5245       if (bitpos != 0)
5246         loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5247     }
5248   return loc_note;
5249 }
5250
5251 /* This function either modifies location piece list *DEST in
5252    place (if SRC and INNER is NULL), or copies location piece list
5253    *SRC to *DEST while modifying it.  Location BITPOS is modified
5254    to contain LOC_NOTE, any pieces overlapping it are removed resp.
5255    not copied and if needed some padding around it is added.
5256    When modifying in place, DEST should point to EXPR_LIST where
5257    earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5258    to the start of the whole list and INNER points to the EXPR_LIST
5259    where earlier pieces cover PIECE_BITPOS bits.  */
5260
5261 static void
5262 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5263                    HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5264                    HOST_WIDE_INT bitsize, rtx loc_note)
5265 {
5266   HOST_WIDE_INT diff;
5267   bool copy = inner != NULL;
5268
5269   if (copy)
5270     {
5271       /* First copy all nodes preceding the current bitpos.  */
5272       while (src != inner)
5273         {
5274           *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5275                                    decl_piece_bitsize (*src), NULL_RTX);
5276           dest = &XEXP (*dest, 1);
5277           src = &XEXP (*src, 1);
5278         }
5279     }
5280   /* Add padding if needed.  */
5281   if (bitpos != piece_bitpos)
5282     {
5283       *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5284                                copy ? NULL_RTX : *dest);
5285       dest = &XEXP (*dest, 1);
5286     }
5287   else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5288     {
5289       gcc_assert (!copy);
5290       /* A piece with correct bitpos and bitsize already exist,
5291          just update the location for it and return.  */
5292       *decl_piece_varloc_ptr (*dest) = loc_note;
5293       return;
5294     }
5295   /* Add the piece that changed.  */
5296   *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5297   dest = &XEXP (*dest, 1);
5298   /* Skip over pieces that overlap it.  */
5299   diff = bitpos - piece_bitpos + bitsize;
5300   if (!copy)
5301     src = dest;
5302   while (diff > 0 && *src)
5303     {
5304       rtx piece = *src;
5305       diff -= decl_piece_bitsize (piece);
5306       if (copy)
5307         src = &XEXP (piece, 1);
5308       else
5309         {
5310           *src = XEXP (piece, 1);
5311           free_EXPR_LIST_node (piece);
5312         }
5313     }
5314   /* Add padding if needed.  */
5315   if (diff < 0 && *src)
5316     {
5317       if (!copy)
5318         dest = src;
5319       *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5320       dest = &XEXP (*dest, 1);
5321     }
5322   if (!copy)
5323     return;
5324   /* Finally copy all nodes following it.  */
5325   while (*src)
5326     {
5327       *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5328                                decl_piece_bitsize (*src), NULL_RTX);
5329       dest = &XEXP (*dest, 1);
5330       src = &XEXP (*src, 1);
5331     }
5332 }
5333
5334 /* Add a variable location node to the linked list for DECL.  */
5335
5336 static struct var_loc_node *
5337 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5338 {
5339   unsigned int decl_id;
5340   var_loc_list *temp;
5341   struct var_loc_node *loc = NULL;
5342   HOST_WIDE_INT bitsize = -1, bitpos = -1;
5343
5344   if (TREE_CODE (decl) == VAR_DECL
5345       && DECL_HAS_DEBUG_EXPR_P (decl))
5346     {
5347       tree realdecl = DECL_DEBUG_EXPR (decl);
5348       if (handled_component_p (realdecl)
5349           || (TREE_CODE (realdecl) == MEM_REF
5350               && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5351         {
5352           HOST_WIDE_INT maxsize;
5353           bool reverse;
5354           tree innerdecl
5355             = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize,
5356                                        &reverse);
5357           if (!DECL_P (innerdecl)
5358               || DECL_IGNORED_P (innerdecl)
5359               || TREE_STATIC (innerdecl)
5360               || bitsize <= 0
5361               || bitpos + bitsize > 256
5362               || bitsize != maxsize)
5363             return NULL;
5364           decl = innerdecl;
5365         }
5366     }
5367
5368   decl_id = DECL_UID (decl);
5369   var_loc_list **slot
5370     = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5371   if (*slot == NULL)
5372     {
5373       temp = ggc_cleared_alloc<var_loc_list> ();
5374       temp->decl_id = decl_id;
5375       *slot = temp;
5376     }
5377   else
5378     temp = *slot;
5379
5380   /* For PARM_DECLs try to keep around the original incoming value,
5381      even if that means we'll emit a zero-range .debug_loc entry.  */
5382   if (temp->last
5383       && temp->first == temp->last
5384       && TREE_CODE (decl) == PARM_DECL
5385       && NOTE_P (temp->first->loc)
5386       && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5387       && DECL_INCOMING_RTL (decl)
5388       && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5389       && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5390          == GET_CODE (DECL_INCOMING_RTL (decl))
5391       && prev_real_insn (temp->first->loc) == NULL_RTX
5392       && (bitsize != -1
5393           || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5394                            NOTE_VAR_LOCATION_LOC (loc_note))
5395           || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5396               != NOTE_VAR_LOCATION_STATUS (loc_note))))
5397     {
5398       loc = ggc_cleared_alloc<var_loc_node> ();
5399       temp->first->next = loc;
5400       temp->last = loc;
5401       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5402     }
5403   else if (temp->last)
5404     {
5405       struct var_loc_node *last = temp->last, *unused = NULL;
5406       rtx *piece_loc = NULL, last_loc_note;
5407       HOST_WIDE_INT piece_bitpos = 0;
5408       if (last->next)
5409         {
5410           last = last->next;
5411           gcc_assert (last->next == NULL);
5412         }
5413       if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5414         {
5415           piece_loc = &last->loc;
5416           do
5417             {
5418               HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5419               if (piece_bitpos + cur_bitsize > bitpos)
5420                 break;
5421               piece_bitpos += cur_bitsize;
5422               piece_loc = &XEXP (*piece_loc, 1);
5423             }
5424           while (*piece_loc);
5425         }
5426       /* TEMP->LAST here is either pointer to the last but one or
5427          last element in the chained list, LAST is pointer to the
5428          last element.  */
5429       if (label && strcmp (last->label, label) == 0)
5430         {
5431           /* For SRA optimized variables if there weren't any real
5432              insns since last note, just modify the last node.  */
5433           if (piece_loc != NULL)
5434             {
5435               adjust_piece_list (piece_loc, NULL, NULL,
5436                                  bitpos, piece_bitpos, bitsize, loc_note);
5437               return NULL;
5438             }
5439           /* If the last note doesn't cover any instructions, remove it.  */
5440           if (temp->last != last)
5441             {
5442               temp->last->next = NULL;
5443               unused = last;
5444               last = temp->last;
5445               gcc_assert (strcmp (last->label, label) != 0);
5446             }
5447           else
5448             {
5449               gcc_assert (temp->first == temp->last
5450                           || (temp->first->next == temp->last
5451                               && TREE_CODE (decl) == PARM_DECL));
5452               memset (temp->last, '\0', sizeof (*temp->last));
5453               temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5454               return temp->last;
5455             }
5456         }
5457       if (bitsize == -1 && NOTE_P (last->loc))
5458         last_loc_note = last->loc;
5459       else if (piece_loc != NULL
5460                && *piece_loc != NULL_RTX
5461                && piece_bitpos == bitpos
5462                && decl_piece_bitsize (*piece_loc) == bitsize)
5463         last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5464       else
5465         last_loc_note = NULL_RTX;
5466       /* If the current location is the same as the end of the list,
5467          and either both or neither of the locations is uninitialized,
5468          we have nothing to do.  */
5469       if (last_loc_note == NULL_RTX
5470           || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5471                             NOTE_VAR_LOCATION_LOC (loc_note)))
5472           || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5473                != NOTE_VAR_LOCATION_STATUS (loc_note))
5474               && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5475                    == VAR_INIT_STATUS_UNINITIALIZED)
5476                   || (NOTE_VAR_LOCATION_STATUS (loc_note)
5477                       == VAR_INIT_STATUS_UNINITIALIZED))))
5478         {
5479           /* Add LOC to the end of list and update LAST.  If the last
5480              element of the list has been removed above, reuse its
5481              memory for the new node, otherwise allocate a new one.  */
5482           if (unused)
5483             {
5484               loc = unused;
5485               memset (loc, '\0', sizeof (*loc));
5486             }
5487           else
5488             loc = ggc_cleared_alloc<var_loc_node> ();
5489           if (bitsize == -1 || piece_loc == NULL)
5490             loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5491           else
5492             adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5493                                bitpos, piece_bitpos, bitsize, loc_note);
5494           last->next = loc;
5495           /* Ensure TEMP->LAST will point either to the new last but one
5496              element of the chain, or to the last element in it.  */
5497           if (last != temp->last)
5498             temp->last = last;
5499         }
5500       else if (unused)
5501         ggc_free (unused);
5502     }
5503   else
5504     {
5505       loc = ggc_cleared_alloc<var_loc_node> ();
5506       temp->first = loc;
5507       temp->last = loc;
5508       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5509     }
5510   return loc;
5511 }
5512 \f
5513 /* Keep track of the number of spaces used to indent the
5514    output of the debugging routines that print the structure of
5515    the DIE internal representation.  */
5516 static int print_indent;
5517
5518 /* Indent the line the number of spaces given by print_indent.  */
5519
5520 static inline void
5521 print_spaces (FILE *outfile)
5522 {
5523   fprintf (outfile, "%*s", print_indent, "");
5524 }
5525
5526 /* Print a type signature in hex.  */
5527
5528 static inline void
5529 print_signature (FILE *outfile, char *sig)
5530 {
5531   int i;
5532
5533   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5534     fprintf (outfile, "%02x", sig[i] & 0xff);
5535 }
5536
5537 static inline void
5538 print_discr_value (FILE *outfile, dw_discr_value *discr_value)
5539 {
5540   if (discr_value->pos)
5541     fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, discr_value->v.sval);
5542   else
5543     fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, discr_value->v.uval);
5544 }
5545
5546 static void print_loc_descr (dw_loc_descr_ref, FILE *);
5547
5548 /* Print the value associated to the VAL DWARF value node to OUTFILE.  If
5549    RECURSE, output location descriptor operations.  */
5550
5551 static void
5552 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
5553 {
5554   switch (val->val_class)
5555     {
5556     case dw_val_class_addr:
5557       fprintf (outfile, "address");
5558       break;
5559     case dw_val_class_offset:
5560       fprintf (outfile, "offset");
5561       break;
5562     case dw_val_class_loc:
5563       fprintf (outfile, "location descriptor");
5564       if (val->v.val_loc == NULL)
5565         fprintf (outfile, " -> <null>\n");
5566       else if (recurse)
5567         {
5568           fprintf (outfile, ":\n");
5569           print_indent += 4;
5570           print_loc_descr (val->v.val_loc, outfile);
5571           print_indent -= 4;
5572         }
5573       else
5574         fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
5575       break;
5576     case dw_val_class_loc_list:
5577       fprintf (outfile, "location list -> label:%s",
5578                val->v.val_loc_list->ll_symbol);
5579       break;
5580     case dw_val_class_range_list:
5581       fprintf (outfile, "range list");
5582       break;
5583     case dw_val_class_const:
5584       fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
5585       break;
5586     case dw_val_class_unsigned_const:
5587       fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
5588       break;
5589     case dw_val_class_const_double:
5590       fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
5591                         HOST_WIDE_INT_PRINT_UNSIGNED")",
5592                val->v.val_double.high,
5593                val->v.val_double.low);
5594       break;
5595     case dw_val_class_wide_int:
5596       {
5597         int i = val->v.val_wide->get_len ();
5598         fprintf (outfile, "constant (");
5599         gcc_assert (i > 0);
5600         if (val->v.val_wide->elt (i - 1) == 0)
5601           fprintf (outfile, "0x");
5602         fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5603                  val->v.val_wide->elt (--i));
5604         while (--i >= 0)
5605           fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5606                    val->v.val_wide->elt (i));
5607         fprintf (outfile, ")");
5608         break;
5609       }
5610     case dw_val_class_vec:
5611       fprintf (outfile, "floating-point or vector constant");
5612       break;
5613     case dw_val_class_flag:
5614       fprintf (outfile, "%u", val->v.val_flag);
5615       break;
5616     case dw_val_class_die_ref:
5617       if (val->v.val_die_ref.die != NULL)
5618         {
5619           dw_die_ref die = val->v.val_die_ref.die;
5620
5621           if (die->comdat_type_p)
5622             {
5623               fprintf (outfile, "die -> signature: ");
5624               print_signature (outfile,
5625                                die->die_id.die_type_node->signature);
5626             }
5627           else if (die->die_id.die_symbol)
5628             fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
5629           else
5630             fprintf (outfile, "die -> %ld", die->die_offset);
5631           fprintf (outfile, " (%p)", (void *) die);
5632         }
5633       else
5634         fprintf (outfile, "die -> <null>");
5635       break;
5636     case dw_val_class_vms_delta:
5637       fprintf (outfile, "delta: @slotcount(%s-%s)",
5638                val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
5639       break;
5640     case dw_val_class_lbl_id:
5641     case dw_val_class_lineptr:
5642     case dw_val_class_macptr:
5643     case dw_val_class_high_pc:
5644       fprintf (outfile, "label: %s", val->v.val_lbl_id);
5645       break;
5646     case dw_val_class_str:
5647       if (val->v.val_str->str != NULL)
5648         fprintf (outfile, "\"%s\"", val->v.val_str->str);
5649       else
5650         fprintf (outfile, "<null>");
5651       break;
5652     case dw_val_class_file:
5653       fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
5654                val->v.val_file->emitted_number);
5655       break;
5656     case dw_val_class_data8:
5657       {
5658         int i;
5659
5660         for (i = 0; i < 8; i++)
5661           fprintf (outfile, "%02x", val->v.val_data8[i]);
5662         break;
5663       }
5664     case dw_val_class_discr_value:
5665       print_discr_value (outfile, &val->v.val_discr_value);
5666       break;
5667     case dw_val_class_discr_list:
5668       for (dw_discr_list_ref node = val->v.val_discr_list;
5669            node != NULL;
5670            node = node->dw_discr_next)
5671         {
5672           if (node->dw_discr_range)
5673             {
5674               fprintf (outfile, " .. ");
5675               print_discr_value (outfile, &node->dw_discr_lower_bound);
5676               print_discr_value (outfile, &node->dw_discr_upper_bound);
5677             }
5678           else
5679             print_discr_value (outfile, &node->dw_discr_lower_bound);
5680
5681           if (node->dw_discr_next != NULL)
5682             fprintf (outfile, " | ");
5683         }
5684     default:
5685       break;
5686     }
5687 }
5688
5689 /* Likewise, for a DIE attribute.  */
5690
5691 static void
5692 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
5693 {
5694   print_dw_val (&a->dw_attr_val, recurse, outfile);
5695 }
5696
5697
5698 /* Print the list of operands in the LOC location description to OUTFILE.  This
5699    routine is a debugging aid only.  */
5700
5701 static void
5702 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
5703 {
5704   dw_loc_descr_ref l = loc;
5705
5706   if (loc == NULL)
5707     {
5708       print_spaces (outfile);
5709       fprintf (outfile, "<null>\n");
5710       return;
5711     }
5712
5713   for (l = loc; l != NULL; l = l->dw_loc_next)
5714     {
5715       print_spaces (outfile);
5716       fprintf (outfile, "(%p) %s",
5717                (void *) l,
5718                dwarf_stack_op_name (l->dw_loc_opc));
5719       if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
5720         {
5721           fprintf (outfile, " ");
5722           print_dw_val (&l->dw_loc_oprnd1, false, outfile);
5723         }
5724       if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
5725         {
5726           fprintf (outfile, ", ");
5727           print_dw_val (&l->dw_loc_oprnd2, false, outfile);
5728         }
5729       fprintf (outfile, "\n");
5730     }
5731 }
5732
5733 /* Print the information associated with a given DIE, and its children.
5734    This routine is a debugging aid only.  */
5735
5736 static void
5737 print_die (dw_die_ref die, FILE *outfile)
5738 {
5739   dw_attr_node *a;
5740   dw_die_ref c;
5741   unsigned ix;
5742
5743   print_spaces (outfile);
5744   fprintf (outfile, "DIE %4ld: %s (%p)\n",
5745            die->die_offset, dwarf_tag_name (die->die_tag),
5746            (void*) die);
5747   print_spaces (outfile);
5748   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
5749   fprintf (outfile, " offset: %ld", die->die_offset);
5750   fprintf (outfile, " mark: %d\n", die->die_mark);
5751
5752   if (die->comdat_type_p)
5753     {
5754       print_spaces (outfile);
5755       fprintf (outfile, "  signature: ");
5756       print_signature (outfile, die->die_id.die_type_node->signature);
5757       fprintf (outfile, "\n");
5758     }
5759
5760   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5761     {
5762       print_spaces (outfile);
5763       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
5764
5765       print_attribute (a, true, outfile);
5766       fprintf (outfile, "\n");
5767     }
5768
5769   if (die->die_child != NULL)
5770     {
5771       print_indent += 4;
5772       FOR_EACH_CHILD (die, c, print_die (c, outfile));
5773       print_indent -= 4;
5774     }
5775   if (print_indent == 0)
5776     fprintf (outfile, "\n");
5777 }
5778
5779 /* Print the list of operations in the LOC location description.  */
5780
5781 DEBUG_FUNCTION void
5782 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
5783 {
5784   print_loc_descr (loc, stderr);
5785 }
5786
5787 /* Print the information collected for a given DIE.  */
5788
5789 DEBUG_FUNCTION void
5790 debug_dwarf_die (dw_die_ref die)
5791 {
5792   print_die (die, stderr);
5793 }
5794
5795 DEBUG_FUNCTION void
5796 debug (die_struct &ref)
5797 {
5798   print_die (&ref, stderr);
5799 }
5800
5801 DEBUG_FUNCTION void
5802 debug (die_struct *ptr)
5803 {
5804   if (ptr)
5805     debug (*ptr);
5806   else
5807     fprintf (stderr, "<nil>\n");
5808 }
5809
5810
5811 /* Print all DWARF information collected for the compilation unit.
5812    This routine is a debugging aid only.  */
5813
5814 DEBUG_FUNCTION void
5815 debug_dwarf (void)
5816 {
5817   print_indent = 0;
5818   print_die (comp_unit_die (), stderr);
5819 }
5820
5821 /* Sanity checks on DIEs.  */
5822
5823 static void
5824 check_die (dw_die_ref die)
5825 {
5826   unsigned ix;
5827   dw_attr_node *a;
5828   bool inline_found = false;
5829   int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
5830   int n_decl_line = 0, n_decl_file = 0;
5831   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5832     {
5833       switch (a->dw_attr)
5834         {
5835         case DW_AT_inline:
5836           if (a->dw_attr_val.v.val_unsigned)
5837             inline_found = true;
5838           break;
5839         case DW_AT_location:
5840           ++n_location;
5841           break;
5842         case DW_AT_low_pc:
5843           ++n_low_pc;
5844           break;
5845         case DW_AT_high_pc:
5846           ++n_high_pc;
5847           break;
5848         case DW_AT_artificial:
5849           ++n_artificial;
5850           break;
5851         case DW_AT_decl_line:
5852           ++n_decl_line;
5853           break;
5854         case DW_AT_decl_file:
5855           ++n_decl_file;
5856           break;
5857         default:
5858           break;
5859         }
5860     }
5861   if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
5862       || n_decl_line > 1 || n_decl_file > 1)
5863     {
5864       fprintf (stderr, "Duplicate attributes in DIE:\n");
5865       debug_dwarf_die (die);
5866       gcc_unreachable ();
5867     }
5868   if (inline_found)
5869     {
5870       /* A debugging information entry that is a member of an abstract
5871          instance tree [that has DW_AT_inline] should not contain any
5872          attributes which describe aspects of the subroutine which vary
5873          between distinct inlined expansions or distinct out-of-line
5874          expansions.  */
5875       FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5876         gcc_assert (a->dw_attr != DW_AT_low_pc
5877                     && a->dw_attr != DW_AT_high_pc
5878                     && a->dw_attr != DW_AT_location
5879                     && a->dw_attr != DW_AT_frame_base
5880                     && a->dw_attr != DW_AT_GNU_all_call_sites);
5881     }
5882 }
5883 \f
5884 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
5885    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
5886    DIE that marks the start of the DIEs for this include file.  */
5887
5888 static dw_die_ref
5889 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5890 {
5891   const char *filename = get_AT_string (bincl_die, DW_AT_name);
5892   dw_die_ref new_unit = gen_compile_unit_die (filename);
5893
5894   new_unit->die_sib = old_unit;
5895   return new_unit;
5896 }
5897
5898 /* Close an include-file CU and reopen the enclosing one.  */
5899
5900 static dw_die_ref
5901 pop_compile_unit (dw_die_ref old_unit)
5902 {
5903   dw_die_ref new_unit = old_unit->die_sib;
5904
5905   old_unit->die_sib = NULL;
5906   return new_unit;
5907 }
5908
5909 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5910 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5911 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5912
5913 /* Calculate the checksum of a location expression.  */
5914
5915 static inline void
5916 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5917 {
5918   int tem;
5919   inchash::hash hstate;
5920   hashval_t hash;
5921
5922   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5923   CHECKSUM (tem);
5924   hash_loc_operands (loc, hstate);
5925   hash = hstate.end();
5926   CHECKSUM (hash);
5927 }
5928
5929 /* Calculate the checksum of an attribute.  */
5930
5931 static void
5932 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
5933 {
5934   dw_loc_descr_ref loc;
5935   rtx r;
5936
5937   CHECKSUM (at->dw_attr);
5938
5939   /* We don't care that this was compiled with a different compiler
5940      snapshot; if the output is the same, that's what matters.  */
5941   if (at->dw_attr == DW_AT_producer)
5942     return;
5943
5944   switch (AT_class (at))
5945     {
5946     case dw_val_class_const:
5947       CHECKSUM (at->dw_attr_val.v.val_int);
5948       break;
5949     case dw_val_class_unsigned_const:
5950       CHECKSUM (at->dw_attr_val.v.val_unsigned);
5951       break;
5952     case dw_val_class_const_double:
5953       CHECKSUM (at->dw_attr_val.v.val_double);
5954       break;
5955     case dw_val_class_wide_int:
5956       CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
5957                       get_full_len (*at->dw_attr_val.v.val_wide)
5958                       * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
5959       break;
5960     case dw_val_class_vec:
5961       CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5962                       (at->dw_attr_val.v.val_vec.length
5963                        * at->dw_attr_val.v.val_vec.elt_size));
5964       break;
5965     case dw_val_class_flag:
5966       CHECKSUM (at->dw_attr_val.v.val_flag);
5967       break;
5968     case dw_val_class_str:
5969       CHECKSUM_STRING (AT_string (at));
5970       break;
5971
5972     case dw_val_class_addr:
5973       r = AT_addr (at);
5974       gcc_assert (GET_CODE (r) == SYMBOL_REF);
5975       CHECKSUM_STRING (XSTR (r, 0));
5976       break;
5977
5978     case dw_val_class_offset:
5979       CHECKSUM (at->dw_attr_val.v.val_offset);
5980       break;
5981
5982     case dw_val_class_loc:
5983       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5984         loc_checksum (loc, ctx);
5985       break;
5986
5987     case dw_val_class_die_ref:
5988       die_checksum (AT_ref (at), ctx, mark);
5989       break;
5990
5991     case dw_val_class_fde_ref:
5992     case dw_val_class_vms_delta:
5993     case dw_val_class_lbl_id:
5994     case dw_val_class_lineptr:
5995     case dw_val_class_macptr:
5996     case dw_val_class_high_pc:
5997       break;
5998
5999     case dw_val_class_file:
6000       CHECKSUM_STRING (AT_file (at)->filename);
6001       break;
6002
6003     case dw_val_class_data8:
6004       CHECKSUM (at->dw_attr_val.v.val_data8);
6005       break;
6006
6007     default:
6008       break;
6009     }
6010 }
6011
6012 /* Calculate the checksum of a DIE.  */
6013
6014 static void
6015 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6016 {
6017   dw_die_ref c;
6018   dw_attr_node *a;
6019   unsigned ix;
6020
6021   /* To avoid infinite recursion.  */
6022   if (die->die_mark)
6023     {
6024       CHECKSUM (die->die_mark);
6025       return;
6026     }
6027   die->die_mark = ++(*mark);
6028
6029   CHECKSUM (die->die_tag);
6030
6031   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6032     attr_checksum (a, ctx, mark);
6033
6034   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6035 }
6036
6037 #undef CHECKSUM
6038 #undef CHECKSUM_BLOCK
6039 #undef CHECKSUM_STRING
6040
6041 /* For DWARF-4 types, include the trailing NULL when checksumming strings.  */
6042 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6043 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6044 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
6045 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
6046 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
6047 #define CHECKSUM_ATTR(FOO) \
6048   if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
6049
6050 /* Calculate the checksum of a number in signed LEB128 format.  */
6051
6052 static void
6053 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
6054 {
6055   unsigned char byte;
6056   bool more;
6057
6058   while (1)
6059     {
6060       byte = (value & 0x7f);
6061       value >>= 7;
6062       more = !((value == 0 && (byte & 0x40) == 0)
6063                 || (value == -1 && (byte & 0x40) != 0));
6064       if (more)
6065         byte |= 0x80;
6066       CHECKSUM (byte);
6067       if (!more)
6068         break;
6069     }
6070 }
6071
6072 /* Calculate the checksum of a number in unsigned LEB128 format.  */
6073
6074 static void
6075 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
6076 {
6077   while (1)
6078     {
6079       unsigned char byte = (value & 0x7f);
6080       value >>= 7;
6081       if (value != 0)
6082         /* More bytes to follow.  */
6083         byte |= 0x80;
6084       CHECKSUM (byte);
6085       if (value == 0)
6086         break;
6087     }
6088 }
6089
6090 /* Checksum the context of the DIE.  This adds the names of any
6091    surrounding namespaces or structures to the checksum.  */
6092
6093 static void
6094 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
6095 {
6096   const char *name;
6097   dw_die_ref spec;
6098   int tag = die->die_tag;
6099
6100   if (tag != DW_TAG_namespace
6101       && tag != DW_TAG_structure_type
6102       && tag != DW_TAG_class_type)
6103     return;
6104
6105   name = get_AT_string (die, DW_AT_name);
6106
6107   spec = get_AT_ref (die, DW_AT_specification);
6108   if (spec != NULL)
6109     die = spec;
6110
6111   if (die->die_parent != NULL)
6112     checksum_die_context (die->die_parent, ctx);
6113
6114   CHECKSUM_ULEB128 ('C');
6115   CHECKSUM_ULEB128 (tag);
6116   if (name != NULL)
6117     CHECKSUM_STRING (name);
6118 }
6119
6120 /* Calculate the checksum of a location expression.  */
6121
6122 static inline void
6123 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6124 {
6125   /* Special case for lone DW_OP_plus_uconst: checksum as if the location
6126      were emitted as a DW_FORM_sdata instead of a location expression.  */
6127   if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
6128     {
6129       CHECKSUM_ULEB128 (DW_FORM_sdata);
6130       CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
6131       return;
6132     }
6133
6134   /* Otherwise, just checksum the raw location expression.  */
6135   while (loc != NULL)
6136     {
6137       inchash::hash hstate;
6138       hashval_t hash;
6139
6140       CHECKSUM_ULEB128 (loc->dtprel);
6141       CHECKSUM_ULEB128 (loc->dw_loc_opc);
6142       hash_loc_operands (loc, hstate);
6143       hash = hstate.end ();
6144       CHECKSUM (hash);
6145       loc = loc->dw_loc_next;
6146     }
6147 }
6148
6149 /* Calculate the checksum of an attribute.  */
6150
6151 static void
6152 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
6153                        struct md5_ctx *ctx, int *mark)
6154 {
6155   dw_loc_descr_ref loc;
6156   rtx r;
6157
6158   if (AT_class (at) == dw_val_class_die_ref)
6159     {
6160       dw_die_ref target_die = AT_ref (at);
6161
6162       /* For pointer and reference types, we checksum only the (qualified)
6163          name of the target type (if there is a name).  For friend entries,
6164          we checksum only the (qualified) name of the target type or function.
6165          This allows the checksum to remain the same whether the target type
6166          is complete or not.  */
6167       if ((at->dw_attr == DW_AT_type
6168            && (tag == DW_TAG_pointer_type
6169                || tag == DW_TAG_reference_type
6170                || tag == DW_TAG_rvalue_reference_type
6171                || tag == DW_TAG_ptr_to_member_type))
6172           || (at->dw_attr == DW_AT_friend
6173               && tag == DW_TAG_friend))
6174         {
6175           dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
6176
6177           if (name_attr != NULL)
6178             {
6179               dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6180
6181               if (decl == NULL)
6182                 decl = target_die;
6183               CHECKSUM_ULEB128 ('N');
6184               CHECKSUM_ULEB128 (at->dw_attr);
6185               if (decl->die_parent != NULL)
6186                 checksum_die_context (decl->die_parent, ctx);
6187               CHECKSUM_ULEB128 ('E');
6188               CHECKSUM_STRING (AT_string (name_attr));
6189               return;
6190             }
6191         }
6192
6193       /* For all other references to another DIE, we check to see if the
6194          target DIE has already been visited.  If it has, we emit a
6195          backward reference; if not, we descend recursively.  */
6196       if (target_die->die_mark > 0)
6197         {
6198           CHECKSUM_ULEB128 ('R');
6199           CHECKSUM_ULEB128 (at->dw_attr);
6200           CHECKSUM_ULEB128 (target_die->die_mark);
6201         }
6202       else
6203         {
6204           dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6205
6206           if (decl == NULL)
6207             decl = target_die;
6208           target_die->die_mark = ++(*mark);
6209           CHECKSUM_ULEB128 ('T');
6210           CHECKSUM_ULEB128 (at->dw_attr);
6211           if (decl->die_parent != NULL)
6212             checksum_die_context (decl->die_parent, ctx);
6213           die_checksum_ordered (target_die, ctx, mark);
6214         }
6215       return;
6216     }
6217
6218   CHECKSUM_ULEB128 ('A');
6219   CHECKSUM_ULEB128 (at->dw_attr);
6220
6221   switch (AT_class (at))
6222     {
6223     case dw_val_class_const:
6224       CHECKSUM_ULEB128 (DW_FORM_sdata);
6225       CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
6226       break;
6227
6228     case dw_val_class_unsigned_const:
6229       CHECKSUM_ULEB128 (DW_FORM_sdata);
6230       CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6231       break;
6232
6233     case dw_val_class_const_double:
6234       CHECKSUM_ULEB128 (DW_FORM_block);
6235       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6236       CHECKSUM (at->dw_attr_val.v.val_double);
6237       break;
6238
6239     case dw_val_class_wide_int:
6240       CHECKSUM_ULEB128 (DW_FORM_block);
6241       CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
6242                         * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
6243       CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6244                       get_full_len (*at->dw_attr_val.v.val_wide)
6245                       * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6246       break;
6247
6248     case dw_val_class_vec:
6249       CHECKSUM_ULEB128 (DW_FORM_block);
6250       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6251                         * at->dw_attr_val.v.val_vec.elt_size);
6252       CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6253                       (at->dw_attr_val.v.val_vec.length
6254                        * at->dw_attr_val.v.val_vec.elt_size));
6255       break;
6256
6257     case dw_val_class_flag:
6258       CHECKSUM_ULEB128 (DW_FORM_flag);
6259       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6260       break;
6261
6262     case dw_val_class_str:
6263       CHECKSUM_ULEB128 (DW_FORM_string);
6264       CHECKSUM_STRING (AT_string (at));
6265       break;
6266
6267     case dw_val_class_addr:
6268       r = AT_addr (at);
6269       gcc_assert (GET_CODE (r) == SYMBOL_REF);
6270       CHECKSUM_ULEB128 (DW_FORM_string);
6271       CHECKSUM_STRING (XSTR (r, 0));
6272       break;
6273
6274     case dw_val_class_offset:
6275       CHECKSUM_ULEB128 (DW_FORM_sdata);
6276       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6277       break;
6278
6279     case dw_val_class_loc:
6280       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6281         loc_checksum_ordered (loc, ctx);
6282       break;
6283
6284     case dw_val_class_fde_ref:
6285     case dw_val_class_lbl_id:
6286     case dw_val_class_lineptr:
6287     case dw_val_class_macptr:
6288     case dw_val_class_high_pc:
6289       break;
6290
6291     case dw_val_class_file:
6292       CHECKSUM_ULEB128 (DW_FORM_string);
6293       CHECKSUM_STRING (AT_file (at)->filename);
6294       break;
6295
6296     case dw_val_class_data8:
6297       CHECKSUM (at->dw_attr_val.v.val_data8);
6298       break;
6299
6300     default:
6301       break;
6302     }
6303 }
6304
6305 struct checksum_attributes
6306 {
6307   dw_attr_node *at_name;
6308   dw_attr_node *at_type;
6309   dw_attr_node *at_friend;
6310   dw_attr_node *at_accessibility;
6311   dw_attr_node *at_address_class;
6312   dw_attr_node *at_allocated;
6313   dw_attr_node *at_artificial;
6314   dw_attr_node *at_associated;
6315   dw_attr_node *at_binary_scale;
6316   dw_attr_node *at_bit_offset;
6317   dw_attr_node *at_bit_size;
6318   dw_attr_node *at_bit_stride;
6319   dw_attr_node *at_byte_size;
6320   dw_attr_node *at_byte_stride;
6321   dw_attr_node *at_const_value;
6322   dw_attr_node *at_containing_type;
6323   dw_attr_node *at_count;
6324   dw_attr_node *at_data_location;
6325   dw_attr_node *at_data_member_location;
6326   dw_attr_node *at_decimal_scale;
6327   dw_attr_node *at_decimal_sign;
6328   dw_attr_node *at_default_value;
6329   dw_attr_node *at_digit_count;
6330   dw_attr_node *at_discr;
6331   dw_attr_node *at_discr_list;
6332   dw_attr_node *at_discr_value;
6333   dw_attr_node *at_encoding;
6334   dw_attr_node *at_endianity;
6335   dw_attr_node *at_explicit;
6336   dw_attr_node *at_is_optional;
6337   dw_attr_node *at_location;
6338   dw_attr_node *at_lower_bound;
6339   dw_attr_node *at_mutable;
6340   dw_attr_node *at_ordering;
6341   dw_attr_node *at_picture_string;
6342   dw_attr_node *at_prototyped;
6343   dw_attr_node *at_small;
6344   dw_attr_node *at_segment;
6345   dw_attr_node *at_string_length;
6346   dw_attr_node *at_threads_scaled;
6347   dw_attr_node *at_upper_bound;
6348   dw_attr_node *at_use_location;
6349   dw_attr_node *at_use_UTF8;
6350   dw_attr_node *at_variable_parameter;
6351   dw_attr_node *at_virtuality;
6352   dw_attr_node *at_visibility;
6353   dw_attr_node *at_vtable_elem_location;
6354 };
6355
6356 /* Collect the attributes that we will want to use for the checksum.  */
6357
6358 static void
6359 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6360 {
6361   dw_attr_node *a;
6362   unsigned ix;
6363
6364   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6365     {
6366       switch (a->dw_attr)
6367         {
6368         case DW_AT_name:
6369           attrs->at_name = a;
6370           break;
6371         case DW_AT_type:
6372           attrs->at_type = a;
6373           break;
6374         case DW_AT_friend:
6375           attrs->at_friend = a;
6376           break;
6377         case DW_AT_accessibility:
6378           attrs->at_accessibility = a;
6379           break;
6380         case DW_AT_address_class:
6381           attrs->at_address_class = a;
6382           break;
6383         case DW_AT_allocated:
6384           attrs->at_allocated = a;
6385           break;
6386         case DW_AT_artificial:
6387           attrs->at_artificial = a;
6388           break;
6389         case DW_AT_associated:
6390           attrs->at_associated = a;
6391           break;
6392         case DW_AT_binary_scale:
6393           attrs->at_binary_scale = a;
6394           break;
6395         case DW_AT_bit_offset:
6396           attrs->at_bit_offset = a;
6397           break;
6398         case DW_AT_bit_size:
6399           attrs->at_bit_size = a;
6400           break;
6401         case DW_AT_bit_stride:
6402           attrs->at_bit_stride = a;
6403           break;
6404         case DW_AT_byte_size:
6405           attrs->at_byte_size = a;
6406           break;
6407         case DW_AT_byte_stride:
6408           attrs->at_byte_stride = a;
6409           break;
6410         case DW_AT_const_value:
6411           attrs->at_const_value = a;
6412           break;
6413         case DW_AT_containing_type:
6414           attrs->at_containing_type = a;
6415           break;
6416         case DW_AT_count:
6417           attrs->at_count = a;
6418           break;
6419         case DW_AT_data_location:
6420           attrs->at_data_location = a;
6421           break;
6422         case DW_AT_data_member_location:
6423           attrs->at_data_member_location = a;
6424           break;
6425         case DW_AT_decimal_scale:
6426           attrs->at_decimal_scale = a;
6427           break;
6428         case DW_AT_decimal_sign:
6429           attrs->at_decimal_sign = a;
6430           break;
6431         case DW_AT_default_value:
6432           attrs->at_default_value = a;
6433           break;
6434         case DW_AT_digit_count:
6435           attrs->at_digit_count = a;
6436           break;
6437         case DW_AT_discr:
6438           attrs->at_discr = a;
6439           break;
6440         case DW_AT_discr_list:
6441           attrs->at_discr_list = a;
6442           break;
6443         case DW_AT_discr_value:
6444           attrs->at_discr_value = a;
6445           break;
6446         case DW_AT_encoding:
6447           attrs->at_encoding = a;
6448           break;
6449         case DW_AT_endianity:
6450           attrs->at_endianity = a;
6451           break;
6452         case DW_AT_explicit:
6453           attrs->at_explicit = a;
6454           break;
6455         case DW_AT_is_optional:
6456           attrs->at_is_optional = a;
6457           break;
6458         case DW_AT_location:
6459           attrs->at_location = a;
6460           break;
6461         case DW_AT_lower_bound:
6462           attrs->at_lower_bound = a;
6463           break;
6464         case DW_AT_mutable:
6465           attrs->at_mutable = a;
6466           break;
6467         case DW_AT_ordering:
6468           attrs->at_ordering = a;
6469           break;
6470         case DW_AT_picture_string:
6471           attrs->at_picture_string = a;
6472           break;
6473         case DW_AT_prototyped:
6474           attrs->at_prototyped = a;
6475           break;
6476         case DW_AT_small:
6477           attrs->at_small = a;
6478           break;
6479         case DW_AT_segment:
6480           attrs->at_segment = a;
6481           break;
6482         case DW_AT_string_length:
6483           attrs->at_string_length = a;
6484           break;
6485         case DW_AT_threads_scaled:
6486           attrs->at_threads_scaled = a;
6487           break;
6488         case DW_AT_upper_bound:
6489           attrs->at_upper_bound = a;
6490           break;
6491         case DW_AT_use_location:
6492           attrs->at_use_location = a;
6493           break;
6494         case DW_AT_use_UTF8:
6495           attrs->at_use_UTF8 = a;
6496           break;
6497         case DW_AT_variable_parameter:
6498           attrs->at_variable_parameter = a;
6499           break;
6500         case DW_AT_virtuality:
6501           attrs->at_virtuality = a;
6502           break;
6503         case DW_AT_visibility:
6504           attrs->at_visibility = a;
6505           break;
6506         case DW_AT_vtable_elem_location:
6507           attrs->at_vtable_elem_location = a;
6508           break;
6509         default:
6510           break;
6511         }
6512     }
6513 }
6514
6515 /* Calculate the checksum of a DIE, using an ordered subset of attributes.  */
6516
6517 static void
6518 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6519 {
6520   dw_die_ref c;
6521   dw_die_ref decl;
6522   struct checksum_attributes attrs;
6523
6524   CHECKSUM_ULEB128 ('D');
6525   CHECKSUM_ULEB128 (die->die_tag);
6526
6527   memset (&attrs, 0, sizeof (attrs));
6528
6529   decl = get_AT_ref (die, DW_AT_specification);
6530   if (decl != NULL)
6531     collect_checksum_attributes (&attrs, decl);
6532   collect_checksum_attributes (&attrs, die);
6533
6534   CHECKSUM_ATTR (attrs.at_name);
6535   CHECKSUM_ATTR (attrs.at_accessibility);
6536   CHECKSUM_ATTR (attrs.at_address_class);
6537   CHECKSUM_ATTR (attrs.at_allocated);
6538   CHECKSUM_ATTR (attrs.at_artificial);
6539   CHECKSUM_ATTR (attrs.at_associated);
6540   CHECKSUM_ATTR (attrs.at_binary_scale);
6541   CHECKSUM_ATTR (attrs.at_bit_offset);
6542   CHECKSUM_ATTR (attrs.at_bit_size);
6543   CHECKSUM_ATTR (attrs.at_bit_stride);
6544   CHECKSUM_ATTR (attrs.at_byte_size);
6545   CHECKSUM_ATTR (attrs.at_byte_stride);
6546   CHECKSUM_ATTR (attrs.at_const_value);
6547   CHECKSUM_ATTR (attrs.at_containing_type);
6548   CHECKSUM_ATTR (attrs.at_count);
6549   CHECKSUM_ATTR (attrs.at_data_location);
6550   CHECKSUM_ATTR (attrs.at_data_member_location);
6551   CHECKSUM_ATTR (attrs.at_decimal_scale);
6552   CHECKSUM_ATTR (attrs.at_decimal_sign);
6553   CHECKSUM_ATTR (attrs.at_default_value);
6554   CHECKSUM_ATTR (attrs.at_digit_count);
6555   CHECKSUM_ATTR (attrs.at_discr);
6556   CHECKSUM_ATTR (attrs.at_discr_list);
6557   CHECKSUM_ATTR (attrs.at_discr_value);
6558   CHECKSUM_ATTR (attrs.at_encoding);
6559   CHECKSUM_ATTR (attrs.at_endianity);
6560   CHECKSUM_ATTR (attrs.at_explicit);
6561   CHECKSUM_ATTR (attrs.at_is_optional);
6562   CHECKSUM_ATTR (attrs.at_location);
6563   CHECKSUM_ATTR (attrs.at_lower_bound);
6564   CHECKSUM_ATTR (attrs.at_mutable);
6565   CHECKSUM_ATTR (attrs.at_ordering);
6566   CHECKSUM_ATTR (attrs.at_picture_string);
6567   CHECKSUM_ATTR (attrs.at_prototyped);
6568   CHECKSUM_ATTR (attrs.at_small);
6569   CHECKSUM_ATTR (attrs.at_segment);
6570   CHECKSUM_ATTR (attrs.at_string_length);
6571   CHECKSUM_ATTR (attrs.at_threads_scaled);
6572   CHECKSUM_ATTR (attrs.at_upper_bound);
6573   CHECKSUM_ATTR (attrs.at_use_location);
6574   CHECKSUM_ATTR (attrs.at_use_UTF8);
6575   CHECKSUM_ATTR (attrs.at_variable_parameter);
6576   CHECKSUM_ATTR (attrs.at_virtuality);
6577   CHECKSUM_ATTR (attrs.at_visibility);
6578   CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6579   CHECKSUM_ATTR (attrs.at_type);
6580   CHECKSUM_ATTR (attrs.at_friend);
6581
6582   /* Checksum the child DIEs.  */
6583   c = die->die_child;
6584   if (c) do {
6585     dw_attr_node *name_attr;
6586
6587     c = c->die_sib;
6588     name_attr = get_AT (c, DW_AT_name);
6589     if (is_template_instantiation (c))
6590       {
6591         /* Ignore instantiations of member type and function templates.  */
6592       }
6593     else if (name_attr != NULL
6594              && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6595       {
6596         /* Use a shallow checksum for named nested types and member
6597            functions.  */
6598         CHECKSUM_ULEB128 ('S');
6599         CHECKSUM_ULEB128 (c->die_tag);
6600         CHECKSUM_STRING (AT_string (name_attr));
6601       }
6602     else
6603       {
6604         /* Use a deep checksum for other children.  */
6605         /* Mark this DIE so it gets processed when unmarking.  */
6606         if (c->die_mark == 0)
6607           c->die_mark = -1;
6608         die_checksum_ordered (c, ctx, mark);
6609       }
6610   } while (c != die->die_child);
6611
6612   CHECKSUM_ULEB128 (0);
6613 }
6614
6615 /* Add a type name and tag to a hash.  */
6616 static void
6617 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6618 {
6619   CHECKSUM_ULEB128 (tag);
6620   CHECKSUM_STRING (name);
6621 }
6622
6623 #undef CHECKSUM
6624 #undef CHECKSUM_STRING
6625 #undef CHECKSUM_ATTR
6626 #undef CHECKSUM_LEB128
6627 #undef CHECKSUM_ULEB128
6628
6629 /* Generate the type signature for DIE.  This is computed by generating an
6630    MD5 checksum over the DIE's tag, its relevant attributes, and its
6631    children.  Attributes that are references to other DIEs are processed
6632    by recursion, using the MARK field to prevent infinite recursion.
6633    If the DIE is nested inside a namespace or another type, we also
6634    need to include that context in the signature.  The lower 64 bits
6635    of the resulting MD5 checksum comprise the signature.  */
6636
6637 static void
6638 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6639 {
6640   int mark;
6641   const char *name;
6642   unsigned char checksum[16];
6643   struct md5_ctx ctx;
6644   dw_die_ref decl;
6645   dw_die_ref parent;
6646
6647   name = get_AT_string (die, DW_AT_name);
6648   decl = get_AT_ref (die, DW_AT_specification);
6649   parent = get_die_parent (die);
6650
6651   /* First, compute a signature for just the type name (and its surrounding
6652      context, if any.  This is stored in the type unit DIE for link-time
6653      ODR (one-definition rule) checking.  */
6654
6655   if (is_cxx () && name != NULL)
6656     {
6657       md5_init_ctx (&ctx);
6658
6659       /* Checksum the names of surrounding namespaces and structures.  */
6660       if (parent != NULL)
6661         checksum_die_context (parent, &ctx);
6662
6663       /* Checksum the current DIE. */
6664       die_odr_checksum (die->die_tag, name, &ctx);
6665       md5_finish_ctx (&ctx, checksum);
6666
6667       add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6668     }
6669
6670   /* Next, compute the complete type signature.  */
6671
6672   md5_init_ctx (&ctx);
6673   mark = 1;
6674   die->die_mark = mark;
6675
6676   /* Checksum the names of surrounding namespaces and structures.  */
6677   if (parent != NULL)
6678     checksum_die_context (parent, &ctx);
6679
6680   /* Checksum the DIE and its children.  */
6681   die_checksum_ordered (die, &ctx, &mark);
6682   unmark_all_dies (die);
6683   md5_finish_ctx (&ctx, checksum);
6684
6685   /* Store the signature in the type node and link the type DIE and the
6686      type node together.  */
6687   memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6688           DWARF_TYPE_SIGNATURE_SIZE);
6689   die->comdat_type_p = true;
6690   die->die_id.die_type_node = type_node;
6691   type_node->type_die = die;
6692
6693   /* If the DIE is a specification, link its declaration to the type node
6694      as well.  */
6695   if (decl != NULL)
6696     {
6697       decl->comdat_type_p = true;
6698       decl->die_id.die_type_node = type_node;
6699     }
6700 }
6701
6702 /* Do the location expressions look same?  */
6703 static inline int
6704 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6705 {
6706   return loc1->dw_loc_opc == loc2->dw_loc_opc
6707          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6708          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6709 }
6710
6711 /* Do the values look the same?  */
6712 static int
6713 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6714 {
6715   dw_loc_descr_ref loc1, loc2;
6716   rtx r1, r2;
6717
6718   if (v1->val_class != v2->val_class)
6719     return 0;
6720
6721   switch (v1->val_class)
6722     {
6723     case dw_val_class_const:
6724       return v1->v.val_int == v2->v.val_int;
6725     case dw_val_class_unsigned_const:
6726       return v1->v.val_unsigned == v2->v.val_unsigned;
6727     case dw_val_class_const_double:
6728       return v1->v.val_double.high == v2->v.val_double.high
6729              && v1->v.val_double.low == v2->v.val_double.low;
6730     case dw_val_class_wide_int:
6731       return *v1->v.val_wide == *v2->v.val_wide;
6732     case dw_val_class_vec:
6733       if (v1->v.val_vec.length != v2->v.val_vec.length
6734           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6735         return 0;
6736       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6737                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
6738         return 0;
6739       return 1;
6740     case dw_val_class_flag:
6741       return v1->v.val_flag == v2->v.val_flag;
6742     case dw_val_class_str:
6743       return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6744
6745     case dw_val_class_addr:
6746       r1 = v1->v.val_addr;
6747       r2 = v2->v.val_addr;
6748       if (GET_CODE (r1) != GET_CODE (r2))
6749         return 0;
6750       return !rtx_equal_p (r1, r2);
6751
6752     case dw_val_class_offset:
6753       return v1->v.val_offset == v2->v.val_offset;
6754
6755     case dw_val_class_loc:
6756       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6757            loc1 && loc2;
6758            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6759         if (!same_loc_p (loc1, loc2, mark))
6760           return 0;
6761       return !loc1 && !loc2;
6762
6763     case dw_val_class_die_ref:
6764       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6765
6766     case dw_val_class_fde_ref:
6767     case dw_val_class_vms_delta:
6768     case dw_val_class_lbl_id:
6769     case dw_val_class_lineptr:
6770     case dw_val_class_macptr:
6771     case dw_val_class_high_pc:
6772       return 1;
6773
6774     case dw_val_class_file:
6775       return v1->v.val_file == v2->v.val_file;
6776
6777     case dw_val_class_data8:
6778       return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6779
6780     default:
6781       return 1;
6782     }
6783 }
6784
6785 /* Do the attributes look the same?  */
6786
6787 static int
6788 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
6789 {
6790   if (at1->dw_attr != at2->dw_attr)
6791     return 0;
6792
6793   /* We don't care that this was compiled with a different compiler
6794      snapshot; if the output is the same, that's what matters. */
6795   if (at1->dw_attr == DW_AT_producer)
6796     return 1;
6797
6798   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6799 }
6800
6801 /* Do the dies look the same?  */
6802
6803 static int
6804 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6805 {
6806   dw_die_ref c1, c2;
6807   dw_attr_node *a1;
6808   unsigned ix;
6809
6810   /* To avoid infinite recursion.  */
6811   if (die1->die_mark)
6812     return die1->die_mark == die2->die_mark;
6813   die1->die_mark = die2->die_mark = ++(*mark);
6814
6815   if (die1->die_tag != die2->die_tag)
6816     return 0;
6817
6818   if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6819     return 0;
6820
6821   FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6822     if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6823       return 0;
6824
6825   c1 = die1->die_child;
6826   c2 = die2->die_child;
6827   if (! c1)
6828     {
6829       if (c2)
6830         return 0;
6831     }
6832   else
6833     for (;;)
6834       {
6835         if (!same_die_p (c1, c2, mark))
6836           return 0;
6837         c1 = c1->die_sib;
6838         c2 = c2->die_sib;
6839         if (c1 == die1->die_child)
6840           {
6841             if (c2 == die2->die_child)
6842               break;
6843             else
6844               return 0;
6845           }
6846     }
6847
6848   return 1;
6849 }
6850
6851 /* Do the dies look the same?  Wrapper around same_die_p.  */
6852
6853 static int
6854 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6855 {
6856   int mark = 0;
6857   int ret = same_die_p (die1, die2, &mark);
6858
6859   unmark_all_dies (die1);
6860   unmark_all_dies (die2);
6861
6862   return ret;
6863 }
6864
6865 /* The prefix to attach to symbols on DIEs in the current comdat debug
6866    info section.  */
6867 static const char *comdat_symbol_id;
6868
6869 /* The index of the current symbol within the current comdat CU.  */
6870 static unsigned int comdat_symbol_number;
6871
6872 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6873    children, and set comdat_symbol_id accordingly.  */
6874
6875 static void
6876 compute_section_prefix (dw_die_ref unit_die)
6877 {
6878   const char *die_name = get_AT_string (unit_die, DW_AT_name);
6879   const char *base = die_name ? lbasename (die_name) : "anonymous";
6880   char *name = XALLOCAVEC (char, strlen (base) + 64);
6881   char *p;
6882   int i, mark;
6883   unsigned char checksum[16];
6884   struct md5_ctx ctx;
6885
6886   /* Compute the checksum of the DIE, then append part of it as hex digits to
6887      the name filename of the unit.  */
6888
6889   md5_init_ctx (&ctx);
6890   mark = 0;
6891   die_checksum (unit_die, &ctx, &mark);
6892   unmark_all_dies (unit_die);
6893   md5_finish_ctx (&ctx, checksum);
6894
6895   sprintf (name, "%s.", base);
6896   clean_symbol_name (name);
6897
6898   p = name + strlen (name);
6899   for (i = 0; i < 4; i++)
6900     {
6901       sprintf (p, "%.2x", checksum[i]);
6902       p += 2;
6903     }
6904
6905   comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6906   comdat_symbol_number = 0;
6907 }
6908
6909 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
6910
6911 static int
6912 is_type_die (dw_die_ref die)
6913 {
6914   switch (die->die_tag)
6915     {
6916     case DW_TAG_array_type:
6917     case DW_TAG_class_type:
6918     case DW_TAG_interface_type:
6919     case DW_TAG_enumeration_type:
6920     case DW_TAG_pointer_type:
6921     case DW_TAG_reference_type:
6922     case DW_TAG_rvalue_reference_type:
6923     case DW_TAG_string_type:
6924     case DW_TAG_structure_type:
6925     case DW_TAG_subroutine_type:
6926     case DW_TAG_union_type:
6927     case DW_TAG_ptr_to_member_type:
6928     case DW_TAG_set_type:
6929     case DW_TAG_subrange_type:
6930     case DW_TAG_base_type:
6931     case DW_TAG_const_type:
6932     case DW_TAG_file_type:
6933     case DW_TAG_packed_type:
6934     case DW_TAG_volatile_type:
6935     case DW_TAG_typedef:
6936       return 1;
6937     default:
6938       return 0;
6939     }
6940 }
6941
6942 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6943    Basically, we want to choose the bits that are likely to be shared between
6944    compilations (types) and leave out the bits that are specific to individual
6945    compilations (functions).  */
6946
6947 static int
6948 is_comdat_die (dw_die_ref c)
6949 {
6950   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6951      we do for stabs.  The advantage is a greater likelihood of sharing between
6952      objects that don't include headers in the same order (and therefore would
6953      put the base types in a different comdat).  jason 8/28/00 */
6954
6955   if (c->die_tag == DW_TAG_base_type)
6956     return 0;
6957
6958   if (c->die_tag == DW_TAG_pointer_type
6959       || c->die_tag == DW_TAG_reference_type
6960       || c->die_tag == DW_TAG_rvalue_reference_type
6961       || c->die_tag == DW_TAG_const_type
6962       || c->die_tag == DW_TAG_volatile_type)
6963     {
6964       dw_die_ref t = get_AT_ref (c, DW_AT_type);
6965
6966       return t ? is_comdat_die (t) : 0;
6967     }
6968
6969   return is_type_die (c);
6970 }
6971
6972 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6973    compilation unit.  */
6974
6975 static int
6976 is_symbol_die (dw_die_ref c)
6977 {
6978   return (is_type_die (c)
6979           || is_declaration_die (c)
6980           || c->die_tag == DW_TAG_namespace
6981           || c->die_tag == DW_TAG_module);
6982 }
6983
6984 /* Returns true iff C is a compile-unit DIE.  */
6985
6986 static inline bool
6987 is_cu_die (dw_die_ref c)
6988 {
6989   return c && c->die_tag == DW_TAG_compile_unit;
6990 }
6991
6992 /* Returns true iff C is a unit DIE of some sort.  */
6993
6994 static inline bool
6995 is_unit_die (dw_die_ref c)
6996 {
6997   return c && (c->die_tag == DW_TAG_compile_unit
6998                || c->die_tag == DW_TAG_partial_unit
6999                || c->die_tag == DW_TAG_type_unit);
7000 }
7001
7002 /* Returns true iff C is a namespace DIE.  */
7003
7004 static inline bool
7005 is_namespace_die (dw_die_ref c)
7006 {
7007   return c && c->die_tag == DW_TAG_namespace;
7008 }
7009
7010 /* Returns true iff C is a class or structure DIE.  */
7011
7012 static inline bool
7013 is_class_die (dw_die_ref c)
7014 {
7015   return c && (c->die_tag == DW_TAG_class_type
7016                || c->die_tag == DW_TAG_structure_type);
7017 }
7018
7019 /* Return non-zero if this DIE is a template parameter.  */
7020
7021 static inline bool
7022 is_template_parameter (dw_die_ref die)
7023 {
7024   switch (die->die_tag)
7025     {
7026     case DW_TAG_template_type_param:
7027     case DW_TAG_template_value_param:
7028     case DW_TAG_GNU_template_template_param:
7029     case DW_TAG_GNU_template_parameter_pack:
7030       return true;
7031     default:
7032       return false;
7033     }
7034 }
7035
7036 /* Return non-zero if this DIE represents a template instantiation.  */
7037
7038 static inline bool
7039 is_template_instantiation (dw_die_ref die)
7040 {
7041   dw_die_ref c;
7042
7043   if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
7044     return false;
7045   FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
7046   return false;
7047 }
7048
7049 static char *
7050 gen_internal_sym (const char *prefix)
7051 {
7052   char buf[256];
7053
7054   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7055   return xstrdup (buf);
7056 }
7057
7058 /* Assign symbols to all worthy DIEs under DIE.  */
7059
7060 static void
7061 assign_symbol_names (dw_die_ref die)
7062 {
7063   dw_die_ref c;
7064
7065   if (is_symbol_die (die) && !die->comdat_type_p)
7066     {
7067       if (comdat_symbol_id)
7068         {
7069           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
7070
7071           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
7072                    comdat_symbol_id, comdat_symbol_number++);
7073           die->die_id.die_symbol = xstrdup (p);
7074         }
7075       else
7076         die->die_id.die_symbol = gen_internal_sym ("LDIE");
7077     }
7078
7079   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
7080 }
7081
7082 struct cu_hash_table_entry
7083 {
7084   dw_die_ref cu;
7085   unsigned min_comdat_num, max_comdat_num;
7086   struct cu_hash_table_entry *next;
7087 };
7088
7089 /* Helpers to manipulate hash table of CUs.  */
7090
7091 struct cu_hash_table_entry_hasher : pointer_hash <cu_hash_table_entry>
7092 {
7093   typedef die_struct *compare_type;
7094   static inline hashval_t hash (const cu_hash_table_entry *);
7095   static inline bool equal (const cu_hash_table_entry *, const die_struct *);
7096   static inline void remove (cu_hash_table_entry *);
7097 };
7098
7099 inline hashval_t
7100 cu_hash_table_entry_hasher::hash (const cu_hash_table_entry *entry)
7101 {
7102   return htab_hash_string (entry->cu->die_id.die_symbol);
7103 }
7104
7105 inline bool
7106 cu_hash_table_entry_hasher::equal (const cu_hash_table_entry *entry1,
7107                                    const die_struct *entry2)
7108 {
7109   return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
7110 }
7111
7112 inline void
7113 cu_hash_table_entry_hasher::remove (cu_hash_table_entry *entry)
7114 {
7115   struct cu_hash_table_entry *next;
7116
7117   while (entry)
7118     {
7119       next = entry->next;
7120       free (entry);
7121       entry = next;
7122     }
7123 }
7124
7125 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
7126
7127 /* Check whether we have already seen this CU and set up SYM_NUM
7128    accordingly.  */
7129 static int
7130 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
7131 {
7132   struct cu_hash_table_entry dummy;
7133   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7134
7135   dummy.max_comdat_num = 0;
7136
7137   slot = htable->find_slot_with_hash (cu,
7138                                       htab_hash_string (cu->die_id.die_symbol),
7139                                       INSERT);
7140   entry = *slot;
7141
7142   for (; entry; last = entry, entry = entry->next)
7143     {
7144       if (same_die_p_wrap (cu, entry->cu))
7145         break;
7146     }
7147
7148   if (entry)
7149     {
7150       *sym_num = entry->min_comdat_num;
7151       return 1;
7152     }
7153
7154   entry = XCNEW (struct cu_hash_table_entry);
7155   entry->cu = cu;
7156   entry->min_comdat_num = *sym_num = last->max_comdat_num;
7157   entry->next = *slot;
7158   *slot = entry;
7159
7160   return 0;
7161 }
7162
7163 /* Record SYM_NUM to record of CU in HTABLE.  */
7164 static void
7165 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
7166                              unsigned int sym_num)
7167 {
7168   struct cu_hash_table_entry **slot, *entry;
7169
7170   slot = htable->find_slot_with_hash (cu,
7171                                       htab_hash_string (cu->die_id.die_symbol),
7172                                       NO_INSERT);
7173   entry = *slot;
7174
7175   entry->max_comdat_num = sym_num;
7176 }
7177
7178 /* Traverse the DIE (which is always comp_unit_die), and set up
7179    additional compilation units for each of the include files we see
7180    bracketed by BINCL/EINCL.  */
7181
7182 static void
7183 break_out_includes (dw_die_ref die)
7184 {
7185   dw_die_ref c;
7186   dw_die_ref unit = NULL;
7187   limbo_die_node *node, **pnode;
7188
7189   c = die->die_child;
7190   if (c) do {
7191     dw_die_ref prev = c;
7192     c = c->die_sib;
7193     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7194            || (unit && is_comdat_die (c)))
7195       {
7196         dw_die_ref next = c->die_sib;
7197
7198         /* This DIE is for a secondary CU; remove it from the main one.  */
7199         remove_child_with_prev (c, prev);
7200
7201         if (c->die_tag == DW_TAG_GNU_BINCL)
7202           unit = push_new_compile_unit (unit, c);
7203         else if (c->die_tag == DW_TAG_GNU_EINCL)
7204           unit = pop_compile_unit (unit);
7205         else
7206           add_child_die (unit, c);
7207         c = next;
7208         if (c == die->die_child)
7209           break;
7210       }
7211   } while (c != die->die_child);
7212
7213 #if 0
7214   /* We can only use this in debugging, since the frontend doesn't check
7215      to make sure that we leave every include file we enter.  */
7216   gcc_assert (!unit);
7217 #endif
7218
7219   assign_symbol_names (die);
7220   cu_hash_type cu_hash_table (10);
7221   for (node = limbo_die_list, pnode = &limbo_die_list;
7222        node;
7223        node = node->next)
7224     {
7225       int is_dupl;
7226
7227       compute_section_prefix (node->die);
7228       is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
7229                         &comdat_symbol_number);
7230       assign_symbol_names (node->die);
7231       if (is_dupl)
7232         *pnode = node->next;
7233       else
7234         {
7235           pnode = &node->next;
7236           record_comdat_symbol_number (node->die, &cu_hash_table,
7237                 comdat_symbol_number);
7238         }
7239     }
7240 }
7241
7242 /* Return non-zero if this DIE is a declaration.  */
7243
7244 static int
7245 is_declaration_die (dw_die_ref die)
7246 {
7247   dw_attr_node *a;
7248   unsigned ix;
7249
7250   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7251     if (a->dw_attr == DW_AT_declaration)
7252       return 1;
7253
7254   return 0;
7255 }
7256
7257 /* Return non-zero if this DIE is nested inside a subprogram.  */
7258
7259 static int
7260 is_nested_in_subprogram (dw_die_ref die)
7261 {
7262   dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7263
7264   if (decl == NULL)
7265     decl = die;
7266   return local_scope_p (decl);
7267 }
7268
7269 /* Return non-zero if this DIE contains a defining declaration of a
7270    subprogram.  */
7271
7272 static int
7273 contains_subprogram_definition (dw_die_ref die)
7274 {
7275   dw_die_ref c;
7276
7277   if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7278     return 1;
7279   FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7280   return 0;
7281 }
7282
7283 /* Return non-zero if this is a type DIE that should be moved to a
7284    COMDAT .debug_types section.  */
7285
7286 static int
7287 should_move_die_to_comdat (dw_die_ref die)
7288 {
7289   switch (die->die_tag)
7290     {
7291     case DW_TAG_class_type:
7292     case DW_TAG_structure_type:
7293     case DW_TAG_enumeration_type:
7294     case DW_TAG_union_type:
7295       /* Don't move declarations, inlined instances, types nested in a
7296          subprogram, or types that contain subprogram definitions.  */
7297       if (is_declaration_die (die)
7298           || get_AT (die, DW_AT_abstract_origin)
7299           || is_nested_in_subprogram (die)
7300           || contains_subprogram_definition (die))
7301         return 0;
7302       return 1;
7303     case DW_TAG_array_type:
7304     case DW_TAG_interface_type:
7305     case DW_TAG_pointer_type:
7306     case DW_TAG_reference_type:
7307     case DW_TAG_rvalue_reference_type:
7308     case DW_TAG_string_type:
7309     case DW_TAG_subroutine_type:
7310     case DW_TAG_ptr_to_member_type:
7311     case DW_TAG_set_type:
7312     case DW_TAG_subrange_type:
7313     case DW_TAG_base_type:
7314     case DW_TAG_const_type:
7315     case DW_TAG_file_type:
7316     case DW_TAG_packed_type:
7317     case DW_TAG_volatile_type:
7318     case DW_TAG_typedef:
7319     default:
7320       return 0;
7321     }
7322 }
7323
7324 /* Make a clone of DIE.  */
7325
7326 static dw_die_ref
7327 clone_die (dw_die_ref die)
7328 {
7329   dw_die_ref clone;
7330   dw_attr_node *a;
7331   unsigned ix;
7332
7333   clone = ggc_cleared_alloc<die_node> ();
7334   clone->die_tag = die->die_tag;
7335
7336   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7337     add_dwarf_attr (clone, a);
7338
7339   return clone;
7340 }
7341
7342 /* Make a clone of the tree rooted at DIE.  */
7343
7344 static dw_die_ref
7345 clone_tree (dw_die_ref die)
7346 {
7347   dw_die_ref c;
7348   dw_die_ref clone = clone_die (die);
7349
7350   FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7351
7352   return clone;
7353 }
7354
7355 /* Make a clone of DIE as a declaration.  */
7356
7357 static dw_die_ref
7358 clone_as_declaration (dw_die_ref die)
7359 {
7360   dw_die_ref clone;
7361   dw_die_ref decl;
7362   dw_attr_node *a;
7363   unsigned ix;
7364
7365   /* If the DIE is already a declaration, just clone it.  */
7366   if (is_declaration_die (die))
7367     return clone_die (die);
7368
7369   /* If the DIE is a specification, just clone its declaration DIE.  */
7370   decl = get_AT_ref (die, DW_AT_specification);
7371   if (decl != NULL)
7372     {
7373       clone = clone_die (decl);
7374       if (die->comdat_type_p)
7375         add_AT_die_ref (clone, DW_AT_signature, die);
7376       return clone;
7377     }
7378
7379   clone = ggc_cleared_alloc<die_node> ();
7380   clone->die_tag = die->die_tag;
7381
7382   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7383     {
7384       /* We don't want to copy over all attributes.
7385          For example we don't want DW_AT_byte_size because otherwise we will no
7386          longer have a declaration and GDB will treat it as a definition.  */
7387
7388       switch (a->dw_attr)
7389         {
7390         case DW_AT_abstract_origin:
7391         case DW_AT_artificial:
7392         case DW_AT_containing_type:
7393         case DW_AT_external:
7394         case DW_AT_name:
7395         case DW_AT_type:
7396         case DW_AT_virtuality:
7397         case DW_AT_linkage_name:
7398         case DW_AT_MIPS_linkage_name:
7399           add_dwarf_attr (clone, a);
7400           break;
7401         case DW_AT_byte_size:
7402         default:
7403           break;
7404         }
7405     }
7406
7407   if (die->comdat_type_p)
7408     add_AT_die_ref (clone, DW_AT_signature, die);
7409
7410   add_AT_flag (clone, DW_AT_declaration, 1);
7411   return clone;
7412 }
7413
7414
7415 /* Structure to map a DIE in one CU to its copy in a comdat type unit.  */
7416
7417 struct decl_table_entry
7418 {
7419   dw_die_ref orig;
7420   dw_die_ref copy;
7421 };
7422
7423 /* Helpers to manipulate hash table of copied declarations.  */
7424
7425 /* Hashtable helpers.  */
7426
7427 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
7428 {
7429   typedef die_struct *compare_type;
7430   static inline hashval_t hash (const decl_table_entry *);
7431   static inline bool equal (const decl_table_entry *, const die_struct *);
7432 };
7433
7434 inline hashval_t
7435 decl_table_entry_hasher::hash (const decl_table_entry *entry)
7436 {
7437   return htab_hash_pointer (entry->orig);
7438 }
7439
7440 inline bool
7441 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
7442                                 const die_struct *entry2)
7443 {
7444   return entry1->orig == entry2;
7445 }
7446
7447 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7448
7449 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7450    or type unit entry, to a new tree.  Adds the new tree to UNIT and returns
7451    a pointer to the copy of DIE.  If DECL_TABLE is provided, it is used
7452    to check if the ancestor has already been copied into UNIT.  */
7453
7454 static dw_die_ref
7455 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7456                     decl_hash_type *decl_table)
7457 {
7458   dw_die_ref parent = die->die_parent;
7459   dw_die_ref new_parent = unit;
7460   dw_die_ref copy;
7461   decl_table_entry **slot = NULL;
7462   struct decl_table_entry *entry = NULL;
7463
7464   if (decl_table)
7465     {
7466       /* Check if the entry has already been copied to UNIT.  */
7467       slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7468                                               INSERT);
7469       if (*slot != HTAB_EMPTY_ENTRY)
7470         {
7471           entry = *slot;
7472           return entry->copy;
7473         }
7474
7475       /* Record in DECL_TABLE that DIE has been copied to UNIT.  */
7476       entry = XCNEW (struct decl_table_entry);
7477       entry->orig = die;
7478       entry->copy = NULL;
7479       *slot = entry;
7480     }
7481
7482   if (parent != NULL)
7483     {
7484       dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7485       if (spec != NULL)
7486         parent = spec;
7487       if (!is_unit_die (parent))
7488         new_parent = copy_ancestor_tree (unit, parent, decl_table);
7489     }
7490
7491   copy = clone_as_declaration (die);
7492   add_child_die (new_parent, copy);
7493
7494   if (decl_table)
7495     {
7496       /* Record the pointer to the copy.  */
7497       entry->copy = copy;
7498     }
7499
7500   return copy;
7501 }
7502 /* Copy the declaration context to the new type unit DIE.  This includes
7503    any surrounding namespace or type declarations.  If the DIE has an
7504    AT_specification attribute, it also includes attributes and children
7505    attached to the specification, and returns a pointer to the original
7506    parent of the declaration DIE.  Returns NULL otherwise.  */
7507
7508 static dw_die_ref
7509 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7510 {
7511   dw_die_ref decl;
7512   dw_die_ref new_decl;
7513   dw_die_ref orig_parent = NULL;
7514
7515   decl = get_AT_ref (die, DW_AT_specification);
7516   if (decl == NULL)
7517     decl = die;
7518   else
7519     {
7520       unsigned ix;
7521       dw_die_ref c;
7522       dw_attr_node *a;
7523
7524       /* The original DIE will be changed to a declaration, and must
7525          be moved to be a child of the original declaration DIE.  */
7526       orig_parent = decl->die_parent;
7527
7528       /* Copy the type node pointer from the new DIE to the original
7529          declaration DIE so we can forward references later.  */
7530       decl->comdat_type_p = true;
7531       decl->die_id.die_type_node = die->die_id.die_type_node;
7532
7533       remove_AT (die, DW_AT_specification);
7534
7535       FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7536         {
7537           if (a->dw_attr != DW_AT_name
7538               && a->dw_attr != DW_AT_declaration
7539               && a->dw_attr != DW_AT_external)
7540             add_dwarf_attr (die, a);
7541         }
7542
7543       FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7544     }
7545
7546   if (decl->die_parent != NULL
7547       && !is_unit_die (decl->die_parent))
7548     {
7549       new_decl = copy_ancestor_tree (unit, decl, NULL);
7550       if (new_decl != NULL)
7551         {
7552           remove_AT (new_decl, DW_AT_signature);
7553           add_AT_specification (die, new_decl);
7554         }
7555     }
7556
7557   return orig_parent;
7558 }
7559
7560 /* Generate the skeleton ancestor tree for the given NODE, then clone
7561    the DIE and add the clone into the tree.  */
7562
7563 static void
7564 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7565 {
7566   if (node->new_die != NULL)
7567     return;
7568
7569   node->new_die = clone_as_declaration (node->old_die);
7570
7571   if (node->parent != NULL)
7572     {
7573       generate_skeleton_ancestor_tree (node->parent);
7574       add_child_die (node->parent->new_die, node->new_die);
7575     }
7576 }
7577
7578 /* Generate a skeleton tree of DIEs containing any declarations that are
7579    found in the original tree.  We traverse the tree looking for declaration
7580    DIEs, and construct the skeleton from the bottom up whenever we find one.  */
7581
7582 static void
7583 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7584 {
7585   skeleton_chain_node node;
7586   dw_die_ref c;
7587   dw_die_ref first;
7588   dw_die_ref prev = NULL;
7589   dw_die_ref next = NULL;
7590
7591   node.parent = parent;
7592
7593   first = c = parent->old_die->die_child;
7594   if (c)
7595     next = c->die_sib;
7596   if (c) do {
7597     if (prev == NULL || prev->die_sib == c)
7598       prev = c;
7599     c = next;
7600     next = (c == first ? NULL : c->die_sib);
7601     node.old_die = c;
7602     node.new_die = NULL;
7603     if (is_declaration_die (c))
7604       {
7605         if (is_template_instantiation (c))
7606           {
7607             /* Instantiated templates do not need to be cloned into the
7608                type unit.  Just move the DIE and its children back to
7609                the skeleton tree (in the main CU).  */
7610             remove_child_with_prev (c, prev);
7611             add_child_die (parent->new_die, c);
7612             c = prev;
7613           }
7614         else
7615           {
7616             /* Clone the existing DIE, move the original to the skeleton
7617                tree (which is in the main CU), and put the clone, with
7618                all the original's children, where the original came from
7619                (which is about to be moved to the type unit).  */
7620             dw_die_ref clone = clone_die (c);
7621             move_all_children (c, clone);
7622
7623             /* If the original has a DW_AT_object_pointer attribute,
7624                it would now point to a child DIE just moved to the
7625                cloned tree, so we need to remove that attribute from
7626                the original.  */
7627             remove_AT (c, DW_AT_object_pointer);
7628
7629             replace_child (c, clone, prev);
7630             generate_skeleton_ancestor_tree (parent);
7631             add_child_die (parent->new_die, c);
7632             node.new_die = c;
7633             c = clone;
7634           }
7635       }
7636     generate_skeleton_bottom_up (&node);
7637   } while (next != NULL);
7638 }
7639
7640 /* Wrapper function for generate_skeleton_bottom_up.  */
7641
7642 static dw_die_ref
7643 generate_skeleton (dw_die_ref die)
7644 {
7645   skeleton_chain_node node;
7646
7647   node.old_die = die;
7648   node.new_die = NULL;
7649   node.parent = NULL;
7650
7651   /* If this type definition is nested inside another type,
7652      and is not an instantiation of a template, always leave
7653      at least a declaration in its place.  */
7654   if (die->die_parent != NULL
7655       && is_type_die (die->die_parent)
7656       && !is_template_instantiation (die))
7657     node.new_die = clone_as_declaration (die);
7658
7659   generate_skeleton_bottom_up (&node);
7660   return node.new_die;
7661 }
7662
7663 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7664    declaration.  The original DIE is moved to a new compile unit so that
7665    existing references to it follow it to the new location.  If any of the
7666    original DIE's descendants is a declaration, we need to replace the
7667    original DIE with a skeleton tree and move the declarations back into the
7668    skeleton tree.  */
7669
7670 static dw_die_ref
7671 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7672                                        dw_die_ref prev)
7673 {
7674   dw_die_ref skeleton, orig_parent;
7675
7676   /* Copy the declaration context to the type unit DIE.  If the returned
7677      ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7678      that DIE.  */
7679   orig_parent = copy_declaration_context (unit, child);
7680
7681   skeleton = generate_skeleton (child);
7682   if (skeleton == NULL)
7683     remove_child_with_prev (child, prev);
7684   else
7685     {
7686       skeleton->comdat_type_p = true;
7687       skeleton->die_id.die_type_node = child->die_id.die_type_node;
7688
7689       /* If the original DIE was a specification, we need to put
7690          the skeleton under the parent DIE of the declaration.
7691          This leaves the original declaration in the tree, but
7692          it will be pruned later since there are no longer any
7693          references to it.  */
7694       if (orig_parent != NULL)
7695         {
7696           remove_child_with_prev (child, prev);
7697           add_child_die (orig_parent, skeleton);
7698         }
7699       else
7700         replace_child (child, skeleton, prev);
7701     }
7702
7703   return skeleton;
7704 }
7705
7706 static void
7707 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
7708                                comdat_type_node *type_node,
7709                                hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs);
7710
7711 /* Helper for copy_dwarf_procs_ref_in_dies.  Make a copy of the DIE DWARF
7712    procedure, put it under TYPE_NODE and return the copy.  Continue looking for
7713    DWARF procedure references in the DW_AT_location attribute.  */
7714
7715 static dw_die_ref
7716 copy_dwarf_procedure (dw_die_ref die,
7717                       comdat_type_node *type_node,
7718                       hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
7719 {
7720   /* We do this for COMDAT section, which is DWARFv4 specific, so
7721      DWARF procedure are always DW_TAG_dwarf_procedure DIEs (unlike
7722      DW_TAG_variable in DWARFv3).  */
7723   gcc_assert (die->die_tag == DW_TAG_dwarf_procedure);
7724
7725   /* DWARF procedures are not supposed to have children...  */
7726   gcc_assert (die->die_child == NULL);
7727
7728   /* ... and they are supposed to have only one attribute: DW_AT_location.  */
7729   gcc_assert (vec_safe_length (die->die_attr) == 1
7730               && ((*die->die_attr)[0].dw_attr == DW_AT_location));
7731
7732   /* Do not copy more than once DWARF procedures.  */
7733   bool existed;
7734   dw_die_ref &die_copy = copied_dwarf_procs.get_or_insert (die, &existed);
7735   if (existed)
7736     return die_copy;
7737
7738   die_copy = clone_die (die);
7739   add_child_die (type_node->root_die, die_copy);
7740   copy_dwarf_procs_ref_in_attrs (die_copy, type_node, copied_dwarf_procs);
7741   return die_copy;
7742 }
7743
7744 /* Helper for copy_dwarf_procs_ref_in_dies.  Look for references to DWARF
7745    procedures in DIE's attributes.  */
7746
7747 static void
7748 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
7749                                comdat_type_node *type_node,
7750                                hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
7751 {
7752   dw_attr_node *a;
7753   unsigned i;
7754
7755   FOR_EACH_VEC_SAFE_ELT (die->die_attr, i, a)
7756     {
7757       dw_loc_descr_ref loc;
7758
7759       if (a->dw_attr_val.val_class != dw_val_class_loc)
7760         continue;
7761
7762       for (loc = a->dw_attr_val.v.val_loc; loc != NULL; loc = loc->dw_loc_next)
7763         {
7764           switch (loc->dw_loc_opc)
7765             {
7766             case DW_OP_call2:
7767             case DW_OP_call4:
7768             case DW_OP_call_ref:
7769               gcc_assert (loc->dw_loc_oprnd1.val_class
7770                           == dw_val_class_die_ref);
7771               loc->dw_loc_oprnd1.v.val_die_ref.die
7772                 = copy_dwarf_procedure (loc->dw_loc_oprnd1.v.val_die_ref.die,
7773                                         type_node,
7774                                         copied_dwarf_procs);
7775
7776             default:
7777               break;
7778             }
7779         }
7780     }
7781 }
7782
7783 /* Copy DWARF procedures that are referenced by the DIE tree to TREE_NODE and
7784    rewrite references to point to the copies.
7785
7786    References are looked for in DIE's attributes and recursively in all its
7787    children attributes that are location descriptions. COPIED_DWARF_PROCS is a
7788    mapping from old DWARF procedures to their copy. It is used not to copy
7789    twice the same DWARF procedure under TYPE_NODE.  */
7790
7791 static void
7792 copy_dwarf_procs_ref_in_dies (dw_die_ref die,
7793                               comdat_type_node *type_node,
7794                               hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
7795 {
7796   dw_die_ref c;
7797
7798   copy_dwarf_procs_ref_in_attrs (die, type_node, copied_dwarf_procs);
7799   FOR_EACH_CHILD (die, c, copy_dwarf_procs_ref_in_dies (c,
7800                                                         type_node,
7801                                                         copied_dwarf_procs));
7802 }
7803
7804 /* Traverse the DIE and set up additional .debug_types sections for each
7805    type worthy of being placed in a COMDAT section.  */
7806
7807 static void
7808 break_out_comdat_types (dw_die_ref die)
7809 {
7810   dw_die_ref c;
7811   dw_die_ref first;
7812   dw_die_ref prev = NULL;
7813   dw_die_ref next = NULL;
7814   dw_die_ref unit = NULL;
7815
7816   first = c = die->die_child;
7817   if (c)
7818     next = c->die_sib;
7819   if (c) do {
7820     if (prev == NULL || prev->die_sib == c)
7821       prev = c;
7822     c = next;
7823     next = (c == first ? NULL : c->die_sib);
7824     if (should_move_die_to_comdat (c))
7825       {
7826         dw_die_ref replacement;
7827         comdat_type_node *type_node;
7828
7829         /* Break out nested types into their own type units.  */
7830         break_out_comdat_types (c);
7831
7832         /* Create a new type unit DIE as the root for the new tree, and
7833            add it to the list of comdat types.  */
7834         unit = new_die (DW_TAG_type_unit, NULL, NULL);
7835         add_AT_unsigned (unit, DW_AT_language,
7836                          get_AT_unsigned (comp_unit_die (), DW_AT_language));
7837         type_node = ggc_cleared_alloc<comdat_type_node> ();
7838         type_node->root_die = unit;
7839         type_node->next = comdat_type_list;
7840         comdat_type_list = type_node;
7841
7842         /* Generate the type signature.  */
7843         generate_type_signature (c, type_node);
7844
7845         /* Copy the declaration context, attributes, and children of the
7846            declaration into the new type unit DIE, then remove this DIE
7847            from the main CU (or replace it with a skeleton if necessary).  */
7848         replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7849         type_node->skeleton_die = replacement;
7850
7851         /* Add the DIE to the new compunit.  */
7852         add_child_die (unit, c);
7853
7854         /* Types can reference DWARF procedures for type size or data location
7855            expressions.  Calls in DWARF expressions cannot target procedures
7856            that are not in the same section.  So we must copy DWARF procedures
7857            along with this type and then rewrite references to them.  */
7858         hash_map<dw_die_ref, dw_die_ref> copied_dwarf_procs;
7859         copy_dwarf_procs_ref_in_dies (c, type_node, copied_dwarf_procs);
7860
7861         if (replacement != NULL)
7862           c = replacement;
7863       }
7864     else if (c->die_tag == DW_TAG_namespace
7865              || c->die_tag == DW_TAG_class_type
7866              || c->die_tag == DW_TAG_structure_type
7867              || c->die_tag == DW_TAG_union_type)
7868       {
7869         /* Look for nested types that can be broken out.  */
7870         break_out_comdat_types (c);
7871       }
7872   } while (next != NULL);
7873 }
7874
7875 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
7876    Enter all the cloned children into the hash table decl_table.  */
7877
7878 static dw_die_ref
7879 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
7880 {
7881   dw_die_ref c;
7882   dw_die_ref clone;
7883   struct decl_table_entry *entry;
7884   decl_table_entry **slot;
7885
7886   if (die->die_tag == DW_TAG_subprogram)
7887     clone = clone_as_declaration (die);
7888   else
7889     clone = clone_die (die);
7890
7891   slot = decl_table->find_slot_with_hash (die,
7892                                           htab_hash_pointer (die), INSERT);
7893
7894   /* Assert that DIE isn't in the hash table yet.  If it would be there
7895      before, the ancestors would be necessarily there as well, therefore
7896      clone_tree_partial wouldn't be called.  */
7897   gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7898
7899   entry = XCNEW (struct decl_table_entry);
7900   entry->orig = die;
7901   entry->copy = clone;
7902   *slot = entry;
7903
7904   if (die->die_tag != DW_TAG_subprogram)
7905     FOR_EACH_CHILD (die, c,
7906                     add_child_die (clone, clone_tree_partial (c, decl_table)));
7907
7908   return clone;
7909 }
7910
7911 /* Walk the DIE and its children, looking for references to incomplete
7912    or trivial types that are unmarked (i.e., that are not in the current
7913    type_unit).  */
7914
7915 static void
7916 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
7917 {
7918   dw_die_ref c;
7919   dw_attr_node *a;
7920   unsigned ix;
7921
7922   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7923     {
7924       if (AT_class (a) == dw_val_class_die_ref)
7925         {
7926           dw_die_ref targ = AT_ref (a);
7927           decl_table_entry **slot;
7928           struct decl_table_entry *entry;
7929
7930           if (targ->die_mark != 0 || targ->comdat_type_p)
7931             continue;
7932
7933           slot = decl_table->find_slot_with_hash (targ,
7934                                                   htab_hash_pointer (targ),
7935                                                   INSERT);
7936
7937           if (*slot != HTAB_EMPTY_ENTRY)
7938             {
7939               /* TARG has already been copied, so we just need to
7940                  modify the reference to point to the copy.  */
7941               entry = *slot;
7942               a->dw_attr_val.v.val_die_ref.die = entry->copy;
7943             }
7944           else
7945             {
7946               dw_die_ref parent = unit;
7947               dw_die_ref copy = clone_die (targ);
7948
7949               /* Record in DECL_TABLE that TARG has been copied.
7950                  Need to do this now, before the recursive call,
7951                  because DECL_TABLE may be expanded and SLOT
7952                  would no longer be a valid pointer.  */
7953               entry = XCNEW (struct decl_table_entry);
7954               entry->orig = targ;
7955               entry->copy = copy;
7956               *slot = entry;
7957
7958               /* If TARG is not a declaration DIE, we need to copy its
7959                  children.  */
7960               if (!is_declaration_die (targ))
7961                 {
7962                   FOR_EACH_CHILD (
7963                       targ, c,
7964                       add_child_die (copy,
7965                                      clone_tree_partial (c, decl_table)));
7966                 }
7967
7968               /* Make sure the cloned tree is marked as part of the
7969                  type unit.  */
7970               mark_dies (copy);
7971
7972               /* If TARG has surrounding context, copy its ancestor tree
7973                  into the new type unit.  */
7974               if (targ->die_parent != NULL
7975                   && !is_unit_die (targ->die_parent))
7976                 parent = copy_ancestor_tree (unit, targ->die_parent,
7977                                              decl_table);
7978
7979               add_child_die (parent, copy);
7980               a->dw_attr_val.v.val_die_ref.die = copy;
7981
7982               /* Make sure the newly-copied DIE is walked.  If it was
7983                  installed in a previously-added context, it won't
7984                  get visited otherwise.  */
7985               if (parent != unit)
7986                 {
7987                   /* Find the highest point of the newly-added tree,
7988                      mark each node along the way, and walk from there.  */
7989                   parent->die_mark = 1;
7990                   while (parent->die_parent
7991                          && parent->die_parent->die_mark == 0)
7992                     {
7993                       parent = parent->die_parent;
7994                       parent->die_mark = 1;
7995                     }
7996                   copy_decls_walk (unit, parent, decl_table);
7997                 }
7998             }
7999         }
8000     }
8001
8002   FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8003 }
8004
8005 /* Copy declarations for "unworthy" types into the new comdat section.
8006    Incomplete types, modified types, and certain other types aren't broken
8007    out into comdat sections of their own, so they don't have a signature,
8008    and we need to copy the declaration into the same section so that we
8009    don't have an external reference.  */
8010
8011 static void
8012 copy_decls_for_unworthy_types (dw_die_ref unit)
8013 {
8014   mark_dies (unit);
8015   decl_hash_type decl_table (10);
8016   copy_decls_walk (unit, unit, &decl_table);
8017   unmark_dies (unit);
8018 }
8019
8020 /* Traverse the DIE and add a sibling attribute if it may have the
8021    effect of speeding up access to siblings.  To save some space,
8022    avoid generating sibling attributes for DIE's without children.  */
8023
8024 static void
8025 add_sibling_attributes (dw_die_ref die)
8026 {
8027   dw_die_ref c;
8028
8029   if (! die->die_child)
8030     return;
8031
8032   if (die->die_parent && die != die->die_parent->die_child)
8033     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8034
8035   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8036 }
8037
8038 /* Output all location lists for the DIE and its children.  */
8039
8040 static void
8041 output_location_lists (dw_die_ref die)
8042 {
8043   dw_die_ref c;
8044   dw_attr_node *a;
8045   unsigned ix;
8046
8047   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8048     if (AT_class (a) == dw_val_class_loc_list)
8049       output_loc_list (AT_loc_list (a));
8050
8051   FOR_EACH_CHILD (die, c, output_location_lists (c));
8052 }
8053
8054 /* We want to limit the number of external references, because they are
8055    larger than local references: a relocation takes multiple words, and
8056    even a sig8 reference is always eight bytes, whereas a local reference
8057    can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
8058    So if we encounter multiple external references to the same type DIE, we
8059    make a local typedef stub for it and redirect all references there.
8060
8061    This is the element of the hash table for keeping track of these
8062    references.  */
8063
8064 struct external_ref
8065 {
8066   dw_die_ref type;
8067   dw_die_ref stub;
8068   unsigned n_refs;
8069 };
8070
8071 /* Hashtable helpers.  */
8072
8073 struct external_ref_hasher : free_ptr_hash <external_ref>
8074 {
8075   static inline hashval_t hash (const external_ref *);
8076   static inline bool equal (const external_ref *, const external_ref *);
8077 };
8078
8079 inline hashval_t
8080 external_ref_hasher::hash (const external_ref *r)
8081 {
8082   dw_die_ref die = r->type;
8083   hashval_t h = 0;
8084
8085   /* We can't use the address of the DIE for hashing, because
8086      that will make the order of the stub DIEs non-deterministic.  */
8087   if (! die->comdat_type_p)
8088     /* We have a symbol; use it to compute a hash.  */
8089     h = htab_hash_string (die->die_id.die_symbol);
8090   else
8091     {
8092       /* We have a type signature; use a subset of the bits as the hash.
8093          The 8-byte signature is at least as large as hashval_t.  */
8094       comdat_type_node *type_node = die->die_id.die_type_node;
8095       memcpy (&h, type_node->signature, sizeof (h));
8096     }
8097   return h;
8098 }
8099
8100 inline bool
8101 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
8102 {
8103   return r1->type == r2->type;
8104 }
8105
8106 typedef hash_table<external_ref_hasher> external_ref_hash_type;
8107
8108 /* Return a pointer to the external_ref for references to DIE.  */
8109
8110 static struct external_ref *
8111 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
8112 {
8113   struct external_ref ref, *ref_p;
8114   external_ref **slot;
8115
8116   ref.type = die;
8117   slot = map->find_slot (&ref, INSERT);
8118   if (*slot != HTAB_EMPTY_ENTRY)
8119     return *slot;
8120
8121   ref_p = XCNEW (struct external_ref);
8122   ref_p->type = die;
8123   *slot = ref_p;
8124   return ref_p;
8125 }
8126
8127 /* Subroutine of optimize_external_refs, below.
8128
8129    If we see a type skeleton, record it as our stub.  If we see external
8130    references, remember how many we've seen.  */
8131
8132 static void
8133 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
8134 {
8135   dw_die_ref c;
8136   dw_attr_node *a;
8137   unsigned ix;
8138   struct external_ref *ref_p;
8139
8140   if (is_type_die (die)
8141       && (c = get_AT_ref (die, DW_AT_signature)))
8142     {
8143       /* This is a local skeleton; use it for local references.  */
8144       ref_p = lookup_external_ref (map, c);
8145       ref_p->stub = die;
8146     }
8147
8148   /* Scan the DIE references, and remember any that refer to DIEs from
8149      other CUs (i.e. those which are not marked).  */
8150   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8151     if (AT_class (a) == dw_val_class_die_ref
8152         && (c = AT_ref (a))->die_mark == 0
8153         && is_type_die (c))
8154       {
8155         ref_p = lookup_external_ref (map, c);
8156         ref_p->n_refs++;
8157       }
8158
8159   FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
8160 }
8161
8162 /* htab_traverse callback function for optimize_external_refs, below.  SLOT
8163    points to an external_ref, DATA is the CU we're processing.  If we don't
8164    already have a local stub, and we have multiple refs, build a stub.  */
8165
8166 int
8167 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
8168 {
8169   struct external_ref *ref_p = *slot;
8170
8171   if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
8172     {
8173       /* We have multiple references to this type, so build a small stub.
8174          Both of these forms are a bit dodgy from the perspective of the
8175          DWARF standard, since technically they should have names.  */
8176       dw_die_ref cu = data;
8177       dw_die_ref type = ref_p->type;
8178       dw_die_ref stub = NULL;
8179
8180       if (type->comdat_type_p)
8181         {
8182           /* If we refer to this type via sig8, use AT_signature.  */
8183           stub = new_die (type->die_tag, cu, NULL_TREE);
8184           add_AT_die_ref (stub, DW_AT_signature, type);
8185         }
8186       else
8187         {
8188           /* Otherwise, use a typedef with no name.  */
8189           stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
8190           add_AT_die_ref (stub, DW_AT_type, type);
8191         }
8192
8193       stub->die_mark++;
8194       ref_p->stub = stub;
8195     }
8196   return 1;
8197 }
8198
8199 /* DIE is a unit; look through all the DIE references to see if there are
8200    any external references to types, and if so, create local stubs for
8201    them which will be applied in build_abbrev_table.  This is useful because
8202    references to local DIEs are smaller.  */
8203
8204 static external_ref_hash_type *
8205 optimize_external_refs (dw_die_ref die)
8206 {
8207   external_ref_hash_type *map = new external_ref_hash_type (10);
8208   optimize_external_refs_1 (die, map);
8209   map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
8210   return map;
8211 }
8212
8213 /* The format of each DIE (and its attribute value pairs) is encoded in an
8214    abbreviation table.  This routine builds the abbreviation table and assigns
8215    a unique abbreviation id for each abbreviation entry.  The children of each
8216    die are visited recursively.  */
8217
8218 static void
8219 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
8220 {
8221   unsigned long abbrev_id;
8222   unsigned int n_alloc;
8223   dw_die_ref c;
8224   dw_attr_node *a;
8225   unsigned ix;
8226
8227   /* Scan the DIE references, and replace any that refer to
8228      DIEs from other CUs (i.e. those which are not marked) with
8229      the local stubs we built in optimize_external_refs.  */
8230   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8231     if (AT_class (a) == dw_val_class_die_ref
8232         && (c = AT_ref (a))->die_mark == 0)
8233       {
8234         struct external_ref *ref_p;
8235         gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
8236
8237         ref_p = lookup_external_ref (extern_map, c);
8238         if (ref_p->stub && ref_p->stub != die)
8239           change_AT_die_ref (a, ref_p->stub);
8240         else
8241           /* We aren't changing this reference, so mark it external.  */
8242           set_AT_ref_external (a, 1);
8243       }
8244
8245   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8246     {
8247       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
8248       dw_attr_node *die_a, *abbrev_a;
8249       unsigned ix;
8250       bool ok = true;
8251
8252       if (abbrev->die_tag != die->die_tag)
8253         continue;
8254       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8255         continue;
8256
8257       if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
8258         continue;
8259
8260       FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
8261         {
8262           abbrev_a = &(*abbrev->die_attr)[ix];
8263           if ((abbrev_a->dw_attr != die_a->dw_attr)
8264               || (value_format (abbrev_a) != value_format (die_a)))
8265             {
8266               ok = false;
8267               break;
8268             }
8269         }
8270       if (ok)
8271         break;
8272     }
8273
8274   if (abbrev_id >= abbrev_die_table_in_use)
8275     {
8276       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
8277         {
8278           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
8279           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
8280                                             n_alloc);
8281
8282           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
8283                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
8284           abbrev_die_table_allocated = n_alloc;
8285         }
8286
8287       ++abbrev_die_table_in_use;
8288       abbrev_die_table[abbrev_id] = die;
8289     }
8290
8291   die->die_abbrev = abbrev_id;
8292   FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
8293 }
8294 \f
8295 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
8296
8297 static int
8298 constant_size (unsigned HOST_WIDE_INT value)
8299 {
8300   int log;
8301
8302   if (value == 0)
8303     log = 0;
8304   else
8305     log = floor_log2 (value);
8306
8307   log = log / 8;
8308   log = 1 << (floor_log2 (log) + 1);
8309
8310   return log;
8311 }
8312
8313 /* Return the size of a DIE as it is represented in the
8314    .debug_info section.  */
8315
8316 static unsigned long
8317 size_of_die (dw_die_ref die)
8318 {
8319   unsigned long size = 0;
8320   dw_attr_node *a;
8321   unsigned ix;
8322   enum dwarf_form form;
8323
8324   size += size_of_uleb128 (die->die_abbrev);
8325   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8326     {
8327       switch (AT_class (a))
8328         {
8329         case dw_val_class_addr:
8330           if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8331             {
8332               gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8333               size += size_of_uleb128 (AT_index (a));
8334             }
8335           else
8336             size += DWARF2_ADDR_SIZE;
8337           break;
8338         case dw_val_class_offset:
8339           size += DWARF_OFFSET_SIZE;
8340           break;
8341         case dw_val_class_loc:
8342           {
8343             unsigned long lsize = size_of_locs (AT_loc (a));
8344
8345             /* Block length.  */
8346             if (dwarf_version >= 4)
8347               size += size_of_uleb128 (lsize);
8348             else
8349               size += constant_size (lsize);
8350             size += lsize;
8351           }
8352           break;
8353         case dw_val_class_loc_list:
8354           if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8355             {
8356               gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8357               size += size_of_uleb128 (AT_index (a));
8358             }
8359           else
8360             size += DWARF_OFFSET_SIZE;
8361           break;
8362         case dw_val_class_range_list:
8363           size += DWARF_OFFSET_SIZE;
8364           break;
8365         case dw_val_class_const:
8366           size += size_of_sleb128 (AT_int (a));
8367           break;
8368         case dw_val_class_unsigned_const:
8369           {
8370             int csize = constant_size (AT_unsigned (a));
8371             if (dwarf_version == 3
8372                 && a->dw_attr == DW_AT_data_member_location
8373                 && csize >= 4)
8374               size += size_of_uleb128 (AT_unsigned (a));
8375             else
8376               size += csize;
8377           }
8378           break;
8379         case dw_val_class_const_double:
8380           size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
8381           if (HOST_BITS_PER_WIDE_INT >= 64)
8382             size++; /* block */
8383           break;
8384         case dw_val_class_wide_int:
8385           size += (get_full_len (*a->dw_attr_val.v.val_wide)
8386                    * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
8387           if (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT
8388               > 64)
8389             size++; /* block */
8390           break;
8391         case dw_val_class_vec:
8392           size += constant_size (a->dw_attr_val.v.val_vec.length
8393                                  * a->dw_attr_val.v.val_vec.elt_size)
8394                   + a->dw_attr_val.v.val_vec.length
8395                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
8396           break;
8397         case dw_val_class_flag:
8398           if (dwarf_version >= 4)
8399             /* Currently all add_AT_flag calls pass in 1 as last argument,
8400                so DW_FORM_flag_present can be used.  If that ever changes,
8401                we'll need to use DW_FORM_flag and have some optimization
8402                in build_abbrev_table that will change those to
8403                DW_FORM_flag_present if it is set to 1 in all DIEs using
8404                the same abbrev entry.  */
8405             gcc_assert (a->dw_attr_val.v.val_flag == 1);
8406           else
8407             size += 1;
8408           break;
8409         case dw_val_class_die_ref:
8410           if (AT_ref_external (a))
8411             {
8412               /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
8413                  we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
8414                  is sized by target address length, whereas in DWARF3
8415                  it's always sized as an offset.  */
8416               if (use_debug_types)
8417                 size += DWARF_TYPE_SIGNATURE_SIZE;
8418               else if (dwarf_version == 2)
8419                 size += DWARF2_ADDR_SIZE;
8420               else
8421                 size += DWARF_OFFSET_SIZE;
8422             }
8423           else
8424             size += DWARF_OFFSET_SIZE;
8425           break;
8426         case dw_val_class_fde_ref:
8427           size += DWARF_OFFSET_SIZE;
8428           break;
8429         case dw_val_class_lbl_id:
8430           if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8431             {
8432               gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8433               size += size_of_uleb128 (AT_index (a));
8434             }
8435           else
8436             size += DWARF2_ADDR_SIZE;
8437           break;
8438         case dw_val_class_lineptr:
8439         case dw_val_class_macptr:
8440           size += DWARF_OFFSET_SIZE;
8441           break;
8442         case dw_val_class_str:
8443           form = AT_string_form (a);
8444           if (form == DW_FORM_strp)
8445             size += DWARF_OFFSET_SIZE;
8446          else if (form == DW_FORM_GNU_str_index)
8447             size += size_of_uleb128 (AT_index (a));
8448           else
8449             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8450           break;
8451         case dw_val_class_file:
8452           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8453           break;
8454         case dw_val_class_data8:
8455           size += 8;
8456           break;
8457         case dw_val_class_vms_delta:
8458           size += DWARF_OFFSET_SIZE;
8459           break;
8460         case dw_val_class_high_pc:
8461           size += DWARF2_ADDR_SIZE;
8462           break;
8463         case dw_val_class_discr_value:
8464           size += size_of_discr_value (&a->dw_attr_val.v.val_discr_value);
8465           break;
8466         case dw_val_class_discr_list:
8467             {
8468               unsigned block_size = size_of_discr_list (AT_discr_list (a));
8469
8470               /* This is a block, so we have the block length and then its
8471                  data.  */
8472               size += constant_size (block_size) + block_size;
8473             }
8474           break;
8475         default:
8476           gcc_unreachable ();
8477         }
8478     }
8479
8480   return size;
8481 }
8482
8483 /* Size the debugging information associated with a given DIE.  Visits the
8484    DIE's children recursively.  Updates the global variable next_die_offset, on
8485    each time through.  Uses the current value of next_die_offset to update the
8486    die_offset field in each DIE.  */
8487
8488 static void
8489 calc_die_sizes (dw_die_ref die)
8490 {
8491   dw_die_ref c;
8492
8493   gcc_assert (die->die_offset == 0
8494               || (unsigned long int) die->die_offset == next_die_offset);
8495   die->die_offset = next_die_offset;
8496   next_die_offset += size_of_die (die);
8497
8498   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8499
8500   if (die->die_child != NULL)
8501     /* Count the null byte used to terminate sibling lists.  */
8502     next_die_offset += 1;
8503 }
8504
8505 /* Size just the base type children at the start of the CU.
8506    This is needed because build_abbrev needs to size locs
8507    and sizing of type based stack ops needs to know die_offset
8508    values for the base types.  */
8509
8510 static void
8511 calc_base_type_die_sizes (void)
8512 {
8513   unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8514   unsigned int i;
8515   dw_die_ref base_type;
8516 #if ENABLE_ASSERT_CHECKING
8517   dw_die_ref prev = comp_unit_die ()->die_child;
8518 #endif
8519
8520   die_offset += size_of_die (comp_unit_die ());
8521   for (i = 0; base_types.iterate (i, &base_type); i++)
8522     {
8523 #if ENABLE_ASSERT_CHECKING
8524       gcc_assert (base_type->die_offset == 0
8525                   && prev->die_sib == base_type
8526                   && base_type->die_child == NULL
8527                   && base_type->die_abbrev);
8528       prev = base_type;
8529 #endif
8530       base_type->die_offset = die_offset;
8531       die_offset += size_of_die (base_type);
8532     }
8533 }
8534
8535 /* Set the marks for a die and its children.  We do this so
8536    that we know whether or not a reference needs to use FORM_ref_addr; only
8537    DIEs in the same CU will be marked.  We used to clear out the offset
8538    and use that as the flag, but ran into ordering problems.  */
8539
8540 static void
8541 mark_dies (dw_die_ref die)
8542 {
8543   dw_die_ref c;
8544
8545   gcc_assert (!die->die_mark);
8546
8547   die->die_mark = 1;
8548   FOR_EACH_CHILD (die, c, mark_dies (c));
8549 }
8550
8551 /* Clear the marks for a die and its children.  */
8552
8553 static void
8554 unmark_dies (dw_die_ref die)
8555 {
8556   dw_die_ref c;
8557
8558   if (! use_debug_types)
8559     gcc_assert (die->die_mark);
8560
8561   die->die_mark = 0;
8562   FOR_EACH_CHILD (die, c, unmark_dies (c));
8563 }
8564
8565 /* Clear the marks for a die, its children and referred dies.  */
8566
8567 static void
8568 unmark_all_dies (dw_die_ref die)
8569 {
8570   dw_die_ref c;
8571   dw_attr_node *a;
8572   unsigned ix;
8573
8574   if (!die->die_mark)
8575     return;
8576   die->die_mark = 0;
8577
8578   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8579
8580   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8581     if (AT_class (a) == dw_val_class_die_ref)
8582       unmark_all_dies (AT_ref (a));
8583 }
8584
8585 /* Calculate if the entry should appear in the final output file.  It may be
8586    from a pruned a type.  */
8587
8588 static bool
8589 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
8590 {
8591   /* By limiting gnu pubnames to definitions only, gold can generate a
8592      gdb index without entries for declarations, which don't include
8593      enough information to be useful.  */
8594   if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
8595     return false;
8596
8597   if (table == pubname_table)
8598     {
8599       /* Enumerator names are part of the pubname table, but the
8600          parent DW_TAG_enumeration_type die may have been pruned.
8601          Don't output them if that is the case.  */
8602       if (p->die->die_tag == DW_TAG_enumerator &&
8603           (p->die->die_parent == NULL
8604            || !p->die->die_parent->die_perennial_p))
8605         return false;
8606
8607       /* Everything else in the pubname table is included.  */
8608       return true;
8609     }
8610
8611   /* The pubtypes table shouldn't include types that have been
8612      pruned.  */
8613   return (p->die->die_offset != 0
8614           || !flag_eliminate_unused_debug_types);
8615 }
8616
8617 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8618    generated for the compilation unit.  */
8619
8620 static unsigned long
8621 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8622 {
8623   unsigned long size;
8624   unsigned i;
8625   pubname_entry *p;
8626   int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8627
8628   size = DWARF_PUBNAMES_HEADER_SIZE;
8629   FOR_EACH_VEC_ELT (*names, i, p)
8630     if (include_pubname_in_output (names, p))
8631       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8632
8633   size += DWARF_OFFSET_SIZE;
8634   return size;
8635 }
8636
8637 /* Return the size of the information in the .debug_aranges section.  */
8638
8639 static unsigned long
8640 size_of_aranges (void)
8641 {
8642   unsigned long size;
8643
8644   size = DWARF_ARANGES_HEADER_SIZE;
8645
8646   /* Count the address/length pair for this compilation unit.  */
8647   if (text_section_used)
8648     size += 2 * DWARF2_ADDR_SIZE;
8649   if (cold_text_section_used)
8650     size += 2 * DWARF2_ADDR_SIZE;
8651   if (have_multiple_function_sections)
8652     {
8653       unsigned fde_idx;
8654       dw_fde_ref fde;
8655
8656       FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8657         {
8658           if (DECL_IGNORED_P (fde->decl))
8659             continue;
8660           if (!fde->in_std_section)
8661             size += 2 * DWARF2_ADDR_SIZE;
8662           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8663             size += 2 * DWARF2_ADDR_SIZE;
8664         }
8665     }
8666
8667   /* Count the two zero words used to terminated the address range table.  */
8668   size += 2 * DWARF2_ADDR_SIZE;
8669   return size;
8670 }
8671 \f
8672 /* Select the encoding of an attribute value.  */
8673
8674 static enum dwarf_form
8675 value_format (dw_attr_node *a)
8676 {
8677   switch (AT_class (a))
8678     {
8679     case dw_val_class_addr:
8680       /* Only very few attributes allow DW_FORM_addr.  */
8681       switch (a->dw_attr)
8682         {
8683         case DW_AT_low_pc:
8684         case DW_AT_high_pc:
8685         case DW_AT_entry_pc:
8686         case DW_AT_trampoline:
8687           return (AT_index (a) == NOT_INDEXED
8688                   ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8689         default:
8690           break;
8691         }
8692       switch (DWARF2_ADDR_SIZE)
8693         {
8694         case 1:
8695           return DW_FORM_data1;
8696         case 2:
8697           return DW_FORM_data2;
8698         case 4:
8699           return DW_FORM_data4;
8700         case 8:
8701           return DW_FORM_data8;
8702         default:
8703           gcc_unreachable ();
8704         }
8705     case dw_val_class_range_list:
8706     case dw_val_class_loc_list:
8707       if (dwarf_version >= 4)
8708         return DW_FORM_sec_offset;
8709       /* FALLTHRU */
8710     case dw_val_class_vms_delta:
8711     case dw_val_class_offset:
8712       switch (DWARF_OFFSET_SIZE)
8713         {
8714         case 4:
8715           return DW_FORM_data4;
8716         case 8:
8717           return DW_FORM_data8;
8718         default:
8719           gcc_unreachable ();
8720         }
8721     case dw_val_class_loc:
8722       if (dwarf_version >= 4)
8723         return DW_FORM_exprloc;
8724       switch (constant_size (size_of_locs (AT_loc (a))))
8725         {
8726         case 1:
8727           return DW_FORM_block1;
8728         case 2:
8729           return DW_FORM_block2;
8730         case 4:
8731           return DW_FORM_block4;
8732         default:
8733           gcc_unreachable ();
8734         }
8735     case dw_val_class_const:
8736       return DW_FORM_sdata;
8737     case dw_val_class_unsigned_const:
8738       switch (constant_size (AT_unsigned (a)))
8739         {
8740         case 1:
8741           return DW_FORM_data1;
8742         case 2:
8743           return DW_FORM_data2;
8744         case 4:
8745           /* In DWARF3 DW_AT_data_member_location with
8746              DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8747              constant, so we need to use DW_FORM_udata if we need
8748              a large constant.  */
8749           if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8750             return DW_FORM_udata;
8751           return DW_FORM_data4;
8752         case 8:
8753           if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8754             return DW_FORM_udata;
8755           return DW_FORM_data8;
8756         default:
8757           gcc_unreachable ();
8758         }
8759     case dw_val_class_const_double:
8760       switch (HOST_BITS_PER_WIDE_INT)
8761         {
8762         case 8:
8763           return DW_FORM_data2;
8764         case 16:
8765           return DW_FORM_data4;
8766         case 32:
8767           return DW_FORM_data8;
8768         case 64:
8769         default:
8770           return DW_FORM_block1;
8771         }
8772     case dw_val_class_wide_int:
8773       switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
8774         {
8775         case 8:
8776           return DW_FORM_data1;
8777         case 16:
8778           return DW_FORM_data2;
8779         case 32:
8780           return DW_FORM_data4;
8781         case 64:
8782           return DW_FORM_data8;
8783         default:
8784           return DW_FORM_block1;
8785         }
8786     case dw_val_class_vec:
8787       switch (constant_size (a->dw_attr_val.v.val_vec.length
8788                              * a->dw_attr_val.v.val_vec.elt_size))
8789         {
8790         case 1:
8791           return DW_FORM_block1;
8792         case 2:
8793           return DW_FORM_block2;
8794         case 4:
8795           return DW_FORM_block4;
8796         default:
8797           gcc_unreachable ();
8798         }
8799     case dw_val_class_flag:
8800       if (dwarf_version >= 4)
8801         {
8802           /* Currently all add_AT_flag calls pass in 1 as last argument,
8803              so DW_FORM_flag_present can be used.  If that ever changes,
8804              we'll need to use DW_FORM_flag and have some optimization
8805              in build_abbrev_table that will change those to
8806              DW_FORM_flag_present if it is set to 1 in all DIEs using
8807              the same abbrev entry.  */
8808           gcc_assert (a->dw_attr_val.v.val_flag == 1);
8809           return DW_FORM_flag_present;
8810         }
8811       return DW_FORM_flag;
8812     case dw_val_class_die_ref:
8813       if (AT_ref_external (a))
8814         return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8815       else
8816         return DW_FORM_ref;
8817     case dw_val_class_fde_ref:
8818       return DW_FORM_data;
8819     case dw_val_class_lbl_id:
8820       return (AT_index (a) == NOT_INDEXED
8821               ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8822     case dw_val_class_lineptr:
8823     case dw_val_class_macptr:
8824       return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8825     case dw_val_class_str:
8826       return AT_string_form (a);
8827     case dw_val_class_file:
8828       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8829         {
8830         case 1:
8831           return DW_FORM_data1;
8832         case 2:
8833           return DW_FORM_data2;
8834         case 4:
8835           return DW_FORM_data4;
8836         default:
8837           gcc_unreachable ();
8838         }
8839
8840     case dw_val_class_data8:
8841       return DW_FORM_data8;
8842
8843     case dw_val_class_high_pc:
8844       switch (DWARF2_ADDR_SIZE)
8845         {
8846         case 1:
8847           return DW_FORM_data1;
8848         case 2:
8849           return DW_FORM_data2;
8850         case 4:
8851           return DW_FORM_data4;
8852         case 8:
8853           return DW_FORM_data8;
8854         default:
8855           gcc_unreachable ();
8856         }
8857
8858     case dw_val_class_discr_value:
8859       return (a->dw_attr_val.v.val_discr_value.pos
8860               ? DW_FORM_udata
8861               : DW_FORM_sdata);
8862     case dw_val_class_discr_list:
8863       switch (constant_size (size_of_discr_list (AT_discr_list (a))))
8864         {
8865         case 1:
8866           return DW_FORM_block1;
8867         case 2:
8868           return DW_FORM_block2;
8869         case 4:
8870           return DW_FORM_block4;
8871         default:
8872           gcc_unreachable ();
8873         }
8874
8875     default:
8876       gcc_unreachable ();
8877     }
8878 }
8879
8880 /* Output the encoding of an attribute value.  */
8881
8882 static void
8883 output_value_format (dw_attr_node *a)
8884 {
8885   enum dwarf_form form = value_format (a);
8886
8887   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8888 }
8889
8890 /* Given a die and id, produce the appropriate abbreviations.  */
8891
8892 static void
8893 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8894 {
8895   unsigned ix;
8896   dw_attr_node *a_attr;
8897
8898   dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8899   dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8900                                dwarf_tag_name (abbrev->die_tag));
8901
8902   if (abbrev->die_child != NULL)
8903     dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8904   else
8905     dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8906
8907   for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8908     {
8909       dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8910                                    dwarf_attr_name (a_attr->dw_attr));
8911       output_value_format (a_attr);
8912     }
8913
8914   dw2_asm_output_data (1, 0, NULL);
8915   dw2_asm_output_data (1, 0, NULL);
8916 }
8917
8918
8919 /* Output the .debug_abbrev section which defines the DIE abbreviation
8920    table.  */
8921
8922 static void
8923 output_abbrev_section (void)
8924 {
8925   unsigned long abbrev_id;
8926
8927   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8928     output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8929
8930   /* Terminate the table.  */
8931   dw2_asm_output_data (1, 0, NULL);
8932 }
8933
8934 /* Output a symbol we can use to refer to this DIE from another CU.  */
8935
8936 static inline void
8937 output_die_symbol (dw_die_ref die)
8938 {
8939   const char *sym = die->die_id.die_symbol;
8940
8941   gcc_assert (!die->comdat_type_p);
8942
8943   if (sym == 0)
8944     return;
8945
8946   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8947     /* We make these global, not weak; if the target doesn't support
8948        .linkonce, it doesn't support combining the sections, so debugging
8949        will break.  */
8950     targetm.asm_out.globalize_label (asm_out_file, sym);
8951
8952   ASM_OUTPUT_LABEL (asm_out_file, sym);
8953 }
8954
8955 /* Return a new location list, given the begin and end range, and the
8956    expression.  */
8957
8958 static inline dw_loc_list_ref
8959 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8960               const char *section)
8961 {
8962   dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
8963
8964   retlist->begin = begin;
8965   retlist->begin_entry = NULL;
8966   retlist->end = end;
8967   retlist->expr = expr;
8968   retlist->section = section;
8969
8970   return retlist;
8971 }
8972
8973 /* Generate a new internal symbol for this location list node, if it
8974    hasn't got one yet.  */
8975
8976 static inline void
8977 gen_llsym (dw_loc_list_ref list)
8978 {
8979   gcc_assert (!list->ll_symbol);
8980   list->ll_symbol = gen_internal_sym ("LLST");
8981 }
8982
8983 /* Output the location list given to us.  */
8984
8985 static void
8986 output_loc_list (dw_loc_list_ref list_head)
8987 {
8988   dw_loc_list_ref curr = list_head;
8989
8990   if (list_head->emitted)
8991     return;
8992   list_head->emitted = true;
8993
8994   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8995
8996   /* Walk the location list, and output each range + expression.  */
8997   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8998     {
8999       unsigned long size;
9000       /* Don't output an entry that starts and ends at the same address.  */
9001       if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
9002         continue;
9003       size = size_of_locs (curr->expr);
9004       /* If the expression is too large, drop it on the floor.  We could
9005          perhaps put it into DW_TAG_dwarf_procedure and refer to that
9006          in the expression, but >= 64KB expressions for a single value
9007          in a single range are unlikely very useful.  */
9008       if (size > 0xffff)
9009         continue;
9010       if (dwarf_split_debug_info)
9011         {
9012           dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
9013                                "Location list start/length entry (%s)",
9014                                list_head->ll_symbol);
9015           dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9016                                        "Location list range start index (%s)",
9017                                        curr->begin);
9018           /* The length field is 4 bytes.  If we ever need to support
9019             an 8-byte length, we can add a new DW_LLE code or fall back
9020             to DW_LLE_GNU_start_end_entry.  */
9021           dw2_asm_output_delta (4, curr->end, curr->begin,
9022                                 "Location list range length (%s)",
9023                                 list_head->ll_symbol);
9024         }
9025       else if (!have_multiple_function_sections)
9026         {
9027           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
9028                                 "Location list begin address (%s)",
9029                                 list_head->ll_symbol);
9030           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
9031                                 "Location list end address (%s)",
9032                                 list_head->ll_symbol);
9033         }
9034       else
9035         {
9036           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9037                                "Location list begin address (%s)",
9038                                list_head->ll_symbol);
9039           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9040                                "Location list end address (%s)",
9041                                list_head->ll_symbol);
9042         }
9043
9044       /* Output the block length for this list of location operations.  */
9045       gcc_assert (size <= 0xffff);
9046       dw2_asm_output_data (2, size, "%s", "Location expression size");
9047
9048       output_loc_sequence (curr->expr, -1);
9049     }
9050
9051   if (dwarf_split_debug_info)
9052     dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
9053                          "Location list terminator (%s)",
9054                          list_head->ll_symbol);
9055   else
9056     {
9057       dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9058                            "Location list terminator begin (%s)",
9059                            list_head->ll_symbol);
9060       dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9061                            "Location list terminator end (%s)",
9062                            list_head->ll_symbol);
9063     }
9064 }
9065
9066 /* Output a range_list offset into the debug_range section.  Emit a
9067    relocated reference if val_entry is NULL, otherwise, emit an
9068    indirect reference.  */
9069
9070 static void
9071 output_range_list_offset (dw_attr_node *a)
9072 {
9073   const char *name = dwarf_attr_name (a->dw_attr);
9074
9075   if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
9076     {
9077       char *p = strchr (ranges_section_label, '\0');
9078       sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
9079       dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
9080                              debug_ranges_section, "%s", name);
9081       *p = '\0';
9082     }
9083   else
9084     dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
9085                          "%s (offset from %s)", name, ranges_section_label);
9086 }
9087
9088 /* Output the offset into the debug_loc section.  */
9089
9090 static void
9091 output_loc_list_offset (dw_attr_node *a)
9092 {
9093   char *sym = AT_loc_list (a)->ll_symbol;
9094
9095   gcc_assert (sym);
9096   if (dwarf_split_debug_info)
9097     dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
9098                           "%s", dwarf_attr_name (a->dw_attr));
9099   else
9100     dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
9101                            "%s", dwarf_attr_name (a->dw_attr));
9102 }
9103
9104 /* Output an attribute's index or value appropriately.  */
9105
9106 static void
9107 output_attr_index_or_value (dw_attr_node *a)
9108 {
9109   const char *name = dwarf_attr_name (a->dw_attr);
9110
9111   if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9112     {
9113       dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
9114       return;
9115     }
9116   switch (AT_class (a))
9117     {
9118       case dw_val_class_addr:
9119         dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
9120         break;
9121       case dw_val_class_high_pc:
9122       case dw_val_class_lbl_id:
9123         dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
9124         break;
9125       case dw_val_class_loc_list:
9126         output_loc_list_offset (a);
9127         break;
9128       default:
9129         gcc_unreachable ();
9130     }
9131 }
9132
9133 /* Output a type signature.  */
9134
9135 static inline void
9136 output_signature (const char *sig, const char *name)
9137 {
9138   int i;
9139
9140   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9141     dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
9142 }
9143
9144 /* Output a discriminant value.  */
9145
9146 static inline void
9147 output_discr_value (dw_discr_value *discr_value, const char *name)
9148 {
9149   if (discr_value->pos)
9150     dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
9151   else
9152     dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
9153 }
9154
9155 /* Output the DIE and its attributes.  Called recursively to generate
9156    the definitions of each child DIE.  */
9157
9158 static void
9159 output_die (dw_die_ref die)
9160 {
9161   dw_attr_node *a;
9162   dw_die_ref c;
9163   unsigned long size;
9164   unsigned ix;
9165
9166   /* If someone in another CU might refer to us, set up a symbol for
9167      them to point to.  */
9168   if (! die->comdat_type_p && die->die_id.die_symbol)
9169     output_die_symbol (die);
9170
9171   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
9172                                (unsigned long)die->die_offset,
9173                                dwarf_tag_name (die->die_tag));
9174
9175   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9176     {
9177       const char *name = dwarf_attr_name (a->dw_attr);
9178
9179       switch (AT_class (a))
9180         {
9181         case dw_val_class_addr:
9182           output_attr_index_or_value (a);
9183           break;
9184
9185         case dw_val_class_offset:
9186           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
9187                                "%s", name);
9188           break;
9189
9190         case dw_val_class_range_list:
9191           output_range_list_offset (a);
9192           break;
9193
9194         case dw_val_class_loc:
9195           size = size_of_locs (AT_loc (a));
9196
9197           /* Output the block length for this list of location operations.  */
9198           if (dwarf_version >= 4)
9199             dw2_asm_output_data_uleb128 (size, "%s", name);
9200           else
9201             dw2_asm_output_data (constant_size (size), size, "%s", name);
9202
9203           output_loc_sequence (AT_loc (a), -1);
9204           break;
9205
9206         case dw_val_class_const:
9207           /* ??? It would be slightly more efficient to use a scheme like is
9208              used for unsigned constants below, but gdb 4.x does not sign
9209              extend.  Gdb 5.x does sign extend.  */
9210           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
9211           break;
9212
9213         case dw_val_class_unsigned_const:
9214           {
9215             int csize = constant_size (AT_unsigned (a));
9216             if (dwarf_version == 3
9217                 && a->dw_attr == DW_AT_data_member_location
9218                 && csize >= 4)
9219               dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
9220             else
9221               dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
9222           }
9223           break;
9224
9225         case dw_val_class_const_double:
9226           {
9227             unsigned HOST_WIDE_INT first, second;
9228
9229             if (HOST_BITS_PER_WIDE_INT >= 64)
9230               dw2_asm_output_data (1,
9231                                    HOST_BITS_PER_DOUBLE_INT
9232                                    / HOST_BITS_PER_CHAR,
9233                                    NULL);
9234
9235             if (WORDS_BIG_ENDIAN)
9236               {
9237                 first = a->dw_attr_val.v.val_double.high;
9238                 second = a->dw_attr_val.v.val_double.low;
9239               }
9240             else
9241               {
9242                 first = a->dw_attr_val.v.val_double.low;
9243                 second = a->dw_attr_val.v.val_double.high;
9244               }
9245
9246             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
9247                                  first, "%s", name);
9248             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
9249                                  second, NULL);
9250           }
9251           break;
9252
9253         case dw_val_class_wide_int:
9254           {
9255             int i;
9256             int len = get_full_len (*a->dw_attr_val.v.val_wide);
9257             int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
9258             if (len * HOST_BITS_PER_WIDE_INT > 64)
9259               dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide) * l,
9260                                    NULL);
9261
9262             if (WORDS_BIG_ENDIAN)
9263               for (i = len - 1; i >= 0; --i)
9264                 {
9265                   dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
9266                                        "%s", name);
9267                   name = "";
9268                 }
9269             else
9270               for (i = 0; i < len; ++i)
9271                 {
9272                   dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
9273                                        "%s", name);
9274                   name = "";
9275                 }
9276           }
9277           break;
9278
9279         case dw_val_class_vec:
9280           {
9281             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
9282             unsigned int len = a->dw_attr_val.v.val_vec.length;
9283             unsigned int i;
9284             unsigned char *p;
9285
9286             dw2_asm_output_data (constant_size (len * elt_size),
9287                                  len * elt_size, "%s", name);
9288             if (elt_size > sizeof (HOST_WIDE_INT))
9289               {
9290                 elt_size /= 2;
9291                 len *= 2;
9292               }
9293             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
9294                  i < len;
9295                  i++, p += elt_size)
9296               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
9297                                    "fp or vector constant word %u", i);
9298             break;
9299           }
9300
9301         case dw_val_class_flag:
9302           if (dwarf_version >= 4)
9303             {
9304               /* Currently all add_AT_flag calls pass in 1 as last argument,
9305                  so DW_FORM_flag_present can be used.  If that ever changes,
9306                  we'll need to use DW_FORM_flag and have some optimization
9307                  in build_abbrev_table that will change those to
9308                  DW_FORM_flag_present if it is set to 1 in all DIEs using
9309                  the same abbrev entry.  */
9310               gcc_assert (AT_flag (a) == 1);
9311               if (flag_debug_asm)
9312                 fprintf (asm_out_file, "\t\t\t%s %s\n",
9313                          ASM_COMMENT_START, name);
9314               break;
9315             }
9316           dw2_asm_output_data (1, AT_flag (a), "%s", name);
9317           break;
9318
9319         case dw_val_class_loc_list:
9320           output_attr_index_or_value (a);
9321           break;
9322
9323         case dw_val_class_die_ref:
9324           if (AT_ref_external (a))
9325             {
9326               if (AT_ref (a)->comdat_type_p)
9327                 {
9328                   comdat_type_node *type_node =
9329                     AT_ref (a)->die_id.die_type_node;
9330
9331                   gcc_assert (type_node);
9332                   output_signature (type_node->signature, name);
9333                 }
9334               else
9335                 {
9336                   const char *sym = AT_ref (a)->die_id.die_symbol;
9337                   int size;
9338
9339                   gcc_assert (sym);
9340                   /* In DWARF2, DW_FORM_ref_addr is sized by target address
9341                      length, whereas in DWARF3 it's always sized as an
9342                      offset.  */
9343                   if (dwarf_version == 2)
9344                     size = DWARF2_ADDR_SIZE;
9345                   else
9346                     size = DWARF_OFFSET_SIZE;
9347                   dw2_asm_output_offset (size, sym, debug_info_section, "%s",
9348                                          name);
9349                 }
9350             }
9351           else
9352             {
9353               gcc_assert (AT_ref (a)->die_offset);
9354               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
9355                                    "%s", name);
9356             }
9357           break;
9358
9359         case dw_val_class_fde_ref:
9360           {
9361             char l1[20];
9362
9363             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
9364                                          a->dw_attr_val.v.val_fde_index * 2);
9365             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
9366                                    "%s", name);
9367           }
9368           break;
9369
9370         case dw_val_class_vms_delta:
9371 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
9372           dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
9373                                     AT_vms_delta2 (a), AT_vms_delta1 (a),
9374                                     "%s", name);
9375 #else
9376           dw2_asm_output_delta (DWARF_OFFSET_SIZE,
9377                                 AT_vms_delta2 (a), AT_vms_delta1 (a),
9378                                 "%s", name);
9379 #endif
9380           break;
9381
9382         case dw_val_class_lbl_id:
9383           output_attr_index_or_value (a);
9384           break;
9385
9386         case dw_val_class_lineptr:
9387           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9388                                  debug_line_section, "%s", name);
9389           break;
9390
9391         case dw_val_class_macptr:
9392           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9393                                  debug_macinfo_section, "%s", name);
9394           break;
9395
9396         case dw_val_class_str:
9397           if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
9398             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9399                                    a->dw_attr_val.v.val_str->label,
9400                                    debug_str_section,
9401                                    "%s: \"%s\"", name, AT_string (a));
9402           else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
9403             dw2_asm_output_data_uleb128 (AT_index (a),
9404                                          "%s: \"%s\"", name, AT_string (a));
9405           else
9406             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
9407           break;
9408
9409         case dw_val_class_file:
9410           {
9411             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
9412
9413             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
9414                                  a->dw_attr_val.v.val_file->filename);
9415             break;
9416           }
9417
9418         case dw_val_class_data8:
9419           {
9420             int i;
9421
9422             for (i = 0; i < 8; i++)
9423               dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
9424                                    i == 0 ? "%s" : NULL, name);
9425             break;
9426           }
9427
9428         case dw_val_class_high_pc:
9429           dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
9430                                 get_AT_low_pc (die), "DW_AT_high_pc");
9431           break;
9432
9433         case dw_val_class_discr_value:
9434           output_discr_value (&a->dw_attr_val.v.val_discr_value, name);
9435           break;
9436
9437         case dw_val_class_discr_list:
9438           {
9439             dw_discr_list_ref list = AT_discr_list (a);
9440             const int size = size_of_discr_list (list);
9441
9442             /* This is a block, so output its length first.  */
9443             dw2_asm_output_data (constant_size (size), size,
9444                                  "%s: block size", name);
9445
9446             for (; list != NULL; list = list->dw_discr_next)
9447               {
9448                 /* One byte for the discriminant value descriptor, and then as
9449                    many LEB128 numbers as required.  */
9450                 if (list->dw_discr_range)
9451                   dw2_asm_output_data (1, DW_DSC_range,
9452                                        "%s: DW_DSC_range", name);
9453                 else
9454                   dw2_asm_output_data (1, DW_DSC_label,
9455                                        "%s: DW_DSC_label", name);
9456
9457                 output_discr_value (&list->dw_discr_lower_bound, name);
9458                 if (list->dw_discr_range)
9459                   output_discr_value (&list->dw_discr_upper_bound, name);
9460               }
9461             break;
9462           }
9463
9464         default:
9465           gcc_unreachable ();
9466         }
9467     }
9468
9469   FOR_EACH_CHILD (die, c, output_die (c));
9470
9471   /* Add null byte to terminate sibling list.  */
9472   if (die->die_child != NULL)
9473     dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
9474                          (unsigned long) die->die_offset);
9475 }
9476
9477 /* Output the compilation unit that appears at the beginning of the
9478    .debug_info section, and precedes the DIE descriptions.  */
9479
9480 static void
9481 output_compilation_unit_header (void)
9482 {
9483   /* We don't support actual DWARFv5 units yet, we just use some
9484      DWARFv5 draft DIE tags in DWARFv4 format.  */
9485   int ver = dwarf_version < 5 ? dwarf_version : 4;
9486
9487   if (!XCOFF_DEBUGGING_INFO)
9488     {
9489       if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9490         dw2_asm_output_data (4, 0xffffffff,
9491           "Initial length escape value indicating 64-bit DWARF extension");
9492       dw2_asm_output_data (DWARF_OFFSET_SIZE,
9493                            next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
9494                            "Length of Compilation Unit Info");
9495     }
9496
9497   dw2_asm_output_data (2, ver, "DWARF version number");
9498   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
9499                          debug_abbrev_section,
9500                          "Offset Into Abbrev. Section");
9501   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9502 }
9503
9504 /* Output the compilation unit DIE and its children.  */
9505
9506 static void
9507 output_comp_unit (dw_die_ref die, int output_if_empty)
9508 {
9509   const char *secname, *oldsym;
9510   char *tmp;
9511
9512   /* Unless we are outputting main CU, we may throw away empty ones.  */
9513   if (!output_if_empty && die->die_child == NULL)
9514     return;
9515
9516   /* Even if there are no children of this DIE, we must output the information
9517      about the compilation unit.  Otherwise, on an empty translation unit, we
9518      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
9519      will then complain when examining the file.  First mark all the DIEs in
9520      this CU so we know which get local refs.  */
9521   mark_dies (die);
9522
9523   external_ref_hash_type *extern_map = optimize_external_refs (die);
9524
9525   build_abbrev_table (die, extern_map);
9526
9527   delete extern_map;
9528
9529   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
9530   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
9531   calc_die_sizes (die);
9532
9533   oldsym = die->die_id.die_symbol;
9534   if (oldsym)
9535     {
9536       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
9537
9538       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
9539       secname = tmp;
9540       die->die_id.die_symbol = NULL;
9541       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9542     }
9543   else
9544     {
9545       switch_to_section (debug_info_section);
9546       ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
9547       info_section_emitted = true;
9548     }
9549
9550   /* Output debugging information.  */
9551   output_compilation_unit_header ();
9552   output_die (die);
9553
9554   /* Leave the marks on the main CU, so we can check them in
9555      output_pubnames.  */
9556   if (oldsym)
9557     {
9558       unmark_dies (die);
9559       die->die_id.die_symbol = oldsym;
9560     }
9561 }
9562
9563 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
9564    and .debug_pubtypes.  This is configured per-target, but can be
9565    overridden by the -gpubnames or -gno-pubnames options.  */
9566
9567 static inline bool
9568 want_pubnames (void)
9569 {
9570   if (debug_info_level <= DINFO_LEVEL_TERSE)
9571     return false;
9572   if (debug_generate_pub_sections != -1)
9573     return debug_generate_pub_sections;
9574   return targetm.want_debug_pub_sections;
9575 }
9576
9577 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes.  */
9578
9579 static void
9580 add_AT_pubnames (dw_die_ref die)
9581 {
9582   if (want_pubnames ())
9583     add_AT_flag (die, DW_AT_GNU_pubnames, 1);
9584 }
9585
9586 /* Add a string attribute value to a skeleton DIE.  */
9587
9588 static inline void
9589 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
9590                         const char *str)
9591 {
9592   dw_attr_node attr;
9593   struct indirect_string_node *node;
9594
9595   if (! skeleton_debug_str_hash)
9596     skeleton_debug_str_hash
9597       = hash_table<indirect_string_hasher>::create_ggc (10);
9598
9599   node = find_AT_string_in_table (str, skeleton_debug_str_hash);
9600   find_string_form (node);
9601   if (node->form == DW_FORM_GNU_str_index)
9602     node->form = DW_FORM_strp;
9603
9604   attr.dw_attr = attr_kind;
9605   attr.dw_attr_val.val_class = dw_val_class_str;
9606   attr.dw_attr_val.val_entry = NULL;
9607   attr.dw_attr_val.v.val_str = node;
9608   add_dwarf_attr (die, &attr);
9609 }
9610
9611 /* Helper function to generate top-level dies for skeleton debug_info and
9612    debug_types.  */
9613
9614 static void
9615 add_top_level_skeleton_die_attrs (dw_die_ref die)
9616 {
9617   const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
9618   const char *comp_dir = comp_dir_string ();
9619
9620   add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
9621   if (comp_dir != NULL)
9622     add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
9623   add_AT_pubnames (die);
9624   add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
9625 }
9626
9627 /* Output skeleton debug sections that point to the dwo file.  */
9628
9629 static void
9630 output_skeleton_debug_sections (dw_die_ref comp_unit)
9631 {
9632   /* We don't support actual DWARFv5 units yet, we just use some
9633      DWARFv5 draft DIE tags in DWARFv4 format.  */
9634   int ver = dwarf_version < 5 ? dwarf_version : 4;
9635
9636   /* These attributes will be found in the full debug_info section.  */
9637   remove_AT (comp_unit, DW_AT_producer);
9638   remove_AT (comp_unit, DW_AT_language);
9639
9640   switch_to_section (debug_skeleton_info_section);
9641   ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
9642
9643   /* Produce the skeleton compilation-unit header.  This one differs enough from
9644      a normal CU header that it's better not to call output_compilation_unit
9645      header.  */
9646   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9647     dw2_asm_output_data (4, 0xffffffff,
9648       "Initial length escape value indicating 64-bit DWARF extension");
9649
9650   dw2_asm_output_data (DWARF_OFFSET_SIZE,
9651                        DWARF_COMPILE_UNIT_HEADER_SIZE
9652                        - DWARF_INITIAL_LENGTH_SIZE
9653                        + size_of_die (comp_unit),
9654                       "Length of Compilation Unit Info");
9655   dw2_asm_output_data (2, ver, "DWARF version number");
9656   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
9657                          debug_abbrev_section,
9658                          "Offset Into Abbrev. Section");
9659   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9660
9661   comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
9662   output_die (comp_unit);
9663
9664   /* Build the skeleton debug_abbrev section.  */
9665   switch_to_section (debug_skeleton_abbrev_section);
9666   ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
9667
9668   output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
9669
9670   dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
9671 }
9672
9673 /* Output a comdat type unit DIE and its children.  */
9674
9675 static void
9676 output_comdat_type_unit (comdat_type_node *node)
9677 {
9678   const char *secname;
9679   char *tmp;
9680   int i;
9681 #if defined (OBJECT_FORMAT_ELF)
9682   tree comdat_key;
9683 #endif
9684
9685   /* First mark all the DIEs in this CU so we know which get local refs.  */
9686   mark_dies (node->root_die);
9687
9688   external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
9689
9690   build_abbrev_table (node->root_die, extern_map);
9691
9692   delete extern_map;
9693   extern_map = NULL;
9694
9695   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
9696   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
9697   calc_die_sizes (node->root_die);
9698
9699 #if defined (OBJECT_FORMAT_ELF)
9700   if (!dwarf_split_debug_info)
9701     secname = ".debug_types";
9702   else
9703     secname = ".debug_types.dwo";
9704
9705   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9706   sprintf (tmp, "wt.");
9707   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9708     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9709   comdat_key = get_identifier (tmp);
9710   targetm.asm_out.named_section (secname,
9711                                  SECTION_DEBUG | SECTION_LINKONCE,
9712                                  comdat_key);
9713 #else
9714   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9715   sprintf (tmp, ".gnu.linkonce.wt.");
9716   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9717     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9718   secname = tmp;
9719   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9720 #endif
9721
9722   /* Output debugging information.  */
9723   output_compilation_unit_header ();
9724   output_signature (node->signature, "Type Signature");
9725   dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9726                        "Offset to Type DIE");
9727   output_die (node->root_die);
9728
9729   unmark_dies (node->root_die);
9730 }
9731
9732 /* Return the DWARF2/3 pubname associated with a decl.  */
9733
9734 static const char *
9735 dwarf2_name (tree decl, int scope)
9736 {
9737   if (DECL_NAMELESS (decl))
9738     return NULL;
9739   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9740 }
9741
9742 /* Add a new entry to .debug_pubnames if appropriate.  */
9743
9744 static void
9745 add_pubname_string (const char *str, dw_die_ref die)
9746 {
9747   pubname_entry e;
9748
9749   e.die = die;
9750   e.name = xstrdup (str);
9751   vec_safe_push (pubname_table, e);
9752 }
9753
9754 static void
9755 add_pubname (tree decl, dw_die_ref die)
9756 {
9757   if (!want_pubnames ())
9758     return;
9759
9760   /* Don't add items to the table when we expect that the consumer will have
9761      just read the enclosing die.  For example, if the consumer is looking at a
9762      class_member, it will either be inside the class already, or will have just
9763      looked up the class to find the member.  Either way, searching the class is
9764      faster than searching the index.  */
9765   if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9766       || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9767     {
9768       const char *name = dwarf2_name (decl, 1);
9769
9770       if (name)
9771         add_pubname_string (name, die);
9772     }
9773 }
9774
9775 /* Add an enumerator to the pubnames section.  */
9776
9777 static void
9778 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9779 {
9780   pubname_entry e;
9781
9782   gcc_assert (scope_name);
9783   e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9784   e.die = die;
9785   vec_safe_push (pubname_table, e);
9786 }
9787
9788 /* Add a new entry to .debug_pubtypes if appropriate.  */
9789
9790 static void
9791 add_pubtype (tree decl, dw_die_ref die)
9792 {
9793   pubname_entry e;
9794
9795   if (!want_pubnames ())
9796     return;
9797
9798   if ((TREE_PUBLIC (decl)
9799        || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9800       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9801     {
9802       tree scope = NULL;
9803       const char *scope_name = "";
9804       const char *sep = is_cxx () ? "::" : ".";
9805       const char *name;
9806
9807       scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9808       if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9809         {
9810           scope_name = lang_hooks.dwarf_name (scope, 1);
9811           if (scope_name != NULL && scope_name[0] != '\0')
9812             scope_name = concat (scope_name, sep, NULL);
9813           else
9814             scope_name = "";
9815         }
9816
9817       if (TYPE_P (decl))
9818         name = type_tag (decl);
9819       else
9820         name = lang_hooks.dwarf_name (decl, 1);
9821
9822       /* If we don't have a name for the type, there's no point in adding
9823          it to the table.  */
9824       if (name != NULL && name[0] != '\0')
9825         {
9826           e.die = die;
9827           e.name = concat (scope_name, name, NULL);
9828           vec_safe_push (pubtype_table, e);
9829         }
9830
9831       /* Although it might be more consistent to add the pubinfo for the
9832          enumerators as their dies are created, they should only be added if the
9833          enum type meets the criteria above.  So rather than re-check the parent
9834          enum type whenever an enumerator die is created, just output them all
9835          here.  This isn't protected by the name conditional because anonymous
9836          enums don't have names.  */
9837       if (die->die_tag == DW_TAG_enumeration_type)
9838         {
9839           dw_die_ref c;
9840
9841           FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9842         }
9843     }
9844 }
9845
9846 /* Output a single entry in the pubnames table.  */
9847
9848 static void
9849 output_pubname (dw_offset die_offset, pubname_entry *entry)
9850 {
9851   dw_die_ref die = entry->die;
9852   int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9853
9854   dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9855
9856   if (debug_generate_pub_sections == 2)
9857     {
9858       /* This logic follows gdb's method for determining the value of the flag
9859          byte.  */
9860       uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9861       switch (die->die_tag)
9862       {
9863         case DW_TAG_typedef:
9864         case DW_TAG_base_type:
9865         case DW_TAG_subrange_type:
9866           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9867           GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9868           break;
9869         case DW_TAG_enumerator:
9870           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9871                                           GDB_INDEX_SYMBOL_KIND_VARIABLE);
9872           if (!is_cxx () && !is_java ())
9873             GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9874           break;
9875         case DW_TAG_subprogram:
9876           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9877                                           GDB_INDEX_SYMBOL_KIND_FUNCTION);
9878           if (!is_ada ())
9879             GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9880           break;
9881         case DW_TAG_constant:
9882           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9883                                           GDB_INDEX_SYMBOL_KIND_VARIABLE);
9884           GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9885           break;
9886         case DW_TAG_variable:
9887           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9888                                           GDB_INDEX_SYMBOL_KIND_VARIABLE);
9889           GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9890           break;
9891         case DW_TAG_namespace:
9892         case DW_TAG_imported_declaration:
9893           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9894           break;
9895         case DW_TAG_class_type:
9896         case DW_TAG_interface_type:
9897         case DW_TAG_structure_type:
9898         case DW_TAG_union_type:
9899         case DW_TAG_enumeration_type:
9900           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9901           if (!is_cxx () && !is_java ())
9902             GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9903           break;
9904         default:
9905           /* An unusual tag.  Leave the flag-byte empty.  */
9906           break;
9907       }
9908       dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9909                            "GDB-index flags");
9910     }
9911
9912   dw2_asm_output_nstring (entry->name, -1, "external name");
9913 }
9914
9915
9916 /* Output the public names table used to speed up access to externally
9917    visible names; or the public types table used to find type definitions.  */
9918
9919 static void
9920 output_pubnames (vec<pubname_entry, va_gc> *names)
9921 {
9922   unsigned i;
9923   unsigned long pubnames_length = size_of_pubnames (names);
9924   pubname_entry *pub;
9925
9926   if (!XCOFF_DEBUGGING_INFO)
9927     {
9928       if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9929         dw2_asm_output_data (4, 0xffffffff,
9930           "Initial length escape value indicating 64-bit DWARF extension");
9931       dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
9932                            "Pub Info Length");
9933     }
9934
9935   /* Version number for pubnames/pubtypes is independent of dwarf version.  */
9936   dw2_asm_output_data (2, 2, "DWARF Version");
9937
9938   if (dwarf_split_debug_info)
9939     dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9940                            debug_skeleton_info_section,
9941                            "Offset of Compilation Unit Info");
9942   else
9943     dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9944                            debug_info_section,
9945                            "Offset of Compilation Unit Info");
9946   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9947                        "Compilation Unit Length");
9948
9949   FOR_EACH_VEC_ELT (*names, i, pub)
9950     {
9951       if (include_pubname_in_output (names, pub))
9952         {
9953           dw_offset die_offset = pub->die->die_offset;
9954
9955           /* We shouldn't see pubnames for DIEs outside of the main CU.  */
9956           if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9957             gcc_assert (pub->die->die_mark);
9958
9959           /* If we're putting types in their own .debug_types sections,
9960              the .debug_pubtypes table will still point to the compile
9961              unit (not the type unit), so we want to use the offset of
9962              the skeleton DIE (if there is one).  */
9963           if (pub->die->comdat_type_p && names == pubtype_table)
9964             {
9965               comdat_type_node *type_node = pub->die->die_id.die_type_node;
9966
9967               if (type_node != NULL)
9968                 die_offset = (type_node->skeleton_die != NULL
9969                               ? type_node->skeleton_die->die_offset
9970                               : comp_unit_die ()->die_offset);
9971             }
9972
9973           output_pubname (die_offset, pub);
9974         }
9975     }
9976
9977   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9978 }
9979
9980 /* Output public names and types tables if necessary.  */
9981
9982 static void
9983 output_pubtables (void)
9984 {
9985   if (!want_pubnames () || !info_section_emitted)
9986     return;
9987
9988   switch_to_section (debug_pubnames_section);
9989   output_pubnames (pubname_table);
9990   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
9991      It shouldn't hurt to emit it always, since pure DWARF2 consumers
9992      simply won't look for the section.  */
9993   switch_to_section (debug_pubtypes_section);
9994   output_pubnames (pubtype_table);
9995 }
9996
9997
9998 /* Output the information that goes into the .debug_aranges table.
9999    Namely, define the beginning and ending address range of the
10000    text section generated for this compilation unit.  */
10001
10002 static void
10003 output_aranges (void)
10004 {
10005   unsigned i;
10006   unsigned long aranges_length = size_of_aranges ();
10007   
10008   if (!XCOFF_DEBUGGING_INFO)
10009     {
10010       if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10011         dw2_asm_output_data (4, 0xffffffff,
10012           "Initial length escape value indicating 64-bit DWARF extension");
10013       dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
10014                            "Length of Address Ranges Info");
10015     }
10016
10017   /* Version number for aranges is still 2, even up to DWARF5.  */
10018   dw2_asm_output_data (2, 2, "DWARF Version");
10019   if (dwarf_split_debug_info)
10020     dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10021                            debug_skeleton_info_section,
10022                            "Offset of Compilation Unit Info");
10023   else
10024     dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10025                            debug_info_section,
10026                            "Offset of Compilation Unit Info");
10027   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
10028   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
10029
10030   /* We need to align to twice the pointer size here.  */
10031   if (DWARF_ARANGES_PAD_SIZE)
10032     {
10033       /* Pad using a 2 byte words so that padding is correct for any
10034          pointer size.  */
10035       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
10036                            2 * DWARF2_ADDR_SIZE);
10037       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
10038         dw2_asm_output_data (2, 0, NULL);
10039     }
10040
10041   /* It is necessary not to output these entries if the sections were
10042      not used; if the sections were not used, the length will be 0 and
10043      the address may end up as 0 if the section is discarded by ld
10044      --gc-sections, leaving an invalid (0, 0) entry that can be
10045      confused with the terminator.  */
10046   if (text_section_used)
10047     {
10048       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
10049       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
10050                             text_section_label, "Length");
10051     }
10052   if (cold_text_section_used)
10053     {
10054       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
10055                            "Address");
10056       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
10057                             cold_text_section_label, "Length");
10058     }
10059
10060   if (have_multiple_function_sections)
10061     {
10062       unsigned fde_idx;
10063       dw_fde_ref fde;
10064
10065       FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
10066         {
10067           if (DECL_IGNORED_P (fde->decl))
10068             continue;
10069           if (!fde->in_std_section)
10070             {
10071               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
10072                                    "Address");
10073               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
10074                                     fde->dw_fde_begin, "Length");
10075             }
10076           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
10077             {
10078               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
10079                                    "Address");
10080               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
10081                                     fde->dw_fde_second_begin, "Length");
10082             }
10083         }
10084     }
10085
10086   /* Output the terminator words.  */
10087   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10088   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10089 }
10090
10091 /* Add a new entry to .debug_ranges.  Return the offset at which it
10092    was placed.  */
10093
10094 static unsigned int
10095 add_ranges_num (int num)
10096 {
10097   unsigned int in_use = ranges_table_in_use;
10098
10099   if (in_use == ranges_table_allocated)
10100     {
10101       ranges_table_allocated += RANGES_TABLE_INCREMENT;
10102       ranges_table = GGC_RESIZEVEC (dw_ranges, ranges_table,
10103                                     ranges_table_allocated);
10104       memset (ranges_table + ranges_table_in_use, 0,
10105               RANGES_TABLE_INCREMENT * sizeof (dw_ranges));
10106     }
10107
10108   ranges_table[in_use].num = num;
10109   ranges_table_in_use = in_use + 1;
10110
10111   return in_use * 2 * DWARF2_ADDR_SIZE;
10112 }
10113
10114 /* Add a new entry to .debug_ranges corresponding to a block, or a
10115    range terminator if BLOCK is NULL.  */
10116
10117 static unsigned int
10118 add_ranges (const_tree block)
10119 {
10120   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
10121 }
10122
10123 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
10124    When using dwarf_split_debug_info, address attributes in dies destined
10125    for the final executable should be direct references--setting the
10126    parameter force_direct ensures this behavior.  */
10127
10128 static void
10129 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
10130                       bool *added, bool force_direct)
10131 {
10132   unsigned int in_use = ranges_by_label_in_use;
10133   unsigned int offset;
10134
10135   if (in_use == ranges_by_label_allocated)
10136     {
10137       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
10138       ranges_by_label = GGC_RESIZEVEC (dw_ranges_by_label, ranges_by_label,
10139                                        ranges_by_label_allocated);
10140       memset (ranges_by_label + ranges_by_label_in_use, 0,
10141               RANGES_TABLE_INCREMENT * sizeof (dw_ranges_by_label));
10142     }
10143
10144   ranges_by_label[in_use].begin = begin;
10145   ranges_by_label[in_use].end = end;
10146   ranges_by_label_in_use = in_use + 1;
10147
10148   offset = add_ranges_num (-(int)in_use - 1);
10149   if (!*added)
10150     {
10151       add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
10152       *added = true;
10153     }
10154 }
10155
10156 static void
10157 output_ranges (void)
10158 {
10159   unsigned i;
10160   static const char *const start_fmt = "Offset %#x";
10161   const char *fmt = start_fmt;
10162
10163   for (i = 0; i < ranges_table_in_use; i++)
10164     {
10165       int block_num = ranges_table[i].num;
10166
10167       if (block_num > 0)
10168         {
10169           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
10170           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
10171
10172           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
10173           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
10174
10175           /* If all code is in the text section, then the compilation
10176              unit base address defaults to DW_AT_low_pc, which is the
10177              base of the text section.  */
10178           if (!have_multiple_function_sections)
10179             {
10180               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
10181                                     text_section_label,
10182                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
10183               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
10184                                     text_section_label, NULL);
10185             }
10186
10187           /* Otherwise, the compilation unit base address is zero,
10188              which allows us to use absolute addresses, and not worry
10189              about whether the target supports cross-section
10190              arithmetic.  */
10191           else
10192             {
10193               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
10194                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
10195               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
10196             }
10197
10198           fmt = NULL;
10199         }
10200
10201       /* Negative block_num stands for an index into ranges_by_label.  */
10202       else if (block_num < 0)
10203         {
10204           int lab_idx = - block_num - 1;
10205
10206           if (!have_multiple_function_sections)
10207             {
10208               gcc_unreachable ();
10209 #if 0
10210               /* If we ever use add_ranges_by_labels () for a single
10211                  function section, all we have to do is to take out
10212                  the #if 0 above.  */
10213               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
10214                                     ranges_by_label[lab_idx].begin,
10215                                     text_section_label,
10216                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
10217               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
10218                                     ranges_by_label[lab_idx].end,
10219                                     text_section_label, NULL);
10220 #endif
10221             }
10222           else
10223             {
10224               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
10225                                    ranges_by_label[lab_idx].begin,
10226                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
10227               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
10228                                    ranges_by_label[lab_idx].end,
10229                                    NULL);
10230             }
10231         }
10232       else
10233         {
10234           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10235           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10236           fmt = start_fmt;
10237         }
10238     }
10239 }
10240
10241 /* Data structure containing information about input files.  */
10242 struct file_info
10243 {
10244   const char *path;     /* Complete file name.  */
10245   const char *fname;    /* File name part.  */
10246   int length;           /* Length of entire string.  */
10247   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
10248   int dir_idx;          /* Index in directory table.  */
10249 };
10250
10251 /* Data structure containing information about directories with source
10252    files.  */
10253 struct dir_info
10254 {
10255   const char *path;     /* Path including directory name.  */
10256   int length;           /* Path length.  */
10257   int prefix;           /* Index of directory entry which is a prefix.  */
10258   int count;            /* Number of files in this directory.  */
10259   int dir_idx;          /* Index of directory used as base.  */
10260 };
10261
10262 /* Callback function for file_info comparison.  We sort by looking at
10263    the directories in the path.  */
10264
10265 static int
10266 file_info_cmp (const void *p1, const void *p2)
10267 {
10268   const struct file_info *const s1 = (const struct file_info *) p1;
10269   const struct file_info *const s2 = (const struct file_info *) p2;
10270   const unsigned char *cp1;
10271   const unsigned char *cp2;
10272
10273   /* Take care of file names without directories.  We need to make sure that
10274      we return consistent values to qsort since some will get confused if
10275      we return the same value when identical operands are passed in opposite
10276      orders.  So if neither has a directory, return 0 and otherwise return
10277      1 or -1 depending on which one has the directory.  */
10278   if ((s1->path == s1->fname || s2->path == s2->fname))
10279     return (s2->path == s2->fname) - (s1->path == s1->fname);
10280
10281   cp1 = (const unsigned char *) s1->path;
10282   cp2 = (const unsigned char *) s2->path;
10283
10284   while (1)
10285     {
10286       ++cp1;
10287       ++cp2;
10288       /* Reached the end of the first path?  If so, handle like above.  */
10289       if ((cp1 == (const unsigned char *) s1->fname)
10290           || (cp2 == (const unsigned char *) s2->fname))
10291         return ((cp2 == (const unsigned char *) s2->fname)
10292                 - (cp1 == (const unsigned char *) s1->fname));
10293
10294       /* Character of current path component the same?  */
10295       else if (*cp1 != *cp2)
10296         return *cp1 - *cp2;
10297     }
10298 }
10299
10300 struct file_name_acquire_data
10301 {
10302   struct file_info *files;
10303   int used_files;
10304   int max_files;
10305 };
10306
10307 /* Traversal function for the hash table.  */
10308
10309 int
10310 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
10311 {
10312   struct dwarf_file_data *d = *slot;
10313   struct file_info *fi;
10314   const char *f;
10315
10316   gcc_assert (fnad->max_files >= d->emitted_number);
10317
10318   if (! d->emitted_number)
10319     return 1;
10320
10321   gcc_assert (fnad->max_files != fnad->used_files);
10322
10323   fi = fnad->files + fnad->used_files++;
10324
10325   /* Skip all leading "./".  */
10326   f = d->filename;
10327   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
10328     f += 2;
10329
10330   /* Create a new array entry.  */
10331   fi->path = f;
10332   fi->length = strlen (f);
10333   fi->file_idx = d;
10334
10335   /* Search for the file name part.  */
10336   f = strrchr (f, DIR_SEPARATOR);
10337 #if defined (DIR_SEPARATOR_2)
10338   {
10339     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
10340
10341     if (g != NULL)
10342       {
10343         if (f == NULL || f < g)
10344           f = g;
10345       }
10346   }
10347 #endif
10348
10349   fi->fname = f == NULL ? fi->path : f + 1;
10350   return 1;
10351 }
10352
10353 /* Output the directory table and the file name table.  We try to minimize
10354    the total amount of memory needed.  A heuristic is used to avoid large
10355    slowdowns with many input files.  */
10356
10357 static void
10358 output_file_names (void)
10359 {
10360   struct file_name_acquire_data fnad;
10361   int numfiles;
10362   struct file_info *files;
10363   struct dir_info *dirs;
10364   int *saved;
10365   int *savehere;
10366   int *backmap;
10367   int ndirs;
10368   int idx_offset;
10369   int i;
10370
10371   if (!last_emitted_file)
10372     {
10373       dw2_asm_output_data (1, 0, "End directory table");
10374       dw2_asm_output_data (1, 0, "End file name table");
10375       return;
10376     }
10377
10378   numfiles = last_emitted_file->emitted_number;
10379
10380   /* Allocate the various arrays we need.  */
10381   files = XALLOCAVEC (struct file_info, numfiles);
10382   dirs = XALLOCAVEC (struct dir_info, numfiles);
10383
10384   fnad.files = files;
10385   fnad.used_files = 0;
10386   fnad.max_files = numfiles;
10387   file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
10388   gcc_assert (fnad.used_files == fnad.max_files);
10389
10390   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
10391
10392   /* Find all the different directories used.  */
10393   dirs[0].path = files[0].path;
10394   dirs[0].length = files[0].fname - files[0].path;
10395   dirs[0].prefix = -1;
10396   dirs[0].count = 1;
10397   dirs[0].dir_idx = 0;
10398   files[0].dir_idx = 0;
10399   ndirs = 1;
10400
10401   for (i = 1; i < numfiles; i++)
10402     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
10403         && memcmp (dirs[ndirs - 1].path, files[i].path,
10404                    dirs[ndirs - 1].length) == 0)
10405       {
10406         /* Same directory as last entry.  */
10407         files[i].dir_idx = ndirs - 1;
10408         ++dirs[ndirs - 1].count;
10409       }
10410     else
10411       {
10412         int j;
10413
10414         /* This is a new directory.  */
10415         dirs[ndirs].path = files[i].path;
10416         dirs[ndirs].length = files[i].fname - files[i].path;
10417         dirs[ndirs].count = 1;
10418         dirs[ndirs].dir_idx = ndirs;
10419         files[i].dir_idx = ndirs;
10420
10421         /* Search for a prefix.  */
10422         dirs[ndirs].prefix = -1;
10423         for (j = 0; j < ndirs; j++)
10424           if (dirs[j].length < dirs[ndirs].length
10425               && dirs[j].length > 1
10426               && (dirs[ndirs].prefix == -1
10427                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
10428               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
10429             dirs[ndirs].prefix = j;
10430
10431         ++ndirs;
10432       }
10433
10434   /* Now to the actual work.  We have to find a subset of the directories which
10435      allow expressing the file name using references to the directory table
10436      with the least amount of characters.  We do not do an exhaustive search
10437      where we would have to check out every combination of every single
10438      possible prefix.  Instead we use a heuristic which provides nearly optimal
10439      results in most cases and never is much off.  */
10440   saved = XALLOCAVEC (int, ndirs);
10441   savehere = XALLOCAVEC (int, ndirs);
10442
10443   memset (saved, '\0', ndirs * sizeof (saved[0]));
10444   for (i = 0; i < ndirs; i++)
10445     {
10446       int j;
10447       int total;
10448
10449       /* We can always save some space for the current directory.  But this
10450          does not mean it will be enough to justify adding the directory.  */
10451       savehere[i] = dirs[i].length;
10452       total = (savehere[i] - saved[i]) * dirs[i].count;
10453
10454       for (j = i + 1; j < ndirs; j++)
10455         {
10456           savehere[j] = 0;
10457           if (saved[j] < dirs[i].length)
10458             {
10459               /* Determine whether the dirs[i] path is a prefix of the
10460                  dirs[j] path.  */
10461               int k;
10462
10463               k = dirs[j].prefix;
10464               while (k != -1 && k != (int) i)
10465                 k = dirs[k].prefix;
10466
10467               if (k == (int) i)
10468                 {
10469                   /* Yes it is.  We can possibly save some memory by
10470                      writing the filenames in dirs[j] relative to
10471                      dirs[i].  */
10472                   savehere[j] = dirs[i].length;
10473                   total += (savehere[j] - saved[j]) * dirs[j].count;
10474                 }
10475             }
10476         }
10477
10478       /* Check whether we can save enough to justify adding the dirs[i]
10479          directory.  */
10480       if (total > dirs[i].length + 1)
10481         {
10482           /* It's worthwhile adding.  */
10483           for (j = i; j < ndirs; j++)
10484             if (savehere[j] > 0)
10485               {
10486                 /* Remember how much we saved for this directory so far.  */
10487                 saved[j] = savehere[j];
10488
10489                 /* Remember the prefix directory.  */
10490                 dirs[j].dir_idx = i;
10491               }
10492         }
10493     }
10494
10495   /* Emit the directory name table.  */
10496   idx_offset = dirs[0].length > 0 ? 1 : 0;
10497   for (i = 1 - idx_offset; i < ndirs; i++)
10498     dw2_asm_output_nstring (dirs[i].path,
10499                             dirs[i].length
10500                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
10501                             "Directory Entry: %#x", i + idx_offset);
10502
10503   dw2_asm_output_data (1, 0, "End directory table");
10504
10505   /* We have to emit them in the order of emitted_number since that's
10506      used in the debug info generation.  To do this efficiently we
10507      generate a back-mapping of the indices first.  */
10508   backmap = XALLOCAVEC (int, numfiles);
10509   for (i = 0; i < numfiles; i++)
10510     backmap[files[i].file_idx->emitted_number - 1] = i;
10511
10512   /* Now write all the file names.  */
10513   for (i = 0; i < numfiles; i++)
10514     {
10515       int file_idx = backmap[i];
10516       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
10517
10518 #ifdef VMS_DEBUGGING_INFO
10519 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
10520
10521       /* Setting these fields can lead to debugger miscomparisons,
10522          but VMS Debug requires them to be set correctly.  */
10523
10524       int ver;
10525       long long cdt;
10526       long siz;
10527       int maxfilelen = strlen (files[file_idx].path)
10528                                + dirs[dir_idx].length
10529                                + MAX_VMS_VERSION_LEN + 1;
10530       char *filebuf = XALLOCAVEC (char, maxfilelen);
10531
10532       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
10533       snprintf (filebuf, maxfilelen, "%s;%d",
10534                 files[file_idx].path + dirs[dir_idx].length, ver);
10535
10536       dw2_asm_output_nstring
10537         (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
10538
10539       /* Include directory index.  */
10540       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10541
10542       /* Modification time.  */
10543       dw2_asm_output_data_uleb128
10544         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
10545           ? cdt : 0,
10546          NULL);
10547
10548       /* File length in bytes.  */
10549       dw2_asm_output_data_uleb128
10550         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
10551           ? siz : 0,
10552          NULL);
10553 #else
10554       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
10555                               "File Entry: %#x", (unsigned) i + 1);
10556
10557       /* Include directory index.  */
10558       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10559
10560       /* Modification time.  */
10561       dw2_asm_output_data_uleb128 (0, NULL);
10562
10563       /* File length in bytes.  */
10564       dw2_asm_output_data_uleb128 (0, NULL);
10565 #endif /* VMS_DEBUGGING_INFO */
10566     }
10567
10568   dw2_asm_output_data (1, 0, "End file name table");
10569 }
10570
10571
10572 /* Output one line number table into the .debug_line section.  */
10573
10574 static void
10575 output_one_line_info_table (dw_line_info_table *table)
10576 {
10577   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
10578   unsigned int current_line = 1;
10579   bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
10580   dw_line_info_entry *ent;
10581   size_t i;
10582
10583   FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
10584     {
10585       switch (ent->opcode)
10586         {
10587         case LI_set_address:
10588           /* ??? Unfortunately, we have little choice here currently, and
10589              must always use the most general form.  GCC does not know the
10590              address delta itself, so we can't use DW_LNS_advance_pc.  Many
10591              ports do have length attributes which will give an upper bound
10592              on the address range.  We could perhaps use length attributes
10593              to determine when it is safe to use DW_LNS_fixed_advance_pc.  */
10594           ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
10595
10596           /* This can handle any delta.  This takes
10597              4+DWARF2_ADDR_SIZE bytes.  */
10598           dw2_asm_output_data (1, 0, "set address %s", line_label);
10599           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10600           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10601           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10602           break;
10603
10604         case LI_set_line:
10605           if (ent->val == current_line)
10606             {
10607               /* We still need to start a new row, so output a copy insn.  */
10608               dw2_asm_output_data (1, DW_LNS_copy,
10609                                    "copy line %u", current_line);
10610             }
10611           else
10612             {
10613               int line_offset = ent->val - current_line;
10614               int line_delta = line_offset - DWARF_LINE_BASE;
10615
10616               current_line = ent->val;
10617               if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10618                 {
10619                   /* This can handle deltas from -10 to 234, using the current
10620                      definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
10621                      This takes 1 byte.  */
10622                   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10623                                        "line %u", current_line);
10624                 }
10625               else
10626                 {
10627                   /* This can handle any delta.  This takes at least 4 bytes,
10628                      depending on the value being encoded.  */
10629                   dw2_asm_output_data (1, DW_LNS_advance_line,
10630                                        "advance to line %u", current_line);
10631                   dw2_asm_output_data_sleb128 (line_offset, NULL);
10632                   dw2_asm_output_data (1, DW_LNS_copy, NULL);
10633                 }
10634             }
10635           break;
10636
10637         case LI_set_file:
10638           dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
10639           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10640           break;
10641
10642         case LI_set_column:
10643           dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
10644           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10645           break;
10646
10647         case LI_negate_stmt:
10648           current_is_stmt = !current_is_stmt;
10649           dw2_asm_output_data (1, DW_LNS_negate_stmt,
10650                                "is_stmt %d", current_is_stmt);
10651           break;
10652
10653         case LI_set_prologue_end:
10654           dw2_asm_output_data (1, DW_LNS_set_prologue_end,
10655                                "set prologue end");
10656           break;
10657           
10658         case LI_set_epilogue_begin:
10659           dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
10660                                "set epilogue begin");
10661           break;
10662
10663         case LI_set_discriminator:
10664           dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
10665           dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
10666           dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
10667           dw2_asm_output_data_uleb128 (ent->val, NULL);
10668           break;
10669         }
10670     }
10671
10672   /* Emit debug info for the address of the end of the table.  */
10673   dw2_asm_output_data (1, 0, "set address %s", table->end_label);
10674   dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10675   dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10676   dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10677
10678   dw2_asm_output_data (1, 0, "end sequence");
10679   dw2_asm_output_data_uleb128 (1, NULL);
10680   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10681 }
10682
10683 /* Output the source line number correspondence information.  This
10684    information goes into the .debug_line section.  */
10685
10686 static void
10687 output_line_info (bool prologue_only)
10688 {
10689   char l1[20], l2[20], p1[20], p2[20];
10690   /* We don't support DWARFv5 line tables yet.  */
10691   int ver = dwarf_version < 5 ? dwarf_version : 4;
10692   bool saw_one = false;
10693   int opc;
10694
10695   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10696   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10697   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10698   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10699
10700   if (!XCOFF_DEBUGGING_INFO)
10701     {
10702       if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10703         dw2_asm_output_data (4, 0xffffffff,
10704           "Initial length escape value indicating 64-bit DWARF extension");
10705       dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10706                             "Length of Source Line Info");
10707     }
10708
10709   ASM_OUTPUT_LABEL (asm_out_file, l1);
10710
10711   dw2_asm_output_data (2, ver, "DWARF Version");
10712   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10713   ASM_OUTPUT_LABEL (asm_out_file, p1);
10714
10715   /* Define the architecture-dependent minimum instruction length (in bytes).
10716      In this implementation of DWARF, this field is used for information
10717      purposes only.  Since GCC generates assembly language, we have no
10718      a priori knowledge of how many instruction bytes are generated for each
10719      source line, and therefore can use only the DW_LNE_set_address and
10720      DW_LNS_fixed_advance_pc line information commands.  Accordingly, we fix
10721      this as '1', which is "correct enough" for all architectures,
10722      and don't let the target override.  */
10723   dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10724
10725   if (ver >= 4)
10726     dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10727                          "Maximum Operations Per Instruction");
10728   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10729                        "Default is_stmt_start flag");
10730   dw2_asm_output_data (1, DWARF_LINE_BASE,
10731                        "Line Base Value (Special Opcodes)");
10732   dw2_asm_output_data (1, DWARF_LINE_RANGE,
10733                        "Line Range Value (Special Opcodes)");
10734   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10735                        "Special Opcode Base");
10736
10737   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10738     {
10739       int n_op_args;
10740       switch (opc)
10741         {
10742         case DW_LNS_advance_pc:
10743         case DW_LNS_advance_line:
10744         case DW_LNS_set_file:
10745         case DW_LNS_set_column:
10746         case DW_LNS_fixed_advance_pc:
10747         case DW_LNS_set_isa:
10748           n_op_args = 1;
10749           break;
10750         default:
10751           n_op_args = 0;
10752           break;
10753         }
10754
10755       dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10756                            opc, n_op_args);
10757     }
10758
10759   /* Write out the information about the files we use.  */
10760   output_file_names ();
10761   ASM_OUTPUT_LABEL (asm_out_file, p2);
10762   if (prologue_only)
10763     {
10764       /* Output the marker for the end of the line number info.  */
10765       ASM_OUTPUT_LABEL (asm_out_file, l2);
10766       return;
10767     }
10768
10769   if (separate_line_info)
10770     {
10771       dw_line_info_table *table;
10772       size_t i;
10773
10774       FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10775         if (table->in_use)
10776           {
10777             output_one_line_info_table (table);
10778             saw_one = true;
10779           }
10780     }
10781   if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10782     {
10783       output_one_line_info_table (cold_text_section_line_info);
10784       saw_one = true;
10785     }
10786
10787   /* ??? Some Darwin linkers crash on a .debug_line section with no
10788      sequences.  Further, merely a DW_LNE_end_sequence entry is not
10789      sufficient -- the address column must also be initialized.
10790      Make sure to output at least one set_address/end_sequence pair,
10791      choosing .text since that section is always present.  */
10792   if (text_section_line_info->in_use || !saw_one)
10793     output_one_line_info_table (text_section_line_info);
10794
10795   /* Output the marker for the end of the line number info.  */
10796   ASM_OUTPUT_LABEL (asm_out_file, l2);
10797 }
10798 \f
10799 /* Given a pointer to a tree node for some base type, return a pointer to
10800    a DIE that describes the given type.
10801
10802    This routine must only be called for GCC type nodes that correspond to
10803    Dwarf base (fundamental) types.  */
10804
10805 static dw_die_ref
10806 base_type_die (tree type)
10807 {
10808   dw_die_ref base_type_result;
10809   enum dwarf_type encoding;
10810   bool fpt_used = false;
10811   struct fixed_point_type_info fpt_info;
10812   tree type_bias = NULL_TREE;
10813
10814   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10815     return 0;
10816
10817   /* If this is a subtype that should not be emitted as a subrange type,
10818      use the base type.  See subrange_type_for_debug_p.  */
10819   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10820     type = TREE_TYPE (type);
10821
10822   switch (TREE_CODE (type))
10823     {
10824     case INTEGER_TYPE:
10825       if ((dwarf_version >= 4 || !dwarf_strict)
10826           && TYPE_NAME (type)
10827           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10828           && DECL_IS_BUILTIN (TYPE_NAME (type))
10829           && DECL_NAME (TYPE_NAME (type)))
10830         {
10831           const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10832           if (strcmp (name, "char16_t") == 0
10833               || strcmp (name, "char32_t") == 0)
10834             {
10835               encoding = DW_ATE_UTF;
10836               break;
10837             }
10838         }
10839       if ((dwarf_version >= 3 || !dwarf_strict)
10840           && lang_hooks.types.get_fixed_point_type_info)
10841         {
10842           memset (&fpt_info, 0, sizeof (fpt_info));
10843           if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
10844             {
10845               fpt_used = true;
10846               encoding = ((TYPE_UNSIGNED (type))
10847                           ? DW_ATE_unsigned_fixed
10848                           : DW_ATE_signed_fixed);
10849               break;
10850             }
10851         }
10852       if (TYPE_STRING_FLAG (type))
10853         {
10854           if (TYPE_UNSIGNED (type))
10855             encoding = DW_ATE_unsigned_char;
10856           else
10857             encoding = DW_ATE_signed_char;
10858         }
10859       else if (TYPE_UNSIGNED (type))
10860         encoding = DW_ATE_unsigned;
10861       else
10862         encoding = DW_ATE_signed;
10863
10864       if (!dwarf_strict
10865           && lang_hooks.types.get_type_bias)
10866         type_bias = lang_hooks.types.get_type_bias (type);
10867       break;
10868
10869     case REAL_TYPE:
10870       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10871         {
10872           if (dwarf_version >= 3 || !dwarf_strict)
10873             encoding = DW_ATE_decimal_float;
10874           else
10875             encoding = DW_ATE_lo_user;
10876         }
10877       else
10878         encoding = DW_ATE_float;
10879       break;
10880
10881     case FIXED_POINT_TYPE:
10882       if (!(dwarf_version >= 3 || !dwarf_strict))
10883         encoding = DW_ATE_lo_user;
10884       else if (TYPE_UNSIGNED (type))
10885         encoding = DW_ATE_unsigned_fixed;
10886       else
10887         encoding = DW_ATE_signed_fixed;
10888       break;
10889
10890       /* Dwarf2 doesn't know anything about complex ints, so use
10891          a user defined type for it.  */
10892     case COMPLEX_TYPE:
10893       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10894         encoding = DW_ATE_complex_float;
10895       else
10896         encoding = DW_ATE_lo_user;
10897       break;
10898
10899     case BOOLEAN_TYPE:
10900       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
10901       encoding = DW_ATE_boolean;
10902       break;
10903
10904     default:
10905       /* No other TREE_CODEs are Dwarf fundamental types.  */
10906       gcc_unreachable ();
10907     }
10908
10909   base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10910
10911   add_AT_unsigned (base_type_result, DW_AT_byte_size,
10912                    int_size_in_bytes (type));
10913   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10914
10915   if (fpt_used)
10916     {
10917       switch (fpt_info.scale_factor_kind)
10918         {
10919         case fixed_point_scale_factor_binary:
10920           add_AT_int (base_type_result, DW_AT_binary_scale,
10921                       fpt_info.scale_factor.binary);
10922           break;
10923
10924         case fixed_point_scale_factor_decimal:
10925           add_AT_int (base_type_result, DW_AT_decimal_scale,
10926                       fpt_info.scale_factor.decimal);
10927           break;
10928
10929         case fixed_point_scale_factor_arbitrary:
10930           /* Arbitrary scale factors cannot be described in standard DWARF,
10931              yet.  */
10932           if (!dwarf_strict)
10933             {
10934               /* Describe the scale factor as a rational constant.  */
10935               const dw_die_ref scale_factor
10936                 = new_die (DW_TAG_constant, comp_unit_die (), type);
10937
10938               add_AT_unsigned (scale_factor, DW_AT_GNU_numerator,
10939                                fpt_info.scale_factor.arbitrary.numerator);
10940               add_AT_int (scale_factor, DW_AT_GNU_denominator,
10941                           fpt_info.scale_factor.arbitrary.denominator);
10942
10943               add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
10944             }
10945           break;
10946
10947         default:
10948           gcc_unreachable ();
10949         }
10950     }
10951   if (type_bias != NULL)
10952     add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
10953                      dw_scalar_form_constant
10954                      | dw_scalar_form_exprloc
10955                      | dw_scalar_form_reference,
10956                      NULL);
10957   add_pubtype (type, base_type_result);
10958
10959   return base_type_result;
10960 }
10961
10962 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
10963    named 'auto' in its type: return true for it, false otherwise.  */
10964
10965 static inline bool
10966 is_cxx_auto (tree type)
10967 {
10968   if (is_cxx ())
10969     {
10970       tree name = TYPE_IDENTIFIER (type);
10971       if (name == get_identifier ("auto")
10972           || name == get_identifier ("decltype(auto)"))
10973         return true;
10974     }
10975   return false;
10976 }
10977
10978 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10979    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
10980
10981 static inline int
10982 is_base_type (tree type)
10983 {
10984   switch (TREE_CODE (type))
10985     {
10986     case ERROR_MARK:
10987     case VOID_TYPE:
10988     case INTEGER_TYPE:
10989     case REAL_TYPE:
10990     case FIXED_POINT_TYPE:
10991     case COMPLEX_TYPE:
10992     case BOOLEAN_TYPE:
10993     case POINTER_BOUNDS_TYPE:
10994       return 1;
10995
10996     case ARRAY_TYPE:
10997     case RECORD_TYPE:
10998     case UNION_TYPE:
10999     case QUAL_UNION_TYPE:
11000     case ENUMERAL_TYPE:
11001     case FUNCTION_TYPE:
11002     case METHOD_TYPE:
11003     case POINTER_TYPE:
11004     case REFERENCE_TYPE:
11005     case NULLPTR_TYPE:
11006     case OFFSET_TYPE:
11007     case LANG_TYPE:
11008     case VECTOR_TYPE:
11009       return 0;
11010
11011     default:
11012       if (is_cxx_auto (type))
11013         return 0;
11014       gcc_unreachable ();
11015     }
11016
11017   return 0;
11018 }
11019
11020 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
11021    node, return the size in bits for the type if it is a constant, or else
11022    return the alignment for the type if the type's size is not constant, or
11023    else return BITS_PER_WORD if the type actually turns out to be an
11024    ERROR_MARK node.  */
11025
11026 static inline unsigned HOST_WIDE_INT
11027 simple_type_size_in_bits (const_tree type)
11028 {
11029   if (TREE_CODE (type) == ERROR_MARK)
11030     return BITS_PER_WORD;
11031   else if (TYPE_SIZE (type) == NULL_TREE)
11032     return 0;
11033   else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
11034     return tree_to_uhwi (TYPE_SIZE (type));
11035   else
11036     return TYPE_ALIGN (type);
11037 }
11038
11039 /* Similarly, but return an offset_int instead of UHWI.  */
11040
11041 static inline offset_int
11042 offset_int_type_size_in_bits (const_tree type)
11043 {
11044   if (TREE_CODE (type) == ERROR_MARK)
11045     return BITS_PER_WORD;
11046   else if (TYPE_SIZE (type) == NULL_TREE)
11047     return 0;
11048   else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
11049     return wi::to_offset (TYPE_SIZE (type));
11050   else
11051     return TYPE_ALIGN (type);
11052 }
11053
11054 /*  Given a pointer to a tree node for a subrange type, return a pointer
11055     to a DIE that describes the given type.  */
11056
11057 static dw_die_ref
11058 subrange_type_die (tree type, tree low, tree high, tree bias,
11059                    dw_die_ref context_die)
11060 {
11061   dw_die_ref subrange_die;
11062   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
11063
11064   if (context_die == NULL)
11065     context_die = comp_unit_die ();
11066
11067   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
11068
11069   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
11070     {
11071       /* The size of the subrange type and its base type do not match,
11072          so we need to generate a size attribute for the subrange type.  */
11073       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
11074     }
11075
11076   if (low)
11077     add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
11078   if (high)
11079     add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
11080   if (bias && !dwarf_strict)
11081     add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
11082                      dw_scalar_form_constant
11083                      | dw_scalar_form_exprloc
11084                      | dw_scalar_form_reference,
11085                      NULL);
11086
11087   return subrange_die;
11088 }
11089
11090 /* Returns the (const and/or volatile) cv_qualifiers associated with
11091    the decl node.  This will normally be augmented with the
11092    cv_qualifiers of the underlying type in add_type_attribute.  */
11093
11094 static int
11095 decl_quals (const_tree decl)
11096 {
11097   return ((TREE_READONLY (decl)
11098            ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
11099           | (TREE_THIS_VOLATILE (decl)
11100              ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
11101 }
11102
11103 /* Determine the TYPE whose qualifiers match the largest strict subset
11104    of the given TYPE_QUALS, and return its qualifiers.  Ignore all
11105    qualifiers outside QUAL_MASK.  */
11106
11107 static int
11108 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
11109 {
11110   tree t;
11111   int best_rank = 0, best_qual = 0, max_rank;
11112
11113   type_quals &= qual_mask;
11114   max_rank = popcount_hwi (type_quals) - 1;
11115
11116   for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
11117        t = TYPE_NEXT_VARIANT (t))
11118     {
11119       int q = TYPE_QUALS (t) & qual_mask;
11120
11121       if ((q & type_quals) == q && q != type_quals
11122           && check_base_type (t, type))
11123         {
11124           int rank = popcount_hwi (q);
11125
11126           if (rank > best_rank)
11127             {
11128               best_rank = rank;
11129               best_qual = q;
11130             }
11131         }
11132     }
11133
11134   return best_qual;
11135 }
11136
11137 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
11138    entry that chains various modifiers in front of the given type.  */
11139
11140 static dw_die_ref
11141 modified_type_die (tree type, int cv_quals, dw_die_ref context_die)
11142 {
11143   enum tree_code code = TREE_CODE (type);
11144   dw_die_ref mod_type_die;
11145   dw_die_ref sub_die = NULL;
11146   tree item_type = NULL;
11147   tree qualified_type;
11148   tree name, low, high;
11149   dw_die_ref mod_scope;
11150   /* Only these cv-qualifiers are currently handled.  */
11151   const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
11152                             | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
11153
11154   if (code == ERROR_MARK)
11155     return NULL;
11156
11157   if (lang_hooks.types.get_debug_type)
11158     {
11159       tree debug_type = lang_hooks.types.get_debug_type (type);
11160
11161       if (debug_type != NULL_TREE && debug_type != type)
11162         return modified_type_die (debug_type, cv_quals, context_die);
11163     }
11164
11165   cv_quals &= cv_qual_mask;
11166
11167   /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
11168      tag modifier (and not an attribute) old consumers won't be able
11169      to handle it.  */
11170   if (dwarf_version < 3)
11171     cv_quals &= ~TYPE_QUAL_RESTRICT;
11172
11173   /* Likewise for DW_TAG_atomic_type for DWARFv5.  */
11174   if (dwarf_version < 5)
11175     cv_quals &= ~TYPE_QUAL_ATOMIC;
11176
11177   /* See if we already have the appropriately qualified variant of
11178      this type.  */
11179   qualified_type = get_qualified_type (type, cv_quals);
11180
11181   if (qualified_type == sizetype
11182       && TYPE_NAME (qualified_type)
11183       && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
11184     {
11185       tree t = TREE_TYPE (TYPE_NAME (qualified_type));
11186
11187       gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
11188                            && TYPE_PRECISION (t)
11189                            == TYPE_PRECISION (qualified_type)
11190                            && TYPE_UNSIGNED (t)
11191                            == TYPE_UNSIGNED (qualified_type));
11192       qualified_type = t;
11193     }
11194
11195   /* If we do, then we can just use its DIE, if it exists.  */
11196   if (qualified_type)
11197     {
11198       mod_type_die = lookup_type_die (qualified_type);
11199       if (mod_type_die)
11200         return mod_type_die;
11201     }
11202
11203   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
11204
11205   /* Handle C typedef types.  */
11206   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
11207       && !DECL_ARTIFICIAL (name))
11208     {
11209       tree dtype = TREE_TYPE (name);
11210
11211       if (qualified_type == dtype)
11212         {
11213           /* For a named type, use the typedef.  */
11214           gen_type_die (qualified_type, context_die);
11215           return lookup_type_die (qualified_type);
11216         }
11217       else
11218         {
11219           int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
11220           dquals &= cv_qual_mask;
11221           if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
11222               || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
11223             /* cv-unqualified version of named type.  Just use
11224                the unnamed type to which it refers.  */
11225             return modified_type_die (DECL_ORIGINAL_TYPE (name),
11226                                       cv_quals, context_die);
11227           /* Else cv-qualified version of named type; fall through.  */
11228         }
11229     }
11230
11231   mod_scope = scope_die_for (type, context_die);
11232
11233   if (cv_quals)
11234     {
11235       struct qual_info { int q; enum dwarf_tag t; };
11236       static const struct qual_info qual_info[] =
11237         {
11238           { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type },
11239           { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
11240           { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
11241           { TYPE_QUAL_CONST, DW_TAG_const_type },
11242         };
11243       int sub_quals;
11244       unsigned i;
11245
11246       /* Determine a lesser qualified type that most closely matches
11247          this one.  Then generate DW_TAG_* entries for the remaining
11248          qualifiers.  */
11249       sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
11250                                                   cv_qual_mask);
11251       mod_type_die = modified_type_die (type, sub_quals, context_die);
11252
11253       for (i = 0; i < sizeof (qual_info) / sizeof (qual_info[0]); i++)
11254         if (qual_info[i].q & cv_quals & ~sub_quals)
11255           {
11256             dw_die_ref d = new_die (qual_info[i].t, mod_scope, type);
11257             if (mod_type_die)
11258               add_AT_die_ref (d, DW_AT_type, mod_type_die);
11259             mod_type_die = d;
11260           }
11261     }
11262   else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
11263     {
11264       dwarf_tag tag = DW_TAG_pointer_type;
11265       if (code == REFERENCE_TYPE)
11266         {
11267           if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
11268             tag = DW_TAG_rvalue_reference_type;
11269           else
11270             tag = DW_TAG_reference_type;
11271         }
11272       mod_type_die = new_die (tag, mod_scope, type);
11273
11274       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
11275                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
11276       item_type = TREE_TYPE (type);
11277
11278       addr_space_t as = TYPE_ADDR_SPACE (item_type);
11279       if (!ADDR_SPACE_GENERIC_P (as))
11280         {
11281           int action = targetm.addr_space.debug (as);
11282           if (action >= 0)
11283             {
11284               /* Positive values indicate an address_class.  */
11285               add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
11286             }
11287           else
11288             {
11289               /* Negative values indicate an (inverted) segment base reg.  */
11290               dw_loc_descr_ref d
11291                 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
11292               add_AT_loc (mod_type_die, DW_AT_segment, d);
11293             }
11294         }
11295     }
11296   else if (code == INTEGER_TYPE
11297            && TREE_TYPE (type) != NULL_TREE
11298            && subrange_type_for_debug_p (type, &low, &high))
11299     {
11300       tree bias = NULL_TREE;
11301       if (lang_hooks.types.get_type_bias)
11302         bias = lang_hooks.types.get_type_bias (type);
11303       mod_type_die = subrange_type_die (type, low, high, bias, context_die);
11304       item_type = TREE_TYPE (type);
11305     }
11306   else if (is_base_type (type))
11307     mod_type_die = base_type_die (type);
11308   else
11309     {
11310       gen_type_die (type, context_die);
11311
11312       /* We have to get the type_main_variant here (and pass that to the
11313          `lookup_type_die' routine) because the ..._TYPE node we have
11314          might simply be a *copy* of some original type node (where the
11315          copy was created to help us keep track of typedef names) and
11316          that copy might have a different TYPE_UID from the original
11317          ..._TYPE node.  */
11318       if (TREE_CODE (type) != VECTOR_TYPE)
11319         return lookup_type_die (type_main_variant (type));
11320       else
11321         /* Vectors have the debugging information in the type,
11322            not the main variant.  */
11323         return lookup_type_die (type);
11324     }
11325
11326   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
11327      don't output a DW_TAG_typedef, since there isn't one in the
11328      user's program; just attach a DW_AT_name to the type.
11329      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
11330      if the base type already has the same name.  */
11331   if (name
11332       && ((TREE_CODE (name) != TYPE_DECL
11333            && (qualified_type == TYPE_MAIN_VARIANT (type)
11334                || (cv_quals == TYPE_UNQUALIFIED)))
11335           || (TREE_CODE (name) == TYPE_DECL
11336               && TREE_TYPE (name) == qualified_type
11337               && DECL_NAME (name))))
11338     {
11339       if (TREE_CODE (name) == TYPE_DECL)
11340         /* Could just call add_name_and_src_coords_attributes here,
11341            but since this is a builtin type it doesn't have any
11342            useful source coordinates anyway.  */
11343         name = DECL_NAME (name);
11344       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
11345     }
11346   /* This probably indicates a bug.  */
11347   else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
11348     {
11349       name = TYPE_IDENTIFIER (type);
11350       add_name_attribute (mod_type_die,
11351                           name ? IDENTIFIER_POINTER (name) : "__unknown__");
11352     }
11353
11354   if (qualified_type)
11355     equate_type_number_to_die (qualified_type, mod_type_die);
11356
11357   if (item_type)
11358     /* We must do this after the equate_type_number_to_die call, in case
11359        this is a recursive type.  This ensures that the modified_type_die
11360        recursion will terminate even if the type is recursive.  Recursive
11361        types are possible in Ada.  */
11362     sub_die = modified_type_die (item_type,
11363                                  TYPE_QUALS_NO_ADDR_SPACE (item_type),
11364                                  context_die);
11365
11366   if (sub_die != NULL)
11367     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
11368
11369   add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
11370   if (TYPE_ARTIFICIAL (type))
11371     add_AT_flag (mod_type_die, DW_AT_artificial, 1);
11372
11373   return mod_type_die;
11374 }
11375
11376 /* Generate DIEs for the generic parameters of T.
11377    T must be either a generic type or a generic function.
11378    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
11379
11380 static void
11381 gen_generic_params_dies (tree t)
11382 {
11383   tree parms, args;
11384   int parms_num, i;
11385   dw_die_ref die = NULL;
11386   int non_default;
11387
11388   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
11389     return;
11390
11391   if (TYPE_P (t))
11392     die = lookup_type_die (t);
11393   else if (DECL_P (t))
11394     die = lookup_decl_die (t);
11395
11396   gcc_assert (die);
11397
11398   parms = lang_hooks.get_innermost_generic_parms (t);
11399   if (!parms)
11400     /* T has no generic parameter. It means T is neither a generic type
11401        or function. End of story.  */
11402     return;
11403
11404   parms_num = TREE_VEC_LENGTH (parms);
11405   args = lang_hooks.get_innermost_generic_args (t);
11406   if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
11407     non_default = int_cst_value (TREE_CHAIN (args));
11408   else
11409     non_default = TREE_VEC_LENGTH (args);
11410   for (i = 0; i < parms_num; i++)
11411     {
11412       tree parm, arg, arg_pack_elems;
11413       dw_die_ref parm_die;
11414
11415       parm = TREE_VEC_ELT (parms, i);
11416       arg = TREE_VEC_ELT (args, i);
11417       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
11418       gcc_assert (parm && TREE_VALUE (parm) && arg);
11419
11420       if (parm && TREE_VALUE (parm) && arg)
11421         {
11422           /* If PARM represents a template parameter pack,
11423              emit a DW_TAG_GNU_template_parameter_pack DIE, followed
11424              by DW_TAG_template_*_parameter DIEs for the argument
11425              pack elements of ARG. Note that ARG would then be
11426              an argument pack.  */
11427           if (arg_pack_elems)
11428             parm_die = template_parameter_pack_die (TREE_VALUE (parm),
11429                                                     arg_pack_elems,
11430                                                     die);
11431           else
11432             parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
11433                                               true /* emit name */, die);
11434           if (i >= non_default)
11435             add_AT_flag (parm_die, DW_AT_default_value, 1);
11436         }
11437     }
11438 }
11439
11440 /* Create and return a DIE for PARM which should be
11441    the representation of a generic type parameter.
11442    For instance, in the C++ front end, PARM would be a template parameter.
11443    ARG is the argument to PARM.
11444    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
11445    name of the PARM.
11446    PARENT_DIE is the parent DIE which the new created DIE should be added to,
11447    as a child node.  */
11448
11449 static dw_die_ref
11450 generic_parameter_die (tree parm, tree arg,
11451                        bool emit_name_p,
11452                        dw_die_ref parent_die)
11453 {
11454   dw_die_ref tmpl_die = NULL;
11455   const char *name = NULL;
11456
11457   if (!parm || !DECL_NAME (parm) || !arg)
11458     return NULL;
11459
11460   /* We support non-type generic parameters and arguments,
11461      type generic parameters and arguments, as well as
11462      generic generic parameters (a.k.a. template template parameters in C++)
11463      and arguments.  */
11464   if (TREE_CODE (parm) == PARM_DECL)
11465     /* PARM is a nontype generic parameter  */
11466     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
11467   else if (TREE_CODE (parm) == TYPE_DECL)
11468     /* PARM is a type generic parameter.  */
11469     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
11470   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
11471     /* PARM is a generic generic parameter.
11472        Its DIE is a GNU extension. It shall have a
11473        DW_AT_name attribute to represent the name of the template template
11474        parameter, and a DW_AT_GNU_template_name attribute to represent the
11475        name of the template template argument.  */
11476     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
11477                         parent_die, parm);
11478   else
11479     gcc_unreachable ();
11480
11481   if (tmpl_die)
11482     {
11483       tree tmpl_type;
11484
11485       /* If PARM is a generic parameter pack, it means we are
11486          emitting debug info for a template argument pack element.
11487          In other terms, ARG is a template argument pack element.
11488          In that case, we don't emit any DW_AT_name attribute for
11489          the die.  */
11490       if (emit_name_p)
11491         {
11492           name = IDENTIFIER_POINTER (DECL_NAME (parm));
11493           gcc_assert (name);
11494           add_AT_string (tmpl_die, DW_AT_name, name);
11495         }
11496
11497       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
11498         {
11499           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
11500              TMPL_DIE should have a child DW_AT_type attribute that is set
11501              to the type of the argument to PARM, which is ARG.
11502              If PARM is a type generic parameter, TMPL_DIE should have a
11503              child DW_AT_type that is set to ARG.  */
11504           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
11505           add_type_attribute (tmpl_die, tmpl_type,
11506                               (TREE_THIS_VOLATILE (tmpl_type)
11507                                ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
11508                               parent_die);
11509         }
11510       else
11511         {
11512           /* So TMPL_DIE is a DIE representing a
11513              a generic generic template parameter, a.k.a template template
11514              parameter in C++ and arg is a template.  */
11515
11516           /* The DW_AT_GNU_template_name attribute of the DIE must be set
11517              to the name of the argument.  */
11518           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
11519           if (name)
11520             add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
11521         }
11522
11523       if (TREE_CODE (parm) == PARM_DECL)
11524         /* So PARM is a non-type generic parameter.
11525            DWARF3 5.6.8 says we must set a DW_AT_const_value child
11526            attribute of TMPL_DIE which value represents the value
11527            of ARG.
11528            We must be careful here:
11529            The value of ARG might reference some function decls.
11530            We might currently be emitting debug info for a generic
11531            type and types are emitted before function decls, we don't
11532            know if the function decls referenced by ARG will actually be
11533            emitted after cgraph computations.
11534            So must defer the generation of the DW_AT_const_value to
11535            after cgraph is ready.  */
11536         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
11537     }
11538
11539   return tmpl_die;
11540 }
11541
11542 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
11543    PARM_PACK must be a template parameter pack. The returned DIE
11544    will be child DIE of PARENT_DIE.  */
11545
11546 static dw_die_ref
11547 template_parameter_pack_die (tree parm_pack,
11548                              tree parm_pack_args,
11549                              dw_die_ref parent_die)
11550 {
11551   dw_die_ref die;
11552   int j;
11553
11554   gcc_assert (parent_die && parm_pack);
11555
11556   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
11557   add_name_and_src_coords_attributes (die, parm_pack);
11558   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
11559     generic_parameter_die (parm_pack,
11560                            TREE_VEC_ELT (parm_pack_args, j),
11561                            false /* Don't emit DW_AT_name */,
11562                            die);
11563   return die;
11564 }
11565
11566 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
11567    an enumerated type.  */
11568
11569 static inline int
11570 type_is_enum (const_tree type)
11571 {
11572   return TREE_CODE (type) == ENUMERAL_TYPE;
11573 }
11574
11575 /* Return the DBX register number described by a given RTL node.  */
11576
11577 static unsigned int
11578 dbx_reg_number (const_rtx rtl)
11579 {
11580   unsigned regno = REGNO (rtl);
11581
11582   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
11583
11584 #ifdef LEAF_REG_REMAP
11585   if (crtl->uses_only_leaf_regs)
11586     {
11587       int leaf_reg = LEAF_REG_REMAP (regno);
11588       if (leaf_reg != -1)
11589         regno = (unsigned) leaf_reg;
11590     }
11591 #endif
11592
11593   regno = DBX_REGISTER_NUMBER (regno);
11594   gcc_assert (regno != INVALID_REGNUM);
11595   return regno;
11596 }
11597
11598 /* Optionally add a DW_OP_piece term to a location description expression.
11599    DW_OP_piece is only added if the location description expression already
11600    doesn't end with DW_OP_piece.  */
11601
11602 static void
11603 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
11604 {
11605   dw_loc_descr_ref loc;
11606
11607   if (*list_head != NULL)
11608     {
11609       /* Find the end of the chain.  */
11610       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
11611         ;
11612
11613       if (loc->dw_loc_opc != DW_OP_piece)
11614         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
11615     }
11616 }
11617
11618 /* Return a location descriptor that designates a machine register or
11619    zero if there is none.  */
11620
11621 static dw_loc_descr_ref
11622 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
11623 {
11624   rtx regs;
11625
11626   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
11627     return 0;
11628
11629   /* We only use "frame base" when we're sure we're talking about the
11630      post-prologue local stack frame.  We do this by *not* running
11631      register elimination until this point, and recognizing the special
11632      argument pointer and soft frame pointer rtx's.
11633      Use DW_OP_fbreg offset DW_OP_stack_value in this case.  */
11634   if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
11635       && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
11636     {
11637       dw_loc_descr_ref result = NULL;
11638
11639       if (dwarf_version >= 4 || !dwarf_strict)
11640         {
11641           result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
11642                                        initialized);
11643           if (result)
11644             add_loc_descr (&result,
11645                            new_loc_descr (DW_OP_stack_value, 0, 0));
11646         }
11647       return result;
11648     }
11649
11650   regs = targetm.dwarf_register_span (rtl);
11651
11652   if (REG_NREGS (rtl) > 1 || regs)
11653     return multiple_reg_loc_descriptor (rtl, regs, initialized);
11654   else
11655     {
11656       unsigned int dbx_regnum = dbx_reg_number (rtl);
11657       if (dbx_regnum == IGNORED_DWARF_REGNUM)
11658         return 0;
11659       return one_reg_loc_descriptor (dbx_regnum, initialized);
11660     }
11661 }
11662
11663 /* Return a location descriptor that designates a machine register for
11664    a given hard register number.  */
11665
11666 static dw_loc_descr_ref
11667 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
11668 {
11669   dw_loc_descr_ref reg_loc_descr;
11670
11671   if (regno <= 31)
11672     reg_loc_descr
11673       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
11674   else
11675     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
11676
11677   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11678     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11679
11680   return reg_loc_descr;
11681 }
11682
11683 /* Given an RTL of a register, return a location descriptor that
11684    designates a value that spans more than one register.  */
11685
11686 static dw_loc_descr_ref
11687 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
11688                              enum var_init_status initialized)
11689 {
11690   int size, i;
11691   dw_loc_descr_ref loc_result = NULL;
11692
11693   /* Simple, contiguous registers.  */
11694   if (regs == NULL_RTX)
11695     {
11696       unsigned reg = REGNO (rtl);
11697       int nregs;
11698
11699 #ifdef LEAF_REG_REMAP
11700       if (crtl->uses_only_leaf_regs)
11701         {
11702           int leaf_reg = LEAF_REG_REMAP (reg);
11703           if (leaf_reg != -1)
11704             reg = (unsigned) leaf_reg;
11705         }
11706 #endif
11707
11708       gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
11709       nregs = REG_NREGS (rtl);
11710
11711       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
11712
11713       loc_result = NULL;
11714       while (nregs--)
11715         {
11716           dw_loc_descr_ref t;
11717
11718           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
11719                                       VAR_INIT_STATUS_INITIALIZED);
11720           add_loc_descr (&loc_result, t);
11721           add_loc_descr_op_piece (&loc_result, size);
11722           ++reg;
11723         }
11724       return loc_result;
11725     }
11726
11727   /* Now onto stupid register sets in non contiguous locations.  */
11728
11729   gcc_assert (GET_CODE (regs) == PARALLEL);
11730
11731   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
11732   loc_result = NULL;
11733
11734   for (i = 0; i < XVECLEN (regs, 0); ++i)
11735     {
11736       dw_loc_descr_ref t;
11737
11738       t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
11739                                   VAR_INIT_STATUS_INITIALIZED);
11740       add_loc_descr (&loc_result, t);
11741       add_loc_descr_op_piece (&loc_result, size);
11742     }
11743
11744   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11745     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11746   return loc_result;
11747 }
11748
11749 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
11750
11751 /* Return a location descriptor that designates a constant i,
11752    as a compound operation from constant (i >> shift), constant shift
11753    and DW_OP_shl.  */
11754
11755 static dw_loc_descr_ref
11756 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11757 {
11758   dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
11759   add_loc_descr (&ret, int_loc_descriptor (shift));
11760   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11761   return ret;
11762 }
11763
11764 /* Return a location descriptor that designates a constant.  */
11765
11766 static dw_loc_descr_ref
11767 int_loc_descriptor (HOST_WIDE_INT i)
11768 {
11769   enum dwarf_location_atom op;
11770
11771   /* Pick the smallest representation of a constant, rather than just
11772      defaulting to the LEB encoding.  */
11773   if (i >= 0)
11774     {
11775       int clz = clz_hwi (i);
11776       int ctz = ctz_hwi (i);
11777       if (i <= 31)
11778         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
11779       else if (i <= 0xff)
11780         op = DW_OP_const1u;
11781       else if (i <= 0xffff)
11782         op = DW_OP_const2u;
11783       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11784                && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11785         /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
11786            DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
11787            while DW_OP_const4u is 5 bytes.  */
11788         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
11789       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11790                && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11791         /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
11792            while DW_OP_const4u is 5 bytes.  */
11793         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11794       else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11795         op = DW_OP_const4u;
11796       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11797                && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11798         /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
11799            while DW_OP_constu of constant >= 0x100000000 takes at least
11800            6 bytes.  */
11801         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11802       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11803                && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
11804                   >= HOST_BITS_PER_WIDE_INT)
11805         /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
11806            DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
11807            while DW_OP_constu takes in this case at least 6 bytes.  */
11808         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
11809       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11810                && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11811                && size_of_uleb128 (i) > 6)
11812         /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes.  */
11813         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
11814       else
11815         op = DW_OP_constu;
11816     }
11817   else
11818     {
11819       if (i >= -0x80)
11820         op = DW_OP_const1s;
11821       else if (i >= -0x8000)
11822         op = DW_OP_const2s;
11823       else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11824         {
11825           if (size_of_int_loc_descriptor (i) < 5)
11826             {
11827               dw_loc_descr_ref ret = int_loc_descriptor (-i);
11828               add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11829               return ret;
11830             }
11831           op = DW_OP_const4s;
11832         }
11833       else
11834         {
11835           if (size_of_int_loc_descriptor (i)
11836               < (unsigned long) 1 + size_of_sleb128 (i))
11837             {
11838               dw_loc_descr_ref ret = int_loc_descriptor (-i);
11839               add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11840               return ret;
11841             }
11842           op = DW_OP_consts;
11843         }
11844     }
11845
11846   return new_loc_descr (op, i, 0);
11847 }
11848
11849 /* Likewise, for unsigned constants.  */
11850
11851 static dw_loc_descr_ref
11852 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
11853 {
11854   const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
11855   const unsigned HOST_WIDE_INT max_uint
11856     = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
11857
11858   /* If possible, use the clever signed constants handling.  */
11859   if (i <= max_int)
11860     return int_loc_descriptor ((HOST_WIDE_INT) i);
11861
11862   /* Here, we are left with positive numbers that cannot be represented as
11863      HOST_WIDE_INT, i.e.:
11864          max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
11865
11866      Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
11867      whereas may be better to output a negative integer: thanks to integer
11868      wrapping, we know that:
11869          x = x - 2 ** DWARF2_ADDR_SIZE
11870            = x - 2 * (max (HOST_WIDE_INT) + 1)
11871      So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
11872      small negative integers.  Let's try that in cases it will clearly improve
11873      the encoding: there is no gain turning DW_OP_const4u into
11874      DW_OP_const4s.  */
11875   if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
11876       && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
11877           || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
11878     {
11879       const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
11880
11881       /* Now, -1 <  first_shift <= max (HOST_WIDE_INT)
11882          i.e.  0 <= first_shift <= max (HOST_WIDE_INT).  */
11883       const HOST_WIDE_INT second_shift
11884         = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
11885
11886       /* So we finally have:
11887               -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
11888          i.e.  min (HOST_WIDE_INT)     <= second_shift <  0.  */
11889       return int_loc_descriptor (second_shift);
11890     }
11891
11892   /* Last chance: fallback to a simple constant operation.  */
11893   return new_loc_descr
11894      ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11895       ? DW_OP_const4u
11896       : DW_OP_const8u,
11897       i, 0);
11898 }
11899
11900 /* Generate and return a location description that computes the unsigned
11901    comparison of the two stack top entries (a OP b where b is the top-most
11902    entry and a is the second one).  The KIND of comparison can be LT_EXPR,
11903    LE_EXPR, GT_EXPR or GE_EXPR.  */
11904
11905 static dw_loc_descr_ref
11906 uint_comparison_loc_list (enum tree_code kind)
11907 {
11908   enum dwarf_location_atom op, flip_op;
11909   dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
11910
11911   switch (kind)
11912     {
11913     case LT_EXPR:
11914       op = DW_OP_lt;
11915       break;
11916     case LE_EXPR:
11917       op = DW_OP_le;
11918       break;
11919     case GT_EXPR:
11920       op = DW_OP_gt;
11921       break;
11922     case GE_EXPR:
11923       op = DW_OP_ge;
11924       break;
11925     default:
11926       gcc_unreachable ();
11927     }
11928
11929   bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11930   jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
11931
11932   /* Until DWARFv4, operations all work on signed integers.  It is nevertheless
11933      possible to perform unsigned comparisons: we just have to distinguish
11934      three cases:
11935
11936        1. when a and b have the same sign (as signed integers); then we should
11937           return: a OP(signed) b;
11938
11939        2. when a is a negative signed integer while b is a positive one, then a
11940           is a greater unsigned integer than b; likewise when a and b's roles
11941           are flipped.
11942
11943      So first, compare the sign of the two operands.  */
11944   ret = new_loc_descr (DW_OP_over, 0, 0);
11945   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11946   add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
11947   /* If they have different signs (i.e. they have different sign bits), then
11948      the stack top value has now the sign bit set and thus it's smaller than
11949      zero.  */
11950   add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
11951   add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
11952   add_loc_descr (&ret, bra_node);
11953
11954   /* We are in case 1.  At this point, we know both operands have the same
11955      sign, to it's safe to use the built-in signed comparison.  */
11956   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11957   add_loc_descr (&ret, jmp_node);
11958
11959   /* We are in case 2.  Here, we know both operands do not have the same sign,
11960      so we have to flip the signed comparison.  */
11961   flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
11962   tmp = new_loc_descr (flip_op, 0, 0);
11963   bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11964   bra_node->dw_loc_oprnd1.v.val_loc = tmp;
11965   add_loc_descr (&ret, tmp);
11966
11967   /* This dummy operation is necessary to make the two branches join.  */
11968   tmp = new_loc_descr (DW_OP_nop, 0, 0);
11969   jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11970   jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
11971   add_loc_descr (&ret, tmp);
11972
11973   return ret;
11974 }
11975
11976 /* Likewise, but takes the location description lists (might be destructive on
11977    them).  Return NULL if either is NULL or if concatenation fails.  */
11978
11979 static dw_loc_list_ref
11980 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
11981                                enum tree_code kind)
11982 {
11983   if (left == NULL || right == NULL)
11984     return NULL;
11985
11986   add_loc_list (&left, right);
11987   if (left == NULL)
11988     return NULL;
11989
11990   add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
11991   return left;
11992 }
11993
11994 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
11995    without actually allocating it.  */
11996
11997 static unsigned long
11998 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11999 {
12000   return size_of_int_loc_descriptor (i >> shift)
12001          + size_of_int_loc_descriptor (shift)
12002          + 1;
12003 }
12004
12005 /* Return size_of_locs (int_loc_descriptor (i)) without
12006    actually allocating it.  */
12007
12008 static unsigned long
12009 size_of_int_loc_descriptor (HOST_WIDE_INT i)
12010 {
12011   unsigned long s;
12012
12013   if (i >= 0)
12014     {
12015       int clz, ctz;
12016       if (i <= 31)
12017         return 1;
12018       else if (i <= 0xff)
12019         return 2;
12020       else if (i <= 0xffff)
12021         return 3;
12022       clz = clz_hwi (i);
12023       ctz = ctz_hwi (i);
12024       if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
12025           && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
12026         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
12027                                                     - clz - 5);
12028       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
12029                && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
12030         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
12031                                                     - clz - 8);
12032       else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
12033         return 5;
12034       s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
12035       if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
12036           && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
12037         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
12038                                                     - clz - 8);
12039       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
12040                && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
12041         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
12042                                                     - clz - 16);
12043       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
12044                && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
12045                && s > 6)
12046         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
12047                                                     - clz - 32);
12048       else
12049         return 1 + s;
12050     }
12051   else
12052     {
12053       if (i >= -0x80)
12054         return 2;
12055       else if (i >= -0x8000)
12056         return 3;
12057       else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
12058         {
12059           if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
12060             {
12061               s = size_of_int_loc_descriptor (-i) + 1;
12062               if (s < 5)
12063                 return s;
12064             }
12065           return 5;
12066         }
12067       else
12068         {
12069           unsigned long r = 1 + size_of_sleb128 (i);
12070           if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
12071             {
12072               s = size_of_int_loc_descriptor (-i) + 1;
12073               if (s < r)
12074                 return s;
12075             }
12076           return r;
12077         }
12078     }
12079 }
12080
12081 /* Return loc description representing "address" of integer value.
12082    This can appear only as toplevel expression.  */
12083
12084 static dw_loc_descr_ref
12085 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
12086 {
12087   int litsize;
12088   dw_loc_descr_ref loc_result = NULL;
12089
12090   if (!(dwarf_version >= 4 || !dwarf_strict))
12091     return NULL;
12092
12093   litsize = size_of_int_loc_descriptor (i);
12094   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
12095      is more compact.  For DW_OP_stack_value we need:
12096      litsize + 1 (DW_OP_stack_value)
12097      and for DW_OP_implicit_value:
12098      1 (DW_OP_implicit_value) + 1 (length) + size.  */
12099   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
12100     {
12101       loc_result = int_loc_descriptor (i);
12102       add_loc_descr (&loc_result,
12103                      new_loc_descr (DW_OP_stack_value, 0, 0));
12104       return loc_result;
12105     }
12106
12107   loc_result = new_loc_descr (DW_OP_implicit_value,
12108                               size, 0);
12109   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12110   loc_result->dw_loc_oprnd2.v.val_int = i;
12111   return loc_result;
12112 }
12113
12114 /* Return a location descriptor that designates a base+offset location.  */
12115
12116 static dw_loc_descr_ref
12117 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
12118                  enum var_init_status initialized)
12119 {
12120   unsigned int regno;
12121   dw_loc_descr_ref result;
12122   dw_fde_ref fde = cfun->fde;
12123
12124   /* We only use "frame base" when we're sure we're talking about the
12125      post-prologue local stack frame.  We do this by *not* running
12126      register elimination until this point, and recognizing the special
12127      argument pointer and soft frame pointer rtx's.  */
12128   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
12129     {
12130       rtx elim = (ira_use_lra_p
12131                   ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
12132                   : eliminate_regs (reg, VOIDmode, NULL_RTX));
12133
12134       if (elim != reg)
12135         {
12136           if (GET_CODE (elim) == PLUS)
12137             {
12138               offset += INTVAL (XEXP (elim, 1));
12139               elim = XEXP (elim, 0);
12140             }
12141           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
12142                        && (elim == hard_frame_pointer_rtx
12143                            || elim == stack_pointer_rtx))
12144                       || elim == (frame_pointer_needed
12145                                   ? hard_frame_pointer_rtx
12146                                   : stack_pointer_rtx));
12147
12148           /* If drap register is used to align stack, use frame
12149              pointer + offset to access stack variables.  If stack
12150              is aligned without drap, use stack pointer + offset to
12151              access stack variables.  */
12152           if (crtl->stack_realign_tried
12153               && reg == frame_pointer_rtx)
12154             {
12155               int base_reg
12156                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
12157                                       ? HARD_FRAME_POINTER_REGNUM
12158                                       : REGNO (elim));
12159               return new_reg_loc_descr (base_reg, offset);
12160             }
12161
12162           gcc_assert (frame_pointer_fb_offset_valid);
12163           offset += frame_pointer_fb_offset;
12164           return new_loc_descr (DW_OP_fbreg, offset, 0);
12165         }
12166     }
12167
12168   regno = REGNO (reg);
12169 #ifdef LEAF_REG_REMAP
12170   if (crtl->uses_only_leaf_regs)
12171     {
12172       int leaf_reg = LEAF_REG_REMAP (regno);
12173       if (leaf_reg != -1)
12174         regno = (unsigned) leaf_reg;
12175     }
12176 #endif
12177   regno = DWARF_FRAME_REGNUM (regno);
12178
12179   if (!optimize && fde
12180       && (fde->drap_reg == regno || fde->vdrap_reg == regno))
12181     {
12182       /* Use cfa+offset to represent the location of arguments passed
12183          on the stack when drap is used to align stack.
12184          Only do this when not optimizing, for optimized code var-tracking
12185          is supposed to track where the arguments live and the register
12186          used as vdrap or drap in some spot might be used for something
12187          else in other part of the routine.  */
12188       return new_loc_descr (DW_OP_fbreg, offset, 0);
12189     }
12190
12191   if (regno <= 31)
12192     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
12193                             offset, 0);
12194   else
12195     result = new_loc_descr (DW_OP_bregx, regno, offset);
12196
12197   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
12198     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12199
12200   return result;
12201 }
12202
12203 /* Return true if this RTL expression describes a base+offset calculation.  */
12204
12205 static inline int
12206 is_based_loc (const_rtx rtl)
12207 {
12208   return (GET_CODE (rtl) == PLUS
12209           && ((REG_P (XEXP (rtl, 0))
12210                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
12211                && CONST_INT_P (XEXP (rtl, 1)))));
12212 }
12213
12214 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
12215    failed.  */
12216
12217 static dw_loc_descr_ref
12218 tls_mem_loc_descriptor (rtx mem)
12219 {
12220   tree base;
12221   dw_loc_descr_ref loc_result;
12222
12223   if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
12224     return NULL;
12225
12226   base = get_base_address (MEM_EXPR (mem));
12227   if (base == NULL
12228       || TREE_CODE (base) != VAR_DECL
12229       || !DECL_THREAD_LOCAL_P (base))
12230     return NULL;
12231
12232   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
12233   if (loc_result == NULL)
12234     return NULL;
12235
12236   if (MEM_OFFSET (mem))
12237     loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
12238
12239   return loc_result;
12240 }
12241
12242 /* Output debug info about reason why we failed to expand expression as dwarf
12243    expression.  */
12244
12245 static void
12246 expansion_failed (tree expr, rtx rtl, char const *reason)
12247 {
12248   if (dump_file && (dump_flags & TDF_DETAILS))
12249     {
12250       fprintf (dump_file, "Failed to expand as dwarf: ");
12251       if (expr)
12252         print_generic_expr (dump_file, expr, dump_flags);
12253       if (rtl)
12254         {
12255           fprintf (dump_file, "\n");
12256           print_rtl (dump_file, rtl);
12257         }
12258       fprintf (dump_file, "\nReason: %s\n", reason);
12259     }
12260 }
12261
12262 /* Helper function for const_ok_for_output.  */
12263
12264 static bool
12265 const_ok_for_output_1 (rtx rtl)
12266 {
12267   if (GET_CODE (rtl) == UNSPEC)
12268     {
12269       /* If delegitimize_address couldn't do anything with the UNSPEC, assume
12270          we can't express it in the debug info.  */
12271       /* Don't complain about TLS UNSPECs, those are just too hard to
12272          delegitimize.  Note this could be a non-decl SYMBOL_REF such as
12273          one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
12274          rather than DECL_THREAD_LOCAL_P is not just an optimization.  */
12275       if (flag_checking
12276           && (XVECLEN (rtl, 0) == 0
12277               || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
12278               || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
12279         inform (current_function_decl
12280                 ? DECL_SOURCE_LOCATION (current_function_decl)
12281                 : UNKNOWN_LOCATION,
12282 #if NUM_UNSPEC_VALUES > 0
12283                 "non-delegitimized UNSPEC %s (%d) found in variable location",
12284                 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
12285                  ? unspec_strings[XINT (rtl, 1)] : "unknown"),
12286                 XINT (rtl, 1));
12287 #else
12288                 "non-delegitimized UNSPEC %d found in variable location",
12289                 XINT (rtl, 1));
12290 #endif
12291       expansion_failed (NULL_TREE, rtl,
12292                         "UNSPEC hasn't been delegitimized.\n");
12293       return false;
12294     }
12295
12296   if (targetm.const_not_ok_for_debug_p (rtl))
12297     {
12298       expansion_failed (NULL_TREE, rtl,
12299                         "Expression rejected for debug by the backend.\n");
12300       return false;
12301     }
12302
12303   /* FIXME: Refer to PR60655. It is possible for simplification
12304      of rtl expressions in var tracking to produce such expressions.
12305      We should really identify / validate expressions
12306      enclosed in CONST that can be handled by assemblers on various
12307      targets and only handle legitimate cases here.  */
12308   if (GET_CODE (rtl) != SYMBOL_REF)
12309     {
12310       if (GET_CODE (rtl) == NOT)
12311         return false;
12312       return true;
12313     }
12314
12315   if (CONSTANT_POOL_ADDRESS_P (rtl))
12316     {
12317       bool marked;
12318       get_pool_constant_mark (rtl, &marked);
12319       /* If all references to this pool constant were optimized away,
12320          it was not output and thus we can't represent it.  */
12321       if (!marked)
12322         {
12323           expansion_failed (NULL_TREE, rtl,
12324                             "Constant was removed from constant pool.\n");
12325           return false;
12326         }
12327     }
12328
12329   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12330     return false;
12331
12332   /* Avoid references to external symbols in debug info, on several targets
12333      the linker might even refuse to link when linking a shared library,
12334      and in many other cases the relocations for .debug_info/.debug_loc are
12335      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
12336      to be defined within the same shared library or executable are fine.  */
12337   if (SYMBOL_REF_EXTERNAL_P (rtl))
12338     {
12339       tree decl = SYMBOL_REF_DECL (rtl);
12340
12341       if (decl == NULL || !targetm.binds_local_p (decl))
12342         {
12343           expansion_failed (NULL_TREE, rtl,
12344                             "Symbol not defined in current TU.\n");
12345           return false;
12346         }
12347     }
12348
12349   return true;
12350 }
12351
12352 /* Return true if constant RTL can be emitted in DW_OP_addr or
12353    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
12354    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
12355
12356 static bool
12357 const_ok_for_output (rtx rtl)
12358 {
12359   if (GET_CODE (rtl) == SYMBOL_REF)
12360     return const_ok_for_output_1 (rtl);
12361
12362   if (GET_CODE (rtl) == CONST)
12363     {
12364       subrtx_var_iterator::array_type array;
12365       FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
12366         if (!const_ok_for_output_1 (*iter))
12367           return false;
12368       return true;
12369     }
12370
12371   return true;
12372 }
12373
12374 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
12375    if possible, NULL otherwise.  */
12376
12377 static dw_die_ref
12378 base_type_for_mode (machine_mode mode, bool unsignedp)
12379 {
12380   dw_die_ref type_die;
12381   tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
12382
12383   if (type == NULL)
12384     return NULL;
12385   switch (TREE_CODE (type))
12386     {
12387     case INTEGER_TYPE:
12388     case REAL_TYPE:
12389       break;
12390     default:
12391       return NULL;
12392     }
12393   type_die = lookup_type_die (type);
12394   if (!type_die)
12395     type_die = modified_type_die (type, TYPE_UNQUALIFIED, comp_unit_die ());
12396   if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
12397     return NULL;
12398   return type_die;
12399 }
12400
12401 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
12402    type matching MODE, or, if MODE is narrower than or as wide as
12403    DWARF2_ADDR_SIZE, untyped.  Return NULL if the conversion is not
12404    possible.  */
12405
12406 static dw_loc_descr_ref
12407 convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
12408 {
12409   machine_mode outer_mode = mode;
12410   dw_die_ref type_die;
12411   dw_loc_descr_ref cvt;
12412
12413   if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12414     {
12415       add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
12416       return op;
12417     }
12418   type_die = base_type_for_mode (outer_mode, 1);
12419   if (type_die == NULL)
12420     return NULL;
12421   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12422   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12423   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12424   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12425   add_loc_descr (&op, cvt);
12426   return op;
12427 }
12428
12429 /* Return location descriptor for comparison OP with operands OP0 and OP1.  */
12430
12431 static dw_loc_descr_ref
12432 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
12433                         dw_loc_descr_ref op1)
12434 {
12435   dw_loc_descr_ref ret = op0;
12436   add_loc_descr (&ret, op1);
12437   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12438   if (STORE_FLAG_VALUE != 1)
12439     {
12440       add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
12441       add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
12442     }
12443   return ret;
12444 }
12445
12446 /* Return location descriptor for signed comparison OP RTL.  */
12447
12448 static dw_loc_descr_ref
12449 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
12450                          machine_mode mem_mode)
12451 {
12452   machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
12453   dw_loc_descr_ref op0, op1;
12454   int shift;
12455
12456   if (op_mode == VOIDmode)
12457     op_mode = GET_MODE (XEXP (rtl, 1));
12458   if (op_mode == VOIDmode)
12459     return NULL;
12460
12461   if (dwarf_strict
12462       && (GET_MODE_CLASS (op_mode) != MODE_INT
12463           || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
12464     return NULL;
12465
12466   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
12467                             VAR_INIT_STATUS_INITIALIZED);
12468   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
12469                             VAR_INIT_STATUS_INITIALIZED);
12470
12471   if (op0 == NULL || op1 == NULL)
12472     return NULL;
12473
12474   if (GET_MODE_CLASS (op_mode) != MODE_INT
12475       || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
12476     return compare_loc_descriptor (op, op0, op1);
12477
12478   if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
12479     {
12480       dw_die_ref type_die = base_type_for_mode (op_mode, 0);
12481       dw_loc_descr_ref cvt;
12482
12483       if (type_die == NULL)
12484         return NULL;
12485       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12486       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12487       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12488       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12489       add_loc_descr (&op0, cvt);
12490       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12491       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12492       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12493       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12494       add_loc_descr (&op1, cvt);
12495       return compare_loc_descriptor (op, op0, op1);
12496     }
12497
12498   shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
12499   /* For eq/ne, if the operands are known to be zero-extended,
12500      there is no need to do the fancy shifting up.  */
12501   if (op == DW_OP_eq || op == DW_OP_ne)
12502     {
12503       dw_loc_descr_ref last0, last1;
12504       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
12505         ;
12506       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
12507         ;
12508       /* deref_size zero extends, and for constants we can check
12509          whether they are zero extended or not.  */
12510       if (((last0->dw_loc_opc == DW_OP_deref_size
12511             && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
12512            || (CONST_INT_P (XEXP (rtl, 0))
12513                && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
12514                   == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
12515           && ((last1->dw_loc_opc == DW_OP_deref_size
12516                && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
12517               || (CONST_INT_P (XEXP (rtl, 1))
12518                   && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
12519                      == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
12520         return compare_loc_descriptor (op, op0, op1);
12521
12522       /* EQ/NE comparison against constant in narrower type than
12523          DWARF2_ADDR_SIZE can be performed either as
12524          DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
12525          DW_OP_{eq,ne}
12526          or
12527          DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
12528          DW_OP_{eq,ne}.  Pick whatever is shorter.  */
12529       if (CONST_INT_P (XEXP (rtl, 1))
12530           && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
12531           && (size_of_int_loc_descriptor (shift) + 1
12532               + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
12533               >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
12534                  + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
12535                                                & GET_MODE_MASK (op_mode))))
12536         {
12537           add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
12538           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12539           op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
12540                                     & GET_MODE_MASK (op_mode));
12541           return compare_loc_descriptor (op, op0, op1);
12542         }
12543     }
12544   add_loc_descr (&op0, int_loc_descriptor (shift));
12545   add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
12546   if (CONST_INT_P (XEXP (rtl, 1)))
12547     op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
12548   else
12549     {
12550       add_loc_descr (&op1, int_loc_descriptor (shift));
12551       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
12552     }
12553   return compare_loc_descriptor (op, op0, op1);
12554 }
12555
12556 /* Return location descriptor for unsigned comparison OP RTL.  */
12557
12558 static dw_loc_descr_ref
12559 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
12560                          machine_mode mem_mode)
12561 {
12562   machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
12563   dw_loc_descr_ref op0, op1;
12564
12565   if (op_mode == VOIDmode)
12566     op_mode = GET_MODE (XEXP (rtl, 1));
12567   if (op_mode == VOIDmode)
12568     return NULL;
12569   if (GET_MODE_CLASS (op_mode) != MODE_INT)
12570     return NULL;
12571
12572   if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
12573     return NULL;
12574
12575   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
12576                             VAR_INIT_STATUS_INITIALIZED);
12577   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
12578                             VAR_INIT_STATUS_INITIALIZED);
12579
12580   if (op0 == NULL || op1 == NULL)
12581     return NULL;
12582
12583   if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
12584     {
12585       HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
12586       dw_loc_descr_ref last0, last1;
12587       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
12588         ;
12589       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
12590         ;
12591       if (CONST_INT_P (XEXP (rtl, 0)))
12592         op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
12593       /* deref_size zero extends, so no need to mask it again.  */
12594       else if (last0->dw_loc_opc != DW_OP_deref_size
12595                || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
12596         {
12597           add_loc_descr (&op0, int_loc_descriptor (mask));
12598           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12599         }
12600       if (CONST_INT_P (XEXP (rtl, 1)))
12601         op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
12602       /* deref_size zero extends, so no need to mask it again.  */
12603       else if (last1->dw_loc_opc != DW_OP_deref_size
12604                || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
12605         {
12606           add_loc_descr (&op1, int_loc_descriptor (mask));
12607           add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12608         }
12609     }
12610   else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
12611     {
12612       HOST_WIDE_INT bias = 1;
12613       bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12614       add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12615       if (CONST_INT_P (XEXP (rtl, 1)))
12616         op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
12617                                   + INTVAL (XEXP (rtl, 1)));
12618       else
12619         add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
12620                                             bias, 0));
12621     }
12622   return compare_loc_descriptor (op, op0, op1);
12623 }
12624
12625 /* Return location descriptor for {U,S}{MIN,MAX}.  */
12626
12627 static dw_loc_descr_ref
12628 minmax_loc_descriptor (rtx rtl, machine_mode mode,
12629                        machine_mode mem_mode)
12630 {
12631   enum dwarf_location_atom op;
12632   dw_loc_descr_ref op0, op1, ret;
12633   dw_loc_descr_ref bra_node, drop_node;
12634
12635   if (dwarf_strict
12636       && (GET_MODE_CLASS (mode) != MODE_INT
12637           || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
12638     return NULL;
12639
12640   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12641                             VAR_INIT_STATUS_INITIALIZED);
12642   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12643                             VAR_INIT_STATUS_INITIALIZED);
12644
12645   if (op0 == NULL || op1 == NULL)
12646     return NULL;
12647
12648   add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
12649   add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
12650   add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
12651   if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
12652     {
12653       if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12654         {
12655           HOST_WIDE_INT mask = GET_MODE_MASK (mode);
12656           add_loc_descr (&op0, int_loc_descriptor (mask));
12657           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12658           add_loc_descr (&op1, int_loc_descriptor (mask));
12659           add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12660         }
12661       else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12662         {
12663           HOST_WIDE_INT bias = 1;
12664           bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12665           add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12666           add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12667         }
12668     }
12669   else if (GET_MODE_CLASS (mode) == MODE_INT
12670            && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12671     {
12672       int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
12673       add_loc_descr (&op0, int_loc_descriptor (shift));
12674       add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
12675       add_loc_descr (&op1, int_loc_descriptor (shift));
12676       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
12677     }
12678   else if (GET_MODE_CLASS (mode) == MODE_INT
12679            && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12680     {
12681       dw_die_ref type_die = base_type_for_mode (mode, 0);
12682       dw_loc_descr_ref cvt;
12683       if (type_die == NULL)
12684         return NULL;
12685       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12686       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12687       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12688       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12689       add_loc_descr (&op0, cvt);
12690       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12691       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12692       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12693       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12694       add_loc_descr (&op1, cvt);
12695     }
12696
12697   if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
12698     op = DW_OP_lt;
12699   else
12700     op = DW_OP_gt;
12701   ret = op0;
12702   add_loc_descr (&ret, op1);
12703   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12704   bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12705   add_loc_descr (&ret, bra_node);
12706   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12707   drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12708   add_loc_descr (&ret, drop_node);
12709   bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12710   bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12711   if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
12712       && GET_MODE_CLASS (mode) == MODE_INT
12713       && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12714     ret = convert_descriptor_to_mode (mode, ret);
12715   return ret;
12716 }
12717
12718 /* Helper function for mem_loc_descriptor.  Perform OP binary op,
12719    but after converting arguments to type_die, afterwards
12720    convert back to unsigned.  */
12721
12722 static dw_loc_descr_ref
12723 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
12724              machine_mode mode, machine_mode mem_mode)
12725 {
12726   dw_loc_descr_ref cvt, op0, op1;
12727
12728   if (type_die == NULL)
12729     return NULL;
12730   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12731                             VAR_INIT_STATUS_INITIALIZED);
12732   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12733                             VAR_INIT_STATUS_INITIALIZED);
12734   if (op0 == NULL || op1 == NULL)
12735     return NULL;
12736   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12737   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12738   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12739   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12740   add_loc_descr (&op0, cvt);
12741   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12742   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12743   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12744   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12745   add_loc_descr (&op1, cvt);
12746   add_loc_descr (&op0, op1);
12747   add_loc_descr (&op0, new_loc_descr (op, 0, 0));
12748   return convert_descriptor_to_mode (mode, op0);
12749 }
12750
12751 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
12752    const0 is DW_OP_lit0 or corresponding typed constant,
12753    const1 is DW_OP_lit1 or corresponding typed constant
12754    and constMSB is constant with just the MSB bit set
12755    for the mode):
12756        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12757    L1: const0 DW_OP_swap
12758    L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
12759        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12760    L3: DW_OP_drop
12761    L4: DW_OP_nop
12762
12763    CTZ is similar:
12764        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12765    L1: const0 DW_OP_swap
12766    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12767        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12768    L3: DW_OP_drop
12769    L4: DW_OP_nop
12770
12771    FFS is similar:
12772        DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
12773    L1: const1 DW_OP_swap
12774    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12775        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12776    L3: DW_OP_drop
12777    L4: DW_OP_nop  */
12778
12779 static dw_loc_descr_ref
12780 clz_loc_descriptor (rtx rtl, machine_mode mode,
12781                     machine_mode mem_mode)
12782 {
12783   dw_loc_descr_ref op0, ret, tmp;
12784   HOST_WIDE_INT valv;
12785   dw_loc_descr_ref l1jump, l1label;
12786   dw_loc_descr_ref l2jump, l2label;
12787   dw_loc_descr_ref l3jump, l3label;
12788   dw_loc_descr_ref l4jump, l4label;
12789   rtx msb;
12790
12791   if (GET_MODE_CLASS (mode) != MODE_INT
12792       || GET_MODE (XEXP (rtl, 0)) != mode)
12793     return NULL;
12794
12795   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12796                             VAR_INIT_STATUS_INITIALIZED);
12797   if (op0 == NULL)
12798     return NULL;
12799   ret = op0;
12800   if (GET_CODE (rtl) == CLZ)
12801     {
12802       if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12803         valv = GET_MODE_BITSIZE (mode);
12804     }
12805   else if (GET_CODE (rtl) == FFS)
12806     valv = 0;
12807   else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12808     valv = GET_MODE_BITSIZE (mode);
12809   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12810   l1jump = new_loc_descr (DW_OP_bra, 0, 0);
12811   add_loc_descr (&ret, l1jump);
12812   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12813   tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
12814                             VAR_INIT_STATUS_INITIALIZED);
12815   if (tmp == NULL)
12816     return NULL;
12817   add_loc_descr (&ret, tmp);
12818   l4jump = new_loc_descr (DW_OP_skip, 0, 0);
12819   add_loc_descr (&ret, l4jump);
12820   l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
12821                                 ? const1_rtx : const0_rtx,
12822                                 mode, mem_mode,
12823                                 VAR_INIT_STATUS_INITIALIZED);
12824   if (l1label == NULL)
12825     return NULL;
12826   add_loc_descr (&ret, l1label);
12827   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12828   l2label = new_loc_descr (DW_OP_dup, 0, 0);
12829   add_loc_descr (&ret, l2label);
12830   if (GET_CODE (rtl) != CLZ)
12831     msb = const1_rtx;
12832   else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
12833     msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
12834                    << (GET_MODE_BITSIZE (mode) - 1));
12835   else
12836     msb = immed_wide_int_const
12837       (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
12838                             GET_MODE_PRECISION (mode)), mode);
12839   if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
12840     tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12841                          ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
12842                          ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
12843   else
12844     tmp = mem_loc_descriptor (msb, mode, mem_mode,
12845                               VAR_INIT_STATUS_INITIALIZED);
12846   if (tmp == NULL)
12847     return NULL;
12848   add_loc_descr (&ret, tmp);
12849   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12850   l3jump = new_loc_descr (DW_OP_bra, 0, 0);
12851   add_loc_descr (&ret, l3jump);
12852   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12853                             VAR_INIT_STATUS_INITIALIZED);
12854   if (tmp == NULL)
12855     return NULL;
12856   add_loc_descr (&ret, tmp);
12857   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
12858                                       ? DW_OP_shl : DW_OP_shr, 0, 0));
12859   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12860   add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
12861   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12862   l2jump = new_loc_descr (DW_OP_skip, 0, 0);
12863   add_loc_descr (&ret, l2jump);
12864   l3label = new_loc_descr (DW_OP_drop, 0, 0);
12865   add_loc_descr (&ret, l3label);
12866   l4label = new_loc_descr (DW_OP_nop, 0, 0);
12867   add_loc_descr (&ret, l4label);
12868   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12869   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12870   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12871   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12872   l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12873   l3jump->dw_loc_oprnd1.v.val_loc = l3label;
12874   l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12875   l4jump->dw_loc_oprnd1.v.val_loc = l4label;
12876   return ret;
12877 }
12878
12879 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
12880    const1 is DW_OP_lit1 or corresponding typed constant):
12881        const0 DW_OP_swap
12882    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12883        DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12884    L2: DW_OP_drop
12885
12886    PARITY is similar:
12887    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12888        DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12889    L2: DW_OP_drop  */
12890
12891 static dw_loc_descr_ref
12892 popcount_loc_descriptor (rtx rtl, machine_mode mode,
12893                          machine_mode mem_mode)
12894 {
12895   dw_loc_descr_ref op0, ret, tmp;
12896   dw_loc_descr_ref l1jump, l1label;
12897   dw_loc_descr_ref l2jump, l2label;
12898
12899   if (GET_MODE_CLASS (mode) != MODE_INT
12900       || GET_MODE (XEXP (rtl, 0)) != mode)
12901     return NULL;
12902
12903   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12904                             VAR_INIT_STATUS_INITIALIZED);
12905   if (op0 == NULL)
12906     return NULL;
12907   ret = op0;
12908   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12909                             VAR_INIT_STATUS_INITIALIZED);
12910   if (tmp == NULL)
12911     return NULL;
12912   add_loc_descr (&ret, tmp);
12913   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12914   l1label = new_loc_descr (DW_OP_dup, 0, 0);
12915   add_loc_descr (&ret, l1label);
12916   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12917   add_loc_descr (&ret, l2jump);
12918   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12919   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12920   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12921                             VAR_INIT_STATUS_INITIALIZED);
12922   if (tmp == NULL)
12923     return NULL;
12924   add_loc_descr (&ret, tmp);
12925   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12926   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
12927                                       ? DW_OP_plus : DW_OP_xor, 0, 0));
12928   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12929   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12930                             VAR_INIT_STATUS_INITIALIZED);
12931   add_loc_descr (&ret, tmp);
12932   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12933   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12934   add_loc_descr (&ret, l1jump);
12935   l2label = new_loc_descr (DW_OP_drop, 0, 0);
12936   add_loc_descr (&ret, l2label);
12937   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12938   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12939   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12940   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12941   return ret;
12942 }
12943
12944 /* BSWAP (constS is initial shift count, either 56 or 24):
12945        constS const0
12946    L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
12947        const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
12948        DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
12949        DW_OP_minus DW_OP_swap DW_OP_skip <L1>
12950    L2: DW_OP_drop DW_OP_swap DW_OP_drop  */
12951
12952 static dw_loc_descr_ref
12953 bswap_loc_descriptor (rtx rtl, machine_mode mode,
12954                       machine_mode mem_mode)
12955 {
12956   dw_loc_descr_ref op0, ret, tmp;
12957   dw_loc_descr_ref l1jump, l1label;
12958   dw_loc_descr_ref l2jump, l2label;
12959
12960   if (GET_MODE_CLASS (mode) != MODE_INT
12961       || BITS_PER_UNIT != 8
12962       || (GET_MODE_BITSIZE (mode) != 32
12963           &&  GET_MODE_BITSIZE (mode) != 64))
12964     return NULL;
12965
12966   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12967                             VAR_INIT_STATUS_INITIALIZED);
12968   if (op0 == NULL)
12969     return NULL;
12970
12971   ret = op0;
12972   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12973                             mode, mem_mode,
12974                             VAR_INIT_STATUS_INITIALIZED);
12975   if (tmp == NULL)
12976     return NULL;
12977   add_loc_descr (&ret, tmp);
12978   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12979                             VAR_INIT_STATUS_INITIALIZED);
12980   if (tmp == NULL)
12981     return NULL;
12982   add_loc_descr (&ret, tmp);
12983   l1label = new_loc_descr (DW_OP_pick, 2, 0);
12984   add_loc_descr (&ret, l1label);
12985   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12986                             mode, mem_mode,
12987                             VAR_INIT_STATUS_INITIALIZED);
12988   add_loc_descr (&ret, tmp);
12989   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
12990   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12991   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12992   tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
12993                             VAR_INIT_STATUS_INITIALIZED);
12994   if (tmp == NULL)
12995     return NULL;
12996   add_loc_descr (&ret, tmp);
12997   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12998   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
12999   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13000   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
13001   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13002   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
13003   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
13004                             VAR_INIT_STATUS_INITIALIZED);
13005   add_loc_descr (&ret, tmp);
13006   add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
13007   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
13008   add_loc_descr (&ret, l2jump);
13009   tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
13010                             VAR_INIT_STATUS_INITIALIZED);
13011   add_loc_descr (&ret, tmp);
13012   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
13013   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13014   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
13015   add_loc_descr (&ret, l1jump);
13016   l2label = new_loc_descr (DW_OP_drop, 0, 0);
13017   add_loc_descr (&ret, l2label);
13018   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13019   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
13020   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13021   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
13022   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13023   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
13024   return ret;
13025 }
13026
13027 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
13028    DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
13029    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
13030    DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
13031
13032    ROTATERT is similar:
13033    DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
13034    DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
13035    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or  */
13036
13037 static dw_loc_descr_ref
13038 rotate_loc_descriptor (rtx rtl, machine_mode mode,
13039                        machine_mode mem_mode)
13040 {
13041   rtx rtlop1 = XEXP (rtl, 1);
13042   dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
13043   int i;
13044
13045   if (GET_MODE_CLASS (mode) != MODE_INT)
13046     return NULL;
13047
13048   if (GET_MODE (rtlop1) != VOIDmode
13049       && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
13050     rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
13051   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13052                             VAR_INIT_STATUS_INITIALIZED);
13053   op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
13054                             VAR_INIT_STATUS_INITIALIZED);
13055   if (op0 == NULL || op1 == NULL)
13056     return NULL;
13057   if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
13058     for (i = 0; i < 2; i++)
13059       {
13060         if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
13061           mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
13062                                         mode, mem_mode,
13063                                         VAR_INIT_STATUS_INITIALIZED);
13064         else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
13065           mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
13066                                    ? DW_OP_const4u
13067                                    : HOST_BITS_PER_WIDE_INT == 64
13068                                    ? DW_OP_const8u : DW_OP_constu,
13069                                    GET_MODE_MASK (mode), 0);
13070         else
13071           mask[i] = NULL;
13072         if (mask[i] == NULL)
13073           return NULL;
13074         add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
13075       }
13076   ret = op0;
13077   add_loc_descr (&ret, op1);
13078   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
13079   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
13080   if (GET_CODE (rtl) == ROTATERT)
13081     {
13082       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13083       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
13084                                           GET_MODE_BITSIZE (mode), 0));
13085     }
13086   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13087   if (mask[0] != NULL)
13088     add_loc_descr (&ret, mask[0]);
13089   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
13090   if (mask[1] != NULL)
13091     {
13092       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13093       add_loc_descr (&ret, mask[1]);
13094       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13095     }
13096   if (GET_CODE (rtl) == ROTATE)
13097     {
13098       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13099       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
13100                                           GET_MODE_BITSIZE (mode), 0));
13101     }
13102   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
13103   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
13104   return ret;
13105 }
13106
13107 /* Helper function for mem_loc_descriptor.  Return DW_OP_GNU_parameter_ref
13108    for DEBUG_PARAMETER_REF RTL.  */
13109
13110 static dw_loc_descr_ref
13111 parameter_ref_descriptor (rtx rtl)
13112 {
13113   dw_loc_descr_ref ret;
13114   dw_die_ref ref;
13115
13116   if (dwarf_strict)
13117     return NULL;
13118   gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
13119   ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
13120   ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
13121   if (ref)
13122     {
13123       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13124       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13125       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13126     }
13127   else
13128     {
13129       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13130       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
13131     }
13132   return ret;
13133 }
13134
13135 /* The following routine converts the RTL for a variable or parameter
13136    (resident in memory) into an equivalent Dwarf representation of a
13137    mechanism for getting the address of that same variable onto the top of a
13138    hypothetical "address evaluation" stack.
13139
13140    When creating memory location descriptors, we are effectively transforming
13141    the RTL for a memory-resident object into its Dwarf postfix expression
13142    equivalent.  This routine recursively descends an RTL tree, turning
13143    it into Dwarf postfix code as it goes.
13144
13145    MODE is the mode that should be assumed for the rtl if it is VOIDmode.
13146
13147    MEM_MODE is the mode of the memory reference, needed to handle some
13148    autoincrement addressing modes.
13149
13150    Return 0 if we can't represent the location.  */
13151
13152 dw_loc_descr_ref
13153 mem_loc_descriptor (rtx rtl, machine_mode mode,
13154                     machine_mode mem_mode,
13155                     enum var_init_status initialized)
13156 {
13157   dw_loc_descr_ref mem_loc_result = NULL;
13158   enum dwarf_location_atom op;
13159   dw_loc_descr_ref op0, op1;
13160   rtx inner = NULL_RTX;
13161
13162   if (mode == VOIDmode)
13163     mode = GET_MODE (rtl);
13164
13165   /* Note that for a dynamically sized array, the location we will generate a
13166      description of here will be the lowest numbered location which is
13167      actually within the array.  That's *not* necessarily the same as the
13168      zeroth element of the array.  */
13169
13170   rtl = targetm.delegitimize_address (rtl);
13171
13172   if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
13173     return NULL;
13174
13175   switch (GET_CODE (rtl))
13176     {
13177     case POST_INC:
13178     case POST_DEC:
13179     case POST_MODIFY:
13180       return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
13181
13182     case SUBREG:
13183       /* The case of a subreg may arise when we have a local (register)
13184          variable or a formal (register) parameter which doesn't quite fill
13185          up an entire register.  For now, just assume that it is
13186          legitimate to make the Dwarf info refer to the whole register which
13187          contains the given subreg.  */
13188       if (!subreg_lowpart_p (rtl))
13189         break;
13190       inner = SUBREG_REG (rtl);
13191     case TRUNCATE:
13192       if (inner == NULL_RTX)
13193         inner = XEXP (rtl, 0);
13194       if (GET_MODE_CLASS (mode) == MODE_INT
13195           && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
13196           && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13197 #ifdef POINTERS_EXTEND_UNSIGNED
13198               || (mode == Pmode && mem_mode != VOIDmode)
13199 #endif
13200              )
13201           && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
13202         {
13203           mem_loc_result = mem_loc_descriptor (inner,
13204                                                GET_MODE (inner),
13205                                                mem_mode, initialized);
13206           break;
13207         }
13208       if (dwarf_strict)
13209         break;
13210       if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
13211         break;
13212       if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
13213           && (GET_MODE_CLASS (mode) != MODE_INT
13214               || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
13215         break;
13216       else
13217         {
13218           dw_die_ref type_die;
13219           dw_loc_descr_ref cvt;
13220
13221           mem_loc_result = mem_loc_descriptor (inner,
13222                                                GET_MODE (inner),
13223                                                mem_mode, initialized);
13224           if (mem_loc_result == NULL)
13225             break;
13226           type_die = base_type_for_mode (mode,
13227                                          GET_MODE_CLASS (mode) == MODE_INT);
13228           if (type_die == NULL)
13229             {
13230               mem_loc_result = NULL;
13231               break;
13232             }
13233           if (GET_MODE_SIZE (mode)
13234               != GET_MODE_SIZE (GET_MODE (inner)))
13235             cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13236           else
13237             cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
13238           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13239           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13240           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13241           add_loc_descr (&mem_loc_result, cvt);
13242         }
13243       break;
13244
13245     case REG:
13246       if (GET_MODE_CLASS (mode) != MODE_INT
13247           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
13248               && rtl != arg_pointer_rtx
13249               && rtl != frame_pointer_rtx
13250 #ifdef POINTERS_EXTEND_UNSIGNED
13251               && (mode != Pmode || mem_mode == VOIDmode)
13252 #endif
13253               ))
13254         {
13255           dw_die_ref type_die;
13256           unsigned int dbx_regnum;
13257
13258           if (dwarf_strict)
13259             break;
13260           if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
13261             break;
13262           type_die = base_type_for_mode (mode,
13263                                          GET_MODE_CLASS (mode) == MODE_INT);
13264           if (type_die == NULL)
13265             break;
13266
13267           dbx_regnum = dbx_reg_number (rtl);
13268           if (dbx_regnum == IGNORED_DWARF_REGNUM)
13269             break;
13270           mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
13271                                           dbx_regnum, 0);
13272           mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
13273           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
13274           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
13275           break;
13276         }
13277       /* Whenever a register number forms a part of the description of the
13278          method for calculating the (dynamic) address of a memory resident
13279          object, DWARF rules require the register number be referred to as
13280          a "base register".  This distinction is not based in any way upon
13281          what category of register the hardware believes the given register
13282          belongs to.  This is strictly DWARF terminology we're dealing with
13283          here. Note that in cases where the location of a memory-resident
13284          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
13285          OP_CONST (0)) the actual DWARF location descriptor that we generate
13286          may just be OP_BASEREG (basereg).  This may look deceptively like
13287          the object in question was allocated to a register (rather than in
13288          memory) so DWARF consumers need to be aware of the subtle
13289          distinction between OP_REG and OP_BASEREG.  */
13290       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
13291         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
13292       else if (stack_realign_drap
13293                && crtl->drap_reg
13294                && crtl->args.internal_arg_pointer == rtl
13295                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
13296         {
13297           /* If RTL is internal_arg_pointer, which has been optimized
13298              out, use DRAP instead.  */
13299           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
13300                                             VAR_INIT_STATUS_INITIALIZED);
13301         }
13302       break;
13303
13304     case SIGN_EXTEND:
13305     case ZERO_EXTEND:
13306       if (GET_MODE_CLASS (mode) != MODE_INT)
13307         break;
13308       op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13309                                 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13310       if (op0 == 0)
13311         break;
13312       else if (GET_CODE (rtl) == ZERO_EXTEND
13313                && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13314                && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
13315                   < HOST_BITS_PER_WIDE_INT
13316                /* If DW_OP_const{1,2,4}u won't be used, it is shorter
13317                   to expand zero extend as two shifts instead of
13318                   masking.  */
13319                && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
13320         {
13321           machine_mode imode = GET_MODE (XEXP (rtl, 0));
13322           mem_loc_result = op0;
13323           add_loc_descr (&mem_loc_result,
13324                          int_loc_descriptor (GET_MODE_MASK (imode)));
13325           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
13326         }
13327       else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13328         {
13329           int shift = DWARF2_ADDR_SIZE
13330                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
13331           shift *= BITS_PER_UNIT;
13332           if (GET_CODE (rtl) == SIGN_EXTEND)
13333             op = DW_OP_shra;
13334           else
13335             op = DW_OP_shr;
13336           mem_loc_result = op0;
13337           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13338           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13339           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13340           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13341         }
13342       else if (!dwarf_strict)
13343         {
13344           dw_die_ref type_die1, type_die2;
13345           dw_loc_descr_ref cvt;
13346
13347           type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
13348                                           GET_CODE (rtl) == ZERO_EXTEND);
13349           if (type_die1 == NULL)
13350             break;
13351           type_die2 = base_type_for_mode (mode, 1);
13352           if (type_die2 == NULL)
13353             break;
13354           mem_loc_result = op0;
13355           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13356           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13357           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
13358           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13359           add_loc_descr (&mem_loc_result, cvt);
13360           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13361           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13362           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
13363           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13364           add_loc_descr (&mem_loc_result, cvt);
13365         }
13366       break;
13367
13368     case MEM:
13369       {
13370         rtx new_rtl = avoid_constant_pool_reference (rtl);
13371         if (new_rtl != rtl)
13372           {
13373             mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
13374                                                  initialized);
13375             if (mem_loc_result != NULL)
13376               return mem_loc_result;
13377           }
13378       }
13379       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
13380                                            get_address_mode (rtl), mode,
13381                                            VAR_INIT_STATUS_INITIALIZED);
13382       if (mem_loc_result == NULL)
13383         mem_loc_result = tls_mem_loc_descriptor (rtl);
13384       if (mem_loc_result != NULL)
13385         {
13386           if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
13387               || GET_MODE_CLASS (mode) != MODE_INT)
13388             {
13389               dw_die_ref type_die;
13390               dw_loc_descr_ref deref;
13391
13392               if (dwarf_strict)
13393                 return NULL;
13394               type_die
13395                 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
13396               if (type_die == NULL)
13397                 return NULL;
13398               deref = new_loc_descr (DW_OP_GNU_deref_type,
13399                                      GET_MODE_SIZE (mode), 0);
13400               deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
13401               deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
13402               deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
13403               add_loc_descr (&mem_loc_result, deref);
13404             }
13405           else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
13406             add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
13407           else
13408             add_loc_descr (&mem_loc_result,
13409                            new_loc_descr (DW_OP_deref_size,
13410                                           GET_MODE_SIZE (mode), 0));
13411         }
13412       break;
13413
13414     case LO_SUM:
13415       return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
13416
13417     case LABEL_REF:
13418       /* Some ports can transform a symbol ref into a label ref, because
13419          the symbol ref is too far away and has to be dumped into a constant
13420          pool.  */
13421     case CONST:
13422     case SYMBOL_REF:
13423       if ((GET_MODE_CLASS (mode) != MODE_INT
13424            && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
13425           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
13426 #ifdef POINTERS_EXTEND_UNSIGNED
13427               && (mode != Pmode || mem_mode == VOIDmode)
13428 #endif
13429               ))
13430         break;
13431       if (GET_CODE (rtl) == SYMBOL_REF
13432           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13433         {
13434           dw_loc_descr_ref temp;
13435
13436           /* If this is not defined, we have no way to emit the data.  */
13437           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
13438             break;
13439
13440           temp = new_addr_loc_descr (rtl, dtprel_true);
13441
13442           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
13443           add_loc_descr (&mem_loc_result, temp);
13444
13445           break;
13446         }
13447
13448       if (!const_ok_for_output (rtl))
13449         {
13450           if (GET_CODE (rtl) == CONST)
13451             mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13452                                                  initialized);
13453           break;
13454         }
13455
13456     symref:
13457       mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
13458       vec_safe_push (used_rtx_array, rtl);
13459       break;
13460
13461     case CONCAT:
13462     case CONCATN:
13463     case VAR_LOCATION:
13464     case DEBUG_IMPLICIT_PTR:
13465       expansion_failed (NULL_TREE, rtl,
13466                         "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
13467       return 0;
13468
13469     case ENTRY_VALUE:
13470       if (dwarf_strict)
13471         return NULL;
13472       if (REG_P (ENTRY_VALUE_EXP (rtl)))
13473         {
13474           if (GET_MODE_CLASS (mode) != MODE_INT
13475               || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
13476             op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
13477                                       VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13478           else
13479             {
13480               unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
13481               if (dbx_regnum == IGNORED_DWARF_REGNUM)
13482                 return NULL;
13483               op0 = one_reg_loc_descriptor (dbx_regnum,
13484                                             VAR_INIT_STATUS_INITIALIZED);
13485             }
13486         }
13487       else if (MEM_P (ENTRY_VALUE_EXP (rtl))
13488                && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
13489         {
13490           op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
13491                                     VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13492           if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
13493             return NULL;
13494         }
13495       else
13496         gcc_unreachable ();
13497       if (op0 == NULL)
13498         return NULL;
13499       mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
13500       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
13501       mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
13502       break;
13503
13504     case DEBUG_PARAMETER_REF:
13505       mem_loc_result = parameter_ref_descriptor (rtl);
13506       break;
13507
13508     case PRE_MODIFY:
13509       /* Extract the PLUS expression nested inside and fall into
13510          PLUS code below.  */
13511       rtl = XEXP (rtl, 1);
13512       goto plus;
13513
13514     case PRE_INC:
13515     case PRE_DEC:
13516       /* Turn these into a PLUS expression and fall into the PLUS code
13517          below.  */
13518       rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
13519                           gen_int_mode (GET_CODE (rtl) == PRE_INC
13520                                         ? GET_MODE_UNIT_SIZE (mem_mode)
13521                                         : -GET_MODE_UNIT_SIZE (mem_mode),
13522                                         mode));
13523
13524       /* ... fall through ...  */
13525
13526     case PLUS:
13527     plus:
13528       if (is_based_loc (rtl)
13529           && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13530               || XEXP (rtl, 0) == arg_pointer_rtx
13531               || XEXP (rtl, 0) == frame_pointer_rtx)
13532           && GET_MODE_CLASS (mode) == MODE_INT)
13533         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
13534                                           INTVAL (XEXP (rtl, 1)),
13535                                           VAR_INIT_STATUS_INITIALIZED);
13536       else
13537         {
13538           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13539                                                VAR_INIT_STATUS_INITIALIZED);
13540           if (mem_loc_result == 0)
13541             break;
13542
13543           if (CONST_INT_P (XEXP (rtl, 1))
13544               && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13545             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
13546           else
13547             {
13548               op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13549                                         VAR_INIT_STATUS_INITIALIZED);
13550               if (op1 == 0)
13551                 return NULL;
13552               add_loc_descr (&mem_loc_result, op1);
13553               add_loc_descr (&mem_loc_result,
13554                              new_loc_descr (DW_OP_plus, 0, 0));
13555             }
13556         }
13557       break;
13558
13559     /* If a pseudo-reg is optimized away, it is possible for it to
13560        be replaced with a MEM containing a multiply or shift.  */
13561     case MINUS:
13562       op = DW_OP_minus;
13563       goto do_binop;
13564
13565     case MULT:
13566       op = DW_OP_mul;
13567       goto do_binop;
13568
13569     case DIV:
13570       if (!dwarf_strict
13571           && GET_MODE_CLASS (mode) == MODE_INT
13572           && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
13573         {
13574           mem_loc_result = typed_binop (DW_OP_div, rtl,
13575                                         base_type_for_mode (mode, 0),
13576                                         mode, mem_mode);
13577           break;
13578         }
13579       op = DW_OP_div;
13580       goto do_binop;
13581
13582     case UMOD:
13583       op = DW_OP_mod;
13584       goto do_binop;
13585
13586     case ASHIFT:
13587       op = DW_OP_shl;
13588       goto do_shift;
13589
13590     case ASHIFTRT:
13591       op = DW_OP_shra;
13592       goto do_shift;
13593
13594     case LSHIFTRT:
13595       op = DW_OP_shr;
13596       goto do_shift;
13597
13598     do_shift:
13599       if (GET_MODE_CLASS (mode) != MODE_INT)
13600         break;
13601       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13602                                 VAR_INIT_STATUS_INITIALIZED);
13603       {
13604         rtx rtlop1 = XEXP (rtl, 1);
13605         if (GET_MODE (rtlop1) != VOIDmode
13606             && GET_MODE_BITSIZE (GET_MODE (rtlop1))
13607                < GET_MODE_BITSIZE (mode))
13608           rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
13609         op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
13610                                   VAR_INIT_STATUS_INITIALIZED);
13611       }
13612
13613       if (op0 == 0 || op1 == 0)
13614         break;
13615
13616       mem_loc_result = op0;
13617       add_loc_descr (&mem_loc_result, op1);
13618       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13619       break;
13620
13621     case AND:
13622       op = DW_OP_and;
13623       goto do_binop;
13624
13625     case IOR:
13626       op = DW_OP_or;
13627       goto do_binop;
13628
13629     case XOR:
13630       op = DW_OP_xor;
13631       goto do_binop;
13632
13633     do_binop:
13634       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13635                                 VAR_INIT_STATUS_INITIALIZED);
13636       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13637                                 VAR_INIT_STATUS_INITIALIZED);
13638
13639       if (op0 == 0 || op1 == 0)
13640         break;
13641
13642       mem_loc_result = op0;
13643       add_loc_descr (&mem_loc_result, op1);
13644       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13645       break;
13646
13647     case MOD:
13648       if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
13649         {
13650           mem_loc_result = typed_binop (DW_OP_mod, rtl,
13651                                         base_type_for_mode (mode, 0),
13652                                         mode, mem_mode);
13653           break;
13654         }
13655
13656       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13657                                 VAR_INIT_STATUS_INITIALIZED);
13658       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13659                                 VAR_INIT_STATUS_INITIALIZED);
13660
13661       if (op0 == 0 || op1 == 0)
13662         break;
13663
13664       mem_loc_result = op0;
13665       add_loc_descr (&mem_loc_result, op1);
13666       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13667       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13668       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
13669       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13670       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
13671       break;
13672
13673     case UDIV:
13674       if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
13675         {
13676           if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
13677             {
13678               op = DW_OP_div;
13679               goto do_binop;
13680             }
13681           mem_loc_result = typed_binop (DW_OP_div, rtl,
13682                                         base_type_for_mode (mode, 1),
13683                                         mode, mem_mode);
13684         }
13685       break;
13686
13687     case NOT:
13688       op = DW_OP_not;
13689       goto do_unop;
13690
13691     case ABS:
13692       op = DW_OP_abs;
13693       goto do_unop;
13694
13695     case NEG:
13696       op = DW_OP_neg;
13697       goto do_unop;
13698
13699     do_unop:
13700       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13701                                 VAR_INIT_STATUS_INITIALIZED);
13702
13703       if (op0 == 0)
13704         break;
13705
13706       mem_loc_result = op0;
13707       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13708       break;
13709
13710     case CONST_INT:
13711       if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13712 #ifdef POINTERS_EXTEND_UNSIGNED
13713           || (mode == Pmode
13714               && mem_mode != VOIDmode
13715               && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
13716 #endif
13717           )
13718         {
13719           mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13720           break;
13721         }
13722       if (!dwarf_strict
13723           && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
13724               || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
13725         {
13726           dw_die_ref type_die = base_type_for_mode (mode, 1);
13727           machine_mode amode;
13728           if (type_die == NULL)
13729             return NULL;
13730           amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
13731                                  MODE_INT, 0);
13732           if (INTVAL (rtl) >= 0
13733               && amode != BLKmode
13734               && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
13735               /* const DW_OP_GNU_convert <XXX> vs.
13736                  DW_OP_GNU_const_type <XXX, 1, const>.  */
13737               && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
13738                  < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
13739             {
13740               mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13741               op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13742               op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13743               op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13744               op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
13745               add_loc_descr (&mem_loc_result, op0);
13746               return mem_loc_result;
13747             }
13748           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
13749                                           INTVAL (rtl));
13750           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13751           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13752           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13753           if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
13754             mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13755           else
13756             {
13757               mem_loc_result->dw_loc_oprnd2.val_class
13758                 = dw_val_class_const_double;
13759               mem_loc_result->dw_loc_oprnd2.v.val_double
13760                 = double_int::from_shwi (INTVAL (rtl));
13761             }
13762         }
13763       break;
13764
13765     case CONST_DOUBLE:
13766       if (!dwarf_strict)
13767         {
13768           dw_die_ref type_die;
13769
13770           /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
13771              CONST_DOUBLE rtx could represent either a large integer
13772              or a floating-point constant.  If TARGET_SUPPORTS_WIDE_INT != 0,
13773              the value is always a floating point constant.
13774
13775              When it is an integer, a CONST_DOUBLE is used whenever
13776              the constant requires 2 HWIs to be adequately represented.
13777              We output CONST_DOUBLEs as blocks.  */
13778           if (mode == VOIDmode
13779               || (GET_MODE (rtl) == VOIDmode
13780                   && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
13781             break;
13782           type_die = base_type_for_mode (mode,
13783                                          GET_MODE_CLASS (mode) == MODE_INT);
13784           if (type_die == NULL)
13785             return NULL;
13786           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13787           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13788           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13789           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13790 #if TARGET_SUPPORTS_WIDE_INT == 0
13791           if (!SCALAR_FLOAT_MODE_P (mode))
13792             {
13793               mem_loc_result->dw_loc_oprnd2.val_class
13794                 = dw_val_class_const_double;
13795               mem_loc_result->dw_loc_oprnd2.v.val_double
13796                 = rtx_to_double_int (rtl);
13797             }
13798           else
13799 #endif
13800             {
13801               unsigned int length = GET_MODE_SIZE (mode);
13802               unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13803
13804               insert_float (rtl, array);
13805               mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13806               mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13807               mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13808               mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13809             }
13810         }
13811       break;
13812
13813     case CONST_WIDE_INT:
13814       if (!dwarf_strict)
13815         {
13816           dw_die_ref type_die;
13817
13818           type_die = base_type_for_mode (mode,
13819                                          GET_MODE_CLASS (mode) == MODE_INT);
13820           if (type_die == NULL)
13821             return NULL;
13822           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13823           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13824           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13825           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13826           mem_loc_result->dw_loc_oprnd2.val_class
13827             = dw_val_class_wide_int;
13828           mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
13829           *mem_loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13830         }
13831       break;
13832
13833     case EQ:
13834       mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
13835       break;
13836
13837     case GE:
13838       mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13839       break;
13840
13841     case GT:
13842       mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13843       break;
13844
13845     case LE:
13846       mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13847       break;
13848
13849     case LT:
13850       mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13851       break;
13852
13853     case NE:
13854       mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
13855       break;
13856
13857     case GEU:
13858       mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13859       break;
13860
13861     case GTU:
13862       mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13863       break;
13864
13865     case LEU:
13866       mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13867       break;
13868
13869     case LTU:
13870       mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13871       break;
13872
13873     case UMIN:
13874     case UMAX:
13875       if (GET_MODE_CLASS (mode) != MODE_INT)
13876         break;
13877       /* FALLTHRU */
13878     case SMIN:
13879     case SMAX:
13880       mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
13881       break;
13882
13883     case ZERO_EXTRACT:
13884     case SIGN_EXTRACT:
13885       if (CONST_INT_P (XEXP (rtl, 1))
13886           && CONST_INT_P (XEXP (rtl, 2))
13887           && ((unsigned) INTVAL (XEXP (rtl, 1))
13888               + (unsigned) INTVAL (XEXP (rtl, 2))
13889               <= GET_MODE_BITSIZE (mode))
13890           && GET_MODE_CLASS (mode) == MODE_INT
13891           && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13892           && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
13893         {
13894           int shift, size;
13895           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13896                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
13897           if (op0 == 0)
13898             break;
13899           if (GET_CODE (rtl) == SIGN_EXTRACT)
13900             op = DW_OP_shra;
13901           else
13902             op = DW_OP_shr;
13903           mem_loc_result = op0;
13904           size = INTVAL (XEXP (rtl, 1));
13905           shift = INTVAL (XEXP (rtl, 2));
13906           if (BITS_BIG_ENDIAN)
13907             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
13908                     - shift - size;
13909           if (shift + size != (int) DWARF2_ADDR_SIZE)
13910             {
13911               add_loc_descr (&mem_loc_result,
13912                              int_loc_descriptor (DWARF2_ADDR_SIZE
13913                                                  - shift - size));
13914               add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13915             }
13916           if (size != (int) DWARF2_ADDR_SIZE)
13917             {
13918               add_loc_descr (&mem_loc_result,
13919                              int_loc_descriptor (DWARF2_ADDR_SIZE - size));
13920               add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13921             }
13922         }
13923       break;
13924
13925     case IF_THEN_ELSE:
13926       {
13927         dw_loc_descr_ref op2, bra_node, drop_node;
13928         op0 = mem_loc_descriptor (XEXP (rtl, 0),
13929                                   GET_MODE (XEXP (rtl, 0)) == VOIDmode
13930                                   ? word_mode : GET_MODE (XEXP (rtl, 0)),
13931                                   mem_mode, VAR_INIT_STATUS_INITIALIZED);
13932         op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13933                                   VAR_INIT_STATUS_INITIALIZED);
13934         op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
13935                                   VAR_INIT_STATUS_INITIALIZED);
13936         if (op0 == NULL || op1 == NULL || op2 == NULL)
13937           break;
13938
13939         mem_loc_result = op1;
13940         add_loc_descr (&mem_loc_result, op2);
13941         add_loc_descr (&mem_loc_result, op0);
13942         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13943         add_loc_descr (&mem_loc_result, bra_node);
13944         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
13945         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
13946         add_loc_descr (&mem_loc_result, drop_node);
13947         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13948         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
13949       }
13950       break;
13951
13952     case FLOAT_EXTEND:
13953     case FLOAT_TRUNCATE:
13954     case FLOAT:
13955     case UNSIGNED_FLOAT:
13956     case FIX:
13957     case UNSIGNED_FIX:
13958       if (!dwarf_strict)
13959         {
13960           dw_die_ref type_die;
13961           dw_loc_descr_ref cvt;
13962
13963           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13964                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
13965           if (op0 == NULL)
13966             break;
13967           if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
13968               && (GET_CODE (rtl) == FLOAT
13969                   || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
13970                      <= DWARF2_ADDR_SIZE))
13971             {
13972               type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
13973                                              GET_CODE (rtl) == UNSIGNED_FLOAT);
13974               if (type_die == NULL)
13975                 break;
13976               cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13977               cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13978               cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13979               cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13980               add_loc_descr (&op0, cvt);
13981             }
13982           type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
13983           if (type_die == NULL)
13984             break;
13985           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13986           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13987           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13988           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13989           add_loc_descr (&op0, cvt);
13990           if (GET_MODE_CLASS (mode) == MODE_INT
13991               && (GET_CODE (rtl) == FIX
13992                   || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
13993             {
13994               op0 = convert_descriptor_to_mode (mode, op0);
13995               if (op0 == NULL)
13996                 break;
13997             }
13998           mem_loc_result = op0;
13999         }
14000       break;
14001
14002     case CLZ:
14003     case CTZ:
14004     case FFS:
14005       mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
14006       break;
14007
14008     case POPCOUNT:
14009     case PARITY:
14010       mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
14011       break;
14012
14013     case BSWAP:
14014       mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
14015       break;
14016
14017     case ROTATE:
14018     case ROTATERT:
14019       mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
14020       break;
14021
14022     case COMPARE:
14023       /* In theory, we could implement the above.  */
14024       /* DWARF cannot represent the unsigned compare operations
14025          natively.  */
14026     case SS_MULT:
14027     case US_MULT:
14028     case SS_DIV:
14029     case US_DIV:
14030     case SS_PLUS:
14031     case US_PLUS:
14032     case SS_MINUS:
14033     case US_MINUS:
14034     case SS_NEG:
14035     case US_NEG:
14036     case SS_ABS:
14037     case SS_ASHIFT:
14038     case US_ASHIFT:
14039     case SS_TRUNCATE:
14040     case US_TRUNCATE:
14041     case UNORDERED:
14042     case ORDERED:
14043     case UNEQ:
14044     case UNGE:
14045     case UNGT:
14046     case UNLE:
14047     case UNLT:
14048     case LTGT:
14049     case FRACT_CONVERT:
14050     case UNSIGNED_FRACT_CONVERT:
14051     case SAT_FRACT:
14052     case UNSIGNED_SAT_FRACT:
14053     case SQRT:
14054     case ASM_OPERANDS:
14055     case VEC_MERGE:
14056     case VEC_SELECT:
14057     case VEC_CONCAT:
14058     case VEC_DUPLICATE:
14059     case UNSPEC:
14060     case HIGH:
14061     case FMA:
14062     case STRICT_LOW_PART:
14063     case CONST_VECTOR:
14064     case CONST_FIXED:
14065     case CLRSB:
14066     case CLOBBER:
14067       /* If delegitimize_address couldn't do anything with the UNSPEC, we
14068          can't express it in the debug info.  This can happen e.g. with some
14069          TLS UNSPECs.  */
14070       break;
14071
14072     case CONST_STRING:
14073       resolve_one_addr (&rtl);
14074       goto symref;
14075
14076     default:
14077       if (flag_checking)
14078         {
14079           print_rtl (stderr, rtl);
14080           gcc_unreachable ();
14081         }
14082       break;
14083     }
14084
14085   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14086     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14087
14088   return mem_loc_result;
14089 }
14090
14091 /* Return a descriptor that describes the concatenation of two locations.
14092    This is typically a complex variable.  */
14093
14094 static dw_loc_descr_ref
14095 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
14096 {
14097   dw_loc_descr_ref cc_loc_result = NULL;
14098   dw_loc_descr_ref x0_ref
14099     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14100   dw_loc_descr_ref x1_ref
14101     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14102
14103   if (x0_ref == 0 || x1_ref == 0)
14104     return 0;
14105
14106   cc_loc_result = x0_ref;
14107   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
14108
14109   add_loc_descr (&cc_loc_result, x1_ref);
14110   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
14111
14112   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14113     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14114
14115   return cc_loc_result;
14116 }
14117
14118 /* Return a descriptor that describes the concatenation of N
14119    locations.  */
14120
14121 static dw_loc_descr_ref
14122 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
14123 {
14124   unsigned int i;
14125   dw_loc_descr_ref cc_loc_result = NULL;
14126   unsigned int n = XVECLEN (concatn, 0);
14127
14128   for (i = 0; i < n; ++i)
14129     {
14130       dw_loc_descr_ref ref;
14131       rtx x = XVECEXP (concatn, 0, i);
14132
14133       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14134       if (ref == NULL)
14135         return NULL;
14136
14137       add_loc_descr (&cc_loc_result, ref);
14138       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
14139     }
14140
14141   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14142     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14143
14144   return cc_loc_result;
14145 }
14146
14147 /* Helper function for loc_descriptor.  Return DW_OP_GNU_implicit_pointer
14148    for DEBUG_IMPLICIT_PTR RTL.  */
14149
14150 static dw_loc_descr_ref
14151 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
14152 {
14153   dw_loc_descr_ref ret;
14154   dw_die_ref ref;
14155
14156   if (dwarf_strict)
14157     return NULL;
14158   gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
14159               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
14160               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
14161   ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
14162   ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
14163   ret->dw_loc_oprnd2.val_class = dw_val_class_const;
14164   if (ref)
14165     {
14166       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14167       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14168       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14169     }
14170   else
14171     {
14172       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14173       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
14174     }
14175   return ret;
14176 }
14177
14178 /* Output a proper Dwarf location descriptor for a variable or parameter
14179    which is either allocated in a register or in a memory location.  For a
14180    register, we just generate an OP_REG and the register number.  For a
14181    memory location we provide a Dwarf postfix expression describing how to
14182    generate the (dynamic) address of the object onto the address stack.
14183
14184    MODE is mode of the decl if this loc_descriptor is going to be used in
14185    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
14186    allowed, VOIDmode otherwise.
14187
14188    If we don't know how to describe it, return 0.  */
14189
14190 static dw_loc_descr_ref
14191 loc_descriptor (rtx rtl, machine_mode mode,
14192                 enum var_init_status initialized)
14193 {
14194   dw_loc_descr_ref loc_result = NULL;
14195
14196   switch (GET_CODE (rtl))
14197     {
14198     case SUBREG:
14199       /* The case of a subreg may arise when we have a local (register)
14200          variable or a formal (register) parameter which doesn't quite fill
14201          up an entire register.  For now, just assume that it is
14202          legitimate to make the Dwarf info refer to the whole register which
14203          contains the given subreg.  */
14204       if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
14205         loc_result = loc_descriptor (SUBREG_REG (rtl),
14206                                      GET_MODE (SUBREG_REG (rtl)), initialized);
14207       else
14208         goto do_default;
14209       break;
14210
14211     case REG:
14212       loc_result = reg_loc_descriptor (rtl, initialized);
14213       break;
14214
14215     case MEM:
14216       loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
14217                                        GET_MODE (rtl), initialized);
14218       if (loc_result == NULL)
14219         loc_result = tls_mem_loc_descriptor (rtl);
14220       if (loc_result == NULL)
14221         {
14222           rtx new_rtl = avoid_constant_pool_reference (rtl);
14223           if (new_rtl != rtl)
14224             loc_result = loc_descriptor (new_rtl, mode, initialized);
14225         }
14226       break;
14227
14228     case CONCAT:
14229       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
14230                                           initialized);
14231       break;
14232
14233     case CONCATN:
14234       loc_result = concatn_loc_descriptor (rtl, initialized);
14235       break;
14236
14237     case VAR_LOCATION:
14238       /* Single part.  */
14239       if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
14240         {
14241           rtx loc = PAT_VAR_LOCATION_LOC (rtl);
14242           if (GET_CODE (loc) == EXPR_LIST)
14243             loc = XEXP (loc, 0);
14244           loc_result = loc_descriptor (loc, mode, initialized);
14245           break;
14246         }
14247
14248       rtl = XEXP (rtl, 1);
14249       /* FALLTHRU */
14250
14251     case PARALLEL:
14252       {
14253         rtvec par_elems = XVEC (rtl, 0);
14254         int num_elem = GET_NUM_ELEM (par_elems);
14255         machine_mode mode;
14256         int i;
14257
14258         /* Create the first one, so we have something to add to.  */
14259         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
14260                                      VOIDmode, initialized);
14261         if (loc_result == NULL)
14262           return NULL;
14263         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
14264         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14265         for (i = 1; i < num_elem; i++)
14266           {
14267             dw_loc_descr_ref temp;
14268
14269             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
14270                                    VOIDmode, initialized);
14271             if (temp == NULL)
14272               return NULL;
14273             add_loc_descr (&loc_result, temp);
14274             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
14275             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14276           }
14277       }
14278       break;
14279
14280     case CONST_INT:
14281       if (mode != VOIDmode && mode != BLKmode)
14282         loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
14283                                                     INTVAL (rtl));
14284       break;
14285
14286     case CONST_DOUBLE:
14287       if (mode == VOIDmode)
14288         mode = GET_MODE (rtl);
14289
14290       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14291         {
14292           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14293
14294           /* Note that a CONST_DOUBLE rtx could represent either an integer
14295              or a floating-point constant.  A CONST_DOUBLE is used whenever
14296              the constant requires more than one word in order to be
14297              adequately represented.  We output CONST_DOUBLEs as blocks.  */
14298           loc_result = new_loc_descr (DW_OP_implicit_value,
14299                                       GET_MODE_SIZE (mode), 0);
14300 #if TARGET_SUPPORTS_WIDE_INT == 0
14301           if (!SCALAR_FLOAT_MODE_P (mode))
14302             {
14303               loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
14304               loc_result->dw_loc_oprnd2.v.val_double
14305                 = rtx_to_double_int (rtl);
14306             }
14307           else
14308 #endif
14309             {
14310               unsigned int length = GET_MODE_SIZE (mode);
14311               unsigned char *array = ggc_vec_alloc<unsigned char> (length);
14312
14313               insert_float (rtl, array);
14314               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14315               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
14316               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
14317               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14318             }
14319         }
14320       break;
14321
14322     case CONST_WIDE_INT:
14323       if (mode == VOIDmode)
14324         mode = GET_MODE (rtl);
14325
14326       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14327         {
14328           loc_result = new_loc_descr (DW_OP_implicit_value,
14329                                       GET_MODE_SIZE (mode), 0);
14330           loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
14331           loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
14332           *loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
14333         }
14334       break;
14335
14336     case CONST_VECTOR:
14337       if (mode == VOIDmode)
14338         mode = GET_MODE (rtl);
14339
14340       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14341         {
14342           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
14343           unsigned int length = CONST_VECTOR_NUNITS (rtl);
14344           unsigned char *array
14345             = ggc_vec_alloc<unsigned char> (length * elt_size);
14346           unsigned int i;
14347           unsigned char *p;
14348           machine_mode imode = GET_MODE_INNER (mode);
14349
14350           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14351           switch (GET_MODE_CLASS (mode))
14352             {
14353             case MODE_VECTOR_INT:
14354               for (i = 0, p = array; i < length; i++, p += elt_size)
14355                 {
14356                   rtx elt = CONST_VECTOR_ELT (rtl, i);
14357                   insert_wide_int (std::make_pair (elt, imode), p, elt_size);
14358                 }
14359               break;
14360
14361             case MODE_VECTOR_FLOAT:
14362               for (i = 0, p = array; i < length; i++, p += elt_size)
14363                 {
14364                   rtx elt = CONST_VECTOR_ELT (rtl, i);
14365                   insert_float (elt, p);
14366                 }
14367               break;
14368
14369             default:
14370               gcc_unreachable ();
14371             }
14372
14373           loc_result = new_loc_descr (DW_OP_implicit_value,
14374                                       length * elt_size, 0);
14375           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14376           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
14377           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
14378           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14379         }
14380       break;
14381
14382     case CONST:
14383       if (mode == VOIDmode
14384           || CONST_SCALAR_INT_P (XEXP (rtl, 0))
14385           || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
14386           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
14387         {
14388           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
14389           break;
14390         }
14391       /* FALLTHROUGH */
14392     case SYMBOL_REF:
14393       if (!const_ok_for_output (rtl))
14394         break;
14395     case LABEL_REF:
14396       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
14397           && (dwarf_version >= 4 || !dwarf_strict))
14398         {
14399          loc_result = new_addr_loc_descr (rtl, dtprel_false);
14400           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14401           vec_safe_push (used_rtx_array, rtl);
14402         }
14403       break;
14404
14405     case DEBUG_IMPLICIT_PTR:
14406       loc_result = implicit_ptr_descriptor (rtl, 0);
14407       break;
14408
14409     case PLUS:
14410       if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
14411           && CONST_INT_P (XEXP (rtl, 1)))
14412         {
14413           loc_result
14414             = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
14415           break;
14416         }
14417       /* FALLTHRU */
14418     do_default:
14419     default:
14420       if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
14421            && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14422            && dwarf_version >= 4)
14423           || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
14424         {
14425           /* Value expression.  */
14426           loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
14427           if (loc_result)
14428             add_loc_descr (&loc_result,
14429                            new_loc_descr (DW_OP_stack_value, 0, 0));
14430         }
14431       break;
14432     }
14433
14434   return loc_result;
14435 }
14436
14437 /* We need to figure out what section we should use as the base for the
14438    address ranges where a given location is valid.
14439    1. If this particular DECL has a section associated with it, use that.
14440    2. If this function has a section associated with it, use that.
14441    3. Otherwise, use the text section.
14442    XXX: If you split a variable across multiple sections, we won't notice.  */
14443
14444 static const char *
14445 secname_for_decl (const_tree decl)
14446 {
14447   const char *secname;
14448
14449   if (VAR_OR_FUNCTION_DECL_P (decl)
14450       && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
14451       && DECL_SECTION_NAME (decl))
14452     secname = DECL_SECTION_NAME (decl);
14453   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
14454     secname = DECL_SECTION_NAME (current_function_decl);
14455   else if (cfun && in_cold_section_p)
14456     secname = crtl->subsections.cold_section_label;
14457   else
14458     secname = text_section_label;
14459
14460   return secname;
14461 }
14462
14463 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
14464
14465 static bool
14466 decl_by_reference_p (tree decl)
14467 {
14468   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
14469            || TREE_CODE (decl) == VAR_DECL)
14470           && DECL_BY_REFERENCE (decl));
14471 }
14472
14473 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
14474    for VARLOC.  */
14475
14476 static dw_loc_descr_ref
14477 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
14478                enum var_init_status initialized)
14479 {
14480   int have_address = 0;
14481   dw_loc_descr_ref descr;
14482   machine_mode mode;
14483
14484   if (want_address != 2)
14485     {
14486       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
14487       /* Single part.  */
14488       if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14489         {
14490           varloc = PAT_VAR_LOCATION_LOC (varloc);
14491           if (GET_CODE (varloc) == EXPR_LIST)
14492             varloc = XEXP (varloc, 0);
14493           mode = GET_MODE (varloc);
14494           if (MEM_P (varloc))
14495             {
14496               rtx addr = XEXP (varloc, 0);
14497               descr = mem_loc_descriptor (addr, get_address_mode (varloc),
14498                                           mode, initialized);
14499               if (descr)
14500                 have_address = 1;
14501               else
14502                 {
14503                   rtx x = avoid_constant_pool_reference (varloc);
14504                   if (x != varloc)
14505                     descr = mem_loc_descriptor (x, mode, VOIDmode,
14506                                                 initialized);
14507                 }
14508             }
14509           else
14510             descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
14511         }
14512       else
14513         return 0;
14514     }
14515   else
14516     {
14517       if (GET_CODE (varloc) == VAR_LOCATION)
14518         mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
14519       else
14520         mode = DECL_MODE (loc);
14521       descr = loc_descriptor (varloc, mode, initialized);
14522       have_address = 1;
14523     }
14524
14525   if (!descr)
14526     return 0;
14527
14528   if (want_address == 2 && !have_address
14529       && (dwarf_version >= 4 || !dwarf_strict))
14530     {
14531       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14532         {
14533           expansion_failed (loc, NULL_RTX,
14534                             "DWARF address size mismatch");
14535           return 0;
14536         }
14537       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
14538       have_address = 1;
14539     }
14540   /* Show if we can't fill the request for an address.  */
14541   if (want_address && !have_address)
14542     {
14543       expansion_failed (loc, NULL_RTX,
14544                         "Want address and only have value");
14545       return 0;
14546     }
14547
14548   /* If we've got an address and don't want one, dereference.  */
14549   if (!want_address && have_address)
14550     {
14551       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14552       enum dwarf_location_atom op;
14553
14554       if (size > DWARF2_ADDR_SIZE || size == -1)
14555         {
14556           expansion_failed (loc, NULL_RTX,
14557                             "DWARF address size mismatch");
14558           return 0;
14559         }
14560       else if (size == DWARF2_ADDR_SIZE)
14561         op = DW_OP_deref;
14562       else
14563         op = DW_OP_deref_size;
14564
14565       add_loc_descr (&descr, new_loc_descr (op, size, 0));
14566     }
14567
14568   return descr;
14569 }
14570
14571 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
14572    if it is not possible.  */
14573
14574 static dw_loc_descr_ref
14575 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
14576 {
14577   if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
14578     return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
14579   else if (dwarf_version >= 3 || !dwarf_strict)
14580     return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
14581   else
14582     return NULL;
14583 }
14584
14585 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
14586    for VAR_LOC_NOTE for variable DECL that has been optimized by SRA.  */
14587
14588 static dw_loc_descr_ref
14589 dw_sra_loc_expr (tree decl, rtx loc)
14590 {
14591   rtx p;
14592   unsigned HOST_WIDE_INT padsize = 0;
14593   dw_loc_descr_ref descr, *descr_tail;
14594   unsigned HOST_WIDE_INT decl_size;
14595   rtx varloc;
14596   enum var_init_status initialized;
14597
14598   if (DECL_SIZE (decl) == NULL
14599       || !tree_fits_uhwi_p (DECL_SIZE (decl)))
14600     return NULL;
14601
14602   decl_size = tree_to_uhwi (DECL_SIZE (decl));
14603   descr = NULL;
14604   descr_tail = &descr;
14605
14606   for (p = loc; p; p = XEXP (p, 1))
14607     {
14608       unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
14609       rtx loc_note = *decl_piece_varloc_ptr (p);
14610       dw_loc_descr_ref cur_descr;
14611       dw_loc_descr_ref *tail, last = NULL;
14612       unsigned HOST_WIDE_INT opsize = 0;
14613
14614       if (loc_note == NULL_RTX
14615           || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
14616         {
14617           padsize += bitsize;
14618           continue;
14619         }
14620       initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
14621       varloc = NOTE_VAR_LOCATION (loc_note);
14622       cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
14623       if (cur_descr == NULL)
14624         {
14625           padsize += bitsize;
14626           continue;
14627         }
14628
14629       /* Check that cur_descr either doesn't use
14630          DW_OP_*piece operations, or their sum is equal
14631          to bitsize.  Otherwise we can't embed it.  */
14632       for (tail = &cur_descr; *tail != NULL;
14633            tail = &(*tail)->dw_loc_next)
14634         if ((*tail)->dw_loc_opc == DW_OP_piece)
14635           {
14636             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
14637                       * BITS_PER_UNIT;
14638             last = *tail;
14639           }
14640         else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
14641           {
14642             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
14643             last = *tail;
14644           }
14645
14646       if (last != NULL && opsize != bitsize)
14647         {
14648           padsize += bitsize;
14649           /* Discard the current piece of the descriptor and release any
14650              addr_table entries it uses.  */
14651           remove_loc_list_addr_table_entries (cur_descr);
14652           continue;
14653         }
14654
14655       /* If there is a hole, add DW_OP_*piece after empty DWARF
14656          expression, which means that those bits are optimized out.  */
14657       if (padsize)
14658         {
14659           if (padsize > decl_size)
14660             {
14661               remove_loc_list_addr_table_entries (cur_descr);
14662               goto discard_descr;
14663             }
14664           decl_size -= padsize;
14665           *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
14666           if (*descr_tail == NULL)
14667             {
14668               remove_loc_list_addr_table_entries (cur_descr);
14669               goto discard_descr;
14670             }
14671           descr_tail = &(*descr_tail)->dw_loc_next;
14672           padsize = 0;
14673         }
14674       *descr_tail = cur_descr;
14675       descr_tail = tail;
14676       if (bitsize > decl_size)
14677         goto discard_descr;
14678       decl_size -= bitsize;
14679       if (last == NULL)
14680         {
14681           HOST_WIDE_INT offset = 0;
14682           if (GET_CODE (varloc) == VAR_LOCATION
14683               && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14684             {
14685               varloc = PAT_VAR_LOCATION_LOC (varloc);
14686               if (GET_CODE (varloc) == EXPR_LIST)
14687                 varloc = XEXP (varloc, 0);
14688             }
14689           do 
14690             {
14691               if (GET_CODE (varloc) == CONST
14692                   || GET_CODE (varloc) == SIGN_EXTEND
14693                   || GET_CODE (varloc) == ZERO_EXTEND)
14694                 varloc = XEXP (varloc, 0);
14695               else if (GET_CODE (varloc) == SUBREG)
14696                 varloc = SUBREG_REG (varloc);
14697               else
14698                 break;
14699             }
14700           while (1);
14701           /* DW_OP_bit_size offset should be zero for register
14702              or implicit location descriptions and empty location
14703              descriptions, but for memory addresses needs big endian
14704              adjustment.  */
14705           if (MEM_P (varloc))
14706             {
14707               unsigned HOST_WIDE_INT memsize
14708                 = MEM_SIZE (varloc) * BITS_PER_UNIT;
14709               if (memsize != bitsize)
14710                 {
14711                   if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
14712                       && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
14713                     goto discard_descr;
14714                   if (memsize < bitsize)
14715                     goto discard_descr;
14716                   if (BITS_BIG_ENDIAN)
14717                     offset = memsize - bitsize;
14718                 }
14719             }
14720
14721           *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
14722           if (*descr_tail == NULL)
14723             goto discard_descr;
14724           descr_tail = &(*descr_tail)->dw_loc_next;
14725         }
14726     }
14727
14728   /* If there were any non-empty expressions, add padding till the end of
14729      the decl.  */
14730   if (descr != NULL && decl_size != 0)
14731     {
14732       *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
14733       if (*descr_tail == NULL)
14734         goto discard_descr;
14735     }
14736   return descr;
14737
14738 discard_descr:
14739   /* Discard the descriptor and release any addr_table entries it uses.  */
14740   remove_loc_list_addr_table_entries (descr);
14741   return NULL;
14742 }
14743
14744 /* Return the dwarf representation of the location list LOC_LIST of
14745    DECL.  WANT_ADDRESS has the same meaning as in loc_list_from_tree
14746    function.  */
14747
14748 static dw_loc_list_ref
14749 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
14750 {
14751   const char *endname, *secname;
14752   rtx varloc;
14753   enum var_init_status initialized;
14754   struct var_loc_node *node;
14755   dw_loc_descr_ref descr;
14756   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
14757   dw_loc_list_ref list = NULL;
14758   dw_loc_list_ref *listp = &list;
14759
14760   /* Now that we know what section we are using for a base,
14761      actually construct the list of locations.
14762      The first location information is what is passed to the
14763      function that creates the location list, and the remaining
14764      locations just get added on to that list.
14765      Note that we only know the start address for a location
14766      (IE location changes), so to build the range, we use
14767      the range [current location start, next location start].
14768      This means we have to special case the last node, and generate
14769      a range of [last location start, end of function label].  */
14770
14771   secname = secname_for_decl (decl);
14772
14773   for (node = loc_list->first; node; node = node->next)
14774     if (GET_CODE (node->loc) == EXPR_LIST
14775         || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
14776       {
14777         if (GET_CODE (node->loc) == EXPR_LIST)
14778           {
14779             /* This requires DW_OP_{,bit_}piece, which is not usable
14780                inside DWARF expressions.  */
14781             if (want_address != 2)
14782               continue;
14783             descr = dw_sra_loc_expr (decl, node->loc);
14784             if (descr == NULL)
14785               continue;
14786           }
14787         else
14788           {
14789             initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14790             varloc = NOTE_VAR_LOCATION (node->loc);
14791             descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
14792           }
14793         if (descr)
14794           {
14795             bool range_across_switch = false;
14796             /* If section switch happens in between node->label
14797                and node->next->label (or end of function) and
14798                we can't emit it as a single entry list,
14799                emit two ranges, first one ending at the end
14800                of first partition and second one starting at the
14801                beginning of second partition.  */
14802             if (node == loc_list->last_before_switch
14803                 && (node != loc_list->first || loc_list->first->next)
14804                 && current_function_decl)
14805               {
14806                 endname = cfun->fde->dw_fde_end;
14807                 range_across_switch = true;
14808               }
14809             /* The variable has a location between NODE->LABEL and
14810                NODE->NEXT->LABEL.  */
14811             else if (node->next)
14812               endname = node->next->label;
14813             /* If the variable has a location at the last label
14814                it keeps its location until the end of function.  */
14815             else if (!current_function_decl)
14816               endname = text_end_label;
14817             else
14818               {
14819                 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14820                                              current_function_funcdef_no);
14821                 endname = ggc_strdup (label_id);
14822               }
14823
14824             *listp = new_loc_list (descr, node->label, endname, secname);
14825             if (TREE_CODE (decl) == PARM_DECL
14826                 && node == loc_list->first
14827                 && NOTE_P (node->loc)
14828                 && strcmp (node->label, endname) == 0)
14829               (*listp)->force = true;
14830             listp = &(*listp)->dw_loc_next;
14831
14832             if (range_across_switch)
14833               {
14834                 if (GET_CODE (node->loc) == EXPR_LIST)
14835                   descr = dw_sra_loc_expr (decl, node->loc);
14836                 else
14837                   {
14838                     initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14839                     varloc = NOTE_VAR_LOCATION (node->loc);
14840                     descr = dw_loc_list_1 (decl, varloc, want_address,
14841                                            initialized);
14842                   }
14843                 gcc_assert (descr);
14844                 /* The variable has a location between NODE->LABEL and
14845                    NODE->NEXT->LABEL.  */
14846                 if (node->next)
14847                   endname = node->next->label;
14848                 else
14849                   endname = cfun->fde->dw_fde_second_end;
14850                 *listp = new_loc_list (descr,
14851                                        cfun->fde->dw_fde_second_begin,
14852                                        endname, secname);
14853                 listp = &(*listp)->dw_loc_next;
14854               }
14855           }
14856       }
14857
14858   /* Try to avoid the overhead of a location list emitting a location
14859      expression instead, but only if we didn't have more than one
14860      location entry in the first place.  If some entries were not
14861      representable, we don't want to pretend a single entry that was
14862      applies to the entire scope in which the variable is
14863      available.  */
14864   if (list && loc_list->first->next)
14865     gen_llsym (list);
14866
14867   return list;
14868 }
14869
14870 /* Return if the loc_list has only single element and thus can be represented
14871    as location description.   */
14872
14873 static bool
14874 single_element_loc_list_p (dw_loc_list_ref list)
14875 {
14876   gcc_assert (!list->dw_loc_next || list->ll_symbol);
14877   return !list->ll_symbol;
14878 }
14879
14880 /* To each location in list LIST add loc descr REF.  */
14881
14882 static void
14883 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
14884 {
14885   dw_loc_descr_ref copy;
14886   add_loc_descr (&list->expr, ref);
14887   list = list->dw_loc_next;
14888   while (list)
14889     {
14890       copy = ggc_alloc<dw_loc_descr_node> ();
14891       memcpy (copy, ref, sizeof (dw_loc_descr_node));
14892       add_loc_descr (&list->expr, copy);
14893       while (copy->dw_loc_next)
14894         {
14895           dw_loc_descr_ref new_copy = ggc_alloc<dw_loc_descr_node> ();
14896           memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
14897           copy->dw_loc_next = new_copy;
14898           copy = new_copy;
14899         }
14900       list = list->dw_loc_next;
14901     }
14902 }
14903
14904 /* Given two lists RET and LIST
14905    produce location list that is result of adding expression in LIST
14906    to expression in RET on each position in program.
14907    Might be destructive on both RET and LIST.
14908
14909    TODO: We handle only simple cases of RET or LIST having at most one
14910    element. General case would inolve sorting the lists in program order
14911    and merging them that will need some additional work.
14912    Adding that will improve quality of debug info especially for SRA-ed
14913    structures.  */
14914
14915 static void
14916 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
14917 {
14918   if (!list)
14919     return;
14920   if (!*ret)
14921     {
14922       *ret = list;
14923       return;
14924     }
14925   if (!list->dw_loc_next)
14926     {
14927       add_loc_descr_to_each (*ret, list->expr);
14928       return;
14929     }
14930   if (!(*ret)->dw_loc_next)
14931     {
14932       add_loc_descr_to_each (list, (*ret)->expr);
14933       *ret = list;
14934       return;
14935     }
14936   expansion_failed (NULL_TREE, NULL_RTX,
14937                     "Don't know how to merge two non-trivial"
14938                     " location lists.\n");
14939   *ret = NULL;
14940   return;
14941 }
14942
14943 /* LOC is constant expression.  Try a luck, look it up in constant
14944    pool and return its loc_descr of its address.  */
14945
14946 static dw_loc_descr_ref
14947 cst_pool_loc_descr (tree loc)
14948 {
14949   /* Get an RTL for this, if something has been emitted.  */
14950   rtx rtl = lookup_constant_def (loc);
14951
14952   if (!rtl || !MEM_P (rtl))
14953     {
14954       gcc_assert (!rtl);
14955       return 0;
14956     }
14957   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
14958
14959   /* TODO: We might get more coverage if we was actually delaying expansion
14960      of all expressions till end of compilation when constant pools are fully
14961      populated.  */
14962   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
14963     {
14964       expansion_failed (loc, NULL_RTX,
14965                         "CST value in contant pool but not marked.");
14966       return 0;
14967     }
14968   return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
14969                              GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
14970 }
14971
14972 /* Return dw_loc_list representing address of addr_expr LOC
14973    by looking for inner INDIRECT_REF expression and turning
14974    it into simple arithmetics.
14975
14976    See loc_list_from_tree for the meaning of CONTEXT.  */
14977
14978 static dw_loc_list_ref
14979 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
14980                                                    const loc_descr_context *context)
14981 {
14982   tree obj, offset;
14983   HOST_WIDE_INT bitsize, bitpos, bytepos;
14984   machine_mode mode;
14985   int unsignedp, reversep, volatilep = 0;
14986   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14987
14988   obj = get_inner_reference (TREE_OPERAND (loc, 0),
14989                              &bitsize, &bitpos, &offset, &mode,
14990                              &unsignedp, &reversep, &volatilep, false);
14991   STRIP_NOPS (obj);
14992   if (bitpos % BITS_PER_UNIT)
14993     {
14994       expansion_failed (loc, NULL_RTX, "bitfield access");
14995       return 0;
14996     }
14997   if (!INDIRECT_REF_P (obj))
14998     {
14999       expansion_failed (obj,
15000                         NULL_RTX, "no indirect ref in inner refrence");
15001       return 0;
15002     }
15003   if (!offset && !bitpos)
15004     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
15005                                    context);
15006   else if (toplev
15007            && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
15008            && (dwarf_version >= 4 || !dwarf_strict))
15009     {
15010       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
15011       if (!list_ret)
15012         return 0;
15013       if (offset)
15014         {
15015           /* Variable offset.  */
15016           list_ret1 = loc_list_from_tree (offset, 0, context);
15017           if (list_ret1 == 0)
15018             return 0;
15019           add_loc_list (&list_ret, list_ret1);
15020           if (!list_ret)
15021             return 0;
15022           add_loc_descr_to_each (list_ret,
15023                                  new_loc_descr (DW_OP_plus, 0, 0));
15024         }
15025       bytepos = bitpos / BITS_PER_UNIT;
15026       if (bytepos > 0)
15027         add_loc_descr_to_each (list_ret,
15028                                new_loc_descr (DW_OP_plus_uconst,
15029                                               bytepos, 0));
15030       else if (bytepos < 0)
15031         loc_list_plus_const (list_ret, bytepos);
15032       add_loc_descr_to_each (list_ret,
15033                              new_loc_descr (DW_OP_stack_value, 0, 0));
15034     }
15035   return list_ret;
15036 }
15037
15038 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
15039    all operations from LOC are nops, move to the last one.  Insert in NOPS all
15040    operations that are skipped.  */
15041
15042 static void
15043 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
15044                           hash_set<dw_loc_descr_ref> &nops)
15045 {
15046   while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
15047     {
15048       nops.add (loc);
15049       loc = loc->dw_loc_next;
15050     }
15051 }
15052
15053 /* Helper for loc_descr_without_nops: free the location description operation
15054    P.  */
15055
15056 bool
15057 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
15058 {
15059   ggc_free (loc);
15060   return true;
15061 }
15062
15063 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
15064    finishes LOC.  */
15065
15066 static void
15067 loc_descr_without_nops (dw_loc_descr_ref &loc)
15068 {
15069   if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
15070     return;
15071
15072   /* Set of all DW_OP_nop operations we remove.  */
15073   hash_set<dw_loc_descr_ref> nops;
15074
15075   /* First, strip all prefix NOP operations in order to keep the head of the
15076      operations list.  */
15077   loc_descr_to_next_no_nop (loc, nops);
15078
15079   for (dw_loc_descr_ref cur = loc; cur != NULL;)
15080     {
15081       /* For control flow operations: strip "prefix" nops in destination
15082          labels.  */
15083       if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
15084         loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
15085       if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
15086         loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
15087
15088       /* Do the same for the operations that follow, then move to the next
15089          iteration.  */
15090       if (cur->dw_loc_next != NULL)
15091         loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
15092       cur = cur->dw_loc_next;
15093     }
15094
15095   nops.traverse<void *, free_loc_descr> (NULL);
15096 }
15097
15098
15099 struct dwarf_procedure_info;
15100
15101 /* Helper structure for location descriptions generation.  */
15102 struct loc_descr_context
15103 {
15104   /* The type that is implicitly referenced by DW_OP_push_object_address, or
15105      NULL_TREE if DW_OP_push_object_address in invalid for this location
15106      description.  This is used when processing PLACEHOLDER_EXPR nodes.  */
15107   tree context_type;
15108   /* The ..._DECL node that should be translated as a
15109      DW_OP_push_object_address operation.  */
15110   tree base_decl;
15111   /* Information about the DWARF procedure we are currently generating. NULL if
15112      we are not generating a DWARF procedure.  */
15113   struct dwarf_procedure_info *dpi;
15114 };
15115
15116 /* DWARF procedures generation
15117
15118    DWARF expressions (aka. location descriptions) are used to encode variable
15119    things such as sizes or offsets.  Such computations can have redundant parts
15120    that can be factorized in order to reduce the size of the output debug
15121    information.  This is the whole point of DWARF procedures.
15122
15123    Thanks to stor-layout.c, size and offset expressions in GENERIC trees are
15124    already factorized into functions ("size functions") in order to handle very
15125    big and complex types.  Such functions are quite simple: they have integral
15126    arguments, they return an integral result and their body contains only a
15127    return statement with arithmetic expressions.  This is the only kind of
15128    function we are interested in translating into DWARF procedures, here.
15129
15130    DWARF expressions and DWARF procedure are executed using a stack, so we have
15131    to define some calling convention for them to interact.  Let's say that:
15132
15133    - Before calling a DWARF procedure, DWARF expressions must push on the stack
15134      all arguments in reverse order (right-to-left) so that when the DWARF
15135      procedure execution starts, the first argument is the top of the stack.
15136
15137    - Then, when returning, the DWARF procedure must have consumed all arguments
15138      on the stack, must have pushed the result and touched nothing else.
15139
15140    - Each integral argument and the result are integral types can be hold in a
15141      single stack slot.
15142
15143    - We call "frame offset" the number of stack slots that are "under DWARF
15144      procedure control": it includes the arguments slots, the temporaries and
15145      the result slot. Thus, it is equal to the number of arguments when the
15146      procedure execution starts and must be equal to one (the result) when it
15147      returns.  */
15148
15149 /* Helper structure used when generating operations for a DWARF procedure.  */
15150 struct dwarf_procedure_info
15151 {
15152   /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
15153      currently translated.  */
15154   tree fndecl;
15155   /* The number of arguments FNDECL takes.  */
15156   unsigned args_count;
15157 };
15158
15159 /* Return a pointer to a newly created DIE node for a DWARF procedure.  Add
15160    LOCATION as its DW_AT_location attribute.  If FNDECL is not NULL_TREE,
15161    equate it to this DIE.  */
15162
15163 static dw_die_ref
15164 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
15165                     dw_die_ref parent_die)
15166 {
15167   const bool dwarf_proc_supported = dwarf_version >= 4;
15168   dw_die_ref dwarf_proc_die;
15169
15170   if ((dwarf_version < 3 && dwarf_strict)
15171       || location == NULL)
15172     return NULL;
15173
15174   dwarf_proc_die  = new_die (dwarf_proc_supported
15175                              ? DW_TAG_dwarf_procedure
15176                              : DW_TAG_variable,
15177                              parent_die,
15178                              fndecl);
15179   if (fndecl)
15180     equate_decl_number_to_die (fndecl, dwarf_proc_die);
15181   if (!dwarf_proc_supported)
15182     add_AT_flag (dwarf_proc_die, DW_AT_artificial, 1);
15183   add_AT_loc (dwarf_proc_die, DW_AT_location, location);
15184   return dwarf_proc_die;
15185 }
15186
15187 /* Return whether TYPE is a supported type as a DWARF procedure argument
15188    type or return type (we handle only scalar types and pointer types that
15189    aren't wider than the DWARF expression evaluation stack.  */
15190
15191 static bool
15192 is_handled_procedure_type (tree type)
15193 {
15194   return ((INTEGRAL_TYPE_P (type)
15195            || TREE_CODE (type) == OFFSET_TYPE
15196            || TREE_CODE (type) == POINTER_TYPE)
15197           && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
15198 }
15199
15200 /* Helper for resolve_args_picking.  Stop when coming across VISITED nodes.  */
15201
15202 static bool
15203 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
15204                         struct dwarf_procedure_info *dpi,
15205                         hash_set<dw_loc_descr_ref> &visited)
15206 {
15207   /* The "frame_offset" identifier is already used to name a macro... */
15208   unsigned frame_offset_ = initial_frame_offset;
15209   dw_loc_descr_ref l;
15210
15211   for (l = loc; l != NULL;)
15212     {
15213       /* If we already met this node, there is nothing to compute anymore.  */
15214       if (visited.add (l))
15215         {
15216 #if ENABLE_CHECKING
15217           /* Make sure that the stack size is consistent wherever the execution
15218              flow comes from.  */
15219           gcc_assert ((unsigned) l->dw_loc_frame_offset == frame_offset_);
15220 #endif
15221           break;
15222         }
15223 #if ENABLE_CHECKING
15224       l->dw_loc_frame_offset = frame_offset_;
15225 #endif
15226
15227       /* If needed, relocate the picking offset with respect to the frame
15228          offset. */
15229       if (l->dw_loc_opc == DW_OP_pick && l->frame_offset_rel)
15230         {
15231           /* frame_offset_ is the size of the current stack frame, including
15232              incoming arguments. Besides, the arguments are pushed
15233              right-to-left.  Thus, in order to access the Nth argument from
15234              this operation node, the picking has to skip temporaries *plus*
15235              one stack slot per argument (0 for the first one, 1 for the second
15236              one, etc.).
15237
15238              The targetted argument number (N) is already set as the operand,
15239              and the number of temporaries can be computed with:
15240                frame_offsets_ - dpi->args_count */
15241           l->dw_loc_oprnd1.v.val_unsigned += frame_offset_ - dpi->args_count;
15242
15243           /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)...  */
15244           if (l->dw_loc_oprnd1.v.val_unsigned > 255)
15245             return false;
15246         }
15247
15248       /* Update frame_offset according to the effect the current operation has
15249          on the stack.  */
15250       switch (l->dw_loc_opc)
15251         {
15252         case DW_OP_deref:
15253         case DW_OP_swap:
15254         case DW_OP_rot:
15255         case DW_OP_abs:
15256         case DW_OP_not:
15257         case DW_OP_plus_uconst:
15258         case DW_OP_skip:
15259         case DW_OP_reg0:
15260         case DW_OP_reg1:
15261         case DW_OP_reg2:
15262         case DW_OP_reg3:
15263         case DW_OP_reg4:
15264         case DW_OP_reg5:
15265         case DW_OP_reg6:
15266         case DW_OP_reg7:
15267         case DW_OP_reg8:
15268         case DW_OP_reg9:
15269         case DW_OP_reg10:
15270         case DW_OP_reg11:
15271         case DW_OP_reg12:
15272         case DW_OP_reg13:
15273         case DW_OP_reg14:
15274         case DW_OP_reg15:
15275         case DW_OP_reg16:
15276         case DW_OP_reg17:
15277         case DW_OP_reg18:
15278         case DW_OP_reg19:
15279         case DW_OP_reg20:
15280         case DW_OP_reg21:
15281         case DW_OP_reg22:
15282         case DW_OP_reg23:
15283         case DW_OP_reg24:
15284         case DW_OP_reg25:
15285         case DW_OP_reg26:
15286         case DW_OP_reg27:
15287         case DW_OP_reg28:
15288         case DW_OP_reg29:
15289         case DW_OP_reg30:
15290         case DW_OP_reg31:
15291         case DW_OP_bregx:
15292         case DW_OP_piece:
15293         case DW_OP_deref_size:
15294         case DW_OP_nop:
15295         case DW_OP_form_tls_address:
15296         case DW_OP_bit_piece:
15297         case DW_OP_implicit_value:
15298         case DW_OP_stack_value:
15299           break;
15300
15301         case DW_OP_addr:
15302         case DW_OP_const1u:
15303         case DW_OP_const1s:
15304         case DW_OP_const2u:
15305         case DW_OP_const2s:
15306         case DW_OP_const4u:
15307         case DW_OP_const4s:
15308         case DW_OP_const8u:
15309         case DW_OP_const8s:
15310         case DW_OP_constu:
15311         case DW_OP_consts:
15312         case DW_OP_dup:
15313         case DW_OP_over:
15314         case DW_OP_pick:
15315         case DW_OP_lit0:
15316         case DW_OP_lit1:
15317         case DW_OP_lit2:
15318         case DW_OP_lit3:
15319         case DW_OP_lit4:
15320         case DW_OP_lit5:
15321         case DW_OP_lit6:
15322         case DW_OP_lit7:
15323         case DW_OP_lit8:
15324         case DW_OP_lit9:
15325         case DW_OP_lit10:
15326         case DW_OP_lit11:
15327         case DW_OP_lit12:
15328         case DW_OP_lit13:
15329         case DW_OP_lit14:
15330         case DW_OP_lit15:
15331         case DW_OP_lit16:
15332         case DW_OP_lit17:
15333         case DW_OP_lit18:
15334         case DW_OP_lit19:
15335         case DW_OP_lit20:
15336         case DW_OP_lit21:
15337         case DW_OP_lit22:
15338         case DW_OP_lit23:
15339         case DW_OP_lit24:
15340         case DW_OP_lit25:
15341         case DW_OP_lit26:
15342         case DW_OP_lit27:
15343         case DW_OP_lit28:
15344         case DW_OP_lit29:
15345         case DW_OP_lit30:
15346         case DW_OP_lit31:
15347         case DW_OP_breg0:
15348         case DW_OP_breg1:
15349         case DW_OP_breg2:
15350         case DW_OP_breg3:
15351         case DW_OP_breg4:
15352         case DW_OP_breg5:
15353         case DW_OP_breg6:
15354         case DW_OP_breg7:
15355         case DW_OP_breg8:
15356         case DW_OP_breg9:
15357         case DW_OP_breg10:
15358         case DW_OP_breg11:
15359         case DW_OP_breg12:
15360         case DW_OP_breg13:
15361         case DW_OP_breg14:
15362         case DW_OP_breg15:
15363         case DW_OP_breg16:
15364         case DW_OP_breg17:
15365         case DW_OP_breg18:
15366         case DW_OP_breg19:
15367         case DW_OP_breg20:
15368         case DW_OP_breg21:
15369         case DW_OP_breg22:
15370         case DW_OP_breg23:
15371         case DW_OP_breg24:
15372         case DW_OP_breg25:
15373         case DW_OP_breg26:
15374         case DW_OP_breg27:
15375         case DW_OP_breg28:
15376         case DW_OP_breg29:
15377         case DW_OP_breg30:
15378         case DW_OP_breg31:
15379         case DW_OP_fbreg:
15380         case DW_OP_push_object_address:
15381         case DW_OP_call_frame_cfa:
15382           ++frame_offset_;
15383           break;
15384
15385         case DW_OP_drop:
15386         case DW_OP_xderef:
15387         case DW_OP_and:
15388         case DW_OP_div:
15389         case DW_OP_minus:
15390         case DW_OP_mod:
15391         case DW_OP_mul:
15392         case DW_OP_neg:
15393         case DW_OP_or:
15394         case DW_OP_plus:
15395         case DW_OP_shl:
15396         case DW_OP_shr:
15397         case DW_OP_shra:
15398         case DW_OP_xor:
15399         case DW_OP_bra:
15400         case DW_OP_eq:
15401         case DW_OP_ge:
15402         case DW_OP_gt:
15403         case DW_OP_le:
15404         case DW_OP_lt:
15405         case DW_OP_ne:
15406         case DW_OP_regx:
15407         case DW_OP_xderef_size:
15408           --frame_offset_;
15409           break;
15410
15411         case DW_OP_call2:
15412         case DW_OP_call4:
15413         case DW_OP_call_ref:
15414           {
15415             dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
15416             int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
15417
15418             if (stack_usage == NULL)
15419               return false;
15420             frame_offset += *stack_usage;
15421             break;
15422           }
15423
15424         case DW_OP_GNU_push_tls_address:
15425         case DW_OP_GNU_uninit:
15426         case DW_OP_GNU_encoded_addr:
15427         case DW_OP_GNU_implicit_pointer:
15428         case DW_OP_GNU_entry_value:
15429         case DW_OP_GNU_const_type:
15430         case DW_OP_GNU_regval_type:
15431         case DW_OP_GNU_deref_type:
15432         case DW_OP_GNU_convert:
15433         case DW_OP_GNU_reinterpret:
15434         case DW_OP_GNU_parameter_ref:
15435           /* loc_list_from_tree will probably not output these operations for
15436              size functions, so assume they will not appear here.  */
15437           /* Fall through...  */
15438
15439         default:
15440           gcc_unreachable ();
15441         }
15442
15443       /* Now, follow the control flow (except subroutine calls).  */
15444       switch (l->dw_loc_opc)
15445         {
15446         case DW_OP_bra:
15447           if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
15448                                        visited))
15449             return false;
15450           /* Fall through... */
15451
15452         case DW_OP_skip:
15453           l = l->dw_loc_oprnd1.v.val_loc;
15454           break;
15455
15456         case DW_OP_stack_value:
15457           return true;
15458
15459         default:
15460           l = l->dw_loc_next;
15461           break;
15462         }
15463     }
15464
15465   return true;
15466 }
15467
15468 /* Make a DFS over operations reachable through LOC (i.e. follow branch
15469    operations) in order to resolve the operand of DW_OP_pick operations that
15470    target DWARF procedure arguments (DPI).  Stop at already visited nodes.
15471    INITIAL_FRAME_OFFSET is the frame offset *before* LOC is executed.  Return
15472    if all relocations were successful.  */
15473
15474 static bool
15475 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
15476                       struct dwarf_procedure_info *dpi)
15477 {
15478   hash_set<dw_loc_descr_ref> visited;
15479
15480   return resolve_args_picking_1 (loc, initial_frame_offset, dpi, visited);
15481 }
15482
15483 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
15484    Return NULL if it is not possible.  */
15485
15486 static dw_die_ref
15487 function_to_dwarf_procedure (tree fndecl)
15488 {
15489   struct loc_descr_context ctx;
15490   struct dwarf_procedure_info dpi;
15491   dw_die_ref dwarf_proc_die;
15492   tree tree_body = DECL_SAVED_TREE (fndecl);
15493   dw_loc_descr_ref loc_body, epilogue;
15494
15495   tree cursor;
15496   unsigned i;
15497
15498   /* Do not generate multiple DWARF procedures for the same function
15499      declaration.  */
15500   dwarf_proc_die = lookup_decl_die (fndecl);
15501   if (dwarf_proc_die != NULL)
15502     return dwarf_proc_die;
15503
15504   /* DWARF procedures are available starting with the DWARFv3 standard, but
15505      it's the DWARFv4 standard that introduces the DW_TAG_dwarf_procedure
15506      DIE.  */
15507   if (dwarf_version < 3 && dwarf_strict)
15508     return NULL;
15509
15510   /* We handle only functions for which we still have a body, that return a
15511      supported type and that takes arguments with supported types.  Note that
15512      there is no point translating functions that return nothing.  */
15513   if (tree_body == NULL_TREE
15514       || DECL_RESULT (fndecl) == NULL_TREE
15515       || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
15516     return NULL;
15517
15518   for (cursor = DECL_ARGUMENTS (fndecl);
15519        cursor != NULL_TREE;
15520        cursor = TREE_CHAIN (cursor))
15521     if (!is_handled_procedure_type (TREE_TYPE (cursor)))
15522       return NULL;
15523
15524   /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)).  */
15525   if (TREE_CODE (tree_body) != RETURN_EXPR)
15526     return NULL;
15527   tree_body = TREE_OPERAND (tree_body, 0);
15528   if (TREE_CODE (tree_body) != MODIFY_EXPR
15529       || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
15530     return NULL;
15531   tree_body = TREE_OPERAND (tree_body, 1);
15532
15533   /* Try to translate the body expression itself.  Note that this will probably
15534      cause an infinite recursion if its call graph has a cycle.  This is very
15535      unlikely for size functions, however, so don't bother with such things at
15536      the moment.  */
15537   ctx.context_type = NULL_TREE;
15538   ctx.base_decl = NULL_TREE;
15539   ctx.dpi = &dpi;
15540   dpi.fndecl = fndecl;
15541   dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
15542   loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
15543   if (!loc_body)
15544     return NULL;
15545
15546   /* After evaluating all operands in "loc_body", we should still have on the
15547      stack all arguments plus the desired function result (top of the stack).
15548      Generate code in order to keep only the result in our stack frame.  */
15549   epilogue = NULL;
15550   for (i = 0; i < dpi.args_count; ++i)
15551     {
15552       dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
15553       op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
15554       op_couple->dw_loc_next->dw_loc_next = epilogue;
15555       epilogue = op_couple;
15556     }
15557   add_loc_descr (&loc_body, epilogue);
15558   if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
15559     return NULL;
15560
15561   /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
15562      because they are considered useful.  Now there is an epilogue, they are
15563      not anymore, so give it another try.   */
15564   loc_descr_without_nops (loc_body);
15565
15566   /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
15567      a DW_TAG_dwarf_procedure, so we may have a conflict, here.  It's unlikely,
15568      though, given that size functions do not come from source, so they should
15569      not have a dedicated DW_TAG_subprogram DIE.  */
15570   dwarf_proc_die
15571     = new_dwarf_proc_die (loc_body, fndecl,
15572                           get_context_die (DECL_CONTEXT (fndecl)));
15573
15574   /* The called DWARF procedure consumes one stack slot per argument and
15575      returns one stack slot.  */
15576   dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
15577
15578   return dwarf_proc_die;
15579 }
15580
15581
15582 /* Generate Dwarf location list representing LOC.
15583    If WANT_ADDRESS is false, expression computing LOC will be computed
15584    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
15585    if WANT_ADDRESS is 2, expression computing address useable in location
15586      will be returned (i.e. DW_OP_reg can be used
15587      to refer to register values).
15588
15589    CONTEXT provides information to customize the location descriptions
15590    generation.  Its context_type field specifies what type is implicitly
15591    referenced by DW_OP_push_object_address.  If it is NULL_TREE, this operation
15592    will not be generated.
15593
15594    Its DPI field determines whether we are generating a DWARF expression for a
15595    DWARF procedure, so PARM_DECL references are processed specifically.
15596
15597    If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
15598    and dpi fields were null.  */
15599
15600 static dw_loc_list_ref
15601 loc_list_from_tree_1 (tree loc, int want_address,
15602                       const struct loc_descr_context *context)
15603 {
15604   dw_loc_descr_ref ret = NULL, ret1 = NULL;
15605   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15606   int have_address = 0;
15607   enum dwarf_location_atom op;
15608
15609   /* ??? Most of the time we do not take proper care for sign/zero
15610      extending the values properly.  Hopefully this won't be a real
15611      problem...  */
15612
15613   if (context != NULL
15614       && context->base_decl == loc
15615       && want_address == 0)
15616     {
15617       if (dwarf_version >= 3 || !dwarf_strict)
15618         return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
15619                              NULL, NULL, NULL);
15620       else
15621         return NULL;
15622     }
15623
15624   switch (TREE_CODE (loc))
15625     {
15626     case ERROR_MARK:
15627       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
15628       return 0;
15629
15630     case PLACEHOLDER_EXPR:
15631       /* This case involves extracting fields from an object to determine the
15632          position of other fields. It is supposed to appear only as the first
15633          operand of COMPONENT_REF nodes and to reference precisely the type
15634          that the context allows.  */
15635       if (context != NULL
15636           && TREE_TYPE (loc) == context->context_type
15637           && want_address >= 1)
15638         {
15639           if (dwarf_version >= 3 || !dwarf_strict)
15640             {
15641               ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
15642               have_address = 1;
15643               break;
15644             }
15645           else
15646             return NULL;
15647         }
15648       else
15649         expansion_failed (loc, NULL_RTX,
15650                           "PLACEHOLDER_EXPR for an unexpected type");
15651       break;
15652
15653     case CALL_EXPR:
15654         {
15655           const int nargs = call_expr_nargs (loc);
15656           tree callee = get_callee_fndecl (loc);
15657           int i;
15658           dw_die_ref dwarf_proc;
15659
15660           if (callee == NULL_TREE)
15661             goto call_expansion_failed;
15662
15663           /* We handle only functions that return an integer.  */
15664           if (!is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee))))
15665             goto call_expansion_failed;
15666
15667           dwarf_proc = function_to_dwarf_procedure (callee);
15668           if (dwarf_proc == NULL)
15669             goto call_expansion_failed;
15670
15671           /* Evaluate arguments right-to-left so that the first argument will
15672              be the top-most one on the stack.  */
15673           for (i = nargs - 1; i >= 0; --i)
15674             {
15675               dw_loc_descr_ref loc_descr
15676                 = loc_descriptor_from_tree (CALL_EXPR_ARG (loc, i), 0,
15677                                             context);
15678
15679               if (loc_descr == NULL)
15680                 goto call_expansion_failed;
15681
15682               add_loc_descr (&ret, loc_descr);
15683             }
15684
15685           ret1 = new_loc_descr (DW_OP_call4, 0, 0);
15686           ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15687           ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
15688           ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
15689           add_loc_descr (&ret, ret1);
15690           break;
15691
15692         call_expansion_failed:
15693           expansion_failed (loc, NULL_RTX, "CALL_EXPR");
15694           /* There are no opcodes for these operations.  */
15695           return 0;
15696         }
15697
15698     case PREINCREMENT_EXPR:
15699     case PREDECREMENT_EXPR:
15700     case POSTINCREMENT_EXPR:
15701     case POSTDECREMENT_EXPR:
15702       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
15703       /* There are no opcodes for these operations.  */
15704       return 0;
15705
15706     case ADDR_EXPR:
15707       /* If we already want an address, see if there is INDIRECT_REF inside
15708          e.g. for &this->field.  */
15709       if (want_address)
15710         {
15711           list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
15712                        (loc, want_address == 2, context);
15713           if (list_ret)
15714             have_address = 1;
15715           else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
15716                    && (ret = cst_pool_loc_descr (loc)))
15717             have_address = 1;
15718         }
15719         /* Otherwise, process the argument and look for the address.  */
15720       if (!list_ret && !ret)
15721         list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
15722       else
15723         {
15724           if (want_address)
15725             expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
15726           return NULL;
15727         }
15728       break;
15729
15730     case VAR_DECL:
15731       if (DECL_THREAD_LOCAL_P (loc))
15732         {
15733           rtx rtl;
15734          enum dwarf_location_atom tls_op;
15735          enum dtprel_bool dtprel = dtprel_false;
15736
15737           if (targetm.have_tls)
15738             {
15739               /* If this is not defined, we have no way to emit the
15740                  data.  */
15741               if (!targetm.asm_out.output_dwarf_dtprel)
15742                 return 0;
15743
15744                /* The way DW_OP_GNU_push_tls_address is specified, we
15745                   can only look up addresses of objects in the current
15746                   module.  We used DW_OP_addr as first op, but that's
15747                   wrong, because DW_OP_addr is relocated by the debug
15748                   info consumer, while DW_OP_GNU_push_tls_address
15749                   operand shouldn't be.  */
15750               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
15751                 return 0;
15752              dtprel = dtprel_true;
15753              tls_op = DW_OP_GNU_push_tls_address;
15754             }
15755           else
15756             {
15757               if (!targetm.emutls.debug_form_tls_address
15758                   || !(dwarf_version >= 3 || !dwarf_strict))
15759                 return 0;
15760               /* We stuffed the control variable into the DECL_VALUE_EXPR
15761                  to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
15762                  no longer appear in gimple code.  We used the control
15763                  variable in specific so that we could pick it up here.  */
15764               loc = DECL_VALUE_EXPR (loc);
15765               tls_op = DW_OP_form_tls_address;
15766             }
15767
15768           rtl = rtl_for_decl_location (loc);
15769           if (rtl == NULL_RTX)
15770             return 0;
15771
15772           if (!MEM_P (rtl))
15773             return 0;
15774           rtl = XEXP (rtl, 0);
15775           if (! CONSTANT_P (rtl))
15776             return 0;
15777
15778           ret = new_addr_loc_descr (rtl, dtprel);
15779           ret1 = new_loc_descr (tls_op, 0, 0);
15780           add_loc_descr (&ret, ret1);
15781
15782           have_address = 1;
15783           break;
15784         }
15785       /* FALLTHRU */
15786
15787     case PARM_DECL:
15788       if (context != NULL && context->dpi != NULL
15789           && DECL_CONTEXT (loc) == context->dpi->fndecl)
15790         {
15791           /* We are generating code for a DWARF procedure and we want to access
15792              one of its arguments: find the appropriate argument offset and let
15793              the resolve_args_picking pass compute the offset that complies
15794              with the stack frame size.  */
15795           unsigned i = 0;
15796           tree cursor;
15797
15798           for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
15799                cursor != NULL_TREE && cursor != loc;
15800                cursor = TREE_CHAIN (cursor), ++i)
15801             ;
15802           /* If we are translating a DWARF procedure, all referenced parameters
15803              must belong to the current function.  */
15804           gcc_assert (cursor != NULL_TREE);
15805
15806           ret = new_loc_descr (DW_OP_pick, i, 0);
15807           ret->frame_offset_rel = 1;
15808           break;
15809         }
15810       /* FALLTHRU */
15811
15812     case RESULT_DECL:
15813       if (DECL_HAS_VALUE_EXPR_P (loc))
15814         return loc_list_from_tree_1 (DECL_VALUE_EXPR (loc),
15815                                      want_address, context);
15816       /* FALLTHRU */
15817
15818     case FUNCTION_DECL:
15819       {
15820         rtx rtl;
15821         var_loc_list *loc_list = lookup_decl_loc (loc);
15822
15823         if (loc_list && loc_list->first)
15824           {
15825             list_ret = dw_loc_list (loc_list, loc, want_address);
15826             have_address = want_address != 0;
15827             break;
15828           }
15829         rtl = rtl_for_decl_location (loc);
15830         if (rtl == NULL_RTX)
15831           {
15832             expansion_failed (loc, NULL_RTX, "DECL has no RTL");
15833             return 0;
15834           }
15835         else if (CONST_INT_P (rtl))
15836           {
15837             HOST_WIDE_INT val = INTVAL (rtl);
15838             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15839               val &= GET_MODE_MASK (DECL_MODE (loc));
15840             ret = int_loc_descriptor (val);
15841           }
15842         else if (GET_CODE (rtl) == CONST_STRING)
15843           {
15844             expansion_failed (loc, NULL_RTX, "CONST_STRING");
15845             return 0;
15846           }
15847         else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
15848           ret = new_addr_loc_descr (rtl, dtprel_false);
15849         else
15850           {
15851             machine_mode mode, mem_mode;
15852
15853             /* Certain constructs can only be represented at top-level.  */
15854             if (want_address == 2)
15855               {
15856                 ret = loc_descriptor (rtl, VOIDmode,
15857                                       VAR_INIT_STATUS_INITIALIZED);
15858                 have_address = 1;
15859               }
15860             else
15861               {
15862                 mode = GET_MODE (rtl);
15863                 mem_mode = VOIDmode;
15864                 if (MEM_P (rtl))
15865                   {
15866                     mem_mode = mode;
15867                     mode = get_address_mode (rtl);
15868                     rtl = XEXP (rtl, 0);
15869                     have_address = 1;
15870                   }
15871                 ret = mem_loc_descriptor (rtl, mode, mem_mode,
15872                                           VAR_INIT_STATUS_INITIALIZED);
15873               }
15874             if (!ret)
15875               expansion_failed (loc, rtl,
15876                                 "failed to produce loc descriptor for rtl");
15877           }
15878       }
15879       break;
15880
15881     case MEM_REF:
15882       if (!integer_zerop (TREE_OPERAND (loc, 1)))
15883         {
15884           have_address = 1;
15885           goto do_plus;
15886         }
15887       /* Fallthru.  */
15888     case INDIRECT_REF:
15889       list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
15890       have_address = 1;
15891       break;
15892
15893     case TARGET_MEM_REF:
15894     case SSA_NAME:
15895     case DEBUG_EXPR_DECL:
15896       return NULL;
15897
15898     case COMPOUND_EXPR:
15899       return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
15900                                    context);
15901
15902     CASE_CONVERT:
15903     case VIEW_CONVERT_EXPR:
15904     case SAVE_EXPR:
15905     case MODIFY_EXPR:
15906     case NON_LVALUE_EXPR:
15907       return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
15908                                    context);
15909
15910     case COMPONENT_REF:
15911     case BIT_FIELD_REF:
15912     case ARRAY_REF:
15913     case ARRAY_RANGE_REF:
15914     case REALPART_EXPR:
15915     case IMAGPART_EXPR:
15916       {
15917         tree obj, offset;
15918         HOST_WIDE_INT bitsize, bitpos, bytepos;
15919         machine_mode mode;
15920         int unsignedp, reversep, volatilep = 0;
15921
15922         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
15923                                    &unsignedp, &reversep, &volatilep, false);
15924
15925         gcc_assert (obj != loc);
15926
15927         list_ret = loc_list_from_tree_1 (obj,
15928                                          want_address == 2
15929                                          && !bitpos && !offset ? 2 : 1,
15930                                          context);
15931         /* TODO: We can extract value of the small expression via shifting even
15932            for nonzero bitpos.  */
15933         if (list_ret == 0)
15934           return 0;
15935         if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
15936           {
15937             expansion_failed (loc, NULL_RTX,
15938                               "bitfield access");
15939             return 0;
15940           }
15941
15942         if (offset != NULL_TREE)
15943           {
15944             /* Variable offset.  */
15945             list_ret1 = loc_list_from_tree_1 (offset, 0, context);
15946             if (list_ret1 == 0)
15947               return 0;
15948             add_loc_list (&list_ret, list_ret1);
15949             if (!list_ret)
15950               return 0;
15951             add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
15952           }
15953
15954         bytepos = bitpos / BITS_PER_UNIT;
15955         if (bytepos > 0)
15956           add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
15957         else if (bytepos < 0)
15958           loc_list_plus_const (list_ret, bytepos);
15959
15960         have_address = 1;
15961         break;
15962       }
15963
15964     case INTEGER_CST:
15965       if ((want_address || !tree_fits_shwi_p (loc))
15966           && (ret = cst_pool_loc_descr (loc)))
15967         have_address = 1;
15968       else if (want_address == 2
15969                && tree_fits_shwi_p (loc)
15970                && (ret = address_of_int_loc_descriptor
15971                            (int_size_in_bytes (TREE_TYPE (loc)),
15972                             tree_to_shwi (loc))))
15973         have_address = 1;
15974       else if (tree_fits_shwi_p (loc))
15975         ret = int_loc_descriptor (tree_to_shwi (loc));
15976       else if (tree_fits_uhwi_p (loc))
15977         ret = uint_loc_descriptor (tree_to_uhwi (loc));
15978       else
15979         {
15980           expansion_failed (loc, NULL_RTX,
15981                             "Integer operand is not host integer");
15982           return 0;
15983         }
15984       break;
15985
15986     case CONSTRUCTOR:
15987     case REAL_CST:
15988     case STRING_CST:
15989     case COMPLEX_CST:
15990       if ((ret = cst_pool_loc_descr (loc)))
15991         have_address = 1;
15992       else
15993       /* We can construct small constants here using int_loc_descriptor.  */
15994         expansion_failed (loc, NULL_RTX,
15995                           "constructor or constant not in constant pool");
15996       break;
15997
15998     case TRUTH_AND_EXPR:
15999     case TRUTH_ANDIF_EXPR:
16000     case BIT_AND_EXPR:
16001       op = DW_OP_and;
16002       goto do_binop;
16003
16004     case TRUTH_XOR_EXPR:
16005     case BIT_XOR_EXPR:
16006       op = DW_OP_xor;
16007       goto do_binop;
16008
16009     case TRUTH_OR_EXPR:
16010     case TRUTH_ORIF_EXPR:
16011     case BIT_IOR_EXPR:
16012       op = DW_OP_or;
16013       goto do_binop;
16014
16015     case FLOOR_DIV_EXPR:
16016     case CEIL_DIV_EXPR:
16017     case ROUND_DIV_EXPR:
16018     case TRUNC_DIV_EXPR:
16019     case EXACT_DIV_EXPR:
16020       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
16021         return 0;
16022       op = DW_OP_div;
16023       goto do_binop;
16024
16025     case MINUS_EXPR:
16026       op = DW_OP_minus;
16027       goto do_binop;
16028
16029     case FLOOR_MOD_EXPR:
16030     case CEIL_MOD_EXPR:
16031     case ROUND_MOD_EXPR:
16032     case TRUNC_MOD_EXPR:
16033       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
16034         {
16035           op = DW_OP_mod;
16036           goto do_binop;
16037         }
16038       list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16039       list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
16040       if (list_ret == 0 || list_ret1 == 0)
16041         return 0;
16042
16043       add_loc_list (&list_ret, list_ret1);
16044       if (list_ret == 0)
16045         return 0;
16046       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
16047       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
16048       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
16049       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
16050       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
16051       break;
16052
16053     case MULT_EXPR:
16054       op = DW_OP_mul;
16055       goto do_binop;
16056
16057     case LSHIFT_EXPR:
16058       op = DW_OP_shl;
16059       goto do_binop;
16060
16061     case RSHIFT_EXPR:
16062       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
16063       goto do_binop;
16064
16065     case POINTER_PLUS_EXPR:
16066     case PLUS_EXPR:
16067     do_plus:
16068       if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
16069         {
16070           /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
16071              smarter to encode their opposite.  The DW_OP_plus_uconst operation
16072              takes 1 + X bytes, X being the size of the ULEB128 addend.  On the
16073              other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
16074              bytes, Y being the size of the operation that pushes the opposite
16075              of the addend.  So let's choose the smallest representation.  */
16076           const tree tree_addend = TREE_OPERAND (loc, 1);
16077           offset_int wi_addend;
16078           HOST_WIDE_INT shwi_addend;
16079           dw_loc_descr_ref loc_naddend;
16080
16081           list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16082           if (list_ret == 0)
16083             return 0;
16084
16085           /* Try to get the literal to push.  It is the opposite of the addend,
16086              so as we rely on wrapping during DWARF evaluation, first decode
16087              the literal as a "DWARF-sized" signed number.  */
16088           wi_addend = wi::to_offset (tree_addend);
16089           wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
16090           shwi_addend = wi_addend.to_shwi ();
16091           loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
16092                         ? int_loc_descriptor (-shwi_addend)
16093                         : NULL;
16094
16095           if (loc_naddend != NULL
16096               && ((unsigned) size_of_uleb128 (shwi_addend)
16097                   > size_of_loc_descr (loc_naddend)))
16098             {
16099               add_loc_descr_to_each (list_ret, loc_naddend);
16100               add_loc_descr_to_each (list_ret,
16101                                      new_loc_descr (DW_OP_minus, 0, 0));
16102             }
16103           else
16104             {
16105               for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
16106                 {
16107                   loc_naddend = loc_cur;
16108                   loc_cur = loc_cur->dw_loc_next;
16109                   ggc_free (loc_naddend);
16110                 }
16111               loc_list_plus_const (list_ret, wi_addend.to_shwi ());
16112             }
16113           break;
16114         }
16115
16116       op = DW_OP_plus;
16117       goto do_binop;
16118
16119     case LE_EXPR:
16120       op = DW_OP_le;
16121       goto do_comp_binop;
16122
16123     case GE_EXPR:
16124       op = DW_OP_ge;
16125       goto do_comp_binop;
16126
16127     case LT_EXPR:
16128       op = DW_OP_lt;
16129       goto do_comp_binop;
16130
16131     case GT_EXPR:
16132       op = DW_OP_gt;
16133       goto do_comp_binop;
16134
16135     do_comp_binop:
16136       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
16137         {
16138           list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
16139           list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
16140           list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
16141                                                     TREE_CODE (loc));
16142           break;
16143         }
16144       else
16145         goto do_binop;
16146
16147     case EQ_EXPR:
16148       op = DW_OP_eq;
16149       goto do_binop;
16150
16151     case NE_EXPR:
16152       op = DW_OP_ne;
16153       goto do_binop;
16154
16155     do_binop:
16156       list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16157       list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
16158       if (list_ret == 0 || list_ret1 == 0)
16159         return 0;
16160
16161       add_loc_list (&list_ret, list_ret1);
16162       if (list_ret == 0)
16163         return 0;
16164       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
16165       break;
16166
16167     case TRUTH_NOT_EXPR:
16168     case BIT_NOT_EXPR:
16169       op = DW_OP_not;
16170       goto do_unop;
16171
16172     case ABS_EXPR:
16173       op = DW_OP_abs;
16174       goto do_unop;
16175
16176     case NEGATE_EXPR:
16177       op = DW_OP_neg;
16178       goto do_unop;
16179
16180     do_unop:
16181       list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16182       if (list_ret == 0)
16183         return 0;
16184
16185       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
16186       break;
16187
16188     case MIN_EXPR:
16189     case MAX_EXPR:
16190       {
16191         const enum tree_code code =
16192           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
16193
16194         loc = build3 (COND_EXPR, TREE_TYPE (loc),
16195                       build2 (code, integer_type_node,
16196                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
16197                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
16198       }
16199
16200       /* ... fall through ...  */
16201
16202     case COND_EXPR:
16203       {
16204         dw_loc_descr_ref lhs
16205           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
16206         dw_loc_list_ref rhs
16207           = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
16208         dw_loc_descr_ref bra_node, jump_node, tmp;
16209
16210         list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16211         if (list_ret == 0 || lhs == 0 || rhs == 0)
16212           return 0;
16213
16214         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
16215         add_loc_descr_to_each (list_ret, bra_node);
16216
16217         add_loc_list (&list_ret, rhs);
16218         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
16219         add_loc_descr_to_each (list_ret, jump_node);
16220
16221         add_loc_descr_to_each (list_ret, lhs);
16222         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
16223         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
16224
16225         /* ??? Need a node to point the skip at.  Use a nop.  */
16226         tmp = new_loc_descr (DW_OP_nop, 0, 0);
16227         add_loc_descr_to_each (list_ret, tmp);
16228         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
16229         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
16230       }
16231       break;
16232
16233     case FIX_TRUNC_EXPR:
16234       return 0;
16235
16236     default:
16237       /* Leave front-end specific codes as simply unknown.  This comes
16238          up, for instance, with the C STMT_EXPR.  */
16239       if ((unsigned int) TREE_CODE (loc)
16240           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
16241         {
16242           expansion_failed (loc, NULL_RTX,
16243                             "language specific tree node");
16244           return 0;
16245         }
16246
16247       /* Otherwise this is a generic code; we should just lists all of
16248          these explicitly.  We forgot one.  */
16249       if (flag_checking)
16250         gcc_unreachable ();
16251
16252       /* In a release build, we want to degrade gracefully: better to
16253          generate incomplete debugging information than to crash.  */
16254       return NULL;
16255     }
16256
16257   if (!ret && !list_ret)
16258     return 0;
16259
16260   if (want_address == 2 && !have_address
16261       && (dwarf_version >= 4 || !dwarf_strict))
16262     {
16263       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
16264         {
16265           expansion_failed (loc, NULL_RTX,
16266                             "DWARF address size mismatch");
16267           return 0;
16268         }
16269       if (ret)
16270         add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
16271       else
16272         add_loc_descr_to_each (list_ret,
16273                                new_loc_descr (DW_OP_stack_value, 0, 0));
16274       have_address = 1;
16275     }
16276   /* Show if we can't fill the request for an address.  */
16277   if (want_address && !have_address)
16278     {
16279       expansion_failed (loc, NULL_RTX,
16280                         "Want address and only have value");
16281       return 0;
16282     }
16283
16284   gcc_assert (!ret || !list_ret);
16285
16286   /* If we've got an address and don't want one, dereference.  */
16287   if (!want_address && have_address)
16288     {
16289       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
16290
16291       if (size > DWARF2_ADDR_SIZE || size == -1)
16292         {
16293           expansion_failed (loc, NULL_RTX,
16294                             "DWARF address size mismatch");
16295           return 0;
16296         }
16297       else if (size == DWARF2_ADDR_SIZE)
16298         op = DW_OP_deref;
16299       else
16300         op = DW_OP_deref_size;
16301
16302       if (ret)
16303         add_loc_descr (&ret, new_loc_descr (op, size, 0));
16304       else
16305         add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
16306     }
16307   if (ret)
16308     list_ret = new_loc_list (ret, NULL, NULL, NULL);
16309
16310   return list_ret;
16311 }
16312
16313 /* Likewise, but strip useless DW_OP_nop operations in the resulting
16314    expressions.  */
16315
16316 static dw_loc_list_ref
16317 loc_list_from_tree (tree loc, int want_address,
16318                     const struct loc_descr_context *context)
16319 {
16320   dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
16321
16322   for (dw_loc_list_ref loc_cur = result;
16323        loc_cur != NULL; loc_cur =
16324        loc_cur->dw_loc_next)
16325     loc_descr_without_nops (loc_cur->expr);
16326   return result;
16327 }
16328
16329 /* Same as above but return only single location expression.  */
16330 static dw_loc_descr_ref
16331 loc_descriptor_from_tree (tree loc, int want_address,
16332                           const struct loc_descr_context *context)
16333 {
16334   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
16335   if (!ret)
16336     return NULL;
16337   if (ret->dw_loc_next)
16338     {
16339       expansion_failed (loc, NULL_RTX,
16340                         "Location list where only loc descriptor needed");
16341       return NULL;
16342     }
16343   return ret->expr;
16344 }
16345
16346 /* Given a value, round it up to the lowest multiple of `boundary'
16347    which is not less than the value itself.  */
16348
16349 static inline HOST_WIDE_INT
16350 ceiling (HOST_WIDE_INT value, unsigned int boundary)
16351 {
16352   return (((value + boundary - 1) / boundary) * boundary);
16353 }
16354
16355 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
16356    pointer to the declared type for the relevant field variable, or return
16357    `integer_type_node' if the given node turns out to be an
16358    ERROR_MARK node.  */
16359
16360 static inline tree
16361 field_type (const_tree decl)
16362 {
16363   tree type;
16364
16365   if (TREE_CODE (decl) == ERROR_MARK)
16366     return integer_type_node;
16367
16368   type = DECL_BIT_FIELD_TYPE (decl);
16369   if (type == NULL_TREE)
16370     type = TREE_TYPE (decl);
16371
16372   return type;
16373 }
16374
16375 /* Given a pointer to a tree node, return the alignment in bits for
16376    it, or else return BITS_PER_WORD if the node actually turns out to
16377    be an ERROR_MARK node.  */
16378
16379 static inline unsigned
16380 simple_type_align_in_bits (const_tree type)
16381 {
16382   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
16383 }
16384
16385 static inline unsigned
16386 simple_decl_align_in_bits (const_tree decl)
16387 {
16388   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
16389 }
16390
16391 /* Return the result of rounding T up to ALIGN.  */
16392
16393 static inline offset_int
16394 round_up_to_align (const offset_int &t, unsigned int align)
16395 {
16396   return wi::udiv_trunc (t + align - 1, align) * align;
16397 }
16398
16399 /* Compute the size of TYPE in bytes.  If possible, return NULL and store the
16400    size as an integer constant in CST_SIZE.  Otherwise, if possible, return a
16401    DWARF expression that computes the size.  Return NULL and set CST_SIZE to -1
16402    if we fail to return the size in one of these two forms.  */
16403
16404 static dw_loc_descr_ref
16405 type_byte_size (const_tree type, HOST_WIDE_INT *cst_size)
16406 {
16407   tree tree_size;
16408   struct loc_descr_context ctx;
16409
16410   /* Return a constant integer in priority, if possible.  */
16411   *cst_size = int_size_in_bytes (type);
16412   if (*cst_size != -1)
16413     return NULL;
16414
16415   ctx.context_type = const_cast<tree> (type);
16416   ctx.base_decl = NULL_TREE;
16417   ctx.dpi = NULL;
16418
16419   type = TYPE_MAIN_VARIANT (type);
16420   tree_size = TYPE_SIZE_UNIT (type);
16421   return ((tree_size != NULL_TREE)
16422           ? loc_descriptor_from_tree (tree_size, 0, &ctx)
16423           : NULL);
16424 }
16425
16426 /* Helper structure for RECORD_TYPE processing.  */
16427 struct vlr_context
16428 {
16429   /* Root RECORD_TYPE.  It is needed to generate data member location
16430      descriptions in variable-length records (VLR), but also to cope with
16431      variants, which are composed of nested structures multiplexed with
16432      QUAL_UNION_TYPE nodes.  Each time such a structure is passed to a
16433      function processing a FIELD_DECL, it is required to be non null.  */
16434   tree struct_type;
16435   /* When generating a variant part in a RECORD_TYPE (i.e. a nested
16436      QUAL_UNION_TYPE), this holds an expression that computes the offset for
16437      this variant part as part of the root record (in storage units).  For
16438      regular records, it must be NULL_TREE.  */
16439   tree variant_part_offset;
16440 };
16441
16442 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
16443    addressed byte of the "containing object" for the given FIELD_DECL. If
16444    possible, return a native constant through CST_OFFSET (in which case NULL is
16445    returned); otherwise return a DWARF expression that computes the offset.
16446
16447    Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
16448    that offset is, either because the argument turns out to be a pointer to an
16449    ERROR_MARK node, or because the offset expression is too complex for us.
16450
16451    CTX is required: see the comment for VLR_CONTEXT.  */
16452
16453 static dw_loc_descr_ref
16454 field_byte_offset (const_tree decl, struct vlr_context *ctx,
16455                    HOST_WIDE_INT *cst_offset)
16456 {
16457   offset_int object_offset_in_bits;
16458   offset_int object_offset_in_bytes;
16459   offset_int bitpos_int;
16460   bool is_byte_offset_cst, is_bit_offset_cst;
16461   tree tree_result;
16462   dw_loc_list_ref loc_result;
16463
16464   *cst_offset = 0;
16465
16466   if (TREE_CODE (decl) == ERROR_MARK)
16467     return NULL;
16468   else
16469     gcc_assert (TREE_CODE (decl) == FIELD_DECL);
16470
16471   is_bit_offset_cst = TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST;
16472   is_byte_offset_cst = TREE_CODE (DECL_FIELD_OFFSET (decl)) != INTEGER_CST;
16473
16474   /* We cannot handle variable bit offsets at the moment, so abort if it's the
16475      case.  */
16476   if (is_bit_offset_cst)
16477     return NULL;
16478
16479 #ifdef PCC_BITFIELD_TYPE_MATTERS
16480   /* We used to handle only constant offsets in all cases.  Now, we handle
16481      properly dynamic byte offsets only when PCC bitfield type doesn't
16482      matter.  */
16483   if (PCC_BITFIELD_TYPE_MATTERS && is_byte_offset_cst && is_bit_offset_cst)
16484     {
16485       tree type;
16486       tree field_size_tree;
16487       offset_int deepest_bitpos;
16488       offset_int field_size_in_bits;
16489       unsigned int type_align_in_bits;
16490       unsigned int decl_align_in_bits;
16491       offset_int type_size_in_bits;
16492
16493       bitpos_int = wi::to_offset (bit_position (decl));
16494       type = field_type (decl);
16495       type_size_in_bits = offset_int_type_size_in_bits (type);
16496       type_align_in_bits = simple_type_align_in_bits (type);
16497
16498       field_size_tree = DECL_SIZE (decl);
16499
16500       /* The size could be unspecified if there was an error, or for
16501          a flexible array member.  */
16502       if (!field_size_tree)
16503         field_size_tree = bitsize_zero_node;
16504
16505       /* If the size of the field is not constant, use the type size.  */
16506       if (TREE_CODE (field_size_tree) == INTEGER_CST)
16507         field_size_in_bits = wi::to_offset (field_size_tree);
16508       else
16509         field_size_in_bits = type_size_in_bits;
16510
16511       decl_align_in_bits = simple_decl_align_in_bits (decl);
16512
16513       /* The GCC front-end doesn't make any attempt to keep track of the
16514          starting bit offset (relative to the start of the containing
16515          structure type) of the hypothetical "containing object" for a
16516          bit-field.  Thus, when computing the byte offset value for the
16517          start of the "containing object" of a bit-field, we must deduce
16518          this information on our own. This can be rather tricky to do in
16519          some cases.  For example, handling the following structure type
16520          definition when compiling for an i386/i486 target (which only
16521          aligns long long's to 32-bit boundaries) can be very tricky:
16522
16523          struct S { int field1; long long field2:31; };
16524
16525          Fortunately, there is a simple rule-of-thumb which can be used
16526          in such cases.  When compiling for an i386/i486, GCC will
16527          allocate 8 bytes for the structure shown above.  It decides to
16528          do this based upon one simple rule for bit-field allocation.
16529          GCC allocates each "containing object" for each bit-field at
16530          the first (i.e. lowest addressed) legitimate alignment boundary
16531          (based upon the required minimum alignment for the declared
16532          type of the field) which it can possibly use, subject to the
16533          condition that there is still enough available space remaining
16534          in the containing object (when allocated at the selected point)
16535          to fully accommodate all of the bits of the bit-field itself.
16536
16537          This simple rule makes it obvious why GCC allocates 8 bytes for
16538          each object of the structure type shown above.  When looking
16539          for a place to allocate the "containing object" for `field2',
16540          the compiler simply tries to allocate a 64-bit "containing
16541          object" at each successive 32-bit boundary (starting at zero)
16542          until it finds a place to allocate that 64- bit field such that
16543          at least 31 contiguous (and previously unallocated) bits remain
16544          within that selected 64 bit field.  (As it turns out, for the
16545          example above, the compiler finds it is OK to allocate the
16546          "containing object" 64-bit field at bit-offset zero within the
16547          structure type.)
16548
16549          Here we attempt to work backwards from the limited set of facts
16550          we're given, and we try to deduce from those facts, where GCC
16551          must have believed that the containing object started (within
16552          the structure type). The value we deduce is then used (by the
16553          callers of this routine) to generate DW_AT_location and
16554          DW_AT_bit_offset attributes for fields (both bit-fields and, in
16555          the case of DW_AT_location, regular fields as well).  */
16556
16557       /* Figure out the bit-distance from the start of the structure to
16558          the "deepest" bit of the bit-field.  */
16559       deepest_bitpos = bitpos_int + field_size_in_bits;
16560
16561       /* This is the tricky part.  Use some fancy footwork to deduce
16562          where the lowest addressed bit of the containing object must
16563          be.  */
16564       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
16565
16566       /* Round up to type_align by default.  This works best for
16567          bitfields.  */
16568       object_offset_in_bits
16569         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
16570
16571       if (wi::gtu_p (object_offset_in_bits, bitpos_int))
16572         {
16573           object_offset_in_bits = deepest_bitpos - type_size_in_bits;
16574
16575           /* Round up to decl_align instead.  */
16576           object_offset_in_bits
16577             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
16578         }
16579     }
16580 #endif /* PCC_BITFIELD_TYPE_MATTERS */
16581
16582   tree_result = byte_position (decl);
16583   if (ctx->variant_part_offset != NULL_TREE)
16584     tree_result = fold (build2 (PLUS_EXPR, TREE_TYPE (tree_result),
16585                                 ctx->variant_part_offset, tree_result));
16586
16587   /* If the byte offset is a constant, it's simplier to handle a native
16588      constant rather than a DWARF expression.  */
16589   if (TREE_CODE (tree_result) == INTEGER_CST)
16590     {
16591       *cst_offset = wi::to_offset (tree_result).to_shwi ();
16592       return NULL;
16593     }
16594   struct loc_descr_context loc_ctx = {
16595     ctx->struct_type, /* context_type */
16596     NULL_TREE,        /* base_decl */
16597     NULL              /* dpi */
16598   };
16599   loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
16600
16601   /* We want a DWARF expression: abort if we only have a location list with
16602      multiple elements.  */
16603   if (!loc_result || !single_element_loc_list_p (loc_result))
16604     return NULL;
16605   else
16606     return loc_result->expr;
16607 }
16608 \f
16609 /* The following routines define various Dwarf attributes and any data
16610    associated with them.  */
16611
16612 /* Add a location description attribute value to a DIE.
16613
16614    This emits location attributes suitable for whole variables and
16615    whole parameters.  Note that the location attributes for struct fields are
16616    generated by the routine `data_member_location_attribute' below.  */
16617
16618 static inline void
16619 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
16620                              dw_loc_list_ref descr)
16621 {
16622   if (descr == 0)
16623     return;
16624   if (single_element_loc_list_p (descr))
16625     add_AT_loc (die, attr_kind, descr->expr);
16626   else
16627     add_AT_loc_list (die, attr_kind, descr);
16628 }
16629
16630 /* Add DW_AT_accessibility attribute to DIE if needed.  */
16631
16632 static void
16633 add_accessibility_attribute (dw_die_ref die, tree decl)
16634 {
16635   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
16636      children, otherwise the default is DW_ACCESS_public.  In DWARF2
16637      the default has always been DW_ACCESS_public.  */
16638   if (TREE_PROTECTED (decl))
16639     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
16640   else if (TREE_PRIVATE (decl))
16641     {
16642       if (dwarf_version == 2
16643           || die->die_parent == NULL
16644           || die->die_parent->die_tag != DW_TAG_class_type)
16645         add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
16646     }
16647   else if (dwarf_version > 2
16648            && die->die_parent
16649            && die->die_parent->die_tag == DW_TAG_class_type)
16650     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
16651 }
16652
16653 /* Attach the specialized form of location attribute used for data members of
16654    struct and union types.  In the special case of a FIELD_DECL node which
16655    represents a bit-field, the "offset" part of this special location
16656    descriptor must indicate the distance in bytes from the lowest-addressed
16657    byte of the containing struct or union type to the lowest-addressed byte of
16658    the "containing object" for the bit-field.  (See the `field_byte_offset'
16659    function above).
16660
16661    For any given bit-field, the "containing object" is a hypothetical object
16662    (of some integral or enum type) within which the given bit-field lives.  The
16663    type of this hypothetical "containing object" is always the same as the
16664    declared type of the individual bit-field itself (for GCC anyway... the
16665    DWARF spec doesn't actually mandate this).  Note that it is the size (in
16666    bytes) of the hypothetical "containing object" which will be given in the
16667    DW_AT_byte_size attribute for this bit-field.  (See the
16668    `byte_size_attribute' function below.)  It is also used when calculating the
16669    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
16670    function below.)
16671
16672    CTX is required: see the comment for VLR_CONTEXT.  */
16673
16674 static void
16675 add_data_member_location_attribute (dw_die_ref die,
16676                                     tree decl,
16677                                     struct vlr_context *ctx)
16678 {
16679   HOST_WIDE_INT offset;
16680   dw_loc_descr_ref loc_descr = 0;
16681
16682   if (TREE_CODE (decl) == TREE_BINFO)
16683     {
16684       /* We're working on the TAG_inheritance for a base class.  */
16685       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
16686         {
16687           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
16688              aren't at a fixed offset from all (sub)objects of the same
16689              type.  We need to extract the appropriate offset from our
16690              vtable.  The following dwarf expression means
16691
16692                BaseAddr = ObAddr + *((*ObAddr) - Offset)
16693
16694              This is specific to the V3 ABI, of course.  */
16695
16696           dw_loc_descr_ref tmp;
16697
16698           /* Make a copy of the object address.  */
16699           tmp = new_loc_descr (DW_OP_dup, 0, 0);
16700           add_loc_descr (&loc_descr, tmp);
16701
16702           /* Extract the vtable address.  */
16703           tmp = new_loc_descr (DW_OP_deref, 0, 0);
16704           add_loc_descr (&loc_descr, tmp);
16705
16706           /* Calculate the address of the offset.  */
16707           offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
16708           gcc_assert (offset < 0);
16709
16710           tmp = int_loc_descriptor (-offset);
16711           add_loc_descr (&loc_descr, tmp);
16712           tmp = new_loc_descr (DW_OP_minus, 0, 0);
16713           add_loc_descr (&loc_descr, tmp);
16714
16715           /* Extract the offset.  */
16716           tmp = new_loc_descr (DW_OP_deref, 0, 0);
16717           add_loc_descr (&loc_descr, tmp);
16718
16719           /* Add it to the object address.  */
16720           tmp = new_loc_descr (DW_OP_plus, 0, 0);
16721           add_loc_descr (&loc_descr, tmp);
16722         }
16723       else
16724         offset = tree_to_shwi (BINFO_OFFSET (decl));
16725     }
16726   else
16727     {
16728       loc_descr = field_byte_offset (decl, ctx, &offset);
16729
16730       /* Data member location evalutation start with the base address on the
16731          stack.  Compute the field offset and add it to this base address.  */
16732       if (loc_descr != NULL)
16733         add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
16734     }
16735
16736   /* If loc_descr is available then we know the field offset is dynamic.
16737      However, GDB does not handle dynamic field offsets very well at the
16738      moment.  */
16739   if (loc_descr != NULL && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
16740     {
16741       loc_descr = NULL;
16742       offset = 0;
16743     }
16744
16745   if (! loc_descr)
16746     {
16747       if (dwarf_version > 2)
16748         {
16749           /* Don't need to output a location expression, just the constant. */
16750           if (offset < 0)
16751             add_AT_int (die, DW_AT_data_member_location, offset);
16752           else
16753             add_AT_unsigned (die, DW_AT_data_member_location, offset);
16754           return;
16755         }
16756       else
16757         {
16758           enum dwarf_location_atom op;
16759
16760           /* The DWARF2 standard says that we should assume that the structure
16761              address is already on the stack, so we can specify a structure
16762              field address by using DW_OP_plus_uconst.  */
16763           op = DW_OP_plus_uconst;
16764           loc_descr = new_loc_descr (op, offset, 0);
16765         }
16766     }
16767
16768   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
16769 }
16770
16771 /* Writes integer values to dw_vec_const array.  */
16772
16773 static void
16774 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
16775 {
16776   while (size != 0)
16777     {
16778       *dest++ = val & 0xff;
16779       val >>= 8;
16780       --size;
16781     }
16782 }
16783
16784 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
16785
16786 static HOST_WIDE_INT
16787 extract_int (const unsigned char *src, unsigned int size)
16788 {
16789   HOST_WIDE_INT val = 0;
16790
16791   src += size;
16792   while (size != 0)
16793     {
16794       val <<= 8;
16795       val |= *--src & 0xff;
16796       --size;
16797     }
16798   return val;
16799 }
16800
16801 /* Writes wide_int values to dw_vec_const array.  */
16802
16803 static void
16804 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
16805 {
16806   int i;
16807
16808   if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
16809     {
16810       insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
16811       return;
16812     }
16813
16814   /* We'd have to extend this code to support odd sizes.  */
16815   gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
16816
16817   int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
16818
16819   if (WORDS_BIG_ENDIAN)
16820     for (i = n - 1; i >= 0; i--)
16821       {
16822         insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
16823         dest += sizeof (HOST_WIDE_INT);
16824       }
16825   else
16826     for (i = 0; i < n; i++)
16827       {
16828         insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
16829         dest += sizeof (HOST_WIDE_INT);
16830       }
16831 }
16832
16833 /* Writes floating point values to dw_vec_const array.  */
16834
16835 static void
16836 insert_float (const_rtx rtl, unsigned char *array)
16837 {
16838   long val[4];
16839   int i;
16840
16841   real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), GET_MODE (rtl));
16842
16843   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
16844   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
16845     {
16846       insert_int (val[i], 4, array);
16847       array += 4;
16848     }
16849 }
16850
16851 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
16852    does not have a "location" either in memory or in a register.  These
16853    things can arise in GNU C when a constant is passed as an actual parameter
16854    to an inlined function.  They can also arise in C++ where declared
16855    constants do not necessarily get memory "homes".  */
16856
16857 static bool
16858 add_const_value_attribute (dw_die_ref die, rtx rtl)
16859 {
16860   switch (GET_CODE (rtl))
16861     {
16862     case CONST_INT:
16863       {
16864         HOST_WIDE_INT val = INTVAL (rtl);
16865
16866         if (val < 0)
16867           add_AT_int (die, DW_AT_const_value, val);
16868         else
16869           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
16870       }
16871       return true;
16872
16873     case CONST_WIDE_INT:
16874       {
16875         wide_int w1 = std::make_pair (rtl, MAX_MODE_INT);
16876         unsigned int prec = MIN (wi::min_precision (w1, UNSIGNED),
16877                                  (unsigned int)CONST_WIDE_INT_NUNITS (rtl) * HOST_BITS_PER_WIDE_INT);
16878         wide_int w = wi::zext (w1, prec);
16879         add_AT_wide (die, DW_AT_const_value, w);
16880       }
16881       return true;
16882
16883     case CONST_DOUBLE:
16884       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
16885          floating-point constant.  A CONST_DOUBLE is used whenever the
16886          constant requires more than one word in order to be adequately
16887          represented.  */
16888       {
16889         machine_mode mode = GET_MODE (rtl);
16890
16891         if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
16892           add_AT_double (die, DW_AT_const_value,
16893                          CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
16894         else
16895           {
16896             unsigned int length = GET_MODE_SIZE (mode);
16897             unsigned char *array = ggc_vec_alloc<unsigned char> (length);
16898
16899             insert_float (rtl, array);
16900             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
16901           }
16902       }
16903       return true;
16904
16905     case CONST_VECTOR:
16906       {
16907         machine_mode mode = GET_MODE (rtl);
16908         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
16909         unsigned int length = CONST_VECTOR_NUNITS (rtl);
16910         unsigned char *array
16911           = ggc_vec_alloc<unsigned char> (length * elt_size);
16912         unsigned int i;
16913         unsigned char *p;
16914         machine_mode imode = GET_MODE_INNER (mode);
16915
16916         switch (GET_MODE_CLASS (mode))
16917           {
16918           case MODE_VECTOR_INT:
16919             for (i = 0, p = array; i < length; i++, p += elt_size)
16920               {
16921                 rtx elt = CONST_VECTOR_ELT (rtl, i);
16922                 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
16923               }
16924             break;
16925
16926           case MODE_VECTOR_FLOAT:
16927             for (i = 0, p = array; i < length; i++, p += elt_size)
16928               {
16929                 rtx elt = CONST_VECTOR_ELT (rtl, i);
16930                 insert_float (elt, p);
16931               }
16932             break;
16933
16934           default:
16935             gcc_unreachable ();
16936           }
16937
16938         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
16939       }
16940       return true;
16941
16942     case CONST_STRING:
16943       if (dwarf_version >= 4 || !dwarf_strict)
16944         {
16945           dw_loc_descr_ref loc_result;
16946           resolve_one_addr (&rtl);
16947         rtl_addr:
16948           loc_result = new_addr_loc_descr (rtl, dtprel_false);
16949           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
16950           add_AT_loc (die, DW_AT_location, loc_result);
16951           vec_safe_push (used_rtx_array, rtl);
16952           return true;
16953         }
16954       return false;
16955
16956     case CONST:
16957       if (CONSTANT_P (XEXP (rtl, 0)))
16958         return add_const_value_attribute (die, XEXP (rtl, 0));
16959       /* FALLTHROUGH */
16960     case SYMBOL_REF:
16961       if (!const_ok_for_output (rtl))
16962         return false;
16963     case LABEL_REF:
16964       if (dwarf_version >= 4 || !dwarf_strict)
16965         goto rtl_addr;
16966       return false;
16967
16968     case PLUS:
16969       /* In cases where an inlined instance of an inline function is passed
16970          the address of an `auto' variable (which is local to the caller) we
16971          can get a situation where the DECL_RTL of the artificial local
16972          variable (for the inlining) which acts as a stand-in for the
16973          corresponding formal parameter (of the inline function) will look
16974          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
16975          exactly a compile-time constant expression, but it isn't the address
16976          of the (artificial) local variable either.  Rather, it represents the
16977          *value* which the artificial local variable always has during its
16978          lifetime.  We currently have no way to represent such quasi-constant
16979          values in Dwarf, so for now we just punt and generate nothing.  */
16980       return false;
16981
16982     case HIGH:
16983     case CONST_FIXED:
16984       return false;
16985
16986     case MEM:
16987       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
16988           && MEM_READONLY_P (rtl)
16989           && GET_MODE (rtl) == BLKmode)
16990         {
16991           add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
16992           return true;
16993         }
16994       return false;
16995
16996     default:
16997       /* No other kinds of rtx should be possible here.  */
16998       gcc_unreachable ();
16999     }
17000   return false;
17001 }
17002
17003 /* Determine whether the evaluation of EXPR references any variables
17004    or functions which aren't otherwise used (and therefore may not be
17005    output).  */
17006 static tree
17007 reference_to_unused (tree * tp, int * walk_subtrees,
17008                      void * data ATTRIBUTE_UNUSED)
17009 {
17010   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
17011     *walk_subtrees = 0;
17012
17013   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
17014       && ! TREE_ASM_WRITTEN (*tp))
17015     return *tp;
17016   /* ???  The C++ FE emits debug information for using decls, so
17017      putting gcc_unreachable here falls over.  See PR31899.  For now
17018      be conservative.  */
17019   else if (!symtab->global_info_ready
17020            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
17021     return *tp;
17022   else if (TREE_CODE (*tp) == VAR_DECL)
17023     {
17024       varpool_node *node = varpool_node::get (*tp);
17025       if (!node || !node->definition)
17026         return *tp;
17027     }
17028   else if (TREE_CODE (*tp) == FUNCTION_DECL
17029            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
17030     {
17031       /* The call graph machinery must have finished analyzing,
17032          optimizing and gimplifying the CU by now.
17033          So if *TP has no call graph node associated
17034          to it, it means *TP will not be emitted.  */
17035       if (!cgraph_node::get (*tp))
17036         return *tp;
17037     }
17038   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
17039     return *tp;
17040
17041   return NULL_TREE;
17042 }
17043
17044 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
17045    for use in a later add_const_value_attribute call.  */
17046
17047 static rtx
17048 rtl_for_decl_init (tree init, tree type)
17049 {
17050   rtx rtl = NULL_RTX;
17051
17052   STRIP_NOPS (init);
17053
17054   /* If a variable is initialized with a string constant without embedded
17055      zeros, build CONST_STRING.  */
17056   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
17057     {
17058       tree enttype = TREE_TYPE (type);
17059       tree domain = TYPE_DOMAIN (type);
17060       machine_mode mode = TYPE_MODE (enttype);
17061
17062       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
17063           && domain
17064           && integer_zerop (TYPE_MIN_VALUE (domain))
17065           && compare_tree_int (TYPE_MAX_VALUE (domain),
17066                                TREE_STRING_LENGTH (init) - 1) == 0
17067           && ((size_t) TREE_STRING_LENGTH (init)
17068               == strlen (TREE_STRING_POINTER (init)) + 1))
17069         {
17070           rtl = gen_rtx_CONST_STRING (VOIDmode,
17071                                       ggc_strdup (TREE_STRING_POINTER (init)));
17072           rtl = gen_rtx_MEM (BLKmode, rtl);
17073           MEM_READONLY_P (rtl) = 1;
17074         }
17075     }
17076   /* Other aggregates, and complex values, could be represented using
17077      CONCAT: FIXME!  */
17078   else if (AGGREGATE_TYPE_P (type)
17079            || (TREE_CODE (init) == VIEW_CONVERT_EXPR
17080                && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
17081            || TREE_CODE (type) == COMPLEX_TYPE)
17082     ;
17083   /* Vectors only work if their mode is supported by the target.
17084      FIXME: generic vectors ought to work too.  */
17085   else if (TREE_CODE (type) == VECTOR_TYPE
17086            && !VECTOR_MODE_P (TYPE_MODE (type)))
17087     ;
17088   /* If the initializer is something that we know will expand into an
17089      immediate RTL constant, expand it now.  We must be careful not to
17090      reference variables which won't be output.  */
17091   else if (initializer_constant_valid_p (init, type)
17092            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
17093     {
17094       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
17095          possible.  */
17096       if (TREE_CODE (type) == VECTOR_TYPE)
17097         switch (TREE_CODE (init))
17098           {
17099           case VECTOR_CST:
17100             break;
17101           case CONSTRUCTOR:
17102             if (TREE_CONSTANT (init))
17103               {
17104                 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
17105                 bool constant_p = true;
17106                 tree value;
17107                 unsigned HOST_WIDE_INT ix;
17108
17109                 /* Even when ctor is constant, it might contain non-*_CST
17110                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
17111                    belong into VECTOR_CST nodes.  */
17112                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
17113                   if (!CONSTANT_CLASS_P (value))
17114                     {
17115                       constant_p = false;
17116                       break;
17117                     }
17118
17119                 if (constant_p)
17120                   {
17121                     init = build_vector_from_ctor (type, elts);
17122                     break;
17123                   }
17124               }
17125             /* FALLTHRU */
17126
17127           default:
17128             return NULL;
17129           }
17130
17131       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
17132
17133       /* If expand_expr returns a MEM, it wasn't immediate.  */
17134       gcc_assert (!rtl || !MEM_P (rtl));
17135     }
17136
17137   return rtl;
17138 }
17139
17140 /* Generate RTL for the variable DECL to represent its location.  */
17141
17142 static rtx
17143 rtl_for_decl_location (tree decl)
17144 {
17145   rtx rtl;
17146
17147   /* Here we have to decide where we are going to say the parameter "lives"
17148      (as far as the debugger is concerned).  We only have a couple of
17149      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
17150
17151      DECL_RTL normally indicates where the parameter lives during most of the
17152      activation of the function.  If optimization is enabled however, this
17153      could be either NULL or else a pseudo-reg.  Both of those cases indicate
17154      that the parameter doesn't really live anywhere (as far as the code
17155      generation parts of GCC are concerned) during most of the function's
17156      activation.  That will happen (for example) if the parameter is never
17157      referenced within the function.
17158
17159      We could just generate a location descriptor here for all non-NULL
17160      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
17161      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
17162      where DECL_RTL is NULL or is a pseudo-reg.
17163
17164      Note however that we can only get away with using DECL_INCOMING_RTL as
17165      a backup substitute for DECL_RTL in certain limited cases.  In cases
17166      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
17167      we can be sure that the parameter was passed using the same type as it is
17168      declared to have within the function, and that its DECL_INCOMING_RTL
17169      points us to a place where a value of that type is passed.
17170
17171      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
17172      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
17173      because in these cases DECL_INCOMING_RTL points us to a value of some
17174      type which is *different* from the type of the parameter itself.  Thus,
17175      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
17176      such cases, the debugger would end up (for example) trying to fetch a
17177      `float' from a place which actually contains the first part of a
17178      `double'.  That would lead to really incorrect and confusing
17179      output at debug-time.
17180
17181      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
17182      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
17183      are a couple of exceptions however.  On little-endian machines we can
17184      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
17185      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
17186      an integral type that is smaller than TREE_TYPE (decl). These cases arise
17187      when (on a little-endian machine) a non-prototyped function has a
17188      parameter declared to be of type `short' or `char'.  In such cases,
17189      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
17190      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
17191      passed `int' value.  If the debugger then uses that address to fetch
17192      a `short' or a `char' (on a little-endian machine) the result will be
17193      the correct data, so we allow for such exceptional cases below.
17194
17195      Note that our goal here is to describe the place where the given formal
17196      parameter lives during most of the function's activation (i.e. between the
17197      end of the prologue and the start of the epilogue).  We'll do that as best
17198      as we can. Note however that if the given formal parameter is modified
17199      sometime during the execution of the function, then a stack backtrace (at
17200      debug-time) will show the function as having been called with the *new*
17201      value rather than the value which was originally passed in.  This happens
17202      rarely enough that it is not a major problem, but it *is* a problem, and
17203      I'd like to fix it.
17204
17205      A future version of dwarf2out.c may generate two additional attributes for
17206      any given DW_TAG_formal_parameter DIE which will describe the "passed
17207      type" and the "passed location" for the given formal parameter in addition
17208      to the attributes we now generate to indicate the "declared type" and the
17209      "active location" for each parameter.  This additional set of attributes
17210      could be used by debuggers for stack backtraces. Separately, note that
17211      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
17212      This happens (for example) for inlined-instances of inline function formal
17213      parameters which are never referenced.  This really shouldn't be
17214      happening.  All PARM_DECL nodes should get valid non-NULL
17215      DECL_INCOMING_RTL values.  FIXME.  */
17216
17217   /* Use DECL_RTL as the "location" unless we find something better.  */
17218   rtl = DECL_RTL_IF_SET (decl);
17219
17220   /* When generating abstract instances, ignore everything except
17221      constants, symbols living in memory, and symbols living in
17222      fixed registers.  */
17223   if (! reload_completed)
17224     {
17225       if (rtl
17226           && (CONSTANT_P (rtl)
17227               || (MEM_P (rtl)
17228                   && CONSTANT_P (XEXP (rtl, 0)))
17229               || (REG_P (rtl)
17230                   && TREE_CODE (decl) == VAR_DECL
17231                   && TREE_STATIC (decl))))
17232         {
17233           rtl = targetm.delegitimize_address (rtl);
17234           return rtl;
17235         }
17236       rtl = NULL_RTX;
17237     }
17238   else if (TREE_CODE (decl) == PARM_DECL)
17239     {
17240       if (rtl == NULL_RTX
17241           || is_pseudo_reg (rtl)
17242           || (MEM_P (rtl)
17243               && is_pseudo_reg (XEXP (rtl, 0))
17244               && DECL_INCOMING_RTL (decl)
17245               && MEM_P (DECL_INCOMING_RTL (decl))
17246               && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
17247         {
17248           tree declared_type = TREE_TYPE (decl);
17249           tree passed_type = DECL_ARG_TYPE (decl);
17250           machine_mode dmode = TYPE_MODE (declared_type);
17251           machine_mode pmode = TYPE_MODE (passed_type);
17252
17253           /* This decl represents a formal parameter which was optimized out.
17254              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
17255              all cases where (rtl == NULL_RTX) just below.  */
17256           if (dmode == pmode)
17257             rtl = DECL_INCOMING_RTL (decl);
17258           else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
17259                    && SCALAR_INT_MODE_P (dmode)
17260                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
17261                    && DECL_INCOMING_RTL (decl))
17262             {
17263               rtx inc = DECL_INCOMING_RTL (decl);
17264               if (REG_P (inc))
17265                 rtl = inc;
17266               else if (MEM_P (inc))
17267                 {
17268                   if (BYTES_BIG_ENDIAN)
17269                     rtl = adjust_address_nv (inc, dmode,
17270                                              GET_MODE_SIZE (pmode)
17271                                              - GET_MODE_SIZE (dmode));
17272                   else
17273                     rtl = inc;
17274                 }
17275             }
17276         }
17277
17278       /* If the parm was passed in registers, but lives on the stack, then
17279          make a big endian correction if the mode of the type of the
17280          parameter is not the same as the mode of the rtl.  */
17281       /* ??? This is the same series of checks that are made in dbxout.c before
17282          we reach the big endian correction code there.  It isn't clear if all
17283          of these checks are necessary here, but keeping them all is the safe
17284          thing to do.  */
17285       else if (MEM_P (rtl)
17286                && XEXP (rtl, 0) != const0_rtx
17287                && ! CONSTANT_P (XEXP (rtl, 0))
17288                /* Not passed in memory.  */
17289                && !MEM_P (DECL_INCOMING_RTL (decl))
17290                /* Not passed by invisible reference.  */
17291                && (!REG_P (XEXP (rtl, 0))
17292                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
17293                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
17294 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
17295                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
17296 #endif
17297                      )
17298                /* Big endian correction check.  */
17299                && BYTES_BIG_ENDIAN
17300                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
17301                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
17302                    < UNITS_PER_WORD))
17303         {
17304           machine_mode addr_mode = get_address_mode (rtl);
17305           int offset = (UNITS_PER_WORD
17306                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
17307
17308           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
17309                              plus_constant (addr_mode, XEXP (rtl, 0), offset));
17310         }
17311     }
17312   else if (TREE_CODE (decl) == VAR_DECL
17313            && rtl
17314            && MEM_P (rtl)
17315            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
17316            && BYTES_BIG_ENDIAN)
17317     {
17318       machine_mode addr_mode = get_address_mode (rtl);
17319       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
17320       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
17321
17322       /* If a variable is declared "register" yet is smaller than
17323          a register, then if we store the variable to memory, it
17324          looks like we're storing a register-sized value, when in
17325          fact we are not.  We need to adjust the offset of the
17326          storage location to reflect the actual value's bytes,
17327          else gdb will not be able to display it.  */
17328       if (rsize > dsize)
17329         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
17330                            plus_constant (addr_mode, XEXP (rtl, 0),
17331                                           rsize - dsize));
17332     }
17333
17334   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
17335      and will have been substituted directly into all expressions that use it.
17336      C does not have such a concept, but C++ and other languages do.  */
17337   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
17338     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
17339
17340   if (rtl)
17341     rtl = targetm.delegitimize_address (rtl);
17342
17343   /* If we don't look past the constant pool, we risk emitting a
17344      reference to a constant pool entry that isn't referenced from
17345      code, and thus is not emitted.  */
17346   if (rtl)
17347     rtl = avoid_constant_pool_reference (rtl);
17348
17349   /* Try harder to get a rtl.  If this symbol ends up not being emitted
17350      in the current CU, resolve_addr will remove the expression referencing
17351      it.  */
17352   if (rtl == NULL_RTX
17353       && TREE_CODE (decl) == VAR_DECL
17354       && !DECL_EXTERNAL (decl)
17355       && TREE_STATIC (decl)
17356       && DECL_NAME (decl)
17357       && !DECL_HARD_REGISTER (decl)
17358       && DECL_MODE (decl) != VOIDmode)
17359     {
17360       rtl = make_decl_rtl_for_debug (decl);
17361       if (!MEM_P (rtl)
17362           || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
17363           || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
17364         rtl = NULL_RTX;
17365     }
17366
17367   return rtl;
17368 }
17369
17370 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
17371    returned.  If so, the decl for the COMMON block is returned, and the
17372    value is the offset into the common block for the symbol.  */
17373
17374 static tree
17375 fortran_common (tree decl, HOST_WIDE_INT *value)
17376 {
17377   tree val_expr, cvar;
17378   machine_mode mode;
17379   HOST_WIDE_INT bitsize, bitpos;
17380   tree offset;
17381   int unsignedp, reversep, volatilep = 0;
17382
17383   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
17384      it does not have a value (the offset into the common area), or if it
17385      is thread local (as opposed to global) then it isn't common, and shouldn't
17386      be handled as such.  */
17387   if (TREE_CODE (decl) != VAR_DECL
17388       || !TREE_STATIC (decl)
17389       || !DECL_HAS_VALUE_EXPR_P (decl)
17390       || !is_fortran ())
17391     return NULL_TREE;
17392
17393   val_expr = DECL_VALUE_EXPR (decl);
17394   if (TREE_CODE (val_expr) != COMPONENT_REF)
17395     return NULL_TREE;
17396
17397   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
17398                               &unsignedp, &reversep, &volatilep, true);
17399
17400   if (cvar == NULL_TREE
17401       || TREE_CODE (cvar) != VAR_DECL
17402       || DECL_ARTIFICIAL (cvar)
17403       || !TREE_PUBLIC (cvar))
17404     return NULL_TREE;
17405
17406   *value = 0;
17407   if (offset != NULL)
17408     {
17409       if (!tree_fits_shwi_p (offset))
17410         return NULL_TREE;
17411       *value = tree_to_shwi (offset);
17412     }
17413   if (bitpos != 0)
17414     *value += bitpos / BITS_PER_UNIT;
17415
17416   return cvar;
17417 }
17418
17419 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
17420    data attribute for a variable or a parameter.  We generate the
17421    DW_AT_const_value attribute only in those cases where the given variable
17422    or parameter does not have a true "location" either in memory or in a
17423    register.  This can happen (for example) when a constant is passed as an
17424    actual argument in a call to an inline function.  (It's possible that
17425    these things can crop up in other ways also.)  Note that one type of
17426    constant value which can be passed into an inlined function is a constant
17427    pointer.  This can happen for example if an actual argument in an inlined
17428    function call evaluates to a compile-time constant address.
17429
17430    CACHE_P is true if it is worth caching the location list for DECL,
17431    so that future calls can reuse it rather than regenerate it from scratch.
17432    This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
17433    since we will need to refer to them each time the function is inlined.  */
17434
17435 static bool
17436 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
17437 {
17438   rtx rtl;
17439   dw_loc_list_ref list;
17440   var_loc_list *loc_list;
17441   cached_dw_loc_list *cache;
17442
17443   if (early_dwarf)
17444     return false;
17445
17446   if (TREE_CODE (decl) == ERROR_MARK)
17447     return false;
17448
17449   if (get_AT (die, DW_AT_location)
17450       || get_AT (die, DW_AT_const_value))
17451     return true;
17452
17453   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
17454               || TREE_CODE (decl) == RESULT_DECL);
17455
17456   /* Try to get some constant RTL for this decl, and use that as the value of
17457      the location.  */
17458
17459   rtl = rtl_for_decl_location (decl);
17460   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
17461       && add_const_value_attribute (die, rtl))
17462     return true;
17463
17464   /* See if we have single element location list that is equivalent to
17465      a constant value.  That way we are better to use add_const_value_attribute
17466      rather than expanding constant value equivalent.  */
17467   loc_list = lookup_decl_loc (decl);
17468   if (loc_list
17469       && loc_list->first
17470       && loc_list->first->next == NULL
17471       && NOTE_P (loc_list->first->loc)
17472       && NOTE_VAR_LOCATION (loc_list->first->loc)
17473       && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
17474     {
17475       struct var_loc_node *node;
17476
17477       node = loc_list->first;
17478       rtl = NOTE_VAR_LOCATION_LOC (node->loc);
17479       if (GET_CODE (rtl) == EXPR_LIST)
17480         rtl = XEXP (rtl, 0);
17481       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
17482           && add_const_value_attribute (die, rtl))
17483          return true;
17484     }
17485   /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
17486      list several times.  See if we've already cached the contents.  */
17487   list = NULL;
17488   if (loc_list == NULL || cached_dw_loc_list_table == NULL)
17489     cache_p = false;
17490   if (cache_p)
17491     {
17492       cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
17493       if (cache)
17494         list = cache->loc_list;
17495     }
17496   if (list == NULL)
17497     {
17498       list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
17499                                  NULL);
17500       /* It is usually worth caching this result if the decl is from
17501          BLOCK_NONLOCALIZED_VARS and if the list has at least two elements.  */
17502       if (cache_p && list && list->dw_loc_next)
17503         {
17504           cached_dw_loc_list **slot
17505             = cached_dw_loc_list_table->find_slot_with_hash (decl,
17506                                                              DECL_UID (decl),
17507                                                              INSERT);
17508           cache = ggc_cleared_alloc<cached_dw_loc_list> ();
17509           cache->decl_id = DECL_UID (decl);
17510           cache->loc_list = list;
17511           *slot = cache;
17512         }
17513     }
17514   if (list)
17515     {
17516       add_AT_location_description (die, DW_AT_location, list);
17517       return true;
17518     }
17519   /* None of that worked, so it must not really have a location;
17520      try adding a constant value attribute from the DECL_INITIAL.  */
17521   return tree_add_const_value_attribute_for_decl (die, decl);
17522 }
17523
17524 /* Helper function for tree_add_const_value_attribute.  Natively encode
17525    initializer INIT into an array.  Return true if successful.  */
17526
17527 static bool
17528 native_encode_initializer (tree init, unsigned char *array, int size)
17529 {
17530   tree type;
17531
17532   if (init == NULL_TREE)
17533     return false;
17534
17535   STRIP_NOPS (init);
17536   switch (TREE_CODE (init))
17537     {
17538     case STRING_CST:
17539       type = TREE_TYPE (init);
17540       if (TREE_CODE (type) == ARRAY_TYPE)
17541         {
17542           tree enttype = TREE_TYPE (type);
17543           machine_mode mode = TYPE_MODE (enttype);
17544
17545           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
17546             return false;
17547           if (int_size_in_bytes (type) != size)
17548             return false;
17549           if (size > TREE_STRING_LENGTH (init))
17550             {
17551               memcpy (array, TREE_STRING_POINTER (init),
17552                       TREE_STRING_LENGTH (init));
17553               memset (array + TREE_STRING_LENGTH (init),
17554                       '\0', size - TREE_STRING_LENGTH (init));
17555             }
17556           else
17557             memcpy (array, TREE_STRING_POINTER (init), size);
17558           return true;
17559         }
17560       return false;
17561     case CONSTRUCTOR:
17562       type = TREE_TYPE (init);
17563       if (int_size_in_bytes (type) != size)
17564         return false;
17565       if (TREE_CODE (type) == ARRAY_TYPE)
17566         {
17567           HOST_WIDE_INT min_index;
17568           unsigned HOST_WIDE_INT cnt;
17569           int curpos = 0, fieldsize;
17570           constructor_elt *ce;
17571
17572           if (TYPE_DOMAIN (type) == NULL_TREE
17573               || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
17574             return false;
17575
17576           fieldsize = int_size_in_bytes (TREE_TYPE (type));
17577           if (fieldsize <= 0)
17578             return false;
17579
17580           min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
17581           memset (array, '\0', size);
17582           FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
17583             {
17584               tree val = ce->value;
17585               tree index = ce->index;
17586               int pos = curpos;
17587               if (index && TREE_CODE (index) == RANGE_EXPR)
17588                 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
17589                       * fieldsize;
17590               else if (index)
17591                 pos = (tree_to_shwi (index) - min_index) * fieldsize;
17592
17593               if (val)
17594                 {
17595                   STRIP_NOPS (val);
17596                   if (!native_encode_initializer (val, array + pos, fieldsize))
17597                     return false;
17598                 }
17599               curpos = pos + fieldsize;
17600               if (index && TREE_CODE (index) == RANGE_EXPR)
17601                 {
17602                   int count = tree_to_shwi (TREE_OPERAND (index, 1))
17603                               - tree_to_shwi (TREE_OPERAND (index, 0));
17604                   while (count-- > 0)
17605                     {
17606                       if (val)
17607                         memcpy (array + curpos, array + pos, fieldsize);
17608                       curpos += fieldsize;
17609                     }
17610                 }
17611               gcc_assert (curpos <= size);
17612             }
17613           return true;
17614         }
17615       else if (TREE_CODE (type) == RECORD_TYPE
17616                || TREE_CODE (type) == UNION_TYPE)
17617         {
17618           tree field = NULL_TREE;
17619           unsigned HOST_WIDE_INT cnt;
17620           constructor_elt *ce;
17621
17622           if (int_size_in_bytes (type) != size)
17623             return false;
17624
17625           if (TREE_CODE (type) == RECORD_TYPE)
17626             field = TYPE_FIELDS (type);
17627
17628           FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
17629             {
17630               tree val = ce->value;
17631               int pos, fieldsize;
17632
17633               if (ce->index != 0)
17634                 field = ce->index;
17635
17636               if (val)
17637                 STRIP_NOPS (val);
17638
17639               if (field == NULL_TREE || DECL_BIT_FIELD (field))
17640                 return false;
17641
17642               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
17643                   && TYPE_DOMAIN (TREE_TYPE (field))
17644                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
17645                 return false;
17646               else if (DECL_SIZE_UNIT (field) == NULL_TREE
17647                        || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
17648                 return false;
17649               fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
17650               pos = int_byte_position (field);
17651               gcc_assert (pos + fieldsize <= size);
17652               if (val
17653                   && !native_encode_initializer (val, array + pos, fieldsize))
17654                 return false;
17655             }
17656           return true;
17657         }
17658       return false;
17659     case VIEW_CONVERT_EXPR:
17660     case NON_LVALUE_EXPR:
17661       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
17662     default:
17663       return native_encode_expr (init, array, size) == size;
17664     }
17665 }
17666
17667 /* Attach a DW_AT_const_value attribute to DIE. The value of the
17668    attribute is the const value T.  */
17669
17670 static bool
17671 tree_add_const_value_attribute (dw_die_ref die, tree t)
17672 {
17673   tree init;
17674   tree type = TREE_TYPE (t);
17675   rtx rtl;
17676
17677   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
17678     return false;
17679
17680   init = t;
17681   gcc_assert (!DECL_P (init));
17682
17683   rtl = rtl_for_decl_init (init, type);
17684   if (rtl)
17685     return add_const_value_attribute (die, rtl);
17686   /* If the host and target are sane, try harder.  */
17687   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
17688            && initializer_constant_valid_p (init, type))
17689     {
17690       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
17691       if (size > 0 && (int) size == size)
17692         {
17693           unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
17694
17695           if (native_encode_initializer (init, array, size))
17696             {
17697               add_AT_vec (die, DW_AT_const_value, size, 1, array);
17698               return true;
17699             }
17700           ggc_free (array);
17701         }
17702     }
17703   return false;
17704 }
17705
17706 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
17707    attribute is the const value of T, where T is an integral constant
17708    variable with static storage duration
17709    (so it can't be a PARM_DECL or a RESULT_DECL).  */
17710
17711 static bool
17712 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
17713 {
17714
17715   if (!decl
17716       || (TREE_CODE (decl) != VAR_DECL
17717           && TREE_CODE (decl) != CONST_DECL)
17718       || (TREE_CODE (decl) == VAR_DECL
17719           && !TREE_STATIC (decl)))
17720     return false;
17721
17722   if (TREE_READONLY (decl)
17723       && ! TREE_THIS_VOLATILE (decl)
17724       && DECL_INITIAL (decl))
17725     /* OK */;
17726   else
17727     return false;
17728
17729   /* Don't add DW_AT_const_value if abstract origin already has one.  */
17730   if (get_AT (var_die, DW_AT_const_value))
17731     return false;
17732
17733   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
17734 }
17735
17736 /* Convert the CFI instructions for the current function into a
17737    location list.  This is used for DW_AT_frame_base when we targeting
17738    a dwarf2 consumer that does not support the dwarf3
17739    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
17740    expressions.  */
17741
17742 static dw_loc_list_ref
17743 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
17744 {
17745   int ix;
17746   dw_fde_ref fde;
17747   dw_loc_list_ref list, *list_tail;
17748   dw_cfi_ref cfi;
17749   dw_cfa_location last_cfa, next_cfa;
17750   const char *start_label, *last_label, *section;
17751   dw_cfa_location remember;
17752
17753   fde = cfun->fde;
17754   gcc_assert (fde != NULL);
17755
17756   section = secname_for_decl (current_function_decl);
17757   list_tail = &list;
17758   list = NULL;
17759
17760   memset (&next_cfa, 0, sizeof (next_cfa));
17761   next_cfa.reg = INVALID_REGNUM;
17762   remember = next_cfa;
17763
17764   start_label = fde->dw_fde_begin;
17765
17766   /* ??? Bald assumption that the CIE opcode list does not contain
17767      advance opcodes.  */
17768   FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
17769     lookup_cfa_1 (cfi, &next_cfa, &remember);
17770
17771   last_cfa = next_cfa;
17772   last_label = start_label;
17773
17774   if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
17775     {
17776       /* If the first partition contained no CFI adjustments, the
17777          CIE opcodes apply to the whole first partition.  */
17778       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17779                                  fde->dw_fde_begin, fde->dw_fde_end, section);
17780       list_tail =&(*list_tail)->dw_loc_next;
17781       start_label = last_label = fde->dw_fde_second_begin;
17782     }
17783
17784   FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
17785     {
17786       switch (cfi->dw_cfi_opc)
17787         {
17788         case DW_CFA_set_loc:
17789         case DW_CFA_advance_loc1:
17790         case DW_CFA_advance_loc2:
17791         case DW_CFA_advance_loc4:
17792           if (!cfa_equal_p (&last_cfa, &next_cfa))
17793             {
17794               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17795                                          start_label, last_label, section);
17796
17797               list_tail = &(*list_tail)->dw_loc_next;
17798               last_cfa = next_cfa;
17799               start_label = last_label;
17800             }
17801           last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
17802           break;
17803
17804         case DW_CFA_advance_loc:
17805           /* The encoding is complex enough that we should never emit this.  */
17806           gcc_unreachable ();
17807
17808         default:
17809           lookup_cfa_1 (cfi, &next_cfa, &remember);
17810           break;
17811         }
17812       if (ix + 1 == fde->dw_fde_switch_cfi_index)
17813         {
17814           if (!cfa_equal_p (&last_cfa, &next_cfa))
17815             {
17816               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17817                                          start_label, last_label, section);
17818
17819               list_tail = &(*list_tail)->dw_loc_next;
17820               last_cfa = next_cfa;
17821               start_label = last_label;
17822             }
17823           *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17824                                      start_label, fde->dw_fde_end, section);
17825           list_tail = &(*list_tail)->dw_loc_next;
17826           start_label = last_label = fde->dw_fde_second_begin;
17827         }
17828     }
17829
17830   if (!cfa_equal_p (&last_cfa, &next_cfa))
17831     {
17832       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17833                                  start_label, last_label, section);
17834       list_tail = &(*list_tail)->dw_loc_next;
17835       start_label = last_label;
17836     }
17837
17838   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
17839                              start_label,
17840                              fde->dw_fde_second_begin
17841                              ? fde->dw_fde_second_end : fde->dw_fde_end,
17842                              section);
17843
17844   if (list && list->dw_loc_next)
17845     gen_llsym (list);
17846
17847   return list;
17848 }
17849
17850 /* Compute a displacement from the "steady-state frame pointer" to the
17851    frame base (often the same as the CFA), and store it in
17852    frame_pointer_fb_offset.  OFFSET is added to the displacement
17853    before the latter is negated.  */
17854
17855 static void
17856 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
17857 {
17858   rtx reg, elim;
17859
17860 #ifdef FRAME_POINTER_CFA_OFFSET
17861   reg = frame_pointer_rtx;
17862   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
17863 #else
17864   reg = arg_pointer_rtx;
17865   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
17866 #endif
17867
17868   elim = (ira_use_lra_p
17869           ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
17870           : eliminate_regs (reg, VOIDmode, NULL_RTX));
17871   if (GET_CODE (elim) == PLUS)
17872     {
17873       offset += INTVAL (XEXP (elim, 1));
17874       elim = XEXP (elim, 0);
17875     }
17876
17877   frame_pointer_fb_offset = -offset;
17878
17879   /* ??? AVR doesn't set up valid eliminations when there is no stack frame
17880      in which to eliminate.  This is because it's stack pointer isn't 
17881      directly accessible as a register within the ISA.  To work around
17882      this, assume that while we cannot provide a proper value for
17883      frame_pointer_fb_offset, we won't need one either.  */
17884   frame_pointer_fb_offset_valid
17885     = ((SUPPORTS_STACK_ALIGNMENT
17886         && (elim == hard_frame_pointer_rtx
17887             || elim == stack_pointer_rtx))
17888        || elim == (frame_pointer_needed
17889                    ? hard_frame_pointer_rtx
17890                    : stack_pointer_rtx));
17891 }
17892
17893 /* Generate a DW_AT_name attribute given some string value to be included as
17894    the value of the attribute.  */
17895
17896 static void
17897 add_name_attribute (dw_die_ref die, const char *name_string)
17898 {
17899   if (name_string != NULL && *name_string != 0)
17900     {
17901       if (demangle_name_func)
17902         name_string = (*demangle_name_func) (name_string);
17903
17904       add_AT_string (die, DW_AT_name, name_string);
17905     }
17906 }
17907
17908 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
17909    DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
17910    of TYPE accordingly.
17911
17912    ??? This is a temporary measure until after we're able to generate
17913    regular DWARF for the complex Ada type system.  */
17914
17915 static void 
17916 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
17917                                      dw_die_ref context_die)
17918 {
17919   tree dtype;
17920   dw_die_ref dtype_die;
17921
17922   if (!lang_hooks.types.descriptive_type)
17923     return;
17924
17925   dtype = lang_hooks.types.descriptive_type (type);
17926   if (!dtype)
17927     return;
17928
17929   dtype_die = lookup_type_die (dtype);
17930   if (!dtype_die)
17931     {
17932       gen_type_die (dtype, context_die);
17933       dtype_die = lookup_type_die (dtype);
17934       gcc_assert (dtype_die);
17935     }
17936
17937   add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
17938 }
17939
17940 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir.  */
17941
17942 static const char *
17943 comp_dir_string (void)
17944 {
17945   const char *wd;
17946   char *wd1;
17947   static const char *cached_wd = NULL;
17948
17949   if (cached_wd != NULL)
17950     return cached_wd;
17951
17952   wd = get_src_pwd ();
17953   if (wd == NULL)
17954     return NULL;
17955
17956   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
17957     {
17958       int wdlen;
17959
17960       wdlen = strlen (wd);
17961       wd1 = ggc_vec_alloc<char> (wdlen + 2);
17962       strcpy (wd1, wd);
17963       wd1 [wdlen] = DIR_SEPARATOR;
17964       wd1 [wdlen + 1] = 0;
17965       wd = wd1;
17966     }
17967
17968   cached_wd = remap_debug_filename (wd);
17969   return cached_wd;
17970 }
17971
17972 /* Generate a DW_AT_comp_dir attribute for DIE.  */
17973
17974 static void
17975 add_comp_dir_attribute (dw_die_ref die)
17976 {
17977   const char * wd = comp_dir_string ();
17978   if (wd != NULL)
17979     add_AT_string (die, DW_AT_comp_dir, wd);
17980 }
17981
17982 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
17983    pointer computation, ...), output a representation for that bound according
17984    to the accepted FORMS (see enum dw_scalar_form) and add it to DIE.  See
17985    loc_list_from_tree for the meaning of CONTEXT.  */
17986
17987 static void
17988 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
17989                  int forms, const struct loc_descr_context *context)
17990 {
17991   dw_die_ref ctx, decl_die;
17992   dw_loc_list_ref list;
17993
17994   bool strip_conversions = true;
17995
17996   while (strip_conversions)
17997     switch (TREE_CODE (value))
17998       {
17999       case ERROR_MARK:
18000       case SAVE_EXPR:
18001         return;
18002
18003       CASE_CONVERT:
18004       case VIEW_CONVERT_EXPR:
18005         value = TREE_OPERAND (value, 0);
18006         break;
18007
18008       default:
18009         strip_conversions = false;
18010         break;
18011       }
18012
18013   /* If possible and permitted, output the attribute as a constant.  */
18014   if ((forms & dw_scalar_form_constant) != 0
18015       && TREE_CODE (value) == INTEGER_CST)
18016     {
18017       unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
18018
18019       /* If HOST_WIDE_INT is big enough then represent the bound as
18020          a constant value.  We need to choose a form based on
18021          whether the type is signed or unsigned.  We cannot just
18022          call add_AT_unsigned if the value itself is positive
18023          (add_AT_unsigned might add the unsigned value encoded as
18024          DW_FORM_data[1248]).  Some DWARF consumers will lookup the
18025          bounds type and then sign extend any unsigned values found
18026          for signed types.  This is needed only for
18027          DW_AT_{lower,upper}_bound, since for most other attributes,
18028          consumers will treat DW_FORM_data[1248] as unsigned values,
18029          regardless of the underlying type.  */
18030       if (prec <= HOST_BITS_PER_WIDE_INT
18031           || tree_fits_uhwi_p (value))
18032         {
18033           if (TYPE_UNSIGNED (TREE_TYPE (value)))
18034             add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
18035           else
18036             add_AT_int (die, attr, TREE_INT_CST_LOW (value));
18037         }
18038       else
18039         /* Otherwise represent the bound as an unsigned value with
18040            the precision of its type.  The precision and signedness
18041            of the type will be necessary to re-interpret it
18042            unambiguously.  */
18043         add_AT_wide (die, attr, value);
18044       return;
18045     }
18046
18047   /* Otherwise, if it's possible and permitted too, output a reference to
18048      another DIE.  */
18049   if ((forms & dw_scalar_form_reference) != 0)
18050     {
18051       tree decl = NULL_TREE;
18052
18053       /* Some type attributes reference an outer type.  For instance, the upper
18054          bound of an array may reference an embedding record (this happens in
18055          Ada).  */
18056       if (TREE_CODE (value) == COMPONENT_REF
18057           && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
18058           && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
18059         decl = TREE_OPERAND (value, 1);
18060
18061       else if (TREE_CODE (value) == VAR_DECL
18062                || TREE_CODE (value) == PARM_DECL
18063                || TREE_CODE (value) == RESULT_DECL)
18064         decl = value;
18065
18066       if (decl != NULL_TREE)
18067         {
18068           dw_die_ref decl_die = lookup_decl_die (decl);
18069
18070           /* ??? Can this happen, or should the variable have been bound
18071              first?  Probably it can, since I imagine that we try to create
18072              the types of parameters in the order in which they exist in
18073              the list, and won't have created a forward reference to a
18074              later parameter.  */
18075           if (decl_die != NULL)
18076             {
18077               add_AT_die_ref (die, attr, decl_die);
18078               return;
18079             }
18080         }
18081     }
18082
18083   /* Last chance: try to create a stack operation procedure to evaluate the
18084      value.  Do nothing if even that is not possible or permitted.  */
18085   if ((forms & dw_scalar_form_exprloc) == 0)
18086     return;
18087
18088   list = loc_list_from_tree (value, 2, context);
18089   if (list == NULL || single_element_loc_list_p (list))
18090     {
18091       /* If this attribute is not a reference nor constant, it is
18092          a DWARF expression rather than location description.  For that
18093          loc_list_from_tree (value, 0, &context) is needed.  */
18094       dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
18095       if (list2 && single_element_loc_list_p (list2))
18096         {
18097           add_AT_loc (die, attr, list2->expr);
18098           return;
18099         }
18100     }
18101
18102   /* If that failed to give a single element location list, fall back to
18103      outputting this as a reference... still if permitted.  */
18104   if (list == NULL || (forms & dw_scalar_form_reference) == 0)
18105     return;
18106
18107   if (current_function_decl == 0)
18108     ctx = comp_unit_die ();
18109   else
18110     ctx = lookup_decl_die (current_function_decl);
18111
18112   decl_die = new_die (DW_TAG_variable, ctx, value);
18113   add_AT_flag (decl_die, DW_AT_artificial, 1);
18114   add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, ctx);
18115   add_AT_location_description (decl_die, DW_AT_location, list);
18116   add_AT_die_ref (die, attr, decl_die);
18117 }
18118
18119 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
18120    default.  */
18121
18122 static int
18123 lower_bound_default (void)
18124 {
18125   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
18126     {
18127     case DW_LANG_C:
18128     case DW_LANG_C89:
18129     case DW_LANG_C99:
18130     case DW_LANG_C11:
18131     case DW_LANG_C_plus_plus:
18132     case DW_LANG_C_plus_plus_11:
18133     case DW_LANG_C_plus_plus_14:
18134     case DW_LANG_ObjC:
18135     case DW_LANG_ObjC_plus_plus:
18136     case DW_LANG_Java:
18137       return 0;
18138     case DW_LANG_Fortran77:
18139     case DW_LANG_Fortran90:
18140     case DW_LANG_Fortran95:
18141     case DW_LANG_Fortran03:
18142     case DW_LANG_Fortran08:
18143       return 1;
18144     case DW_LANG_UPC:
18145     case DW_LANG_D:
18146     case DW_LANG_Python:
18147       return dwarf_version >= 4 ? 0 : -1;
18148     case DW_LANG_Ada95:
18149     case DW_LANG_Ada83:
18150     case DW_LANG_Cobol74:
18151     case DW_LANG_Cobol85:
18152     case DW_LANG_Pascal83:
18153     case DW_LANG_Modula2:
18154     case DW_LANG_PLI:
18155       return dwarf_version >= 4 ? 1 : -1;
18156     default:
18157       return -1;
18158     }
18159 }
18160
18161 /* Given a tree node describing an array bound (either lower or upper) output
18162    a representation for that bound.  */
18163
18164 static void
18165 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
18166                 tree bound, const struct loc_descr_context *context)
18167 {
18168   int dflt;
18169
18170   while (1)
18171     switch (TREE_CODE (bound))
18172       {
18173       /* Strip all conversions.  */
18174       CASE_CONVERT:
18175       case VIEW_CONVERT_EXPR:
18176         bound = TREE_OPERAND (bound, 0);
18177         break;
18178
18179       /* All fixed-bounds are represented by INTEGER_CST nodes.  Lower bounds
18180          are even omitted when they are the default.  */
18181       case INTEGER_CST:
18182         /* If the value for this bound is the default one, we can even omit the
18183            attribute.  */
18184         if (bound_attr == DW_AT_lower_bound
18185             && tree_fits_shwi_p (bound)
18186             && (dflt = lower_bound_default ()) != -1
18187             && tree_to_shwi (bound) == dflt)
18188           return;
18189
18190         /* FALLTHRU */
18191
18192       default:
18193         /* Because of the complex interaction there can be with other GNAT
18194            encodings, GDB isn't ready yet to handle proper DWARF description
18195            for self-referencial subrange bounds: let GNAT encodings do the
18196            magic in such a case.  */
18197         if (gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL
18198             && contains_placeholder_p (bound))
18199           return;
18200
18201         add_scalar_info (subrange_die, bound_attr, bound,
18202                          dw_scalar_form_constant
18203                          | dw_scalar_form_exprloc
18204                          | dw_scalar_form_reference,
18205                          context);
18206         return;
18207       }
18208 }
18209
18210 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
18211    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
18212    Note that the block of subscript information for an array type also
18213    includes information about the element type of the given array type.
18214
18215    This function reuses previously set type and bound information if
18216    available.  */
18217
18218 static void
18219 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
18220 {
18221   unsigned dimension_number;
18222   tree lower, upper;
18223   dw_die_ref child = type_die->die_child;
18224
18225   for (dimension_number = 0;
18226        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
18227        type = TREE_TYPE (type), dimension_number++)
18228     {
18229       tree domain = TYPE_DOMAIN (type);
18230
18231       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
18232         break;
18233
18234       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
18235          and (in GNU C only) variable bounds.  Handle all three forms
18236          here.  */
18237
18238       /* Find and reuse a previously generated DW_TAG_subrange_type if
18239          available.
18240
18241          For multi-dimensional arrays, as we iterate through the
18242          various dimensions in the enclosing for loop above, we also
18243          iterate through the DIE children and pick at each
18244          DW_TAG_subrange_type previously generated (if available).
18245          Each child DW_TAG_subrange_type DIE describes the range of
18246          the current dimension.  At this point we should have as many
18247          DW_TAG_subrange_type's as we have dimensions in the
18248          array.  */
18249       dw_die_ref subrange_die = NULL;
18250       if (child)
18251         while (1)
18252           {
18253             child = child->die_sib;
18254             if (child->die_tag == DW_TAG_subrange_type)
18255               subrange_die = child;
18256             if (child == type_die->die_child)
18257               {
18258                 /* If we wrapped around, stop looking next time.  */
18259                 child = NULL;
18260                 break;
18261               }
18262             if (child->die_tag == DW_TAG_subrange_type)
18263               break;
18264           }
18265       if (!subrange_die)
18266         subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
18267
18268       if (domain)
18269         {
18270           /* We have an array type with specified bounds.  */
18271           lower = TYPE_MIN_VALUE (domain);
18272           upper = TYPE_MAX_VALUE (domain);
18273
18274           /* Define the index type.  */
18275           if (TREE_TYPE (domain)
18276               && !get_AT (subrange_die, DW_AT_type))
18277             {
18278               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
18279                  TREE_TYPE field.  We can't emit debug info for this
18280                  because it is an unnamed integral type.  */
18281               if (TREE_CODE (domain) == INTEGER_TYPE
18282                   && TYPE_NAME (domain) == NULL_TREE
18283                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
18284                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
18285                 ;
18286               else
18287                 add_type_attribute (subrange_die, TREE_TYPE (domain),
18288                                     TYPE_UNQUALIFIED, type_die);
18289             }
18290
18291           /* ??? If upper is NULL, the array has unspecified length,
18292              but it does have a lower bound.  This happens with Fortran
18293                dimension arr(N:*)
18294              Since the debugger is definitely going to need to know N
18295              to produce useful results, go ahead and output the lower
18296              bound solo, and hope the debugger can cope.  */
18297
18298           if (!get_AT (subrange_die, DW_AT_lower_bound))
18299             add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
18300           if (upper && !get_AT (subrange_die, DW_AT_upper_bound))
18301             add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
18302         }
18303
18304       /* Otherwise we have an array type with an unspecified length.  The
18305          DWARF-2 spec does not say how to handle this; let's just leave out the
18306          bounds.  */
18307     }
18308 }
18309
18310 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size.  */
18311
18312 static void
18313 add_byte_size_attribute (dw_die_ref die, tree tree_node)
18314 {
18315   dw_die_ref decl_die;
18316   HOST_WIDE_INT size;
18317   dw_loc_descr_ref size_expr = NULL;
18318
18319   switch (TREE_CODE (tree_node))
18320     {
18321     case ERROR_MARK:
18322       size = 0;
18323       break;
18324     case ENUMERAL_TYPE:
18325     case RECORD_TYPE:
18326     case UNION_TYPE:
18327     case QUAL_UNION_TYPE:
18328       if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
18329           && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
18330         {
18331           add_AT_die_ref (die, DW_AT_byte_size, decl_die);
18332           return;
18333         }
18334       size_expr = type_byte_size (tree_node, &size);
18335       break;
18336     case FIELD_DECL:
18337       /* For a data member of a struct or union, the DW_AT_byte_size is
18338          generally given as the number of bytes normally allocated for an
18339          object of the *declared* type of the member itself.  This is true
18340          even for bit-fields.  */
18341       size = int_size_in_bytes (field_type (tree_node));
18342       break;
18343     default:
18344       gcc_unreachable ();
18345     }
18346
18347   /* Support for dynamically-sized objects was introduced by DWARFv3.
18348      At the moment, GDB does not handle variable byte sizes very well,
18349      though.  */
18350   if ((dwarf_version >= 3 || !dwarf_strict)
18351       && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL
18352       && size_expr != NULL)
18353     add_AT_loc (die, DW_AT_byte_size, size_expr);
18354
18355   /* Note that `size' might be -1 when we get to this point.  If it is, that
18356      indicates that the byte size of the entity in question is variable and
18357      that we could not generate a DWARF expression that computes it.  */
18358   if (size >= 0)
18359     add_AT_unsigned (die, DW_AT_byte_size, size);
18360 }
18361
18362 /* For a FIELD_DECL node which represents a bit-field, output an attribute
18363    which specifies the distance in bits from the highest order bit of the
18364    "containing object" for the bit-field to the highest order bit of the
18365    bit-field itself.
18366
18367    For any given bit-field, the "containing object" is a hypothetical object
18368    (of some integral or enum type) within which the given bit-field lives.  The
18369    type of this hypothetical "containing object" is always the same as the
18370    declared type of the individual bit-field itself.  The determination of the
18371    exact location of the "containing object" for a bit-field is rather
18372    complicated.  It's handled by the `field_byte_offset' function (above).
18373
18374    CTX is required: see the comment for VLR_CONTEXT.
18375
18376    Note that it is the size (in bytes) of the hypothetical "containing object"
18377    which will be given in the DW_AT_byte_size attribute for this bit-field.
18378    (See `byte_size_attribute' above).  */
18379
18380 static inline void
18381 add_bit_offset_attribute (dw_die_ref die, tree decl, struct vlr_context *ctx)
18382 {
18383   HOST_WIDE_INT object_offset_in_bytes;
18384   tree original_type = DECL_BIT_FIELD_TYPE (decl);
18385   HOST_WIDE_INT bitpos_int;
18386   HOST_WIDE_INT highest_order_object_bit_offset;
18387   HOST_WIDE_INT highest_order_field_bit_offset;
18388   HOST_WIDE_INT bit_offset;
18389
18390   field_byte_offset (decl, ctx, &object_offset_in_bytes);
18391
18392   /* Must be a field and a bit field.  */
18393   gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
18394
18395   /* We can't yet handle bit-fields whose offsets are variable, so if we
18396      encounter such things, just return without generating any attribute
18397      whatsoever.  Likewise for variable or too large size.  */
18398   if (! tree_fits_shwi_p (bit_position (decl))
18399       || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
18400     return;
18401
18402   bitpos_int = int_bit_position (decl);
18403
18404   /* Note that the bit offset is always the distance (in bits) from the
18405      highest-order bit of the "containing object" to the highest-order bit of
18406      the bit-field itself.  Since the "high-order end" of any object or field
18407      is different on big-endian and little-endian machines, the computation
18408      below must take account of these differences.  */
18409   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
18410   highest_order_field_bit_offset = bitpos_int;
18411
18412   if (! BYTES_BIG_ENDIAN)
18413     {
18414       highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
18415       highest_order_object_bit_offset +=
18416         simple_type_size_in_bits (original_type);
18417     }
18418
18419   bit_offset
18420     = (! BYTES_BIG_ENDIAN
18421        ? highest_order_object_bit_offset - highest_order_field_bit_offset
18422        : highest_order_field_bit_offset - highest_order_object_bit_offset);
18423
18424   if (bit_offset < 0)
18425     add_AT_int (die, DW_AT_bit_offset, bit_offset);
18426   else
18427     add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
18428 }
18429
18430 /* For a FIELD_DECL node which represents a bit field, output an attribute
18431    which specifies the length in bits of the given field.  */
18432
18433 static inline void
18434 add_bit_size_attribute (dw_die_ref die, tree decl)
18435 {
18436   /* Must be a field and a bit field.  */
18437   gcc_assert (TREE_CODE (decl) == FIELD_DECL
18438               && DECL_BIT_FIELD_TYPE (decl));
18439
18440   if (tree_fits_uhwi_p (DECL_SIZE (decl)))
18441     add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
18442 }
18443
18444 /* If the compiled language is ANSI C, then add a 'prototyped'
18445    attribute, if arg types are given for the parameters of a function.  */
18446
18447 static inline void
18448 add_prototyped_attribute (dw_die_ref die, tree func_type)
18449 {
18450   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
18451     {
18452     case DW_LANG_C:
18453     case DW_LANG_C89:
18454     case DW_LANG_C99:
18455     case DW_LANG_C11:
18456     case DW_LANG_ObjC:
18457       if (prototype_p (func_type))
18458         add_AT_flag (die, DW_AT_prototyped, 1);
18459       break;
18460     default:
18461       break;
18462     }
18463 }
18464
18465 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
18466    by looking in either the type declaration or object declaration
18467    equate table.  */
18468
18469 static inline dw_die_ref
18470 add_abstract_origin_attribute (dw_die_ref die, tree origin)
18471 {
18472   dw_die_ref origin_die = NULL;
18473
18474   if (TREE_CODE (origin) != FUNCTION_DECL)
18475     {
18476       /* We may have gotten separated from the block for the inlined
18477          function, if we're in an exception handler or some such; make
18478          sure that the abstract function has been written out.
18479
18480          Doing this for nested functions is wrong, however; functions are
18481          distinct units, and our context might not even be inline.  */
18482       tree fn = origin;
18483
18484       if (TYPE_P (fn))
18485         fn = TYPE_STUB_DECL (fn);
18486
18487       fn = decl_function_context (fn);
18488       if (fn)
18489         dwarf2out_abstract_function (fn);
18490     }
18491
18492   if (DECL_P (origin))
18493     origin_die = lookup_decl_die (origin);
18494   else if (TYPE_P (origin))
18495     origin_die = lookup_type_die (origin);
18496
18497   /* XXX: Functions that are never lowered don't always have correct block
18498      trees (in the case of java, they simply have no block tree, in some other
18499      languages).  For these functions, there is nothing we can really do to
18500      output correct debug info for inlined functions in all cases.  Rather
18501      than die, we'll just produce deficient debug info now, in that we will
18502      have variables without a proper abstract origin.  In the future, when all
18503      functions are lowered, we should re-add a gcc_assert (origin_die)
18504      here.  */
18505
18506   if (origin_die)
18507     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
18508   return origin_die;
18509 }
18510
18511 /* We do not currently support the pure_virtual attribute.  */
18512
18513 static inline void
18514 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
18515 {
18516   if (DECL_VINDEX (func_decl))
18517     {
18518       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
18519
18520       if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
18521         add_AT_loc (die, DW_AT_vtable_elem_location,
18522                     new_loc_descr (DW_OP_constu,
18523                                    tree_to_shwi (DECL_VINDEX (func_decl)),
18524                                    0));
18525
18526       /* GNU extension: Record what type this method came from originally.  */
18527       if (debug_info_level > DINFO_LEVEL_TERSE
18528           && DECL_CONTEXT (func_decl))
18529         add_AT_die_ref (die, DW_AT_containing_type,
18530                         lookup_type_die (DECL_CONTEXT (func_decl)));
18531     }
18532 }
18533 \f
18534 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
18535    given decl.  This used to be a vendor extension until after DWARF 4
18536    standardized it.  */
18537
18538 static void
18539 add_linkage_attr (dw_die_ref die, tree decl)
18540 {
18541   const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
18542
18543   /* Mimic what assemble_name_raw does with a leading '*'.  */
18544   if (name[0] == '*')
18545     name = &name[1];
18546
18547   if (dwarf_version >= 4)
18548     add_AT_string (die, DW_AT_linkage_name, name);
18549   else
18550     add_AT_string (die, DW_AT_MIPS_linkage_name, name);
18551 }
18552
18553 /* Add source coordinate attributes for the given decl.  */
18554
18555 static void
18556 add_src_coords_attributes (dw_die_ref die, tree decl)
18557 {
18558   expanded_location s;
18559
18560   if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
18561     return;
18562   s = expand_location (DECL_SOURCE_LOCATION (decl));
18563   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
18564   add_AT_unsigned (die, DW_AT_decl_line, s.line);
18565 }
18566
18567 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl.  */
18568
18569 static void
18570 add_linkage_name (dw_die_ref die, tree decl)
18571 {
18572   if (debug_info_level > DINFO_LEVEL_NONE
18573       && (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
18574       && TREE_PUBLIC (decl)
18575       && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
18576       && die->die_tag != DW_TAG_member)
18577     {
18578       /* Defer until we have an assembler name set.  */
18579       if (!DECL_ASSEMBLER_NAME_SET_P (decl))
18580         {
18581           limbo_die_node *asm_name;
18582
18583           asm_name = ggc_cleared_alloc<limbo_die_node> ();
18584           asm_name->die = die;
18585           asm_name->created_for = decl;
18586           asm_name->next = deferred_asm_name;
18587           deferred_asm_name = asm_name;
18588         }
18589       else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
18590         add_linkage_attr (die, decl);
18591     }
18592 }
18593
18594 /* Add a DW_AT_name attribute and source coordinate attribute for the
18595    given decl, but only if it actually has a name.  */
18596
18597 static void
18598 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
18599 {
18600   tree decl_name;
18601
18602   decl_name = DECL_NAME (decl);
18603   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
18604     {
18605       const char *name = dwarf2_name (decl, 0);
18606       if (name)
18607         add_name_attribute (die, name);
18608       if (! DECL_ARTIFICIAL (decl))
18609         add_src_coords_attributes (die, decl);
18610
18611       add_linkage_name (die, decl);
18612     }
18613
18614 #ifdef VMS_DEBUGGING_INFO
18615   /* Get the function's name, as described by its RTL.  This may be different
18616      from the DECL_NAME name used in the source file.  */
18617   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
18618     {
18619       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
18620                   XEXP (DECL_RTL (decl), 0), false);
18621       vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
18622     }
18623 #endif /* VMS_DEBUGGING_INFO */
18624 }
18625
18626 /* Add VALUE as a DW_AT_discr_value attribute to DIE.  */
18627
18628 static void
18629 add_discr_value (dw_die_ref die, dw_discr_value *value)
18630 {
18631   dw_attr_node attr;
18632
18633   attr.dw_attr = DW_AT_discr_value;
18634   attr.dw_attr_val.val_class = dw_val_class_discr_value;
18635   attr.dw_attr_val.val_entry = NULL;
18636   attr.dw_attr_val.v.val_discr_value.pos = value->pos;
18637   if (value->pos)
18638     attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
18639   else
18640     attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
18641   add_dwarf_attr (die, &attr);
18642 }
18643
18644 /* Add DISCR_LIST as a DW_AT_discr_list to DIE.  */
18645
18646 static void
18647 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
18648 {
18649   dw_attr_node attr;
18650
18651   attr.dw_attr = DW_AT_discr_list;
18652   attr.dw_attr_val.val_class = dw_val_class_discr_list;
18653   attr.dw_attr_val.val_entry = NULL;
18654   attr.dw_attr_val.v.val_discr_list = discr_list;
18655   add_dwarf_attr (die, &attr);
18656 }
18657
18658 static inline dw_discr_list_ref
18659 AT_discr_list (dw_attr_node *attr)
18660 {
18661   return attr->dw_attr_val.v.val_discr_list;
18662 }
18663
18664 #ifdef VMS_DEBUGGING_INFO
18665 /* Output the debug main pointer die for VMS */
18666
18667 void
18668 dwarf2out_vms_debug_main_pointer (void)
18669 {
18670   char label[MAX_ARTIFICIAL_LABEL_BYTES];
18671   dw_die_ref die;
18672
18673   /* Allocate the VMS debug main subprogram die.  */
18674   die = ggc_cleared_alloc<die_node> ();
18675   die->die_tag = DW_TAG_subprogram;
18676   add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
18677   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
18678                                current_function_funcdef_no);
18679   add_AT_lbl_id (die, DW_AT_entry_pc, label);
18680
18681   /* Make it the first child of comp_unit_die ().  */
18682   die->die_parent = comp_unit_die ();
18683   if (comp_unit_die ()->die_child)
18684     {
18685       die->die_sib = comp_unit_die ()->die_child->die_sib;
18686       comp_unit_die ()->die_child->die_sib = die;
18687     }
18688   else
18689     {
18690       die->die_sib = die;
18691       comp_unit_die ()->die_child = die;
18692     }
18693 }
18694 #endif /* VMS_DEBUGGING_INFO */
18695
18696 /* Push a new declaration scope.  */
18697
18698 static void
18699 push_decl_scope (tree scope)
18700 {
18701   vec_safe_push (decl_scope_table, scope);
18702 }
18703
18704 /* Pop a declaration scope.  */
18705
18706 static inline void
18707 pop_decl_scope (void)
18708 {
18709   decl_scope_table->pop ();
18710 }
18711
18712 /* walk_tree helper function for uses_local_type, below.  */
18713
18714 static tree
18715 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
18716 {
18717   if (!TYPE_P (*tp))
18718     *walk_subtrees = 0;
18719   else
18720     {
18721       tree name = TYPE_NAME (*tp);
18722       if (name && DECL_P (name) && decl_function_context (name))
18723         return *tp;
18724     }
18725   return NULL_TREE;
18726 }
18727
18728 /* If TYPE involves a function-local type (including a local typedef to a
18729    non-local type), returns that type; otherwise returns NULL_TREE.  */
18730
18731 static tree
18732 uses_local_type (tree type)
18733 {
18734   tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
18735   return used;
18736 }
18737
18738 /* Return the DIE for the scope that immediately contains this type.
18739    Non-named types that do not involve a function-local type get global
18740    scope.  Named types nested in namespaces or other types get their
18741    containing scope.  All other types (i.e. function-local named types) get
18742    the current active scope.  */
18743
18744 static dw_die_ref
18745 scope_die_for (tree t, dw_die_ref context_die)
18746 {
18747   dw_die_ref scope_die = NULL;
18748   tree containing_scope;
18749
18750   /* Non-types always go in the current scope.  */
18751   gcc_assert (TYPE_P (t));
18752
18753   /* Use the scope of the typedef, rather than the scope of the type
18754      it refers to.  */
18755   if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
18756     containing_scope = DECL_CONTEXT (TYPE_NAME (t));
18757   else
18758     containing_scope = TYPE_CONTEXT (t);
18759
18760   /* Use the containing namespace if there is one.  */
18761   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
18762     {
18763       if (context_die == lookup_decl_die (containing_scope))
18764         /* OK */;
18765       else if (debug_info_level > DINFO_LEVEL_TERSE)
18766         context_die = get_context_die (containing_scope);
18767       else
18768         containing_scope = NULL_TREE;
18769     }
18770
18771   /* Ignore function type "scopes" from the C frontend.  They mean that
18772      a tagged type is local to a parmlist of a function declarator, but
18773      that isn't useful to DWARF.  */
18774   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
18775     containing_scope = NULL_TREE;
18776
18777   if (SCOPE_FILE_SCOPE_P (containing_scope))
18778     {
18779       /* If T uses a local type keep it local as well, to avoid references
18780          to function-local DIEs from outside the function.  */
18781       if (current_function_decl && uses_local_type (t))
18782         scope_die = context_die;
18783       else
18784         scope_die = comp_unit_die ();
18785     }
18786   else if (TYPE_P (containing_scope))
18787     {
18788       /* For types, we can just look up the appropriate DIE.  */
18789       if (debug_info_level > DINFO_LEVEL_TERSE)
18790         scope_die = get_context_die (containing_scope);
18791       else
18792         {
18793           scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
18794           if (scope_die == NULL)
18795             scope_die = comp_unit_die ();
18796         }
18797     }
18798   else
18799     scope_die = context_die;
18800
18801   return scope_die;
18802 }
18803
18804 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
18805
18806 static inline int
18807 local_scope_p (dw_die_ref context_die)
18808 {
18809   for (; context_die; context_die = context_die->die_parent)
18810     if (context_die->die_tag == DW_TAG_inlined_subroutine
18811         || context_die->die_tag == DW_TAG_subprogram)
18812       return 1;
18813
18814   return 0;
18815 }
18816
18817 /* Returns nonzero if CONTEXT_DIE is a class.  */
18818
18819 static inline int
18820 class_scope_p (dw_die_ref context_die)
18821 {
18822   return (context_die
18823           && (context_die->die_tag == DW_TAG_structure_type
18824               || context_die->die_tag == DW_TAG_class_type
18825               || context_die->die_tag == DW_TAG_interface_type
18826               || context_die->die_tag == DW_TAG_union_type));
18827 }
18828
18829 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
18830    whether or not to treat a DIE in this context as a declaration.  */
18831
18832 static inline int
18833 class_or_namespace_scope_p (dw_die_ref context_die)
18834 {
18835   return (class_scope_p (context_die)
18836           || (context_die && context_die->die_tag == DW_TAG_namespace));
18837 }
18838
18839 /* Many forms of DIEs require a "type description" attribute.  This
18840    routine locates the proper "type descriptor" die for the type given
18841    by 'type' plus any additional qualifiers given by 'cv_quals', and
18842    adds a DW_AT_type attribute below the given die.  */
18843
18844 static void
18845 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
18846                     dw_die_ref context_die)
18847 {
18848   enum tree_code code  = TREE_CODE (type);
18849   dw_die_ref type_die  = NULL;
18850
18851   /* ??? If this type is an unnamed subrange type of an integral, floating-point
18852      or fixed-point type, use the inner type.  This is because we have no
18853      support for unnamed types in base_type_die.  This can happen if this is
18854      an Ada subrange type.  Correct solution is emit a subrange type die.  */
18855   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
18856       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
18857     type = TREE_TYPE (type), code = TREE_CODE (type);
18858
18859   if (code == ERROR_MARK
18860       /* Handle a special case.  For functions whose return type is void, we
18861          generate *no* type attribute.  (Note that no object may have type
18862          `void', so this only applies to function return types).  */
18863       || code == VOID_TYPE)
18864     return;
18865
18866   type_die = modified_type_die (type,
18867                                 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
18868                                 context_die);
18869
18870   if (type_die != NULL)
18871     add_AT_die_ref (object_die, DW_AT_type, type_die);
18872 }
18873
18874 /* Given an object die, add the calling convention attribute for the
18875    function call type.  */
18876 static void
18877 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
18878 {
18879   enum dwarf_calling_convention value = DW_CC_normal;
18880
18881   value = ((enum dwarf_calling_convention)
18882            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
18883
18884   if (is_fortran ()
18885       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
18886     {
18887       /* DWARF 2 doesn't provide a way to identify a program's source-level
18888         entry point.  DW_AT_calling_convention attributes are only meant
18889         to describe functions' calling conventions.  However, lacking a
18890         better way to signal the Fortran main program, we used this for 
18891         a long time, following existing custom.  Now, DWARF 4 has 
18892         DW_AT_main_subprogram, which we add below, but some tools still
18893         rely on the old way, which we thus keep.  */
18894       value = DW_CC_program;
18895
18896       if (dwarf_version >= 4 || !dwarf_strict)
18897         add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
18898     }
18899
18900   /* Only add the attribute if the backend requests it, and
18901      is not DW_CC_normal.  */
18902   if (value && (value != DW_CC_normal))
18903     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
18904 }
18905
18906 /* Given a tree pointer to a struct, class, union, or enum type node, return
18907    a pointer to the (string) tag name for the given type, or zero if the type
18908    was declared without a tag.  */
18909
18910 static const char *
18911 type_tag (const_tree type)
18912 {
18913   const char *name = 0;
18914
18915   if (TYPE_NAME (type) != 0)
18916     {
18917       tree t = 0;
18918
18919       /* Find the IDENTIFIER_NODE for the type name.  */
18920       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
18921           && !TYPE_NAMELESS (type))
18922         t = TYPE_NAME (type);
18923
18924       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
18925          a TYPE_DECL node, regardless of whether or not a `typedef' was
18926          involved.  */
18927       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
18928                && ! DECL_IGNORED_P (TYPE_NAME (type)))
18929         {
18930           /* We want to be extra verbose.  Don't call dwarf_name if
18931              DECL_NAME isn't set.  The default hook for decl_printable_name
18932              doesn't like that, and in this context it's correct to return
18933              0, instead of "<anonymous>" or the like.  */
18934           if (DECL_NAME (TYPE_NAME (type))
18935               && !DECL_NAMELESS (TYPE_NAME (type)))
18936             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
18937         }
18938
18939       /* Now get the name as a string, or invent one.  */
18940       if (!name && t != 0)
18941         name = IDENTIFIER_POINTER (t);
18942     }
18943
18944   return (name == 0 || *name == '\0') ? 0 : name;
18945 }
18946
18947 /* Return the type associated with a data member, make a special check
18948    for bit field types.  */
18949
18950 static inline tree
18951 member_declared_type (const_tree member)
18952 {
18953   return (DECL_BIT_FIELD_TYPE (member)
18954           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
18955 }
18956
18957 /* Get the decl's label, as described by its RTL. This may be different
18958    from the DECL_NAME name used in the source file.  */
18959
18960 #if 0
18961 static const char *
18962 decl_start_label (tree decl)
18963 {
18964   rtx x;
18965   const char *fnname;
18966
18967   x = DECL_RTL (decl);
18968   gcc_assert (MEM_P (x));
18969
18970   x = XEXP (x, 0);
18971   gcc_assert (GET_CODE (x) == SYMBOL_REF);
18972
18973   fnname = XSTR (x, 0);
18974   return fnname;
18975 }
18976 #endif
18977 \f
18978 /* For variable-length arrays that have been previously generated, but
18979    may be incomplete due to missing subscript info, fill the subscript
18980    info.  Return TRUE if this is one of those cases.  */
18981 static bool
18982 fill_variable_array_bounds (tree type)
18983 {
18984   if (TREE_ASM_WRITTEN (type)
18985       && TREE_CODE (type) == ARRAY_TYPE
18986       && variably_modified_type_p (type, NULL))
18987     {
18988       dw_die_ref array_die = lookup_type_die (type);
18989       if (!array_die)
18990         return false;
18991       add_subscript_info (array_die, type, !is_ada ());
18992       return true;
18993     }
18994   return false;
18995 }
18996
18997 /* These routines generate the internal representation of the DIE's for
18998    the compilation unit.  Debugging information is collected by walking
18999    the declaration trees passed in from dwarf2out_decl().  */
19000
19001 static void
19002 gen_array_type_die (tree type, dw_die_ref context_die)
19003 {
19004   dw_die_ref array_die;
19005
19006   /* GNU compilers represent multidimensional array types as sequences of one
19007      dimensional array types whose element types are themselves array types.
19008      We sometimes squish that down to a single array_type DIE with multiple
19009      subscripts in the Dwarf debugging info.  The draft Dwarf specification
19010      say that we are allowed to do this kind of compression in C, because
19011      there is no difference between an array of arrays and a multidimensional
19012      array.  We don't do this for Ada to remain as close as possible to the
19013      actual representation, which is especially important against the language
19014      flexibilty wrt arrays of variable size.  */
19015
19016   bool collapse_nested_arrays = !is_ada ();
19017
19018   if (fill_variable_array_bounds (type))
19019     return;
19020
19021   dw_die_ref scope_die = scope_die_for (type, context_die);
19022   tree element_type;
19023
19024   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
19025      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
19026   if (TYPE_STRING_FLAG (type)
19027       && TREE_CODE (type) == ARRAY_TYPE
19028       && is_fortran ()
19029       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
19030     {
19031       HOST_WIDE_INT size;
19032
19033       array_die = new_die (DW_TAG_string_type, scope_die, type);
19034       add_name_attribute (array_die, type_tag (type));
19035       equate_type_number_to_die (type, array_die);
19036       size = int_size_in_bytes (type);
19037       if (size >= 0)
19038         add_AT_unsigned (array_die, DW_AT_byte_size, size);
19039       else if (TYPE_DOMAIN (type) != NULL_TREE
19040                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
19041                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
19042         {
19043           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
19044           dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2, NULL);
19045
19046           size = int_size_in_bytes (TREE_TYPE (szdecl));
19047           if (loc && size > 0)
19048             {
19049               add_AT_location_description (array_die, DW_AT_string_length, loc);
19050               if (size != DWARF2_ADDR_SIZE)
19051                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
19052             }
19053         }
19054       return;
19055     }
19056
19057   array_die = new_die (DW_TAG_array_type, scope_die, type);
19058   add_name_attribute (array_die, type_tag (type));
19059   equate_type_number_to_die (type, array_die);
19060
19061   if (TREE_CODE (type) == VECTOR_TYPE)
19062     add_AT_flag (array_die, DW_AT_GNU_vector, 1);
19063
19064   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
19065   if (is_fortran ()
19066       && TREE_CODE (type) == ARRAY_TYPE
19067       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
19068       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
19069     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
19070
19071 #if 0
19072   /* We default the array ordering.  SDB will probably do
19073      the right things even if DW_AT_ordering is not present.  It's not even
19074      an issue until we start to get into multidimensional arrays anyway.  If
19075      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
19076      then we'll have to put the DW_AT_ordering attribute back in.  (But if
19077      and when we find out that we need to put these in, we will only do so
19078      for multidimensional arrays.  */
19079   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
19080 #endif
19081
19082   if (TREE_CODE (type) == VECTOR_TYPE)
19083     {
19084       /* For VECTOR_TYPEs we use an array die with appropriate bounds.  */
19085       dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
19086       add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
19087       add_bound_info (subrange_die, DW_AT_upper_bound,
19088                       size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
19089     }
19090   else
19091     add_subscript_info (array_die, type, collapse_nested_arrays);
19092
19093   /* Add representation of the type of the elements of this array type and
19094      emit the corresponding DIE if we haven't done it already.  */
19095   element_type = TREE_TYPE (type);
19096   if (collapse_nested_arrays)
19097     while (TREE_CODE (element_type) == ARRAY_TYPE)
19098       {
19099         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
19100           break;
19101         element_type = TREE_TYPE (element_type);
19102       }
19103
19104   add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED, context_die);
19105
19106   add_gnat_descriptive_type_attribute (array_die, type, context_die);
19107   if (TYPE_ARTIFICIAL (type))
19108     add_AT_flag (array_die, DW_AT_artificial, 1);
19109
19110   if (get_AT (array_die, DW_AT_name))
19111     add_pubtype (type, array_die);
19112 }
19113
19114 /* This routine generates DIE for array with hidden descriptor, details
19115    are filled into *info by a langhook.  */
19116
19117 static void
19118 gen_descr_array_type_die (tree type, struct array_descr_info *info,
19119                           dw_die_ref context_die)
19120 {
19121   const dw_die_ref scope_die = scope_die_for (type, context_die);
19122   const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
19123   const struct loc_descr_context context = { type, info->base_decl, NULL };
19124   int dim;
19125
19126   add_name_attribute (array_die, type_tag (type));
19127   equate_type_number_to_die (type, array_die);
19128
19129   if (info->ndimensions > 1)
19130     switch (info->ordering)
19131       {
19132       case array_descr_ordering_row_major:
19133         add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
19134         break;
19135       case array_descr_ordering_column_major:
19136         add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
19137         break;
19138       default:
19139         break;
19140       }
19141
19142   if (dwarf_version >= 3 || !dwarf_strict)
19143     {
19144       if (info->data_location)
19145         add_scalar_info (array_die, DW_AT_data_location, info->data_location,
19146                          dw_scalar_form_exprloc, &context);
19147       if (info->associated)
19148         add_scalar_info (array_die, DW_AT_associated, info->associated,
19149                          dw_scalar_form_constant
19150                          | dw_scalar_form_exprloc
19151                          | dw_scalar_form_reference, &context);
19152       if (info->allocated)
19153         add_scalar_info (array_die, DW_AT_allocated, info->allocated,
19154                          dw_scalar_form_constant
19155                          | dw_scalar_form_exprloc
19156                          | dw_scalar_form_reference, &context);
19157       if (info->stride)
19158         {
19159           const enum dwarf_attribute attr
19160             = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
19161           const int forms
19162             = (info->stride_in_bits)
19163               ? dw_scalar_form_constant
19164               : (dw_scalar_form_constant
19165                  | dw_scalar_form_exprloc
19166                  | dw_scalar_form_reference);
19167
19168           add_scalar_info (array_die, attr, info->stride, forms, &context);
19169         }
19170     }
19171
19172   add_gnat_descriptive_type_attribute (array_die, type, context_die);
19173
19174   for (dim = 0; dim < info->ndimensions; dim++)
19175     {
19176       dw_die_ref subrange_die
19177         = new_die (DW_TAG_subrange_type, array_die, NULL);
19178
19179       if (info->dimen[dim].bounds_type)
19180         add_type_attribute (subrange_die,
19181                             info->dimen[dim].bounds_type, 0,
19182                             context_die);
19183       if (info->dimen[dim].lower_bound)
19184         add_bound_info (subrange_die, DW_AT_lower_bound,
19185                         info->dimen[dim].lower_bound, &context);
19186       if (info->dimen[dim].upper_bound)
19187         add_bound_info (subrange_die, DW_AT_upper_bound,
19188                         info->dimen[dim].upper_bound, &context);
19189       if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
19190         add_scalar_info (subrange_die, DW_AT_byte_stride,
19191                          info->dimen[dim].stride,
19192                          dw_scalar_form_constant
19193                          | dw_scalar_form_exprloc
19194                          | dw_scalar_form_reference,
19195                          &context);
19196     }
19197
19198   gen_type_die (info->element_type, context_die);
19199   add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
19200                       context_die);
19201
19202   if (get_AT (array_die, DW_AT_name))
19203     add_pubtype (type, array_die);
19204 }
19205
19206 #if 0
19207 static void
19208 gen_entry_point_die (tree decl, dw_die_ref context_die)
19209 {
19210   tree origin = decl_ultimate_origin (decl);
19211   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
19212
19213   if (origin != NULL)
19214     add_abstract_origin_attribute (decl_die, origin);
19215   else
19216     {
19217       add_name_and_src_coords_attributes (decl_die, decl);
19218       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
19219                           TYPE_UNQUALIFIED, context_die);
19220     }
19221
19222   if (DECL_ABSTRACT_P (decl))
19223     equate_decl_number_to_die (decl, decl_die);
19224   else
19225     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
19226 }
19227 #endif
19228
19229 /* Walk through the list of incomplete types again, trying once more to
19230    emit full debugging info for them.  */
19231
19232 static void
19233 retry_incomplete_types (void)
19234 {
19235   int i;
19236
19237   for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
19238     if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
19239       gen_type_die ((*incomplete_types)[i], comp_unit_die ());
19240 }
19241
19242 /* Determine what tag to use for a record type.  */
19243
19244 static enum dwarf_tag
19245 record_type_tag (tree type)
19246 {
19247   if (! lang_hooks.types.classify_record)
19248     return DW_TAG_structure_type;
19249
19250   switch (lang_hooks.types.classify_record (type))
19251     {
19252     case RECORD_IS_STRUCT:
19253       return DW_TAG_structure_type;
19254
19255     case RECORD_IS_CLASS:
19256       return DW_TAG_class_type;
19257
19258     case RECORD_IS_INTERFACE:
19259       if (dwarf_version >= 3 || !dwarf_strict)
19260         return DW_TAG_interface_type;
19261       return DW_TAG_structure_type;
19262
19263     default:
19264       gcc_unreachable ();
19265     }
19266 }
19267
19268 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
19269    include all of the information about the enumeration values also. Each
19270    enumerated type name/value is listed as a child of the enumerated type
19271    DIE.  */
19272
19273 static dw_die_ref
19274 gen_enumeration_type_die (tree type, dw_die_ref context_die)
19275 {
19276   dw_die_ref type_die = lookup_type_die (type);
19277
19278   if (type_die == NULL)
19279     {
19280       type_die = new_die (DW_TAG_enumeration_type,
19281                           scope_die_for (type, context_die), type);
19282       equate_type_number_to_die (type, type_die);
19283       add_name_attribute (type_die, type_tag (type));
19284       if (dwarf_version >= 4 || !dwarf_strict)
19285         {
19286           if (ENUM_IS_SCOPED (type))
19287             add_AT_flag (type_die, DW_AT_enum_class, 1);
19288           if (ENUM_IS_OPAQUE (type))
19289             add_AT_flag (type_die, DW_AT_declaration, 1);
19290         }
19291     }
19292   else if (! TYPE_SIZE (type))
19293     return type_die;
19294   else
19295     remove_AT (type_die, DW_AT_declaration);
19296
19297   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
19298      given enum type is incomplete, do not generate the DW_AT_byte_size
19299      attribute or the DW_AT_element_list attribute.  */
19300   if (TYPE_SIZE (type))
19301     {
19302       tree link;
19303
19304       TREE_ASM_WRITTEN (type) = 1;
19305       add_byte_size_attribute (type_die, type);
19306       if (dwarf_version >= 3 || !dwarf_strict)
19307         {
19308           tree underlying = lang_hooks.types.enum_underlying_base_type (type);
19309           add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED,
19310                               context_die);
19311         }
19312       if (TYPE_STUB_DECL (type) != NULL_TREE)
19313         {
19314           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19315           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19316         }
19317
19318       /* If the first reference to this type was as the return type of an
19319          inline function, then it may not have a parent.  Fix this now.  */
19320       if (type_die->die_parent == NULL)
19321         add_child_die (scope_die_for (type, context_die), type_die);
19322
19323       for (link = TYPE_VALUES (type);
19324            link != NULL; link = TREE_CHAIN (link))
19325         {
19326           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
19327           tree value = TREE_VALUE (link);
19328
19329           add_name_attribute (enum_die,
19330                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
19331
19332           if (TREE_CODE (value) == CONST_DECL)
19333             value = DECL_INITIAL (value);
19334
19335           if (simple_type_size_in_bits (TREE_TYPE (value))
19336               <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
19337             {
19338               /* For constant forms created by add_AT_unsigned DWARF
19339                  consumers (GDB, elfutils, etc.) always zero extend
19340                  the value.  Only when the actual value is negative
19341                  do we need to use add_AT_int to generate a constant
19342                  form that can represent negative values.  */
19343               HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
19344               if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
19345                 add_AT_unsigned (enum_die, DW_AT_const_value,
19346                                  (unsigned HOST_WIDE_INT) val);
19347               else
19348                 add_AT_int (enum_die, DW_AT_const_value, val);
19349             }
19350           else
19351             /* Enumeration constants may be wider than HOST_WIDE_INT.  Handle
19352                that here.  TODO: This should be re-worked to use correct
19353                signed/unsigned double tags for all cases.  */
19354             add_AT_wide (enum_die, DW_AT_const_value, value);
19355         }
19356
19357       add_gnat_descriptive_type_attribute (type_die, type, context_die);
19358       if (TYPE_ARTIFICIAL (type))
19359         add_AT_flag (type_die, DW_AT_artificial, 1);
19360     }
19361   else
19362     add_AT_flag (type_die, DW_AT_declaration, 1);
19363
19364   add_pubtype (type, type_die);
19365
19366   return type_die;
19367 }
19368
19369 /* Generate a DIE to represent either a real live formal parameter decl or to
19370    represent just the type of some formal parameter position in some function
19371    type.
19372
19373    Note that this routine is a bit unusual because its argument may be a
19374    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
19375    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
19376    node.  If it's the former then this function is being called to output a
19377    DIE to represent a formal parameter object (or some inlining thereof).  If
19378    it's the latter, then this function is only being called to output a
19379    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
19380    argument type of some subprogram type.
19381    If EMIT_NAME_P is true, name and source coordinate attributes
19382    are emitted.  */
19383
19384 static dw_die_ref
19385 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
19386                           dw_die_ref context_die)
19387 {
19388   tree node_or_origin = node ? node : origin;
19389   tree ultimate_origin;
19390   dw_die_ref parm_die = NULL;
19391   
19392   if (TREE_CODE_CLASS (TREE_CODE (node_or_origin)) == tcc_declaration)
19393     {
19394       parm_die = lookup_decl_die (node);
19395
19396       /* If the contexts differ, we may not be talking about the same
19397          thing.  */
19398       if (parm_die && parm_die->die_parent != context_die)
19399         {
19400           if (!DECL_ABSTRACT_P (node))
19401             {
19402               /* This can happen when creating an inlined instance, in
19403                  which case we need to create a new DIE that will get
19404                  annotated with DW_AT_abstract_origin.  */
19405               parm_die = NULL;
19406             }
19407           else
19408             {
19409               /* FIXME: Reuse DIE even with a differing context.
19410
19411                  This can happen when calling
19412                  dwarf2out_abstract_function to build debug info for
19413                  the abstract instance of a function for which we have
19414                  already generated a DIE in
19415                  dwarf2out_early_global_decl.
19416
19417                  Once we remove dwarf2out_abstract_function, we should
19418                  have a call to gcc_unreachable here.  */
19419             }
19420         }
19421
19422       if (parm_die && parm_die->die_parent == NULL)
19423         {
19424           /* Check that parm_die already has the right attributes that
19425              we would have added below.  If any attributes are
19426              missing, fall through to add them.  */
19427           if (! DECL_ABSTRACT_P (node_or_origin)
19428               && !get_AT (parm_die, DW_AT_location)
19429               && !get_AT (parm_die, DW_AT_const_value))
19430             /* We are missing  location info, and are about to add it.  */
19431             ;
19432           else
19433             {
19434               add_child_die (context_die, parm_die);
19435               return parm_die;
19436             }
19437         }
19438     }
19439
19440   /* If we have a previously generated DIE, use it, unless this is an
19441      concrete instance (origin != NULL), in which case we need a new
19442      DIE with a corresponding DW_AT_abstract_origin.  */
19443   bool reusing_die;
19444   if (parm_die && origin == NULL)
19445     reusing_die = true;
19446   else
19447     {
19448       parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
19449       reusing_die = false;
19450     }
19451
19452   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
19453     {
19454     case tcc_declaration:
19455       ultimate_origin = decl_ultimate_origin (node_or_origin);
19456       if (node || ultimate_origin)
19457         origin = ultimate_origin;
19458
19459       if (reusing_die)
19460         goto add_location;
19461
19462       if (origin != NULL)
19463         add_abstract_origin_attribute (parm_die, origin);
19464       else if (emit_name_p)
19465         add_name_and_src_coords_attributes (parm_die, node);
19466       if (origin == NULL
19467           || (! DECL_ABSTRACT_P (node_or_origin)
19468               && variably_modified_type_p (TREE_TYPE (node_or_origin),
19469                                            decl_function_context
19470                                                             (node_or_origin))))
19471         {
19472           tree type = TREE_TYPE (node_or_origin);
19473           if (decl_by_reference_p (node_or_origin))
19474             add_type_attribute (parm_die, TREE_TYPE (type),
19475                                 TYPE_UNQUALIFIED, context_die);
19476           else
19477             add_type_attribute (parm_die, type,
19478                                 decl_quals (node_or_origin),
19479                                 context_die);
19480         }
19481       if (origin == NULL && DECL_ARTIFICIAL (node))
19482         add_AT_flag (parm_die, DW_AT_artificial, 1);
19483     add_location:
19484       if (node && node != origin)
19485         equate_decl_number_to_die (node, parm_die);
19486       if (! DECL_ABSTRACT_P (node_or_origin))
19487         add_location_or_const_value_attribute (parm_die, node_or_origin,
19488                                                node == NULL);
19489
19490       break;
19491
19492     case tcc_type:
19493       /* We were called with some kind of a ..._TYPE node.  */
19494       add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED,
19495                           context_die);
19496       break;
19497
19498     default:
19499       gcc_unreachable ();
19500     }
19501
19502   return parm_die;
19503 }
19504
19505 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
19506    children DW_TAG_formal_parameter DIEs representing the arguments of the
19507    parameter pack.
19508
19509    PARM_PACK must be a function parameter pack.
19510    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
19511    must point to the subsequent arguments of the function PACK_ARG belongs to.
19512    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
19513    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
19514    following the last one for which a DIE was generated.  */
19515
19516 static dw_die_ref
19517 gen_formal_parameter_pack_die  (tree parm_pack,
19518                                 tree pack_arg,
19519                                 dw_die_ref subr_die,
19520                                 tree *next_arg)
19521 {
19522   tree arg;
19523   dw_die_ref parm_pack_die;
19524
19525   gcc_assert (parm_pack
19526               && lang_hooks.function_parameter_pack_p (parm_pack)
19527               && subr_die);
19528
19529   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
19530   add_src_coords_attributes (parm_pack_die, parm_pack);
19531
19532   for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
19533     {
19534       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
19535                                                                  parm_pack))
19536         break;
19537       gen_formal_parameter_die (arg, NULL,
19538                                 false /* Don't emit name attribute.  */,
19539                                 parm_pack_die);
19540     }
19541   if (next_arg)
19542     *next_arg = arg;
19543   return parm_pack_die;
19544 }
19545
19546 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
19547    at the end of an (ANSI prototyped) formal parameters list.  */
19548
19549 static void
19550 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
19551 {
19552   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
19553 }
19554
19555 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
19556    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
19557    parameters as specified in some function type specification (except for
19558    those which appear as part of a function *definition*).  */
19559
19560 static void
19561 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
19562 {
19563   tree link;
19564   tree formal_type = NULL;
19565   tree first_parm_type;
19566   tree arg;
19567
19568   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
19569     {
19570       arg = DECL_ARGUMENTS (function_or_method_type);
19571       function_or_method_type = TREE_TYPE (function_or_method_type);
19572     }
19573   else
19574     arg = NULL_TREE;
19575
19576   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
19577
19578   /* Make our first pass over the list of formal parameter types and output a
19579      DW_TAG_formal_parameter DIE for each one.  */
19580   for (link = first_parm_type; link; )
19581     {
19582       dw_die_ref parm_die;
19583
19584       formal_type = TREE_VALUE (link);
19585       if (formal_type == void_type_node)
19586         break;
19587
19588       /* Output a (nameless) DIE to represent the formal parameter itself.  */
19589       if (!POINTER_BOUNDS_TYPE_P (formal_type))
19590         {
19591           parm_die = gen_formal_parameter_die (formal_type, NULL,
19592                                                true /* Emit name attribute.  */,
19593                                                context_die);
19594           if (TREE_CODE (function_or_method_type) == METHOD_TYPE
19595               && link == first_parm_type)
19596             {
19597               add_AT_flag (parm_die, DW_AT_artificial, 1);
19598               if (dwarf_version >= 3 || !dwarf_strict)
19599                 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
19600             }
19601           else if (arg && DECL_ARTIFICIAL (arg))
19602             add_AT_flag (parm_die, DW_AT_artificial, 1);
19603         }
19604
19605       link = TREE_CHAIN (link);
19606       if (arg)
19607         arg = DECL_CHAIN (arg);
19608     }
19609
19610   /* If this function type has an ellipsis, add a
19611      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
19612   if (formal_type != void_type_node)
19613     gen_unspecified_parameters_die (function_or_method_type, context_die);
19614
19615   /* Make our second (and final) pass over the list of formal parameter types
19616      and output DIEs to represent those types (as necessary).  */
19617   for (link = TYPE_ARG_TYPES (function_or_method_type);
19618        link && TREE_VALUE (link);
19619        link = TREE_CHAIN (link))
19620     gen_type_die (TREE_VALUE (link), context_die);
19621 }
19622
19623 /* We want to generate the DIE for TYPE so that we can generate the
19624    die for MEMBER, which has been defined; we will need to refer back
19625    to the member declaration nested within TYPE.  If we're trying to
19626    generate minimal debug info for TYPE, processing TYPE won't do the
19627    trick; we need to attach the member declaration by hand.  */
19628
19629 static void
19630 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
19631 {
19632   gen_type_die (type, context_die);
19633
19634   /* If we're trying to avoid duplicate debug info, we may not have
19635      emitted the member decl for this function.  Emit it now.  */
19636   if (TYPE_STUB_DECL (type)
19637       && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
19638       && ! lookup_decl_die (member))
19639     {
19640       dw_die_ref type_die;
19641       gcc_assert (!decl_ultimate_origin (member));
19642
19643       push_decl_scope (type);
19644       type_die = lookup_type_die_strip_naming_typedef (type);
19645       if (TREE_CODE (member) == FUNCTION_DECL)
19646         gen_subprogram_die (member, type_die);
19647       else if (TREE_CODE (member) == FIELD_DECL)
19648         {
19649           /* Ignore the nameless fields that are used to skip bits but handle
19650              C++ anonymous unions and structs.  */
19651           if (DECL_NAME (member) != NULL_TREE
19652               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
19653               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
19654             {
19655               struct vlr_context vlr_ctx = {
19656                 DECL_CONTEXT (member), /* struct_type */
19657                 NULL_TREE /* variant_part_offset */
19658               };
19659               gen_type_die (member_declared_type (member), type_die);
19660               gen_field_die (member, &vlr_ctx, type_die);
19661             }
19662         }
19663       else
19664         gen_variable_die (member, NULL_TREE, type_die);
19665
19666       pop_decl_scope ();
19667     }
19668 }
19669 \f
19670 /* Forward declare these functions, because they are mutually recursive
19671   with their set_block_* pairing functions.  */
19672 static void set_decl_origin_self (tree);
19673 static void set_decl_abstract_flags (tree, vec<tree> &);
19674
19675 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
19676    given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
19677    that it points to the node itself, thus indicating that the node is its
19678    own (abstract) origin.  Additionally, if the BLOCK_ABSTRACT_ORIGIN for
19679    the given node is NULL, recursively descend the decl/block tree which
19680    it is the root of, and for each other ..._DECL or BLOCK node contained
19681    therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
19682    still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
19683    values to point to themselves.  */
19684
19685 static void
19686 set_block_origin_self (tree stmt)
19687 {
19688   if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
19689     {
19690       BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
19691
19692       {
19693         tree local_decl;
19694
19695         for (local_decl = BLOCK_VARS (stmt);
19696              local_decl != NULL_TREE;
19697              local_decl = DECL_CHAIN (local_decl))
19698           /* Do not recurse on nested functions since the inlining status
19699              of parent and child can be different as per the DWARF spec.  */
19700           if (TREE_CODE (local_decl) != FUNCTION_DECL
19701               && !DECL_EXTERNAL (local_decl))
19702             set_decl_origin_self (local_decl);
19703       }
19704
19705       {
19706         tree subblock;
19707
19708         for (subblock = BLOCK_SUBBLOCKS (stmt);
19709              subblock != NULL_TREE;
19710              subblock = BLOCK_CHAIN (subblock))
19711           set_block_origin_self (subblock);     /* Recurse.  */
19712       }
19713     }
19714 }
19715
19716 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
19717    the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
19718    node to so that it points to the node itself, thus indicating that the
19719    node represents its own (abstract) origin.  Additionally, if the
19720    DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
19721    the decl/block tree of which the given node is the root of, and for
19722    each other ..._DECL or BLOCK node contained therein whose
19723    DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
19724    set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
19725    point to themselves.  */
19726
19727 static void
19728 set_decl_origin_self (tree decl)
19729 {
19730   if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
19731     {
19732       DECL_ABSTRACT_ORIGIN (decl) = decl;
19733       if (TREE_CODE (decl) == FUNCTION_DECL)
19734         {
19735           tree arg;
19736
19737           for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
19738             DECL_ABSTRACT_ORIGIN (arg) = arg;
19739           if (DECL_INITIAL (decl) != NULL_TREE
19740               && DECL_INITIAL (decl) != error_mark_node)
19741             set_block_origin_self (DECL_INITIAL (decl));
19742         }
19743     }
19744 }
19745 \f
19746 /* Given a pointer to some BLOCK node, set the BLOCK_ABSTRACT flag to 1
19747    and if it wasn't 1 before, push it to abstract_vec vector.
19748    For all local decls and all local sub-blocks (recursively) do it
19749    too.  */
19750
19751 static void
19752 set_block_abstract_flags (tree stmt, vec<tree> &abstract_vec)
19753 {
19754   tree local_decl;
19755   tree subblock;
19756   unsigned int i;
19757
19758   if (!BLOCK_ABSTRACT (stmt))
19759     {
19760       abstract_vec.safe_push (stmt);
19761       BLOCK_ABSTRACT (stmt) = 1;
19762     }
19763
19764   for (local_decl = BLOCK_VARS (stmt);
19765        local_decl != NULL_TREE;
19766        local_decl = DECL_CHAIN (local_decl))
19767     if (! DECL_EXTERNAL (local_decl))
19768       set_decl_abstract_flags (local_decl, abstract_vec);
19769
19770   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
19771     {
19772       local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
19773       if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
19774           || TREE_CODE (local_decl) == PARM_DECL)
19775         set_decl_abstract_flags (local_decl, abstract_vec);
19776     }
19777
19778   for (subblock = BLOCK_SUBBLOCKS (stmt);
19779        subblock != NULL_TREE;
19780        subblock = BLOCK_CHAIN (subblock))
19781     set_block_abstract_flags (subblock, abstract_vec);
19782 }
19783
19784 /* Given a pointer to some ..._DECL node, set DECL_ABSTRACT_P flag on it
19785    to 1 and if it wasn't 1 before, push to abstract_vec vector.
19786    In the case where the decl is a FUNCTION_DECL also set the abstract
19787    flags for all of the parameters, local vars, local
19788    blocks and sub-blocks (recursively).  */
19789
19790 static void
19791 set_decl_abstract_flags (tree decl, vec<tree> &abstract_vec)
19792 {
19793   if (!DECL_ABSTRACT_P (decl))
19794     {
19795       abstract_vec.safe_push (decl);
19796       DECL_ABSTRACT_P (decl) = 1;
19797     }
19798
19799   if (TREE_CODE (decl) == FUNCTION_DECL)
19800     {
19801       tree arg;
19802
19803       for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
19804         if (!DECL_ABSTRACT_P (arg))
19805           {
19806             abstract_vec.safe_push (arg);
19807             DECL_ABSTRACT_P (arg) = 1;
19808           }
19809       if (DECL_INITIAL (decl) != NULL_TREE
19810           && DECL_INITIAL (decl) != error_mark_node)
19811         set_block_abstract_flags (DECL_INITIAL (decl), abstract_vec);
19812     }
19813 }
19814
19815 /* Generate the DWARF2 info for the "abstract" instance of a function which we
19816    may later generate inlined and/or out-of-line instances of.
19817
19818    FIXME: In the early-dwarf world, this function, and most of the
19819           DECL_ABSTRACT code should be obsoleted.  The early DIE _is_
19820           the abstract instance.  All we would need to do is annotate
19821           the early DIE with the appropriate DW_AT_inline in late
19822           dwarf (perhaps in gen_inlined_subroutine_die).
19823
19824           However, we can't do this yet, because LTO streaming of DIEs
19825           has not been implemented yet.  */
19826
19827 static void
19828 dwarf2out_abstract_function (tree decl)
19829 {
19830   dw_die_ref old_die;
19831   tree save_fn;
19832   tree context;
19833   hash_table<decl_loc_hasher> *old_decl_loc_table;
19834   hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
19835   int old_call_site_count, old_tail_call_site_count;
19836   struct call_arg_loc_node *old_call_arg_locations;
19837
19838   /* Make sure we have the actual abstract inline, not a clone.  */
19839   decl = DECL_ORIGIN (decl);
19840
19841   old_die = lookup_decl_die (decl);
19842   if (old_die && get_AT (old_die, DW_AT_inline))
19843     /* We've already generated the abstract instance.  */
19844     return;
19845
19846   /* We can be called while recursively when seeing block defining inlined subroutine
19847      DIE.  Be sure to not clobber the outer location table nor use it or we would
19848      get locations in abstract instantces.  */
19849   old_decl_loc_table = decl_loc_table;
19850   decl_loc_table = NULL;
19851   old_cached_dw_loc_list_table = cached_dw_loc_list_table;
19852   cached_dw_loc_list_table = NULL;
19853   old_call_arg_locations = call_arg_locations;
19854   call_arg_locations = NULL;
19855   old_call_site_count = call_site_count;
19856   call_site_count = -1;
19857   old_tail_call_site_count = tail_call_site_count;
19858   tail_call_site_count = -1;
19859
19860   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
19861      we don't get confused by DECL_ABSTRACT_P.  */
19862   if (debug_info_level > DINFO_LEVEL_TERSE)
19863     {
19864       context = decl_class_context (decl);
19865       if (context)
19866         gen_type_die_for_member
19867           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
19868     }
19869
19870   /* Pretend we've just finished compiling this function.  */
19871   save_fn = current_function_decl;
19872   current_function_decl = decl;
19873
19874   auto_vec<tree, 64> abstract_vec;
19875   set_decl_abstract_flags (decl, abstract_vec);
19876   dwarf2out_decl (decl);
19877   unsigned int i;
19878   tree t;
19879   FOR_EACH_VEC_ELT (abstract_vec, i, t)
19880     if (TREE_CODE (t) == BLOCK)
19881       BLOCK_ABSTRACT (t) = 0;
19882     else
19883       DECL_ABSTRACT_P (t) = 0;
19884
19885   current_function_decl = save_fn;
19886   decl_loc_table = old_decl_loc_table;
19887   cached_dw_loc_list_table = old_cached_dw_loc_list_table;
19888   call_arg_locations = old_call_arg_locations;
19889   call_site_count = old_call_site_count;
19890   tail_call_site_count = old_tail_call_site_count;
19891 }
19892
19893 /* Helper function of premark_used_types() which gets called through
19894    htab_traverse.
19895
19896    Marks the DIE of a given type in *SLOT as perennial, so it never gets
19897    marked as unused by prune_unused_types.  */
19898
19899 bool
19900 premark_used_types_helper (tree const &type, void *)
19901 {
19902   dw_die_ref die;
19903
19904   die = lookup_type_die (type);
19905   if (die != NULL)
19906     die->die_perennial_p = 1;
19907   return true;
19908 }
19909
19910 /* Helper function of premark_types_used_by_global_vars which gets called
19911    through htab_traverse.
19912
19913    Marks the DIE of a given type in *SLOT as perennial, so it never gets
19914    marked as unused by prune_unused_types. The DIE of the type is marked
19915    only if the global variable using the type will actually be emitted.  */
19916
19917 int
19918 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
19919                                           void *)
19920 {
19921   struct types_used_by_vars_entry *entry;
19922   dw_die_ref die;
19923
19924   entry = (struct types_used_by_vars_entry *) *slot;
19925   gcc_assert (entry->type != NULL
19926               && entry->var_decl != NULL);
19927   die = lookup_type_die (entry->type);
19928   if (die)
19929     {
19930       /* Ask cgraph if the global variable really is to be emitted.
19931          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
19932       varpool_node *node = varpool_node::get (entry->var_decl);
19933       if (node && node->definition)
19934         {
19935           die->die_perennial_p = 1;
19936           /* Keep the parent DIEs as well.  */
19937           while ((die = die->die_parent) && die->die_perennial_p == 0)
19938             die->die_perennial_p = 1;
19939         }
19940     }
19941   return 1;
19942 }
19943
19944 /* Mark all members of used_types_hash as perennial.  */
19945
19946 static void
19947 premark_used_types (struct function *fun)
19948 {
19949   if (fun && fun->used_types_hash)
19950     fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
19951 }
19952
19953 /* Mark all members of types_used_by_vars_entry as perennial.  */
19954
19955 static void
19956 premark_types_used_by_global_vars (void)
19957 {
19958   if (types_used_by_vars_hash)
19959     types_used_by_vars_hash
19960       ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
19961 }
19962
19963 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
19964    for CA_LOC call arg loc node.  */
19965
19966 static dw_die_ref
19967 gen_call_site_die (tree decl, dw_die_ref subr_die,
19968                    struct call_arg_loc_node *ca_loc)
19969 {
19970   dw_die_ref stmt_die = NULL, die;
19971   tree block = ca_loc->block;
19972
19973   while (block
19974          && block != DECL_INITIAL (decl)
19975          && TREE_CODE (block) == BLOCK)
19976     {
19977       stmt_die = BLOCK_DIE (block);
19978       if (stmt_die)
19979         break;
19980       block = BLOCK_SUPERCONTEXT (block);
19981     }
19982   if (stmt_die == NULL)
19983     stmt_die = subr_die;
19984   die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
19985   add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
19986   if (ca_loc->tail_call_p)
19987     add_AT_flag (die, DW_AT_GNU_tail_call, 1);
19988   if (ca_loc->symbol_ref)
19989     {
19990       dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
19991       if (tdie)
19992         add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
19993       else
19994         add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
19995     }
19996   return die;
19997 }
19998
19999 /* Generate a DIE to represent a declared function (either file-scope or
20000    block-local).  */
20001
20002 static void
20003 gen_subprogram_die (tree decl, dw_die_ref context_die)
20004 {
20005   tree origin = decl_ultimate_origin (decl);
20006   dw_die_ref subr_die;
20007   dw_die_ref old_die = lookup_decl_die (decl);
20008
20009   /* This function gets called multiple times for different stages of
20010      the debug process.  For example, for func() in this code:
20011
20012         namespace S
20013         {
20014           void func() { ... }
20015         }
20016
20017      ...we get called 4 times.  Twice in early debug and twice in
20018      late debug:
20019
20020      Early debug
20021      -----------
20022
20023        1. Once while generating func() within the namespace.  This is
20024           the declaration.  The declaration bit below is set, as the
20025           context is the namespace.
20026
20027           A new DIE will be generated with DW_AT_declaration set.
20028
20029        2. Once for func() itself.  This is the specification.  The
20030           declaration bit below is clear as the context is the CU.
20031
20032           We will use the cached DIE from (1) to create a new DIE with
20033           DW_AT_specification pointing to the declaration in (1).
20034
20035      Late debug via rest_of_handle_final()
20036      -------------------------------------
20037
20038        3. Once generating func() within the namespace.  This is also the
20039           declaration, as in (1), but this time we will early exit below
20040           as we have a cached DIE and a declaration needs no additional
20041           annotations (no locations), as the source declaration line
20042           info is enough.
20043
20044        4. Once for func() itself.  As in (2), this is the specification,
20045           but this time we will re-use the cached DIE, and just annotate
20046           it with the location information that should now be available.
20047
20048      For something without namespaces, but with abstract instances, we
20049      are also called a multiple times:
20050
20051         class Base
20052         {
20053         public:
20054           Base ();        // constructor declaration (1)
20055         };
20056
20057         Base::Base () { } // constructor specification (2)
20058
20059     Early debug
20060     -----------
20061
20062        1. Once for the Base() constructor by virtue of it being a
20063           member of the Base class.  This is done via
20064           rest_of_type_compilation.
20065
20066           This is a declaration, so a new DIE will be created with
20067           DW_AT_declaration.
20068
20069        2. Once for the Base() constructor definition, but this time
20070           while generating the abstract instance of the base
20071           constructor (__base_ctor) which is being generated via early
20072           debug of reachable functions.
20073
20074           Even though we have a cached version of the declaration (1),
20075           we will create a DW_AT_specification of the declaration DIE
20076           in (1).
20077
20078        3. Once for the __base_ctor itself, but this time, we generate
20079           an DW_AT_abstract_origin version of the DW_AT_specification in
20080           (2).
20081
20082     Late debug via rest_of_handle_final
20083     -----------------------------------
20084
20085        4. One final time for the __base_ctor (which will have a cached
20086           DIE with DW_AT_abstract_origin created in (3).  This time,
20087           we will just annotate the location information now
20088           available.
20089   */
20090   int declaration = (current_function_decl != decl
20091                      || class_or_namespace_scope_p (context_die));
20092
20093   premark_used_types (DECL_STRUCT_FUNCTION (decl));
20094
20095   /* Now that the C++ front end lazily declares artificial member fns, we
20096      might need to retrofit the declaration into its class.  */
20097   if (!declaration && !origin && !old_die
20098       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
20099       && !class_or_namespace_scope_p (context_die)
20100       && debug_info_level > DINFO_LEVEL_TERSE)
20101     old_die = force_decl_die (decl);
20102
20103   /* An inlined instance, tag a new DIE with DW_AT_abstract_origin.  */
20104   if (origin != NULL)
20105     {
20106       gcc_assert (!declaration || local_scope_p (context_die));
20107
20108       /* Fixup die_parent for the abstract instance of a nested
20109          inline function.  */
20110       if (old_die && old_die->die_parent == NULL)
20111         add_child_die (context_die, old_die);
20112
20113       if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
20114         {
20115           /* If we have a DW_AT_abstract_origin we have a working
20116              cached version.  */
20117           subr_die = old_die;
20118         }
20119       else
20120         {
20121           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
20122           add_abstract_origin_attribute (subr_die, origin);
20123           /*  This is where the actual code for a cloned function is.
20124               Let's emit linkage name attribute for it.  This helps
20125               debuggers to e.g, set breakpoints into
20126               constructors/destructors when the user asks "break
20127               K::K".  */
20128           add_linkage_name (subr_die, decl);
20129         }
20130     }
20131   /* A cached copy, possibly from early dwarf generation.  Reuse as
20132      much as possible.  */
20133   else if (old_die)
20134     {
20135       /* A declaration that has been previously dumped needs no
20136          additional information.  */
20137       if (declaration)
20138         return;
20139
20140       if (!get_AT_flag (old_die, DW_AT_declaration)
20141           /* We can have a normal definition following an inline one in the
20142              case of redefinition of GNU C extern inlines.
20143              It seems reasonable to use AT_specification in this case.  */
20144           && !get_AT (old_die, DW_AT_inline))
20145         {
20146           /* Detect and ignore this case, where we are trying to output
20147              something we have already output.  */
20148           if (get_AT (old_die, DW_AT_low_pc)
20149               || get_AT (old_die, DW_AT_ranges))
20150             return;
20151
20152           /* If we have no location information, this must be a
20153              partially generated DIE from early dwarf generation.
20154              Fall through and generate it.  */
20155         }
20156
20157       /* If the definition comes from the same place as the declaration,
20158          maybe use the old DIE.  We always want the DIE for this function
20159          that has the *_pc attributes to be under comp_unit_die so the
20160          debugger can find it.  We also need to do this for abstract
20161          instances of inlines, since the spec requires the out-of-line copy
20162          to have the same parent.  For local class methods, this doesn't
20163          apply; we just use the old DIE.  */
20164       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
20165       struct dwarf_file_data * file_index = lookup_filename (s.file);
20166       if ((is_cu_die (old_die->die_parent)
20167            /* This condition fixes the inconsistency/ICE with the
20168               following Fortran test (or some derivative thereof) while
20169               building libgfortran:
20170
20171                  module some_m
20172                  contains
20173                     logical function funky (FLAG)
20174                       funky = .true.
20175                    end function
20176                  end module
20177            */
20178            || (old_die->die_parent
20179                && old_die->die_parent->die_tag == DW_TAG_module)
20180            || context_die == NULL)
20181            && (DECL_ARTIFICIAL (decl)
20182                || (get_AT_file (old_die, DW_AT_decl_file) == file_index
20183                    && (get_AT_unsigned (old_die, DW_AT_decl_line)
20184                        == (unsigned) s.line))))
20185         {
20186           subr_die = old_die;
20187
20188           /* Clear out the declaration attribute, but leave the
20189              parameters so they can be augmented with location
20190              information later.  Unless this was a declaration, in
20191              which case, wipe out the nameless parameters and recreate
20192              them further down.  */
20193           if (remove_AT (subr_die, DW_AT_declaration))
20194             {
20195
20196               remove_AT (subr_die, DW_AT_object_pointer);
20197               remove_child_TAG (subr_die, DW_TAG_formal_parameter);
20198             }
20199         }
20200       /* Make a specification pointing to the previously built
20201          declaration.  */
20202       else
20203         {
20204           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
20205           add_AT_specification (subr_die, old_die);
20206           add_pubname (decl, subr_die);
20207           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
20208             add_AT_file (subr_die, DW_AT_decl_file, file_index);
20209           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
20210             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
20211
20212           /* If the prototype had an 'auto' or 'decltype(auto)' return type,
20213              emit the real type on the definition die.  */
20214           if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
20215             {
20216               dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
20217               if (die == auto_die || die == decltype_auto_die)
20218                 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
20219                                     TYPE_UNQUALIFIED, context_die);
20220             }
20221         }
20222     }
20223   /* Create a fresh DIE for anything else.  */
20224   else
20225     {
20226       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
20227
20228       if (TREE_PUBLIC (decl))
20229         add_AT_flag (subr_die, DW_AT_external, 1);
20230
20231       add_name_and_src_coords_attributes (subr_die, decl);
20232       add_pubname (decl, subr_die);
20233       if (debug_info_level > DINFO_LEVEL_TERSE)
20234         {
20235           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
20236           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
20237                               TYPE_UNQUALIFIED, context_die);
20238         }
20239
20240       add_pure_or_virtual_attribute (subr_die, decl);
20241       if (DECL_ARTIFICIAL (decl))
20242         add_AT_flag (subr_die, DW_AT_artificial, 1);
20243
20244       if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
20245         add_AT_flag (subr_die, DW_AT_noreturn, 1);
20246
20247       add_accessibility_attribute (subr_die, decl);
20248     }
20249
20250   /* Unless we have an existing non-declaration DIE, equate the new
20251      DIE.  */
20252   if (!old_die || is_declaration_die (old_die))
20253     equate_decl_number_to_die (decl, subr_die);
20254
20255   if (declaration)
20256     {
20257       if (!old_die || !get_AT (old_die, DW_AT_inline))
20258         {
20259           add_AT_flag (subr_die, DW_AT_declaration, 1);
20260
20261           /* If this is an explicit function declaration then generate
20262              a DW_AT_explicit attribute.  */
20263           if (lang_hooks.decls.function_decl_explicit_p (decl)
20264               && (dwarf_version >= 3 || !dwarf_strict))
20265             add_AT_flag (subr_die, DW_AT_explicit, 1);
20266
20267           /* If this is a C++11 deleted special function member then generate
20268              a DW_AT_GNU_deleted attribute.  */
20269           if (lang_hooks.decls.function_decl_deleted_p (decl)
20270               && (! dwarf_strict))
20271             add_AT_flag (subr_die, DW_AT_GNU_deleted, 1);
20272         }
20273     }
20274   /* Tag abstract instances with DW_AT_inline.  */
20275   else if (DECL_ABSTRACT_P (decl))
20276     {
20277       if (DECL_DECLARED_INLINE_P (decl))
20278         {
20279           if (cgraph_function_possibly_inlined_p (decl))
20280             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
20281           else
20282             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
20283         }
20284       else
20285         {
20286           if (cgraph_function_possibly_inlined_p (decl))
20287             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
20288           else
20289             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
20290         }
20291
20292       if (DECL_DECLARED_INLINE_P (decl)
20293           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
20294         add_AT_flag (subr_die, DW_AT_artificial, 1);
20295     }
20296   /* For non DECL_EXTERNALs, if range information is available, fill
20297      the DIE with it.  */
20298   else if (!DECL_EXTERNAL (decl) && !early_dwarf)
20299     {
20300       HOST_WIDE_INT cfa_fb_offset;
20301
20302       struct function *fun = DECL_STRUCT_FUNCTION (decl);
20303
20304       if (!flag_reorder_blocks_and_partition)
20305         {
20306           dw_fde_ref fde = fun->fde;
20307           if (fde->dw_fde_begin)
20308             {
20309               /* We have already generated the labels.  */
20310              add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
20311                                  fde->dw_fde_end, false);
20312             }
20313           else
20314             {
20315               /* Create start/end labels and add the range.  */
20316               char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
20317               char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
20318               ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
20319                                            current_function_funcdef_no);
20320               ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
20321                                            current_function_funcdef_no);
20322              add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
20323                                  false);
20324             }
20325
20326 #if VMS_DEBUGGING_INFO
20327       /* HP OpenVMS Industry Standard 64: DWARF Extensions
20328          Section 2.3 Prologue and Epilogue Attributes:
20329          When a breakpoint is set on entry to a function, it is generally
20330          desirable for execution to be suspended, not on the very first
20331          instruction of the function, but rather at a point after the
20332          function's frame has been set up, after any language defined local
20333          declaration processing has been completed, and before execution of
20334          the first statement of the function begins. Debuggers generally
20335          cannot properly determine where this point is.  Similarly for a
20336          breakpoint set on exit from a function. The prologue and epilogue
20337          attributes allow a compiler to communicate the location(s) to use.  */
20338
20339       {
20340         if (fde->dw_fde_vms_end_prologue)
20341           add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
20342             fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
20343
20344         if (fde->dw_fde_vms_begin_epilogue)
20345           add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
20346             fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
20347       }
20348 #endif
20349
20350         }
20351       else
20352         {
20353           /* Generate pubnames entries for the split function code ranges.  */
20354           dw_fde_ref fde = fun->fde;
20355
20356           if (fde->dw_fde_second_begin)
20357             {
20358               if (dwarf_version >= 3 || !dwarf_strict)
20359                 {
20360                   /* We should use ranges for non-contiguous code section 
20361                      addresses.  Use the actual code range for the initial
20362                      section, since the HOT/COLD labels might precede an 
20363                      alignment offset.  */
20364                   bool range_list_added = false;
20365                   add_ranges_by_labels (subr_die, fde->dw_fde_begin,
20366                                         fde->dw_fde_end, &range_list_added,
20367                                         false);
20368                   add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
20369                                         fde->dw_fde_second_end,
20370                                        &range_list_added, false);
20371                   if (range_list_added)
20372                     add_ranges (NULL);
20373                 }
20374               else
20375                 {
20376                   /* There is no real support in DW2 for this .. so we make
20377                      a work-around.  First, emit the pub name for the segment
20378                      containing the function label.  Then make and emit a
20379                      simplified subprogram DIE for the second segment with the
20380                      name pre-fixed by __hot/cold_sect_of_.  We use the same
20381                      linkage name for the second die so that gdb will find both
20382                      sections when given "b foo".  */
20383                   const char *name = NULL;
20384                   tree decl_name = DECL_NAME (decl);
20385                   dw_die_ref seg_die;
20386
20387                   /* Do the 'primary' section.   */
20388                   add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
20389                                       fde->dw_fde_end, false);
20390
20391                   /* Build a minimal DIE for the secondary section.  */
20392                   seg_die = new_die (DW_TAG_subprogram,
20393                                      subr_die->die_parent, decl);
20394
20395                   if (TREE_PUBLIC (decl))
20396                     add_AT_flag (seg_die, DW_AT_external, 1);
20397
20398                   if (decl_name != NULL 
20399                       && IDENTIFIER_POINTER (decl_name) != NULL)
20400                     {
20401                       name = dwarf2_name (decl, 1);
20402                       if (! DECL_ARTIFICIAL (decl))
20403                         add_src_coords_attributes (seg_die, decl);
20404
20405                       add_linkage_name (seg_die, decl);
20406                     }
20407                   gcc_assert (name != NULL);
20408                   add_pure_or_virtual_attribute (seg_die, decl);
20409                   if (DECL_ARTIFICIAL (decl))
20410                     add_AT_flag (seg_die, DW_AT_artificial, 1);
20411
20412                   name = concat ("__second_sect_of_", name, NULL); 
20413                   add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
20414                                       fde->dw_fde_second_end, false);
20415                   add_name_attribute (seg_die, name);
20416                   if (want_pubnames ())
20417                     add_pubname_string (name, seg_die);
20418                 }
20419             }
20420           else
20421            add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
20422                                false);
20423         }
20424
20425       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
20426
20427       /* We define the "frame base" as the function's CFA.  This is more
20428          convenient for several reasons: (1) It's stable across the prologue
20429          and epilogue, which makes it better than just a frame pointer,
20430          (2) With dwarf3, there exists a one-byte encoding that allows us
20431          to reference the .debug_frame data by proxy, but failing that,
20432          (3) We can at least reuse the code inspection and interpretation
20433          code that determines the CFA position at various points in the
20434          function.  */
20435       if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
20436         {
20437           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
20438           add_AT_loc (subr_die, DW_AT_frame_base, op);
20439         }
20440       else
20441         {
20442           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
20443           if (list->dw_loc_next)
20444             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
20445           else
20446             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
20447         }
20448
20449       /* Compute a displacement from the "steady-state frame pointer" to
20450          the CFA.  The former is what all stack slots and argument slots
20451          will reference in the rtl; the latter is what we've told the
20452          debugger about.  We'll need to adjust all frame_base references
20453          by this displacement.  */
20454       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
20455
20456       if (fun->static_chain_decl)
20457         {
20458           /* DWARF requires here a location expression that computes the
20459              address of the enclosing subprogram's frame base.  The machinery
20460              in tree-nested.c is supposed to store this specific address in the
20461              last field of the FRAME record.  */
20462           const tree frame_type
20463             = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
20464           const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
20465
20466           tree fb_expr
20467             = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
20468           fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
20469                             fb_expr, fb_decl, NULL_TREE);
20470
20471           add_AT_location_description (subr_die, DW_AT_static_link,
20472                                        loc_list_from_tree (fb_expr, 0, NULL));
20473         }
20474     }
20475
20476   /* Generate child dies for template paramaters.  */
20477   if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
20478     gen_generic_params_dies (decl);
20479
20480   /* Now output descriptions of the arguments for this function. This gets
20481      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
20482      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
20483      `...' at the end of the formal parameter list.  In order to find out if
20484      there was a trailing ellipsis or not, we must instead look at the type
20485      associated with the FUNCTION_DECL.  This will be a node of type
20486      FUNCTION_TYPE. If the chain of type nodes hanging off of this
20487      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
20488      an ellipsis at the end.  */
20489
20490   /* In the case where we are describing a mere function declaration, all we
20491      need to do here (and all we *can* do here) is to describe the *types* of
20492      its formal parameters.  */
20493   if (debug_info_level <= DINFO_LEVEL_TERSE)
20494     ;
20495   else if (declaration)
20496     gen_formal_types_die (decl, subr_die);
20497   else
20498     {
20499       /* Generate DIEs to represent all known formal parameters.  */
20500       tree parm = DECL_ARGUMENTS (decl);
20501       tree generic_decl = early_dwarf
20502         ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
20503       tree generic_decl_parm = generic_decl
20504                                 ? DECL_ARGUMENTS (generic_decl)
20505                                 : NULL;
20506
20507       /* Now we want to walk the list of parameters of the function and
20508          emit their relevant DIEs.
20509
20510          We consider the case of DECL being an instance of a generic function
20511          as well as it being a normal function.
20512
20513          If DECL is an instance of a generic function we walk the
20514          parameters of the generic function declaration _and_ the parameters of
20515          DECL itself. This is useful because we want to emit specific DIEs for
20516          function parameter packs and those are declared as part of the
20517          generic function declaration. In that particular case,
20518          the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
20519          That DIE has children DIEs representing the set of arguments
20520          of the pack. Note that the set of pack arguments can be empty.
20521          In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
20522          children DIE.
20523
20524          Otherwise, we just consider the parameters of DECL.  */
20525       while (generic_decl_parm || parm)
20526         {
20527           if (generic_decl_parm
20528               && lang_hooks.function_parameter_pack_p (generic_decl_parm))
20529             gen_formal_parameter_pack_die (generic_decl_parm,
20530                                            parm, subr_die,
20531                                            &parm);
20532           else if (parm && !POINTER_BOUNDS_P (parm))
20533             {
20534               dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
20535
20536               if (parm == DECL_ARGUMENTS (decl)
20537                   && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
20538                   && parm_die
20539                   && (dwarf_version >= 3 || !dwarf_strict))
20540                 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
20541
20542               parm = DECL_CHAIN (parm);
20543             }
20544           else if (parm)
20545             parm = DECL_CHAIN (parm);
20546
20547           if (generic_decl_parm)
20548             generic_decl_parm = DECL_CHAIN (generic_decl_parm);
20549         }
20550
20551       /* Decide whether we need an unspecified_parameters DIE at the end.
20552          There are 2 more cases to do this for: 1) the ansi ... declaration -
20553          this is detectable when the end of the arg list is not a
20554          void_type_node 2) an unprototyped function declaration (not a
20555          definition).  This just means that we have no info about the
20556          parameters at all.  */
20557       if (prototype_p (TREE_TYPE (decl)))
20558         {
20559           /* This is the prototyped case, check for....  */
20560           if (stdarg_p (TREE_TYPE (decl)))
20561             gen_unspecified_parameters_die (decl, subr_die);
20562         }
20563       else if (DECL_INITIAL (decl) == NULL_TREE)
20564         gen_unspecified_parameters_die (decl, subr_die);
20565     }
20566
20567   if (subr_die != old_die)
20568     /* Add the calling convention attribute if requested.  */
20569     add_calling_convention_attribute (subr_die, decl);
20570
20571   /* Output Dwarf info for all of the stuff within the body of the function
20572      (if it has one - it may be just a declaration).
20573
20574      OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
20575      a function.  This BLOCK actually represents the outermost binding contour
20576      for the function, i.e. the contour in which the function's formal
20577      parameters and labels get declared. Curiously, it appears that the front
20578      end doesn't actually put the PARM_DECL nodes for the current function onto
20579      the BLOCK_VARS list for this outer scope, but are strung off of the
20580      DECL_ARGUMENTS list for the function instead.
20581
20582      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
20583      the LABEL_DECL nodes for the function however, and we output DWARF info
20584      for those in decls_for_scope.  Just within the `outer_scope' there will be
20585      a BLOCK node representing the function's outermost pair of curly braces,
20586      and any blocks used for the base and member initializers of a C++
20587      constructor function.  */
20588   tree outer_scope = DECL_INITIAL (decl);
20589   if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
20590     {
20591       int call_site_note_count = 0;
20592       int tail_call_site_note_count = 0;
20593
20594       /* Emit a DW_TAG_variable DIE for a named return value.  */
20595       if (DECL_NAME (DECL_RESULT (decl)))
20596         gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
20597
20598       /* The first time through decls_for_scope we will generate the
20599          DIEs for the locals.  The second time, we fill in the
20600          location info.  */
20601       decls_for_scope (outer_scope, subr_die);
20602
20603       if (call_arg_locations && !dwarf_strict)
20604         {
20605           struct call_arg_loc_node *ca_loc;
20606           for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
20607             {
20608               dw_die_ref die = NULL;
20609               rtx tloc = NULL_RTX, tlocc = NULL_RTX;
20610               rtx arg, next_arg;
20611
20612               for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
20613                           ? NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note)
20614                           : NULL_RTX);
20615                    arg; arg = next_arg)
20616                 {
20617                   dw_loc_descr_ref reg, val;
20618                   machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
20619                   dw_die_ref cdie, tdie = NULL;
20620
20621                   next_arg = XEXP (arg, 1);
20622                   if (REG_P (XEXP (XEXP (arg, 0), 0))
20623                       && next_arg
20624                       && MEM_P (XEXP (XEXP (next_arg, 0), 0))
20625                       && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
20626                       && REGNO (XEXP (XEXP (arg, 0), 0))
20627                          == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
20628                     next_arg = XEXP (next_arg, 1);
20629                   if (mode == VOIDmode)
20630                     {
20631                       mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
20632                       if (mode == VOIDmode)
20633                         mode = GET_MODE (XEXP (arg, 0));
20634                     }
20635                   if (mode == VOIDmode || mode == BLKmode)
20636                     continue;
20637                   /* Get dynamic information about call target only if we
20638                      have no static information: we cannot generate both
20639                      DW_AT_abstract_origin and DW_AT_GNU_call_site_target
20640                      attributes.  */
20641                   if (ca_loc->symbol_ref == NULL_RTX)
20642                     {
20643                       if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
20644                         {
20645                           tloc = XEXP (XEXP (arg, 0), 1);
20646                           continue;
20647                         }
20648                       else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
20649                                && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
20650                         {
20651                           tlocc = XEXP (XEXP (arg, 0), 1);
20652                           continue;
20653                         }
20654                     }
20655                   reg = NULL;
20656                   if (REG_P (XEXP (XEXP (arg, 0), 0)))
20657                     reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
20658                                               VAR_INIT_STATUS_INITIALIZED);
20659                   else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
20660                     {
20661                       rtx mem = XEXP (XEXP (arg, 0), 0);
20662                       reg = mem_loc_descriptor (XEXP (mem, 0),
20663                                                 get_address_mode (mem),
20664                                                 GET_MODE (mem),
20665                                                 VAR_INIT_STATUS_INITIALIZED);
20666                     }
20667                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
20668                            == DEBUG_PARAMETER_REF)
20669                     {
20670                       tree tdecl
20671                         = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
20672                       tdie = lookup_decl_die (tdecl);
20673                       if (tdie == NULL)
20674                         continue;
20675                     }
20676                   else
20677                     continue;
20678                   if (reg == NULL
20679                       && GET_CODE (XEXP (XEXP (arg, 0), 0))
20680                          != DEBUG_PARAMETER_REF)
20681                     continue;
20682                   val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
20683                                             VOIDmode,
20684                                             VAR_INIT_STATUS_INITIALIZED);
20685                   if (val == NULL)
20686                     continue;
20687                   if (die == NULL)
20688                     die = gen_call_site_die (decl, subr_die, ca_loc);
20689                   cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
20690                                   NULL_TREE);
20691                   if (reg != NULL)
20692                     add_AT_loc (cdie, DW_AT_location, reg);
20693                   else if (tdie != NULL)
20694                     add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
20695                   add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
20696                   if (next_arg != XEXP (arg, 1))
20697                     {
20698                       mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
20699                       if (mode == VOIDmode)
20700                         mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
20701                       val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
20702                                                             0), 1),
20703                                                 mode, VOIDmode,
20704                                                 VAR_INIT_STATUS_INITIALIZED);
20705                       if (val != NULL)
20706                         add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
20707                     }
20708                 }
20709               if (die == NULL
20710                   && (ca_loc->symbol_ref || tloc))
20711                 die = gen_call_site_die (decl, subr_die, ca_loc);
20712               if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
20713                 {
20714                   dw_loc_descr_ref tval = NULL;
20715
20716                   if (tloc != NULL_RTX)
20717                     tval = mem_loc_descriptor (tloc,
20718                                                GET_MODE (tloc) == VOIDmode
20719                                                ? Pmode : GET_MODE (tloc),
20720                                                VOIDmode,
20721                                                VAR_INIT_STATUS_INITIALIZED);
20722                   if (tval)
20723                     add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
20724                   else if (tlocc != NULL_RTX)
20725                     {
20726                       tval = mem_loc_descriptor (tlocc,
20727                                                  GET_MODE (tlocc) == VOIDmode
20728                                                  ? Pmode : GET_MODE (tlocc),
20729                                                  VOIDmode,
20730                                                  VAR_INIT_STATUS_INITIALIZED);
20731                       if (tval)
20732                         add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
20733                                     tval);
20734                     }
20735                 }
20736               if (die != NULL)
20737                 {
20738                   call_site_note_count++;
20739                   if (ca_loc->tail_call_p)
20740                     tail_call_site_note_count++;
20741                 }
20742             }
20743         }
20744       call_arg_locations = NULL;
20745       call_arg_loc_last = NULL;
20746       if (tail_call_site_count >= 0
20747           && tail_call_site_count == tail_call_site_note_count
20748           && !dwarf_strict)
20749         {
20750           if (call_site_count >= 0
20751               && call_site_count == call_site_note_count)
20752             add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
20753           else
20754             add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
20755         }
20756       call_site_count = -1;
20757       tail_call_site_count = -1;
20758     }
20759 }
20760
20761 /* Returns a hash value for X (which really is a die_struct).  */
20762
20763 hashval_t
20764 block_die_hasher::hash (die_struct *d)
20765 {
20766   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
20767 }
20768
20769 /* Return nonzero if decl_id and die_parent of die_struct X is the same
20770    as decl_id and die_parent of die_struct Y.  */
20771
20772 bool
20773 block_die_hasher::equal (die_struct *x, die_struct *y)
20774 {
20775   return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
20776 }
20777
20778 /* Return TRUE if DECL, which may have been previously generated as
20779    OLD_DIE, is a candidate for a DW_AT_specification.  DECLARATION is
20780    true if decl (or its origin) is either an extern declaration or a
20781    class/namespace scoped declaration.
20782
20783    The declare_in_namespace support causes us to get two DIEs for one
20784    variable, both of which are declarations.  We want to avoid
20785    considering one to be a specification, so we must test for
20786    DECLARATION and DW_AT_declaration.  */
20787 static inline bool
20788 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
20789 {
20790   return (old_die && TREE_STATIC (decl) && !declaration
20791           && get_AT_flag (old_die, DW_AT_declaration) == 1);
20792 }
20793
20794 /* Return true if DECL is a local static.  */
20795
20796 static inline bool
20797 local_function_static (tree decl)
20798 {
20799   gcc_assert (TREE_CODE (decl) == VAR_DECL);
20800   return TREE_STATIC (decl)
20801     && DECL_CONTEXT (decl)
20802     && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
20803 }
20804
20805 /* Generate a DIE to represent a declared data object.
20806    Either DECL or ORIGIN must be non-null.  */
20807
20808 static void
20809 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
20810 {
20811   HOST_WIDE_INT off = 0;
20812   tree com_decl;
20813   tree decl_or_origin = decl ? decl : origin;
20814   tree ultimate_origin;
20815   dw_die_ref var_die;
20816   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
20817   dw_die_ref origin_die = NULL;
20818   bool declaration = (DECL_EXTERNAL (decl_or_origin)
20819                       || class_or_namespace_scope_p (context_die));
20820   bool specialization_p = false;
20821
20822   ultimate_origin = decl_ultimate_origin (decl_or_origin);
20823   if (decl || ultimate_origin)
20824     origin = ultimate_origin;
20825   com_decl = fortran_common (decl_or_origin, &off);
20826
20827   /* Symbol in common gets emitted as a child of the common block, in the form
20828      of a data member.  */
20829   if (com_decl)
20830     {
20831       dw_die_ref com_die;
20832       dw_loc_list_ref loc;
20833       die_node com_die_arg;
20834
20835       var_die = lookup_decl_die (decl_or_origin);
20836       if (var_die)
20837         {
20838           if (get_AT (var_die, DW_AT_location) == NULL)
20839             {
20840               loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
20841               if (loc)
20842                 {
20843                   if (off)
20844                     {
20845                       /* Optimize the common case.  */
20846                       if (single_element_loc_list_p (loc)
20847                           && loc->expr->dw_loc_opc == DW_OP_addr
20848                           && loc->expr->dw_loc_next == NULL
20849                           && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
20850                              == SYMBOL_REF)
20851                         {
20852                           rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
20853                           loc->expr->dw_loc_oprnd1.v.val_addr
20854                             = plus_constant (GET_MODE (x), x , off);
20855                         }
20856                       else
20857                         loc_list_plus_const (loc, off);
20858                     }
20859                   add_AT_location_description (var_die, DW_AT_location, loc);
20860                   remove_AT (var_die, DW_AT_declaration);
20861                 }
20862             }
20863           return;
20864         }
20865
20866       if (common_block_die_table == NULL)
20867         common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
20868
20869       com_die_arg.decl_id = DECL_UID (com_decl);
20870       com_die_arg.die_parent = context_die;
20871       com_die = common_block_die_table->find (&com_die_arg);
20872       loc = loc_list_from_tree (com_decl, 2, NULL);
20873       if (com_die == NULL)
20874         {
20875           const char *cnam
20876             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
20877           die_node **slot;
20878
20879           com_die = new_die (DW_TAG_common_block, context_die, decl);
20880           add_name_and_src_coords_attributes (com_die, com_decl);
20881           if (loc)
20882             {
20883               add_AT_location_description (com_die, DW_AT_location, loc);
20884               /* Avoid sharing the same loc descriptor between
20885                  DW_TAG_common_block and DW_TAG_variable.  */
20886               loc = loc_list_from_tree (com_decl, 2, NULL);
20887             }
20888           else if (DECL_EXTERNAL (decl))
20889             add_AT_flag (com_die, DW_AT_declaration, 1);
20890           if (want_pubnames ())
20891             add_pubname_string (cnam, com_die); /* ??? needed? */
20892           com_die->decl_id = DECL_UID (com_decl);
20893           slot = common_block_die_table->find_slot (com_die, INSERT);
20894           *slot = com_die;
20895         }
20896       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
20897         {
20898           add_AT_location_description (com_die, DW_AT_location, loc);
20899           loc = loc_list_from_tree (com_decl, 2, NULL);
20900           remove_AT (com_die, DW_AT_declaration);
20901         }
20902       var_die = new_die (DW_TAG_variable, com_die, decl);
20903       add_name_and_src_coords_attributes (var_die, decl);
20904       add_type_attribute (var_die, TREE_TYPE (decl), decl_quals (decl),
20905                           context_die);
20906       add_AT_flag (var_die, DW_AT_external, 1);
20907       if (loc)
20908         {
20909           if (off)
20910             {
20911               /* Optimize the common case.  */
20912               if (single_element_loc_list_p (loc)
20913                   && loc->expr->dw_loc_opc == DW_OP_addr
20914                   && loc->expr->dw_loc_next == NULL
20915                   && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
20916                 {
20917                   rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
20918                   loc->expr->dw_loc_oprnd1.v.val_addr
20919                     = plus_constant (GET_MODE (x), x, off);
20920                 }
20921               else
20922                 loc_list_plus_const (loc, off);
20923             }
20924           add_AT_location_description (var_die, DW_AT_location, loc);
20925         }
20926       else if (DECL_EXTERNAL (decl))
20927         add_AT_flag (var_die, DW_AT_declaration, 1);
20928       equate_decl_number_to_die (decl, var_die);
20929       return;
20930     }
20931
20932   if (old_die)
20933     {
20934       if (declaration)
20935         {
20936           /* A declaration that has been previously dumped, needs no
20937              further annotations, since it doesn't need location on
20938              the second pass.  */
20939           return;
20940         }
20941       else if (decl_will_get_specification_p (old_die, decl, declaration)
20942                && !get_AT (old_die, DW_AT_specification))
20943         {
20944           /* Fall-thru so we can make a new variable die along with a
20945              DW_AT_specification.  */
20946         }
20947       else if (origin && old_die->die_parent != context_die)
20948         {
20949           /* If we will be creating an inlined instance, we need a
20950              new DIE that will get annotated with
20951              DW_AT_abstract_origin.  Clear things so we can get a
20952              new DIE.  */
20953           gcc_assert (!DECL_ABSTRACT_P (decl));
20954           old_die = NULL;
20955         }
20956       else
20957         {
20958           /* If a DIE was dumped early, it still needs location info.
20959              Skip to where we fill the location bits.  */
20960           var_die = old_die;
20961           goto gen_variable_die_location;
20962         }
20963     }
20964
20965   /* For static data members, the declaration in the class is supposed
20966      to have DW_TAG_member tag; the specification should still be
20967      DW_TAG_variable referencing the DW_TAG_member DIE.  */
20968   if (declaration && class_scope_p (context_die))
20969     var_die = new_die (DW_TAG_member, context_die, decl);
20970   else
20971     var_die = new_die (DW_TAG_variable, context_die, decl);
20972
20973   if (origin != NULL)
20974     origin_die = add_abstract_origin_attribute (var_die, origin);
20975
20976   /* Loop unrolling can create multiple blocks that refer to the same
20977      static variable, so we must test for the DW_AT_declaration flag.
20978
20979      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
20980      copy decls and set the DECL_ABSTRACT_P flag on them instead of
20981      sharing them.
20982
20983      ??? Duplicated blocks have been rewritten to use .debug_ranges.  */
20984   else if (decl_will_get_specification_p (old_die, decl, declaration))
20985     {
20986       /* This is a definition of a C++ class level static.  */
20987       add_AT_specification (var_die, old_die);
20988       specialization_p = true;
20989       if (DECL_NAME (decl))
20990         {
20991           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
20992           struct dwarf_file_data * file_index = lookup_filename (s.file);
20993
20994           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
20995             add_AT_file (var_die, DW_AT_decl_file, file_index);
20996
20997           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
20998             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
20999
21000           if (old_die->die_tag == DW_TAG_member)
21001             add_linkage_name (var_die, decl);
21002         }
21003     }
21004   else
21005     add_name_and_src_coords_attributes (var_die, decl);
21006
21007   if ((origin == NULL && !specialization_p)
21008       || (origin != NULL
21009           && !DECL_ABSTRACT_P (decl_or_origin)
21010           && variably_modified_type_p (TREE_TYPE (decl_or_origin),
21011                                        decl_function_context
21012                                                         (decl_or_origin))))
21013     {
21014       tree type = TREE_TYPE (decl_or_origin);
21015
21016       if (decl_by_reference_p (decl_or_origin))
21017         add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
21018                             context_die);
21019       else
21020         add_type_attribute (var_die, type, decl_quals (decl_or_origin),
21021                             context_die);
21022     }
21023
21024   if (origin == NULL && !specialization_p)
21025     {
21026       if (TREE_PUBLIC (decl))
21027         add_AT_flag (var_die, DW_AT_external, 1);
21028
21029       if (DECL_ARTIFICIAL (decl))
21030         add_AT_flag (var_die, DW_AT_artificial, 1);
21031
21032       add_accessibility_attribute (var_die, decl);
21033     }
21034
21035   if (declaration)
21036     add_AT_flag (var_die, DW_AT_declaration, 1);
21037
21038   if (decl && (DECL_ABSTRACT_P (decl)
21039                || !old_die || is_declaration_die (old_die)))
21040     equate_decl_number_to_die (decl, var_die);
21041
21042  gen_variable_die_location:
21043   if (! declaration
21044       && (! DECL_ABSTRACT_P (decl_or_origin)
21045           /* Local static vars are shared between all clones/inlines,
21046              so emit DW_AT_location on the abstract DIE if DECL_RTL is
21047              already set.  */
21048           || (TREE_CODE (decl_or_origin) == VAR_DECL
21049               && TREE_STATIC (decl_or_origin)
21050               && DECL_RTL_SET_P (decl_or_origin)))
21051       /* When abstract origin already has DW_AT_location attribute, no need
21052          to add it again.  */
21053       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
21054     {
21055       if (early_dwarf)
21056         add_pubname (decl_or_origin, var_die);
21057       else
21058         add_location_or_const_value_attribute (var_die, decl_or_origin,
21059                                                decl == NULL);
21060     }
21061   else
21062     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
21063 }
21064
21065 /* Generate a DIE to represent a named constant.  */
21066
21067 static void
21068 gen_const_die (tree decl, dw_die_ref context_die)
21069 {
21070   dw_die_ref const_die;
21071   tree type = TREE_TYPE (decl);
21072
21073   const_die = lookup_decl_die (decl);
21074   if (const_die)
21075     return;
21076
21077   const_die = new_die (DW_TAG_constant, context_die, decl);
21078   equate_decl_number_to_die (decl, const_die);
21079   add_name_and_src_coords_attributes (const_die, decl);
21080   add_type_attribute (const_die, type, TYPE_QUAL_CONST, context_die);
21081   if (TREE_PUBLIC (decl))
21082     add_AT_flag (const_die, DW_AT_external, 1);
21083   if (DECL_ARTIFICIAL (decl))
21084     add_AT_flag (const_die, DW_AT_artificial, 1);
21085   tree_add_const_value_attribute_for_decl (const_die, decl);
21086 }
21087
21088 /* Generate a DIE to represent a label identifier.  */
21089
21090 static void
21091 gen_label_die (tree decl, dw_die_ref context_die)
21092 {
21093   tree origin = decl_ultimate_origin (decl);
21094   dw_die_ref lbl_die = lookup_decl_die (decl);
21095   rtx insn;
21096   char label[MAX_ARTIFICIAL_LABEL_BYTES];
21097
21098   if (!lbl_die)
21099     {
21100       lbl_die = new_die (DW_TAG_label, context_die, decl);
21101       equate_decl_number_to_die (decl, lbl_die);
21102
21103       if (origin != NULL)
21104         add_abstract_origin_attribute (lbl_die, origin);
21105       else
21106         add_name_and_src_coords_attributes (lbl_die, decl);
21107     }
21108
21109   if (DECL_ABSTRACT_P (decl))
21110     equate_decl_number_to_die (decl, lbl_die);
21111   else
21112     {
21113       insn = DECL_RTL_IF_SET (decl);
21114
21115       /* Deleted labels are programmer specified labels which have been
21116          eliminated because of various optimizations.  We still emit them
21117          here so that it is possible to put breakpoints on them.  */
21118       if (insn
21119           && (LABEL_P (insn)
21120               || ((NOTE_P (insn)
21121                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
21122         {
21123           /* When optimization is enabled (via -O) some parts of the compiler
21124              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
21125              represent source-level labels which were explicitly declared by
21126              the user.  This really shouldn't be happening though, so catch
21127              it if it ever does happen.  */
21128           gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
21129
21130           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
21131           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
21132         }
21133       else if (insn
21134                && NOTE_P (insn)
21135                && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
21136                && CODE_LABEL_NUMBER (insn) != -1)
21137         {
21138           ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
21139           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
21140         }
21141     }
21142 }
21143
21144 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
21145    attributes to the DIE for a block STMT, to describe where the inlined
21146    function was called from.  This is similar to add_src_coords_attributes.  */
21147
21148 static inline void
21149 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
21150 {
21151   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
21152
21153   if (dwarf_version >= 3 || !dwarf_strict)
21154     {
21155       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
21156       add_AT_unsigned (die, DW_AT_call_line, s.line);
21157     }
21158 }
21159
21160
21161 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
21162    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
21163
21164 static inline void
21165 add_high_low_attributes (tree stmt, dw_die_ref die)
21166 {
21167   char label[MAX_ARTIFICIAL_LABEL_BYTES];
21168
21169   if (BLOCK_FRAGMENT_CHAIN (stmt)
21170       && (dwarf_version >= 3 || !dwarf_strict))
21171     {
21172       tree chain, superblock = NULL_TREE;
21173       dw_die_ref pdie;
21174       dw_attr_node *attr = NULL;
21175
21176       if (inlined_function_outer_scope_p (stmt))
21177         {
21178           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
21179                                        BLOCK_NUMBER (stmt));
21180           add_AT_lbl_id (die, DW_AT_entry_pc, label);
21181         }
21182
21183       /* Optimize duplicate .debug_ranges lists or even tails of
21184          lists.  If this BLOCK has same ranges as its supercontext,
21185          lookup DW_AT_ranges attribute in the supercontext (and
21186          recursively so), verify that the ranges_table contains the
21187          right values and use it instead of adding a new .debug_range.  */
21188       for (chain = stmt, pdie = die;
21189            BLOCK_SAME_RANGE (chain);
21190            chain = BLOCK_SUPERCONTEXT (chain))
21191         {
21192           dw_attr_node *new_attr;
21193
21194           pdie = pdie->die_parent;
21195           if (pdie == NULL)
21196             break;
21197           if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
21198             break;
21199           new_attr = get_AT (pdie, DW_AT_ranges);
21200           if (new_attr == NULL
21201               || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
21202             break;
21203           attr = new_attr;
21204           superblock = BLOCK_SUPERCONTEXT (chain);
21205         }
21206       if (attr != NULL
21207           && (ranges_table[attr->dw_attr_val.v.val_offset
21208                            / 2 / DWARF2_ADDR_SIZE].num
21209               == BLOCK_NUMBER (superblock))
21210           && BLOCK_FRAGMENT_CHAIN (superblock))
21211         {
21212           unsigned long off = attr->dw_attr_val.v.val_offset
21213                               / 2 / DWARF2_ADDR_SIZE;
21214           unsigned long supercnt = 0, thiscnt = 0;
21215           for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
21216                chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
21217             {
21218               ++supercnt;
21219               gcc_checking_assert (ranges_table[off + supercnt].num
21220                                    == BLOCK_NUMBER (chain));
21221             }
21222           gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
21223           for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
21224                chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
21225             ++thiscnt;
21226           gcc_assert (supercnt >= thiscnt);
21227           add_AT_range_list (die, DW_AT_ranges,
21228                              ((off + supercnt - thiscnt)
21229                               * 2 * DWARF2_ADDR_SIZE),
21230                              false);
21231           return;
21232         }
21233
21234       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
21235
21236       chain = BLOCK_FRAGMENT_CHAIN (stmt);
21237       do
21238         {
21239           add_ranges (chain);
21240           chain = BLOCK_FRAGMENT_CHAIN (chain);
21241         }
21242       while (chain);
21243       add_ranges (NULL);
21244     }
21245   else
21246     {
21247       char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
21248       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
21249                                    BLOCK_NUMBER (stmt));
21250       ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
21251                                    BLOCK_NUMBER (stmt));
21252       add_AT_low_high_pc (die, label, label_high, false);
21253     }
21254 }
21255
21256 /* Generate a DIE for a lexical block.  */
21257
21258 static void
21259 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
21260 {
21261   dw_die_ref old_die = BLOCK_DIE (stmt);
21262   dw_die_ref stmt_die = NULL;
21263   if (!old_die)
21264     {
21265       stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
21266       BLOCK_DIE (stmt) = stmt_die;
21267     }
21268
21269   if (BLOCK_ABSTRACT (stmt))
21270     {
21271       if (old_die)
21272         {
21273           /* This must have been generated early and it won't even
21274              need location information since it's a DW_AT_inline
21275              function.  */
21276           if (flag_checking)
21277             for (dw_die_ref c = context_die; c; c = c->die_parent)
21278               if (c->die_tag == DW_TAG_inlined_subroutine
21279                   || c->die_tag == DW_TAG_subprogram)
21280                 {
21281                   gcc_assert (get_AT (c, DW_AT_inline));
21282                   break;
21283                 }
21284           return;
21285         }
21286     }
21287   else if (BLOCK_ABSTRACT_ORIGIN (stmt))
21288     {
21289       /* If this is an inlined instance, create a new lexical die for
21290          anything below to attach DW_AT_abstract_origin to.  */
21291       if (old_die)
21292         {
21293           stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
21294           BLOCK_DIE (stmt) = stmt_die;
21295           old_die = NULL;
21296         }
21297     }
21298
21299   if (old_die)
21300     stmt_die = old_die;
21301
21302   /* A non abstract block whose blocks have already been reordered
21303      should have the instruction range for this block.  If so, set the
21304      high/low attributes.  */
21305   if (!early_dwarf && !BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
21306     {
21307       gcc_assert (stmt_die);
21308       add_high_low_attributes (stmt, stmt_die);
21309     }
21310
21311   decls_for_scope (stmt, stmt_die);
21312 }
21313
21314 /* Generate a DIE for an inlined subprogram.  */
21315
21316 static void
21317 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
21318 {
21319   tree decl;
21320
21321   /* The instance of function that is effectively being inlined shall not
21322      be abstract.  */
21323   gcc_assert (! BLOCK_ABSTRACT (stmt));
21324
21325   decl = block_ultimate_origin (stmt);
21326
21327   /* Make sure any inlined functions are known to be inlineable.  */
21328   gcc_checking_assert (DECL_ABSTRACT_P (decl)
21329                        || cgraph_function_possibly_inlined_p (decl));
21330
21331   /* Emit info for the abstract instance first, if we haven't yet.  We
21332      must emit this even if the block is abstract, otherwise when we
21333      emit the block below (or elsewhere), we may end up trying to emit
21334      a die whose origin die hasn't been emitted, and crashing.  */
21335   dwarf2out_abstract_function (decl);
21336
21337   if (! BLOCK_ABSTRACT (stmt))
21338     {
21339       dw_die_ref subr_die
21340         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
21341
21342       if (call_arg_locations)
21343         BLOCK_DIE (stmt) = subr_die;
21344       add_abstract_origin_attribute (subr_die, decl);
21345       if (TREE_ASM_WRITTEN (stmt))
21346         add_high_low_attributes (stmt, subr_die);
21347       add_call_src_coords_attributes (stmt, subr_die);
21348
21349       decls_for_scope (stmt, subr_die);
21350     }
21351 }
21352
21353 /* Generate a DIE for a field in a record, or structure.  CTX is required: see
21354    the comment for VLR_CONTEXT.  */
21355
21356 static void
21357 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
21358 {
21359   dw_die_ref decl_die;
21360
21361   if (TREE_TYPE (decl) == error_mark_node)
21362     return;
21363
21364   decl_die = new_die (DW_TAG_member, context_die, decl);
21365   add_name_and_src_coords_attributes (decl_die, decl);
21366   add_type_attribute (decl_die, member_declared_type (decl),
21367                       decl_quals (decl), context_die);
21368
21369   if (DECL_BIT_FIELD_TYPE (decl))
21370     {
21371       add_byte_size_attribute (decl_die, decl);
21372       add_bit_size_attribute (decl_die, decl);
21373       add_bit_offset_attribute (decl_die, decl, ctx);
21374     }
21375
21376   /* If we have a variant part offset, then we are supposed to process a member
21377      of a QUAL_UNION_TYPE, which is how we represent variant parts in
21378      trees.  */
21379   gcc_assert (ctx->variant_part_offset == NULL_TREE
21380               || TREE_CODE (DECL_FIELD_CONTEXT (decl)) != QUAL_UNION_TYPE);
21381   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
21382     add_data_member_location_attribute (decl_die, decl, ctx);
21383
21384   if (DECL_ARTIFICIAL (decl))
21385     add_AT_flag (decl_die, DW_AT_artificial, 1);
21386
21387   add_accessibility_attribute (decl_die, decl);
21388
21389   /* Equate decl number to die, so that we can look up this decl later on.  */
21390   equate_decl_number_to_die (decl, decl_die);
21391 }
21392
21393 #if 0
21394 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
21395    Use modified_type_die instead.
21396    We keep this code here just in case these types of DIEs may be needed to
21397    represent certain things in other languages (e.g. Pascal) someday.  */
21398
21399 static void
21400 gen_pointer_type_die (tree type, dw_die_ref context_die)
21401 {
21402   dw_die_ref ptr_die
21403     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
21404
21405   equate_type_number_to_die (type, ptr_die);
21406   add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
21407                       context_die);
21408   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
21409 }
21410
21411 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
21412    Use modified_type_die instead.
21413    We keep this code here just in case these types of DIEs may be needed to
21414    represent certain things in other languages (e.g. Pascal) someday.  */
21415
21416 static void
21417 gen_reference_type_die (tree type, dw_die_ref context_die)
21418 {
21419   dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
21420
21421   if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
21422     ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
21423   else
21424     ref_die = new_die (DW_TAG_reference_type, scope_die, type);
21425
21426   equate_type_number_to_die (type, ref_die);
21427   add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
21428                       context_die);
21429   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
21430 }
21431 #endif
21432
21433 /* Generate a DIE for a pointer to a member type.  */
21434
21435 static void
21436 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
21437 {
21438   dw_die_ref ptr_die
21439     = new_die (DW_TAG_ptr_to_member_type,
21440                scope_die_for (type, context_die), type);
21441
21442   equate_type_number_to_die (type, ptr_die);
21443   add_AT_die_ref (ptr_die, DW_AT_containing_type,
21444                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
21445   add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
21446                       context_die);
21447 }
21448
21449 static char *producer_string;
21450
21451 /* Return a heap allocated producer string including command line options
21452    if -grecord-gcc-switches.  */
21453
21454 static char *
21455 gen_producer_string (void)
21456 {
21457   size_t j;
21458   auto_vec<const char *> switches;
21459   const char *language_string = lang_hooks.name;
21460   char *producer, *tail;
21461   const char *p;
21462   size_t len = dwarf_record_gcc_switches ? 0 : 3;
21463   size_t plen = strlen (language_string) + 1 + strlen (version_string);
21464
21465   for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
21466     switch (save_decoded_options[j].opt_index)
21467       {
21468       case OPT_o:
21469       case OPT_d:
21470       case OPT_dumpbase:
21471       case OPT_dumpdir:
21472       case OPT_auxbase:
21473       case OPT_auxbase_strip:
21474       case OPT_quiet:
21475       case OPT_version:
21476       case OPT_v:
21477       case OPT_w:
21478       case OPT_L:
21479       case OPT_D:
21480       case OPT_I:
21481       case OPT_U:
21482       case OPT_SPECIAL_unknown:
21483       case OPT_SPECIAL_ignore:
21484       case OPT_SPECIAL_program_name:
21485       case OPT_SPECIAL_input_file:
21486       case OPT_grecord_gcc_switches:
21487       case OPT_gno_record_gcc_switches:
21488       case OPT__output_pch_:
21489       case OPT_fdiagnostics_show_location_:
21490       case OPT_fdiagnostics_show_option:
21491       case OPT_fdiagnostics_show_caret:
21492       case OPT_fdiagnostics_color_:
21493       case OPT_fverbose_asm:
21494       case OPT____:
21495       case OPT__sysroot_:
21496       case OPT_nostdinc:
21497       case OPT_nostdinc__:
21498       case OPT_fpreprocessed:
21499       case OPT_fltrans_output_list_:
21500       case OPT_fresolution_:
21501       case OPT_fdebug_prefix_map_:
21502         /* Ignore these.  */
21503         continue;
21504       default:
21505         if (cl_options[save_decoded_options[j].opt_index].flags
21506             & CL_NO_DWARF_RECORD)
21507           continue;
21508         gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
21509                              == '-');
21510         switch (save_decoded_options[j].canonical_option[0][1])
21511           {
21512           case 'M':
21513           case 'i':
21514           case 'W':
21515             continue;
21516           case 'f':
21517             if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
21518                          "dump", 4) == 0)
21519               continue;
21520             break;
21521           default:
21522             break;
21523           }
21524         switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
21525         len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
21526         break;
21527       }
21528
21529   producer = XNEWVEC (char, plen + 1 + len + 1);
21530   tail = producer;
21531   sprintf (tail, "%s %s", language_string, version_string);
21532   tail += plen;
21533
21534   FOR_EACH_VEC_ELT (switches, j, p)
21535     {
21536       len = strlen (p);
21537       *tail = ' ';
21538       memcpy (tail + 1, p, len);
21539       tail += len + 1;
21540     }
21541
21542   *tail = '\0';
21543   return producer;
21544 }
21545
21546 /* Given a C and/or C++ language/version string return the "highest".
21547    C++ is assumed to be "higher" than C in this case.  Used for merging
21548    LTO translation unit languages.  */
21549 static const char *
21550 highest_c_language (const char *lang1, const char *lang2)
21551 {
21552   if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
21553     return "GNU C++14";
21554   if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
21555     return "GNU C++11";
21556   if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
21557     return "GNU C++98";
21558
21559   if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
21560     return "GNU C11";
21561   if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
21562     return "GNU C99";
21563   if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
21564     return "GNU C89";
21565
21566   gcc_unreachable ();
21567 }
21568
21569
21570 /* Generate the DIE for the compilation unit.  */
21571
21572 static dw_die_ref
21573 gen_compile_unit_die (const char *filename)
21574 {
21575   dw_die_ref die;
21576   const char *language_string = lang_hooks.name;
21577   int language;
21578
21579   die = new_die (DW_TAG_compile_unit, NULL, NULL);
21580
21581   if (filename)
21582     {
21583       add_name_attribute (die, filename);
21584       /* Don't add cwd for <built-in>.  */
21585       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
21586         add_comp_dir_attribute (die);
21587     }
21588
21589   add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
21590
21591   /* If our producer is LTO try to figure out a common language to use
21592      from the global list of translation units.  */
21593   if (strcmp (language_string, "GNU GIMPLE") == 0)
21594     {
21595       unsigned i;
21596       tree t;
21597       const char *common_lang = NULL;
21598
21599       FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
21600         {
21601           if (!TRANSLATION_UNIT_LANGUAGE (t))
21602             continue;
21603           if (!common_lang)
21604             common_lang = TRANSLATION_UNIT_LANGUAGE (t);
21605           else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
21606             ;
21607           else if (strncmp (common_lang, "GNU C", 5) == 0
21608                     && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
21609             /* Mixing C and C++ is ok, use C++ in that case.  */
21610             common_lang = highest_c_language (common_lang,
21611                                               TRANSLATION_UNIT_LANGUAGE (t));
21612           else
21613             {
21614               /* Fall back to C.  */
21615               common_lang = NULL;
21616               break;
21617             }
21618         }
21619
21620       if (common_lang)
21621         language_string = common_lang;
21622     }
21623
21624   language = DW_LANG_C;
21625   if (strncmp (language_string, "GNU C", 5) == 0
21626       && ISDIGIT (language_string[5]))
21627     {
21628       language = DW_LANG_C89;
21629       if (dwarf_version >= 3 || !dwarf_strict)
21630         {
21631           if (strcmp (language_string, "GNU C89") != 0)
21632             language = DW_LANG_C99;
21633
21634           if (dwarf_version >= 5 /* || !dwarf_strict */)
21635             if (strcmp (language_string, "GNU C11") == 0)
21636               language = DW_LANG_C11;
21637         }
21638     }
21639   else if (strncmp (language_string, "GNU C++", 7) == 0)
21640     {
21641       language = DW_LANG_C_plus_plus;
21642       if (dwarf_version >= 5 /* || !dwarf_strict */)
21643         {
21644           if (strcmp (language_string, "GNU C++11") == 0)
21645             language = DW_LANG_C_plus_plus_11;
21646           else if (strcmp (language_string, "GNU C++14") == 0)
21647             language = DW_LANG_C_plus_plus_14;
21648         }
21649     }
21650   else if (strcmp (language_string, "GNU F77") == 0)
21651     language = DW_LANG_Fortran77;
21652   else if (strcmp (language_string, "GNU Pascal") == 0)
21653     language = DW_LANG_Pascal83;
21654   else if (dwarf_version >= 3 || !dwarf_strict)
21655     {
21656       if (strcmp (language_string, "GNU Ada") == 0)
21657         language = DW_LANG_Ada95;
21658       else if (strncmp (language_string, "GNU Fortran", 11) == 0)
21659         {
21660           language = DW_LANG_Fortran95;
21661           if (dwarf_version >= 5 /* || !dwarf_strict */)
21662             {
21663               if (strcmp (language_string, "GNU Fortran2003") == 0)
21664                 language = DW_LANG_Fortran03;
21665               else if (strcmp (language_string, "GNU Fortran2008") == 0)
21666                 language = DW_LANG_Fortran08;
21667             }
21668         }
21669       else if (strcmp (language_string, "GNU Java") == 0)
21670         language = DW_LANG_Java;
21671       else if (strcmp (language_string, "GNU Objective-C") == 0)
21672         language = DW_LANG_ObjC;
21673       else if (strcmp (language_string, "GNU Objective-C++") == 0)
21674         language = DW_LANG_ObjC_plus_plus;
21675       else if (dwarf_version >= 5 || !dwarf_strict)
21676         {
21677           if (strcmp (language_string, "GNU Go") == 0)
21678             language = DW_LANG_Go;
21679         }
21680     }
21681   /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works.  */
21682   else if (strncmp (language_string, "GNU Fortran", 11) == 0)
21683     language = DW_LANG_Fortran90;
21684
21685   add_AT_unsigned (die, DW_AT_language, language);
21686
21687   switch (language)
21688     {
21689     case DW_LANG_Fortran77:
21690     case DW_LANG_Fortran90:
21691     case DW_LANG_Fortran95:
21692     case DW_LANG_Fortran03:
21693     case DW_LANG_Fortran08:
21694       /* Fortran has case insensitive identifiers and the front-end
21695          lowercases everything.  */
21696       add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
21697       break;
21698     default:
21699       /* The default DW_ID_case_sensitive doesn't need to be specified.  */
21700       break;
21701     }
21702   return die;
21703 }
21704
21705 /* Generate the DIE for a base class.  */
21706
21707 static void
21708 gen_inheritance_die (tree binfo, tree access, tree type,
21709                      dw_die_ref context_die)
21710 {
21711   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
21712   struct vlr_context ctx = { type, NULL };
21713
21714   add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, context_die);
21715   add_data_member_location_attribute (die, binfo, &ctx);
21716
21717   if (BINFO_VIRTUAL_P (binfo))
21718     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
21719
21720   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
21721      children, otherwise the default is DW_ACCESS_public.  In DWARF2
21722      the default has always been DW_ACCESS_private.  */
21723   if (access == access_public_node)
21724     {
21725       if (dwarf_version == 2
21726           || context_die->die_tag == DW_TAG_class_type)
21727       add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
21728     }
21729   else if (access == access_protected_node)
21730     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
21731   else if (dwarf_version > 2
21732            && context_die->die_tag != DW_TAG_class_type)
21733     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
21734 }
21735
21736 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
21737    structure.  */
21738 static bool
21739 is_variant_part (tree decl)
21740 {
21741   return (TREE_CODE (decl) == FIELD_DECL
21742           && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
21743 }
21744
21745 /* Check that OPERAND is a reference to a field in STRUCT_TYPE.  If it is,
21746    return the FIELD_DECL.  Return NULL_TREE otherwise.  */
21747
21748 static tree
21749 analyze_discr_in_predicate (tree operand, tree struct_type)
21750 {
21751   bool continue_stripping = true;
21752   while (continue_stripping)
21753     switch (TREE_CODE (operand))
21754       {
21755       CASE_CONVERT:
21756         operand = TREE_OPERAND (operand, 0);
21757         break;
21758       default:
21759         continue_stripping = false;
21760         break;
21761       }
21762
21763   /* Match field access to members of struct_type only.  */
21764   if (TREE_CODE (operand) == COMPONENT_REF
21765       && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
21766       && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
21767       && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
21768     return TREE_OPERAND (operand, 1);
21769   else
21770     return NULL_TREE;
21771 }
21772
21773 /* Check that SRC is a constant integer that can be represented as a native
21774    integer constant (either signed or unsigned).  If so, store it into DEST and
21775    return true.  Return false otherwise. */
21776
21777 static bool
21778 get_discr_value (tree src, dw_discr_value *dest)
21779 {
21780   bool is_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
21781
21782   if (TREE_CODE (src) != INTEGER_CST
21783       || !(is_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
21784     return false;
21785
21786   dest->pos = is_unsigned;
21787   if (is_unsigned)
21788     dest->v.uval = tree_to_uhwi (src);
21789   else
21790     dest->v.sval = tree_to_shwi (src);
21791
21792   return true;
21793 }
21794
21795 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
21796    FIELD_DECL in STRUCT_TYPE that represents a variant part.  If unsuccessful,
21797    store NULL_TREE in DISCR_DECL.  Otherwise:
21798
21799      - store the discriminant field in STRUCT_TYPE that controls the variant
21800        part to *DISCR_DECL
21801
21802      - put in *DISCR_LISTS_P an array where for each variant, the item
21803        represents the corresponding matching list of discriminant values.
21804
21805      - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
21806        the above array.
21807
21808    Note that when the array is allocated (i.e. when the analysis is
21809    successful), it is up to the caller to free the array.  */
21810
21811 static void
21812 analyze_variants_discr (tree variant_part_decl,
21813                         tree struct_type,
21814                         tree *discr_decl,
21815                         dw_discr_list_ref **discr_lists_p,
21816                         unsigned *discr_lists_length)
21817 {
21818   tree variant_part_type = TREE_TYPE (variant_part_decl);
21819   tree variant;
21820   dw_discr_list_ref *discr_lists;
21821   unsigned i;
21822
21823   /* Compute how many variants there are in this variant part.  */
21824   *discr_lists_length = 0;
21825   for (variant = TYPE_FIELDS (variant_part_type);
21826        variant != NULL_TREE;
21827        variant = DECL_CHAIN (variant))
21828     ++*discr_lists_length;
21829
21830   *discr_decl = NULL_TREE;
21831   *discr_lists_p
21832     = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
21833                                      sizeof (**discr_lists_p));
21834   discr_lists = *discr_lists_p;
21835
21836   /* And then analyze all variants to extract discriminant information for all
21837      of them.  This analysis is conservative: as soon as we detect something we
21838      do not support, abort everything and pretend we found nothing.  */
21839   for (variant = TYPE_FIELDS (variant_part_type), i = 0;
21840        variant != NULL_TREE;
21841        variant = DECL_CHAIN (variant), ++i)
21842     {
21843       tree match_expr = DECL_QUALIFIER (variant);
21844
21845       /* Now, try to analyze the predicate and deduce a discriminant for
21846          it.  */
21847       if (match_expr == boolean_true_node)
21848         /* Typically happens for the default variant: it matches all cases that
21849            previous variants rejected.  Don't output any matching value for
21850            this one.  */
21851         continue;
21852
21853       /* The following loop tries to iterate over each discriminant
21854          possibility: single values or ranges.  */
21855       while (match_expr != NULL_TREE)
21856         {
21857           tree next_round_match_expr;
21858           tree candidate_discr = NULL_TREE;
21859           dw_discr_list_ref new_node = NULL;
21860
21861           /* Possibilities are matched one after the other by nested
21862              TRUTH_ORIF_EXPR expressions.  Process the current possibility and
21863              continue with the rest at next iteration.  */
21864           if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
21865             {
21866               next_round_match_expr = TREE_OPERAND (match_expr, 0);
21867               match_expr = TREE_OPERAND (match_expr, 1);
21868             }
21869           else
21870             next_round_match_expr = NULL_TREE;
21871
21872           if (match_expr == boolean_false_node)
21873             /* This sub-expression matches nothing: just wait for the next
21874                one.  */
21875             ;
21876
21877           else if (TREE_CODE (match_expr) == EQ_EXPR)
21878             {
21879               /* We are matching:  <discr_field> == <integer_cst>
21880                  This sub-expression matches a single value.  */
21881               tree integer_cst = TREE_OPERAND (match_expr, 1);
21882
21883               candidate_discr
21884                = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
21885                                              struct_type);
21886
21887               new_node = ggc_cleared_alloc<dw_discr_list_node> ();
21888               if (!get_discr_value (integer_cst,
21889                                     &new_node->dw_discr_lower_bound))
21890                 goto abort;
21891               new_node->dw_discr_range = false;
21892             }
21893
21894           else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
21895             {
21896               /* We are matching:
21897                    <discr_field> > <integer_cst>
21898                    && <discr_field> < <integer_cst>.
21899                  This sub-expression matches the range of values between the
21900                  two matched integer constants.  Note that comparisons can be
21901                  inclusive or exclusive.  */
21902               tree candidate_discr_1, candidate_discr_2;
21903               tree lower_cst, upper_cst;
21904               bool lower_cst_included, upper_cst_included;
21905               tree lower_op = TREE_OPERAND (match_expr, 0);
21906               tree upper_op = TREE_OPERAND (match_expr, 1);
21907
21908               /* When the comparison is exclusive, the integer constant is not
21909                  the discriminant range bound we are looking for: we will have
21910                  to increment or decrement it.  */
21911               if (TREE_CODE (lower_op) == GE_EXPR)
21912                 lower_cst_included = true;
21913               else if (TREE_CODE (lower_op) == GT_EXPR)
21914                 lower_cst_included = false;
21915               else
21916                 goto abort;
21917
21918               if (TREE_CODE (upper_op) == LE_EXPR)
21919                 upper_cst_included = true;
21920               else if (TREE_CODE (upper_op) == LT_EXPR)
21921                 upper_cst_included = false;
21922               else
21923                 goto abort;
21924
21925               /* Extract the discriminant from the first operand and check it
21926                  is consistant with the same analysis in the second
21927                  operand.  */
21928               candidate_discr_1
21929                 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
21930                                               struct_type);
21931               candidate_discr_2
21932                 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
21933                                               struct_type);
21934               if (candidate_discr_1 == candidate_discr_2)
21935                 candidate_discr = candidate_discr_1;
21936               else
21937                 goto abort;
21938
21939               /* Extract bounds from both.  */
21940               new_node = ggc_cleared_alloc<dw_discr_list_node> ();
21941               lower_cst = TREE_OPERAND (lower_op, 1);
21942               upper_cst = TREE_OPERAND (upper_op, 1);
21943
21944               if (!lower_cst_included)
21945                 lower_cst
21946                   = fold (build2 (PLUS_EXPR, TREE_TYPE (lower_cst),
21947                                   lower_cst,
21948                                   build_int_cst (TREE_TYPE (lower_cst), 1)));
21949               if (!upper_cst_included)
21950                 upper_cst
21951                   = fold (build2 (MINUS_EXPR, TREE_TYPE (upper_cst),
21952                                   upper_cst,
21953                                   build_int_cst (TREE_TYPE (upper_cst), 1)));
21954
21955               if (!get_discr_value (lower_cst,
21956                                     &new_node->dw_discr_lower_bound)
21957                   || !get_discr_value (upper_cst,
21958                                        &new_node->dw_discr_upper_bound))
21959                 goto abort;
21960
21961               new_node->dw_discr_range = true;
21962             }
21963
21964           else
21965             /* Unsupported sub-expression: we cannot determine the set of
21966                matching discriminant values.  Abort everything.  */
21967             goto abort;
21968
21969           /* If the discriminant info is not consistant with what we saw so
21970              far, consider the analysis failed and abort everything.  */
21971           if (candidate_discr == NULL_TREE
21972               || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
21973             goto abort;
21974           else
21975             *discr_decl = candidate_discr;
21976
21977           if (new_node != NULL)
21978             {
21979               new_node->dw_discr_next = discr_lists[i];
21980               discr_lists[i] = new_node;
21981             }
21982           match_expr = next_round_match_expr;
21983         }
21984     }
21985
21986   /* If we reach this point, we could match everything we were interested
21987      in.  */
21988   return;
21989
21990 abort:
21991   /* Clean all data structure and return no result.  */
21992   free (*discr_lists_p);
21993   *discr_lists_p = NULL;
21994   *discr_decl = NULL_TREE;
21995 }
21996
21997 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
21998    of STRUCT_TYPE, a record type.  This new DIE is emitted as the next child
21999    under CONTEXT_DIE.
22000
22001    Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
22002    QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter.  The members for
22003    this type, which are record types, represent the available variants and each
22004    has a DECL_QUALIFIER attribute.  The discriminant and the discriminant
22005    values are inferred from these attributes.
22006
22007    In trees, the offsets for the fields inside these sub-records are relative
22008    to the variant part itself, whereas the corresponding DIEs should have
22009    offset attributes that are relative to the embedding record base address.
22010    This is why the caller must provide a VARIANT_PART_OFFSET expression: it
22011    must be an expression that computes the offset of the variant part to
22012    describe in DWARF.  */
22013
22014 static void
22015 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
22016                   dw_die_ref context_die)
22017 {
22018   const tree variant_part_type = TREE_TYPE (variant_part_decl);
22019   tree variant_part_offset = vlr_ctx->variant_part_offset;
22020   struct loc_descr_context ctx = {
22021     vlr_ctx->struct_type, /* context_type */
22022     NULL_TREE,            /* base_decl */
22023     NULL                  /* dpi */
22024   };
22025
22026   /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
22027      NULL_TREE if there is no such field.  */
22028   tree discr_decl = NULL_TREE;
22029   dw_discr_list_ref *discr_lists;
22030   unsigned discr_lists_length = 0;
22031   unsigned i;
22032
22033   dw_die_ref dwarf_proc_die = NULL;
22034   dw_die_ref variant_part_die
22035     = new_die (DW_TAG_variant_part, context_die, variant_part_type);
22036
22037   equate_decl_number_to_die (variant_part_decl, variant_part_die);
22038
22039   analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
22040                           &discr_decl, &discr_lists, &discr_lists_length);
22041
22042   if (discr_decl != NULL_TREE)
22043     {
22044       dw_die_ref discr_die = lookup_decl_die (discr_decl);
22045
22046       if (discr_die)
22047         add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
22048       else
22049         /* We have no DIE for the discriminant, so just discard all
22050            discrimimant information in the output.  */
22051         discr_decl = NULL_TREE;
22052     }
22053
22054   /* If the offset for this variant part is more complex than a constant,
22055      create a DWARF procedure for it so that we will not have to generate DWARF
22056      expressions for it for each member.  */
22057   if (TREE_CODE (variant_part_offset) != INTEGER_CST
22058       && (dwarf_version >= 3 || !dwarf_strict))
22059     {
22060       const tree dwarf_proc_fndecl
22061         = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
22062                       build_function_type (TREE_TYPE (variant_part_offset),
22063                                            NULL_TREE));
22064       const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
22065       const dw_loc_descr_ref dwarf_proc_body
22066         = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
22067
22068       dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
22069                                            dwarf_proc_fndecl, context_die);
22070       if (dwarf_proc_die != NULL)
22071         variant_part_offset = dwarf_proc_call;
22072     }
22073
22074   /* Output DIEs for all variants.  */
22075   i = 0;
22076   for (tree variant = TYPE_FIELDS (variant_part_type);
22077        variant != NULL_TREE;
22078        variant = DECL_CHAIN (variant), ++i)
22079     {
22080       tree variant_type = TREE_TYPE (variant);
22081       dw_die_ref variant_die;
22082
22083       /* All variants (i.e. members of a variant part) are supposed to be
22084          encoded as structures.  Sub-variant parts are QUAL_UNION_TYPE fields
22085          under these records.  */
22086       gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
22087
22088       variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
22089       equate_decl_number_to_die (variant, variant_die);
22090
22091       /* Output discriminant values this variant matches, if any.  */
22092       if (discr_decl == NULL || discr_lists[i] == NULL)
22093         /* In the case we have discriminant information at all, this is
22094            probably the default variant: as the standard says, don't
22095            output any discriminant value/list attribute.  */
22096         ;
22097       else if (discr_lists[i]->dw_discr_next == NULL
22098                && !discr_lists[i]->dw_discr_range)
22099         /* If there is only one accepted value, don't bother outputting a
22100            list.  */
22101         add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
22102       else
22103         add_discr_list (variant_die, discr_lists[i]);
22104
22105       for (tree member = TYPE_FIELDS (variant_type);
22106            member != NULL_TREE;
22107            member = DECL_CHAIN (member))
22108         {
22109           struct vlr_context vlr_sub_ctx = {
22110             vlr_ctx->struct_type, /* struct_type */
22111             NULL                  /* variant_part_offset */
22112           };
22113           if (is_variant_part (member))
22114             {
22115               /* All offsets for fields inside variant parts are relative to
22116                  the top-level embedding RECORD_TYPE's base address.  On the
22117                  other hand, offsets in GCC's types are relative to the
22118                  nested-most variant part.  So we have to sum offsets each time
22119                  we recurse.  */
22120
22121               vlr_sub_ctx.variant_part_offset
22122                 = fold (build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
22123                                 variant_part_offset, byte_position (member)));
22124               gen_variant_part (member, &vlr_sub_ctx, variant_die);
22125             }
22126           else
22127             {
22128               vlr_sub_ctx.variant_part_offset = variant_part_offset;
22129               gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
22130             }
22131         }
22132     }
22133
22134   free (discr_lists);
22135 }
22136
22137 /* Generate a DIE for a class member.  */
22138
22139 static void
22140 gen_member_die (tree type, dw_die_ref context_die)
22141 {
22142   tree member;
22143   tree binfo = TYPE_BINFO (type);
22144   dw_die_ref child;
22145
22146   /* If this is not an incomplete type, output descriptions of each of its
22147      members. Note that as we output the DIEs necessary to represent the
22148      members of this record or union type, we will also be trying to output
22149      DIEs to represent the *types* of those members. However the `type'
22150      function (above) will specifically avoid generating type DIEs for member
22151      types *within* the list of member DIEs for this (containing) type except
22152      for those types (of members) which are explicitly marked as also being
22153      members of this (containing) type themselves.  The g++ front- end can
22154      force any given type to be treated as a member of some other (containing)
22155      type by setting the TYPE_CONTEXT of the given (member) type to point to
22156      the TREE node representing the appropriate (containing) type.  */
22157
22158   /* First output info about the base classes.  */
22159   if (binfo)
22160     {
22161       vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
22162       int i;
22163       tree base;
22164
22165       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
22166         gen_inheritance_die (base,
22167                              (accesses ? (*accesses)[i] : access_public_node),
22168                              type,
22169                              context_die);
22170     }
22171
22172   /* Now output info about the data members and type members.  */
22173   for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
22174     {
22175       struct vlr_context vlr_ctx = { type, NULL_TREE };
22176
22177       /* If we thought we were generating minimal debug info for TYPE
22178          and then changed our minds, some of the member declarations
22179          may have already been defined.  Don't define them again, but
22180          do put them in the right order.  */
22181
22182       child = lookup_decl_die (member);
22183       if (child)
22184         splice_child_die (context_die, child);
22185
22186       /* Do not generate standard DWARF for variant parts if we are generating
22187          the corresponding GNAT encodings: DIEs generated for both would
22188          conflict in our mappings.  */
22189       else if (is_variant_part (member)
22190                && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
22191         {
22192           vlr_ctx.variant_part_offset = byte_position (member);
22193           gen_variant_part (member, &vlr_ctx, context_die);
22194         }
22195       else
22196         {
22197           vlr_ctx.variant_part_offset = NULL_TREE;
22198           gen_decl_die (member, NULL, &vlr_ctx, context_die);
22199         }
22200     }
22201
22202   /* We do not keep type methods in type variants.  */
22203   gcc_assert (TYPE_MAIN_VARIANT (type) == type);
22204   /* Now output info about the function members (if any).  */
22205   if (TYPE_METHODS (type) != error_mark_node)
22206     for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
22207       {
22208         /* Don't include clones in the member list.  */
22209         if (DECL_ABSTRACT_ORIGIN (member))
22210           continue;
22211         /* Nor constructors for anonymous classes.  */
22212         if (DECL_ARTIFICIAL (member)
22213             && dwarf2_name (member, 0) == NULL)
22214           continue;
22215
22216         child = lookup_decl_die (member);
22217         if (child)
22218           splice_child_die (context_die, child);
22219         else
22220           gen_decl_die (member, NULL, NULL, context_die);
22221       }
22222 }
22223
22224 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
22225    is set, we pretend that the type was never defined, so we only get the
22226    member DIEs needed by later specification DIEs.  */
22227
22228 static void
22229 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
22230                                 enum debug_info_usage usage)
22231 {
22232   if (TREE_ASM_WRITTEN (type))
22233     {
22234       /* Fill in the bound of variable-length fields in late dwarf if
22235          still incomplete.  */
22236       if (!early_dwarf && variably_modified_type_p (type, NULL))
22237         for (tree member = TYPE_FIELDS (type);
22238              member;
22239              member = DECL_CHAIN (member))
22240           fill_variable_array_bounds (TREE_TYPE (member));
22241       return;
22242     }
22243
22244   dw_die_ref type_die = lookup_type_die (type);
22245   dw_die_ref scope_die = 0;
22246   int nested = 0;
22247   int complete = (TYPE_SIZE (type)
22248                   && (! TYPE_STUB_DECL (type)
22249                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
22250   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
22251   complete = complete && should_emit_struct_debug (type, usage);
22252
22253   if (type_die && ! complete)
22254     return;
22255
22256   if (TYPE_CONTEXT (type) != NULL_TREE
22257       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
22258           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
22259     nested = 1;
22260
22261   scope_die = scope_die_for (type, context_die);
22262
22263   /* Generate child dies for template paramaters.  */
22264   if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
22265     schedule_generic_params_dies_gen (type);
22266
22267   if (! type_die || (nested && is_cu_die (scope_die)))
22268     /* First occurrence of type or toplevel definition of nested class.  */
22269     {
22270       dw_die_ref old_die = type_die;
22271
22272       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
22273                           ? record_type_tag (type) : DW_TAG_union_type,
22274                           scope_die, type);
22275       equate_type_number_to_die (type, type_die);
22276       if (old_die)
22277         add_AT_specification (type_die, old_die);
22278       else
22279         add_name_attribute (type_die, type_tag (type));
22280     }
22281   else
22282     remove_AT (type_die, DW_AT_declaration);
22283
22284   /* If this type has been completed, then give it a byte_size attribute and
22285      then give a list of members.  */
22286   if (complete && !ns_decl)
22287     {
22288       /* Prevent infinite recursion in cases where the type of some member of
22289          this type is expressed in terms of this type itself.  */
22290       TREE_ASM_WRITTEN (type) = 1;
22291       add_byte_size_attribute (type_die, type);
22292       if (TYPE_STUB_DECL (type) != NULL_TREE)
22293         {
22294           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
22295           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
22296         }
22297
22298       /* If the first reference to this type was as the return type of an
22299          inline function, then it may not have a parent.  Fix this now.  */
22300       if (type_die->die_parent == NULL)
22301         add_child_die (scope_die, type_die);
22302
22303       push_decl_scope (type);
22304       gen_member_die (type, type_die);
22305       pop_decl_scope ();
22306
22307       add_gnat_descriptive_type_attribute (type_die, type, context_die);
22308       if (TYPE_ARTIFICIAL (type))
22309         add_AT_flag (type_die, DW_AT_artificial, 1);
22310
22311       /* GNU extension: Record what type our vtable lives in.  */
22312       if (TYPE_VFIELD (type))
22313         {
22314           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
22315
22316           gen_type_die (vtype, context_die);
22317           add_AT_die_ref (type_die, DW_AT_containing_type,
22318                           lookup_type_die (vtype));
22319         }
22320     }
22321   else
22322     {
22323       add_AT_flag (type_die, DW_AT_declaration, 1);
22324
22325       /* We don't need to do this for function-local types.  */
22326       if (TYPE_STUB_DECL (type)
22327           && ! decl_function_context (TYPE_STUB_DECL (type)))
22328         vec_safe_push (incomplete_types, type);
22329     }
22330
22331   if (get_AT (type_die, DW_AT_name))
22332     add_pubtype (type, type_die);
22333 }
22334
22335 /* Generate a DIE for a subroutine _type_.  */
22336
22337 static void
22338 gen_subroutine_type_die (tree type, dw_die_ref context_die)
22339 {
22340   tree return_type = TREE_TYPE (type);
22341   dw_die_ref subr_die
22342     = new_die (DW_TAG_subroutine_type,
22343                scope_die_for (type, context_die), type);
22344
22345   equate_type_number_to_die (type, subr_die);
22346   add_prototyped_attribute (subr_die, type);
22347   add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, context_die);
22348   gen_formal_types_die (type, subr_die);
22349
22350   if (get_AT (subr_die, DW_AT_name))
22351     add_pubtype (type, subr_die);
22352 }
22353
22354 /* Generate a DIE for a type definition.  */
22355
22356 static void
22357 gen_typedef_die (tree decl, dw_die_ref context_die)
22358 {
22359   dw_die_ref type_die;
22360   tree origin;
22361
22362   if (TREE_ASM_WRITTEN (decl))
22363     {
22364       if (DECL_ORIGINAL_TYPE (decl))
22365         fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
22366       return;
22367     }
22368
22369   TREE_ASM_WRITTEN (decl) = 1;
22370   type_die = new_die (DW_TAG_typedef, context_die, decl);
22371   origin = decl_ultimate_origin (decl);
22372   if (origin != NULL)
22373     add_abstract_origin_attribute (type_die, origin);
22374   else
22375     {
22376       tree type;
22377
22378       add_name_and_src_coords_attributes (type_die, decl);
22379       if (DECL_ORIGINAL_TYPE (decl))
22380         {
22381           type = DECL_ORIGINAL_TYPE (decl);
22382
22383           if (type == error_mark_node)
22384             return;
22385
22386           gcc_assert (type != TREE_TYPE (decl));
22387           equate_type_number_to_die (TREE_TYPE (decl), type_die);
22388         }
22389       else
22390         {
22391           type = TREE_TYPE (decl);
22392
22393           if (type == error_mark_node)
22394             return;
22395
22396           if (is_naming_typedef_decl (TYPE_NAME (type)))
22397             {
22398               /* Here, we are in the case of decl being a typedef naming
22399                  an anonymous type, e.g:
22400                      typedef struct {...} foo;
22401                  In that case TREE_TYPE (decl) is not a typedef variant
22402                  type and TYPE_NAME of the anonymous type is set to the
22403                  TYPE_DECL of the typedef. This construct is emitted by
22404                  the C++ FE.
22405
22406                  TYPE is the anonymous struct named by the typedef
22407                  DECL. As we need the DW_AT_type attribute of the
22408                  DW_TAG_typedef to point to the DIE of TYPE, let's
22409                  generate that DIE right away. add_type_attribute
22410                  called below will then pick (via lookup_type_die) that
22411                  anonymous struct DIE.  */
22412               if (!TREE_ASM_WRITTEN (type))
22413                 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
22414
22415               /* This is a GNU Extension.  We are adding a
22416                  DW_AT_linkage_name attribute to the DIE of the
22417                  anonymous struct TYPE.  The value of that attribute
22418                  is the name of the typedef decl naming the anonymous
22419                  struct.  This greatly eases the work of consumers of
22420                  this debug info.  */
22421               add_linkage_attr (lookup_type_die (type), decl);
22422             }
22423         }
22424
22425       add_type_attribute (type_die, type, decl_quals (decl), context_die);
22426
22427       if (is_naming_typedef_decl (decl))
22428         /* We want that all subsequent calls to lookup_type_die with
22429            TYPE in argument yield the DW_TAG_typedef we have just
22430            created.  */
22431         equate_type_number_to_die (type, type_die);
22432
22433       add_accessibility_attribute (type_die, decl);
22434     }
22435
22436   if (DECL_ABSTRACT_P (decl))
22437     equate_decl_number_to_die (decl, type_die);
22438
22439   if (get_AT (type_die, DW_AT_name))
22440     add_pubtype (decl, type_die);
22441 }
22442
22443 /* Generate a DIE for a struct, class, enum or union type.  */
22444
22445 static void
22446 gen_tagged_type_die (tree type,
22447                      dw_die_ref context_die,
22448                      enum debug_info_usage usage)
22449 {
22450   int need_pop;
22451
22452   if (type == NULL_TREE
22453       || !is_tagged_type (type))
22454     return;
22455
22456   if (TREE_ASM_WRITTEN (type))
22457     need_pop = 0;
22458   /* If this is a nested type whose containing class hasn't been written
22459      out yet, writing it out will cover this one, too.  This does not apply
22460      to instantiations of member class templates; they need to be added to
22461      the containing class as they are generated.  FIXME: This hurts the
22462      idea of combining type decls from multiple TUs, since we can't predict
22463      what set of template instantiations we'll get.  */
22464   else if (TYPE_CONTEXT (type)
22465       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
22466       && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
22467     {
22468       gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
22469
22470       if (TREE_ASM_WRITTEN (type))
22471         return;
22472
22473       /* If that failed, attach ourselves to the stub.  */
22474       push_decl_scope (TYPE_CONTEXT (type));
22475       context_die = lookup_type_die (TYPE_CONTEXT (type));
22476       need_pop = 1;
22477     }
22478   else if (TYPE_CONTEXT (type) != NULL_TREE
22479            && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
22480     {
22481       /* If this type is local to a function that hasn't been written
22482          out yet, use a NULL context for now; it will be fixed up in
22483          decls_for_scope.  */
22484       context_die = lookup_decl_die (TYPE_CONTEXT (type));
22485       /* A declaration DIE doesn't count; nested types need to go in the
22486          specification.  */
22487       if (context_die && is_declaration_die (context_die))
22488         context_die = NULL;
22489       need_pop = 0;
22490     }
22491   else
22492     {
22493       context_die = declare_in_namespace (type, context_die);
22494       need_pop = 0;
22495     }
22496
22497   if (TREE_CODE (type) == ENUMERAL_TYPE)
22498     {
22499       /* This might have been written out by the call to
22500          declare_in_namespace.  */
22501       if (!TREE_ASM_WRITTEN (type))
22502         gen_enumeration_type_die (type, context_die);
22503     }
22504   else
22505     gen_struct_or_union_type_die (type, context_die, usage);
22506
22507   if (need_pop)
22508     pop_decl_scope ();
22509
22510   /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
22511      it up if it is ever completed.  gen_*_type_die will set it for us
22512      when appropriate.  */
22513 }
22514
22515 /* Generate a type description DIE.  */
22516
22517 static void
22518 gen_type_die_with_usage (tree type, dw_die_ref context_die,
22519                          enum debug_info_usage usage)
22520 {
22521   struct array_descr_info info;
22522
22523   if (type == NULL_TREE || type == error_mark_node)
22524     return;
22525
22526   if (flag_checking && type)
22527      verify_type (type);
22528
22529   if (TYPE_NAME (type) != NULL_TREE
22530       && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
22531       && is_redundant_typedef (TYPE_NAME (type))
22532       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
22533     /* The DECL of this type is a typedef we don't want to emit debug
22534        info for but we want debug info for its underlying typedef.
22535        This can happen for e.g, the injected-class-name of a C++
22536        type.  */
22537     type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
22538
22539   /* If TYPE is a typedef type variant, let's generate debug info
22540      for the parent typedef which TYPE is a type of.  */
22541   if (typedef_variant_p (type))
22542     {
22543       if (TREE_ASM_WRITTEN (type))
22544         return;
22545
22546       /* Prevent broken recursion; we can't hand off to the same type.  */
22547       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
22548
22549       /* Give typedefs the right scope.  */
22550       context_die = scope_die_for (type, context_die);
22551
22552       TREE_ASM_WRITTEN (type) = 1;
22553
22554       gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
22555       return;
22556     }
22557
22558   /* If type is an anonymous tagged type named by a typedef, let's
22559      generate debug info for the typedef.  */
22560   if (is_naming_typedef_decl (TYPE_NAME (type)))
22561     {
22562       /* Use the DIE of the containing namespace as the parent DIE of
22563          the type description DIE we want to generate.  */
22564       if (DECL_CONTEXT (TYPE_NAME (type))
22565           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
22566         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
22567
22568       gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
22569       return;
22570     }
22571
22572   /* We are going to output a DIE to represent the unqualified version
22573      of this type (i.e. without any const or volatile qualifiers) so
22574      get the main variant (i.e. the unqualified version) of this type
22575      now.  (Vectors and arrays are special because the debugging info is in the
22576      cloned type itself).  */
22577   if (TREE_CODE (type) != VECTOR_TYPE
22578       && TREE_CODE (type) != ARRAY_TYPE)
22579     type = type_main_variant (type);
22580
22581   /* If this is an array type with hidden descriptor, handle it first.  */
22582   if (!TREE_ASM_WRITTEN (type)
22583       && lang_hooks.types.get_array_descr_info)
22584     {
22585       memset (&info, 0, sizeof (info));
22586       if (lang_hooks.types.get_array_descr_info (type, &info))
22587         {
22588           /* Fortran sometimes emits array types with no dimension.  */
22589           gcc_assert (info.ndimensions >= 0
22590                       && (info.ndimensions
22591                           <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
22592           gen_descr_array_type_die (type, &info, context_die);
22593           TREE_ASM_WRITTEN (type) = 1;
22594           return;
22595         }
22596     }
22597
22598   if (TREE_ASM_WRITTEN (type))
22599     {
22600       /* Variable-length types may be incomplete even if
22601          TREE_ASM_WRITTEN.  For such types, fall through to
22602          gen_array_type_die() and possibly fill in
22603          DW_AT_{upper,lower}_bound attributes.  */
22604       if ((TREE_CODE (type) != ARRAY_TYPE
22605            && TREE_CODE (type) != RECORD_TYPE
22606            && TREE_CODE (type) != UNION_TYPE
22607            && TREE_CODE (type) != QUAL_UNION_TYPE)
22608           || !variably_modified_type_p (type, NULL))
22609         return;
22610     }
22611
22612   switch (TREE_CODE (type))
22613     {
22614     case ERROR_MARK:
22615       break;
22616
22617     case POINTER_TYPE:
22618     case REFERENCE_TYPE:
22619       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
22620          ensures that the gen_type_die recursion will terminate even if the
22621          type is recursive.  Recursive types are possible in Ada.  */
22622       /* ??? We could perhaps do this for all types before the switch
22623          statement.  */
22624       TREE_ASM_WRITTEN (type) = 1;
22625
22626       /* For these types, all that is required is that we output a DIE (or a
22627          set of DIEs) to represent the "basis" type.  */
22628       gen_type_die_with_usage (TREE_TYPE (type), context_die,
22629                                 DINFO_USAGE_IND_USE);
22630       break;
22631
22632     case OFFSET_TYPE:
22633       /* This code is used for C++ pointer-to-data-member types.
22634          Output a description of the relevant class type.  */
22635       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
22636                                         DINFO_USAGE_IND_USE);
22637
22638       /* Output a description of the type of the object pointed to.  */
22639       gen_type_die_with_usage (TREE_TYPE (type), context_die,
22640                                         DINFO_USAGE_IND_USE);
22641
22642       /* Now output a DIE to represent this pointer-to-data-member type
22643          itself.  */
22644       gen_ptr_to_mbr_type_die (type, context_die);
22645       break;
22646
22647     case FUNCTION_TYPE:
22648       /* Force out return type (in case it wasn't forced out already).  */
22649       gen_type_die_with_usage (TREE_TYPE (type), context_die,
22650                                         DINFO_USAGE_DIR_USE);
22651       gen_subroutine_type_die (type, context_die);
22652       break;
22653
22654     case METHOD_TYPE:
22655       /* Force out return type (in case it wasn't forced out already).  */
22656       gen_type_die_with_usage (TREE_TYPE (type), context_die,
22657                                         DINFO_USAGE_DIR_USE);
22658       gen_subroutine_type_die (type, context_die);
22659       break;
22660
22661     case ARRAY_TYPE:
22662     case VECTOR_TYPE:
22663       gen_array_type_die (type, context_die);
22664       break;
22665
22666     case ENUMERAL_TYPE:
22667     case RECORD_TYPE:
22668     case UNION_TYPE:
22669     case QUAL_UNION_TYPE:
22670       gen_tagged_type_die (type, context_die, usage);
22671       return;
22672
22673     case VOID_TYPE:
22674     case INTEGER_TYPE:
22675     case REAL_TYPE:
22676     case FIXED_POINT_TYPE:
22677     case COMPLEX_TYPE:
22678     case BOOLEAN_TYPE:
22679     case POINTER_BOUNDS_TYPE:
22680       /* No DIEs needed for fundamental types.  */
22681       break;
22682
22683     case NULLPTR_TYPE:
22684     case LANG_TYPE:
22685       /* Just use DW_TAG_unspecified_type.  */
22686       {
22687         dw_die_ref type_die = lookup_type_die (type);
22688         if (type_die == NULL)
22689           {
22690             tree name = TYPE_IDENTIFIER (type);
22691             type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
22692                                 type);
22693             add_name_attribute (type_die, IDENTIFIER_POINTER (name));
22694             equate_type_number_to_die (type, type_die);
22695           }
22696       }
22697       break;
22698
22699     default:
22700       if (is_cxx_auto (type))
22701         {
22702           tree name = TYPE_IDENTIFIER (type);
22703           dw_die_ref *die = (name == get_identifier ("auto")
22704                              ? &auto_die : &decltype_auto_die);
22705           if (!*die)
22706             {
22707               *die = new_die (DW_TAG_unspecified_type,
22708                               comp_unit_die (), NULL_TREE);
22709               add_name_attribute (*die, IDENTIFIER_POINTER (name));
22710             }
22711           equate_type_number_to_die (type, *die);
22712           break;
22713         }
22714       gcc_unreachable ();
22715     }
22716
22717   TREE_ASM_WRITTEN (type) = 1;
22718 }
22719
22720 static void
22721 gen_type_die (tree type, dw_die_ref context_die)
22722 {
22723   if (type != error_mark_node)
22724     {
22725       gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
22726       if (flag_checking)
22727         {
22728           dw_die_ref die = lookup_type_die (type);
22729           if (die)
22730             check_die (die);
22731         }
22732     }
22733 }
22734
22735 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
22736    things which are local to the given block.  */
22737
22738 static void
22739 gen_block_die (tree stmt, dw_die_ref context_die)
22740 {
22741   int must_output_die = 0;
22742   bool inlined_func;
22743
22744   /* Ignore blocks that are NULL.  */
22745   if (stmt == NULL_TREE)
22746     return;
22747
22748   inlined_func = inlined_function_outer_scope_p (stmt);
22749
22750   /* If the block is one fragment of a non-contiguous block, do not
22751      process the variables, since they will have been done by the
22752      origin block.  Do process subblocks.  */
22753   if (BLOCK_FRAGMENT_ORIGIN (stmt))
22754     {
22755       tree sub;
22756
22757       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
22758         gen_block_die (sub, context_die);
22759
22760       return;
22761     }
22762
22763   /* Determine if we need to output any Dwarf DIEs at all to represent this
22764      block.  */
22765   if (inlined_func)
22766     /* The outer scopes for inlinings *must* always be represented.  We
22767        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
22768     must_output_die = 1;
22769   else
22770     {
22771       /* Determine if this block directly contains any "significant"
22772          local declarations which we will need to output DIEs for.  */
22773       if (debug_info_level > DINFO_LEVEL_TERSE)
22774         /* We are not in terse mode so *any* local declaration counts
22775            as being a "significant" one.  */
22776         must_output_die = ((BLOCK_VARS (stmt) != NULL
22777                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
22778                            && (TREE_USED (stmt)
22779                                || TREE_ASM_WRITTEN (stmt)
22780                                || BLOCK_ABSTRACT (stmt)));
22781       else if ((TREE_USED (stmt)
22782                 || TREE_ASM_WRITTEN (stmt)
22783                 || BLOCK_ABSTRACT (stmt))
22784                && !dwarf2out_ignore_block (stmt))
22785         must_output_die = 1;
22786     }
22787
22788   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
22789      DIE for any block which contains no significant local declarations at
22790      all.  Rather, in such cases we just call `decls_for_scope' so that any
22791      needed Dwarf info for any sub-blocks will get properly generated. Note
22792      that in terse mode, our definition of what constitutes a "significant"
22793      local declaration gets restricted to include only inlined function
22794      instances and local (nested) function definitions.  */
22795   if (must_output_die)
22796     {
22797       if (inlined_func)
22798         {
22799           /* If STMT block is abstract, that means we have been called
22800              indirectly from dwarf2out_abstract_function.
22801              That function rightfully marks the descendent blocks (of
22802              the abstract function it is dealing with) as being abstract,
22803              precisely to prevent us from emitting any
22804              DW_TAG_inlined_subroutine DIE as a descendent
22805              of an abstract function instance. So in that case, we should
22806              not call gen_inlined_subroutine_die.
22807
22808              Later though, when cgraph asks dwarf2out to emit info
22809              for the concrete instance of the function decl into which
22810              the concrete instance of STMT got inlined, the later will lead
22811              to the generation of a DW_TAG_inlined_subroutine DIE.  */
22812           if (! BLOCK_ABSTRACT (stmt))
22813             gen_inlined_subroutine_die (stmt, context_die);
22814         }
22815       else
22816         gen_lexical_block_die (stmt, context_die);
22817     }
22818   else
22819     decls_for_scope (stmt, context_die);
22820 }
22821
22822 /* Process variable DECL (or variable with origin ORIGIN) within
22823    block STMT and add it to CONTEXT_DIE.  */
22824 static void
22825 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
22826 {
22827   dw_die_ref die;
22828   tree decl_or_origin = decl ? decl : origin;
22829
22830   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
22831     die = lookup_decl_die (decl_or_origin);
22832   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
22833            && TYPE_DECL_IS_STUB (decl_or_origin))
22834     die = lookup_type_die (TREE_TYPE (decl_or_origin));
22835   else
22836     die = NULL;
22837
22838   if (die != NULL && die->die_parent == NULL)
22839     add_child_die (context_die, die);
22840   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
22841     {
22842       if (early_dwarf)
22843         dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
22844                                              stmt, context_die);
22845     }
22846   else
22847     gen_decl_die (decl, origin, NULL, context_die);
22848 }
22849
22850 /* Generate all of the decls declared within a given scope and (recursively)
22851    all of its sub-blocks.  */
22852
22853 static void
22854 decls_for_scope (tree stmt, dw_die_ref context_die)
22855 {
22856   tree decl;
22857   unsigned int i;
22858   tree subblocks;
22859
22860   /* Ignore NULL blocks.  */
22861   if (stmt == NULL_TREE)
22862     return;
22863
22864   /* Output the DIEs to represent all of the data objects and typedefs
22865      declared directly within this block but not within any nested
22866      sub-blocks.  Also, nested function and tag DIEs have been
22867      generated with a parent of NULL; fix that up now.  We don't
22868      have to do this if we're at -g1.  */
22869   if (debug_info_level > DINFO_LEVEL_TERSE)
22870     {
22871       for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
22872         process_scope_var (stmt, decl, NULL_TREE, context_die);
22873       for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
22874         process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
22875                            context_die);
22876     }
22877
22878   /* Even if we're at -g1, we need to process the subblocks in order to get
22879      inlined call information.  */
22880
22881   /* Output the DIEs to represent all sub-blocks (and the items declared
22882      therein) of this block.  */
22883   for (subblocks = BLOCK_SUBBLOCKS (stmt);
22884        subblocks != NULL;
22885        subblocks = BLOCK_CHAIN (subblocks))
22886     gen_block_die (subblocks, context_die);
22887 }
22888
22889 /* Is this a typedef we can avoid emitting?  */
22890
22891 bool
22892 is_redundant_typedef (const_tree decl)
22893 {
22894   if (TYPE_DECL_IS_STUB (decl))
22895     return true;
22896
22897   if (DECL_ARTIFICIAL (decl)
22898       && DECL_CONTEXT (decl)
22899       && is_tagged_type (DECL_CONTEXT (decl))
22900       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
22901       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
22902     /* Also ignore the artificial member typedef for the class name.  */
22903     return true;
22904
22905   return false;
22906 }
22907
22908 /* Return TRUE if TYPE is a typedef that names a type for linkage
22909    purposes. This kind of typedefs is produced by the C++ FE for
22910    constructs like:
22911
22912    typedef struct {...} foo;
22913
22914    In that case, there is no typedef variant type produced for foo.
22915    Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
22916    struct type.  */
22917
22918 static bool
22919 is_naming_typedef_decl (const_tree decl)
22920 {
22921   if (decl == NULL_TREE
22922       || TREE_CODE (decl) != TYPE_DECL
22923       || !is_tagged_type (TREE_TYPE (decl))
22924       || DECL_IS_BUILTIN (decl)
22925       || is_redundant_typedef (decl)
22926       /* It looks like Ada produces TYPE_DECLs that are very similar
22927          to C++ naming typedefs but that have different
22928          semantics. Let's be specific to c++ for now.  */
22929       || !is_cxx ())
22930     return FALSE;
22931
22932   return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
22933           && TYPE_NAME (TREE_TYPE (decl)) == decl
22934           && (TYPE_STUB_DECL (TREE_TYPE (decl))
22935               != TYPE_NAME (TREE_TYPE (decl))));
22936 }
22937
22938 /* Looks up the DIE for a context.  */
22939
22940 static inline dw_die_ref
22941 lookup_context_die (tree context)
22942 {
22943   if (context)
22944     {
22945       /* Find die that represents this context.  */
22946       if (TYPE_P (context))
22947         {
22948           context = TYPE_MAIN_VARIANT (context);
22949           dw_die_ref ctx = lookup_type_die (context);
22950           if (!ctx)
22951             return NULL;
22952           return strip_naming_typedef (context, ctx);
22953         }
22954       else
22955         return lookup_decl_die (context);
22956     }
22957   return comp_unit_die ();
22958 }
22959
22960 /* Returns the DIE for a context.  */
22961
22962 static inline dw_die_ref
22963 get_context_die (tree context)
22964 {
22965   if (context)
22966     {
22967       /* Find die that represents this context.  */
22968       if (TYPE_P (context))
22969         {
22970           context = TYPE_MAIN_VARIANT (context);
22971           return strip_naming_typedef (context, force_type_die (context));
22972         }
22973       else
22974         return force_decl_die (context);
22975     }
22976   return comp_unit_die ();
22977 }
22978
22979 /* Returns the DIE for decl.  A DIE will always be returned.  */
22980
22981 static dw_die_ref
22982 force_decl_die (tree decl)
22983 {
22984   dw_die_ref decl_die;
22985   unsigned saved_external_flag;
22986   tree save_fn = NULL_TREE;
22987   decl_die = lookup_decl_die (decl);
22988   if (!decl_die)
22989     {
22990       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
22991
22992       decl_die = lookup_decl_die (decl);
22993       if (decl_die)
22994         return decl_die;
22995
22996       switch (TREE_CODE (decl))
22997         {
22998         case FUNCTION_DECL:
22999           /* Clear current_function_decl, so that gen_subprogram_die thinks
23000              that this is a declaration. At this point, we just want to force
23001              declaration die.  */
23002           save_fn = current_function_decl;
23003           current_function_decl = NULL_TREE;
23004           gen_subprogram_die (decl, context_die);
23005           current_function_decl = save_fn;
23006           break;
23007
23008         case VAR_DECL:
23009           /* Set external flag to force declaration die. Restore it after
23010            gen_decl_die() call.  */
23011           saved_external_flag = DECL_EXTERNAL (decl);
23012           DECL_EXTERNAL (decl) = 1;
23013           gen_decl_die (decl, NULL, NULL, context_die);
23014           DECL_EXTERNAL (decl) = saved_external_flag;
23015           break;
23016
23017         case NAMESPACE_DECL:
23018           if (dwarf_version >= 3 || !dwarf_strict)
23019             dwarf2out_decl (decl);
23020           else
23021             /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
23022             decl_die = comp_unit_die ();
23023           break;
23024
23025         case TRANSLATION_UNIT_DECL:
23026           decl_die = comp_unit_die ();
23027           break;
23028
23029         default:
23030           gcc_unreachable ();
23031         }
23032
23033       /* We should be able to find the DIE now.  */
23034       if (!decl_die)
23035         decl_die = lookup_decl_die (decl);
23036       gcc_assert (decl_die);
23037     }
23038
23039   return decl_die;
23040 }
23041
23042 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
23043    always returned.  */
23044
23045 static dw_die_ref
23046 force_type_die (tree type)
23047 {
23048   dw_die_ref type_die;
23049
23050   type_die = lookup_type_die (type);
23051   if (!type_die)
23052     {
23053       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
23054
23055       type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
23056                                     context_die);
23057       gcc_assert (type_die);
23058     }
23059   return type_die;
23060 }
23061
23062 /* Force out any required namespaces to be able to output DECL,
23063    and return the new context_die for it, if it's changed.  */
23064
23065 static dw_die_ref
23066 setup_namespace_context (tree thing, dw_die_ref context_die)
23067 {
23068   tree context = (DECL_P (thing)
23069                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
23070   if (context && TREE_CODE (context) == NAMESPACE_DECL)
23071     /* Force out the namespace.  */
23072     context_die = force_decl_die (context);
23073
23074   return context_die;
23075 }
23076
23077 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
23078    type) within its namespace, if appropriate.
23079
23080    For compatibility with older debuggers, namespace DIEs only contain
23081    declarations; all definitions are emitted at CU scope, with
23082    DW_AT_specification pointing to the declaration (like with class
23083    members).  */
23084
23085 static dw_die_ref
23086 declare_in_namespace (tree thing, dw_die_ref context_die)
23087 {
23088   dw_die_ref ns_context;
23089
23090   if (debug_info_level <= DINFO_LEVEL_TERSE)
23091     return context_die;
23092
23093   /* External declarations in the local scope only need to be emitted
23094      once, not once in the namespace and once in the scope.
23095
23096      This avoids declaring the `extern' below in the
23097      namespace DIE as well as in the innermost scope:
23098
23099           namespace S
23100           {
23101             int i=5;
23102             int foo()
23103             {
23104               int i=8;
23105               extern int i;
23106               return i;
23107             }
23108           }
23109   */
23110   if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
23111     return context_die;
23112
23113   /* If this decl is from an inlined function, then don't try to emit it in its
23114      namespace, as we will get confused.  It would have already been emitted
23115      when the abstract instance of the inline function was emitted anyways.  */
23116   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
23117     return context_die;
23118
23119   ns_context = setup_namespace_context (thing, context_die);
23120
23121   if (ns_context != context_die)
23122     {
23123       if (is_fortran ())
23124         return ns_context;
23125       if (DECL_P (thing))
23126         gen_decl_die (thing, NULL, NULL, ns_context);
23127       else
23128         gen_type_die (thing, ns_context);
23129     }
23130   return context_die;
23131 }
23132
23133 /* Generate a DIE for a namespace or namespace alias.  */
23134
23135 static void
23136 gen_namespace_die (tree decl, dw_die_ref context_die)
23137 {
23138   dw_die_ref namespace_die;
23139
23140   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
23141      they are an alias of.  */
23142   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
23143     {
23144       /* Output a real namespace or module.  */
23145       context_die = setup_namespace_context (decl, comp_unit_die ());
23146       namespace_die = new_die (is_fortran ()
23147                                ? DW_TAG_module : DW_TAG_namespace,
23148                                context_die, decl);
23149       /* For Fortran modules defined in different CU don't add src coords.  */
23150       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
23151         {
23152           const char *name = dwarf2_name (decl, 0);
23153           if (name)
23154             add_name_attribute (namespace_die, name);
23155         }
23156       else
23157         add_name_and_src_coords_attributes (namespace_die, decl);
23158       if (DECL_EXTERNAL (decl))
23159         add_AT_flag (namespace_die, DW_AT_declaration, 1);
23160       equate_decl_number_to_die (decl, namespace_die);
23161     }
23162   else
23163     {
23164       /* Output a namespace alias.  */
23165
23166       /* Force out the namespace we are an alias of, if necessary.  */
23167       dw_die_ref origin_die
23168         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
23169
23170       if (DECL_FILE_SCOPE_P (decl)
23171           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
23172         context_die = setup_namespace_context (decl, comp_unit_die ());
23173       /* Now create the namespace alias DIE.  */
23174       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
23175       add_name_and_src_coords_attributes (namespace_die, decl);
23176       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
23177       equate_decl_number_to_die (decl, namespace_die);
23178     }
23179   /* Bypass dwarf2_name's check for DECL_NAMELESS.  */
23180   if (want_pubnames ())
23181     add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
23182 }
23183
23184 /* Generate Dwarf debug information for a decl described by DECL.
23185    The return value is currently only meaningful for PARM_DECLs,
23186    for all other decls it returns NULL.
23187
23188    If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
23189    It can be NULL otherwise.  */
23190
23191 static dw_die_ref
23192 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
23193               dw_die_ref context_die)
23194 {
23195   tree decl_or_origin = decl ? decl : origin;
23196   tree class_origin = NULL, ultimate_origin;
23197
23198   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
23199     return NULL;
23200
23201   /* Ignore pointer bounds decls.  */
23202   if (DECL_P (decl_or_origin)
23203       && TREE_TYPE (decl_or_origin)
23204       && POINTER_BOUNDS_P (decl_or_origin))
23205     return NULL;
23206
23207   switch (TREE_CODE (decl_or_origin))
23208     {
23209     case ERROR_MARK:
23210       break;
23211
23212     case CONST_DECL:
23213       if (!is_fortran () && !is_ada ())
23214         {
23215           /* The individual enumerators of an enum type get output when we output
23216              the Dwarf representation of the relevant enum type itself.  */
23217           break;
23218         }
23219
23220       /* Emit its type.  */
23221       gen_type_die (TREE_TYPE (decl), context_die);
23222
23223       /* And its containing namespace.  */
23224       context_die = declare_in_namespace (decl, context_die);
23225
23226       gen_const_die (decl, context_die);
23227       break;
23228
23229     case FUNCTION_DECL:
23230       /* Don't output any DIEs to represent mere function declarations,
23231          unless they are class members or explicit block externs.  */
23232       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
23233           && DECL_FILE_SCOPE_P (decl_or_origin)
23234           && (current_function_decl == NULL_TREE
23235               || DECL_ARTIFICIAL (decl_or_origin)))
23236         break;
23237
23238 #if 0
23239       /* FIXME */
23240       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
23241          on local redeclarations of global functions.  That seems broken.  */
23242       if (current_function_decl != decl)
23243         /* This is only a declaration.  */;
23244 #endif
23245
23246       /* If we're emitting a clone, emit info for the abstract instance.  */
23247       if (origin || DECL_ORIGIN (decl) != decl)
23248         dwarf2out_abstract_function (origin
23249                                      ? DECL_ORIGIN (origin)
23250                                      : DECL_ABSTRACT_ORIGIN (decl));
23251
23252       /* If we're emitting an out-of-line copy of an inline function,
23253          emit info for the abstract instance and set up to refer to it.  */
23254       else if (cgraph_function_possibly_inlined_p (decl)
23255                && ! DECL_ABSTRACT_P (decl)
23256                && ! class_or_namespace_scope_p (context_die)
23257                /* dwarf2out_abstract_function won't emit a die if this is just
23258                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
23259                   that case, because that works only if we have a die.  */
23260                && DECL_INITIAL (decl) != NULL_TREE)
23261         {
23262           dwarf2out_abstract_function (decl);
23263           set_decl_origin_self (decl);
23264         }
23265
23266       /* Otherwise we're emitting the primary DIE for this decl.  */
23267       else if (debug_info_level > DINFO_LEVEL_TERSE)
23268         {
23269           /* Before we describe the FUNCTION_DECL itself, make sure that we
23270              have its containing type.  */
23271           if (!origin)
23272             origin = decl_class_context (decl);
23273           if (origin != NULL_TREE)
23274             gen_type_die (origin, context_die);
23275
23276           /* And its return type.  */
23277           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
23278
23279           /* And its virtual context.  */
23280           if (DECL_VINDEX (decl) != NULL_TREE)
23281             gen_type_die (DECL_CONTEXT (decl), context_die);
23282
23283           /* Make sure we have a member DIE for decl.  */
23284           if (origin != NULL_TREE)
23285             gen_type_die_for_member (origin, decl, context_die);
23286
23287           /* And its containing namespace.  */
23288           context_die = declare_in_namespace (decl, context_die);
23289         }
23290
23291       /* Now output a DIE to represent the function itself.  */
23292       if (decl)
23293         gen_subprogram_die (decl, context_die);
23294       break;
23295
23296     case TYPE_DECL:
23297       /* If we are in terse mode, don't generate any DIEs to represent any
23298          actual typedefs.  */
23299       if (debug_info_level <= DINFO_LEVEL_TERSE)
23300         break;
23301
23302       /* In the special case of a TYPE_DECL node representing the declaration
23303          of some type tag, if the given TYPE_DECL is marked as having been
23304          instantiated from some other (original) TYPE_DECL node (e.g. one which
23305          was generated within the original definition of an inline function) we
23306          used to generate a special (abbreviated) DW_TAG_structure_type,
23307          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
23308          should be actually referencing those DIEs, as variable DIEs with that
23309          type would be emitted already in the abstract origin, so it was always
23310          removed during unused type prunning.  Don't add anything in this
23311          case.  */
23312       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
23313         break;
23314
23315       if (is_redundant_typedef (decl))
23316         gen_type_die (TREE_TYPE (decl), context_die);
23317       else
23318         /* Output a DIE to represent the typedef itself.  */
23319         gen_typedef_die (decl, context_die);
23320       break;
23321
23322     case LABEL_DECL:
23323       if (debug_info_level >= DINFO_LEVEL_NORMAL)
23324         gen_label_die (decl, context_die);
23325       break;
23326
23327     case VAR_DECL:
23328     case RESULT_DECL:
23329       /* If we are in terse mode, don't generate any DIEs to represent any
23330          variable declarations or definitions.  */
23331       if (debug_info_level <= DINFO_LEVEL_TERSE)
23332         break;
23333
23334       /* Output any DIEs that are needed to specify the type of this data
23335          object.  */
23336       if (decl_by_reference_p (decl_or_origin))
23337         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
23338       else
23339         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
23340
23341       /* And its containing type.  */
23342       class_origin = decl_class_context (decl_or_origin);
23343       if (class_origin != NULL_TREE)
23344         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
23345
23346       /* And its containing namespace.  */
23347       context_die = declare_in_namespace (decl_or_origin, context_die);
23348
23349       /* Now output the DIE to represent the data object itself.  This gets
23350          complicated because of the possibility that the VAR_DECL really
23351          represents an inlined instance of a formal parameter for an inline
23352          function.  */
23353       ultimate_origin = decl_ultimate_origin (decl_or_origin);
23354       if (ultimate_origin != NULL_TREE
23355           && TREE_CODE (ultimate_origin) == PARM_DECL)
23356         gen_formal_parameter_die (decl, origin,
23357                                   true /* Emit name attribute.  */,
23358                                   context_die);
23359       else
23360         gen_variable_die (decl, origin, context_die);
23361       break;
23362
23363     case FIELD_DECL:
23364       gcc_assert (ctx != NULL && ctx->struct_type != NULL);
23365       /* Ignore the nameless fields that are used to skip bits but handle C++
23366          anonymous unions and structs.  */
23367       if (DECL_NAME (decl) != NULL_TREE
23368           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
23369           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
23370         {
23371           gen_type_die (member_declared_type (decl), context_die);
23372           gen_field_die (decl, ctx, context_die);
23373         }
23374       break;
23375
23376     case PARM_DECL:
23377       if (DECL_BY_REFERENCE (decl_or_origin))
23378         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
23379       else
23380         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
23381       return gen_formal_parameter_die (decl, origin,
23382                                        true /* Emit name attribute.  */,
23383                                        context_die);
23384
23385     case NAMESPACE_DECL:
23386       if (dwarf_version >= 3 || !dwarf_strict)
23387         gen_namespace_die (decl, context_die);
23388       break;
23389
23390     case IMPORTED_DECL:
23391       dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
23392                                            DECL_CONTEXT (decl), context_die);
23393       break;
23394
23395     case NAMELIST_DECL:
23396       gen_namelist_decl (DECL_NAME (decl), context_die,
23397                          NAMELIST_DECL_ASSOCIATED_DECL (decl));
23398       break;
23399
23400     default:
23401       /* Probably some frontend-internal decl.  Assume we don't care.  */
23402       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
23403       break;
23404     }
23405
23406   return NULL;
23407 }
23408 \f
23409 /* Output initial debug information for global DECL.  Called at the
23410    end of the parsing process.
23411
23412    This is the initial debug generation process.  As such, the DIEs
23413    generated may be incomplete.  A later debug generation pass
23414    (dwarf2out_late_global_decl) will augment the information generated
23415    in this pass (e.g., with complete location info).  */
23416
23417 static void
23418 dwarf2out_early_global_decl (tree decl)
23419 {
23420   set_early_dwarf s;
23421
23422   /* gen_decl_die() will set DECL_ABSTRACT because
23423      cgraph_function_possibly_inlined_p() returns true.  This is in
23424      turn will cause DW_AT_inline attributes to be set.
23425
23426      This happens because at early dwarf generation, there is no
23427      cgraph information, causing cgraph_function_possibly_inlined_p()
23428      to return true.  Trick cgraph_function_possibly_inlined_p()
23429      while we generate dwarf early.  */
23430   bool save = symtab->global_info_ready;
23431   symtab->global_info_ready = true;
23432
23433   /* We don't handle TYPE_DECLs.  If required, they'll be reached via
23434      other DECLs and they can point to template types or other things
23435      that dwarf2out can't handle when done via dwarf2out_decl.  */
23436   if (TREE_CODE (decl) != TYPE_DECL
23437       && TREE_CODE (decl) != PARM_DECL)
23438     {
23439       tree save_fndecl = current_function_decl;
23440       if (TREE_CODE (decl) == FUNCTION_DECL)
23441         {
23442           /* No cfun means the symbol has no body, so there's nothing
23443              to emit.  */
23444           if (!DECL_STRUCT_FUNCTION (decl))
23445             goto early_decl_exit;
23446
23447           current_function_decl = decl;
23448         }
23449       dwarf2out_decl (decl);
23450       if (TREE_CODE (decl) == FUNCTION_DECL)
23451         current_function_decl = save_fndecl;
23452     }
23453  early_decl_exit:
23454   symtab->global_info_ready = save;
23455 }
23456
23457 /* Output debug information for global decl DECL.  Called from
23458    toplev.c after compilation proper has finished.  */
23459
23460 static void
23461 dwarf2out_late_global_decl (tree decl)
23462 {
23463   /* We have to generate early debug late for LTO.  */
23464   if (in_lto_p)
23465     dwarf2out_early_global_decl (decl);
23466
23467     /* Fill-in any location information we were unable to determine
23468        on the first pass.  */
23469   if (TREE_CODE (decl) == VAR_DECL
23470       && !POINTER_BOUNDS_P (decl))
23471     {
23472       dw_die_ref die = lookup_decl_die (decl);
23473       if (die)
23474         add_location_or_const_value_attribute (die, decl, false);
23475     }
23476 }
23477
23478 /* Output debug information for type decl DECL.  Called from toplev.c
23479    and from language front ends (to record built-in types).  */
23480 static void
23481 dwarf2out_type_decl (tree decl, int local)
23482 {
23483   if (!local)
23484     {
23485       set_early_dwarf s;
23486       dwarf2out_decl (decl);
23487     }
23488 }
23489
23490 /* Output debug information for imported module or decl DECL.
23491    NAME is non-NULL name in the lexical block if the decl has been renamed.
23492    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
23493    that DECL belongs to.
23494    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
23495 static void
23496 dwarf2out_imported_module_or_decl_1 (tree decl,
23497                                      tree name,
23498                                      tree lexical_block,
23499                                      dw_die_ref lexical_block_die)
23500 {
23501   expanded_location xloc;
23502   dw_die_ref imported_die = NULL;
23503   dw_die_ref at_import_die;
23504
23505   if (TREE_CODE (decl) == IMPORTED_DECL)
23506     {
23507       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
23508       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
23509       gcc_assert (decl);
23510     }
23511   else
23512     xloc = expand_location (input_location);
23513
23514   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
23515     {
23516       at_import_die = force_type_die (TREE_TYPE (decl));
23517       /* For namespace N { typedef void T; } using N::T; base_type_die
23518          returns NULL, but DW_TAG_imported_declaration requires
23519          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
23520       if (!at_import_die)
23521         {
23522           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
23523           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
23524           at_import_die = lookup_type_die (TREE_TYPE (decl));
23525           gcc_assert (at_import_die);
23526         }
23527     }
23528   else
23529     {
23530       at_import_die = lookup_decl_die (decl);
23531       if (!at_import_die)
23532         {
23533           /* If we're trying to avoid duplicate debug info, we may not have
23534              emitted the member decl for this field.  Emit it now.  */
23535           if (TREE_CODE (decl) == FIELD_DECL)
23536             {
23537               tree type = DECL_CONTEXT (decl);
23538
23539               if (TYPE_CONTEXT (type)
23540                   && TYPE_P (TYPE_CONTEXT (type))
23541                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
23542                                                 DINFO_USAGE_DIR_USE))
23543                 return;
23544               gen_type_die_for_member (type, decl,
23545                                        get_context_die (TYPE_CONTEXT (type)));
23546             }
23547           if (TREE_CODE (decl) == NAMELIST_DECL)
23548             at_import_die = gen_namelist_decl (DECL_NAME (decl),
23549                                          get_context_die (DECL_CONTEXT (decl)),
23550                                          NULL_TREE);
23551           else
23552             at_import_die = force_decl_die (decl);
23553         }
23554     }
23555
23556   if (TREE_CODE (decl) == NAMESPACE_DECL)
23557     {
23558       if (dwarf_version >= 3 || !dwarf_strict)
23559         imported_die = new_die (DW_TAG_imported_module,
23560                                 lexical_block_die,
23561                                 lexical_block);
23562       else
23563         return;
23564     }
23565   else
23566     imported_die = new_die (DW_TAG_imported_declaration,
23567                             lexical_block_die,
23568                             lexical_block);
23569
23570   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
23571   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
23572   if (name)
23573     add_AT_string (imported_die, DW_AT_name,
23574                    IDENTIFIER_POINTER (name));
23575   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
23576 }
23577
23578 /* Output debug information for imported module or decl DECL.
23579    NAME is non-NULL name in context if the decl has been renamed.
23580    CHILD is true if decl is one of the renamed decls as part of
23581    importing whole module.  */
23582
23583 static void
23584 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
23585                                    bool child)
23586 {
23587   /* dw_die_ref at_import_die;  */
23588   dw_die_ref scope_die;
23589
23590   if (debug_info_level <= DINFO_LEVEL_TERSE)
23591     return;
23592
23593   gcc_assert (decl);
23594
23595   set_early_dwarf s;
23596
23597   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
23598      We need decl DIE for reference and scope die. First, get DIE for the decl
23599      itself.  */
23600
23601   /* Get the scope die for decl context. Use comp_unit_die for global module
23602      or decl. If die is not found for non globals, force new die.  */
23603   if (context
23604       && TYPE_P (context)
23605       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
23606     return;
23607
23608   if (!(dwarf_version >= 3 || !dwarf_strict))
23609     return;
23610
23611   scope_die = get_context_die (context);
23612
23613   if (child)
23614     {
23615       gcc_assert (scope_die->die_child);
23616       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
23617       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
23618       scope_die = scope_die->die_child;
23619     }
23620
23621   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
23622   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
23623 }
23624
23625 /* Output debug information for namelists.   */
23626
23627 static dw_die_ref
23628 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
23629 {
23630   dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
23631   tree value;
23632   unsigned i;
23633
23634   if (debug_info_level <= DINFO_LEVEL_TERSE)
23635     return NULL;
23636
23637   gcc_assert (scope_die != NULL);
23638   nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
23639   add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
23640
23641   /* If there are no item_decls, we have a nondefining namelist, e.g.
23642      with USE association; hence, set DW_AT_declaration.  */
23643   if (item_decls == NULL_TREE)
23644     {
23645       add_AT_flag (nml_die, DW_AT_declaration, 1);
23646       return nml_die;
23647     }
23648
23649   FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
23650     {
23651       nml_item_ref_die = lookup_decl_die (value);
23652       if (!nml_item_ref_die)
23653         nml_item_ref_die = force_decl_die (value);
23654
23655       nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
23656       add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
23657     }
23658   return nml_die;
23659 }
23660
23661
23662 /* Write the debugging output for DECL and return the DIE.  */
23663
23664 static void
23665 dwarf2out_decl (tree decl)
23666 {
23667   dw_die_ref context_die = comp_unit_die ();
23668
23669   switch (TREE_CODE (decl))
23670     {
23671     case ERROR_MARK:
23672       return;
23673
23674     case FUNCTION_DECL:
23675       /* What we would really like to do here is to filter out all mere
23676          file-scope declarations of file-scope functions which are never
23677          referenced later within this translation unit (and keep all of ones
23678          that *are* referenced later on) but we aren't clairvoyant, so we have
23679          no idea which functions will be referenced in the future (i.e. later
23680          on within the current translation unit). So here we just ignore all
23681          file-scope function declarations which are not also definitions.  If
23682          and when the debugger needs to know something about these functions,
23683          it will have to hunt around and find the DWARF information associated
23684          with the definition of the function.
23685
23686          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
23687          nodes represent definitions and which ones represent mere
23688          declarations.  We have to check DECL_INITIAL instead. That's because
23689          the C front-end supports some weird semantics for "extern inline"
23690          function definitions.  These can get inlined within the current
23691          translation unit (and thus, we need to generate Dwarf info for their
23692          abstract instances so that the Dwarf info for the concrete inlined
23693          instances can have something to refer to) but the compiler never
23694          generates any out-of-lines instances of such things (despite the fact
23695          that they *are* definitions).
23696
23697          The important point is that the C front-end marks these "extern
23698          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
23699          them anyway. Note that the C++ front-end also plays some similar games
23700          for inline function definitions appearing within include files which
23701          also contain `#pragma interface' pragmas.
23702
23703          If we are called from dwarf2out_abstract_function output a DIE
23704          anyway.  We can end up here this way with early inlining and LTO
23705          where the inlined function is output in a different LTRANS unit
23706          or not at all.  */
23707       if (DECL_INITIAL (decl) == NULL_TREE
23708           && ! DECL_ABSTRACT_P (decl))
23709         return;
23710
23711       /* If we're a nested function, initially use a parent of NULL; if we're
23712          a plain function, this will be fixed up in decls_for_scope.  If
23713          we're a method, it will be ignored, since we already have a DIE.  */
23714       if (decl_function_context (decl)
23715           /* But if we're in terse mode, we don't care about scope.  */
23716           && debug_info_level > DINFO_LEVEL_TERSE)
23717         context_die = NULL;
23718       break;
23719
23720     case VAR_DECL:
23721       /* For local statics lookup proper context die.  */
23722       if (local_function_static (decl))
23723         context_die = lookup_decl_die (DECL_CONTEXT (decl));
23724
23725       /* If we are in terse mode, don't generate any DIEs to represent any
23726          variable declarations or definitions.  */
23727       if (debug_info_level <= DINFO_LEVEL_TERSE)
23728         return;
23729       break;
23730
23731     case CONST_DECL:
23732       if (debug_info_level <= DINFO_LEVEL_TERSE)
23733         return;
23734       if (!is_fortran () && !is_ada ())
23735         return;
23736       if (TREE_STATIC (decl) && decl_function_context (decl))
23737         context_die = lookup_decl_die (DECL_CONTEXT (decl));
23738       break;
23739
23740     case NAMESPACE_DECL:
23741     case IMPORTED_DECL:
23742       if (debug_info_level <= DINFO_LEVEL_TERSE)
23743         return;
23744       if (lookup_decl_die (decl) != NULL)
23745         return;
23746       break;
23747
23748     case TYPE_DECL:
23749       /* Don't emit stubs for types unless they are needed by other DIEs.  */
23750       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
23751         return;
23752
23753       /* Don't bother trying to generate any DIEs to represent any of the
23754          normal built-in types for the language we are compiling.  */
23755       if (DECL_IS_BUILTIN (decl))
23756         return;
23757
23758       /* If we are in terse mode, don't generate any DIEs for types.  */
23759       if (debug_info_level <= DINFO_LEVEL_TERSE)
23760         return;
23761
23762       /* If we're a function-scope tag, initially use a parent of NULL;
23763          this will be fixed up in decls_for_scope.  */
23764       if (decl_function_context (decl))
23765         context_die = NULL;
23766
23767       break;
23768
23769     case NAMELIST_DECL:
23770       break;
23771
23772     default:
23773       return;
23774     }
23775
23776   gen_decl_die (decl, NULL, NULL, context_die);
23777
23778   if (flag_checking)
23779     {
23780       dw_die_ref die = lookup_decl_die (decl);
23781       if (die)
23782         check_die (die);
23783     }
23784 }
23785
23786 /* Write the debugging output for DECL.  */
23787
23788 static void
23789 dwarf2out_function_decl (tree decl)
23790 {
23791   dwarf2out_decl (decl);
23792   call_arg_locations = NULL;
23793   call_arg_loc_last = NULL;
23794   call_site_count = -1;
23795   tail_call_site_count = -1;
23796   decl_loc_table->empty ();
23797   cached_dw_loc_list_table->empty ();
23798 }
23799
23800 /* Output a marker (i.e. a label) for the beginning of the generated code for
23801    a lexical block.  */
23802
23803 static void
23804 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
23805                        unsigned int blocknum)
23806 {
23807   switch_to_section (current_function_section ());
23808   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
23809 }
23810
23811 /* Output a marker (i.e. a label) for the end of the generated code for a
23812    lexical block.  */
23813
23814 static void
23815 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
23816 {
23817   switch_to_section (current_function_section ());
23818   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
23819 }
23820
23821 /* Returns nonzero if it is appropriate not to emit any debugging
23822    information for BLOCK, because it doesn't contain any instructions.
23823
23824    Don't allow this for blocks with nested functions or local classes
23825    as we would end up with orphans, and in the presence of scheduling
23826    we may end up calling them anyway.  */
23827
23828 static bool
23829 dwarf2out_ignore_block (const_tree block)
23830 {
23831   tree decl;
23832   unsigned int i;
23833
23834   for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
23835     if (TREE_CODE (decl) == FUNCTION_DECL
23836         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
23837       return 0;
23838   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
23839     {
23840       decl = BLOCK_NONLOCALIZED_VAR (block, i);
23841       if (TREE_CODE (decl) == FUNCTION_DECL
23842           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
23843       return 0;
23844     }
23845
23846   return 1;
23847 }
23848
23849 /* Hash table routines for file_hash.  */
23850
23851 bool
23852 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
23853 {
23854   return filename_cmp (p1->filename, p2) == 0;
23855 }
23856
23857 hashval_t
23858 dwarf_file_hasher::hash (dwarf_file_data *p)
23859 {
23860   return htab_hash_string (p->filename);
23861 }
23862
23863 /* Lookup FILE_NAME (in the list of filenames that we know about here in
23864    dwarf2out.c) and return its "index".  The index of each (known) filename is
23865    just a unique number which is associated with only that one filename.  We
23866    need such numbers for the sake of generating labels (in the .debug_sfnames
23867    section) and references to those files numbers (in the .debug_srcinfo
23868    and .debug_macinfo sections).  If the filename given as an argument is not
23869    found in our current list, add it to the list and assign it the next
23870    available unique index number.  */
23871
23872 static struct dwarf_file_data *
23873 lookup_filename (const char *file_name)
23874 {
23875   struct dwarf_file_data * created;
23876
23877   if (!file_name)
23878     return NULL;
23879
23880   dwarf_file_data **slot
23881     = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
23882                                        INSERT);
23883   if (*slot)
23884     return *slot;
23885
23886   created = ggc_alloc<dwarf_file_data> ();
23887   created->filename = file_name;
23888   created->emitted_number = 0;
23889   *slot = created;
23890   return created;
23891 }
23892
23893 /* If the assembler will construct the file table, then translate the compiler
23894    internal file table number into the assembler file table number, and emit
23895    a .file directive if we haven't already emitted one yet.  The file table
23896    numbers are different because we prune debug info for unused variables and
23897    types, which may include filenames.  */
23898
23899 static int
23900 maybe_emit_file (struct dwarf_file_data * fd)
23901 {
23902   if (! fd->emitted_number)
23903     {
23904       if (last_emitted_file)
23905         fd->emitted_number = last_emitted_file->emitted_number + 1;
23906       else
23907         fd->emitted_number = 1;
23908       last_emitted_file = fd;
23909
23910       if (DWARF2_ASM_LINE_DEBUG_INFO)
23911         {
23912           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
23913           output_quoted_string (asm_out_file,
23914                                 remap_debug_filename (fd->filename));
23915           fputc ('\n', asm_out_file);
23916         }
23917     }
23918
23919   return fd->emitted_number;
23920 }
23921
23922 /* Schedule generation of a DW_AT_const_value attribute to DIE.
23923    That generation should happen after function debug info has been
23924    generated. The value of the attribute is the constant value of ARG.  */
23925
23926 static void
23927 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
23928 {
23929   die_arg_entry entry;
23930
23931   if (!die || !arg)
23932     return;
23933
23934   gcc_assert (early_dwarf);
23935
23936   if (!tmpl_value_parm_die_table)
23937     vec_alloc (tmpl_value_parm_die_table, 32);
23938
23939   entry.die = die;
23940   entry.arg = arg;
23941   vec_safe_push (tmpl_value_parm_die_table, entry);
23942 }
23943
23944 /* Return TRUE if T is an instance of generic type, FALSE
23945    otherwise.  */
23946
23947 static bool
23948 generic_type_p (tree t)
23949 {
23950   if (t == NULL_TREE || !TYPE_P (t))
23951     return false;
23952   return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
23953 }
23954
23955 /* Schedule the generation of the generic parameter dies for the
23956   instance of generic type T. The proper generation itself is later
23957   done by gen_scheduled_generic_parms_dies. */
23958
23959 static void
23960 schedule_generic_params_dies_gen (tree t)
23961 {
23962   if (!generic_type_p (t))
23963     return;
23964
23965   gcc_assert (early_dwarf);
23966
23967   if (!generic_type_instances)
23968     vec_alloc (generic_type_instances, 256);
23969
23970   vec_safe_push (generic_type_instances, t);
23971 }
23972
23973 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
23974    by append_entry_to_tmpl_value_parm_die_table. This function must
23975    be called after function DIEs have been generated.  */
23976
23977 static void
23978 gen_remaining_tmpl_value_param_die_attribute (void)
23979 {
23980   if (tmpl_value_parm_die_table)
23981     {
23982       unsigned i, j;
23983       die_arg_entry *e;
23984
23985       /* We do this in two phases - first get the cases we can
23986          handle during early-finish, preserving those we cannot
23987          (containing symbolic constants where we don't yet know
23988          whether we are going to output the referenced symbols).
23989          For those we try again at late-finish.  */
23990       j = 0;
23991       FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
23992         {
23993           if (!tree_add_const_value_attribute (e->die, e->arg))
23994             (*tmpl_value_parm_die_table)[j++] = *e;
23995         }
23996       tmpl_value_parm_die_table->truncate (j);
23997     }
23998 }
23999
24000 /* Generate generic parameters DIEs for instances of generic types
24001    that have been previously scheduled by
24002    schedule_generic_params_dies_gen. This function must be called
24003    after all the types of the CU have been laid out.  */
24004
24005 static void
24006 gen_scheduled_generic_parms_dies (void)
24007 {
24008   unsigned i;
24009   tree t;
24010
24011   if (!generic_type_instances)
24012     return;
24013   
24014   /* We end up "recursing" into schedule_generic_params_dies_gen, so
24015      pretend this generation is part of "early dwarf" as well.  */
24016   set_early_dwarf s;
24017
24018   FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
24019     if (COMPLETE_TYPE_P (t))
24020       gen_generic_params_dies (t);
24021
24022   generic_type_instances = NULL;
24023 }
24024
24025
24026 /* Replace DW_AT_name for the decl with name.  */
24027
24028 static void
24029 dwarf2out_set_name (tree decl, tree name)
24030 {
24031   dw_die_ref die;
24032   dw_attr_node *attr;
24033   const char *dname;
24034
24035   die = TYPE_SYMTAB_DIE (decl);
24036   if (!die)
24037     return;
24038
24039   dname = dwarf2_name (name, 0);
24040   if (!dname)
24041     return;
24042
24043   attr = get_AT (die, DW_AT_name);
24044   if (attr)
24045     {
24046       struct indirect_string_node *node;
24047
24048       node = find_AT_string (dname);
24049       /* replace the string.  */
24050       attr->dw_attr_val.v.val_str = node;
24051     }
24052
24053   else
24054     add_name_attribute (die, dname);
24055 }
24056
24057 /* True if before or during processing of the first function being emitted.  */
24058 static bool in_first_function_p = true;
24059 /* True if loc_note during dwarf2out_var_location call might still be
24060    before first real instruction at address equal to .Ltext0.  */
24061 static bool maybe_at_text_label_p = true;
24062 /* One above highest N where .LVLN label might be equal to .Ltext0 label.  */
24063 static unsigned int first_loclabel_num_not_at_text_label;
24064
24065 /* Called by the final INSN scan whenever we see a var location.  We
24066    use it to drop labels in the right places, and throw the location in
24067    our lookup table.  */
24068
24069 static void
24070 dwarf2out_var_location (rtx_insn *loc_note)
24071 {
24072   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
24073   struct var_loc_node *newloc;
24074   rtx_insn *next_real, *next_note;
24075   rtx_insn *call_insn = NULL;
24076   static const char *last_label;
24077   static const char *last_postcall_label;
24078   static bool last_in_cold_section_p;
24079   static rtx_insn *expected_next_loc_note;
24080   tree decl;
24081   bool var_loc_p;
24082
24083   if (!NOTE_P (loc_note))
24084     {
24085       if (CALL_P (loc_note))
24086         {
24087           call_site_count++;
24088           if (SIBLING_CALL_P (loc_note))
24089             tail_call_site_count++;
24090           if (optimize == 0 && !flag_var_tracking)
24091             {
24092               /* When the var-tracking pass is not running, there is no note
24093                  for indirect calls whose target is compile-time known. In this
24094                  case, process such calls specifically so that we generate call
24095                  sites for them anyway.  */
24096               rtx x = PATTERN (loc_note);
24097               if (GET_CODE (x) == PARALLEL)
24098                 x = XVECEXP (x, 0, 0);
24099               if (GET_CODE (x) == SET)
24100                 x = SET_SRC (x);
24101               if (GET_CODE (x) == CALL)
24102                 x = XEXP (x, 0);
24103               if (!MEM_P (x)
24104                   || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
24105                   || !SYMBOL_REF_DECL (XEXP (x, 0))
24106                   || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
24107                       != FUNCTION_DECL))
24108                 {
24109                   call_insn = loc_note;
24110                   loc_note = NULL;
24111                   var_loc_p = false;
24112
24113                   next_real = next_real_insn (call_insn);
24114                   next_note = NULL;
24115                   cached_next_real_insn = NULL;
24116                   goto create_label;
24117                 }
24118             }
24119         }
24120       return;
24121     }
24122
24123   var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
24124   if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
24125     return;
24126
24127   /* Optimize processing a large consecutive sequence of location
24128      notes so we don't spend too much time in next_real_insn.  If the
24129      next insn is another location note, remember the next_real_insn
24130      calculation for next time.  */
24131   next_real = cached_next_real_insn;
24132   if (next_real)
24133     {
24134       if (expected_next_loc_note != loc_note)
24135         next_real = NULL;
24136     }
24137
24138   next_note = NEXT_INSN (loc_note);
24139   if (! next_note
24140       || next_note->deleted ()
24141       || ! NOTE_P (next_note)
24142       || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
24143           && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
24144     next_note = NULL;
24145
24146   if (! next_real)
24147     next_real = next_real_insn (loc_note);
24148
24149   if (next_note)
24150     {
24151       expected_next_loc_note = next_note;
24152       cached_next_real_insn = next_real;
24153     }
24154   else
24155     cached_next_real_insn = NULL;
24156
24157   /* If there are no instructions which would be affected by this note,
24158      don't do anything.  */
24159   if (var_loc_p
24160       && next_real == NULL_RTX
24161       && !NOTE_DURING_CALL_P (loc_note))
24162     return;
24163
24164 create_label:
24165
24166   if (next_real == NULL_RTX)
24167     next_real = get_last_insn ();
24168
24169   /* If there were any real insns between note we processed last time
24170      and this note (or if it is the first note), clear
24171      last_{,postcall_}label so that they are not reused this time.  */
24172   if (last_var_location_insn == NULL_RTX
24173       || last_var_location_insn != next_real
24174       || last_in_cold_section_p != in_cold_section_p)
24175     {
24176       last_label = NULL;
24177       last_postcall_label = NULL;
24178     }
24179
24180   if (var_loc_p)
24181     {
24182       decl = NOTE_VAR_LOCATION_DECL (loc_note);
24183       newloc = add_var_loc_to_decl (decl, loc_note,
24184                                     NOTE_DURING_CALL_P (loc_note)
24185                                     ? last_postcall_label : last_label);
24186       if (newloc == NULL)
24187         return;
24188     }
24189   else
24190     {
24191       decl = NULL_TREE;
24192       newloc = NULL;
24193     }
24194
24195   /* If there were no real insns between note we processed last time
24196      and this note, use the label we emitted last time.  Otherwise
24197      create a new label and emit it.  */
24198   if (last_label == NULL)
24199     {
24200       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
24201       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
24202       loclabel_num++;
24203       last_label = ggc_strdup (loclabel);
24204       /* See if loclabel might be equal to .Ltext0.  If yes,
24205          bump first_loclabel_num_not_at_text_label.  */
24206       if (!have_multiple_function_sections
24207           && in_first_function_p
24208           && maybe_at_text_label_p)
24209         {
24210           static rtx_insn *last_start;
24211           rtx_insn *insn;
24212           for (insn = loc_note; insn; insn = previous_insn (insn))
24213             if (insn == last_start)
24214               break;
24215             else if (!NONDEBUG_INSN_P (insn))
24216               continue;
24217             else
24218               {
24219                 rtx body = PATTERN (insn);
24220                 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
24221                   continue;
24222                 /* Inline asm could occupy zero bytes.  */
24223                 else if (GET_CODE (body) == ASM_INPUT
24224                          || asm_noperands (body) >= 0)
24225                   continue;
24226 #ifdef HAVE_attr_length
24227                 else if (get_attr_min_length (insn) == 0)
24228                   continue;
24229 #endif
24230                 else
24231                   {
24232                     /* Assume insn has non-zero length.  */
24233                     maybe_at_text_label_p = false;
24234                     break;
24235                   }
24236               }
24237           if (maybe_at_text_label_p)
24238             {
24239               last_start = loc_note;
24240               first_loclabel_num_not_at_text_label = loclabel_num;
24241             }
24242         }
24243     }
24244
24245   gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
24246               || (loc_note != NULL_RTX && call_insn == NULL_RTX));
24247
24248   if (!var_loc_p)
24249     {
24250       struct call_arg_loc_node *ca_loc
24251         = ggc_cleared_alloc<call_arg_loc_node> ();
24252       rtx_insn *prev
24253         = loc_note != NULL_RTX ? prev_real_insn (loc_note) : call_insn;
24254
24255       ca_loc->call_arg_loc_note = loc_note;
24256       ca_loc->next = NULL;
24257       ca_loc->label = last_label;
24258       gcc_assert (prev
24259                   && (CALL_P (prev)
24260                       || (NONJUMP_INSN_P (prev)
24261                           && GET_CODE (PATTERN (prev)) == SEQUENCE
24262                           && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
24263       if (!CALL_P (prev))
24264         prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
24265       ca_loc->tail_call_p = SIBLING_CALL_P (prev);
24266
24267       /* Look for a SYMBOL_REF in the "prev" instruction.  */
24268       rtx x = get_call_rtx_from (PATTERN (prev));
24269       if (x)
24270         {
24271           /* Try to get the call symbol, if any.  */
24272           if (MEM_P (XEXP (x, 0)))
24273             x = XEXP (x, 0);
24274           /* First, look for a memory access to a symbol_ref.  */
24275           if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
24276               && SYMBOL_REF_DECL (XEXP (x, 0))
24277               && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
24278             ca_loc->symbol_ref = XEXP (x, 0);
24279           /* Otherwise, look at a compile-time known user-level function
24280              declaration.  */
24281           else if (MEM_P (x)
24282                    && MEM_EXPR (x)
24283                    && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
24284             ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
24285         }
24286
24287       ca_loc->block = insn_scope (prev);
24288       if (call_arg_locations)
24289         call_arg_loc_last->next = ca_loc;
24290       else
24291         call_arg_locations = ca_loc;
24292       call_arg_loc_last = ca_loc;
24293     }
24294   else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
24295     newloc->label = last_label;
24296   else
24297     {
24298       if (!last_postcall_label)
24299         {
24300           sprintf (loclabel, "%s-1", last_label);
24301           last_postcall_label = ggc_strdup (loclabel);
24302         }
24303       newloc->label = last_postcall_label;
24304     }
24305
24306   last_var_location_insn = next_real;
24307   last_in_cold_section_p = in_cold_section_p;
24308 }
24309
24310 /* Called from finalize_size_functions for size functions so that their body
24311    can be encoded in the debug info to describe the layout of variable-length
24312    structures.  */
24313
24314 static void
24315 dwarf2out_size_function (tree decl)
24316 {
24317   function_to_dwarf_procedure (decl);
24318 }
24319
24320 /* Note in one location list that text section has changed.  */
24321
24322 int
24323 var_location_switch_text_section_1 (var_loc_list **slot, void *)
24324 {
24325   var_loc_list *list = *slot;
24326   if (list->first)
24327     list->last_before_switch
24328       = list->last->next ? list->last->next : list->last;
24329   return 1;
24330 }
24331
24332 /* Note in all location lists that text section has changed.  */
24333
24334 static void
24335 var_location_switch_text_section (void)
24336 {
24337   if (decl_loc_table == NULL)
24338     return;
24339
24340   decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
24341 }
24342
24343 /* Create a new line number table.  */
24344
24345 static dw_line_info_table *
24346 new_line_info_table (void)
24347 {
24348   dw_line_info_table *table;
24349
24350   table = ggc_cleared_alloc<dw_line_info_table> ();
24351   table->file_num = 1;
24352   table->line_num = 1;
24353   table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
24354
24355   return table;
24356 }
24357
24358 /* Lookup the "current" table into which we emit line info, so
24359    that we don't have to do it for every source line.  */
24360
24361 static void
24362 set_cur_line_info_table (section *sec)
24363 {
24364   dw_line_info_table *table;
24365
24366   if (sec == text_section)
24367     table = text_section_line_info;
24368   else if (sec == cold_text_section)
24369     {
24370       table = cold_text_section_line_info;
24371       if (!table)
24372         {
24373           cold_text_section_line_info = table = new_line_info_table ();
24374           table->end_label = cold_end_label;
24375         }
24376     }
24377   else
24378     {
24379       const char *end_label;
24380
24381       if (flag_reorder_blocks_and_partition)
24382         {
24383           if (in_cold_section_p)
24384             end_label = crtl->subsections.cold_section_end_label;
24385           else
24386             end_label = crtl->subsections.hot_section_end_label;
24387         }
24388       else
24389         {
24390           char label[MAX_ARTIFICIAL_LABEL_BYTES];
24391           ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
24392                                        current_function_funcdef_no);
24393           end_label = ggc_strdup (label);
24394         }
24395
24396       table = new_line_info_table ();
24397       table->end_label = end_label;
24398
24399       vec_safe_push (separate_line_info, table);
24400     }
24401
24402   if (DWARF2_ASM_LINE_DEBUG_INFO)
24403     table->is_stmt = (cur_line_info_table
24404                       ? cur_line_info_table->is_stmt
24405                       : DWARF_LINE_DEFAULT_IS_STMT_START);
24406   cur_line_info_table = table;
24407 }
24408
24409
24410 /* We need to reset the locations at the beginning of each
24411    function. We can't do this in the end_function hook, because the
24412    declarations that use the locations won't have been output when
24413    that hook is called.  Also compute have_multiple_function_sections here.  */
24414
24415 static void
24416 dwarf2out_begin_function (tree fun)
24417 {
24418   section *sec = function_section (fun);
24419
24420   if (sec != text_section)
24421     have_multiple_function_sections = true;
24422
24423   if (flag_reorder_blocks_and_partition && !cold_text_section)
24424     {
24425       gcc_assert (current_function_decl == fun);
24426       cold_text_section = unlikely_text_section ();
24427       switch_to_section (cold_text_section);
24428       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
24429       switch_to_section (sec);
24430     }
24431
24432   dwarf2out_note_section_used ();
24433   call_site_count = 0;
24434   tail_call_site_count = 0;
24435
24436   set_cur_line_info_table (sec);
24437 }
24438
24439 /* Helper function of dwarf2out_end_function, called only after emitting
24440    the very first function into assembly.  Check if some .debug_loc range
24441    might end with a .LVL* label that could be equal to .Ltext0.
24442    In that case we must force using absolute addresses in .debug_loc ranges,
24443    because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
24444    .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
24445    list terminator.
24446    Set have_multiple_function_sections to true in that case and
24447    terminate htab traversal.  */
24448
24449 int
24450 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
24451 {
24452   var_loc_list *entry = *slot;
24453   struct var_loc_node *node;
24454
24455   node = entry->first;
24456   if (node && node->next && node->next->label)
24457     {
24458       unsigned int i;
24459       const char *label = node->next->label;
24460       char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
24461
24462       for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
24463         {
24464           ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
24465           if (strcmp (label, loclabel) == 0)
24466             {
24467               have_multiple_function_sections = true;
24468               return 0;
24469             }
24470         }
24471     }
24472   return 1;
24473 }
24474
24475 /* Hook called after emitting a function into assembly.
24476    This does something only for the very first function emitted.  */
24477
24478 static void
24479 dwarf2out_end_function (unsigned int)
24480 {
24481   if (in_first_function_p
24482       && !have_multiple_function_sections
24483       && first_loclabel_num_not_at_text_label
24484       && decl_loc_table)
24485     decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
24486   in_first_function_p = false;
24487   maybe_at_text_label_p = false;
24488 }
24489
24490 /* Temporary holder for dwarf2out_register_main_translation_unit.  Used to let
24491    front-ends register a translation unit even before dwarf2out_init is
24492    called.  */
24493 static tree main_translation_unit = NULL_TREE;
24494
24495 /* Hook called by front-ends after they built their main translation unit.
24496    Associate comp_unit_die to UNIT.  */
24497
24498 static void
24499 dwarf2out_register_main_translation_unit (tree unit)
24500 {
24501   gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
24502               && main_translation_unit == NULL_TREE);
24503   main_translation_unit = unit;
24504   /* If dwarf2out_init has not been called yet, it will perform the association
24505      itself looking at main_translation_unit.  */
24506   if (decl_die_table != NULL)
24507     equate_decl_number_to_die (unit, comp_unit_die ());
24508 }
24509
24510 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE.  */
24511
24512 static void
24513 push_dw_line_info_entry (dw_line_info_table *table,
24514                          enum dw_line_info_opcode opcode, unsigned int val)
24515 {
24516   dw_line_info_entry e;
24517   e.opcode = opcode;
24518   e.val = val;
24519   vec_safe_push (table->entries, e);
24520 }
24521
24522 /* Output a label to mark the beginning of a source code line entry
24523    and record information relating to this source line, in
24524    'line_info_table' for later output of the .debug_line section.  */
24525 /* ??? The discriminator parameter ought to be unsigned.  */
24526
24527 static void
24528 dwarf2out_source_line (unsigned int line, const char *filename,
24529                        int discriminator, bool is_stmt)
24530 {
24531   unsigned int file_num;
24532   dw_line_info_table *table;
24533
24534   if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
24535     return;
24536
24537   /* The discriminator column was added in dwarf4.  Simplify the below
24538      by simply removing it if we're not supposed to output it.  */
24539   if (dwarf_version < 4 && dwarf_strict)
24540     discriminator = 0;
24541
24542   table = cur_line_info_table;
24543   file_num = maybe_emit_file (lookup_filename (filename));
24544
24545   /* ??? TODO: Elide duplicate line number entries.  Traditionally,
24546      the debugger has used the second (possibly duplicate) line number
24547      at the beginning of the function to mark the end of the prologue.
24548      We could eliminate any other duplicates within the function.  For
24549      Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
24550      that second line number entry.  */
24551   /* Recall that this end-of-prologue indication is *not* the same thing
24552      as the end_prologue debug hook.  The NOTE_INSN_PROLOGUE_END note,
24553      to which the hook corresponds, follows the last insn that was 
24554      emitted by gen_prologue.  What we need is to precede the first insn
24555      that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
24556      insn that corresponds to something the user wrote.  These may be
24557      very different locations once scheduling is enabled.  */
24558
24559   if (0 && file_num == table->file_num
24560       && line == table->line_num
24561       && discriminator == table->discrim_num
24562       && is_stmt == table->is_stmt)
24563     return;
24564
24565   switch_to_section (current_function_section ());
24566
24567   /* If requested, emit something human-readable.  */
24568   if (flag_debug_asm)
24569     fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
24570
24571   if (DWARF2_ASM_LINE_DEBUG_INFO)
24572     {
24573       /* Emit the .loc directive understood by GNU as.  */
24574       /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
24575          file_num, line, is_stmt, discriminator */
24576       fputs ("\t.loc ", asm_out_file);
24577       fprint_ul (asm_out_file, file_num);
24578       putc (' ', asm_out_file);
24579       fprint_ul (asm_out_file, line);
24580       putc (' ', asm_out_file);
24581       putc ('0', asm_out_file);
24582
24583       if (is_stmt != table->is_stmt)
24584         {
24585           fputs (" is_stmt ", asm_out_file);
24586           putc (is_stmt ? '1' : '0', asm_out_file);
24587         }
24588       if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
24589         {
24590           gcc_assert (discriminator > 0);
24591           fputs (" discriminator ", asm_out_file);
24592           fprint_ul (asm_out_file, (unsigned long) discriminator);
24593         }
24594       putc ('\n', asm_out_file);
24595     }
24596   else
24597     {
24598       unsigned int label_num = ++line_info_label_num;
24599
24600       targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
24601
24602       push_dw_line_info_entry (table, LI_set_address, label_num);
24603       if (file_num != table->file_num)
24604         push_dw_line_info_entry (table, LI_set_file, file_num);
24605       if (discriminator != table->discrim_num)
24606         push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
24607       if (is_stmt != table->is_stmt)
24608         push_dw_line_info_entry (table, LI_negate_stmt, 0);
24609       push_dw_line_info_entry (table, LI_set_line, line);
24610     }
24611
24612   table->file_num = file_num;
24613   table->line_num = line;
24614   table->discrim_num = discriminator;
24615   table->is_stmt = is_stmt;
24616   table->in_use = true;
24617 }
24618
24619 /* Record the beginning of a new source file.  */
24620
24621 static void
24622 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
24623 {
24624   if (flag_eliminate_dwarf2_dups)
24625     {
24626       /* Record the beginning of the file for break_out_includes.  */
24627       dw_die_ref bincl_die;
24628
24629       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
24630       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
24631     }
24632
24633   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
24634     {
24635       macinfo_entry e;
24636       e.code = DW_MACINFO_start_file;
24637       e.lineno = lineno;
24638       e.info = ggc_strdup (filename);
24639       vec_safe_push (macinfo_table, e);
24640     }
24641 }
24642
24643 /* Record the end of a source file.  */
24644
24645 static void
24646 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
24647 {
24648   if (flag_eliminate_dwarf2_dups)
24649     /* Record the end of the file for break_out_includes.  */
24650     new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
24651
24652   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
24653     {
24654       macinfo_entry e;
24655       e.code = DW_MACINFO_end_file;
24656       e.lineno = lineno;
24657       e.info = NULL;
24658       vec_safe_push (macinfo_table, e);
24659     }
24660 }
24661
24662 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
24663    the tail part of the directive line, i.e. the part which is past the
24664    initial whitespace, #, whitespace, directive-name, whitespace part.  */
24665
24666 static void
24667 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
24668                   const char *buffer ATTRIBUTE_UNUSED)
24669 {
24670   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
24671     {
24672       macinfo_entry e;
24673       /* Insert a dummy first entry to be able to optimize the whole
24674          predefined macro block using DW_MACRO_GNU_transparent_include.  */
24675       if (macinfo_table->is_empty () && lineno <= 1)
24676         {
24677           e.code = 0;
24678           e.lineno = 0;
24679           e.info = NULL;
24680           vec_safe_push (macinfo_table, e);
24681         }
24682       e.code = DW_MACINFO_define;
24683       e.lineno = lineno;
24684       e.info = ggc_strdup (buffer);
24685       vec_safe_push (macinfo_table, e);
24686     }
24687 }
24688
24689 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
24690    the tail part of the directive line, i.e. the part which is past the
24691    initial whitespace, #, whitespace, directive-name, whitespace part.  */
24692
24693 static void
24694 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
24695                  const char *buffer ATTRIBUTE_UNUSED)
24696 {
24697   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
24698     {
24699       macinfo_entry e;
24700       /* Insert a dummy first entry to be able to optimize the whole
24701          predefined macro block using DW_MACRO_GNU_transparent_include.  */
24702       if (macinfo_table->is_empty () && lineno <= 1)
24703         {
24704           e.code = 0;
24705           e.lineno = 0;
24706           e.info = NULL;
24707           vec_safe_push (macinfo_table, e);
24708         }
24709       e.code = DW_MACINFO_undef;
24710       e.lineno = lineno;
24711       e.info = ggc_strdup (buffer);
24712       vec_safe_push (macinfo_table, e);
24713     }
24714 }
24715
24716 /* Helpers to manipulate hash table of CUs.  */
24717
24718 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
24719 {
24720   static inline hashval_t hash (const macinfo_entry *);
24721   static inline bool equal (const macinfo_entry *, const macinfo_entry *);
24722 };
24723
24724 inline hashval_t
24725 macinfo_entry_hasher::hash (const macinfo_entry *entry)
24726 {
24727   return htab_hash_string (entry->info);
24728 }
24729
24730 inline bool
24731 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
24732                              const macinfo_entry *entry2)
24733 {
24734   return !strcmp (entry1->info, entry2->info);
24735 }
24736
24737 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
24738
24739 /* Output a single .debug_macinfo entry.  */
24740
24741 static void
24742 output_macinfo_op (macinfo_entry *ref)
24743 {
24744   int file_num;
24745   size_t len;
24746   struct indirect_string_node *node;
24747   char label[MAX_ARTIFICIAL_LABEL_BYTES];
24748   struct dwarf_file_data *fd;
24749
24750   switch (ref->code)
24751     {
24752     case DW_MACINFO_start_file:
24753       fd = lookup_filename (ref->info);
24754       file_num = maybe_emit_file (fd);
24755       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
24756       dw2_asm_output_data_uleb128 (ref->lineno,
24757                                    "Included from line number %lu", 
24758                                    (unsigned long) ref->lineno);
24759       dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
24760       break;
24761     case DW_MACINFO_end_file:
24762       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
24763       break;
24764     case DW_MACINFO_define:
24765     case DW_MACINFO_undef:
24766       len = strlen (ref->info) + 1;
24767       if (!dwarf_strict
24768           && len > DWARF_OFFSET_SIZE
24769           && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
24770           && (debug_str_section->common.flags & SECTION_MERGE) != 0)
24771         {
24772           ref->code = ref->code == DW_MACINFO_define
24773                       ? DW_MACRO_GNU_define_indirect
24774                       : DW_MACRO_GNU_undef_indirect;
24775           output_macinfo_op (ref);
24776           return;
24777         }
24778       dw2_asm_output_data (1, ref->code,
24779                            ref->code == DW_MACINFO_define
24780                            ? "Define macro" : "Undefine macro");
24781       dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu", 
24782                                    (unsigned long) ref->lineno);
24783       dw2_asm_output_nstring (ref->info, -1, "The macro");
24784       break;
24785     case DW_MACRO_GNU_define_indirect:
24786     case DW_MACRO_GNU_undef_indirect:
24787       node = find_AT_string (ref->info);
24788       gcc_assert (node
24789                   && ((node->form == DW_FORM_strp)
24790                       || (node->form == DW_FORM_GNU_str_index)));
24791       dw2_asm_output_data (1, ref->code,
24792                            ref->code == DW_MACRO_GNU_define_indirect
24793                            ? "Define macro indirect"
24794                            : "Undefine macro indirect");
24795       dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
24796                                    (unsigned long) ref->lineno);
24797       if (node->form == DW_FORM_strp)
24798         dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
24799                                debug_str_section, "The macro: \"%s\"",
24800                                ref->info);
24801       else
24802         dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
24803                                      ref->info);
24804       break;
24805     case DW_MACRO_GNU_transparent_include:
24806       dw2_asm_output_data (1, ref->code, "Transparent include");
24807       ASM_GENERATE_INTERNAL_LABEL (label,
24808                                    DEBUG_MACRO_SECTION_LABEL, ref->lineno);
24809       dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
24810       break;
24811     default:
24812       fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
24813                ASM_COMMENT_START, (unsigned long) ref->code);
24814       break;
24815     }
24816 }
24817
24818 /* Attempt to make a sequence of define/undef macinfo ops shareable with
24819    other compilation unit .debug_macinfo sections.  IDX is the first
24820    index of a define/undef, return the number of ops that should be
24821    emitted in a comdat .debug_macinfo section and emit
24822    a DW_MACRO_GNU_transparent_include entry referencing it.
24823    If the define/undef entry should be emitted normally, return 0.  */
24824
24825 static unsigned
24826 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
24827                         macinfo_hash_type **macinfo_htab)
24828 {
24829   macinfo_entry *first, *second, *cur, *inc;
24830   char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
24831   unsigned char checksum[16];
24832   struct md5_ctx ctx;
24833   char *grp_name, *tail;
24834   const char *base;
24835   unsigned int i, count, encoded_filename_len, linebuf_len;
24836   macinfo_entry **slot;
24837
24838   first = &(*macinfo_table)[idx];
24839   second = &(*macinfo_table)[idx + 1];
24840
24841   /* Optimize only if there are at least two consecutive define/undef ops,
24842      and either all of them are before first DW_MACINFO_start_file
24843      with lineno {0,1} (i.e. predefined macro block), or all of them are
24844      in some included header file.  */
24845   if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
24846     return 0;
24847   if (vec_safe_is_empty (files))
24848     {
24849       if (first->lineno > 1 || second->lineno > 1)
24850         return 0;
24851     }
24852   else if (first->lineno == 0)
24853     return 0;
24854
24855   /* Find the last define/undef entry that can be grouped together
24856      with first and at the same time compute md5 checksum of their
24857      codes, linenumbers and strings.  */
24858   md5_init_ctx (&ctx);
24859   for (i = idx; macinfo_table->iterate (i, &cur); i++)
24860     if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
24861       break;
24862     else if (vec_safe_is_empty (files) && cur->lineno > 1)
24863       break;
24864     else
24865       {
24866         unsigned char code = cur->code;
24867         md5_process_bytes (&code, 1, &ctx);
24868         checksum_uleb128 (cur->lineno, &ctx);
24869         md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
24870       }
24871   md5_finish_ctx (&ctx, checksum);
24872   count = i - idx;
24873
24874   /* From the containing include filename (if any) pick up just
24875      usable characters from its basename.  */
24876   if (vec_safe_is_empty (files))
24877     base = "";
24878   else
24879     base = lbasename (files->last ().info);
24880   for (encoded_filename_len = 0, i = 0; base[i]; i++)
24881     if (ISIDNUM (base[i]) || base[i] == '.')
24882       encoded_filename_len++;
24883   /* Count . at the end.  */
24884   if (encoded_filename_len)
24885     encoded_filename_len++;
24886
24887   sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
24888   linebuf_len = strlen (linebuf);
24889
24890   /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum>  */
24891   grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
24892                          + 16 * 2 + 1);
24893   memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
24894   tail = grp_name + 4;
24895   if (encoded_filename_len)
24896     {
24897       for (i = 0; base[i]; i++)
24898         if (ISIDNUM (base[i]) || base[i] == '.')
24899           *tail++ = base[i];
24900       *tail++ = '.';
24901     }
24902   memcpy (tail, linebuf, linebuf_len);
24903   tail += linebuf_len;
24904   *tail++ = '.';
24905   for (i = 0; i < 16; i++)
24906     sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
24907
24908   /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
24909      in the empty vector entry before the first define/undef.  */
24910   inc = &(*macinfo_table)[idx - 1];
24911   inc->code = DW_MACRO_GNU_transparent_include;
24912   inc->lineno = 0;
24913   inc->info = ggc_strdup (grp_name);
24914   if (!*macinfo_htab)
24915     *macinfo_htab = new macinfo_hash_type (10);
24916   /* Avoid emitting duplicates.  */
24917   slot = (*macinfo_htab)->find_slot (inc, INSERT);
24918   if (*slot != NULL)
24919     {
24920       inc->code = 0;
24921       inc->info = NULL;
24922       /* If such an entry has been used before, just emit
24923          a DW_MACRO_GNU_transparent_include op.  */
24924       inc = *slot;
24925       output_macinfo_op (inc);
24926       /* And clear all macinfo_entry in the range to avoid emitting them
24927          in the second pass.  */
24928       for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
24929         {
24930           cur->code = 0;
24931           cur->info = NULL;
24932         }
24933     }
24934   else
24935     {
24936       *slot = inc;
24937       inc->lineno = (*macinfo_htab)->elements ();
24938       output_macinfo_op (inc);
24939     }
24940   return count;
24941 }
24942
24943 /* Save any strings needed by the macinfo table in the debug str
24944    table.  All strings must be collected into the table by the time
24945    index_string is called.  */
24946
24947 static void
24948 save_macinfo_strings (void)
24949 {
24950   unsigned len;
24951   unsigned i;
24952   macinfo_entry *ref;
24953
24954   for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
24955     {
24956       switch (ref->code)
24957         {
24958           /* Match the logic in output_macinfo_op to decide on
24959              indirect strings.  */
24960           case DW_MACINFO_define:
24961           case DW_MACINFO_undef:
24962             len = strlen (ref->info) + 1;
24963             if (!dwarf_strict
24964                 && len > DWARF_OFFSET_SIZE
24965                 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
24966                 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
24967               set_indirect_string (find_AT_string (ref->info));
24968             break;
24969           case DW_MACRO_GNU_define_indirect:
24970           case DW_MACRO_GNU_undef_indirect:
24971             set_indirect_string (find_AT_string (ref->info));
24972             break;
24973           default:
24974             break;
24975         }
24976     }
24977 }
24978
24979 /* Output macinfo section(s).  */
24980
24981 static void
24982 output_macinfo (void)
24983 {
24984   unsigned i;
24985   unsigned long length = vec_safe_length (macinfo_table);
24986   macinfo_entry *ref;
24987   vec<macinfo_entry, va_gc> *files = NULL;
24988   macinfo_hash_type *macinfo_htab = NULL;
24989
24990   if (! length)
24991     return;
24992
24993   /* output_macinfo* uses these interchangeably.  */
24994   gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
24995               && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
24996               && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
24997               && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
24998
24999   /* For .debug_macro emit the section header.  */
25000   if (!dwarf_strict)
25001     {
25002       dw2_asm_output_data (2, 4, "DWARF macro version number");
25003       if (DWARF_OFFSET_SIZE == 8)
25004         dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
25005       else
25006         dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
25007       dw2_asm_output_offset (DWARF_OFFSET_SIZE,
25008                              (!dwarf_split_debug_info ? debug_line_section_label
25009                               : debug_skeleton_line_section_label),
25010                              debug_line_section, NULL);
25011     }
25012
25013   /* In the first loop, it emits the primary .debug_macinfo section
25014      and after each emitted op the macinfo_entry is cleared.
25015      If a longer range of define/undef ops can be optimized using
25016      DW_MACRO_GNU_transparent_include, the
25017      DW_MACRO_GNU_transparent_include op is emitted and kept in
25018      the vector before the first define/undef in the range and the
25019      whole range of define/undef ops is not emitted and kept.  */
25020   for (i = 0; macinfo_table->iterate (i, &ref); i++)
25021     {
25022       switch (ref->code)
25023         {
25024         case DW_MACINFO_start_file:
25025           vec_safe_push (files, *ref);
25026           break;
25027         case DW_MACINFO_end_file:
25028           if (!vec_safe_is_empty (files))
25029             files->pop ();
25030           break;
25031         case DW_MACINFO_define:
25032         case DW_MACINFO_undef:
25033           if (!dwarf_strict
25034               && HAVE_COMDAT_GROUP
25035               && vec_safe_length (files) != 1
25036               && i > 0
25037               && i + 1 < length
25038               && (*macinfo_table)[i - 1].code == 0)
25039             {
25040               unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
25041               if (count)
25042                 {
25043                   i += count - 1;
25044                   continue;
25045                 }
25046             }
25047           break;
25048         case 0:
25049           /* A dummy entry may be inserted at the beginning to be able
25050              to optimize the whole block of predefined macros.  */
25051           if (i == 0)
25052             continue;
25053         default:
25054           break;
25055         }
25056       output_macinfo_op (ref);
25057       ref->info = NULL;
25058       ref->code = 0;
25059     }
25060
25061   if (!macinfo_htab)
25062     return;
25063
25064   delete macinfo_htab;
25065   macinfo_htab = NULL;
25066
25067   /* If any DW_MACRO_GNU_transparent_include were used, on those
25068      DW_MACRO_GNU_transparent_include entries terminate the
25069      current chain and switch to a new comdat .debug_macinfo
25070      section and emit the define/undef entries within it.  */
25071   for (i = 0; macinfo_table->iterate (i, &ref); i++)
25072     switch (ref->code)
25073       {
25074       case 0:
25075         continue;
25076       case DW_MACRO_GNU_transparent_include:
25077         {
25078           char label[MAX_ARTIFICIAL_LABEL_BYTES];
25079           tree comdat_key = get_identifier (ref->info);
25080           /* Terminate the previous .debug_macinfo section.  */
25081           dw2_asm_output_data (1, 0, "End compilation unit");
25082           targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
25083                                          SECTION_DEBUG
25084                                          | SECTION_LINKONCE,
25085                                          comdat_key);
25086           ASM_GENERATE_INTERNAL_LABEL (label,
25087                                        DEBUG_MACRO_SECTION_LABEL,
25088                                        ref->lineno);
25089           ASM_OUTPUT_LABEL (asm_out_file, label);
25090           ref->code = 0;
25091           ref->info = NULL;
25092           dw2_asm_output_data (2, 4, "DWARF macro version number");
25093           if (DWARF_OFFSET_SIZE == 8)
25094             dw2_asm_output_data (1, 1, "Flags: 64-bit");
25095           else
25096             dw2_asm_output_data (1, 0, "Flags: 32-bit");
25097         }
25098         break;
25099       case DW_MACINFO_define:
25100       case DW_MACINFO_undef:
25101         output_macinfo_op (ref);
25102         ref->code = 0;
25103         ref->info = NULL;
25104         break;
25105       default:
25106         gcc_unreachable ();
25107       }
25108 }
25109
25110 /* Set up for Dwarf output at the start of compilation.  */
25111
25112 static void
25113 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
25114 {
25115   /* This option is currently broken, see (PR53118 and PR46102).  */
25116   if (flag_eliminate_dwarf2_dups
25117       && strstr (lang_hooks.name, "C++"))
25118     {
25119       warning (0, "-feliminate-dwarf2-dups is broken for C++, ignoring");
25120       flag_eliminate_dwarf2_dups = 0;
25121     }
25122
25123   /* Allocate the file_table.  */
25124   file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
25125
25126 #ifndef DWARF2_LINENO_DEBUGGING_INFO
25127   /* Allocate the decl_die_table.  */
25128   decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
25129
25130   /* Allocate the decl_loc_table.  */
25131   decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
25132
25133   /* Allocate the cached_dw_loc_list_table.  */
25134   cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
25135
25136   /* Allocate the initial hunk of the decl_scope_table.  */
25137   vec_alloc (decl_scope_table, 256);
25138
25139   /* Allocate the initial hunk of the abbrev_die_table.  */
25140   abbrev_die_table = ggc_cleared_vec_alloc<dw_die_ref>
25141     (ABBREV_DIE_TABLE_INCREMENT);
25142   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
25143   /* Zero-th entry is allocated, but unused.  */
25144   abbrev_die_table_in_use = 1;
25145
25146   /* Allocate the dwarf_proc_stack_usage_map.  */
25147   dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
25148
25149   /* Allocate the pubtypes and pubnames vectors.  */
25150   vec_alloc (pubname_table, 32);
25151   vec_alloc (pubtype_table, 32);
25152
25153   vec_alloc (incomplete_types, 64);
25154
25155   vec_alloc (used_rtx_array, 32);
25156
25157   if (!dwarf_split_debug_info)
25158     {
25159       debug_info_section = get_section (DEBUG_INFO_SECTION,
25160                                         SECTION_DEBUG, NULL);
25161       debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
25162                                           SECTION_DEBUG, NULL);
25163       debug_loc_section = get_section (DEBUG_LOC_SECTION,
25164                                        SECTION_DEBUG, NULL);
25165     }
25166   else
25167     {
25168       debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
25169                                         SECTION_DEBUG | SECTION_EXCLUDE, NULL);
25170       debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
25171                                           SECTION_DEBUG | SECTION_EXCLUDE,
25172                                           NULL);
25173       debug_addr_section = get_section (DEBUG_ADDR_SECTION,
25174                                         SECTION_DEBUG, NULL);
25175       debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
25176                                                  SECTION_DEBUG, NULL);
25177       debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
25178                                                    SECTION_DEBUG, NULL);
25179       ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
25180                                   DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
25181
25182       /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
25183          the main .o, but the skeleton_line goes into the split off dwo.  */
25184       debug_skeleton_line_section
25185           = get_section (DEBUG_DWO_LINE_SECTION,
25186                          SECTION_DEBUG | SECTION_EXCLUDE, NULL);
25187       ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
25188                                    DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
25189       debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
25190                                                SECTION_DEBUG | SECTION_EXCLUDE,
25191                                                NULL);
25192       ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
25193                                    DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
25194       debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
25195                                        SECTION_DEBUG | SECTION_EXCLUDE, NULL);
25196       debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
25197                                            DEBUG_STR_DWO_SECTION_FLAGS, NULL);
25198     }
25199   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
25200                                        SECTION_DEBUG, NULL);
25201   debug_macinfo_section = get_section (dwarf_strict
25202                                        ? DEBUG_MACINFO_SECTION
25203                                        : DEBUG_MACRO_SECTION,
25204                                        DEBUG_MACRO_SECTION_FLAGS, NULL);
25205   debug_line_section = get_section (DEBUG_LINE_SECTION,
25206                                     SECTION_DEBUG, NULL);
25207   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
25208                                         SECTION_DEBUG, NULL);
25209   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
25210                                         SECTION_DEBUG, NULL);
25211   debug_str_section = get_section (DEBUG_STR_SECTION,
25212                                    DEBUG_STR_SECTION_FLAGS, NULL);
25213   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
25214                                       SECTION_DEBUG, NULL);
25215   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
25216                                      SECTION_DEBUG, NULL);
25217
25218   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
25219   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
25220                                DEBUG_ABBREV_SECTION_LABEL, 0);
25221   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
25222   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
25223                                COLD_TEXT_SECTION_LABEL, 0);
25224   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
25225
25226   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
25227                                DEBUG_INFO_SECTION_LABEL, 0);
25228   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
25229                                DEBUG_LINE_SECTION_LABEL, 0);
25230   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
25231                                DEBUG_RANGES_SECTION_LABEL, 0);
25232   ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
25233                                DEBUG_ADDR_SECTION_LABEL, 0);
25234   ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
25235                                dwarf_strict
25236                                ? DEBUG_MACINFO_SECTION_LABEL
25237                                : DEBUG_MACRO_SECTION_LABEL, 0);
25238   ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
25239
25240   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
25241     vec_alloc (macinfo_table, 64);
25242
25243   switch_to_section (text_section);
25244   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
25245 #endif
25246
25247   /* Make sure the line number table for .text always exists.  */
25248   text_section_line_info = new_line_info_table ();
25249   text_section_line_info->end_label = text_end_label;
25250
25251 #ifdef DWARF2_LINENO_DEBUGGING_INFO
25252   cur_line_info_table = text_section_line_info;
25253 #endif
25254
25255   /* If front-ends already registered a main translation unit but we were not
25256      ready to perform the association, do this now.  */
25257   if (main_translation_unit != NULL_TREE)
25258     equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
25259 }
25260
25261 /* Called before compile () starts outputtting functions, variables
25262    and toplevel asms into assembly.  */
25263
25264 static void
25265 dwarf2out_assembly_start (void)
25266 {
25267   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
25268       && dwarf2out_do_cfi_asm ()
25269       && (!(flag_unwind_tables || flag_exceptions)
25270           || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
25271     fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
25272 }
25273
25274 /* A helper function for dwarf2out_finish called through
25275    htab_traverse.  Assign a string its index.  All strings must be
25276    collected into the table by the time index_string is called,
25277    because the indexing code relies on htab_traverse to traverse nodes
25278    in the same order for each run. */
25279
25280 int
25281 index_string (indirect_string_node **h, unsigned int *index)
25282 {
25283   indirect_string_node *node = *h;
25284
25285   find_string_form (node);
25286   if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
25287     {
25288       gcc_assert (node->index == NO_INDEX_ASSIGNED);
25289       node->index = *index;
25290       *index += 1;
25291     }
25292   return 1;
25293 }
25294
25295 /* A helper function for output_indirect_strings called through
25296    htab_traverse.  Output the offset to a string and update the
25297    current offset.  */
25298
25299 int
25300 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
25301 {
25302   indirect_string_node *node = *h;
25303
25304   if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
25305     {
25306       /* Assert that this node has been assigned an index.  */
25307       gcc_assert (node->index != NO_INDEX_ASSIGNED
25308                   && node->index != NOT_INDEXED);
25309       dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
25310                            "indexed string 0x%x: %s", node->index, node->str);
25311       *offset += strlen (node->str) + 1;
25312     }
25313   return 1;
25314 }
25315
25316 /* A helper function for dwarf2out_finish called through
25317    htab_traverse.  Output the indexed string.  */
25318
25319 int
25320 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
25321 {
25322   struct indirect_string_node *node = *h;
25323
25324   if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
25325     {
25326       /* Assert that the strings are output in the same order as their
25327          indexes were assigned.  */
25328       gcc_assert (*cur_idx == node->index);
25329       assemble_string (node->str, strlen (node->str) + 1);
25330       *cur_idx += 1;
25331     }
25332   return 1;
25333 }
25334
25335 /* A helper function for dwarf2out_finish called through
25336    htab_traverse.  Emit one queued .debug_str string.  */
25337
25338 int
25339 output_indirect_string (indirect_string_node **h, void *)
25340 {
25341   struct indirect_string_node *node = *h;
25342
25343   node->form = find_string_form (node);
25344   if (node->form == DW_FORM_strp && node->refcount > 0)
25345     {
25346       ASM_OUTPUT_LABEL (asm_out_file, node->label);
25347       assemble_string (node->str, strlen (node->str) + 1);
25348     }
25349
25350   return 1;
25351 }
25352
25353 /* Output the indexed string table.  */
25354
25355 static void
25356 output_indirect_strings (void)
25357 {
25358   switch_to_section (debug_str_section);
25359   if (!dwarf_split_debug_info)
25360     debug_str_hash->traverse<void *, output_indirect_string> (NULL);
25361   else
25362     {
25363       unsigned int offset = 0;
25364       unsigned int cur_idx = 0;
25365
25366       skeleton_debug_str_hash->traverse<void *, output_indirect_string> (NULL);
25367
25368       switch_to_section (debug_str_offsets_section);
25369       debug_str_hash->traverse_noresize
25370         <unsigned int *, output_index_string_offset> (&offset);
25371       switch_to_section (debug_str_dwo_section);
25372       debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
25373         (&cur_idx);
25374     }
25375 }
25376
25377 /* Callback for htab_traverse to assign an index to an entry in the
25378    table, and to write that entry to the .debug_addr section.  */
25379
25380 int
25381 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
25382 {
25383   addr_table_entry *entry = *slot;
25384
25385   if (entry->refcount == 0)
25386     {
25387       gcc_assert (entry->index == NO_INDEX_ASSIGNED
25388                   || entry->index == NOT_INDEXED);
25389       return 1;
25390     }
25391
25392   gcc_assert (entry->index == *cur_index);
25393   (*cur_index)++;
25394
25395   switch (entry->kind)
25396     {
25397       case ate_kind_rtx:
25398         dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
25399                                  "0x%x", entry->index);
25400         break;
25401       case ate_kind_rtx_dtprel:
25402         gcc_assert (targetm.asm_out.output_dwarf_dtprel);
25403         targetm.asm_out.output_dwarf_dtprel (asm_out_file,
25404                                              DWARF2_ADDR_SIZE,
25405                                              entry->addr.rtl);
25406         fputc ('\n', asm_out_file);
25407         break;
25408       case ate_kind_label:
25409         dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
25410                                  "0x%x", entry->index);
25411         break;
25412       default:
25413         gcc_unreachable ();
25414     }
25415   return 1;
25416 }
25417
25418 /* Produce the .debug_addr section.  */
25419
25420 static void
25421 output_addr_table (void)
25422 {
25423   unsigned int index = 0;
25424   if (addr_index_table == NULL || addr_index_table->size () == 0)
25425     return;
25426
25427   switch_to_section (debug_addr_section);
25428   addr_index_table
25429     ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
25430 }
25431
25432 #if ENABLE_ASSERT_CHECKING
25433 /* Verify that all marks are clear.  */
25434
25435 static void
25436 verify_marks_clear (dw_die_ref die)
25437 {
25438   dw_die_ref c;
25439
25440   gcc_assert (! die->die_mark);
25441   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
25442 }
25443 #endif /* ENABLE_ASSERT_CHECKING */
25444
25445 /* Clear the marks for a die and its children.
25446    Be cool if the mark isn't set.  */
25447
25448 static void
25449 prune_unmark_dies (dw_die_ref die)
25450 {
25451   dw_die_ref c;
25452
25453   if (die->die_mark)
25454     die->die_mark = 0;
25455   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
25456 }
25457
25458 /* Given LOC that is referenced by a DIE we're marking as used, find all
25459    referenced DWARF procedures it references and mark them as used.  */
25460
25461 static void
25462 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
25463 {
25464   for (; loc != NULL; loc = loc->dw_loc_next)
25465     switch (loc->dw_loc_opc)
25466       {
25467       case DW_OP_call2:
25468       case DW_OP_call4:
25469       case DW_OP_call_ref:
25470         prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
25471         break;
25472       default:
25473         break;
25474       }
25475 }
25476
25477 /* Given DIE that we're marking as used, find any other dies
25478    it references as attributes and mark them as used.  */
25479
25480 static void
25481 prune_unused_types_walk_attribs (dw_die_ref die)
25482 {
25483   dw_attr_node *a;
25484   unsigned ix;
25485
25486   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
25487     {
25488       switch (AT_class (a))
25489         {
25490         /* Make sure DWARF procedures referenced by location descriptions will
25491            get emitted.  */
25492         case dw_val_class_loc:
25493           prune_unused_types_walk_loc_descr (AT_loc (a));
25494           break;
25495         case dw_val_class_loc_list:
25496           for (dw_loc_list_ref list = AT_loc_list (a);
25497                list != NULL;
25498                list = list->dw_loc_next)
25499             prune_unused_types_walk_loc_descr (list->expr);
25500           break;
25501
25502         case dw_val_class_die_ref:
25503           /* A reference to another DIE.
25504              Make sure that it will get emitted.
25505              If it was broken out into a comdat group, don't follow it.  */
25506           if (! AT_ref (a)->comdat_type_p
25507               || a->dw_attr == DW_AT_specification)
25508             prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
25509           break;
25510
25511         case dw_val_class_str:
25512           /* Set the string's refcount to 0 so that prune_unused_types_mark
25513              accounts properly for it.  */
25514           a->dw_attr_val.v.val_str->refcount = 0;
25515           break;
25516
25517         default:
25518           break;
25519         }
25520     }
25521 }
25522
25523 /* Mark the generic parameters and arguments children DIEs of DIE.  */
25524
25525 static void
25526 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
25527 {
25528   dw_die_ref c;
25529
25530   if (die == NULL || die->die_child == NULL)
25531     return;
25532   c = die->die_child;
25533   do
25534     {
25535       if (is_template_parameter (c))
25536         prune_unused_types_mark (c, 1);
25537       c = c->die_sib;
25538     } while (c && c != die->die_child);
25539 }
25540
25541 /* Mark DIE as being used.  If DOKIDS is true, then walk down
25542    to DIE's children.  */
25543
25544 static void
25545 prune_unused_types_mark (dw_die_ref die, int dokids)
25546 {
25547   dw_die_ref c;
25548
25549   if (die->die_mark == 0)
25550     {
25551       /* We haven't done this node yet.  Mark it as used.  */
25552       die->die_mark = 1;
25553       /* If this is the DIE of a generic type instantiation,
25554          mark the children DIEs that describe its generic parms and
25555          args.  */
25556       prune_unused_types_mark_generic_parms_dies (die);
25557
25558       /* We also have to mark its parents as used.
25559          (But we don't want to mark our parent's kids due to this,
25560          unless it is a class.)  */
25561       if (die->die_parent)
25562         prune_unused_types_mark (die->die_parent,
25563                                  class_scope_p (die->die_parent));
25564
25565       /* Mark any referenced nodes.  */
25566       prune_unused_types_walk_attribs (die);
25567
25568       /* If this node is a specification,
25569          also mark the definition, if it exists.  */
25570       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
25571         prune_unused_types_mark (die->die_definition, 1);
25572     }
25573
25574   if (dokids && die->die_mark != 2)
25575     {
25576       /* We need to walk the children, but haven't done so yet.
25577          Remember that we've walked the kids.  */
25578       die->die_mark = 2;
25579
25580       /* If this is an array type, we need to make sure our
25581          kids get marked, even if they're types.  If we're
25582          breaking out types into comdat sections, do this
25583          for all type definitions.  */
25584       if (die->die_tag == DW_TAG_array_type
25585           || (use_debug_types
25586               && is_type_die (die) && ! is_declaration_die (die)))
25587         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
25588       else
25589         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
25590     }
25591 }
25592
25593 /* For local classes, look if any static member functions were emitted
25594    and if so, mark them.  */
25595
25596 static void
25597 prune_unused_types_walk_local_classes (dw_die_ref die)
25598 {
25599   dw_die_ref c;
25600
25601   if (die->die_mark == 2)
25602     return;
25603
25604   switch (die->die_tag)
25605     {
25606     case DW_TAG_structure_type:
25607     case DW_TAG_union_type:
25608     case DW_TAG_class_type:
25609       break;
25610
25611     case DW_TAG_subprogram:
25612       if (!get_AT_flag (die, DW_AT_declaration)
25613           || die->die_definition != NULL)
25614         prune_unused_types_mark (die, 1);
25615       return;
25616
25617     default:
25618       return;
25619     }
25620
25621   /* Mark children.  */
25622   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
25623 }
25624
25625 /* Walk the tree DIE and mark types that we actually use.  */
25626
25627 static void
25628 prune_unused_types_walk (dw_die_ref die)
25629 {
25630   dw_die_ref c;
25631
25632   /* Don't do anything if this node is already marked and
25633      children have been marked as well.  */
25634   if (die->die_mark == 2)
25635     return;
25636
25637   switch (die->die_tag)
25638     {
25639     case DW_TAG_structure_type:
25640     case DW_TAG_union_type:
25641     case DW_TAG_class_type:
25642       if (die->die_perennial_p)
25643         break;
25644
25645       for (c = die->die_parent; c; c = c->die_parent)
25646         if (c->die_tag == DW_TAG_subprogram)
25647           break;
25648
25649       /* Finding used static member functions inside of classes
25650          is needed just for local classes, because for other classes
25651          static member function DIEs with DW_AT_specification
25652          are emitted outside of the DW_TAG_*_type.  If we ever change
25653          it, we'd need to call this even for non-local classes.  */
25654       if (c)
25655         prune_unused_types_walk_local_classes (die);
25656
25657       /* It's a type node --- don't mark it.  */
25658       return;
25659
25660     case DW_TAG_const_type:
25661     case DW_TAG_packed_type:
25662     case DW_TAG_pointer_type:
25663     case DW_TAG_reference_type:
25664     case DW_TAG_rvalue_reference_type:
25665     case DW_TAG_volatile_type:
25666     case DW_TAG_typedef:
25667     case DW_TAG_array_type:
25668     case DW_TAG_interface_type:
25669     case DW_TAG_friend:
25670     case DW_TAG_enumeration_type:
25671     case DW_TAG_subroutine_type:
25672     case DW_TAG_string_type:
25673     case DW_TAG_set_type:
25674     case DW_TAG_subrange_type:
25675     case DW_TAG_ptr_to_member_type:
25676     case DW_TAG_file_type:
25677       /* Type nodes are useful only when other DIEs reference them --- don't
25678          mark them.  */
25679       /* FALLTHROUGH */
25680
25681     case DW_TAG_dwarf_procedure:
25682       /* Likewise for DWARF procedures.  */
25683
25684       if (die->die_perennial_p)
25685         break;
25686
25687       return;
25688
25689     default:
25690       /* Mark everything else.  */
25691       break;
25692   }
25693
25694   if (die->die_mark == 0)
25695     {
25696       die->die_mark = 1;
25697
25698       /* Now, mark any dies referenced from here.  */
25699       prune_unused_types_walk_attribs (die);
25700     }
25701
25702   die->die_mark = 2;
25703
25704   /* Mark children.  */
25705   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
25706 }
25707
25708 /* Increment the string counts on strings referred to from DIE's
25709    attributes.  */
25710
25711 static void
25712 prune_unused_types_update_strings (dw_die_ref die)
25713 {
25714   dw_attr_node *a;
25715   unsigned ix;
25716
25717   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
25718     if (AT_class (a) == dw_val_class_str)
25719       {
25720         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
25721         s->refcount++;
25722         /* Avoid unnecessarily putting strings that are used less than
25723            twice in the hash table.  */
25724         if (s->refcount
25725             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
25726           {
25727             indirect_string_node **slot
25728               = debug_str_hash->find_slot_with_hash (s->str,
25729                                                      htab_hash_string (s->str),
25730                                                      INSERT);
25731             gcc_assert (*slot == NULL);
25732             *slot = s;
25733           }
25734       }
25735 }
25736
25737 /* Remove from the tree DIE any dies that aren't marked.  */
25738
25739 static void
25740 prune_unused_types_prune (dw_die_ref die)
25741 {
25742   dw_die_ref c;
25743
25744   gcc_assert (die->die_mark);
25745   prune_unused_types_update_strings (die);
25746
25747   if (! die->die_child)
25748     return;
25749
25750   c = die->die_child;
25751   do {
25752     dw_die_ref prev = c;
25753     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
25754       if (c == die->die_child)
25755         {
25756           /* No marked children between 'prev' and the end of the list.  */
25757           if (prev == c)
25758             /* No marked children at all.  */
25759             die->die_child = NULL;
25760           else
25761             {
25762               prev->die_sib = c->die_sib;
25763               die->die_child = prev;
25764             }
25765           return;
25766         }
25767
25768     if (c != prev->die_sib)
25769       prev->die_sib = c;
25770     prune_unused_types_prune (c);
25771   } while (c != die->die_child);
25772 }
25773
25774 /* Remove dies representing declarations that we never use.  */
25775
25776 static void
25777 prune_unused_types (void)
25778 {
25779   unsigned int i;
25780   limbo_die_node *node;
25781   comdat_type_node *ctnode;
25782   pubname_entry *pub;
25783   dw_die_ref base_type;
25784
25785 #if ENABLE_ASSERT_CHECKING
25786   /* All the marks should already be clear.  */
25787   verify_marks_clear (comp_unit_die ());
25788   for (node = limbo_die_list; node; node = node->next)
25789     verify_marks_clear (node->die);
25790   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
25791     verify_marks_clear (ctnode->root_die);
25792 #endif /* ENABLE_ASSERT_CHECKING */
25793
25794   /* Mark types that are used in global variables.  */
25795   premark_types_used_by_global_vars ();
25796
25797   /* Set the mark on nodes that are actually used.  */
25798   prune_unused_types_walk (comp_unit_die ());
25799   for (node = limbo_die_list; node; node = node->next)
25800     prune_unused_types_walk (node->die);
25801   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
25802     {
25803       prune_unused_types_walk (ctnode->root_die);
25804       prune_unused_types_mark (ctnode->type_die, 1);
25805     }
25806
25807   /* Also set the mark on nodes referenced from the pubname_table.  Enumerators
25808      are unusual in that they are pubnames that are the children of pubtypes.
25809      They should only be marked via their parent DW_TAG_enumeration_type die,
25810      not as roots in themselves.  */
25811   FOR_EACH_VEC_ELT (*pubname_table, i, pub)
25812     if (pub->die->die_tag != DW_TAG_enumerator)
25813       prune_unused_types_mark (pub->die, 1);
25814   for (i = 0; base_types.iterate (i, &base_type); i++)
25815     prune_unused_types_mark (base_type, 1);
25816
25817   if (debug_str_hash)
25818     debug_str_hash->empty ();
25819   if (skeleton_debug_str_hash)
25820     skeleton_debug_str_hash->empty ();
25821   prune_unused_types_prune (comp_unit_die ());
25822   for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
25823     {
25824       node = *pnode;
25825       if (!node->die->die_mark)
25826         *pnode = node->next;
25827       else
25828         {
25829           prune_unused_types_prune (node->die);
25830           pnode = &node->next;
25831         }
25832     }
25833   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
25834     prune_unused_types_prune (ctnode->root_die);
25835
25836   /* Leave the marks clear.  */
25837   prune_unmark_dies (comp_unit_die ());
25838   for (node = limbo_die_list; node; node = node->next)
25839     prune_unmark_dies (node->die);
25840   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
25841     prune_unmark_dies (ctnode->root_die);
25842 }
25843
25844 /* Set the parameter to true if there are any relative pathnames in
25845    the file table.  */
25846 int
25847 file_table_relative_p (dwarf_file_data **slot, bool *p)
25848 {
25849   struct dwarf_file_data *d = *slot;
25850   if (!IS_ABSOLUTE_PATH (d->filename))
25851     {
25852       *p = true;
25853       return 0;
25854     }
25855   return 1;
25856 }
25857
25858 /* Helpers to manipulate hash table of comdat type units.  */
25859
25860 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
25861 {
25862   static inline hashval_t hash (const comdat_type_node *);
25863   static inline bool equal (const comdat_type_node *, const comdat_type_node *);
25864 };
25865
25866 inline hashval_t
25867 comdat_type_hasher::hash (const comdat_type_node *type_node)
25868 {
25869   hashval_t h;
25870   memcpy (&h, type_node->signature, sizeof (h));
25871   return h;
25872 }
25873
25874 inline bool
25875 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
25876                            const comdat_type_node *type_node_2)
25877 {
25878   return (! memcmp (type_node_1->signature, type_node_2->signature,
25879                     DWARF_TYPE_SIGNATURE_SIZE));
25880 }
25881
25882 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
25883    to the location it would have been added, should we know its
25884    DECL_ASSEMBLER_NAME when we added other attributes.  This will
25885    probably improve compactness of debug info, removing equivalent
25886    abbrevs, and hide any differences caused by deferring the
25887    computation of the assembler name, triggered by e.g. PCH.  */
25888
25889 static inline void
25890 move_linkage_attr (dw_die_ref die)
25891 {
25892   unsigned ix = vec_safe_length (die->die_attr);
25893   dw_attr_node linkage = (*die->die_attr)[ix - 1];
25894
25895   gcc_assert (linkage.dw_attr == DW_AT_linkage_name
25896               || linkage.dw_attr == DW_AT_MIPS_linkage_name);
25897
25898   while (--ix > 0)
25899     {
25900       dw_attr_node *prev = &(*die->die_attr)[ix - 1];
25901
25902       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
25903         break;
25904     }
25905
25906   if (ix != vec_safe_length (die->die_attr) - 1)
25907     {
25908       die->die_attr->pop ();
25909       die->die_attr->quick_insert (ix, linkage);
25910     }
25911 }
25912
25913 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
25914    referenced from typed stack ops and count how often they are used.  */
25915
25916 static void
25917 mark_base_types (dw_loc_descr_ref loc)
25918 {
25919   dw_die_ref base_type = NULL;
25920
25921   for (; loc; loc = loc->dw_loc_next)
25922     {
25923       switch (loc->dw_loc_opc)
25924         {
25925         case DW_OP_GNU_regval_type:
25926         case DW_OP_GNU_deref_type:
25927           base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
25928           break;
25929         case DW_OP_GNU_convert:
25930         case DW_OP_GNU_reinterpret:
25931           if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
25932             continue;
25933           /* FALLTHRU */
25934         case DW_OP_GNU_const_type:
25935           base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
25936           break;
25937         case DW_OP_GNU_entry_value:
25938           mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
25939           continue;
25940         default:
25941           continue;
25942         }
25943       gcc_assert (base_type->die_parent == comp_unit_die ());
25944       if (base_type->die_mark)
25945         base_type->die_mark++;
25946       else
25947         {
25948           base_types.safe_push (base_type);
25949           base_type->die_mark = 1;
25950         }
25951     }
25952 }
25953
25954 /* Comparison function for sorting marked base types.  */
25955
25956 static int
25957 base_type_cmp (const void *x, const void *y)
25958 {
25959   dw_die_ref dx = *(const dw_die_ref *) x;
25960   dw_die_ref dy = *(const dw_die_ref *) y;
25961   unsigned int byte_size1, byte_size2;
25962   unsigned int encoding1, encoding2;
25963   if (dx->die_mark > dy->die_mark)
25964     return -1;
25965   if (dx->die_mark < dy->die_mark)
25966     return 1;
25967   byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
25968   byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
25969   if (byte_size1 < byte_size2)
25970     return 1;
25971   if (byte_size1 > byte_size2)
25972     return -1;
25973   encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
25974   encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
25975   if (encoding1 < encoding2)
25976     return 1;
25977   if (encoding1 > encoding2)
25978     return -1;
25979   return 0;
25980 }
25981
25982 /* Move base types marked by mark_base_types as early as possible
25983    in the CU, sorted by decreasing usage count both to make the
25984    uleb128 references as small as possible and to make sure they
25985    will have die_offset already computed by calc_die_sizes when
25986    sizes of typed stack loc ops is computed.  */
25987
25988 static void
25989 move_marked_base_types (void)
25990 {
25991   unsigned int i;
25992   dw_die_ref base_type, die, c;
25993
25994   if (base_types.is_empty ())
25995     return;
25996
25997   /* Sort by decreasing usage count, they will be added again in that
25998      order later on.  */
25999   base_types.qsort (base_type_cmp);
26000   die = comp_unit_die ();
26001   c = die->die_child;
26002   do
26003     {
26004       dw_die_ref prev = c;
26005       c = c->die_sib;
26006       while (c->die_mark)
26007         {
26008           remove_child_with_prev (c, prev);
26009           /* As base types got marked, there must be at least
26010              one node other than DW_TAG_base_type.  */
26011           gcc_assert (c != c->die_sib);
26012           c = c->die_sib;
26013         }
26014     }
26015   while (c != die->die_child);
26016   gcc_assert (die->die_child);
26017   c = die->die_child;
26018   for (i = 0; base_types.iterate (i, &base_type); i++)
26019     {
26020       base_type->die_mark = 0;
26021       base_type->die_sib = c->die_sib;
26022       c->die_sib = base_type;
26023       c = base_type;
26024     }
26025 }
26026
26027 /* Helper function for resolve_addr, attempt to resolve
26028    one CONST_STRING, return true if successful.  Similarly verify that
26029    SYMBOL_REFs refer to variables emitted in the current CU.  */
26030
26031 static bool
26032 resolve_one_addr (rtx *addr)
26033 {
26034   rtx rtl = *addr;
26035
26036   if (GET_CODE (rtl) == CONST_STRING)
26037     {
26038       size_t len = strlen (XSTR (rtl, 0)) + 1;
26039       tree t = build_string (len, XSTR (rtl, 0));
26040       tree tlen = size_int (len - 1);
26041       TREE_TYPE (t)
26042         = build_array_type (char_type_node, build_index_type (tlen));
26043       rtl = lookup_constant_def (t);
26044       if (!rtl || !MEM_P (rtl))
26045         return false;
26046       rtl = XEXP (rtl, 0);
26047       if (GET_CODE (rtl) == SYMBOL_REF
26048           && SYMBOL_REF_DECL (rtl)
26049           && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
26050         return false;
26051       vec_safe_push (used_rtx_array, rtl);
26052       *addr = rtl;
26053       return true;
26054     }
26055
26056   if (GET_CODE (rtl) == SYMBOL_REF
26057       && SYMBOL_REF_DECL (rtl))
26058     {
26059       if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
26060         {
26061           if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
26062             return false;
26063         }
26064       else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
26065         return false;
26066     }
26067
26068   if (GET_CODE (rtl) == CONST)
26069     {
26070       subrtx_ptr_iterator::array_type array;
26071       FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
26072         if (!resolve_one_addr (*iter))
26073           return false;
26074     }
26075
26076   return true;
26077 }
26078
26079 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
26080    if possible, and create DW_TAG_dwarf_procedure that can be referenced
26081    from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet.  */
26082
26083 static rtx
26084 string_cst_pool_decl (tree t)
26085 {
26086   rtx rtl = output_constant_def (t, 1);
26087   unsigned char *array;
26088   dw_loc_descr_ref l;
26089   tree decl;
26090   size_t len;
26091   dw_die_ref ref;
26092
26093   if (!rtl || !MEM_P (rtl))
26094     return NULL_RTX;
26095   rtl = XEXP (rtl, 0);
26096   if (GET_CODE (rtl) != SYMBOL_REF
26097       || SYMBOL_REF_DECL (rtl) == NULL_TREE)
26098     return NULL_RTX;
26099
26100   decl = SYMBOL_REF_DECL (rtl);
26101   if (!lookup_decl_die (decl))
26102     {
26103       len = TREE_STRING_LENGTH (t);
26104       vec_safe_push (used_rtx_array, rtl);
26105       ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
26106       array = ggc_vec_alloc<unsigned char> (len);
26107       memcpy (array, TREE_STRING_POINTER (t), len);
26108       l = new_loc_descr (DW_OP_implicit_value, len, 0);
26109       l->dw_loc_oprnd2.val_class = dw_val_class_vec;
26110       l->dw_loc_oprnd2.v.val_vec.length = len;
26111       l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
26112       l->dw_loc_oprnd2.v.val_vec.array = array;
26113       add_AT_loc (ref, DW_AT_location, l);
26114       equate_decl_number_to_die (decl, ref);
26115     }
26116   return rtl;
26117 }
26118
26119 /* Helper function of resolve_addr_in_expr.  LOC is
26120    a DW_OP_addr followed by DW_OP_stack_value, either at the start
26121    of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
26122    resolved.  Replace it (both DW_OP_addr and DW_OP_stack_value)
26123    with DW_OP_GNU_implicit_pointer if possible
26124    and return true, if unsuccessful, return false.  */
26125
26126 static bool
26127 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
26128 {
26129   rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
26130   HOST_WIDE_INT offset = 0;
26131   dw_die_ref ref = NULL;
26132   tree decl;
26133
26134   if (GET_CODE (rtl) == CONST
26135       && GET_CODE (XEXP (rtl, 0)) == PLUS
26136       && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
26137     {
26138       offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
26139       rtl = XEXP (XEXP (rtl, 0), 0);
26140     }
26141   if (GET_CODE (rtl) == CONST_STRING)
26142     {
26143       size_t len = strlen (XSTR (rtl, 0)) + 1;
26144       tree t = build_string (len, XSTR (rtl, 0));
26145       tree tlen = size_int (len - 1);
26146
26147       TREE_TYPE (t)
26148         = build_array_type (char_type_node, build_index_type (tlen));
26149       rtl = string_cst_pool_decl (t);
26150       if (!rtl)
26151         return false;
26152     }
26153   if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
26154     {
26155       decl = SYMBOL_REF_DECL (rtl);
26156       if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
26157         {
26158           ref = lookup_decl_die (decl);
26159           if (ref && (get_AT (ref, DW_AT_location)
26160                       || get_AT (ref, DW_AT_const_value)))
26161             {
26162               loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
26163               loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
26164               loc->dw_loc_oprnd1.val_entry = NULL;
26165               loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
26166               loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
26167               loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
26168               loc->dw_loc_oprnd2.v.val_int = offset;
26169               return true;
26170             }
26171         }
26172     }
26173   return false;
26174 }
26175
26176 /* Helper function for resolve_addr, handle one location
26177    expression, return false if at least one CONST_STRING or SYMBOL_REF in
26178    the location list couldn't be resolved.  */
26179
26180 static bool
26181 resolve_addr_in_expr (dw_loc_descr_ref loc)
26182 {
26183   dw_loc_descr_ref keep = NULL;
26184   for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
26185     switch (loc->dw_loc_opc)
26186       {
26187       case DW_OP_addr:
26188         if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
26189           {
26190             if ((prev == NULL
26191                  || prev->dw_loc_opc == DW_OP_piece
26192                  || prev->dw_loc_opc == DW_OP_bit_piece)
26193                 && loc->dw_loc_next
26194                 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
26195                 && !dwarf_strict
26196                 && optimize_one_addr_into_implicit_ptr (loc))
26197               break;
26198             return false;
26199           }
26200         break;
26201       case DW_OP_GNU_addr_index:
26202       case DW_OP_GNU_const_index:
26203         if (loc->dw_loc_opc == DW_OP_GNU_addr_index
26204             || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
26205           {
26206             rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
26207             if (!resolve_one_addr (&rtl))
26208               return false;
26209             remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
26210             loc->dw_loc_oprnd1.val_entry =
26211                 add_addr_table_entry (rtl, ate_kind_rtx);
26212           }
26213         break;
26214       case DW_OP_const4u:
26215       case DW_OP_const8u:
26216         if (loc->dtprel
26217             && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
26218           return false;
26219         break;
26220       case DW_OP_plus_uconst:
26221         if (size_of_loc_descr (loc)
26222             > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
26223               + 1
26224             && loc->dw_loc_oprnd1.v.val_unsigned > 0)
26225           {
26226             dw_loc_descr_ref repl
26227               = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
26228             add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
26229             add_loc_descr (&repl, loc->dw_loc_next);
26230             *loc = *repl;
26231           }
26232         break;
26233       case DW_OP_implicit_value:
26234         if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
26235             && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
26236           return false;
26237         break;
26238       case DW_OP_GNU_implicit_pointer:
26239       case DW_OP_GNU_parameter_ref:
26240         if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
26241           {
26242             dw_die_ref ref
26243               = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
26244             if (ref == NULL)
26245               return false;
26246             loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
26247             loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
26248             loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
26249           }
26250         break;
26251       case DW_OP_GNU_const_type:
26252       case DW_OP_GNU_regval_type:
26253       case DW_OP_GNU_deref_type:
26254       case DW_OP_GNU_convert:
26255       case DW_OP_GNU_reinterpret:
26256         while (loc->dw_loc_next
26257                && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
26258           {
26259             dw_die_ref base1, base2;
26260             unsigned enc1, enc2, size1, size2;
26261             if (loc->dw_loc_opc == DW_OP_GNU_regval_type
26262                 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
26263               base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
26264             else if (loc->dw_loc_oprnd1.val_class
26265                      == dw_val_class_unsigned_const)
26266               break;
26267             else
26268               base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
26269             if (loc->dw_loc_next->dw_loc_oprnd1.val_class
26270                 == dw_val_class_unsigned_const)
26271               break;
26272             base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
26273             gcc_assert (base1->die_tag == DW_TAG_base_type
26274                         && base2->die_tag == DW_TAG_base_type);
26275             enc1 = get_AT_unsigned (base1, DW_AT_encoding);
26276             enc2 = get_AT_unsigned (base2, DW_AT_encoding);
26277             size1 = get_AT_unsigned (base1, DW_AT_byte_size);
26278             size2 = get_AT_unsigned (base2, DW_AT_byte_size);
26279             if (size1 == size2
26280                 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
26281                      && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
26282                      && loc != keep)
26283                     || enc1 == enc2))
26284               {
26285                 /* Optimize away next DW_OP_GNU_convert after
26286                    adjusting LOC's base type die reference.  */
26287                 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
26288                     || loc->dw_loc_opc == DW_OP_GNU_deref_type)
26289                   loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
26290                 else
26291                   loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
26292                 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
26293                 continue;
26294               }
26295             /* Don't change integer DW_OP_GNU_convert after e.g. floating
26296                point typed stack entry.  */
26297             else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
26298               keep = loc->dw_loc_next;
26299             break;
26300           }
26301         break;
26302       default:
26303         break;
26304       }
26305   return true;
26306 }
26307
26308 /* Helper function of resolve_addr.  DIE had DW_AT_location of
26309    DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
26310    and DW_OP_addr couldn't be resolved.  resolve_addr has already
26311    removed the DW_AT_location attribute.  This function attempts to
26312    add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
26313    to it or DW_AT_const_value attribute, if possible.  */
26314
26315 static void
26316 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
26317 {
26318   if (TREE_CODE (decl) != VAR_DECL
26319       || lookup_decl_die (decl) != die
26320       || DECL_EXTERNAL (decl)
26321       || !TREE_STATIC (decl)
26322       || DECL_INITIAL (decl) == NULL_TREE
26323       || DECL_P (DECL_INITIAL (decl))
26324       || get_AT (die, DW_AT_const_value))
26325     return;
26326
26327   tree init = DECL_INITIAL (decl);
26328   HOST_WIDE_INT offset = 0;
26329   /* For variables that have been optimized away and thus
26330      don't have a memory location, see if we can emit
26331      DW_AT_const_value instead.  */
26332   if (tree_add_const_value_attribute (die, init))
26333     return;
26334   if (dwarf_strict)
26335     return;
26336   /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
26337      and ADDR_EXPR refers to a decl that has DW_AT_location or
26338      DW_AT_const_value (but isn't addressable, otherwise
26339      resolving the original DW_OP_addr wouldn't fail), see if
26340      we can add DW_OP_GNU_implicit_pointer.  */
26341   STRIP_NOPS (init);
26342   if (TREE_CODE (init) == POINTER_PLUS_EXPR
26343       && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
26344     {
26345       offset = tree_to_shwi (TREE_OPERAND (init, 1));
26346       init = TREE_OPERAND (init, 0);
26347       STRIP_NOPS (init);
26348     }
26349   if (TREE_CODE (init) != ADDR_EXPR)
26350     return;
26351   if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
26352        && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
26353       || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
26354           && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
26355           && TREE_OPERAND (init, 0) != decl))
26356     {
26357       dw_die_ref ref;
26358       dw_loc_descr_ref l;
26359
26360       if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
26361         {
26362           rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
26363           if (!rtl)
26364             return;
26365           decl = SYMBOL_REF_DECL (rtl);
26366         }
26367       else
26368         decl = TREE_OPERAND (init, 0);
26369       ref = lookup_decl_die (decl);
26370       if (ref == NULL
26371           || (!get_AT (ref, DW_AT_location)
26372               && !get_AT (ref, DW_AT_const_value)))
26373         return;
26374       l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
26375       l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
26376       l->dw_loc_oprnd1.v.val_die_ref.die = ref;
26377       l->dw_loc_oprnd1.v.val_die_ref.external = 0;
26378       add_AT_loc (die, DW_AT_location, l);
26379     }
26380 }
26381
26382 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
26383    an address in .rodata section if the string literal is emitted there,
26384    or remove the containing location list or replace DW_AT_const_value
26385    with DW_AT_location and empty location expression, if it isn't found
26386    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
26387    to something that has been emitted in the current CU.  */
26388
26389 static void
26390 resolve_addr (dw_die_ref die)
26391 {
26392   dw_die_ref c;
26393   dw_attr_node *a;
26394   dw_loc_list_ref *curr, *start, loc;
26395   unsigned ix;
26396
26397   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
26398     switch (AT_class (a))
26399       {
26400       case dw_val_class_loc_list:
26401         start = curr = AT_loc_list_ptr (a);
26402         loc = *curr;
26403         gcc_assert (loc);
26404         /* The same list can be referenced more than once.  See if we have
26405            already recorded the result from a previous pass.  */
26406         if (loc->replaced)
26407           *curr = loc->dw_loc_next;
26408         else if (!loc->resolved_addr)
26409           {
26410             /* As things stand, we do not expect or allow one die to
26411                reference a suffix of another die's location list chain.
26412                References must be identical or completely separate.
26413                There is therefore no need to cache the result of this
26414                pass on any list other than the first; doing so
26415                would lead to unnecessary writes.  */
26416             while (*curr)
26417               {
26418                 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
26419                 if (!resolve_addr_in_expr ((*curr)->expr))
26420                   {
26421                     dw_loc_list_ref next = (*curr)->dw_loc_next;
26422                     dw_loc_descr_ref l = (*curr)->expr;
26423
26424                     if (next && (*curr)->ll_symbol)
26425                       {
26426                         gcc_assert (!next->ll_symbol);
26427                         next->ll_symbol = (*curr)->ll_symbol;
26428                       }
26429                     if (dwarf_split_debug_info)
26430                       remove_loc_list_addr_table_entries (l);
26431                     *curr = next;
26432                   }
26433                 else
26434                   {
26435                     mark_base_types ((*curr)->expr);
26436                     curr = &(*curr)->dw_loc_next;
26437                   }
26438               }
26439             if (loc == *start)
26440               loc->resolved_addr = 1;
26441             else
26442               {
26443                 loc->replaced = 1;
26444                 loc->dw_loc_next = *start;
26445               }
26446           }
26447         if (!*start)
26448           {
26449             remove_AT (die, a->dw_attr);
26450             ix--;
26451           }
26452         break;
26453       case dw_val_class_loc:
26454         {
26455           dw_loc_descr_ref l = AT_loc (a);
26456           /* For -gdwarf-2 don't attempt to optimize
26457              DW_AT_data_member_location containing
26458              DW_OP_plus_uconst - older consumers might
26459              rely on it being that op instead of a more complex,
26460              but shorter, location description.  */
26461           if ((dwarf_version > 2
26462                || a->dw_attr != DW_AT_data_member_location
26463                || l == NULL
26464                || l->dw_loc_opc != DW_OP_plus_uconst
26465                || l->dw_loc_next != NULL)
26466               && !resolve_addr_in_expr (l))
26467             {
26468               if (dwarf_split_debug_info)
26469                 remove_loc_list_addr_table_entries (l);
26470               if (l != NULL
26471                   && l->dw_loc_next == NULL
26472                   && l->dw_loc_opc == DW_OP_addr
26473                   && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
26474                   && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
26475                   && a->dw_attr == DW_AT_location)
26476                 {
26477                   tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
26478                   remove_AT (die, a->dw_attr);
26479                   ix--;
26480                   optimize_location_into_implicit_ptr (die, decl);
26481                   break;
26482                 }
26483               remove_AT (die, a->dw_attr);
26484               ix--;
26485             }
26486           else
26487             mark_base_types (l);
26488         }
26489         break;
26490       case dw_val_class_addr:
26491         if (a->dw_attr == DW_AT_const_value
26492             && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
26493           {
26494             if (AT_index (a) != NOT_INDEXED)
26495               remove_addr_table_entry (a->dw_attr_val.val_entry);
26496             remove_AT (die, a->dw_attr);
26497             ix--;
26498           }
26499         if (die->die_tag == DW_TAG_GNU_call_site
26500             && a->dw_attr == DW_AT_abstract_origin)
26501           {
26502             tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
26503             dw_die_ref tdie = lookup_decl_die (tdecl);
26504             dw_die_ref cdie;
26505             if (tdie == NULL
26506                 && DECL_EXTERNAL (tdecl)
26507                 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
26508                 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
26509               {
26510                 /* Creating a full DIE for tdecl is overly expensive and
26511                    at this point even wrong when in the LTO phase
26512                    as it can end up generating new type DIEs we didn't
26513                    output and thus optimize_external_refs will crash.  */
26514                 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
26515                 add_AT_flag (tdie, DW_AT_external, 1);
26516                 add_AT_flag (tdie, DW_AT_declaration, 1);
26517                 add_linkage_attr (tdie, tdecl);
26518                 add_name_and_src_coords_attributes (tdie, tdecl);
26519                 equate_decl_number_to_die (tdecl, tdie);
26520               }
26521             if (tdie)
26522               {
26523                 a->dw_attr_val.val_class = dw_val_class_die_ref;
26524                 a->dw_attr_val.v.val_die_ref.die = tdie;
26525                 a->dw_attr_val.v.val_die_ref.external = 0;
26526               }
26527             else
26528               {
26529                 if (AT_index (a) != NOT_INDEXED)
26530                   remove_addr_table_entry (a->dw_attr_val.val_entry);
26531                 remove_AT (die, a->dw_attr);
26532                 ix--;
26533               }
26534           }
26535         break;
26536       default:
26537         break;
26538       }
26539
26540   FOR_EACH_CHILD (die, c, resolve_addr (c));
26541 }
26542 \f
26543 /* Helper routines for optimize_location_lists.
26544    This pass tries to share identical local lists in .debug_loc
26545    section.  */
26546
26547 /* Iteratively hash operands of LOC opcode into HSTATE.  */
26548
26549 static void
26550 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
26551 {
26552   dw_val_ref val1 = &loc->dw_loc_oprnd1;
26553   dw_val_ref val2 = &loc->dw_loc_oprnd2;
26554
26555   switch (loc->dw_loc_opc)
26556     {
26557     case DW_OP_const4u:
26558     case DW_OP_const8u:
26559       if (loc->dtprel)
26560         goto hash_addr;
26561       /* FALLTHRU */
26562     case DW_OP_const1u:
26563     case DW_OP_const1s:
26564     case DW_OP_const2u:
26565     case DW_OP_const2s:
26566     case DW_OP_const4s:
26567     case DW_OP_const8s:
26568     case DW_OP_constu:
26569     case DW_OP_consts:
26570     case DW_OP_pick:
26571     case DW_OP_plus_uconst:
26572     case DW_OP_breg0:
26573     case DW_OP_breg1:
26574     case DW_OP_breg2:
26575     case DW_OP_breg3:
26576     case DW_OP_breg4:
26577     case DW_OP_breg5:
26578     case DW_OP_breg6:
26579     case DW_OP_breg7:
26580     case DW_OP_breg8:
26581     case DW_OP_breg9:
26582     case DW_OP_breg10:
26583     case DW_OP_breg11:
26584     case DW_OP_breg12:
26585     case DW_OP_breg13:
26586     case DW_OP_breg14:
26587     case DW_OP_breg15:
26588     case DW_OP_breg16:
26589     case DW_OP_breg17:
26590     case DW_OP_breg18:
26591     case DW_OP_breg19:
26592     case DW_OP_breg20:
26593     case DW_OP_breg21:
26594     case DW_OP_breg22:
26595     case DW_OP_breg23:
26596     case DW_OP_breg24:
26597     case DW_OP_breg25:
26598     case DW_OP_breg26:
26599     case DW_OP_breg27:
26600     case DW_OP_breg28:
26601     case DW_OP_breg29:
26602     case DW_OP_breg30:
26603     case DW_OP_breg31:
26604     case DW_OP_regx:
26605     case DW_OP_fbreg:
26606     case DW_OP_piece:
26607     case DW_OP_deref_size:
26608     case DW_OP_xderef_size:
26609       hstate.add_object (val1->v.val_int);
26610       break;
26611     case DW_OP_skip:
26612     case DW_OP_bra:
26613       {
26614         int offset;
26615
26616         gcc_assert (val1->val_class == dw_val_class_loc);
26617         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
26618         hstate.add_object (offset);
26619       }
26620       break;
26621     case DW_OP_implicit_value:
26622       hstate.add_object (val1->v.val_unsigned);
26623       switch (val2->val_class)
26624         {
26625         case dw_val_class_const:
26626           hstate.add_object (val2->v.val_int);
26627           break;
26628         case dw_val_class_vec:
26629           {
26630             unsigned int elt_size = val2->v.val_vec.elt_size;
26631             unsigned int len = val2->v.val_vec.length;
26632
26633             hstate.add_int (elt_size);
26634             hstate.add_int (len);
26635             hstate.add (val2->v.val_vec.array, len * elt_size);
26636           }
26637           break;
26638         case dw_val_class_const_double:
26639           hstate.add_object (val2->v.val_double.low);
26640           hstate.add_object (val2->v.val_double.high);
26641           break;
26642         case dw_val_class_wide_int:
26643           hstate.add (val2->v.val_wide->get_val (),
26644                       get_full_len (*val2->v.val_wide)
26645                       * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
26646           break;
26647         case dw_val_class_addr: 
26648           inchash::add_rtx (val2->v.val_addr, hstate);
26649           break;
26650         default:
26651           gcc_unreachable ();
26652         }
26653       break;
26654     case DW_OP_bregx:
26655     case DW_OP_bit_piece:
26656       hstate.add_object (val1->v.val_int);
26657       hstate.add_object (val2->v.val_int);
26658       break;
26659     case DW_OP_addr:
26660     hash_addr:
26661       if (loc->dtprel)
26662         {
26663           unsigned char dtprel = 0xd1;
26664           hstate.add_object (dtprel);
26665         }
26666       inchash::add_rtx (val1->v.val_addr, hstate);
26667       break;
26668     case DW_OP_GNU_addr_index:
26669     case DW_OP_GNU_const_index:
26670       {
26671         if (loc->dtprel)
26672           {
26673             unsigned char dtprel = 0xd1;
26674             hstate.add_object (dtprel);
26675           }
26676         inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
26677       }
26678       break;
26679     case DW_OP_GNU_implicit_pointer:
26680       hstate.add_int (val2->v.val_int);
26681       break;
26682     case DW_OP_GNU_entry_value:
26683       hstate.add_object (val1->v.val_loc);
26684       break;
26685     case DW_OP_GNU_regval_type:
26686     case DW_OP_GNU_deref_type:
26687       {
26688         unsigned int byte_size
26689           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
26690         unsigned int encoding
26691           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
26692         hstate.add_object (val1->v.val_int);
26693         hstate.add_object (byte_size);
26694         hstate.add_object (encoding);
26695       }
26696       break;
26697     case DW_OP_GNU_convert:
26698     case DW_OP_GNU_reinterpret:
26699       if (val1->val_class == dw_val_class_unsigned_const)
26700         {
26701           hstate.add_object (val1->v.val_unsigned);
26702           break;
26703         }
26704       /* FALLTHRU */
26705     case DW_OP_GNU_const_type:
26706       {
26707         unsigned int byte_size
26708           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
26709         unsigned int encoding
26710           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
26711         hstate.add_object (byte_size);
26712         hstate.add_object (encoding);
26713         if (loc->dw_loc_opc != DW_OP_GNU_const_type)
26714           break;
26715         hstate.add_object (val2->val_class);
26716         switch (val2->val_class)
26717           {
26718           case dw_val_class_const:
26719             hstate.add_object (val2->v.val_int);
26720             break;
26721           case dw_val_class_vec:
26722             {
26723               unsigned int elt_size = val2->v.val_vec.elt_size;
26724               unsigned int len = val2->v.val_vec.length;
26725
26726               hstate.add_object (elt_size);
26727               hstate.add_object (len);
26728               hstate.add (val2->v.val_vec.array, len * elt_size);
26729             }
26730             break;
26731           case dw_val_class_const_double:
26732             hstate.add_object (val2->v.val_double.low);
26733             hstate.add_object (val2->v.val_double.high);
26734             break;
26735           case dw_val_class_wide_int:
26736             hstate.add (val2->v.val_wide->get_val (),
26737                         get_full_len (*val2->v.val_wide)
26738                         * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
26739             break;
26740           default:
26741             gcc_unreachable ();
26742           }
26743       }
26744       break;
26745
26746     default:
26747       /* Other codes have no operands.  */
26748       break;
26749     }
26750 }
26751
26752 /* Iteratively hash the whole DWARF location expression LOC into HSTATE.  */
26753
26754 static inline void
26755 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
26756 {
26757   dw_loc_descr_ref l;
26758   bool sizes_computed = false;
26759   /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed.  */
26760   size_of_locs (loc);
26761
26762   for (l = loc; l != NULL; l = l->dw_loc_next)
26763     {
26764       enum dwarf_location_atom opc = l->dw_loc_opc;
26765       hstate.add_object (opc);
26766       if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
26767         {
26768           size_of_locs (loc);
26769           sizes_computed = true;
26770         }
26771       hash_loc_operands (l, hstate);
26772     }
26773 }
26774
26775 /* Compute hash of the whole location list LIST_HEAD.  */
26776
26777 static inline void
26778 hash_loc_list (dw_loc_list_ref list_head)
26779 {
26780   dw_loc_list_ref curr = list_head;
26781   inchash::hash hstate;
26782
26783   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
26784     {
26785       hstate.add (curr->begin, strlen (curr->begin) + 1);
26786       hstate.add (curr->end, strlen (curr->end) + 1);
26787       if (curr->section)
26788         hstate.add (curr->section, strlen (curr->section) + 1);
26789       hash_locs (curr->expr, hstate);
26790     }
26791   list_head->hash = hstate.end ();
26792 }
26793
26794 /* Return true if X and Y opcodes have the same operands.  */
26795
26796 static inline bool
26797 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
26798 {
26799   dw_val_ref valx1 = &x->dw_loc_oprnd1;
26800   dw_val_ref valx2 = &x->dw_loc_oprnd2;
26801   dw_val_ref valy1 = &y->dw_loc_oprnd1;
26802   dw_val_ref valy2 = &y->dw_loc_oprnd2;
26803
26804   switch (x->dw_loc_opc)
26805     {
26806     case DW_OP_const4u:
26807     case DW_OP_const8u:
26808       if (x->dtprel)
26809         goto hash_addr;
26810       /* FALLTHRU */
26811     case DW_OP_const1u:
26812     case DW_OP_const1s:
26813     case DW_OP_const2u:
26814     case DW_OP_const2s:
26815     case DW_OP_const4s:
26816     case DW_OP_const8s:
26817     case DW_OP_constu:
26818     case DW_OP_consts:
26819     case DW_OP_pick:
26820     case DW_OP_plus_uconst:
26821     case DW_OP_breg0:
26822     case DW_OP_breg1:
26823     case DW_OP_breg2:
26824     case DW_OP_breg3:
26825     case DW_OP_breg4:
26826     case DW_OP_breg5:
26827     case DW_OP_breg6:
26828     case DW_OP_breg7:
26829     case DW_OP_breg8:
26830     case DW_OP_breg9:
26831     case DW_OP_breg10:
26832     case DW_OP_breg11:
26833     case DW_OP_breg12:
26834     case DW_OP_breg13:
26835     case DW_OP_breg14:
26836     case DW_OP_breg15:
26837     case DW_OP_breg16:
26838     case DW_OP_breg17:
26839     case DW_OP_breg18:
26840     case DW_OP_breg19:
26841     case DW_OP_breg20:
26842     case DW_OP_breg21:
26843     case DW_OP_breg22:
26844     case DW_OP_breg23:
26845     case DW_OP_breg24:
26846     case DW_OP_breg25:
26847     case DW_OP_breg26:
26848     case DW_OP_breg27:
26849     case DW_OP_breg28:
26850     case DW_OP_breg29:
26851     case DW_OP_breg30:
26852     case DW_OP_breg31:
26853     case DW_OP_regx:
26854     case DW_OP_fbreg:
26855     case DW_OP_piece:
26856     case DW_OP_deref_size:
26857     case DW_OP_xderef_size:
26858       return valx1->v.val_int == valy1->v.val_int;
26859     case DW_OP_skip:
26860     case DW_OP_bra:
26861       /* If splitting debug info, the use of DW_OP_GNU_addr_index
26862         can cause irrelevant differences in dw_loc_addr.  */
26863       gcc_assert (valx1->val_class == dw_val_class_loc
26864                   && valy1->val_class == dw_val_class_loc
26865                   && (dwarf_split_debug_info
26866                       || x->dw_loc_addr == y->dw_loc_addr));
26867       return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
26868     case DW_OP_implicit_value:
26869       if (valx1->v.val_unsigned != valy1->v.val_unsigned
26870           || valx2->val_class != valy2->val_class)
26871         return false;
26872       switch (valx2->val_class)
26873         {
26874         case dw_val_class_const:
26875           return valx2->v.val_int == valy2->v.val_int;
26876         case dw_val_class_vec:
26877           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
26878                  && valx2->v.val_vec.length == valy2->v.val_vec.length
26879                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
26880                             valx2->v.val_vec.elt_size
26881                             * valx2->v.val_vec.length) == 0;
26882         case dw_val_class_const_double:
26883           return valx2->v.val_double.low == valy2->v.val_double.low
26884                  && valx2->v.val_double.high == valy2->v.val_double.high;
26885         case dw_val_class_wide_int:
26886           return *valx2->v.val_wide == *valy2->v.val_wide;
26887         case dw_val_class_addr:
26888           return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
26889         default:
26890           gcc_unreachable ();
26891         }
26892     case DW_OP_bregx:
26893     case DW_OP_bit_piece:
26894       return valx1->v.val_int == valy1->v.val_int
26895              && valx2->v.val_int == valy2->v.val_int;
26896     case DW_OP_addr:
26897     hash_addr:
26898       return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
26899     case DW_OP_GNU_addr_index:
26900     case DW_OP_GNU_const_index:
26901       {
26902         rtx ax1 = valx1->val_entry->addr.rtl;
26903         rtx ay1 = valy1->val_entry->addr.rtl;
26904         return rtx_equal_p (ax1, ay1);
26905       }
26906     case DW_OP_GNU_implicit_pointer:
26907       return valx1->val_class == dw_val_class_die_ref
26908              && valx1->val_class == valy1->val_class
26909              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
26910              && valx2->v.val_int == valy2->v.val_int;
26911     case DW_OP_GNU_entry_value:
26912       return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
26913     case DW_OP_GNU_const_type:
26914       if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
26915           || valx2->val_class != valy2->val_class)
26916         return false;
26917       switch (valx2->val_class)
26918         {
26919         case dw_val_class_const:
26920           return valx2->v.val_int == valy2->v.val_int;
26921         case dw_val_class_vec:
26922           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
26923                  && valx2->v.val_vec.length == valy2->v.val_vec.length
26924                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
26925                             valx2->v.val_vec.elt_size
26926                             * valx2->v.val_vec.length) == 0;
26927         case dw_val_class_const_double:
26928           return valx2->v.val_double.low == valy2->v.val_double.low
26929                  && valx2->v.val_double.high == valy2->v.val_double.high;
26930         case dw_val_class_wide_int:
26931           return *valx2->v.val_wide == *valy2->v.val_wide;
26932         default:
26933           gcc_unreachable ();
26934         }
26935     case DW_OP_GNU_regval_type:
26936     case DW_OP_GNU_deref_type:
26937       return valx1->v.val_int == valy1->v.val_int
26938              && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
26939     case DW_OP_GNU_convert:
26940     case DW_OP_GNU_reinterpret:
26941       if (valx1->val_class != valy1->val_class)
26942         return false;
26943       if (valx1->val_class == dw_val_class_unsigned_const)
26944         return valx1->v.val_unsigned == valy1->v.val_unsigned;
26945       return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
26946     case DW_OP_GNU_parameter_ref:
26947       return valx1->val_class == dw_val_class_die_ref
26948              && valx1->val_class == valy1->val_class
26949              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
26950     default:
26951       /* Other codes have no operands.  */
26952       return true;
26953     }
26954 }
26955
26956 /* Return true if DWARF location expressions X and Y are the same.  */
26957
26958 static inline bool
26959 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
26960 {
26961   for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
26962     if (x->dw_loc_opc != y->dw_loc_opc
26963         || x->dtprel != y->dtprel
26964         || !compare_loc_operands (x, y))
26965       break;
26966   return x == NULL && y == NULL;
26967 }
26968
26969 /* Hashtable helpers.  */
26970
26971 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
26972 {
26973   static inline hashval_t hash (const dw_loc_list_struct *);
26974   static inline bool equal (const dw_loc_list_struct *,
26975                             const dw_loc_list_struct *);
26976 };
26977
26978 /* Return precomputed hash of location list X.  */
26979
26980 inline hashval_t
26981 loc_list_hasher::hash (const dw_loc_list_struct *x)
26982 {
26983   return x->hash;
26984 }
26985
26986 /* Return true if location lists A and B are the same.  */
26987
26988 inline bool
26989 loc_list_hasher::equal (const dw_loc_list_struct *a,
26990                         const dw_loc_list_struct *b)
26991 {
26992   if (a == b)
26993     return 1;
26994   if (a->hash != b->hash)
26995     return 0;
26996   for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
26997     if (strcmp (a->begin, b->begin) != 0
26998         || strcmp (a->end, b->end) != 0
26999         || (a->section == NULL) != (b->section == NULL)
27000         || (a->section && strcmp (a->section, b->section) != 0)
27001         || !compare_locs (a->expr, b->expr))
27002       break;
27003   return a == NULL && b == NULL;
27004 }
27005
27006 typedef hash_table<loc_list_hasher> loc_list_hash_type;
27007
27008
27009 /* Recursively optimize location lists referenced from DIE
27010    children and share them whenever possible.  */
27011
27012 static void
27013 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
27014 {
27015   dw_die_ref c;
27016   dw_attr_node *a;
27017   unsigned ix;
27018   dw_loc_list_struct **slot;
27019
27020   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27021     if (AT_class (a) == dw_val_class_loc_list)
27022       {
27023         dw_loc_list_ref list = AT_loc_list (a);
27024         /* TODO: perform some optimizations here, before hashing
27025            it and storing into the hash table.  */
27026         hash_loc_list (list);
27027         slot = htab->find_slot_with_hash (list, list->hash, INSERT);
27028         if (*slot == NULL)
27029           *slot = list;
27030         else
27031           a->dw_attr_val.v.val_loc_list = *slot;
27032       }
27033
27034   FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
27035 }
27036
27037
27038 /* Recursively assign each location list a unique index into the debug_addr
27039    section.  */
27040
27041 static void
27042 index_location_lists (dw_die_ref die)
27043 {
27044   dw_die_ref c;
27045   dw_attr_node *a;
27046   unsigned ix;
27047
27048   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27049     if (AT_class (a) == dw_val_class_loc_list)
27050       {
27051         dw_loc_list_ref list = AT_loc_list (a);
27052         dw_loc_list_ref curr;
27053         for (curr = list; curr != NULL; curr = curr->dw_loc_next)
27054           {
27055             /* Don't index an entry that has already been indexed
27056                or won't be output.  */
27057             if (curr->begin_entry != NULL
27058                 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
27059               continue;
27060
27061             curr->begin_entry
27062                 = add_addr_table_entry (xstrdup (curr->begin),
27063                                         ate_kind_label);
27064           }
27065       }
27066
27067   FOR_EACH_CHILD (die, c, index_location_lists (c));
27068 }
27069
27070 /* Optimize location lists referenced from DIE
27071    children and share them whenever possible.  */
27072
27073 static void
27074 optimize_location_lists (dw_die_ref die)
27075 {
27076   loc_list_hash_type htab (500);
27077   optimize_location_lists_1 (die, &htab);
27078 }
27079 \f
27080 /* Traverse the limbo die list, and add parent/child links.  The only
27081    dies without parents that should be here are concrete instances of
27082    inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
27083    For concrete instances, we can get the parent die from the abstract
27084    instance.  */
27085
27086 static void
27087 flush_limbo_die_list (void)
27088 {
27089   limbo_die_node *node, *next_node;
27090
27091   for (node = limbo_die_list; node; node = next_node)
27092     {
27093       dw_die_ref die = node->die;
27094       next_node = node->next;
27095
27096       if (die->die_parent == NULL)
27097         {
27098           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
27099
27100           if (origin && origin->die_parent)
27101             add_child_die (origin->die_parent, die);
27102           else if (is_cu_die (die))
27103             ;
27104           else if (seen_error ())
27105             /* It's OK to be confused by errors in the input.  */
27106             add_child_die (comp_unit_die (), die);
27107           else
27108             {
27109               /* In certain situations, the lexical block containing a
27110                  nested function can be optimized away, which results
27111                  in the nested function die being orphaned.  Likewise
27112                  with the return type of that nested function.  Force
27113                  this to be a child of the containing function.
27114
27115                  It may happen that even the containing function got fully
27116                  inlined and optimized out.  In that case we are lost and
27117                  assign the empty child.  This should not be big issue as
27118                  the function is likely unreachable too.  */
27119               gcc_assert (node->created_for);
27120
27121               if (DECL_P (node->created_for))
27122                 origin = get_context_die (DECL_CONTEXT (node->created_for));
27123               else if (TYPE_P (node->created_for))
27124                 origin = scope_die_for (node->created_for, comp_unit_die ());
27125               else
27126                 origin = comp_unit_die ();
27127
27128               add_child_die (origin, die);
27129             }
27130         }
27131     }
27132
27133   limbo_die_list = NULL;
27134 }
27135
27136 /* Output stuff that dwarf requires at the end of every file,
27137    and generate the DWARF-2 debugging info.  */
27138
27139 static void
27140 dwarf2out_finish (const char *filename)
27141 {
27142   comdat_type_node *ctnode;
27143   dw_die_ref main_comp_unit_die;
27144
27145   /* Flush out any latecomers to the limbo party.  */
27146   flush_limbo_die_list ();
27147
27148   /* We shouldn't have any symbols with delayed asm names for
27149      DIEs generated after early finish.  */
27150   gcc_assert (deferred_asm_name == NULL);
27151
27152   /* PCH might result in DW_AT_producer string being restored from the
27153      header compilation, so always fill it with empty string initially
27154      and overwrite only here.  */
27155   dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
27156   producer_string = gen_producer_string ();
27157   producer->dw_attr_val.v.val_str->refcount--;
27158   producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
27159
27160   gen_remaining_tmpl_value_param_die_attribute ();
27161
27162   /* Add the name for the main input file now.  We delayed this from
27163      dwarf2out_init to avoid complications with PCH.
27164      For LTO produced units use a fixed artificial name to avoid
27165      leaking tempfile names into the dwarf.  */
27166   if (!in_lto_p)
27167     add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
27168   else
27169     add_name_attribute (comp_unit_die (), "<artificial>");
27170   if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
27171     add_comp_dir_attribute (comp_unit_die ());
27172   else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
27173     {
27174       bool p = false;
27175       file_table->traverse<bool *, file_table_relative_p> (&p);
27176       if (p)
27177         add_comp_dir_attribute (comp_unit_die ());
27178     }
27179
27180 #if ENABLE_ASSERT_CHECKING
27181   {
27182     dw_die_ref die = comp_unit_die (), c;
27183     FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
27184   }
27185 #endif
27186   resolve_addr (comp_unit_die ());
27187   move_marked_base_types ();
27188
27189   /* Walk through the list of incomplete types again, trying once more to
27190      emit full debugging info for them.  */
27191   retry_incomplete_types ();
27192
27193   if (flag_eliminate_unused_debug_types)
27194     prune_unused_types ();
27195
27196   /* Generate separate COMDAT sections for type DIEs. */
27197   if (use_debug_types)
27198     {
27199       break_out_comdat_types (comp_unit_die ());
27200
27201       /* Each new type_unit DIE was added to the limbo die list when created.
27202          Since these have all been added to comdat_type_list, clear the
27203          limbo die list.  */
27204       limbo_die_list = NULL;
27205
27206       /* For each new comdat type unit, copy declarations for incomplete
27207          types to make the new unit self-contained (i.e., no direct
27208          references to the main compile unit).  */
27209       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
27210         copy_decls_for_unworthy_types (ctnode->root_die);
27211       copy_decls_for_unworthy_types (comp_unit_die ());
27212
27213       /* In the process of copying declarations from one unit to another,
27214          we may have left some declarations behind that are no longer
27215          referenced.  Prune them.  */
27216       prune_unused_types ();
27217     }
27218
27219   /* Generate separate CUs for each of the include files we've seen.
27220      They will go into limbo_die_list.  */
27221   if (flag_eliminate_dwarf2_dups)
27222     break_out_includes (comp_unit_die ());
27223
27224   /* Traverse the DIE's and add sibling attributes to those DIE's that
27225      have children.  */
27226   add_sibling_attributes (comp_unit_die ());
27227   limbo_die_node *node;
27228   for (node = limbo_die_list; node; node = node->next)
27229     add_sibling_attributes (node->die);
27230   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
27231     add_sibling_attributes (ctnode->root_die);
27232
27233   /* When splitting DWARF info, we put some attributes in the
27234      skeleton compile_unit DIE that remains in the .o, while
27235      most attributes go in the DWO compile_unit_die.  */
27236   if (dwarf_split_debug_info)
27237     main_comp_unit_die = gen_compile_unit_die (NULL);
27238   else
27239     main_comp_unit_die = comp_unit_die ();
27240
27241   /* Output a terminator label for the .text section.  */
27242   switch_to_section (text_section);
27243   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
27244   if (cold_text_section)
27245     {
27246       switch_to_section (cold_text_section);
27247       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
27248     }
27249
27250   /* We can only use the low/high_pc attributes if all of the code was
27251      in .text.  */
27252   if (!have_multiple_function_sections 
27253       || (dwarf_version < 3 && dwarf_strict))
27254     {
27255       /* Don't add if the CU has no associated code.  */
27256       if (text_section_used)
27257         add_AT_low_high_pc (main_comp_unit_die, text_section_label,
27258                             text_end_label, true);
27259     }
27260   else
27261     {
27262       unsigned fde_idx;
27263       dw_fde_ref fde;
27264       bool range_list_added = false;
27265
27266       if (text_section_used)
27267         add_ranges_by_labels (main_comp_unit_die, text_section_label,
27268                               text_end_label, &range_list_added, true);
27269       if (cold_text_section_used)
27270         add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
27271                               cold_end_label, &range_list_added, true);
27272
27273       FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
27274         {
27275           if (DECL_IGNORED_P (fde->decl))
27276             continue;
27277           if (!fde->in_std_section)
27278             add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
27279                                   fde->dw_fde_end, &range_list_added,
27280                                   true);
27281           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
27282             add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
27283                                   fde->dw_fde_second_end, &range_list_added,
27284                                   true);
27285         }
27286
27287       if (range_list_added)
27288         {
27289           /* We need to give .debug_loc and .debug_ranges an appropriate
27290              "base address".  Use zero so that these addresses become
27291              absolute.  Historically, we've emitted the unexpected
27292              DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
27293              Emit both to give time for other tools to adapt.  */
27294           add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
27295           if (! dwarf_strict && dwarf_version < 4)
27296             add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
27297
27298           add_ranges (NULL);
27299         }
27300     }
27301
27302   if (debug_info_level >= DINFO_LEVEL_TERSE)
27303     add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
27304                     debug_line_section_label);
27305
27306   if (have_macinfo)
27307     add_AT_macptr (comp_unit_die (),
27308                    dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
27309                    macinfo_section_label);
27310
27311   if (dwarf_split_debug_info)
27312     {
27313       /* optimize_location_lists calculates the size of the lists,
27314          so index them first, and assign indices to the entries.
27315          Although optimize_location_lists will remove entries from
27316          the table, it only does so for duplicates, and therefore
27317          only reduces ref_counts to 1.  */
27318       index_location_lists (comp_unit_die ());
27319
27320       if (addr_index_table != NULL)
27321         {
27322           unsigned int index = 0;
27323           addr_index_table
27324             ->traverse_noresize<unsigned int *, index_addr_table_entry>
27325             (&index);
27326         }
27327     }
27328
27329   if (have_location_lists)
27330     optimize_location_lists (comp_unit_die ());
27331
27332   save_macinfo_strings ();
27333
27334   if (dwarf_split_debug_info)
27335     {
27336       unsigned int index = 0;
27337
27338       /* Add attributes common to skeleton compile_units and
27339          type_units.  Because these attributes include strings, it
27340          must be done before freezing the string table.  Top-level
27341          skeleton die attrs are added when the skeleton type unit is
27342          created, so ensure it is created by this point.  */
27343       add_top_level_skeleton_die_attrs (main_comp_unit_die);
27344       debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
27345     }
27346
27347   /* Output all of the compilation units.  We put the main one last so that
27348      the offsets are available to output_pubnames.  */
27349   for (node = limbo_die_list; node; node = node->next)
27350     output_comp_unit (node->die, 0);
27351
27352   hash_table<comdat_type_hasher> comdat_type_table (100);
27353   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
27354     {
27355       comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
27356
27357       /* Don't output duplicate types.  */
27358       if (*slot != HTAB_EMPTY_ENTRY)
27359         continue;
27360
27361       /* Add a pointer to the line table for the main compilation unit
27362          so that the debugger can make sense of DW_AT_decl_file
27363          attributes.  */
27364       if (debug_info_level >= DINFO_LEVEL_TERSE)
27365         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
27366                         (!dwarf_split_debug_info
27367                          ? debug_line_section_label
27368                          : debug_skeleton_line_section_label));
27369
27370       output_comdat_type_unit (ctnode);
27371       *slot = ctnode;
27372     }
27373
27374   /* The AT_pubnames attribute needs to go in all skeleton dies, including
27375      both the main_cu and all skeleton TUs.  Making this call unconditional
27376      would end up either adding a second copy of the AT_pubnames attribute, or
27377      requiring a special case in add_top_level_skeleton_die_attrs.  */
27378   if (!dwarf_split_debug_info)
27379     add_AT_pubnames (comp_unit_die ());
27380
27381   if (dwarf_split_debug_info)
27382     {
27383       int mark;
27384       unsigned char checksum[16];
27385       struct md5_ctx ctx;
27386
27387       /* Compute a checksum of the comp_unit to use as the dwo_id.  */
27388       md5_init_ctx (&ctx);
27389       mark = 0;
27390       die_checksum (comp_unit_die (), &ctx, &mark);
27391       unmark_all_dies (comp_unit_die ());
27392       md5_finish_ctx (&ctx, checksum);
27393
27394       /* Use the first 8 bytes of the checksum as the dwo_id,
27395         and add it to both comp-unit DIEs.  */
27396       add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
27397       add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
27398
27399       /* Add the base offset of the ranges table to the skeleton
27400         comp-unit DIE.  */
27401       if (ranges_table_in_use)
27402         add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
27403                         ranges_section_label);
27404
27405       switch_to_section (debug_addr_section);
27406       ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
27407       output_addr_table ();
27408     }
27409
27410   /* Output the main compilation unit if non-empty or if .debug_macinfo
27411      or .debug_macro will be emitted.  */
27412   output_comp_unit (comp_unit_die (), have_macinfo);
27413
27414   if (dwarf_split_debug_info && info_section_emitted)
27415     output_skeleton_debug_sections (main_comp_unit_die);
27416
27417   /* Output the abbreviation table.  */
27418   if (abbrev_die_table_in_use != 1)
27419     {
27420       switch_to_section (debug_abbrev_section);
27421       ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
27422       output_abbrev_section ();
27423     }
27424
27425   /* Output location list section if necessary.  */
27426   if (have_location_lists)
27427     {
27428       /* Output the location lists info.  */
27429       switch_to_section (debug_loc_section);
27430       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
27431       output_location_lists (comp_unit_die ());
27432     }
27433
27434   output_pubtables ();
27435
27436   /* Output the address range information if a CU (.debug_info section)
27437      was emitted.  We output an empty table even if we had no functions
27438      to put in it.  This because the consumer has no way to tell the
27439      difference between an empty table that we omitted and failure to
27440      generate a table that would have contained data.  */
27441   if (info_section_emitted)
27442     {
27443       switch_to_section (debug_aranges_section);
27444       output_aranges ();
27445     }
27446
27447   /* Output ranges section if necessary.  */
27448   if (ranges_table_in_use)
27449     {
27450       switch_to_section (debug_ranges_section);
27451       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
27452       output_ranges ();
27453     }
27454
27455   /* Have to end the macro section.  */
27456   if (have_macinfo)
27457     {
27458       switch_to_section (debug_macinfo_section);
27459       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
27460       output_macinfo ();
27461       dw2_asm_output_data (1, 0, "End compilation unit");
27462     }
27463
27464   /* Output the source line correspondence table.  We must do this
27465      even if there is no line information.  Otherwise, on an empty
27466      translation unit, we will generate a present, but empty,
27467      .debug_info section.  IRIX 6.5 `nm' will then complain when
27468      examining the file.  This is done late so that any filenames
27469      used by the debug_info section are marked as 'used'.  */
27470   switch_to_section (debug_line_section);
27471   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
27472   if (! DWARF2_ASM_LINE_DEBUG_INFO)
27473     output_line_info (false);
27474
27475   if (dwarf_split_debug_info && info_section_emitted)
27476     {
27477       switch_to_section (debug_skeleton_line_section);
27478       ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
27479       output_line_info (true);
27480     }
27481
27482   /* If we emitted any indirect strings, output the string table too.  */
27483   if (debug_str_hash || skeleton_debug_str_hash)
27484     output_indirect_strings ();
27485 }
27486
27487 /* Perform any cleanups needed after the early debug generation pass
27488    has run.  */
27489
27490 static void
27491 dwarf2out_early_finish (void)
27492 {
27493   limbo_die_node *node;
27494
27495   /* Add DW_AT_linkage_name for all deferred DIEs.  */
27496   for (node = deferred_asm_name; node; node = node->next)
27497     {
27498       tree decl = node->created_for;
27499       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
27500           /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
27501              ended up in deferred_asm_name before we knew it was
27502              constant and never written to disk.  */
27503           && DECL_ASSEMBLER_NAME (decl))
27504         {
27505           add_linkage_attr (node->die, decl);
27506           move_linkage_attr (node->die);
27507         }
27508     }
27509   deferred_asm_name = NULL;
27510
27511   /* The point here is to flush out the limbo list so that it is empty
27512      and we don't need to stream it for LTO.  */
27513   flush_limbo_die_list ();
27514
27515   gen_scheduled_generic_parms_dies ();
27516   gen_remaining_tmpl_value_param_die_attribute ();
27517 }
27518
27519 /* Reset all state within dwarf2out.c so that we can rerun the compiler
27520    within the same process.  For use by toplev::finalize.  */
27521
27522 void
27523 dwarf2out_c_finalize (void)
27524 {
27525   last_var_location_insn = NULL;
27526   cached_next_real_insn = NULL;
27527   used_rtx_array = NULL;
27528   incomplete_types = NULL;
27529   decl_scope_table = NULL;
27530   debug_info_section = NULL;
27531   debug_skeleton_info_section = NULL;
27532   debug_abbrev_section = NULL;
27533   debug_skeleton_abbrev_section = NULL;
27534   debug_aranges_section = NULL;
27535   debug_addr_section = NULL;
27536   debug_macinfo_section = NULL;
27537   debug_line_section = NULL;
27538   debug_skeleton_line_section = NULL;
27539   debug_loc_section = NULL;
27540   debug_pubnames_section = NULL;
27541   debug_pubtypes_section = NULL;
27542   debug_str_section = NULL;
27543   debug_str_dwo_section = NULL;
27544   debug_str_offsets_section = NULL;
27545   debug_ranges_section = NULL;
27546   debug_frame_section = NULL;
27547   fde_vec = NULL;
27548   debug_str_hash = NULL;
27549   skeleton_debug_str_hash = NULL;
27550   dw2_string_counter = 0;
27551   have_multiple_function_sections = false;
27552   text_section_used = false;
27553   cold_text_section_used = false;
27554   cold_text_section = NULL;
27555   current_unit_personality = NULL;
27556
27557   next_die_offset = 0;
27558   single_comp_unit_die = NULL;
27559   comdat_type_list = NULL;
27560   limbo_die_list = NULL;
27561   file_table = NULL;
27562   decl_die_table = NULL;
27563   common_block_die_table = NULL;
27564   decl_loc_table = NULL;
27565   call_arg_locations = NULL;
27566   call_arg_loc_last = NULL;
27567   call_site_count = -1;
27568   tail_call_site_count = -1;
27569   cached_dw_loc_list_table = NULL;
27570   abbrev_die_table = NULL;
27571   abbrev_die_table_allocated = 0;
27572   abbrev_die_table_in_use = 0;
27573   delete dwarf_proc_stack_usage_map;
27574   dwarf_proc_stack_usage_map = NULL;
27575   line_info_label_num = 0;
27576   cur_line_info_table = NULL;
27577   text_section_line_info = NULL;
27578   cold_text_section_line_info = NULL;
27579   separate_line_info = NULL;
27580   info_section_emitted = false;
27581   pubname_table = NULL;
27582   pubtype_table = NULL;
27583   macinfo_table = NULL;
27584   ranges_table = NULL;
27585   ranges_table_allocated = 0;
27586   ranges_table_in_use = 0;
27587   ranges_by_label = 0;
27588   ranges_by_label_allocated = 0;
27589   ranges_by_label_in_use = 0;
27590   have_location_lists = false;
27591   loclabel_num = 0;
27592   poc_label_num = 0;
27593   last_emitted_file = NULL;
27594   label_num = 0;
27595   tmpl_value_parm_die_table = NULL;
27596   generic_type_instances = NULL;
27597   frame_pointer_fb_offset = 0;
27598   frame_pointer_fb_offset_valid = false;
27599   base_types.release ();
27600   XDELETEVEC (producer_string);
27601   producer_string = NULL;
27602 }
27603
27604 #include "gt-dwarf2out.h"