xref: /netbsd-src/external/gpl3/gcc.old/dist/gcc/dwarf2out.c (revision c38e7cc395b1472a774ff828e46123de44c628e9)
1 /* Output Dwarf2 format symbol table information from GCC.
2    Copyright (C) 1992-2015 Free Software Foundation, Inc.
3    Contributed by Gary Funck (gary@intrepid.com).
4    Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
5    Extensively modified by Jason Merrill (jason@cygnus.com).
6 
7 This file is part of GCC.
8 
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
13 
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
17 for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3.  If not see
21 <http://www.gnu.org/licenses/>.  */
22 
23 /* TODO: Emit .debug_line header even when there are no functions, since
24 	   the file numbers are used by .debug_info.  Alternately, leave
25 	   out locations for types and decls.
26 	 Avoid talking about ctors and op= for PODs.
27 	 Factor out common prologue sequences into multiple CIEs.  */
28 
29 /* The first part of this file deals with the DWARF 2 frame unwind
30    information, which is also used by the GCC efficient exception handling
31    mechanism.  The second part, controlled only by an #ifdef
32    DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
33    information.  */
34 
35 /* DWARF2 Abbreviation Glossary:
36 
37    CFA = Canonical Frame Address
38 	   a fixed address on the stack which identifies a call frame.
39 	   We define it to be the value of SP just before the call insn.
40 	   The CFA register and offset, which may change during the course
41 	   of the function, are used to calculate its value at runtime.
42 
43    CFI = Call Frame Instruction
44 	   an instruction for the DWARF2 abstract machine
45 
46    CIE = Common Information Entry
47 	   information describing information common to one or more FDEs
48 
49    DIE = Debugging Information Entry
50 
51    FDE = Frame Description Entry
52 	   information describing the stack call frame, in particular,
53 	   how to restore registers
54 
55    DW_CFA_... = DWARF2 CFA call frame instruction
56    DW_TAG_... = DWARF2 DIE tag */
57 
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "tm.h"
62 #include "rtl.h"
63 #include "hash-set.h"
64 #include "machmode.h"
65 #include "vec.h"
66 #include "double-int.h"
67 #include "input.h"
68 #include "alias.h"
69 #include "symtab.h"
70 #include "wide-int.h"
71 #include "inchash.h"
72 #include "real.h"
73 #include "tree.h"
74 #include "fold-const.h"
75 #include "stringpool.h"
76 #include "stor-layout.h"
77 #include "varasm.h"
78 #include "hashtab.h"
79 #include "hard-reg-set.h"
80 #include "function.h"
81 #include "emit-rtl.h"
82 #include "hash-table.h"
83 #include "version.h"
84 #include "flags.h"
85 #include "regs.h"
86 #include "rtlhash.h"
87 #include "insn-config.h"
88 #include "reload.h"
89 #include "output.h"
90 #include "statistics.h"
91 #include "fixed-value.h"
92 #include "expmed.h"
93 #include "dojump.h"
94 #include "explow.h"
95 #include "calls.h"
96 #include "stmt.h"
97 #include "expr.h"
98 #include "except.h"
99 #include "dwarf2.h"
100 #include "dwarf2out.h"
101 #include "dwarf2asm.h"
102 #include "toplev.h"
103 #include "md5.h"
104 #include "tm_p.h"
105 #include "diagnostic.h"
106 #include "tree-pretty-print.h"
107 #include "debug.h"
108 #include "target.h"
109 #include "common/common-target.h"
110 #include "langhooks.h"
111 #include "hash-map.h"
112 #include "is-a.h"
113 #include "plugin-api.h"
114 #include "ipa-ref.h"
115 #include "cgraph.h"
116 #include "ira.h"
117 #include "lra.h"
118 #include "dumpfile.h"
119 #include "opts.h"
120 #include "tree-dfa.h"
121 #include "gdb/gdb-index.h"
122 #include "rtl-iter.h"
123 
124 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
125 static rtx_insn *last_var_location_insn;
126 static rtx_insn *cached_next_real_insn;
127 static void dwarf2out_decl (tree);
128 
129 #ifdef VMS_DEBUGGING_INFO
130 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
131 
132 /* Define this macro to be a nonzero value if the directory specifications
133     which are output in the debug info should end with a separator.  */
134 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
135 /* Define this macro to evaluate to a nonzero value if GCC should refrain
136    from generating indirect strings in DWARF2 debug information, for instance
137    if your target is stuck with an old version of GDB that is unable to
138    process them properly or uses VMS Debug.  */
139 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
140 #else
141 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
142 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
143 #endif
144 
145 /* ??? Poison these here until it can be done generically.  They've been
146    totally replaced in this file; make sure it stays that way.  */
147 #undef DWARF2_UNWIND_INFO
148 #undef DWARF2_FRAME_INFO
149 #if (GCC_VERSION >= 3000)
150  #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
151 #endif
152 
153 /* The size of the target's pointer type.  */
154 #ifndef PTR_SIZE
155 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
156 #endif
157 
158 /* Array of RTXes referenced by the debugging information, which therefore
159    must be kept around forever.  */
160 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
161 
162 /* A pointer to the base of a list of incomplete types which might be
163    completed at some later time.  incomplete_types_list needs to be a
164    vec<tree, va_gc> *because we want to tell the garbage collector about
165    it.  */
166 static GTY(()) vec<tree, va_gc> *incomplete_types;
167 
168 /* A pointer to the base of a table of references to declaration
169    scopes.  This table is a display which tracks the nesting
170    of declaration scopes at the current scope and containing
171    scopes.  This table is used to find the proper place to
172    define type declaration DIE's.  */
173 static GTY(()) vec<tree, va_gc> *decl_scope_table;
174 
175 /* Pointers to various DWARF2 sections.  */
176 static GTY(()) section *debug_info_section;
177 static GTY(()) section *debug_skeleton_info_section;
178 static GTY(()) section *debug_abbrev_section;
179 static GTY(()) section *debug_skeleton_abbrev_section;
180 static GTY(()) section *debug_aranges_section;
181 static GTY(()) section *debug_addr_section;
182 static GTY(()) section *debug_macinfo_section;
183 static GTY(()) section *debug_line_section;
184 static GTY(()) section *debug_skeleton_line_section;
185 static GTY(()) section *debug_loc_section;
186 static GTY(()) section *debug_pubnames_section;
187 static GTY(()) section *debug_pubtypes_section;
188 static GTY(()) section *debug_str_section;
189 static GTY(()) section *debug_str_dwo_section;
190 static GTY(()) section *debug_str_offsets_section;
191 static GTY(()) section *debug_ranges_section;
192 static GTY(()) section *debug_frame_section;
193 
194 /* Maximum size (in bytes) of an artificially generated label.  */
195 #define MAX_ARTIFICIAL_LABEL_BYTES	30
196 
197 /* According to the (draft) DWARF 3 specification, the initial length
198    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
199    bytes are 0xffffffff, followed by the length stored in the next 8
200    bytes.
201 
202    However, the SGI/MIPS ABI uses an initial length which is equal to
203    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
204 
205 #ifndef DWARF_INITIAL_LENGTH_SIZE
206 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
207 #endif
208 
209 /* Round SIZE up to the nearest BOUNDARY.  */
210 #define DWARF_ROUND(SIZE,BOUNDARY) \
211   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
212 
213 /* CIE identifier.  */
214 #if HOST_BITS_PER_WIDE_INT >= 64
215 #define DWARF_CIE_ID \
216   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
217 #else
218 #define DWARF_CIE_ID DW_CIE_ID
219 #endif
220 
221 
222 /* A vector for a table that contains frame description
223    information for each routine.  */
224 #define NOT_INDEXED (-1U)
225 #define NO_INDEX_ASSIGNED (-2U)
226 
227 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
228 
229 struct GTY((for_user)) indirect_string_node {
230   const char *str;
231   unsigned int refcount;
232   enum dwarf_form form;
233   char *label;
234   unsigned int index;
235 };
236 
237 struct indirect_string_hasher : ggc_hasher<indirect_string_node *>
238 {
239   typedef const char *compare_type;
240 
241   static hashval_t hash (indirect_string_node *);
242   static bool equal (indirect_string_node *, const char *);
243 };
244 
245 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
246 
247 /* With split_debug_info, both the comp_dir and dwo_name go in the
248    main object file, rather than the dwo, similar to the force_direct
249    parameter elsewhere but with additional complications:
250 
251    1) The string is needed in both the main object file and the dwo.
252    That is, the comp_dir and dwo_name will appear in both places.
253 
254    2) Strings can use three forms: DW_FORM_string, DW_FORM_strp or
255    DW_FORM_GNU_str_index.
256 
257    3) GCC chooses the form to use late, depending on the size and
258    reference count.
259 
260    Rather than forcing the all debug string handling functions and
261    callers to deal with these complications, simply use a separate,
262    special-cased string table for any attribute that should go in the
263    main object file.  This limits the complexity to just the places
264    that need it.  */
265 
266 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
267 
268 static GTY(()) int dw2_string_counter;
269 
270 /* True if the compilation unit places functions in more than one section.  */
271 static GTY(()) bool have_multiple_function_sections = false;
272 
273 /* Whether the default text and cold text sections have been used at all.  */
274 
275 static GTY(()) bool text_section_used = false;
276 static GTY(()) bool cold_text_section_used = false;
277 
278 /* The default cold text section.  */
279 static GTY(()) section *cold_text_section;
280 
281 /* The DIE for C++14 'auto' in a function return type.  */
282 static GTY(()) dw_die_ref auto_die;
283 
284 /* The DIE for C++14 'decltype(auto)' in a function return type.  */
285 static GTY(()) dw_die_ref decltype_auto_die;
286 
287 /* Forward declarations for functions defined in this file.  */
288 
289 static char *stripattributes (const char *);
290 static void output_call_frame_info (int);
291 static void dwarf2out_note_section_used (void);
292 
293 /* Personality decl of current unit.  Used only when assembler does not support
294    personality CFI.  */
295 static GTY(()) rtx current_unit_personality;
296 
297 /* Data and reference forms for relocatable data.  */
298 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
299 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
300 
301 #ifndef DEBUG_FRAME_SECTION
302 #define DEBUG_FRAME_SECTION	".debug_frame"
303 #endif
304 
305 #ifndef FUNC_BEGIN_LABEL
306 #define FUNC_BEGIN_LABEL	"LFB"
307 #endif
308 
309 #ifndef FUNC_END_LABEL
310 #define FUNC_END_LABEL		"LFE"
311 #endif
312 
313 #ifndef PROLOGUE_END_LABEL
314 #define PROLOGUE_END_LABEL	"LPE"
315 #endif
316 
317 #ifndef EPILOGUE_BEGIN_LABEL
318 #define EPILOGUE_BEGIN_LABEL	"LEB"
319 #endif
320 
321 #ifndef FRAME_BEGIN_LABEL
322 #define FRAME_BEGIN_LABEL	"Lframe"
323 #endif
324 #define CIE_AFTER_SIZE_LABEL	"LSCIE"
325 #define CIE_END_LABEL		"LECIE"
326 #define FDE_LABEL		"LSFDE"
327 #define FDE_AFTER_SIZE_LABEL	"LASFDE"
328 #define FDE_END_LABEL		"LEFDE"
329 #define LINE_NUMBER_BEGIN_LABEL	"LSLT"
330 #define LINE_NUMBER_END_LABEL	"LELT"
331 #define LN_PROLOG_AS_LABEL	"LASLTP"
332 #define LN_PROLOG_END_LABEL	"LELTP"
333 #define DIE_LABEL_PREFIX	"DW"
334 
335 /* Match the base name of a file to the base name of a compilation unit. */
336 
337 static int
338 matches_main_base (const char *path)
339 {
340   /* Cache the last query. */
341   static const char *last_path = NULL;
342   static int last_match = 0;
343   if (path != last_path)
344     {
345       const char *base;
346       int length = base_of_path (path, &base);
347       last_path = path;
348       last_match = (length == main_input_baselength
349                     && memcmp (base, main_input_basename, length) == 0);
350     }
351   return last_match;
352 }
353 
354 #ifdef DEBUG_DEBUG_STRUCT
355 
356 static int
357 dump_struct_debug (tree type, enum debug_info_usage usage,
358 		   enum debug_struct_file criterion, int generic,
359 		   int matches, int result)
360 {
361   /* Find the type name. */
362   tree type_decl = TYPE_STUB_DECL (type);
363   tree t = type_decl;
364   const char *name = 0;
365   if (TREE_CODE (t) == TYPE_DECL)
366     t = DECL_NAME (t);
367   if (t)
368     name = IDENTIFIER_POINTER (t);
369 
370   fprintf (stderr, "	struct %d %s %s %s %s %d %p %s\n",
371 	   criterion,
372            DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
373            matches ? "bas" : "hdr",
374            generic ? "gen" : "ord",
375            usage == DINFO_USAGE_DFN ? ";" :
376              usage == DINFO_USAGE_DIR_USE ? "." : "*",
377            result,
378            (void*) type_decl, name);
379   return result;
380 }
381 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
382   dump_struct_debug (type, usage, criterion, generic, matches, result)
383 
384 #else
385 
386 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
387   (result)
388 
389 #endif
390 
391 /* Get the number of HOST_WIDE_INTs needed to represent the precision
392    of the number.  Some constants have a large uniform precision, so
393    we get the precision needed for the actual value of the number.  */
394 
395 static unsigned int
396 get_full_len (const wide_int &op)
397 {
398   int prec = wi::min_precision (op, UNSIGNED);
399   return ((prec + HOST_BITS_PER_WIDE_INT - 1)
400 	  / HOST_BITS_PER_WIDE_INT);
401 }
402 
403 static bool
404 should_emit_struct_debug (tree type, enum debug_info_usage usage)
405 {
406   enum debug_struct_file criterion;
407   tree type_decl;
408   bool generic = lang_hooks.types.generic_p (type);
409 
410   if (generic)
411     criterion = debug_struct_generic[usage];
412   else
413     criterion = debug_struct_ordinary[usage];
414 
415   if (criterion == DINFO_STRUCT_FILE_NONE)
416     return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
417   if (criterion == DINFO_STRUCT_FILE_ANY)
418     return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
419 
420   type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
421 
422   if (type_decl != NULL)
423     {
424      if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
425         return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
426 
427       if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
428         return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
429     }
430 
431   return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
432 }
433 
434 /* Return a pointer to a copy of the section string name S with all
435    attributes stripped off, and an asterisk prepended (for assemble_name).  */
436 
437 static inline char *
438 stripattributes (const char *s)
439 {
440   char *stripped = XNEWVEC (char, strlen (s) + 2);
441   char *p = stripped;
442 
443   *p++ = '*';
444 
445   while (*s && *s != ',')
446     *p++ = *s++;
447 
448   *p = '\0';
449   return stripped;
450 }
451 
452 /* Switch [BACK] to eh_frame_section.  If we don't have an eh_frame_section,
453    switch to the data section instead, and write out a synthetic start label
454    for collect2 the first time around.  */
455 
456 static void
457 switch_to_eh_frame_section (bool back)
458 {
459   tree label;
460 
461 #ifdef EH_FRAME_SECTION_NAME
462   if (eh_frame_section == 0)
463     {
464       int flags;
465 
466       if (EH_TABLES_CAN_BE_READ_ONLY)
467 	{
468 	  int fde_encoding;
469 	  int per_encoding;
470 	  int lsda_encoding;
471 
472 	  fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
473 						       /*global=*/0);
474 	  per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
475 						       /*global=*/1);
476 	  lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
477 							/*global=*/0);
478 	  flags = ((! flag_pic
479 		    || ((fde_encoding & 0x70) != DW_EH_PE_absptr
480 			&& (fde_encoding & 0x70) != DW_EH_PE_aligned
481 			&& (per_encoding & 0x70) != DW_EH_PE_absptr
482 			&& (per_encoding & 0x70) != DW_EH_PE_aligned
483 			&& (lsda_encoding & 0x70) != DW_EH_PE_absptr
484 			&& (lsda_encoding & 0x70) != DW_EH_PE_aligned))
485 		   ? 0 : SECTION_WRITE);
486 	}
487       else
488 	flags = SECTION_WRITE;
489       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
490     }
491 #endif /* EH_FRAME_SECTION_NAME */
492 
493   if (eh_frame_section)
494     switch_to_section (eh_frame_section);
495   else
496     {
497       /* We have no special eh_frame section.  Put the information in
498 	 the data section and emit special labels to guide collect2.  */
499       switch_to_section (data_section);
500 
501       if (!back)
502 	{
503 	  label = get_file_function_name ("F");
504 	  ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
505 	  targetm.asm_out.globalize_label (asm_out_file,
506 					   IDENTIFIER_POINTER (label));
507 	  ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
508 	}
509     }
510 }
511 
512 /* Switch [BACK] to the eh or debug frame table section, depending on
513    FOR_EH.  */
514 
515 static void
516 switch_to_frame_table_section (int for_eh, bool back)
517 {
518   if (for_eh)
519     switch_to_eh_frame_section (back);
520   else
521     {
522       if (!debug_frame_section)
523 	debug_frame_section = get_section (DEBUG_FRAME_SECTION,
524 					   SECTION_DEBUG, NULL);
525       switch_to_section (debug_frame_section);
526     }
527 }
528 
529 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
530 
531 enum dw_cfi_oprnd_type
532 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
533 {
534   switch (cfi)
535     {
536     case DW_CFA_nop:
537     case DW_CFA_GNU_window_save:
538     case DW_CFA_remember_state:
539     case DW_CFA_restore_state:
540       return dw_cfi_oprnd_unused;
541 
542     case DW_CFA_set_loc:
543     case DW_CFA_advance_loc1:
544     case DW_CFA_advance_loc2:
545     case DW_CFA_advance_loc4:
546     case DW_CFA_MIPS_advance_loc8:
547       return dw_cfi_oprnd_addr;
548 
549     case DW_CFA_offset:
550     case DW_CFA_offset_extended:
551     case DW_CFA_def_cfa:
552     case DW_CFA_offset_extended_sf:
553     case DW_CFA_def_cfa_sf:
554     case DW_CFA_restore:
555     case DW_CFA_restore_extended:
556     case DW_CFA_undefined:
557     case DW_CFA_same_value:
558     case DW_CFA_def_cfa_register:
559     case DW_CFA_register:
560     case DW_CFA_expression:
561       return dw_cfi_oprnd_reg_num;
562 
563     case DW_CFA_def_cfa_offset:
564     case DW_CFA_GNU_args_size:
565     case DW_CFA_def_cfa_offset_sf:
566       return dw_cfi_oprnd_offset;
567 
568     case DW_CFA_def_cfa_expression:
569       return dw_cfi_oprnd_loc;
570 
571     default:
572       gcc_unreachable ();
573     }
574 }
575 
576 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
577 
578 enum dw_cfi_oprnd_type
579 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
580 {
581   switch (cfi)
582     {
583     case DW_CFA_def_cfa:
584     case DW_CFA_def_cfa_sf:
585     case DW_CFA_offset:
586     case DW_CFA_offset_extended_sf:
587     case DW_CFA_offset_extended:
588       return dw_cfi_oprnd_offset;
589 
590     case DW_CFA_register:
591       return dw_cfi_oprnd_reg_num;
592 
593     case DW_CFA_expression:
594       return dw_cfi_oprnd_loc;
595 
596     default:
597       return dw_cfi_oprnd_unused;
598     }
599 }
600 
601 /* Output one FDE.  */
602 
603 static void
604 output_fde (dw_fde_ref fde, bool for_eh, bool second,
605 	    char *section_start_label, int fde_encoding, char *augmentation,
606 	    bool any_lsda_needed, int lsda_encoding)
607 {
608   const char *begin, *end;
609   static unsigned int j;
610   char l1[20], l2[20];
611 
612   targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
613 				     /* empty */ 0);
614   targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
615 				  for_eh + j);
616   ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
617   ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
618   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
619     dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
620 			 " indicating 64-bit DWARF extension");
621   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
622 			"FDE Length");
623   ASM_OUTPUT_LABEL (asm_out_file, l1);
624 
625   if (for_eh)
626     dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
627   else
628     dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
629 			   debug_frame_section, "FDE CIE offset");
630 
631   begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
632   end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
633 
634   if (for_eh)
635     {
636       rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
637       SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
638       dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
639 				       "FDE initial location");
640       dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
641 			    end, begin, "FDE address range");
642     }
643   else
644     {
645       dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
646       dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
647     }
648 
649   if (augmentation[0])
650     {
651       if (any_lsda_needed)
652 	{
653 	  int size = size_of_encoded_value (lsda_encoding);
654 
655 	  if (lsda_encoding == DW_EH_PE_aligned)
656 	    {
657 	      int offset = (  4		/* Length */
658 			    + 4		/* CIE offset */
659 			    + 2 * size_of_encoded_value (fde_encoding)
660 			    + 1		/* Augmentation size */ );
661 	      int pad = -offset & (PTR_SIZE - 1);
662 
663 	      size += pad;
664 	      gcc_assert (size_of_uleb128 (size) == 1);
665 	    }
666 
667 	  dw2_asm_output_data_uleb128 (size, "Augmentation size");
668 
669 	  if (fde->uses_eh_lsda)
670 	    {
671 	      ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
672 					   fde->funcdef_number);
673 	      dw2_asm_output_encoded_addr_rtx (lsda_encoding,
674 					       gen_rtx_SYMBOL_REF (Pmode, l1),
675 					       false,
676 					       "Language Specific Data Area");
677 	    }
678 	  else
679 	    {
680 	      if (lsda_encoding == DW_EH_PE_aligned)
681 		ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
682 	      dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
683 				   "Language Specific Data Area (none)");
684 	    }
685 	}
686       else
687 	dw2_asm_output_data_uleb128 (0, "Augmentation size");
688     }
689 
690   /* Loop through the Call Frame Instructions associated with this FDE.  */
691   fde->dw_fde_current_label = begin;
692   {
693     size_t from, until, i;
694 
695     from = 0;
696     until = vec_safe_length (fde->dw_fde_cfi);
697 
698     if (fde->dw_fde_second_begin == NULL)
699       ;
700     else if (!second)
701       until = fde->dw_fde_switch_cfi_index;
702     else
703       from = fde->dw_fde_switch_cfi_index;
704 
705     for (i = from; i < until; i++)
706       output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
707   }
708 
709   /* If we are to emit a ref/link from function bodies to their frame tables,
710      do it now.  This is typically performed to make sure that tables
711      associated with functions are dragged with them and not discarded in
712      garbage collecting links. We need to do this on a per function basis to
713      cope with -ffunction-sections.  */
714 
715 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
716   /* Switch to the function section, emit the ref to the tables, and
717      switch *back* into the table section.  */
718   switch_to_section (function_section (fde->decl));
719   ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
720   switch_to_frame_table_section (for_eh, true);
721 #endif
722 
723   /* Pad the FDE out to an address sized boundary.  */
724   ASM_OUTPUT_ALIGN (asm_out_file,
725 		    floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
726   ASM_OUTPUT_LABEL (asm_out_file, l2);
727 
728   j += 2;
729 }
730 
731 /* Return true if frame description entry FDE is needed for EH.  */
732 
733 static bool
734 fde_needed_for_eh_p (dw_fde_ref fde)
735 {
736   if (flag_asynchronous_unwind_tables)
737     return true;
738 
739   if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
740     return true;
741 
742   if (fde->uses_eh_lsda)
743     return true;
744 
745   /* If exceptions are enabled, we have collected nothrow info.  */
746   if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
747     return false;
748 
749   return true;
750 }
751 
752 /* Output the call frame information used to record information
753    that relates to calculating the frame pointer, and records the
754    location of saved registers.  */
755 
756 static void
757 output_call_frame_info (int for_eh)
758 {
759   unsigned int i;
760   dw_fde_ref fde;
761   dw_cfi_ref cfi;
762   char l1[20], l2[20], section_start_label[20];
763   bool any_lsda_needed = false;
764   char augmentation[6];
765   int augmentation_size;
766   int fde_encoding = DW_EH_PE_absptr;
767   int per_encoding = DW_EH_PE_absptr;
768   int lsda_encoding = DW_EH_PE_absptr;
769   int return_reg;
770   rtx personality = NULL;
771   int dw_cie_version;
772 
773   /* Don't emit a CIE if there won't be any FDEs.  */
774   if (!fde_vec)
775     return;
776 
777   /* Nothing to do if the assembler's doing it all.  */
778   if (dwarf2out_do_cfi_asm ())
779     return;
780 
781   /* If we don't have any functions we'll want to unwind out of, don't emit
782      any EH unwind information.  If we make FDEs linkonce, we may have to
783      emit an empty label for an FDE that wouldn't otherwise be emitted.  We
784      want to avoid having an FDE kept around when the function it refers to
785      is discarded.  Example where this matters: a primary function template
786      in C++ requires EH information, an explicit specialization doesn't.  */
787   if (for_eh)
788     {
789       bool any_eh_needed = false;
790 
791       FOR_EACH_VEC_ELT (*fde_vec, i, fde)
792 	{
793 	  if (fde->uses_eh_lsda)
794 	    any_eh_needed = any_lsda_needed = true;
795 	  else if (fde_needed_for_eh_p (fde))
796 	    any_eh_needed = true;
797 	  else if (TARGET_USES_WEAK_UNWIND_INFO)
798 	    targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
799 	}
800 
801       if (!any_eh_needed)
802 	return;
803     }
804 
805   /* We're going to be generating comments, so turn on app.  */
806   if (flag_debug_asm)
807     app_enable ();
808 
809   /* Switch to the proper frame section, first time.  */
810   switch_to_frame_table_section (for_eh, false);
811 
812   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
813   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
814 
815   /* Output the CIE.  */
816   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
817   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
818   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
819     dw2_asm_output_data (4, 0xffffffff,
820       "Initial length escape value indicating 64-bit DWARF extension");
821   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
822 			"Length of Common Information Entry");
823   ASM_OUTPUT_LABEL (asm_out_file, l1);
824 
825   /* Now that the CIE pointer is PC-relative for EH,
826      use 0 to identify the CIE.  */
827   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
828 		       (for_eh ? 0 : DWARF_CIE_ID),
829 		       "CIE Identifier Tag");
830 
831   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
832      use CIE version 1, unless that would produce incorrect results
833      due to overflowing the return register column.  */
834   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
835   dw_cie_version = 1;
836   if (return_reg >= 256 || dwarf_version > 2)
837     dw_cie_version = 3;
838   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
839 
840   augmentation[0] = 0;
841   augmentation_size = 0;
842 
843   personality = current_unit_personality;
844   if (for_eh)
845     {
846       char *p;
847 
848       /* Augmentation:
849 	 z	Indicates that a uleb128 is present to size the
850 		augmentation section.
851 	 L	Indicates the encoding (and thus presence) of
852 		an LSDA pointer in the FDE augmentation.
853 	 R	Indicates a non-default pointer encoding for
854 		FDE code pointers.
855 	 P	Indicates the presence of an encoding + language
856 		personality routine in the CIE augmentation.  */
857 
858       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
859       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
860       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
861 
862       p = augmentation + 1;
863       if (personality)
864 	{
865 	  *p++ = 'P';
866 	  augmentation_size += 1 + size_of_encoded_value (per_encoding);
867 	  assemble_external_libcall (personality);
868 	}
869       if (any_lsda_needed)
870 	{
871 	  *p++ = 'L';
872 	  augmentation_size += 1;
873 	}
874       if (fde_encoding != DW_EH_PE_absptr)
875 	{
876 	  *p++ = 'R';
877 	  augmentation_size += 1;
878 	}
879       if (p > augmentation + 1)
880 	{
881 	  augmentation[0] = 'z';
882 	  *p = '\0';
883 	}
884 
885       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
886       if (personality && per_encoding == DW_EH_PE_aligned)
887 	{
888 	  int offset = (  4		/* Length */
889 			+ 4		/* CIE Id */
890 			+ 1		/* CIE version */
891 			+ strlen (augmentation) + 1	/* Augmentation */
892 			+ size_of_uleb128 (1)		/* Code alignment */
893 			+ size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
894 			+ 1		/* RA column */
895 			+ 1		/* Augmentation size */
896 			+ 1		/* Personality encoding */ );
897 	  int pad = -offset & (PTR_SIZE - 1);
898 
899 	  augmentation_size += pad;
900 
901 	  /* Augmentations should be small, so there's scarce need to
902 	     iterate for a solution.  Die if we exceed one uleb128 byte.  */
903 	  gcc_assert (size_of_uleb128 (augmentation_size) == 1);
904 	}
905     }
906 
907   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
908   if (dw_cie_version >= 4)
909     {
910       dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
911       dw2_asm_output_data (1, 0, "CIE Segment Size");
912     }
913   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
914   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
915 			       "CIE Data Alignment Factor");
916 
917   if (dw_cie_version == 1)
918     dw2_asm_output_data (1, return_reg, "CIE RA Column");
919   else
920     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
921 
922   if (augmentation[0])
923     {
924       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
925       if (personality)
926 	{
927 	  dw2_asm_output_data (1, per_encoding, "Personality (%s)",
928 			       eh_data_format_name (per_encoding));
929 	  dw2_asm_output_encoded_addr_rtx (per_encoding,
930 					   personality,
931 					   true, NULL);
932 	}
933 
934       if (any_lsda_needed)
935 	dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
936 			     eh_data_format_name (lsda_encoding));
937 
938       if (fde_encoding != DW_EH_PE_absptr)
939 	dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
940 			     eh_data_format_name (fde_encoding));
941     }
942 
943   FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
944     output_cfi (cfi, NULL, for_eh);
945 
946   /* Pad the CIE out to an address sized boundary.  */
947   ASM_OUTPUT_ALIGN (asm_out_file,
948 		    floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
949   ASM_OUTPUT_LABEL (asm_out_file, l2);
950 
951   /* Loop through all of the FDE's.  */
952   FOR_EACH_VEC_ELT (*fde_vec, i, fde)
953     {
954       unsigned int k;
955 
956       /* Don't emit EH unwind info for leaf functions that don't need it.  */
957       if (for_eh && !fde_needed_for_eh_p (fde))
958 	continue;
959 
960       for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
961 	output_fde (fde, for_eh, k, section_start_label, fde_encoding,
962 		    augmentation, any_lsda_needed, lsda_encoding);
963     }
964 
965   if (for_eh && targetm.terminate_dw2_eh_frame_info)
966     dw2_asm_output_data (4, 0, "End of Table");
967 
968   /* Turn off app to make assembly quicker.  */
969   if (flag_debug_asm)
970     app_disable ();
971 }
972 
973 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed.  */
974 
975 static void
976 dwarf2out_do_cfi_startproc (bool second)
977 {
978   int enc;
979   rtx ref;
980   rtx personality = get_personality_function (current_function_decl);
981 
982   fprintf (asm_out_file, "\t.cfi_startproc\n");
983 
984   if (personality)
985     {
986       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
987       ref = personality;
988 
989       /* ??? The GAS support isn't entirely consistent.  We have to
990 	 handle indirect support ourselves, but PC-relative is done
991 	 in the assembler.  Further, the assembler can't handle any
992 	 of the weirder relocation types.  */
993       if (enc & DW_EH_PE_indirect)
994 	ref = dw2_force_const_mem (ref, true);
995 
996       fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
997       output_addr_const (asm_out_file, ref);
998       fputc ('\n', asm_out_file);
999     }
1000 
1001   if (crtl->uses_eh_lsda)
1002     {
1003       char lab[20];
1004 
1005       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
1006       ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
1007 				   current_function_funcdef_no);
1008       ref = gen_rtx_SYMBOL_REF (Pmode, lab);
1009       SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
1010 
1011       if (enc & DW_EH_PE_indirect)
1012 	ref = dw2_force_const_mem (ref, true);
1013 
1014       fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
1015       output_addr_const (asm_out_file, ref);
1016       fputc ('\n', asm_out_file);
1017     }
1018 }
1019 
1020 /* Allocate CURRENT_FDE.  Immediately initialize all we can, noting that
1021    this allocation may be done before pass_final.  */
1022 
1023 dw_fde_ref
1024 dwarf2out_alloc_current_fde (void)
1025 {
1026   dw_fde_ref fde;
1027 
1028   fde = ggc_cleared_alloc<dw_fde_node> ();
1029   fde->decl = current_function_decl;
1030   fde->funcdef_number = current_function_funcdef_no;
1031   fde->fde_index = vec_safe_length (fde_vec);
1032   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1033   fde->uses_eh_lsda = crtl->uses_eh_lsda;
1034   fde->nothrow = crtl->nothrow;
1035   fde->drap_reg = INVALID_REGNUM;
1036   fde->vdrap_reg = INVALID_REGNUM;
1037 
1038   /* Record the FDE associated with this function.  */
1039   cfun->fde = fde;
1040   vec_safe_push (fde_vec, fde);
1041 
1042   return fde;
1043 }
1044 
1045 /* Output a marker (i.e. a label) for the beginning of a function, before
1046    the prologue.  */
1047 
1048 void
1049 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1050 			  const char *file ATTRIBUTE_UNUSED)
1051 {
1052   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1053   char * dup_label;
1054   dw_fde_ref fde;
1055   section *fnsec;
1056   bool do_frame;
1057 
1058   current_function_func_begin_label = NULL;
1059 
1060   do_frame = dwarf2out_do_frame ();
1061 
1062   /* ??? current_function_func_begin_label is also used by except.c for
1063      call-site information.  We must emit this label if it might be used.  */
1064   if (!do_frame
1065       && (!flag_exceptions
1066 	  || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1067     return;
1068 
1069   fnsec = function_section (current_function_decl);
1070   switch_to_section (fnsec);
1071   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1072 			       current_function_funcdef_no);
1073   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1074 			  current_function_funcdef_no);
1075   dup_label = xstrdup (label);
1076   current_function_func_begin_label = dup_label;
1077 
1078   /* We can elide the fde allocation if we're not emitting debug info.  */
1079   if (!do_frame)
1080     return;
1081 
1082   /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1083      emit insns as rtx but bypass the bulk of rest_of_compilation, which
1084      would include pass_dwarf2_frame.  If we've not created the FDE yet,
1085      do so now.  */
1086   fde = cfun->fde;
1087   if (fde == NULL)
1088     fde = dwarf2out_alloc_current_fde ();
1089 
1090   /* Initialize the bits of CURRENT_FDE that were not available earlier.  */
1091   fde->dw_fde_begin = dup_label;
1092   fde->dw_fde_current_label = dup_label;
1093   fde->in_std_section = (fnsec == text_section
1094 			 || (cold_text_section && fnsec == cold_text_section));
1095 
1096   /* We only want to output line number information for the genuine dwarf2
1097      prologue case, not the eh frame case.  */
1098 #ifdef DWARF2_DEBUGGING_INFO
1099   if (file)
1100     dwarf2out_source_line (line, file, 0, true);
1101 #endif
1102 
1103   if (dwarf2out_do_cfi_asm ())
1104     dwarf2out_do_cfi_startproc (false);
1105   else
1106     {
1107       rtx personality = get_personality_function (current_function_decl);
1108       if (!current_unit_personality)
1109         current_unit_personality = personality;
1110 
1111       /* We cannot keep a current personality per function as without CFI
1112 	 asm, at the point where we emit the CFI data, there is no current
1113 	 function anymore.  */
1114       if (personality && current_unit_personality != personality)
1115 	sorry ("multiple EH personalities are supported only with assemblers "
1116 	       "supporting .cfi_personality directive");
1117     }
1118 }
1119 
1120 /* Output a marker (i.e. a label) for the end of the generated code
1121    for a function prologue.  This gets called *after* the prologue code has
1122    been generated.  */
1123 
1124 void
1125 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1126 			const char *file ATTRIBUTE_UNUSED)
1127 {
1128   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1129 
1130   /* Output a label to mark the endpoint of the code generated for this
1131      function.  */
1132   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1133 			       current_function_funcdef_no);
1134   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1135 			  current_function_funcdef_no);
1136   cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1137 }
1138 
1139 /* Output a marker (i.e. a label) for the beginning of the generated code
1140    for a function epilogue.  This gets called *before* the prologue code has
1141    been generated.  */
1142 
1143 void
1144 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1145 			  const char *file ATTRIBUTE_UNUSED)
1146 {
1147   dw_fde_ref fde = cfun->fde;
1148   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1149 
1150   if (fde->dw_fde_vms_begin_epilogue)
1151     return;
1152 
1153   /* Output a label to mark the endpoint of the code generated for this
1154      function.  */
1155   ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1156 			       current_function_funcdef_no);
1157   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1158 			  current_function_funcdef_no);
1159   fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1160 }
1161 
1162 /* Output a marker (i.e. a label) for the absolute end of the generated code
1163    for a function definition.  This gets called *after* the epilogue code has
1164    been generated.  */
1165 
1166 void
1167 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1168 			const char *file ATTRIBUTE_UNUSED)
1169 {
1170   dw_fde_ref fde;
1171   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1172 
1173   last_var_location_insn = NULL;
1174   cached_next_real_insn = NULL;
1175 
1176   if (dwarf2out_do_cfi_asm ())
1177     fprintf (asm_out_file, "\t.cfi_endproc\n");
1178 
1179   /* Output a label to mark the endpoint of the code generated for this
1180      function.  */
1181   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1182 			       current_function_funcdef_no);
1183   ASM_OUTPUT_LABEL (asm_out_file, label);
1184   fde = cfun->fde;
1185   gcc_assert (fde != NULL);
1186   if (fde->dw_fde_second_begin == NULL)
1187     fde->dw_fde_end = xstrdup (label);
1188 }
1189 
1190 void
1191 dwarf2out_frame_finish (void)
1192 {
1193   /* Output call frame information.  */
1194   if (targetm.debug_unwind_info () == UI_DWARF2)
1195     output_call_frame_info (0);
1196 
1197   /* Output another copy for the unwinder.  */
1198   if ((flag_unwind_tables || flag_exceptions)
1199       && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1200     output_call_frame_info (1);
1201 }
1202 
1203 /* Note that the current function section is being used for code.  */
1204 
1205 static void
1206 dwarf2out_note_section_used (void)
1207 {
1208   section *sec = current_function_section ();
1209   if (sec == text_section)
1210     text_section_used = true;
1211   else if (sec == cold_text_section)
1212     cold_text_section_used = true;
1213 }
1214 
1215 static void var_location_switch_text_section (void);
1216 static void set_cur_line_info_table (section *);
1217 
1218 void
1219 dwarf2out_switch_text_section (void)
1220 {
1221   section *sect;
1222   dw_fde_ref fde = cfun->fde;
1223 
1224   gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1225 
1226   if (!in_cold_section_p)
1227     {
1228       fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1229       fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1230       fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1231     }
1232   else
1233     {
1234       fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1235       fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1236       fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1237     }
1238   have_multiple_function_sections = true;
1239 
1240   /* There is no need to mark used sections when not debugging.  */
1241   if (cold_text_section != NULL)
1242     dwarf2out_note_section_used ();
1243 
1244   if (dwarf2out_do_cfi_asm ())
1245     fprintf (asm_out_file, "\t.cfi_endproc\n");
1246 
1247   /* Now do the real section switch.  */
1248   sect = current_function_section ();
1249   switch_to_section (sect);
1250 
1251   fde->second_in_std_section
1252     = (sect == text_section
1253        || (cold_text_section && sect == cold_text_section));
1254 
1255   if (dwarf2out_do_cfi_asm ())
1256     dwarf2out_do_cfi_startproc (true);
1257 
1258   var_location_switch_text_section ();
1259 
1260   if (cold_text_section != NULL)
1261     set_cur_line_info_table (sect);
1262 }
1263 
1264 /* And now, the subset of the debugging information support code necessary
1265    for emitting location expressions.  */
1266 
1267 /* Data about a single source file.  */
1268 struct GTY((for_user)) dwarf_file_data {
1269   const char * filename;
1270   int emitted_number;
1271 };
1272 
1273 typedef struct GTY(()) deferred_locations_struct
1274 {
1275   tree variable;
1276   dw_die_ref die;
1277 } deferred_locations;
1278 
1279 
1280 static GTY(()) vec<deferred_locations, va_gc> *deferred_locations_list;
1281 
1282 
1283 /* Describe an entry into the .debug_addr section.  */
1284 
1285 enum ate_kind {
1286   ate_kind_rtx,
1287   ate_kind_rtx_dtprel,
1288   ate_kind_label
1289 };
1290 
1291 typedef struct GTY((for_user)) addr_table_entry_struct {
1292   enum ate_kind kind;
1293   unsigned int refcount;
1294   unsigned int index;
1295   union addr_table_entry_struct_union
1296     {
1297       rtx GTY ((tag ("0"))) rtl;
1298       char * GTY ((tag ("1"))) label;
1299     }
1300   GTY ((desc ("%1.kind"))) addr;
1301 }
1302 addr_table_entry;
1303 
1304 /* Location lists are ranges + location descriptions for that range,
1305    so you can track variables that are in different places over
1306    their entire life.  */
1307 typedef struct GTY(()) dw_loc_list_struct {
1308   dw_loc_list_ref dw_loc_next;
1309   const char *begin; /* Label and addr_entry for start of range */
1310   addr_table_entry *begin_entry;
1311   const char *end;  /* Label for end of range */
1312   char *ll_symbol; /* Label for beginning of location list.
1313 		      Only on head of list */
1314   const char *section; /* Section this loclist is relative to */
1315   dw_loc_descr_ref expr;
1316   hashval_t hash;
1317   /* True if all addresses in this and subsequent lists are known to be
1318      resolved.  */
1319   bool resolved_addr;
1320   /* True if this list has been replaced by dw_loc_next.  */
1321   bool replaced;
1322   bool emitted;
1323   /* True if the range should be emitted even if begin and end
1324      are the same.  */
1325   bool force;
1326 } dw_loc_list_node;
1327 
1328 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1329 
1330 /* Convert a DWARF stack opcode into its string name.  */
1331 
1332 static const char *
1333 dwarf_stack_op_name (unsigned int op)
1334 {
1335   const char *name = get_DW_OP_name (op);
1336 
1337   if (name != NULL)
1338     return name;
1339 
1340   return "OP_<unknown>";
1341 }
1342 
1343 /* Return a pointer to a newly allocated location description.  Location
1344    descriptions are simple expression terms that can be strung
1345    together to form more complicated location (address) descriptions.  */
1346 
1347 static inline dw_loc_descr_ref
1348 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1349 	       unsigned HOST_WIDE_INT oprnd2)
1350 {
1351   dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1352 
1353   descr->dw_loc_opc = op;
1354   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1355   descr->dw_loc_oprnd1.val_entry = NULL;
1356   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1357   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1358   descr->dw_loc_oprnd2.val_entry = NULL;
1359   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1360 
1361   return descr;
1362 }
1363 
1364 /* Return a pointer to a newly allocated location description for
1365    REG and OFFSET.  */
1366 
1367 static inline dw_loc_descr_ref
1368 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
1369 {
1370   if (reg <= 31)
1371     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1372 			  offset, 0);
1373   else
1374     return new_loc_descr (DW_OP_bregx, reg, offset);
1375 }
1376 
1377 /* Add a location description term to a location description expression.  */
1378 
1379 static inline void
1380 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1381 {
1382   dw_loc_descr_ref *d;
1383 
1384   /* Find the end of the chain.  */
1385   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1386     ;
1387 
1388   *d = descr;
1389 }
1390 
1391 /* Compare two location operands for exact equality.  */
1392 
1393 static bool
1394 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1395 {
1396   if (a->val_class != b->val_class)
1397     return false;
1398   switch (a->val_class)
1399     {
1400     case dw_val_class_none:
1401       return true;
1402     case dw_val_class_addr:
1403       return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1404 
1405     case dw_val_class_offset:
1406     case dw_val_class_unsigned_const:
1407     case dw_val_class_const:
1408     case dw_val_class_range_list:
1409     case dw_val_class_lineptr:
1410     case dw_val_class_macptr:
1411       /* These are all HOST_WIDE_INT, signed or unsigned.  */
1412       return a->v.val_unsigned == b->v.val_unsigned;
1413 
1414     case dw_val_class_loc:
1415       return a->v.val_loc == b->v.val_loc;
1416     case dw_val_class_loc_list:
1417       return a->v.val_loc_list == b->v.val_loc_list;
1418     case dw_val_class_die_ref:
1419       return a->v.val_die_ref.die == b->v.val_die_ref.die;
1420     case dw_val_class_fde_ref:
1421       return a->v.val_fde_index == b->v.val_fde_index;
1422     case dw_val_class_lbl_id:
1423     case dw_val_class_high_pc:
1424       return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1425     case dw_val_class_str:
1426       return a->v.val_str == b->v.val_str;
1427     case dw_val_class_flag:
1428       return a->v.val_flag == b->v.val_flag;
1429     case dw_val_class_file:
1430       return a->v.val_file == b->v.val_file;
1431     case dw_val_class_decl_ref:
1432       return a->v.val_decl_ref == b->v.val_decl_ref;
1433 
1434     case dw_val_class_const_double:
1435       return (a->v.val_double.high == b->v.val_double.high
1436 	      && a->v.val_double.low == b->v.val_double.low);
1437 
1438     case dw_val_class_wide_int:
1439       return *a->v.val_wide == *b->v.val_wide;
1440 
1441     case dw_val_class_vec:
1442       {
1443 	size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1444 	size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1445 
1446 	return (a_len == b_len
1447 		&& !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1448       }
1449 
1450     case dw_val_class_data8:
1451       return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1452 
1453     case dw_val_class_vms_delta:
1454       return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1455               && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1456     }
1457   gcc_unreachable ();
1458 }
1459 
1460 /* Compare two location atoms for exact equality.  */
1461 
1462 static bool
1463 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1464 {
1465   if (a->dw_loc_opc != b->dw_loc_opc)
1466     return false;
1467 
1468   /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1469      address size, but since we always allocate cleared storage it
1470      should be zero for other types of locations.  */
1471   if (a->dtprel != b->dtprel)
1472     return false;
1473 
1474   return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1475 	  && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1476 }
1477 
1478 /* Compare two complete location expressions for exact equality.  */
1479 
1480 bool
1481 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1482 {
1483   while (1)
1484     {
1485       if (a == b)
1486 	return true;
1487       if (a == NULL || b == NULL)
1488 	return false;
1489       if (!loc_descr_equal_p_1 (a, b))
1490 	return false;
1491 
1492       a = a->dw_loc_next;
1493       b = b->dw_loc_next;
1494     }
1495 }
1496 
1497 
1498 /* Add a constant OFFSET to a location expression.  */
1499 
1500 static void
1501 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1502 {
1503   dw_loc_descr_ref loc;
1504   HOST_WIDE_INT *p;
1505 
1506   gcc_assert (*list_head != NULL);
1507 
1508   if (!offset)
1509     return;
1510 
1511   /* Find the end of the chain.  */
1512   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1513     ;
1514 
1515   p = NULL;
1516   if (loc->dw_loc_opc == DW_OP_fbreg
1517       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1518     p = &loc->dw_loc_oprnd1.v.val_int;
1519   else if (loc->dw_loc_opc == DW_OP_bregx)
1520     p = &loc->dw_loc_oprnd2.v.val_int;
1521 
1522   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1523      offset.  Don't optimize if an signed integer overflow would happen.  */
1524   if (p != NULL
1525       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1526 	  || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1527     *p += offset;
1528 
1529   else if (offset > 0)
1530     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1531 
1532   else
1533     {
1534       loc->dw_loc_next = int_loc_descriptor (-offset);
1535       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1536     }
1537 }
1538 
1539 /* Add a constant OFFSET to a location list.  */
1540 
1541 static void
1542 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1543 {
1544   dw_loc_list_ref d;
1545   for (d = list_head; d != NULL; d = d->dw_loc_next)
1546     loc_descr_plus_const (&d->expr, offset);
1547 }
1548 
1549 #define DWARF_REF_SIZE	\
1550   (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1551 
1552 static unsigned long int get_base_type_offset (dw_die_ref);
1553 
1554 /* Return the size of a location descriptor.  */
1555 
1556 static unsigned long
1557 size_of_loc_descr (dw_loc_descr_ref loc)
1558 {
1559   unsigned long size = 1;
1560 
1561   switch (loc->dw_loc_opc)
1562     {
1563     case DW_OP_addr:
1564       size += DWARF2_ADDR_SIZE;
1565       break;
1566     case DW_OP_GNU_addr_index:
1567     case DW_OP_GNU_const_index:
1568       gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1569       size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1570       break;
1571     case DW_OP_const1u:
1572     case DW_OP_const1s:
1573       size += 1;
1574       break;
1575     case DW_OP_const2u:
1576     case DW_OP_const2s:
1577       size += 2;
1578       break;
1579     case DW_OP_const4u:
1580     case DW_OP_const4s:
1581       size += 4;
1582       break;
1583     case DW_OP_const8u:
1584     case DW_OP_const8s:
1585       size += 8;
1586       break;
1587     case DW_OP_constu:
1588       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1589       break;
1590     case DW_OP_consts:
1591       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1592       break;
1593     case DW_OP_pick:
1594       size += 1;
1595       break;
1596     case DW_OP_plus_uconst:
1597       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1598       break;
1599     case DW_OP_skip:
1600     case DW_OP_bra:
1601       size += 2;
1602       break;
1603     case DW_OP_breg0:
1604     case DW_OP_breg1:
1605     case DW_OP_breg2:
1606     case DW_OP_breg3:
1607     case DW_OP_breg4:
1608     case DW_OP_breg5:
1609     case DW_OP_breg6:
1610     case DW_OP_breg7:
1611     case DW_OP_breg8:
1612     case DW_OP_breg9:
1613     case DW_OP_breg10:
1614     case DW_OP_breg11:
1615     case DW_OP_breg12:
1616     case DW_OP_breg13:
1617     case DW_OP_breg14:
1618     case DW_OP_breg15:
1619     case DW_OP_breg16:
1620     case DW_OP_breg17:
1621     case DW_OP_breg18:
1622     case DW_OP_breg19:
1623     case DW_OP_breg20:
1624     case DW_OP_breg21:
1625     case DW_OP_breg22:
1626     case DW_OP_breg23:
1627     case DW_OP_breg24:
1628     case DW_OP_breg25:
1629     case DW_OP_breg26:
1630     case DW_OP_breg27:
1631     case DW_OP_breg28:
1632     case DW_OP_breg29:
1633     case DW_OP_breg30:
1634     case DW_OP_breg31:
1635       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1636       break;
1637     case DW_OP_regx:
1638       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1639       break;
1640     case DW_OP_fbreg:
1641       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1642       break;
1643     case DW_OP_bregx:
1644       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1645       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1646       break;
1647     case DW_OP_piece:
1648       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1649       break;
1650     case DW_OP_bit_piece:
1651       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1652       size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1653       break;
1654     case DW_OP_deref_size:
1655     case DW_OP_xderef_size:
1656       size += 1;
1657       break;
1658     case DW_OP_call2:
1659       size += 2;
1660       break;
1661     case DW_OP_call4:
1662       size += 4;
1663       break;
1664     case DW_OP_call_ref:
1665       size += DWARF_REF_SIZE;
1666       break;
1667     case DW_OP_implicit_value:
1668       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1669 	      + loc->dw_loc_oprnd1.v.val_unsigned;
1670       break;
1671     case DW_OP_GNU_implicit_pointer:
1672       size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1673       break;
1674     case DW_OP_GNU_entry_value:
1675       {
1676 	unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1677 	size += size_of_uleb128 (op_size) + op_size;
1678 	break;
1679       }
1680     case DW_OP_GNU_const_type:
1681       {
1682 	unsigned long o
1683 	  = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1684 	size += size_of_uleb128 (o) + 1;
1685 	switch (loc->dw_loc_oprnd2.val_class)
1686 	  {
1687 	  case dw_val_class_vec:
1688 	    size += loc->dw_loc_oprnd2.v.val_vec.length
1689 		    * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1690 	    break;
1691 	  case dw_val_class_const:
1692 	    size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1693 	    break;
1694 	  case dw_val_class_const_double:
1695 	    size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1696 	    break;
1697 	  case dw_val_class_wide_int:
1698 	    size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1699 		     * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1700 	    break;
1701 	  default:
1702 	    gcc_unreachable ();
1703 	  }
1704 	break;
1705       }
1706     case DW_OP_GNU_regval_type:
1707       {
1708 	unsigned long o
1709 	  = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1710 	size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1711 		+ size_of_uleb128 (o);
1712       }
1713       break;
1714     case DW_OP_GNU_deref_type:
1715       {
1716 	unsigned long o
1717 	  = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1718 	size += 1 + size_of_uleb128 (o);
1719       }
1720       break;
1721     case DW_OP_GNU_convert:
1722     case DW_OP_GNU_reinterpret:
1723       if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1724 	size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1725       else
1726 	{
1727 	  unsigned long o
1728 	    = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1729 	  size += size_of_uleb128 (o);
1730 	}
1731       break;
1732     case DW_OP_GNU_parameter_ref:
1733       size += 4;
1734       break;
1735     default:
1736       break;
1737     }
1738 
1739   return size;
1740 }
1741 
1742 /* Return the size of a series of location descriptors.  */
1743 
1744 unsigned long
1745 size_of_locs (dw_loc_descr_ref loc)
1746 {
1747   dw_loc_descr_ref l;
1748   unsigned long size;
1749 
1750   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1751      field, to avoid writing to a PCH file.  */
1752   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1753     {
1754       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1755 	break;
1756       size += size_of_loc_descr (l);
1757     }
1758   if (! l)
1759     return size;
1760 
1761   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1762     {
1763       l->dw_loc_addr = size;
1764       size += size_of_loc_descr (l);
1765     }
1766 
1767   return size;
1768 }
1769 
1770 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1771 static void get_ref_die_offset_label (char *, dw_die_ref);
1772 static unsigned long int get_ref_die_offset (dw_die_ref);
1773 
1774 /* Output location description stack opcode's operands (if any).
1775    The for_eh_or_skip parameter controls whether register numbers are
1776    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1777    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1778    info).  This should be suppressed for the cases that have not been converted
1779    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
1780 
1781 static void
1782 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1783 {
1784   dw_val_ref val1 = &loc->dw_loc_oprnd1;
1785   dw_val_ref val2 = &loc->dw_loc_oprnd2;
1786 
1787   switch (loc->dw_loc_opc)
1788     {
1789 #ifdef DWARF2_DEBUGGING_INFO
1790     case DW_OP_const2u:
1791     case DW_OP_const2s:
1792       dw2_asm_output_data (2, val1->v.val_int, NULL);
1793       break;
1794     case DW_OP_const4u:
1795       if (loc->dtprel)
1796 	{
1797 	  gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1798 	  targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1799 					       val1->v.val_addr);
1800 	  fputc ('\n', asm_out_file);
1801 	  break;
1802 	}
1803       /* FALLTHRU */
1804     case DW_OP_const4s:
1805       dw2_asm_output_data (4, val1->v.val_int, NULL);
1806       break;
1807     case DW_OP_const8u:
1808       if (loc->dtprel)
1809 	{
1810 	  gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1811 	  targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1812 					       val1->v.val_addr);
1813 	  fputc ('\n', asm_out_file);
1814 	  break;
1815 	}
1816       /* FALLTHRU */
1817     case DW_OP_const8s:
1818       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1819       dw2_asm_output_data (8, val1->v.val_int, NULL);
1820       break;
1821     case DW_OP_skip:
1822     case DW_OP_bra:
1823       {
1824 	int offset;
1825 
1826 	gcc_assert (val1->val_class == dw_val_class_loc);
1827 	offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1828 
1829 	dw2_asm_output_data (2, offset, NULL);
1830       }
1831       break;
1832     case DW_OP_implicit_value:
1833       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1834       switch (val2->val_class)
1835 	{
1836 	case dw_val_class_const:
1837 	  dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1838 	  break;
1839 	case dw_val_class_vec:
1840 	  {
1841 	    unsigned int elt_size = val2->v.val_vec.elt_size;
1842 	    unsigned int len = val2->v.val_vec.length;
1843 	    unsigned int i;
1844 	    unsigned char *p;
1845 
1846 	    if (elt_size > sizeof (HOST_WIDE_INT))
1847 	      {
1848 		elt_size /= 2;
1849 		len *= 2;
1850 	      }
1851 	    for (i = 0, p = val2->v.val_vec.array;
1852 		 i < len;
1853 		 i++, p += elt_size)
1854 	      dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1855 				   "fp or vector constant word %u", i);
1856 	  }
1857 	  break;
1858 	case dw_val_class_const_double:
1859 	  {
1860 	    unsigned HOST_WIDE_INT first, second;
1861 
1862 	    if (WORDS_BIG_ENDIAN)
1863 	      {
1864 		first = val2->v.val_double.high;
1865 		second = val2->v.val_double.low;
1866 	      }
1867 	    else
1868 	      {
1869 		first = val2->v.val_double.low;
1870 		second = val2->v.val_double.high;
1871 	      }
1872 	    dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1873 				 first, NULL);
1874 	    dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1875 				 second, NULL);
1876 	  }
1877 	  break;
1878 	case dw_val_class_wide_int:
1879 	  {
1880 	    int i;
1881 	    int len = get_full_len (*val2->v.val_wide);
1882 	    if (WORDS_BIG_ENDIAN)
1883 	      for (i = len - 1; i >= 0; --i)
1884 		dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1885 				     val2->v.val_wide->elt (i), NULL);
1886 	    else
1887 	      for (i = 0; i < len; ++i)
1888 		dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1889 				     val2->v.val_wide->elt (i), NULL);
1890 	  }
1891 	  break;
1892 	case dw_val_class_addr:
1893 	  gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1894 	  dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1895 	  break;
1896 	default:
1897 	  gcc_unreachable ();
1898 	}
1899       break;
1900 #else
1901     case DW_OP_const2u:
1902     case DW_OP_const2s:
1903     case DW_OP_const4u:
1904     case DW_OP_const4s:
1905     case DW_OP_const8u:
1906     case DW_OP_const8s:
1907     case DW_OP_skip:
1908     case DW_OP_bra:
1909     case DW_OP_implicit_value:
1910       /* We currently don't make any attempt to make sure these are
1911 	 aligned properly like we do for the main unwind info, so
1912 	 don't support emitting things larger than a byte if we're
1913 	 only doing unwinding.  */
1914       gcc_unreachable ();
1915 #endif
1916     case DW_OP_const1u:
1917     case DW_OP_const1s:
1918       dw2_asm_output_data (1, val1->v.val_int, NULL);
1919       break;
1920     case DW_OP_constu:
1921       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1922       break;
1923     case DW_OP_consts:
1924       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1925       break;
1926     case DW_OP_pick:
1927       dw2_asm_output_data (1, val1->v.val_int, NULL);
1928       break;
1929     case DW_OP_plus_uconst:
1930       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1931       break;
1932     case DW_OP_breg0:
1933     case DW_OP_breg1:
1934     case DW_OP_breg2:
1935     case DW_OP_breg3:
1936     case DW_OP_breg4:
1937     case DW_OP_breg5:
1938     case DW_OP_breg6:
1939     case DW_OP_breg7:
1940     case DW_OP_breg8:
1941     case DW_OP_breg9:
1942     case DW_OP_breg10:
1943     case DW_OP_breg11:
1944     case DW_OP_breg12:
1945     case DW_OP_breg13:
1946     case DW_OP_breg14:
1947     case DW_OP_breg15:
1948     case DW_OP_breg16:
1949     case DW_OP_breg17:
1950     case DW_OP_breg18:
1951     case DW_OP_breg19:
1952     case DW_OP_breg20:
1953     case DW_OP_breg21:
1954     case DW_OP_breg22:
1955     case DW_OP_breg23:
1956     case DW_OP_breg24:
1957     case DW_OP_breg25:
1958     case DW_OP_breg26:
1959     case DW_OP_breg27:
1960     case DW_OP_breg28:
1961     case DW_OP_breg29:
1962     case DW_OP_breg30:
1963     case DW_OP_breg31:
1964       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1965       break;
1966     case DW_OP_regx:
1967       {
1968 	unsigned r = val1->v.val_unsigned;
1969 	if (for_eh_or_skip >= 0)
1970 	  r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1971 	gcc_assert (size_of_uleb128 (r)
1972 		    == size_of_uleb128 (val1->v.val_unsigned));
1973 	dw2_asm_output_data_uleb128 (r, NULL);
1974       }
1975       break;
1976     case DW_OP_fbreg:
1977       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1978       break;
1979     case DW_OP_bregx:
1980       {
1981 	unsigned r = val1->v.val_unsigned;
1982 	if (for_eh_or_skip >= 0)
1983 	  r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1984 	gcc_assert (size_of_uleb128 (r)
1985 		    == size_of_uleb128 (val1->v.val_unsigned));
1986 	dw2_asm_output_data_uleb128 (r, NULL);
1987 	dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1988       }
1989       break;
1990     case DW_OP_piece:
1991       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1992       break;
1993     case DW_OP_bit_piece:
1994       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1995       dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1996       break;
1997     case DW_OP_deref_size:
1998     case DW_OP_xderef_size:
1999       dw2_asm_output_data (1, val1->v.val_int, NULL);
2000       break;
2001 
2002     case DW_OP_addr:
2003       if (loc->dtprel)
2004 	{
2005 	  if (targetm.asm_out.output_dwarf_dtprel)
2006 	    {
2007 	      targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2008 						   DWARF2_ADDR_SIZE,
2009 						   val1->v.val_addr);
2010 	      fputc ('\n', asm_out_file);
2011 	    }
2012 	  else
2013 	    gcc_unreachable ();
2014 	}
2015       else
2016 	{
2017 #ifdef DWARF2_DEBUGGING_INFO
2018 	  dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2019 #else
2020 	  gcc_unreachable ();
2021 #endif
2022 	}
2023       break;
2024 
2025     case DW_OP_GNU_addr_index:
2026     case DW_OP_GNU_const_index:
2027       gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2028       dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2029                                    "(index into .debug_addr)");
2030       break;
2031 
2032     case DW_OP_GNU_implicit_pointer:
2033       {
2034 	char label[MAX_ARTIFICIAL_LABEL_BYTES
2035 		   + HOST_BITS_PER_WIDE_INT / 2 + 2];
2036 	gcc_assert (val1->val_class == dw_val_class_die_ref);
2037 	get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2038 	dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2039 	dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2040       }
2041       break;
2042 
2043     case DW_OP_GNU_entry_value:
2044       dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2045       output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2046       break;
2047 
2048     case DW_OP_GNU_const_type:
2049       {
2050 	unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2051 	gcc_assert (o);
2052 	dw2_asm_output_data_uleb128 (o, NULL);
2053 	switch (val2->val_class)
2054 	  {
2055 	  case dw_val_class_const:
2056 	    l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2057 	    dw2_asm_output_data (1, l, NULL);
2058 	    dw2_asm_output_data (l, val2->v.val_int, NULL);
2059 	    break;
2060 	  case dw_val_class_vec:
2061 	    {
2062 	      unsigned int elt_size = val2->v.val_vec.elt_size;
2063 	      unsigned int len = val2->v.val_vec.length;
2064 	      unsigned int i;
2065 	      unsigned char *p;
2066 
2067 	      l = len * elt_size;
2068 	      dw2_asm_output_data (1, l, NULL);
2069 	      if (elt_size > sizeof (HOST_WIDE_INT))
2070 		{
2071 		  elt_size /= 2;
2072 		  len *= 2;
2073 		}
2074 	      for (i = 0, p = val2->v.val_vec.array;
2075 		   i < len;
2076 		   i++, p += elt_size)
2077 		dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2078 				     "fp or vector constant word %u", i);
2079 	    }
2080 	    break;
2081 	  case dw_val_class_const_double:
2082 	    {
2083 	      unsigned HOST_WIDE_INT first, second;
2084 	      l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2085 
2086 	      dw2_asm_output_data (1, 2 * l, NULL);
2087 	      if (WORDS_BIG_ENDIAN)
2088 		{
2089 		  first = val2->v.val_double.high;
2090 		  second = val2->v.val_double.low;
2091 		}
2092 	      else
2093 		{
2094 		  first = val2->v.val_double.low;
2095 		  second = val2->v.val_double.high;
2096 		}
2097 	      dw2_asm_output_data (l, first, NULL);
2098 	      dw2_asm_output_data (l, second, NULL);
2099 	    }
2100 	    break;
2101 	  case dw_val_class_wide_int:
2102 	    {
2103 	      int i;
2104 	      int len = get_full_len (*val2->v.val_wide);
2105 	      l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2106 
2107 	      dw2_asm_output_data (1, len * l, NULL);
2108 	      if (WORDS_BIG_ENDIAN)
2109 		for (i = len - 1; i >= 0; --i)
2110 		  dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2111 	      else
2112 		for (i = 0; i < len; ++i)
2113 		  dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2114 	    }
2115 	    break;
2116 	  default:
2117 	    gcc_unreachable ();
2118 	  }
2119       }
2120       break;
2121     case DW_OP_GNU_regval_type:
2122       {
2123 	unsigned r = val1->v.val_unsigned;
2124 	unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2125 	gcc_assert (o);
2126 	if (for_eh_or_skip >= 0)
2127 	  {
2128 	    r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2129 	    gcc_assert (size_of_uleb128 (r)
2130 			== size_of_uleb128 (val1->v.val_unsigned));
2131 	  }
2132 	dw2_asm_output_data_uleb128 (r, NULL);
2133 	dw2_asm_output_data_uleb128 (o, NULL);
2134       }
2135       break;
2136     case DW_OP_GNU_deref_type:
2137       {
2138 	unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2139 	gcc_assert (o);
2140 	dw2_asm_output_data (1, val1->v.val_int, NULL);
2141 	dw2_asm_output_data_uleb128 (o, NULL);
2142       }
2143       break;
2144     case DW_OP_GNU_convert:
2145     case DW_OP_GNU_reinterpret:
2146       if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2147 	dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2148       else
2149 	{
2150 	  unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2151 	  gcc_assert (o);
2152 	  dw2_asm_output_data_uleb128 (o, NULL);
2153 	}
2154       break;
2155 
2156     case DW_OP_GNU_parameter_ref:
2157       {
2158 	unsigned long o;
2159 	gcc_assert (val1->val_class == dw_val_class_die_ref);
2160 	o = get_ref_die_offset (val1->v.val_die_ref.die);
2161 	dw2_asm_output_data (4, o, NULL);
2162       }
2163       break;
2164 
2165     default:
2166       /* Other codes have no operands.  */
2167       break;
2168     }
2169 }
2170 
2171 /* Output a sequence of location operations.
2172    The for_eh_or_skip parameter controls whether register numbers are
2173    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2174    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2175    info).  This should be suppressed for the cases that have not been converted
2176    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
2177 
2178 void
2179 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2180 {
2181   for (; loc != NULL; loc = loc->dw_loc_next)
2182     {
2183       enum dwarf_location_atom opc = loc->dw_loc_opc;
2184       /* Output the opcode.  */
2185       if (for_eh_or_skip >= 0
2186           && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2187 	{
2188 	  unsigned r = (opc - DW_OP_breg0);
2189 	  r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2190 	  gcc_assert (r <= 31);
2191 	  opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2192 	}
2193       else if (for_eh_or_skip >= 0
2194 	       && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2195 	{
2196 	  unsigned r = (opc - DW_OP_reg0);
2197 	  r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2198 	  gcc_assert (r <= 31);
2199 	  opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2200 	}
2201 
2202       dw2_asm_output_data (1, opc,
2203 			     "%s", dwarf_stack_op_name (opc));
2204 
2205       /* Output the operand(s) (if any).  */
2206       output_loc_operands (loc, for_eh_or_skip);
2207     }
2208 }
2209 
2210 /* Output location description stack opcode's operands (if any).
2211    The output is single bytes on a line, suitable for .cfi_escape.  */
2212 
2213 static void
2214 output_loc_operands_raw (dw_loc_descr_ref loc)
2215 {
2216   dw_val_ref val1 = &loc->dw_loc_oprnd1;
2217   dw_val_ref val2 = &loc->dw_loc_oprnd2;
2218 
2219   switch (loc->dw_loc_opc)
2220     {
2221     case DW_OP_addr:
2222     case DW_OP_GNU_addr_index:
2223     case DW_OP_GNU_const_index:
2224     case DW_OP_implicit_value:
2225       /* We cannot output addresses in .cfi_escape, only bytes.  */
2226       gcc_unreachable ();
2227 
2228     case DW_OP_const1u:
2229     case DW_OP_const1s:
2230     case DW_OP_pick:
2231     case DW_OP_deref_size:
2232     case DW_OP_xderef_size:
2233       fputc (',', asm_out_file);
2234       dw2_asm_output_data_raw (1, val1->v.val_int);
2235       break;
2236 
2237     case DW_OP_const2u:
2238     case DW_OP_const2s:
2239       fputc (',', asm_out_file);
2240       dw2_asm_output_data_raw (2, val1->v.val_int);
2241       break;
2242 
2243     case DW_OP_const4u:
2244     case DW_OP_const4s:
2245       fputc (',', asm_out_file);
2246       dw2_asm_output_data_raw (4, val1->v.val_int);
2247       break;
2248 
2249     case DW_OP_const8u:
2250     case DW_OP_const8s:
2251       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2252       fputc (',', asm_out_file);
2253       dw2_asm_output_data_raw (8, val1->v.val_int);
2254       break;
2255 
2256     case DW_OP_skip:
2257     case DW_OP_bra:
2258       {
2259 	int offset;
2260 
2261 	gcc_assert (val1->val_class == dw_val_class_loc);
2262 	offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2263 
2264         fputc (',', asm_out_file);
2265 	dw2_asm_output_data_raw (2, offset);
2266       }
2267       break;
2268 
2269     case DW_OP_regx:
2270       {
2271 	unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2272 	gcc_assert (size_of_uleb128 (r)
2273 		    == size_of_uleb128 (val1->v.val_unsigned));
2274 	fputc (',', asm_out_file);
2275 	dw2_asm_output_data_uleb128_raw (r);
2276       }
2277       break;
2278 
2279     case DW_OP_constu:
2280     case DW_OP_plus_uconst:
2281     case DW_OP_piece:
2282       fputc (',', asm_out_file);
2283       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2284       break;
2285 
2286     case DW_OP_bit_piece:
2287       fputc (',', asm_out_file);
2288       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2289       dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2290       break;
2291 
2292     case DW_OP_consts:
2293     case DW_OP_breg0:
2294     case DW_OP_breg1:
2295     case DW_OP_breg2:
2296     case DW_OP_breg3:
2297     case DW_OP_breg4:
2298     case DW_OP_breg5:
2299     case DW_OP_breg6:
2300     case DW_OP_breg7:
2301     case DW_OP_breg8:
2302     case DW_OP_breg9:
2303     case DW_OP_breg10:
2304     case DW_OP_breg11:
2305     case DW_OP_breg12:
2306     case DW_OP_breg13:
2307     case DW_OP_breg14:
2308     case DW_OP_breg15:
2309     case DW_OP_breg16:
2310     case DW_OP_breg17:
2311     case DW_OP_breg18:
2312     case DW_OP_breg19:
2313     case DW_OP_breg20:
2314     case DW_OP_breg21:
2315     case DW_OP_breg22:
2316     case DW_OP_breg23:
2317     case DW_OP_breg24:
2318     case DW_OP_breg25:
2319     case DW_OP_breg26:
2320     case DW_OP_breg27:
2321     case DW_OP_breg28:
2322     case DW_OP_breg29:
2323     case DW_OP_breg30:
2324     case DW_OP_breg31:
2325     case DW_OP_fbreg:
2326       fputc (',', asm_out_file);
2327       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2328       break;
2329 
2330     case DW_OP_bregx:
2331       {
2332 	unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2333 	gcc_assert (size_of_uleb128 (r)
2334 		    == size_of_uleb128 (val1->v.val_unsigned));
2335 	fputc (',', asm_out_file);
2336 	dw2_asm_output_data_uleb128_raw (r);
2337 	fputc (',', asm_out_file);
2338 	dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2339       }
2340       break;
2341 
2342     case DW_OP_GNU_implicit_pointer:
2343     case DW_OP_GNU_entry_value:
2344     case DW_OP_GNU_const_type:
2345     case DW_OP_GNU_regval_type:
2346     case DW_OP_GNU_deref_type:
2347     case DW_OP_GNU_convert:
2348     case DW_OP_GNU_reinterpret:
2349     case DW_OP_GNU_parameter_ref:
2350       gcc_unreachable ();
2351       break;
2352 
2353     default:
2354       /* Other codes have no operands.  */
2355       break;
2356     }
2357 }
2358 
2359 void
2360 output_loc_sequence_raw (dw_loc_descr_ref loc)
2361 {
2362   while (1)
2363     {
2364       enum dwarf_location_atom opc = loc->dw_loc_opc;
2365       /* Output the opcode.  */
2366       if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2367 	{
2368 	  unsigned r = (opc - DW_OP_breg0);
2369 	  r = DWARF2_FRAME_REG_OUT (r, 1);
2370 	  gcc_assert (r <= 31);
2371 	  opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2372 	}
2373       else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2374 	{
2375 	  unsigned r = (opc - DW_OP_reg0);
2376 	  r = DWARF2_FRAME_REG_OUT (r, 1);
2377 	  gcc_assert (r <= 31);
2378 	  opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2379 	}
2380       /* Output the opcode.  */
2381       fprintf (asm_out_file, "%#x", opc);
2382       output_loc_operands_raw (loc);
2383 
2384       if (!loc->dw_loc_next)
2385 	break;
2386       loc = loc->dw_loc_next;
2387 
2388       fputc (',', asm_out_file);
2389     }
2390 }
2391 
2392 /* This function builds a dwarf location descriptor sequence from a
2393    dw_cfa_location, adding the given OFFSET to the result of the
2394    expression.  */
2395 
2396 struct dw_loc_descr_node *
2397 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2398 {
2399   struct dw_loc_descr_node *head, *tmp;
2400 
2401   offset += cfa->offset;
2402 
2403   if (cfa->indirect)
2404     {
2405       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2406       head->dw_loc_oprnd1.val_class = dw_val_class_const;
2407       head->dw_loc_oprnd1.val_entry = NULL;
2408       tmp = new_loc_descr (DW_OP_deref, 0, 0);
2409       add_loc_descr (&head, tmp);
2410       if (offset != 0)
2411 	{
2412 	  tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2413 	  add_loc_descr (&head, tmp);
2414 	}
2415     }
2416   else
2417     head = new_reg_loc_descr (cfa->reg, offset);
2418 
2419   return head;
2420 }
2421 
2422 /* This function builds a dwarf location descriptor sequence for
2423    the address at OFFSET from the CFA when stack is aligned to
2424    ALIGNMENT byte.  */
2425 
2426 struct dw_loc_descr_node *
2427 build_cfa_aligned_loc (dw_cfa_location *cfa,
2428 		       HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2429 {
2430   struct dw_loc_descr_node *head;
2431   unsigned int dwarf_fp
2432     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2433 
2434   /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
2435   if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2436     {
2437       head = new_reg_loc_descr (dwarf_fp, 0);
2438       add_loc_descr (&head, int_loc_descriptor (alignment));
2439       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2440       loc_descr_plus_const (&head, offset);
2441     }
2442   else
2443     head = new_reg_loc_descr (dwarf_fp, offset);
2444   return head;
2445 }
2446 
2447 /* And now, the support for symbolic debugging information.  */
2448 
2449 /* .debug_str support.  */
2450 
2451 static void dwarf2out_init (const char *);
2452 static void dwarf2out_finish (const char *);
2453 static void dwarf2out_assembly_start (void);
2454 static void dwarf2out_define (unsigned int, const char *);
2455 static void dwarf2out_undef (unsigned int, const char *);
2456 static void dwarf2out_start_source_file (unsigned, const char *);
2457 static void dwarf2out_end_source_file (unsigned);
2458 static void dwarf2out_function_decl (tree);
2459 static void dwarf2out_begin_block (unsigned, unsigned);
2460 static void dwarf2out_end_block (unsigned, unsigned);
2461 static bool dwarf2out_ignore_block (const_tree);
2462 static void dwarf2out_global_decl (tree);
2463 static void dwarf2out_type_decl (tree, int);
2464 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2465 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2466 						 dw_die_ref);
2467 static void dwarf2out_abstract_function (tree);
2468 static void dwarf2out_var_location (rtx_insn *);
2469 static void dwarf2out_begin_function (tree);
2470 static void dwarf2out_end_function (unsigned int);
2471 static void dwarf2out_register_main_translation_unit (tree unit);
2472 static void dwarf2out_set_name (tree, tree);
2473 
2474 /* The debug hooks structure.  */
2475 
2476 const struct gcc_debug_hooks dwarf2_debug_hooks =
2477 {
2478   dwarf2out_init,
2479   dwarf2out_finish,
2480   dwarf2out_assembly_start,
2481   dwarf2out_define,
2482   dwarf2out_undef,
2483   dwarf2out_start_source_file,
2484   dwarf2out_end_source_file,
2485   dwarf2out_begin_block,
2486   dwarf2out_end_block,
2487   dwarf2out_ignore_block,
2488   dwarf2out_source_line,
2489   dwarf2out_begin_prologue,
2490 #if VMS_DEBUGGING_INFO
2491   dwarf2out_vms_end_prologue,
2492   dwarf2out_vms_begin_epilogue,
2493 #else
2494   debug_nothing_int_charstar,
2495   debug_nothing_int_charstar,
2496 #endif
2497   dwarf2out_end_epilogue,
2498   dwarf2out_begin_function,
2499   dwarf2out_end_function,	/* end_function */
2500   dwarf2out_register_main_translation_unit,
2501   dwarf2out_function_decl,	/* function_decl */
2502   dwarf2out_global_decl,
2503   dwarf2out_type_decl,		/* type_decl */
2504   dwarf2out_imported_module_or_decl,
2505   debug_nothing_tree,		/* deferred_inline_function */
2506   /* The DWARF 2 backend tries to reduce debugging bloat by not
2507      emitting the abstract description of inline functions until
2508      something tries to reference them.  */
2509   dwarf2out_abstract_function,	/* outlining_inline_function */
2510   debug_nothing_rtx_code_label,	/* label */
2511   debug_nothing_int,		/* handle_pch */
2512   dwarf2out_var_location,
2513   dwarf2out_switch_text_section,
2514   dwarf2out_set_name,
2515   1,                            /* start_end_main_source_file */
2516   TYPE_SYMTAB_IS_DIE            /* tree_type_symtab_field */
2517 };
2518 
2519 /* NOTE: In the comments in this file, many references are made to
2520    "Debugging Information Entries".  This term is abbreviated as `DIE'
2521    throughout the remainder of this file.  */
2522 
2523 /* An internal representation of the DWARF output is built, and then
2524    walked to generate the DWARF debugging info.  The walk of the internal
2525    representation is done after the entire program has been compiled.
2526    The types below are used to describe the internal representation.  */
2527 
2528 /* Whether to put type DIEs into their own section .debug_types instead
2529    of making them part of the .debug_info section.  Only supported for
2530    Dwarf V4 or higher and the user didn't disable them through
2531    -fno-debug-types-section.  It is more efficient to put them in a
2532    separate comdat sections since the linker will then be able to
2533    remove duplicates.  But not all tools support .debug_types sections
2534    yet.  */
2535 
2536 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2537 
2538 /* Various DIE's use offsets relative to the beginning of the
2539    .debug_info section to refer to each other.  */
2540 
2541 typedef long int dw_offset;
2542 
2543 /* Define typedefs here to avoid circular dependencies.  */
2544 
2545 typedef struct dw_attr_struct *dw_attr_ref;
2546 typedef struct dw_line_info_struct *dw_line_info_ref;
2547 typedef struct pubname_struct *pubname_ref;
2548 typedef struct dw_ranges_struct *dw_ranges_ref;
2549 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2550 typedef struct comdat_type_struct *comdat_type_node_ref;
2551 
2552 /* The entries in the line_info table more-or-less mirror the opcodes
2553    that are used in the real dwarf line table.  Arrays of these entries
2554    are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2555    supported.  */
2556 
2557 enum dw_line_info_opcode {
2558   /* Emit DW_LNE_set_address; the operand is the label index.  */
2559   LI_set_address,
2560 
2561   /* Emit a row to the matrix with the given line.  This may be done
2562      via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2563      special opcodes.  */
2564   LI_set_line,
2565 
2566   /* Emit a DW_LNS_set_file.  */
2567   LI_set_file,
2568 
2569   /* Emit a DW_LNS_set_column.  */
2570   LI_set_column,
2571 
2572   /* Emit a DW_LNS_negate_stmt; the operand is ignored.  */
2573   LI_negate_stmt,
2574 
2575   /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored.  */
2576   LI_set_prologue_end,
2577   LI_set_epilogue_begin,
2578 
2579   /* Emit a DW_LNE_set_discriminator.  */
2580   LI_set_discriminator
2581 };
2582 
2583 typedef struct GTY(()) dw_line_info_struct {
2584   enum dw_line_info_opcode opcode;
2585   unsigned int val;
2586 } dw_line_info_entry;
2587 
2588 
2589 typedef struct GTY(()) dw_line_info_table_struct {
2590   /* The label that marks the end of this section.  */
2591   const char *end_label;
2592 
2593   /* The values for the last row of the matrix, as collected in the table.
2594      These are used to minimize the changes to the next row.  */
2595   unsigned int file_num;
2596   unsigned int line_num;
2597   unsigned int column_num;
2598   int discrim_num;
2599   bool is_stmt;
2600   bool in_use;
2601 
2602   vec<dw_line_info_entry, va_gc> *entries;
2603 } dw_line_info_table;
2604 
2605 typedef dw_line_info_table *dw_line_info_table_p;
2606 
2607 
2608 /* Each DIE attribute has a field specifying the attribute kind,
2609    a link to the next attribute in the chain, and an attribute value.
2610    Attributes are typically linked below the DIE they modify.  */
2611 
2612 typedef struct GTY(()) dw_attr_struct {
2613   enum dwarf_attribute dw_attr;
2614   dw_val_node dw_attr_val;
2615 }
2616 dw_attr_node;
2617 
2618 
2619 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
2620    The children of each node form a circular list linked by
2621    die_sib.  die_child points to the node *before* the "first" child node.  */
2622 
2623 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
2624   union die_symbol_or_type_node
2625     {
2626       const char * GTY ((tag ("0"))) die_symbol;
2627       comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2628     }
2629   GTY ((desc ("%0.comdat_type_p"))) die_id;
2630   vec<dw_attr_node, va_gc> *die_attr;
2631   dw_die_ref die_parent;
2632   dw_die_ref die_child;
2633   dw_die_ref die_sib;
2634   dw_die_ref die_definition; /* ref from a specification to its definition */
2635   dw_offset die_offset;
2636   unsigned long die_abbrev;
2637   int die_mark;
2638   unsigned int decl_id;
2639   enum dwarf_tag die_tag;
2640   /* Die is used and must not be pruned as unused.  */
2641   BOOL_BITFIELD die_perennial_p : 1;
2642   BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2643   /* Lots of spare bits.  */
2644 }
2645 die_node;
2646 
2647 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
2648 #define FOR_EACH_CHILD(die, c, expr) do {	\
2649   c = die->die_child;				\
2650   if (c) do {					\
2651     c = c->die_sib;				\
2652     expr;					\
2653   } while (c != die->die_child);		\
2654 } while (0)
2655 
2656 /* The pubname structure */
2657 
2658 typedef struct GTY(()) pubname_struct {
2659   dw_die_ref die;
2660   const char *name;
2661 }
2662 pubname_entry;
2663 
2664 
2665 struct GTY(()) dw_ranges_struct {
2666   /* If this is positive, it's a block number, otherwise it's a
2667      bitwise-negated index into dw_ranges_by_label.  */
2668   int num;
2669 };
2670 
2671 /* A structure to hold a macinfo entry.  */
2672 
2673 typedef struct GTY(()) macinfo_struct {
2674   unsigned char code;
2675   unsigned HOST_WIDE_INT lineno;
2676   const char *info;
2677 }
2678 macinfo_entry;
2679 
2680 
2681 struct GTY(()) dw_ranges_by_label_struct {
2682   const char *begin;
2683   const char *end;
2684 };
2685 
2686 /* The comdat type node structure.  */
2687 typedef struct GTY(()) comdat_type_struct
2688 {
2689   dw_die_ref root_die;
2690   dw_die_ref type_die;
2691   dw_die_ref skeleton_die;
2692   char signature[DWARF_TYPE_SIGNATURE_SIZE];
2693   struct comdat_type_struct *next;
2694 }
2695 comdat_type_node;
2696 
2697 /* The limbo die list structure.  */
2698 typedef struct GTY(()) limbo_die_struct {
2699   dw_die_ref die;
2700   tree created_for;
2701   struct limbo_die_struct *next;
2702 }
2703 limbo_die_node;
2704 
2705 typedef struct skeleton_chain_struct
2706 {
2707   dw_die_ref old_die;
2708   dw_die_ref new_die;
2709   struct skeleton_chain_struct *parent;
2710 }
2711 skeleton_chain_node;
2712 
2713 /* Define a macro which returns nonzero for a TYPE_DECL which was
2714    implicitly generated for a type.
2715 
2716    Note that, unlike the C front-end (which generates a NULL named
2717    TYPE_DECL node for each complete tagged type, each array type,
2718    and each function type node created) the C++ front-end generates
2719    a _named_ TYPE_DECL node for each tagged type node created.
2720    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2721    generate a DW_TAG_typedef DIE for them.  Likewise with the Ada
2722    front-end, but for each type, tagged or not.  */
2723 
2724 #define TYPE_DECL_IS_STUB(decl)				\
2725   (DECL_NAME (decl) == NULL_TREE			\
2726    || (DECL_ARTIFICIAL (decl)				\
2727        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))	\
2728 	   /* This is necessary for stub decls that	\
2729 	      appear in nested inline functions.  */	\
2730 	   || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE	\
2731 	       && (decl_ultimate_origin (decl)		\
2732 		   == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2733 
2734 /* Information concerning the compilation unit's programming
2735    language, and compiler version.  */
2736 
2737 /* Fixed size portion of the DWARF compilation unit header.  */
2738 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2739   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2740 
2741 /* Fixed size portion of the DWARF comdat type unit header.  */
2742 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2743   (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2744    + DWARF_OFFSET_SIZE)
2745 
2746 /* Fixed size portion of public names info.  */
2747 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2748 
2749 /* Fixed size portion of the address range info.  */
2750 #define DWARF_ARANGES_HEADER_SIZE					\
2751   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,	\
2752 		DWARF2_ADDR_SIZE * 2)					\
2753    - DWARF_INITIAL_LENGTH_SIZE)
2754 
2755 /* Size of padding portion in the address range info.  It must be
2756    aligned to twice the pointer size.  */
2757 #define DWARF_ARANGES_PAD_SIZE \
2758   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2759 		DWARF2_ADDR_SIZE * 2)				   \
2760    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2761 
2762 /* Use assembler line directives if available.  */
2763 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2764 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2765 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2766 #else
2767 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2768 #endif
2769 #endif
2770 
2771 /* Minimum line offset in a special line info. opcode.
2772    This value was chosen to give a reasonable range of values.  */
2773 #define DWARF_LINE_BASE  -10
2774 
2775 /* First special line opcode - leave room for the standard opcodes.  */
2776 #define DWARF_LINE_OPCODE_BASE  ((int)DW_LNS_set_isa + 1)
2777 
2778 /* Range of line offsets in a special line info. opcode.  */
2779 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
2780 
2781 /* Flag that indicates the initial value of the is_stmt_start flag.
2782    In the present implementation, we do not mark any lines as
2783    the beginning of a source statement, because that information
2784    is not made available by the GCC front-end.  */
2785 #define	DWARF_LINE_DEFAULT_IS_STMT_START 1
2786 
2787 /* Maximum number of operations per instruction bundle.  */
2788 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2789 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2790 #endif
2791 
2792 /* This location is used by calc_die_sizes() to keep track
2793    the offset of each DIE within the .debug_info section.  */
2794 static unsigned long next_die_offset;
2795 
2796 /* Record the root of the DIE's built for the current compilation unit.  */
2797 static GTY(()) dw_die_ref single_comp_unit_die;
2798 
2799 /* A list of type DIEs that have been separated into comdat sections.  */
2800 static GTY(()) comdat_type_node *comdat_type_list;
2801 
2802 /* A list of DIEs with a NULL parent waiting to be relocated.  */
2803 static GTY(()) limbo_die_node *limbo_die_list;
2804 
2805 /* A list of DIEs for which we may have to generate
2806    DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set.  */
2807 static GTY(()) limbo_die_node *deferred_asm_name;
2808 
2809 struct dwarf_file_hasher : ggc_hasher<dwarf_file_data *>
2810 {
2811   typedef const char *compare_type;
2812 
2813   static hashval_t hash (dwarf_file_data *);
2814   static bool equal (dwarf_file_data *, const char *);
2815 };
2816 
2817 /* Filenames referenced by this compilation unit.  */
2818 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
2819 
2820 struct decl_die_hasher : ggc_hasher<die_node *>
2821 {
2822   typedef tree compare_type;
2823 
2824   static hashval_t hash (die_node *);
2825   static bool equal (die_node *, tree);
2826 };
2827 /* A hash table of references to DIE's that describe declarations.
2828    The key is a DECL_UID() which is a unique number identifying each decl.  */
2829 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
2830 
2831 struct block_die_hasher : ggc_hasher<die_struct *>
2832 {
2833   static hashval_t hash (die_struct *);
2834   static bool equal (die_struct *, die_struct *);
2835 };
2836 
2837 /* A hash table of references to DIE's that describe COMMON blocks.
2838    The key is DECL_UID() ^ die_parent.  */
2839 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
2840 
2841 typedef struct GTY(()) die_arg_entry_struct {
2842     dw_die_ref die;
2843     tree arg;
2844 } die_arg_entry;
2845 
2846 
2847 /* Node of the variable location list.  */
2848 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2849   /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2850      EXPR_LIST chain.  For small bitsizes, bitsize is encoded
2851      in mode of the EXPR_LIST node and first EXPR_LIST operand
2852      is either NOTE_INSN_VAR_LOCATION for a piece with a known
2853      location or NULL for padding.  For larger bitsizes,
2854      mode is 0 and first operand is a CONCAT with bitsize
2855      as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2856      NULL as second operand.  */
2857   rtx GTY (()) loc;
2858   const char * GTY (()) label;
2859   struct var_loc_node * GTY (()) next;
2860 };
2861 
2862 /* Variable location list.  */
2863 struct GTY ((for_user)) var_loc_list_def {
2864   struct var_loc_node * GTY (()) first;
2865 
2866   /* Pointer to the last but one or last element of the
2867      chained list.  If the list is empty, both first and
2868      last are NULL, if the list contains just one node
2869      or the last node certainly is not redundant, it points
2870      to the last node, otherwise points to the last but one.
2871      Do not mark it for GC because it is marked through the chain.  */
2872   struct var_loc_node * GTY ((skip ("%h"))) last;
2873 
2874   /* Pointer to the last element before section switch,
2875      if NULL, either sections weren't switched or first
2876      is after section switch.  */
2877   struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2878 
2879   /* DECL_UID of the variable decl.  */
2880   unsigned int decl_id;
2881 };
2882 typedef struct var_loc_list_def var_loc_list;
2883 
2884 /* Call argument location list.  */
2885 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2886   rtx GTY (()) call_arg_loc_note;
2887   const char * GTY (()) label;
2888   tree GTY (()) block;
2889   bool tail_call_p;
2890   rtx GTY (()) symbol_ref;
2891   struct call_arg_loc_node * GTY (()) next;
2892 };
2893 
2894 
2895 struct decl_loc_hasher : ggc_hasher<var_loc_list *>
2896 {
2897   typedef const_tree compare_type;
2898 
2899   static hashval_t hash (var_loc_list *);
2900   static bool equal (var_loc_list *, const_tree);
2901 };
2902 
2903 /* Table of decl location linked lists.  */
2904 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
2905 
2906 /* Head and tail of call_arg_loc chain.  */
2907 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2908 static struct call_arg_loc_node *call_arg_loc_last;
2909 
2910 /* Number of call sites in the current function.  */
2911 static int call_site_count = -1;
2912 /* Number of tail call sites in the current function.  */
2913 static int tail_call_site_count = -1;
2914 
2915 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
2916    DIEs.  */
2917 static vec<dw_die_ref> block_map;
2918 
2919 /* A cached location list.  */
2920 struct GTY ((for_user)) cached_dw_loc_list_def {
2921   /* The DECL_UID of the decl that this entry describes.  */
2922   unsigned int decl_id;
2923 
2924   /* The cached location list.  */
2925   dw_loc_list_ref loc_list;
2926 };
2927 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2928 
2929 struct dw_loc_list_hasher : ggc_hasher<cached_dw_loc_list *>
2930 {
2931 
2932   typedef const_tree compare_type;
2933 
2934   static hashval_t hash (cached_dw_loc_list *);
2935   static bool equal (cached_dw_loc_list *, const_tree);
2936 };
2937 
2938 /* Table of cached location lists.  */
2939 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
2940 
2941 /* A pointer to the base of a list of references to DIE's that
2942    are uniquely identified by their tag, presence/absence of
2943    children DIE's, and list of attribute/value pairs.  */
2944 static GTY((length ("abbrev_die_table_allocated")))
2945   dw_die_ref *abbrev_die_table;
2946 
2947 /* Number of elements currently allocated for abbrev_die_table.  */
2948 static GTY(()) unsigned abbrev_die_table_allocated;
2949 
2950 /* Number of elements in type_die_table currently in use.  */
2951 static GTY(()) unsigned abbrev_die_table_in_use;
2952 
2953 /* Size (in elements) of increments by which we may expand the
2954    abbrev_die_table.  */
2955 #define ABBREV_DIE_TABLE_INCREMENT 256
2956 
2957 /* A global counter for generating labels for line number data.  */
2958 static unsigned int line_info_label_num;
2959 
2960 /* The current table to which we should emit line number information
2961    for the current function.  This will be set up at the beginning of
2962    assembly for the function.  */
2963 static dw_line_info_table *cur_line_info_table;
2964 
2965 /* The two default tables of line number info.  */
2966 static GTY(()) dw_line_info_table *text_section_line_info;
2967 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2968 
2969 /* The set of all non-default tables of line number info.  */
2970 static GTY(()) vec<dw_line_info_table_p, va_gc> *separate_line_info;
2971 
2972 /* A flag to tell pubnames/types export if there is an info section to
2973    refer to.  */
2974 static bool info_section_emitted;
2975 
2976 /* A pointer to the base of a table that contains a list of publicly
2977    accessible names.  */
2978 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
2979 
2980 /* A pointer to the base of a table that contains a list of publicly
2981    accessible types.  */
2982 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
2983 
2984 /* A pointer to the base of a table that contains a list of macro
2985    defines/undefines (and file start/end markers).  */
2986 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
2987 
2988 /* True if .debug_macinfo or .debug_macros section is going to be
2989    emitted.  */
2990 #define have_macinfo \
2991   (debug_info_level >= DINFO_LEVEL_VERBOSE \
2992    && !macinfo_table->is_empty ())
2993 
2994 /* Array of dies for which we should generate .debug_ranges info.  */
2995 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
2996 
2997 /* Number of elements currently allocated for ranges_table.  */
2998 static GTY(()) unsigned ranges_table_allocated;
2999 
3000 /* Number of elements in ranges_table currently in use.  */
3001 static GTY(()) unsigned ranges_table_in_use;
3002 
3003 /* Array of pairs of labels referenced in ranges_table.  */
3004 static GTY ((length ("ranges_by_label_allocated")))
3005      dw_ranges_by_label_ref ranges_by_label;
3006 
3007 /* Number of elements currently allocated for ranges_by_label.  */
3008 static GTY(()) unsigned ranges_by_label_allocated;
3009 
3010 /* Number of elements in ranges_by_label currently in use.  */
3011 static GTY(()) unsigned ranges_by_label_in_use;
3012 
3013 /* Size (in elements) of increments by which we may expand the
3014    ranges_table.  */
3015 #define RANGES_TABLE_INCREMENT 64
3016 
3017 /* Whether we have location lists that need outputting */
3018 static GTY(()) bool have_location_lists;
3019 
3020 /* Unique label counter.  */
3021 static GTY(()) unsigned int loclabel_num;
3022 
3023 /* Unique label counter for point-of-call tables.  */
3024 static GTY(()) unsigned int poc_label_num;
3025 
3026 /* The last file entry emitted by maybe_emit_file().  */
3027 static GTY(()) struct dwarf_file_data * last_emitted_file;
3028 
3029 /* Number of internal labels generated by gen_internal_sym().  */
3030 static GTY(()) int label_num;
3031 
3032 /* Cached result of previous call to lookup_filename.  */
3033 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
3034 
3035 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3036 
3037 /* Instances of generic types for which we need to generate debug
3038    info that describe their generic parameters and arguments. That
3039    generation needs to happen once all types are properly laid out so
3040    we do it at the end of compilation.  */
3041 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3042 
3043 /* Offset from the "steady-state frame pointer" to the frame base,
3044    within the current function.  */
3045 static HOST_WIDE_INT frame_pointer_fb_offset;
3046 static bool frame_pointer_fb_offset_valid;
3047 
3048 static vec<dw_die_ref> base_types;
3049 
3050 /* Flags to represent a set of attribute classes for attributes that represent
3051    a scalar value (bounds, pointers, ...).  */
3052 enum dw_scalar_form
3053 {
3054   dw_scalar_form_constant = 0x01,
3055   dw_scalar_form_exprloc = 0x02,
3056   dw_scalar_form_reference = 0x04
3057 };
3058 
3059 /* Forward declarations for functions defined in this file.  */
3060 
3061 static int is_pseudo_reg (const_rtx);
3062 static tree type_main_variant (tree);
3063 static int is_tagged_type (const_tree);
3064 static const char *dwarf_tag_name (unsigned);
3065 static const char *dwarf_attr_name (unsigned);
3066 static const char *dwarf_form_name (unsigned);
3067 static tree decl_ultimate_origin (const_tree);
3068 static tree decl_class_context (tree);
3069 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
3070 static inline enum dw_val_class AT_class (dw_attr_ref);
3071 static inline unsigned int AT_index (dw_attr_ref);
3072 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3073 static inline unsigned AT_flag (dw_attr_ref);
3074 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3075 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
3076 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3077 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
3078 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3079 			   HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3080 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3081 			       unsigned int, unsigned char *);
3082 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3083 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3084 static inline const char *AT_string (dw_attr_ref);
3085 static enum dwarf_form AT_string_form (dw_attr_ref);
3086 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3087 static void add_AT_specification (dw_die_ref, dw_die_ref);
3088 static inline dw_die_ref AT_ref (dw_attr_ref);
3089 static inline int AT_ref_external (dw_attr_ref);
3090 static inline void set_AT_ref_external (dw_attr_ref, int);
3091 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3092 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3093 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
3094 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3095 			     dw_loc_list_ref);
3096 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
3097 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3098 static void remove_addr_table_entry (addr_table_entry *);
3099 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3100 static inline rtx AT_addr (dw_attr_ref);
3101 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3102 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3103 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3104 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3105 			   unsigned HOST_WIDE_INT);
3106 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3107                                unsigned long, bool);
3108 static inline const char *AT_lbl (dw_attr_ref);
3109 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
3110 static const char *get_AT_low_pc (dw_die_ref);
3111 static const char *get_AT_hi_pc (dw_die_ref);
3112 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3113 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3114 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3115 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3116 static bool is_cxx (void);
3117 static bool is_fortran (void);
3118 static bool is_ada (void);
3119 static void remove_AT (dw_die_ref, enum dwarf_attribute);
3120 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3121 static void add_child_die (dw_die_ref, dw_die_ref);
3122 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3123 static dw_die_ref lookup_type_die (tree);
3124 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3125 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3126 static void equate_type_number_to_die (tree, dw_die_ref);
3127 static dw_die_ref lookup_decl_die (tree);
3128 static var_loc_list *lookup_decl_loc (const_tree);
3129 static void equate_decl_number_to_die (tree, dw_die_ref);
3130 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3131 static void print_spaces (FILE *);
3132 static void print_die (dw_die_ref, FILE *);
3133 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3134 static dw_die_ref pop_compile_unit (dw_die_ref);
3135 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3136 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3137 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3138 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3139 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3140 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3141 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
3142 				   struct md5_ctx *, int *);
3143 struct checksum_attributes;
3144 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3145 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3146 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3147 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3148 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3149 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3150 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3151 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3152 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3153 static void compute_section_prefix (dw_die_ref);
3154 static int is_type_die (dw_die_ref);
3155 static int is_comdat_die (dw_die_ref);
3156 static int is_symbol_die (dw_die_ref);
3157 static inline bool is_template_instantiation (dw_die_ref);
3158 static void assign_symbol_names (dw_die_ref);
3159 static void break_out_includes (dw_die_ref);
3160 static int is_declaration_die (dw_die_ref);
3161 static int should_move_die_to_comdat (dw_die_ref);
3162 static dw_die_ref clone_as_declaration (dw_die_ref);
3163 static dw_die_ref clone_die (dw_die_ref);
3164 static dw_die_ref clone_tree (dw_die_ref);
3165 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3166 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3167 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3168 static dw_die_ref generate_skeleton (dw_die_ref);
3169 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3170                                                          dw_die_ref,
3171                                                          dw_die_ref);
3172 static void break_out_comdat_types (dw_die_ref);
3173 static void copy_decls_for_unworthy_types (dw_die_ref);
3174 
3175 static void add_sibling_attributes (dw_die_ref);
3176 static void output_location_lists (dw_die_ref);
3177 static int constant_size (unsigned HOST_WIDE_INT);
3178 static unsigned long size_of_die (dw_die_ref);
3179 static void calc_die_sizes (dw_die_ref);
3180 static void calc_base_type_die_sizes (void);
3181 static void mark_dies (dw_die_ref);
3182 static void unmark_dies (dw_die_ref);
3183 static void unmark_all_dies (dw_die_ref);
3184 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3185 static unsigned long size_of_aranges (void);
3186 static enum dwarf_form value_format (dw_attr_ref);
3187 static void output_value_format (dw_attr_ref);
3188 static void output_abbrev_section (void);
3189 static void output_die_abbrevs (unsigned long, dw_die_ref);
3190 static void output_die_symbol (dw_die_ref);
3191 static void output_die (dw_die_ref);
3192 static void output_compilation_unit_header (void);
3193 static void output_comp_unit (dw_die_ref, int);
3194 static void output_comdat_type_unit (comdat_type_node *);
3195 static const char *dwarf2_name (tree, int);
3196 static void add_pubname (tree, dw_die_ref);
3197 static void add_enumerator_pubname (const char *, dw_die_ref);
3198 static void add_pubname_string (const char *, dw_die_ref);
3199 static void add_pubtype (tree, dw_die_ref);
3200 static void output_pubnames (vec<pubname_entry, va_gc> *);
3201 static void output_aranges (unsigned long);
3202 static unsigned int add_ranges_num (int);
3203 static unsigned int add_ranges (const_tree);
3204 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3205                                   bool *, bool);
3206 static void output_ranges (void);
3207 static dw_line_info_table *new_line_info_table (void);
3208 static void output_line_info (bool);
3209 static void output_file_names (void);
3210 static dw_die_ref base_type_die (tree);
3211 static int is_base_type (tree);
3212 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3213 static int decl_quals (const_tree);
3214 static dw_die_ref modified_type_die (tree, int, dw_die_ref);
3215 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3216 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3217 static int type_is_enum (const_tree);
3218 static unsigned int dbx_reg_number (const_rtx);
3219 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3220 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3221 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3222 						enum var_init_status);
3223 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3224 						     enum var_init_status);
3225 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3226 					 enum var_init_status);
3227 static int is_based_loc (const_rtx);
3228 static bool resolve_one_addr (rtx *);
3229 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3230 					       enum var_init_status);
3231 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3232 					enum var_init_status);
3233 struct loc_descr_context;
3234 static dw_loc_list_ref loc_list_from_tree (tree, int,
3235 					   const struct loc_descr_context *);
3236 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3237 						  const struct loc_descr_context *);
3238 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3239 static tree field_type (const_tree);
3240 static unsigned int simple_type_align_in_bits (const_tree);
3241 static unsigned int simple_decl_align_in_bits (const_tree);
3242 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3243 static HOST_WIDE_INT field_byte_offset (const_tree);
3244 static void add_AT_location_description	(dw_die_ref, enum dwarf_attribute,
3245 					 dw_loc_list_ref);
3246 static void add_data_member_location_attribute (dw_die_ref, tree);
3247 static bool add_const_value_attribute (dw_die_ref, rtx);
3248 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3249 static void insert_wide_int (const wide_int &, unsigned char *, int);
3250 static void insert_float (const_rtx, unsigned char *);
3251 static rtx rtl_for_decl_location (tree);
3252 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3253 						   enum dwarf_attribute);
3254 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3255 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3256 static void add_name_attribute (dw_die_ref, const char *);
3257 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3258 static void add_comp_dir_attribute (dw_die_ref);
3259 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3260 			     const struct loc_descr_context *);
3261 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3262 			    const struct loc_descr_context *);
3263 static void add_subscript_info (dw_die_ref, tree, bool);
3264 static void add_byte_size_attribute (dw_die_ref, tree);
3265 static void add_bit_offset_attribute (dw_die_ref, tree);
3266 static void add_bit_size_attribute (dw_die_ref, tree);
3267 static void add_prototyped_attribute (dw_die_ref, tree);
3268 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3269 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3270 static void add_src_coords_attributes (dw_die_ref, tree);
3271 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3272 static void push_decl_scope (tree);
3273 static void pop_decl_scope (void);
3274 static dw_die_ref scope_die_for (tree, dw_die_ref);
3275 static inline int local_scope_p (dw_die_ref);
3276 static inline int class_scope_p (dw_die_ref);
3277 static inline int class_or_namespace_scope_p (dw_die_ref);
3278 static void add_type_attribute (dw_die_ref, tree, int, dw_die_ref);
3279 static void add_calling_convention_attribute (dw_die_ref, tree);
3280 static const char *type_tag (const_tree);
3281 static tree member_declared_type (const_tree);
3282 #if 0
3283 static const char *decl_start_label (tree);
3284 #endif
3285 static void gen_array_type_die (tree, dw_die_ref);
3286 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3287 #if 0
3288 static void gen_entry_point_die (tree, dw_die_ref);
3289 #endif
3290 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3291 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3292 static dw_die_ref gen_formal_parameter_pack_die  (tree, tree, dw_die_ref, tree*);
3293 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3294 static void gen_formal_types_die (tree, dw_die_ref);
3295 static void gen_subprogram_die (tree, dw_die_ref);
3296 static void gen_variable_die (tree, tree, dw_die_ref);
3297 static void gen_const_die (tree, dw_die_ref);
3298 static void gen_label_die (tree, dw_die_ref);
3299 static void gen_lexical_block_die (tree, dw_die_ref);
3300 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3301 static void gen_field_die (tree, dw_die_ref);
3302 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3303 static dw_die_ref gen_compile_unit_die (const char *);
3304 static void gen_inheritance_die (tree, tree, dw_die_ref);
3305 static void gen_member_die (tree, dw_die_ref);
3306 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3307 						enum debug_info_usage);
3308 static void gen_subroutine_type_die (tree, dw_die_ref);
3309 static void gen_typedef_die (tree, dw_die_ref);
3310 static void gen_type_die (tree, dw_die_ref);
3311 static void gen_block_die (tree, dw_die_ref);
3312 static void decls_for_scope (tree, dw_die_ref);
3313 static inline int is_redundant_typedef (const_tree);
3314 static bool is_naming_typedef_decl (const_tree);
3315 static inline dw_die_ref get_context_die (tree);
3316 static void gen_namespace_die (tree, dw_die_ref);
3317 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3318 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3319 static dw_die_ref force_decl_die (tree);
3320 static dw_die_ref force_type_die (tree);
3321 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3322 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3323 static struct dwarf_file_data * lookup_filename (const char *);
3324 static void retry_incomplete_types (void);
3325 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3326 static void gen_generic_params_dies (tree);
3327 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3328 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3329 static void splice_child_die (dw_die_ref, dw_die_ref);
3330 static int file_info_cmp (const void *, const void *);
3331 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3332 				     const char *, const char *);
3333 static void output_loc_list (dw_loc_list_ref);
3334 static char *gen_internal_sym (const char *);
3335 static bool want_pubnames (void);
3336 
3337 static void prune_unmark_dies (dw_die_ref);
3338 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3339 static void prune_unused_types_mark (dw_die_ref, int);
3340 static void prune_unused_types_walk (dw_die_ref);
3341 static void prune_unused_types_walk_attribs (dw_die_ref);
3342 static void prune_unused_types_prune (dw_die_ref);
3343 static void prune_unused_types (void);
3344 static int maybe_emit_file (struct dwarf_file_data *fd);
3345 static inline const char *AT_vms_delta1 (dw_attr_ref);
3346 static inline const char *AT_vms_delta2 (dw_attr_ref);
3347 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3348 				     const char *, const char *);
3349 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3350 static void gen_remaining_tmpl_value_param_die_attribute (void);
3351 static bool generic_type_p (tree);
3352 static void schedule_generic_params_dies_gen (tree t);
3353 static void gen_scheduled_generic_parms_dies (void);
3354 
3355 static const char *comp_dir_string (void);
3356 
3357 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3358 
3359 /* enum for tracking thread-local variables whose address is really an offset
3360    relative to the TLS pointer, which will need link-time relocation, but will
3361    not need relocation by the DWARF consumer.  */
3362 
3363 enum dtprel_bool
3364 {
3365   dtprel_false = 0,
3366   dtprel_true = 1
3367 };
3368 
3369 /* Return the operator to use for an address of a variable.  For dtprel_true, we
3370    use DW_OP_const*.  For regular variables, which need both link-time
3371    relocation and consumer-level relocation (e.g., to account for shared objects
3372    loaded at a random address), we use DW_OP_addr*.  */
3373 
3374 static inline enum dwarf_location_atom
3375 dw_addr_op (enum dtprel_bool dtprel)
3376 {
3377   if (dtprel == dtprel_true)
3378     return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3379             : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3380   else
3381     return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3382 }
3383 
3384 /* Return a pointer to a newly allocated address location description.  If
3385    dwarf_split_debug_info is true, then record the address with the appropriate
3386    relocation.  */
3387 static inline dw_loc_descr_ref
3388 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3389 {
3390   dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3391 
3392   ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3393   ref->dw_loc_oprnd1.v.val_addr = addr;
3394   ref->dtprel = dtprel;
3395   if (dwarf_split_debug_info)
3396     ref->dw_loc_oprnd1.val_entry
3397         = add_addr_table_entry (addr,
3398                                 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3399   else
3400     ref->dw_loc_oprnd1.val_entry = NULL;
3401 
3402   return ref;
3403 }
3404 
3405 /* Section names used to hold DWARF debugging information.  */
3406 
3407 #ifndef DEBUG_INFO_SECTION
3408 #define DEBUG_INFO_SECTION	".debug_info"
3409 #endif
3410 #ifndef DEBUG_DWO_INFO_SECTION
3411 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3412 #endif
3413 #ifndef DEBUG_ABBREV_SECTION
3414 #define DEBUG_ABBREV_SECTION	".debug_abbrev"
3415 #endif
3416 #ifndef DEBUG_DWO_ABBREV_SECTION
3417 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3418 #endif
3419 #ifndef DEBUG_ARANGES_SECTION
3420 #define DEBUG_ARANGES_SECTION	".debug_aranges"
3421 #endif
3422 #ifndef DEBUG_ADDR_SECTION
3423 #define DEBUG_ADDR_SECTION     ".debug_addr"
3424 #endif
3425 #ifndef DEBUG_NORM_MACINFO_SECTION
3426 #define DEBUG_NORM_MACINFO_SECTION     ".debug_macinfo"
3427 #endif
3428 #ifndef DEBUG_DWO_MACINFO_SECTION
3429 #define DEBUG_DWO_MACINFO_SECTION      ".debug_macinfo.dwo"
3430 #endif
3431 #ifndef DEBUG_MACINFO_SECTION
3432 #define DEBUG_MACINFO_SECTION                                           \
3433   (!dwarf_split_debug_info                                              \
3434    ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3435 #endif
3436 #ifndef DEBUG_NORM_MACRO_SECTION
3437 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3438 #endif
3439 #ifndef DEBUG_DWO_MACRO_SECTION
3440 #define DEBUG_DWO_MACRO_SECTION        ".debug_macro.dwo"
3441 #endif
3442 #ifndef DEBUG_MACRO_SECTION
3443 #define DEBUG_MACRO_SECTION                                             \
3444   (!dwarf_split_debug_info                                              \
3445    ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3446 #endif
3447 #ifndef DEBUG_LINE_SECTION
3448 #define DEBUG_LINE_SECTION	".debug_line"
3449 #endif
3450 #ifndef DEBUG_DWO_LINE_SECTION
3451 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3452 #endif
3453 #ifndef DEBUG_LOC_SECTION
3454 #define DEBUG_LOC_SECTION	".debug_loc"
3455 #endif
3456 #ifndef DEBUG_DWO_LOC_SECTION
3457 #define DEBUG_DWO_LOC_SECTION  ".debug_loc.dwo"
3458 #endif
3459 #ifndef DEBUG_PUBNAMES_SECTION
3460 #define DEBUG_PUBNAMES_SECTION	\
3461   ((debug_generate_pub_sections == 2) \
3462    ? ".debug_gnu_pubnames" : ".debug_pubnames")
3463 #endif
3464 #ifndef DEBUG_PUBTYPES_SECTION
3465 #define DEBUG_PUBTYPES_SECTION	\
3466   ((debug_generate_pub_sections == 2) \
3467    ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3468 #endif
3469 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3470 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3471 #ifndef DEBUG_STR_OFFSETS_SECTION
3472 #define DEBUG_STR_OFFSETS_SECTION                                       \
3473   (!dwarf_split_debug_info                                              \
3474    ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3475 #endif
3476 #ifndef DEBUG_STR_DWO_SECTION
3477 #define DEBUG_STR_DWO_SECTION   ".debug_str.dwo"
3478 #endif
3479 #ifndef DEBUG_STR_SECTION
3480 #define DEBUG_STR_SECTION  ".debug_str"
3481 #endif
3482 #ifndef DEBUG_RANGES_SECTION
3483 #define DEBUG_RANGES_SECTION	".debug_ranges"
3484 #endif
3485 
3486 /* Standard ELF section names for compiled code and data.  */
3487 #ifndef TEXT_SECTION_NAME
3488 #define TEXT_SECTION_NAME	".text"
3489 #endif
3490 
3491 /* Section flags for .debug_macinfo/.debug_macro section.  */
3492 #define DEBUG_MACRO_SECTION_FLAGS                                       \
3493   (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3494 
3495 /* Section flags for .debug_str section.  */
3496 #define DEBUG_STR_SECTION_FLAGS                                 \
3497   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
3498    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
3499    : SECTION_DEBUG)
3500 
3501 /* Section flags for .debug_str.dwo section.  */
3502 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3503 
3504 /* Labels we insert at beginning sections we can reference instead of
3505    the section names themselves.  */
3506 
3507 #ifndef TEXT_SECTION_LABEL
3508 #define TEXT_SECTION_LABEL                 "Ltext"
3509 #endif
3510 #ifndef COLD_TEXT_SECTION_LABEL
3511 #define COLD_TEXT_SECTION_LABEL             "Ltext_cold"
3512 #endif
3513 #ifndef DEBUG_LINE_SECTION_LABEL
3514 #define DEBUG_LINE_SECTION_LABEL           "Ldebug_line"
3515 #endif
3516 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3517 #define DEBUG_SKELETON_LINE_SECTION_LABEL   "Lskeleton_debug_line"
3518 #endif
3519 #ifndef DEBUG_INFO_SECTION_LABEL
3520 #define DEBUG_INFO_SECTION_LABEL           "Ldebug_info"
3521 #endif
3522 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3523 #define DEBUG_SKELETON_INFO_SECTION_LABEL   "Lskeleton_debug_info"
3524 #endif
3525 #ifndef DEBUG_ABBREV_SECTION_LABEL
3526 #define DEBUG_ABBREV_SECTION_LABEL         "Ldebug_abbrev"
3527 #endif
3528 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3529 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3530 #endif
3531 #ifndef DEBUG_ADDR_SECTION_LABEL
3532 #define DEBUG_ADDR_SECTION_LABEL           "Ldebug_addr"
3533 #endif
3534 #ifndef DEBUG_LOC_SECTION_LABEL
3535 #define DEBUG_LOC_SECTION_LABEL                    "Ldebug_loc"
3536 #endif
3537 #ifndef DEBUG_RANGES_SECTION_LABEL
3538 #define DEBUG_RANGES_SECTION_LABEL         "Ldebug_ranges"
3539 #endif
3540 #ifndef DEBUG_MACINFO_SECTION_LABEL
3541 #define DEBUG_MACINFO_SECTION_LABEL         "Ldebug_macinfo"
3542 #endif
3543 #ifndef DEBUG_MACRO_SECTION_LABEL
3544 #define DEBUG_MACRO_SECTION_LABEL          "Ldebug_macro"
3545 #endif
3546 #define SKELETON_COMP_DIE_ABBREV 1
3547 #define SKELETON_TYPE_DIE_ABBREV 2
3548 
3549 /* Definitions of defaults for formats and names of various special
3550    (artificial) labels which may be generated within this file (when the -g
3551    options is used and DWARF2_DEBUGGING_INFO is in effect.
3552    If necessary, these may be overridden from within the tm.h file, but
3553    typically, overriding these defaults is unnecessary.  */
3554 
3555 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3556 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3557 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3558 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3559 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3560 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3561 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3562 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3563 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3564 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3565 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3566 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3567 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3568 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3569 
3570 #ifndef TEXT_END_LABEL
3571 #define TEXT_END_LABEL		"Letext"
3572 #endif
3573 #ifndef COLD_END_LABEL
3574 #define COLD_END_LABEL          "Letext_cold"
3575 #endif
3576 #ifndef BLOCK_BEGIN_LABEL
3577 #define BLOCK_BEGIN_LABEL	"LBB"
3578 #endif
3579 #ifndef BLOCK_END_LABEL
3580 #define BLOCK_END_LABEL		"LBE"
3581 #endif
3582 #ifndef LINE_CODE_LABEL
3583 #define LINE_CODE_LABEL		"LM"
3584 #endif
3585 
3586 
3587 /* Return the root of the DIE's built for the current compilation unit.  */
3588 static dw_die_ref
3589 comp_unit_die (void)
3590 {
3591   if (!single_comp_unit_die)
3592     single_comp_unit_die = gen_compile_unit_die (NULL);
3593   return single_comp_unit_die;
3594 }
3595 
3596 /* We allow a language front-end to designate a function that is to be
3597    called to "demangle" any name before it is put into a DIE.  */
3598 
3599 static const char *(*demangle_name_func) (const char *);
3600 
3601 void
3602 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3603 {
3604   demangle_name_func = func;
3605 }
3606 
3607 /* Test if rtl node points to a pseudo register.  */
3608 
3609 static inline int
3610 is_pseudo_reg (const_rtx rtl)
3611 {
3612   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3613 	  || (GET_CODE (rtl) == SUBREG
3614 	      && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3615 }
3616 
3617 /* Return a reference to a type, with its const and volatile qualifiers
3618    removed.  */
3619 
3620 static inline tree
3621 type_main_variant (tree type)
3622 {
3623   type = TYPE_MAIN_VARIANT (type);
3624 
3625   /* ??? There really should be only one main variant among any group of
3626      variants of a given type (and all of the MAIN_VARIANT values for all
3627      members of the group should point to that one type) but sometimes the C
3628      front-end messes this up for array types, so we work around that bug
3629      here.  */
3630   if (TREE_CODE (type) == ARRAY_TYPE)
3631     while (type != TYPE_MAIN_VARIANT (type))
3632       type = TYPE_MAIN_VARIANT (type);
3633 
3634   return type;
3635 }
3636 
3637 /* Return nonzero if the given type node represents a tagged type.  */
3638 
3639 static inline int
3640 is_tagged_type (const_tree type)
3641 {
3642   enum tree_code code = TREE_CODE (type);
3643 
3644   return (code == RECORD_TYPE || code == UNION_TYPE
3645 	  || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3646 }
3647 
3648 /* Set label to debug_info_section_label + die_offset of a DIE reference.  */
3649 
3650 static void
3651 get_ref_die_offset_label (char *label, dw_die_ref ref)
3652 {
3653   sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3654 }
3655 
3656 /* Return die_offset of a DIE reference to a base type.  */
3657 
3658 static unsigned long int
3659 get_base_type_offset (dw_die_ref ref)
3660 {
3661   if (ref->die_offset)
3662     return ref->die_offset;
3663   if (comp_unit_die ()->die_abbrev)
3664     {
3665       calc_base_type_die_sizes ();
3666       gcc_assert (ref->die_offset);
3667     }
3668   return ref->die_offset;
3669 }
3670 
3671 /* Return die_offset of a DIE reference other than base type.  */
3672 
3673 static unsigned long int
3674 get_ref_die_offset (dw_die_ref ref)
3675 {
3676   gcc_assert (ref->die_offset);
3677   return ref->die_offset;
3678 }
3679 
3680 /* Convert a DIE tag into its string name.  */
3681 
3682 static const char *
3683 dwarf_tag_name (unsigned int tag)
3684 {
3685   const char *name = get_DW_TAG_name (tag);
3686 
3687   if (name != NULL)
3688     return name;
3689 
3690   return "DW_TAG_<unknown>";
3691 }
3692 
3693 /* Convert a DWARF attribute code into its string name.  */
3694 
3695 static const char *
3696 dwarf_attr_name (unsigned int attr)
3697 {
3698   const char *name;
3699 
3700   switch (attr)
3701     {
3702 #if VMS_DEBUGGING_INFO
3703     case DW_AT_HP_prologue:
3704       return "DW_AT_HP_prologue";
3705 #else
3706     case DW_AT_MIPS_loop_unroll_factor:
3707       return "DW_AT_MIPS_loop_unroll_factor";
3708 #endif
3709 
3710 #if VMS_DEBUGGING_INFO
3711     case DW_AT_HP_epilogue:
3712       return "DW_AT_HP_epilogue";
3713 #else
3714     case DW_AT_MIPS_stride:
3715       return "DW_AT_MIPS_stride";
3716 #endif
3717     }
3718 
3719   name = get_DW_AT_name (attr);
3720 
3721   if (name != NULL)
3722     return name;
3723 
3724   return "DW_AT_<unknown>";
3725 }
3726 
3727 /* Convert a DWARF value form code into its string name.  */
3728 
3729 static const char *
3730 dwarf_form_name (unsigned int form)
3731 {
3732   const char *name = get_DW_FORM_name (form);
3733 
3734   if (name != NULL)
3735     return name;
3736 
3737   return "DW_FORM_<unknown>";
3738 }
3739 
3740 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
3741    instance of an inlined instance of a decl which is local to an inline
3742    function, so we have to trace all of the way back through the origin chain
3743    to find out what sort of node actually served as the original seed for the
3744    given block.  */
3745 
3746 static tree
3747 decl_ultimate_origin (const_tree decl)
3748 {
3749   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3750     return NULL_TREE;
3751 
3752   /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
3753      we're trying to output the abstract instance of this function.  */
3754   if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3755     return NULL_TREE;
3756 
3757   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3758      most distant ancestor, this should never happen.  */
3759   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3760 
3761   return DECL_ABSTRACT_ORIGIN (decl);
3762 }
3763 
3764 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
3765    of a virtual function may refer to a base class, so we check the 'this'
3766    parameter.  */
3767 
3768 static tree
3769 decl_class_context (tree decl)
3770 {
3771   tree context = NULL_TREE;
3772 
3773   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3774     context = DECL_CONTEXT (decl);
3775   else
3776     context = TYPE_MAIN_VARIANT
3777       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3778 
3779   if (context && !TYPE_P (context))
3780     context = NULL_TREE;
3781 
3782   return context;
3783 }
3784 
3785 /* Add an attribute/value pair to a DIE.  */
3786 
3787 static inline void
3788 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
3789 {
3790   /* Maybe this should be an assert?  */
3791   if (die == NULL)
3792     return;
3793 
3794   vec_safe_reserve (die->die_attr, 1);
3795   vec_safe_push (die->die_attr, *attr);
3796 }
3797 
3798 static inline enum dw_val_class
3799 AT_class (dw_attr_ref a)
3800 {
3801   return a->dw_attr_val.val_class;
3802 }
3803 
3804 /* Return the index for any attribute that will be referenced with a
3805    DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index.  String indices
3806    are stored in dw_attr_val.v.val_str for reference counting
3807    pruning.  */
3808 
3809 static inline unsigned int
3810 AT_index (dw_attr_ref a)
3811 {
3812   if (AT_class (a) == dw_val_class_str)
3813     return a->dw_attr_val.v.val_str->index;
3814   else if (a->dw_attr_val.val_entry != NULL)
3815     return a->dw_attr_val.val_entry->index;
3816   return NOT_INDEXED;
3817 }
3818 
3819 /* Add a flag value attribute to a DIE.  */
3820 
3821 static inline void
3822 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3823 {
3824   dw_attr_node attr;
3825 
3826   attr.dw_attr = attr_kind;
3827   attr.dw_attr_val.val_class = dw_val_class_flag;
3828   attr.dw_attr_val.val_entry = NULL;
3829   attr.dw_attr_val.v.val_flag = flag;
3830   add_dwarf_attr (die, &attr);
3831 }
3832 
3833 static inline unsigned
3834 AT_flag (dw_attr_ref a)
3835 {
3836   gcc_assert (a && AT_class (a) == dw_val_class_flag);
3837   return a->dw_attr_val.v.val_flag;
3838 }
3839 
3840 /* Add a signed integer attribute value to a DIE.  */
3841 
3842 static inline void
3843 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3844 {
3845   dw_attr_node attr;
3846 
3847   attr.dw_attr = attr_kind;
3848   attr.dw_attr_val.val_class = dw_val_class_const;
3849   attr.dw_attr_val.val_entry = NULL;
3850   attr.dw_attr_val.v.val_int = int_val;
3851   add_dwarf_attr (die, &attr);
3852 }
3853 
3854 static inline HOST_WIDE_INT
3855 AT_int (dw_attr_ref a)
3856 {
3857   gcc_assert (a && AT_class (a) == dw_val_class_const);
3858   return a->dw_attr_val.v.val_int;
3859 }
3860 
3861 /* Add an unsigned integer attribute value to a DIE.  */
3862 
3863 static inline void
3864 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3865 		 unsigned HOST_WIDE_INT unsigned_val)
3866 {
3867   dw_attr_node attr;
3868 
3869   attr.dw_attr = attr_kind;
3870   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3871   attr.dw_attr_val.val_entry = NULL;
3872   attr.dw_attr_val.v.val_unsigned = unsigned_val;
3873   add_dwarf_attr (die, &attr);
3874 }
3875 
3876 static inline unsigned HOST_WIDE_INT
3877 AT_unsigned (dw_attr_ref a)
3878 {
3879   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3880   return a->dw_attr_val.v.val_unsigned;
3881 }
3882 
3883 /* Add an unsigned wide integer attribute value to a DIE.  */
3884 
3885 static inline void
3886 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
3887 	     const wide_int& w)
3888 {
3889   dw_attr_node attr;
3890 
3891   attr.dw_attr = attr_kind;
3892   attr.dw_attr_val.val_class = dw_val_class_wide_int;
3893   attr.dw_attr_val.val_entry = NULL;
3894   attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
3895   *attr.dw_attr_val.v.val_wide = w;
3896   add_dwarf_attr (die, &attr);
3897 }
3898 
3899 /* Add an unsigned double integer attribute value to a DIE.  */
3900 
3901 static inline void
3902 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3903 	       HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3904 {
3905   dw_attr_node attr;
3906 
3907   attr.dw_attr = attr_kind;
3908   attr.dw_attr_val.val_class = dw_val_class_const_double;
3909   attr.dw_attr_val.val_entry = NULL;
3910   attr.dw_attr_val.v.val_double.high = high;
3911   attr.dw_attr_val.v.val_double.low = low;
3912   add_dwarf_attr (die, &attr);
3913 }
3914 
3915 /* Add a floating point attribute value to a DIE and return it.  */
3916 
3917 static inline void
3918 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3919 	    unsigned int length, unsigned int elt_size, unsigned char *array)
3920 {
3921   dw_attr_node attr;
3922 
3923   attr.dw_attr = attr_kind;
3924   attr.dw_attr_val.val_class = dw_val_class_vec;
3925   attr.dw_attr_val.val_entry = NULL;
3926   attr.dw_attr_val.v.val_vec.length = length;
3927   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3928   attr.dw_attr_val.v.val_vec.array = array;
3929   add_dwarf_attr (die, &attr);
3930 }
3931 
3932 /* Add an 8-byte data attribute value to a DIE.  */
3933 
3934 static inline void
3935 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3936               unsigned char data8[8])
3937 {
3938   dw_attr_node attr;
3939 
3940   attr.dw_attr = attr_kind;
3941   attr.dw_attr_val.val_class = dw_val_class_data8;
3942   attr.dw_attr_val.val_entry = NULL;
3943   memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3944   add_dwarf_attr (die, &attr);
3945 }
3946 
3947 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE.  When using
3948    dwarf_split_debug_info, address attributes in dies destined for the
3949    final executable have force_direct set to avoid using indexed
3950    references.  */
3951 
3952 static inline void
3953 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3954                     bool force_direct)
3955 {
3956   dw_attr_node attr;
3957   char * lbl_id;
3958 
3959   lbl_id = xstrdup (lbl_low);
3960   attr.dw_attr = DW_AT_low_pc;
3961   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3962   attr.dw_attr_val.v.val_lbl_id = lbl_id;
3963   if (dwarf_split_debug_info && !force_direct)
3964     attr.dw_attr_val.val_entry
3965         = add_addr_table_entry (lbl_id, ate_kind_label);
3966   else
3967     attr.dw_attr_val.val_entry = NULL;
3968   add_dwarf_attr (die, &attr);
3969 
3970   attr.dw_attr = DW_AT_high_pc;
3971   if (dwarf_version < 4)
3972     attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3973   else
3974     attr.dw_attr_val.val_class = dw_val_class_high_pc;
3975   lbl_id = xstrdup (lbl_high);
3976   attr.dw_attr_val.v.val_lbl_id = lbl_id;
3977   if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3978       && dwarf_split_debug_info && !force_direct)
3979     attr.dw_attr_val.val_entry
3980         = add_addr_table_entry (lbl_id, ate_kind_label);
3981   else
3982     attr.dw_attr_val.val_entry = NULL;
3983   add_dwarf_attr (die, &attr);
3984 }
3985 
3986 /* Hash and equality functions for debug_str_hash.  */
3987 
3988 hashval_t
3989 indirect_string_hasher::hash (indirect_string_node *x)
3990 {
3991   return htab_hash_string (x->str);
3992 }
3993 
3994 bool
3995 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
3996 {
3997   return strcmp (x1->str, x2) == 0;
3998 }
3999 
4000 /* Add STR to the given string hash table.  */
4001 
4002 static struct indirect_string_node *
4003 find_AT_string_in_table (const char *str,
4004 			 hash_table<indirect_string_hasher> *table)
4005 {
4006   struct indirect_string_node *node;
4007 
4008   indirect_string_node **slot
4009     = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
4010   if (*slot == NULL)
4011     {
4012       node = ggc_cleared_alloc<indirect_string_node> ();
4013       node->str = ggc_strdup (str);
4014       *slot = node;
4015     }
4016   else
4017     node = *slot;
4018 
4019   node->refcount++;
4020   return node;
4021 }
4022 
4023 /* Add STR to the indirect string hash table.  */
4024 
4025 static struct indirect_string_node *
4026 find_AT_string (const char *str)
4027 {
4028   if (! debug_str_hash)
4029     debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4030 
4031   return find_AT_string_in_table (str, debug_str_hash);
4032 }
4033 
4034 /* Add a string attribute value to a DIE.  */
4035 
4036 static inline void
4037 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4038 {
4039   dw_attr_node attr;
4040   struct indirect_string_node *node;
4041 
4042   node = find_AT_string (str);
4043 
4044   attr.dw_attr = attr_kind;
4045   attr.dw_attr_val.val_class = dw_val_class_str;
4046   attr.dw_attr_val.val_entry = NULL;
4047   attr.dw_attr_val.v.val_str = node;
4048   add_dwarf_attr (die, &attr);
4049 }
4050 
4051 static inline const char *
4052 AT_string (dw_attr_ref a)
4053 {
4054   gcc_assert (a && AT_class (a) == dw_val_class_str);
4055   return a->dw_attr_val.v.val_str->str;
4056 }
4057 
4058 /* Call this function directly to bypass AT_string_form's logic to put
4059    the string inline in the die. */
4060 
4061 static void
4062 set_indirect_string (struct indirect_string_node *node)
4063 {
4064   char label[32];
4065   /* Already indirect is a no op.  */
4066   if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
4067     {
4068       gcc_assert (node->label);
4069       return;
4070     }
4071   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4072   ++dw2_string_counter;
4073   node->label = xstrdup (label);
4074 
4075   if (!dwarf_split_debug_info)
4076     {
4077       node->form = DW_FORM_strp;
4078       node->index = NOT_INDEXED;
4079     }
4080   else
4081     {
4082       node->form = DW_FORM_GNU_str_index;
4083       node->index = NO_INDEX_ASSIGNED;
4084     }
4085 }
4086 
4087 /* Find out whether a string should be output inline in DIE
4088    or out-of-line in .debug_str section.  */
4089 
4090 static enum dwarf_form
4091 find_string_form (struct indirect_string_node *node)
4092 {
4093   unsigned int len;
4094 
4095   if (node->form)
4096     return node->form;
4097 
4098   len = strlen (node->str) + 1;
4099 
4100   /* If the string is shorter or equal to the size of the reference, it is
4101      always better to put it inline.  */
4102   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4103     return node->form = DW_FORM_string;
4104 
4105   /* If we cannot expect the linker to merge strings in .debug_str
4106      section, only put it into .debug_str if it is worth even in this
4107      single module.  */
4108   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4109       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4110       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4111     return node->form = DW_FORM_string;
4112 
4113   set_indirect_string (node);
4114 
4115   return node->form;
4116 }
4117 
4118 /* Find out whether the string referenced from the attribute should be
4119    output inline in DIE or out-of-line in .debug_str section.  */
4120 
4121 static enum dwarf_form
4122 AT_string_form (dw_attr_ref a)
4123 {
4124   gcc_assert (a && AT_class (a) == dw_val_class_str);
4125   return find_string_form (a->dw_attr_val.v.val_str);
4126 }
4127 
4128 /* Add a DIE reference attribute value to a DIE.  */
4129 
4130 static inline void
4131 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4132 {
4133   dw_attr_node attr;
4134 
4135 #ifdef ENABLE_CHECKING
4136   gcc_assert (targ_die != NULL);
4137 #else
4138   /* With LTO we can end up trying to reference something we didn't create
4139      a DIE for.  Avoid crashing later on a NULL referenced DIE.  */
4140   if (targ_die == NULL)
4141     return;
4142 #endif
4143 
4144   attr.dw_attr = attr_kind;
4145   attr.dw_attr_val.val_class = dw_val_class_die_ref;
4146   attr.dw_attr_val.val_entry = NULL;
4147   attr.dw_attr_val.v.val_die_ref.die = targ_die;
4148   attr.dw_attr_val.v.val_die_ref.external = 0;
4149   add_dwarf_attr (die, &attr);
4150 }
4151 
4152 /* Change DIE reference REF to point to NEW_DIE instead.  */
4153 
4154 static inline void
4155 change_AT_die_ref (dw_attr_ref ref, dw_die_ref new_die)
4156 {
4157   gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4158   ref->dw_attr_val.v.val_die_ref.die = new_die;
4159   ref->dw_attr_val.v.val_die_ref.external = 0;
4160 }
4161 
4162 /* Add an AT_specification attribute to a DIE, and also make the back
4163    pointer from the specification to the definition.  */
4164 
4165 static inline void
4166 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4167 {
4168   add_AT_die_ref (die, DW_AT_specification, targ_die);
4169   gcc_assert (!targ_die->die_definition);
4170   targ_die->die_definition = die;
4171 }
4172 
4173 static inline dw_die_ref
4174 AT_ref (dw_attr_ref a)
4175 {
4176   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4177   return a->dw_attr_val.v.val_die_ref.die;
4178 }
4179 
4180 static inline int
4181 AT_ref_external (dw_attr_ref a)
4182 {
4183   if (a && AT_class (a) == dw_val_class_die_ref)
4184     return a->dw_attr_val.v.val_die_ref.external;
4185 
4186   return 0;
4187 }
4188 
4189 static inline void
4190 set_AT_ref_external (dw_attr_ref a, int i)
4191 {
4192   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4193   a->dw_attr_val.v.val_die_ref.external = i;
4194 }
4195 
4196 /* Add an FDE reference attribute value to a DIE.  */
4197 
4198 static inline void
4199 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4200 {
4201   dw_attr_node attr;
4202 
4203   attr.dw_attr = attr_kind;
4204   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4205   attr.dw_attr_val.val_entry = NULL;
4206   attr.dw_attr_val.v.val_fde_index = targ_fde;
4207   add_dwarf_attr (die, &attr);
4208 }
4209 
4210 /* Add a location description attribute value to a DIE.  */
4211 
4212 static inline void
4213 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4214 {
4215   dw_attr_node attr;
4216 
4217   attr.dw_attr = attr_kind;
4218   attr.dw_attr_val.val_class = dw_val_class_loc;
4219   attr.dw_attr_val.val_entry = NULL;
4220   attr.dw_attr_val.v.val_loc = loc;
4221   add_dwarf_attr (die, &attr);
4222 }
4223 
4224 static inline dw_loc_descr_ref
4225 AT_loc (dw_attr_ref a)
4226 {
4227   gcc_assert (a && AT_class (a) == dw_val_class_loc);
4228   return a->dw_attr_val.v.val_loc;
4229 }
4230 
4231 static inline void
4232 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4233 {
4234   dw_attr_node attr;
4235 
4236   attr.dw_attr = attr_kind;
4237   attr.dw_attr_val.val_class = dw_val_class_loc_list;
4238   attr.dw_attr_val.val_entry = NULL;
4239   attr.dw_attr_val.v.val_loc_list = loc_list;
4240   add_dwarf_attr (die, &attr);
4241   have_location_lists = true;
4242 }
4243 
4244 static inline dw_loc_list_ref
4245 AT_loc_list (dw_attr_ref a)
4246 {
4247   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4248   return a->dw_attr_val.v.val_loc_list;
4249 }
4250 
4251 static inline dw_loc_list_ref *
4252 AT_loc_list_ptr (dw_attr_ref a)
4253 {
4254   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4255   return &a->dw_attr_val.v.val_loc_list;
4256 }
4257 
4258 struct addr_hasher : ggc_hasher<addr_table_entry *>
4259 {
4260   static hashval_t hash (addr_table_entry *);
4261   static bool equal (addr_table_entry *, addr_table_entry *);
4262 };
4263 
4264 /* Table of entries into the .debug_addr section.  */
4265 
4266 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4267 
4268 /* Hash an address_table_entry.  */
4269 
4270 hashval_t
4271 addr_hasher::hash (addr_table_entry *a)
4272 {
4273   inchash::hash hstate;
4274   switch (a->kind)
4275     {
4276       case ate_kind_rtx:
4277 	hstate.add_int (0);
4278 	break;
4279       case ate_kind_rtx_dtprel:
4280 	hstate.add_int (1);
4281 	break;
4282       case ate_kind_label:
4283         return htab_hash_string (a->addr.label);
4284       default:
4285         gcc_unreachable ();
4286     }
4287   inchash::add_rtx (a->addr.rtl, hstate);
4288   return hstate.end ();
4289 }
4290 
4291 /* Determine equality for two address_table_entries.  */
4292 
4293 bool
4294 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4295 {
4296   if (a1->kind != a2->kind)
4297     return 0;
4298   switch (a1->kind)
4299     {
4300       case ate_kind_rtx:
4301       case ate_kind_rtx_dtprel:
4302         return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4303       case ate_kind_label:
4304         return strcmp (a1->addr.label, a2->addr.label) == 0;
4305       default:
4306         gcc_unreachable ();
4307     }
4308 }
4309 
4310 /* Initialize an addr_table_entry.  */
4311 
4312 void
4313 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4314 {
4315   e->kind = kind;
4316   switch (kind)
4317     {
4318       case ate_kind_rtx:
4319       case ate_kind_rtx_dtprel:
4320         e->addr.rtl = (rtx) addr;
4321         break;
4322       case ate_kind_label:
4323         e->addr.label = (char *) addr;
4324         break;
4325     }
4326   e->refcount = 0;
4327   e->index = NO_INDEX_ASSIGNED;
4328 }
4329 
4330 /* Add attr to the address table entry to the table.  Defer setting an
4331    index until output time.  */
4332 
4333 static addr_table_entry *
4334 add_addr_table_entry (void *addr, enum ate_kind kind)
4335 {
4336   addr_table_entry *node;
4337   addr_table_entry finder;
4338 
4339   gcc_assert (dwarf_split_debug_info);
4340   if (! addr_index_table)
4341     addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4342   init_addr_table_entry (&finder, kind, addr);
4343   addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4344 
4345   if (*slot == HTAB_EMPTY_ENTRY)
4346     {
4347       node = ggc_cleared_alloc<addr_table_entry> ();
4348       init_addr_table_entry (node, kind, addr);
4349       *slot = node;
4350     }
4351   else
4352     node = *slot;
4353 
4354   node->refcount++;
4355   return node;
4356 }
4357 
4358 /* Remove an entry from the addr table by decrementing its refcount.
4359    Strictly, decrementing the refcount would be enough, but the
4360    assertion that the entry is actually in the table has found
4361    bugs.  */
4362 
4363 static void
4364 remove_addr_table_entry (addr_table_entry *entry)
4365 {
4366   gcc_assert (dwarf_split_debug_info && addr_index_table);
4367   /* After an index is assigned, the table is frozen.  */
4368   gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4369   entry->refcount--;
4370 }
4371 
4372 /* Given a location list, remove all addresses it refers to from the
4373    address_table.  */
4374 
4375 static void
4376 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4377 {
4378   for (; descr; descr = descr->dw_loc_next)
4379     if (descr->dw_loc_oprnd1.val_entry != NULL)
4380       {
4381         gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4382         remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4383       }
4384 }
4385 
4386 /* A helper function for dwarf2out_finish called through
4387    htab_traverse.  Assign an addr_table_entry its index.  All entries
4388    must be collected into the table when this function is called,
4389    because the indexing code relies on htab_traverse to traverse nodes
4390    in the same order for each run. */
4391 
4392 int
4393 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4394 {
4395   addr_table_entry *node = *h;
4396 
4397   /* Don't index unreferenced nodes.  */
4398   if (node->refcount == 0)
4399     return 1;
4400 
4401   gcc_assert (node->index == NO_INDEX_ASSIGNED);
4402   node->index = *index;
4403   *index += 1;
4404 
4405   return 1;
4406 }
4407 
4408 /* Add an address constant attribute value to a DIE.  When using
4409    dwarf_split_debug_info, address attributes in dies destined for the
4410    final executable should be direct references--setting the parameter
4411    force_direct ensures this behavior.  */
4412 
4413 static inline void
4414 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4415              bool force_direct)
4416 {
4417   dw_attr_node attr;
4418 
4419   attr.dw_attr = attr_kind;
4420   attr.dw_attr_val.val_class = dw_val_class_addr;
4421   attr.dw_attr_val.v.val_addr = addr;
4422   if (dwarf_split_debug_info && !force_direct)
4423     attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4424   else
4425     attr.dw_attr_val.val_entry = NULL;
4426   add_dwarf_attr (die, &attr);
4427 }
4428 
4429 /* Get the RTX from to an address DIE attribute.  */
4430 
4431 static inline rtx
4432 AT_addr (dw_attr_ref a)
4433 {
4434   gcc_assert (a && AT_class (a) == dw_val_class_addr);
4435   return a->dw_attr_val.v.val_addr;
4436 }
4437 
4438 /* Add a file attribute value to a DIE.  */
4439 
4440 static inline void
4441 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4442 	     struct dwarf_file_data *fd)
4443 {
4444   dw_attr_node attr;
4445 
4446   attr.dw_attr = attr_kind;
4447   attr.dw_attr_val.val_class = dw_val_class_file;
4448   attr.dw_attr_val.val_entry = NULL;
4449   attr.dw_attr_val.v.val_file = fd;
4450   add_dwarf_attr (die, &attr);
4451 }
4452 
4453 /* Get the dwarf_file_data from a file DIE attribute.  */
4454 
4455 static inline struct dwarf_file_data *
4456 AT_file (dw_attr_ref a)
4457 {
4458   gcc_assert (a && AT_class (a) == dw_val_class_file);
4459   return a->dw_attr_val.v.val_file;
4460 }
4461 
4462 /* Add a vms delta attribute value to a DIE.  */
4463 
4464 static inline void
4465 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4466 		  const char *lbl1, const char *lbl2)
4467 {
4468   dw_attr_node attr;
4469 
4470   attr.dw_attr = attr_kind;
4471   attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4472   attr.dw_attr_val.val_entry = NULL;
4473   attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4474   attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4475   add_dwarf_attr (die, &attr);
4476 }
4477 
4478 /* Add a label identifier attribute value to a DIE.  */
4479 
4480 static inline void
4481 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4482                const char *lbl_id)
4483 {
4484   dw_attr_node attr;
4485 
4486   attr.dw_attr = attr_kind;
4487   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4488   attr.dw_attr_val.val_entry = NULL;
4489   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4490   if (dwarf_split_debug_info)
4491     attr.dw_attr_val.val_entry
4492         = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4493                                 ate_kind_label);
4494   add_dwarf_attr (die, &attr);
4495 }
4496 
4497 /* Add a section offset attribute value to a DIE, an offset into the
4498    debug_line section.  */
4499 
4500 static inline void
4501 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4502 		const char *label)
4503 {
4504   dw_attr_node attr;
4505 
4506   attr.dw_attr = attr_kind;
4507   attr.dw_attr_val.val_class = dw_val_class_lineptr;
4508   attr.dw_attr_val.val_entry = NULL;
4509   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4510   add_dwarf_attr (die, &attr);
4511 }
4512 
4513 /* Add a section offset attribute value to a DIE, an offset into the
4514    debug_macinfo section.  */
4515 
4516 static inline void
4517 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4518 	       const char *label)
4519 {
4520   dw_attr_node attr;
4521 
4522   attr.dw_attr = attr_kind;
4523   attr.dw_attr_val.val_class = dw_val_class_macptr;
4524   attr.dw_attr_val.val_entry = NULL;
4525   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4526   add_dwarf_attr (die, &attr);
4527 }
4528 
4529 /* Add an offset attribute value to a DIE.  */
4530 
4531 static inline void
4532 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4533 	       unsigned HOST_WIDE_INT offset)
4534 {
4535   dw_attr_node attr;
4536 
4537   attr.dw_attr = attr_kind;
4538   attr.dw_attr_val.val_class = dw_val_class_offset;
4539   attr.dw_attr_val.val_entry = NULL;
4540   attr.dw_attr_val.v.val_offset = offset;
4541   add_dwarf_attr (die, &attr);
4542 }
4543 
4544 /* Add a range_list attribute value to a DIE.  When using
4545    dwarf_split_debug_info, address attributes in dies destined for the
4546    final executable should be direct references--setting the parameter
4547    force_direct ensures this behavior.  */
4548 
4549 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4550 #define RELOCATED_OFFSET (NULL)
4551 
4552 static void
4553 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4554                    long unsigned int offset, bool force_direct)
4555 {
4556   dw_attr_node attr;
4557 
4558   attr.dw_attr = attr_kind;
4559   attr.dw_attr_val.val_class = dw_val_class_range_list;
4560   /* For the range_list attribute, use val_entry to store whether the
4561      offset should follow split-debug-info or normal semantics.  This
4562      value is read in output_range_list_offset.  */
4563   if (dwarf_split_debug_info && !force_direct)
4564     attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4565   else
4566     attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4567   attr.dw_attr_val.v.val_offset = offset;
4568   add_dwarf_attr (die, &attr);
4569 }
4570 
4571 /* Return the start label of a delta attribute.  */
4572 
4573 static inline const char *
4574 AT_vms_delta1 (dw_attr_ref a)
4575 {
4576   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4577   return a->dw_attr_val.v.val_vms_delta.lbl1;
4578 }
4579 
4580 /* Return the end label of a delta attribute.  */
4581 
4582 static inline const char *
4583 AT_vms_delta2 (dw_attr_ref a)
4584 {
4585   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4586   return a->dw_attr_val.v.val_vms_delta.lbl2;
4587 }
4588 
4589 static inline const char *
4590 AT_lbl (dw_attr_ref a)
4591 {
4592   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4593 		    || AT_class (a) == dw_val_class_lineptr
4594 		    || AT_class (a) == dw_val_class_macptr
4595 		    || AT_class (a) == dw_val_class_high_pc));
4596   return a->dw_attr_val.v.val_lbl_id;
4597 }
4598 
4599 /* Get the attribute of type attr_kind.  */
4600 
4601 static dw_attr_ref
4602 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4603 {
4604   dw_attr_ref a;
4605   unsigned ix;
4606   dw_die_ref spec = NULL;
4607 
4608   if (! die)
4609     return NULL;
4610 
4611   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4612     if (a->dw_attr == attr_kind)
4613       return a;
4614     else if (a->dw_attr == DW_AT_specification
4615 	     || a->dw_attr == DW_AT_abstract_origin)
4616       spec = AT_ref (a);
4617 
4618   if (spec)
4619     return get_AT (spec, attr_kind);
4620 
4621   return NULL;
4622 }
4623 
4624 /* Returns the parent of the declaration of DIE.  */
4625 
4626 static dw_die_ref
4627 get_die_parent (dw_die_ref die)
4628 {
4629   dw_die_ref t;
4630 
4631   if (!die)
4632     return NULL;
4633 
4634   if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4635       || (t = get_AT_ref (die, DW_AT_specification)))
4636     die = t;
4637 
4638   return die->die_parent;
4639 }
4640 
4641 /* Return the "low pc" attribute value, typically associated with a subprogram
4642    DIE.  Return null if the "low pc" attribute is either not present, or if it
4643    cannot be represented as an assembler label identifier.  */
4644 
4645 static inline const char *
4646 get_AT_low_pc (dw_die_ref die)
4647 {
4648   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4649 
4650   return a ? AT_lbl (a) : NULL;
4651 }
4652 
4653 /* Return the "high pc" attribute value, typically associated with a subprogram
4654    DIE.  Return null if the "high pc" attribute is either not present, or if it
4655    cannot be represented as an assembler label identifier.  */
4656 
4657 static inline const char *
4658 get_AT_hi_pc (dw_die_ref die)
4659 {
4660   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4661 
4662   return a ? AT_lbl (a) : NULL;
4663 }
4664 
4665 /* Return the value of the string attribute designated by ATTR_KIND, or
4666    NULL if it is not present.  */
4667 
4668 static inline const char *
4669 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4670 {
4671   dw_attr_ref a = get_AT (die, attr_kind);
4672 
4673   return a ? AT_string (a) : NULL;
4674 }
4675 
4676 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4677    if it is not present.  */
4678 
4679 static inline int
4680 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4681 {
4682   dw_attr_ref a = get_AT (die, attr_kind);
4683 
4684   return a ? AT_flag (a) : 0;
4685 }
4686 
4687 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4688    if it is not present.  */
4689 
4690 static inline unsigned
4691 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4692 {
4693   dw_attr_ref a = get_AT (die, attr_kind);
4694 
4695   return a ? AT_unsigned (a) : 0;
4696 }
4697 
4698 static inline dw_die_ref
4699 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4700 {
4701   dw_attr_ref a = get_AT (die, attr_kind);
4702 
4703   return a ? AT_ref (a) : NULL;
4704 }
4705 
4706 static inline struct dwarf_file_data *
4707 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4708 {
4709   dw_attr_ref a = get_AT (die, attr_kind);
4710 
4711   return a ? AT_file (a) : NULL;
4712 }
4713 
4714 /* Return TRUE if the language is C++.  */
4715 
4716 static inline bool
4717 is_cxx (void)
4718 {
4719   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4720 
4721   return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
4722 	  || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
4723 }
4724 
4725 /* Return TRUE if the language is Java.  */
4726 
4727 static inline bool
4728 is_java (void)
4729 {
4730   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4731 
4732   return lang == DW_LANG_Java;
4733 }
4734 
4735 /* Return TRUE if the language is Fortran.  */
4736 
4737 static inline bool
4738 is_fortran (void)
4739 {
4740   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4741 
4742   return (lang == DW_LANG_Fortran77
4743 	  || lang == DW_LANG_Fortran90
4744 	  || lang == DW_LANG_Fortran95
4745 	  || lang == DW_LANG_Fortran03
4746 	  || lang == DW_LANG_Fortran08);
4747 }
4748 
4749 /* Return TRUE if the language is Ada.  */
4750 
4751 static inline bool
4752 is_ada (void)
4753 {
4754   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4755 
4756   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4757 }
4758 
4759 /* Remove the specified attribute if present.  */
4760 
4761 static void
4762 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4763 {
4764   dw_attr_ref a;
4765   unsigned ix;
4766 
4767   if (! die)
4768     return;
4769 
4770   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4771     if (a->dw_attr == attr_kind)
4772       {
4773 	if (AT_class (a) == dw_val_class_str)
4774 	  if (a->dw_attr_val.v.val_str->refcount)
4775 	    a->dw_attr_val.v.val_str->refcount--;
4776 
4777 	/* vec::ordered_remove should help reduce the number of abbrevs
4778 	   that are needed.  */
4779 	die->die_attr->ordered_remove (ix);
4780 	return;
4781       }
4782 }
4783 
4784 /* Remove CHILD from its parent.  PREV must have the property that
4785    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
4786 
4787 static void
4788 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4789 {
4790   gcc_assert (child->die_parent == prev->die_parent);
4791   gcc_assert (prev->die_sib == child);
4792   if (prev == child)
4793     {
4794       gcc_assert (child->die_parent->die_child == child);
4795       prev = NULL;
4796     }
4797   else
4798     prev->die_sib = child->die_sib;
4799   if (child->die_parent->die_child == child)
4800     child->die_parent->die_child = prev;
4801 }
4802 
4803 /* Replace OLD_CHILD with NEW_CHILD.  PREV must have the property that
4804    PREV->DIE_SIB == OLD_CHILD.  Does not alter OLD_CHILD.  */
4805 
4806 static void
4807 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4808 {
4809   dw_die_ref parent = old_child->die_parent;
4810 
4811   gcc_assert (parent == prev->die_parent);
4812   gcc_assert (prev->die_sib == old_child);
4813 
4814   new_child->die_parent = parent;
4815   if (prev == old_child)
4816     {
4817       gcc_assert (parent->die_child == old_child);
4818       new_child->die_sib = new_child;
4819     }
4820   else
4821     {
4822       prev->die_sib = new_child;
4823       new_child->die_sib = old_child->die_sib;
4824     }
4825   if (old_child->die_parent->die_child == old_child)
4826     old_child->die_parent->die_child = new_child;
4827 }
4828 
4829 /* Move all children from OLD_PARENT to NEW_PARENT.  */
4830 
4831 static void
4832 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4833 {
4834   dw_die_ref c;
4835   new_parent->die_child = old_parent->die_child;
4836   old_parent->die_child = NULL;
4837   FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4838 }
4839 
4840 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
4841    matches TAG.  */
4842 
4843 static void
4844 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4845 {
4846   dw_die_ref c;
4847 
4848   c = die->die_child;
4849   if (c) do {
4850     dw_die_ref prev = c;
4851     c = c->die_sib;
4852     while (c->die_tag == tag)
4853       {
4854 	remove_child_with_prev (c, prev);
4855 	/* Might have removed every child.  */
4856 	if (c == c->die_sib)
4857 	  return;
4858 	c = c->die_sib;
4859       }
4860   } while (c != die->die_child);
4861 }
4862 
4863 /* Add a CHILD_DIE as the last child of DIE.  */
4864 
4865 static void
4866 add_child_die (dw_die_ref die, dw_die_ref child_die)
4867 {
4868   /* FIXME this should probably be an assert.  */
4869   if (! die || ! child_die)
4870     return;
4871   gcc_assert (die != child_die);
4872 
4873   child_die->die_parent = die;
4874   if (die->die_child)
4875     {
4876       child_die->die_sib = die->die_child->die_sib;
4877       die->die_child->die_sib = child_die;
4878     }
4879   else
4880     child_die->die_sib = child_die;
4881   die->die_child = child_die;
4882 }
4883 
4884 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4885    is the specification, to the end of PARENT's list of children.
4886    This is done by removing and re-adding it.  */
4887 
4888 static void
4889 splice_child_die (dw_die_ref parent, dw_die_ref child)
4890 {
4891   dw_die_ref p;
4892 
4893   /* We want the declaration DIE from inside the class, not the
4894      specification DIE at toplevel.  */
4895   if (child->die_parent != parent)
4896     {
4897       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4898 
4899       if (tmp)
4900 	child = tmp;
4901     }
4902 
4903   gcc_assert (child->die_parent == parent
4904 	      || (child->die_parent
4905 		  == get_AT_ref (parent, DW_AT_specification)));
4906 
4907   for (p = child->die_parent->die_child; ; p = p->die_sib)
4908     if (p->die_sib == child)
4909       {
4910 	remove_child_with_prev (child, p);
4911 	break;
4912       }
4913 
4914   add_child_die (parent, child);
4915 }
4916 
4917 /* Return a pointer to a newly created DIE node.  */
4918 
4919 static inline dw_die_ref
4920 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4921 {
4922   dw_die_ref die = ggc_cleared_alloc<die_node> ();
4923 
4924   die->die_tag = tag_value;
4925 
4926   if (parent_die != NULL)
4927     add_child_die (parent_die, die);
4928   else
4929     {
4930       limbo_die_node *limbo_node;
4931 
4932       limbo_node = ggc_cleared_alloc<limbo_die_node> ();
4933       limbo_node->die = die;
4934       limbo_node->created_for = t;
4935       limbo_node->next = limbo_die_list;
4936       limbo_die_list = limbo_node;
4937     }
4938 
4939   return die;
4940 }
4941 
4942 /* Return the DIE associated with the given type specifier.  */
4943 
4944 static inline dw_die_ref
4945 lookup_type_die (tree type)
4946 {
4947   return TYPE_SYMTAB_DIE (type);
4948 }
4949 
4950 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4951    anonymous type named by the typedef TYPE_DIE, return the DIE of the
4952    anonymous type instead the one of the naming typedef.  */
4953 
4954 static inline dw_die_ref
4955 strip_naming_typedef (tree type, dw_die_ref type_die)
4956 {
4957   if (type
4958       && TREE_CODE (type) == RECORD_TYPE
4959       && type_die
4960       && type_die->die_tag == DW_TAG_typedef
4961       && is_naming_typedef_decl (TYPE_NAME (type)))
4962     type_die = get_AT_ref (type_die, DW_AT_type);
4963   return type_die;
4964 }
4965 
4966 /* Like lookup_type_die, but if type is an anonymous type named by a
4967    typedef[1], return the DIE of the anonymous type instead the one of
4968    the naming typedef.  This is because in gen_typedef_die, we did
4969    equate the anonymous struct named by the typedef with the DIE of
4970    the naming typedef. So by default, lookup_type_die on an anonymous
4971    struct yields the DIE of the naming typedef.
4972 
4973    [1]: Read the comment of is_naming_typedef_decl to learn about what
4974    a naming typedef is.  */
4975 
4976 static inline dw_die_ref
4977 lookup_type_die_strip_naming_typedef (tree type)
4978 {
4979   dw_die_ref die = lookup_type_die (type);
4980   return strip_naming_typedef (type, die);
4981 }
4982 
4983 /* Equate a DIE to a given type specifier.  */
4984 
4985 static inline void
4986 equate_type_number_to_die (tree type, dw_die_ref type_die)
4987 {
4988   TYPE_SYMTAB_DIE (type) = type_die;
4989 }
4990 
4991 /* Returns a hash value for X (which really is a die_struct).  */
4992 
4993 inline hashval_t
4994 decl_die_hasher::hash (die_node *x)
4995 {
4996   return (hashval_t) x->decl_id;
4997 }
4998 
4999 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
5000 
5001 inline bool
5002 decl_die_hasher::equal (die_node *x, tree y)
5003 {
5004   return (x->decl_id == DECL_UID (y));
5005 }
5006 
5007 /* Return the DIE associated with a given declaration.  */
5008 
5009 static inline dw_die_ref
5010 lookup_decl_die (tree decl)
5011 {
5012   return decl_die_table->find_with_hash (decl, DECL_UID (decl));
5013 }
5014 
5015 /* Returns a hash value for X (which really is a var_loc_list).  */
5016 
5017 inline hashval_t
5018 decl_loc_hasher::hash (var_loc_list *x)
5019 {
5020   return (hashval_t) x->decl_id;
5021 }
5022 
5023 /* Return nonzero if decl_id of var_loc_list X is the same as
5024    UID of decl *Y.  */
5025 
5026 inline bool
5027 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5028 {
5029   return (x->decl_id == DECL_UID (y));
5030 }
5031 
5032 /* Return the var_loc list associated with a given declaration.  */
5033 
5034 static inline var_loc_list *
5035 lookup_decl_loc (const_tree decl)
5036 {
5037   if (!decl_loc_table)
5038     return NULL;
5039   return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5040 }
5041 
5042 /* Returns a hash value for X (which really is a cached_dw_loc_list_list).  */
5043 
5044 inline hashval_t
5045 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5046 {
5047   return (hashval_t) x->decl_id;
5048 }
5049 
5050 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5051    UID of decl *Y.  */
5052 
5053 inline bool
5054 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5055 {
5056   return (x->decl_id == DECL_UID (y));
5057 }
5058 
5059 /* Equate a DIE to a particular declaration.  */
5060 
5061 static void
5062 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5063 {
5064   unsigned int decl_id = DECL_UID (decl);
5065 
5066   *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5067   decl_die->decl_id = decl_id;
5068 }
5069 
5070 /* Return how many bits covers PIECE EXPR_LIST.  */
5071 
5072 static HOST_WIDE_INT
5073 decl_piece_bitsize (rtx piece)
5074 {
5075   int ret = (int) GET_MODE (piece);
5076   if (ret)
5077     return ret;
5078   gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5079 	      && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5080   return INTVAL (XEXP (XEXP (piece, 0), 0));
5081 }
5082 
5083 /* Return pointer to the location of location note in PIECE EXPR_LIST.  */
5084 
5085 static rtx *
5086 decl_piece_varloc_ptr (rtx piece)
5087 {
5088   if ((int) GET_MODE (piece))
5089     return &XEXP (piece, 0);
5090   else
5091     return &XEXP (XEXP (piece, 0), 1);
5092 }
5093 
5094 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5095    Next is the chain of following piece nodes.  */
5096 
5097 static rtx_expr_list *
5098 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5099 {
5100   if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
5101     return alloc_EXPR_LIST (bitsize, loc_note, next);
5102   else
5103     return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5104 					       GEN_INT (bitsize),
5105 					       loc_note), next);
5106 }
5107 
5108 /* Return rtx that should be stored into loc field for
5109    LOC_NOTE and BITPOS/BITSIZE.  */
5110 
5111 static rtx
5112 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5113 		      HOST_WIDE_INT bitsize)
5114 {
5115   if (bitsize != -1)
5116     {
5117       loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5118       if (bitpos != 0)
5119 	loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5120     }
5121   return loc_note;
5122 }
5123 
5124 /* This function either modifies location piece list *DEST in
5125    place (if SRC and INNER is NULL), or copies location piece list
5126    *SRC to *DEST while modifying it.  Location BITPOS is modified
5127    to contain LOC_NOTE, any pieces overlapping it are removed resp.
5128    not copied and if needed some padding around it is added.
5129    When modifying in place, DEST should point to EXPR_LIST where
5130    earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5131    to the start of the whole list and INNER points to the EXPR_LIST
5132    where earlier pieces cover PIECE_BITPOS bits.  */
5133 
5134 static void
5135 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5136 		   HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5137 		   HOST_WIDE_INT bitsize, rtx loc_note)
5138 {
5139   HOST_WIDE_INT diff;
5140   bool copy = inner != NULL;
5141 
5142   if (copy)
5143     {
5144       /* First copy all nodes preceding the current bitpos.  */
5145       while (src != inner)
5146 	{
5147 	  *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5148 				   decl_piece_bitsize (*src), NULL_RTX);
5149 	  dest = &XEXP (*dest, 1);
5150 	  src = &XEXP (*src, 1);
5151 	}
5152     }
5153   /* Add padding if needed.  */
5154   if (bitpos != piece_bitpos)
5155     {
5156       *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5157 			       copy ? NULL_RTX : *dest);
5158       dest = &XEXP (*dest, 1);
5159     }
5160   else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5161     {
5162       gcc_assert (!copy);
5163       /* A piece with correct bitpos and bitsize already exist,
5164 	 just update the location for it and return.  */
5165       *decl_piece_varloc_ptr (*dest) = loc_note;
5166       return;
5167     }
5168   /* Add the piece that changed.  */
5169   *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5170   dest = &XEXP (*dest, 1);
5171   /* Skip over pieces that overlap it.  */
5172   diff = bitpos - piece_bitpos + bitsize;
5173   if (!copy)
5174     src = dest;
5175   while (diff > 0 && *src)
5176     {
5177       rtx piece = *src;
5178       diff -= decl_piece_bitsize (piece);
5179       if (copy)
5180 	src = &XEXP (piece, 1);
5181       else
5182 	{
5183 	  *src = XEXP (piece, 1);
5184 	  free_EXPR_LIST_node (piece);
5185 	}
5186     }
5187   /* Add padding if needed.  */
5188   if (diff < 0 && *src)
5189     {
5190       if (!copy)
5191 	dest = src;
5192       *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5193       dest = &XEXP (*dest, 1);
5194     }
5195   if (!copy)
5196     return;
5197   /* Finally copy all nodes following it.  */
5198   while (*src)
5199     {
5200       *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5201 			       decl_piece_bitsize (*src), NULL_RTX);
5202       dest = &XEXP (*dest, 1);
5203       src = &XEXP (*src, 1);
5204     }
5205 }
5206 
5207 /* Add a variable location node to the linked list for DECL.  */
5208 
5209 static struct var_loc_node *
5210 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5211 {
5212   unsigned int decl_id;
5213   var_loc_list *temp;
5214   struct var_loc_node *loc = NULL;
5215   HOST_WIDE_INT bitsize = -1, bitpos = -1;
5216 
5217   if (TREE_CODE (decl) == VAR_DECL
5218       && DECL_HAS_DEBUG_EXPR_P (decl))
5219     {
5220       tree realdecl = DECL_DEBUG_EXPR (decl);
5221       if (handled_component_p (realdecl)
5222 	  || (TREE_CODE (realdecl) == MEM_REF
5223 	      && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5224 	{
5225 	  HOST_WIDE_INT maxsize;
5226 	  tree innerdecl;
5227 	  innerdecl
5228 	    = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5229 	  if (!DECL_P (innerdecl)
5230 	      || DECL_IGNORED_P (innerdecl)
5231 	      || TREE_STATIC (innerdecl)
5232 	      || bitsize <= 0
5233 	      || bitpos + bitsize > 256
5234 	      || bitsize != maxsize)
5235 	    return NULL;
5236 	  decl = innerdecl;
5237 	}
5238     }
5239 
5240   decl_id = DECL_UID (decl);
5241   var_loc_list **slot
5242     = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5243   if (*slot == NULL)
5244     {
5245       temp = ggc_cleared_alloc<var_loc_list> ();
5246       temp->decl_id = decl_id;
5247       *slot = temp;
5248     }
5249   else
5250     temp = *slot;
5251 
5252   /* For PARM_DECLs try to keep around the original incoming value,
5253      even if that means we'll emit a zero-range .debug_loc entry.  */
5254   if (temp->last
5255       && temp->first == temp->last
5256       && TREE_CODE (decl) == PARM_DECL
5257       && NOTE_P (temp->first->loc)
5258       && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5259       && DECL_INCOMING_RTL (decl)
5260       && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5261       && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5262 	 == GET_CODE (DECL_INCOMING_RTL (decl))
5263       && prev_real_insn (temp->first->loc) == NULL_RTX
5264       && (bitsize != -1
5265 	  || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5266 			   NOTE_VAR_LOCATION_LOC (loc_note))
5267 	  || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5268 	      != NOTE_VAR_LOCATION_STATUS (loc_note))))
5269     {
5270       loc = ggc_cleared_alloc<var_loc_node> ();
5271       temp->first->next = loc;
5272       temp->last = loc;
5273       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5274     }
5275   else if (temp->last)
5276     {
5277       struct var_loc_node *last = temp->last, *unused = NULL;
5278       rtx *piece_loc = NULL, last_loc_note;
5279       HOST_WIDE_INT piece_bitpos = 0;
5280       if (last->next)
5281 	{
5282 	  last = last->next;
5283 	  gcc_assert (last->next == NULL);
5284 	}
5285       if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5286 	{
5287 	  piece_loc = &last->loc;
5288 	  do
5289 	    {
5290 	      HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5291 	      if (piece_bitpos + cur_bitsize > bitpos)
5292 		break;
5293 	      piece_bitpos += cur_bitsize;
5294 	      piece_loc = &XEXP (*piece_loc, 1);
5295 	    }
5296 	  while (*piece_loc);
5297 	}
5298       /* TEMP->LAST here is either pointer to the last but one or
5299 	 last element in the chained list, LAST is pointer to the
5300 	 last element.  */
5301       if (label && strcmp (last->label, label) == 0)
5302 	{
5303 	  /* For SRA optimized variables if there weren't any real
5304 	     insns since last note, just modify the last node.  */
5305 	  if (piece_loc != NULL)
5306 	    {
5307 	      adjust_piece_list (piece_loc, NULL, NULL,
5308 				 bitpos, piece_bitpos, bitsize, loc_note);
5309 	      return NULL;
5310 	    }
5311 	  /* If the last note doesn't cover any instructions, remove it.  */
5312 	  if (temp->last != last)
5313 	    {
5314 	      temp->last->next = NULL;
5315 	      unused = last;
5316 	      last = temp->last;
5317 	      gcc_assert (strcmp (last->label, label) != 0);
5318 	    }
5319 	  else
5320 	    {
5321 	      gcc_assert (temp->first == temp->last
5322 			  || (temp->first->next == temp->last
5323 			      && TREE_CODE (decl) == PARM_DECL));
5324 	      memset (temp->last, '\0', sizeof (*temp->last));
5325 	      temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5326 	      return temp->last;
5327 	    }
5328 	}
5329       if (bitsize == -1 && NOTE_P (last->loc))
5330 	last_loc_note = last->loc;
5331       else if (piece_loc != NULL
5332 	       && *piece_loc != NULL_RTX
5333 	       && piece_bitpos == bitpos
5334 	       && decl_piece_bitsize (*piece_loc) == bitsize)
5335 	last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5336       else
5337 	last_loc_note = NULL_RTX;
5338       /* If the current location is the same as the end of the list,
5339 	 and either both or neither of the locations is uninitialized,
5340 	 we have nothing to do.  */
5341       if (last_loc_note == NULL_RTX
5342 	  || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5343 			    NOTE_VAR_LOCATION_LOC (loc_note)))
5344 	  || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5345 	       != NOTE_VAR_LOCATION_STATUS (loc_note))
5346 	      && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5347 		   == VAR_INIT_STATUS_UNINITIALIZED)
5348 		  || (NOTE_VAR_LOCATION_STATUS (loc_note)
5349 		      == VAR_INIT_STATUS_UNINITIALIZED))))
5350 	{
5351 	  /* Add LOC to the end of list and update LAST.  If the last
5352 	     element of the list has been removed above, reuse its
5353 	     memory for the new node, otherwise allocate a new one.  */
5354 	  if (unused)
5355 	    {
5356 	      loc = unused;
5357 	      memset (loc, '\0', sizeof (*loc));
5358 	    }
5359 	  else
5360 	    loc = ggc_cleared_alloc<var_loc_node> ();
5361 	  if (bitsize == -1 || piece_loc == NULL)
5362 	    loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5363 	  else
5364 	    adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5365 			       bitpos, piece_bitpos, bitsize, loc_note);
5366 	  last->next = loc;
5367 	  /* Ensure TEMP->LAST will point either to the new last but one
5368 	     element of the chain, or to the last element in it.  */
5369 	  if (last != temp->last)
5370 	    temp->last = last;
5371 	}
5372       else if (unused)
5373 	ggc_free (unused);
5374     }
5375   else
5376     {
5377       loc = ggc_cleared_alloc<var_loc_node> ();
5378       temp->first = loc;
5379       temp->last = loc;
5380       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5381     }
5382   return loc;
5383 }
5384 
5385 /* Keep track of the number of spaces used to indent the
5386    output of the debugging routines that print the structure of
5387    the DIE internal representation.  */
5388 static int print_indent;
5389 
5390 /* Indent the line the number of spaces given by print_indent.  */
5391 
5392 static inline void
5393 print_spaces (FILE *outfile)
5394 {
5395   fprintf (outfile, "%*s", print_indent, "");
5396 }
5397 
5398 /* Print a type signature in hex.  */
5399 
5400 static inline void
5401 print_signature (FILE *outfile, char *sig)
5402 {
5403   int i;
5404 
5405   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5406     fprintf (outfile, "%02x", sig[i] & 0xff);
5407 }
5408 
5409 static void print_loc_descr (dw_loc_descr_ref, FILE *);
5410 
5411 /* Print the value associated to the VAL DWARF value node to OUTFILE.  If
5412    RECURSE, output location descriptor operations.  */
5413 
5414 static void
5415 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
5416 {
5417   switch (val->val_class)
5418     {
5419     case dw_val_class_addr:
5420       fprintf (outfile, "address");
5421       break;
5422     case dw_val_class_offset:
5423       fprintf (outfile, "offset");
5424       break;
5425     case dw_val_class_loc:
5426       fprintf (outfile, "location descriptor");
5427       if (val->v.val_loc == NULL)
5428 	fprintf (outfile, " -> <null>\n");
5429       else if (recurse)
5430 	{
5431 	  fprintf (outfile, ":\n");
5432 	  print_indent += 4;
5433 	  print_loc_descr (val->v.val_loc, outfile);
5434 	  print_indent -= 4;
5435 	}
5436       else
5437 	fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
5438       break;
5439     case dw_val_class_loc_list:
5440       fprintf (outfile, "location list -> label:%s",
5441 	       val->v.val_loc_list->ll_symbol);
5442       break;
5443     case dw_val_class_range_list:
5444       fprintf (outfile, "range list");
5445       break;
5446     case dw_val_class_const:
5447       fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
5448       break;
5449     case dw_val_class_unsigned_const:
5450       fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
5451       break;
5452     case dw_val_class_const_double:
5453       fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
5454 			HOST_WIDE_INT_PRINT_UNSIGNED")",
5455 	       val->v.val_double.high,
5456 	       val->v.val_double.low);
5457       break;
5458     case dw_val_class_wide_int:
5459       {
5460 	int i = val->v.val_wide->get_len ();
5461 	fprintf (outfile, "constant (");
5462 	gcc_assert (i > 0);
5463 	if (val->v.val_wide->elt (i - 1) == 0)
5464 	  fprintf (outfile, "0x");
5465 	fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5466 		 val->v.val_wide->elt (--i));
5467 	while (--i >= 0)
5468 	  fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5469 		   val->v.val_wide->elt (i));
5470 	fprintf (outfile, ")");
5471 	break;
5472       }
5473     case dw_val_class_vec:
5474       fprintf (outfile, "floating-point or vector constant");
5475       break;
5476     case dw_val_class_flag:
5477       fprintf (outfile, "%u", val->v.val_flag);
5478       break;
5479     case dw_val_class_die_ref:
5480       if (val->v.val_die_ref.die != NULL)
5481 	{
5482 	  dw_die_ref die = val->v.val_die_ref.die;
5483 
5484 	  if (die->comdat_type_p)
5485 	    {
5486 	      fprintf (outfile, "die -> signature: ");
5487 	      print_signature (outfile,
5488 			       die->die_id.die_type_node->signature);
5489 	    }
5490 	  else if (die->die_id.die_symbol)
5491 	    fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
5492 	  else
5493 	    fprintf (outfile, "die -> %ld", die->die_offset);
5494 	  fprintf (outfile, " (%p)", (void *) die);
5495 	}
5496       else
5497 	fprintf (outfile, "die -> <null>");
5498       break;
5499     case dw_val_class_vms_delta:
5500       fprintf (outfile, "delta: @slotcount(%s-%s)",
5501 	       val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
5502       break;
5503     case dw_val_class_lbl_id:
5504     case dw_val_class_lineptr:
5505     case dw_val_class_macptr:
5506     case dw_val_class_high_pc:
5507       fprintf (outfile, "label: %s", val->v.val_lbl_id);
5508       break;
5509     case dw_val_class_str:
5510       if (val->v.val_str->str != NULL)
5511 	fprintf (outfile, "\"%s\"", val->v.val_str->str);
5512       else
5513 	fprintf (outfile, "<null>");
5514       break;
5515     case dw_val_class_file:
5516       fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
5517 	       val->v.val_file->emitted_number);
5518       break;
5519     case dw_val_class_data8:
5520       {
5521 	int i;
5522 
5523 	for (i = 0; i < 8; i++)
5524 	  fprintf (outfile, "%02x", val->v.val_data8[i]);
5525 	break;
5526       }
5527     default:
5528       break;
5529     }
5530 }
5531 
5532 /* Likewise, for a DIE attribute.  */
5533 
5534 static void
5535 print_attribute (dw_attr_ref a, bool recurse, FILE *outfile)
5536 {
5537   print_dw_val (&a->dw_attr_val, recurse, outfile);
5538 }
5539 
5540 
5541 /* Print the list of operands in the LOC location description to OUTFILE.  This
5542    routine is a debugging aid only.  */
5543 
5544 static void
5545 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
5546 {
5547   dw_loc_descr_ref l = loc;
5548 
5549   if (loc == NULL)
5550     {
5551       print_spaces (outfile);
5552       fprintf (outfile, "<null>\n");
5553       return;
5554     }
5555 
5556   for (l = loc; l != NULL; l = l->dw_loc_next)
5557     {
5558       print_spaces (outfile);
5559       fprintf (outfile, "(%p) %s",
5560 	       (void *) l,
5561 	       dwarf_stack_op_name (l->dw_loc_opc));
5562       if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
5563 	{
5564 	  fprintf (outfile, " ");
5565 	  print_dw_val (&l->dw_loc_oprnd1, false, outfile);
5566 	}
5567       if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
5568 	{
5569 	  fprintf (outfile, ", ");
5570 	  print_dw_val (&l->dw_loc_oprnd2, false, outfile);
5571 	}
5572       fprintf (outfile, "\n");
5573     }
5574 }
5575 
5576 /* Print the information associated with a given DIE, and its children.
5577    This routine is a debugging aid only.  */
5578 
5579 static void
5580 print_die (dw_die_ref die, FILE *outfile)
5581 {
5582   dw_attr_ref a;
5583   dw_die_ref c;
5584   unsigned ix;
5585 
5586   print_spaces (outfile);
5587   fprintf (outfile, "DIE %4ld: %s (%p)\n",
5588 	   die->die_offset, dwarf_tag_name (die->die_tag),
5589 	   (void*) die);
5590   print_spaces (outfile);
5591   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
5592   fprintf (outfile, " offset: %ld", die->die_offset);
5593   fprintf (outfile, " mark: %d\n", die->die_mark);
5594 
5595   if (die->comdat_type_p)
5596     {
5597       print_spaces (outfile);
5598       fprintf (outfile, "  signature: ");
5599       print_signature (outfile, die->die_id.die_type_node->signature);
5600       fprintf (outfile, "\n");
5601     }
5602 
5603   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5604     {
5605       print_spaces (outfile);
5606       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
5607 
5608       print_attribute (a, true, outfile);
5609       fprintf (outfile, "\n");
5610     }
5611 
5612   if (die->die_child != NULL)
5613     {
5614       print_indent += 4;
5615       FOR_EACH_CHILD (die, c, print_die (c, outfile));
5616       print_indent -= 4;
5617     }
5618   if (print_indent == 0)
5619     fprintf (outfile, "\n");
5620 }
5621 
5622 /* Print the list of operations in the LOC location description.  */
5623 
5624 DEBUG_FUNCTION void
5625 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
5626 {
5627   print_loc_descr (loc, stderr);
5628 }
5629 
5630 /* Print the information collected for a given DIE.  */
5631 
5632 DEBUG_FUNCTION void
5633 debug_dwarf_die (dw_die_ref die)
5634 {
5635   print_die (die, stderr);
5636 }
5637 
5638 DEBUG_FUNCTION void
5639 debug (die_struct &ref)
5640 {
5641   print_die (&ref, stderr);
5642 }
5643 
5644 DEBUG_FUNCTION void
5645 debug (die_struct *ptr)
5646 {
5647   if (ptr)
5648     debug (*ptr);
5649   else
5650     fprintf (stderr, "<nil>\n");
5651 }
5652 
5653 
5654 /* Print all DWARF information collected for the compilation unit.
5655    This routine is a debugging aid only.  */
5656 
5657 DEBUG_FUNCTION void
5658 debug_dwarf (void)
5659 {
5660   print_indent = 0;
5661   print_die (comp_unit_die (), stderr);
5662 }
5663 
5664 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
5665    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
5666    DIE that marks the start of the DIEs for this include file.  */
5667 
5668 static dw_die_ref
5669 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5670 {
5671   const char *filename = get_AT_string (bincl_die, DW_AT_name);
5672   dw_die_ref new_unit = gen_compile_unit_die (filename);
5673 
5674   new_unit->die_sib = old_unit;
5675   return new_unit;
5676 }
5677 
5678 /* Close an include-file CU and reopen the enclosing one.  */
5679 
5680 static dw_die_ref
5681 pop_compile_unit (dw_die_ref old_unit)
5682 {
5683   dw_die_ref new_unit = old_unit->die_sib;
5684 
5685   old_unit->die_sib = NULL;
5686   return new_unit;
5687 }
5688 
5689 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5690 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5691 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5692 
5693 /* Calculate the checksum of a location expression.  */
5694 
5695 static inline void
5696 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5697 {
5698   int tem;
5699   inchash::hash hstate;
5700   hashval_t hash;
5701 
5702   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5703   CHECKSUM (tem);
5704   hash_loc_operands (loc, hstate);
5705   hash = hstate.end();
5706   CHECKSUM (hash);
5707 }
5708 
5709 /* Calculate the checksum of an attribute.  */
5710 
5711 static void
5712 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5713 {
5714   dw_loc_descr_ref loc;
5715   rtx r;
5716 
5717   CHECKSUM (at->dw_attr);
5718 
5719   /* We don't care that this was compiled with a different compiler
5720      snapshot; if the output is the same, that's what matters.  */
5721   if (at->dw_attr == DW_AT_producer)
5722     return;
5723 
5724   switch (AT_class (at))
5725     {
5726     case dw_val_class_const:
5727       CHECKSUM (at->dw_attr_val.v.val_int);
5728       break;
5729     case dw_val_class_unsigned_const:
5730       CHECKSUM (at->dw_attr_val.v.val_unsigned);
5731       break;
5732     case dw_val_class_const_double:
5733       CHECKSUM (at->dw_attr_val.v.val_double);
5734       break;
5735     case dw_val_class_wide_int:
5736       CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
5737 		      get_full_len (*at->dw_attr_val.v.val_wide)
5738 		      * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
5739       break;
5740     case dw_val_class_vec:
5741       CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5742 		      (at->dw_attr_val.v.val_vec.length
5743 		       * at->dw_attr_val.v.val_vec.elt_size));
5744       break;
5745     case dw_val_class_flag:
5746       CHECKSUM (at->dw_attr_val.v.val_flag);
5747       break;
5748     case dw_val_class_str:
5749       CHECKSUM_STRING (AT_string (at));
5750       break;
5751 
5752     case dw_val_class_addr:
5753       r = AT_addr (at);
5754       gcc_assert (GET_CODE (r) == SYMBOL_REF);
5755       CHECKSUM_STRING (XSTR (r, 0));
5756       break;
5757 
5758     case dw_val_class_offset:
5759       CHECKSUM (at->dw_attr_val.v.val_offset);
5760       break;
5761 
5762     case dw_val_class_loc:
5763       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5764 	loc_checksum (loc, ctx);
5765       break;
5766 
5767     case dw_val_class_die_ref:
5768       die_checksum (AT_ref (at), ctx, mark);
5769       break;
5770 
5771     case dw_val_class_fde_ref:
5772     case dw_val_class_vms_delta:
5773     case dw_val_class_lbl_id:
5774     case dw_val_class_lineptr:
5775     case dw_val_class_macptr:
5776     case dw_val_class_high_pc:
5777       break;
5778 
5779     case dw_val_class_file:
5780       CHECKSUM_STRING (AT_file (at)->filename);
5781       break;
5782 
5783     case dw_val_class_data8:
5784       CHECKSUM (at->dw_attr_val.v.val_data8);
5785       break;
5786 
5787     default:
5788       break;
5789     }
5790 }
5791 
5792 /* Calculate the checksum of a DIE.  */
5793 
5794 static void
5795 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5796 {
5797   dw_die_ref c;
5798   dw_attr_ref a;
5799   unsigned ix;
5800 
5801   /* To avoid infinite recursion.  */
5802   if (die->die_mark)
5803     {
5804       CHECKSUM (die->die_mark);
5805       return;
5806     }
5807   die->die_mark = ++(*mark);
5808 
5809   CHECKSUM (die->die_tag);
5810 
5811   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5812     attr_checksum (a, ctx, mark);
5813 
5814   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5815 }
5816 
5817 #undef CHECKSUM
5818 #undef CHECKSUM_BLOCK
5819 #undef CHECKSUM_STRING
5820 
5821 /* For DWARF-4 types, include the trailing NULL when checksumming strings.  */
5822 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5823 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5824 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5825 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5826 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5827 #define CHECKSUM_ATTR(FOO) \
5828   if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5829 
5830 /* Calculate the checksum of a number in signed LEB128 format.  */
5831 
5832 static void
5833 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5834 {
5835   unsigned char byte;
5836   bool more;
5837 
5838   while (1)
5839     {
5840       byte = (value & 0x7f);
5841       value >>= 7;
5842       more = !((value == 0 && (byte & 0x40) == 0)
5843 		|| (value == -1 && (byte & 0x40) != 0));
5844       if (more)
5845 	byte |= 0x80;
5846       CHECKSUM (byte);
5847       if (!more)
5848 	break;
5849     }
5850 }
5851 
5852 /* Calculate the checksum of a number in unsigned LEB128 format.  */
5853 
5854 static void
5855 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5856 {
5857   while (1)
5858     {
5859       unsigned char byte = (value & 0x7f);
5860       value >>= 7;
5861       if (value != 0)
5862 	/* More bytes to follow.  */
5863 	byte |= 0x80;
5864       CHECKSUM (byte);
5865       if (value == 0)
5866 	break;
5867     }
5868 }
5869 
5870 /* Checksum the context of the DIE.  This adds the names of any
5871    surrounding namespaces or structures to the checksum.  */
5872 
5873 static void
5874 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5875 {
5876   const char *name;
5877   dw_die_ref spec;
5878   int tag = die->die_tag;
5879 
5880   if (tag != DW_TAG_namespace
5881       && tag != DW_TAG_structure_type
5882       && tag != DW_TAG_class_type)
5883     return;
5884 
5885   name = get_AT_string (die, DW_AT_name);
5886 
5887   spec = get_AT_ref (die, DW_AT_specification);
5888   if (spec != NULL)
5889     die = spec;
5890 
5891   if (die->die_parent != NULL)
5892     checksum_die_context (die->die_parent, ctx);
5893 
5894   CHECKSUM_ULEB128 ('C');
5895   CHECKSUM_ULEB128 (tag);
5896   if (name != NULL)
5897     CHECKSUM_STRING (name);
5898 }
5899 
5900 /* Calculate the checksum of a location expression.  */
5901 
5902 static inline void
5903 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5904 {
5905   /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5906      were emitted as a DW_FORM_sdata instead of a location expression.  */
5907   if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5908     {
5909       CHECKSUM_ULEB128 (DW_FORM_sdata);
5910       CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5911       return;
5912     }
5913 
5914   /* Otherwise, just checksum the raw location expression.  */
5915   while (loc != NULL)
5916     {
5917       inchash::hash hstate;
5918       hashval_t hash;
5919 
5920       CHECKSUM_ULEB128 (loc->dtprel);
5921       CHECKSUM_ULEB128 (loc->dw_loc_opc);
5922       hash_loc_operands (loc, hstate);
5923       hash = hstate.end ();
5924       CHECKSUM (hash);
5925       loc = loc->dw_loc_next;
5926     }
5927 }
5928 
5929 /* Calculate the checksum of an attribute.  */
5930 
5931 static void
5932 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5933 		       struct md5_ctx *ctx, int *mark)
5934 {
5935   dw_loc_descr_ref loc;
5936   rtx r;
5937 
5938   if (AT_class (at) == dw_val_class_die_ref)
5939     {
5940       dw_die_ref target_die = AT_ref (at);
5941 
5942       /* For pointer and reference types, we checksum only the (qualified)
5943 	 name of the target type (if there is a name).  For friend entries,
5944 	 we checksum only the (qualified) name of the target type or function.
5945 	 This allows the checksum to remain the same whether the target type
5946 	 is complete or not.  */
5947       if ((at->dw_attr == DW_AT_type
5948 	   && (tag == DW_TAG_pointer_type
5949 	       || tag == DW_TAG_reference_type
5950 	       || tag == DW_TAG_rvalue_reference_type
5951 	       || tag == DW_TAG_ptr_to_member_type))
5952 	  || (at->dw_attr == DW_AT_friend
5953 	      && tag == DW_TAG_friend))
5954 	{
5955 	  dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5956 
5957 	  if (name_attr != NULL)
5958 	    {
5959 	      dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5960 
5961 	      if (decl == NULL)
5962 		decl = target_die;
5963 	      CHECKSUM_ULEB128 ('N');
5964 	      CHECKSUM_ULEB128 (at->dw_attr);
5965 	      if (decl->die_parent != NULL)
5966 		checksum_die_context (decl->die_parent, ctx);
5967 	      CHECKSUM_ULEB128 ('E');
5968 	      CHECKSUM_STRING (AT_string (name_attr));
5969 	      return;
5970 	    }
5971 	}
5972 
5973       /* For all other references to another DIE, we check to see if the
5974          target DIE has already been visited.  If it has, we emit a
5975          backward reference; if not, we descend recursively.  */
5976       if (target_die->die_mark > 0)
5977         {
5978 	  CHECKSUM_ULEB128 ('R');
5979 	  CHECKSUM_ULEB128 (at->dw_attr);
5980 	  CHECKSUM_ULEB128 (target_die->die_mark);
5981         }
5982       else
5983         {
5984 	  dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5985 
5986 	  if (decl == NULL)
5987 	    decl = target_die;
5988 	  target_die->die_mark = ++(*mark);
5989 	  CHECKSUM_ULEB128 ('T');
5990 	  CHECKSUM_ULEB128 (at->dw_attr);
5991 	  if (decl->die_parent != NULL)
5992 	    checksum_die_context (decl->die_parent, ctx);
5993 	  die_checksum_ordered (target_die, ctx, mark);
5994         }
5995       return;
5996     }
5997 
5998   CHECKSUM_ULEB128 ('A');
5999   CHECKSUM_ULEB128 (at->dw_attr);
6000 
6001   switch (AT_class (at))
6002     {
6003     case dw_val_class_const:
6004       CHECKSUM_ULEB128 (DW_FORM_sdata);
6005       CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
6006       break;
6007 
6008     case dw_val_class_unsigned_const:
6009       CHECKSUM_ULEB128 (DW_FORM_sdata);
6010       CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6011       break;
6012 
6013     case dw_val_class_const_double:
6014       CHECKSUM_ULEB128 (DW_FORM_block);
6015       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6016       CHECKSUM (at->dw_attr_val.v.val_double);
6017       break;
6018 
6019     case dw_val_class_wide_int:
6020       CHECKSUM_ULEB128 (DW_FORM_block);
6021       CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
6022 			* HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
6023       CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6024 		      get_full_len (*at->dw_attr_val.v.val_wide)
6025 		      * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6026       break;
6027 
6028     case dw_val_class_vec:
6029       CHECKSUM_ULEB128 (DW_FORM_block);
6030       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6031 			* at->dw_attr_val.v.val_vec.elt_size);
6032       CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6033 		      (at->dw_attr_val.v.val_vec.length
6034 		       * at->dw_attr_val.v.val_vec.elt_size));
6035       break;
6036 
6037     case dw_val_class_flag:
6038       CHECKSUM_ULEB128 (DW_FORM_flag);
6039       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6040       break;
6041 
6042     case dw_val_class_str:
6043       CHECKSUM_ULEB128 (DW_FORM_string);
6044       CHECKSUM_STRING (AT_string (at));
6045       break;
6046 
6047     case dw_val_class_addr:
6048       r = AT_addr (at);
6049       gcc_assert (GET_CODE (r) == SYMBOL_REF);
6050       CHECKSUM_ULEB128 (DW_FORM_string);
6051       CHECKSUM_STRING (XSTR (r, 0));
6052       break;
6053 
6054     case dw_val_class_offset:
6055       CHECKSUM_ULEB128 (DW_FORM_sdata);
6056       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6057       break;
6058 
6059     case dw_val_class_loc:
6060       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6061 	loc_checksum_ordered (loc, ctx);
6062       break;
6063 
6064     case dw_val_class_fde_ref:
6065     case dw_val_class_lbl_id:
6066     case dw_val_class_lineptr:
6067     case dw_val_class_macptr:
6068     case dw_val_class_high_pc:
6069       break;
6070 
6071     case dw_val_class_file:
6072       CHECKSUM_ULEB128 (DW_FORM_string);
6073       CHECKSUM_STRING (AT_file (at)->filename);
6074       break;
6075 
6076     case dw_val_class_data8:
6077       CHECKSUM (at->dw_attr_val.v.val_data8);
6078       break;
6079 
6080     default:
6081       break;
6082     }
6083 }
6084 
6085 struct checksum_attributes
6086 {
6087   dw_attr_ref at_name;
6088   dw_attr_ref at_type;
6089   dw_attr_ref at_friend;
6090   dw_attr_ref at_accessibility;
6091   dw_attr_ref at_address_class;
6092   dw_attr_ref at_allocated;
6093   dw_attr_ref at_artificial;
6094   dw_attr_ref at_associated;
6095   dw_attr_ref at_binary_scale;
6096   dw_attr_ref at_bit_offset;
6097   dw_attr_ref at_bit_size;
6098   dw_attr_ref at_bit_stride;
6099   dw_attr_ref at_byte_size;
6100   dw_attr_ref at_byte_stride;
6101   dw_attr_ref at_const_value;
6102   dw_attr_ref at_containing_type;
6103   dw_attr_ref at_count;
6104   dw_attr_ref at_data_location;
6105   dw_attr_ref at_data_member_location;
6106   dw_attr_ref at_decimal_scale;
6107   dw_attr_ref at_decimal_sign;
6108   dw_attr_ref at_default_value;
6109   dw_attr_ref at_digit_count;
6110   dw_attr_ref at_discr;
6111   dw_attr_ref at_discr_list;
6112   dw_attr_ref at_discr_value;
6113   dw_attr_ref at_encoding;
6114   dw_attr_ref at_endianity;
6115   dw_attr_ref at_explicit;
6116   dw_attr_ref at_is_optional;
6117   dw_attr_ref at_location;
6118   dw_attr_ref at_lower_bound;
6119   dw_attr_ref at_mutable;
6120   dw_attr_ref at_ordering;
6121   dw_attr_ref at_picture_string;
6122   dw_attr_ref at_prototyped;
6123   dw_attr_ref at_small;
6124   dw_attr_ref at_segment;
6125   dw_attr_ref at_string_length;
6126   dw_attr_ref at_threads_scaled;
6127   dw_attr_ref at_upper_bound;
6128   dw_attr_ref at_use_location;
6129   dw_attr_ref at_use_UTF8;
6130   dw_attr_ref at_variable_parameter;
6131   dw_attr_ref at_virtuality;
6132   dw_attr_ref at_visibility;
6133   dw_attr_ref at_vtable_elem_location;
6134 };
6135 
6136 /* Collect the attributes that we will want to use for the checksum.  */
6137 
6138 static void
6139 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6140 {
6141   dw_attr_ref a;
6142   unsigned ix;
6143 
6144   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6145     {
6146       switch (a->dw_attr)
6147         {
6148         case DW_AT_name:
6149           attrs->at_name = a;
6150           break;
6151         case DW_AT_type:
6152           attrs->at_type = a;
6153           break;
6154         case DW_AT_friend:
6155           attrs->at_friend = a;
6156           break;
6157         case DW_AT_accessibility:
6158           attrs->at_accessibility = a;
6159           break;
6160         case DW_AT_address_class:
6161           attrs->at_address_class = a;
6162           break;
6163         case DW_AT_allocated:
6164           attrs->at_allocated = a;
6165           break;
6166         case DW_AT_artificial:
6167           attrs->at_artificial = a;
6168           break;
6169         case DW_AT_associated:
6170           attrs->at_associated = a;
6171           break;
6172         case DW_AT_binary_scale:
6173           attrs->at_binary_scale = a;
6174           break;
6175         case DW_AT_bit_offset:
6176           attrs->at_bit_offset = a;
6177           break;
6178         case DW_AT_bit_size:
6179           attrs->at_bit_size = a;
6180           break;
6181         case DW_AT_bit_stride:
6182           attrs->at_bit_stride = a;
6183           break;
6184         case DW_AT_byte_size:
6185           attrs->at_byte_size = a;
6186           break;
6187         case DW_AT_byte_stride:
6188           attrs->at_byte_stride = a;
6189           break;
6190         case DW_AT_const_value:
6191           attrs->at_const_value = a;
6192           break;
6193         case DW_AT_containing_type:
6194           attrs->at_containing_type = a;
6195           break;
6196         case DW_AT_count:
6197           attrs->at_count = a;
6198           break;
6199         case DW_AT_data_location:
6200           attrs->at_data_location = a;
6201           break;
6202         case DW_AT_data_member_location:
6203           attrs->at_data_member_location = a;
6204           break;
6205         case DW_AT_decimal_scale:
6206           attrs->at_decimal_scale = a;
6207           break;
6208         case DW_AT_decimal_sign:
6209           attrs->at_decimal_sign = a;
6210           break;
6211         case DW_AT_default_value:
6212           attrs->at_default_value = a;
6213           break;
6214         case DW_AT_digit_count:
6215           attrs->at_digit_count = a;
6216           break;
6217         case DW_AT_discr:
6218           attrs->at_discr = a;
6219           break;
6220         case DW_AT_discr_list:
6221           attrs->at_discr_list = a;
6222           break;
6223         case DW_AT_discr_value:
6224           attrs->at_discr_value = a;
6225           break;
6226         case DW_AT_encoding:
6227           attrs->at_encoding = a;
6228           break;
6229         case DW_AT_endianity:
6230           attrs->at_endianity = a;
6231           break;
6232         case DW_AT_explicit:
6233           attrs->at_explicit = a;
6234           break;
6235         case DW_AT_is_optional:
6236           attrs->at_is_optional = a;
6237           break;
6238         case DW_AT_location:
6239           attrs->at_location = a;
6240           break;
6241         case DW_AT_lower_bound:
6242           attrs->at_lower_bound = a;
6243           break;
6244         case DW_AT_mutable:
6245           attrs->at_mutable = a;
6246           break;
6247         case DW_AT_ordering:
6248           attrs->at_ordering = a;
6249           break;
6250         case DW_AT_picture_string:
6251           attrs->at_picture_string = a;
6252           break;
6253         case DW_AT_prototyped:
6254           attrs->at_prototyped = a;
6255           break;
6256         case DW_AT_small:
6257           attrs->at_small = a;
6258           break;
6259         case DW_AT_segment:
6260           attrs->at_segment = a;
6261           break;
6262         case DW_AT_string_length:
6263           attrs->at_string_length = a;
6264           break;
6265         case DW_AT_threads_scaled:
6266           attrs->at_threads_scaled = a;
6267           break;
6268         case DW_AT_upper_bound:
6269           attrs->at_upper_bound = a;
6270           break;
6271         case DW_AT_use_location:
6272           attrs->at_use_location = a;
6273           break;
6274         case DW_AT_use_UTF8:
6275           attrs->at_use_UTF8 = a;
6276           break;
6277         case DW_AT_variable_parameter:
6278           attrs->at_variable_parameter = a;
6279           break;
6280         case DW_AT_virtuality:
6281           attrs->at_virtuality = a;
6282           break;
6283         case DW_AT_visibility:
6284           attrs->at_visibility = a;
6285           break;
6286         case DW_AT_vtable_elem_location:
6287           attrs->at_vtable_elem_location = a;
6288           break;
6289         default:
6290           break;
6291         }
6292     }
6293 }
6294 
6295 /* Calculate the checksum of a DIE, using an ordered subset of attributes.  */
6296 
6297 static void
6298 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6299 {
6300   dw_die_ref c;
6301   dw_die_ref decl;
6302   struct checksum_attributes attrs;
6303 
6304   CHECKSUM_ULEB128 ('D');
6305   CHECKSUM_ULEB128 (die->die_tag);
6306 
6307   memset (&attrs, 0, sizeof (attrs));
6308 
6309   decl = get_AT_ref (die, DW_AT_specification);
6310   if (decl != NULL)
6311     collect_checksum_attributes (&attrs, decl);
6312   collect_checksum_attributes (&attrs, die);
6313 
6314   CHECKSUM_ATTR (attrs.at_name);
6315   CHECKSUM_ATTR (attrs.at_accessibility);
6316   CHECKSUM_ATTR (attrs.at_address_class);
6317   CHECKSUM_ATTR (attrs.at_allocated);
6318   CHECKSUM_ATTR (attrs.at_artificial);
6319   CHECKSUM_ATTR (attrs.at_associated);
6320   CHECKSUM_ATTR (attrs.at_binary_scale);
6321   CHECKSUM_ATTR (attrs.at_bit_offset);
6322   CHECKSUM_ATTR (attrs.at_bit_size);
6323   CHECKSUM_ATTR (attrs.at_bit_stride);
6324   CHECKSUM_ATTR (attrs.at_byte_size);
6325   CHECKSUM_ATTR (attrs.at_byte_stride);
6326   CHECKSUM_ATTR (attrs.at_const_value);
6327   CHECKSUM_ATTR (attrs.at_containing_type);
6328   CHECKSUM_ATTR (attrs.at_count);
6329   CHECKSUM_ATTR (attrs.at_data_location);
6330   CHECKSUM_ATTR (attrs.at_data_member_location);
6331   CHECKSUM_ATTR (attrs.at_decimal_scale);
6332   CHECKSUM_ATTR (attrs.at_decimal_sign);
6333   CHECKSUM_ATTR (attrs.at_default_value);
6334   CHECKSUM_ATTR (attrs.at_digit_count);
6335   CHECKSUM_ATTR (attrs.at_discr);
6336   CHECKSUM_ATTR (attrs.at_discr_list);
6337   CHECKSUM_ATTR (attrs.at_discr_value);
6338   CHECKSUM_ATTR (attrs.at_encoding);
6339   CHECKSUM_ATTR (attrs.at_endianity);
6340   CHECKSUM_ATTR (attrs.at_explicit);
6341   CHECKSUM_ATTR (attrs.at_is_optional);
6342   CHECKSUM_ATTR (attrs.at_location);
6343   CHECKSUM_ATTR (attrs.at_lower_bound);
6344   CHECKSUM_ATTR (attrs.at_mutable);
6345   CHECKSUM_ATTR (attrs.at_ordering);
6346   CHECKSUM_ATTR (attrs.at_picture_string);
6347   CHECKSUM_ATTR (attrs.at_prototyped);
6348   CHECKSUM_ATTR (attrs.at_small);
6349   CHECKSUM_ATTR (attrs.at_segment);
6350   CHECKSUM_ATTR (attrs.at_string_length);
6351   CHECKSUM_ATTR (attrs.at_threads_scaled);
6352   CHECKSUM_ATTR (attrs.at_upper_bound);
6353   CHECKSUM_ATTR (attrs.at_use_location);
6354   CHECKSUM_ATTR (attrs.at_use_UTF8);
6355   CHECKSUM_ATTR (attrs.at_variable_parameter);
6356   CHECKSUM_ATTR (attrs.at_virtuality);
6357   CHECKSUM_ATTR (attrs.at_visibility);
6358   CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6359   CHECKSUM_ATTR (attrs.at_type);
6360   CHECKSUM_ATTR (attrs.at_friend);
6361 
6362   /* Checksum the child DIEs.  */
6363   c = die->die_child;
6364   if (c) do {
6365     dw_attr_ref name_attr;
6366 
6367     c = c->die_sib;
6368     name_attr = get_AT (c, DW_AT_name);
6369     if (is_template_instantiation (c))
6370       {
6371 	/* Ignore instantiations of member type and function templates.  */
6372       }
6373     else if (name_attr != NULL
6374 	     && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6375       {
6376 	/* Use a shallow checksum for named nested types and member
6377 	   functions.  */
6378         CHECKSUM_ULEB128 ('S');
6379         CHECKSUM_ULEB128 (c->die_tag);
6380         CHECKSUM_STRING (AT_string (name_attr));
6381       }
6382     else
6383       {
6384 	/* Use a deep checksum for other children.  */
6385         /* Mark this DIE so it gets processed when unmarking.  */
6386         if (c->die_mark == 0)
6387           c->die_mark = -1;
6388         die_checksum_ordered (c, ctx, mark);
6389       }
6390   } while (c != die->die_child);
6391 
6392   CHECKSUM_ULEB128 (0);
6393 }
6394 
6395 /* Add a type name and tag to a hash.  */
6396 static void
6397 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6398 {
6399   CHECKSUM_ULEB128 (tag);
6400   CHECKSUM_STRING (name);
6401 }
6402 
6403 #undef CHECKSUM
6404 #undef CHECKSUM_STRING
6405 #undef CHECKSUM_ATTR
6406 #undef CHECKSUM_LEB128
6407 #undef CHECKSUM_ULEB128
6408 
6409 /* Generate the type signature for DIE.  This is computed by generating an
6410    MD5 checksum over the DIE's tag, its relevant attributes, and its
6411    children.  Attributes that are references to other DIEs are processed
6412    by recursion, using the MARK field to prevent infinite recursion.
6413    If the DIE is nested inside a namespace or another type, we also
6414    need to include that context in the signature.  The lower 64 bits
6415    of the resulting MD5 checksum comprise the signature.  */
6416 
6417 static void
6418 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6419 {
6420   int mark;
6421   const char *name;
6422   unsigned char checksum[16];
6423   struct md5_ctx ctx;
6424   dw_die_ref decl;
6425   dw_die_ref parent;
6426 
6427   name = get_AT_string (die, DW_AT_name);
6428   decl = get_AT_ref (die, DW_AT_specification);
6429   parent = get_die_parent (die);
6430 
6431   /* First, compute a signature for just the type name (and its surrounding
6432      context, if any.  This is stored in the type unit DIE for link-time
6433      ODR (one-definition rule) checking.  */
6434 
6435   if (is_cxx () && name != NULL)
6436     {
6437       md5_init_ctx (&ctx);
6438 
6439       /* Checksum the names of surrounding namespaces and structures.  */
6440       if (parent != NULL)
6441         checksum_die_context (parent, &ctx);
6442 
6443       /* Checksum the current DIE. */
6444       die_odr_checksum (die->die_tag, name, &ctx);
6445       md5_finish_ctx (&ctx, checksum);
6446 
6447       add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6448     }
6449 
6450   /* Next, compute the complete type signature.  */
6451 
6452   md5_init_ctx (&ctx);
6453   mark = 1;
6454   die->die_mark = mark;
6455 
6456   /* Checksum the names of surrounding namespaces and structures.  */
6457   if (parent != NULL)
6458     checksum_die_context (parent, &ctx);
6459 
6460   /* Checksum the DIE and its children.  */
6461   die_checksum_ordered (die, &ctx, &mark);
6462   unmark_all_dies (die);
6463   md5_finish_ctx (&ctx, checksum);
6464 
6465   /* Store the signature in the type node and link the type DIE and the
6466      type node together.  */
6467   memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6468           DWARF_TYPE_SIGNATURE_SIZE);
6469   die->comdat_type_p = true;
6470   die->die_id.die_type_node = type_node;
6471   type_node->type_die = die;
6472 
6473   /* If the DIE is a specification, link its declaration to the type node
6474      as well.  */
6475   if (decl != NULL)
6476     {
6477       decl->comdat_type_p = true;
6478       decl->die_id.die_type_node = type_node;
6479     }
6480 }
6481 
6482 /* Do the location expressions look same?  */
6483 static inline int
6484 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6485 {
6486   return loc1->dw_loc_opc == loc2->dw_loc_opc
6487 	 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6488 	 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6489 }
6490 
6491 /* Do the values look the same?  */
6492 static int
6493 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6494 {
6495   dw_loc_descr_ref loc1, loc2;
6496   rtx r1, r2;
6497 
6498   if (v1->val_class != v2->val_class)
6499     return 0;
6500 
6501   switch (v1->val_class)
6502     {
6503     case dw_val_class_const:
6504       return v1->v.val_int == v2->v.val_int;
6505     case dw_val_class_unsigned_const:
6506       return v1->v.val_unsigned == v2->v.val_unsigned;
6507     case dw_val_class_const_double:
6508       return v1->v.val_double.high == v2->v.val_double.high
6509 	     && v1->v.val_double.low == v2->v.val_double.low;
6510     case dw_val_class_wide_int:
6511       return *v1->v.val_wide == *v2->v.val_wide;
6512     case dw_val_class_vec:
6513       if (v1->v.val_vec.length != v2->v.val_vec.length
6514 	  || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6515 	return 0;
6516       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6517 		  v1->v.val_vec.length * v1->v.val_vec.elt_size))
6518 	return 0;
6519       return 1;
6520     case dw_val_class_flag:
6521       return v1->v.val_flag == v2->v.val_flag;
6522     case dw_val_class_str:
6523       return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6524 
6525     case dw_val_class_addr:
6526       r1 = v1->v.val_addr;
6527       r2 = v2->v.val_addr;
6528       if (GET_CODE (r1) != GET_CODE (r2))
6529 	return 0;
6530       return !rtx_equal_p (r1, r2);
6531 
6532     case dw_val_class_offset:
6533       return v1->v.val_offset == v2->v.val_offset;
6534 
6535     case dw_val_class_loc:
6536       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6537 	   loc1 && loc2;
6538 	   loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6539 	if (!same_loc_p (loc1, loc2, mark))
6540 	  return 0;
6541       return !loc1 && !loc2;
6542 
6543     case dw_val_class_die_ref:
6544       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6545 
6546     case dw_val_class_fde_ref:
6547     case dw_val_class_vms_delta:
6548     case dw_val_class_lbl_id:
6549     case dw_val_class_lineptr:
6550     case dw_val_class_macptr:
6551     case dw_val_class_high_pc:
6552       return 1;
6553 
6554     case dw_val_class_file:
6555       return v1->v.val_file == v2->v.val_file;
6556 
6557     case dw_val_class_data8:
6558       return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6559 
6560     default:
6561       return 1;
6562     }
6563 }
6564 
6565 /* Do the attributes look the same?  */
6566 
6567 static int
6568 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6569 {
6570   if (at1->dw_attr != at2->dw_attr)
6571     return 0;
6572 
6573   /* We don't care that this was compiled with a different compiler
6574      snapshot; if the output is the same, that's what matters. */
6575   if (at1->dw_attr == DW_AT_producer)
6576     return 1;
6577 
6578   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6579 }
6580 
6581 /* Do the dies look the same?  */
6582 
6583 static int
6584 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6585 {
6586   dw_die_ref c1, c2;
6587   dw_attr_ref a1;
6588   unsigned ix;
6589 
6590   /* To avoid infinite recursion.  */
6591   if (die1->die_mark)
6592     return die1->die_mark == die2->die_mark;
6593   die1->die_mark = die2->die_mark = ++(*mark);
6594 
6595   if (die1->die_tag != die2->die_tag)
6596     return 0;
6597 
6598   if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6599     return 0;
6600 
6601   FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6602     if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6603       return 0;
6604 
6605   c1 = die1->die_child;
6606   c2 = die2->die_child;
6607   if (! c1)
6608     {
6609       if (c2)
6610 	return 0;
6611     }
6612   else
6613     for (;;)
6614       {
6615 	if (!same_die_p (c1, c2, mark))
6616 	  return 0;
6617 	c1 = c1->die_sib;
6618 	c2 = c2->die_sib;
6619 	if (c1 == die1->die_child)
6620 	  {
6621 	    if (c2 == die2->die_child)
6622 	      break;
6623 	    else
6624 	      return 0;
6625 	  }
6626     }
6627 
6628   return 1;
6629 }
6630 
6631 /* Do the dies look the same?  Wrapper around same_die_p.  */
6632 
6633 static int
6634 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6635 {
6636   int mark = 0;
6637   int ret = same_die_p (die1, die2, &mark);
6638 
6639   unmark_all_dies (die1);
6640   unmark_all_dies (die2);
6641 
6642   return ret;
6643 }
6644 
6645 /* The prefix to attach to symbols on DIEs in the current comdat debug
6646    info section.  */
6647 static const char *comdat_symbol_id;
6648 
6649 /* The index of the current symbol within the current comdat CU.  */
6650 static unsigned int comdat_symbol_number;
6651 
6652 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6653    children, and set comdat_symbol_id accordingly.  */
6654 
6655 static void
6656 compute_section_prefix (dw_die_ref unit_die)
6657 {
6658   const char *die_name = get_AT_string (unit_die, DW_AT_name);
6659   const char *base = die_name ? lbasename (die_name) : "anonymous";
6660   char *name = XALLOCAVEC (char, strlen (base) + 64);
6661   char *p;
6662   int i, mark;
6663   unsigned char checksum[16];
6664   struct md5_ctx ctx;
6665 
6666   /* Compute the checksum of the DIE, then append part of it as hex digits to
6667      the name filename of the unit.  */
6668 
6669   md5_init_ctx (&ctx);
6670   mark = 0;
6671   die_checksum (unit_die, &ctx, &mark);
6672   unmark_all_dies (unit_die);
6673   md5_finish_ctx (&ctx, checksum);
6674 
6675   sprintf (name, "%s.", base);
6676   clean_symbol_name (name);
6677 
6678   p = name + strlen (name);
6679   for (i = 0; i < 4; i++)
6680     {
6681       sprintf (p, "%.2x", checksum[i]);
6682       p += 2;
6683     }
6684 
6685   comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6686   comdat_symbol_number = 0;
6687 }
6688 
6689 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
6690 
6691 static int
6692 is_type_die (dw_die_ref die)
6693 {
6694   switch (die->die_tag)
6695     {
6696     case DW_TAG_array_type:
6697     case DW_TAG_class_type:
6698     case DW_TAG_interface_type:
6699     case DW_TAG_enumeration_type:
6700     case DW_TAG_pointer_type:
6701     case DW_TAG_reference_type:
6702     case DW_TAG_rvalue_reference_type:
6703     case DW_TAG_string_type:
6704     case DW_TAG_structure_type:
6705     case DW_TAG_subroutine_type:
6706     case DW_TAG_union_type:
6707     case DW_TAG_ptr_to_member_type:
6708     case DW_TAG_set_type:
6709     case DW_TAG_subrange_type:
6710     case DW_TAG_base_type:
6711     case DW_TAG_const_type:
6712     case DW_TAG_file_type:
6713     case DW_TAG_packed_type:
6714     case DW_TAG_volatile_type:
6715     case DW_TAG_typedef:
6716       return 1;
6717     default:
6718       return 0;
6719     }
6720 }
6721 
6722 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6723    Basically, we want to choose the bits that are likely to be shared between
6724    compilations (types) and leave out the bits that are specific to individual
6725    compilations (functions).  */
6726 
6727 static int
6728 is_comdat_die (dw_die_ref c)
6729 {
6730   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6731      we do for stabs.  The advantage is a greater likelihood of sharing between
6732      objects that don't include headers in the same order (and therefore would
6733      put the base types in a different comdat).  jason 8/28/00 */
6734 
6735   if (c->die_tag == DW_TAG_base_type)
6736     return 0;
6737 
6738   if (c->die_tag == DW_TAG_pointer_type
6739       || c->die_tag == DW_TAG_reference_type
6740       || c->die_tag == DW_TAG_rvalue_reference_type
6741       || c->die_tag == DW_TAG_const_type
6742       || c->die_tag == DW_TAG_volatile_type)
6743     {
6744       dw_die_ref t = get_AT_ref (c, DW_AT_type);
6745 
6746       return t ? is_comdat_die (t) : 0;
6747     }
6748 
6749   return is_type_die (c);
6750 }
6751 
6752 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6753    compilation unit.  */
6754 
6755 static int
6756 is_symbol_die (dw_die_ref c)
6757 {
6758   return (is_type_die (c)
6759 	  || is_declaration_die (c)
6760 	  || c->die_tag == DW_TAG_namespace
6761 	  || c->die_tag == DW_TAG_module);
6762 }
6763 
6764 /* Returns true iff C is a compile-unit DIE.  */
6765 
6766 static inline bool
6767 is_cu_die (dw_die_ref c)
6768 {
6769   return c && c->die_tag == DW_TAG_compile_unit;
6770 }
6771 
6772 /* Returns true iff C is a unit DIE of some sort.  */
6773 
6774 static inline bool
6775 is_unit_die (dw_die_ref c)
6776 {
6777   return c && (c->die_tag == DW_TAG_compile_unit
6778 	       || c->die_tag == DW_TAG_partial_unit
6779 	       || c->die_tag == DW_TAG_type_unit);
6780 }
6781 
6782 /* Returns true iff C is a namespace DIE.  */
6783 
6784 static inline bool
6785 is_namespace_die (dw_die_ref c)
6786 {
6787   return c && c->die_tag == DW_TAG_namespace;
6788 }
6789 
6790 /* Returns true iff C is a class or structure DIE.  */
6791 
6792 static inline bool
6793 is_class_die (dw_die_ref c)
6794 {
6795   return c && (c->die_tag == DW_TAG_class_type
6796                || c->die_tag == DW_TAG_structure_type);
6797 }
6798 
6799 /* Return non-zero if this DIE is a template parameter.  */
6800 
6801 static inline bool
6802 is_template_parameter (dw_die_ref die)
6803 {
6804   switch (die->die_tag)
6805     {
6806     case DW_TAG_template_type_param:
6807     case DW_TAG_template_value_param:
6808     case DW_TAG_GNU_template_template_param:
6809     case DW_TAG_GNU_template_parameter_pack:
6810       return true;
6811     default:
6812       return false;
6813     }
6814 }
6815 
6816 /* Return non-zero if this DIE represents a template instantiation.  */
6817 
6818 static inline bool
6819 is_template_instantiation (dw_die_ref die)
6820 {
6821   dw_die_ref c;
6822 
6823   if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
6824     return false;
6825   FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
6826   return false;
6827 }
6828 
6829 static char *
6830 gen_internal_sym (const char *prefix)
6831 {
6832   char buf[256];
6833 
6834   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6835   return xstrdup (buf);
6836 }
6837 
6838 /* Assign symbols to all worthy DIEs under DIE.  */
6839 
6840 static void
6841 assign_symbol_names (dw_die_ref die)
6842 {
6843   dw_die_ref c;
6844 
6845   if (is_symbol_die (die) && !die->comdat_type_p)
6846     {
6847       if (comdat_symbol_id)
6848 	{
6849 	  char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6850 
6851 	  sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6852 		   comdat_symbol_id, comdat_symbol_number++);
6853 	  die->die_id.die_symbol = xstrdup (p);
6854 	}
6855       else
6856 	die->die_id.die_symbol = gen_internal_sym ("LDIE");
6857     }
6858 
6859   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6860 }
6861 
6862 struct cu_hash_table_entry
6863 {
6864   dw_die_ref cu;
6865   unsigned min_comdat_num, max_comdat_num;
6866   struct cu_hash_table_entry *next;
6867 };
6868 
6869 /* Helpers to manipulate hash table of CUs.  */
6870 
6871 struct cu_hash_table_entry_hasher
6872 {
6873   typedef cu_hash_table_entry value_type;
6874   typedef die_struct compare_type;
6875   static inline hashval_t hash (const value_type *);
6876   static inline bool equal (const value_type *, const compare_type *);
6877   static inline void remove (value_type *);
6878 };
6879 
6880 inline hashval_t
6881 cu_hash_table_entry_hasher::hash (const value_type *entry)
6882 {
6883   return htab_hash_string (entry->cu->die_id.die_symbol);
6884 }
6885 
6886 inline bool
6887 cu_hash_table_entry_hasher::equal (const value_type *entry1,
6888 				   const compare_type *entry2)
6889 {
6890   return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6891 }
6892 
6893 inline void
6894 cu_hash_table_entry_hasher::remove (value_type *entry)
6895 {
6896   struct cu_hash_table_entry *next;
6897 
6898   while (entry)
6899     {
6900       next = entry->next;
6901       free (entry);
6902       entry = next;
6903     }
6904 }
6905 
6906 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
6907 
6908 /* Check whether we have already seen this CU and set up SYM_NUM
6909    accordingly.  */
6910 static int
6911 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
6912 {
6913   struct cu_hash_table_entry dummy;
6914   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6915 
6916   dummy.max_comdat_num = 0;
6917 
6918   slot = htable->find_slot_with_hash (cu,
6919 				      htab_hash_string (cu->die_id.die_symbol),
6920 				      INSERT);
6921   entry = *slot;
6922 
6923   for (; entry; last = entry, entry = entry->next)
6924     {
6925       if (same_die_p_wrap (cu, entry->cu))
6926 	break;
6927     }
6928 
6929   if (entry)
6930     {
6931       *sym_num = entry->min_comdat_num;
6932       return 1;
6933     }
6934 
6935   entry = XCNEW (struct cu_hash_table_entry);
6936   entry->cu = cu;
6937   entry->min_comdat_num = *sym_num = last->max_comdat_num;
6938   entry->next = *slot;
6939   *slot = entry;
6940 
6941   return 0;
6942 }
6943 
6944 /* Record SYM_NUM to record of CU in HTABLE.  */
6945 static void
6946 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
6947 			     unsigned int sym_num)
6948 {
6949   struct cu_hash_table_entry **slot, *entry;
6950 
6951   slot = htable->find_slot_with_hash (cu,
6952 				      htab_hash_string (cu->die_id.die_symbol),
6953 				      NO_INSERT);
6954   entry = *slot;
6955 
6956   entry->max_comdat_num = sym_num;
6957 }
6958 
6959 /* Traverse the DIE (which is always comp_unit_die), and set up
6960    additional compilation units for each of the include files we see
6961    bracketed by BINCL/EINCL.  */
6962 
6963 static void
6964 break_out_includes (dw_die_ref die)
6965 {
6966   dw_die_ref c;
6967   dw_die_ref unit = NULL;
6968   limbo_die_node *node, **pnode;
6969 
6970   c = die->die_child;
6971   if (c) do {
6972     dw_die_ref prev = c;
6973     c = c->die_sib;
6974     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6975 	   || (unit && is_comdat_die (c)))
6976       {
6977 	dw_die_ref next = c->die_sib;
6978 
6979 	/* This DIE is for a secondary CU; remove it from the main one.  */
6980 	remove_child_with_prev (c, prev);
6981 
6982 	if (c->die_tag == DW_TAG_GNU_BINCL)
6983 	  unit = push_new_compile_unit (unit, c);
6984 	else if (c->die_tag == DW_TAG_GNU_EINCL)
6985 	  unit = pop_compile_unit (unit);
6986 	else
6987 	  add_child_die (unit, c);
6988 	c = next;
6989 	if (c == die->die_child)
6990 	  break;
6991       }
6992   } while (c != die->die_child);
6993 
6994 #if 0
6995   /* We can only use this in debugging, since the frontend doesn't check
6996      to make sure that we leave every include file we enter.  */
6997   gcc_assert (!unit);
6998 #endif
6999 
7000   assign_symbol_names (die);
7001   cu_hash_type cu_hash_table (10);
7002   for (node = limbo_die_list, pnode = &limbo_die_list;
7003        node;
7004        node = node->next)
7005     {
7006       int is_dupl;
7007 
7008       compute_section_prefix (node->die);
7009       is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
7010 			&comdat_symbol_number);
7011       assign_symbol_names (node->die);
7012       if (is_dupl)
7013 	*pnode = node->next;
7014       else
7015 	{
7016 	  pnode = &node->next;
7017 	  record_comdat_symbol_number (node->die, &cu_hash_table,
7018 		comdat_symbol_number);
7019 	}
7020     }
7021 }
7022 
7023 /* Return non-zero if this DIE is a declaration.  */
7024 
7025 static int
7026 is_declaration_die (dw_die_ref die)
7027 {
7028   dw_attr_ref a;
7029   unsigned ix;
7030 
7031   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7032     if (a->dw_attr == DW_AT_declaration)
7033       return 1;
7034 
7035   return 0;
7036 }
7037 
7038 /* Return non-zero if this DIE is nested inside a subprogram.  */
7039 
7040 static int
7041 is_nested_in_subprogram (dw_die_ref die)
7042 {
7043   dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7044 
7045   if (decl == NULL)
7046     decl = die;
7047   return local_scope_p (decl);
7048 }
7049 
7050 /* Return non-zero if this DIE contains a defining declaration of a
7051    subprogram.  */
7052 
7053 static int
7054 contains_subprogram_definition (dw_die_ref die)
7055 {
7056   dw_die_ref c;
7057 
7058   if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7059     return 1;
7060   FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7061   return 0;
7062 }
7063 
7064 /* Return non-zero if this is a type DIE that should be moved to a
7065    COMDAT .debug_types section.  */
7066 
7067 static int
7068 should_move_die_to_comdat (dw_die_ref die)
7069 {
7070   switch (die->die_tag)
7071     {
7072     case DW_TAG_class_type:
7073     case DW_TAG_structure_type:
7074     case DW_TAG_enumeration_type:
7075     case DW_TAG_union_type:
7076       /* Don't move declarations, inlined instances, types nested in a
7077 	 subprogram, or types that contain subprogram definitions.  */
7078       if (is_declaration_die (die)
7079           || get_AT (die, DW_AT_abstract_origin)
7080           || is_nested_in_subprogram (die)
7081           || contains_subprogram_definition (die))
7082         return 0;
7083       return 1;
7084     case DW_TAG_array_type:
7085     case DW_TAG_interface_type:
7086     case DW_TAG_pointer_type:
7087     case DW_TAG_reference_type:
7088     case DW_TAG_rvalue_reference_type:
7089     case DW_TAG_string_type:
7090     case DW_TAG_subroutine_type:
7091     case DW_TAG_ptr_to_member_type:
7092     case DW_TAG_set_type:
7093     case DW_TAG_subrange_type:
7094     case DW_TAG_base_type:
7095     case DW_TAG_const_type:
7096     case DW_TAG_file_type:
7097     case DW_TAG_packed_type:
7098     case DW_TAG_volatile_type:
7099     case DW_TAG_typedef:
7100     default:
7101       return 0;
7102     }
7103 }
7104 
7105 /* Make a clone of DIE.  */
7106 
7107 static dw_die_ref
7108 clone_die (dw_die_ref die)
7109 {
7110   dw_die_ref clone;
7111   dw_attr_ref a;
7112   unsigned ix;
7113 
7114   clone = ggc_cleared_alloc<die_node> ();
7115   clone->die_tag = die->die_tag;
7116 
7117   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7118     add_dwarf_attr (clone, a);
7119 
7120   return clone;
7121 }
7122 
7123 /* Make a clone of the tree rooted at DIE.  */
7124 
7125 static dw_die_ref
7126 clone_tree (dw_die_ref die)
7127 {
7128   dw_die_ref c;
7129   dw_die_ref clone = clone_die (die);
7130 
7131   FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7132 
7133   return clone;
7134 }
7135 
7136 /* Make a clone of DIE as a declaration.  */
7137 
7138 static dw_die_ref
7139 clone_as_declaration (dw_die_ref die)
7140 {
7141   dw_die_ref clone;
7142   dw_die_ref decl;
7143   dw_attr_ref a;
7144   unsigned ix;
7145 
7146   /* If the DIE is already a declaration, just clone it.  */
7147   if (is_declaration_die (die))
7148     return clone_die (die);
7149 
7150   /* If the DIE is a specification, just clone its declaration DIE.  */
7151   decl = get_AT_ref (die, DW_AT_specification);
7152   if (decl != NULL)
7153     {
7154       clone = clone_die (decl);
7155       if (die->comdat_type_p)
7156 	add_AT_die_ref (clone, DW_AT_signature, die);
7157       return clone;
7158     }
7159 
7160   clone = ggc_cleared_alloc<die_node> ();
7161   clone->die_tag = die->die_tag;
7162 
7163   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7164     {
7165       /* We don't want to copy over all attributes.
7166          For example we don't want DW_AT_byte_size because otherwise we will no
7167          longer have a declaration and GDB will treat it as a definition.  */
7168 
7169       switch (a->dw_attr)
7170         {
7171         case DW_AT_abstract_origin:
7172         case DW_AT_artificial:
7173         case DW_AT_containing_type:
7174         case DW_AT_external:
7175         case DW_AT_name:
7176         case DW_AT_type:
7177         case DW_AT_virtuality:
7178         case DW_AT_linkage_name:
7179         case DW_AT_MIPS_linkage_name:
7180           add_dwarf_attr (clone, a);
7181           break;
7182         case DW_AT_byte_size:
7183         default:
7184           break;
7185         }
7186     }
7187 
7188   if (die->comdat_type_p)
7189     add_AT_die_ref (clone, DW_AT_signature, die);
7190 
7191   add_AT_flag (clone, DW_AT_declaration, 1);
7192   return clone;
7193 }
7194 
7195 
7196 /* Structure to map a DIE in one CU to its copy in a comdat type unit.  */
7197 
7198 struct decl_table_entry
7199 {
7200   dw_die_ref orig;
7201   dw_die_ref copy;
7202 };
7203 
7204 /* Helpers to manipulate hash table of copied declarations.  */
7205 
7206 /* Hashtable helpers.  */
7207 
7208 struct decl_table_entry_hasher : typed_free_remove <decl_table_entry>
7209 {
7210   typedef decl_table_entry value_type;
7211   typedef die_struct compare_type;
7212   static inline hashval_t hash (const value_type *);
7213   static inline bool equal (const value_type *, const compare_type *);
7214 };
7215 
7216 inline hashval_t
7217 decl_table_entry_hasher::hash (const value_type *entry)
7218 {
7219   return htab_hash_pointer (entry->orig);
7220 }
7221 
7222 inline bool
7223 decl_table_entry_hasher::equal (const value_type *entry1,
7224 				const compare_type *entry2)
7225 {
7226   return entry1->orig == entry2;
7227 }
7228 
7229 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7230 
7231 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7232    or type unit entry, to a new tree.  Adds the new tree to UNIT and returns
7233    a pointer to the copy of DIE.  If DECL_TABLE is provided, it is used
7234    to check if the ancestor has already been copied into UNIT.  */
7235 
7236 static dw_die_ref
7237 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7238 		    decl_hash_type *decl_table)
7239 {
7240   dw_die_ref parent = die->die_parent;
7241   dw_die_ref new_parent = unit;
7242   dw_die_ref copy;
7243   decl_table_entry **slot = NULL;
7244   struct decl_table_entry *entry = NULL;
7245 
7246   if (decl_table)
7247     {
7248       /* Check if the entry has already been copied to UNIT.  */
7249       slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7250 					      INSERT);
7251       if (*slot != HTAB_EMPTY_ENTRY)
7252         {
7253           entry = *slot;
7254           return entry->copy;
7255         }
7256 
7257       /* Record in DECL_TABLE that DIE has been copied to UNIT.  */
7258       entry = XCNEW (struct decl_table_entry);
7259       entry->orig = die;
7260       entry->copy = NULL;
7261       *slot = entry;
7262     }
7263 
7264   if (parent != NULL)
7265     {
7266       dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7267       if (spec != NULL)
7268         parent = spec;
7269       if (!is_unit_die (parent))
7270         new_parent = copy_ancestor_tree (unit, parent, decl_table);
7271     }
7272 
7273   copy = clone_as_declaration (die);
7274   add_child_die (new_parent, copy);
7275 
7276   if (decl_table)
7277     {
7278       /* Record the pointer to the copy.  */
7279       entry->copy = copy;
7280     }
7281 
7282   return copy;
7283 }
7284 /* Copy the declaration context to the new type unit DIE.  This includes
7285    any surrounding namespace or type declarations.  If the DIE has an
7286    AT_specification attribute, it also includes attributes and children
7287    attached to the specification, and returns a pointer to the original
7288    parent of the declaration DIE.  Returns NULL otherwise.  */
7289 
7290 static dw_die_ref
7291 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7292 {
7293   dw_die_ref decl;
7294   dw_die_ref new_decl;
7295   dw_die_ref orig_parent = NULL;
7296 
7297   decl = get_AT_ref (die, DW_AT_specification);
7298   if (decl == NULL)
7299     decl = die;
7300   else
7301     {
7302       unsigned ix;
7303       dw_die_ref c;
7304       dw_attr_ref a;
7305 
7306       /* The original DIE will be changed to a declaration, and must
7307          be moved to be a child of the original declaration DIE.  */
7308       orig_parent = decl->die_parent;
7309 
7310       /* Copy the type node pointer from the new DIE to the original
7311          declaration DIE so we can forward references later.  */
7312       decl->comdat_type_p = true;
7313       decl->die_id.die_type_node = die->die_id.die_type_node;
7314 
7315       remove_AT (die, DW_AT_specification);
7316 
7317       FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7318         {
7319           if (a->dw_attr != DW_AT_name
7320               && a->dw_attr != DW_AT_declaration
7321               && a->dw_attr != DW_AT_external)
7322             add_dwarf_attr (die, a);
7323         }
7324 
7325       FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7326     }
7327 
7328   if (decl->die_parent != NULL
7329       && !is_unit_die (decl->die_parent))
7330     {
7331       new_decl = copy_ancestor_tree (unit, decl, NULL);
7332       if (new_decl != NULL)
7333         {
7334           remove_AT (new_decl, DW_AT_signature);
7335           add_AT_specification (die, new_decl);
7336         }
7337     }
7338 
7339   return orig_parent;
7340 }
7341 
7342 /* Generate the skeleton ancestor tree for the given NODE, then clone
7343    the DIE and add the clone into the tree.  */
7344 
7345 static void
7346 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7347 {
7348   if (node->new_die != NULL)
7349     return;
7350 
7351   node->new_die = clone_as_declaration (node->old_die);
7352 
7353   if (node->parent != NULL)
7354     {
7355       generate_skeleton_ancestor_tree (node->parent);
7356       add_child_die (node->parent->new_die, node->new_die);
7357     }
7358 }
7359 
7360 /* Generate a skeleton tree of DIEs containing any declarations that are
7361    found in the original tree.  We traverse the tree looking for declaration
7362    DIEs, and construct the skeleton from the bottom up whenever we find one.  */
7363 
7364 static void
7365 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7366 {
7367   skeleton_chain_node node;
7368   dw_die_ref c;
7369   dw_die_ref first;
7370   dw_die_ref prev = NULL;
7371   dw_die_ref next = NULL;
7372 
7373   node.parent = parent;
7374 
7375   first = c = parent->old_die->die_child;
7376   if (c)
7377     next = c->die_sib;
7378   if (c) do {
7379     if (prev == NULL || prev->die_sib == c)
7380       prev = c;
7381     c = next;
7382     next = (c == first ? NULL : c->die_sib);
7383     node.old_die = c;
7384     node.new_die = NULL;
7385     if (is_declaration_die (c))
7386       {
7387 	if (is_template_instantiation (c))
7388 	  {
7389 	    /* Instantiated templates do not need to be cloned into the
7390 	       type unit.  Just move the DIE and its children back to
7391 	       the skeleton tree (in the main CU).  */
7392 	    remove_child_with_prev (c, prev);
7393 	    add_child_die (parent->new_die, c);
7394 	    c = prev;
7395 	  }
7396 	else
7397 	  {
7398 	    /* Clone the existing DIE, move the original to the skeleton
7399 	       tree (which is in the main CU), and put the clone, with
7400 	       all the original's children, where the original came from
7401 	       (which is about to be moved to the type unit).  */
7402 	    dw_die_ref clone = clone_die (c);
7403 	    move_all_children (c, clone);
7404 
7405 	    /* If the original has a DW_AT_object_pointer attribute,
7406 	       it would now point to a child DIE just moved to the
7407 	       cloned tree, so we need to remove that attribute from
7408 	       the original.  */
7409 	    remove_AT (c, DW_AT_object_pointer);
7410 
7411 	    replace_child (c, clone, prev);
7412 	    generate_skeleton_ancestor_tree (parent);
7413 	    add_child_die (parent->new_die, c);
7414 	    node.new_die = c;
7415 	    c = clone;
7416 	  }
7417       }
7418     generate_skeleton_bottom_up (&node);
7419   } while (next != NULL);
7420 }
7421 
7422 /* Wrapper function for generate_skeleton_bottom_up.  */
7423 
7424 static dw_die_ref
7425 generate_skeleton (dw_die_ref die)
7426 {
7427   skeleton_chain_node node;
7428 
7429   node.old_die = die;
7430   node.new_die = NULL;
7431   node.parent = NULL;
7432 
7433   /* If this type definition is nested inside another type,
7434      and is not an instantiation of a template, always leave
7435      at least a declaration in its place.  */
7436   if (die->die_parent != NULL
7437       && is_type_die (die->die_parent)
7438       && !is_template_instantiation (die))
7439     node.new_die = clone_as_declaration (die);
7440 
7441   generate_skeleton_bottom_up (&node);
7442   return node.new_die;
7443 }
7444 
7445 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7446    declaration.  The original DIE is moved to a new compile unit so that
7447    existing references to it follow it to the new location.  If any of the
7448    original DIE's descendants is a declaration, we need to replace the
7449    original DIE with a skeleton tree and move the declarations back into the
7450    skeleton tree.  */
7451 
7452 static dw_die_ref
7453 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7454 				       dw_die_ref prev)
7455 {
7456   dw_die_ref skeleton, orig_parent;
7457 
7458   /* Copy the declaration context to the type unit DIE.  If the returned
7459      ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7460      that DIE.  */
7461   orig_parent = copy_declaration_context (unit, child);
7462 
7463   skeleton = generate_skeleton (child);
7464   if (skeleton == NULL)
7465     remove_child_with_prev (child, prev);
7466   else
7467     {
7468       skeleton->comdat_type_p = true;
7469       skeleton->die_id.die_type_node = child->die_id.die_type_node;
7470 
7471       /* If the original DIE was a specification, we need to put
7472          the skeleton under the parent DIE of the declaration.
7473 	 This leaves the original declaration in the tree, but
7474 	 it will be pruned later since there are no longer any
7475 	 references to it.  */
7476       if (orig_parent != NULL)
7477 	{
7478 	  remove_child_with_prev (child, prev);
7479 	  add_child_die (orig_parent, skeleton);
7480 	}
7481       else
7482 	replace_child (child, skeleton, prev);
7483     }
7484 
7485   return skeleton;
7486 }
7487 
7488 /* Traverse the DIE and set up additional .debug_types sections for each
7489    type worthy of being placed in a COMDAT section.  */
7490 
7491 static void
7492 break_out_comdat_types (dw_die_ref die)
7493 {
7494   dw_die_ref c;
7495   dw_die_ref first;
7496   dw_die_ref prev = NULL;
7497   dw_die_ref next = NULL;
7498   dw_die_ref unit = NULL;
7499 
7500   first = c = die->die_child;
7501   if (c)
7502     next = c->die_sib;
7503   if (c) do {
7504     if (prev == NULL || prev->die_sib == c)
7505       prev = c;
7506     c = next;
7507     next = (c == first ? NULL : c->die_sib);
7508     if (should_move_die_to_comdat (c))
7509       {
7510         dw_die_ref replacement;
7511 	comdat_type_node_ref type_node;
7512 
7513         /* Break out nested types into their own type units.  */
7514         break_out_comdat_types (c);
7515 
7516         /* Create a new type unit DIE as the root for the new tree, and
7517            add it to the list of comdat types.  */
7518         unit = new_die (DW_TAG_type_unit, NULL, NULL);
7519         add_AT_unsigned (unit, DW_AT_language,
7520                          get_AT_unsigned (comp_unit_die (), DW_AT_language));
7521         type_node = ggc_cleared_alloc<comdat_type_node> ();
7522         type_node->root_die = unit;
7523         type_node->next = comdat_type_list;
7524         comdat_type_list = type_node;
7525 
7526         /* Generate the type signature.  */
7527         generate_type_signature (c, type_node);
7528 
7529         /* Copy the declaration context, attributes, and children of the
7530            declaration into the new type unit DIE, then remove this DIE
7531 	   from the main CU (or replace it with a skeleton if necessary).  */
7532 	replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7533 	type_node->skeleton_die = replacement;
7534 
7535         /* Add the DIE to the new compunit.  */
7536 	add_child_die (unit, c);
7537 
7538         if (replacement != NULL)
7539           c = replacement;
7540       }
7541     else if (c->die_tag == DW_TAG_namespace
7542              || c->die_tag == DW_TAG_class_type
7543              || c->die_tag == DW_TAG_structure_type
7544              || c->die_tag == DW_TAG_union_type)
7545       {
7546         /* Look for nested types that can be broken out.  */
7547         break_out_comdat_types (c);
7548       }
7549   } while (next != NULL);
7550 }
7551 
7552 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
7553    Enter all the cloned children into the hash table decl_table.  */
7554 
7555 static dw_die_ref
7556 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
7557 {
7558   dw_die_ref c;
7559   dw_die_ref clone;
7560   struct decl_table_entry *entry;
7561   decl_table_entry **slot;
7562 
7563   if (die->die_tag == DW_TAG_subprogram)
7564     clone = clone_as_declaration (die);
7565   else
7566     clone = clone_die (die);
7567 
7568   slot = decl_table->find_slot_with_hash (die,
7569 					  htab_hash_pointer (die), INSERT);
7570 
7571   /* Assert that DIE isn't in the hash table yet.  If it would be there
7572      before, the ancestors would be necessarily there as well, therefore
7573      clone_tree_partial wouldn't be called.  */
7574   gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7575 
7576   entry = XCNEW (struct decl_table_entry);
7577   entry->orig = die;
7578   entry->copy = clone;
7579   *slot = entry;
7580 
7581   if (die->die_tag != DW_TAG_subprogram)
7582     FOR_EACH_CHILD (die, c,
7583 		    add_child_die (clone, clone_tree_partial (c, decl_table)));
7584 
7585   return clone;
7586 }
7587 
7588 /* Walk the DIE and its children, looking for references to incomplete
7589    or trivial types that are unmarked (i.e., that are not in the current
7590    type_unit).  */
7591 
7592 static void
7593 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
7594 {
7595   dw_die_ref c;
7596   dw_attr_ref a;
7597   unsigned ix;
7598 
7599   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7600     {
7601       if (AT_class (a) == dw_val_class_die_ref)
7602         {
7603           dw_die_ref targ = AT_ref (a);
7604           decl_table_entry **slot;
7605           struct decl_table_entry *entry;
7606 
7607           if (targ->die_mark != 0 || targ->comdat_type_p)
7608             continue;
7609 
7610           slot = decl_table->find_slot_with_hash (targ,
7611 						  htab_hash_pointer (targ),
7612 						  INSERT);
7613 
7614           if (*slot != HTAB_EMPTY_ENTRY)
7615             {
7616               /* TARG has already been copied, so we just need to
7617                  modify the reference to point to the copy.  */
7618               entry = *slot;
7619               a->dw_attr_val.v.val_die_ref.die = entry->copy;
7620             }
7621           else
7622             {
7623               dw_die_ref parent = unit;
7624 	      dw_die_ref copy = clone_die (targ);
7625 
7626               /* Record in DECL_TABLE that TARG has been copied.
7627                  Need to do this now, before the recursive call,
7628                  because DECL_TABLE may be expanded and SLOT
7629                  would no longer be a valid pointer.  */
7630               entry = XCNEW (struct decl_table_entry);
7631               entry->orig = targ;
7632               entry->copy = copy;
7633               *slot = entry;
7634 
7635 	      /* If TARG is not a declaration DIE, we need to copy its
7636 	         children.  */
7637 	      if (!is_declaration_die (targ))
7638 		{
7639 		  FOR_EACH_CHILD (
7640 		      targ, c,
7641 		      add_child_die (copy,
7642 				     clone_tree_partial (c, decl_table)));
7643 		}
7644 
7645               /* Make sure the cloned tree is marked as part of the
7646                  type unit.  */
7647               mark_dies (copy);
7648 
7649               /* If TARG has surrounding context, copy its ancestor tree
7650                  into the new type unit.  */
7651               if (targ->die_parent != NULL
7652 		  && !is_unit_die (targ->die_parent))
7653                 parent = copy_ancestor_tree (unit, targ->die_parent,
7654                                              decl_table);
7655 
7656               add_child_die (parent, copy);
7657               a->dw_attr_val.v.val_die_ref.die = copy;
7658 
7659               /* Make sure the newly-copied DIE is walked.  If it was
7660                  installed in a previously-added context, it won't
7661                  get visited otherwise.  */
7662               if (parent != unit)
7663 		{
7664 		  /* Find the highest point of the newly-added tree,
7665 		     mark each node along the way, and walk from there.  */
7666 		  parent->die_mark = 1;
7667 		  while (parent->die_parent
7668 		  	 && parent->die_parent->die_mark == 0)
7669 		    {
7670 		      parent = parent->die_parent;
7671 		      parent->die_mark = 1;
7672 		    }
7673 		  copy_decls_walk (unit, parent, decl_table);
7674 		}
7675             }
7676         }
7677     }
7678 
7679   FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7680 }
7681 
7682 /* Copy declarations for "unworthy" types into the new comdat section.
7683    Incomplete types, modified types, and certain other types aren't broken
7684    out into comdat sections of their own, so they don't have a signature,
7685    and we need to copy the declaration into the same section so that we
7686    don't have an external reference.  */
7687 
7688 static void
7689 copy_decls_for_unworthy_types (dw_die_ref unit)
7690 {
7691   mark_dies (unit);
7692   decl_hash_type decl_table (10);
7693   copy_decls_walk (unit, unit, &decl_table);
7694   unmark_dies (unit);
7695 }
7696 
7697 /* Traverse the DIE and add a sibling attribute if it may have the
7698    effect of speeding up access to siblings.  To save some space,
7699    avoid generating sibling attributes for DIE's without children.  */
7700 
7701 static void
7702 add_sibling_attributes (dw_die_ref die)
7703 {
7704   dw_die_ref c;
7705 
7706   if (! die->die_child)
7707     return;
7708 
7709   if (die->die_parent && die != die->die_parent->die_child)
7710     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7711 
7712   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7713 }
7714 
7715 /* Output all location lists for the DIE and its children.  */
7716 
7717 static void
7718 output_location_lists (dw_die_ref die)
7719 {
7720   dw_die_ref c;
7721   dw_attr_ref a;
7722   unsigned ix;
7723 
7724   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7725     if (AT_class (a) == dw_val_class_loc_list)
7726       output_loc_list (AT_loc_list (a));
7727 
7728   FOR_EACH_CHILD (die, c, output_location_lists (c));
7729 }
7730 
7731 /* We want to limit the number of external references, because they are
7732    larger than local references: a relocation takes multiple words, and
7733    even a sig8 reference is always eight bytes, whereas a local reference
7734    can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7735    So if we encounter multiple external references to the same type DIE, we
7736    make a local typedef stub for it and redirect all references there.
7737 
7738    This is the element of the hash table for keeping track of these
7739    references.  */
7740 
7741 struct external_ref
7742 {
7743   dw_die_ref type;
7744   dw_die_ref stub;
7745   unsigned n_refs;
7746 };
7747 
7748 /* Hashtable helpers.  */
7749 
7750 struct external_ref_hasher : typed_free_remove <external_ref>
7751 {
7752   typedef external_ref value_type;
7753   typedef external_ref compare_type;
7754   static inline hashval_t hash (const value_type *);
7755   static inline bool equal (const value_type *, const compare_type *);
7756 };
7757 
7758 inline hashval_t
7759 external_ref_hasher::hash (const value_type *r)
7760 {
7761   dw_die_ref die = r->type;
7762   hashval_t h = 0;
7763 
7764   /* We can't use the address of the DIE for hashing, because
7765      that will make the order of the stub DIEs non-deterministic.  */
7766   if (! die->comdat_type_p)
7767     /* We have a symbol; use it to compute a hash.  */
7768     h = htab_hash_string (die->die_id.die_symbol);
7769   else
7770     {
7771       /* We have a type signature; use a subset of the bits as the hash.
7772 	 The 8-byte signature is at least as large as hashval_t.  */
7773       comdat_type_node_ref type_node = die->die_id.die_type_node;
7774       memcpy (&h, type_node->signature, sizeof (h));
7775     }
7776   return h;
7777 }
7778 
7779 inline bool
7780 external_ref_hasher::equal (const value_type *r1, const compare_type *r2)
7781 {
7782   return r1->type == r2->type;
7783 }
7784 
7785 typedef hash_table<external_ref_hasher> external_ref_hash_type;
7786 
7787 /* Return a pointer to the external_ref for references to DIE.  */
7788 
7789 static struct external_ref *
7790 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
7791 {
7792   struct external_ref ref, *ref_p;
7793   external_ref **slot;
7794 
7795   ref.type = die;
7796   slot = map->find_slot (&ref, INSERT);
7797   if (*slot != HTAB_EMPTY_ENTRY)
7798     return *slot;
7799 
7800   ref_p = XCNEW (struct external_ref);
7801   ref_p->type = die;
7802   *slot = ref_p;
7803   return ref_p;
7804 }
7805 
7806 /* Subroutine of optimize_external_refs, below.
7807 
7808    If we see a type skeleton, record it as our stub.  If we see external
7809    references, remember how many we've seen.  */
7810 
7811 static void
7812 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
7813 {
7814   dw_die_ref c;
7815   dw_attr_ref a;
7816   unsigned ix;
7817   struct external_ref *ref_p;
7818 
7819   if (is_type_die (die)
7820       && (c = get_AT_ref (die, DW_AT_signature)))
7821     {
7822       /* This is a local skeleton; use it for local references.  */
7823       ref_p = lookup_external_ref (map, c);
7824       ref_p->stub = die;
7825     }
7826 
7827   /* Scan the DIE references, and remember any that refer to DIEs from
7828      other CUs (i.e. those which are not marked).  */
7829   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7830     if (AT_class (a) == dw_val_class_die_ref
7831 	&& (c = AT_ref (a))->die_mark == 0
7832 	&& is_type_die (c))
7833       {
7834 	ref_p = lookup_external_ref (map, c);
7835 	ref_p->n_refs++;
7836       }
7837 
7838   FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7839 }
7840 
7841 /* htab_traverse callback function for optimize_external_refs, below.  SLOT
7842    points to an external_ref, DATA is the CU we're processing.  If we don't
7843    already have a local stub, and we have multiple refs, build a stub.  */
7844 
7845 int
7846 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
7847 {
7848   struct external_ref *ref_p = *slot;
7849 
7850   if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7851     {
7852       /* We have multiple references to this type, so build a small stub.
7853 	 Both of these forms are a bit dodgy from the perspective of the
7854 	 DWARF standard, since technically they should have names.  */
7855       dw_die_ref cu = data;
7856       dw_die_ref type = ref_p->type;
7857       dw_die_ref stub = NULL;
7858 
7859       if (type->comdat_type_p)
7860 	{
7861 	  /* If we refer to this type via sig8, use AT_signature.  */
7862 	  stub = new_die (type->die_tag, cu, NULL_TREE);
7863 	  add_AT_die_ref (stub, DW_AT_signature, type);
7864 	}
7865       else
7866 	{
7867 	  /* Otherwise, use a typedef with no name.  */
7868 	  stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7869 	  add_AT_die_ref (stub, DW_AT_type, type);
7870 	}
7871 
7872       stub->die_mark++;
7873       ref_p->stub = stub;
7874     }
7875   return 1;
7876 }
7877 
7878 /* DIE is a unit; look through all the DIE references to see if there are
7879    any external references to types, and if so, create local stubs for
7880    them which will be applied in build_abbrev_table.  This is useful because
7881    references to local DIEs are smaller.  */
7882 
7883 static external_ref_hash_type *
7884 optimize_external_refs (dw_die_ref die)
7885 {
7886   external_ref_hash_type *map = new external_ref_hash_type (10);
7887   optimize_external_refs_1 (die, map);
7888   map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
7889   return map;
7890 }
7891 
7892 /* The format of each DIE (and its attribute value pairs) is encoded in an
7893    abbreviation table.  This routine builds the abbreviation table and assigns
7894    a unique abbreviation id for each abbreviation entry.  The children of each
7895    die are visited recursively.  */
7896 
7897 static void
7898 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
7899 {
7900   unsigned long abbrev_id;
7901   unsigned int n_alloc;
7902   dw_die_ref c;
7903   dw_attr_ref a;
7904   unsigned ix;
7905 
7906   /* Scan the DIE references, and replace any that refer to
7907      DIEs from other CUs (i.e. those which are not marked) with
7908      the local stubs we built in optimize_external_refs.  */
7909   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7910     if (AT_class (a) == dw_val_class_die_ref
7911 	&& (c = AT_ref (a))->die_mark == 0)
7912       {
7913 	struct external_ref *ref_p;
7914 	gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
7915 
7916 	ref_p = lookup_external_ref (extern_map, c);
7917 	if (ref_p->stub && ref_p->stub != die)
7918 	  change_AT_die_ref (a, ref_p->stub);
7919 	else
7920 	  /* We aren't changing this reference, so mark it external.  */
7921 	  set_AT_ref_external (a, 1);
7922       }
7923 
7924   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7925     {
7926       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7927       dw_attr_ref die_a, abbrev_a;
7928       unsigned ix;
7929       bool ok = true;
7930 
7931       if (abbrev->die_tag != die->die_tag)
7932 	continue;
7933       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7934 	continue;
7935 
7936       if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
7937 	continue;
7938 
7939       FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
7940 	{
7941 	  abbrev_a = &(*abbrev->die_attr)[ix];
7942 	  if ((abbrev_a->dw_attr != die_a->dw_attr)
7943 	      || (value_format (abbrev_a) != value_format (die_a)))
7944 	    {
7945 	      ok = false;
7946 	      break;
7947 	    }
7948 	}
7949       if (ok)
7950 	break;
7951     }
7952 
7953   if (abbrev_id >= abbrev_die_table_in_use)
7954     {
7955       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7956 	{
7957 	  n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7958 	  abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7959 					    n_alloc);
7960 
7961 	  memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7962 		 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7963 	  abbrev_die_table_allocated = n_alloc;
7964 	}
7965 
7966       ++abbrev_die_table_in_use;
7967       abbrev_die_table[abbrev_id] = die;
7968     }
7969 
7970   die->die_abbrev = abbrev_id;
7971   FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
7972 }
7973 
7974 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
7975 
7976 static int
7977 constant_size (unsigned HOST_WIDE_INT value)
7978 {
7979   int log;
7980 
7981   if (value == 0)
7982     log = 0;
7983   else
7984     log = floor_log2 (value);
7985 
7986   log = log / 8;
7987   log = 1 << (floor_log2 (log) + 1);
7988 
7989   return log;
7990 }
7991 
7992 /* Return the size of a DIE as it is represented in the
7993    .debug_info section.  */
7994 
7995 static unsigned long
7996 size_of_die (dw_die_ref die)
7997 {
7998   unsigned long size = 0;
7999   dw_attr_ref a;
8000   unsigned ix;
8001   enum dwarf_form form;
8002 
8003   size += size_of_uleb128 (die->die_abbrev);
8004   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8005     {
8006       switch (AT_class (a))
8007 	{
8008 	case dw_val_class_addr:
8009           if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8010             {
8011               gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8012               size += size_of_uleb128 (AT_index (a));
8013             }
8014           else
8015             size += DWARF2_ADDR_SIZE;
8016 	  break;
8017 	case dw_val_class_offset:
8018 	  size += DWARF_OFFSET_SIZE;
8019 	  break;
8020 	case dw_val_class_loc:
8021 	  {
8022 	    unsigned long lsize = size_of_locs (AT_loc (a));
8023 
8024 	    /* Block length.  */
8025 	    if (dwarf_version >= 4)
8026 	      size += size_of_uleb128 (lsize);
8027 	    else
8028 	      size += constant_size (lsize);
8029 	    size += lsize;
8030 	  }
8031 	  break;
8032 	case dw_val_class_loc_list:
8033           if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8034             {
8035               gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8036               size += size_of_uleb128 (AT_index (a));
8037             }
8038           else
8039             size += DWARF_OFFSET_SIZE;
8040 	  break;
8041 	case dw_val_class_range_list:
8042           size += DWARF_OFFSET_SIZE;
8043 	  break;
8044 	case dw_val_class_const:
8045 	  size += size_of_sleb128 (AT_int (a));
8046 	  break;
8047 	case dw_val_class_unsigned_const:
8048 	  {
8049 	    int csize = constant_size (AT_unsigned (a));
8050 	    if (dwarf_version == 3
8051 		&& a->dw_attr == DW_AT_data_member_location
8052 		&& csize >= 4)
8053 	      size += size_of_uleb128 (AT_unsigned (a));
8054 	    else
8055 	      size += csize;
8056 	  }
8057 	  break;
8058 	case dw_val_class_const_double:
8059 	  size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
8060 	  if (HOST_BITS_PER_WIDE_INT >= 64)
8061 	    size++; /* block */
8062 	  break;
8063 	case dw_val_class_wide_int:
8064 	  size += (get_full_len (*a->dw_attr_val.v.val_wide)
8065 		   * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
8066 	  if (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT
8067 	      > 64)
8068 	    size++; /* block */
8069 	  break;
8070 	case dw_val_class_vec:
8071 	  size += constant_size (a->dw_attr_val.v.val_vec.length
8072 				 * a->dw_attr_val.v.val_vec.elt_size)
8073 		  + a->dw_attr_val.v.val_vec.length
8074 		    * a->dw_attr_val.v.val_vec.elt_size; /* block */
8075 	  break;
8076 	case dw_val_class_flag:
8077 	  if (dwarf_version >= 4)
8078 	    /* Currently all add_AT_flag calls pass in 1 as last argument,
8079 	       so DW_FORM_flag_present can be used.  If that ever changes,
8080 	       we'll need to use DW_FORM_flag and have some optimization
8081 	       in build_abbrev_table that will change those to
8082 	       DW_FORM_flag_present if it is set to 1 in all DIEs using
8083 	       the same abbrev entry.  */
8084 	    gcc_assert (a->dw_attr_val.v.val_flag == 1);
8085 	  else
8086 	    size += 1;
8087 	  break;
8088 	case dw_val_class_die_ref:
8089 	  if (AT_ref_external (a))
8090 	    {
8091 	      /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
8092 		 we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
8093 		 is sized by target address length, whereas in DWARF3
8094 		 it's always sized as an offset.  */
8095 	      if (use_debug_types)
8096 		size += DWARF_TYPE_SIGNATURE_SIZE;
8097 	      else if (dwarf_version == 2)
8098 		size += DWARF2_ADDR_SIZE;
8099 	      else
8100 		size += DWARF_OFFSET_SIZE;
8101 	    }
8102 	  else
8103 	    size += DWARF_OFFSET_SIZE;
8104 	  break;
8105 	case dw_val_class_fde_ref:
8106 	  size += DWARF_OFFSET_SIZE;
8107 	  break;
8108 	case dw_val_class_lbl_id:
8109           if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8110             {
8111               gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8112               size += size_of_uleb128 (AT_index (a));
8113             }
8114           else
8115             size += DWARF2_ADDR_SIZE;
8116 	  break;
8117 	case dw_val_class_lineptr:
8118 	case dw_val_class_macptr:
8119           size += DWARF_OFFSET_SIZE;
8120 	  break;
8121 	case dw_val_class_str:
8122           form = AT_string_form (a);
8123           if (form == DW_FORM_strp)
8124 	    size += DWARF_OFFSET_SIZE;
8125          else if (form == DW_FORM_GNU_str_index)
8126             size += size_of_uleb128 (AT_index (a));
8127 	  else
8128 	    size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8129 	  break;
8130 	case dw_val_class_file:
8131 	  size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8132 	  break;
8133 	case dw_val_class_data8:
8134 	  size += 8;
8135 	  break;
8136 	case dw_val_class_vms_delta:
8137 	  size += DWARF_OFFSET_SIZE;
8138 	  break;
8139 	case dw_val_class_high_pc:
8140 	  size += DWARF2_ADDR_SIZE;
8141 	  break;
8142 	default:
8143 	  gcc_unreachable ();
8144 	}
8145     }
8146 
8147   return size;
8148 }
8149 
8150 /* Size the debugging information associated with a given DIE.  Visits the
8151    DIE's children recursively.  Updates the global variable next_die_offset, on
8152    each time through.  Uses the current value of next_die_offset to update the
8153    die_offset field in each DIE.  */
8154 
8155 static void
8156 calc_die_sizes (dw_die_ref die)
8157 {
8158   dw_die_ref c;
8159 
8160   gcc_assert (die->die_offset == 0
8161 	      || (unsigned long int) die->die_offset == next_die_offset);
8162   die->die_offset = next_die_offset;
8163   next_die_offset += size_of_die (die);
8164 
8165   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8166 
8167   if (die->die_child != NULL)
8168     /* Count the null byte used to terminate sibling lists.  */
8169     next_die_offset += 1;
8170 }
8171 
8172 /* Size just the base type children at the start of the CU.
8173    This is needed because build_abbrev needs to size locs
8174    and sizing of type based stack ops needs to know die_offset
8175    values for the base types.  */
8176 
8177 static void
8178 calc_base_type_die_sizes (void)
8179 {
8180   unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8181   unsigned int i;
8182   dw_die_ref base_type;
8183 #if ENABLE_ASSERT_CHECKING
8184   dw_die_ref prev = comp_unit_die ()->die_child;
8185 #endif
8186 
8187   die_offset += size_of_die (comp_unit_die ());
8188   for (i = 0; base_types.iterate (i, &base_type); i++)
8189     {
8190 #if ENABLE_ASSERT_CHECKING
8191       gcc_assert (base_type->die_offset == 0
8192 		  && prev->die_sib == base_type
8193 		  && base_type->die_child == NULL
8194 		  && base_type->die_abbrev);
8195       prev = base_type;
8196 #endif
8197       base_type->die_offset = die_offset;
8198       die_offset += size_of_die (base_type);
8199     }
8200 }
8201 
8202 /* Set the marks for a die and its children.  We do this so
8203    that we know whether or not a reference needs to use FORM_ref_addr; only
8204    DIEs in the same CU will be marked.  We used to clear out the offset
8205    and use that as the flag, but ran into ordering problems.  */
8206 
8207 static void
8208 mark_dies (dw_die_ref die)
8209 {
8210   dw_die_ref c;
8211 
8212   gcc_assert (!die->die_mark);
8213 
8214   die->die_mark = 1;
8215   FOR_EACH_CHILD (die, c, mark_dies (c));
8216 }
8217 
8218 /* Clear the marks for a die and its children.  */
8219 
8220 static void
8221 unmark_dies (dw_die_ref die)
8222 {
8223   dw_die_ref c;
8224 
8225   if (! use_debug_types)
8226     gcc_assert (die->die_mark);
8227 
8228   die->die_mark = 0;
8229   FOR_EACH_CHILD (die, c, unmark_dies (c));
8230 }
8231 
8232 /* Clear the marks for a die, its children and referred dies.  */
8233 
8234 static void
8235 unmark_all_dies (dw_die_ref die)
8236 {
8237   dw_die_ref c;
8238   dw_attr_ref a;
8239   unsigned ix;
8240 
8241   if (!die->die_mark)
8242     return;
8243   die->die_mark = 0;
8244 
8245   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8246 
8247   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8248     if (AT_class (a) == dw_val_class_die_ref)
8249       unmark_all_dies (AT_ref (a));
8250 }
8251 
8252 /* Calculate if the entry should appear in the final output file.  It may be
8253    from a pruned a type.  */
8254 
8255 static bool
8256 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
8257 {
8258   /* By limiting gnu pubnames to definitions only, gold can generate a
8259      gdb index without entries for declarations, which don't include
8260      enough information to be useful.  */
8261   if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
8262     return false;
8263 
8264   if (table == pubname_table)
8265     {
8266       /* Enumerator names are part of the pubname table, but the
8267          parent DW_TAG_enumeration_type die may have been pruned.
8268          Don't output them if that is the case.  */
8269       if (p->die->die_tag == DW_TAG_enumerator &&
8270           (p->die->die_parent == NULL
8271            || !p->die->die_parent->die_perennial_p))
8272         return false;
8273 
8274       /* Everything else in the pubname table is included.  */
8275       return true;
8276     }
8277 
8278   /* The pubtypes table shouldn't include types that have been
8279      pruned.  */
8280   return (p->die->die_offset != 0
8281           || !flag_eliminate_unused_debug_types);
8282 }
8283 
8284 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8285    generated for the compilation unit.  */
8286 
8287 static unsigned long
8288 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8289 {
8290   unsigned long size;
8291   unsigned i;
8292   pubname_ref p;
8293   int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8294 
8295   size = DWARF_PUBNAMES_HEADER_SIZE;
8296   FOR_EACH_VEC_ELT (*names, i, p)
8297     if (include_pubname_in_output (names, p))
8298       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8299 
8300   size += DWARF_OFFSET_SIZE;
8301   return size;
8302 }
8303 
8304 /* Return the size of the information in the .debug_aranges section.  */
8305 
8306 static unsigned long
8307 size_of_aranges (void)
8308 {
8309   unsigned long size;
8310 
8311   size = DWARF_ARANGES_HEADER_SIZE;
8312 
8313   /* Count the address/length pair for this compilation unit.  */
8314   if (text_section_used)
8315     size += 2 * DWARF2_ADDR_SIZE;
8316   if (cold_text_section_used)
8317     size += 2 * DWARF2_ADDR_SIZE;
8318   if (have_multiple_function_sections)
8319     {
8320       unsigned fde_idx;
8321       dw_fde_ref fde;
8322 
8323       FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8324 	{
8325 	  if (DECL_IGNORED_P (fde->decl))
8326 	    continue;
8327 	  if (!fde->in_std_section)
8328 	    size += 2 * DWARF2_ADDR_SIZE;
8329 	  if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8330 	    size += 2 * DWARF2_ADDR_SIZE;
8331 	}
8332     }
8333 
8334   /* Count the two zero words used to terminated the address range table.  */
8335   size += 2 * DWARF2_ADDR_SIZE;
8336   return size;
8337 }
8338 
8339 /* Select the encoding of an attribute value.  */
8340 
8341 static enum dwarf_form
8342 value_format (dw_attr_ref a)
8343 {
8344   switch (AT_class (a))
8345     {
8346     case dw_val_class_addr:
8347       /* Only very few attributes allow DW_FORM_addr.  */
8348       switch (a->dw_attr)
8349 	{
8350 	case DW_AT_low_pc:
8351 	case DW_AT_high_pc:
8352 	case DW_AT_entry_pc:
8353 	case DW_AT_trampoline:
8354           return (AT_index (a) == NOT_INDEXED
8355                   ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8356 	default:
8357 	  break;
8358 	}
8359       switch (DWARF2_ADDR_SIZE)
8360 	{
8361 	case 1:
8362 	  return DW_FORM_data1;
8363 	case 2:
8364 	  return DW_FORM_data2;
8365 	case 4:
8366 	  return DW_FORM_data4;
8367 	case 8:
8368 	  return DW_FORM_data8;
8369 	default:
8370 	  gcc_unreachable ();
8371 	}
8372     case dw_val_class_range_list:
8373     case dw_val_class_loc_list:
8374       if (dwarf_version >= 4)
8375 	return DW_FORM_sec_offset;
8376       /* FALLTHRU */
8377     case dw_val_class_vms_delta:
8378     case dw_val_class_offset:
8379       switch (DWARF_OFFSET_SIZE)
8380 	{
8381 	case 4:
8382 	  return DW_FORM_data4;
8383 	case 8:
8384 	  return DW_FORM_data8;
8385 	default:
8386 	  gcc_unreachable ();
8387 	}
8388     case dw_val_class_loc:
8389       if (dwarf_version >= 4)
8390 	return DW_FORM_exprloc;
8391       switch (constant_size (size_of_locs (AT_loc (a))))
8392 	{
8393 	case 1:
8394 	  return DW_FORM_block1;
8395 	case 2:
8396 	  return DW_FORM_block2;
8397 	case 4:
8398 	  return DW_FORM_block4;
8399 	default:
8400 	  gcc_unreachable ();
8401 	}
8402     case dw_val_class_const:
8403       return DW_FORM_sdata;
8404     case dw_val_class_unsigned_const:
8405       switch (constant_size (AT_unsigned (a)))
8406 	{
8407 	case 1:
8408 	  return DW_FORM_data1;
8409 	case 2:
8410 	  return DW_FORM_data2;
8411 	case 4:
8412 	  /* In DWARF3 DW_AT_data_member_location with
8413 	     DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8414 	     constant, so we need to use DW_FORM_udata if we need
8415 	     a large constant.  */
8416 	  if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8417 	    return DW_FORM_udata;
8418 	  return DW_FORM_data4;
8419 	case 8:
8420 	  if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8421 	    return DW_FORM_udata;
8422 	  return DW_FORM_data8;
8423 	default:
8424 	  gcc_unreachable ();
8425 	}
8426     case dw_val_class_const_double:
8427       switch (HOST_BITS_PER_WIDE_INT)
8428 	{
8429 	case 8:
8430 	  return DW_FORM_data2;
8431 	case 16:
8432 	  return DW_FORM_data4;
8433 	case 32:
8434 	  return DW_FORM_data8;
8435 	case 64:
8436 	default:
8437 	  return DW_FORM_block1;
8438 	}
8439     case dw_val_class_wide_int:
8440       switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
8441 	{
8442 	case 8:
8443 	  return DW_FORM_data1;
8444 	case 16:
8445 	  return DW_FORM_data2;
8446 	case 32:
8447 	  return DW_FORM_data4;
8448 	case 64:
8449 	  return DW_FORM_data8;
8450 	default:
8451 	  return DW_FORM_block1;
8452 	}
8453     case dw_val_class_vec:
8454       switch (constant_size (a->dw_attr_val.v.val_vec.length
8455 			     * a->dw_attr_val.v.val_vec.elt_size))
8456 	{
8457 	case 1:
8458 	  return DW_FORM_block1;
8459 	case 2:
8460 	  return DW_FORM_block2;
8461 	case 4:
8462 	  return DW_FORM_block4;
8463 	default:
8464 	  gcc_unreachable ();
8465 	}
8466     case dw_val_class_flag:
8467       if (dwarf_version >= 4)
8468 	{
8469 	  /* Currently all add_AT_flag calls pass in 1 as last argument,
8470 	     so DW_FORM_flag_present can be used.  If that ever changes,
8471 	     we'll need to use DW_FORM_flag and have some optimization
8472 	     in build_abbrev_table that will change those to
8473 	     DW_FORM_flag_present if it is set to 1 in all DIEs using
8474 	     the same abbrev entry.  */
8475 	  gcc_assert (a->dw_attr_val.v.val_flag == 1);
8476 	  return DW_FORM_flag_present;
8477 	}
8478       return DW_FORM_flag;
8479     case dw_val_class_die_ref:
8480       if (AT_ref_external (a))
8481 	return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8482       else
8483 	return DW_FORM_ref;
8484     case dw_val_class_fde_ref:
8485       return DW_FORM_data;
8486     case dw_val_class_lbl_id:
8487       return (AT_index (a) == NOT_INDEXED
8488               ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8489     case dw_val_class_lineptr:
8490     case dw_val_class_macptr:
8491       return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8492     case dw_val_class_str:
8493       return AT_string_form (a);
8494     case dw_val_class_file:
8495       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8496 	{
8497 	case 1:
8498 	  return DW_FORM_data1;
8499 	case 2:
8500 	  return DW_FORM_data2;
8501 	case 4:
8502 	  return DW_FORM_data4;
8503 	default:
8504 	  gcc_unreachable ();
8505 	}
8506 
8507     case dw_val_class_data8:
8508       return DW_FORM_data8;
8509 
8510     case dw_val_class_high_pc:
8511       switch (DWARF2_ADDR_SIZE)
8512 	{
8513 	case 1:
8514 	  return DW_FORM_data1;
8515 	case 2:
8516 	  return DW_FORM_data2;
8517 	case 4:
8518 	  return DW_FORM_data4;
8519 	case 8:
8520 	  return DW_FORM_data8;
8521 	default:
8522 	  gcc_unreachable ();
8523 	}
8524 
8525     default:
8526       gcc_unreachable ();
8527     }
8528 }
8529 
8530 /* Output the encoding of an attribute value.  */
8531 
8532 static void
8533 output_value_format (dw_attr_ref a)
8534 {
8535   enum dwarf_form form = value_format (a);
8536 
8537   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8538 }
8539 
8540 /* Given a die and id, produce the appropriate abbreviations.  */
8541 
8542 static void
8543 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8544 {
8545   unsigned ix;
8546   dw_attr_ref a_attr;
8547 
8548   dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8549   dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8550                                dwarf_tag_name (abbrev->die_tag));
8551 
8552   if (abbrev->die_child != NULL)
8553     dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8554   else
8555     dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8556 
8557   for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8558     {
8559       dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8560                                    dwarf_attr_name (a_attr->dw_attr));
8561       output_value_format (a_attr);
8562     }
8563 
8564   dw2_asm_output_data (1, 0, NULL);
8565   dw2_asm_output_data (1, 0, NULL);
8566 }
8567 
8568 
8569 /* Output the .debug_abbrev section which defines the DIE abbreviation
8570    table.  */
8571 
8572 static void
8573 output_abbrev_section (void)
8574 {
8575   unsigned long abbrev_id;
8576 
8577   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8578     output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8579 
8580   /* Terminate the table.  */
8581   dw2_asm_output_data (1, 0, NULL);
8582 }
8583 
8584 /* Output a symbol we can use to refer to this DIE from another CU.  */
8585 
8586 static inline void
8587 output_die_symbol (dw_die_ref die)
8588 {
8589   const char *sym = die->die_id.die_symbol;
8590 
8591   gcc_assert (!die->comdat_type_p);
8592 
8593   if (sym == 0)
8594     return;
8595 
8596   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8597     /* We make these global, not weak; if the target doesn't support
8598        .linkonce, it doesn't support combining the sections, so debugging
8599        will break.  */
8600     targetm.asm_out.globalize_label (asm_out_file, sym);
8601 
8602   ASM_OUTPUT_LABEL (asm_out_file, sym);
8603 }
8604 
8605 /* Return a new location list, given the begin and end range, and the
8606    expression.  */
8607 
8608 static inline dw_loc_list_ref
8609 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8610 	      const char *section)
8611 {
8612   dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
8613 
8614   retlist->begin = begin;
8615   retlist->begin_entry = NULL;
8616   retlist->end = end;
8617   retlist->expr = expr;
8618   retlist->section = section;
8619 
8620   return retlist;
8621 }
8622 
8623 /* Generate a new internal symbol for this location list node, if it
8624    hasn't got one yet.  */
8625 
8626 static inline void
8627 gen_llsym (dw_loc_list_ref list)
8628 {
8629   gcc_assert (!list->ll_symbol);
8630   list->ll_symbol = gen_internal_sym ("LLST");
8631 }
8632 
8633 /* Output the location list given to us.  */
8634 
8635 static void
8636 output_loc_list (dw_loc_list_ref list_head)
8637 {
8638   dw_loc_list_ref curr = list_head;
8639 
8640   if (list_head->emitted)
8641     return;
8642   list_head->emitted = true;
8643 
8644   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8645 
8646   /* Walk the location list, and output each range + expression.  */
8647   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8648     {
8649       unsigned long size;
8650       /* Don't output an entry that starts and ends at the same address.  */
8651       if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8652 	continue;
8653       size = size_of_locs (curr->expr);
8654       /* If the expression is too large, drop it on the floor.  We could
8655 	 perhaps put it into DW_TAG_dwarf_procedure and refer to that
8656 	 in the expression, but >= 64KB expressions for a single value
8657 	 in a single range are unlikely very useful.  */
8658       if (size > 0xffff)
8659 	continue;
8660       if (dwarf_split_debug_info)
8661         {
8662           dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8663                                "Location list start/length entry (%s)",
8664                                list_head->ll_symbol);
8665           dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8666                                        "Location list range start index (%s)",
8667                                        curr->begin);
8668           /* The length field is 4 bytes.  If we ever need to support
8669             an 8-byte length, we can add a new DW_LLE code or fall back
8670             to DW_LLE_GNU_start_end_entry.  */
8671           dw2_asm_output_delta (4, curr->end, curr->begin,
8672                                 "Location list range length (%s)",
8673                                 list_head->ll_symbol);
8674         }
8675       else if (!have_multiple_function_sections)
8676 	{
8677 	  dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8678 				"Location list begin address (%s)",
8679 				list_head->ll_symbol);
8680 	  dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8681 				"Location list end address (%s)",
8682 				list_head->ll_symbol);
8683 	}
8684       else
8685 	{
8686 	  dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8687 			       "Location list begin address (%s)",
8688 			       list_head->ll_symbol);
8689 	  dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8690 			       "Location list end address (%s)",
8691 			       list_head->ll_symbol);
8692 	}
8693 
8694       /* Output the block length for this list of location operations.  */
8695       gcc_assert (size <= 0xffff);
8696       dw2_asm_output_data (2, size, "%s", "Location expression size");
8697 
8698       output_loc_sequence (curr->expr, -1);
8699     }
8700 
8701   if (dwarf_split_debug_info)
8702     dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8703                          "Location list terminator (%s)",
8704                          list_head->ll_symbol);
8705   else
8706     {
8707       dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8708                            "Location list terminator begin (%s)",
8709                            list_head->ll_symbol);
8710       dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8711                            "Location list terminator end (%s)",
8712                            list_head->ll_symbol);
8713     }
8714 }
8715 
8716 /* Output a range_list offset into the debug_range section.  Emit a
8717    relocated reference if val_entry is NULL, otherwise, emit an
8718    indirect reference.  */
8719 
8720 static void
8721 output_range_list_offset (dw_attr_ref a)
8722 {
8723   const char *name = dwarf_attr_name (a->dw_attr);
8724 
8725   if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8726     {
8727       char *p = strchr (ranges_section_label, '\0');
8728       sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8729       dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8730                              debug_ranges_section, "%s", name);
8731       *p = '\0';
8732     }
8733   else
8734     dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8735                          "%s (offset from %s)", name, ranges_section_label);
8736 }
8737 
8738 /* Output the offset into the debug_loc section.  */
8739 
8740 static void
8741 output_loc_list_offset (dw_attr_ref a)
8742 {
8743   char *sym = AT_loc_list (a)->ll_symbol;
8744 
8745   gcc_assert (sym);
8746   if (dwarf_split_debug_info)
8747     dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8748                           "%s", dwarf_attr_name (a->dw_attr));
8749   else
8750     dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8751                            "%s", dwarf_attr_name (a->dw_attr));
8752 }
8753 
8754 /* Output an attribute's index or value appropriately.  */
8755 
8756 static void
8757 output_attr_index_or_value (dw_attr_ref a)
8758 {
8759   const char *name = dwarf_attr_name (a->dw_attr);
8760 
8761   if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8762     {
8763       dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8764       return;
8765     }
8766   switch (AT_class (a))
8767     {
8768       case dw_val_class_addr:
8769         dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8770         break;
8771       case dw_val_class_high_pc:
8772       case dw_val_class_lbl_id:
8773         dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8774         break;
8775       case dw_val_class_loc_list:
8776         output_loc_list_offset (a);
8777         break;
8778       default:
8779         gcc_unreachable ();
8780     }
8781 }
8782 
8783 /* Output a type signature.  */
8784 
8785 static inline void
8786 output_signature (const char *sig, const char *name)
8787 {
8788   int i;
8789 
8790   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8791     dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8792 }
8793 
8794 /* Output the DIE and its attributes.  Called recursively to generate
8795    the definitions of each child DIE.  */
8796 
8797 static void
8798 output_die (dw_die_ref die)
8799 {
8800   dw_attr_ref a;
8801   dw_die_ref c;
8802   unsigned long size;
8803   unsigned ix;
8804 
8805   /* If someone in another CU might refer to us, set up a symbol for
8806      them to point to.  */
8807   if (! die->comdat_type_p && die->die_id.die_symbol)
8808     output_die_symbol (die);
8809 
8810   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8811 			       (unsigned long)die->die_offset,
8812 			       dwarf_tag_name (die->die_tag));
8813 
8814   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8815     {
8816       const char *name = dwarf_attr_name (a->dw_attr);
8817 
8818       switch (AT_class (a))
8819 	{
8820 	case dw_val_class_addr:
8821           output_attr_index_or_value (a);
8822 	  break;
8823 
8824 	case dw_val_class_offset:
8825 	  dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8826 			       "%s", name);
8827 	  break;
8828 
8829 	case dw_val_class_range_list:
8830           output_range_list_offset (a);
8831 	  break;
8832 
8833 	case dw_val_class_loc:
8834 	  size = size_of_locs (AT_loc (a));
8835 
8836 	  /* Output the block length for this list of location operations.  */
8837 	  if (dwarf_version >= 4)
8838 	    dw2_asm_output_data_uleb128 (size, "%s", name);
8839 	  else
8840 	    dw2_asm_output_data (constant_size (size), size, "%s", name);
8841 
8842 	  output_loc_sequence (AT_loc (a), -1);
8843 	  break;
8844 
8845 	case dw_val_class_const:
8846 	  /* ??? It would be slightly more efficient to use a scheme like is
8847 	     used for unsigned constants below, but gdb 4.x does not sign
8848 	     extend.  Gdb 5.x does sign extend.  */
8849 	  dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8850 	  break;
8851 
8852 	case dw_val_class_unsigned_const:
8853 	  {
8854 	    int csize = constant_size (AT_unsigned (a));
8855 	    if (dwarf_version == 3
8856 		&& a->dw_attr == DW_AT_data_member_location
8857 		&& csize >= 4)
8858 	      dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8859 	    else
8860 	      dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8861 	  }
8862 	  break;
8863 
8864 	case dw_val_class_const_double:
8865 	  {
8866 	    unsigned HOST_WIDE_INT first, second;
8867 
8868 	    if (HOST_BITS_PER_WIDE_INT >= 64)
8869 	      dw2_asm_output_data (1,
8870 				   HOST_BITS_PER_DOUBLE_INT
8871 				   / HOST_BITS_PER_CHAR,
8872 				   NULL);
8873 
8874 	    if (WORDS_BIG_ENDIAN)
8875 	      {
8876 		first = a->dw_attr_val.v.val_double.high;
8877 		second = a->dw_attr_val.v.val_double.low;
8878 	      }
8879 	    else
8880 	      {
8881 		first = a->dw_attr_val.v.val_double.low;
8882 		second = a->dw_attr_val.v.val_double.high;
8883 	      }
8884 
8885 	    dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8886                                  first, "%s", name);
8887 	    dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8888 				 second, NULL);
8889 	  }
8890 	  break;
8891 
8892 	case dw_val_class_wide_int:
8893 	  {
8894 	    int i;
8895 	    int len = get_full_len (*a->dw_attr_val.v.val_wide);
8896 	    int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
8897 	    if (len * HOST_BITS_PER_WIDE_INT > 64)
8898 	      dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide) * l,
8899 				   NULL);
8900 
8901 	    if (WORDS_BIG_ENDIAN)
8902 	      for (i = len - 1; i >= 0; --i)
8903 		{
8904 		  dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8905 				       "%s", name);
8906 		  name = "";
8907 		}
8908 	    else
8909 	      for (i = 0; i < len; ++i)
8910 		{
8911 		  dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8912 				       "%s", name);
8913 		  name = "";
8914 		}
8915 	  }
8916 	  break;
8917 
8918 	case dw_val_class_vec:
8919 	  {
8920 	    unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8921 	    unsigned int len = a->dw_attr_val.v.val_vec.length;
8922 	    unsigned int i;
8923 	    unsigned char *p;
8924 
8925 	    dw2_asm_output_data (constant_size (len * elt_size),
8926 				 len * elt_size, "%s", name);
8927 	    if (elt_size > sizeof (HOST_WIDE_INT))
8928 	      {
8929 		elt_size /= 2;
8930 		len *= 2;
8931 	      }
8932 	    for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8933 		 i < len;
8934 		 i++, p += elt_size)
8935 	      dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8936 				   "fp or vector constant word %u", i);
8937 	    break;
8938 	  }
8939 
8940 	case dw_val_class_flag:
8941 	  if (dwarf_version >= 4)
8942 	    {
8943 	      /* Currently all add_AT_flag calls pass in 1 as last argument,
8944 		 so DW_FORM_flag_present can be used.  If that ever changes,
8945 		 we'll need to use DW_FORM_flag and have some optimization
8946 		 in build_abbrev_table that will change those to
8947 		 DW_FORM_flag_present if it is set to 1 in all DIEs using
8948 		 the same abbrev entry.  */
8949 	      gcc_assert (AT_flag (a) == 1);
8950 	      if (flag_debug_asm)
8951 		fprintf (asm_out_file, "\t\t\t%s %s\n",
8952 			 ASM_COMMENT_START, name);
8953 	      break;
8954 	    }
8955 	  dw2_asm_output_data (1, AT_flag (a), "%s", name);
8956 	  break;
8957 
8958 	case dw_val_class_loc_list:
8959           output_attr_index_or_value (a);
8960 	  break;
8961 
8962 	case dw_val_class_die_ref:
8963 	  if (AT_ref_external (a))
8964 	    {
8965 	      if (AT_ref (a)->comdat_type_p)
8966 	        {
8967 	          comdat_type_node_ref type_node =
8968 	            AT_ref (a)->die_id.die_type_node;
8969 
8970 	          gcc_assert (type_node);
8971 	          output_signature (type_node->signature, name);
8972 	        }
8973 	      else
8974 	        {
8975 		  const char *sym = AT_ref (a)->die_id.die_symbol;
8976 		  int size;
8977 
8978 		  gcc_assert (sym);
8979 		  /* In DWARF2, DW_FORM_ref_addr is sized by target address
8980 		     length, whereas in DWARF3 it's always sized as an
8981 		     offset.  */
8982 		  if (dwarf_version == 2)
8983 		    size = DWARF2_ADDR_SIZE;
8984 		  else
8985 		    size = DWARF_OFFSET_SIZE;
8986 		  dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8987 					 name);
8988 		}
8989 	    }
8990 	  else
8991 	    {
8992 	      gcc_assert (AT_ref (a)->die_offset);
8993 	      dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8994 				   "%s", name);
8995 	    }
8996 	  break;
8997 
8998 	case dw_val_class_fde_ref:
8999 	  {
9000 	    char l1[20];
9001 
9002 	    ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
9003 					 a->dw_attr_val.v.val_fde_index * 2);
9004 	    dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
9005 				   "%s", name);
9006 	  }
9007 	  break;
9008 
9009 	case dw_val_class_vms_delta:
9010 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
9011 	  dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
9012 				    AT_vms_delta2 (a), AT_vms_delta1 (a),
9013 				    "%s", name);
9014 #else
9015 	  dw2_asm_output_delta (DWARF_OFFSET_SIZE,
9016 				AT_vms_delta2 (a), AT_vms_delta1 (a),
9017 				"%s", name);
9018 #endif
9019 	  break;
9020 
9021 	case dw_val_class_lbl_id:
9022           output_attr_index_or_value (a);
9023 	  break;
9024 
9025 	case dw_val_class_lineptr:
9026 	  dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9027 				 debug_line_section, "%s", name);
9028 	  break;
9029 
9030 	case dw_val_class_macptr:
9031 	  dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9032 				 debug_macinfo_section, "%s", name);
9033 	  break;
9034 
9035 	case dw_val_class_str:
9036           if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
9037             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9038                                    a->dw_attr_val.v.val_str->label,
9039                                    debug_str_section,
9040                                    "%s: \"%s\"", name, AT_string (a));
9041           else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
9042             dw2_asm_output_data_uleb128 (AT_index (a),
9043                                          "%s: \"%s\"", name, AT_string (a));
9044           else
9045 	    dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
9046 	  break;
9047 
9048 	case dw_val_class_file:
9049 	  {
9050 	    int f = maybe_emit_file (a->dw_attr_val.v.val_file);
9051 
9052 	    dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
9053 				 a->dw_attr_val.v.val_file->filename);
9054 	    break;
9055 	  }
9056 
9057 	case dw_val_class_data8:
9058 	  {
9059 	    int i;
9060 
9061 	    for (i = 0; i < 8; i++)
9062 	      dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
9063 				   i == 0 ? "%s" : NULL, name);
9064 	    break;
9065 	  }
9066 
9067 	case dw_val_class_high_pc:
9068 	  dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
9069 				get_AT_low_pc (die), "DW_AT_high_pc");
9070 	  break;
9071 
9072 	default:
9073 	  gcc_unreachable ();
9074 	}
9075     }
9076 
9077   FOR_EACH_CHILD (die, c, output_die (c));
9078 
9079   /* Add null byte to terminate sibling list.  */
9080   if (die->die_child != NULL)
9081     dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
9082 			 (unsigned long) die->die_offset);
9083 }
9084 
9085 /* Output the compilation unit that appears at the beginning of the
9086    .debug_info section, and precedes the DIE descriptions.  */
9087 
9088 static void
9089 output_compilation_unit_header (void)
9090 {
9091   /* We don't support actual DWARFv5 units yet, we just use some
9092      DWARFv5 draft DIE tags in DWARFv4 format.  */
9093   int ver = dwarf_version < 5 ? dwarf_version : 4;
9094 
9095   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9096     dw2_asm_output_data (4, 0xffffffff,
9097       "Initial length escape value indicating 64-bit DWARF extension");
9098   dw2_asm_output_data (DWARF_OFFSET_SIZE,
9099 		       next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
9100 		       "Length of Compilation Unit Info");
9101   dw2_asm_output_data (2, ver, "DWARF version number");
9102   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
9103 			 debug_abbrev_section,
9104 			 "Offset Into Abbrev. Section");
9105   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9106 }
9107 
9108 /* Output the compilation unit DIE and its children.  */
9109 
9110 static void
9111 output_comp_unit (dw_die_ref die, int output_if_empty)
9112 {
9113   const char *secname, *oldsym;
9114   char *tmp;
9115 
9116   /* Unless we are outputting main CU, we may throw away empty ones.  */
9117   if (!output_if_empty && die->die_child == NULL)
9118     return;
9119 
9120   /* Even if there are no children of this DIE, we must output the information
9121      about the compilation unit.  Otherwise, on an empty translation unit, we
9122      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
9123      will then complain when examining the file.  First mark all the DIEs in
9124      this CU so we know which get local refs.  */
9125   mark_dies (die);
9126 
9127   external_ref_hash_type *extern_map = optimize_external_refs (die);
9128 
9129   build_abbrev_table (die, extern_map);
9130 
9131   delete extern_map;
9132 
9133   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
9134   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
9135   calc_die_sizes (die);
9136 
9137   oldsym = die->die_id.die_symbol;
9138   if (oldsym)
9139     {
9140       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
9141 
9142       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
9143       secname = tmp;
9144       die->die_id.die_symbol = NULL;
9145       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9146     }
9147   else
9148     {
9149       switch_to_section (debug_info_section);
9150       ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
9151       info_section_emitted = true;
9152     }
9153 
9154   /* Output debugging information.  */
9155   output_compilation_unit_header ();
9156   output_die (die);
9157 
9158   /* Leave the marks on the main CU, so we can check them in
9159      output_pubnames.  */
9160   if (oldsym)
9161     {
9162       unmark_dies (die);
9163       die->die_id.die_symbol = oldsym;
9164     }
9165 }
9166 
9167 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
9168    and .debug_pubtypes.  This is configured per-target, but can be
9169    overridden by the -gpubnames or -gno-pubnames options.  */
9170 
9171 static inline bool
9172 want_pubnames (void)
9173 {
9174   if (debug_info_level <= DINFO_LEVEL_TERSE)
9175     return false;
9176   if (debug_generate_pub_sections != -1)
9177     return debug_generate_pub_sections;
9178   return targetm.want_debug_pub_sections;
9179 }
9180 
9181 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes.  */
9182 
9183 static void
9184 add_AT_pubnames (dw_die_ref die)
9185 {
9186   if (want_pubnames ())
9187     add_AT_flag (die, DW_AT_GNU_pubnames, 1);
9188 }
9189 
9190 /* Add a string attribute value to a skeleton DIE.  */
9191 
9192 static inline void
9193 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
9194                         const char *str)
9195 {
9196   dw_attr_node attr;
9197   struct indirect_string_node *node;
9198 
9199   if (! skeleton_debug_str_hash)
9200     skeleton_debug_str_hash
9201       = hash_table<indirect_string_hasher>::create_ggc (10);
9202 
9203   node = find_AT_string_in_table (str, skeleton_debug_str_hash);
9204   find_string_form (node);
9205   if (node->form == DW_FORM_GNU_str_index)
9206     node->form = DW_FORM_strp;
9207 
9208   attr.dw_attr = attr_kind;
9209   attr.dw_attr_val.val_class = dw_val_class_str;
9210   attr.dw_attr_val.val_entry = NULL;
9211   attr.dw_attr_val.v.val_str = node;
9212   add_dwarf_attr (die, &attr);
9213 }
9214 
9215 /* Helper function to generate top-level dies for skeleton debug_info and
9216    debug_types.  */
9217 
9218 static void
9219 add_top_level_skeleton_die_attrs (dw_die_ref die)
9220 {
9221   const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
9222   const char *comp_dir = comp_dir_string ();
9223 
9224   add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
9225   if (comp_dir != NULL)
9226     add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
9227   add_AT_pubnames (die);
9228   add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
9229 }
9230 
9231 /* Output skeleton debug sections that point to the dwo file.  */
9232 
9233 static void
9234 output_skeleton_debug_sections (dw_die_ref comp_unit)
9235 {
9236   /* We don't support actual DWARFv5 units yet, we just use some
9237      DWARFv5 draft DIE tags in DWARFv4 format.  */
9238   int ver = dwarf_version < 5 ? dwarf_version : 4;
9239 
9240   /* These attributes will be found in the full debug_info section.  */
9241   remove_AT (comp_unit, DW_AT_producer);
9242   remove_AT (comp_unit, DW_AT_language);
9243 
9244   switch_to_section (debug_skeleton_info_section);
9245   ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
9246 
9247   /* Produce the skeleton compilation-unit header.  This one differs enough from
9248      a normal CU header that it's better not to call output_compilation_unit
9249      header.  */
9250   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9251     dw2_asm_output_data (4, 0xffffffff,
9252       "Initial length escape value indicating 64-bit DWARF extension");
9253 
9254   dw2_asm_output_data (DWARF_OFFSET_SIZE,
9255                        DWARF_COMPILE_UNIT_HEADER_SIZE
9256                        - DWARF_INITIAL_LENGTH_SIZE
9257                        + size_of_die (comp_unit),
9258                       "Length of Compilation Unit Info");
9259   dw2_asm_output_data (2, ver, "DWARF version number");
9260   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
9261                          debug_abbrev_section,
9262                          "Offset Into Abbrev. Section");
9263   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9264 
9265   comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
9266   output_die (comp_unit);
9267 
9268   /* Build the skeleton debug_abbrev section.  */
9269   switch_to_section (debug_skeleton_abbrev_section);
9270   ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
9271 
9272   output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
9273 
9274   dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
9275 }
9276 
9277 /* Output a comdat type unit DIE and its children.  */
9278 
9279 static void
9280 output_comdat_type_unit (comdat_type_node *node)
9281 {
9282   const char *secname;
9283   char *tmp;
9284   int i;
9285 #if defined (OBJECT_FORMAT_ELF)
9286   tree comdat_key;
9287 #endif
9288 
9289   /* First mark all the DIEs in this CU so we know which get local refs.  */
9290   mark_dies (node->root_die);
9291 
9292   external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
9293 
9294   build_abbrev_table (node->root_die, extern_map);
9295 
9296   delete extern_map;
9297   extern_map = NULL;
9298 
9299   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
9300   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
9301   calc_die_sizes (node->root_die);
9302 
9303 #if defined (OBJECT_FORMAT_ELF)
9304   if (!dwarf_split_debug_info)
9305     secname = ".debug_types";
9306   else
9307     secname = ".debug_types.dwo";
9308 
9309   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9310   sprintf (tmp, "wt.");
9311   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9312     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9313   comdat_key = get_identifier (tmp);
9314   targetm.asm_out.named_section (secname,
9315                                  SECTION_DEBUG | SECTION_LINKONCE,
9316                                  comdat_key);
9317 #else
9318   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9319   sprintf (tmp, ".gnu.linkonce.wt.");
9320   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9321     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9322   secname = tmp;
9323   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9324 #endif
9325 
9326   /* Output debugging information.  */
9327   output_compilation_unit_header ();
9328   output_signature (node->signature, "Type Signature");
9329   dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9330 		       "Offset to Type DIE");
9331   output_die (node->root_die);
9332 
9333   unmark_dies (node->root_die);
9334 }
9335 
9336 /* Return the DWARF2/3 pubname associated with a decl.  */
9337 
9338 static const char *
9339 dwarf2_name (tree decl, int scope)
9340 {
9341   if (DECL_NAMELESS (decl))
9342     return NULL;
9343   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9344 }
9345 
9346 /* Add a new entry to .debug_pubnames if appropriate.  */
9347 
9348 static void
9349 add_pubname_string (const char *str, dw_die_ref die)
9350 {
9351   pubname_entry e;
9352 
9353   e.die = die;
9354   e.name = xstrdup (str);
9355   vec_safe_push (pubname_table, e);
9356 }
9357 
9358 static void
9359 add_pubname (tree decl, dw_die_ref die)
9360 {
9361   if (!want_pubnames ())
9362     return;
9363 
9364   /* Don't add items to the table when we expect that the consumer will have
9365      just read the enclosing die.  For example, if the consumer is looking at a
9366      class_member, it will either be inside the class already, or will have just
9367      looked up the class to find the member.  Either way, searching the class is
9368      faster than searching the index.  */
9369   if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9370       || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9371     {
9372       const char *name = dwarf2_name (decl, 1);
9373 
9374       if (name)
9375 	add_pubname_string (name, die);
9376     }
9377 }
9378 
9379 /* Add an enumerator to the pubnames section.  */
9380 
9381 static void
9382 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9383 {
9384   pubname_entry e;
9385 
9386   gcc_assert (scope_name);
9387   e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9388   e.die = die;
9389   vec_safe_push (pubname_table, e);
9390 }
9391 
9392 /* Add a new entry to .debug_pubtypes if appropriate.  */
9393 
9394 static void
9395 add_pubtype (tree decl, dw_die_ref die)
9396 {
9397   pubname_entry e;
9398 
9399   if (!want_pubnames ())
9400     return;
9401 
9402   if ((TREE_PUBLIC (decl)
9403        || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9404       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9405     {
9406       tree scope = NULL;
9407       const char *scope_name = "";
9408       const char *sep = is_cxx () ? "::" : ".";
9409       const char *name;
9410 
9411       scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9412       if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9413         {
9414           scope_name = lang_hooks.dwarf_name (scope, 1);
9415           if (scope_name != NULL && scope_name[0] != '\0')
9416             scope_name = concat (scope_name, sep, NULL);
9417           else
9418             scope_name = "";
9419 	}
9420 
9421       if (TYPE_P (decl))
9422         name = type_tag (decl);
9423       else
9424         name = lang_hooks.dwarf_name (decl, 1);
9425 
9426       /* If we don't have a name for the type, there's no point in adding
9427 	 it to the table.  */
9428       if (name != NULL && name[0] != '\0')
9429         {
9430           e.die = die;
9431           e.name = concat (scope_name, name, NULL);
9432           vec_safe_push (pubtype_table, e);
9433         }
9434 
9435       /* Although it might be more consistent to add the pubinfo for the
9436          enumerators as their dies are created, they should only be added if the
9437          enum type meets the criteria above.  So rather than re-check the parent
9438          enum type whenever an enumerator die is created, just output them all
9439          here.  This isn't protected by the name conditional because anonymous
9440          enums don't have names.  */
9441       if (die->die_tag == DW_TAG_enumeration_type)
9442         {
9443           dw_die_ref c;
9444 
9445           FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9446         }
9447     }
9448 }
9449 
9450 /* Output a single entry in the pubnames table.  */
9451 
9452 static void
9453 output_pubname (dw_offset die_offset, pubname_entry *entry)
9454 {
9455   dw_die_ref die = entry->die;
9456   int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9457 
9458   dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9459 
9460   if (debug_generate_pub_sections == 2)
9461     {
9462       /* This logic follows gdb's method for determining the value of the flag
9463          byte.  */
9464       uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9465       switch (die->die_tag)
9466       {
9467         case DW_TAG_typedef:
9468         case DW_TAG_base_type:
9469         case DW_TAG_subrange_type:
9470           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9471           GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9472           break;
9473         case DW_TAG_enumerator:
9474           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9475                                           GDB_INDEX_SYMBOL_KIND_VARIABLE);
9476           if (!is_cxx () && !is_java ())
9477             GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9478           break;
9479         case DW_TAG_subprogram:
9480           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9481                                           GDB_INDEX_SYMBOL_KIND_FUNCTION);
9482           if (!is_ada ())
9483             GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9484           break;
9485         case DW_TAG_constant:
9486           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9487                                           GDB_INDEX_SYMBOL_KIND_VARIABLE);
9488           GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9489           break;
9490         case DW_TAG_variable:
9491           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9492                                           GDB_INDEX_SYMBOL_KIND_VARIABLE);
9493           GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9494           break;
9495         case DW_TAG_namespace:
9496         case DW_TAG_imported_declaration:
9497           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9498           break;
9499         case DW_TAG_class_type:
9500         case DW_TAG_interface_type:
9501         case DW_TAG_structure_type:
9502         case DW_TAG_union_type:
9503         case DW_TAG_enumeration_type:
9504           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9505           if (!is_cxx () && !is_java ())
9506 	    GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9507           break;
9508         default:
9509           /* An unusual tag.  Leave the flag-byte empty.  */
9510           break;
9511       }
9512       dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9513                            "GDB-index flags");
9514     }
9515 
9516   dw2_asm_output_nstring (entry->name, -1, "external name");
9517 }
9518 
9519 
9520 /* Output the public names table used to speed up access to externally
9521    visible names; or the public types table used to find type definitions.  */
9522 
9523 static void
9524 output_pubnames (vec<pubname_entry, va_gc> *names)
9525 {
9526   unsigned i;
9527   unsigned long pubnames_length = size_of_pubnames (names);
9528   pubname_ref pub;
9529 
9530   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9531     dw2_asm_output_data (4, 0xffffffff,
9532       "Initial length escape value indicating 64-bit DWARF extension");
9533   dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length, "Pub Info Length");
9534 
9535   /* Version number for pubnames/pubtypes is independent of dwarf version.  */
9536   dw2_asm_output_data (2, 2, "DWARF Version");
9537 
9538   if (dwarf_split_debug_info)
9539     dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9540                            debug_skeleton_info_section,
9541                            "Offset of Compilation Unit Info");
9542   else
9543     dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9544                            debug_info_section,
9545                            "Offset of Compilation Unit Info");
9546   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9547 		       "Compilation Unit Length");
9548 
9549   FOR_EACH_VEC_ELT (*names, i, pub)
9550     {
9551       if (include_pubname_in_output (names, pub))
9552 	{
9553 	  dw_offset die_offset = pub->die->die_offset;
9554 
9555           /* We shouldn't see pubnames for DIEs outside of the main CU.  */
9556           if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9557             gcc_assert (pub->die->die_mark);
9558 
9559 	  /* If we're putting types in their own .debug_types sections,
9560 	     the .debug_pubtypes table will still point to the compile
9561 	     unit (not the type unit), so we want to use the offset of
9562 	     the skeleton DIE (if there is one).  */
9563 	  if (pub->die->comdat_type_p && names == pubtype_table)
9564 	    {
9565 	      comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9566 
9567 	      if (type_node != NULL)
9568 	        die_offset = (type_node->skeleton_die != NULL
9569 			      ? type_node->skeleton_die->die_offset
9570 			      : comp_unit_die ()->die_offset);
9571 	    }
9572 
9573           output_pubname (die_offset, pub);
9574 	}
9575     }
9576 
9577   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9578 }
9579 
9580 /* Output public names and types tables if necessary.  */
9581 
9582 static void
9583 output_pubtables (void)
9584 {
9585   if (!want_pubnames () || !info_section_emitted)
9586     return;
9587 
9588   switch_to_section (debug_pubnames_section);
9589   output_pubnames (pubname_table);
9590   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
9591      It shouldn't hurt to emit it always, since pure DWARF2 consumers
9592      simply won't look for the section.  */
9593   switch_to_section (debug_pubtypes_section);
9594   output_pubnames (pubtype_table);
9595 }
9596 
9597 
9598 /* Output the information that goes into the .debug_aranges table.
9599    Namely, define the beginning and ending address range of the
9600    text section generated for this compilation unit.  */
9601 
9602 static void
9603 output_aranges (unsigned long aranges_length)
9604 {
9605   unsigned i;
9606 
9607   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9608     dw2_asm_output_data (4, 0xffffffff,
9609       "Initial length escape value indicating 64-bit DWARF extension");
9610   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9611 		       "Length of Address Ranges Info");
9612   /* Version number for aranges is still 2, even up to DWARF5.  */
9613   dw2_asm_output_data (2, 2, "DWARF Version");
9614   if (dwarf_split_debug_info)
9615     dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9616                            debug_skeleton_info_section,
9617                            "Offset of Compilation Unit Info");
9618   else
9619     dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9620                            debug_info_section,
9621                            "Offset of Compilation Unit Info");
9622   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9623   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9624 
9625   /* We need to align to twice the pointer size here.  */
9626   if (DWARF_ARANGES_PAD_SIZE)
9627     {
9628       /* Pad using a 2 byte words so that padding is correct for any
9629 	 pointer size.  */
9630       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9631 			   2 * DWARF2_ADDR_SIZE);
9632       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9633 	dw2_asm_output_data (2, 0, NULL);
9634     }
9635 
9636   /* It is necessary not to output these entries if the sections were
9637      not used; if the sections were not used, the length will be 0 and
9638      the address may end up as 0 if the section is discarded by ld
9639      --gc-sections, leaving an invalid (0, 0) entry that can be
9640      confused with the terminator.  */
9641   if (text_section_used)
9642     {
9643       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9644       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9645 			    text_section_label, "Length");
9646     }
9647   if (cold_text_section_used)
9648     {
9649       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9650 			   "Address");
9651       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9652 			    cold_text_section_label, "Length");
9653     }
9654 
9655   if (have_multiple_function_sections)
9656     {
9657       unsigned fde_idx;
9658       dw_fde_ref fde;
9659 
9660       FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9661 	{
9662 	  if (DECL_IGNORED_P (fde->decl))
9663 	    continue;
9664 	  if (!fde->in_std_section)
9665 	    {
9666 	      dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9667 				   "Address");
9668 	      dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9669 				    fde->dw_fde_begin, "Length");
9670 	    }
9671 	  if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9672 	    {
9673 	      dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9674 				   "Address");
9675 	      dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9676 				    fde->dw_fde_second_begin, "Length");
9677 	    }
9678 	}
9679     }
9680 
9681   /* Output the terminator words.  */
9682   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9683   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9684 }
9685 
9686 /* Add a new entry to .debug_ranges.  Return the offset at which it
9687    was placed.  */
9688 
9689 static unsigned int
9690 add_ranges_num (int num)
9691 {
9692   unsigned int in_use = ranges_table_in_use;
9693 
9694   if (in_use == ranges_table_allocated)
9695     {
9696       ranges_table_allocated += RANGES_TABLE_INCREMENT;
9697       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9698 				    ranges_table_allocated);
9699       memset (ranges_table + ranges_table_in_use, 0,
9700 	      RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9701     }
9702 
9703   ranges_table[in_use].num = num;
9704   ranges_table_in_use = in_use + 1;
9705 
9706   return in_use * 2 * DWARF2_ADDR_SIZE;
9707 }
9708 
9709 /* Add a new entry to .debug_ranges corresponding to a block, or a
9710    range terminator if BLOCK is NULL.  */
9711 
9712 static unsigned int
9713 add_ranges (const_tree block)
9714 {
9715   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9716 }
9717 
9718 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9719    When using dwarf_split_debug_info, address attributes in dies destined
9720    for the final executable should be direct references--setting the
9721    parameter force_direct ensures this behavior.  */
9722 
9723 static void
9724 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9725                       bool *added, bool force_direct)
9726 {
9727   unsigned int in_use = ranges_by_label_in_use;
9728   unsigned int offset;
9729 
9730   if (in_use == ranges_by_label_allocated)
9731     {
9732       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9733       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9734 				       ranges_by_label,
9735 				       ranges_by_label_allocated);
9736       memset (ranges_by_label + ranges_by_label_in_use, 0,
9737 	      RANGES_TABLE_INCREMENT
9738 	      * sizeof (struct dw_ranges_by_label_struct));
9739     }
9740 
9741   ranges_by_label[in_use].begin = begin;
9742   ranges_by_label[in_use].end = end;
9743   ranges_by_label_in_use = in_use + 1;
9744 
9745   offset = add_ranges_num (-(int)in_use - 1);
9746   if (!*added)
9747     {
9748       add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9749       *added = true;
9750     }
9751 }
9752 
9753 static void
9754 output_ranges (void)
9755 {
9756   unsigned i;
9757   static const char *const start_fmt = "Offset %#x";
9758   const char *fmt = start_fmt;
9759 
9760   for (i = 0; i < ranges_table_in_use; i++)
9761     {
9762       int block_num = ranges_table[i].num;
9763 
9764       if (block_num > 0)
9765 	{
9766 	  char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9767 	  char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9768 
9769 	  ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9770 	  ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9771 
9772 	  /* If all code is in the text section, then the compilation
9773 	     unit base address defaults to DW_AT_low_pc, which is the
9774 	     base of the text section.  */
9775 	  if (!have_multiple_function_sections)
9776 	    {
9777 	      dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9778 				    text_section_label,
9779 				    fmt, i * 2 * DWARF2_ADDR_SIZE);
9780 	      dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9781 				    text_section_label, NULL);
9782 	    }
9783 
9784 	  /* Otherwise, the compilation unit base address is zero,
9785 	     which allows us to use absolute addresses, and not worry
9786 	     about whether the target supports cross-section
9787 	     arithmetic.  */
9788 	  else
9789 	    {
9790 	      dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9791 				   fmt, i * 2 * DWARF2_ADDR_SIZE);
9792 	      dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9793 	    }
9794 
9795 	  fmt = NULL;
9796 	}
9797 
9798       /* Negative block_num stands for an index into ranges_by_label.  */
9799       else if (block_num < 0)
9800 	{
9801 	  int lab_idx = - block_num - 1;
9802 
9803 	  if (!have_multiple_function_sections)
9804 	    {
9805 	      gcc_unreachable ();
9806 #if 0
9807 	      /* If we ever use add_ranges_by_labels () for a single
9808 		 function section, all we have to do is to take out
9809 		 the #if 0 above.  */
9810 	      dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9811 				    ranges_by_label[lab_idx].begin,
9812 				    text_section_label,
9813 				    fmt, i * 2 * DWARF2_ADDR_SIZE);
9814 	      dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9815 				    ranges_by_label[lab_idx].end,
9816 				    text_section_label, NULL);
9817 #endif
9818 	    }
9819 	  else
9820 	    {
9821 	      dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9822 				   ranges_by_label[lab_idx].begin,
9823 				   fmt, i * 2 * DWARF2_ADDR_SIZE);
9824 	      dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9825 				   ranges_by_label[lab_idx].end,
9826 				   NULL);
9827 	    }
9828 	}
9829       else
9830 	{
9831 	  dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9832 	  dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9833 	  fmt = start_fmt;
9834 	}
9835     }
9836 }
9837 
9838 /* Data structure containing information about input files.  */
9839 struct file_info
9840 {
9841   const char *path;	/* Complete file name.  */
9842   const char *fname;	/* File name part.  */
9843   int length;		/* Length of entire string.  */
9844   struct dwarf_file_data * file_idx;	/* Index in input file table.  */
9845   int dir_idx;		/* Index in directory table.  */
9846 };
9847 
9848 /* Data structure containing information about directories with source
9849    files.  */
9850 struct dir_info
9851 {
9852   const char *path;	/* Path including directory name.  */
9853   int length;		/* Path length.  */
9854   int prefix;		/* Index of directory entry which is a prefix.  */
9855   int count;		/* Number of files in this directory.  */
9856   int dir_idx;		/* Index of directory used as base.  */
9857 };
9858 
9859 /* Callback function for file_info comparison.  We sort by looking at
9860    the directories in the path.  */
9861 
9862 static int
9863 file_info_cmp (const void *p1, const void *p2)
9864 {
9865   const struct file_info *const s1 = (const struct file_info *) p1;
9866   const struct file_info *const s2 = (const struct file_info *) p2;
9867   const unsigned char *cp1;
9868   const unsigned char *cp2;
9869 
9870   /* Take care of file names without directories.  We need to make sure that
9871      we return consistent values to qsort since some will get confused if
9872      we return the same value when identical operands are passed in opposite
9873      orders.  So if neither has a directory, return 0 and otherwise return
9874      1 or -1 depending on which one has the directory.  */
9875   if ((s1->path == s1->fname || s2->path == s2->fname))
9876     return (s2->path == s2->fname) - (s1->path == s1->fname);
9877 
9878   cp1 = (const unsigned char *) s1->path;
9879   cp2 = (const unsigned char *) s2->path;
9880 
9881   while (1)
9882     {
9883       ++cp1;
9884       ++cp2;
9885       /* Reached the end of the first path?  If so, handle like above.  */
9886       if ((cp1 == (const unsigned char *) s1->fname)
9887 	  || (cp2 == (const unsigned char *) s2->fname))
9888 	return ((cp2 == (const unsigned char *) s2->fname)
9889 		- (cp1 == (const unsigned char *) s1->fname));
9890 
9891       /* Character of current path component the same?  */
9892       else if (*cp1 != *cp2)
9893 	return *cp1 - *cp2;
9894     }
9895 }
9896 
9897 struct file_name_acquire_data
9898 {
9899   struct file_info *files;
9900   int used_files;
9901   int max_files;
9902 };
9903 
9904 /* Traversal function for the hash table.  */
9905 
9906 int
9907 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
9908 {
9909   struct dwarf_file_data *d = *slot;
9910   struct file_info *fi;
9911   const char *f;
9912 
9913   gcc_assert (fnad->max_files >= d->emitted_number);
9914 
9915   if (! d->emitted_number)
9916     return 1;
9917 
9918   gcc_assert (fnad->max_files != fnad->used_files);
9919 
9920   fi = fnad->files + fnad->used_files++;
9921 
9922   /* Skip all leading "./".  */
9923   f = d->filename;
9924   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9925     f += 2;
9926 
9927   /* Create a new array entry.  */
9928   fi->path = f;
9929   fi->length = strlen (f);
9930   fi->file_idx = d;
9931 
9932   /* Search for the file name part.  */
9933   f = strrchr (f, DIR_SEPARATOR);
9934 #if defined (DIR_SEPARATOR_2)
9935   {
9936     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9937 
9938     if (g != NULL)
9939       {
9940 	if (f == NULL || f < g)
9941 	  f = g;
9942       }
9943   }
9944 #endif
9945 
9946   fi->fname = f == NULL ? fi->path : f + 1;
9947   return 1;
9948 }
9949 
9950 /* Output the directory table and the file name table.  We try to minimize
9951    the total amount of memory needed.  A heuristic is used to avoid large
9952    slowdowns with many input files.  */
9953 
9954 static void
9955 output_file_names (void)
9956 {
9957   struct file_name_acquire_data fnad;
9958   int numfiles;
9959   struct file_info *files;
9960   struct dir_info *dirs;
9961   int *saved;
9962   int *savehere;
9963   int *backmap;
9964   int ndirs;
9965   int idx_offset;
9966   int i;
9967 
9968   if (!last_emitted_file)
9969     {
9970       dw2_asm_output_data (1, 0, "End directory table");
9971       dw2_asm_output_data (1, 0, "End file name table");
9972       return;
9973     }
9974 
9975   numfiles = last_emitted_file->emitted_number;
9976 
9977   /* Allocate the various arrays we need.  */
9978   files = XALLOCAVEC (struct file_info, numfiles);
9979   dirs = XALLOCAVEC (struct dir_info, numfiles);
9980 
9981   fnad.files = files;
9982   fnad.used_files = 0;
9983   fnad.max_files = numfiles;
9984   file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
9985   gcc_assert (fnad.used_files == fnad.max_files);
9986 
9987   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9988 
9989   /* Find all the different directories used.  */
9990   dirs[0].path = files[0].path;
9991   dirs[0].length = files[0].fname - files[0].path;
9992   dirs[0].prefix = -1;
9993   dirs[0].count = 1;
9994   dirs[0].dir_idx = 0;
9995   files[0].dir_idx = 0;
9996   ndirs = 1;
9997 
9998   for (i = 1; i < numfiles; i++)
9999     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
10000 	&& memcmp (dirs[ndirs - 1].path, files[i].path,
10001 		   dirs[ndirs - 1].length) == 0)
10002       {
10003 	/* Same directory as last entry.  */
10004 	files[i].dir_idx = ndirs - 1;
10005 	++dirs[ndirs - 1].count;
10006       }
10007     else
10008       {
10009 	int j;
10010 
10011 	/* This is a new directory.  */
10012 	dirs[ndirs].path = files[i].path;
10013 	dirs[ndirs].length = files[i].fname - files[i].path;
10014 	dirs[ndirs].count = 1;
10015 	dirs[ndirs].dir_idx = ndirs;
10016 	files[i].dir_idx = ndirs;
10017 
10018 	/* Search for a prefix.  */
10019 	dirs[ndirs].prefix = -1;
10020 	for (j = 0; j < ndirs; j++)
10021 	  if (dirs[j].length < dirs[ndirs].length
10022 	      && dirs[j].length > 1
10023 	      && (dirs[ndirs].prefix == -1
10024 		  || dirs[j].length > dirs[dirs[ndirs].prefix].length)
10025 	      && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
10026 	    dirs[ndirs].prefix = j;
10027 
10028 	++ndirs;
10029       }
10030 
10031   /* Now to the actual work.  We have to find a subset of the directories which
10032      allow expressing the file name using references to the directory table
10033      with the least amount of characters.  We do not do an exhaustive search
10034      where we would have to check out every combination of every single
10035      possible prefix.  Instead we use a heuristic which provides nearly optimal
10036      results in most cases and never is much off.  */
10037   saved = XALLOCAVEC (int, ndirs);
10038   savehere = XALLOCAVEC (int, ndirs);
10039 
10040   memset (saved, '\0', ndirs * sizeof (saved[0]));
10041   for (i = 0; i < ndirs; i++)
10042     {
10043       int j;
10044       int total;
10045 
10046       /* We can always save some space for the current directory.  But this
10047 	 does not mean it will be enough to justify adding the directory.  */
10048       savehere[i] = dirs[i].length;
10049       total = (savehere[i] - saved[i]) * dirs[i].count;
10050 
10051       for (j = i + 1; j < ndirs; j++)
10052 	{
10053 	  savehere[j] = 0;
10054 	  if (saved[j] < dirs[i].length)
10055 	    {
10056 	      /* Determine whether the dirs[i] path is a prefix of the
10057 		 dirs[j] path.  */
10058 	      int k;
10059 
10060 	      k = dirs[j].prefix;
10061 	      while (k != -1 && k != (int) i)
10062 		k = dirs[k].prefix;
10063 
10064 	      if (k == (int) i)
10065 		{
10066 		  /* Yes it is.  We can possibly save some memory by
10067 		     writing the filenames in dirs[j] relative to
10068 		     dirs[i].  */
10069 		  savehere[j] = dirs[i].length;
10070 		  total += (savehere[j] - saved[j]) * dirs[j].count;
10071 		}
10072 	    }
10073 	}
10074 
10075       /* Check whether we can save enough to justify adding the dirs[i]
10076 	 directory.  */
10077       if (total > dirs[i].length + 1)
10078 	{
10079 	  /* It's worthwhile adding.  */
10080 	  for (j = i; j < ndirs; j++)
10081 	    if (savehere[j] > 0)
10082 	      {
10083 		/* Remember how much we saved for this directory so far.  */
10084 		saved[j] = savehere[j];
10085 
10086 		/* Remember the prefix directory.  */
10087 		dirs[j].dir_idx = i;
10088 	      }
10089 	}
10090     }
10091 
10092   /* Emit the directory name table.  */
10093   idx_offset = dirs[0].length > 0 ? 1 : 0;
10094   for (i = 1 - idx_offset; i < ndirs; i++)
10095     dw2_asm_output_nstring (dirs[i].path,
10096 			    dirs[i].length
10097 			     - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
10098 			    "Directory Entry: %#x", i + idx_offset);
10099 
10100   dw2_asm_output_data (1, 0, "End directory table");
10101 
10102   /* We have to emit them in the order of emitted_number since that's
10103      used in the debug info generation.  To do this efficiently we
10104      generate a back-mapping of the indices first.  */
10105   backmap = XALLOCAVEC (int, numfiles);
10106   for (i = 0; i < numfiles; i++)
10107     backmap[files[i].file_idx->emitted_number - 1] = i;
10108 
10109   /* Now write all the file names.  */
10110   for (i = 0; i < numfiles; i++)
10111     {
10112       int file_idx = backmap[i];
10113       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
10114 
10115 #ifdef VMS_DEBUGGING_INFO
10116 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
10117 
10118       /* Setting these fields can lead to debugger miscomparisons,
10119          but VMS Debug requires them to be set correctly.  */
10120 
10121       int ver;
10122       long long cdt;
10123       long siz;
10124       int maxfilelen = strlen (files[file_idx].path)
10125 			       + dirs[dir_idx].length
10126 			       + MAX_VMS_VERSION_LEN + 1;
10127       char *filebuf = XALLOCAVEC (char, maxfilelen);
10128 
10129       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
10130       snprintf (filebuf, maxfilelen, "%s;%d",
10131 	        files[file_idx].path + dirs[dir_idx].length, ver);
10132 
10133       dw2_asm_output_nstring
10134 	(filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
10135 
10136       /* Include directory index.  */
10137       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10138 
10139       /* Modification time.  */
10140       dw2_asm_output_data_uleb128
10141         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
10142 	  ? cdt : 0,
10143 	 NULL);
10144 
10145       /* File length in bytes.  */
10146       dw2_asm_output_data_uleb128
10147         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
10148       	  ? siz : 0,
10149 	 NULL);
10150 #else
10151       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
10152 			      "File Entry: %#x", (unsigned) i + 1);
10153 
10154       /* Include directory index.  */
10155       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10156 
10157       /* Modification time.  */
10158       dw2_asm_output_data_uleb128 (0, NULL);
10159 
10160       /* File length in bytes.  */
10161       dw2_asm_output_data_uleb128 (0, NULL);
10162 #endif /* VMS_DEBUGGING_INFO */
10163     }
10164 
10165   dw2_asm_output_data (1, 0, "End file name table");
10166 }
10167 
10168 
10169 /* Output one line number table into the .debug_line section.  */
10170 
10171 static void
10172 output_one_line_info_table (dw_line_info_table *table)
10173 {
10174   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
10175   unsigned int current_line = 1;
10176   bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
10177   dw_line_info_entry *ent;
10178   size_t i;
10179 
10180   FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
10181     {
10182       switch (ent->opcode)
10183 	{
10184 	case LI_set_address:
10185 	  /* ??? Unfortunately, we have little choice here currently, and
10186 	     must always use the most general form.  GCC does not know the
10187 	     address delta itself, so we can't use DW_LNS_advance_pc.  Many
10188 	     ports do have length attributes which will give an upper bound
10189 	     on the address range.  We could perhaps use length attributes
10190 	     to determine when it is safe to use DW_LNS_fixed_advance_pc.  */
10191 	  ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
10192 
10193 	  /* This can handle any delta.  This takes
10194 	     4+DWARF2_ADDR_SIZE bytes.  */
10195 	  dw2_asm_output_data (1, 0, "set address %s", line_label);
10196 	  dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10197 	  dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10198 	  dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10199 	  break;
10200 
10201 	case LI_set_line:
10202 	  if (ent->val == current_line)
10203 	    {
10204 	      /* We still need to start a new row, so output a copy insn.  */
10205 	      dw2_asm_output_data (1, DW_LNS_copy,
10206 				   "copy line %u", current_line);
10207 	    }
10208 	  else
10209 	    {
10210 	      int line_offset = ent->val - current_line;
10211 	      int line_delta = line_offset - DWARF_LINE_BASE;
10212 
10213 	      current_line = ent->val;
10214 	      if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10215 		{
10216 		  /* This can handle deltas from -10 to 234, using the current
10217 		     definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
10218 		     This takes 1 byte.  */
10219 		  dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10220 				       "line %u", current_line);
10221 		}
10222 	      else
10223 		{
10224 		  /* This can handle any delta.  This takes at least 4 bytes,
10225 		     depending on the value being encoded.  */
10226 		  dw2_asm_output_data (1, DW_LNS_advance_line,
10227 				       "advance to line %u", current_line);
10228 		  dw2_asm_output_data_sleb128 (line_offset, NULL);
10229 		  dw2_asm_output_data (1, DW_LNS_copy, NULL);
10230 		}
10231 	    }
10232 	  break;
10233 
10234 	case LI_set_file:
10235 	  dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
10236 	  dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10237 	  break;
10238 
10239 	case LI_set_column:
10240 	  dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
10241 	  dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10242 	  break;
10243 
10244 	case LI_negate_stmt:
10245 	  current_is_stmt = !current_is_stmt;
10246 	  dw2_asm_output_data (1, DW_LNS_negate_stmt,
10247 			       "is_stmt %d", current_is_stmt);
10248 	  break;
10249 
10250 	case LI_set_prologue_end:
10251 	  dw2_asm_output_data (1, DW_LNS_set_prologue_end,
10252 			       "set prologue end");
10253 	  break;
10254 
10255 	case LI_set_epilogue_begin:
10256 	  dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
10257 			       "set epilogue begin");
10258 	  break;
10259 
10260 	case LI_set_discriminator:
10261 	  dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
10262 	  dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
10263 	  dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
10264 	  dw2_asm_output_data_uleb128 (ent->val, NULL);
10265 	  break;
10266 	}
10267     }
10268 
10269   /* Emit debug info for the address of the end of the table.  */
10270   dw2_asm_output_data (1, 0, "set address %s", table->end_label);
10271   dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10272   dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10273   dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10274 
10275   dw2_asm_output_data (1, 0, "end sequence");
10276   dw2_asm_output_data_uleb128 (1, NULL);
10277   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10278 }
10279 
10280 /* Output the source line number correspondence information.  This
10281    information goes into the .debug_line section.  */
10282 
10283 static void
10284 output_line_info (bool prologue_only)
10285 {
10286   char l1[20], l2[20], p1[20], p2[20];
10287   /* We don't support DWARFv5 line tables yet.  */
10288   int ver = dwarf_version < 5 ? dwarf_version : 4;
10289   bool saw_one = false;
10290   int opc;
10291 
10292   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10293   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10294   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10295   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10296 
10297   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10298     dw2_asm_output_data (4, 0xffffffff,
10299       "Initial length escape value indicating 64-bit DWARF extension");
10300   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10301 			"Length of Source Line Info");
10302   ASM_OUTPUT_LABEL (asm_out_file, l1);
10303 
10304   dw2_asm_output_data (2, ver, "DWARF Version");
10305   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10306   ASM_OUTPUT_LABEL (asm_out_file, p1);
10307 
10308   /* Define the architecture-dependent minimum instruction length (in bytes).
10309      In this implementation of DWARF, this field is used for information
10310      purposes only.  Since GCC generates assembly language, we have no
10311      a priori knowledge of how many instruction bytes are generated for each
10312      source line, and therefore can use only the DW_LNE_set_address and
10313      DW_LNS_fixed_advance_pc line information commands.  Accordingly, we fix
10314      this as '1', which is "correct enough" for all architectures,
10315      and don't let the target override.  */
10316   dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10317 
10318   if (ver >= 4)
10319     dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10320 			 "Maximum Operations Per Instruction");
10321   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10322 		       "Default is_stmt_start flag");
10323   dw2_asm_output_data (1, DWARF_LINE_BASE,
10324 		       "Line Base Value (Special Opcodes)");
10325   dw2_asm_output_data (1, DWARF_LINE_RANGE,
10326 		       "Line Range Value (Special Opcodes)");
10327   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10328 		       "Special Opcode Base");
10329 
10330   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10331     {
10332       int n_op_args;
10333       switch (opc)
10334 	{
10335 	case DW_LNS_advance_pc:
10336 	case DW_LNS_advance_line:
10337 	case DW_LNS_set_file:
10338 	case DW_LNS_set_column:
10339 	case DW_LNS_fixed_advance_pc:
10340 	case DW_LNS_set_isa:
10341 	  n_op_args = 1;
10342 	  break;
10343 	default:
10344 	  n_op_args = 0;
10345 	  break;
10346 	}
10347 
10348       dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10349 			   opc, n_op_args);
10350     }
10351 
10352   /* Write out the information about the files we use.  */
10353   output_file_names ();
10354   ASM_OUTPUT_LABEL (asm_out_file, p2);
10355   if (prologue_only)
10356     {
10357       /* Output the marker for the end of the line number info.  */
10358       ASM_OUTPUT_LABEL (asm_out_file, l2);
10359       return;
10360     }
10361 
10362   if (separate_line_info)
10363     {
10364       dw_line_info_table *table;
10365       size_t i;
10366 
10367       FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10368 	if (table->in_use)
10369 	  {
10370 	    output_one_line_info_table (table);
10371 	    saw_one = true;
10372 	  }
10373     }
10374   if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10375     {
10376       output_one_line_info_table (cold_text_section_line_info);
10377       saw_one = true;
10378     }
10379 
10380   /* ??? Some Darwin linkers crash on a .debug_line section with no
10381      sequences.  Further, merely a DW_LNE_end_sequence entry is not
10382      sufficient -- the address column must also be initialized.
10383      Make sure to output at least one set_address/end_sequence pair,
10384      choosing .text since that section is always present.  */
10385   if (text_section_line_info->in_use || !saw_one)
10386     output_one_line_info_table (text_section_line_info);
10387 
10388   /* Output the marker for the end of the line number info.  */
10389   ASM_OUTPUT_LABEL (asm_out_file, l2);
10390 }
10391 
10392 /* Given a pointer to a tree node for some base type, return a pointer to
10393    a DIE that describes the given type.
10394 
10395    This routine must only be called for GCC type nodes that correspond to
10396    Dwarf base (fundamental) types.  */
10397 
10398 static dw_die_ref
10399 base_type_die (tree type)
10400 {
10401   dw_die_ref base_type_result;
10402   enum dwarf_type encoding;
10403 
10404   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10405     return 0;
10406 
10407   /* If this is a subtype that should not be emitted as a subrange type,
10408      use the base type.  See subrange_type_for_debug_p.  */
10409   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10410     type = TREE_TYPE (type);
10411 
10412   switch (TREE_CODE (type))
10413     {
10414     case INTEGER_TYPE:
10415       if ((dwarf_version >= 4 || !dwarf_strict)
10416 	  && TYPE_NAME (type)
10417 	  && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10418 	  && DECL_IS_BUILTIN (TYPE_NAME (type))
10419 	  && DECL_NAME (TYPE_NAME (type)))
10420 	{
10421 	  const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10422 	  if (strcmp (name, "char16_t") == 0
10423 	      || strcmp (name, "char32_t") == 0)
10424 	    {
10425 	      encoding = DW_ATE_UTF;
10426 	      break;
10427 	    }
10428 	}
10429       if (TYPE_STRING_FLAG (type))
10430 	{
10431 	  if (TYPE_UNSIGNED (type))
10432 	    encoding = DW_ATE_unsigned_char;
10433 	  else
10434 	    encoding = DW_ATE_signed_char;
10435 	}
10436       else if (TYPE_UNSIGNED (type))
10437 	encoding = DW_ATE_unsigned;
10438       else
10439 	encoding = DW_ATE_signed;
10440       break;
10441 
10442     case REAL_TYPE:
10443       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10444 	{
10445 	  if (dwarf_version >= 3 || !dwarf_strict)
10446 	    encoding = DW_ATE_decimal_float;
10447 	  else
10448 	    encoding = DW_ATE_lo_user;
10449 	}
10450       else
10451 	encoding = DW_ATE_float;
10452       break;
10453 
10454     case FIXED_POINT_TYPE:
10455       if (!(dwarf_version >= 3 || !dwarf_strict))
10456 	encoding = DW_ATE_lo_user;
10457       else if (TYPE_UNSIGNED (type))
10458 	encoding = DW_ATE_unsigned_fixed;
10459       else
10460 	encoding = DW_ATE_signed_fixed;
10461       break;
10462 
10463       /* Dwarf2 doesn't know anything about complex ints, so use
10464 	 a user defined type for it.  */
10465     case COMPLEX_TYPE:
10466       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10467 	encoding = DW_ATE_complex_float;
10468       else
10469 	encoding = DW_ATE_lo_user;
10470       break;
10471 
10472     case BOOLEAN_TYPE:
10473       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
10474       encoding = DW_ATE_boolean;
10475       break;
10476 
10477     default:
10478       /* No other TREE_CODEs are Dwarf fundamental types.  */
10479       gcc_unreachable ();
10480     }
10481 
10482   base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10483 
10484   add_AT_unsigned (base_type_result, DW_AT_byte_size,
10485 		   int_size_in_bytes (type));
10486   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10487   add_pubtype (type, base_type_result);
10488 
10489   return base_type_result;
10490 }
10491 
10492 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
10493    named 'auto' in its type: return true for it, false otherwise.  */
10494 
10495 static inline bool
10496 is_cxx_auto (tree type)
10497 {
10498   if (is_cxx ())
10499     {
10500       tree name = TYPE_IDENTIFIER (type);
10501       if (name == get_identifier ("auto")
10502 	  || name == get_identifier ("decltype(auto)"))
10503 	return true;
10504     }
10505   return false;
10506 }
10507 
10508 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10509    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
10510 
10511 static inline int
10512 is_base_type (tree type)
10513 {
10514   switch (TREE_CODE (type))
10515     {
10516     case ERROR_MARK:
10517     case VOID_TYPE:
10518     case INTEGER_TYPE:
10519     case REAL_TYPE:
10520     case FIXED_POINT_TYPE:
10521     case COMPLEX_TYPE:
10522     case BOOLEAN_TYPE:
10523     case POINTER_BOUNDS_TYPE:
10524       return 1;
10525 
10526     case ARRAY_TYPE:
10527     case RECORD_TYPE:
10528     case UNION_TYPE:
10529     case QUAL_UNION_TYPE:
10530     case ENUMERAL_TYPE:
10531     case FUNCTION_TYPE:
10532     case METHOD_TYPE:
10533     case POINTER_TYPE:
10534     case REFERENCE_TYPE:
10535     case NULLPTR_TYPE:
10536     case OFFSET_TYPE:
10537     case LANG_TYPE:
10538     case VECTOR_TYPE:
10539       return 0;
10540 
10541     default:
10542       if (is_cxx_auto (type))
10543 	return 0;
10544       gcc_unreachable ();
10545     }
10546 
10547   return 0;
10548 }
10549 
10550 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10551    node, return the size in bits for the type if it is a constant, or else
10552    return the alignment for the type if the type's size is not constant, or
10553    else return BITS_PER_WORD if the type actually turns out to be an
10554    ERROR_MARK node.  */
10555 
10556 static inline unsigned HOST_WIDE_INT
10557 simple_type_size_in_bits (const_tree type)
10558 {
10559   if (TREE_CODE (type) == ERROR_MARK)
10560     return BITS_PER_WORD;
10561   else if (TYPE_SIZE (type) == NULL_TREE)
10562     return 0;
10563   else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
10564     return tree_to_uhwi (TYPE_SIZE (type));
10565   else
10566     return TYPE_ALIGN (type);
10567 }
10568 
10569 /* Similarly, but return an offset_int instead of UHWI.  */
10570 
10571 static inline offset_int
10572 offset_int_type_size_in_bits (const_tree type)
10573 {
10574   if (TREE_CODE (type) == ERROR_MARK)
10575     return BITS_PER_WORD;
10576   else if (TYPE_SIZE (type) == NULL_TREE)
10577     return 0;
10578   else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10579     return wi::to_offset (TYPE_SIZE (type));
10580   else
10581     return TYPE_ALIGN (type);
10582 }
10583 
10584 /*  Given a pointer to a tree node for a subrange type, return a pointer
10585     to a DIE that describes the given type.  */
10586 
10587 static dw_die_ref
10588 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10589 {
10590   dw_die_ref subrange_die;
10591   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10592 
10593   if (context_die == NULL)
10594     context_die = comp_unit_die ();
10595 
10596   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10597 
10598   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10599     {
10600       /* The size of the subrange type and its base type do not match,
10601 	 so we need to generate a size attribute for the subrange type.  */
10602       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10603     }
10604 
10605   if (low)
10606     add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
10607   if (high)
10608     add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
10609 
10610   return subrange_die;
10611 }
10612 
10613 /* Returns the (const and/or volatile) cv_qualifiers associated with
10614    the decl node.  This will normally be augmented with the
10615    cv_qualifiers of the underlying type in add_type_attribute.  */
10616 
10617 static int
10618 decl_quals (const_tree decl)
10619 {
10620   return ((TREE_READONLY (decl)
10621 	   ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
10622 	  | (TREE_THIS_VOLATILE (decl)
10623 	     ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
10624 }
10625 
10626 /* Determine the TYPE whose qualifiers match the largest strict subset
10627    of the given TYPE_QUALS, and return its qualifiers.  Ignore all
10628    qualifiers outside QUAL_MASK.  */
10629 
10630 static int
10631 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
10632 {
10633   tree t;
10634   int best_rank = 0, best_qual = 0, max_rank;
10635 
10636   type_quals &= qual_mask;
10637   max_rank = popcount_hwi (type_quals) - 1;
10638 
10639   for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
10640        t = TYPE_NEXT_VARIANT (t))
10641     {
10642       int q = TYPE_QUALS (t) & qual_mask;
10643 
10644       if ((q & type_quals) == q && q != type_quals
10645 	  && check_base_type (t, type))
10646 	{
10647 	  int rank = popcount_hwi (q);
10648 
10649 	  if (rank > best_rank)
10650 	    {
10651 	      best_rank = rank;
10652 	      best_qual = q;
10653 	    }
10654 	}
10655     }
10656 
10657   return best_qual;
10658 }
10659 
10660 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10661    entry that chains various modifiers in front of the given type.  */
10662 
10663 static dw_die_ref
10664 modified_type_die (tree type, int cv_quals, dw_die_ref context_die)
10665 {
10666   enum tree_code code = TREE_CODE (type);
10667   dw_die_ref mod_type_die;
10668   dw_die_ref sub_die = NULL;
10669   tree item_type = NULL;
10670   tree qualified_type;
10671   tree name, low, high;
10672   dw_die_ref mod_scope;
10673   /* Only these cv-qualifiers are currently handled.  */
10674   const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
10675 			    | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
10676 
10677   if (code == ERROR_MARK)
10678     return NULL;
10679 
10680   cv_quals &= cv_qual_mask;
10681 
10682   /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
10683      tag modifier (and not an attribute) old consumers won't be able
10684      to handle it.  */
10685   if (dwarf_version < 3)
10686     cv_quals &= ~TYPE_QUAL_RESTRICT;
10687 
10688   /* Likewise for DW_TAG_atomic_type for DWARFv5.  */
10689   if (dwarf_version < 5)
10690     cv_quals &= ~TYPE_QUAL_ATOMIC;
10691 
10692   /* See if we already have the appropriately qualified variant of
10693      this type.  */
10694   qualified_type = get_qualified_type (type, cv_quals);
10695 
10696   if (qualified_type == sizetype
10697       && TYPE_NAME (qualified_type)
10698       && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10699     {
10700       tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10701 
10702       gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10703 			   && TYPE_PRECISION (t)
10704 			   == TYPE_PRECISION (qualified_type)
10705 			   && TYPE_UNSIGNED (t)
10706 			   == TYPE_UNSIGNED (qualified_type));
10707       qualified_type = t;
10708     }
10709 
10710   /* If we do, then we can just use its DIE, if it exists.  */
10711   if (qualified_type)
10712     {
10713       mod_type_die = lookup_type_die (qualified_type);
10714       if (mod_type_die)
10715 	return mod_type_die;
10716     }
10717 
10718   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10719 
10720   /* Handle C typedef types.  */
10721   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10722       && !DECL_ARTIFICIAL (name))
10723     {
10724       tree dtype = TREE_TYPE (name);
10725 
10726       if (qualified_type == dtype)
10727 	{
10728 	  /* For a named type, use the typedef.  */
10729 	  gen_type_die (qualified_type, context_die);
10730 	  return lookup_type_die (qualified_type);
10731 	}
10732       else
10733 	{
10734 	  int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
10735 	  dquals &= cv_qual_mask;
10736 	  if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
10737 	      || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
10738 	    /* cv-unqualified version of named type.  Just use
10739 	       the unnamed type to which it refers.  */
10740 	    return modified_type_die (DECL_ORIGINAL_TYPE (name),
10741 				      cv_quals, context_die);
10742 	  /* Else cv-qualified version of named type; fall through.  */
10743 	}
10744     }
10745 
10746   mod_scope = scope_die_for (type, context_die);
10747 
10748   if (cv_quals)
10749     {
10750       struct qual_info { int q; enum dwarf_tag t; };
10751       static const struct qual_info qual_info[] =
10752 	{
10753 	  { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type },
10754 	  { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
10755 	  { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
10756 	  { TYPE_QUAL_CONST, DW_TAG_const_type },
10757 	};
10758       int sub_quals;
10759       unsigned i;
10760 
10761       /* Determine a lesser qualified type that most closely matches
10762 	 this one.  Then generate DW_TAG_* entries for the remaining
10763 	 qualifiers.  */
10764       sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
10765 						  cv_qual_mask);
10766       mod_type_die = modified_type_die (type, sub_quals, context_die);
10767 
10768       for (i = 0; i < sizeof (qual_info) / sizeof (qual_info[0]); i++)
10769 	if (qual_info[i].q & cv_quals & ~sub_quals)
10770 	  {
10771 	    dw_die_ref d = new_die (qual_info[i].t, mod_scope, type);
10772 	    if (mod_type_die)
10773 	      add_AT_die_ref (d, DW_AT_type, mod_type_die);
10774 	    mod_type_die = d;
10775 	  }
10776     }
10777   else if (code == POINTER_TYPE)
10778     {
10779       mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10780       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10781 		       simple_type_size_in_bits (type) / BITS_PER_UNIT);
10782       item_type = TREE_TYPE (type);
10783       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10784 	add_AT_unsigned (mod_type_die, DW_AT_address_class,
10785 			 TYPE_ADDR_SPACE (item_type));
10786     }
10787   else if (code == REFERENCE_TYPE)
10788     {
10789       if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10790 	mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10791 				type);
10792       else
10793 	mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10794       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10795 		       simple_type_size_in_bits (type) / BITS_PER_UNIT);
10796       item_type = TREE_TYPE (type);
10797       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10798 	add_AT_unsigned (mod_type_die, DW_AT_address_class,
10799 			 TYPE_ADDR_SPACE (item_type));
10800     }
10801   else if (code == INTEGER_TYPE
10802 	   && TREE_TYPE (type) != NULL_TREE
10803 	   && subrange_type_for_debug_p (type, &low, &high))
10804     {
10805       mod_type_die = subrange_type_die (type, low, high, context_die);
10806       item_type = TREE_TYPE (type);
10807     }
10808   else if (is_base_type (type))
10809     mod_type_die = base_type_die (type);
10810   else
10811     {
10812       gen_type_die (type, context_die);
10813 
10814       /* We have to get the type_main_variant here (and pass that to the
10815 	 `lookup_type_die' routine) because the ..._TYPE node we have
10816 	 might simply be a *copy* of some original type node (where the
10817 	 copy was created to help us keep track of typedef names) and
10818 	 that copy might have a different TYPE_UID from the original
10819 	 ..._TYPE node.  */
10820       if (TREE_CODE (type) != VECTOR_TYPE
10821 	  && TREE_CODE (type) != ARRAY_TYPE)
10822 	return lookup_type_die (type_main_variant (type));
10823       else
10824 	/* Vectors have the debugging information in the type,
10825 	   not the main variant.  */
10826 	return lookup_type_die (type);
10827     }
10828 
10829   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
10830      don't output a DW_TAG_typedef, since there isn't one in the
10831      user's program; just attach a DW_AT_name to the type.
10832      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10833      if the base type already has the same name.  */
10834   if (name
10835       && ((TREE_CODE (name) != TYPE_DECL
10836 	   && (qualified_type == TYPE_MAIN_VARIANT (type)
10837 	       || (cv_quals == TYPE_UNQUALIFIED)))
10838 	  || (TREE_CODE (name) == TYPE_DECL
10839 	      && TREE_TYPE (name) == qualified_type
10840 	      && DECL_NAME (name))))
10841     {
10842       if (TREE_CODE (name) == TYPE_DECL)
10843 	/* Could just call add_name_and_src_coords_attributes here,
10844 	   but since this is a builtin type it doesn't have any
10845 	   useful source coordinates anyway.  */
10846 	name = DECL_NAME (name);
10847       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10848     }
10849   /* This probably indicates a bug.  */
10850   else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10851     {
10852       name = TYPE_IDENTIFIER (type);
10853       add_name_attribute (mod_type_die,
10854 			  name ? IDENTIFIER_POINTER (name) : "__unknown__");
10855     }
10856 
10857   if (qualified_type)
10858     equate_type_number_to_die (qualified_type, mod_type_die);
10859 
10860   if (item_type)
10861     /* We must do this after the equate_type_number_to_die call, in case
10862        this is a recursive type.  This ensures that the modified_type_die
10863        recursion will terminate even if the type is recursive.  Recursive
10864        types are possible in Ada.  */
10865     sub_die = modified_type_die (item_type,
10866 				 TYPE_QUALS_NO_ADDR_SPACE (item_type),
10867 				 context_die);
10868 
10869   if (sub_die != NULL)
10870     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10871 
10872   add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10873   if (TYPE_ARTIFICIAL (type))
10874     add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10875 
10876   return mod_type_die;
10877 }
10878 
10879 /* Generate DIEs for the generic parameters of T.
10880    T must be either a generic type or a generic function.
10881    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
10882 
10883 static void
10884 gen_generic_params_dies (tree t)
10885 {
10886   tree parms, args;
10887   int parms_num, i;
10888   dw_die_ref die = NULL;
10889   int non_default;
10890 
10891   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10892     return;
10893 
10894   if (TYPE_P (t))
10895     die = lookup_type_die (t);
10896   else if (DECL_P (t))
10897     die = lookup_decl_die (t);
10898 
10899   gcc_assert (die);
10900 
10901   parms = lang_hooks.get_innermost_generic_parms (t);
10902   if (!parms)
10903     /* T has no generic parameter. It means T is neither a generic type
10904        or function. End of story.  */
10905     return;
10906 
10907   parms_num = TREE_VEC_LENGTH (parms);
10908   args = lang_hooks.get_innermost_generic_args (t);
10909   if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
10910     non_default = int_cst_value (TREE_CHAIN (args));
10911   else
10912     non_default = TREE_VEC_LENGTH (args);
10913   for (i = 0; i < parms_num; i++)
10914     {
10915       tree parm, arg, arg_pack_elems;
10916       dw_die_ref parm_die;
10917 
10918       parm = TREE_VEC_ELT (parms, i);
10919       arg = TREE_VEC_ELT (args, i);
10920       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10921       gcc_assert (parm && TREE_VALUE (parm) && arg);
10922 
10923       if (parm && TREE_VALUE (parm) && arg)
10924 	{
10925 	  /* If PARM represents a template parameter pack,
10926 	     emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10927 	     by DW_TAG_template_*_parameter DIEs for the argument
10928 	     pack elements of ARG. Note that ARG would then be
10929 	     an argument pack.  */
10930 	  if (arg_pack_elems)
10931 	    parm_die = template_parameter_pack_die (TREE_VALUE (parm),
10932 						    arg_pack_elems,
10933 						    die);
10934 	  else
10935 	    parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
10936 					      true /* emit name */, die);
10937 	  if (i >= non_default)
10938 	    add_AT_flag (parm_die, DW_AT_default_value, 1);
10939 	}
10940     }
10941 }
10942 
10943 /* Create and return a DIE for PARM which should be
10944    the representation of a generic type parameter.
10945    For instance, in the C++ front end, PARM would be a template parameter.
10946    ARG is the argument to PARM.
10947    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
10948    name of the PARM.
10949    PARENT_DIE is the parent DIE which the new created DIE should be added to,
10950    as a child node.  */
10951 
10952 static dw_die_ref
10953 generic_parameter_die (tree parm, tree arg,
10954 		       bool emit_name_p,
10955 		       dw_die_ref parent_die)
10956 {
10957   dw_die_ref tmpl_die = NULL;
10958   const char *name = NULL;
10959 
10960   if (!parm || !DECL_NAME (parm) || !arg)
10961     return NULL;
10962 
10963   /* We support non-type generic parameters and arguments,
10964      type generic parameters and arguments, as well as
10965      generic generic parameters (a.k.a. template template parameters in C++)
10966      and arguments.  */
10967   if (TREE_CODE (parm) == PARM_DECL)
10968     /* PARM is a nontype generic parameter  */
10969     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10970   else if (TREE_CODE (parm) == TYPE_DECL)
10971     /* PARM is a type generic parameter.  */
10972     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10973   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10974     /* PARM is a generic generic parameter.
10975        Its DIE is a GNU extension. It shall have a
10976        DW_AT_name attribute to represent the name of the template template
10977        parameter, and a DW_AT_GNU_template_name attribute to represent the
10978        name of the template template argument.  */
10979     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10980 			parent_die, parm);
10981   else
10982     gcc_unreachable ();
10983 
10984   if (tmpl_die)
10985     {
10986       tree tmpl_type;
10987 
10988       /* If PARM is a generic parameter pack, it means we are
10989          emitting debug info for a template argument pack element.
10990 	 In other terms, ARG is a template argument pack element.
10991 	 In that case, we don't emit any DW_AT_name attribute for
10992 	 the die.  */
10993       if (emit_name_p)
10994 	{
10995 	  name = IDENTIFIER_POINTER (DECL_NAME (parm));
10996 	  gcc_assert (name);
10997 	  add_AT_string (tmpl_die, DW_AT_name, name);
10998 	}
10999 
11000       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
11001 	{
11002 	  /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
11003 	     TMPL_DIE should have a child DW_AT_type attribute that is set
11004 	     to the type of the argument to PARM, which is ARG.
11005 	     If PARM is a type generic parameter, TMPL_DIE should have a
11006 	     child DW_AT_type that is set to ARG.  */
11007 	  tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
11008 	  add_type_attribute (tmpl_die, tmpl_type,
11009 			      (TREE_THIS_VOLATILE (tmpl_type)
11010 			       ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
11011 			      parent_die);
11012 	}
11013       else
11014 	{
11015 	  /* So TMPL_DIE is a DIE representing a
11016 	     a generic generic template parameter, a.k.a template template
11017 	     parameter in C++ and arg is a template.  */
11018 
11019 	  /* The DW_AT_GNU_template_name attribute of the DIE must be set
11020 	     to the name of the argument.  */
11021 	  name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
11022 	  if (name)
11023 	    add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
11024 	}
11025 
11026       if (TREE_CODE (parm) == PARM_DECL)
11027 	/* So PARM is a non-type generic parameter.
11028 	   DWARF3 5.6.8 says we must set a DW_AT_const_value child
11029 	   attribute of TMPL_DIE which value represents the value
11030 	   of ARG.
11031 	   We must be careful here:
11032 	   The value of ARG might reference some function decls.
11033 	   We might currently be emitting debug info for a generic
11034 	   type and types are emitted before function decls, we don't
11035 	   know if the function decls referenced by ARG will actually be
11036 	   emitted after cgraph computations.
11037 	   So must defer the generation of the DW_AT_const_value to
11038 	   after cgraph is ready.  */
11039 	append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
11040     }
11041 
11042   return tmpl_die;
11043 }
11044 
11045 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
11046    PARM_PACK must be a template parameter pack. The returned DIE
11047    will be child DIE of PARENT_DIE.  */
11048 
11049 static dw_die_ref
11050 template_parameter_pack_die (tree parm_pack,
11051 			     tree parm_pack_args,
11052 			     dw_die_ref parent_die)
11053 {
11054   dw_die_ref die;
11055   int j;
11056 
11057   gcc_assert (parent_die && parm_pack);
11058 
11059   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
11060   add_name_and_src_coords_attributes (die, parm_pack);
11061   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
11062     generic_parameter_die (parm_pack,
11063 			   TREE_VEC_ELT (parm_pack_args, j),
11064 			   false /* Don't emit DW_AT_name */,
11065 			   die);
11066   return die;
11067 }
11068 
11069 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
11070    an enumerated type.  */
11071 
11072 static inline int
11073 type_is_enum (const_tree type)
11074 {
11075   return TREE_CODE (type) == ENUMERAL_TYPE;
11076 }
11077 
11078 /* Return the DBX register number described by a given RTL node.  */
11079 
11080 static unsigned int
11081 dbx_reg_number (const_rtx rtl)
11082 {
11083   unsigned regno = REGNO (rtl);
11084 
11085   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
11086 
11087 #ifdef LEAF_REG_REMAP
11088   if (crtl->uses_only_leaf_regs)
11089     {
11090       int leaf_reg = LEAF_REG_REMAP (regno);
11091       if (leaf_reg != -1)
11092 	regno = (unsigned) leaf_reg;
11093     }
11094 #endif
11095 
11096   regno = DBX_REGISTER_NUMBER (regno);
11097   gcc_assert (regno != INVALID_REGNUM);
11098   return regno;
11099 }
11100 
11101 /* Optionally add a DW_OP_piece term to a location description expression.
11102    DW_OP_piece is only added if the location description expression already
11103    doesn't end with DW_OP_piece.  */
11104 
11105 static void
11106 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
11107 {
11108   dw_loc_descr_ref loc;
11109 
11110   if (*list_head != NULL)
11111     {
11112       /* Find the end of the chain.  */
11113       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
11114 	;
11115 
11116       if (loc->dw_loc_opc != DW_OP_piece)
11117 	loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
11118     }
11119 }
11120 
11121 /* Return a location descriptor that designates a machine register or
11122    zero if there is none.  */
11123 
11124 static dw_loc_descr_ref
11125 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
11126 {
11127   rtx regs;
11128 
11129   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
11130     return 0;
11131 
11132   /* We only use "frame base" when we're sure we're talking about the
11133      post-prologue local stack frame.  We do this by *not* running
11134      register elimination until this point, and recognizing the special
11135      argument pointer and soft frame pointer rtx's.
11136      Use DW_OP_fbreg offset DW_OP_stack_value in this case.  */
11137   if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
11138       && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
11139     {
11140       dw_loc_descr_ref result = NULL;
11141 
11142       if (dwarf_version >= 4 || !dwarf_strict)
11143 	{
11144 	  result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
11145 				       initialized);
11146 	  if (result)
11147 	    add_loc_descr (&result,
11148 			   new_loc_descr (DW_OP_stack_value, 0, 0));
11149 	}
11150       return result;
11151     }
11152 
11153   regs = targetm.dwarf_register_span (rtl);
11154 
11155   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
11156     return multiple_reg_loc_descriptor (rtl, regs, initialized);
11157   else
11158     {
11159       unsigned int dbx_regnum = dbx_reg_number (rtl);
11160       if (dbx_regnum == IGNORED_DWARF_REGNUM)
11161 	return 0;
11162       return one_reg_loc_descriptor (dbx_regnum, initialized);
11163     }
11164 }
11165 
11166 /* Return a location descriptor that designates a machine register for
11167    a given hard register number.  */
11168 
11169 static dw_loc_descr_ref
11170 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
11171 {
11172   dw_loc_descr_ref reg_loc_descr;
11173 
11174   if (regno <= 31)
11175     reg_loc_descr
11176       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
11177   else
11178     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
11179 
11180   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11181     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11182 
11183   return reg_loc_descr;
11184 }
11185 
11186 /* Given an RTL of a register, return a location descriptor that
11187    designates a value that spans more than one register.  */
11188 
11189 static dw_loc_descr_ref
11190 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
11191 			     enum var_init_status initialized)
11192 {
11193   int size, i;
11194   dw_loc_descr_ref loc_result = NULL;
11195 
11196   /* Simple, contiguous registers.  */
11197   if (regs == NULL_RTX)
11198     {
11199       unsigned reg = REGNO (rtl);
11200       int nregs;
11201 
11202 #ifdef LEAF_REG_REMAP
11203       if (crtl->uses_only_leaf_regs)
11204 	{
11205 	  int leaf_reg = LEAF_REG_REMAP (reg);
11206 	  if (leaf_reg != -1)
11207 	    reg = (unsigned) leaf_reg;
11208 	}
11209 #endif
11210 
11211       gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
11212       nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
11213 
11214       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
11215 
11216       loc_result = NULL;
11217       while (nregs--)
11218 	{
11219 	  dw_loc_descr_ref t;
11220 
11221 	  t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
11222 				      VAR_INIT_STATUS_INITIALIZED);
11223 	  add_loc_descr (&loc_result, t);
11224 	  add_loc_descr_op_piece (&loc_result, size);
11225 	  ++reg;
11226 	}
11227       return loc_result;
11228     }
11229 
11230   /* Now onto stupid register sets in non contiguous locations.  */
11231 
11232   gcc_assert (GET_CODE (regs) == PARALLEL);
11233 
11234   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
11235   loc_result = NULL;
11236 
11237   for (i = 0; i < XVECLEN (regs, 0); ++i)
11238     {
11239       dw_loc_descr_ref t;
11240 
11241       t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
11242 				  VAR_INIT_STATUS_INITIALIZED);
11243       add_loc_descr (&loc_result, t);
11244       add_loc_descr_op_piece (&loc_result, size);
11245     }
11246 
11247   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11248     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11249   return loc_result;
11250 }
11251 
11252 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
11253 
11254 /* Return a location descriptor that designates a constant i,
11255    as a compound operation from constant (i >> shift), constant shift
11256    and DW_OP_shl.  */
11257 
11258 static dw_loc_descr_ref
11259 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11260 {
11261   dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
11262   add_loc_descr (&ret, int_loc_descriptor (shift));
11263   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11264   return ret;
11265 }
11266 
11267 /* Return a location descriptor that designates a constant.  */
11268 
11269 static dw_loc_descr_ref
11270 int_loc_descriptor (HOST_WIDE_INT i)
11271 {
11272   enum dwarf_location_atom op;
11273 
11274   /* Pick the smallest representation of a constant, rather than just
11275      defaulting to the LEB encoding.  */
11276   if (i >= 0)
11277     {
11278       int clz = clz_hwi (i);
11279       int ctz = ctz_hwi (i);
11280       if (i <= 31)
11281 	op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
11282       else if (i <= 0xff)
11283 	op = DW_OP_const1u;
11284       else if (i <= 0xffff)
11285 	op = DW_OP_const2u;
11286       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11287 	       && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11288 	/* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
11289 	   DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
11290 	   while DW_OP_const4u is 5 bytes.  */
11291 	return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
11292       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11293 	       && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11294 	/* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
11295 	   while DW_OP_const4u is 5 bytes.  */
11296 	return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11297       else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11298 	op = DW_OP_const4u;
11299       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11300 	       && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11301 	/* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
11302 	   while DW_OP_constu of constant >= 0x100000000 takes at least
11303 	   6 bytes.  */
11304 	return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11305       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11306 	       && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
11307 		  >= HOST_BITS_PER_WIDE_INT)
11308 	/* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
11309 	   DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
11310 	   while DW_OP_constu takes in this case at least 6 bytes.  */
11311 	return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
11312       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11313 	       && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11314 	       && size_of_uleb128 (i) > 6)
11315 	/* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes.  */
11316 	return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
11317       else
11318 	op = DW_OP_constu;
11319     }
11320   else
11321     {
11322       if (i >= -0x80)
11323 	op = DW_OP_const1s;
11324       else if (i >= -0x8000)
11325 	op = DW_OP_const2s;
11326       else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11327 	{
11328 	  if (size_of_int_loc_descriptor (i) < 5)
11329 	    {
11330 	      dw_loc_descr_ref ret = int_loc_descriptor (-i);
11331 	      add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11332 	      return ret;
11333 	    }
11334 	  op = DW_OP_const4s;
11335 	}
11336       else
11337 	{
11338 	  if (size_of_int_loc_descriptor (i)
11339 	      < (unsigned long) 1 + size_of_sleb128 (i))
11340 	    {
11341 	      dw_loc_descr_ref ret = int_loc_descriptor (-i);
11342 	      add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11343 	      return ret;
11344 	    }
11345 	  op = DW_OP_consts;
11346 	}
11347     }
11348 
11349   return new_loc_descr (op, i, 0);
11350 }
11351 
11352 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
11353    without actually allocating it.  */
11354 
11355 static unsigned long
11356 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11357 {
11358   return size_of_int_loc_descriptor (i >> shift)
11359 	 + size_of_int_loc_descriptor (shift)
11360 	 + 1;
11361 }
11362 
11363 /* Return size_of_locs (int_loc_descriptor (i)) without
11364    actually allocating it.  */
11365 
11366 static unsigned long
11367 size_of_int_loc_descriptor (HOST_WIDE_INT i)
11368 {
11369   unsigned long s;
11370 
11371   if (i >= 0)
11372     {
11373       int clz, ctz;
11374       if (i <= 31)
11375 	return 1;
11376       else if (i <= 0xff)
11377 	return 2;
11378       else if (i <= 0xffff)
11379 	return 3;
11380       clz = clz_hwi (i);
11381       ctz = ctz_hwi (i);
11382       if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11383 	  && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11384 	return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11385 						    - clz - 5);
11386       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11387 	       && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11388 	return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11389 						    - clz - 8);
11390       else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11391 	return 5;
11392       s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
11393       if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11394 	  && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11395 	return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11396 						    - clz - 8);
11397       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11398 	       && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
11399 	return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11400 						    - clz - 16);
11401       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11402 	       && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11403 	       && s > 6)
11404 	return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11405 						    - clz - 32);
11406       else
11407 	return 1 + s;
11408     }
11409   else
11410     {
11411       if (i >= -0x80)
11412 	return 2;
11413       else if (i >= -0x8000)
11414 	return 3;
11415       else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11416 	{
11417 	  if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11418 	    {
11419 	      s = size_of_int_loc_descriptor (-i) + 1;
11420 	      if (s < 5)
11421 		return s;
11422 	    }
11423 	  return 5;
11424 	}
11425       else
11426 	{
11427 	  unsigned long r = 1 + size_of_sleb128 (i);
11428 	  if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11429 	    {
11430 	      s = size_of_int_loc_descriptor (-i) + 1;
11431 	      if (s < r)
11432 		return s;
11433 	    }
11434 	  return r;
11435 	}
11436     }
11437 }
11438 
11439 /* Return loc description representing "address" of integer value.
11440    This can appear only as toplevel expression.  */
11441 
11442 static dw_loc_descr_ref
11443 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
11444 {
11445   int litsize;
11446   dw_loc_descr_ref loc_result = NULL;
11447 
11448   if (!(dwarf_version >= 4 || !dwarf_strict))
11449     return NULL;
11450 
11451   litsize = size_of_int_loc_descriptor (i);
11452   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
11453      is more compact.  For DW_OP_stack_value we need:
11454      litsize + 1 (DW_OP_stack_value)
11455      and for DW_OP_implicit_value:
11456      1 (DW_OP_implicit_value) + 1 (length) + size.  */
11457   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
11458     {
11459       loc_result = int_loc_descriptor (i);
11460       add_loc_descr (&loc_result,
11461 		     new_loc_descr (DW_OP_stack_value, 0, 0));
11462       return loc_result;
11463     }
11464 
11465   loc_result = new_loc_descr (DW_OP_implicit_value,
11466 			      size, 0);
11467   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11468   loc_result->dw_loc_oprnd2.v.val_int = i;
11469   return loc_result;
11470 }
11471 
11472 /* Return a location descriptor that designates a base+offset location.  */
11473 
11474 static dw_loc_descr_ref
11475 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
11476 		 enum var_init_status initialized)
11477 {
11478   unsigned int regno;
11479   dw_loc_descr_ref result;
11480   dw_fde_ref fde = cfun->fde;
11481 
11482   /* We only use "frame base" when we're sure we're talking about the
11483      post-prologue local stack frame.  We do this by *not* running
11484      register elimination until this point, and recognizing the special
11485      argument pointer and soft frame pointer rtx's.  */
11486   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
11487     {
11488       rtx elim = (ira_use_lra_p
11489 		  ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
11490 		  : eliminate_regs (reg, VOIDmode, NULL_RTX));
11491 
11492       if (elim != reg)
11493 	{
11494 	  if (GET_CODE (elim) == PLUS)
11495 	    {
11496 	      offset += INTVAL (XEXP (elim, 1));
11497 	      elim = XEXP (elim, 0);
11498 	    }
11499 	  gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11500 		       && (elim == hard_frame_pointer_rtx
11501 			   || elim == stack_pointer_rtx))
11502 	              || elim == (frame_pointer_needed
11503 				  ? hard_frame_pointer_rtx
11504 				  : stack_pointer_rtx));
11505 
11506 	  /* If drap register is used to align stack, use frame
11507 	     pointer + offset to access stack variables.  If stack
11508 	     is aligned without drap, use stack pointer + offset to
11509 	     access stack variables.  */
11510 	  if (crtl->stack_realign_tried
11511 	      && reg == frame_pointer_rtx)
11512 	    {
11513 	      int base_reg
11514 		= DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
11515 				      ? HARD_FRAME_POINTER_REGNUM
11516 				      : REGNO (elim));
11517 	      return new_reg_loc_descr (base_reg, offset);
11518 	    }
11519 
11520 	  gcc_assert (frame_pointer_fb_offset_valid);
11521 	  offset += frame_pointer_fb_offset;
11522 	  return new_loc_descr (DW_OP_fbreg, offset, 0);
11523 	}
11524     }
11525 
11526   regno = REGNO (reg);
11527 #ifdef LEAF_REG_REMAP
11528   if (crtl->uses_only_leaf_regs)
11529     {
11530       int leaf_reg = LEAF_REG_REMAP (regno);
11531       if (leaf_reg != -1)
11532 	regno = (unsigned) leaf_reg;
11533     }
11534 #endif
11535   regno = DWARF_FRAME_REGNUM (regno);
11536 
11537   if (!optimize && fde
11538       && (fde->drap_reg == regno || fde->vdrap_reg == regno))
11539     {
11540       /* Use cfa+offset to represent the location of arguments passed
11541 	 on the stack when drap is used to align stack.
11542 	 Only do this when not optimizing, for optimized code var-tracking
11543 	 is supposed to track where the arguments live and the register
11544 	 used as vdrap or drap in some spot might be used for something
11545 	 else in other part of the routine.  */
11546       return new_loc_descr (DW_OP_fbreg, offset, 0);
11547     }
11548 
11549   if (regno <= 31)
11550     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11551 			    offset, 0);
11552   else
11553     result = new_loc_descr (DW_OP_bregx, regno, offset);
11554 
11555   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11556     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11557 
11558   return result;
11559 }
11560 
11561 /* Return true if this RTL expression describes a base+offset calculation.  */
11562 
11563 static inline int
11564 is_based_loc (const_rtx rtl)
11565 {
11566   return (GET_CODE (rtl) == PLUS
11567 	  && ((REG_P (XEXP (rtl, 0))
11568 	       && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11569 	       && CONST_INT_P (XEXP (rtl, 1)))));
11570 }
11571 
11572 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11573    failed.  */
11574 
11575 static dw_loc_descr_ref
11576 tls_mem_loc_descriptor (rtx mem)
11577 {
11578   tree base;
11579   dw_loc_descr_ref loc_result;
11580 
11581   if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11582     return NULL;
11583 
11584   base = get_base_address (MEM_EXPR (mem));
11585   if (base == NULL
11586       || TREE_CODE (base) != VAR_DECL
11587       || !DECL_THREAD_LOCAL_P (base))
11588     return NULL;
11589 
11590   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
11591   if (loc_result == NULL)
11592     return NULL;
11593 
11594   if (MEM_OFFSET (mem))
11595     loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11596 
11597   return loc_result;
11598 }
11599 
11600 /* Output debug info about reason why we failed to expand expression as dwarf
11601    expression.  */
11602 
11603 static void
11604 expansion_failed (tree expr, rtx rtl, char const *reason)
11605 {
11606   if (dump_file && (dump_flags & TDF_DETAILS))
11607     {
11608       fprintf (dump_file, "Failed to expand as dwarf: ");
11609       if (expr)
11610 	print_generic_expr (dump_file, expr, dump_flags);
11611       if (rtl)
11612 	{
11613 	  fprintf (dump_file, "\n");
11614 	  print_rtl (dump_file, rtl);
11615 	}
11616       fprintf (dump_file, "\nReason: %s\n", reason);
11617     }
11618 }
11619 
11620 /* Helper function for const_ok_for_output.  */
11621 
11622 static bool
11623 const_ok_for_output_1 (rtx rtl)
11624 {
11625   if (GET_CODE (rtl) == UNSPEC)
11626     {
11627       /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11628 	 we can't express it in the debug info.  */
11629 #ifdef ENABLE_CHECKING
11630       /* Don't complain about TLS UNSPECs, those are just too hard to
11631 	 delegitimize.  Note this could be a non-decl SYMBOL_REF such as
11632 	 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11633 	 rather than DECL_THREAD_LOCAL_P is not just an optimization.  */
11634       if (XVECLEN (rtl, 0) == 0
11635 	  || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11636 	  || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11637 	inform (current_function_decl
11638 		? DECL_SOURCE_LOCATION (current_function_decl)
11639 		: UNKNOWN_LOCATION,
11640 #if NUM_UNSPEC_VALUES > 0
11641 		"non-delegitimized UNSPEC %s (%d) found in variable location",
11642 		((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11643 		 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11644 		XINT (rtl, 1));
11645 #else
11646 		"non-delegitimized UNSPEC %d found in variable location",
11647 		XINT (rtl, 1));
11648 #endif
11649 #endif
11650       expansion_failed (NULL_TREE, rtl,
11651 			"UNSPEC hasn't been delegitimized.\n");
11652       return false;
11653     }
11654 
11655   if (targetm.const_not_ok_for_debug_p (rtl))
11656     {
11657       expansion_failed (NULL_TREE, rtl,
11658 			"Expression rejected for debug by the backend.\n");
11659       return false;
11660     }
11661 
11662   /* FIXME: Refer to PR60655. It is possible for simplification
11663      of rtl expressions in var tracking to produce such expressions.
11664      We should really identify / validate expressions
11665      enclosed in CONST that can be handled by assemblers on various
11666      targets and only handle legitimate cases here.  */
11667   if (GET_CODE (rtl) != SYMBOL_REF)
11668     {
11669       if (GET_CODE (rtl) == NOT)
11670 	return false;
11671       return true;
11672     }
11673 
11674   if (CONSTANT_POOL_ADDRESS_P (rtl))
11675     {
11676       bool marked;
11677       get_pool_constant_mark (rtl, &marked);
11678       /* If all references to this pool constant were optimized away,
11679 	 it was not output and thus we can't represent it.  */
11680       if (!marked)
11681 	{
11682 	  expansion_failed (NULL_TREE, rtl,
11683 			    "Constant was removed from constant pool.\n");
11684 	  return false;
11685 	}
11686     }
11687 
11688   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11689     return false;
11690 
11691   /* Avoid references to external symbols in debug info, on several targets
11692      the linker might even refuse to link when linking a shared library,
11693      and in many other cases the relocations for .debug_info/.debug_loc are
11694      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
11695      to be defined within the same shared library or executable are fine.  */
11696   if (SYMBOL_REF_EXTERNAL_P (rtl))
11697     {
11698       tree decl = SYMBOL_REF_DECL (rtl);
11699 
11700       if (decl == NULL || !targetm.binds_local_p (decl))
11701 	{
11702 	  expansion_failed (NULL_TREE, rtl,
11703 			    "Symbol not defined in current TU.\n");
11704 	  return false;
11705 	}
11706     }
11707 
11708   return true;
11709 }
11710 
11711 /* Return true if constant RTL can be emitted in DW_OP_addr or
11712    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
11713    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
11714 
11715 static bool
11716 const_ok_for_output (rtx rtl)
11717 {
11718   if (GET_CODE (rtl) == SYMBOL_REF)
11719     return const_ok_for_output_1 (rtl);
11720 
11721   if (GET_CODE (rtl) == CONST)
11722     {
11723       subrtx_var_iterator::array_type array;
11724       FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
11725 	if (!const_ok_for_output_1 (*iter))
11726 	  return false;
11727       return true;
11728     }
11729 
11730   return true;
11731 }
11732 
11733 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11734    if possible, NULL otherwise.  */
11735 
11736 static dw_die_ref
11737 base_type_for_mode (machine_mode mode, bool unsignedp)
11738 {
11739   dw_die_ref type_die;
11740   tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11741 
11742   if (type == NULL)
11743     return NULL;
11744   switch (TREE_CODE (type))
11745     {
11746     case INTEGER_TYPE:
11747     case REAL_TYPE:
11748       break;
11749     default:
11750       return NULL;
11751     }
11752   type_die = lookup_type_die (type);
11753   if (!type_die)
11754     type_die = modified_type_die (type, TYPE_UNQUALIFIED, comp_unit_die ());
11755   if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11756     return NULL;
11757   return type_die;
11758 }
11759 
11760 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11761    type matching MODE, or, if MODE is narrower than or as wide as
11762    DWARF2_ADDR_SIZE, untyped.  Return NULL if the conversion is not
11763    possible.  */
11764 
11765 static dw_loc_descr_ref
11766 convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
11767 {
11768   machine_mode outer_mode = mode;
11769   dw_die_ref type_die;
11770   dw_loc_descr_ref cvt;
11771 
11772   if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11773     {
11774       add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11775       return op;
11776     }
11777   type_die = base_type_for_mode (outer_mode, 1);
11778   if (type_die == NULL)
11779     return NULL;
11780   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11781   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11782   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11783   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11784   add_loc_descr (&op, cvt);
11785   return op;
11786 }
11787 
11788 /* Return location descriptor for comparison OP with operands OP0 and OP1.  */
11789 
11790 static dw_loc_descr_ref
11791 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11792 			dw_loc_descr_ref op1)
11793 {
11794   dw_loc_descr_ref ret = op0;
11795   add_loc_descr (&ret, op1);
11796   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11797   if (STORE_FLAG_VALUE != 1)
11798     {
11799       add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11800       add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11801     }
11802   return ret;
11803 }
11804 
11805 /* Return location descriptor for signed comparison OP RTL.  */
11806 
11807 static dw_loc_descr_ref
11808 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11809 			 machine_mode mem_mode)
11810 {
11811   machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11812   dw_loc_descr_ref op0, op1;
11813   int shift;
11814 
11815   if (op_mode == VOIDmode)
11816     op_mode = GET_MODE (XEXP (rtl, 1));
11817   if (op_mode == VOIDmode)
11818     return NULL;
11819 
11820   if (dwarf_strict
11821       && (GET_MODE_CLASS (op_mode) != MODE_INT
11822 	  || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11823     return NULL;
11824 
11825   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11826 			    VAR_INIT_STATUS_INITIALIZED);
11827   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11828 			    VAR_INIT_STATUS_INITIALIZED);
11829 
11830   if (op0 == NULL || op1 == NULL)
11831     return NULL;
11832 
11833   if (GET_MODE_CLASS (op_mode) != MODE_INT
11834       || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11835     return compare_loc_descriptor (op, op0, op1);
11836 
11837   if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11838     {
11839       dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11840       dw_loc_descr_ref cvt;
11841 
11842       if (type_die == NULL)
11843 	return NULL;
11844       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11845       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11846       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11847       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11848       add_loc_descr (&op0, cvt);
11849       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11850       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11851       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11852       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11853       add_loc_descr (&op1, cvt);
11854       return compare_loc_descriptor (op, op0, op1);
11855     }
11856 
11857   shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11858   /* For eq/ne, if the operands are known to be zero-extended,
11859      there is no need to do the fancy shifting up.  */
11860   if (op == DW_OP_eq || op == DW_OP_ne)
11861     {
11862       dw_loc_descr_ref last0, last1;
11863       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11864 	;
11865       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11866 	;
11867       /* deref_size zero extends, and for constants we can check
11868 	 whether they are zero extended or not.  */
11869       if (((last0->dw_loc_opc == DW_OP_deref_size
11870 	    && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11871 	   || (CONST_INT_P (XEXP (rtl, 0))
11872 	       && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11873 		  == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11874 	  && ((last1->dw_loc_opc == DW_OP_deref_size
11875 	       && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11876 	      || (CONST_INT_P (XEXP (rtl, 1))
11877 		  && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11878 		     == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11879 	return compare_loc_descriptor (op, op0, op1);
11880 
11881       /* EQ/NE comparison against constant in narrower type than
11882 	 DWARF2_ADDR_SIZE can be performed either as
11883 	 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11884 	 DW_OP_{eq,ne}
11885 	 or
11886 	 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11887 	 DW_OP_{eq,ne}.  Pick whatever is shorter.  */
11888       if (CONST_INT_P (XEXP (rtl, 1))
11889 	  && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11890 	  && (size_of_int_loc_descriptor (shift) + 1
11891 	      + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11892 	      >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11893 		 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11894 					       & GET_MODE_MASK (op_mode))))
11895 	{
11896 	  add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11897 	  add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11898 	  op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11899 				    & GET_MODE_MASK (op_mode));
11900 	  return compare_loc_descriptor (op, op0, op1);
11901 	}
11902     }
11903   add_loc_descr (&op0, int_loc_descriptor (shift));
11904   add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11905   if (CONST_INT_P (XEXP (rtl, 1)))
11906     op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11907   else
11908     {
11909       add_loc_descr (&op1, int_loc_descriptor (shift));
11910       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11911     }
11912   return compare_loc_descriptor (op, op0, op1);
11913 }
11914 
11915 /* Return location descriptor for unsigned comparison OP RTL.  */
11916 
11917 static dw_loc_descr_ref
11918 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11919 			 machine_mode mem_mode)
11920 {
11921   machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11922   dw_loc_descr_ref op0, op1;
11923 
11924   if (op_mode == VOIDmode)
11925     op_mode = GET_MODE (XEXP (rtl, 1));
11926   if (op_mode == VOIDmode)
11927     return NULL;
11928   if (GET_MODE_CLASS (op_mode) != MODE_INT)
11929     return NULL;
11930 
11931   if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11932     return NULL;
11933 
11934   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11935 			    VAR_INIT_STATUS_INITIALIZED);
11936   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11937 			    VAR_INIT_STATUS_INITIALIZED);
11938 
11939   if (op0 == NULL || op1 == NULL)
11940     return NULL;
11941 
11942   if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
11943     {
11944       HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
11945       dw_loc_descr_ref last0, last1;
11946       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11947 	;
11948       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11949 	;
11950       if (CONST_INT_P (XEXP (rtl, 0)))
11951 	op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
11952       /* deref_size zero extends, so no need to mask it again.  */
11953       else if (last0->dw_loc_opc != DW_OP_deref_size
11954 	       || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11955 	{
11956 	  add_loc_descr (&op0, int_loc_descriptor (mask));
11957 	  add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11958 	}
11959       if (CONST_INT_P (XEXP (rtl, 1)))
11960 	op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11961       /* deref_size zero extends, so no need to mask it again.  */
11962       else if (last1->dw_loc_opc != DW_OP_deref_size
11963 	       || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11964 	{
11965 	  add_loc_descr (&op1, int_loc_descriptor (mask));
11966 	  add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11967 	}
11968     }
11969   else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11970     {
11971       HOST_WIDE_INT bias = 1;
11972       bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11973       add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11974       if (CONST_INT_P (XEXP (rtl, 1)))
11975 	op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11976 				  + INTVAL (XEXP (rtl, 1)));
11977       else
11978 	add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
11979 					    bias, 0));
11980     }
11981   return compare_loc_descriptor (op, op0, op1);
11982 }
11983 
11984 /* Return location descriptor for {U,S}{MIN,MAX}.  */
11985 
11986 static dw_loc_descr_ref
11987 minmax_loc_descriptor (rtx rtl, machine_mode mode,
11988 		       machine_mode mem_mode)
11989 {
11990   enum dwarf_location_atom op;
11991   dw_loc_descr_ref op0, op1, ret;
11992   dw_loc_descr_ref bra_node, drop_node;
11993 
11994   if (dwarf_strict
11995       && (GET_MODE_CLASS (mode) != MODE_INT
11996 	  || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
11997     return NULL;
11998 
11999   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12000 			    VAR_INIT_STATUS_INITIALIZED);
12001   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12002 			    VAR_INIT_STATUS_INITIALIZED);
12003 
12004   if (op0 == NULL || op1 == NULL)
12005     return NULL;
12006 
12007   add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
12008   add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
12009   add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
12010   if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
12011     {
12012       if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12013 	{
12014 	  HOST_WIDE_INT mask = GET_MODE_MASK (mode);
12015 	  add_loc_descr (&op0, int_loc_descriptor (mask));
12016 	  add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12017 	  add_loc_descr (&op1, int_loc_descriptor (mask));
12018 	  add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12019 	}
12020       else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12021 	{
12022 	  HOST_WIDE_INT bias = 1;
12023 	  bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12024 	  add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12025 	  add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12026 	}
12027     }
12028   else if (GET_MODE_CLASS (mode) == MODE_INT
12029 	   && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12030     {
12031       int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
12032       add_loc_descr (&op0, int_loc_descriptor (shift));
12033       add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
12034       add_loc_descr (&op1, int_loc_descriptor (shift));
12035       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
12036     }
12037   else if (GET_MODE_CLASS (mode) == MODE_INT
12038 	   && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12039     {
12040       dw_die_ref type_die = base_type_for_mode (mode, 0);
12041       dw_loc_descr_ref cvt;
12042       if (type_die == NULL)
12043 	return NULL;
12044       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12045       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12046       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12047       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12048       add_loc_descr (&op0, cvt);
12049       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12050       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12051       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12052       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12053       add_loc_descr (&op1, cvt);
12054     }
12055 
12056   if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
12057     op = DW_OP_lt;
12058   else
12059     op = DW_OP_gt;
12060   ret = op0;
12061   add_loc_descr (&ret, op1);
12062   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12063   bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12064   add_loc_descr (&ret, bra_node);
12065   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12066   drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12067   add_loc_descr (&ret, drop_node);
12068   bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12069   bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12070   if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
12071       && GET_MODE_CLASS (mode) == MODE_INT
12072       && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12073     ret = convert_descriptor_to_mode (mode, ret);
12074   return ret;
12075 }
12076 
12077 /* Helper function for mem_loc_descriptor.  Perform OP binary op,
12078    but after converting arguments to type_die, afterwards
12079    convert back to unsigned.  */
12080 
12081 static dw_loc_descr_ref
12082 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
12083 	     machine_mode mode, machine_mode mem_mode)
12084 {
12085   dw_loc_descr_ref cvt, op0, op1;
12086 
12087   if (type_die == NULL)
12088     return NULL;
12089   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12090 			    VAR_INIT_STATUS_INITIALIZED);
12091   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12092 			    VAR_INIT_STATUS_INITIALIZED);
12093   if (op0 == NULL || op1 == NULL)
12094     return NULL;
12095   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12096   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12097   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12098   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12099   add_loc_descr (&op0, cvt);
12100   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12101   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12102   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12103   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12104   add_loc_descr (&op1, cvt);
12105   add_loc_descr (&op0, op1);
12106   add_loc_descr (&op0, new_loc_descr (op, 0, 0));
12107   return convert_descriptor_to_mode (mode, op0);
12108 }
12109 
12110 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
12111    const0 is DW_OP_lit0 or corresponding typed constant,
12112    const1 is DW_OP_lit1 or corresponding typed constant
12113    and constMSB is constant with just the MSB bit set
12114    for the mode):
12115        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12116    L1: const0 DW_OP_swap
12117    L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
12118        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12119    L3: DW_OP_drop
12120    L4: DW_OP_nop
12121 
12122    CTZ is similar:
12123        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12124    L1: const0 DW_OP_swap
12125    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12126        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12127    L3: DW_OP_drop
12128    L4: DW_OP_nop
12129 
12130    FFS is similar:
12131        DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
12132    L1: const1 DW_OP_swap
12133    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12134        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12135    L3: DW_OP_drop
12136    L4: DW_OP_nop  */
12137 
12138 static dw_loc_descr_ref
12139 clz_loc_descriptor (rtx rtl, machine_mode mode,
12140 		    machine_mode mem_mode)
12141 {
12142   dw_loc_descr_ref op0, ret, tmp;
12143   HOST_WIDE_INT valv;
12144   dw_loc_descr_ref l1jump, l1label;
12145   dw_loc_descr_ref l2jump, l2label;
12146   dw_loc_descr_ref l3jump, l3label;
12147   dw_loc_descr_ref l4jump, l4label;
12148   rtx msb;
12149 
12150   if (GET_MODE_CLASS (mode) != MODE_INT
12151       || GET_MODE (XEXP (rtl, 0)) != mode)
12152     return NULL;
12153 
12154   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12155 			    VAR_INIT_STATUS_INITIALIZED);
12156   if (op0 == NULL)
12157     return NULL;
12158   ret = op0;
12159   if (GET_CODE (rtl) == CLZ)
12160     {
12161       if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12162 	valv = GET_MODE_BITSIZE (mode);
12163     }
12164   else if (GET_CODE (rtl) == FFS)
12165     valv = 0;
12166   else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12167     valv = GET_MODE_BITSIZE (mode);
12168   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12169   l1jump = new_loc_descr (DW_OP_bra, 0, 0);
12170   add_loc_descr (&ret, l1jump);
12171   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12172   tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
12173 			    VAR_INIT_STATUS_INITIALIZED);
12174   if (tmp == NULL)
12175     return NULL;
12176   add_loc_descr (&ret, tmp);
12177   l4jump = new_loc_descr (DW_OP_skip, 0, 0);
12178   add_loc_descr (&ret, l4jump);
12179   l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
12180 				? const1_rtx : const0_rtx,
12181 				mode, mem_mode,
12182 				VAR_INIT_STATUS_INITIALIZED);
12183   if (l1label == NULL)
12184     return NULL;
12185   add_loc_descr (&ret, l1label);
12186   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12187   l2label = new_loc_descr (DW_OP_dup, 0, 0);
12188   add_loc_descr (&ret, l2label);
12189   if (GET_CODE (rtl) != CLZ)
12190     msb = const1_rtx;
12191   else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
12192     msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
12193 		   << (GET_MODE_BITSIZE (mode) - 1));
12194   else
12195     msb = immed_wide_int_const
12196       (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
12197 			    GET_MODE_PRECISION (mode)), mode);
12198   if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
12199     tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12200 			 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
12201 			 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
12202   else
12203     tmp = mem_loc_descriptor (msb, mode, mem_mode,
12204 			      VAR_INIT_STATUS_INITIALIZED);
12205   if (tmp == NULL)
12206     return NULL;
12207   add_loc_descr (&ret, tmp);
12208   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12209   l3jump = new_loc_descr (DW_OP_bra, 0, 0);
12210   add_loc_descr (&ret, l3jump);
12211   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12212 			    VAR_INIT_STATUS_INITIALIZED);
12213   if (tmp == NULL)
12214     return NULL;
12215   add_loc_descr (&ret, tmp);
12216   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
12217 				      ? DW_OP_shl : DW_OP_shr, 0, 0));
12218   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12219   add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
12220   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12221   l2jump = new_loc_descr (DW_OP_skip, 0, 0);
12222   add_loc_descr (&ret, l2jump);
12223   l3label = new_loc_descr (DW_OP_drop, 0, 0);
12224   add_loc_descr (&ret, l3label);
12225   l4label = new_loc_descr (DW_OP_nop, 0, 0);
12226   add_loc_descr (&ret, l4label);
12227   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12228   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12229   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12230   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12231   l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12232   l3jump->dw_loc_oprnd1.v.val_loc = l3label;
12233   l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12234   l4jump->dw_loc_oprnd1.v.val_loc = l4label;
12235   return ret;
12236 }
12237 
12238 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
12239    const1 is DW_OP_lit1 or corresponding typed constant):
12240        const0 DW_OP_swap
12241    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12242        DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12243    L2: DW_OP_drop
12244 
12245    PARITY is similar:
12246    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12247        DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12248    L2: DW_OP_drop  */
12249 
12250 static dw_loc_descr_ref
12251 popcount_loc_descriptor (rtx rtl, machine_mode mode,
12252 			 machine_mode mem_mode)
12253 {
12254   dw_loc_descr_ref op0, ret, tmp;
12255   dw_loc_descr_ref l1jump, l1label;
12256   dw_loc_descr_ref l2jump, l2label;
12257 
12258   if (GET_MODE_CLASS (mode) != MODE_INT
12259       || GET_MODE (XEXP (rtl, 0)) != mode)
12260     return NULL;
12261 
12262   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12263 			    VAR_INIT_STATUS_INITIALIZED);
12264   if (op0 == NULL)
12265     return NULL;
12266   ret = op0;
12267   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12268 			    VAR_INIT_STATUS_INITIALIZED);
12269   if (tmp == NULL)
12270     return NULL;
12271   add_loc_descr (&ret, tmp);
12272   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12273   l1label = new_loc_descr (DW_OP_dup, 0, 0);
12274   add_loc_descr (&ret, l1label);
12275   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12276   add_loc_descr (&ret, l2jump);
12277   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12278   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12279   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12280 			    VAR_INIT_STATUS_INITIALIZED);
12281   if (tmp == NULL)
12282     return NULL;
12283   add_loc_descr (&ret, tmp);
12284   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12285   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
12286 				      ? DW_OP_plus : DW_OP_xor, 0, 0));
12287   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12288   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12289 			    VAR_INIT_STATUS_INITIALIZED);
12290   add_loc_descr (&ret, tmp);
12291   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12292   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12293   add_loc_descr (&ret, l1jump);
12294   l2label = new_loc_descr (DW_OP_drop, 0, 0);
12295   add_loc_descr (&ret, l2label);
12296   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12297   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12298   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12299   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12300   return ret;
12301 }
12302 
12303 /* BSWAP (constS is initial shift count, either 56 or 24):
12304        constS const0
12305    L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
12306        const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
12307        DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
12308        DW_OP_minus DW_OP_swap DW_OP_skip <L1>
12309    L2: DW_OP_drop DW_OP_swap DW_OP_drop  */
12310 
12311 static dw_loc_descr_ref
12312 bswap_loc_descriptor (rtx rtl, machine_mode mode,
12313 		      machine_mode mem_mode)
12314 {
12315   dw_loc_descr_ref op0, ret, tmp;
12316   dw_loc_descr_ref l1jump, l1label;
12317   dw_loc_descr_ref l2jump, l2label;
12318 
12319   if (GET_MODE_CLASS (mode) != MODE_INT
12320       || BITS_PER_UNIT != 8
12321       || (GET_MODE_BITSIZE (mode) != 32
12322 	  &&  GET_MODE_BITSIZE (mode) != 64))
12323     return NULL;
12324 
12325   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12326 			    VAR_INIT_STATUS_INITIALIZED);
12327   if (op0 == NULL)
12328     return NULL;
12329 
12330   ret = op0;
12331   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12332 			    mode, mem_mode,
12333 			    VAR_INIT_STATUS_INITIALIZED);
12334   if (tmp == NULL)
12335     return NULL;
12336   add_loc_descr (&ret, tmp);
12337   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12338 			    VAR_INIT_STATUS_INITIALIZED);
12339   if (tmp == NULL)
12340     return NULL;
12341   add_loc_descr (&ret, tmp);
12342   l1label = new_loc_descr (DW_OP_pick, 2, 0);
12343   add_loc_descr (&ret, l1label);
12344   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12345 			    mode, mem_mode,
12346 			    VAR_INIT_STATUS_INITIALIZED);
12347   add_loc_descr (&ret, tmp);
12348   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
12349   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12350   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12351   tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
12352 			    VAR_INIT_STATUS_INITIALIZED);
12353   if (tmp == NULL)
12354     return NULL;
12355   add_loc_descr (&ret, tmp);
12356   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12357   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
12358   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12359   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12360   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12361   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12362   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12363 			    VAR_INIT_STATUS_INITIALIZED);
12364   add_loc_descr (&ret, tmp);
12365   add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
12366   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12367   add_loc_descr (&ret, l2jump);
12368   tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
12369 			    VAR_INIT_STATUS_INITIALIZED);
12370   add_loc_descr (&ret, tmp);
12371   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12372   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12373   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12374   add_loc_descr (&ret, l1jump);
12375   l2label = new_loc_descr (DW_OP_drop, 0, 0);
12376   add_loc_descr (&ret, l2label);
12377   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12378   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12379   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12380   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12381   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12382   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12383   return ret;
12384 }
12385 
12386 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
12387    DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12388    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
12389    DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
12390 
12391    ROTATERT is similar:
12392    DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
12393    DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12394    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or  */
12395 
12396 static dw_loc_descr_ref
12397 rotate_loc_descriptor (rtx rtl, machine_mode mode,
12398 		       machine_mode mem_mode)
12399 {
12400   rtx rtlop1 = XEXP (rtl, 1);
12401   dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
12402   int i;
12403 
12404   if (GET_MODE_CLASS (mode) != MODE_INT)
12405     return NULL;
12406 
12407   if (GET_MODE (rtlop1) != VOIDmode
12408       && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
12409     rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12410   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12411 			    VAR_INIT_STATUS_INITIALIZED);
12412   op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12413 			    VAR_INIT_STATUS_INITIALIZED);
12414   if (op0 == NULL || op1 == NULL)
12415     return NULL;
12416   if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12417     for (i = 0; i < 2; i++)
12418       {
12419 	if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
12420 	  mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
12421 					mode, mem_mode,
12422 					VAR_INIT_STATUS_INITIALIZED);
12423 	else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12424 	  mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12425 				   ? DW_OP_const4u
12426 				   : HOST_BITS_PER_WIDE_INT == 64
12427 				   ? DW_OP_const8u : DW_OP_constu,
12428 				   GET_MODE_MASK (mode), 0);
12429 	else
12430 	  mask[i] = NULL;
12431 	if (mask[i] == NULL)
12432 	  return NULL;
12433 	add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
12434       }
12435   ret = op0;
12436   add_loc_descr (&ret, op1);
12437   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12438   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12439   if (GET_CODE (rtl) == ROTATERT)
12440     {
12441       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12442       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12443 					  GET_MODE_BITSIZE (mode), 0));
12444     }
12445   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12446   if (mask[0] != NULL)
12447     add_loc_descr (&ret, mask[0]);
12448   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12449   if (mask[1] != NULL)
12450     {
12451       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12452       add_loc_descr (&ret, mask[1]);
12453       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12454     }
12455   if (GET_CODE (rtl) == ROTATE)
12456     {
12457       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12458       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12459 					  GET_MODE_BITSIZE (mode), 0));
12460     }
12461   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12462   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12463   return ret;
12464 }
12465 
12466 /* Helper function for mem_loc_descriptor.  Return DW_OP_GNU_parameter_ref
12467    for DEBUG_PARAMETER_REF RTL.  */
12468 
12469 static dw_loc_descr_ref
12470 parameter_ref_descriptor (rtx rtl)
12471 {
12472   dw_loc_descr_ref ret;
12473   dw_die_ref ref;
12474 
12475   if (dwarf_strict)
12476     return NULL;
12477   gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
12478   ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
12479   ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
12480   if (ref)
12481     {
12482       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12483       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12484       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12485     }
12486   else
12487     {
12488       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12489       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
12490     }
12491   return ret;
12492 }
12493 
12494 /* The following routine converts the RTL for a variable or parameter
12495    (resident in memory) into an equivalent Dwarf representation of a
12496    mechanism for getting the address of that same variable onto the top of a
12497    hypothetical "address evaluation" stack.
12498 
12499    When creating memory location descriptors, we are effectively transforming
12500    the RTL for a memory-resident object into its Dwarf postfix expression
12501    equivalent.  This routine recursively descends an RTL tree, turning
12502    it into Dwarf postfix code as it goes.
12503 
12504    MODE is the mode that should be assumed for the rtl if it is VOIDmode.
12505 
12506    MEM_MODE is the mode of the memory reference, needed to handle some
12507    autoincrement addressing modes.
12508 
12509    Return 0 if we can't represent the location.  */
12510 
12511 dw_loc_descr_ref
12512 mem_loc_descriptor (rtx rtl, machine_mode mode,
12513 		    machine_mode mem_mode,
12514 		    enum var_init_status initialized)
12515 {
12516   dw_loc_descr_ref mem_loc_result = NULL;
12517   enum dwarf_location_atom op;
12518   dw_loc_descr_ref op0, op1;
12519   rtx inner = NULL_RTX;
12520 
12521   if (mode == VOIDmode)
12522     mode = GET_MODE (rtl);
12523 
12524   /* Note that for a dynamically sized array, the location we will generate a
12525      description of here will be the lowest numbered location which is
12526      actually within the array.  That's *not* necessarily the same as the
12527      zeroth element of the array.  */
12528 
12529   rtl = targetm.delegitimize_address (rtl);
12530 
12531   if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
12532     return NULL;
12533 
12534   switch (GET_CODE (rtl))
12535     {
12536     case POST_INC:
12537     case POST_DEC:
12538     case POST_MODIFY:
12539       return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
12540 
12541     case SUBREG:
12542       /* The case of a subreg may arise when we have a local (register)
12543 	 variable or a formal (register) parameter which doesn't quite fill
12544 	 up an entire register.  For now, just assume that it is
12545 	 legitimate to make the Dwarf info refer to the whole register which
12546 	 contains the given subreg.  */
12547       if (!subreg_lowpart_p (rtl))
12548 	break;
12549       inner = SUBREG_REG (rtl);
12550     case TRUNCATE:
12551       if (inner == NULL_RTX)
12552         inner = XEXP (rtl, 0);
12553       if (GET_MODE_CLASS (mode) == MODE_INT
12554 	  && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
12555 	  && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12556 #ifdef POINTERS_EXTEND_UNSIGNED
12557 	      || (mode == Pmode && mem_mode != VOIDmode)
12558 #endif
12559 	     )
12560 	  && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
12561 	{
12562 	  mem_loc_result = mem_loc_descriptor (inner,
12563 					       GET_MODE (inner),
12564 					       mem_mode, initialized);
12565 	  break;
12566 	}
12567       if (dwarf_strict)
12568 	break;
12569       if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
12570 	break;
12571       if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
12572 	  && (GET_MODE_CLASS (mode) != MODE_INT
12573 	      || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
12574 	break;
12575       else
12576 	{
12577 	  dw_die_ref type_die;
12578 	  dw_loc_descr_ref cvt;
12579 
12580 	  mem_loc_result = mem_loc_descriptor (inner,
12581 					       GET_MODE (inner),
12582 					       mem_mode, initialized);
12583 	  if (mem_loc_result == NULL)
12584 	    break;
12585 	  type_die = base_type_for_mode (mode,
12586 					 GET_MODE_CLASS (mode) == MODE_INT);
12587 	  if (type_die == NULL)
12588 	    {
12589 	      mem_loc_result = NULL;
12590 	      break;
12591 	    }
12592 	  if (GET_MODE_SIZE (mode)
12593 	      != GET_MODE_SIZE (GET_MODE (inner)))
12594 	    cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12595 	  else
12596 	    cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12597 	  cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12598 	  cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12599 	  cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12600 	  add_loc_descr (&mem_loc_result, cvt);
12601 	}
12602       break;
12603 
12604     case REG:
12605       if (GET_MODE_CLASS (mode) != MODE_INT
12606 	  || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12607 	      && rtl != arg_pointer_rtx
12608 	      && rtl != frame_pointer_rtx
12609 #ifdef POINTERS_EXTEND_UNSIGNED
12610 	      && (mode != Pmode || mem_mode == VOIDmode)
12611 #endif
12612 	      ))
12613 	{
12614 	  dw_die_ref type_die;
12615 	  unsigned int dbx_regnum;
12616 
12617 	  if (dwarf_strict)
12618 	    break;
12619 	  if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12620 	    break;
12621 	  type_die = base_type_for_mode (mode,
12622 					 GET_MODE_CLASS (mode) == MODE_INT);
12623 	  if (type_die == NULL)
12624 	    break;
12625 
12626 	  dbx_regnum = dbx_reg_number (rtl);
12627 	  if (dbx_regnum == IGNORED_DWARF_REGNUM)
12628 	    break;
12629 	  mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12630 					  dbx_regnum, 0);
12631 	  mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12632 	  mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12633 	  mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12634 	  break;
12635 	}
12636       /* Whenever a register number forms a part of the description of the
12637 	 method for calculating the (dynamic) address of a memory resident
12638 	 object, DWARF rules require the register number be referred to as
12639 	 a "base register".  This distinction is not based in any way upon
12640 	 what category of register the hardware believes the given register
12641 	 belongs to.  This is strictly DWARF terminology we're dealing with
12642 	 here. Note that in cases where the location of a memory-resident
12643 	 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12644 	 OP_CONST (0)) the actual DWARF location descriptor that we generate
12645 	 may just be OP_BASEREG (basereg).  This may look deceptively like
12646 	 the object in question was allocated to a register (rather than in
12647 	 memory) so DWARF consumers need to be aware of the subtle
12648 	 distinction between OP_REG and OP_BASEREG.  */
12649       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12650 	mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12651       else if (stack_realign_drap
12652 	       && crtl->drap_reg
12653 	       && crtl->args.internal_arg_pointer == rtl
12654 	       && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12655 	{
12656 	  /* If RTL is internal_arg_pointer, which has been optimized
12657 	     out, use DRAP instead.  */
12658 	  mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12659 					    VAR_INIT_STATUS_INITIALIZED);
12660 	}
12661       break;
12662 
12663     case SIGN_EXTEND:
12664     case ZERO_EXTEND:
12665       if (GET_MODE_CLASS (mode) != MODE_INT)
12666 	break;
12667       op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12668 				mem_mode, VAR_INIT_STATUS_INITIALIZED);
12669       if (op0 == 0)
12670 	break;
12671       else if (GET_CODE (rtl) == ZERO_EXTEND
12672 	       && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12673 	       && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12674 		  < HOST_BITS_PER_WIDE_INT
12675 	       /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12676 		  to expand zero extend as two shifts instead of
12677 		  masking.  */
12678 	       && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12679 	{
12680 	  machine_mode imode = GET_MODE (XEXP (rtl, 0));
12681 	  mem_loc_result = op0;
12682 	  add_loc_descr (&mem_loc_result,
12683 			 int_loc_descriptor (GET_MODE_MASK (imode)));
12684 	  add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12685 	}
12686       else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12687 	{
12688 	  int shift = DWARF2_ADDR_SIZE
12689 		      - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12690 	  shift *= BITS_PER_UNIT;
12691 	  if (GET_CODE (rtl) == SIGN_EXTEND)
12692 	    op = DW_OP_shra;
12693 	  else
12694 	    op = DW_OP_shr;
12695 	  mem_loc_result = op0;
12696 	  add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12697 	  add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12698 	  add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12699 	  add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12700 	}
12701       else if (!dwarf_strict)
12702 	{
12703 	  dw_die_ref type_die1, type_die2;
12704 	  dw_loc_descr_ref cvt;
12705 
12706 	  type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12707 					  GET_CODE (rtl) == ZERO_EXTEND);
12708 	  if (type_die1 == NULL)
12709 	    break;
12710 	  type_die2 = base_type_for_mode (mode, 1);
12711 	  if (type_die2 == NULL)
12712 	    break;
12713 	  mem_loc_result = op0;
12714 	  cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12715 	  cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12716 	  cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12717 	  cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12718 	  add_loc_descr (&mem_loc_result, cvt);
12719 	  cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12720 	  cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12721 	  cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12722 	  cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12723 	  add_loc_descr (&mem_loc_result, cvt);
12724 	}
12725       break;
12726 
12727     case MEM:
12728       {
12729 	rtx new_rtl = avoid_constant_pool_reference (rtl);
12730 	if (new_rtl != rtl)
12731 	  {
12732 	    mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12733 						 initialized);
12734 	    if (mem_loc_result != NULL)
12735 	      return mem_loc_result;
12736 	  }
12737       }
12738       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12739 					   get_address_mode (rtl), mode,
12740 					   VAR_INIT_STATUS_INITIALIZED);
12741       if (mem_loc_result == NULL)
12742 	mem_loc_result = tls_mem_loc_descriptor (rtl);
12743       if (mem_loc_result != NULL)
12744 	{
12745 	  if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12746 	      || GET_MODE_CLASS (mode) != MODE_INT)
12747 	    {
12748 	      dw_die_ref type_die;
12749 	      dw_loc_descr_ref deref;
12750 
12751 	      if (dwarf_strict)
12752 		return NULL;
12753 	      type_die
12754 		= base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12755 	      if (type_die == NULL)
12756 		return NULL;
12757 	      deref = new_loc_descr (DW_OP_GNU_deref_type,
12758 				     GET_MODE_SIZE (mode), 0);
12759 	      deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12760 	      deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12761 	      deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12762 	      add_loc_descr (&mem_loc_result, deref);
12763 	    }
12764 	  else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12765 	    add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12766 	  else
12767 	    add_loc_descr (&mem_loc_result,
12768 			   new_loc_descr (DW_OP_deref_size,
12769 					  GET_MODE_SIZE (mode), 0));
12770 	}
12771       break;
12772 
12773     case LO_SUM:
12774       return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12775 
12776     case LABEL_REF:
12777       /* Some ports can transform a symbol ref into a label ref, because
12778 	 the symbol ref is too far away and has to be dumped into a constant
12779 	 pool.  */
12780     case CONST:
12781     case SYMBOL_REF:
12782       if ((GET_MODE_CLASS (mode) != MODE_INT
12783 	   && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
12784 	  || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12785 #ifdef POINTERS_EXTEND_UNSIGNED
12786 	      && (mode != Pmode || mem_mode == VOIDmode)
12787 #endif
12788 	      ))
12789 	break;
12790       if (GET_CODE (rtl) == SYMBOL_REF
12791 	  && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12792 	{
12793 	  dw_loc_descr_ref temp;
12794 
12795 	  /* If this is not defined, we have no way to emit the data.  */
12796 	  if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12797 	    break;
12798 
12799           temp = new_addr_loc_descr (rtl, dtprel_true);
12800 
12801 	  mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12802 	  add_loc_descr (&mem_loc_result, temp);
12803 
12804 	  break;
12805 	}
12806 
12807       if (!const_ok_for_output (rtl))
12808 	break;
12809 
12810     symref:
12811       mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12812       vec_safe_push (used_rtx_array, rtl);
12813       break;
12814 
12815     case CONCAT:
12816     case CONCATN:
12817     case VAR_LOCATION:
12818     case DEBUG_IMPLICIT_PTR:
12819       expansion_failed (NULL_TREE, rtl,
12820 			"CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12821       return 0;
12822 
12823     case ENTRY_VALUE:
12824       if (dwarf_strict)
12825 	return NULL;
12826       if (REG_P (ENTRY_VALUE_EXP (rtl)))
12827 	{
12828 	  if (GET_MODE_CLASS (mode) != MODE_INT
12829 	      || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12830 	    op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12831 				      VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12832 	  else
12833 	    {
12834               unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12835 	      if (dbx_regnum == IGNORED_DWARF_REGNUM)
12836 		return NULL;
12837 	      op0 = one_reg_loc_descriptor (dbx_regnum,
12838 					    VAR_INIT_STATUS_INITIALIZED);
12839 	    }
12840 	}
12841       else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12842 	       && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12843 	{
12844 	  op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12845 				    VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12846 	  if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12847 	    return NULL;
12848 	}
12849       else
12850 	gcc_unreachable ();
12851       if (op0 == NULL)
12852 	return NULL;
12853       mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12854       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12855       mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12856       break;
12857 
12858     case DEBUG_PARAMETER_REF:
12859       mem_loc_result = parameter_ref_descriptor (rtl);
12860       break;
12861 
12862     case PRE_MODIFY:
12863       /* Extract the PLUS expression nested inside and fall into
12864 	 PLUS code below.  */
12865       rtl = XEXP (rtl, 1);
12866       goto plus;
12867 
12868     case PRE_INC:
12869     case PRE_DEC:
12870       /* Turn these into a PLUS expression and fall into the PLUS code
12871 	 below.  */
12872       rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12873 			  gen_int_mode (GET_CODE (rtl) == PRE_INC
12874 					? GET_MODE_UNIT_SIZE (mem_mode)
12875 					: -GET_MODE_UNIT_SIZE (mem_mode),
12876 					mode));
12877 
12878       /* ... fall through ...  */
12879 
12880     case PLUS:
12881     plus:
12882       if (is_based_loc (rtl)
12883 	  && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12884 	      || XEXP (rtl, 0) == arg_pointer_rtx
12885 	      || XEXP (rtl, 0) == frame_pointer_rtx)
12886 	  && GET_MODE_CLASS (mode) == MODE_INT)
12887 	mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12888 					  INTVAL (XEXP (rtl, 1)),
12889 					  VAR_INIT_STATUS_INITIALIZED);
12890       else
12891 	{
12892 	  mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12893 					       VAR_INIT_STATUS_INITIALIZED);
12894 	  if (mem_loc_result == 0)
12895 	    break;
12896 
12897 	  if (CONST_INT_P (XEXP (rtl, 1))
12898 	      && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12899 	    loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12900 	  else
12901 	    {
12902 	      op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12903 					VAR_INIT_STATUS_INITIALIZED);
12904 	      if (op1 == 0)
12905 		return NULL;
12906 	      add_loc_descr (&mem_loc_result, op1);
12907 	      add_loc_descr (&mem_loc_result,
12908 			     new_loc_descr (DW_OP_plus, 0, 0));
12909 	    }
12910 	}
12911       break;
12912 
12913     /* If a pseudo-reg is optimized away, it is possible for it to
12914        be replaced with a MEM containing a multiply or shift.  */
12915     case MINUS:
12916       op = DW_OP_minus;
12917       goto do_binop;
12918 
12919     case MULT:
12920       op = DW_OP_mul;
12921       goto do_binop;
12922 
12923     case DIV:
12924       if (!dwarf_strict
12925 	  && GET_MODE_CLASS (mode) == MODE_INT
12926 	  && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12927 	{
12928 	  mem_loc_result = typed_binop (DW_OP_div, rtl,
12929 					base_type_for_mode (mode, 0),
12930 					mode, mem_mode);
12931 	  break;
12932 	}
12933       op = DW_OP_div;
12934       goto do_binop;
12935 
12936     case UMOD:
12937       op = DW_OP_mod;
12938       goto do_binop;
12939 
12940     case ASHIFT:
12941       op = DW_OP_shl;
12942       goto do_shift;
12943 
12944     case ASHIFTRT:
12945       op = DW_OP_shra;
12946       goto do_shift;
12947 
12948     case LSHIFTRT:
12949       op = DW_OP_shr;
12950       goto do_shift;
12951 
12952     do_shift:
12953       if (GET_MODE_CLASS (mode) != MODE_INT)
12954 	break;
12955       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12956 				VAR_INIT_STATUS_INITIALIZED);
12957       {
12958 	rtx rtlop1 = XEXP (rtl, 1);
12959 	if (GET_MODE (rtlop1) != VOIDmode
12960 	    && GET_MODE_BITSIZE (GET_MODE (rtlop1))
12961 	       < GET_MODE_BITSIZE (mode))
12962 	  rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12963 	op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12964 				  VAR_INIT_STATUS_INITIALIZED);
12965       }
12966 
12967       if (op0 == 0 || op1 == 0)
12968 	break;
12969 
12970       mem_loc_result = op0;
12971       add_loc_descr (&mem_loc_result, op1);
12972       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12973       break;
12974 
12975     case AND:
12976       op = DW_OP_and;
12977       goto do_binop;
12978 
12979     case IOR:
12980       op = DW_OP_or;
12981       goto do_binop;
12982 
12983     case XOR:
12984       op = DW_OP_xor;
12985       goto do_binop;
12986 
12987     do_binop:
12988       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12989 				VAR_INIT_STATUS_INITIALIZED);
12990       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12991 				VAR_INIT_STATUS_INITIALIZED);
12992 
12993       if (op0 == 0 || op1 == 0)
12994 	break;
12995 
12996       mem_loc_result = op0;
12997       add_loc_descr (&mem_loc_result, op1);
12998       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12999       break;
13000 
13001     case MOD:
13002       if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
13003 	{
13004 	  mem_loc_result = typed_binop (DW_OP_mod, rtl,
13005 					base_type_for_mode (mode, 0),
13006 					mode, mem_mode);
13007 	  break;
13008 	}
13009 
13010       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13011 				VAR_INIT_STATUS_INITIALIZED);
13012       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13013 				VAR_INIT_STATUS_INITIALIZED);
13014 
13015       if (op0 == 0 || op1 == 0)
13016 	break;
13017 
13018       mem_loc_result = op0;
13019       add_loc_descr (&mem_loc_result, op1);
13020       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13021       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13022       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
13023       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13024       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
13025       break;
13026 
13027     case UDIV:
13028       if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
13029 	{
13030 	  if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
13031 	    {
13032 	      op = DW_OP_div;
13033 	      goto do_binop;
13034 	    }
13035 	  mem_loc_result = typed_binop (DW_OP_div, rtl,
13036 					base_type_for_mode (mode, 1),
13037 					mode, mem_mode);
13038 	}
13039       break;
13040 
13041     case NOT:
13042       op = DW_OP_not;
13043       goto do_unop;
13044 
13045     case ABS:
13046       op = DW_OP_abs;
13047       goto do_unop;
13048 
13049     case NEG:
13050       op = DW_OP_neg;
13051       goto do_unop;
13052 
13053     do_unop:
13054       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13055 				VAR_INIT_STATUS_INITIALIZED);
13056 
13057       if (op0 == 0)
13058 	break;
13059 
13060       mem_loc_result = op0;
13061       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13062       break;
13063 
13064     case CONST_INT:
13065       if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13066 #ifdef POINTERS_EXTEND_UNSIGNED
13067 	  || (mode == Pmode
13068 	      && mem_mode != VOIDmode
13069 	      && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
13070 #endif
13071 	  )
13072 	{
13073 	  mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13074 	  break;
13075 	}
13076       if (!dwarf_strict
13077 	  && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
13078 	      || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
13079 	{
13080 	  dw_die_ref type_die = base_type_for_mode (mode, 1);
13081 	  machine_mode amode;
13082 	  if (type_die == NULL)
13083 	    return NULL;
13084 	  amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
13085 				 MODE_INT, 0);
13086 	  if (INTVAL (rtl) >= 0
13087 	      && amode != BLKmode
13088 	      && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
13089 	      /* const DW_OP_GNU_convert <XXX> vs.
13090 		 DW_OP_GNU_const_type <XXX, 1, const>.  */
13091 	      && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
13092 		 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
13093 	    {
13094 	      mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13095 	      op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13096 	      op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13097 	      op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13098 	      op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
13099 	      add_loc_descr (&mem_loc_result, op0);
13100 	      return mem_loc_result;
13101 	    }
13102 	  mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
13103 					  INTVAL (rtl));
13104 	  mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13105 	  mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13106 	  mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13107 	  if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
13108 	    mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13109 	  else
13110 	    {
13111 	      mem_loc_result->dw_loc_oprnd2.val_class
13112 		= dw_val_class_const_double;
13113 	      mem_loc_result->dw_loc_oprnd2.v.val_double
13114 		= double_int::from_shwi (INTVAL (rtl));
13115 	    }
13116 	}
13117       break;
13118 
13119     case CONST_DOUBLE:
13120       if (!dwarf_strict)
13121 	{
13122 	  dw_die_ref type_die;
13123 
13124 	  /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
13125 	     CONST_DOUBLE rtx could represent either a large integer
13126 	     or a floating-point constant.  If TARGET_SUPPORTS_WIDE_INT != 0,
13127 	     the value is always a floating point constant.
13128 
13129 	     When it is an integer, a CONST_DOUBLE is used whenever
13130 	     the constant requires 2 HWIs to be adequately represented.
13131 	     We output CONST_DOUBLEs as blocks.  */
13132 	  if (mode == VOIDmode
13133 	      || (GET_MODE (rtl) == VOIDmode
13134 		  && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
13135 	    break;
13136 	  type_die = base_type_for_mode (mode,
13137 					 GET_MODE_CLASS (mode) == MODE_INT);
13138 	  if (type_die == NULL)
13139 	    return NULL;
13140 	  mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13141 	  mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13142 	  mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13143 	  mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13144 #if TARGET_SUPPORTS_WIDE_INT == 0
13145 	  if (!SCALAR_FLOAT_MODE_P (mode))
13146 	    {
13147 	      mem_loc_result->dw_loc_oprnd2.val_class
13148 		= dw_val_class_const_double;
13149 	      mem_loc_result->dw_loc_oprnd2.v.val_double
13150 		= rtx_to_double_int (rtl);
13151 	    }
13152 	  else
13153 #endif
13154 	    {
13155 	      unsigned int length = GET_MODE_SIZE (mode);
13156 	      unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13157 
13158 	      insert_float (rtl, array);
13159 	      mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13160 	      mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13161 	      mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13162 	      mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13163 	    }
13164 	}
13165       break;
13166 
13167     case CONST_WIDE_INT:
13168       if (!dwarf_strict)
13169 	{
13170 	  dw_die_ref type_die;
13171 
13172 	  type_die = base_type_for_mode (mode,
13173 					 GET_MODE_CLASS (mode) == MODE_INT);
13174 	  if (type_die == NULL)
13175 	    return NULL;
13176 	  mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13177 	  mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13178 	  mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13179 	  mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13180 	  mem_loc_result->dw_loc_oprnd2.val_class
13181 	    = dw_val_class_wide_int;
13182 	  mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
13183 	  *mem_loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13184 	}
13185       break;
13186 
13187     case EQ:
13188       mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
13189       break;
13190 
13191     case GE:
13192       mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13193       break;
13194 
13195     case GT:
13196       mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13197       break;
13198 
13199     case LE:
13200       mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13201       break;
13202 
13203     case LT:
13204       mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13205       break;
13206 
13207     case NE:
13208       mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
13209       break;
13210 
13211     case GEU:
13212       mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13213       break;
13214 
13215     case GTU:
13216       mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13217       break;
13218 
13219     case LEU:
13220       mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13221       break;
13222 
13223     case LTU:
13224       mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13225       break;
13226 
13227     case UMIN:
13228     case UMAX:
13229       if (GET_MODE_CLASS (mode) != MODE_INT)
13230 	break;
13231       /* FALLTHRU */
13232     case SMIN:
13233     case SMAX:
13234       mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
13235       break;
13236 
13237     case ZERO_EXTRACT:
13238     case SIGN_EXTRACT:
13239       if (CONST_INT_P (XEXP (rtl, 1))
13240 	  && CONST_INT_P (XEXP (rtl, 2))
13241 	  && ((unsigned) INTVAL (XEXP (rtl, 1))
13242 	      + (unsigned) INTVAL (XEXP (rtl, 2))
13243 	      <= GET_MODE_BITSIZE (mode))
13244 	  && GET_MODE_CLASS (mode) == MODE_INT
13245 	  && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13246 	  && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
13247 	{
13248 	  int shift, size;
13249 	  op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13250 				    mem_mode, VAR_INIT_STATUS_INITIALIZED);
13251 	  if (op0 == 0)
13252 	    break;
13253 	  if (GET_CODE (rtl) == SIGN_EXTRACT)
13254 	    op = DW_OP_shra;
13255 	  else
13256 	    op = DW_OP_shr;
13257 	  mem_loc_result = op0;
13258 	  size = INTVAL (XEXP (rtl, 1));
13259 	  shift = INTVAL (XEXP (rtl, 2));
13260 	  if (BITS_BIG_ENDIAN)
13261 	    shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
13262 		    - shift - size;
13263 	  if (shift + size != (int) DWARF2_ADDR_SIZE)
13264 	    {
13265 	      add_loc_descr (&mem_loc_result,
13266 			     int_loc_descriptor (DWARF2_ADDR_SIZE
13267 						 - shift - size));
13268 	      add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13269 	    }
13270 	  if (size != (int) DWARF2_ADDR_SIZE)
13271 	    {
13272 	      add_loc_descr (&mem_loc_result,
13273 			     int_loc_descriptor (DWARF2_ADDR_SIZE - size));
13274 	      add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13275 	    }
13276 	}
13277       break;
13278 
13279     case IF_THEN_ELSE:
13280       {
13281 	dw_loc_descr_ref op2, bra_node, drop_node;
13282 	op0 = mem_loc_descriptor (XEXP (rtl, 0),
13283 				  GET_MODE (XEXP (rtl, 0)) == VOIDmode
13284 				  ? word_mode : GET_MODE (XEXP (rtl, 0)),
13285 				  mem_mode, VAR_INIT_STATUS_INITIALIZED);
13286 	op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13287 				  VAR_INIT_STATUS_INITIALIZED);
13288 	op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
13289 				  VAR_INIT_STATUS_INITIALIZED);
13290 	if (op0 == NULL || op1 == NULL || op2 == NULL)
13291 	  break;
13292 
13293 	mem_loc_result = op1;
13294 	add_loc_descr (&mem_loc_result, op2);
13295 	add_loc_descr (&mem_loc_result, op0);
13296 	bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13297 	add_loc_descr (&mem_loc_result, bra_node);
13298 	add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
13299 	drop_node = new_loc_descr (DW_OP_drop, 0, 0);
13300 	add_loc_descr (&mem_loc_result, drop_node);
13301 	bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13302 	bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
13303       }
13304       break;
13305 
13306     case FLOAT_EXTEND:
13307     case FLOAT_TRUNCATE:
13308     case FLOAT:
13309     case UNSIGNED_FLOAT:
13310     case FIX:
13311     case UNSIGNED_FIX:
13312       if (!dwarf_strict)
13313 	{
13314 	  dw_die_ref type_die;
13315 	  dw_loc_descr_ref cvt;
13316 
13317 	  op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13318 				    mem_mode, VAR_INIT_STATUS_INITIALIZED);
13319 	  if (op0 == NULL)
13320 	    break;
13321 	  if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
13322 	      && (GET_CODE (rtl) == FLOAT
13323 		  || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
13324 		     <= DWARF2_ADDR_SIZE))
13325 	    {
13326 	      type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
13327 					     GET_CODE (rtl) == UNSIGNED_FLOAT);
13328 	      if (type_die == NULL)
13329 		break;
13330 	      cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13331 	      cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13332 	      cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13333 	      cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13334 	      add_loc_descr (&op0, cvt);
13335 	    }
13336 	  type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
13337 	  if (type_die == NULL)
13338 	    break;
13339 	  cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13340 	  cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13341 	  cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13342 	  cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13343 	  add_loc_descr (&op0, cvt);
13344 	  if (GET_MODE_CLASS (mode) == MODE_INT
13345 	      && (GET_CODE (rtl) == FIX
13346 		  || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
13347 	    {
13348 	      op0 = convert_descriptor_to_mode (mode, op0);
13349 	      if (op0 == NULL)
13350 		break;
13351 	    }
13352 	  mem_loc_result = op0;
13353 	}
13354       break;
13355 
13356     case CLZ:
13357     case CTZ:
13358     case FFS:
13359       mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
13360       break;
13361 
13362     case POPCOUNT:
13363     case PARITY:
13364       mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
13365       break;
13366 
13367     case BSWAP:
13368       mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
13369       break;
13370 
13371     case ROTATE:
13372     case ROTATERT:
13373       mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
13374       break;
13375 
13376     case COMPARE:
13377       /* In theory, we could implement the above.  */
13378       /* DWARF cannot represent the unsigned compare operations
13379 	 natively.  */
13380     case SS_MULT:
13381     case US_MULT:
13382     case SS_DIV:
13383     case US_DIV:
13384     case SS_PLUS:
13385     case US_PLUS:
13386     case SS_MINUS:
13387     case US_MINUS:
13388     case SS_NEG:
13389     case US_NEG:
13390     case SS_ABS:
13391     case SS_ASHIFT:
13392     case US_ASHIFT:
13393     case SS_TRUNCATE:
13394     case US_TRUNCATE:
13395     case UNORDERED:
13396     case ORDERED:
13397     case UNEQ:
13398     case UNGE:
13399     case UNGT:
13400     case UNLE:
13401     case UNLT:
13402     case LTGT:
13403     case FRACT_CONVERT:
13404     case UNSIGNED_FRACT_CONVERT:
13405     case SAT_FRACT:
13406     case UNSIGNED_SAT_FRACT:
13407     case SQRT:
13408     case ASM_OPERANDS:
13409     case VEC_MERGE:
13410     case VEC_SELECT:
13411     case VEC_CONCAT:
13412     case VEC_DUPLICATE:
13413     case UNSPEC:
13414     case HIGH:
13415     case FMA:
13416     case STRICT_LOW_PART:
13417     case CONST_VECTOR:
13418     case CONST_FIXED:
13419     case CLRSB:
13420     case CLOBBER:
13421       /* If delegitimize_address couldn't do anything with the UNSPEC, we
13422 	 can't express it in the debug info.  This can happen e.g. with some
13423 	 TLS UNSPECs.  */
13424       break;
13425 
13426     case CONST_STRING:
13427       resolve_one_addr (&rtl);
13428       goto symref;
13429 
13430     default:
13431 #ifdef ENABLE_CHECKING
13432       print_rtl (stderr, rtl);
13433       gcc_unreachable ();
13434 #else
13435       break;
13436 #endif
13437     }
13438 
13439   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13440     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13441 
13442   return mem_loc_result;
13443 }
13444 
13445 /* Return a descriptor that describes the concatenation of two locations.
13446    This is typically a complex variable.  */
13447 
13448 static dw_loc_descr_ref
13449 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
13450 {
13451   dw_loc_descr_ref cc_loc_result = NULL;
13452   dw_loc_descr_ref x0_ref
13453     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13454   dw_loc_descr_ref x1_ref
13455     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13456 
13457   if (x0_ref == 0 || x1_ref == 0)
13458     return 0;
13459 
13460   cc_loc_result = x0_ref;
13461   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
13462 
13463   add_loc_descr (&cc_loc_result, x1_ref);
13464   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
13465 
13466   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13467     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13468 
13469   return cc_loc_result;
13470 }
13471 
13472 /* Return a descriptor that describes the concatenation of N
13473    locations.  */
13474 
13475 static dw_loc_descr_ref
13476 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
13477 {
13478   unsigned int i;
13479   dw_loc_descr_ref cc_loc_result = NULL;
13480   unsigned int n = XVECLEN (concatn, 0);
13481 
13482   for (i = 0; i < n; ++i)
13483     {
13484       dw_loc_descr_ref ref;
13485       rtx x = XVECEXP (concatn, 0, i);
13486 
13487       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13488       if (ref == NULL)
13489 	return NULL;
13490 
13491       add_loc_descr (&cc_loc_result, ref);
13492       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
13493     }
13494 
13495   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13496     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13497 
13498   return cc_loc_result;
13499 }
13500 
13501 /* Helper function for loc_descriptor.  Return DW_OP_GNU_implicit_pointer
13502    for DEBUG_IMPLICIT_PTR RTL.  */
13503 
13504 static dw_loc_descr_ref
13505 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
13506 {
13507   dw_loc_descr_ref ret;
13508   dw_die_ref ref;
13509 
13510   if (dwarf_strict)
13511     return NULL;
13512   gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
13513 	      || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
13514 	      || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
13515   ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
13516   ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
13517   ret->dw_loc_oprnd2.val_class = dw_val_class_const;
13518   if (ref)
13519     {
13520       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13521       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13522       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13523     }
13524   else
13525     {
13526       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13527       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
13528     }
13529   return ret;
13530 }
13531 
13532 /* Output a proper Dwarf location descriptor for a variable or parameter
13533    which is either allocated in a register or in a memory location.  For a
13534    register, we just generate an OP_REG and the register number.  For a
13535    memory location we provide a Dwarf postfix expression describing how to
13536    generate the (dynamic) address of the object onto the address stack.
13537 
13538    MODE is mode of the decl if this loc_descriptor is going to be used in
13539    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13540    allowed, VOIDmode otherwise.
13541 
13542    If we don't know how to describe it, return 0.  */
13543 
13544 static dw_loc_descr_ref
13545 loc_descriptor (rtx rtl, machine_mode mode,
13546 		enum var_init_status initialized)
13547 {
13548   dw_loc_descr_ref loc_result = NULL;
13549 
13550   switch (GET_CODE (rtl))
13551     {
13552     case SUBREG:
13553       /* The case of a subreg may arise when we have a local (register)
13554 	 variable or a formal (register) parameter which doesn't quite fill
13555 	 up an entire register.  For now, just assume that it is
13556 	 legitimate to make the Dwarf info refer to the whole register which
13557 	 contains the given subreg.  */
13558       if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
13559 	loc_result = loc_descriptor (SUBREG_REG (rtl),
13560 				     GET_MODE (SUBREG_REG (rtl)), initialized);
13561       else
13562 	goto do_default;
13563       break;
13564 
13565     case REG:
13566       loc_result = reg_loc_descriptor (rtl, initialized);
13567       break;
13568 
13569     case MEM:
13570       loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13571 				       GET_MODE (rtl), initialized);
13572       if (loc_result == NULL)
13573 	loc_result = tls_mem_loc_descriptor (rtl);
13574       if (loc_result == NULL)
13575 	{
13576 	  rtx new_rtl = avoid_constant_pool_reference (rtl);
13577 	  if (new_rtl != rtl)
13578 	    loc_result = loc_descriptor (new_rtl, mode, initialized);
13579 	}
13580       break;
13581 
13582     case CONCAT:
13583       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13584 					  initialized);
13585       break;
13586 
13587     case CONCATN:
13588       loc_result = concatn_loc_descriptor (rtl, initialized);
13589       break;
13590 
13591     case VAR_LOCATION:
13592       /* Single part.  */
13593       if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
13594 	{
13595 	  rtx loc = PAT_VAR_LOCATION_LOC (rtl);
13596 	  if (GET_CODE (loc) == EXPR_LIST)
13597 	    loc = XEXP (loc, 0);
13598 	  loc_result = loc_descriptor (loc, mode, initialized);
13599 	  break;
13600 	}
13601 
13602       rtl = XEXP (rtl, 1);
13603       /* FALLTHRU */
13604 
13605     case PARALLEL:
13606       {
13607 	rtvec par_elems = XVEC (rtl, 0);
13608 	int num_elem = GET_NUM_ELEM (par_elems);
13609 	machine_mode mode;
13610 	int i;
13611 
13612 	/* Create the first one, so we have something to add to.  */
13613 	loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13614 				     VOIDmode, initialized);
13615 	if (loc_result == NULL)
13616 	  return NULL;
13617 	mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13618 	add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13619 	for (i = 1; i < num_elem; i++)
13620 	  {
13621 	    dw_loc_descr_ref temp;
13622 
13623 	    temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13624 				   VOIDmode, initialized);
13625 	    if (temp == NULL)
13626 	      return NULL;
13627 	    add_loc_descr (&loc_result, temp);
13628 	    mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13629 	    add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13630 	  }
13631       }
13632       break;
13633 
13634     case CONST_INT:
13635       if (mode != VOIDmode && mode != BLKmode)
13636 	loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13637 						    INTVAL (rtl));
13638       break;
13639 
13640     case CONST_DOUBLE:
13641       if (mode == VOIDmode)
13642 	mode = GET_MODE (rtl);
13643 
13644       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13645 	{
13646 	  gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13647 
13648 	  /* Note that a CONST_DOUBLE rtx could represent either an integer
13649 	     or a floating-point constant.  A CONST_DOUBLE is used whenever
13650 	     the constant requires more than one word in order to be
13651 	     adequately represented.  We output CONST_DOUBLEs as blocks.  */
13652 	  loc_result = new_loc_descr (DW_OP_implicit_value,
13653 				      GET_MODE_SIZE (mode), 0);
13654 #if TARGET_SUPPORTS_WIDE_INT == 0
13655 	  if (!SCALAR_FLOAT_MODE_P (mode))
13656 	    {
13657 	      loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13658 	      loc_result->dw_loc_oprnd2.v.val_double
13659 	        = rtx_to_double_int (rtl);
13660 	    }
13661 	  else
13662 #endif
13663 	    {
13664 	      unsigned int length = GET_MODE_SIZE (mode);
13665 	      unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13666 
13667 	      insert_float (rtl, array);
13668 	      loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13669 	      loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13670 	      loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13671 	      loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13672 	    }
13673 	}
13674       break;
13675 
13676     case CONST_WIDE_INT:
13677       if (mode == VOIDmode)
13678 	mode = GET_MODE (rtl);
13679 
13680       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13681 	{
13682 	  loc_result = new_loc_descr (DW_OP_implicit_value,
13683 				      GET_MODE_SIZE (mode), 0);
13684 	  loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
13685 	  loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
13686 	  *loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13687 	}
13688       break;
13689 
13690     case CONST_VECTOR:
13691       if (mode == VOIDmode)
13692 	mode = GET_MODE (rtl);
13693 
13694       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13695 	{
13696 	  unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13697 	  unsigned int length = CONST_VECTOR_NUNITS (rtl);
13698 	  unsigned char *array
13699 	    = ggc_vec_alloc<unsigned char> (length * elt_size);
13700 	  unsigned int i;
13701 	  unsigned char *p;
13702 	  machine_mode imode = GET_MODE_INNER (mode);
13703 
13704 	  gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13705 	  switch (GET_MODE_CLASS (mode))
13706 	    {
13707 	    case MODE_VECTOR_INT:
13708 	      for (i = 0, p = array; i < length; i++, p += elt_size)
13709 		{
13710 		  rtx elt = CONST_VECTOR_ELT (rtl, i);
13711 		  insert_wide_int (std::make_pair (elt, imode), p, elt_size);
13712 		}
13713 	      break;
13714 
13715 	    case MODE_VECTOR_FLOAT:
13716 	      for (i = 0, p = array; i < length; i++, p += elt_size)
13717 		{
13718 		  rtx elt = CONST_VECTOR_ELT (rtl, i);
13719 		  insert_float (elt, p);
13720 		}
13721 	      break;
13722 
13723 	    default:
13724 	      gcc_unreachable ();
13725 	    }
13726 
13727 	  loc_result = new_loc_descr (DW_OP_implicit_value,
13728 				      length * elt_size, 0);
13729 	  loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13730 	  loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13731 	  loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13732 	  loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13733 	}
13734       break;
13735 
13736     case CONST:
13737       if (mode == VOIDmode
13738 	  || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13739 	  || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13740 	  || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13741 	{
13742 	  loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13743 	  break;
13744 	}
13745       /* FALLTHROUGH */
13746     case SYMBOL_REF:
13747       if (!const_ok_for_output (rtl))
13748 	break;
13749     case LABEL_REF:
13750       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13751 	  && (dwarf_version >= 4 || !dwarf_strict))
13752 	{
13753          loc_result = new_addr_loc_descr (rtl, dtprel_false);
13754 	  add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13755 	  vec_safe_push (used_rtx_array, rtl);
13756 	}
13757       break;
13758 
13759     case DEBUG_IMPLICIT_PTR:
13760       loc_result = implicit_ptr_descriptor (rtl, 0);
13761       break;
13762 
13763     case PLUS:
13764       if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13765 	  && CONST_INT_P (XEXP (rtl, 1)))
13766 	{
13767 	  loc_result
13768 	    = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13769 	  break;
13770 	}
13771       /* FALLTHRU */
13772     do_default:
13773     default:
13774       if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13775 	   && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13776 	   && dwarf_version >= 4)
13777 	  || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13778 	{
13779 	  /* Value expression.  */
13780 	  loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13781 	  if (loc_result)
13782 	    add_loc_descr (&loc_result,
13783 			   new_loc_descr (DW_OP_stack_value, 0, 0));
13784 	}
13785       break;
13786     }
13787 
13788   return loc_result;
13789 }
13790 
13791 /* We need to figure out what section we should use as the base for the
13792    address ranges where a given location is valid.
13793    1. If this particular DECL has a section associated with it, use that.
13794    2. If this function has a section associated with it, use that.
13795    3. Otherwise, use the text section.
13796    XXX: If you split a variable across multiple sections, we won't notice.  */
13797 
13798 static const char *
13799 secname_for_decl (const_tree decl)
13800 {
13801   const char *secname;
13802 
13803   if (VAR_OR_FUNCTION_DECL_P (decl)
13804       && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
13805       && DECL_SECTION_NAME (decl))
13806     secname = DECL_SECTION_NAME (decl);
13807   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13808     secname = DECL_SECTION_NAME (current_function_decl);
13809   else if (cfun && in_cold_section_p)
13810     secname = crtl->subsections.cold_section_label;
13811   else
13812     secname = text_section_label;
13813 
13814   return secname;
13815 }
13816 
13817 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
13818 
13819 static bool
13820 decl_by_reference_p (tree decl)
13821 {
13822   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13823   	   || TREE_CODE (decl) == VAR_DECL)
13824 	  && DECL_BY_REFERENCE (decl));
13825 }
13826 
13827 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
13828    for VARLOC.  */
13829 
13830 static dw_loc_descr_ref
13831 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13832 	       enum var_init_status initialized)
13833 {
13834   int have_address = 0;
13835   dw_loc_descr_ref descr;
13836   machine_mode mode;
13837 
13838   if (want_address != 2)
13839     {
13840       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13841       /* Single part.  */
13842       if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13843 	{
13844 	  varloc = PAT_VAR_LOCATION_LOC (varloc);
13845 	  if (GET_CODE (varloc) == EXPR_LIST)
13846 	    varloc = XEXP (varloc, 0);
13847 	  mode = GET_MODE (varloc);
13848 	  if (MEM_P (varloc))
13849 	    {
13850 	      rtx addr = XEXP (varloc, 0);
13851 	      descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13852 					  mode, initialized);
13853 	      if (descr)
13854 		have_address = 1;
13855 	      else
13856 		{
13857 		  rtx x = avoid_constant_pool_reference (varloc);
13858 		  if (x != varloc)
13859 		    descr = mem_loc_descriptor (x, mode, VOIDmode,
13860 						initialized);
13861 		}
13862 	    }
13863 	  else
13864 	    descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13865 	}
13866       else
13867 	return 0;
13868     }
13869   else
13870     {
13871       if (GET_CODE (varloc) == VAR_LOCATION)
13872 	mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13873       else
13874 	mode = DECL_MODE (loc);
13875       descr = loc_descriptor (varloc, mode, initialized);
13876       have_address = 1;
13877     }
13878 
13879   if (!descr)
13880     return 0;
13881 
13882   if (want_address == 2 && !have_address
13883       && (dwarf_version >= 4 || !dwarf_strict))
13884     {
13885       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13886 	{
13887 	  expansion_failed (loc, NULL_RTX,
13888 			    "DWARF address size mismatch");
13889 	  return 0;
13890 	}
13891       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13892       have_address = 1;
13893     }
13894   /* Show if we can't fill the request for an address.  */
13895   if (want_address && !have_address)
13896     {
13897       expansion_failed (loc, NULL_RTX,
13898 			"Want address and only have value");
13899       return 0;
13900     }
13901 
13902   /* If we've got an address and don't want one, dereference.  */
13903   if (!want_address && have_address)
13904     {
13905       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13906       enum dwarf_location_atom op;
13907 
13908       if (size > DWARF2_ADDR_SIZE || size == -1)
13909 	{
13910 	  expansion_failed (loc, NULL_RTX,
13911 			    "DWARF address size mismatch");
13912 	  return 0;
13913 	}
13914       else if (size == DWARF2_ADDR_SIZE)
13915 	op = DW_OP_deref;
13916       else
13917 	op = DW_OP_deref_size;
13918 
13919       add_loc_descr (&descr, new_loc_descr (op, size, 0));
13920     }
13921 
13922   return descr;
13923 }
13924 
13925 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
13926    if it is not possible.  */
13927 
13928 static dw_loc_descr_ref
13929 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
13930 {
13931   if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
13932     return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
13933   else if (dwarf_version >= 3 || !dwarf_strict)
13934     return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
13935   else
13936     return NULL;
13937 }
13938 
13939 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
13940    for VAR_LOC_NOTE for variable DECL that has been optimized by SRA.  */
13941 
13942 static dw_loc_descr_ref
13943 dw_sra_loc_expr (tree decl, rtx loc)
13944 {
13945   rtx p;
13946   unsigned HOST_WIDE_INT padsize = 0;
13947   dw_loc_descr_ref descr, *descr_tail;
13948   unsigned HOST_WIDE_INT decl_size;
13949   rtx varloc;
13950   enum var_init_status initialized;
13951 
13952   if (DECL_SIZE (decl) == NULL
13953       || !tree_fits_uhwi_p (DECL_SIZE (decl)))
13954     return NULL;
13955 
13956   decl_size = tree_to_uhwi (DECL_SIZE (decl));
13957   descr = NULL;
13958   descr_tail = &descr;
13959 
13960   for (p = loc; p; p = XEXP (p, 1))
13961     {
13962       unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
13963       rtx loc_note = *decl_piece_varloc_ptr (p);
13964       dw_loc_descr_ref cur_descr;
13965       dw_loc_descr_ref *tail, last = NULL;
13966       unsigned HOST_WIDE_INT opsize = 0;
13967 
13968       if (loc_note == NULL_RTX
13969 	  || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
13970 	{
13971 	  padsize += bitsize;
13972 	  continue;
13973 	}
13974       initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
13975       varloc = NOTE_VAR_LOCATION (loc_note);
13976       cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
13977       if (cur_descr == NULL)
13978 	{
13979 	  padsize += bitsize;
13980 	  continue;
13981 	}
13982 
13983       /* Check that cur_descr either doesn't use
13984 	 DW_OP_*piece operations, or their sum is equal
13985 	 to bitsize.  Otherwise we can't embed it.  */
13986       for (tail = &cur_descr; *tail != NULL;
13987 	   tail = &(*tail)->dw_loc_next)
13988 	if ((*tail)->dw_loc_opc == DW_OP_piece)
13989 	  {
13990 	    opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
13991 		      * BITS_PER_UNIT;
13992 	    last = *tail;
13993 	  }
13994 	else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
13995 	  {
13996 	    opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
13997 	    last = *tail;
13998 	  }
13999 
14000       if (last != NULL && opsize != bitsize)
14001 	{
14002 	  padsize += bitsize;
14003 	  /* Discard the current piece of the descriptor and release any
14004 	     addr_table entries it uses.  */
14005 	  remove_loc_list_addr_table_entries (cur_descr);
14006 	  continue;
14007 	}
14008 
14009       /* If there is a hole, add DW_OP_*piece after empty DWARF
14010 	 expression, which means that those bits are optimized out.  */
14011       if (padsize)
14012 	{
14013 	  if (padsize > decl_size)
14014 	    {
14015 	      remove_loc_list_addr_table_entries (cur_descr);
14016 	      goto discard_descr;
14017 	    }
14018 	  decl_size -= padsize;
14019 	  *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
14020 	  if (*descr_tail == NULL)
14021 	    {
14022 	      remove_loc_list_addr_table_entries (cur_descr);
14023 	      goto discard_descr;
14024 	    }
14025 	  descr_tail = &(*descr_tail)->dw_loc_next;
14026 	  padsize = 0;
14027 	}
14028       *descr_tail = cur_descr;
14029       descr_tail = tail;
14030       if (bitsize > decl_size)
14031 	goto discard_descr;
14032       decl_size -= bitsize;
14033       if (last == NULL)
14034 	{
14035 	  HOST_WIDE_INT offset = 0;
14036 	  if (GET_CODE (varloc) == VAR_LOCATION
14037 	      && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14038 	    {
14039 	      varloc = PAT_VAR_LOCATION_LOC (varloc);
14040 	      if (GET_CODE (varloc) == EXPR_LIST)
14041 		varloc = XEXP (varloc, 0);
14042 	    }
14043 	  do
14044 	    {
14045 	      if (GET_CODE (varloc) == CONST
14046 		  || GET_CODE (varloc) == SIGN_EXTEND
14047 		  || GET_CODE (varloc) == ZERO_EXTEND)
14048 		varloc = XEXP (varloc, 0);
14049 	      else if (GET_CODE (varloc) == SUBREG)
14050 		varloc = SUBREG_REG (varloc);
14051 	      else
14052 		break;
14053 	    }
14054 	  while (1);
14055 	  /* DW_OP_bit_size offset should be zero for register
14056 	     or implicit location descriptions and empty location
14057 	     descriptions, but for memory addresses needs big endian
14058 	     adjustment.  */
14059 	  if (MEM_P (varloc))
14060 	    {
14061 	      unsigned HOST_WIDE_INT memsize
14062 		= MEM_SIZE (varloc) * BITS_PER_UNIT;
14063 	      if (memsize != bitsize)
14064 		{
14065 		  if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
14066 		      && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
14067 		    goto discard_descr;
14068 		  if (memsize < bitsize)
14069 		    goto discard_descr;
14070 		  if (BITS_BIG_ENDIAN)
14071 		    offset = memsize - bitsize;
14072 		}
14073 	    }
14074 
14075 	  *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
14076 	  if (*descr_tail == NULL)
14077 	    goto discard_descr;
14078 	  descr_tail = &(*descr_tail)->dw_loc_next;
14079 	}
14080     }
14081 
14082   /* If there were any non-empty expressions, add padding till the end of
14083      the decl.  */
14084   if (descr != NULL && decl_size != 0)
14085     {
14086       *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
14087       if (*descr_tail == NULL)
14088 	goto discard_descr;
14089     }
14090   return descr;
14091 
14092 discard_descr:
14093   /* Discard the descriptor and release any addr_table entries it uses.  */
14094   remove_loc_list_addr_table_entries (descr);
14095   return NULL;
14096 }
14097 
14098 /* Return the dwarf representation of the location list LOC_LIST of
14099    DECL.  WANT_ADDRESS has the same meaning as in loc_list_from_tree
14100    function.  */
14101 
14102 static dw_loc_list_ref
14103 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
14104 {
14105   const char *endname, *secname;
14106   rtx varloc;
14107   enum var_init_status initialized;
14108   struct var_loc_node *node;
14109   dw_loc_descr_ref descr;
14110   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
14111   dw_loc_list_ref list = NULL;
14112   dw_loc_list_ref *listp = &list;
14113 
14114   /* Now that we know what section we are using for a base,
14115      actually construct the list of locations.
14116      The first location information is what is passed to the
14117      function that creates the location list, and the remaining
14118      locations just get added on to that list.
14119      Note that we only know the start address for a location
14120      (IE location changes), so to build the range, we use
14121      the range [current location start, next location start].
14122      This means we have to special case the last node, and generate
14123      a range of [last location start, end of function label].  */
14124 
14125   secname = secname_for_decl (decl);
14126 
14127   for (node = loc_list->first; node; node = node->next)
14128     if (GET_CODE (node->loc) == EXPR_LIST
14129 	|| NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
14130       {
14131 	if (GET_CODE (node->loc) == EXPR_LIST)
14132 	  {
14133 	    /* This requires DW_OP_{,bit_}piece, which is not usable
14134 	       inside DWARF expressions.  */
14135 	    if (want_address != 2)
14136 	      continue;
14137 	    descr = dw_sra_loc_expr (decl, node->loc);
14138 	    if (descr == NULL)
14139 	      continue;
14140 	  }
14141 	else
14142 	  {
14143 	    initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14144 	    varloc = NOTE_VAR_LOCATION (node->loc);
14145 	    descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
14146 	  }
14147 	if (descr)
14148 	  {
14149 	    bool range_across_switch = false;
14150 	    /* If section switch happens in between node->label
14151 	       and node->next->label (or end of function) and
14152 	       we can't emit it as a single entry list,
14153 	       emit two ranges, first one ending at the end
14154 	       of first partition and second one starting at the
14155 	       beginning of second partition.  */
14156 	    if (node == loc_list->last_before_switch
14157 		&& (node != loc_list->first || loc_list->first->next)
14158 		&& current_function_decl)
14159 	      {
14160 		endname = cfun->fde->dw_fde_end;
14161 		range_across_switch = true;
14162 	      }
14163 	    /* The variable has a location between NODE->LABEL and
14164 	       NODE->NEXT->LABEL.  */
14165 	    else if (node->next)
14166 	      endname = node->next->label;
14167 	    /* If the variable has a location at the last label
14168 	       it keeps its location until the end of function.  */
14169 	    else if (!current_function_decl)
14170 	      endname = text_end_label;
14171 	    else
14172 	      {
14173 		ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14174 					     current_function_funcdef_no);
14175 		endname = ggc_strdup (label_id);
14176 	      }
14177 
14178 	    *listp = new_loc_list (descr, node->label, endname, secname);
14179 	    if (TREE_CODE (decl) == PARM_DECL
14180 		&& node == loc_list->first
14181 		&& NOTE_P (node->loc)
14182 		&& strcmp (node->label, endname) == 0)
14183 	      (*listp)->force = true;
14184 	    listp = &(*listp)->dw_loc_next;
14185 
14186 	    if (range_across_switch)
14187 	      {
14188 		if (GET_CODE (node->loc) == EXPR_LIST)
14189 		  descr = dw_sra_loc_expr (decl, node->loc);
14190 		else
14191 		  {
14192 		    initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14193 		    varloc = NOTE_VAR_LOCATION (node->loc);
14194 		    descr = dw_loc_list_1 (decl, varloc, want_address,
14195 					   initialized);
14196 		  }
14197 		gcc_assert (descr);
14198 		/* The variable has a location between NODE->LABEL and
14199 		   NODE->NEXT->LABEL.  */
14200 		if (node->next)
14201 		  endname = node->next->label;
14202 		else
14203 		  endname = cfun->fde->dw_fde_second_end;
14204 		*listp = new_loc_list (descr,
14205 				       cfun->fde->dw_fde_second_begin,
14206 				       endname, secname);
14207 		listp = &(*listp)->dw_loc_next;
14208 	      }
14209 	  }
14210       }
14211 
14212   /* Try to avoid the overhead of a location list emitting a location
14213      expression instead, but only if we didn't have more than one
14214      location entry in the first place.  If some entries were not
14215      representable, we don't want to pretend a single entry that was
14216      applies to the entire scope in which the variable is
14217      available.  */
14218   if (list && loc_list->first->next)
14219     gen_llsym (list);
14220 
14221   return list;
14222 }
14223 
14224 /* Return if the loc_list has only single element and thus can be represented
14225    as location description.   */
14226 
14227 static bool
14228 single_element_loc_list_p (dw_loc_list_ref list)
14229 {
14230   gcc_assert (!list->dw_loc_next || list->ll_symbol);
14231   return !list->ll_symbol;
14232 }
14233 
14234 /* To each location in list LIST add loc descr REF.  */
14235 
14236 static void
14237 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
14238 {
14239   dw_loc_descr_ref copy;
14240   add_loc_descr (&list->expr, ref);
14241   list = list->dw_loc_next;
14242   while (list)
14243     {
14244       copy = ggc_alloc<dw_loc_descr_node> ();
14245       memcpy (copy, ref, sizeof (dw_loc_descr_node));
14246       add_loc_descr (&list->expr, copy);
14247       while (copy->dw_loc_next)
14248 	{
14249 	  dw_loc_descr_ref new_copy = ggc_alloc<dw_loc_descr_node> ();
14250 	  memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
14251 	  copy->dw_loc_next = new_copy;
14252 	  copy = new_copy;
14253 	}
14254       list = list->dw_loc_next;
14255     }
14256 }
14257 
14258 /* Given two lists RET and LIST
14259    produce location list that is result of adding expression in LIST
14260    to expression in RET on each position in program.
14261    Might be destructive on both RET and LIST.
14262 
14263    TODO: We handle only simple cases of RET or LIST having at most one
14264    element. General case would inolve sorting the lists in program order
14265    and merging them that will need some additional work.
14266    Adding that will improve quality of debug info especially for SRA-ed
14267    structures.  */
14268 
14269 static void
14270 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
14271 {
14272   if (!list)
14273     return;
14274   if (!*ret)
14275     {
14276       *ret = list;
14277       return;
14278     }
14279   if (!list->dw_loc_next)
14280     {
14281       add_loc_descr_to_each (*ret, list->expr);
14282       return;
14283     }
14284   if (!(*ret)->dw_loc_next)
14285     {
14286       add_loc_descr_to_each (list, (*ret)->expr);
14287       *ret = list;
14288       return;
14289     }
14290   expansion_failed (NULL_TREE, NULL_RTX,
14291 		    "Don't know how to merge two non-trivial"
14292 		    " location lists.\n");
14293   *ret = NULL;
14294   return;
14295 }
14296 
14297 /* LOC is constant expression.  Try a luck, look it up in constant
14298    pool and return its loc_descr of its address.  */
14299 
14300 static dw_loc_descr_ref
14301 cst_pool_loc_descr (tree loc)
14302 {
14303   /* Get an RTL for this, if something has been emitted.  */
14304   rtx rtl = lookup_constant_def (loc);
14305 
14306   if (!rtl || !MEM_P (rtl))
14307     {
14308       gcc_assert (!rtl);
14309       return 0;
14310     }
14311   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
14312 
14313   /* TODO: We might get more coverage if we was actually delaying expansion
14314      of all expressions till end of compilation when constant pools are fully
14315      populated.  */
14316   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
14317     {
14318       expansion_failed (loc, NULL_RTX,
14319 			"CST value in contant pool but not marked.");
14320       return 0;
14321     }
14322   return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
14323 			     GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
14324 }
14325 
14326 /* Return dw_loc_list representing address of addr_expr LOC
14327    by looking for inner INDIRECT_REF expression and turning
14328    it into simple arithmetics.
14329 
14330    See loc_list_from_tree for the meaning of CONTEXT.  */
14331 
14332 static dw_loc_list_ref
14333 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
14334 						   const loc_descr_context *context)
14335 {
14336   tree obj, offset;
14337   HOST_WIDE_INT bitsize, bitpos, bytepos;
14338   machine_mode mode;
14339   int unsignedp, volatilep = 0;
14340   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14341 
14342   obj = get_inner_reference (TREE_OPERAND (loc, 0),
14343 			     &bitsize, &bitpos, &offset, &mode,
14344 			     &unsignedp, &volatilep, false);
14345   STRIP_NOPS (obj);
14346   if (bitpos % BITS_PER_UNIT)
14347     {
14348       expansion_failed (loc, NULL_RTX, "bitfield access");
14349       return 0;
14350     }
14351   if (!INDIRECT_REF_P (obj))
14352     {
14353       expansion_failed (obj,
14354 			NULL_RTX, "no indirect ref in inner refrence");
14355       return 0;
14356     }
14357   if (!offset && !bitpos)
14358     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
14359 				   context);
14360   else if (toplev
14361 	   && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
14362 	   && (dwarf_version >= 4 || !dwarf_strict))
14363     {
14364       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
14365       if (!list_ret)
14366 	return 0;
14367       if (offset)
14368 	{
14369 	  /* Variable offset.  */
14370 	  list_ret1 = loc_list_from_tree (offset, 0, context);
14371 	  if (list_ret1 == 0)
14372 	    return 0;
14373 	  add_loc_list (&list_ret, list_ret1);
14374 	  if (!list_ret)
14375 	    return 0;
14376 	  add_loc_descr_to_each (list_ret,
14377 				 new_loc_descr (DW_OP_plus, 0, 0));
14378 	}
14379       bytepos = bitpos / BITS_PER_UNIT;
14380       if (bytepos > 0)
14381 	add_loc_descr_to_each (list_ret,
14382 			       new_loc_descr (DW_OP_plus_uconst,
14383 					      bytepos, 0));
14384       else if (bytepos < 0)
14385 	loc_list_plus_const (list_ret, bytepos);
14386       add_loc_descr_to_each (list_ret,
14387 			     new_loc_descr (DW_OP_stack_value, 0, 0));
14388     }
14389   return list_ret;
14390 }
14391 
14392 
14393 /* Helper structure for location descriptions generation.  */
14394 struct loc_descr_context
14395 {
14396   /* The type that is implicitly referenced by DW_OP_push_object_address, or
14397      NULL_TREE if DW_OP_push_object_address in invalid for this location
14398      description.  This is used when processing PLACEHOLDER_EXPR nodes.  */
14399   tree context_type;
14400   /* The ..._DECL node that should be translated as a
14401      DW_OP_push_object_address operation.  */
14402   tree base_decl;
14403 };
14404 
14405 /* Generate Dwarf location list representing LOC.
14406    If WANT_ADDRESS is false, expression computing LOC will be computed
14407    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
14408    if WANT_ADDRESS is 2, expression computing address useable in location
14409      will be returned (i.e. DW_OP_reg can be used
14410      to refer to register values).
14411 
14412    CONTEXT provides information to customize the location descriptions
14413    generation.  Its context_type field specifies what type is implicitly
14414    referenced by DW_OP_push_object_address.  If it is NULL_TREE, this operation
14415    will not be generated.
14416 
14417    If CONTEXT is NULL, the behavior is the same as if both context_type and
14418    base_decl fields were NULL_TREE.  */
14419 
14420 static dw_loc_list_ref
14421 loc_list_from_tree (tree loc, int want_address,
14422 		    const struct loc_descr_context *context)
14423 {
14424   dw_loc_descr_ref ret = NULL, ret1 = NULL;
14425   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14426   int have_address = 0;
14427   enum dwarf_location_atom op;
14428 
14429   /* ??? Most of the time we do not take proper care for sign/zero
14430      extending the values properly.  Hopefully this won't be a real
14431      problem...  */
14432 
14433   if (context != NULL
14434       && context->base_decl == loc
14435       && want_address == 0)
14436     {
14437       if (dwarf_version >= 3 || !dwarf_strict)
14438 	return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
14439 			     NULL, NULL, NULL);
14440       else
14441 	return NULL;
14442     }
14443 
14444   switch (TREE_CODE (loc))
14445     {
14446     case ERROR_MARK:
14447       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
14448       return 0;
14449 
14450     case PLACEHOLDER_EXPR:
14451       /* This case involves extracting fields from an object to determine the
14452 	 position of other fields. It is supposed to appear only as the first
14453          operand of COMPONENT_REF nodes and to reference precisely the type
14454          that the context allows.  */
14455       if (context != NULL
14456           && TREE_TYPE (loc) == context->context_type
14457 	  && want_address >= 1)
14458 	{
14459 	  if (dwarf_version >= 3 || !dwarf_strict)
14460 	    {
14461 	      ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
14462 	      have_address = 1;
14463 	      break;
14464 	    }
14465 	  else
14466 	    return NULL;
14467 	}
14468       else
14469 	expansion_failed (loc, NULL_RTX,
14470 			  "PLACEHOLDER_EXPR for an unexpected type");
14471       break;
14472 
14473     case CALL_EXPR:
14474       expansion_failed (loc, NULL_RTX, "CALL_EXPR");
14475       /* There are no opcodes for these operations.  */
14476       return 0;
14477 
14478     case PREINCREMENT_EXPR:
14479     case PREDECREMENT_EXPR:
14480     case POSTINCREMENT_EXPR:
14481     case POSTDECREMENT_EXPR:
14482       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
14483       /* There are no opcodes for these operations.  */
14484       return 0;
14485 
14486     case ADDR_EXPR:
14487       /* If we already want an address, see if there is INDIRECT_REF inside
14488          e.g. for &this->field.  */
14489       if (want_address)
14490 	{
14491 	  list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
14492 		       (loc, want_address == 2, context);
14493 	  if (list_ret)
14494 	    have_address = 1;
14495 	  else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
14496 	  	   && (ret = cst_pool_loc_descr (loc)))
14497 	    have_address = 1;
14498 	}
14499         /* Otherwise, process the argument and look for the address.  */
14500       if (!list_ret && !ret)
14501         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1, context);
14502       else
14503 	{
14504 	  if (want_address)
14505 	    expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
14506 	  return NULL;
14507 	}
14508       break;
14509 
14510     case VAR_DECL:
14511       if (DECL_THREAD_LOCAL_P (loc))
14512 	{
14513 	  rtx rtl;
14514          enum dwarf_location_atom tls_op;
14515          enum dtprel_bool dtprel = dtprel_false;
14516 
14517 	  if (targetm.have_tls)
14518 	    {
14519 	      /* If this is not defined, we have no way to emit the
14520 		 data.  */
14521 	      if (!targetm.asm_out.output_dwarf_dtprel)
14522 		return 0;
14523 
14524 	       /* The way DW_OP_GNU_push_tls_address is specified, we
14525 	     	  can only look up addresses of objects in the current
14526 	     	  module.  We used DW_OP_addr as first op, but that's
14527 		  wrong, because DW_OP_addr is relocated by the debug
14528 		  info consumer, while DW_OP_GNU_push_tls_address
14529 		  operand shouldn't be.  */
14530 	      if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
14531 		return 0;
14532              dtprel = dtprel_true;
14533              tls_op = DW_OP_GNU_push_tls_address;
14534 	    }
14535 	  else
14536 	    {
14537 	      if (!targetm.emutls.debug_form_tls_address
14538 		  || !(dwarf_version >= 3 || !dwarf_strict))
14539 		return 0;
14540 	      /* We stuffed the control variable into the DECL_VALUE_EXPR
14541 		 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
14542 		 no longer appear in gimple code.  We used the control
14543 		 variable in specific so that we could pick it up here.  */
14544 	      loc = DECL_VALUE_EXPR (loc);
14545               tls_op = DW_OP_form_tls_address;
14546 	    }
14547 
14548 	  rtl = rtl_for_decl_location (loc);
14549 	  if (rtl == NULL_RTX)
14550 	    return 0;
14551 
14552 	  if (!MEM_P (rtl))
14553 	    return 0;
14554 	  rtl = XEXP (rtl, 0);
14555 	  if (! CONSTANT_P (rtl))
14556 	    return 0;
14557 
14558           ret = new_addr_loc_descr (rtl, dtprel);
14559           ret1 = new_loc_descr (tls_op, 0, 0);
14560 	  add_loc_descr (&ret, ret1);
14561 
14562 	  have_address = 1;
14563 	  break;
14564 	}
14565       /* FALLTHRU */
14566 
14567     case PARM_DECL:
14568     case RESULT_DECL:
14569       if (DECL_HAS_VALUE_EXPR_P (loc))
14570 	return loc_list_from_tree (DECL_VALUE_EXPR (loc),
14571 				   want_address, context);
14572       /* FALLTHRU */
14573 
14574     case FUNCTION_DECL:
14575       {
14576 	rtx rtl;
14577 	var_loc_list *loc_list = lookup_decl_loc (loc);
14578 
14579 	if (loc_list && loc_list->first)
14580 	  {
14581 	    list_ret = dw_loc_list (loc_list, loc, want_address);
14582 	    have_address = want_address != 0;
14583 	    break;
14584 	  }
14585 	rtl = rtl_for_decl_location (loc);
14586 	if (rtl == NULL_RTX)
14587 	  {
14588 	    expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14589 	    return 0;
14590 	  }
14591 	else if (CONST_INT_P (rtl))
14592 	  {
14593 	    HOST_WIDE_INT val = INTVAL (rtl);
14594 	    if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14595 	      val &= GET_MODE_MASK (DECL_MODE (loc));
14596 	    ret = int_loc_descriptor (val);
14597 	  }
14598 	else if (GET_CODE (rtl) == CONST_STRING)
14599 	  {
14600 	    expansion_failed (loc, NULL_RTX, "CONST_STRING");
14601 	    return 0;
14602 	  }
14603 	else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14604           ret = new_addr_loc_descr (rtl, dtprel_false);
14605 	else
14606 	  {
14607 	    machine_mode mode, mem_mode;
14608 
14609 	    /* Certain constructs can only be represented at top-level.  */
14610 	    if (want_address == 2)
14611 	      {
14612 		ret = loc_descriptor (rtl, VOIDmode,
14613 				      VAR_INIT_STATUS_INITIALIZED);
14614 		have_address = 1;
14615 	      }
14616 	    else
14617 	      {
14618 		mode = GET_MODE (rtl);
14619 		mem_mode = VOIDmode;
14620 		if (MEM_P (rtl))
14621 		  {
14622 		    mem_mode = mode;
14623 		    mode = get_address_mode (rtl);
14624 		    rtl = XEXP (rtl, 0);
14625 		    have_address = 1;
14626 		  }
14627 		ret = mem_loc_descriptor (rtl, mode, mem_mode,
14628 					  VAR_INIT_STATUS_INITIALIZED);
14629 	      }
14630 	    if (!ret)
14631 	      expansion_failed (loc, rtl,
14632 				"failed to produce loc descriptor for rtl");
14633 	  }
14634       }
14635       break;
14636 
14637     case MEM_REF:
14638       if (!integer_zerop (TREE_OPERAND (loc, 1)))
14639 	{
14640 	  have_address = 1;
14641 	  goto do_plus;
14642 	}
14643       /* Fallthru.  */
14644     case INDIRECT_REF:
14645       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14646       have_address = 1;
14647       break;
14648 
14649     case TARGET_MEM_REF:
14650     case SSA_NAME:
14651     case DEBUG_EXPR_DECL:
14652       return NULL;
14653 
14654     case COMPOUND_EXPR:
14655       return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address, context);
14656 
14657     CASE_CONVERT:
14658     case VIEW_CONVERT_EXPR:
14659     case SAVE_EXPR:
14660     case MODIFY_EXPR:
14661       return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address, context);
14662 
14663     case COMPONENT_REF:
14664     case BIT_FIELD_REF:
14665     case ARRAY_REF:
14666     case ARRAY_RANGE_REF:
14667     case REALPART_EXPR:
14668     case IMAGPART_EXPR:
14669       {
14670 	tree obj, offset;
14671 	HOST_WIDE_INT bitsize, bitpos, bytepos;
14672 	machine_mode mode;
14673 	int unsignedp, volatilep = 0;
14674 
14675 	obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14676 				   &unsignedp, &volatilep, false);
14677 
14678 	gcc_assert (obj != loc);
14679 
14680 	list_ret = loc_list_from_tree (obj,
14681 				       want_address == 2
14682 				       && !bitpos && !offset ? 2 : 1,
14683 				       context);
14684 	/* TODO: We can extract value of the small expression via shifting even
14685 	   for nonzero bitpos.  */
14686 	if (list_ret == 0)
14687 	  return 0;
14688 	if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14689 	  {
14690 	    expansion_failed (loc, NULL_RTX,
14691 			      "bitfield access");
14692 	    return 0;
14693 	  }
14694 
14695 	if (offset != NULL_TREE)
14696 	  {
14697 	    /* Variable offset.  */
14698 	    list_ret1 = loc_list_from_tree (offset, 0, context);
14699 	    if (list_ret1 == 0)
14700 	      return 0;
14701 	    add_loc_list (&list_ret, list_ret1);
14702 	    if (!list_ret)
14703 	      return 0;
14704 	    add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14705 	  }
14706 
14707 	bytepos = bitpos / BITS_PER_UNIT;
14708 	if (bytepos > 0)
14709 	  add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14710 	else if (bytepos < 0)
14711 	  loc_list_plus_const (list_ret, bytepos);
14712 
14713 	have_address = 1;
14714 	break;
14715       }
14716 
14717     case INTEGER_CST:
14718       if ((want_address || !tree_fits_shwi_p (loc))
14719 	  && (ret = cst_pool_loc_descr (loc)))
14720 	have_address = 1;
14721       else if (want_address == 2
14722 	       && tree_fits_shwi_p (loc)
14723 	       && (ret = address_of_int_loc_descriptor
14724 	       		   (int_size_in_bytes (TREE_TYPE (loc)),
14725 	       		    tree_to_shwi (loc))))
14726 	have_address = 1;
14727       else if (tree_fits_shwi_p (loc))
14728 	ret = int_loc_descriptor (tree_to_shwi (loc));
14729       else
14730 	{
14731 	  expansion_failed (loc, NULL_RTX,
14732 			    "Integer operand is not host integer");
14733 	  return 0;
14734 	}
14735       break;
14736 
14737     case CONSTRUCTOR:
14738     case REAL_CST:
14739     case STRING_CST:
14740     case COMPLEX_CST:
14741       if ((ret = cst_pool_loc_descr (loc)))
14742 	have_address = 1;
14743       else
14744       /* We can construct small constants here using int_loc_descriptor.  */
14745 	expansion_failed (loc, NULL_RTX,
14746 			  "constructor or constant not in constant pool");
14747       break;
14748 
14749     case TRUTH_AND_EXPR:
14750     case TRUTH_ANDIF_EXPR:
14751     case BIT_AND_EXPR:
14752       op = DW_OP_and;
14753       goto do_binop;
14754 
14755     case TRUTH_XOR_EXPR:
14756     case BIT_XOR_EXPR:
14757       op = DW_OP_xor;
14758       goto do_binop;
14759 
14760     case TRUTH_OR_EXPR:
14761     case TRUTH_ORIF_EXPR:
14762     case BIT_IOR_EXPR:
14763       op = DW_OP_or;
14764       goto do_binop;
14765 
14766     case FLOOR_DIV_EXPR:
14767     case CEIL_DIV_EXPR:
14768     case ROUND_DIV_EXPR:
14769     case TRUNC_DIV_EXPR:
14770       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14771 	return 0;
14772       op = DW_OP_div;
14773       goto do_binop;
14774 
14775     case MINUS_EXPR:
14776       op = DW_OP_minus;
14777       goto do_binop;
14778 
14779     case FLOOR_MOD_EXPR:
14780     case CEIL_MOD_EXPR:
14781     case ROUND_MOD_EXPR:
14782     case TRUNC_MOD_EXPR:
14783       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14784 	{
14785 	  op = DW_OP_mod;
14786 	  goto do_binop;
14787 	}
14788       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14789       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
14790       if (list_ret == 0 || list_ret1 == 0)
14791 	return 0;
14792 
14793       add_loc_list (&list_ret, list_ret1);
14794       if (list_ret == 0)
14795 	return 0;
14796       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14797       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14798       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14799       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14800       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14801       break;
14802 
14803     case MULT_EXPR:
14804       op = DW_OP_mul;
14805       goto do_binop;
14806 
14807     case LSHIFT_EXPR:
14808       op = DW_OP_shl;
14809       goto do_binop;
14810 
14811     case RSHIFT_EXPR:
14812       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14813       goto do_binop;
14814 
14815     case POINTER_PLUS_EXPR:
14816     case PLUS_EXPR:
14817     do_plus:
14818       if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
14819 	{
14820 	  list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14821 	  if (list_ret == 0)
14822 	    return 0;
14823 
14824 	  loc_list_plus_const (list_ret, tree_to_shwi (TREE_OPERAND (loc, 1)));
14825 	  break;
14826 	}
14827 
14828       op = DW_OP_plus;
14829       goto do_binop;
14830 
14831     case LE_EXPR:
14832       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14833 	return 0;
14834 
14835       op = DW_OP_le;
14836       goto do_binop;
14837 
14838     case GE_EXPR:
14839       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14840 	return 0;
14841 
14842       op = DW_OP_ge;
14843       goto do_binop;
14844 
14845     case LT_EXPR:
14846       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14847 	return 0;
14848 
14849       op = DW_OP_lt;
14850       goto do_binop;
14851 
14852     case GT_EXPR:
14853       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14854 	return 0;
14855 
14856       op = DW_OP_gt;
14857       goto do_binop;
14858 
14859     case EQ_EXPR:
14860       op = DW_OP_eq;
14861       goto do_binop;
14862 
14863     case NE_EXPR:
14864       op = DW_OP_ne;
14865       goto do_binop;
14866 
14867     do_binop:
14868       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14869       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
14870       if (list_ret == 0 || list_ret1 == 0)
14871 	return 0;
14872 
14873       add_loc_list (&list_ret, list_ret1);
14874       if (list_ret == 0)
14875 	return 0;
14876       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14877       break;
14878 
14879     case TRUTH_NOT_EXPR:
14880     case BIT_NOT_EXPR:
14881       op = DW_OP_not;
14882       goto do_unop;
14883 
14884     case ABS_EXPR:
14885       op = DW_OP_abs;
14886       goto do_unop;
14887 
14888     case NEGATE_EXPR:
14889       op = DW_OP_neg;
14890       goto do_unop;
14891 
14892     do_unop:
14893       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14894       if (list_ret == 0)
14895 	return 0;
14896 
14897       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14898       break;
14899 
14900     case MIN_EXPR:
14901     case MAX_EXPR:
14902       {
14903 	const enum tree_code code =
14904 	  TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14905 
14906 	loc = build3 (COND_EXPR, TREE_TYPE (loc),
14907 		      build2 (code, integer_type_node,
14908 			      TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14909 		      TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14910       }
14911 
14912       /* ... fall through ...  */
14913 
14914     case COND_EXPR:
14915       {
14916 	dw_loc_descr_ref lhs
14917 	  = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
14918 	dw_loc_list_ref rhs
14919 	  = loc_list_from_tree (TREE_OPERAND (loc, 2), 0, context);
14920 	dw_loc_descr_ref bra_node, jump_node, tmp;
14921 
14922 	list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14923 	if (list_ret == 0 || lhs == 0 || rhs == 0)
14924 	  return 0;
14925 
14926 	bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14927 	add_loc_descr_to_each (list_ret, bra_node);
14928 
14929 	add_loc_list (&list_ret, rhs);
14930 	jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14931 	add_loc_descr_to_each (list_ret, jump_node);
14932 
14933 	add_loc_descr_to_each (list_ret, lhs);
14934 	bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14935 	bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14936 
14937 	/* ??? Need a node to point the skip at.  Use a nop.  */
14938 	tmp = new_loc_descr (DW_OP_nop, 0, 0);
14939 	add_loc_descr_to_each (list_ret, tmp);
14940 	jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14941 	jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14942       }
14943       break;
14944 
14945     case FIX_TRUNC_EXPR:
14946       return 0;
14947 
14948     default:
14949       /* Leave front-end specific codes as simply unknown.  This comes
14950 	 up, for instance, with the C STMT_EXPR.  */
14951       if ((unsigned int) TREE_CODE (loc)
14952 	  >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14953 	{
14954 	  expansion_failed (loc, NULL_RTX,
14955 			    "language specific tree node");
14956 	  return 0;
14957 	}
14958 
14959 #ifdef ENABLE_CHECKING
14960       /* Otherwise this is a generic code; we should just lists all of
14961 	 these explicitly.  We forgot one.  */
14962       gcc_unreachable ();
14963 #else
14964       /* In a release build, we want to degrade gracefully: better to
14965 	 generate incomplete debugging information than to crash.  */
14966       return NULL;
14967 #endif
14968     }
14969 
14970   if (!ret && !list_ret)
14971     return 0;
14972 
14973   if (want_address == 2 && !have_address
14974       && (dwarf_version >= 4 || !dwarf_strict))
14975     {
14976       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14977 	{
14978 	  expansion_failed (loc, NULL_RTX,
14979 			    "DWARF address size mismatch");
14980 	  return 0;
14981 	}
14982       if (ret)
14983 	add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
14984       else
14985 	add_loc_descr_to_each (list_ret,
14986 			       new_loc_descr (DW_OP_stack_value, 0, 0));
14987       have_address = 1;
14988     }
14989   /* Show if we can't fill the request for an address.  */
14990   if (want_address && !have_address)
14991     {
14992       expansion_failed (loc, NULL_RTX,
14993 			"Want address and only have value");
14994       return 0;
14995     }
14996 
14997   gcc_assert (!ret || !list_ret);
14998 
14999   /* If we've got an address and don't want one, dereference.  */
15000   if (!want_address && have_address)
15001     {
15002       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15003 
15004       if (size > DWARF2_ADDR_SIZE || size == -1)
15005 	{
15006 	  expansion_failed (loc, NULL_RTX,
15007 			    "DWARF address size mismatch");
15008 	  return 0;
15009 	}
15010       else if (size == DWARF2_ADDR_SIZE)
15011 	op = DW_OP_deref;
15012       else
15013 	op = DW_OP_deref_size;
15014 
15015       if (ret)
15016 	add_loc_descr (&ret, new_loc_descr (op, size, 0));
15017       else
15018 	add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
15019     }
15020   if (ret)
15021     list_ret = new_loc_list (ret, NULL, NULL, NULL);
15022 
15023   return list_ret;
15024 }
15025 
15026 /* Same as above but return only single location expression.  */
15027 static dw_loc_descr_ref
15028 loc_descriptor_from_tree (tree loc, int want_address,
15029 			  const struct loc_descr_context *context)
15030 {
15031   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
15032   if (!ret)
15033     return NULL;
15034   if (ret->dw_loc_next)
15035     {
15036       expansion_failed (loc, NULL_RTX,
15037 			"Location list where only loc descriptor needed");
15038       return NULL;
15039     }
15040   return ret->expr;
15041 }
15042 
15043 /* Given a value, round it up to the lowest multiple of `boundary'
15044    which is not less than the value itself.  */
15045 
15046 static inline HOST_WIDE_INT
15047 ceiling (HOST_WIDE_INT value, unsigned int boundary)
15048 {
15049   return (((value + boundary - 1) / boundary) * boundary);
15050 }
15051 
15052 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
15053    pointer to the declared type for the relevant field variable, or return
15054    `integer_type_node' if the given node turns out to be an
15055    ERROR_MARK node.  */
15056 
15057 static inline tree
15058 field_type (const_tree decl)
15059 {
15060   tree type;
15061 
15062   if (TREE_CODE (decl) == ERROR_MARK)
15063     return integer_type_node;
15064 
15065   type = DECL_BIT_FIELD_TYPE (decl);
15066   if (type == NULL_TREE)
15067     type = TREE_TYPE (decl);
15068 
15069   return type;
15070 }
15071 
15072 /* Given a pointer to a tree node, return the alignment in bits for
15073    it, or else return BITS_PER_WORD if the node actually turns out to
15074    be an ERROR_MARK node.  */
15075 
15076 static inline unsigned
15077 simple_type_align_in_bits (const_tree type)
15078 {
15079   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
15080 }
15081 
15082 static inline unsigned
15083 simple_decl_align_in_bits (const_tree decl)
15084 {
15085   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
15086 }
15087 
15088 /* Return the result of rounding T up to ALIGN.  */
15089 
15090 static inline offset_int
15091 round_up_to_align (const offset_int &t, unsigned int align)
15092 {
15093   return wi::udiv_trunc (t + align - 1, align) * align;
15094 }
15095 
15096 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
15097    lowest addressed byte of the "containing object" for the given FIELD_DECL,
15098    or return 0 if we are unable to determine what that offset is, either
15099    because the argument turns out to be a pointer to an ERROR_MARK node, or
15100    because the offset is actually variable.  (We can't handle the latter case
15101    just yet).  */
15102 
15103 static HOST_WIDE_INT
15104 field_byte_offset (const_tree decl)
15105 {
15106   offset_int object_offset_in_bits;
15107   offset_int object_offset_in_bytes;
15108   offset_int bitpos_int;
15109 
15110   if (TREE_CODE (decl) == ERROR_MARK)
15111     return 0;
15112 
15113   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
15114 
15115   /* We cannot yet cope with fields whose positions are variable, so
15116      for now, when we see such things, we simply return 0.  Someday, we may
15117      be able to handle such cases, but it will be damn difficult.  */
15118   if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
15119     return 0;
15120 
15121   bitpos_int = wi::to_offset (bit_position (decl));
15122 
15123 #ifdef PCC_BITFIELD_TYPE_MATTERS
15124   if (PCC_BITFIELD_TYPE_MATTERS)
15125     {
15126       tree type;
15127       tree field_size_tree;
15128       offset_int deepest_bitpos;
15129       offset_int field_size_in_bits;
15130       unsigned int type_align_in_bits;
15131       unsigned int decl_align_in_bits;
15132       offset_int type_size_in_bits;
15133 
15134       type = field_type (decl);
15135       type_size_in_bits = offset_int_type_size_in_bits (type);
15136       type_align_in_bits = simple_type_align_in_bits (type);
15137 
15138       field_size_tree = DECL_SIZE (decl);
15139 
15140       /* The size could be unspecified if there was an error, or for
15141 	 a flexible array member.  */
15142       if (!field_size_tree)
15143 	field_size_tree = bitsize_zero_node;
15144 
15145       /* If the size of the field is not constant, use the type size.  */
15146       if (TREE_CODE (field_size_tree) == INTEGER_CST)
15147 	field_size_in_bits = wi::to_offset (field_size_tree);
15148       else
15149 	field_size_in_bits = type_size_in_bits;
15150 
15151       decl_align_in_bits = simple_decl_align_in_bits (decl);
15152 
15153       /* The GCC front-end doesn't make any attempt to keep track of the
15154 	 starting bit offset (relative to the start of the containing
15155 	 structure type) of the hypothetical "containing object" for a
15156 	 bit-field.  Thus, when computing the byte offset value for the
15157 	 start of the "containing object" of a bit-field, we must deduce
15158 	 this information on our own. This can be rather tricky to do in
15159 	 some cases.  For example, handling the following structure type
15160 	 definition when compiling for an i386/i486 target (which only
15161 	 aligns long long's to 32-bit boundaries) can be very tricky:
15162 
15163 	 struct S { int field1; long long field2:31; };
15164 
15165 	 Fortunately, there is a simple rule-of-thumb which can be used
15166 	 in such cases.  When compiling for an i386/i486, GCC will
15167 	 allocate 8 bytes for the structure shown above.  It decides to
15168 	 do this based upon one simple rule for bit-field allocation.
15169 	 GCC allocates each "containing object" for each bit-field at
15170 	 the first (i.e. lowest addressed) legitimate alignment boundary
15171 	 (based upon the required minimum alignment for the declared
15172 	 type of the field) which it can possibly use, subject to the
15173 	 condition that there is still enough available space remaining
15174 	 in the containing object (when allocated at the selected point)
15175 	 to fully accommodate all of the bits of the bit-field itself.
15176 
15177 	 This simple rule makes it obvious why GCC allocates 8 bytes for
15178 	 each object of the structure type shown above.  When looking
15179 	 for a place to allocate the "containing object" for `field2',
15180 	 the compiler simply tries to allocate a 64-bit "containing
15181 	 object" at each successive 32-bit boundary (starting at zero)
15182 	 until it finds a place to allocate that 64- bit field such that
15183 	 at least 31 contiguous (and previously unallocated) bits remain
15184 	 within that selected 64 bit field.  (As it turns out, for the
15185 	 example above, the compiler finds it is OK to allocate the
15186 	 "containing object" 64-bit field at bit-offset zero within the
15187 	 structure type.)
15188 
15189 	 Here we attempt to work backwards from the limited set of facts
15190 	 we're given, and we try to deduce from those facts, where GCC
15191 	 must have believed that the containing object started (within
15192 	 the structure type). The value we deduce is then used (by the
15193 	 callers of this routine) to generate DW_AT_location and
15194 	 DW_AT_bit_offset attributes for fields (both bit-fields and, in
15195 	 the case of DW_AT_location, regular fields as well).  */
15196 
15197       /* Figure out the bit-distance from the start of the structure to
15198 	 the "deepest" bit of the bit-field.  */
15199       deepest_bitpos = bitpos_int + field_size_in_bits;
15200 
15201       /* This is the tricky part.  Use some fancy footwork to deduce
15202 	 where the lowest addressed bit of the containing object must
15203 	 be.  */
15204       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15205 
15206       /* Round up to type_align by default.  This works best for
15207 	 bitfields.  */
15208       object_offset_in_bits
15209 	= round_up_to_align (object_offset_in_bits, type_align_in_bits);
15210 
15211       if (wi::gtu_p (object_offset_in_bits, bitpos_int))
15212 	{
15213 	  object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15214 
15215 	  /* Round up to decl_align instead.  */
15216 	  object_offset_in_bits
15217 	    = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
15218 	}
15219     }
15220   else
15221 #endif /* PCC_BITFIELD_TYPE_MATTERS */
15222     object_offset_in_bits = bitpos_int;
15223 
15224   object_offset_in_bytes
15225     = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
15226   return object_offset_in_bytes.to_shwi ();
15227 }
15228 
15229 /* The following routines define various Dwarf attributes and any data
15230    associated with them.  */
15231 
15232 /* Add a location description attribute value to a DIE.
15233 
15234    This emits location attributes suitable for whole variables and
15235    whole parameters.  Note that the location attributes for struct fields are
15236    generated by the routine `data_member_location_attribute' below.  */
15237 
15238 static inline void
15239 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
15240 			     dw_loc_list_ref descr)
15241 {
15242   if (descr == 0)
15243     return;
15244   if (single_element_loc_list_p (descr))
15245     add_AT_loc (die, attr_kind, descr->expr);
15246   else
15247     add_AT_loc_list (die, attr_kind, descr);
15248 }
15249 
15250 /* Add DW_AT_accessibility attribute to DIE if needed.  */
15251 
15252 static void
15253 add_accessibility_attribute (dw_die_ref die, tree decl)
15254 {
15255   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
15256      children, otherwise the default is DW_ACCESS_public.  In DWARF2
15257      the default has always been DW_ACCESS_public.  */
15258   if (TREE_PROTECTED (decl))
15259     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
15260   else if (TREE_PRIVATE (decl))
15261     {
15262       if (dwarf_version == 2
15263 	  || die->die_parent == NULL
15264 	  || die->die_parent->die_tag != DW_TAG_class_type)
15265 	add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
15266     }
15267   else if (dwarf_version > 2
15268 	   && die->die_parent
15269 	   && die->die_parent->die_tag == DW_TAG_class_type)
15270     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
15271 }
15272 
15273 /* Attach the specialized form of location attribute used for data members of
15274    struct and union types.  In the special case of a FIELD_DECL node which
15275    represents a bit-field, the "offset" part of this special location
15276    descriptor must indicate the distance in bytes from the lowest-addressed
15277    byte of the containing struct or union type to the lowest-addressed byte of
15278    the "containing object" for the bit-field.  (See the `field_byte_offset'
15279    function above).
15280 
15281    For any given bit-field, the "containing object" is a hypothetical object
15282    (of some integral or enum type) within which the given bit-field lives.  The
15283    type of this hypothetical "containing object" is always the same as the
15284    declared type of the individual bit-field itself (for GCC anyway... the
15285    DWARF spec doesn't actually mandate this).  Note that it is the size (in
15286    bytes) of the hypothetical "containing object" which will be given in the
15287    DW_AT_byte_size attribute for this bit-field.  (See the
15288    `byte_size_attribute' function below.)  It is also used when calculating the
15289    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
15290    function below.)  */
15291 
15292 static void
15293 add_data_member_location_attribute (dw_die_ref die, tree decl)
15294 {
15295   HOST_WIDE_INT offset;
15296   dw_loc_descr_ref loc_descr = 0;
15297 
15298   if (TREE_CODE (decl) == TREE_BINFO)
15299     {
15300       /* We're working on the TAG_inheritance for a base class.  */
15301       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
15302 	{
15303 	  /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
15304 	     aren't at a fixed offset from all (sub)objects of the same
15305 	     type.  We need to extract the appropriate offset from our
15306 	     vtable.  The following dwarf expression means
15307 
15308 	       BaseAddr = ObAddr + *((*ObAddr) - Offset)
15309 
15310 	     This is specific to the V3 ABI, of course.  */
15311 
15312 	  dw_loc_descr_ref tmp;
15313 
15314 	  /* Make a copy of the object address.  */
15315 	  tmp = new_loc_descr (DW_OP_dup, 0, 0);
15316 	  add_loc_descr (&loc_descr, tmp);
15317 
15318 	  /* Extract the vtable address.  */
15319 	  tmp = new_loc_descr (DW_OP_deref, 0, 0);
15320 	  add_loc_descr (&loc_descr, tmp);
15321 
15322 	  /* Calculate the address of the offset.  */
15323 	  offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
15324 	  gcc_assert (offset < 0);
15325 
15326 	  tmp = int_loc_descriptor (-offset);
15327 	  add_loc_descr (&loc_descr, tmp);
15328 	  tmp = new_loc_descr (DW_OP_minus, 0, 0);
15329 	  add_loc_descr (&loc_descr, tmp);
15330 
15331 	  /* Extract the offset.  */
15332 	  tmp = new_loc_descr (DW_OP_deref, 0, 0);
15333 	  add_loc_descr (&loc_descr, tmp);
15334 
15335 	  /* Add it to the object address.  */
15336 	  tmp = new_loc_descr (DW_OP_plus, 0, 0);
15337 	  add_loc_descr (&loc_descr, tmp);
15338 	}
15339       else
15340 	offset = tree_to_shwi (BINFO_OFFSET (decl));
15341     }
15342   else
15343     offset = field_byte_offset (decl);
15344 
15345   if (! loc_descr)
15346     {
15347       if (dwarf_version > 2)
15348 	{
15349 	  /* Don't need to output a location expression, just the constant. */
15350 	  if (offset < 0)
15351 	    add_AT_int (die, DW_AT_data_member_location, offset);
15352 	  else
15353 	    add_AT_unsigned (die, DW_AT_data_member_location, offset);
15354 	  return;
15355 	}
15356       else
15357 	{
15358 	  enum dwarf_location_atom op;
15359 
15360 	  /* The DWARF2 standard says that we should assume that the structure
15361 	     address is already on the stack, so we can specify a structure
15362 	     field address by using DW_OP_plus_uconst.  */
15363 	  op = DW_OP_plus_uconst;
15364 	  loc_descr = new_loc_descr (op, offset, 0);
15365 	}
15366     }
15367 
15368   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
15369 }
15370 
15371 /* Writes integer values to dw_vec_const array.  */
15372 
15373 static void
15374 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
15375 {
15376   while (size != 0)
15377     {
15378       *dest++ = val & 0xff;
15379       val >>= 8;
15380       --size;
15381     }
15382 }
15383 
15384 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
15385 
15386 static HOST_WIDE_INT
15387 extract_int (const unsigned char *src, unsigned int size)
15388 {
15389   HOST_WIDE_INT val = 0;
15390 
15391   src += size;
15392   while (size != 0)
15393     {
15394       val <<= 8;
15395       val |= *--src & 0xff;
15396       --size;
15397     }
15398   return val;
15399 }
15400 
15401 /* Writes wide_int values to dw_vec_const array.  */
15402 
15403 static void
15404 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
15405 {
15406   int i;
15407 
15408   if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
15409     {
15410       insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
15411       return;
15412     }
15413 
15414   /* We'd have to extend this code to support odd sizes.  */
15415   gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
15416 
15417   int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
15418 
15419   if (WORDS_BIG_ENDIAN)
15420     for (i = n - 1; i >= 0; i--)
15421       {
15422 	insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15423 	dest += sizeof (HOST_WIDE_INT);
15424       }
15425   else
15426     for (i = 0; i < n; i++)
15427       {
15428 	insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15429 	dest += sizeof (HOST_WIDE_INT);
15430       }
15431 }
15432 
15433 /* Writes floating point values to dw_vec_const array.  */
15434 
15435 static void
15436 insert_float (const_rtx rtl, unsigned char *array)
15437 {
15438   REAL_VALUE_TYPE rv;
15439   long val[4];
15440   int i;
15441 
15442   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
15443   real_to_target (val, &rv, GET_MODE (rtl));
15444 
15445   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
15446   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
15447     {
15448       insert_int (val[i], 4, array);
15449       array += 4;
15450     }
15451 }
15452 
15453 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
15454    does not have a "location" either in memory or in a register.  These
15455    things can arise in GNU C when a constant is passed as an actual parameter
15456    to an inlined function.  They can also arise in C++ where declared
15457    constants do not necessarily get memory "homes".  */
15458 
15459 static bool
15460 add_const_value_attribute (dw_die_ref die, rtx rtl)
15461 {
15462   switch (GET_CODE (rtl))
15463     {
15464     case CONST_INT:
15465       {
15466 	HOST_WIDE_INT val = INTVAL (rtl);
15467 
15468 	if (val < 0)
15469 	  add_AT_int (die, DW_AT_const_value, val);
15470 	else
15471 	  add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
15472       }
15473       return true;
15474 
15475     case CONST_WIDE_INT:
15476       {
15477 	wide_int w1 = std::make_pair (rtl, MAX_MODE_INT);
15478 	unsigned int prec = MIN (wi::min_precision (w1, UNSIGNED),
15479 				 (unsigned int)CONST_WIDE_INT_NUNITS (rtl) * HOST_BITS_PER_WIDE_INT);
15480 	wide_int w = wi::zext (w1, prec);
15481 	add_AT_wide (die, DW_AT_const_value, w);
15482       }
15483       return true;
15484 
15485     case CONST_DOUBLE:
15486       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
15487 	 floating-point constant.  A CONST_DOUBLE is used whenever the
15488 	 constant requires more than one word in order to be adequately
15489 	 represented.  */
15490       {
15491 	machine_mode mode = GET_MODE (rtl);
15492 
15493 	if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
15494 	  add_AT_double (die, DW_AT_const_value,
15495 			 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
15496 	else
15497 	  {
15498 	    unsigned int length = GET_MODE_SIZE (mode);
15499 	    unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15500 
15501 	    insert_float (rtl, array);
15502 	    add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
15503 	  }
15504       }
15505       return true;
15506 
15507     case CONST_VECTOR:
15508       {
15509 	machine_mode mode = GET_MODE (rtl);
15510 	unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
15511 	unsigned int length = CONST_VECTOR_NUNITS (rtl);
15512 	unsigned char *array
15513 	  = ggc_vec_alloc<unsigned char> (length * elt_size);
15514 	unsigned int i;
15515 	unsigned char *p;
15516 	machine_mode imode = GET_MODE_INNER (mode);
15517 
15518 	switch (GET_MODE_CLASS (mode))
15519 	  {
15520 	  case MODE_VECTOR_INT:
15521 	    for (i = 0, p = array; i < length; i++, p += elt_size)
15522 	      {
15523 		rtx elt = CONST_VECTOR_ELT (rtl, i);
15524 		insert_wide_int (std::make_pair (elt, imode), p, elt_size);
15525 	      }
15526 	    break;
15527 
15528 	  case MODE_VECTOR_FLOAT:
15529 	    for (i = 0, p = array; i < length; i++, p += elt_size)
15530 	      {
15531 		rtx elt = CONST_VECTOR_ELT (rtl, i);
15532 		insert_float (elt, p);
15533 	      }
15534 	    break;
15535 
15536 	  default:
15537 	    gcc_unreachable ();
15538 	  }
15539 
15540 	add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
15541       }
15542       return true;
15543 
15544     case CONST_STRING:
15545       if (dwarf_version >= 4 || !dwarf_strict)
15546 	{
15547 	  dw_loc_descr_ref loc_result;
15548 	  resolve_one_addr (&rtl);
15549 	rtl_addr:
15550           loc_result = new_addr_loc_descr (rtl, dtprel_false);
15551 	  add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15552 	  add_AT_loc (die, DW_AT_location, loc_result);
15553 	  vec_safe_push (used_rtx_array, rtl);
15554 	  return true;
15555 	}
15556       return false;
15557 
15558     case CONST:
15559       if (CONSTANT_P (XEXP (rtl, 0)))
15560 	return add_const_value_attribute (die, XEXP (rtl, 0));
15561       /* FALLTHROUGH */
15562     case SYMBOL_REF:
15563       if (!const_ok_for_output (rtl))
15564 	return false;
15565     case LABEL_REF:
15566       if (dwarf_version >= 4 || !dwarf_strict)
15567 	goto rtl_addr;
15568       return false;
15569 
15570     case PLUS:
15571       /* In cases where an inlined instance of an inline function is passed
15572 	 the address of an `auto' variable (which is local to the caller) we
15573 	 can get a situation where the DECL_RTL of the artificial local
15574 	 variable (for the inlining) which acts as a stand-in for the
15575 	 corresponding formal parameter (of the inline function) will look
15576 	 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
15577 	 exactly a compile-time constant expression, but it isn't the address
15578 	 of the (artificial) local variable either.  Rather, it represents the
15579 	 *value* which the artificial local variable always has during its
15580 	 lifetime.  We currently have no way to represent such quasi-constant
15581 	 values in Dwarf, so for now we just punt and generate nothing.  */
15582       return false;
15583 
15584     case HIGH:
15585     case CONST_FIXED:
15586       return false;
15587 
15588     case MEM:
15589       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
15590 	  && MEM_READONLY_P (rtl)
15591 	  && GET_MODE (rtl) == BLKmode)
15592 	{
15593 	  add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
15594 	  return true;
15595 	}
15596       return false;
15597 
15598     default:
15599       /* No other kinds of rtx should be possible here.  */
15600       gcc_unreachable ();
15601     }
15602   return false;
15603 }
15604 
15605 /* Determine whether the evaluation of EXPR references any variables
15606    or functions which aren't otherwise used (and therefore may not be
15607    output).  */
15608 static tree
15609 reference_to_unused (tree * tp, int * walk_subtrees,
15610 		     void * data ATTRIBUTE_UNUSED)
15611 {
15612   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15613     *walk_subtrees = 0;
15614 
15615   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15616       && ! TREE_ASM_WRITTEN (*tp))
15617     return *tp;
15618   /* ???  The C++ FE emits debug information for using decls, so
15619      putting gcc_unreachable here falls over.  See PR31899.  For now
15620      be conservative.  */
15621   else if (!symtab->global_info_ready
15622 	   && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15623     return *tp;
15624   else if (TREE_CODE (*tp) == VAR_DECL)
15625     {
15626       varpool_node *node = varpool_node::get (*tp);
15627       if (!node || !node->definition)
15628 	return *tp;
15629     }
15630   else if (TREE_CODE (*tp) == FUNCTION_DECL
15631 	   && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15632     {
15633       /* The call graph machinery must have finished analyzing,
15634          optimizing and gimplifying the CU by now.
15635 	 So if *TP has no call graph node associated
15636 	 to it, it means *TP will not be emitted.  */
15637       if (!cgraph_node::get (*tp))
15638 	return *tp;
15639     }
15640   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15641     return *tp;
15642 
15643   return NULL_TREE;
15644 }
15645 
15646 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15647    for use in a later add_const_value_attribute call.  */
15648 
15649 static rtx
15650 rtl_for_decl_init (tree init, tree type)
15651 {
15652   rtx rtl = NULL_RTX;
15653 
15654   STRIP_NOPS (init);
15655 
15656   /* If a variable is initialized with a string constant without embedded
15657      zeros, build CONST_STRING.  */
15658   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15659     {
15660       tree enttype = TREE_TYPE (type);
15661       tree domain = TYPE_DOMAIN (type);
15662       machine_mode mode = TYPE_MODE (enttype);
15663 
15664       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15665 	  && domain
15666 	  && integer_zerop (TYPE_MIN_VALUE (domain))
15667 	  && compare_tree_int (TYPE_MAX_VALUE (domain),
15668 			       TREE_STRING_LENGTH (init) - 1) == 0
15669 	  && ((size_t) TREE_STRING_LENGTH (init)
15670 	      == strlen (TREE_STRING_POINTER (init)) + 1))
15671 	{
15672 	  rtl = gen_rtx_CONST_STRING (VOIDmode,
15673 				      ggc_strdup (TREE_STRING_POINTER (init)));
15674 	  rtl = gen_rtx_MEM (BLKmode, rtl);
15675 	  MEM_READONLY_P (rtl) = 1;
15676 	}
15677     }
15678   /* Other aggregates, and complex values, could be represented using
15679      CONCAT: FIXME!  */
15680   else if (AGGREGATE_TYPE_P (type)
15681 	   || (TREE_CODE (init) == VIEW_CONVERT_EXPR
15682 	       && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
15683 	   || TREE_CODE (type) == COMPLEX_TYPE)
15684     ;
15685   /* Vectors only work if their mode is supported by the target.
15686      FIXME: generic vectors ought to work too.  */
15687   else if (TREE_CODE (type) == VECTOR_TYPE
15688 	   && !VECTOR_MODE_P (TYPE_MODE (type)))
15689     ;
15690   /* If the initializer is something that we know will expand into an
15691      immediate RTL constant, expand it now.  We must be careful not to
15692      reference variables which won't be output.  */
15693   else if (initializer_constant_valid_p (init, type)
15694 	   && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15695     {
15696       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15697 	 possible.  */
15698       if (TREE_CODE (type) == VECTOR_TYPE)
15699 	switch (TREE_CODE (init))
15700 	  {
15701 	  case VECTOR_CST:
15702 	    break;
15703 	  case CONSTRUCTOR:
15704 	    if (TREE_CONSTANT (init))
15705 	      {
15706 		vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15707 		bool constant_p = true;
15708 		tree value;
15709 		unsigned HOST_WIDE_INT ix;
15710 
15711 		/* Even when ctor is constant, it might contain non-*_CST
15712 		   elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15713 		   belong into VECTOR_CST nodes.  */
15714 		FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15715 		  if (!CONSTANT_CLASS_P (value))
15716 		    {
15717 		      constant_p = false;
15718 		      break;
15719 		    }
15720 
15721 		if (constant_p)
15722 		  {
15723 		    init = build_vector_from_ctor (type, elts);
15724 		    break;
15725 		  }
15726 	      }
15727 	    /* FALLTHRU */
15728 
15729 	  default:
15730 	    return NULL;
15731 	  }
15732 
15733       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15734 
15735       /* If expand_expr returns a MEM, it wasn't immediate.  */
15736       gcc_assert (!rtl || !MEM_P (rtl));
15737     }
15738 
15739   return rtl;
15740 }
15741 
15742 /* Generate RTL for the variable DECL to represent its location.  */
15743 
15744 static rtx
15745 rtl_for_decl_location (tree decl)
15746 {
15747   rtx rtl;
15748 
15749   /* Here we have to decide where we are going to say the parameter "lives"
15750      (as far as the debugger is concerned).  We only have a couple of
15751      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15752 
15753      DECL_RTL normally indicates where the parameter lives during most of the
15754      activation of the function.  If optimization is enabled however, this
15755      could be either NULL or else a pseudo-reg.  Both of those cases indicate
15756      that the parameter doesn't really live anywhere (as far as the code
15757      generation parts of GCC are concerned) during most of the function's
15758      activation.  That will happen (for example) if the parameter is never
15759      referenced within the function.
15760 
15761      We could just generate a location descriptor here for all non-NULL
15762      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15763      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15764      where DECL_RTL is NULL or is a pseudo-reg.
15765 
15766      Note however that we can only get away with using DECL_INCOMING_RTL as
15767      a backup substitute for DECL_RTL in certain limited cases.  In cases
15768      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15769      we can be sure that the parameter was passed using the same type as it is
15770      declared to have within the function, and that its DECL_INCOMING_RTL
15771      points us to a place where a value of that type is passed.
15772 
15773      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15774      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15775      because in these cases DECL_INCOMING_RTL points us to a value of some
15776      type which is *different* from the type of the parameter itself.  Thus,
15777      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15778      such cases, the debugger would end up (for example) trying to fetch a
15779      `float' from a place which actually contains the first part of a
15780      `double'.  That would lead to really incorrect and confusing
15781      output at debug-time.
15782 
15783      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15784      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
15785      are a couple of exceptions however.  On little-endian machines we can
15786      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15787      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15788      an integral type that is smaller than TREE_TYPE (decl). These cases arise
15789      when (on a little-endian machine) a non-prototyped function has a
15790      parameter declared to be of type `short' or `char'.  In such cases,
15791      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15792      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15793      passed `int' value.  If the debugger then uses that address to fetch
15794      a `short' or a `char' (on a little-endian machine) the result will be
15795      the correct data, so we allow for such exceptional cases below.
15796 
15797      Note that our goal here is to describe the place where the given formal
15798      parameter lives during most of the function's activation (i.e. between the
15799      end of the prologue and the start of the epilogue).  We'll do that as best
15800      as we can. Note however that if the given formal parameter is modified
15801      sometime during the execution of the function, then a stack backtrace (at
15802      debug-time) will show the function as having been called with the *new*
15803      value rather than the value which was originally passed in.  This happens
15804      rarely enough that it is not a major problem, but it *is* a problem, and
15805      I'd like to fix it.
15806 
15807      A future version of dwarf2out.c may generate two additional attributes for
15808      any given DW_TAG_formal_parameter DIE which will describe the "passed
15809      type" and the "passed location" for the given formal parameter in addition
15810      to the attributes we now generate to indicate the "declared type" and the
15811      "active location" for each parameter.  This additional set of attributes
15812      could be used by debuggers for stack backtraces. Separately, note that
15813      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15814      This happens (for example) for inlined-instances of inline function formal
15815      parameters which are never referenced.  This really shouldn't be
15816      happening.  All PARM_DECL nodes should get valid non-NULL
15817      DECL_INCOMING_RTL values.  FIXME.  */
15818 
15819   /* Use DECL_RTL as the "location" unless we find something better.  */
15820   rtl = DECL_RTL_IF_SET (decl);
15821 
15822   /* When generating abstract instances, ignore everything except
15823      constants, symbols living in memory, and symbols living in
15824      fixed registers.  */
15825   if (! reload_completed)
15826     {
15827       if (rtl
15828 	  && (CONSTANT_P (rtl)
15829 	      || (MEM_P (rtl)
15830 	          && CONSTANT_P (XEXP (rtl, 0)))
15831 	      || (REG_P (rtl)
15832 	          && TREE_CODE (decl) == VAR_DECL
15833 		  && TREE_STATIC (decl))))
15834 	{
15835 	  rtl = targetm.delegitimize_address (rtl);
15836 	  return rtl;
15837 	}
15838       rtl = NULL_RTX;
15839     }
15840   else if (TREE_CODE (decl) == PARM_DECL)
15841     {
15842       if (rtl == NULL_RTX
15843 	  || is_pseudo_reg (rtl)
15844 	  || (MEM_P (rtl)
15845 	      && is_pseudo_reg (XEXP (rtl, 0))
15846 	      && DECL_INCOMING_RTL (decl)
15847 	      && MEM_P (DECL_INCOMING_RTL (decl))
15848 	      && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15849 	{
15850 	  tree declared_type = TREE_TYPE (decl);
15851 	  tree passed_type = DECL_ARG_TYPE (decl);
15852 	  machine_mode dmode = TYPE_MODE (declared_type);
15853 	  machine_mode pmode = TYPE_MODE (passed_type);
15854 
15855 	  /* This decl represents a formal parameter which was optimized out.
15856 	     Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15857 	     all cases where (rtl == NULL_RTX) just below.  */
15858 	  if (dmode == pmode)
15859 	    rtl = DECL_INCOMING_RTL (decl);
15860 	  else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15861 		   && SCALAR_INT_MODE_P (dmode)
15862 		   && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15863 		   && DECL_INCOMING_RTL (decl))
15864 	    {
15865 	      rtx inc = DECL_INCOMING_RTL (decl);
15866 	      if (REG_P (inc))
15867 		rtl = inc;
15868 	      else if (MEM_P (inc))
15869 		{
15870 		  if (BYTES_BIG_ENDIAN)
15871 		    rtl = adjust_address_nv (inc, dmode,
15872 					     GET_MODE_SIZE (pmode)
15873 					     - GET_MODE_SIZE (dmode));
15874 		  else
15875 		    rtl = inc;
15876 		}
15877 	    }
15878 	}
15879 
15880       /* If the parm was passed in registers, but lives on the stack, then
15881 	 make a big endian correction if the mode of the type of the
15882 	 parameter is not the same as the mode of the rtl.  */
15883       /* ??? This is the same series of checks that are made in dbxout.c before
15884 	 we reach the big endian correction code there.  It isn't clear if all
15885 	 of these checks are necessary here, but keeping them all is the safe
15886 	 thing to do.  */
15887       else if (MEM_P (rtl)
15888 	       && XEXP (rtl, 0) != const0_rtx
15889 	       && ! CONSTANT_P (XEXP (rtl, 0))
15890 	       /* Not passed in memory.  */
15891 	       && !MEM_P (DECL_INCOMING_RTL (decl))
15892 	       /* Not passed by invisible reference.  */
15893 	       && (!REG_P (XEXP (rtl, 0))
15894 		   || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15895 		   || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15896 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15897 		   || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15898 #endif
15899 		     )
15900 	       /* Big endian correction check.  */
15901 	       && BYTES_BIG_ENDIAN
15902 	       && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15903 	       && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15904 		   < UNITS_PER_WORD))
15905 	{
15906 	  machine_mode addr_mode = get_address_mode (rtl);
15907 	  int offset = (UNITS_PER_WORD
15908 			- GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15909 
15910 	  rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15911 			     plus_constant (addr_mode, XEXP (rtl, 0), offset));
15912 	}
15913     }
15914   else if (TREE_CODE (decl) == VAR_DECL
15915 	   && rtl
15916 	   && MEM_P (rtl)
15917 	   && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15918 	   && BYTES_BIG_ENDIAN)
15919     {
15920       machine_mode addr_mode = get_address_mode (rtl);
15921       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15922       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15923 
15924       /* If a variable is declared "register" yet is smaller than
15925 	 a register, then if we store the variable to memory, it
15926 	 looks like we're storing a register-sized value, when in
15927 	 fact we are not.  We need to adjust the offset of the
15928 	 storage location to reflect the actual value's bytes,
15929 	 else gdb will not be able to display it.  */
15930       if (rsize > dsize)
15931 	rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15932 			   plus_constant (addr_mode, XEXP (rtl, 0),
15933 					  rsize - dsize));
15934     }
15935 
15936   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15937      and will have been substituted directly into all expressions that use it.
15938      C does not have such a concept, but C++ and other languages do.  */
15939   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15940     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15941 
15942   if (rtl)
15943     rtl = targetm.delegitimize_address (rtl);
15944 
15945   /* If we don't look past the constant pool, we risk emitting a
15946      reference to a constant pool entry that isn't referenced from
15947      code, and thus is not emitted.  */
15948   if (rtl)
15949     rtl = avoid_constant_pool_reference (rtl);
15950 
15951   /* Try harder to get a rtl.  If this symbol ends up not being emitted
15952      in the current CU, resolve_addr will remove the expression referencing
15953      it.  */
15954   if (rtl == NULL_RTX
15955       && TREE_CODE (decl) == VAR_DECL
15956       && !DECL_EXTERNAL (decl)
15957       && TREE_STATIC (decl)
15958       && DECL_NAME (decl)
15959       && !DECL_HARD_REGISTER (decl)
15960       && DECL_MODE (decl) != VOIDmode)
15961     {
15962       rtl = make_decl_rtl_for_debug (decl);
15963       if (!MEM_P (rtl)
15964 	  || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
15965 	  || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
15966 	rtl = NULL_RTX;
15967     }
15968 
15969   return rtl;
15970 }
15971 
15972 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
15973    returned.  If so, the decl for the COMMON block is returned, and the
15974    value is the offset into the common block for the symbol.  */
15975 
15976 static tree
15977 fortran_common (tree decl, HOST_WIDE_INT *value)
15978 {
15979   tree val_expr, cvar;
15980   machine_mode mode;
15981   HOST_WIDE_INT bitsize, bitpos;
15982   tree offset;
15983   int unsignedp, volatilep = 0;
15984 
15985   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
15986      it does not have a value (the offset into the common area), or if it
15987      is thread local (as opposed to global) then it isn't common, and shouldn't
15988      be handled as such.  */
15989   if (TREE_CODE (decl) != VAR_DECL
15990       || !TREE_STATIC (decl)
15991       || !DECL_HAS_VALUE_EXPR_P (decl)
15992       || !is_fortran ())
15993     return NULL_TREE;
15994 
15995   val_expr = DECL_VALUE_EXPR (decl);
15996   if (TREE_CODE (val_expr) != COMPONENT_REF)
15997     return NULL_TREE;
15998 
15999   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
16000 			      &mode, &unsignedp, &volatilep, true);
16001 
16002   if (cvar == NULL_TREE
16003       || TREE_CODE (cvar) != VAR_DECL
16004       || DECL_ARTIFICIAL (cvar)
16005       || !TREE_PUBLIC (cvar))
16006     return NULL_TREE;
16007 
16008   *value = 0;
16009   if (offset != NULL)
16010     {
16011       if (!tree_fits_shwi_p (offset))
16012 	return NULL_TREE;
16013       *value = tree_to_shwi (offset);
16014     }
16015   if (bitpos != 0)
16016     *value += bitpos / BITS_PER_UNIT;
16017 
16018   return cvar;
16019 }
16020 
16021 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
16022    data attribute for a variable or a parameter.  We generate the
16023    DW_AT_const_value attribute only in those cases where the given variable
16024    or parameter does not have a true "location" either in memory or in a
16025    register.  This can happen (for example) when a constant is passed as an
16026    actual argument in a call to an inline function.  (It's possible that
16027    these things can crop up in other ways also.)  Note that one type of
16028    constant value which can be passed into an inlined function is a constant
16029    pointer.  This can happen for example if an actual argument in an inlined
16030    function call evaluates to a compile-time constant address.
16031 
16032    CACHE_P is true if it is worth caching the location list for DECL,
16033    so that future calls can reuse it rather than regenerate it from scratch.
16034    This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
16035    since we will need to refer to them each time the function is inlined.  */
16036 
16037 static bool
16038 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
16039 				       enum dwarf_attribute attr)
16040 {
16041   rtx rtl;
16042   dw_loc_list_ref list;
16043   var_loc_list *loc_list;
16044   cached_dw_loc_list *cache;
16045 
16046   if (TREE_CODE (decl) == ERROR_MARK)
16047     return false;
16048 
16049   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
16050 	      || TREE_CODE (decl) == RESULT_DECL);
16051 
16052   /* Try to get some constant RTL for this decl, and use that as the value of
16053      the location.  */
16054 
16055   rtl = rtl_for_decl_location (decl);
16056   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16057       && add_const_value_attribute (die, rtl))
16058     return true;
16059 
16060   /* See if we have single element location list that is equivalent to
16061      a constant value.  That way we are better to use add_const_value_attribute
16062      rather than expanding constant value equivalent.  */
16063   loc_list = lookup_decl_loc (decl);
16064   if (loc_list
16065       && loc_list->first
16066       && loc_list->first->next == NULL
16067       && NOTE_P (loc_list->first->loc)
16068       && NOTE_VAR_LOCATION (loc_list->first->loc)
16069       && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
16070     {
16071       struct var_loc_node *node;
16072 
16073       node = loc_list->first;
16074       rtl = NOTE_VAR_LOCATION_LOC (node->loc);
16075       if (GET_CODE (rtl) == EXPR_LIST)
16076 	rtl = XEXP (rtl, 0);
16077       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16078 	  && add_const_value_attribute (die, rtl))
16079 	 return true;
16080     }
16081   /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
16082      list several times.  See if we've already cached the contents.  */
16083   list = NULL;
16084   if (loc_list == NULL || cached_dw_loc_list_table == NULL)
16085     cache_p = false;
16086   if (cache_p)
16087     {
16088       cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
16089       if (cache)
16090 	list = cache->loc_list;
16091     }
16092   if (list == NULL)
16093     {
16094       list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
16095 				 NULL);
16096       /* It is usually worth caching this result if the decl is from
16097 	 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements.  */
16098       if (cache_p && list && list->dw_loc_next)
16099 	{
16100 	  cached_dw_loc_list **slot
16101 	    = cached_dw_loc_list_table->find_slot_with_hash (decl,
16102 							     DECL_UID (decl),
16103 							     INSERT);
16104 	  cache = ggc_cleared_alloc<cached_dw_loc_list> ();
16105 	  cache->decl_id = DECL_UID (decl);
16106 	  cache->loc_list = list;
16107 	  *slot = cache;
16108 	}
16109     }
16110   if (list)
16111     {
16112       add_AT_location_description (die, attr, list);
16113       return true;
16114     }
16115   /* None of that worked, so it must not really have a location;
16116      try adding a constant value attribute from the DECL_INITIAL.  */
16117   return tree_add_const_value_attribute_for_decl (die, decl);
16118 }
16119 
16120 /* Add VARIABLE and DIE into deferred locations list.  */
16121 
16122 static void
16123 defer_location (tree variable, dw_die_ref die)
16124 {
16125   deferred_locations entry;
16126   entry.variable = variable;
16127   entry.die = die;
16128   vec_safe_push (deferred_locations_list, entry);
16129 }
16130 
16131 /* Helper function for tree_add_const_value_attribute.  Natively encode
16132    initializer INIT into an array.  Return true if successful.  */
16133 
16134 static bool
16135 native_encode_initializer (tree init, unsigned char *array, int size)
16136 {
16137   tree type;
16138 
16139   if (init == NULL_TREE)
16140     return false;
16141 
16142   STRIP_NOPS (init);
16143   switch (TREE_CODE (init))
16144     {
16145     case STRING_CST:
16146       type = TREE_TYPE (init);
16147       if (TREE_CODE (type) == ARRAY_TYPE)
16148 	{
16149 	  tree enttype = TREE_TYPE (type);
16150 	  machine_mode mode = TYPE_MODE (enttype);
16151 
16152 	  if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
16153 	    return false;
16154 	  if (int_size_in_bytes (type) != size)
16155 	    return false;
16156 	  if (size > TREE_STRING_LENGTH (init))
16157 	    {
16158 	      memcpy (array, TREE_STRING_POINTER (init),
16159 		      TREE_STRING_LENGTH (init));
16160 	      memset (array + TREE_STRING_LENGTH (init),
16161 		      '\0', size - TREE_STRING_LENGTH (init));
16162 	    }
16163 	  else
16164 	    memcpy (array, TREE_STRING_POINTER (init), size);
16165 	  return true;
16166 	}
16167       return false;
16168     case CONSTRUCTOR:
16169       type = TREE_TYPE (init);
16170       if (int_size_in_bytes (type) != size)
16171 	return false;
16172       if (TREE_CODE (type) == ARRAY_TYPE)
16173 	{
16174 	  HOST_WIDE_INT min_index;
16175 	  unsigned HOST_WIDE_INT cnt;
16176 	  int curpos = 0, fieldsize;
16177 	  constructor_elt *ce;
16178 
16179 	  if (TYPE_DOMAIN (type) == NULL_TREE
16180 	      || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
16181 	    return false;
16182 
16183 	  fieldsize = int_size_in_bytes (TREE_TYPE (type));
16184 	  if (fieldsize <= 0)
16185 	    return false;
16186 
16187 	  min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
16188 	  memset (array, '\0', size);
16189 	  FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16190 	    {
16191 	      tree val = ce->value;
16192 	      tree index = ce->index;
16193 	      int pos = curpos;
16194 	      if (index && TREE_CODE (index) == RANGE_EXPR)
16195 		pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
16196 		      * fieldsize;
16197 	      else if (index)
16198 		pos = (tree_to_shwi (index) - min_index) * fieldsize;
16199 
16200 	      if (val)
16201 		{
16202 		  STRIP_NOPS (val);
16203 		  if (!native_encode_initializer (val, array + pos, fieldsize))
16204 		    return false;
16205 		}
16206 	      curpos = pos + fieldsize;
16207 	      if (index && TREE_CODE (index) == RANGE_EXPR)
16208 		{
16209 		  int count = tree_to_shwi (TREE_OPERAND (index, 1))
16210 			      - tree_to_shwi (TREE_OPERAND (index, 0));
16211 		  while (count-- > 0)
16212 		    {
16213 		      if (val)
16214 			memcpy (array + curpos, array + pos, fieldsize);
16215 		      curpos += fieldsize;
16216 		    }
16217 		}
16218 	      gcc_assert (curpos <= size);
16219 	    }
16220 	  return true;
16221 	}
16222       else if (TREE_CODE (type) == RECORD_TYPE
16223 	       || TREE_CODE (type) == UNION_TYPE)
16224 	{
16225 	  tree field = NULL_TREE;
16226 	  unsigned HOST_WIDE_INT cnt;
16227 	  constructor_elt *ce;
16228 
16229 	  if (int_size_in_bytes (type) != size)
16230 	    return false;
16231 
16232 	  if (TREE_CODE (type) == RECORD_TYPE)
16233 	    field = TYPE_FIELDS (type);
16234 
16235 	  FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16236 	    {
16237 	      tree val = ce->value;
16238 	      int pos, fieldsize;
16239 
16240 	      if (ce->index != 0)
16241 		field = ce->index;
16242 
16243 	      if (val)
16244 		STRIP_NOPS (val);
16245 
16246 	      if (field == NULL_TREE || DECL_BIT_FIELD (field))
16247 		return false;
16248 
16249 	      if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
16250 		  && TYPE_DOMAIN (TREE_TYPE (field))
16251 		  && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
16252 		return false;
16253 	      else if (DECL_SIZE_UNIT (field) == NULL_TREE
16254 		       || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
16255 		return false;
16256 	      fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
16257 	      pos = int_byte_position (field);
16258 	      gcc_assert (pos + fieldsize <= size);
16259 	      if (val && fieldsize != 0
16260 		  && !native_encode_initializer (val, array + pos, fieldsize))
16261 		return false;
16262 	    }
16263 	  return true;
16264 	}
16265       return false;
16266     case VIEW_CONVERT_EXPR:
16267     case NON_LVALUE_EXPR:
16268       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
16269     default:
16270       return native_encode_expr (init, array, size) == size;
16271     }
16272 }
16273 
16274 /* Attach a DW_AT_const_value attribute to DIE. The value of the
16275    attribute is the const value T.  */
16276 
16277 static bool
16278 tree_add_const_value_attribute (dw_die_ref die, tree t)
16279 {
16280   tree init;
16281   tree type = TREE_TYPE (t);
16282   rtx rtl;
16283 
16284   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
16285     return false;
16286 
16287   init = t;
16288   gcc_assert (!DECL_P (init));
16289 
16290   rtl = rtl_for_decl_init (init, type);
16291   if (rtl)
16292     return add_const_value_attribute (die, rtl);
16293   /* If the host and target are sane, try harder.  */
16294   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
16295 	   && initializer_constant_valid_p (init, type))
16296     {
16297       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
16298       if (size > 0 && (int) size == size)
16299 	{
16300 	  unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
16301 
16302 	  if (native_encode_initializer (init, array, size))
16303 	    {
16304 	      add_AT_vec (die, DW_AT_const_value, size, 1, array);
16305 	      return true;
16306 	    }
16307 	  ggc_free (array);
16308 	}
16309     }
16310   return false;
16311 }
16312 
16313 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
16314    attribute is the const value of T, where T is an integral constant
16315    variable with static storage duration
16316    (so it can't be a PARM_DECL or a RESULT_DECL).  */
16317 
16318 static bool
16319 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
16320 {
16321 
16322   if (!decl
16323       || (TREE_CODE (decl) != VAR_DECL
16324 	  && TREE_CODE (decl) != CONST_DECL)
16325       || (TREE_CODE (decl) == VAR_DECL
16326 	  && !TREE_STATIC (decl)))
16327     return false;
16328 
16329     if (TREE_READONLY (decl)
16330 	&& ! TREE_THIS_VOLATILE (decl)
16331 	&& DECL_INITIAL (decl))
16332       /* OK */;
16333     else
16334       return false;
16335 
16336   /* Don't add DW_AT_const_value if abstract origin already has one.  */
16337   if (get_AT (var_die, DW_AT_const_value))
16338     return false;
16339 
16340   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
16341 }
16342 
16343 /* Convert the CFI instructions for the current function into a
16344    location list.  This is used for DW_AT_frame_base when we targeting
16345    a dwarf2 consumer that does not support the dwarf3
16346    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
16347    expressions.  */
16348 
16349 static dw_loc_list_ref
16350 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
16351 {
16352   int ix;
16353   dw_fde_ref fde;
16354   dw_loc_list_ref list, *list_tail;
16355   dw_cfi_ref cfi;
16356   dw_cfa_location last_cfa, next_cfa;
16357   const char *start_label, *last_label, *section;
16358   dw_cfa_location remember;
16359 
16360   fde = cfun->fde;
16361   gcc_assert (fde != NULL);
16362 
16363   section = secname_for_decl (current_function_decl);
16364   list_tail = &list;
16365   list = NULL;
16366 
16367   memset (&next_cfa, 0, sizeof (next_cfa));
16368   next_cfa.reg = INVALID_REGNUM;
16369   remember = next_cfa;
16370 
16371   start_label = fde->dw_fde_begin;
16372 
16373   /* ??? Bald assumption that the CIE opcode list does not contain
16374      advance opcodes.  */
16375   FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
16376     lookup_cfa_1 (cfi, &next_cfa, &remember);
16377 
16378   last_cfa = next_cfa;
16379   last_label = start_label;
16380 
16381   if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
16382     {
16383       /* If the first partition contained no CFI adjustments, the
16384 	 CIE opcodes apply to the whole first partition.  */
16385       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16386 				 fde->dw_fde_begin, fde->dw_fde_end, section);
16387       list_tail =&(*list_tail)->dw_loc_next;
16388       start_label = last_label = fde->dw_fde_second_begin;
16389     }
16390 
16391   FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
16392     {
16393       switch (cfi->dw_cfi_opc)
16394 	{
16395 	case DW_CFA_set_loc:
16396 	case DW_CFA_advance_loc1:
16397 	case DW_CFA_advance_loc2:
16398 	case DW_CFA_advance_loc4:
16399 	  if (!cfa_equal_p (&last_cfa, &next_cfa))
16400 	    {
16401 	      *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16402 					 start_label, last_label, section);
16403 
16404 	      list_tail = &(*list_tail)->dw_loc_next;
16405 	      last_cfa = next_cfa;
16406 	      start_label = last_label;
16407 	    }
16408 	  last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
16409 	  break;
16410 
16411 	case DW_CFA_advance_loc:
16412 	  /* The encoding is complex enough that we should never emit this.  */
16413 	  gcc_unreachable ();
16414 
16415 	default:
16416 	  lookup_cfa_1 (cfi, &next_cfa, &remember);
16417 	  break;
16418 	}
16419       if (ix + 1 == fde->dw_fde_switch_cfi_index)
16420 	{
16421 	  if (!cfa_equal_p (&last_cfa, &next_cfa))
16422 	    {
16423 	      *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16424 					 start_label, last_label, section);
16425 
16426 	      list_tail = &(*list_tail)->dw_loc_next;
16427 	      last_cfa = next_cfa;
16428 	      start_label = last_label;
16429 	    }
16430 	  *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16431 				     start_label, fde->dw_fde_end, section);
16432 	  list_tail = &(*list_tail)->dw_loc_next;
16433 	  start_label = last_label = fde->dw_fde_second_begin;
16434 	}
16435     }
16436 
16437   if (!cfa_equal_p (&last_cfa, &next_cfa))
16438     {
16439       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16440 				 start_label, last_label, section);
16441       list_tail = &(*list_tail)->dw_loc_next;
16442       start_label = last_label;
16443     }
16444 
16445   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
16446 			     start_label,
16447 			     fde->dw_fde_second_begin
16448 			     ? fde->dw_fde_second_end : fde->dw_fde_end,
16449 			     section);
16450 
16451   if (list && list->dw_loc_next)
16452     gen_llsym (list);
16453 
16454   return list;
16455 }
16456 
16457 /* Compute a displacement from the "steady-state frame pointer" to the
16458    frame base (often the same as the CFA), and store it in
16459    frame_pointer_fb_offset.  OFFSET is added to the displacement
16460    before the latter is negated.  */
16461 
16462 static void
16463 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
16464 {
16465   rtx reg, elim;
16466 
16467 #ifdef FRAME_POINTER_CFA_OFFSET
16468   reg = frame_pointer_rtx;
16469   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
16470 #else
16471   reg = arg_pointer_rtx;
16472   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
16473 #endif
16474 
16475   elim = (ira_use_lra_p
16476 	  ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
16477 	  : eliminate_regs (reg, VOIDmode, NULL_RTX));
16478   if (GET_CODE (elim) == PLUS)
16479     {
16480       offset += INTVAL (XEXP (elim, 1));
16481       elim = XEXP (elim, 0);
16482     }
16483 
16484   frame_pointer_fb_offset = -offset;
16485 
16486   /* ??? AVR doesn't set up valid eliminations when there is no stack frame
16487      in which to eliminate.  This is because it's stack pointer isn't
16488      directly accessible as a register within the ISA.  To work around
16489      this, assume that while we cannot provide a proper value for
16490      frame_pointer_fb_offset, we won't need one either.  */
16491   frame_pointer_fb_offset_valid
16492     = ((SUPPORTS_STACK_ALIGNMENT
16493 	&& (elim == hard_frame_pointer_rtx
16494 	    || elim == stack_pointer_rtx))
16495        || elim == (frame_pointer_needed
16496 		   ? hard_frame_pointer_rtx
16497 		   : stack_pointer_rtx));
16498 }
16499 
16500 /* Generate a DW_AT_name attribute given some string value to be included as
16501    the value of the attribute.  */
16502 
16503 static void
16504 add_name_attribute (dw_die_ref die, const char *name_string)
16505 {
16506   if (name_string != NULL && *name_string != 0)
16507     {
16508       if (demangle_name_func)
16509 	name_string = (*demangle_name_func) (name_string);
16510 
16511       add_AT_string (die, DW_AT_name, name_string);
16512     }
16513 }
16514 
16515 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
16516    DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
16517    of TYPE accordingly.
16518 
16519    ??? This is a temporary measure until after we're able to generate
16520    regular DWARF for the complex Ada type system.  */
16521 
16522 static void
16523 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
16524 				     dw_die_ref context_die)
16525 {
16526   tree dtype;
16527   dw_die_ref dtype_die;
16528 
16529   if (!lang_hooks.types.descriptive_type)
16530     return;
16531 
16532   dtype = lang_hooks.types.descriptive_type (type);
16533   if (!dtype)
16534     return;
16535 
16536   dtype_die = lookup_type_die (dtype);
16537   if (!dtype_die)
16538     {
16539       gen_type_die (dtype, context_die);
16540       dtype_die = lookup_type_die (dtype);
16541       gcc_assert (dtype_die);
16542     }
16543 
16544   add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
16545 }
16546 
16547 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir.  */
16548 
16549 static const char *
16550 comp_dir_string (void)
16551 {
16552   const char *wd;
16553   char *wd1;
16554   static const char *cached_wd = NULL;
16555 
16556   if (cached_wd != NULL)
16557     return cached_wd;
16558 
16559   wd = get_src_pwd ();
16560   if (wd == NULL)
16561     return NULL;
16562 
16563   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16564     {
16565       int wdlen;
16566 
16567       wdlen = strlen (wd);
16568       wd1 = ggc_vec_alloc<char> (wdlen + 2);
16569       strcpy (wd1, wd);
16570       wd1 [wdlen] = DIR_SEPARATOR;
16571       wd1 [wdlen + 1] = 0;
16572       wd = wd1;
16573     }
16574 
16575   cached_wd = remap_debug_filename (wd);
16576   return cached_wd;
16577 }
16578 
16579 /* Generate a DW_AT_comp_dir attribute for DIE.  */
16580 
16581 static void
16582 add_comp_dir_attribute (dw_die_ref die)
16583 {
16584   const char * wd = comp_dir_string ();
16585   if (wd != NULL)
16586     add_AT_string (die, DW_AT_comp_dir, wd);
16587 }
16588 
16589 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
16590    pointer computation, ...), output a representation for that bound according
16591    to the accepted FORMS (see enum dw_scalar_form) and add it to DIE.  See
16592    loc_list_from_tree for the meaning of CONTEXT.  */
16593 
16594 static void
16595 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
16596 		 int forms, const struct loc_descr_context *context)
16597 {
16598   dw_die_ref ctx, decl_die;
16599   dw_loc_list_ref list;
16600 
16601   bool strip_conversions = true;
16602 
16603   while (strip_conversions)
16604     switch (TREE_CODE (value))
16605       {
16606       case ERROR_MARK:
16607       case SAVE_EXPR:
16608 	return;
16609 
16610       CASE_CONVERT:
16611       case VIEW_CONVERT_EXPR:
16612 	value = TREE_OPERAND (value, 0);
16613 	break;
16614 
16615       default:
16616 	strip_conversions = false;
16617 	break;
16618       }
16619 
16620   /* If possible and permitted, output the attribute as a constant.  */
16621   if ((forms & dw_scalar_form_constant) != 0
16622       && TREE_CODE (value) == INTEGER_CST)
16623     {
16624       unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
16625 
16626       /* If HOST_WIDE_INT is big enough then represent the bound as
16627 	 a constant value.  We need to choose a form based on
16628 	 whether the type is signed or unsigned.  We cannot just
16629 	 call add_AT_unsigned if the value itself is positive
16630 	 (add_AT_unsigned might add the unsigned value encoded as
16631 	 DW_FORM_data[1248]).  Some DWARF consumers will lookup the
16632 	 bounds type and then sign extend any unsigned values found
16633 	 for signed types.  This is needed only for
16634 	 DW_AT_{lower,upper}_bound, since for most other attributes,
16635 	 consumers will treat DW_FORM_data[1248] as unsigned values,
16636 	 regardless of the underlying type.  */
16637       if (prec <= HOST_BITS_PER_WIDE_INT
16638 	  || tree_fits_uhwi_p (value))
16639 	{
16640 	  if (TYPE_UNSIGNED (TREE_TYPE (value)))
16641 	    add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
16642 	  else
16643 	    add_AT_int (die, attr, TREE_INT_CST_LOW (value));
16644 	}
16645       else
16646 	/* Otherwise represent the bound as an unsigned value with
16647 	   the precision of its type.  The precision and signedness
16648 	   of the type will be necessary to re-interpret it
16649 	   unambiguously.  */
16650 	add_AT_wide (die, attr, value);
16651       return;
16652     }
16653 
16654   /* Otherwise, if it's possible and permitted too, output a reference to
16655      another DIE.  */
16656   if ((forms & dw_scalar_form_reference) != 0)
16657     {
16658       tree decl = NULL_TREE;
16659 
16660       /* Some type attributes reference an outer type.  For instance, the upper
16661 	 bound of an array may reference an embedding record (this happens in
16662 	 Ada).  */
16663       if (TREE_CODE (value) == COMPONENT_REF
16664 	  && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
16665 	  && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
16666 	decl = TREE_OPERAND (value, 1);
16667 
16668       else if (TREE_CODE (value) == VAR_DECL
16669 	       || TREE_CODE (value) == PARM_DECL
16670 	       || TREE_CODE (value) == RESULT_DECL)
16671 	decl = value;
16672 
16673       if (decl != NULL_TREE)
16674 	{
16675 	  dw_die_ref decl_die = lookup_decl_die (decl);
16676 
16677 	  /* ??? Can this happen, or should the variable have been bound
16678 	     first?  Probably it can, since I imagine that we try to create
16679 	     the types of parameters in the order in which they exist in
16680 	     the list, and won't have created a forward reference to a
16681 	     later parameter.  */
16682 	  if (decl_die != NULL)
16683 	    {
16684 	      add_AT_die_ref (die, attr, decl_die);
16685 	      return;
16686 	    }
16687 	}
16688     }
16689 
16690   /* Last chance: try to create a stack operation procedure to evaluate the
16691      value.  Do nothing if even that is not possible or permitted.  */
16692   if ((forms & dw_scalar_form_exprloc) == 0)
16693     return;
16694 
16695   list = loc_list_from_tree (value, 2, context);
16696   if (list == NULL || single_element_loc_list_p (list))
16697     {
16698       /* If this attribute is not a reference nor constant, it is
16699 	 a DWARF expression rather than location description.  For that
16700 	 loc_list_from_tree (value, 0, &context) is needed.  */
16701       dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
16702       if (list2 && single_element_loc_list_p (list2))
16703 	{
16704 	  add_AT_loc (die, attr, list2->expr);
16705 	  return;
16706 	}
16707     }
16708 
16709   /* If that failed to give a single element location list, fall back to
16710      outputting this as a reference... still if permitted.  */
16711   if (list == NULL || (forms & dw_scalar_form_reference) == 0)
16712     return;
16713 
16714   if (current_function_decl == 0)
16715     ctx = comp_unit_die ();
16716   else
16717     ctx = lookup_decl_die (current_function_decl);
16718 
16719   decl_die = new_die (DW_TAG_variable, ctx, value);
16720   add_AT_flag (decl_die, DW_AT_artificial, 1);
16721   add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, ctx);
16722   add_AT_location_description (decl_die, DW_AT_location, list);
16723   add_AT_die_ref (die, attr, decl_die);
16724 }
16725 
16726 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
16727    default.  */
16728 
16729 static int
16730 lower_bound_default (void)
16731 {
16732   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16733     {
16734     case DW_LANG_C:
16735     case DW_LANG_C89:
16736     case DW_LANG_C99:
16737     case DW_LANG_C11:
16738     case DW_LANG_C_plus_plus:
16739     case DW_LANG_C_plus_plus_11:
16740     case DW_LANG_C_plus_plus_14:
16741     case DW_LANG_ObjC:
16742     case DW_LANG_ObjC_plus_plus:
16743     case DW_LANG_Java:
16744       return 0;
16745     case DW_LANG_Fortran77:
16746     case DW_LANG_Fortran90:
16747     case DW_LANG_Fortran95:
16748     case DW_LANG_Fortran03:
16749     case DW_LANG_Fortran08:
16750       return 1;
16751     case DW_LANG_UPC:
16752     case DW_LANG_D:
16753     case DW_LANG_Python:
16754       return dwarf_version >= 4 ? 0 : -1;
16755     case DW_LANG_Ada95:
16756     case DW_LANG_Ada83:
16757     case DW_LANG_Cobol74:
16758     case DW_LANG_Cobol85:
16759     case DW_LANG_Pascal83:
16760     case DW_LANG_Modula2:
16761     case DW_LANG_PLI:
16762       return dwarf_version >= 4 ? 1 : -1;
16763     default:
16764       return -1;
16765     }
16766 }
16767 
16768 /* Given a tree node describing an array bound (either lower or upper) output
16769    a representation for that bound.  */
16770 
16771 static void
16772 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
16773 		tree bound, const struct loc_descr_context *context)
16774 {
16775   int dflt;
16776 
16777   while (1)
16778     switch (TREE_CODE (bound))
16779       {
16780       /* Strip all conversions.  */
16781       CASE_CONVERT:
16782       case VIEW_CONVERT_EXPR:
16783 	bound = TREE_OPERAND (bound, 0);
16784 	break;
16785 
16786       /* All fixed-bounds are represented by INTEGER_CST nodes.  Lower bounds
16787 	 are even omitted when they are the default.  */
16788       case INTEGER_CST:
16789 	/* If the value for this bound is the default one, we can even omit the
16790 	   attribute.  */
16791 	if (bound_attr == DW_AT_lower_bound
16792 	    && tree_fits_shwi_p (bound)
16793 	    && (dflt = lower_bound_default ()) != -1
16794 	    && tree_to_shwi (bound) == dflt)
16795 	  return;
16796 
16797 	/* FALLTHRU */
16798 
16799       default:
16800 	add_scalar_info (subrange_die, bound_attr, bound,
16801 			 dw_scalar_form_constant
16802 			 | dw_scalar_form_exprloc
16803 			 | dw_scalar_form_reference,
16804 			 context);
16805 	return;
16806       }
16807 }
16808 
16809 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16810    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16811    Note that the block of subscript information for an array type also
16812    includes information about the element type of the given array type.  */
16813 
16814 static void
16815 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16816 {
16817   unsigned dimension_number;
16818   tree lower, upper;
16819   dw_die_ref subrange_die;
16820 
16821   for (dimension_number = 0;
16822        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16823        type = TREE_TYPE (type), dimension_number++)
16824     {
16825       tree domain = TYPE_DOMAIN (type);
16826 
16827       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16828 	break;
16829 
16830       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16831 	 and (in GNU C only) variable bounds.  Handle all three forms
16832 	 here.  */
16833       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16834       if (domain)
16835 	{
16836 	  /* We have an array type with specified bounds.  */
16837 	  lower = TYPE_MIN_VALUE (domain);
16838 	  upper = TYPE_MAX_VALUE (domain);
16839 
16840 	  /* Define the index type.  */
16841 	  if (TREE_TYPE (domain))
16842 	    {
16843 	      /* ??? This is probably an Ada unnamed subrange type.  Ignore the
16844 		 TREE_TYPE field.  We can't emit debug info for this
16845 		 because it is an unnamed integral type.  */
16846 	      if (TREE_CODE (domain) == INTEGER_TYPE
16847 		  && TYPE_NAME (domain) == NULL_TREE
16848 		  && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16849 		  && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16850 		;
16851 	      else
16852 		add_type_attribute (subrange_die, TREE_TYPE (domain),
16853 				    TYPE_UNQUALIFIED, type_die);
16854 	    }
16855 
16856 	  /* ??? If upper is NULL, the array has unspecified length,
16857 	     but it does have a lower bound.  This happens with Fortran
16858 	       dimension arr(N:*)
16859 	     Since the debugger is definitely going to need to know N
16860 	     to produce useful results, go ahead and output the lower
16861 	     bound solo, and hope the debugger can cope.  */
16862 
16863 	  add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
16864 	  if (upper)
16865 	    add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
16866 	}
16867 
16868       /* Otherwise we have an array type with an unspecified length.  The
16869 	 DWARF-2 spec does not say how to handle this; let's just leave out the
16870 	 bounds.  */
16871     }
16872 }
16873 
16874 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size.  */
16875 
16876 static void
16877 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16878 {
16879   dw_die_ref decl_die;
16880   HOST_WIDE_INT size;
16881 
16882   switch (TREE_CODE (tree_node))
16883     {
16884     case ERROR_MARK:
16885       size = 0;
16886       break;
16887     case ENUMERAL_TYPE:
16888     case RECORD_TYPE:
16889     case UNION_TYPE:
16890     case QUAL_UNION_TYPE:
16891       if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
16892 	  && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
16893 	{
16894 	  add_AT_die_ref (die, DW_AT_byte_size, decl_die);
16895 	  return;
16896 	}
16897       size = int_size_in_bytes (tree_node);
16898       break;
16899     case FIELD_DECL:
16900       /* For a data member of a struct or union, the DW_AT_byte_size is
16901 	 generally given as the number of bytes normally allocated for an
16902 	 object of the *declared* type of the member itself.  This is true
16903 	 even for bit-fields.  */
16904       size = int_size_in_bytes (field_type (tree_node));
16905       break;
16906     default:
16907       gcc_unreachable ();
16908     }
16909 
16910   /* Note that `size' might be -1 when we get to this point.  If it is, that
16911      indicates that the byte size of the entity in question is variable.  We
16912      have no good way of expressing this fact in Dwarf at the present time,
16913      when location description was not used by the caller code instead.  */
16914   if (size >= 0)
16915     add_AT_unsigned (die, DW_AT_byte_size, size);
16916 }
16917 
16918 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16919    which specifies the distance in bits from the highest order bit of the
16920    "containing object" for the bit-field to the highest order bit of the
16921    bit-field itself.
16922 
16923    For any given bit-field, the "containing object" is a hypothetical object
16924    (of some integral or enum type) within which the given bit-field lives.  The
16925    type of this hypothetical "containing object" is always the same as the
16926    declared type of the individual bit-field itself.  The determination of the
16927    exact location of the "containing object" for a bit-field is rather
16928    complicated.  It's handled by the `field_byte_offset' function (above).
16929 
16930    Note that it is the size (in bytes) of the hypothetical "containing object"
16931    which will be given in the DW_AT_byte_size attribute for this bit-field.
16932    (See `byte_size_attribute' above).  */
16933 
16934 static inline void
16935 add_bit_offset_attribute (dw_die_ref die, tree decl)
16936 {
16937   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16938   tree type = DECL_BIT_FIELD_TYPE (decl);
16939   HOST_WIDE_INT bitpos_int;
16940   HOST_WIDE_INT highest_order_object_bit_offset;
16941   HOST_WIDE_INT highest_order_field_bit_offset;
16942   HOST_WIDE_INT bit_offset;
16943 
16944   /* Must be a field and a bit field.  */
16945   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16946 
16947   /* We can't yet handle bit-fields whose offsets are variable, so if we
16948      encounter such things, just return without generating any attribute
16949      whatsoever.  Likewise for variable or too large size.  */
16950   if (! tree_fits_shwi_p (bit_position (decl))
16951       || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
16952     return;
16953 
16954   bitpos_int = int_bit_position (decl);
16955 
16956   /* Note that the bit offset is always the distance (in bits) from the
16957      highest-order bit of the "containing object" to the highest-order bit of
16958      the bit-field itself.  Since the "high-order end" of any object or field
16959      is different on big-endian and little-endian machines, the computation
16960      below must take account of these differences.  */
16961   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
16962   highest_order_field_bit_offset = bitpos_int;
16963 
16964   if (! BYTES_BIG_ENDIAN)
16965     {
16966       highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
16967       highest_order_object_bit_offset += simple_type_size_in_bits (type);
16968     }
16969 
16970   bit_offset
16971     = (! BYTES_BIG_ENDIAN
16972        ? highest_order_object_bit_offset - highest_order_field_bit_offset
16973        : highest_order_field_bit_offset - highest_order_object_bit_offset);
16974 
16975   if (bit_offset < 0)
16976     add_AT_int (die, DW_AT_bit_offset, bit_offset);
16977   else
16978     add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
16979 }
16980 
16981 /* For a FIELD_DECL node which represents a bit field, output an attribute
16982    which specifies the length in bits of the given field.  */
16983 
16984 static inline void
16985 add_bit_size_attribute (dw_die_ref die, tree decl)
16986 {
16987   /* Must be a field and a bit field.  */
16988   gcc_assert (TREE_CODE (decl) == FIELD_DECL
16989 	      && DECL_BIT_FIELD_TYPE (decl));
16990 
16991   if (tree_fits_uhwi_p (DECL_SIZE (decl)))
16992     add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
16993 }
16994 
16995 /* If the compiled language is ANSI C, then add a 'prototyped'
16996    attribute, if arg types are given for the parameters of a function.  */
16997 
16998 static inline void
16999 add_prototyped_attribute (dw_die_ref die, tree func_type)
17000 {
17001   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
17002     {
17003     case DW_LANG_C:
17004     case DW_LANG_C89:
17005     case DW_LANG_C99:
17006     case DW_LANG_C11:
17007     case DW_LANG_ObjC:
17008       if (prototype_p (func_type))
17009 	add_AT_flag (die, DW_AT_prototyped, 1);
17010       break;
17011     default:
17012       break;
17013     }
17014 }
17015 
17016 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
17017    by looking in either the type declaration or object declaration
17018    equate table.  */
17019 
17020 static inline dw_die_ref
17021 add_abstract_origin_attribute (dw_die_ref die, tree origin)
17022 {
17023   dw_die_ref origin_die = NULL;
17024 
17025   if (TREE_CODE (origin) != FUNCTION_DECL)
17026     {
17027       /* We may have gotten separated from the block for the inlined
17028 	 function, if we're in an exception handler or some such; make
17029 	 sure that the abstract function has been written out.
17030 
17031 	 Doing this for nested functions is wrong, however; functions are
17032 	 distinct units, and our context might not even be inline.  */
17033       tree fn = origin;
17034 
17035       if (TYPE_P (fn))
17036 	fn = TYPE_STUB_DECL (fn);
17037 
17038       fn = decl_function_context (fn);
17039       if (fn)
17040 	dwarf2out_abstract_function (fn);
17041     }
17042 
17043   if (DECL_P (origin))
17044     origin_die = lookup_decl_die (origin);
17045   else if (TYPE_P (origin))
17046     origin_die = lookup_type_die (origin);
17047 
17048   /* XXX: Functions that are never lowered don't always have correct block
17049      trees (in the case of java, they simply have no block tree, in some other
17050      languages).  For these functions, there is nothing we can really do to
17051      output correct debug info for inlined functions in all cases.  Rather
17052      than die, we'll just produce deficient debug info now, in that we will
17053      have variables without a proper abstract origin.  In the future, when all
17054      functions are lowered, we should re-add a gcc_assert (origin_die)
17055      here.  */
17056 
17057   if (origin_die)
17058     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
17059   return origin_die;
17060 }
17061 
17062 /* We do not currently support the pure_virtual attribute.  */
17063 
17064 static inline void
17065 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
17066 {
17067   if (DECL_VINDEX (func_decl))
17068     {
17069       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
17070 
17071       if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
17072 	add_AT_loc (die, DW_AT_vtable_elem_location,
17073 		    new_loc_descr (DW_OP_constu,
17074 				   tree_to_shwi (DECL_VINDEX (func_decl)),
17075 				   0));
17076 
17077       /* GNU extension: Record what type this method came from originally.  */
17078       if (debug_info_level > DINFO_LEVEL_TERSE
17079 	  && DECL_CONTEXT (func_decl))
17080 	add_AT_die_ref (die, DW_AT_containing_type,
17081 			lookup_type_die (DECL_CONTEXT (func_decl)));
17082     }
17083 }
17084 
17085 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
17086    given decl.  This used to be a vendor extension until after DWARF 4
17087    standardized it.  */
17088 
17089 static void
17090 add_linkage_attr (dw_die_ref die, tree decl)
17091 {
17092   const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
17093 
17094   /* Mimic what assemble_name_raw does with a leading '*'.  */
17095   if (name[0] == '*')
17096     name = &name[1];
17097 
17098   if (dwarf_version >= 4)
17099     add_AT_string (die, DW_AT_linkage_name, name);
17100   else
17101     add_AT_string (die, DW_AT_MIPS_linkage_name, name);
17102 }
17103 
17104 /* Add source coordinate attributes for the given decl.  */
17105 
17106 static void
17107 add_src_coords_attributes (dw_die_ref die, tree decl)
17108 {
17109   expanded_location s;
17110 
17111   if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
17112     return;
17113   s = expand_location (DECL_SOURCE_LOCATION (decl));
17114   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
17115   add_AT_unsigned (die, DW_AT_decl_line, s.line);
17116 }
17117 
17118 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl.  */
17119 
17120 static void
17121 add_linkage_name (dw_die_ref die, tree decl)
17122 {
17123   if (debug_info_level > DINFO_LEVEL_NONE
17124       && (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
17125       && TREE_PUBLIC (decl)
17126       && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
17127       && die->die_tag != DW_TAG_member)
17128     {
17129       /* Defer until we have an assembler name set.  */
17130       if (!DECL_ASSEMBLER_NAME_SET_P (decl))
17131 	{
17132 	  limbo_die_node *asm_name;
17133 
17134 	  asm_name = ggc_cleared_alloc<limbo_die_node> ();
17135 	  asm_name->die = die;
17136 	  asm_name->created_for = decl;
17137 	  asm_name->next = deferred_asm_name;
17138 	  deferred_asm_name = asm_name;
17139 	}
17140       else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
17141 	add_linkage_attr (die, decl);
17142     }
17143 }
17144 
17145 /* Add a DW_AT_name attribute and source coordinate attribute for the
17146    given decl, but only if it actually has a name.  */
17147 
17148 static void
17149 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
17150 {
17151   tree decl_name;
17152 
17153   decl_name = DECL_NAME (decl);
17154   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
17155     {
17156       const char *name = dwarf2_name (decl, 0);
17157       if (name)
17158 	add_name_attribute (die, name);
17159       if (! DECL_ARTIFICIAL (decl))
17160 	add_src_coords_attributes (die, decl);
17161 
17162       add_linkage_name (die, decl);
17163     }
17164 
17165 #ifdef VMS_DEBUGGING_INFO
17166   /* Get the function's name, as described by its RTL.  This may be different
17167      from the DECL_NAME name used in the source file.  */
17168   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
17169     {
17170       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
17171                   XEXP (DECL_RTL (decl), 0), false);
17172       vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
17173     }
17174 #endif /* VMS_DEBUGGING_INFO */
17175 }
17176 
17177 #ifdef VMS_DEBUGGING_INFO
17178 /* Output the debug main pointer die for VMS */
17179 
17180 void
17181 dwarf2out_vms_debug_main_pointer (void)
17182 {
17183   char label[MAX_ARTIFICIAL_LABEL_BYTES];
17184   dw_die_ref die;
17185 
17186   /* Allocate the VMS debug main subprogram die.  */
17187   die = ggc_cleared_alloc<die_node> ();
17188   die->die_tag = DW_TAG_subprogram;
17189   add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
17190   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
17191 			       current_function_funcdef_no);
17192   add_AT_lbl_id (die, DW_AT_entry_pc, label);
17193 
17194   /* Make it the first child of comp_unit_die ().  */
17195   die->die_parent = comp_unit_die ();
17196   if (comp_unit_die ()->die_child)
17197     {
17198       die->die_sib = comp_unit_die ()->die_child->die_sib;
17199       comp_unit_die ()->die_child->die_sib = die;
17200     }
17201   else
17202     {
17203       die->die_sib = die;
17204       comp_unit_die ()->die_child = die;
17205     }
17206 }
17207 #endif /* VMS_DEBUGGING_INFO */
17208 
17209 /* Push a new declaration scope.  */
17210 
17211 static void
17212 push_decl_scope (tree scope)
17213 {
17214   vec_safe_push (decl_scope_table, scope);
17215 }
17216 
17217 /* Pop a declaration scope.  */
17218 
17219 static inline void
17220 pop_decl_scope (void)
17221 {
17222   decl_scope_table->pop ();
17223 }
17224 
17225 /* walk_tree helper function for uses_local_type, below.  */
17226 
17227 static tree
17228 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
17229 {
17230   if (!TYPE_P (*tp))
17231     *walk_subtrees = 0;
17232   else
17233     {
17234       tree name = TYPE_NAME (*tp);
17235       if (name && DECL_P (name) && decl_function_context (name))
17236 	return *tp;
17237     }
17238   return NULL_TREE;
17239 }
17240 
17241 /* If TYPE involves a function-local type (including a local typedef to a
17242    non-local type), returns that type; otherwise returns NULL_TREE.  */
17243 
17244 static tree
17245 uses_local_type (tree type)
17246 {
17247   tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
17248   return used;
17249 }
17250 
17251 /* Return the DIE for the scope that immediately contains this type.
17252    Non-named types that do not involve a function-local type get global
17253    scope.  Named types nested in namespaces or other types get their
17254    containing scope.  All other types (i.e. function-local named types) get
17255    the current active scope.  */
17256 
17257 static dw_die_ref
17258 scope_die_for (tree t, dw_die_ref context_die)
17259 {
17260   dw_die_ref scope_die = NULL;
17261   tree containing_scope;
17262 
17263   /* Non-types always go in the current scope.  */
17264   gcc_assert (TYPE_P (t));
17265 
17266   /* Use the scope of the typedef, rather than the scope of the type
17267      it refers to.  */
17268   if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
17269     containing_scope = DECL_CONTEXT (TYPE_NAME (t));
17270   else
17271     containing_scope = TYPE_CONTEXT (t);
17272 
17273   /* Use the containing namespace if there is one.  */
17274   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
17275     {
17276       if (context_die == lookup_decl_die (containing_scope))
17277 	/* OK */;
17278       else if (debug_info_level > DINFO_LEVEL_TERSE)
17279 	context_die = get_context_die (containing_scope);
17280       else
17281 	containing_scope = NULL_TREE;
17282     }
17283 
17284   /* Ignore function type "scopes" from the C frontend.  They mean that
17285      a tagged type is local to a parmlist of a function declarator, but
17286      that isn't useful to DWARF.  */
17287   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
17288     containing_scope = NULL_TREE;
17289 
17290   if (SCOPE_FILE_SCOPE_P (containing_scope))
17291     {
17292       /* If T uses a local type keep it local as well, to avoid references
17293 	 to function-local DIEs from outside the function.  */
17294       if (current_function_decl && uses_local_type (t))
17295 	scope_die = context_die;
17296       else
17297 	scope_die = comp_unit_die ();
17298     }
17299   else if (TYPE_P (containing_scope))
17300     {
17301       /* For types, we can just look up the appropriate DIE.  */
17302       if (debug_info_level > DINFO_LEVEL_TERSE)
17303 	scope_die = get_context_die (containing_scope);
17304       else
17305 	{
17306 	  scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
17307 	  if (scope_die == NULL)
17308 	    scope_die = comp_unit_die ();
17309 	}
17310     }
17311   else
17312     scope_die = context_die;
17313 
17314   return scope_die;
17315 }
17316 
17317 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
17318 
17319 static inline int
17320 local_scope_p (dw_die_ref context_die)
17321 {
17322   for (; context_die; context_die = context_die->die_parent)
17323     if (context_die->die_tag == DW_TAG_inlined_subroutine
17324 	|| context_die->die_tag == DW_TAG_subprogram)
17325       return 1;
17326 
17327   return 0;
17328 }
17329 
17330 /* Returns nonzero if CONTEXT_DIE is a class.  */
17331 
17332 static inline int
17333 class_scope_p (dw_die_ref context_die)
17334 {
17335   return (context_die
17336 	  && (context_die->die_tag == DW_TAG_structure_type
17337 	      || context_die->die_tag == DW_TAG_class_type
17338 	      || context_die->die_tag == DW_TAG_interface_type
17339 	      || context_die->die_tag == DW_TAG_union_type));
17340 }
17341 
17342 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17343    whether or not to treat a DIE in this context as a declaration.  */
17344 
17345 static inline int
17346 class_or_namespace_scope_p (dw_die_ref context_die)
17347 {
17348   return (class_scope_p (context_die)
17349 	  || (context_die && context_die->die_tag == DW_TAG_namespace));
17350 }
17351 
17352 /* Many forms of DIEs require a "type description" attribute.  This
17353    routine locates the proper "type descriptor" die for the type given
17354    by 'type' plus any additional qualifiers given by 'cv_quals', and
17355    adds a DW_AT_type attribute below the given die.  */
17356 
17357 static void
17358 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
17359 		    dw_die_ref context_die)
17360 {
17361   enum tree_code code  = TREE_CODE (type);
17362   dw_die_ref type_die  = NULL;
17363 
17364   /* ??? If this type is an unnamed subrange type of an integral, floating-point
17365      or fixed-point type, use the inner type.  This is because we have no
17366      support for unnamed types in base_type_die.  This can happen if this is
17367      an Ada subrange type.  Correct solution is emit a subrange type die.  */
17368   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
17369       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
17370     type = TREE_TYPE (type), code = TREE_CODE (type);
17371 
17372   if (code == ERROR_MARK
17373       /* Handle a special case.  For functions whose return type is void, we
17374 	 generate *no* type attribute.  (Note that no object may have type
17375 	 `void', so this only applies to function return types).  */
17376       || code == VOID_TYPE)
17377     return;
17378 
17379   type_die = modified_type_die (type,
17380 				cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
17381 				context_die);
17382 
17383   if (type_die != NULL)
17384     add_AT_die_ref (object_die, DW_AT_type, type_die);
17385 }
17386 
17387 /* Given an object die, add the calling convention attribute for the
17388    function call type.  */
17389 static void
17390 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
17391 {
17392   enum dwarf_calling_convention value = DW_CC_normal;
17393 
17394   value = ((enum dwarf_calling_convention)
17395 	   targetm.dwarf_calling_convention (TREE_TYPE (decl)));
17396 
17397   if (is_fortran ()
17398       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
17399     {
17400       /* DWARF 2 doesn't provide a way to identify a program's source-level
17401 	entry point.  DW_AT_calling_convention attributes are only meant
17402 	to describe functions' calling conventions.  However, lacking a
17403 	better way to signal the Fortran main program, we used this for
17404 	a long time, following existing custom.  Now, DWARF 4 has
17405 	DW_AT_main_subprogram, which we add below, but some tools still
17406 	rely on the old way, which we thus keep.  */
17407       value = DW_CC_program;
17408 
17409       if (dwarf_version >= 4 || !dwarf_strict)
17410 	add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
17411     }
17412 
17413   /* Only add the attribute if the backend requests it, and
17414      is not DW_CC_normal.  */
17415   if (value && (value != DW_CC_normal))
17416     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
17417 }
17418 
17419 /* Given a tree pointer to a struct, class, union, or enum type node, return
17420    a pointer to the (string) tag name for the given type, or zero if the type
17421    was declared without a tag.  */
17422 
17423 static const char *
17424 type_tag (const_tree type)
17425 {
17426   const char *name = 0;
17427 
17428   if (TYPE_NAME (type) != 0)
17429     {
17430       tree t = 0;
17431 
17432       /* Find the IDENTIFIER_NODE for the type name.  */
17433       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
17434 	  && !TYPE_NAMELESS (type))
17435 	t = TYPE_NAME (type);
17436 
17437       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
17438 	 a TYPE_DECL node, regardless of whether or not a `typedef' was
17439 	 involved.  */
17440       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
17441 	       && ! DECL_IGNORED_P (TYPE_NAME (type)))
17442 	{
17443 	  /* We want to be extra verbose.  Don't call dwarf_name if
17444 	     DECL_NAME isn't set.  The default hook for decl_printable_name
17445 	     doesn't like that, and in this context it's correct to return
17446 	     0, instead of "<anonymous>" or the like.  */
17447 	  if (DECL_NAME (TYPE_NAME (type))
17448 	      && !DECL_NAMELESS (TYPE_NAME (type)))
17449 	    name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
17450 	}
17451 
17452       /* Now get the name as a string, or invent one.  */
17453       if (!name && t != 0)
17454 	name = IDENTIFIER_POINTER (t);
17455     }
17456 
17457   return (name == 0 || *name == '\0') ? 0 : name;
17458 }
17459 
17460 /* Return the type associated with a data member, make a special check
17461    for bit field types.  */
17462 
17463 static inline tree
17464 member_declared_type (const_tree member)
17465 {
17466   return (DECL_BIT_FIELD_TYPE (member)
17467 	  ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
17468 }
17469 
17470 /* Get the decl's label, as described by its RTL. This may be different
17471    from the DECL_NAME name used in the source file.  */
17472 
17473 #if 0
17474 static const char *
17475 decl_start_label (tree decl)
17476 {
17477   rtx x;
17478   const char *fnname;
17479 
17480   x = DECL_RTL (decl);
17481   gcc_assert (MEM_P (x));
17482 
17483   x = XEXP (x, 0);
17484   gcc_assert (GET_CODE (x) == SYMBOL_REF);
17485 
17486   fnname = XSTR (x, 0);
17487   return fnname;
17488 }
17489 #endif
17490 
17491 /* These routines generate the internal representation of the DIE's for
17492    the compilation unit.  Debugging information is collected by walking
17493    the declaration trees passed in from dwarf2out_decl().  */
17494 
17495 static void
17496 gen_array_type_die (tree type, dw_die_ref context_die)
17497 {
17498   dw_die_ref scope_die = scope_die_for (type, context_die);
17499   dw_die_ref array_die;
17500 
17501   /* GNU compilers represent multidimensional array types as sequences of one
17502      dimensional array types whose element types are themselves array types.
17503      We sometimes squish that down to a single array_type DIE with multiple
17504      subscripts in the Dwarf debugging info.  The draft Dwarf specification
17505      say that we are allowed to do this kind of compression in C, because
17506      there is no difference between an array of arrays and a multidimensional
17507      array.  We don't do this for Ada to remain as close as possible to the
17508      actual representation, which is especially important against the language
17509      flexibilty wrt arrays of variable size.  */
17510 
17511   bool collapse_nested_arrays = !is_ada ();
17512   tree element_type;
17513 
17514   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
17515      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
17516   if (TYPE_STRING_FLAG (type)
17517       && TREE_CODE (type) == ARRAY_TYPE
17518       && is_fortran ()
17519       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
17520     {
17521       HOST_WIDE_INT size;
17522 
17523       array_die = new_die (DW_TAG_string_type, scope_die, type);
17524       add_name_attribute (array_die, type_tag (type));
17525       equate_type_number_to_die (type, array_die);
17526       size = int_size_in_bytes (type);
17527       if (size >= 0)
17528 	add_AT_unsigned (array_die, DW_AT_byte_size, size);
17529       else if (TYPE_DOMAIN (type) != NULL_TREE
17530 	       && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
17531 	       && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17532 	{
17533 	  tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17534 	  dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2, NULL);
17535 
17536 	  size = int_size_in_bytes (TREE_TYPE (szdecl));
17537 	  if (loc && size > 0)
17538 	    {
17539 	      add_AT_location_description (array_die, DW_AT_string_length, loc);
17540 	      if (size != DWARF2_ADDR_SIZE)
17541 		add_AT_unsigned (array_die, DW_AT_byte_size, size);
17542 	    }
17543 	}
17544       return;
17545     }
17546 
17547   array_die = new_die (DW_TAG_array_type, scope_die, type);
17548   add_name_attribute (array_die, type_tag (type));
17549   equate_type_number_to_die (type, array_die);
17550 
17551   if (TREE_CODE (type) == VECTOR_TYPE)
17552     add_AT_flag (array_die, DW_AT_GNU_vector, 1);
17553 
17554   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
17555   if (is_fortran ()
17556       && TREE_CODE (type) == ARRAY_TYPE
17557       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17558       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17559     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17560 
17561 #if 0
17562   /* We default the array ordering.  SDB will probably do
17563      the right things even if DW_AT_ordering is not present.  It's not even
17564      an issue until we start to get into multidimensional arrays anyway.  If
17565      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17566      then we'll have to put the DW_AT_ordering attribute back in.  (But if
17567      and when we find out that we need to put these in, we will only do so
17568      for multidimensional arrays.  */
17569   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17570 #endif
17571 
17572   if (TREE_CODE (type) == VECTOR_TYPE)
17573     {
17574       /* For VECTOR_TYPEs we use an array die with appropriate bounds.  */
17575       dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
17576       add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
17577       add_bound_info (subrange_die, DW_AT_upper_bound,
17578 		      size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
17579     }
17580   else
17581     add_subscript_info (array_die, type, collapse_nested_arrays);
17582 
17583   /* Add representation of the type of the elements of this array type and
17584      emit the corresponding DIE if we haven't done it already.  */
17585   element_type = TREE_TYPE (type);
17586   if (collapse_nested_arrays)
17587     while (TREE_CODE (element_type) == ARRAY_TYPE)
17588       {
17589 	if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17590 	  break;
17591 	element_type = TREE_TYPE (element_type);
17592       }
17593 
17594   add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED, context_die);
17595 
17596   add_gnat_descriptive_type_attribute (array_die, type, context_die);
17597   if (TYPE_ARTIFICIAL (type))
17598     add_AT_flag (array_die, DW_AT_artificial, 1);
17599 
17600   if (get_AT (array_die, DW_AT_name))
17601     add_pubtype (type, array_die);
17602 }
17603 
17604 /* This routine generates DIE for array with hidden descriptor, details
17605    are filled into *info by a langhook.  */
17606 
17607 static void
17608 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17609 			  dw_die_ref context_die)
17610 {
17611   const dw_die_ref scope_die = scope_die_for (type, context_die);
17612   const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
17613   const struct loc_descr_context context = { type, info->base_decl };
17614   int dim;
17615 
17616   add_name_attribute (array_die, type_tag (type));
17617   equate_type_number_to_die (type, array_die);
17618 
17619   if (info->ndimensions > 1)
17620     switch (info->ordering)
17621       {
17622       case array_descr_ordering_row_major:
17623 	add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17624 	break;
17625       case array_descr_ordering_column_major:
17626 	add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17627 	break;
17628       default:
17629 	break;
17630       }
17631 
17632   if (dwarf_version >= 3 || !dwarf_strict)
17633     {
17634       if (info->data_location)
17635 	add_scalar_info (array_die, DW_AT_data_location, info->data_location,
17636 			 dw_scalar_form_exprloc, &context);
17637       if (info->associated)
17638 	add_scalar_info (array_die, DW_AT_associated, info->associated,
17639 			 dw_scalar_form_constant
17640 			 | dw_scalar_form_exprloc
17641 			 | dw_scalar_form_reference, &context);
17642       if (info->allocated)
17643 	add_scalar_info (array_die, DW_AT_allocated, info->allocated,
17644 			 dw_scalar_form_constant
17645 			 | dw_scalar_form_exprloc
17646 			 | dw_scalar_form_reference, &context);
17647     }
17648 
17649   add_gnat_descriptive_type_attribute (array_die, type, context_die);
17650 
17651   for (dim = 0; dim < info->ndimensions; dim++)
17652     {
17653       dw_die_ref subrange_die
17654 	= new_die (DW_TAG_subrange_type, array_die, NULL);
17655 
17656       if (info->dimen[dim].bounds_type)
17657 	add_type_attribute (subrange_die,
17658 			    info->dimen[dim].bounds_type, 0,
17659 			    context_die);
17660       if (info->dimen[dim].lower_bound)
17661 	add_bound_info (subrange_die, DW_AT_lower_bound,
17662 			info->dimen[dim].lower_bound, &context);
17663       if (info->dimen[dim].upper_bound)
17664 	add_bound_info (subrange_die, DW_AT_upper_bound,
17665 			info->dimen[dim].upper_bound, &context);
17666       if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
17667 	add_scalar_info (subrange_die, DW_AT_byte_stride,
17668 			 info->dimen[dim].stride,
17669 			 dw_scalar_form_constant
17670 			 | dw_scalar_form_exprloc
17671 			 | dw_scalar_form_reference,
17672 			 &context);
17673     }
17674 
17675   gen_type_die (info->element_type, context_die);
17676   add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
17677 		      context_die);
17678 
17679   if (get_AT (array_die, DW_AT_name))
17680     add_pubtype (type, array_die);
17681 }
17682 
17683 #if 0
17684 static void
17685 gen_entry_point_die (tree decl, dw_die_ref context_die)
17686 {
17687   tree origin = decl_ultimate_origin (decl);
17688   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17689 
17690   if (origin != NULL)
17691     add_abstract_origin_attribute (decl_die, origin);
17692   else
17693     {
17694       add_name_and_src_coords_attributes (decl_die, decl);
17695       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17696 			  TYPE_UNQUALIFIED, context_die);
17697     }
17698 
17699   if (DECL_ABSTRACT_P (decl))
17700     equate_decl_number_to_die (decl, decl_die);
17701   else
17702     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17703 }
17704 #endif
17705 
17706 /* Walk through the list of incomplete types again, trying once more to
17707    emit full debugging info for them.  */
17708 
17709 static void
17710 retry_incomplete_types (void)
17711 {
17712   int i;
17713 
17714   for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17715     if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17716       gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17717 }
17718 
17719 /* Determine what tag to use for a record type.  */
17720 
17721 static enum dwarf_tag
17722 record_type_tag (tree type)
17723 {
17724   if (! lang_hooks.types.classify_record)
17725     return DW_TAG_structure_type;
17726 
17727   switch (lang_hooks.types.classify_record (type))
17728     {
17729     case RECORD_IS_STRUCT:
17730       return DW_TAG_structure_type;
17731 
17732     case RECORD_IS_CLASS:
17733       return DW_TAG_class_type;
17734 
17735     case RECORD_IS_INTERFACE:
17736       if (dwarf_version >= 3 || !dwarf_strict)
17737 	return DW_TAG_interface_type;
17738       return DW_TAG_structure_type;
17739 
17740     default:
17741       gcc_unreachable ();
17742     }
17743 }
17744 
17745 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
17746    include all of the information about the enumeration values also. Each
17747    enumerated type name/value is listed as a child of the enumerated type
17748    DIE.  */
17749 
17750 static dw_die_ref
17751 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17752 {
17753   dw_die_ref type_die = lookup_type_die (type);
17754 
17755   if (type_die == NULL)
17756     {
17757       type_die = new_die (DW_TAG_enumeration_type,
17758 			  scope_die_for (type, context_die), type);
17759       equate_type_number_to_die (type, type_die);
17760       add_name_attribute (type_die, type_tag (type));
17761       if (dwarf_version >= 4 || !dwarf_strict)
17762 	{
17763 	  if (ENUM_IS_SCOPED (type))
17764 	    add_AT_flag (type_die, DW_AT_enum_class, 1);
17765 	  if (ENUM_IS_OPAQUE (type))
17766 	    add_AT_flag (type_die, DW_AT_declaration, 1);
17767 	}
17768     }
17769   else if (! TYPE_SIZE (type))
17770     return type_die;
17771   else
17772     remove_AT (type_die, DW_AT_declaration);
17773 
17774   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
17775      given enum type is incomplete, do not generate the DW_AT_byte_size
17776      attribute or the DW_AT_element_list attribute.  */
17777   if (TYPE_SIZE (type))
17778     {
17779       tree link;
17780 
17781       TREE_ASM_WRITTEN (type) = 1;
17782       add_byte_size_attribute (type_die, type);
17783       if (dwarf_version >= 3 || !dwarf_strict)
17784 	{
17785 	  tree underlying = lang_hooks.types.enum_underlying_base_type (type);
17786 	  add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED,
17787 			      context_die);
17788 	}
17789       if (TYPE_STUB_DECL (type) != NULL_TREE)
17790 	{
17791 	  add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17792 	  add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17793 	}
17794 
17795       /* If the first reference to this type was as the return type of an
17796 	 inline function, then it may not have a parent.  Fix this now.  */
17797       if (type_die->die_parent == NULL)
17798 	add_child_die (scope_die_for (type, context_die), type_die);
17799 
17800       for (link = TYPE_VALUES (type);
17801 	   link != NULL; link = TREE_CHAIN (link))
17802 	{
17803 	  dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17804 	  tree value = TREE_VALUE (link);
17805 
17806 	  add_name_attribute (enum_die,
17807 			      IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17808 
17809 	  if (TREE_CODE (value) == CONST_DECL)
17810 	    value = DECL_INITIAL (value);
17811 
17812 	  if (simple_type_size_in_bits (TREE_TYPE (value))
17813 	      <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
17814 	    {
17815 	      /* For constant forms created by add_AT_unsigned DWARF
17816 		 consumers (GDB, elfutils, etc.) always zero extend
17817 		 the value.  Only when the actual value is negative
17818 		 do we need to use add_AT_int to generate a constant
17819 		 form that can represent negative values.  */
17820 	      HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
17821 	      if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
17822 		add_AT_unsigned (enum_die, DW_AT_const_value,
17823 				 (unsigned HOST_WIDE_INT) val);
17824 	      else
17825 		add_AT_int (enum_die, DW_AT_const_value, val);
17826 	    }
17827 	  else
17828 	    /* Enumeration constants may be wider than HOST_WIDE_INT.  Handle
17829 	       that here.  TODO: This should be re-worked to use correct
17830 	       signed/unsigned double tags for all cases.  */
17831 	    add_AT_wide (enum_die, DW_AT_const_value, value);
17832 	}
17833 
17834       add_gnat_descriptive_type_attribute (type_die, type, context_die);
17835       if (TYPE_ARTIFICIAL (type))
17836 	add_AT_flag (type_die, DW_AT_artificial, 1);
17837     }
17838   else
17839     add_AT_flag (type_die, DW_AT_declaration, 1);
17840 
17841   add_pubtype (type, type_die);
17842 
17843   return type_die;
17844 }
17845 
17846 /* Generate a DIE to represent either a real live formal parameter decl or to
17847    represent just the type of some formal parameter position in some function
17848    type.
17849 
17850    Note that this routine is a bit unusual because its argument may be a
17851    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17852    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17853    node.  If it's the former then this function is being called to output a
17854    DIE to represent a formal parameter object (or some inlining thereof).  If
17855    it's the latter, then this function is only being called to output a
17856    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17857    argument type of some subprogram type.
17858    If EMIT_NAME_P is true, name and source coordinate attributes
17859    are emitted.  */
17860 
17861 static dw_die_ref
17862 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17863 			  dw_die_ref context_die)
17864 {
17865   tree node_or_origin = node ? node : origin;
17866   tree ultimate_origin;
17867   dw_die_ref parm_die
17868     = new_die (DW_TAG_formal_parameter, context_die, node);
17869 
17870   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
17871     {
17872     case tcc_declaration:
17873       ultimate_origin = decl_ultimate_origin (node_or_origin);
17874       if (node || ultimate_origin)
17875 	origin = ultimate_origin;
17876       if (origin != NULL)
17877 	add_abstract_origin_attribute (parm_die, origin);
17878       else if (emit_name_p)
17879 	add_name_and_src_coords_attributes (parm_die, node);
17880       if (origin == NULL
17881 	  || (! DECL_ABSTRACT_P (node_or_origin)
17882 	      && variably_modified_type_p (TREE_TYPE (node_or_origin),
17883 					   decl_function_context
17884 							    (node_or_origin))))
17885 	{
17886 	  tree type = TREE_TYPE (node_or_origin);
17887 	  if (decl_by_reference_p (node_or_origin))
17888 	    add_type_attribute (parm_die, TREE_TYPE (type),
17889 				TYPE_UNQUALIFIED, context_die);
17890 	  else
17891 	    add_type_attribute (parm_die, type,
17892 				decl_quals (node_or_origin),
17893 				context_die);
17894 	}
17895       if (origin == NULL && DECL_ARTIFICIAL (node))
17896 	add_AT_flag (parm_die, DW_AT_artificial, 1);
17897 
17898       if (node && node != origin)
17899         equate_decl_number_to_die (node, parm_die);
17900       if (! DECL_ABSTRACT_P (node_or_origin))
17901 	add_location_or_const_value_attribute (parm_die, node_or_origin,
17902 					       node == NULL, DW_AT_location);
17903 
17904       break;
17905 
17906     case tcc_type:
17907       /* We were called with some kind of a ..._TYPE node.  */
17908       add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED,
17909 			  context_die);
17910       break;
17911 
17912     default:
17913       gcc_unreachable ();
17914     }
17915 
17916   return parm_die;
17917 }
17918 
17919 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17920    children DW_TAG_formal_parameter DIEs representing the arguments of the
17921    parameter pack.
17922 
17923    PARM_PACK must be a function parameter pack.
17924    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17925    must point to the subsequent arguments of the function PACK_ARG belongs to.
17926    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17927    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17928    following the last one for which a DIE was generated.  */
17929 
17930 static dw_die_ref
17931 gen_formal_parameter_pack_die  (tree parm_pack,
17932 				tree pack_arg,
17933 				dw_die_ref subr_die,
17934 				tree *next_arg)
17935 {
17936   tree arg;
17937   dw_die_ref parm_pack_die;
17938 
17939   gcc_assert (parm_pack
17940 	      && lang_hooks.function_parameter_pack_p (parm_pack)
17941 	      && subr_die);
17942 
17943   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17944   add_src_coords_attributes (parm_pack_die, parm_pack);
17945 
17946   for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
17947     {
17948       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17949 								 parm_pack))
17950 	break;
17951       gen_formal_parameter_die (arg, NULL,
17952 				false /* Don't emit name attribute.  */,
17953 				parm_pack_die);
17954     }
17955   if (next_arg)
17956     *next_arg = arg;
17957   return parm_pack_die;
17958 }
17959 
17960 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17961    at the end of an (ANSI prototyped) formal parameters list.  */
17962 
17963 static void
17964 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
17965 {
17966   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
17967 }
17968 
17969 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
17970    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
17971    parameters as specified in some function type specification (except for
17972    those which appear as part of a function *definition*).  */
17973 
17974 static void
17975 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
17976 {
17977   tree link;
17978   tree formal_type = NULL;
17979   tree first_parm_type;
17980   tree arg;
17981 
17982   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
17983     {
17984       arg = DECL_ARGUMENTS (function_or_method_type);
17985       function_or_method_type = TREE_TYPE (function_or_method_type);
17986     }
17987   else
17988     arg = NULL_TREE;
17989 
17990   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
17991 
17992   /* Make our first pass over the list of formal parameter types and output a
17993      DW_TAG_formal_parameter DIE for each one.  */
17994   for (link = first_parm_type; link; )
17995     {
17996       dw_die_ref parm_die;
17997 
17998       formal_type = TREE_VALUE (link);
17999       if (formal_type == void_type_node)
18000 	break;
18001 
18002       /* Output a (nameless) DIE to represent the formal parameter itself.  */
18003       if (!POINTER_BOUNDS_TYPE_P (formal_type))
18004 	{
18005 	  parm_die = gen_formal_parameter_die (formal_type, NULL,
18006 					       true /* Emit name attribute.  */,
18007 					       context_die);
18008 	  if (TREE_CODE (function_or_method_type) == METHOD_TYPE
18009 	      && link == first_parm_type)
18010 	    {
18011 	      add_AT_flag (parm_die, DW_AT_artificial, 1);
18012 	      if (dwarf_version >= 3 || !dwarf_strict)
18013 		add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
18014 	    }
18015 	  else if (arg && DECL_ARTIFICIAL (arg))
18016 	    add_AT_flag (parm_die, DW_AT_artificial, 1);
18017 	}
18018 
18019       link = TREE_CHAIN (link);
18020       if (arg)
18021 	arg = DECL_CHAIN (arg);
18022     }
18023 
18024   /* If this function type has an ellipsis, add a
18025      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
18026   if (formal_type != void_type_node)
18027     gen_unspecified_parameters_die (function_or_method_type, context_die);
18028 
18029   /* Make our second (and final) pass over the list of formal parameter types
18030      and output DIEs to represent those types (as necessary).  */
18031   for (link = TYPE_ARG_TYPES (function_or_method_type);
18032        link && TREE_VALUE (link);
18033        link = TREE_CHAIN (link))
18034     gen_type_die (TREE_VALUE (link), context_die);
18035 }
18036 
18037 /* We want to generate the DIE for TYPE so that we can generate the
18038    die for MEMBER, which has been defined; we will need to refer back
18039    to the member declaration nested within TYPE.  If we're trying to
18040    generate minimal debug info for TYPE, processing TYPE won't do the
18041    trick; we need to attach the member declaration by hand.  */
18042 
18043 static void
18044 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
18045 {
18046   gen_type_die (type, context_die);
18047 
18048   /* If we're trying to avoid duplicate debug info, we may not have
18049      emitted the member decl for this function.  Emit it now.  */
18050   if (TYPE_STUB_DECL (type)
18051       && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
18052       && ! lookup_decl_die (member))
18053     {
18054       dw_die_ref type_die;
18055       gcc_assert (!decl_ultimate_origin (member));
18056 
18057       push_decl_scope (type);
18058       type_die = lookup_type_die_strip_naming_typedef (type);
18059       if (TREE_CODE (member) == FUNCTION_DECL)
18060 	gen_subprogram_die (member, type_die);
18061       else if (TREE_CODE (member) == FIELD_DECL)
18062 	{
18063 	  /* Ignore the nameless fields that are used to skip bits but handle
18064 	     C++ anonymous unions and structs.  */
18065 	  if (DECL_NAME (member) != NULL_TREE
18066 	      || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
18067 	      || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
18068 	    {
18069 	      gen_type_die (member_declared_type (member), type_die);
18070 	      gen_field_die (member, type_die);
18071 	    }
18072 	}
18073       else
18074 	gen_variable_die (member, NULL_TREE, type_die);
18075 
18076       pop_decl_scope ();
18077     }
18078 }
18079 
18080 /* Forward declare these functions, because they are mutually recursive
18081   with their set_block_* pairing functions.  */
18082 static void set_decl_origin_self (tree);
18083 static void set_decl_abstract_flags (tree, vec<tree> &);
18084 
18085 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
18086    given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
18087    that it points to the node itself, thus indicating that the node is its
18088    own (abstract) origin.  Additionally, if the BLOCK_ABSTRACT_ORIGIN for
18089    the given node is NULL, recursively descend the decl/block tree which
18090    it is the root of, and for each other ..._DECL or BLOCK node contained
18091    therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
18092    still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
18093    values to point to themselves.  */
18094 
18095 static void
18096 set_block_origin_self (tree stmt)
18097 {
18098   if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
18099     {
18100       BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
18101 
18102       {
18103 	tree local_decl;
18104 
18105 	for (local_decl = BLOCK_VARS (stmt);
18106 	     local_decl != NULL_TREE;
18107 	     local_decl = DECL_CHAIN (local_decl))
18108 	  /* Do not recurse on nested functions since the inlining status
18109 	     of parent and child can be different as per the DWARF spec.  */
18110 	  if (TREE_CODE (local_decl) != FUNCTION_DECL
18111 	      && !DECL_EXTERNAL (local_decl))
18112 	    set_decl_origin_self (local_decl);
18113       }
18114 
18115       {
18116 	tree subblock;
18117 
18118 	for (subblock = BLOCK_SUBBLOCKS (stmt);
18119 	     subblock != NULL_TREE;
18120 	     subblock = BLOCK_CHAIN (subblock))
18121 	  set_block_origin_self (subblock);	/* Recurse.  */
18122       }
18123     }
18124 }
18125 
18126 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
18127    the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
18128    node to so that it points to the node itself, thus indicating that the
18129    node represents its own (abstract) origin.  Additionally, if the
18130    DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
18131    the decl/block tree of which the given node is the root of, and for
18132    each other ..._DECL or BLOCK node contained therein whose
18133    DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
18134    set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
18135    point to themselves.  */
18136 
18137 static void
18138 set_decl_origin_self (tree decl)
18139 {
18140   if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
18141     {
18142       DECL_ABSTRACT_ORIGIN (decl) = decl;
18143       if (TREE_CODE (decl) == FUNCTION_DECL)
18144 	{
18145 	  tree arg;
18146 
18147 	  for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
18148 	    DECL_ABSTRACT_ORIGIN (arg) = arg;
18149 	  if (DECL_INITIAL (decl) != NULL_TREE
18150 	      && DECL_INITIAL (decl) != error_mark_node)
18151 	    set_block_origin_self (DECL_INITIAL (decl));
18152 	}
18153     }
18154 }
18155 
18156 /* Given a pointer to some BLOCK node, set the BLOCK_ABSTRACT flag to 1
18157    and if it wasn't 1 before, push it to abstract_vec vector.
18158    For all local decls and all local sub-blocks (recursively) do it
18159    too.  */
18160 
18161 static void
18162 set_block_abstract_flags (tree stmt, vec<tree> &abstract_vec)
18163 {
18164   tree local_decl;
18165   tree subblock;
18166   unsigned int i;
18167 
18168   if (!BLOCK_ABSTRACT (stmt))
18169     {
18170       abstract_vec.safe_push (stmt);
18171       BLOCK_ABSTRACT (stmt) = 1;
18172     }
18173 
18174   for (local_decl = BLOCK_VARS (stmt);
18175        local_decl != NULL_TREE;
18176        local_decl = DECL_CHAIN (local_decl))
18177     if (! DECL_EXTERNAL (local_decl))
18178       set_decl_abstract_flags (local_decl, abstract_vec);
18179 
18180   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
18181     {
18182       local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
18183       if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
18184 	  || TREE_CODE (local_decl) == PARM_DECL)
18185 	set_decl_abstract_flags (local_decl, abstract_vec);
18186     }
18187 
18188   for (subblock = BLOCK_SUBBLOCKS (stmt);
18189        subblock != NULL_TREE;
18190        subblock = BLOCK_CHAIN (subblock))
18191     set_block_abstract_flags (subblock, abstract_vec);
18192 }
18193 
18194 /* Given a pointer to some ..._DECL node, set DECL_ABSTRACT_P flag on it
18195    to 1 and if it wasn't 1 before, push to abstract_vec vector.
18196    In the case where the decl is a FUNCTION_DECL also set the abstract
18197    flags for all of the parameters, local vars, local
18198    blocks and sub-blocks (recursively).  */
18199 
18200 static void
18201 set_decl_abstract_flags (tree decl, vec<tree> &abstract_vec)
18202 {
18203   if (!DECL_ABSTRACT_P (decl))
18204     {
18205       abstract_vec.safe_push (decl);
18206       DECL_ABSTRACT_P (decl) = 1;
18207     }
18208 
18209   if (TREE_CODE (decl) == FUNCTION_DECL)
18210     {
18211       tree arg;
18212 
18213       for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
18214 	if (!DECL_ABSTRACT_P (arg))
18215 	  {
18216 	    abstract_vec.safe_push (arg);
18217 	    DECL_ABSTRACT_P (arg) = 1;
18218 	  }
18219       if (DECL_INITIAL (decl) != NULL_TREE
18220 	  && DECL_INITIAL (decl) != error_mark_node)
18221 	set_block_abstract_flags (DECL_INITIAL (decl), abstract_vec);
18222     }
18223 }
18224 
18225 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18226    may later generate inlined and/or out-of-line instances of.  */
18227 
18228 static void
18229 dwarf2out_abstract_function (tree decl)
18230 {
18231   dw_die_ref old_die;
18232   tree save_fn;
18233   tree context;
18234   hash_table<decl_loc_hasher> *old_decl_loc_table;
18235   hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
18236   int old_call_site_count, old_tail_call_site_count;
18237   struct call_arg_loc_node *old_call_arg_locations;
18238 
18239   /* Make sure we have the actual abstract inline, not a clone.  */
18240   decl = DECL_ORIGIN (decl);
18241 
18242   old_die = lookup_decl_die (decl);
18243   if (old_die && get_AT (old_die, DW_AT_inline))
18244     /* We've already generated the abstract instance.  */
18245     return;
18246 
18247   /* We can be called while recursively when seeing block defining inlined subroutine
18248      DIE.  Be sure to not clobber the outer location table nor use it or we would
18249      get locations in abstract instantces.  */
18250   old_decl_loc_table = decl_loc_table;
18251   decl_loc_table = NULL;
18252   old_cached_dw_loc_list_table = cached_dw_loc_list_table;
18253   cached_dw_loc_list_table = NULL;
18254   old_call_arg_locations = call_arg_locations;
18255   call_arg_locations = NULL;
18256   old_call_site_count = call_site_count;
18257   call_site_count = -1;
18258   old_tail_call_site_count = tail_call_site_count;
18259   tail_call_site_count = -1;
18260 
18261   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18262      we don't get confused by DECL_ABSTRACT_P.  */
18263   if (debug_info_level > DINFO_LEVEL_TERSE)
18264     {
18265       context = decl_class_context (decl);
18266       if (context)
18267 	gen_type_die_for_member
18268 	  (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
18269     }
18270 
18271   /* Pretend we've just finished compiling this function.  */
18272   save_fn = current_function_decl;
18273   current_function_decl = decl;
18274 
18275   auto_vec<tree, 64> abstract_vec;
18276   set_decl_abstract_flags (decl, abstract_vec);
18277   dwarf2out_decl (decl);
18278   unsigned int i;
18279   tree t;
18280   FOR_EACH_VEC_ELT (abstract_vec, i, t)
18281     if (TREE_CODE (t) == BLOCK)
18282       BLOCK_ABSTRACT (t) = 0;
18283     else
18284       DECL_ABSTRACT_P (t) = 0;
18285 
18286   current_function_decl = save_fn;
18287   decl_loc_table = old_decl_loc_table;
18288   cached_dw_loc_list_table = old_cached_dw_loc_list_table;
18289   call_arg_locations = old_call_arg_locations;
18290   call_site_count = old_call_site_count;
18291   tail_call_site_count = old_tail_call_site_count;
18292 }
18293 
18294 /* Helper function of premark_used_types() which gets called through
18295    htab_traverse.
18296 
18297    Marks the DIE of a given type in *SLOT as perennial, so it never gets
18298    marked as unused by prune_unused_types.  */
18299 
18300 bool
18301 premark_used_types_helper (tree const &type, void *)
18302 {
18303   dw_die_ref die;
18304 
18305   die = lookup_type_die (type);
18306   if (die != NULL)
18307     die->die_perennial_p = 1;
18308   return true;
18309 }
18310 
18311 /* Helper function of premark_types_used_by_global_vars which gets called
18312    through htab_traverse.
18313 
18314    Marks the DIE of a given type in *SLOT as perennial, so it never gets
18315    marked as unused by prune_unused_types. The DIE of the type is marked
18316    only if the global variable using the type will actually be emitted.  */
18317 
18318 int
18319 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
18320 					  void *)
18321 {
18322   struct types_used_by_vars_entry *entry;
18323   dw_die_ref die;
18324 
18325   entry = (struct types_used_by_vars_entry *) *slot;
18326   gcc_assert (entry->type != NULL
18327 	      && entry->var_decl != NULL);
18328   die = lookup_type_die (entry->type);
18329   if (die)
18330     {
18331       /* Ask cgraph if the global variable really is to be emitted.
18332          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
18333       varpool_node *node = varpool_node::get (entry->var_decl);
18334       if (node && node->definition)
18335 	{
18336 	  die->die_perennial_p = 1;
18337 	  /* Keep the parent DIEs as well.  */
18338 	  while ((die = die->die_parent) && die->die_perennial_p == 0)
18339 	    die->die_perennial_p = 1;
18340 	}
18341     }
18342   return 1;
18343 }
18344 
18345 /* Mark all members of used_types_hash as perennial.  */
18346 
18347 static void
18348 premark_used_types (struct function *fun)
18349 {
18350   if (fun && fun->used_types_hash)
18351     fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
18352 }
18353 
18354 /* Mark all members of types_used_by_vars_entry as perennial.  */
18355 
18356 static void
18357 premark_types_used_by_global_vars (void)
18358 {
18359   if (types_used_by_vars_hash)
18360     types_used_by_vars_hash
18361       ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
18362 }
18363 
18364 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
18365    for CA_LOC call arg loc node.  */
18366 
18367 static dw_die_ref
18368 gen_call_site_die (tree decl, dw_die_ref subr_die,
18369 		   struct call_arg_loc_node *ca_loc)
18370 {
18371   dw_die_ref stmt_die = NULL, die;
18372   tree block = ca_loc->block;
18373 
18374   while (block
18375 	 && block != DECL_INITIAL (decl)
18376 	 && TREE_CODE (block) == BLOCK)
18377     {
18378       if (block_map.length () > BLOCK_NUMBER (block))
18379 	stmt_die = block_map[BLOCK_NUMBER (block)];
18380       if (stmt_die)
18381 	break;
18382       block = BLOCK_SUPERCONTEXT (block);
18383     }
18384   if (stmt_die == NULL)
18385     stmt_die = subr_die;
18386   die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
18387   add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
18388   if (ca_loc->tail_call_p)
18389     add_AT_flag (die, DW_AT_GNU_tail_call, 1);
18390   if (ca_loc->symbol_ref)
18391     {
18392       dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
18393       if (tdie)
18394 	add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
18395       else
18396         add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
18397     }
18398   return die;
18399 }
18400 
18401 /* Generate a DIE to represent a declared function (either file-scope or
18402    block-local).  */
18403 
18404 static void
18405 gen_subprogram_die (tree decl, dw_die_ref context_die)
18406 {
18407   tree origin = decl_ultimate_origin (decl);
18408   dw_die_ref subr_die;
18409   tree outer_scope;
18410   dw_die_ref old_die = lookup_decl_die (decl);
18411   int declaration = (current_function_decl != decl
18412 		     || class_or_namespace_scope_p (context_die));
18413 
18414   premark_used_types (DECL_STRUCT_FUNCTION (decl));
18415 
18416   /* It is possible to have both DECL_ABSTRACT_P and DECLARATION be true if we
18417      started to generate the abstract instance of an inline, decided to output
18418      its containing class, and proceeded to emit the declaration of the inline
18419      from the member list for the class.  If so, DECLARATION takes priority;
18420      we'll get back to the abstract instance when done with the class.  */
18421 
18422   /* The class-scope declaration DIE must be the primary DIE.  */
18423   if (origin && declaration && class_or_namespace_scope_p (context_die))
18424     {
18425       origin = NULL;
18426       gcc_assert (!old_die);
18427     }
18428 
18429   /* Now that the C++ front end lazily declares artificial member fns, we
18430      might need to retrofit the declaration into its class.  */
18431   if (!declaration && !origin && !old_die
18432       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18433       && !class_or_namespace_scope_p (context_die)
18434       && debug_info_level > DINFO_LEVEL_TERSE)
18435     old_die = force_decl_die (decl);
18436 
18437   if (origin != NULL)
18438     {
18439       gcc_assert (!declaration || local_scope_p (context_die));
18440 
18441       /* Fixup die_parent for the abstract instance of a nested
18442 	 inline function.  */
18443       if (old_die && old_die->die_parent == NULL)
18444 	add_child_die (context_die, old_die);
18445 
18446       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18447       add_abstract_origin_attribute (subr_die, origin);
18448       /*  This is where the actual code for a cloned function is.
18449 	  Let's emit linkage name attribute for it.  This helps
18450 	  debuggers to e.g, set breakpoints into
18451 	  constructors/destructors when the user asks "break
18452 	  K::K".  */
18453       add_linkage_name (subr_die, decl);
18454     }
18455   else if (old_die)
18456     {
18457       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18458       struct dwarf_file_data * file_index = lookup_filename (s.file);
18459 
18460       if (!get_AT_flag (old_die, DW_AT_declaration)
18461 	  /* We can have a normal definition following an inline one in the
18462 	     case of redefinition of GNU C extern inlines.
18463 	     It seems reasonable to use AT_specification in this case.  */
18464 	  && !get_AT (old_die, DW_AT_inline))
18465 	{
18466 	  /* Detect and ignore this case, where we are trying to output
18467 	     something we have already output.  */
18468 	  return;
18469 	}
18470 
18471       /* If the definition comes from the same place as the declaration,
18472 	 maybe use the old DIE.  We always want the DIE for this function
18473 	 that has the *_pc attributes to be under comp_unit_die so the
18474 	 debugger can find it.  We also need to do this for abstract
18475 	 instances of inlines, since the spec requires the out-of-line copy
18476 	 to have the same parent.  For local class methods, this doesn't
18477 	 apply; we just use the old DIE.  */
18478       if ((is_cu_die (old_die->die_parent) || context_die == NULL)
18479 	  && (DECL_ARTIFICIAL (decl)
18480 	      || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18481 		  && (get_AT_unsigned (old_die, DW_AT_decl_line)
18482 		      == (unsigned) s.line))))
18483 	{
18484 	  subr_die = old_die;
18485 
18486 	  /* Clear out the declaration attribute and the formal parameters.
18487 	     Do not remove all children, because it is possible that this
18488 	     declaration die was forced using force_decl_die(). In such
18489 	     cases die that forced declaration die (e.g. TAG_imported_module)
18490 	     is one of the children that we do not want to remove.  */
18491 	  remove_AT (subr_die, DW_AT_declaration);
18492 	  remove_AT (subr_die, DW_AT_object_pointer);
18493 	  remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18494 	}
18495       else
18496 	{
18497 	  subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18498 	  add_AT_specification (subr_die, old_die);
18499           add_pubname (decl, subr_die);
18500 	  if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18501 	    add_AT_file (subr_die, DW_AT_decl_file, file_index);
18502 	  if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18503 	    add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18504 
18505 	  /* If the prototype had an 'auto' or 'decltype(auto)' return type,
18506 	     emit the real type on the definition die.  */
18507 	  if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
18508 	    {
18509 	      dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
18510 	      if (die == auto_die || die == decltype_auto_die)
18511 		add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18512 				    TYPE_UNQUALIFIED, context_die);
18513 	    }
18514 	}
18515     }
18516   else
18517     {
18518       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18519 
18520       if (TREE_PUBLIC (decl))
18521 	add_AT_flag (subr_die, DW_AT_external, 1);
18522 
18523       add_name_and_src_coords_attributes (subr_die, decl);
18524       add_pubname (decl, subr_die);
18525       if (debug_info_level > DINFO_LEVEL_TERSE)
18526 	{
18527 	  add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18528 	  add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18529 			      TYPE_UNQUALIFIED, context_die);
18530 	}
18531 
18532       add_pure_or_virtual_attribute (subr_die, decl);
18533       if (DECL_ARTIFICIAL (decl))
18534 	add_AT_flag (subr_die, DW_AT_artificial, 1);
18535 
18536       if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
18537 	add_AT_flag (subr_die, DW_AT_noreturn, 1);
18538 
18539       add_accessibility_attribute (subr_die, decl);
18540     }
18541 
18542   if (declaration)
18543     {
18544       if (!old_die || !get_AT (old_die, DW_AT_inline))
18545 	{
18546 	  add_AT_flag (subr_die, DW_AT_declaration, 1);
18547 
18548 	  /* If this is an explicit function declaration then generate
18549 	     a DW_AT_explicit attribute.  */
18550 	  if (lang_hooks.decls.function_decl_explicit_p (decl)
18551 	      && (dwarf_version >= 3 || !dwarf_strict))
18552 	    add_AT_flag (subr_die, DW_AT_explicit, 1);
18553 
18554 	  /* If this is a C++11 deleted special function member then generate
18555 	     a DW_AT_GNU_deleted attribute.  */
18556 	  if (lang_hooks.decls.function_decl_deleted_p (decl)
18557 	      && (! dwarf_strict))
18558 	    add_AT_flag (subr_die, DW_AT_GNU_deleted, 1);
18559 
18560 	  /* The first time we see a member function, it is in the context of
18561 	     the class to which it belongs.  We make sure of this by emitting
18562 	     the class first.  The next time is the definition, which is
18563 	     handled above.  The two may come from the same source text.
18564 
18565 	     Note that force_decl_die() forces function declaration die. It is
18566 	     later reused to represent definition.  */
18567 	  equate_decl_number_to_die (decl, subr_die);
18568 	}
18569     }
18570   else if (DECL_ABSTRACT_P (decl))
18571     {
18572       if (DECL_DECLARED_INLINE_P (decl))
18573 	{
18574 	  if (cgraph_function_possibly_inlined_p (decl))
18575 	    add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18576 	  else
18577 	    add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18578 	}
18579       else
18580 	{
18581 	  if (cgraph_function_possibly_inlined_p (decl))
18582 	    add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18583 	  else
18584 	    add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18585 	}
18586 
18587       if (DECL_DECLARED_INLINE_P (decl)
18588 	  && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18589 	add_AT_flag (subr_die, DW_AT_artificial, 1);
18590 
18591       equate_decl_number_to_die (decl, subr_die);
18592     }
18593   else if (!DECL_EXTERNAL (decl))
18594     {
18595       HOST_WIDE_INT cfa_fb_offset;
18596       struct function *fun = DECL_STRUCT_FUNCTION (decl);
18597 
18598       if (!old_die || !get_AT (old_die, DW_AT_inline))
18599 	equate_decl_number_to_die (decl, subr_die);
18600 
18601       gcc_checking_assert (fun);
18602       if (!flag_reorder_blocks_and_partition)
18603 	{
18604 	  dw_fde_ref fde = fun->fde;
18605 	  if (fde->dw_fde_begin)
18606 	    {
18607 	      /* We have already generated the labels.  */
18608              add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18609                                  fde->dw_fde_end, false);
18610 	    }
18611 	  else
18612 	    {
18613 	      /* Create start/end labels and add the range.  */
18614 	      char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
18615 	      char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
18616 	      ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
18617 					   current_function_funcdef_no);
18618 	      ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
18619 					   current_function_funcdef_no);
18620              add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
18621                                  false);
18622 	    }
18623 
18624 #if VMS_DEBUGGING_INFO
18625       /* HP OpenVMS Industry Standard 64: DWARF Extensions
18626 	 Section 2.3 Prologue and Epilogue Attributes:
18627 	 When a breakpoint is set on entry to a function, it is generally
18628 	 desirable for execution to be suspended, not on the very first
18629 	 instruction of the function, but rather at a point after the
18630 	 function's frame has been set up, after any language defined local
18631 	 declaration processing has been completed, and before execution of
18632 	 the first statement of the function begins. Debuggers generally
18633 	 cannot properly determine where this point is.  Similarly for a
18634 	 breakpoint set on exit from a function. The prologue and epilogue
18635 	 attributes allow a compiler to communicate the location(s) to use.  */
18636 
18637       {
18638         if (fde->dw_fde_vms_end_prologue)
18639           add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18640 	    fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18641 
18642         if (fde->dw_fde_vms_begin_epilogue)
18643           add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18644 	    fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18645       }
18646 #endif
18647 
18648 	}
18649       else
18650 	{
18651 	  /* Generate pubnames entries for the split function code ranges.  */
18652 	  dw_fde_ref fde = fun->fde;
18653 
18654 	  if (fde->dw_fde_second_begin)
18655 	    {
18656 	      if (dwarf_version >= 3 || !dwarf_strict)
18657 		{
18658 		  /* We should use ranges for non-contiguous code section
18659 		     addresses.  Use the actual code range for the initial
18660 		     section, since the HOT/COLD labels might precede an
18661 		     alignment offset.  */
18662 		  bool range_list_added = false;
18663 		  add_ranges_by_labels (subr_die, fde->dw_fde_begin,
18664                                         fde->dw_fde_end, &range_list_added,
18665                                         false);
18666 		  add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
18667 					fde->dw_fde_second_end,
18668                                        &range_list_added, false);
18669 		  if (range_list_added)
18670 		    add_ranges (NULL);
18671 		}
18672 	      else
18673 		{
18674 		  /* There is no real support in DW2 for this .. so we make
18675 		     a work-around.  First, emit the pub name for the segment
18676 		     containing the function label.  Then make and emit a
18677 		     simplified subprogram DIE for the second segment with the
18678 		     name pre-fixed by __hot/cold_sect_of_.  We use the same
18679 		     linkage name for the second die so that gdb will find both
18680 		     sections when given "b foo".  */
18681 		  const char *name = NULL;
18682 		  tree decl_name = DECL_NAME (decl);
18683 		  dw_die_ref seg_die;
18684 
18685 		  /* Do the 'primary' section.   */
18686 		  add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18687                                       fde->dw_fde_end, false);
18688 
18689 		  /* Build a minimal DIE for the secondary section.  */
18690 		  seg_die = new_die (DW_TAG_subprogram,
18691 				     subr_die->die_parent, decl);
18692 
18693 		  if (TREE_PUBLIC (decl))
18694 		    add_AT_flag (seg_die, DW_AT_external, 1);
18695 
18696 		  if (decl_name != NULL
18697 		      && IDENTIFIER_POINTER (decl_name) != NULL)
18698 		    {
18699 		      name = dwarf2_name (decl, 1);
18700 		      if (! DECL_ARTIFICIAL (decl))
18701 			add_src_coords_attributes (seg_die, decl);
18702 
18703 		      add_linkage_name (seg_die, decl);
18704 		    }
18705 		  gcc_assert (name != NULL);
18706 		  add_pure_or_virtual_attribute (seg_die, decl);
18707 		  if (DECL_ARTIFICIAL (decl))
18708 		    add_AT_flag (seg_die, DW_AT_artificial, 1);
18709 
18710 		  name = concat ("__second_sect_of_", name, NULL);
18711 		  add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
18712                                       fde->dw_fde_second_end, false);
18713 		  add_name_attribute (seg_die, name);
18714 		  if (want_pubnames ())
18715 		    add_pubname_string (name, seg_die);
18716 		}
18717 	    }
18718 	  else
18719            add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
18720                                false);
18721 	}
18722 
18723       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
18724 
18725       /* We define the "frame base" as the function's CFA.  This is more
18726 	 convenient for several reasons: (1) It's stable across the prologue
18727 	 and epilogue, which makes it better than just a frame pointer,
18728 	 (2) With dwarf3, there exists a one-byte encoding that allows us
18729 	 to reference the .debug_frame data by proxy, but failing that,
18730 	 (3) We can at least reuse the code inspection and interpretation
18731 	 code that determines the CFA position at various points in the
18732 	 function.  */
18733       if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
18734 	{
18735 	  dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
18736 	  add_AT_loc (subr_die, DW_AT_frame_base, op);
18737 	}
18738       else
18739 	{
18740 	  dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
18741 	  if (list->dw_loc_next)
18742 	    add_AT_loc_list (subr_die, DW_AT_frame_base, list);
18743 	  else
18744 	    add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
18745 	}
18746 
18747       /* Compute a displacement from the "steady-state frame pointer" to
18748 	 the CFA.  The former is what all stack slots and argument slots
18749 	 will reference in the rtl; the latter is what we've told the
18750 	 debugger about.  We'll need to adjust all frame_base references
18751 	 by this displacement.  */
18752       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
18753 
18754       if (fun->static_chain_decl)
18755 	add_AT_location_description (subr_die, DW_AT_static_link,
18756 		 loc_list_from_tree (fun->static_chain_decl, 2, NULL));
18757     }
18758 
18759   /* Generate child dies for template paramaters.  */
18760   if (debug_info_level > DINFO_LEVEL_TERSE)
18761     gen_generic_params_dies (decl);
18762 
18763   /* Now output descriptions of the arguments for this function. This gets
18764      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
18765      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
18766      `...' at the end of the formal parameter list.  In order to find out if
18767      there was a trailing ellipsis or not, we must instead look at the type
18768      associated with the FUNCTION_DECL.  This will be a node of type
18769      FUNCTION_TYPE. If the chain of type nodes hanging off of this
18770      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
18771      an ellipsis at the end.  */
18772 
18773   /* In the case where we are describing a mere function declaration, all we
18774      need to do here (and all we *can* do here) is to describe the *types* of
18775      its formal parameters.  */
18776   if (debug_info_level <= DINFO_LEVEL_TERSE)
18777     ;
18778   else if (declaration)
18779     gen_formal_types_die (decl, subr_die);
18780   else
18781     {
18782       /* Generate DIEs to represent all known formal parameters.  */
18783       tree parm = DECL_ARGUMENTS (decl);
18784       tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
18785       tree generic_decl_parm = generic_decl
18786 				? DECL_ARGUMENTS (generic_decl)
18787 				: NULL;
18788 
18789       /* Now we want to walk the list of parameters of the function and
18790 	 emit their relevant DIEs.
18791 
18792 	 We consider the case of DECL being an instance of a generic function
18793 	 as well as it being a normal function.
18794 
18795 	 If DECL is an instance of a generic function we walk the
18796 	 parameters of the generic function declaration _and_ the parameters of
18797 	 DECL itself. This is useful because we want to emit specific DIEs for
18798 	 function parameter packs and those are declared as part of the
18799 	 generic function declaration. In that particular case,
18800 	 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
18801 	 That DIE has children DIEs representing the set of arguments
18802 	 of the pack. Note that the set of pack arguments can be empty.
18803 	 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
18804 	 children DIE.
18805 
18806 	 Otherwise, we just consider the parameters of DECL.  */
18807       while (generic_decl_parm || parm)
18808 	{
18809 	  if (generic_decl_parm
18810 	      && lang_hooks.function_parameter_pack_p (generic_decl_parm))
18811 	    gen_formal_parameter_pack_die (generic_decl_parm,
18812 					   parm, subr_die,
18813 					   &parm);
18814 	  else if (parm && !POINTER_BOUNDS_P (parm))
18815 	    {
18816 	      dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
18817 
18818 	      if (parm == DECL_ARGUMENTS (decl)
18819 		  && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
18820 		  && parm_die
18821 		  && (dwarf_version >= 3 || !dwarf_strict))
18822 		add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
18823 
18824 	      parm = DECL_CHAIN (parm);
18825 	    }
18826 	  else if (parm)
18827 	    parm = DECL_CHAIN (parm);
18828 
18829 	  if (generic_decl_parm)
18830 	    generic_decl_parm = DECL_CHAIN (generic_decl_parm);
18831 	}
18832 
18833       /* Decide whether we need an unspecified_parameters DIE at the end.
18834 	 There are 2 more cases to do this for: 1) the ansi ... declaration -
18835 	 this is detectable when the end of the arg list is not a
18836 	 void_type_node 2) an unprototyped function declaration (not a
18837 	 definition).  This just means that we have no info about the
18838 	 parameters at all.  */
18839       if (prototype_p (TREE_TYPE (decl)))
18840 	{
18841 	  /* This is the prototyped case, check for....  */
18842 	  if (stdarg_p (TREE_TYPE (decl)))
18843 	    gen_unspecified_parameters_die (decl, subr_die);
18844 	}
18845       else if (DECL_INITIAL (decl) == NULL_TREE)
18846 	gen_unspecified_parameters_die (decl, subr_die);
18847     }
18848 
18849   /* Output Dwarf info for all of the stuff within the body of the function
18850      (if it has one - it may be just a declaration).  */
18851   outer_scope = DECL_INITIAL (decl);
18852 
18853   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
18854      a function.  This BLOCK actually represents the outermost binding contour
18855      for the function, i.e. the contour in which the function's formal
18856      parameters and labels get declared. Curiously, it appears that the front
18857      end doesn't actually put the PARM_DECL nodes for the current function onto
18858      the BLOCK_VARS list for this outer scope, but are strung off of the
18859      DECL_ARGUMENTS list for the function instead.
18860 
18861      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
18862      the LABEL_DECL nodes for the function however, and we output DWARF info
18863      for those in decls_for_scope.  Just within the `outer_scope' there will be
18864      a BLOCK node representing the function's outermost pair of curly braces,
18865      and any blocks used for the base and member initializers of a C++
18866      constructor function.  */
18867   if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
18868     {
18869       int call_site_note_count = 0;
18870       int tail_call_site_note_count = 0;
18871 
18872       /* Emit a DW_TAG_variable DIE for a named return value.  */
18873       if (DECL_NAME (DECL_RESULT (decl)))
18874 	gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18875 
18876       decls_for_scope (outer_scope, subr_die);
18877 
18878       if (call_arg_locations && !dwarf_strict)
18879 	{
18880 	  struct call_arg_loc_node *ca_loc;
18881 	  for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
18882 	    {
18883 	      dw_die_ref die = NULL;
18884 	      rtx tloc = NULL_RTX, tlocc = NULL_RTX;
18885 	      rtx arg, next_arg;
18886 
18887 	      for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
18888 		   arg; arg = next_arg)
18889 		{
18890 		  dw_loc_descr_ref reg, val;
18891 		  machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
18892 		  dw_die_ref cdie, tdie = NULL;
18893 
18894 		  next_arg = XEXP (arg, 1);
18895 		  if (REG_P (XEXP (XEXP (arg, 0), 0))
18896 		      && next_arg
18897 		      && MEM_P (XEXP (XEXP (next_arg, 0), 0))
18898 		      && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
18899 		      && REGNO (XEXP (XEXP (arg, 0), 0))
18900 			 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
18901 		    next_arg = XEXP (next_arg, 1);
18902 		  if (mode == VOIDmode)
18903 		    {
18904 		      mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
18905 		      if (mode == VOIDmode)
18906 			mode = GET_MODE (XEXP (arg, 0));
18907 		    }
18908 		  if (mode == VOIDmode || mode == BLKmode)
18909 		    continue;
18910 		  if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
18911 		    {
18912 		      gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18913 		      tloc = XEXP (XEXP (arg, 0), 1);
18914 		      continue;
18915 		    }
18916 		  else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
18917 			   && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
18918 		    {
18919 		      gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18920 		      tlocc = XEXP (XEXP (arg, 0), 1);
18921 		      continue;
18922 		    }
18923 		  reg = NULL;
18924 		  if (REG_P (XEXP (XEXP (arg, 0), 0)))
18925 		    reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
18926 					      VAR_INIT_STATUS_INITIALIZED);
18927 		  else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
18928 		    {
18929 		      rtx mem = XEXP (XEXP (arg, 0), 0);
18930 		      reg = mem_loc_descriptor (XEXP (mem, 0),
18931 						get_address_mode (mem),
18932 						GET_MODE (mem),
18933 						VAR_INIT_STATUS_INITIALIZED);
18934 		    }
18935 		  else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
18936 			   == DEBUG_PARAMETER_REF)
18937 		    {
18938 		      tree tdecl
18939 			= DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
18940 		      tdie = lookup_decl_die (tdecl);
18941 		      if (tdie == NULL)
18942 			continue;
18943 		    }
18944 		  else
18945 		    continue;
18946 		  if (reg == NULL
18947 		      && GET_CODE (XEXP (XEXP (arg, 0), 0))
18948 			 != DEBUG_PARAMETER_REF)
18949 		    continue;
18950 		  val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
18951 					    VOIDmode,
18952 					    VAR_INIT_STATUS_INITIALIZED);
18953 		  if (val == NULL)
18954 		    continue;
18955 		  if (die == NULL)
18956 		    die = gen_call_site_die (decl, subr_die, ca_loc);
18957 		  cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
18958 				  NULL_TREE);
18959 		  if (reg != NULL)
18960 		    add_AT_loc (cdie, DW_AT_location, reg);
18961 		  else if (tdie != NULL)
18962 		    add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
18963 		  add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
18964 		  if (next_arg != XEXP (arg, 1))
18965 		    {
18966 		      mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
18967 		      if (mode == VOIDmode)
18968 			mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
18969 		      val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
18970 							    0), 1),
18971 						mode, VOIDmode,
18972 						VAR_INIT_STATUS_INITIALIZED);
18973 		      if (val != NULL)
18974 			add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
18975 		    }
18976 		}
18977 	      if (die == NULL
18978 		  && (ca_loc->symbol_ref || tloc))
18979 		die = gen_call_site_die (decl, subr_die, ca_loc);
18980 	      if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
18981 		{
18982 		  dw_loc_descr_ref tval = NULL;
18983 
18984 		  if (tloc != NULL_RTX)
18985 		    tval = mem_loc_descriptor (tloc,
18986 					       GET_MODE (tloc) == VOIDmode
18987 					       ? Pmode : GET_MODE (tloc),
18988 					       VOIDmode,
18989 					       VAR_INIT_STATUS_INITIALIZED);
18990 		  if (tval)
18991 		    add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
18992 		  else if (tlocc != NULL_RTX)
18993 		    {
18994 		      tval = mem_loc_descriptor (tlocc,
18995 						 GET_MODE (tlocc) == VOIDmode
18996 						 ? Pmode : GET_MODE (tlocc),
18997 						 VOIDmode,
18998 						 VAR_INIT_STATUS_INITIALIZED);
18999 		      if (tval)
19000 			add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
19001 				    tval);
19002 		    }
19003 		}
19004 	      if (die != NULL)
19005 		{
19006 		  call_site_note_count++;
19007 		  if (ca_loc->tail_call_p)
19008 		    tail_call_site_note_count++;
19009 		}
19010 	    }
19011 	}
19012       call_arg_locations = NULL;
19013       call_arg_loc_last = NULL;
19014       if (tail_call_site_count >= 0
19015 	  && tail_call_site_count == tail_call_site_note_count
19016 	  && !dwarf_strict)
19017 	{
19018 	  if (call_site_count >= 0
19019 	      && call_site_count == call_site_note_count)
19020 	    add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
19021 	  else
19022 	    add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
19023 	}
19024       call_site_count = -1;
19025       tail_call_site_count = -1;
19026     }
19027 
19028   if (subr_die != old_die)
19029     /* Add the calling convention attribute if requested.  */
19030     add_calling_convention_attribute (subr_die, decl);
19031 }
19032 
19033 /* Returns a hash value for X (which really is a die_struct).  */
19034 
19035 hashval_t
19036 block_die_hasher::hash (die_struct *d)
19037 {
19038   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
19039 }
19040 
19041 /* Return nonzero if decl_id and die_parent of die_struct X is the same
19042    as decl_id and die_parent of die_struct Y.  */
19043 
19044 bool
19045 block_die_hasher::equal (die_struct *x, die_struct *y)
19046 {
19047   return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
19048 }
19049 
19050 /* Generate a DIE to represent a declared data object.
19051    Either DECL or ORIGIN must be non-null.  */
19052 
19053 static void
19054 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
19055 {
19056   HOST_WIDE_INT off = 0;
19057   tree com_decl;
19058   tree decl_or_origin = decl ? decl : origin;
19059   tree ultimate_origin;
19060   dw_die_ref var_die;
19061   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
19062   dw_die_ref origin_die;
19063   bool declaration = (DECL_EXTERNAL (decl_or_origin)
19064 		      || class_or_namespace_scope_p (context_die));
19065   bool specialization_p = false;
19066 
19067   ultimate_origin = decl_ultimate_origin (decl_or_origin);
19068   if (decl || ultimate_origin)
19069     origin = ultimate_origin;
19070   com_decl = fortran_common (decl_or_origin, &off);
19071 
19072   /* Symbol in common gets emitted as a child of the common block, in the form
19073      of a data member.  */
19074   if (com_decl)
19075     {
19076       dw_die_ref com_die;
19077       dw_loc_list_ref loc;
19078       die_node com_die_arg;
19079 
19080       var_die = lookup_decl_die (decl_or_origin);
19081       if (var_die)
19082 	{
19083 	  if (get_AT (var_die, DW_AT_location) == NULL)
19084 	    {
19085 	      loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
19086 	      if (loc)
19087 		{
19088 		  if (off)
19089 		    {
19090 		      /* Optimize the common case.  */
19091 		      if (single_element_loc_list_p (loc)
19092 			  && loc->expr->dw_loc_opc == DW_OP_addr
19093 			  && loc->expr->dw_loc_next == NULL
19094 			  && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
19095 			     == SYMBOL_REF)
19096 			{
19097 			  rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
19098 			  loc->expr->dw_loc_oprnd1.v.val_addr
19099 			    = plus_constant (GET_MODE (x), x , off);
19100 			}
19101 		      else
19102 			loc_list_plus_const (loc, off);
19103 		    }
19104 		  add_AT_location_description (var_die, DW_AT_location, loc);
19105 		  remove_AT (var_die, DW_AT_declaration);
19106 		}
19107 	    }
19108 	  return;
19109 	}
19110 
19111       if (common_block_die_table == NULL)
19112 	common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
19113 
19114       com_die_arg.decl_id = DECL_UID (com_decl);
19115       com_die_arg.die_parent = context_die;
19116       com_die = common_block_die_table->find (&com_die_arg);
19117       loc = loc_list_from_tree (com_decl, 2, NULL);
19118       if (com_die == NULL)
19119 	{
19120 	  const char *cnam
19121 	    = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
19122 	  die_node **slot;
19123 
19124 	  com_die = new_die (DW_TAG_common_block, context_die, decl);
19125 	  add_name_and_src_coords_attributes (com_die, com_decl);
19126 	  if (loc)
19127 	    {
19128 	      add_AT_location_description (com_die, DW_AT_location, loc);
19129 	      /* Avoid sharing the same loc descriptor between
19130 		 DW_TAG_common_block and DW_TAG_variable.  */
19131 	      loc = loc_list_from_tree (com_decl, 2, NULL);
19132 	    }
19133 	  else if (DECL_EXTERNAL (decl_or_origin))
19134 	    add_AT_flag (com_die, DW_AT_declaration, 1);
19135 	  if (want_pubnames ())
19136 	    add_pubname_string (cnam, com_die); /* ??? needed? */
19137 	  com_die->decl_id = DECL_UID (com_decl);
19138 	  slot = common_block_die_table->find_slot (com_die, INSERT);
19139 	  *slot = com_die;
19140 	}
19141       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
19142 	{
19143 	  add_AT_location_description (com_die, DW_AT_location, loc);
19144 	  loc = loc_list_from_tree (com_decl, 2, NULL);
19145 	  remove_AT (com_die, DW_AT_declaration);
19146 	}
19147       var_die = new_die (DW_TAG_variable, com_die, decl);
19148       add_name_and_src_coords_attributes (var_die, decl_or_origin);
19149       add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
19150 			  decl_quals (decl_or_origin), context_die);
19151       add_AT_flag (var_die, DW_AT_external, 1);
19152       if (loc)
19153 	{
19154 	  if (off)
19155 	    {
19156 	      /* Optimize the common case.  */
19157 	      if (single_element_loc_list_p (loc)
19158                   && loc->expr->dw_loc_opc == DW_OP_addr
19159 		  && loc->expr->dw_loc_next == NULL
19160 		  && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
19161 		{
19162 		  rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
19163 		  loc->expr->dw_loc_oprnd1.v.val_addr
19164 		    = plus_constant (GET_MODE (x), x, off);
19165 		}
19166 	      else
19167 		loc_list_plus_const (loc, off);
19168 	    }
19169 	  add_AT_location_description (var_die, DW_AT_location, loc);
19170 	}
19171       else if (DECL_EXTERNAL (decl_or_origin))
19172 	add_AT_flag (var_die, DW_AT_declaration, 1);
19173       if (decl)
19174 	equate_decl_number_to_die (decl, var_die);
19175       return;
19176     }
19177 
19178   /* If the compiler emitted a definition for the DECL declaration
19179      and if we already emitted a DIE for it, don't emit a second
19180      DIE for it again. Allow re-declarations of DECLs that are
19181      inside functions, though.  */
19182   if (old_die && declaration && !local_scope_p (context_die))
19183     return;
19184 
19185   /* For static data members, the declaration in the class is supposed
19186      to have DW_TAG_member tag; the specification should still be
19187      DW_TAG_variable referencing the DW_TAG_member DIE.  */
19188   if (declaration && class_scope_p (context_die))
19189     var_die = new_die (DW_TAG_member, context_die, decl);
19190   else
19191     var_die = new_die (DW_TAG_variable, context_die, decl);
19192 
19193   origin_die = NULL;
19194   if (origin != NULL)
19195     origin_die = add_abstract_origin_attribute (var_die, origin);
19196 
19197   /* Loop unrolling can create multiple blocks that refer to the same
19198      static variable, so we must test for the DW_AT_declaration flag.
19199 
19200      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
19201      copy decls and set the DECL_ABSTRACT_P flag on them instead of
19202      sharing them.
19203 
19204      ??? Duplicated blocks have been rewritten to use .debug_ranges.
19205 
19206      ??? The declare_in_namespace support causes us to get two DIEs for one
19207      variable, both of which are declarations.  We want to avoid considering
19208      one to be a specification, so we must test that this DIE is not a
19209      declaration.  */
19210   else if (old_die && TREE_STATIC (decl) && ! declaration
19211 	   && get_AT_flag (old_die, DW_AT_declaration) == 1)
19212     {
19213       /* This is a definition of a C++ class level static.  */
19214       add_AT_specification (var_die, old_die);
19215       specialization_p = true;
19216       if (DECL_NAME (decl))
19217 	{
19218 	  expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19219 	  struct dwarf_file_data * file_index = lookup_filename (s.file);
19220 
19221 	  if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19222 	    add_AT_file (var_die, DW_AT_decl_file, file_index);
19223 
19224 	  if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19225 	    add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19226 
19227 	  if (old_die->die_tag == DW_TAG_member)
19228 	    add_linkage_name (var_die, decl);
19229 	}
19230     }
19231   else
19232     add_name_and_src_coords_attributes (var_die, decl);
19233 
19234   if ((origin == NULL && !specialization_p)
19235       || (origin != NULL
19236 	  && !DECL_ABSTRACT_P (decl_or_origin)
19237 	  && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19238 				       decl_function_context
19239 							(decl_or_origin))))
19240     {
19241       tree type = TREE_TYPE (decl_or_origin);
19242 
19243       if (decl_by_reference_p (decl_or_origin))
19244 	add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19245 			    context_die);
19246       else
19247 	add_type_attribute (var_die, type, decl_quals (decl_or_origin),
19248 			    context_die);
19249     }
19250 
19251   if (origin == NULL && !specialization_p)
19252     {
19253       if (TREE_PUBLIC (decl))
19254 	add_AT_flag (var_die, DW_AT_external, 1);
19255 
19256       if (DECL_ARTIFICIAL (decl))
19257 	add_AT_flag (var_die, DW_AT_artificial, 1);
19258 
19259       add_accessibility_attribute (var_die, decl);
19260     }
19261 
19262   if (declaration)
19263     add_AT_flag (var_die, DW_AT_declaration, 1);
19264 
19265   if (decl && (DECL_ABSTRACT_P (decl) || declaration || old_die == NULL))
19266     equate_decl_number_to_die (decl, var_die);
19267 
19268   if (! declaration
19269       && (! DECL_ABSTRACT_P (decl_or_origin)
19270 	  /* Local static vars are shared between all clones/inlines,
19271 	     so emit DW_AT_location on the abstract DIE if DECL_RTL is
19272 	     already set.  */
19273 	  || (TREE_CODE (decl_or_origin) == VAR_DECL
19274 	      && TREE_STATIC (decl_or_origin)
19275 	      && DECL_RTL_SET_P (decl_or_origin)))
19276       /* When abstract origin already has DW_AT_location attribute, no need
19277 	 to add it again.  */
19278       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19279     {
19280       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
19281           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
19282 	defer_location (decl_or_origin, var_die);
19283       else
19284         add_location_or_const_value_attribute (var_die, decl_or_origin,
19285 					       decl == NULL, DW_AT_location);
19286       add_pubname (decl_or_origin, var_die);
19287     }
19288   else
19289     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19290 }
19291 
19292 /* Generate a DIE to represent a named constant.  */
19293 
19294 static void
19295 gen_const_die (tree decl, dw_die_ref context_die)
19296 {
19297   dw_die_ref const_die;
19298   tree type = TREE_TYPE (decl);
19299 
19300   const_die = new_die (DW_TAG_constant, context_die, decl);
19301   add_name_and_src_coords_attributes (const_die, decl);
19302   add_type_attribute (const_die, type, TYPE_QUAL_CONST, context_die);
19303   if (TREE_PUBLIC (decl))
19304     add_AT_flag (const_die, DW_AT_external, 1);
19305   if (DECL_ARTIFICIAL (decl))
19306     add_AT_flag (const_die, DW_AT_artificial, 1);
19307   tree_add_const_value_attribute_for_decl (const_die, decl);
19308 }
19309 
19310 /* Generate a DIE to represent a label identifier.  */
19311 
19312 static void
19313 gen_label_die (tree decl, dw_die_ref context_die)
19314 {
19315   tree origin = decl_ultimate_origin (decl);
19316   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
19317   rtx insn;
19318   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19319 
19320   if (origin != NULL)
19321     add_abstract_origin_attribute (lbl_die, origin);
19322   else
19323     add_name_and_src_coords_attributes (lbl_die, decl);
19324 
19325   if (DECL_ABSTRACT_P (decl))
19326     equate_decl_number_to_die (decl, lbl_die);
19327   else
19328     {
19329       insn = DECL_RTL_IF_SET (decl);
19330 
19331       /* Deleted labels are programmer specified labels which have been
19332 	 eliminated because of various optimizations.  We still emit them
19333 	 here so that it is possible to put breakpoints on them.  */
19334       if (insn
19335 	  && (LABEL_P (insn)
19336 	      || ((NOTE_P (insn)
19337 	           && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19338 	{
19339 	  /* When optimization is enabled (via -O) some parts of the compiler
19340 	     (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19341 	     represent source-level labels which were explicitly declared by
19342 	     the user.  This really shouldn't be happening though, so catch
19343 	     it if it ever does happen.  */
19344 	  gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
19345 
19346 	  ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19347           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19348 	}
19349       else if (insn
19350 	       && NOTE_P (insn)
19351 	       && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
19352 	       && CODE_LABEL_NUMBER (insn) != -1)
19353 	{
19354 	  ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
19355           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19356 	}
19357     }
19358 }
19359 
19360 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
19361    attributes to the DIE for a block STMT, to describe where the inlined
19362    function was called from.  This is similar to add_src_coords_attributes.  */
19363 
19364 static inline void
19365 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19366 {
19367   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19368 
19369   if (dwarf_version >= 3 || !dwarf_strict)
19370     {
19371       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19372       add_AT_unsigned (die, DW_AT_call_line, s.line);
19373     }
19374 }
19375 
19376 
19377 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19378    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
19379 
19380 static inline void
19381 add_high_low_attributes (tree stmt, dw_die_ref die)
19382 {
19383   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19384 
19385   if (BLOCK_FRAGMENT_CHAIN (stmt)
19386       && (dwarf_version >= 3 || !dwarf_strict))
19387     {
19388       tree chain, superblock = NULL_TREE;
19389       dw_die_ref pdie;
19390       dw_attr_ref attr = NULL;
19391 
19392       if (inlined_function_outer_scope_p (stmt))
19393 	{
19394 	  ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19395 				       BLOCK_NUMBER (stmt));
19396           add_AT_lbl_id (die, DW_AT_entry_pc, label);
19397 	}
19398 
19399       /* Optimize duplicate .debug_ranges lists or even tails of
19400 	 lists.  If this BLOCK has same ranges as its supercontext,
19401 	 lookup DW_AT_ranges attribute in the supercontext (and
19402 	 recursively so), verify that the ranges_table contains the
19403 	 right values and use it instead of adding a new .debug_range.  */
19404       for (chain = stmt, pdie = die;
19405 	   BLOCK_SAME_RANGE (chain);
19406 	   chain = BLOCK_SUPERCONTEXT (chain))
19407 	{
19408 	  dw_attr_ref new_attr;
19409 
19410 	  pdie = pdie->die_parent;
19411 	  if (pdie == NULL)
19412 	    break;
19413 	  if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
19414 	    break;
19415 	  new_attr = get_AT (pdie, DW_AT_ranges);
19416 	  if (new_attr == NULL
19417 	      || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
19418 	    break;
19419 	  attr = new_attr;
19420 	  superblock = BLOCK_SUPERCONTEXT (chain);
19421 	}
19422       if (attr != NULL
19423 	  && (ranges_table[attr->dw_attr_val.v.val_offset
19424 			   / 2 / DWARF2_ADDR_SIZE].num
19425 	      == BLOCK_NUMBER (superblock))
19426 	  && BLOCK_FRAGMENT_CHAIN (superblock))
19427 	{
19428 	  unsigned long off = attr->dw_attr_val.v.val_offset
19429 			      / 2 / DWARF2_ADDR_SIZE;
19430 	  unsigned long supercnt = 0, thiscnt = 0;
19431 	  for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
19432 	       chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19433 	    {
19434 	      ++supercnt;
19435 	      gcc_checking_assert (ranges_table[off + supercnt].num
19436 				   == BLOCK_NUMBER (chain));
19437 	    }
19438 	  gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
19439 	  for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
19440 	       chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19441 	    ++thiscnt;
19442 	  gcc_assert (supercnt >= thiscnt);
19443 	  add_AT_range_list (die, DW_AT_ranges,
19444                              ((off + supercnt - thiscnt)
19445                               * 2 * DWARF2_ADDR_SIZE),
19446                              false);
19447 	  return;
19448 	}
19449 
19450       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
19451 
19452       chain = BLOCK_FRAGMENT_CHAIN (stmt);
19453       do
19454 	{
19455 	  add_ranges (chain);
19456 	  chain = BLOCK_FRAGMENT_CHAIN (chain);
19457 	}
19458       while (chain);
19459       add_ranges (NULL);
19460     }
19461   else
19462     {
19463       char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
19464       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19465 				   BLOCK_NUMBER (stmt));
19466       ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
19467 				   BLOCK_NUMBER (stmt));
19468       add_AT_low_high_pc (die, label, label_high, false);
19469     }
19470 }
19471 
19472 /* Generate a DIE for a lexical block.  */
19473 
19474 static void
19475 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
19476 {
19477   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19478 
19479   if (call_arg_locations)
19480     {
19481       if (block_map.length () <= BLOCK_NUMBER (stmt))
19482 	block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19483       block_map[BLOCK_NUMBER (stmt)] = stmt_die;
19484     }
19485 
19486   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19487     add_high_low_attributes (stmt, stmt_die);
19488 
19489   decls_for_scope (stmt, stmt_die);
19490 }
19491 
19492 /* Generate a DIE for an inlined subprogram.  */
19493 
19494 static void
19495 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
19496 {
19497   tree decl;
19498 
19499   /* The instance of function that is effectively being inlined shall not
19500      be abstract.  */
19501   gcc_assert (! BLOCK_ABSTRACT (stmt));
19502 
19503   decl = block_ultimate_origin (stmt);
19504 
19505   /* Emit info for the abstract instance first, if we haven't yet.  We
19506      must emit this even if the block is abstract, otherwise when we
19507      emit the block below (or elsewhere), we may end up trying to emit
19508      a die whose origin die hasn't been emitted, and crashing.  */
19509   dwarf2out_abstract_function (decl);
19510 
19511   if (! BLOCK_ABSTRACT (stmt))
19512     {
19513       dw_die_ref subr_die
19514 	= new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19515 
19516       if (call_arg_locations)
19517 	{
19518 	  if (block_map.length () <= BLOCK_NUMBER (stmt))
19519 	    block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19520 	  block_map[BLOCK_NUMBER (stmt)] = subr_die;
19521 	}
19522       add_abstract_origin_attribute (subr_die, decl);
19523       if (TREE_ASM_WRITTEN (stmt))
19524         add_high_low_attributes (stmt, subr_die);
19525       add_call_src_coords_attributes (stmt, subr_die);
19526 
19527       decls_for_scope (stmt, subr_die);
19528     }
19529 }
19530 
19531 /* Generate a DIE for a field in a record, or structure.  */
19532 
19533 static void
19534 gen_field_die (tree decl, dw_die_ref context_die)
19535 {
19536   dw_die_ref decl_die;
19537 
19538   if (TREE_TYPE (decl) == error_mark_node)
19539     return;
19540 
19541   decl_die = new_die (DW_TAG_member, context_die, decl);
19542   add_name_and_src_coords_attributes (decl_die, decl);
19543   add_type_attribute (decl_die, member_declared_type (decl),
19544 		      decl_quals (decl), context_die);
19545 
19546   if (DECL_BIT_FIELD_TYPE (decl))
19547     {
19548       add_byte_size_attribute (decl_die, decl);
19549       add_bit_size_attribute (decl_die, decl);
19550       add_bit_offset_attribute (decl_die, decl);
19551     }
19552 
19553   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19554     add_data_member_location_attribute (decl_die, decl);
19555 
19556   if (DECL_ARTIFICIAL (decl))
19557     add_AT_flag (decl_die, DW_AT_artificial, 1);
19558 
19559   add_accessibility_attribute (decl_die, decl);
19560 
19561   /* Equate decl number to die, so that we can look up this decl later on.  */
19562   equate_decl_number_to_die (decl, decl_die);
19563 }
19564 
19565 #if 0
19566 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19567    Use modified_type_die instead.
19568    We keep this code here just in case these types of DIEs may be needed to
19569    represent certain things in other languages (e.g. Pascal) someday.  */
19570 
19571 static void
19572 gen_pointer_type_die (tree type, dw_die_ref context_die)
19573 {
19574   dw_die_ref ptr_die
19575     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19576 
19577   equate_type_number_to_die (type, ptr_die);
19578   add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19579 		      context_die);
19580   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19581 }
19582 
19583 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19584    Use modified_type_die instead.
19585    We keep this code here just in case these types of DIEs may be needed to
19586    represent certain things in other languages (e.g. Pascal) someday.  */
19587 
19588 static void
19589 gen_reference_type_die (tree type, dw_die_ref context_die)
19590 {
19591   dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19592 
19593   if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19594     ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19595   else
19596     ref_die = new_die (DW_TAG_reference_type, scope_die, type);
19597 
19598   equate_type_number_to_die (type, ref_die);
19599   add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19600 		      context_die);
19601   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19602 }
19603 #endif
19604 
19605 /* Generate a DIE for a pointer to a member type.  */
19606 
19607 static void
19608 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
19609 {
19610   dw_die_ref ptr_die
19611     = new_die (DW_TAG_ptr_to_member_type,
19612 	       scope_die_for (type, context_die), type);
19613 
19614   equate_type_number_to_die (type, ptr_die);
19615   add_AT_die_ref (ptr_die, DW_AT_containing_type,
19616 		  lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
19617   add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19618 		      context_die);
19619 }
19620 
19621 typedef const char *dchar_p; /* For DEF_VEC_P.  */
19622 
19623 static char *producer_string;
19624 
19625 /* Return a heap allocated producer string including command line options
19626    if -grecord-gcc-switches.  */
19627 
19628 static char *
19629 gen_producer_string (void)
19630 {
19631   size_t j;
19632   auto_vec<dchar_p> switches;
19633   const char *language_string = lang_hooks.name;
19634   char *producer, *tail;
19635   const char *p;
19636   size_t len = dwarf_record_gcc_switches ? 0 : 3;
19637   size_t plen = strlen (language_string) + 1 + strlen (version_string);
19638 
19639   for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
19640     switch (save_decoded_options[j].opt_index)
19641       {
19642       case OPT_o:
19643       case OPT_d:
19644       case OPT_dumpbase:
19645       case OPT_dumpdir:
19646       case OPT_auxbase:
19647       case OPT_auxbase_strip:
19648       case OPT_quiet:
19649       case OPT_version:
19650       case OPT_v:
19651       case OPT_w:
19652       case OPT_L:
19653       case OPT_D:
19654       case OPT_I:
19655       case OPT_U:
19656       case OPT_SPECIAL_unknown:
19657       case OPT_SPECIAL_ignore:
19658       case OPT_SPECIAL_program_name:
19659       case OPT_SPECIAL_input_file:
19660       case OPT_grecord_gcc_switches:
19661       case OPT_gno_record_gcc_switches:
19662       case OPT__output_pch_:
19663       case OPT_fdiagnostics_show_location_:
19664       case OPT_fdiagnostics_show_option:
19665       case OPT_fdiagnostics_show_caret:
19666       case OPT_fdiagnostics_color_:
19667       case OPT_fverbose_asm:
19668       case OPT____:
19669       case OPT__sysroot_:
19670       case OPT_nostdinc:
19671       case OPT_nostdinc__:
19672       case OPT_fpreprocessed:
19673       case OPT_fltrans_output_list_:
19674       case OPT_fresolution_:
19675 	/* Ignore these.  */
19676 	continue;
19677       default:
19678         if (cl_options[save_decoded_options[j].opt_index].flags
19679 	    & CL_NO_DWARF_RECORD)
19680 	  continue;
19681         gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
19682 			     == '-');
19683         switch (save_decoded_options[j].canonical_option[0][1])
19684 	  {
19685 	  case 'M':
19686 	  case 'i':
19687 	  case 'W':
19688 	    continue;
19689 	  case 'f':
19690 	    if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
19691 			 "dump", 4) == 0)
19692 	      continue;
19693 	    break;
19694 	  default:
19695 	    break;
19696 	  }
19697 	switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
19698 	len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
19699 	break;
19700       }
19701 
19702   producer = XNEWVEC (char, plen + 1 + len + 1);
19703   tail = producer;
19704   sprintf (tail, "%s %s", language_string, version_string);
19705   tail += plen;
19706 
19707   FOR_EACH_VEC_ELT (switches, j, p)
19708     {
19709       len = strlen (p);
19710       *tail = ' ';
19711       memcpy (tail + 1, p, len);
19712       tail += len + 1;
19713     }
19714 
19715   *tail = '\0';
19716   return producer;
19717 }
19718 
19719 /* Given a C and/or C++ language/version string return the "highest".
19720    C++ is assumed to be "higher" than C in this case.  Used for merging
19721    LTO translation unit languages.  */
19722 static const char *
19723 highest_c_language (const char *lang1, const char *lang2)
19724 {
19725   if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
19726     return "GNU C++14";
19727   if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
19728     return "GNU C++11";
19729   if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
19730     return "GNU C++98";
19731 
19732   if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
19733     return "GNU C11";
19734   if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
19735     return "GNU C99";
19736   if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
19737     return "GNU C89";
19738 
19739   gcc_unreachable ();
19740 }
19741 
19742 
19743 /* Generate the DIE for the compilation unit.  */
19744 
19745 static dw_die_ref
19746 gen_compile_unit_die (const char *filename)
19747 {
19748   dw_die_ref die;
19749   const char *language_string = lang_hooks.name;
19750   int language;
19751 
19752   die = new_die (DW_TAG_compile_unit, NULL, NULL);
19753 
19754   if (filename)
19755     {
19756       add_name_attribute (die, filename);
19757       /* Don't add cwd for <built-in>.  */
19758       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19759 	add_comp_dir_attribute (die);
19760     }
19761 
19762   add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
19763 
19764   /* If our producer is LTO try to figure out a common language to use
19765      from the global list of translation units.  */
19766   if (strcmp (language_string, "GNU GIMPLE") == 0)
19767     {
19768       unsigned i;
19769       tree t;
19770       const char *common_lang = NULL;
19771 
19772       FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
19773 	{
19774 	  if (!TRANSLATION_UNIT_LANGUAGE (t))
19775 	    continue;
19776 	  if (!common_lang)
19777 	    common_lang = TRANSLATION_UNIT_LANGUAGE (t);
19778 	  else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
19779 	    ;
19780 	  else if (strncmp (common_lang, "GNU C", 5) == 0
19781 		    && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
19782 	    /* Mixing C and C++ is ok, use C++ in that case.  */
19783 	    common_lang = highest_c_language (common_lang,
19784 					      TRANSLATION_UNIT_LANGUAGE (t));
19785 	  else
19786 	    {
19787 	      /* Fall back to C.  */
19788 	      common_lang = NULL;
19789 	      break;
19790 	    }
19791 	}
19792 
19793       if (common_lang)
19794 	language_string = common_lang;
19795     }
19796 
19797   language = DW_LANG_C;
19798   if (strncmp (language_string, "GNU C", 5) == 0
19799       && ISDIGIT (language_string[5]))
19800     {
19801       language = DW_LANG_C89;
19802       if (dwarf_version >= 3 || !dwarf_strict)
19803 	{
19804 	  if (strcmp (language_string, "GNU C89") != 0)
19805 	    language = DW_LANG_C99;
19806 
19807 	  if (dwarf_version >= 5 /* || !dwarf_strict */)
19808 	    if (strcmp (language_string, "GNU C11") == 0)
19809 	      language = DW_LANG_C11;
19810 	}
19811     }
19812   else if (strncmp (language_string, "GNU C++", 7) == 0)
19813     {
19814       language = DW_LANG_C_plus_plus;
19815       if (dwarf_version >= 5 /* || !dwarf_strict */)
19816 	{
19817 	  if (strcmp (language_string, "GNU C++11") == 0)
19818 	    language = DW_LANG_C_plus_plus_11;
19819 	  else if (strcmp (language_string, "GNU C++14") == 0)
19820 	    language = DW_LANG_C_plus_plus_14;
19821 	}
19822     }
19823   else if (strcmp (language_string, "GNU F77") == 0)
19824     language = DW_LANG_Fortran77;
19825   else if (strcmp (language_string, "GNU Pascal") == 0)
19826     language = DW_LANG_Pascal83;
19827   else if (dwarf_version >= 3 || !dwarf_strict)
19828     {
19829       if (strcmp (language_string, "GNU Ada") == 0)
19830 	language = DW_LANG_Ada95;
19831       else if (strncmp (language_string, "GNU Fortran", 11) == 0)
19832 	{
19833 	  language = DW_LANG_Fortran95;
19834 	  if (dwarf_version >= 5 /* || !dwarf_strict */)
19835 	    {
19836 	      if (strcmp (language_string, "GNU Fortran2003") == 0)
19837 		language = DW_LANG_Fortran03;
19838 	      else if (strcmp (language_string, "GNU Fortran2008") == 0)
19839 		language = DW_LANG_Fortran08;
19840 	    }
19841 	}
19842       else if (strcmp (language_string, "GNU Java") == 0)
19843 	language = DW_LANG_Java;
19844       else if (strcmp (language_string, "GNU Objective-C") == 0)
19845 	language = DW_LANG_ObjC;
19846       else if (strcmp (language_string, "GNU Objective-C++") == 0)
19847 	language = DW_LANG_ObjC_plus_plus;
19848       else if (dwarf_version >= 5 || !dwarf_strict)
19849 	{
19850 	  if (strcmp (language_string, "GNU Go") == 0)
19851 	    language = DW_LANG_Go;
19852 	}
19853     }
19854   /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works.  */
19855   else if (strncmp (language_string, "GNU Fortran", 11) == 0)
19856     language = DW_LANG_Fortran90;
19857 
19858   add_AT_unsigned (die, DW_AT_language, language);
19859 
19860   switch (language)
19861     {
19862     case DW_LANG_Fortran77:
19863     case DW_LANG_Fortran90:
19864     case DW_LANG_Fortran95:
19865     case DW_LANG_Fortran03:
19866     case DW_LANG_Fortran08:
19867       /* Fortran has case insensitive identifiers and the front-end
19868 	 lowercases everything.  */
19869       add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19870       break;
19871     default:
19872       /* The default DW_ID_case_sensitive doesn't need to be specified.  */
19873       break;
19874     }
19875   return die;
19876 }
19877 
19878 /* Generate the DIE for a base class.  */
19879 
19880 static void
19881 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19882 {
19883   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19884 
19885   add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, context_die);
19886   add_data_member_location_attribute (die, binfo);
19887 
19888   if (BINFO_VIRTUAL_P (binfo))
19889     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19890 
19891   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19892      children, otherwise the default is DW_ACCESS_public.  In DWARF2
19893      the default has always been DW_ACCESS_private.  */
19894   if (access == access_public_node)
19895     {
19896       if (dwarf_version == 2
19897 	  || context_die->die_tag == DW_TAG_class_type)
19898       add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19899     }
19900   else if (access == access_protected_node)
19901     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19902   else if (dwarf_version > 2
19903 	   && context_die->die_tag != DW_TAG_class_type)
19904     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19905 }
19906 
19907 /* Generate a DIE for a class member.  */
19908 
19909 static void
19910 gen_member_die (tree type, dw_die_ref context_die)
19911 {
19912   tree member;
19913   tree binfo = TYPE_BINFO (type);
19914   dw_die_ref child;
19915 
19916   /* If this is not an incomplete type, output descriptions of each of its
19917      members. Note that as we output the DIEs necessary to represent the
19918      members of this record or union type, we will also be trying to output
19919      DIEs to represent the *types* of those members. However the `type'
19920      function (above) will specifically avoid generating type DIEs for member
19921      types *within* the list of member DIEs for this (containing) type except
19922      for those types (of members) which are explicitly marked as also being
19923      members of this (containing) type themselves.  The g++ front- end can
19924      force any given type to be treated as a member of some other (containing)
19925      type by setting the TYPE_CONTEXT of the given (member) type to point to
19926      the TREE node representing the appropriate (containing) type.  */
19927 
19928   /* First output info about the base classes.  */
19929   if (binfo)
19930     {
19931       vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
19932       int i;
19933       tree base;
19934 
19935       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19936 	gen_inheritance_die (base,
19937 			     (accesses ? (*accesses)[i] : access_public_node),
19938 			     context_die);
19939     }
19940 
19941   /* Now output info about the data members and type members.  */
19942   for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19943     {
19944       /* If we thought we were generating minimal debug info for TYPE
19945 	 and then changed our minds, some of the member declarations
19946 	 may have already been defined.  Don't define them again, but
19947 	 do put them in the right order.  */
19948 
19949       child = lookup_decl_die (member);
19950       if (child)
19951 	splice_child_die (context_die, child);
19952       else
19953 	gen_decl_die (member, NULL, context_die);
19954     }
19955 
19956   /* Now output info about the function members (if any).  */
19957   for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19958     {
19959       /* Don't include clones in the member list.  */
19960       if (DECL_ABSTRACT_ORIGIN (member))
19961 	continue;
19962 
19963       child = lookup_decl_die (member);
19964       if (child)
19965 	splice_child_die (context_die, child);
19966       else
19967 	gen_decl_die (member, NULL, context_die);
19968     }
19969 }
19970 
19971 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
19972    is set, we pretend that the type was never defined, so we only get the
19973    member DIEs needed by later specification DIEs.  */
19974 
19975 static void
19976 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19977 				enum debug_info_usage usage)
19978 {
19979   dw_die_ref type_die = lookup_type_die (type);
19980   dw_die_ref scope_die = 0;
19981   int nested = 0;
19982   int complete = (TYPE_SIZE (type)
19983 		  && (! TYPE_STUB_DECL (type)
19984 		      || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19985   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19986   complete = complete && should_emit_struct_debug (type, usage);
19987 
19988   if (type_die && ! complete)
19989     return;
19990 
19991   if (TYPE_CONTEXT (type) != NULL_TREE
19992       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19993 	  || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19994     nested = 1;
19995 
19996   scope_die = scope_die_for (type, context_die);
19997 
19998   /* Generate child dies for template paramaters.  */
19999   if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
20000     schedule_generic_params_dies_gen (type);
20001 
20002   if (! type_die || (nested && is_cu_die (scope_die)))
20003     /* First occurrence of type or toplevel definition of nested class.  */
20004     {
20005       dw_die_ref old_die = type_die;
20006 
20007       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
20008 			  ? record_type_tag (type) : DW_TAG_union_type,
20009 			  scope_die, type);
20010       equate_type_number_to_die (type, type_die);
20011       if (old_die)
20012 	add_AT_specification (type_die, old_die);
20013       else
20014 	add_name_attribute (type_die, type_tag (type));
20015     }
20016   else
20017     remove_AT (type_die, DW_AT_declaration);
20018 
20019   /* If this type has been completed, then give it a byte_size attribute and
20020      then give a list of members.  */
20021   if (complete && !ns_decl)
20022     {
20023       /* Prevent infinite recursion in cases where the type of some member of
20024 	 this type is expressed in terms of this type itself.  */
20025       TREE_ASM_WRITTEN (type) = 1;
20026       add_byte_size_attribute (type_die, type);
20027       if (TYPE_STUB_DECL (type) != NULL_TREE)
20028 	{
20029 	  add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
20030 	  add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
20031 	}
20032 
20033       /* If the first reference to this type was as the return type of an
20034 	 inline function, then it may not have a parent.  Fix this now.  */
20035       if (type_die->die_parent == NULL)
20036 	add_child_die (scope_die, type_die);
20037 
20038       push_decl_scope (type);
20039       gen_member_die (type, type_die);
20040       pop_decl_scope ();
20041 
20042       add_gnat_descriptive_type_attribute (type_die, type, context_die);
20043       if (TYPE_ARTIFICIAL (type))
20044 	add_AT_flag (type_die, DW_AT_artificial, 1);
20045 
20046       /* GNU extension: Record what type our vtable lives in.  */
20047       if (TYPE_VFIELD (type))
20048 	{
20049 	  tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
20050 
20051 	  gen_type_die (vtype, context_die);
20052 	  add_AT_die_ref (type_die, DW_AT_containing_type,
20053 			  lookup_type_die (vtype));
20054 	}
20055     }
20056   else
20057     {
20058       add_AT_flag (type_die, DW_AT_declaration, 1);
20059 
20060       /* We don't need to do this for function-local types.  */
20061       if (TYPE_STUB_DECL (type)
20062 	  && ! decl_function_context (TYPE_STUB_DECL (type)))
20063 	vec_safe_push (incomplete_types, type);
20064     }
20065 
20066   if (get_AT (type_die, DW_AT_name))
20067     add_pubtype (type, type_die);
20068 }
20069 
20070 /* Generate a DIE for a subroutine _type_.  */
20071 
20072 static void
20073 gen_subroutine_type_die (tree type, dw_die_ref context_die)
20074 {
20075   tree return_type = TREE_TYPE (type);
20076   dw_die_ref subr_die
20077     = new_die (DW_TAG_subroutine_type,
20078 	       scope_die_for (type, context_die), type);
20079 
20080   equate_type_number_to_die (type, subr_die);
20081   add_prototyped_attribute (subr_die, type);
20082   add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, context_die);
20083   gen_formal_types_die (type, subr_die);
20084 
20085   if (get_AT (subr_die, DW_AT_name))
20086     add_pubtype (type, subr_die);
20087 }
20088 
20089 /* Generate a DIE for a type definition.  */
20090 
20091 static void
20092 gen_typedef_die (tree decl, dw_die_ref context_die)
20093 {
20094   dw_die_ref type_die;
20095   tree origin;
20096 
20097   if (TREE_ASM_WRITTEN (decl))
20098     return;
20099 
20100   TREE_ASM_WRITTEN (decl) = 1;
20101   type_die = new_die (DW_TAG_typedef, context_die, decl);
20102   origin = decl_ultimate_origin (decl);
20103   if (origin != NULL)
20104     add_abstract_origin_attribute (type_die, origin);
20105   else
20106     {
20107       tree type;
20108 
20109       add_name_and_src_coords_attributes (type_die, decl);
20110       if (DECL_ORIGINAL_TYPE (decl))
20111 	{
20112 	  type = DECL_ORIGINAL_TYPE (decl);
20113 
20114 	  gcc_assert (type != TREE_TYPE (decl));
20115 	  equate_type_number_to_die (TREE_TYPE (decl), type_die);
20116 	}
20117       else
20118 	{
20119 	  type = TREE_TYPE (decl);
20120 
20121 	  if (is_naming_typedef_decl (TYPE_NAME (type)))
20122 	    {
20123 	      /* Here, we are in the case of decl being a typedef naming
20124 	         an anonymous type, e.g:
20125 	             typedef struct {...} foo;
20126 	         In that case TREE_TYPE (decl) is not a typedef variant
20127 	         type and TYPE_NAME of the anonymous type is set to the
20128 	         TYPE_DECL of the typedef. This construct is emitted by
20129 	         the C++ FE.
20130 
20131 	         TYPE is the anonymous struct named by the typedef
20132 	         DECL. As we need the DW_AT_type attribute of the
20133 	         DW_TAG_typedef to point to the DIE of TYPE, let's
20134 	         generate that DIE right away. add_type_attribute
20135 	         called below will then pick (via lookup_type_die) that
20136 	         anonymous struct DIE.  */
20137 	      if (!TREE_ASM_WRITTEN (type))
20138 	        gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
20139 
20140 	      /* This is a GNU Extension.  We are adding a
20141 		 DW_AT_linkage_name attribute to the DIE of the
20142 		 anonymous struct TYPE.  The value of that attribute
20143 		 is the name of the typedef decl naming the anonymous
20144 		 struct.  This greatly eases the work of consumers of
20145 		 this debug info.  */
20146 	      add_linkage_attr (lookup_type_die (type), decl);
20147 	    }
20148 	}
20149 
20150       add_type_attribute (type_die, type, decl_quals (decl), context_die);
20151 
20152       if (is_naming_typedef_decl (decl))
20153 	/* We want that all subsequent calls to lookup_type_die with
20154 	   TYPE in argument yield the DW_TAG_typedef we have just
20155 	   created.  */
20156 	equate_type_number_to_die (type, type_die);
20157 
20158       add_accessibility_attribute (type_die, decl);
20159     }
20160 
20161   if (DECL_ABSTRACT_P (decl))
20162     equate_decl_number_to_die (decl, type_die);
20163 
20164   if (get_AT (type_die, DW_AT_name))
20165     add_pubtype (decl, type_die);
20166 }
20167 
20168 /* Generate a DIE for a struct, class, enum or union type.  */
20169 
20170 static void
20171 gen_tagged_type_die (tree type,
20172 		     dw_die_ref context_die,
20173 		     enum debug_info_usage usage)
20174 {
20175   int need_pop;
20176 
20177   if (type == NULL_TREE
20178       || !is_tagged_type (type))
20179     return;
20180 
20181   /* If this is a nested type whose containing class hasn't been written
20182      out yet, writing it out will cover this one, too.  This does not apply
20183      to instantiations of member class templates; they need to be added to
20184      the containing class as they are generated.  FIXME: This hurts the
20185      idea of combining type decls from multiple TUs, since we can't predict
20186      what set of template instantiations we'll get.  */
20187   if (TYPE_CONTEXT (type)
20188       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20189       && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
20190     {
20191       gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
20192 
20193       if (TREE_ASM_WRITTEN (type))
20194 	return;
20195 
20196       /* If that failed, attach ourselves to the stub.  */
20197       push_decl_scope (TYPE_CONTEXT (type));
20198       context_die = lookup_type_die (TYPE_CONTEXT (type));
20199       need_pop = 1;
20200     }
20201   else if (TYPE_CONTEXT (type) != NULL_TREE
20202 	   && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
20203     {
20204       /* If this type is local to a function that hasn't been written
20205 	 out yet, use a NULL context for now; it will be fixed up in
20206 	 decls_for_scope.  */
20207       context_die = lookup_decl_die (TYPE_CONTEXT (type));
20208       /* A declaration DIE doesn't count; nested types need to go in the
20209 	 specification.  */
20210       if (context_die && is_declaration_die (context_die))
20211 	context_die = NULL;
20212       need_pop = 0;
20213     }
20214   else
20215     {
20216       context_die = declare_in_namespace (type, context_die);
20217       need_pop = 0;
20218     }
20219 
20220   if (TREE_CODE (type) == ENUMERAL_TYPE)
20221     {
20222       /* This might have been written out by the call to
20223 	 declare_in_namespace.  */
20224       if (!TREE_ASM_WRITTEN (type))
20225 	gen_enumeration_type_die (type, context_die);
20226     }
20227   else
20228     gen_struct_or_union_type_die (type, context_die, usage);
20229 
20230   if (need_pop)
20231     pop_decl_scope ();
20232 
20233   /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
20234      it up if it is ever completed.  gen_*_type_die will set it for us
20235      when appropriate.  */
20236 }
20237 
20238 /* Generate a type description DIE.  */
20239 
20240 static void
20241 gen_type_die_with_usage (tree type, dw_die_ref context_die,
20242 			 enum debug_info_usage usage)
20243 {
20244   struct array_descr_info info;
20245 
20246   if (type == NULL_TREE || type == error_mark_node)
20247     return;
20248 
20249   if (TYPE_NAME (type) != NULL_TREE
20250       && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20251       && is_redundant_typedef (TYPE_NAME (type))
20252       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
20253     /* The DECL of this type is a typedef we don't want to emit debug
20254        info for but we want debug info for its underlying typedef.
20255        This can happen for e.g, the injected-class-name of a C++
20256        type.  */
20257     type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
20258 
20259   /* If TYPE is a typedef type variant, let's generate debug info
20260      for the parent typedef which TYPE is a type of.  */
20261   if (typedef_variant_p (type))
20262     {
20263       if (TREE_ASM_WRITTEN (type))
20264 	return;
20265 
20266       /* Prevent broken recursion; we can't hand off to the same type.  */
20267       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
20268 
20269       /* Give typedefs the right scope.  */
20270       context_die = scope_die_for (type, context_die);
20271 
20272       TREE_ASM_WRITTEN (type) = 1;
20273 
20274       gen_decl_die (TYPE_NAME (type), NULL, context_die);
20275       return;
20276     }
20277 
20278   /* If type is an anonymous tagged type named by a typedef, let's
20279      generate debug info for the typedef.  */
20280   if (is_naming_typedef_decl (TYPE_NAME (type)))
20281     {
20282       /* Use the DIE of the containing namespace as the parent DIE of
20283          the type description DIE we want to generate.  */
20284       if (DECL_CONTEXT (TYPE_NAME (type))
20285 	  && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20286 	context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20287 
20288       gen_decl_die (TYPE_NAME (type), NULL, context_die);
20289       return;
20290     }
20291 
20292   /* We are going to output a DIE to represent the unqualified version
20293      of this type (i.e. without any const or volatile qualifiers) so
20294      get the main variant (i.e. the unqualified version) of this type
20295      now.  (Vectors and arrays are special because the debugging info is in the
20296      cloned type itself).  */
20297   if (TREE_CODE (type) != VECTOR_TYPE
20298       && TREE_CODE (type) != ARRAY_TYPE)
20299     type = type_main_variant (type);
20300 
20301   /* If this is an array type with hidden descriptor, handle it first.  */
20302   if (!TREE_ASM_WRITTEN (type)
20303       && lang_hooks.types.get_array_descr_info)
20304     {
20305       memset (&info, 0, sizeof (info));
20306       if (lang_hooks.types.get_array_descr_info (type, &info))
20307 	{
20308 	  gen_descr_array_type_die (type, &info, context_die);
20309 	  TREE_ASM_WRITTEN (type) = 1;
20310 	  return;
20311 	}
20312     }
20313 
20314   if (TREE_ASM_WRITTEN (type))
20315     return;
20316 
20317   switch (TREE_CODE (type))
20318     {
20319     case ERROR_MARK:
20320       break;
20321 
20322     case POINTER_TYPE:
20323     case REFERENCE_TYPE:
20324       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
20325 	 ensures that the gen_type_die recursion will terminate even if the
20326 	 type is recursive.  Recursive types are possible in Ada.  */
20327       /* ??? We could perhaps do this for all types before the switch
20328 	 statement.  */
20329       TREE_ASM_WRITTEN (type) = 1;
20330 
20331       /* For these types, all that is required is that we output a DIE (or a
20332 	 set of DIEs) to represent the "basis" type.  */
20333       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20334 				DINFO_USAGE_IND_USE);
20335       break;
20336 
20337     case OFFSET_TYPE:
20338       /* This code is used for C++ pointer-to-data-member types.
20339 	 Output a description of the relevant class type.  */
20340       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
20341 					DINFO_USAGE_IND_USE);
20342 
20343       /* Output a description of the type of the object pointed to.  */
20344       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20345 					DINFO_USAGE_IND_USE);
20346 
20347       /* Now output a DIE to represent this pointer-to-data-member type
20348 	 itself.  */
20349       gen_ptr_to_mbr_type_die (type, context_die);
20350       break;
20351 
20352     case FUNCTION_TYPE:
20353       /* Force out return type (in case it wasn't forced out already).  */
20354       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20355 					DINFO_USAGE_DIR_USE);
20356       gen_subroutine_type_die (type, context_die);
20357       break;
20358 
20359     case METHOD_TYPE:
20360       /* Force out return type (in case it wasn't forced out already).  */
20361       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20362 					DINFO_USAGE_DIR_USE);
20363       gen_subroutine_type_die (type, context_die);
20364       break;
20365 
20366     case ARRAY_TYPE:
20367       gen_array_type_die (type, context_die);
20368       break;
20369 
20370     case VECTOR_TYPE:
20371       gen_array_type_die (type, context_die);
20372       break;
20373 
20374     case ENUMERAL_TYPE:
20375     case RECORD_TYPE:
20376     case UNION_TYPE:
20377     case QUAL_UNION_TYPE:
20378       gen_tagged_type_die (type, context_die, usage);
20379       return;
20380 
20381     case VOID_TYPE:
20382     case INTEGER_TYPE:
20383     case REAL_TYPE:
20384     case FIXED_POINT_TYPE:
20385     case COMPLEX_TYPE:
20386     case BOOLEAN_TYPE:
20387     case POINTER_BOUNDS_TYPE:
20388       /* No DIEs needed for fundamental types.  */
20389       break;
20390 
20391     case NULLPTR_TYPE:
20392     case LANG_TYPE:
20393       /* Just use DW_TAG_unspecified_type.  */
20394       {
20395         dw_die_ref type_die = lookup_type_die (type);
20396         if (type_die == NULL)
20397           {
20398 	    tree name = TYPE_IDENTIFIER (type);
20399             type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
20400 				type);
20401             add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20402             equate_type_number_to_die (type, type_die);
20403           }
20404       }
20405       break;
20406 
20407     default:
20408       if (is_cxx_auto (type))
20409 	{
20410 	  tree name = TYPE_IDENTIFIER (type);
20411 	  dw_die_ref *die = (name == get_identifier ("auto")
20412 			     ? &auto_die : &decltype_auto_die);
20413 	  if (!*die)
20414 	    {
20415 	      *die = new_die (DW_TAG_unspecified_type,
20416 			      comp_unit_die (), NULL_TREE);
20417 	      add_name_attribute (*die, IDENTIFIER_POINTER (name));
20418 	    }
20419 	  equate_type_number_to_die (type, *die);
20420 	  break;
20421 	}
20422       gcc_unreachable ();
20423     }
20424 
20425   TREE_ASM_WRITTEN (type) = 1;
20426 }
20427 
20428 static void
20429 gen_type_die (tree type, dw_die_ref context_die)
20430 {
20431   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20432 }
20433 
20434 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20435    things which are local to the given block.  */
20436 
20437 static void
20438 gen_block_die (tree stmt, dw_die_ref context_die)
20439 {
20440   int must_output_die = 0;
20441   bool inlined_func;
20442 
20443   /* Ignore blocks that are NULL.  */
20444   if (stmt == NULL_TREE)
20445     return;
20446 
20447   inlined_func = inlined_function_outer_scope_p (stmt);
20448 
20449   /* If the block is one fragment of a non-contiguous block, do not
20450      process the variables, since they will have been done by the
20451      origin block.  Do process subblocks.  */
20452   if (BLOCK_FRAGMENT_ORIGIN (stmt))
20453     {
20454       tree sub;
20455 
20456       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20457 	gen_block_die (sub, context_die);
20458 
20459       return;
20460     }
20461 
20462   /* Determine if we need to output any Dwarf DIEs at all to represent this
20463      block.  */
20464   if (inlined_func)
20465     /* The outer scopes for inlinings *must* always be represented.  We
20466        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
20467     must_output_die = 1;
20468   else
20469     {
20470       /* Determine if this block directly contains any "significant"
20471 	 local declarations which we will need to output DIEs for.  */
20472       if (debug_info_level > DINFO_LEVEL_TERSE)
20473 	/* We are not in terse mode so *any* local declaration counts
20474 	   as being a "significant" one.  */
20475 	must_output_die = ((BLOCK_VARS (stmt) != NULL
20476 			    || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20477 			   && (TREE_USED (stmt)
20478 			       || TREE_ASM_WRITTEN (stmt)
20479 			       || BLOCK_ABSTRACT (stmt)));
20480       else if ((TREE_USED (stmt)
20481 		|| TREE_ASM_WRITTEN (stmt)
20482 		|| BLOCK_ABSTRACT (stmt))
20483       	       && !dwarf2out_ignore_block (stmt))
20484 	must_output_die = 1;
20485     }
20486 
20487   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20488      DIE for any block which contains no significant local declarations at
20489      all.  Rather, in such cases we just call `decls_for_scope' so that any
20490      needed Dwarf info for any sub-blocks will get properly generated. Note
20491      that in terse mode, our definition of what constitutes a "significant"
20492      local declaration gets restricted to include only inlined function
20493      instances and local (nested) function definitions.  */
20494   if (must_output_die)
20495     {
20496       if (inlined_func)
20497 	{
20498 	  /* If STMT block is abstract, that means we have been called
20499 	     indirectly from dwarf2out_abstract_function.
20500 	     That function rightfully marks the descendent blocks (of
20501 	     the abstract function it is dealing with) as being abstract,
20502 	     precisely to prevent us from emitting any
20503 	     DW_TAG_inlined_subroutine DIE as a descendent
20504 	     of an abstract function instance. So in that case, we should
20505 	     not call gen_inlined_subroutine_die.
20506 
20507 	     Later though, when cgraph asks dwarf2out to emit info
20508 	     for the concrete instance of the function decl into which
20509 	     the concrete instance of STMT got inlined, the later will lead
20510 	     to the generation of a DW_TAG_inlined_subroutine DIE.  */
20511 	  if (! BLOCK_ABSTRACT (stmt))
20512 	    gen_inlined_subroutine_die (stmt, context_die);
20513 	}
20514       else
20515 	gen_lexical_block_die (stmt, context_die);
20516     }
20517   else
20518     decls_for_scope (stmt, context_die);
20519 }
20520 
20521 /* Process variable DECL (or variable with origin ORIGIN) within
20522    block STMT and add it to CONTEXT_DIE.  */
20523 static void
20524 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20525 {
20526   dw_die_ref die;
20527   tree decl_or_origin = decl ? decl : origin;
20528 
20529   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20530     die = lookup_decl_die (decl_or_origin);
20531   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20532            && TYPE_DECL_IS_STUB (decl_or_origin))
20533     die = lookup_type_die (TREE_TYPE (decl_or_origin));
20534   else
20535     die = NULL;
20536 
20537   if (die != NULL && die->die_parent == NULL)
20538     add_child_die (context_die, die);
20539   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20540     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20541 					 stmt, context_die);
20542   else
20543     gen_decl_die (decl, origin, context_die);
20544 }
20545 
20546 /* Generate all of the decls declared within a given scope and (recursively)
20547    all of its sub-blocks.  */
20548 
20549 static void
20550 decls_for_scope (tree stmt, dw_die_ref context_die)
20551 {
20552   tree decl;
20553   unsigned int i;
20554   tree subblocks;
20555 
20556   /* Ignore NULL blocks.  */
20557   if (stmt == NULL_TREE)
20558     return;
20559 
20560   /* Output the DIEs to represent all of the data objects and typedefs
20561      declared directly within this block but not within any nested
20562      sub-blocks.  Also, nested function and tag DIEs have been
20563      generated with a parent of NULL; fix that up now.  We don't
20564      have to do this if we're at -g1.  */
20565   if (debug_info_level > DINFO_LEVEL_TERSE)
20566     {
20567       for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20568 	process_scope_var (stmt, decl, NULL_TREE, context_die);
20569       for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20570 	process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20571 			   context_die);
20572     }
20573 
20574   /* Even if we're at -g1, we need to process the subblocks in order to get
20575      inlined call information.  */
20576 
20577   /* Output the DIEs to represent all sub-blocks (and the items declared
20578      therein) of this block.  */
20579   for (subblocks = BLOCK_SUBBLOCKS (stmt);
20580        subblocks != NULL;
20581        subblocks = BLOCK_CHAIN (subblocks))
20582     gen_block_die (subblocks, context_die);
20583 }
20584 
20585 /* Is this a typedef we can avoid emitting?  */
20586 
20587 static inline int
20588 is_redundant_typedef (const_tree decl)
20589 {
20590   if (TYPE_DECL_IS_STUB (decl))
20591     return 1;
20592 
20593   if (DECL_ARTIFICIAL (decl)
20594       && DECL_CONTEXT (decl)
20595       && is_tagged_type (DECL_CONTEXT (decl))
20596       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20597       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20598     /* Also ignore the artificial member typedef for the class name.  */
20599     return 1;
20600 
20601   return 0;
20602 }
20603 
20604 /* Return TRUE if TYPE is a typedef that names a type for linkage
20605    purposes. This kind of typedefs is produced by the C++ FE for
20606    constructs like:
20607 
20608    typedef struct {...} foo;
20609 
20610    In that case, there is no typedef variant type produced for foo.
20611    Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20612    struct type.  */
20613 
20614 static bool
20615 is_naming_typedef_decl (const_tree decl)
20616 {
20617   if (decl == NULL_TREE
20618       || TREE_CODE (decl) != TYPE_DECL
20619       || !is_tagged_type (TREE_TYPE (decl))
20620       || DECL_IS_BUILTIN (decl)
20621       || is_redundant_typedef (decl)
20622       /* It looks like Ada produces TYPE_DECLs that are very similar
20623          to C++ naming typedefs but that have different
20624          semantics. Let's be specific to c++ for now.  */
20625       || !is_cxx ())
20626     return FALSE;
20627 
20628   return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20629 	  && TYPE_NAME (TREE_TYPE (decl)) == decl
20630 	  && (TYPE_STUB_DECL (TREE_TYPE (decl))
20631 	      != TYPE_NAME (TREE_TYPE (decl))));
20632 }
20633 
20634 /* Looks up the DIE for a context.  */
20635 
20636 static inline dw_die_ref
20637 lookup_context_die (tree context)
20638 {
20639   if (context)
20640     {
20641       /* Find die that represents this context.  */
20642       if (TYPE_P (context))
20643 	{
20644 	  context = TYPE_MAIN_VARIANT (context);
20645 	  dw_die_ref ctx = lookup_type_die (context);
20646 	  if (!ctx)
20647 	    return NULL;
20648 	  return strip_naming_typedef (context, ctx);
20649 	}
20650       else
20651 	return lookup_decl_die (context);
20652     }
20653   return comp_unit_die ();
20654 }
20655 
20656 /* Returns the DIE for a context.  */
20657 
20658 static inline dw_die_ref
20659 get_context_die (tree context)
20660 {
20661   if (context)
20662     {
20663       /* Find die that represents this context.  */
20664       if (TYPE_P (context))
20665 	{
20666 	  context = TYPE_MAIN_VARIANT (context);
20667 	  return strip_naming_typedef (context, force_type_die (context));
20668 	}
20669       else
20670 	return force_decl_die (context);
20671     }
20672   return comp_unit_die ();
20673 }
20674 
20675 /* Returns the DIE for decl.  A DIE will always be returned.  */
20676 
20677 static dw_die_ref
20678 force_decl_die (tree decl)
20679 {
20680   dw_die_ref decl_die;
20681   unsigned saved_external_flag;
20682   tree save_fn = NULL_TREE;
20683   decl_die = lookup_decl_die (decl);
20684   if (!decl_die)
20685     {
20686       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20687 
20688       decl_die = lookup_decl_die (decl);
20689       if (decl_die)
20690 	return decl_die;
20691 
20692       switch (TREE_CODE (decl))
20693 	{
20694 	case FUNCTION_DECL:
20695 	  /* Clear current_function_decl, so that gen_subprogram_die thinks
20696 	     that this is a declaration. At this point, we just want to force
20697 	     declaration die.  */
20698 	  save_fn = current_function_decl;
20699 	  current_function_decl = NULL_TREE;
20700 	  gen_subprogram_die (decl, context_die);
20701 	  current_function_decl = save_fn;
20702 	  break;
20703 
20704 	case VAR_DECL:
20705 	  /* Set external flag to force declaration die. Restore it after
20706 	   gen_decl_die() call.  */
20707 	  saved_external_flag = DECL_EXTERNAL (decl);
20708 	  DECL_EXTERNAL (decl) = 1;
20709 	  gen_decl_die (decl, NULL, context_die);
20710 	  DECL_EXTERNAL (decl) = saved_external_flag;
20711 	  break;
20712 
20713 	case NAMESPACE_DECL:
20714 	  if (dwarf_version >= 3 || !dwarf_strict)
20715 	    dwarf2out_decl (decl);
20716 	  else
20717 	    /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
20718 	    decl_die = comp_unit_die ();
20719 	  break;
20720 
20721 	case TRANSLATION_UNIT_DECL:
20722 	  decl_die = comp_unit_die ();
20723 	  break;
20724 
20725 	default:
20726 	  gcc_unreachable ();
20727 	}
20728 
20729       /* We should be able to find the DIE now.  */
20730       if (!decl_die)
20731 	decl_die = lookup_decl_die (decl);
20732       gcc_assert (decl_die);
20733     }
20734 
20735   return decl_die;
20736 }
20737 
20738 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
20739    always returned.  */
20740 
20741 static dw_die_ref
20742 force_type_die (tree type)
20743 {
20744   dw_die_ref type_die;
20745 
20746   type_die = lookup_type_die (type);
20747   if (!type_die)
20748     {
20749       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
20750 
20751       type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
20752 				    context_die);
20753       gcc_assert (type_die);
20754     }
20755   return type_die;
20756 }
20757 
20758 /* Force out any required namespaces to be able to output DECL,
20759    and return the new context_die for it, if it's changed.  */
20760 
20761 static dw_die_ref
20762 setup_namespace_context (tree thing, dw_die_ref context_die)
20763 {
20764   tree context = (DECL_P (thing)
20765 		  ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
20766   if (context && TREE_CODE (context) == NAMESPACE_DECL)
20767     /* Force out the namespace.  */
20768     context_die = force_decl_die (context);
20769 
20770   return context_die;
20771 }
20772 
20773 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
20774    type) within its namespace, if appropriate.
20775 
20776    For compatibility with older debuggers, namespace DIEs only contain
20777    declarations; all definitions are emitted at CU scope.  */
20778 
20779 static dw_die_ref
20780 declare_in_namespace (tree thing, dw_die_ref context_die)
20781 {
20782   dw_die_ref ns_context;
20783 
20784   if (debug_info_level <= DINFO_LEVEL_TERSE)
20785     return context_die;
20786 
20787   /* External declarations in the local scope only need to be emitted
20788      once, not once in the namespace and once in the scope.
20789 
20790      This avoids declaring the `extern' below in the
20791      namespace DIE as well as in the innermost scope:
20792 
20793           namespace S
20794 	  {
20795             int i=5;
20796             int foo()
20797 	    {
20798               int i=8;
20799               extern int i;
20800      	      return i;
20801 	    }
20802           }
20803   */
20804   if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
20805     return context_die;
20806 
20807   /* If this decl is from an inlined function, then don't try to emit it in its
20808      namespace, as we will get confused.  It would have already been emitted
20809      when the abstract instance of the inline function was emitted anyways.  */
20810   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
20811     return context_die;
20812 
20813   ns_context = setup_namespace_context (thing, context_die);
20814 
20815   if (ns_context != context_die)
20816     {
20817       if (is_fortran ())
20818 	return ns_context;
20819       if (DECL_P (thing))
20820 	gen_decl_die (thing, NULL, ns_context);
20821       else
20822 	gen_type_die (thing, ns_context);
20823     }
20824   return context_die;
20825 }
20826 
20827 /* Generate a DIE for a namespace or namespace alias.  */
20828 
20829 static void
20830 gen_namespace_die (tree decl, dw_die_ref context_die)
20831 {
20832   dw_die_ref namespace_die;
20833 
20834   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
20835      they are an alias of.  */
20836   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
20837     {
20838       /* Output a real namespace or module.  */
20839       context_die = setup_namespace_context (decl, comp_unit_die ());
20840       namespace_die = new_die (is_fortran ()
20841 			       ? DW_TAG_module : DW_TAG_namespace,
20842 			       context_die, decl);
20843       /* For Fortran modules defined in different CU don't add src coords.  */
20844       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20845 	{
20846 	  const char *name = dwarf2_name (decl, 0);
20847 	  if (name)
20848 	    add_name_attribute (namespace_die, name);
20849 	}
20850       else
20851 	add_name_and_src_coords_attributes (namespace_die, decl);
20852       if (DECL_EXTERNAL (decl))
20853 	add_AT_flag (namespace_die, DW_AT_declaration, 1);
20854       equate_decl_number_to_die (decl, namespace_die);
20855     }
20856   else
20857     {
20858       /* Output a namespace alias.  */
20859 
20860       /* Force out the namespace we are an alias of, if necessary.  */
20861       dw_die_ref origin_die
20862 	= force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20863 
20864       if (DECL_FILE_SCOPE_P (decl)
20865 	  || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20866 	context_die = setup_namespace_context (decl, comp_unit_die ());
20867       /* Now create the namespace alias DIE.  */
20868       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20869       add_name_and_src_coords_attributes (namespace_die, decl);
20870       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20871       equate_decl_number_to_die (decl, namespace_die);
20872     }
20873   /* Bypass dwarf2_name's check for DECL_NAMELESS.  */
20874   if (want_pubnames ())
20875     add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
20876 }
20877 
20878 /* Generate Dwarf debug information for a decl described by DECL.
20879    The return value is currently only meaningful for PARM_DECLs,
20880    for all other decls it returns NULL.  */
20881 
20882 static dw_die_ref
20883 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20884 {
20885   tree decl_or_origin = decl ? decl : origin;
20886   tree class_origin = NULL, ultimate_origin;
20887 
20888   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20889     return NULL;
20890 
20891   /* Ignore pointer bounds decls.  */
20892   if (DECL_P (decl_or_origin)
20893       && TREE_TYPE (decl_or_origin)
20894       && POINTER_BOUNDS_P (decl_or_origin))
20895     return NULL;
20896 
20897   switch (TREE_CODE (decl_or_origin))
20898     {
20899     case ERROR_MARK:
20900       break;
20901 
20902     case CONST_DECL:
20903       if (!is_fortran () && !is_ada ())
20904 	{
20905 	  /* The individual enumerators of an enum type get output when we output
20906 	     the Dwarf representation of the relevant enum type itself.  */
20907 	  break;
20908 	}
20909 
20910       /* Emit its type.  */
20911       gen_type_die (TREE_TYPE (decl), context_die);
20912 
20913       /* And its containing namespace.  */
20914       context_die = declare_in_namespace (decl, context_die);
20915 
20916       gen_const_die (decl, context_die);
20917       break;
20918 
20919     case FUNCTION_DECL:
20920       /* Don't output any DIEs to represent mere function declarations,
20921 	 unless they are class members or explicit block externs.  */
20922       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20923           && DECL_FILE_SCOPE_P (decl_or_origin)
20924 	  && (current_function_decl == NULL_TREE
20925 	      || DECL_ARTIFICIAL (decl_or_origin)))
20926 	break;
20927 
20928 #if 0
20929       /* FIXME */
20930       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20931 	 on local redeclarations of global functions.  That seems broken.  */
20932       if (current_function_decl != decl)
20933 	/* This is only a declaration.  */;
20934 #endif
20935 
20936       /* If we're emitting a clone, emit info for the abstract instance.  */
20937       if (origin || DECL_ORIGIN (decl) != decl)
20938 	dwarf2out_abstract_function (origin
20939 				     ? DECL_ORIGIN (origin)
20940 				     : DECL_ABSTRACT_ORIGIN (decl));
20941 
20942       /* If we're emitting an out-of-line copy of an inline function,
20943 	 emit info for the abstract instance and set up to refer to it.  */
20944       else if (cgraph_function_possibly_inlined_p (decl)
20945 	       && ! DECL_ABSTRACT_P (decl)
20946 	       && ! class_or_namespace_scope_p (context_die)
20947 	       /* dwarf2out_abstract_function won't emit a die if this is just
20948 		  a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
20949 		  that case, because that works only if we have a die.  */
20950 	       && DECL_INITIAL (decl) != NULL_TREE)
20951 	{
20952 	  dwarf2out_abstract_function (decl);
20953 	  set_decl_origin_self (decl);
20954 	}
20955 
20956       /* Otherwise we're emitting the primary DIE for this decl.  */
20957       else if (debug_info_level > DINFO_LEVEL_TERSE)
20958 	{
20959 	  /* Before we describe the FUNCTION_DECL itself, make sure that we
20960 	     have its containing type.  */
20961 	  if (!origin)
20962 	    origin = decl_class_context (decl);
20963 	  if (origin != NULL_TREE)
20964 	    gen_type_die (origin, context_die);
20965 
20966 	  /* And its return type.  */
20967 	  gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20968 
20969 	  /* And its virtual context.  */
20970 	  if (DECL_VINDEX (decl) != NULL_TREE)
20971 	    gen_type_die (DECL_CONTEXT (decl), context_die);
20972 
20973 	  /* Make sure we have a member DIE for decl.  */
20974 	  if (origin != NULL_TREE)
20975 	    gen_type_die_for_member (origin, decl, context_die);
20976 
20977 	  /* And its containing namespace.  */
20978 	  context_die = declare_in_namespace (decl, context_die);
20979 	}
20980 
20981       /* Now output a DIE to represent the function itself.  */
20982       if (decl)
20983         gen_subprogram_die (decl, context_die);
20984       break;
20985 
20986     case TYPE_DECL:
20987       /* If we are in terse mode, don't generate any DIEs to represent any
20988 	 actual typedefs.  */
20989       if (debug_info_level <= DINFO_LEVEL_TERSE)
20990 	break;
20991 
20992       /* In the special case of a TYPE_DECL node representing the declaration
20993 	 of some type tag, if the given TYPE_DECL is marked as having been
20994 	 instantiated from some other (original) TYPE_DECL node (e.g. one which
20995 	 was generated within the original definition of an inline function) we
20996 	 used to generate a special (abbreviated) DW_TAG_structure_type,
20997 	 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
20998 	 should be actually referencing those DIEs, as variable DIEs with that
20999 	 type would be emitted already in the abstract origin, so it was always
21000 	 removed during unused type prunning.  Don't add anything in this
21001 	 case.  */
21002       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
21003 	break;
21004 
21005       if (is_redundant_typedef (decl))
21006 	gen_type_die (TREE_TYPE (decl), context_die);
21007       else
21008 	/* Output a DIE to represent the typedef itself.  */
21009 	gen_typedef_die (decl, context_die);
21010       break;
21011 
21012     case LABEL_DECL:
21013       if (debug_info_level >= DINFO_LEVEL_NORMAL)
21014 	gen_label_die (decl, context_die);
21015       break;
21016 
21017     case VAR_DECL:
21018     case RESULT_DECL:
21019       /* If we are in terse mode, don't generate any DIEs to represent any
21020 	 variable declarations or definitions.  */
21021       if (debug_info_level <= DINFO_LEVEL_TERSE)
21022 	break;
21023 
21024       /* Output any DIEs that are needed to specify the type of this data
21025 	 object.  */
21026       if (decl_by_reference_p (decl_or_origin))
21027 	gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21028       else
21029 	gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21030 
21031       /* And its containing type.  */
21032       class_origin = decl_class_context (decl_or_origin);
21033       if (class_origin != NULL_TREE)
21034 	gen_type_die_for_member (class_origin, decl_or_origin, context_die);
21035 
21036       /* And its containing namespace.  */
21037       context_die = declare_in_namespace (decl_or_origin, context_die);
21038 
21039       /* Now output the DIE to represent the data object itself.  This gets
21040 	 complicated because of the possibility that the VAR_DECL really
21041 	 represents an inlined instance of a formal parameter for an inline
21042 	 function.  */
21043       ultimate_origin = decl_ultimate_origin (decl_or_origin);
21044       if (ultimate_origin != NULL_TREE
21045 	  && TREE_CODE (ultimate_origin) == PARM_DECL)
21046 	gen_formal_parameter_die (decl, origin,
21047 				  true /* Emit name attribute.  */,
21048 				  context_die);
21049       else
21050 	gen_variable_die (decl, origin, context_die);
21051       break;
21052 
21053     case FIELD_DECL:
21054       /* Ignore the nameless fields that are used to skip bits but handle C++
21055 	 anonymous unions and structs.  */
21056       if (DECL_NAME (decl) != NULL_TREE
21057 	  || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
21058 	  || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
21059 	{
21060 	  gen_type_die (member_declared_type (decl), context_die);
21061 	  gen_field_die (decl, context_die);
21062 	}
21063       break;
21064 
21065     case PARM_DECL:
21066       if (DECL_BY_REFERENCE (decl_or_origin))
21067 	gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21068       else
21069 	gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21070       return gen_formal_parameter_die (decl, origin,
21071 				       true /* Emit name attribute.  */,
21072 				       context_die);
21073 
21074     case NAMESPACE_DECL:
21075     case IMPORTED_DECL:
21076       if (dwarf_version >= 3 || !dwarf_strict)
21077 	gen_namespace_die (decl, context_die);
21078       break;
21079 
21080     case NAMELIST_DECL:
21081       gen_namelist_decl (DECL_NAME (decl), context_die,
21082 			 NAMELIST_DECL_ASSOCIATED_DECL (decl));
21083       break;
21084 
21085     default:
21086       /* Probably some frontend-internal decl.  Assume we don't care.  */
21087       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
21088       break;
21089     }
21090 
21091   return NULL;
21092 }
21093 
21094 /* Output debug information for global decl DECL.  Called from toplev.c after
21095    compilation proper has finished.  */
21096 
21097 static void
21098 dwarf2out_global_decl (tree decl)
21099 {
21100   /* Output DWARF2 information for file-scope tentative data object
21101      declarations, file-scope (extern) function declarations (which
21102      had no corresponding body) and file-scope tagged type declarations
21103      and definitions which have not yet been forced out.  */
21104   if ((TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
21105       && !POINTER_BOUNDS_P (decl))
21106     dwarf2out_decl (decl);
21107 }
21108 
21109 /* Output debug information for type decl DECL.  Called from toplev.c
21110    and from language front ends (to record built-in types).  */
21111 static void
21112 dwarf2out_type_decl (tree decl, int local)
21113 {
21114   if (!local)
21115     dwarf2out_decl (decl);
21116 }
21117 
21118 /* Output debug information for imported module or decl DECL.
21119    NAME is non-NULL name in the lexical block if the decl has been renamed.
21120    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
21121    that DECL belongs to.
21122    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
21123 static void
21124 dwarf2out_imported_module_or_decl_1 (tree decl,
21125 				     tree name,
21126 				     tree lexical_block,
21127 				     dw_die_ref lexical_block_die)
21128 {
21129   expanded_location xloc;
21130   dw_die_ref imported_die = NULL;
21131   dw_die_ref at_import_die;
21132 
21133   if (TREE_CODE (decl) == IMPORTED_DECL)
21134     {
21135       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
21136       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
21137       gcc_assert (decl);
21138     }
21139   else
21140     xloc = expand_location (input_location);
21141 
21142   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
21143     {
21144       at_import_die = force_type_die (TREE_TYPE (decl));
21145       /* For namespace N { typedef void T; } using N::T; base_type_die
21146 	 returns NULL, but DW_TAG_imported_declaration requires
21147 	 the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
21148       if (!at_import_die)
21149 	{
21150 	  gcc_assert (TREE_CODE (decl) == TYPE_DECL);
21151 	  gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
21152 	  at_import_die = lookup_type_die (TREE_TYPE (decl));
21153 	  gcc_assert (at_import_die);
21154 	}
21155     }
21156   else
21157     {
21158       at_import_die = lookup_decl_die (decl);
21159       if (!at_import_die)
21160 	{
21161 	  /* If we're trying to avoid duplicate debug info, we may not have
21162 	     emitted the member decl for this field.  Emit it now.  */
21163 	  if (TREE_CODE (decl) == FIELD_DECL)
21164 	    {
21165 	      tree type = DECL_CONTEXT (decl);
21166 
21167 	      if (TYPE_CONTEXT (type)
21168 		  && TYPE_P (TYPE_CONTEXT (type))
21169 		  && !should_emit_struct_debug (TYPE_CONTEXT (type),
21170 						DINFO_USAGE_DIR_USE))
21171 		return;
21172 	      gen_type_die_for_member (type, decl,
21173 				       get_context_die (TYPE_CONTEXT (type)));
21174 	    }
21175 	  if (TREE_CODE (decl) == NAMELIST_DECL)
21176 	    at_import_die = gen_namelist_decl (DECL_NAME (decl),
21177 					 get_context_die (DECL_CONTEXT (decl)),
21178 					 NULL_TREE);
21179 	  else
21180 	    at_import_die = force_decl_die (decl);
21181 	}
21182     }
21183 
21184   if (TREE_CODE (decl) == NAMESPACE_DECL)
21185     {
21186       if (dwarf_version >= 3 || !dwarf_strict)
21187 	imported_die = new_die (DW_TAG_imported_module,
21188 				lexical_block_die,
21189 				lexical_block);
21190       else
21191 	return;
21192     }
21193   else
21194     imported_die = new_die (DW_TAG_imported_declaration,
21195 			    lexical_block_die,
21196 			    lexical_block);
21197 
21198   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
21199   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
21200   if (name)
21201     add_AT_string (imported_die, DW_AT_name,
21202 		   IDENTIFIER_POINTER (name));
21203   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
21204 }
21205 
21206 /* Output debug information for imported module or decl DECL.
21207    NAME is non-NULL name in context if the decl has been renamed.
21208    CHILD is true if decl is one of the renamed decls as part of
21209    importing whole module.  */
21210 
21211 static void
21212 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
21213 				   bool child)
21214 {
21215   /* dw_die_ref at_import_die;  */
21216   dw_die_ref scope_die;
21217 
21218   if (debug_info_level <= DINFO_LEVEL_TERSE)
21219     return;
21220 
21221   gcc_assert (decl);
21222 
21223   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
21224      We need decl DIE for reference and scope die. First, get DIE for the decl
21225      itself.  */
21226 
21227   /* Get the scope die for decl context. Use comp_unit_die for global module
21228      or decl. If die is not found for non globals, force new die.  */
21229   if (context
21230       && TYPE_P (context)
21231       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
21232     return;
21233 
21234   if (!(dwarf_version >= 3 || !dwarf_strict))
21235     return;
21236 
21237   scope_die = get_context_die (context);
21238 
21239   if (child)
21240     {
21241       gcc_assert (scope_die->die_child);
21242       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
21243       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
21244       scope_die = scope_die->die_child;
21245     }
21246 
21247   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
21248   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
21249 
21250 }
21251 
21252 /* Output debug information for namelists.   */
21253 
21254 static dw_die_ref
21255 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
21256 {
21257   dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
21258   tree value;
21259   unsigned i;
21260 
21261   if (debug_info_level <= DINFO_LEVEL_TERSE)
21262     return NULL;
21263 
21264   gcc_assert (scope_die != NULL);
21265   nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
21266   add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
21267 
21268   /* If there are no item_decls, we have a nondefining namelist, e.g.
21269      with USE association; hence, set DW_AT_declaration.  */
21270   if (item_decls == NULL_TREE)
21271     {
21272       add_AT_flag (nml_die, DW_AT_declaration, 1);
21273       return nml_die;
21274     }
21275 
21276   FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
21277     {
21278       nml_item_ref_die = lookup_decl_die (value);
21279       if (!nml_item_ref_die)
21280 	nml_item_ref_die = force_decl_die (value);
21281 
21282       nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
21283       add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
21284     }
21285   return nml_die;
21286 }
21287 
21288 
21289 /* Write the debugging output for DECL.  */
21290 
21291 static void
21292 dwarf2out_decl (tree decl)
21293 {
21294   dw_die_ref context_die = comp_unit_die ();
21295 
21296   switch (TREE_CODE (decl))
21297     {
21298     case ERROR_MARK:
21299       return;
21300 
21301     case FUNCTION_DECL:
21302       /* What we would really like to do here is to filter out all mere
21303 	 file-scope declarations of file-scope functions which are never
21304 	 referenced later within this translation unit (and keep all of ones
21305 	 that *are* referenced later on) but we aren't clairvoyant, so we have
21306 	 no idea which functions will be referenced in the future (i.e. later
21307 	 on within the current translation unit). So here we just ignore all
21308 	 file-scope function declarations which are not also definitions.  If
21309 	 and when the debugger needs to know something about these functions,
21310 	 it will have to hunt around and find the DWARF information associated
21311 	 with the definition of the function.
21312 
21313 	 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
21314 	 nodes represent definitions and which ones represent mere
21315 	 declarations.  We have to check DECL_INITIAL instead. That's because
21316 	 the C front-end supports some weird semantics for "extern inline"
21317 	 function definitions.  These can get inlined within the current
21318 	 translation unit (and thus, we need to generate Dwarf info for their
21319 	 abstract instances so that the Dwarf info for the concrete inlined
21320 	 instances can have something to refer to) but the compiler never
21321 	 generates any out-of-lines instances of such things (despite the fact
21322 	 that they *are* definitions).
21323 
21324 	 The important point is that the C front-end marks these "extern
21325 	 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
21326 	 them anyway. Note that the C++ front-end also plays some similar games
21327 	 for inline function definitions appearing within include files which
21328 	 also contain `#pragma interface' pragmas.
21329 
21330 	 If we are called from dwarf2out_abstract_function output a DIE
21331 	 anyway.  We can end up here this way with early inlining and LTO
21332 	 where the inlined function is output in a different LTRANS unit
21333 	 or not at all.  */
21334       if (DECL_INITIAL (decl) == NULL_TREE
21335 	  && ! DECL_ABSTRACT_P (decl))
21336 	return;
21337 
21338       /* If we're a nested function, initially use a parent of NULL; if we're
21339 	 a plain function, this will be fixed up in decls_for_scope.  If
21340 	 we're a method, it will be ignored, since we already have a DIE.  */
21341       if (decl_function_context (decl)
21342 	  /* But if we're in terse mode, we don't care about scope.  */
21343 	  && debug_info_level > DINFO_LEVEL_TERSE)
21344 	context_die = NULL;
21345       break;
21346 
21347     case VAR_DECL:
21348       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
21349 	 declaration and if the declaration was never even referenced from
21350 	 within this entire compilation unit.  We suppress these DIEs in
21351 	 order to save space in the .debug section (by eliminating entries
21352 	 which are probably useless).  Note that we must not suppress
21353 	 block-local extern declarations (whether used or not) because that
21354 	 would screw-up the debugger's name lookup mechanism and cause it to
21355 	 miss things which really ought to be in scope at a given point.  */
21356       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
21357 	return;
21358 
21359       /* For local statics lookup proper context die.  */
21360       if (TREE_STATIC (decl)
21361 	  && DECL_CONTEXT (decl)
21362 	  && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
21363 	context_die = lookup_decl_die (DECL_CONTEXT (decl));
21364 
21365       /* If we are in terse mode, don't generate any DIEs to represent any
21366 	 variable declarations or definitions.  */
21367       if (debug_info_level <= DINFO_LEVEL_TERSE)
21368 	return;
21369       break;
21370 
21371     case CONST_DECL:
21372       if (debug_info_level <= DINFO_LEVEL_TERSE)
21373 	return;
21374       if (!is_fortran () && !is_ada ())
21375 	return;
21376       if (TREE_STATIC (decl) && decl_function_context (decl))
21377 	context_die = lookup_decl_die (DECL_CONTEXT (decl));
21378       break;
21379 
21380     case NAMESPACE_DECL:
21381     case IMPORTED_DECL:
21382       if (debug_info_level <= DINFO_LEVEL_TERSE)
21383 	return;
21384       if (lookup_decl_die (decl) != NULL)
21385 	return;
21386       break;
21387 
21388     case TYPE_DECL:
21389       /* Don't emit stubs for types unless they are needed by other DIEs.  */
21390       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
21391 	return;
21392 
21393       /* Don't bother trying to generate any DIEs to represent any of the
21394 	 normal built-in types for the language we are compiling.  */
21395       if (DECL_IS_BUILTIN (decl))
21396 	return;
21397 
21398       /* If we are in terse mode, don't generate any DIEs for types.  */
21399       if (debug_info_level <= DINFO_LEVEL_TERSE)
21400 	return;
21401 
21402       /* If we're a function-scope tag, initially use a parent of NULL;
21403 	 this will be fixed up in decls_for_scope.  */
21404       if (decl_function_context (decl))
21405 	context_die = NULL;
21406 
21407       break;
21408 
21409     case NAMELIST_DECL:
21410       break;
21411 
21412     default:
21413       return;
21414     }
21415 
21416   gen_decl_die (decl, NULL, context_die);
21417 }
21418 
21419 /* Write the debugging output for DECL.  */
21420 
21421 static void
21422 dwarf2out_function_decl (tree decl)
21423 {
21424   dwarf2out_decl (decl);
21425   call_arg_locations = NULL;
21426   call_arg_loc_last = NULL;
21427   call_site_count = -1;
21428   tail_call_site_count = -1;
21429   block_map.release ();
21430   decl_loc_table->empty ();
21431   cached_dw_loc_list_table->empty ();
21432 }
21433 
21434 /* Output a marker (i.e. a label) for the beginning of the generated code for
21435    a lexical block.  */
21436 
21437 static void
21438 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21439 		       unsigned int blocknum)
21440 {
21441   switch_to_section (current_function_section ());
21442   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
21443 }
21444 
21445 /* Output a marker (i.e. a label) for the end of the generated code for a
21446    lexical block.  */
21447 
21448 static void
21449 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
21450 {
21451   switch_to_section (current_function_section ());
21452   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
21453 }
21454 
21455 /* Returns nonzero if it is appropriate not to emit any debugging
21456    information for BLOCK, because it doesn't contain any instructions.
21457 
21458    Don't allow this for blocks with nested functions or local classes
21459    as we would end up with orphans, and in the presence of scheduling
21460    we may end up calling them anyway.  */
21461 
21462 static bool
21463 dwarf2out_ignore_block (const_tree block)
21464 {
21465   tree decl;
21466   unsigned int i;
21467 
21468   for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
21469     if (TREE_CODE (decl) == FUNCTION_DECL
21470 	|| (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21471       return 0;
21472   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
21473     {
21474       decl = BLOCK_NONLOCALIZED_VAR (block, i);
21475       if (TREE_CODE (decl) == FUNCTION_DECL
21476 	  || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21477       return 0;
21478     }
21479 
21480   return 1;
21481 }
21482 
21483 /* Hash table routines for file_hash.  */
21484 
21485 bool
21486 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
21487 {
21488   return filename_cmp (p1->filename, p2) == 0;
21489 }
21490 
21491 hashval_t
21492 dwarf_file_hasher::hash (dwarf_file_data *p)
21493 {
21494   return htab_hash_string (p->filename);
21495 }
21496 
21497 /* Lookup FILE_NAME (in the list of filenames that we know about here in
21498    dwarf2out.c) and return its "index".  The index of each (known) filename is
21499    just a unique number which is associated with only that one filename.  We
21500    need such numbers for the sake of generating labels (in the .debug_sfnames
21501    section) and references to those files numbers (in the .debug_srcinfo
21502    and.debug_macinfo sections).  If the filename given as an argument is not
21503    found in our current list, add it to the list and assign it the next
21504    available unique index number.  In order to speed up searches, we remember
21505    the index of the filename was looked up last.  This handles the majority of
21506    all searches.  */
21507 
21508 static struct dwarf_file_data *
21509 lookup_filename (const char *file_name)
21510 {
21511   struct dwarf_file_data * created;
21512 
21513   /* Check to see if the file name that was searched on the previous
21514      call matches this file name.  If so, return the index.  */
21515   if (file_table_last_lookup
21516       && (file_name == file_table_last_lookup->filename
21517 	  || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
21518     return file_table_last_lookup;
21519 
21520   /* Didn't match the previous lookup, search the table.  */
21521   dwarf_file_data **slot
21522     = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
21523 				       INSERT);
21524   if (*slot)
21525     return *slot;
21526 
21527   created = ggc_alloc<dwarf_file_data> ();
21528   created->filename = file_name;
21529   created->emitted_number = 0;
21530   *slot = created;
21531   return created;
21532 }
21533 
21534 /* If the assembler will construct the file table, then translate the compiler
21535    internal file table number into the assembler file table number, and emit
21536    a .file directive if we haven't already emitted one yet.  The file table
21537    numbers are different because we prune debug info for unused variables and
21538    types, which may include filenames.  */
21539 
21540 static int
21541 maybe_emit_file (struct dwarf_file_data * fd)
21542 {
21543   if (! fd->emitted_number)
21544     {
21545       if (last_emitted_file)
21546 	fd->emitted_number = last_emitted_file->emitted_number + 1;
21547       else
21548 	fd->emitted_number = 1;
21549       last_emitted_file = fd;
21550 
21551       if (DWARF2_ASM_LINE_DEBUG_INFO)
21552 	{
21553 	  fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
21554 	  output_quoted_string (asm_out_file,
21555 				remap_debug_filename (fd->filename));
21556 	  fputc ('\n', asm_out_file);
21557 	}
21558     }
21559 
21560   return fd->emitted_number;
21561 }
21562 
21563 /* Schedule generation of a DW_AT_const_value attribute to DIE.
21564    That generation should happen after function debug info has been
21565    generated. The value of the attribute is the constant value of ARG.  */
21566 
21567 static void
21568 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
21569 {
21570   die_arg_entry entry;
21571 
21572   if (!die || !arg)
21573     return;
21574 
21575   if (!tmpl_value_parm_die_table)
21576     vec_alloc (tmpl_value_parm_die_table, 32);
21577 
21578   entry.die = die;
21579   entry.arg = arg;
21580   vec_safe_push (tmpl_value_parm_die_table, entry);
21581 }
21582 
21583 /* Return TRUE if T is an instance of generic type, FALSE
21584    otherwise.  */
21585 
21586 static bool
21587 generic_type_p (tree t)
21588 {
21589   if (t == NULL_TREE || !TYPE_P (t))
21590     return false;
21591   return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
21592 }
21593 
21594 /* Schedule the generation of the generic parameter dies for the
21595   instance of generic type T. The proper generation itself is later
21596   done by gen_scheduled_generic_parms_dies. */
21597 
21598 static void
21599 schedule_generic_params_dies_gen (tree t)
21600 {
21601   if (!generic_type_p (t))
21602     return;
21603 
21604   if (!generic_type_instances)
21605     vec_alloc (generic_type_instances, 256);
21606 
21607   vec_safe_push (generic_type_instances, t);
21608 }
21609 
21610 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21611    by append_entry_to_tmpl_value_parm_die_table. This function must
21612    be called after function DIEs have been generated.  */
21613 
21614 static void
21615 gen_remaining_tmpl_value_param_die_attribute (void)
21616 {
21617   if (tmpl_value_parm_die_table)
21618     {
21619       unsigned i;
21620       die_arg_entry *e;
21621 
21622       FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
21623 	tree_add_const_value_attribute (e->die, e->arg);
21624     }
21625 }
21626 
21627 /* Generate generic parameters DIEs for instances of generic types
21628    that have been previously scheduled by
21629    schedule_generic_params_dies_gen. This function must be called
21630    after all the types of the CU have been laid out.  */
21631 
21632 static void
21633 gen_scheduled_generic_parms_dies (void)
21634 {
21635   unsigned i;
21636   tree t;
21637 
21638   if (!generic_type_instances)
21639     return;
21640 
21641   FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
21642     if (COMPLETE_TYPE_P (t))
21643       gen_generic_params_dies (t);
21644 }
21645 
21646 
21647 /* Replace DW_AT_name for the decl with name.  */
21648 
21649 static void
21650 dwarf2out_set_name (tree decl, tree name)
21651 {
21652   dw_die_ref die;
21653   dw_attr_ref attr;
21654   const char *dname;
21655 
21656   die = TYPE_SYMTAB_DIE (decl);
21657   if (!die)
21658     return;
21659 
21660   dname = dwarf2_name (name, 0);
21661   if (!dname)
21662     return;
21663 
21664   attr = get_AT (die, DW_AT_name);
21665   if (attr)
21666     {
21667       struct indirect_string_node *node;
21668 
21669       node = find_AT_string (dname);
21670       /* replace the string.  */
21671       attr->dw_attr_val.v.val_str = node;
21672     }
21673 
21674   else
21675     add_name_attribute (die, dname);
21676 }
21677 
21678 /* True if before or during processing of the first function being emitted.  */
21679 static bool in_first_function_p = true;
21680 /* True if loc_note during dwarf2out_var_location call might still be
21681    before first real instruction at address equal to .Ltext0.  */
21682 static bool maybe_at_text_label_p = true;
21683 /* One above highest N where .LVLN label might be equal to .Ltext0 label.  */
21684 static unsigned int first_loclabel_num_not_at_text_label;
21685 
21686 /* Called by the final INSN scan whenever we see a var location.  We
21687    use it to drop labels in the right places, and throw the location in
21688    our lookup table.  */
21689 
21690 static void
21691 dwarf2out_var_location (rtx_insn *loc_note)
21692 {
21693   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21694   struct var_loc_node *newloc;
21695   rtx_insn *next_real, *next_note;
21696   static const char *last_label;
21697   static const char *last_postcall_label;
21698   static bool last_in_cold_section_p;
21699   static rtx_insn *expected_next_loc_note;
21700   tree decl;
21701   bool var_loc_p;
21702 
21703   if (!NOTE_P (loc_note))
21704     {
21705       if (CALL_P (loc_note))
21706 	{
21707 	  call_site_count++;
21708 	  if (SIBLING_CALL_P (loc_note))
21709 	    tail_call_site_count++;
21710 	}
21711       return;
21712     }
21713 
21714   var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
21715   if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21716     return;
21717 
21718   /* Optimize processing a large consecutive sequence of location
21719      notes so we don't spend too much time in next_real_insn.  If the
21720      next insn is another location note, remember the next_real_insn
21721      calculation for next time.  */
21722   next_real = cached_next_real_insn;
21723   if (next_real)
21724     {
21725       if (expected_next_loc_note != loc_note)
21726 	next_real = NULL;
21727     }
21728 
21729   next_note = NEXT_INSN (loc_note);
21730   if (! next_note
21731       || next_note->deleted ()
21732       || ! NOTE_P (next_note)
21733       || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
21734 	  && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
21735     next_note = NULL;
21736 
21737   if (! next_real)
21738     next_real = next_real_insn (loc_note);
21739 
21740   if (next_note)
21741     {
21742       expected_next_loc_note = next_note;
21743       cached_next_real_insn = next_real;
21744     }
21745   else
21746     cached_next_real_insn = NULL;
21747 
21748   /* If there are no instructions which would be affected by this note,
21749      don't do anything.  */
21750   if (var_loc_p
21751       && next_real == NULL_RTX
21752       && !NOTE_DURING_CALL_P (loc_note))
21753     return;
21754 
21755   if (next_real == NULL_RTX)
21756     next_real = get_last_insn ();
21757 
21758   /* If there were any real insns between note we processed last time
21759      and this note (or if it is the first note), clear
21760      last_{,postcall_}label so that they are not reused this time.  */
21761   if (last_var_location_insn == NULL_RTX
21762       || last_var_location_insn != next_real
21763       || last_in_cold_section_p != in_cold_section_p)
21764     {
21765       last_label = NULL;
21766       last_postcall_label = NULL;
21767     }
21768 
21769   if (var_loc_p)
21770     {
21771       decl = NOTE_VAR_LOCATION_DECL (loc_note);
21772       newloc = add_var_loc_to_decl (decl, loc_note,
21773 				    NOTE_DURING_CALL_P (loc_note)
21774 				    ? last_postcall_label : last_label);
21775       if (newloc == NULL)
21776 	return;
21777     }
21778   else
21779     {
21780       decl = NULL_TREE;
21781       newloc = NULL;
21782     }
21783 
21784   /* If there were no real insns between note we processed last time
21785      and this note, use the label we emitted last time.  Otherwise
21786      create a new label and emit it.  */
21787   if (last_label == NULL)
21788     {
21789       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21790       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21791       loclabel_num++;
21792       last_label = ggc_strdup (loclabel);
21793       /* See if loclabel might be equal to .Ltext0.  If yes,
21794 	 bump first_loclabel_num_not_at_text_label.  */
21795       if (!have_multiple_function_sections
21796 	  && in_first_function_p
21797 	  && maybe_at_text_label_p)
21798 	{
21799 	  static rtx_insn *last_start;
21800 	  rtx_insn *insn;
21801 	  for (insn = loc_note; insn; insn = previous_insn (insn))
21802 	    if (insn == last_start)
21803 	      break;
21804 	    else if (!NONDEBUG_INSN_P (insn))
21805 	      continue;
21806 	    else
21807 	      {
21808 		rtx body = PATTERN (insn);
21809 		if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
21810 		  continue;
21811 		/* Inline asm could occupy zero bytes.  */
21812 		else if (GET_CODE (body) == ASM_INPUT
21813 			 || asm_noperands (body) >= 0)
21814 		  continue;
21815 #ifdef HAVE_attr_length
21816 		else if (get_attr_min_length (insn) == 0)
21817 		  continue;
21818 #endif
21819 		else
21820 		  {
21821 		    /* Assume insn has non-zero length.  */
21822 		    maybe_at_text_label_p = false;
21823 		    break;
21824 		  }
21825 	      }
21826 	  if (maybe_at_text_label_p)
21827 	    {
21828 	      last_start = loc_note;
21829 	      first_loclabel_num_not_at_text_label = loclabel_num;
21830 	    }
21831 	}
21832     }
21833 
21834   if (!var_loc_p)
21835     {
21836       struct call_arg_loc_node *ca_loc
21837 	= ggc_cleared_alloc<call_arg_loc_node> ();
21838       rtx_insn *prev = prev_real_insn (loc_note);
21839       rtx x;
21840       ca_loc->call_arg_loc_note = loc_note;
21841       ca_loc->next = NULL;
21842       ca_loc->label = last_label;
21843       gcc_assert (prev
21844 		  && (CALL_P (prev)
21845 		      || (NONJUMP_INSN_P (prev)
21846 			  && GET_CODE (PATTERN (prev)) == SEQUENCE
21847 			  && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
21848       if (!CALL_P (prev))
21849 	prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
21850       ca_loc->tail_call_p = SIBLING_CALL_P (prev);
21851       x = get_call_rtx_from (PATTERN (prev));
21852       if (x)
21853 	{
21854 	  x = XEXP (XEXP (x, 0), 0);
21855 	  if (GET_CODE (x) == SYMBOL_REF
21856 	      && SYMBOL_REF_DECL (x)
21857 	      && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
21858 	    ca_loc->symbol_ref = x;
21859 	}
21860       ca_loc->block = insn_scope (prev);
21861       if (call_arg_locations)
21862 	call_arg_loc_last->next = ca_loc;
21863       else
21864 	call_arg_locations = ca_loc;
21865       call_arg_loc_last = ca_loc;
21866     }
21867   else if (!NOTE_DURING_CALL_P (loc_note))
21868     newloc->label = last_label;
21869   else
21870     {
21871       if (!last_postcall_label)
21872 	{
21873 	  sprintf (loclabel, "%s-1", last_label);
21874 	  last_postcall_label = ggc_strdup (loclabel);
21875 	}
21876       newloc->label = last_postcall_label;
21877     }
21878 
21879   last_var_location_insn = next_real;
21880   last_in_cold_section_p = in_cold_section_p;
21881 }
21882 
21883 /* Note in one location list that text section has changed.  */
21884 
21885 int
21886 var_location_switch_text_section_1 (var_loc_list **slot, void *)
21887 {
21888   var_loc_list *list = *slot;
21889   if (list->first)
21890     list->last_before_switch
21891       = list->last->next ? list->last->next : list->last;
21892   return 1;
21893 }
21894 
21895 /* Note in all location lists that text section has changed.  */
21896 
21897 static void
21898 var_location_switch_text_section (void)
21899 {
21900   if (decl_loc_table == NULL)
21901     return;
21902 
21903   decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
21904 }
21905 
21906 /* Create a new line number table.  */
21907 
21908 static dw_line_info_table *
21909 new_line_info_table (void)
21910 {
21911   dw_line_info_table *table;
21912 
21913   table = ggc_cleared_alloc<dw_line_info_table_struct> ();
21914   table->file_num = 1;
21915   table->line_num = 1;
21916   table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
21917 
21918   return table;
21919 }
21920 
21921 /* Lookup the "current" table into which we emit line info, so
21922    that we don't have to do it for every source line.  */
21923 
21924 static void
21925 set_cur_line_info_table (section *sec)
21926 {
21927   dw_line_info_table *table;
21928 
21929   if (sec == text_section)
21930     table = text_section_line_info;
21931   else if (sec == cold_text_section)
21932     {
21933       table = cold_text_section_line_info;
21934       if (!table)
21935 	{
21936 	  cold_text_section_line_info = table = new_line_info_table ();
21937 	  table->end_label = cold_end_label;
21938 	}
21939     }
21940   else
21941     {
21942       const char *end_label;
21943 
21944       if (flag_reorder_blocks_and_partition)
21945 	{
21946 	  if (in_cold_section_p)
21947 	    end_label = crtl->subsections.cold_section_end_label;
21948 	  else
21949 	    end_label = crtl->subsections.hot_section_end_label;
21950 	}
21951       else
21952 	{
21953 	  char label[MAX_ARTIFICIAL_LABEL_BYTES];
21954 	  ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
21955 				       current_function_funcdef_no);
21956 	  end_label = ggc_strdup (label);
21957 	}
21958 
21959       table = new_line_info_table ();
21960       table->end_label = end_label;
21961 
21962       vec_safe_push (separate_line_info, table);
21963     }
21964 
21965   if (DWARF2_ASM_LINE_DEBUG_INFO)
21966     table->is_stmt = (cur_line_info_table
21967 		      ? cur_line_info_table->is_stmt
21968 		      : DWARF_LINE_DEFAULT_IS_STMT_START);
21969   cur_line_info_table = table;
21970 }
21971 
21972 
21973 /* We need to reset the locations at the beginning of each
21974    function. We can't do this in the end_function hook, because the
21975    declarations that use the locations won't have been output when
21976    that hook is called.  Also compute have_multiple_function_sections here.  */
21977 
21978 static void
21979 dwarf2out_begin_function (tree fun)
21980 {
21981   section *sec = function_section (fun);
21982 
21983   if (sec != text_section)
21984     have_multiple_function_sections = true;
21985 
21986   if (flag_reorder_blocks_and_partition && !cold_text_section)
21987     {
21988       gcc_assert (current_function_decl == fun);
21989       cold_text_section = unlikely_text_section ();
21990       switch_to_section (cold_text_section);
21991       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21992       switch_to_section (sec);
21993     }
21994 
21995   dwarf2out_note_section_used ();
21996   call_site_count = 0;
21997   tail_call_site_count = 0;
21998 
21999   set_cur_line_info_table (sec);
22000 }
22001 
22002 /* Helper function of dwarf2out_end_function, called only after emitting
22003    the very first function into assembly.  Check if some .debug_loc range
22004    might end with a .LVL* label that could be equal to .Ltext0.
22005    In that case we must force using absolute addresses in .debug_loc ranges,
22006    because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
22007    .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
22008    list terminator.
22009    Set have_multiple_function_sections to true in that case and
22010    terminate htab traversal.  */
22011 
22012 int
22013 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
22014 {
22015   var_loc_list *entry = *slot;
22016   struct var_loc_node *node;
22017 
22018   node = entry->first;
22019   if (node && node->next && node->next->label)
22020     {
22021       unsigned int i;
22022       const char *label = node->next->label;
22023       char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
22024 
22025       for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
22026 	{
22027 	  ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
22028 	  if (strcmp (label, loclabel) == 0)
22029 	    {
22030 	      have_multiple_function_sections = true;
22031 	      return 0;
22032 	    }
22033 	}
22034     }
22035   return 1;
22036 }
22037 
22038 /* Hook called after emitting a function into assembly.
22039    This does something only for the very first function emitted.  */
22040 
22041 static void
22042 dwarf2out_end_function (unsigned int)
22043 {
22044   if (in_first_function_p
22045       && !have_multiple_function_sections
22046       && first_loclabel_num_not_at_text_label
22047       && decl_loc_table)
22048     decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
22049   in_first_function_p = false;
22050   maybe_at_text_label_p = false;
22051 }
22052 
22053 /* Temporary holder for dwarf2out_register_main_translation_unit.  Used to let
22054    front-ends register a translation unit even before dwarf2out_init is
22055    called.  */
22056 static tree main_translation_unit = NULL_TREE;
22057 
22058 /* Hook called by front-ends after they built their main translation unit.
22059    Associate comp_unit_die to UNIT.  */
22060 
22061 static void
22062 dwarf2out_register_main_translation_unit (tree unit)
22063 {
22064   gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
22065 	      && main_translation_unit == NULL_TREE);
22066   main_translation_unit = unit;
22067   /* If dwarf2out_init has not been called yet, it will perform the association
22068      itself looking at main_translation_unit.  */
22069   if (decl_die_table != NULL)
22070     equate_decl_number_to_die (unit, comp_unit_die ());
22071 }
22072 
22073 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE.  */
22074 
22075 static void
22076 push_dw_line_info_entry (dw_line_info_table *table,
22077 			 enum dw_line_info_opcode opcode, unsigned int val)
22078 {
22079   dw_line_info_entry e;
22080   e.opcode = opcode;
22081   e.val = val;
22082   vec_safe_push (table->entries, e);
22083 }
22084 
22085 /* Output a label to mark the beginning of a source code line entry
22086    and record information relating to this source line, in
22087    'line_info_table' for later output of the .debug_line section.  */
22088 /* ??? The discriminator parameter ought to be unsigned.  */
22089 
22090 static void
22091 dwarf2out_source_line (unsigned int line, const char *filename,
22092                        int discriminator, bool is_stmt)
22093 {
22094   unsigned int file_num;
22095   dw_line_info_table *table;
22096 
22097   if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
22098     return;
22099 
22100   /* The discriminator column was added in dwarf4.  Simplify the below
22101      by simply removing it if we're not supposed to output it.  */
22102   if (dwarf_version < 4 && dwarf_strict)
22103     discriminator = 0;
22104 
22105   table = cur_line_info_table;
22106   file_num = maybe_emit_file (lookup_filename (filename));
22107 
22108   /* ??? TODO: Elide duplicate line number entries.  Traditionally,
22109      the debugger has used the second (possibly duplicate) line number
22110      at the beginning of the function to mark the end of the prologue.
22111      We could eliminate any other duplicates within the function.  For
22112      Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
22113      that second line number entry.  */
22114   /* Recall that this end-of-prologue indication is *not* the same thing
22115      as the end_prologue debug hook.  The NOTE_INSN_PROLOGUE_END note,
22116      to which the hook corresponds, follows the last insn that was
22117      emitted by gen_prologue.  What we need is to precede the first insn
22118      that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
22119      insn that corresponds to something the user wrote.  These may be
22120      very different locations once scheduling is enabled.  */
22121 
22122   if (0 && file_num == table->file_num
22123       && line == table->line_num
22124       && discriminator == table->discrim_num
22125       && is_stmt == table->is_stmt)
22126     return;
22127 
22128   switch_to_section (current_function_section ());
22129 
22130   /* If requested, emit something human-readable.  */
22131   if (flag_debug_asm)
22132     fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
22133 
22134   if (DWARF2_ASM_LINE_DEBUG_INFO)
22135     {
22136       /* Emit the .loc directive understood by GNU as.  */
22137       /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
22138 	 file_num, line, is_stmt, discriminator */
22139       fputs ("\t.loc ", asm_out_file);
22140       fprint_ul (asm_out_file, file_num);
22141       putc (' ', asm_out_file);
22142       fprint_ul (asm_out_file, line);
22143       putc (' ', asm_out_file);
22144       putc ('0', asm_out_file);
22145 
22146       if (is_stmt != table->is_stmt)
22147 	{
22148 	  fputs (" is_stmt ", asm_out_file);
22149 	  putc (is_stmt ? '1' : '0', asm_out_file);
22150 	}
22151       if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
22152 	{
22153 	  gcc_assert (discriminator > 0);
22154 	  fputs (" discriminator ", asm_out_file);
22155 	  fprint_ul (asm_out_file, (unsigned long) discriminator);
22156 	}
22157       putc ('\n', asm_out_file);
22158     }
22159   else
22160     {
22161       unsigned int label_num = ++line_info_label_num;
22162 
22163       targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
22164 
22165       push_dw_line_info_entry (table, LI_set_address, label_num);
22166       if (file_num != table->file_num)
22167 	push_dw_line_info_entry (table, LI_set_file, file_num);
22168       if (discriminator != table->discrim_num)
22169 	push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
22170       if (is_stmt != table->is_stmt)
22171 	push_dw_line_info_entry (table, LI_negate_stmt, 0);
22172       push_dw_line_info_entry (table, LI_set_line, line);
22173     }
22174 
22175   table->file_num = file_num;
22176   table->line_num = line;
22177   table->discrim_num = discriminator;
22178   table->is_stmt = is_stmt;
22179   table->in_use = true;
22180 }
22181 
22182 /* Record the beginning of a new source file.  */
22183 
22184 static void
22185 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
22186 {
22187   if (flag_eliminate_dwarf2_dups)
22188     {
22189       /* Record the beginning of the file for break_out_includes.  */
22190       dw_die_ref bincl_die;
22191 
22192       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
22193       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
22194     }
22195 
22196   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22197     {
22198       macinfo_entry e;
22199       e.code = DW_MACINFO_start_file;
22200       e.lineno = lineno;
22201       e.info = ggc_strdup (filename);
22202       vec_safe_push (macinfo_table, e);
22203     }
22204 }
22205 
22206 /* Record the end of a source file.  */
22207 
22208 static void
22209 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
22210 {
22211   if (flag_eliminate_dwarf2_dups)
22212     /* Record the end of the file for break_out_includes.  */
22213     new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
22214 
22215   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22216     {
22217       macinfo_entry e;
22218       e.code = DW_MACINFO_end_file;
22219       e.lineno = lineno;
22220       e.info = NULL;
22221       vec_safe_push (macinfo_table, e);
22222     }
22223 }
22224 
22225 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
22226    the tail part of the directive line, i.e. the part which is past the
22227    initial whitespace, #, whitespace, directive-name, whitespace part.  */
22228 
22229 static void
22230 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
22231 		  const char *buffer ATTRIBUTE_UNUSED)
22232 {
22233   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22234     {
22235       macinfo_entry e;
22236       /* Insert a dummy first entry to be able to optimize the whole
22237 	 predefined macro block using DW_MACRO_GNU_transparent_include.  */
22238       if (macinfo_table->is_empty () && lineno <= 1)
22239 	{
22240 	  e.code = 0;
22241 	  e.lineno = 0;
22242 	  e.info = NULL;
22243 	  vec_safe_push (macinfo_table, e);
22244 	}
22245       e.code = DW_MACINFO_define;
22246       e.lineno = lineno;
22247       e.info = ggc_strdup (buffer);
22248       vec_safe_push (macinfo_table, e);
22249     }
22250 }
22251 
22252 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
22253    the tail part of the directive line, i.e. the part which is past the
22254    initial whitespace, #, whitespace, directive-name, whitespace part.  */
22255 
22256 static void
22257 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
22258 		 const char *buffer ATTRIBUTE_UNUSED)
22259 {
22260   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22261     {
22262       macinfo_entry e;
22263       /* Insert a dummy first entry to be able to optimize the whole
22264 	 predefined macro block using DW_MACRO_GNU_transparent_include.  */
22265       if (macinfo_table->is_empty () && lineno <= 1)
22266 	{
22267 	  e.code = 0;
22268 	  e.lineno = 0;
22269 	  e.info = NULL;
22270 	  vec_safe_push (macinfo_table, e);
22271 	}
22272       e.code = DW_MACINFO_undef;
22273       e.lineno = lineno;
22274       e.info = ggc_strdup (buffer);
22275       vec_safe_push (macinfo_table, e);
22276     }
22277 }
22278 
22279 /* Helpers to manipulate hash table of CUs.  */
22280 
22281 struct macinfo_entry_hasher : typed_noop_remove <macinfo_entry>
22282 {
22283   typedef macinfo_entry value_type;
22284   typedef macinfo_entry compare_type;
22285   static inline hashval_t hash (const value_type *);
22286   static inline bool equal (const value_type *, const compare_type *);
22287 };
22288 
22289 inline hashval_t
22290 macinfo_entry_hasher::hash (const value_type *entry)
22291 {
22292   return htab_hash_string (entry->info);
22293 }
22294 
22295 inline bool
22296 macinfo_entry_hasher::equal (const value_type *entry1,
22297 			     const compare_type *entry2)
22298 {
22299   return !strcmp (entry1->info, entry2->info);
22300 }
22301 
22302 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
22303 
22304 /* Output a single .debug_macinfo entry.  */
22305 
22306 static void
22307 output_macinfo_op (macinfo_entry *ref)
22308 {
22309   int file_num;
22310   size_t len;
22311   struct indirect_string_node *node;
22312   char label[MAX_ARTIFICIAL_LABEL_BYTES];
22313   struct dwarf_file_data *fd;
22314 
22315   switch (ref->code)
22316     {
22317     case DW_MACINFO_start_file:
22318       fd = lookup_filename (ref->info);
22319       file_num = maybe_emit_file (fd);
22320       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
22321       dw2_asm_output_data_uleb128 (ref->lineno,
22322 				   "Included from line number %lu",
22323 				   (unsigned long) ref->lineno);
22324       dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
22325       break;
22326     case DW_MACINFO_end_file:
22327       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
22328       break;
22329     case DW_MACINFO_define:
22330     case DW_MACINFO_undef:
22331       len = strlen (ref->info) + 1;
22332       if (!dwarf_strict
22333 	  && len > DWARF_OFFSET_SIZE
22334 	  && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
22335 	  && (debug_str_section->common.flags & SECTION_MERGE) != 0)
22336 	{
22337 	  ref->code = ref->code == DW_MACINFO_define
22338 		      ? DW_MACRO_GNU_define_indirect
22339 		      : DW_MACRO_GNU_undef_indirect;
22340 	  output_macinfo_op (ref);
22341 	  return;
22342 	}
22343       dw2_asm_output_data (1, ref->code,
22344 			   ref->code == DW_MACINFO_define
22345 			   ? "Define macro" : "Undefine macro");
22346       dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22347 				   (unsigned long) ref->lineno);
22348       dw2_asm_output_nstring (ref->info, -1, "The macro");
22349       break;
22350     case DW_MACRO_GNU_define_indirect:
22351     case DW_MACRO_GNU_undef_indirect:
22352       node = find_AT_string (ref->info);
22353       gcc_assert (node
22354                   && ((node->form == DW_FORM_strp)
22355                       || (node->form == DW_FORM_GNU_str_index)));
22356       dw2_asm_output_data (1, ref->code,
22357 			   ref->code == DW_MACRO_GNU_define_indirect
22358 			   ? "Define macro indirect"
22359 			   : "Undefine macro indirect");
22360       dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22361 				   (unsigned long) ref->lineno);
22362       if (node->form == DW_FORM_strp)
22363         dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
22364                                debug_str_section, "The macro: \"%s\"",
22365                                ref->info);
22366       else
22367         dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
22368                                      ref->info);
22369       break;
22370     case DW_MACRO_GNU_transparent_include:
22371       dw2_asm_output_data (1, ref->code, "Transparent include");
22372       ASM_GENERATE_INTERNAL_LABEL (label,
22373 				   DEBUG_MACRO_SECTION_LABEL, ref->lineno);
22374       dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
22375       break;
22376     default:
22377       fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
22378 	       ASM_COMMENT_START, (unsigned long) ref->code);
22379       break;
22380     }
22381 }
22382 
22383 /* Attempt to make a sequence of define/undef macinfo ops shareable with
22384    other compilation unit .debug_macinfo sections.  IDX is the first
22385    index of a define/undef, return the number of ops that should be
22386    emitted in a comdat .debug_macinfo section and emit
22387    a DW_MACRO_GNU_transparent_include entry referencing it.
22388    If the define/undef entry should be emitted normally, return 0.  */
22389 
22390 static unsigned
22391 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
22392 			macinfo_hash_type **macinfo_htab)
22393 {
22394   macinfo_entry *first, *second, *cur, *inc;
22395   char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
22396   unsigned char checksum[16];
22397   struct md5_ctx ctx;
22398   char *grp_name, *tail;
22399   const char *base;
22400   unsigned int i, count, encoded_filename_len, linebuf_len;
22401   macinfo_entry **slot;
22402 
22403   first = &(*macinfo_table)[idx];
22404   second = &(*macinfo_table)[idx + 1];
22405 
22406   /* Optimize only if there are at least two consecutive define/undef ops,
22407      and either all of them are before first DW_MACINFO_start_file
22408      with lineno {0,1} (i.e. predefined macro block), or all of them are
22409      in some included header file.  */
22410   if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
22411     return 0;
22412   if (vec_safe_is_empty (files))
22413     {
22414       if (first->lineno > 1 || second->lineno > 1)
22415 	return 0;
22416     }
22417   else if (first->lineno == 0)
22418     return 0;
22419 
22420   /* Find the last define/undef entry that can be grouped together
22421      with first and at the same time compute md5 checksum of their
22422      codes, linenumbers and strings.  */
22423   md5_init_ctx (&ctx);
22424   for (i = idx; macinfo_table->iterate (i, &cur); i++)
22425     if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
22426       break;
22427     else if (vec_safe_is_empty (files) && cur->lineno > 1)
22428       break;
22429     else
22430       {
22431 	unsigned char code = cur->code;
22432 	md5_process_bytes (&code, 1, &ctx);
22433 	checksum_uleb128 (cur->lineno, &ctx);
22434 	md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
22435       }
22436   md5_finish_ctx (&ctx, checksum);
22437   count = i - idx;
22438 
22439   /* From the containing include filename (if any) pick up just
22440      usable characters from its basename.  */
22441   if (vec_safe_is_empty (files))
22442     base = "";
22443   else
22444     base = lbasename (files->last ().info);
22445   for (encoded_filename_len = 0, i = 0; base[i]; i++)
22446     if (ISIDNUM (base[i]) || base[i] == '.')
22447       encoded_filename_len++;
22448   /* Count . at the end.  */
22449   if (encoded_filename_len)
22450     encoded_filename_len++;
22451 
22452   sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
22453   linebuf_len = strlen (linebuf);
22454 
22455   /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum>  */
22456   grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
22457 			 + 16 * 2 + 1);
22458   memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
22459   tail = grp_name + 4;
22460   if (encoded_filename_len)
22461     {
22462       for (i = 0; base[i]; i++)
22463 	if (ISIDNUM (base[i]) || base[i] == '.')
22464 	  *tail++ = base[i];
22465       *tail++ = '.';
22466     }
22467   memcpy (tail, linebuf, linebuf_len);
22468   tail += linebuf_len;
22469   *tail++ = '.';
22470   for (i = 0; i < 16; i++)
22471     sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
22472 
22473   /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
22474      in the empty vector entry before the first define/undef.  */
22475   inc = &(*macinfo_table)[idx - 1];
22476   inc->code = DW_MACRO_GNU_transparent_include;
22477   inc->lineno = 0;
22478   inc->info = ggc_strdup (grp_name);
22479   if (!*macinfo_htab)
22480     *macinfo_htab = new macinfo_hash_type (10);
22481   /* Avoid emitting duplicates.  */
22482   slot = (*macinfo_htab)->find_slot (inc, INSERT);
22483   if (*slot != NULL)
22484     {
22485       inc->code = 0;
22486       inc->info = NULL;
22487       /* If such an entry has been used before, just emit
22488 	 a DW_MACRO_GNU_transparent_include op.  */
22489       inc = *slot;
22490       output_macinfo_op (inc);
22491       /* And clear all macinfo_entry in the range to avoid emitting them
22492 	 in the second pass.  */
22493       for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
22494 	{
22495 	  cur->code = 0;
22496 	  cur->info = NULL;
22497 	}
22498     }
22499   else
22500     {
22501       *slot = inc;
22502       inc->lineno = (*macinfo_htab)->elements ();
22503       output_macinfo_op (inc);
22504     }
22505   return count;
22506 }
22507 
22508 /* Save any strings needed by the macinfo table in the debug str
22509    table.  All strings must be collected into the table by the time
22510    index_string is called.  */
22511 
22512 static void
22513 save_macinfo_strings (void)
22514 {
22515   unsigned len;
22516   unsigned i;
22517   macinfo_entry *ref;
22518 
22519   for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
22520     {
22521       switch (ref->code)
22522         {
22523           /* Match the logic in output_macinfo_op to decide on
22524              indirect strings.  */
22525           case DW_MACINFO_define:
22526           case DW_MACINFO_undef:
22527             len = strlen (ref->info) + 1;
22528             if (!dwarf_strict
22529                 && len > DWARF_OFFSET_SIZE
22530                 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
22531                 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
22532               set_indirect_string (find_AT_string (ref->info));
22533             break;
22534           case DW_MACRO_GNU_define_indirect:
22535           case DW_MACRO_GNU_undef_indirect:
22536             set_indirect_string (find_AT_string (ref->info));
22537             break;
22538           default:
22539             break;
22540         }
22541     }
22542 }
22543 
22544 /* Output macinfo section(s).  */
22545 
22546 static void
22547 output_macinfo (void)
22548 {
22549   unsigned i;
22550   unsigned long length = vec_safe_length (macinfo_table);
22551   macinfo_entry *ref;
22552   vec<macinfo_entry, va_gc> *files = NULL;
22553   macinfo_hash_type *macinfo_htab = NULL;
22554 
22555   if (! length)
22556     return;
22557 
22558   /* output_macinfo* uses these interchangeably.  */
22559   gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
22560 	      && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
22561 	      && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
22562 	      && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
22563 
22564   /* For .debug_macro emit the section header.  */
22565   if (!dwarf_strict)
22566     {
22567       dw2_asm_output_data (2, 4, "DWARF macro version number");
22568       if (DWARF_OFFSET_SIZE == 8)
22569 	dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
22570       else
22571 	dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
22572       dw2_asm_output_offset (DWARF_OFFSET_SIZE,
22573                              (!dwarf_split_debug_info ? debug_line_section_label
22574                               : debug_skeleton_line_section_label),
22575                              debug_line_section, NULL);
22576     }
22577 
22578   /* In the first loop, it emits the primary .debug_macinfo section
22579      and after each emitted op the macinfo_entry is cleared.
22580      If a longer range of define/undef ops can be optimized using
22581      DW_MACRO_GNU_transparent_include, the
22582      DW_MACRO_GNU_transparent_include op is emitted and kept in
22583      the vector before the first define/undef in the range and the
22584      whole range of define/undef ops is not emitted and kept.  */
22585   for (i = 0; macinfo_table->iterate (i, &ref); i++)
22586     {
22587       switch (ref->code)
22588 	{
22589 	case DW_MACINFO_start_file:
22590 	  vec_safe_push (files, *ref);
22591 	  break;
22592 	case DW_MACINFO_end_file:
22593 	  if (!vec_safe_is_empty (files))
22594 	    files->pop ();
22595 	  break;
22596 	case DW_MACINFO_define:
22597 	case DW_MACINFO_undef:
22598 	  if (!dwarf_strict
22599 	      && HAVE_COMDAT_GROUP
22600 	      && vec_safe_length (files) != 1
22601 	      && i > 0
22602 	      && i + 1 < length
22603 	      && (*macinfo_table)[i - 1].code == 0)
22604 	    {
22605 	      unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
22606 	      if (count)
22607 		{
22608 		  i += count - 1;
22609 		  continue;
22610 		}
22611 	    }
22612 	  break;
22613 	case 0:
22614 	  /* A dummy entry may be inserted at the beginning to be able
22615 	     to optimize the whole block of predefined macros.  */
22616 	  if (i == 0)
22617 	    continue;
22618 	default:
22619 	  break;
22620 	}
22621       output_macinfo_op (ref);
22622       ref->info = NULL;
22623       ref->code = 0;
22624     }
22625 
22626   if (!macinfo_htab)
22627     return;
22628 
22629   delete macinfo_htab;
22630   macinfo_htab = NULL;
22631 
22632   /* If any DW_MACRO_GNU_transparent_include were used, on those
22633      DW_MACRO_GNU_transparent_include entries terminate the
22634      current chain and switch to a new comdat .debug_macinfo
22635      section and emit the define/undef entries within it.  */
22636   for (i = 0; macinfo_table->iterate (i, &ref); i++)
22637     switch (ref->code)
22638       {
22639       case 0:
22640 	continue;
22641       case DW_MACRO_GNU_transparent_include:
22642 	{
22643 	  char label[MAX_ARTIFICIAL_LABEL_BYTES];
22644 	  tree comdat_key = get_identifier (ref->info);
22645 	  /* Terminate the previous .debug_macinfo section.  */
22646 	  dw2_asm_output_data (1, 0, "End compilation unit");
22647 	  targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
22648 					 SECTION_DEBUG
22649 					 | SECTION_LINKONCE,
22650 					 comdat_key);
22651 	  ASM_GENERATE_INTERNAL_LABEL (label,
22652 				       DEBUG_MACRO_SECTION_LABEL,
22653 				       ref->lineno);
22654 	  ASM_OUTPUT_LABEL (asm_out_file, label);
22655 	  ref->code = 0;
22656 	  ref->info = NULL;
22657 	  dw2_asm_output_data (2, 4, "DWARF macro version number");
22658 	  if (DWARF_OFFSET_SIZE == 8)
22659 	    dw2_asm_output_data (1, 1, "Flags: 64-bit");
22660 	  else
22661 	    dw2_asm_output_data (1, 0, "Flags: 32-bit");
22662 	}
22663 	break;
22664       case DW_MACINFO_define:
22665       case DW_MACINFO_undef:
22666 	output_macinfo_op (ref);
22667 	ref->code = 0;
22668 	ref->info = NULL;
22669 	break;
22670       default:
22671 	gcc_unreachable ();
22672       }
22673 }
22674 
22675 /* Set up for Dwarf output at the start of compilation.  */
22676 
22677 static void
22678 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
22679 {
22680   /* This option is currently broken, see (PR53118 and PR46102).  */
22681   if (flag_eliminate_dwarf2_dups
22682       && strstr (lang_hooks.name, "C++"))
22683     {
22684       warning (0, "-feliminate-dwarf2-dups is broken for C++, ignoring");
22685       flag_eliminate_dwarf2_dups = 0;
22686     }
22687 
22688   /* Allocate the file_table.  */
22689   file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
22690 
22691   /* Allocate the decl_die_table.  */
22692   decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
22693 
22694   /* Allocate the decl_loc_table.  */
22695   decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
22696 
22697   /* Allocate the cached_dw_loc_list_table.  */
22698   cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
22699 
22700   /* Allocate the initial hunk of the decl_scope_table.  */
22701   vec_alloc (decl_scope_table, 256);
22702 
22703   /* Allocate the initial hunk of the abbrev_die_table.  */
22704   abbrev_die_table = ggc_cleared_vec_alloc<dw_die_ref>
22705     (ABBREV_DIE_TABLE_INCREMENT);
22706   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
22707   /* Zero-th entry is allocated, but unused.  */
22708   abbrev_die_table_in_use = 1;
22709 
22710   /* Allocate the pubtypes and pubnames vectors.  */
22711   vec_alloc (pubname_table, 32);
22712   vec_alloc (pubtype_table, 32);
22713 
22714   vec_alloc (incomplete_types, 64);
22715 
22716   vec_alloc (used_rtx_array, 32);
22717 
22718   if (!dwarf_split_debug_info)
22719     {
22720       debug_info_section = get_section (DEBUG_INFO_SECTION,
22721                                         SECTION_DEBUG, NULL);
22722       debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22723                                           SECTION_DEBUG, NULL);
22724       debug_loc_section = get_section (DEBUG_LOC_SECTION,
22725                                        SECTION_DEBUG, NULL);
22726     }
22727   else
22728     {
22729       debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
22730                                         SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22731       debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
22732                                           SECTION_DEBUG | SECTION_EXCLUDE,
22733                                           NULL);
22734       debug_addr_section = get_section (DEBUG_ADDR_SECTION,
22735                                         SECTION_DEBUG, NULL);
22736       debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
22737                                                  SECTION_DEBUG, NULL);
22738       debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22739                                                    SECTION_DEBUG, NULL);
22740       ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
22741                                   DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
22742 
22743       /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
22744          the main .o, but the skeleton_line goes into the split off dwo.  */
22745       debug_skeleton_line_section
22746           = get_section (DEBUG_DWO_LINE_SECTION,
22747                          SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22748       ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
22749                                    DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
22750       debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
22751                                                SECTION_DEBUG | SECTION_EXCLUDE,
22752                                                NULL);
22753       ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
22754                                    DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
22755       debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
22756                                        SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22757       debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
22758                                            DEBUG_STR_DWO_SECTION_FLAGS, NULL);
22759     }
22760   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
22761 				       SECTION_DEBUG, NULL);
22762   debug_macinfo_section = get_section (dwarf_strict
22763 				       ? DEBUG_MACINFO_SECTION
22764 				       : DEBUG_MACRO_SECTION,
22765                                        DEBUG_MACRO_SECTION_FLAGS, NULL);
22766   debug_line_section = get_section (DEBUG_LINE_SECTION,
22767 				    SECTION_DEBUG, NULL);
22768   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
22769 					SECTION_DEBUG, NULL);
22770   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
22771 					SECTION_DEBUG, NULL);
22772   debug_str_section = get_section (DEBUG_STR_SECTION,
22773 				   DEBUG_STR_SECTION_FLAGS, NULL);
22774   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
22775 				      SECTION_DEBUG, NULL);
22776   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
22777 				     SECTION_DEBUG, NULL);
22778 
22779   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
22780   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
22781 			       DEBUG_ABBREV_SECTION_LABEL, 0);
22782   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
22783   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
22784 			       COLD_TEXT_SECTION_LABEL, 0);
22785   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
22786 
22787   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
22788 			       DEBUG_INFO_SECTION_LABEL, 0);
22789   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
22790 			       DEBUG_LINE_SECTION_LABEL, 0);
22791   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
22792 			       DEBUG_RANGES_SECTION_LABEL, 0);
22793   ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
22794                                DEBUG_ADDR_SECTION_LABEL, 0);
22795   ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
22796 			       dwarf_strict
22797 			       ? DEBUG_MACINFO_SECTION_LABEL
22798 			       : DEBUG_MACRO_SECTION_LABEL, 0);
22799   ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
22800 
22801   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22802     vec_alloc (macinfo_table, 64);
22803 
22804   switch_to_section (text_section);
22805   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
22806 
22807   /* Make sure the line number table for .text always exists.  */
22808   text_section_line_info = new_line_info_table ();
22809   text_section_line_info->end_label = text_end_label;
22810 
22811   /* If front-ends already registered a main translation unit but we were not
22812      ready to perform the association, do this now.  */
22813   if (main_translation_unit != NULL_TREE)
22814     equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
22815 }
22816 
22817 /* Called before compile () starts outputtting functions, variables
22818    and toplevel asms into assembly.  */
22819 
22820 static void
22821 dwarf2out_assembly_start (void)
22822 {
22823   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
22824       && dwarf2out_do_cfi_asm ()
22825       && (!(flag_unwind_tables || flag_exceptions)
22826 	  || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
22827     fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
22828 }
22829 
22830 /* A helper function for dwarf2out_finish called through
22831    htab_traverse.  Assign a string its index.  All strings must be
22832    collected into the table by the time index_string is called,
22833    because the indexing code relies on htab_traverse to traverse nodes
22834    in the same order for each run. */
22835 
22836 int
22837 index_string (indirect_string_node **h, unsigned int *index)
22838 {
22839   indirect_string_node *node = *h;
22840 
22841   find_string_form (node);
22842   if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22843     {
22844       gcc_assert (node->index == NO_INDEX_ASSIGNED);
22845       node->index = *index;
22846       *index += 1;
22847     }
22848   return 1;
22849 }
22850 
22851 /* A helper function for output_indirect_strings called through
22852    htab_traverse.  Output the offset to a string and update the
22853    current offset.  */
22854 
22855 int
22856 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
22857 {
22858   indirect_string_node *node = *h;
22859 
22860   if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22861     {
22862       /* Assert that this node has been assigned an index.  */
22863       gcc_assert (node->index != NO_INDEX_ASSIGNED
22864                   && node->index != NOT_INDEXED);
22865       dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
22866                            "indexed string 0x%x: %s", node->index, node->str);
22867       *offset += strlen (node->str) + 1;
22868     }
22869   return 1;
22870 }
22871 
22872 /* A helper function for dwarf2out_finish called through
22873    htab_traverse.  Output the indexed string.  */
22874 
22875 int
22876 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
22877 {
22878   struct indirect_string_node *node = *h;
22879 
22880   if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22881     {
22882       /* Assert that the strings are output in the same order as their
22883          indexes were assigned.  */
22884       gcc_assert (*cur_idx == node->index);
22885       assemble_string (node->str, strlen (node->str) + 1);
22886       *cur_idx += 1;
22887     }
22888   return 1;
22889 }
22890 
22891 /* A helper function for dwarf2out_finish called through
22892    htab_traverse.  Emit one queued .debug_str string.  */
22893 
22894 int
22895 output_indirect_string (indirect_string_node **h, void *)
22896 {
22897   struct indirect_string_node *node = *h;
22898 
22899   node->form = find_string_form (node);
22900   if (node->form == DW_FORM_strp && node->refcount > 0)
22901     {
22902       ASM_OUTPUT_LABEL (asm_out_file, node->label);
22903       assemble_string (node->str, strlen (node->str) + 1);
22904     }
22905 
22906   return 1;
22907 }
22908 
22909 /* Output the indexed string table.  */
22910 
22911 static void
22912 output_indirect_strings (void)
22913 {
22914   switch_to_section (debug_str_section);
22915   if (!dwarf_split_debug_info)
22916     debug_str_hash->traverse<void *, output_indirect_string> (NULL);
22917   else
22918     {
22919       unsigned int offset = 0;
22920       unsigned int cur_idx = 0;
22921 
22922       skeleton_debug_str_hash->traverse<void *, output_indirect_string> (NULL);
22923 
22924       switch_to_section (debug_str_offsets_section);
22925       debug_str_hash->traverse_noresize
22926 	<unsigned int *, output_index_string_offset> (&offset);
22927       switch_to_section (debug_str_dwo_section);
22928       debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
22929 	(&cur_idx);
22930     }
22931 }
22932 
22933 /* Callback for htab_traverse to assign an index to an entry in the
22934    table, and to write that entry to the .debug_addr section.  */
22935 
22936 int
22937 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
22938 {
22939   addr_table_entry *entry = *slot;
22940 
22941   if (entry->refcount == 0)
22942     {
22943       gcc_assert (entry->index == NO_INDEX_ASSIGNED
22944                   || entry->index == NOT_INDEXED);
22945       return 1;
22946     }
22947 
22948   gcc_assert (entry->index == *cur_index);
22949   (*cur_index)++;
22950 
22951   switch (entry->kind)
22952     {
22953       case ate_kind_rtx:
22954         dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
22955                                  "0x%x", entry->index);
22956         break;
22957       case ate_kind_rtx_dtprel:
22958         gcc_assert (targetm.asm_out.output_dwarf_dtprel);
22959         targetm.asm_out.output_dwarf_dtprel (asm_out_file,
22960                                              DWARF2_ADDR_SIZE,
22961                                              entry->addr.rtl);
22962         fputc ('\n', asm_out_file);
22963         break;
22964       case ate_kind_label:
22965         dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
22966                                  "0x%x", entry->index);
22967         break;
22968       default:
22969         gcc_unreachable ();
22970     }
22971   return 1;
22972 }
22973 
22974 /* Produce the .debug_addr section.  */
22975 
22976 static void
22977 output_addr_table (void)
22978 {
22979   unsigned int index = 0;
22980   if (addr_index_table == NULL || addr_index_table->size () == 0)
22981     return;
22982 
22983   switch_to_section (debug_addr_section);
22984   addr_index_table
22985     ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
22986 }
22987 
22988 #if ENABLE_ASSERT_CHECKING
22989 /* Verify that all marks are clear.  */
22990 
22991 static void
22992 verify_marks_clear (dw_die_ref die)
22993 {
22994   dw_die_ref c;
22995 
22996   gcc_assert (! die->die_mark);
22997   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22998 }
22999 #endif /* ENABLE_ASSERT_CHECKING */
23000 
23001 /* Clear the marks for a die and its children.
23002    Be cool if the mark isn't set.  */
23003 
23004 static void
23005 prune_unmark_dies (dw_die_ref die)
23006 {
23007   dw_die_ref c;
23008 
23009   if (die->die_mark)
23010     die->die_mark = 0;
23011   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
23012 }
23013 
23014 /* Given DIE that we're marking as used, find any other dies
23015    it references as attributes and mark them as used.  */
23016 
23017 static void
23018 prune_unused_types_walk_attribs (dw_die_ref die)
23019 {
23020   dw_attr_ref a;
23021   unsigned ix;
23022 
23023   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23024     {
23025       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
23026 	{
23027 	  /* A reference to another DIE.
23028 	     Make sure that it will get emitted.
23029 	     If it was broken out into a comdat group, don't follow it.  */
23030           if (! AT_ref (a)->comdat_type_p
23031               || a->dw_attr == DW_AT_specification)
23032 	    prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
23033 	}
23034       /* Set the string's refcount to 0 so that prune_unused_types_mark
23035 	 accounts properly for it.  */
23036       if (AT_class (a) == dw_val_class_str)
23037 	a->dw_attr_val.v.val_str->refcount = 0;
23038     }
23039 }
23040 
23041 /* Mark the generic parameters and arguments children DIEs of DIE.  */
23042 
23043 static void
23044 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
23045 {
23046   dw_die_ref c;
23047 
23048   if (die == NULL || die->die_child == NULL)
23049     return;
23050   c = die->die_child;
23051   do
23052     {
23053       if (is_template_parameter (c))
23054 	prune_unused_types_mark (c, 1);
23055       c = c->die_sib;
23056     } while (c && c != die->die_child);
23057 }
23058 
23059 /* Mark DIE as being used.  If DOKIDS is true, then walk down
23060    to DIE's children.  */
23061 
23062 static void
23063 prune_unused_types_mark (dw_die_ref die, int dokids)
23064 {
23065   dw_die_ref c;
23066 
23067   if (die->die_mark == 0)
23068     {
23069       /* We haven't done this node yet.  Mark it as used.  */
23070       die->die_mark = 1;
23071       /* If this is the DIE of a generic type instantiation,
23072 	 mark the children DIEs that describe its generic parms and
23073 	 args.  */
23074       prune_unused_types_mark_generic_parms_dies (die);
23075 
23076       /* We also have to mark its parents as used.
23077 	 (But we don't want to mark our parent's kids due to this,
23078 	 unless it is a class.)  */
23079       if (die->die_parent)
23080 	prune_unused_types_mark (die->die_parent,
23081 				 class_scope_p (die->die_parent));
23082 
23083       /* Mark any referenced nodes.  */
23084       prune_unused_types_walk_attribs (die);
23085 
23086       /* If this node is a specification,
23087 	 also mark the definition, if it exists.  */
23088       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
23089 	prune_unused_types_mark (die->die_definition, 1);
23090     }
23091 
23092   if (dokids && die->die_mark != 2)
23093     {
23094       /* We need to walk the children, but haven't done so yet.
23095 	 Remember that we've walked the kids.  */
23096       die->die_mark = 2;
23097 
23098       /* If this is an array type, we need to make sure our
23099 	 kids get marked, even if they're types.  If we're
23100 	 breaking out types into comdat sections, do this
23101 	 for all type definitions.  */
23102       if (die->die_tag == DW_TAG_array_type
23103           || (use_debug_types
23104               && is_type_die (die) && ! is_declaration_die (die)))
23105 	FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
23106       else
23107 	FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
23108     }
23109 }
23110 
23111 /* For local classes, look if any static member functions were emitted
23112    and if so, mark them.  */
23113 
23114 static void
23115 prune_unused_types_walk_local_classes (dw_die_ref die)
23116 {
23117   dw_die_ref c;
23118 
23119   if (die->die_mark == 2)
23120     return;
23121 
23122   switch (die->die_tag)
23123     {
23124     case DW_TAG_structure_type:
23125     case DW_TAG_union_type:
23126     case DW_TAG_class_type:
23127       break;
23128 
23129     case DW_TAG_subprogram:
23130       if (!get_AT_flag (die, DW_AT_declaration)
23131 	  || die->die_definition != NULL)
23132 	prune_unused_types_mark (die, 1);
23133       return;
23134 
23135     default:
23136       return;
23137     }
23138 
23139   /* Mark children.  */
23140   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
23141 }
23142 
23143 /* Walk the tree DIE and mark types that we actually use.  */
23144 
23145 static void
23146 prune_unused_types_walk (dw_die_ref die)
23147 {
23148   dw_die_ref c;
23149 
23150   /* Don't do anything if this node is already marked and
23151      children have been marked as well.  */
23152   if (die->die_mark == 2)
23153     return;
23154 
23155   switch (die->die_tag)
23156     {
23157     case DW_TAG_structure_type:
23158     case DW_TAG_union_type:
23159     case DW_TAG_class_type:
23160       if (die->die_perennial_p)
23161 	break;
23162 
23163       for (c = die->die_parent; c; c = c->die_parent)
23164 	if (c->die_tag == DW_TAG_subprogram)
23165 	  break;
23166 
23167       /* Finding used static member functions inside of classes
23168 	 is needed just for local classes, because for other classes
23169 	 static member function DIEs with DW_AT_specification
23170 	 are emitted outside of the DW_TAG_*_type.  If we ever change
23171 	 it, we'd need to call this even for non-local classes.  */
23172       if (c)
23173 	prune_unused_types_walk_local_classes (die);
23174 
23175       /* It's a type node --- don't mark it.  */
23176       return;
23177 
23178     case DW_TAG_const_type:
23179     case DW_TAG_packed_type:
23180     case DW_TAG_pointer_type:
23181     case DW_TAG_reference_type:
23182     case DW_TAG_rvalue_reference_type:
23183     case DW_TAG_volatile_type:
23184     case DW_TAG_typedef:
23185     case DW_TAG_array_type:
23186     case DW_TAG_interface_type:
23187     case DW_TAG_friend:
23188     case DW_TAG_variant_part:
23189     case DW_TAG_enumeration_type:
23190     case DW_TAG_subroutine_type:
23191     case DW_TAG_string_type:
23192     case DW_TAG_set_type:
23193     case DW_TAG_subrange_type:
23194     case DW_TAG_ptr_to_member_type:
23195     case DW_TAG_file_type:
23196       if (die->die_perennial_p)
23197 	break;
23198 
23199       /* It's a type node --- don't mark it.  */
23200       return;
23201 
23202     default:
23203       /* Mark everything else.  */
23204       break;
23205   }
23206 
23207   if (die->die_mark == 0)
23208     {
23209       die->die_mark = 1;
23210 
23211       /* Now, mark any dies referenced from here.  */
23212       prune_unused_types_walk_attribs (die);
23213     }
23214 
23215   die->die_mark = 2;
23216 
23217   /* Mark children.  */
23218   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
23219 }
23220 
23221 /* Increment the string counts on strings referred to from DIE's
23222    attributes.  */
23223 
23224 static void
23225 prune_unused_types_update_strings (dw_die_ref die)
23226 {
23227   dw_attr_ref a;
23228   unsigned ix;
23229 
23230   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23231     if (AT_class (a) == dw_val_class_str)
23232       {
23233 	struct indirect_string_node *s = a->dw_attr_val.v.val_str;
23234 	s->refcount++;
23235 	/* Avoid unnecessarily putting strings that are used less than
23236 	   twice in the hash table.  */
23237 	if (s->refcount
23238 	    == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
23239 	  {
23240 	    indirect_string_node **slot
23241 	      = debug_str_hash->find_slot_with_hash (s->str,
23242 						     htab_hash_string (s->str),
23243 						     INSERT);
23244 	    gcc_assert (*slot == NULL);
23245 	    *slot = s;
23246 	  }
23247       }
23248 }
23249 
23250 /* Remove from the tree DIE any dies that aren't marked.  */
23251 
23252 static void
23253 prune_unused_types_prune (dw_die_ref die)
23254 {
23255   dw_die_ref c;
23256 
23257   gcc_assert (die->die_mark);
23258   prune_unused_types_update_strings (die);
23259 
23260   if (! die->die_child)
23261     return;
23262 
23263   c = die->die_child;
23264   do {
23265     dw_die_ref prev = c;
23266     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
23267       if (c == die->die_child)
23268 	{
23269 	  /* No marked children between 'prev' and the end of the list.  */
23270 	  if (prev == c)
23271 	    /* No marked children at all.  */
23272 	    die->die_child = NULL;
23273 	  else
23274 	    {
23275 	      prev->die_sib = c->die_sib;
23276 	      die->die_child = prev;
23277 	    }
23278 	  return;
23279 	}
23280 
23281     if (c != prev->die_sib)
23282       prev->die_sib = c;
23283     prune_unused_types_prune (c);
23284   } while (c != die->die_child);
23285 }
23286 
23287 /* Remove dies representing declarations that we never use.  */
23288 
23289 static void
23290 prune_unused_types (void)
23291 {
23292   unsigned int i;
23293   limbo_die_node *node;
23294   comdat_type_node *ctnode;
23295   pubname_ref pub;
23296   dw_die_ref base_type;
23297 
23298 #if ENABLE_ASSERT_CHECKING
23299   /* All the marks should already be clear.  */
23300   verify_marks_clear (comp_unit_die ());
23301   for (node = limbo_die_list; node; node = node->next)
23302     verify_marks_clear (node->die);
23303   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23304     verify_marks_clear (ctnode->root_die);
23305 #endif /* ENABLE_ASSERT_CHECKING */
23306 
23307   /* Mark types that are used in global variables.  */
23308   premark_types_used_by_global_vars ();
23309 
23310   /* Set the mark on nodes that are actually used.  */
23311   prune_unused_types_walk (comp_unit_die ());
23312   for (node = limbo_die_list; node; node = node->next)
23313     prune_unused_types_walk (node->die);
23314   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23315     {
23316       prune_unused_types_walk (ctnode->root_die);
23317       prune_unused_types_mark (ctnode->type_die, 1);
23318     }
23319 
23320   /* Also set the mark on nodes referenced from the pubname_table.  Enumerators
23321      are unusual in that they are pubnames that are the children of pubtypes.
23322      They should only be marked via their parent DW_TAG_enumeration_type die,
23323      not as roots in themselves.  */
23324   FOR_EACH_VEC_ELT (*pubname_table, i, pub)
23325     if (pub->die->die_tag != DW_TAG_enumerator)
23326       prune_unused_types_mark (pub->die, 1);
23327   for (i = 0; base_types.iterate (i, &base_type); i++)
23328     prune_unused_types_mark (base_type, 1);
23329 
23330   if (debug_str_hash)
23331     debug_str_hash->empty ();
23332   if (skeleton_debug_str_hash)
23333     skeleton_debug_str_hash->empty ();
23334   prune_unused_types_prune (comp_unit_die ());
23335   for (node = limbo_die_list; node; node = node->next)
23336     prune_unused_types_prune (node->die);
23337   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23338     prune_unused_types_prune (ctnode->root_die);
23339 
23340   /* Leave the marks clear.  */
23341   prune_unmark_dies (comp_unit_die ());
23342   for (node = limbo_die_list; node; node = node->next)
23343     prune_unmark_dies (node->die);
23344   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23345     prune_unmark_dies (ctnode->root_die);
23346 }
23347 
23348 /* Set the parameter to true if there are any relative pathnames in
23349    the file table.  */
23350 int
23351 file_table_relative_p (dwarf_file_data **slot, bool *p)
23352 {
23353   struct dwarf_file_data *d = *slot;
23354   if (!IS_ABSOLUTE_PATH (d->filename))
23355     {
23356       *p = true;
23357       return 0;
23358     }
23359   return 1;
23360 }
23361 
23362 /* Helpers to manipulate hash table of comdat type units.  */
23363 
23364 struct comdat_type_hasher : typed_noop_remove <comdat_type_node>
23365 {
23366   typedef comdat_type_node value_type;
23367   typedef comdat_type_node compare_type;
23368   static inline hashval_t hash (const value_type *);
23369   static inline bool equal (const value_type *, const compare_type *);
23370 };
23371 
23372 inline hashval_t
23373 comdat_type_hasher::hash (const value_type *type_node)
23374 {
23375   hashval_t h;
23376   memcpy (&h, type_node->signature, sizeof (h));
23377   return h;
23378 }
23379 
23380 inline bool
23381 comdat_type_hasher::equal (const value_type *type_node_1,
23382 			   const compare_type *type_node_2)
23383 {
23384   return (! memcmp (type_node_1->signature, type_node_2->signature,
23385                     DWARF_TYPE_SIGNATURE_SIZE));
23386 }
23387 
23388 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
23389    to the location it would have been added, should we know its
23390    DECL_ASSEMBLER_NAME when we added other attributes.  This will
23391    probably improve compactness of debug info, removing equivalent
23392    abbrevs, and hide any differences caused by deferring the
23393    computation of the assembler name, triggered by e.g. PCH.  */
23394 
23395 static inline void
23396 move_linkage_attr (dw_die_ref die)
23397 {
23398   unsigned ix = vec_safe_length (die->die_attr);
23399   dw_attr_node linkage = (*die->die_attr)[ix - 1];
23400 
23401   gcc_assert (linkage.dw_attr == DW_AT_linkage_name
23402 	      || linkage.dw_attr == DW_AT_MIPS_linkage_name);
23403 
23404   while (--ix > 0)
23405     {
23406       dw_attr_node *prev = &(*die->die_attr)[ix - 1];
23407 
23408       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
23409 	break;
23410     }
23411 
23412   if (ix != vec_safe_length (die->die_attr) - 1)
23413     {
23414       die->die_attr->pop ();
23415       die->die_attr->quick_insert (ix, linkage);
23416     }
23417 }
23418 
23419 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
23420    referenced from typed stack ops and count how often they are used.  */
23421 
23422 static void
23423 mark_base_types (dw_loc_descr_ref loc)
23424 {
23425   dw_die_ref base_type = NULL;
23426 
23427   for (; loc; loc = loc->dw_loc_next)
23428     {
23429       switch (loc->dw_loc_opc)
23430 	{
23431 	case DW_OP_GNU_regval_type:
23432 	case DW_OP_GNU_deref_type:
23433 	  base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
23434 	  break;
23435 	case DW_OP_GNU_convert:
23436 	case DW_OP_GNU_reinterpret:
23437 	  if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
23438 	    continue;
23439 	  /* FALLTHRU */
23440 	case DW_OP_GNU_const_type:
23441 	  base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
23442 	  break;
23443 	case DW_OP_GNU_entry_value:
23444 	  mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
23445 	  continue;
23446 	default:
23447 	  continue;
23448 	}
23449       gcc_assert (base_type->die_parent == comp_unit_die ());
23450       if (base_type->die_mark)
23451 	base_type->die_mark++;
23452       else
23453 	{
23454 	  base_types.safe_push (base_type);
23455 	  base_type->die_mark = 1;
23456 	}
23457     }
23458 }
23459 
23460 /* Comparison function for sorting marked base types.  */
23461 
23462 static int
23463 base_type_cmp (const void *x, const void *y)
23464 {
23465   dw_die_ref dx = *(const dw_die_ref *) x;
23466   dw_die_ref dy = *(const dw_die_ref *) y;
23467   unsigned int byte_size1, byte_size2;
23468   unsigned int encoding1, encoding2;
23469   if (dx->die_mark > dy->die_mark)
23470     return -1;
23471   if (dx->die_mark < dy->die_mark)
23472     return 1;
23473   byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
23474   byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
23475   if (byte_size1 < byte_size2)
23476     return 1;
23477   if (byte_size1 > byte_size2)
23478     return -1;
23479   encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
23480   encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
23481   if (encoding1 < encoding2)
23482     return 1;
23483   if (encoding1 > encoding2)
23484     return -1;
23485   return 0;
23486 }
23487 
23488 /* Move base types marked by mark_base_types as early as possible
23489    in the CU, sorted by decreasing usage count both to make the
23490    uleb128 references as small as possible and to make sure they
23491    will have die_offset already computed by calc_die_sizes when
23492    sizes of typed stack loc ops is computed.  */
23493 
23494 static void
23495 move_marked_base_types (void)
23496 {
23497   unsigned int i;
23498   dw_die_ref base_type, die, c;
23499 
23500   if (base_types.is_empty ())
23501     return;
23502 
23503   /* Sort by decreasing usage count, they will be added again in that
23504      order later on.  */
23505   base_types.qsort (base_type_cmp);
23506   die = comp_unit_die ();
23507   c = die->die_child;
23508   do
23509     {
23510       dw_die_ref prev = c;
23511       c = c->die_sib;
23512       while (c->die_mark)
23513 	{
23514 	  remove_child_with_prev (c, prev);
23515 	  /* As base types got marked, there must be at least
23516 	     one node other than DW_TAG_base_type.  */
23517 	  gcc_assert (c != c->die_sib);
23518 	  c = c->die_sib;
23519 	}
23520     }
23521   while (c != die->die_child);
23522   gcc_assert (die->die_child);
23523   c = die->die_child;
23524   for (i = 0; base_types.iterate (i, &base_type); i++)
23525     {
23526       base_type->die_mark = 0;
23527       base_type->die_sib = c->die_sib;
23528       c->die_sib = base_type;
23529       c = base_type;
23530     }
23531 }
23532 
23533 /* Helper function for resolve_addr, attempt to resolve
23534    one CONST_STRING, return true if successful.  Similarly verify that
23535    SYMBOL_REFs refer to variables emitted in the current CU.  */
23536 
23537 static bool
23538 resolve_one_addr (rtx *addr)
23539 {
23540   rtx rtl = *addr;
23541 
23542   if (GET_CODE (rtl) == CONST_STRING)
23543     {
23544       size_t len = strlen (XSTR (rtl, 0)) + 1;
23545       tree t = build_string (len, XSTR (rtl, 0));
23546       tree tlen = size_int (len - 1);
23547       TREE_TYPE (t)
23548 	= build_array_type (char_type_node, build_index_type (tlen));
23549       rtl = lookup_constant_def (t);
23550       if (!rtl || !MEM_P (rtl))
23551 	return false;
23552       rtl = XEXP (rtl, 0);
23553       if (GET_CODE (rtl) == SYMBOL_REF
23554 	  && SYMBOL_REF_DECL (rtl)
23555 	  && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23556 	return false;
23557       vec_safe_push (used_rtx_array, rtl);
23558       *addr = rtl;
23559       return true;
23560     }
23561 
23562   if (GET_CODE (rtl) == SYMBOL_REF
23563       && SYMBOL_REF_DECL (rtl))
23564     {
23565       if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
23566 	{
23567 	  if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
23568 	    return false;
23569 	}
23570       else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23571 	return false;
23572     }
23573 
23574   if (GET_CODE (rtl) == CONST)
23575     {
23576       subrtx_ptr_iterator::array_type array;
23577       FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
23578 	if (!resolve_one_addr (*iter))
23579 	  return false;
23580     }
23581 
23582   return true;
23583 }
23584 
23585 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
23586    if possible, and create DW_TAG_dwarf_procedure that can be referenced
23587    from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet.  */
23588 
23589 static rtx
23590 string_cst_pool_decl (tree t)
23591 {
23592   rtx rtl = output_constant_def (t, 1);
23593   unsigned char *array;
23594   dw_loc_descr_ref l;
23595   tree decl;
23596   size_t len;
23597   dw_die_ref ref;
23598 
23599   if (!rtl || !MEM_P (rtl))
23600     return NULL_RTX;
23601   rtl = XEXP (rtl, 0);
23602   if (GET_CODE (rtl) != SYMBOL_REF
23603       || SYMBOL_REF_DECL (rtl) == NULL_TREE)
23604     return NULL_RTX;
23605 
23606   decl = SYMBOL_REF_DECL (rtl);
23607   if (!lookup_decl_die (decl))
23608     {
23609       len = TREE_STRING_LENGTH (t);
23610       vec_safe_push (used_rtx_array, rtl);
23611       ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
23612       array = ggc_vec_alloc<unsigned char> (len);
23613       memcpy (array, TREE_STRING_POINTER (t), len);
23614       l = new_loc_descr (DW_OP_implicit_value, len, 0);
23615       l->dw_loc_oprnd2.val_class = dw_val_class_vec;
23616       l->dw_loc_oprnd2.v.val_vec.length = len;
23617       l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
23618       l->dw_loc_oprnd2.v.val_vec.array = array;
23619       add_AT_loc (ref, DW_AT_location, l);
23620       equate_decl_number_to_die (decl, ref);
23621     }
23622   return rtl;
23623 }
23624 
23625 /* Helper function of resolve_addr_in_expr.  LOC is
23626    a DW_OP_addr followed by DW_OP_stack_value, either at the start
23627    of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
23628    resolved.  Replace it (both DW_OP_addr and DW_OP_stack_value)
23629    with DW_OP_GNU_implicit_pointer if possible
23630    and return true, if unsuccessful, return false.  */
23631 
23632 static bool
23633 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
23634 {
23635   rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
23636   HOST_WIDE_INT offset = 0;
23637   dw_die_ref ref = NULL;
23638   tree decl;
23639 
23640   if (GET_CODE (rtl) == CONST
23641       && GET_CODE (XEXP (rtl, 0)) == PLUS
23642       && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
23643     {
23644       offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
23645       rtl = XEXP (XEXP (rtl, 0), 0);
23646     }
23647   if (GET_CODE (rtl) == CONST_STRING)
23648     {
23649       size_t len = strlen (XSTR (rtl, 0)) + 1;
23650       tree t = build_string (len, XSTR (rtl, 0));
23651       tree tlen = size_int (len - 1);
23652 
23653       TREE_TYPE (t)
23654 	= build_array_type (char_type_node, build_index_type (tlen));
23655       rtl = string_cst_pool_decl (t);
23656       if (!rtl)
23657 	return false;
23658     }
23659   if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
23660     {
23661       decl = SYMBOL_REF_DECL (rtl);
23662       if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
23663 	{
23664 	  ref = lookup_decl_die (decl);
23665 	  if (ref && (get_AT (ref, DW_AT_location)
23666 		      || get_AT (ref, DW_AT_const_value)))
23667 	    {
23668 	      loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
23669 	      loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23670 	      loc->dw_loc_oprnd1.val_entry = NULL;
23671 	      loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23672 	      loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23673 	      loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23674 	      loc->dw_loc_oprnd2.v.val_int = offset;
23675 	      return true;
23676 	    }
23677 	}
23678     }
23679   return false;
23680 }
23681 
23682 /* Helper function for resolve_addr, handle one location
23683    expression, return false if at least one CONST_STRING or SYMBOL_REF in
23684    the location list couldn't be resolved.  */
23685 
23686 static bool
23687 resolve_addr_in_expr (dw_loc_descr_ref loc)
23688 {
23689   dw_loc_descr_ref keep = NULL;
23690   for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
23691     switch (loc->dw_loc_opc)
23692       {
23693       case DW_OP_addr:
23694 	if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
23695 	  {
23696 	    if ((prev == NULL
23697 		 || prev->dw_loc_opc == DW_OP_piece
23698 		 || prev->dw_loc_opc == DW_OP_bit_piece)
23699 		&& loc->dw_loc_next
23700 		&& loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
23701 		&& !dwarf_strict
23702 		&& optimize_one_addr_into_implicit_ptr (loc))
23703 	      break;
23704 	    return false;
23705 	  }
23706 	break;
23707       case DW_OP_GNU_addr_index:
23708       case DW_OP_GNU_const_index:
23709 	if (loc->dw_loc_opc == DW_OP_GNU_addr_index
23710             || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
23711           {
23712             rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
23713             if (!resolve_one_addr (&rtl))
23714               return false;
23715             remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
23716             loc->dw_loc_oprnd1.val_entry =
23717                 add_addr_table_entry (rtl, ate_kind_rtx);
23718           }
23719 	break;
23720       case DW_OP_const4u:
23721       case DW_OP_const8u:
23722 	if (loc->dtprel
23723 	    && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
23724 	  return false;
23725 	break;
23726       case DW_OP_plus_uconst:
23727 	if (size_of_loc_descr (loc)
23728 	    > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
23729 	      + 1
23730 	    && loc->dw_loc_oprnd1.v.val_unsigned > 0)
23731 	  {
23732 	    dw_loc_descr_ref repl
23733 	      = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
23734 	    add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
23735 	    add_loc_descr (&repl, loc->dw_loc_next);
23736 	    *loc = *repl;
23737 	  }
23738 	break;
23739       case DW_OP_implicit_value:
23740 	if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
23741 	    && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
23742 	  return false;
23743 	break;
23744       case DW_OP_GNU_implicit_pointer:
23745       case DW_OP_GNU_parameter_ref:
23746 	if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
23747 	  {
23748 	    dw_die_ref ref
23749 	      = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
23750 	    if (ref == NULL)
23751 	      return false;
23752 	    loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23753 	    loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23754 	    loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23755 	  }
23756 	break;
23757       case DW_OP_GNU_const_type:
23758       case DW_OP_GNU_regval_type:
23759       case DW_OP_GNU_deref_type:
23760       case DW_OP_GNU_convert:
23761       case DW_OP_GNU_reinterpret:
23762 	while (loc->dw_loc_next
23763 	       && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
23764 	  {
23765 	    dw_die_ref base1, base2;
23766 	    unsigned enc1, enc2, size1, size2;
23767 	    if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23768 		|| loc->dw_loc_opc == DW_OP_GNU_deref_type)
23769 	      base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
23770 	    else if (loc->dw_loc_oprnd1.val_class
23771 		     == dw_val_class_unsigned_const)
23772 	      break;
23773 	    else
23774 	      base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
23775 	    if (loc->dw_loc_next->dw_loc_oprnd1.val_class
23776 		== dw_val_class_unsigned_const)
23777 	      break;
23778 	    base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
23779 	    gcc_assert (base1->die_tag == DW_TAG_base_type
23780 			&& base2->die_tag == DW_TAG_base_type);
23781 	    enc1 = get_AT_unsigned (base1, DW_AT_encoding);
23782 	    enc2 = get_AT_unsigned (base2, DW_AT_encoding);
23783 	    size1 = get_AT_unsigned (base1, DW_AT_byte_size);
23784 	    size2 = get_AT_unsigned (base2, DW_AT_byte_size);
23785 	    if (size1 == size2
23786 		&& (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
23787 		     && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
23788 		     && loc != keep)
23789 		    || enc1 == enc2))
23790 	      {
23791 		/* Optimize away next DW_OP_GNU_convert after
23792 		   adjusting LOC's base type die reference.  */
23793 		if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23794 		    || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23795 		  loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
23796 		else
23797 		  loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
23798 		loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23799 		continue;
23800 	      }
23801 	    /* Don't change integer DW_OP_GNU_convert after e.g. floating
23802 	       point typed stack entry.  */
23803 	    else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
23804 	      keep = loc->dw_loc_next;
23805 	    break;
23806 	  }
23807 	break;
23808       default:
23809 	break;
23810       }
23811   return true;
23812 }
23813 
23814 /* Helper function of resolve_addr.  DIE had DW_AT_location of
23815    DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
23816    and DW_OP_addr couldn't be resolved.  resolve_addr has already
23817    removed the DW_AT_location attribute.  This function attempts to
23818    add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
23819    to it or DW_AT_const_value attribute, if possible.  */
23820 
23821 static void
23822 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
23823 {
23824   if (TREE_CODE (decl) != VAR_DECL
23825       || lookup_decl_die (decl) != die
23826       || DECL_EXTERNAL (decl)
23827       || !TREE_STATIC (decl)
23828       || DECL_INITIAL (decl) == NULL_TREE
23829       || DECL_P (DECL_INITIAL (decl))
23830       || get_AT (die, DW_AT_const_value))
23831     return;
23832 
23833   tree init = DECL_INITIAL (decl);
23834   HOST_WIDE_INT offset = 0;
23835   /* For variables that have been optimized away and thus
23836      don't have a memory location, see if we can emit
23837      DW_AT_const_value instead.  */
23838   if (tree_add_const_value_attribute (die, init))
23839     return;
23840   if (dwarf_strict)
23841     return;
23842   /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
23843      and ADDR_EXPR refers to a decl that has DW_AT_location or
23844      DW_AT_const_value (but isn't addressable, otherwise
23845      resolving the original DW_OP_addr wouldn't fail), see if
23846      we can add DW_OP_GNU_implicit_pointer.  */
23847   STRIP_NOPS (init);
23848   if (TREE_CODE (init) == POINTER_PLUS_EXPR
23849       && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
23850     {
23851       offset = tree_to_shwi (TREE_OPERAND (init, 1));
23852       init = TREE_OPERAND (init, 0);
23853       STRIP_NOPS (init);
23854     }
23855   if (TREE_CODE (init) != ADDR_EXPR)
23856     return;
23857   if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
23858        && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
23859       || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
23860 	  && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
23861 	  && TREE_OPERAND (init, 0) != decl))
23862     {
23863       dw_die_ref ref;
23864       dw_loc_descr_ref l;
23865 
23866       if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
23867 	{
23868 	  rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
23869 	  if (!rtl)
23870 	    return;
23871 	  decl = SYMBOL_REF_DECL (rtl);
23872 	}
23873       else
23874 	decl = TREE_OPERAND (init, 0);
23875       ref = lookup_decl_die (decl);
23876       if (ref == NULL
23877 	  || (!get_AT (ref, DW_AT_location)
23878 	      && !get_AT (ref, DW_AT_const_value)))
23879 	return;
23880       l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
23881       l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23882       l->dw_loc_oprnd1.v.val_die_ref.die = ref;
23883       l->dw_loc_oprnd1.v.val_die_ref.external = 0;
23884       add_AT_loc (die, DW_AT_location, l);
23885     }
23886 }
23887 
23888 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
23889    an address in .rodata section if the string literal is emitted there,
23890    or remove the containing location list or replace DW_AT_const_value
23891    with DW_AT_location and empty location expression, if it isn't found
23892    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
23893    to something that has been emitted in the current CU.  */
23894 
23895 static void
23896 resolve_addr (dw_die_ref die)
23897 {
23898   dw_die_ref c;
23899   dw_attr_ref a;
23900   dw_loc_list_ref *curr, *start, loc;
23901   unsigned ix;
23902 
23903   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23904     switch (AT_class (a))
23905       {
23906       case dw_val_class_loc_list:
23907 	start = curr = AT_loc_list_ptr (a);
23908 	loc = *curr;
23909 	gcc_assert (loc);
23910 	/* The same list can be referenced more than once.  See if we have
23911 	   already recorded the result from a previous pass.  */
23912 	if (loc->replaced)
23913 	  *curr = loc->dw_loc_next;
23914 	else if (!loc->resolved_addr)
23915 	  {
23916 	    /* As things stand, we do not expect or allow one die to
23917 	       reference a suffix of another die's location list chain.
23918 	       References must be identical or completely separate.
23919 	       There is therefore no need to cache the result of this
23920 	       pass on any list other than the first; doing so
23921 	       would lead to unnecessary writes.  */
23922 	    while (*curr)
23923 	      {
23924 		gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
23925 		if (!resolve_addr_in_expr ((*curr)->expr))
23926 		  {
23927 		    dw_loc_list_ref next = (*curr)->dw_loc_next;
23928                     dw_loc_descr_ref l = (*curr)->expr;
23929 
23930 		    if (next && (*curr)->ll_symbol)
23931 		      {
23932 			gcc_assert (!next->ll_symbol);
23933 			next->ll_symbol = (*curr)->ll_symbol;
23934 		      }
23935                     if (dwarf_split_debug_info)
23936                       remove_loc_list_addr_table_entries (l);
23937 		    *curr = next;
23938 		  }
23939 		else
23940 		  {
23941 		    mark_base_types ((*curr)->expr);
23942 		    curr = &(*curr)->dw_loc_next;
23943 		  }
23944 	      }
23945 	    if (loc == *start)
23946 	      loc->resolved_addr = 1;
23947 	    else
23948 	      {
23949 		loc->replaced = 1;
23950 		loc->dw_loc_next = *start;
23951 	      }
23952 	  }
23953 	if (!*start)
23954 	  {
23955 	    remove_AT (die, a->dw_attr);
23956 	    ix--;
23957 	  }
23958 	break;
23959       case dw_val_class_loc:
23960 	{
23961 	  dw_loc_descr_ref l = AT_loc (a);
23962 	  /* For -gdwarf-2 don't attempt to optimize
23963 	     DW_AT_data_member_location containing
23964 	     DW_OP_plus_uconst - older consumers might
23965 	     rely on it being that op instead of a more complex,
23966 	     but shorter, location description.  */
23967 	  if ((dwarf_version > 2
23968 	       || a->dw_attr != DW_AT_data_member_location
23969 	       || l == NULL
23970 	       || l->dw_loc_opc != DW_OP_plus_uconst
23971 	       || l->dw_loc_next != NULL)
23972 	      && !resolve_addr_in_expr (l))
23973 	    {
23974 	      if (dwarf_split_debug_info)
23975 		remove_loc_list_addr_table_entries (l);
23976 	      if (l != NULL
23977 		  && l->dw_loc_next == NULL
23978 		  && l->dw_loc_opc == DW_OP_addr
23979 		  && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
23980 		  && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
23981 		  && a->dw_attr == DW_AT_location)
23982 		{
23983 		  tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
23984 		  remove_AT (die, a->dw_attr);
23985 		  ix--;
23986 		  optimize_location_into_implicit_ptr (die, decl);
23987 		  break;
23988 		}
23989 	      remove_AT (die, a->dw_attr);
23990 	      ix--;
23991 	    }
23992 	  else
23993 	    mark_base_types (l);
23994 	}
23995 	break;
23996       case dw_val_class_addr:
23997 	if (a->dw_attr == DW_AT_const_value
23998 	    && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
23999 	  {
24000             if (AT_index (a) != NOT_INDEXED)
24001               remove_addr_table_entry (a->dw_attr_val.val_entry);
24002 	    remove_AT (die, a->dw_attr);
24003 	    ix--;
24004 	  }
24005 	if (die->die_tag == DW_TAG_GNU_call_site
24006 	    && a->dw_attr == DW_AT_abstract_origin)
24007 	  {
24008 	    tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
24009 	    dw_die_ref tdie = lookup_decl_die (tdecl);
24010 	    dw_die_ref cdie;
24011 	    if (tdie == NULL
24012 		&& DECL_EXTERNAL (tdecl)
24013 		&& DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
24014 		&& (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
24015 	      {
24016 		/* Creating a full DIE for tdecl is overly expensive and
24017 		   at this point even wrong when in the LTO phase
24018 		   as it can end up generating new type DIEs we didn't
24019 		   output and thus optimize_external_refs will crash.  */
24020 		tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
24021 		add_AT_flag (tdie, DW_AT_external, 1);
24022 		add_AT_flag (tdie, DW_AT_declaration, 1);
24023 		add_linkage_attr (tdie, tdecl);
24024 		add_name_and_src_coords_attributes (tdie, tdecl);
24025 		equate_decl_number_to_die (tdecl, tdie);
24026 	      }
24027 	    if (tdie)
24028 	      {
24029 		a->dw_attr_val.val_class = dw_val_class_die_ref;
24030 		a->dw_attr_val.v.val_die_ref.die = tdie;
24031 		a->dw_attr_val.v.val_die_ref.external = 0;
24032 	      }
24033 	    else
24034 	      {
24035                 if (AT_index (a) != NOT_INDEXED)
24036                   remove_addr_table_entry (a->dw_attr_val.val_entry);
24037 		remove_AT (die, a->dw_attr);
24038 		ix--;
24039 	      }
24040 	  }
24041 	break;
24042       default:
24043 	break;
24044       }
24045 
24046   FOR_EACH_CHILD (die, c, resolve_addr (c));
24047 }
24048 
24049 /* Helper routines for optimize_location_lists.
24050    This pass tries to share identical local lists in .debug_loc
24051    section.  */
24052 
24053 /* Iteratively hash operands of LOC opcode into HSTATE.  */
24054 
24055 static void
24056 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
24057 {
24058   dw_val_ref val1 = &loc->dw_loc_oprnd1;
24059   dw_val_ref val2 = &loc->dw_loc_oprnd2;
24060 
24061   switch (loc->dw_loc_opc)
24062     {
24063     case DW_OP_const4u:
24064     case DW_OP_const8u:
24065       if (loc->dtprel)
24066 	goto hash_addr;
24067       /* FALLTHRU */
24068     case DW_OP_const1u:
24069     case DW_OP_const1s:
24070     case DW_OP_const2u:
24071     case DW_OP_const2s:
24072     case DW_OP_const4s:
24073     case DW_OP_const8s:
24074     case DW_OP_constu:
24075     case DW_OP_consts:
24076     case DW_OP_pick:
24077     case DW_OP_plus_uconst:
24078     case DW_OP_breg0:
24079     case DW_OP_breg1:
24080     case DW_OP_breg2:
24081     case DW_OP_breg3:
24082     case DW_OP_breg4:
24083     case DW_OP_breg5:
24084     case DW_OP_breg6:
24085     case DW_OP_breg7:
24086     case DW_OP_breg8:
24087     case DW_OP_breg9:
24088     case DW_OP_breg10:
24089     case DW_OP_breg11:
24090     case DW_OP_breg12:
24091     case DW_OP_breg13:
24092     case DW_OP_breg14:
24093     case DW_OP_breg15:
24094     case DW_OP_breg16:
24095     case DW_OP_breg17:
24096     case DW_OP_breg18:
24097     case DW_OP_breg19:
24098     case DW_OP_breg20:
24099     case DW_OP_breg21:
24100     case DW_OP_breg22:
24101     case DW_OP_breg23:
24102     case DW_OP_breg24:
24103     case DW_OP_breg25:
24104     case DW_OP_breg26:
24105     case DW_OP_breg27:
24106     case DW_OP_breg28:
24107     case DW_OP_breg29:
24108     case DW_OP_breg30:
24109     case DW_OP_breg31:
24110     case DW_OP_regx:
24111     case DW_OP_fbreg:
24112     case DW_OP_piece:
24113     case DW_OP_deref_size:
24114     case DW_OP_xderef_size:
24115       hstate.add_object (val1->v.val_int);
24116       break;
24117     case DW_OP_skip:
24118     case DW_OP_bra:
24119       {
24120 	int offset;
24121 
24122 	gcc_assert (val1->val_class == dw_val_class_loc);
24123 	offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
24124 	hstate.add_object (offset);
24125       }
24126       break;
24127     case DW_OP_implicit_value:
24128       hstate.add_object (val1->v.val_unsigned);
24129       switch (val2->val_class)
24130 	{
24131 	case dw_val_class_const:
24132 	  hstate.add_object (val2->v.val_int);
24133 	  break;
24134 	case dw_val_class_vec:
24135 	  {
24136 	    unsigned int elt_size = val2->v.val_vec.elt_size;
24137 	    unsigned int len = val2->v.val_vec.length;
24138 
24139 	    hstate.add_int (elt_size);
24140 	    hstate.add_int (len);
24141 	    hstate.add (val2->v.val_vec.array, len * elt_size);
24142 	  }
24143 	  break;
24144 	case dw_val_class_const_double:
24145 	  hstate.add_object (val2->v.val_double.low);
24146 	  hstate.add_object (val2->v.val_double.high);
24147 	  break;
24148 	case dw_val_class_wide_int:
24149 	  hstate.add (val2->v.val_wide->get_val (),
24150 		      get_full_len (*val2->v.val_wide)
24151 		      * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
24152 	  break;
24153 	case dw_val_class_addr:
24154 	  inchash::add_rtx (val2->v.val_addr, hstate);
24155 	  break;
24156 	default:
24157 	  gcc_unreachable ();
24158 	}
24159       break;
24160     case DW_OP_bregx:
24161     case DW_OP_bit_piece:
24162       hstate.add_object (val1->v.val_int);
24163       hstate.add_object (val2->v.val_int);
24164       break;
24165     case DW_OP_addr:
24166     hash_addr:
24167       if (loc->dtprel)
24168 	{
24169 	  unsigned char dtprel = 0xd1;
24170 	  hstate.add_object (dtprel);
24171 	}
24172       inchash::add_rtx (val1->v.val_addr, hstate);
24173       break;
24174     case DW_OP_GNU_addr_index:
24175     case DW_OP_GNU_const_index:
24176       {
24177         if (loc->dtprel)
24178           {
24179             unsigned char dtprel = 0xd1;
24180 	    hstate.add_object (dtprel);
24181           }
24182         inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
24183       }
24184       break;
24185     case DW_OP_GNU_implicit_pointer:
24186       hstate.add_int (val2->v.val_int);
24187       break;
24188     case DW_OP_GNU_entry_value:
24189       hstate.add_object (val1->v.val_loc);
24190       break;
24191     case DW_OP_GNU_regval_type:
24192     case DW_OP_GNU_deref_type:
24193       {
24194 	unsigned int byte_size
24195 	  = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
24196 	unsigned int encoding
24197 	  = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
24198 	hstate.add_object (val1->v.val_int);
24199 	hstate.add_object (byte_size);
24200 	hstate.add_object (encoding);
24201       }
24202       break;
24203     case DW_OP_GNU_convert:
24204     case DW_OP_GNU_reinterpret:
24205       if (val1->val_class == dw_val_class_unsigned_const)
24206 	{
24207 	  hstate.add_object (val1->v.val_unsigned);
24208 	  break;
24209 	}
24210       /* FALLTHRU */
24211     case DW_OP_GNU_const_type:
24212       {
24213 	unsigned int byte_size
24214 	  = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
24215 	unsigned int encoding
24216 	  = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
24217 	hstate.add_object (byte_size);
24218 	hstate.add_object (encoding);
24219 	if (loc->dw_loc_opc != DW_OP_GNU_const_type)
24220 	  break;
24221 	hstate.add_object (val2->val_class);
24222 	switch (val2->val_class)
24223 	  {
24224 	  case dw_val_class_const:
24225 	    hstate.add_object (val2->v.val_int);
24226 	    break;
24227 	  case dw_val_class_vec:
24228 	    {
24229 	      unsigned int elt_size = val2->v.val_vec.elt_size;
24230 	      unsigned int len = val2->v.val_vec.length;
24231 
24232 	      hstate.add_object (elt_size);
24233 	      hstate.add_object (len);
24234 	      hstate.add (val2->v.val_vec.array, len * elt_size);
24235 	    }
24236 	    break;
24237 	  case dw_val_class_const_double:
24238 	    hstate.add_object (val2->v.val_double.low);
24239 	    hstate.add_object (val2->v.val_double.high);
24240 	    break;
24241 	  case dw_val_class_wide_int:
24242 	    hstate.add (val2->v.val_wide->get_val (),
24243 			get_full_len (*val2->v.val_wide)
24244 			* HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
24245 	    break;
24246 	  default:
24247 	    gcc_unreachable ();
24248 	  }
24249       }
24250       break;
24251 
24252     default:
24253       /* Other codes have no operands.  */
24254       break;
24255     }
24256 }
24257 
24258 /* Iteratively hash the whole DWARF location expression LOC into HSTATE.  */
24259 
24260 static inline void
24261 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
24262 {
24263   dw_loc_descr_ref l;
24264   bool sizes_computed = false;
24265   /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed.  */
24266   size_of_locs (loc);
24267 
24268   for (l = loc; l != NULL; l = l->dw_loc_next)
24269     {
24270       enum dwarf_location_atom opc = l->dw_loc_opc;
24271       hstate.add_object (opc);
24272       if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
24273 	{
24274 	  size_of_locs (loc);
24275 	  sizes_computed = true;
24276 	}
24277       hash_loc_operands (l, hstate);
24278     }
24279 }
24280 
24281 /* Compute hash of the whole location list LIST_HEAD.  */
24282 
24283 static inline void
24284 hash_loc_list (dw_loc_list_ref list_head)
24285 {
24286   dw_loc_list_ref curr = list_head;
24287   inchash::hash hstate;
24288 
24289   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
24290     {
24291       hstate.add (curr->begin, strlen (curr->begin) + 1);
24292       hstate.add (curr->end, strlen (curr->end) + 1);
24293       if (curr->section)
24294 	hstate.add (curr->section, strlen (curr->section) + 1);
24295       hash_locs (curr->expr, hstate);
24296     }
24297   list_head->hash = hstate.end ();
24298 }
24299 
24300 /* Return true if X and Y opcodes have the same operands.  */
24301 
24302 static inline bool
24303 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
24304 {
24305   dw_val_ref valx1 = &x->dw_loc_oprnd1;
24306   dw_val_ref valx2 = &x->dw_loc_oprnd2;
24307   dw_val_ref valy1 = &y->dw_loc_oprnd1;
24308   dw_val_ref valy2 = &y->dw_loc_oprnd2;
24309 
24310   switch (x->dw_loc_opc)
24311     {
24312     case DW_OP_const4u:
24313     case DW_OP_const8u:
24314       if (x->dtprel)
24315 	goto hash_addr;
24316       /* FALLTHRU */
24317     case DW_OP_const1u:
24318     case DW_OP_const1s:
24319     case DW_OP_const2u:
24320     case DW_OP_const2s:
24321     case DW_OP_const4s:
24322     case DW_OP_const8s:
24323     case DW_OP_constu:
24324     case DW_OP_consts:
24325     case DW_OP_pick:
24326     case DW_OP_plus_uconst:
24327     case DW_OP_breg0:
24328     case DW_OP_breg1:
24329     case DW_OP_breg2:
24330     case DW_OP_breg3:
24331     case DW_OP_breg4:
24332     case DW_OP_breg5:
24333     case DW_OP_breg6:
24334     case DW_OP_breg7:
24335     case DW_OP_breg8:
24336     case DW_OP_breg9:
24337     case DW_OP_breg10:
24338     case DW_OP_breg11:
24339     case DW_OP_breg12:
24340     case DW_OP_breg13:
24341     case DW_OP_breg14:
24342     case DW_OP_breg15:
24343     case DW_OP_breg16:
24344     case DW_OP_breg17:
24345     case DW_OP_breg18:
24346     case DW_OP_breg19:
24347     case DW_OP_breg20:
24348     case DW_OP_breg21:
24349     case DW_OP_breg22:
24350     case DW_OP_breg23:
24351     case DW_OP_breg24:
24352     case DW_OP_breg25:
24353     case DW_OP_breg26:
24354     case DW_OP_breg27:
24355     case DW_OP_breg28:
24356     case DW_OP_breg29:
24357     case DW_OP_breg30:
24358     case DW_OP_breg31:
24359     case DW_OP_regx:
24360     case DW_OP_fbreg:
24361     case DW_OP_piece:
24362     case DW_OP_deref_size:
24363     case DW_OP_xderef_size:
24364       return valx1->v.val_int == valy1->v.val_int;
24365     case DW_OP_skip:
24366     case DW_OP_bra:
24367       /* If splitting debug info, the use of DW_OP_GNU_addr_index
24368         can cause irrelevant differences in dw_loc_addr.  */
24369       gcc_assert (valx1->val_class == dw_val_class_loc
24370 		  && valy1->val_class == dw_val_class_loc
24371                   && (dwarf_split_debug_info
24372                       || x->dw_loc_addr == y->dw_loc_addr));
24373       return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
24374     case DW_OP_implicit_value:
24375       if (valx1->v.val_unsigned != valy1->v.val_unsigned
24376 	  || valx2->val_class != valy2->val_class)
24377 	return false;
24378       switch (valx2->val_class)
24379 	{
24380 	case dw_val_class_const:
24381 	  return valx2->v.val_int == valy2->v.val_int;
24382 	case dw_val_class_vec:
24383 	  return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24384 		 && valx2->v.val_vec.length == valy2->v.val_vec.length
24385 		 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24386 			    valx2->v.val_vec.elt_size
24387 			    * valx2->v.val_vec.length) == 0;
24388 	case dw_val_class_const_double:
24389 	  return valx2->v.val_double.low == valy2->v.val_double.low
24390 		 && valx2->v.val_double.high == valy2->v.val_double.high;
24391 	case dw_val_class_wide_int:
24392 	  return *valx2->v.val_wide == *valy2->v.val_wide;
24393 	case dw_val_class_addr:
24394 	  return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
24395 	default:
24396 	  gcc_unreachable ();
24397 	}
24398     case DW_OP_bregx:
24399     case DW_OP_bit_piece:
24400       return valx1->v.val_int == valy1->v.val_int
24401 	     && valx2->v.val_int == valy2->v.val_int;
24402     case DW_OP_addr:
24403     hash_addr:
24404       return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
24405     case DW_OP_GNU_addr_index:
24406     case DW_OP_GNU_const_index:
24407       {
24408         rtx ax1 = valx1->val_entry->addr.rtl;
24409         rtx ay1 = valy1->val_entry->addr.rtl;
24410         return rtx_equal_p (ax1, ay1);
24411       }
24412     case DW_OP_GNU_implicit_pointer:
24413       return valx1->val_class == dw_val_class_die_ref
24414 	     && valx1->val_class == valy1->val_class
24415 	     && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
24416 	     && valx2->v.val_int == valy2->v.val_int;
24417     case DW_OP_GNU_entry_value:
24418       return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
24419     case DW_OP_GNU_const_type:
24420       if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
24421 	  || valx2->val_class != valy2->val_class)
24422 	return false;
24423       switch (valx2->val_class)
24424 	{
24425 	case dw_val_class_const:
24426 	  return valx2->v.val_int == valy2->v.val_int;
24427 	case dw_val_class_vec:
24428 	  return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24429 		 && valx2->v.val_vec.length == valy2->v.val_vec.length
24430 		 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24431 			    valx2->v.val_vec.elt_size
24432 			    * valx2->v.val_vec.length) == 0;
24433 	case dw_val_class_const_double:
24434 	  return valx2->v.val_double.low == valy2->v.val_double.low
24435 		 && valx2->v.val_double.high == valy2->v.val_double.high;
24436 	case dw_val_class_wide_int:
24437 	  return *valx2->v.val_wide == *valy2->v.val_wide;
24438 	default:
24439 	  gcc_unreachable ();
24440 	}
24441     case DW_OP_GNU_regval_type:
24442     case DW_OP_GNU_deref_type:
24443       return valx1->v.val_int == valy1->v.val_int
24444 	     && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
24445     case DW_OP_GNU_convert:
24446     case DW_OP_GNU_reinterpret:
24447       if (valx1->val_class != valy1->val_class)
24448 	return false;
24449       if (valx1->val_class == dw_val_class_unsigned_const)
24450 	return valx1->v.val_unsigned == valy1->v.val_unsigned;
24451       return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24452     case DW_OP_GNU_parameter_ref:
24453       return valx1->val_class == dw_val_class_die_ref
24454 	     && valx1->val_class == valy1->val_class
24455 	     && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24456     default:
24457       /* Other codes have no operands.  */
24458       return true;
24459     }
24460 }
24461 
24462 /* Return true if DWARF location expressions X and Y are the same.  */
24463 
24464 static inline bool
24465 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
24466 {
24467   for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
24468     if (x->dw_loc_opc != y->dw_loc_opc
24469 	|| x->dtprel != y->dtprel
24470 	|| !compare_loc_operands (x, y))
24471       break;
24472   return x == NULL && y == NULL;
24473 }
24474 
24475 /* Hashtable helpers.  */
24476 
24477 struct loc_list_hasher : typed_noop_remove <dw_loc_list_struct>
24478 {
24479   typedef dw_loc_list_struct value_type;
24480   typedef dw_loc_list_struct compare_type;
24481   static inline hashval_t hash (const value_type *);
24482   static inline bool equal (const value_type *, const compare_type *);
24483 };
24484 
24485 /* Return precomputed hash of location list X.  */
24486 
24487 inline hashval_t
24488 loc_list_hasher::hash (const value_type *x)
24489 {
24490   return x->hash;
24491 }
24492 
24493 /* Return true if location lists A and B are the same.  */
24494 
24495 inline bool
24496 loc_list_hasher::equal (const value_type *a, const compare_type *b)
24497 {
24498   if (a == b)
24499     return 1;
24500   if (a->hash != b->hash)
24501     return 0;
24502   for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
24503     if (strcmp (a->begin, b->begin) != 0
24504 	|| strcmp (a->end, b->end) != 0
24505 	|| (a->section == NULL) != (b->section == NULL)
24506 	|| (a->section && strcmp (a->section, b->section) != 0)
24507 	|| !compare_locs (a->expr, b->expr))
24508       break;
24509   return a == NULL && b == NULL;
24510 }
24511 
24512 typedef hash_table<loc_list_hasher> loc_list_hash_type;
24513 
24514 
24515 /* Recursively optimize location lists referenced from DIE
24516    children and share them whenever possible.  */
24517 
24518 static void
24519 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
24520 {
24521   dw_die_ref c;
24522   dw_attr_ref a;
24523   unsigned ix;
24524   dw_loc_list_struct **slot;
24525 
24526   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24527     if (AT_class (a) == dw_val_class_loc_list)
24528       {
24529 	dw_loc_list_ref list = AT_loc_list (a);
24530 	/* TODO: perform some optimizations here, before hashing
24531 	   it and storing into the hash table.  */
24532 	hash_loc_list (list);
24533 	slot = htab->find_slot_with_hash (list, list->hash, INSERT);
24534 	if (*slot == NULL)
24535 	  *slot = list;
24536 	else
24537           a->dw_attr_val.v.val_loc_list = *slot;
24538       }
24539 
24540   FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
24541 }
24542 
24543 
24544 /* Recursively assign each location list a unique index into the debug_addr
24545    section.  */
24546 
24547 static void
24548 index_location_lists (dw_die_ref die)
24549 {
24550   dw_die_ref c;
24551   dw_attr_ref a;
24552   unsigned ix;
24553 
24554   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24555     if (AT_class (a) == dw_val_class_loc_list)
24556       {
24557         dw_loc_list_ref list = AT_loc_list (a);
24558         dw_loc_list_ref curr;
24559         for (curr = list; curr != NULL; curr = curr->dw_loc_next)
24560           {
24561             /* Don't index an entry that has already been indexed
24562                or won't be output.  */
24563             if (curr->begin_entry != NULL
24564                 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
24565               continue;
24566 
24567             curr->begin_entry
24568                 = add_addr_table_entry (xstrdup (curr->begin),
24569                                         ate_kind_label);
24570           }
24571       }
24572 
24573   FOR_EACH_CHILD (die, c, index_location_lists (c));
24574 }
24575 
24576 /* Optimize location lists referenced from DIE
24577    children and share them whenever possible.  */
24578 
24579 static void
24580 optimize_location_lists (dw_die_ref die)
24581 {
24582   loc_list_hash_type htab (500);
24583   optimize_location_lists_1 (die, &htab);
24584 }
24585 
24586 /* Output stuff that dwarf requires at the end of every file,
24587    and generate the DWARF-2 debugging info.  */
24588 
24589 static void
24590 dwarf2out_finish (const char *filename)
24591 {
24592   limbo_die_node *node, *next_node;
24593   comdat_type_node *ctnode;
24594   unsigned int i;
24595   dw_die_ref main_comp_unit_die;
24596 
24597   /* PCH might result in DW_AT_producer string being restored from the
24598      header compilation, so always fill it with empty string initially
24599      and overwrite only here.  */
24600   dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
24601   producer_string = gen_producer_string ();
24602   producer->dw_attr_val.v.val_str->refcount--;
24603   producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
24604 
24605   gen_scheduled_generic_parms_dies ();
24606   gen_remaining_tmpl_value_param_die_attribute ();
24607 
24608   /* Add the name for the main input file now.  We delayed this from
24609      dwarf2out_init to avoid complications with PCH.
24610      For LTO produced units use a fixed artificial name to avoid
24611      leaking tempfile names into the dwarf.  */
24612   if (!in_lto_p)
24613     add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
24614   else
24615     add_name_attribute (comp_unit_die (), "<artificial>");
24616   if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
24617     add_comp_dir_attribute (comp_unit_die ());
24618   else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
24619     {
24620       bool p = false;
24621       file_table->traverse<bool *, file_table_relative_p> (&p);
24622       if (p)
24623 	add_comp_dir_attribute (comp_unit_die ());
24624     }
24625 
24626   if (deferred_locations_list)
24627     for (i = 0; i < deferred_locations_list->length (); i++)
24628       {
24629 	add_location_or_const_value_attribute (
24630 	    (*deferred_locations_list)[i].die,
24631 	    (*deferred_locations_list)[i].variable,
24632 	    false,
24633 	    DW_AT_location);
24634       }
24635 
24636   /* Traverse the limbo die list, and add parent/child links.  The only
24637      dies without parents that should be here are concrete instances of
24638      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
24639      For concrete instances, we can get the parent die from the abstract
24640      instance.  */
24641   for (node = limbo_die_list; node; node = next_node)
24642     {
24643       dw_die_ref die = node->die;
24644       next_node = node->next;
24645 
24646       if (die->die_parent == NULL)
24647 	{
24648 	  dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
24649 
24650 	  if (origin && origin->die_parent)
24651 	    add_child_die (origin->die_parent, die);
24652 	  else if (is_cu_die (die))
24653 	    ;
24654 	  else if (seen_error ())
24655 	    /* It's OK to be confused by errors in the input.  */
24656 	    add_child_die (comp_unit_die (), die);
24657 	  else
24658 	    {
24659 	      /* In certain situations, the lexical block containing a
24660 		 nested function can be optimized away, which results
24661 		 in the nested function die being orphaned.  Likewise
24662 		 with the return type of that nested function.  Force
24663 		 this to be a child of the containing function.
24664 
24665 		 It may happen that even the containing function got fully
24666 		 inlined and optimized out.  In that case we are lost and
24667 		 assign the empty child.  This should not be big issue as
24668 		 the function is likely unreachable too.  */
24669 	      gcc_assert (node->created_for);
24670 
24671 	      if (DECL_P (node->created_for))
24672 		origin = get_context_die (DECL_CONTEXT (node->created_for));
24673 	      else if (TYPE_P (node->created_for))
24674 		origin = scope_die_for (node->created_for, comp_unit_die ());
24675 	      else
24676 		origin = comp_unit_die ();
24677 
24678 	      add_child_die (origin, die);
24679 	    }
24680 	}
24681     }
24682 
24683   limbo_die_list = NULL;
24684 
24685 #if ENABLE_ASSERT_CHECKING
24686   {
24687     dw_die_ref die = comp_unit_die (), c;
24688     FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
24689   }
24690 #endif
24691   resolve_addr (comp_unit_die ());
24692   move_marked_base_types ();
24693 
24694   for (node = deferred_asm_name; node; node = node->next)
24695     {
24696       tree decl = node->created_for;
24697       /* When generating LTO bytecode we can not generate new assembler
24698          names at this point and all important decls got theirs via
24699 	 free-lang-data.  */
24700       if (((!flag_generate_lto && !flag_generate_offload)
24701 	   || DECL_ASSEMBLER_NAME_SET_P (decl))
24702 	  && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
24703 	{
24704 	  add_linkage_attr (node->die, decl);
24705 	  move_linkage_attr (node->die);
24706 	}
24707     }
24708 
24709   deferred_asm_name = NULL;
24710 
24711   /* Walk through the list of incomplete types again, trying once more to
24712      emit full debugging info for them.  */
24713   retry_incomplete_types ();
24714 
24715   if (flag_eliminate_unused_debug_types)
24716     prune_unused_types ();
24717 
24718   /* Generate separate COMDAT sections for type DIEs. */
24719   if (use_debug_types)
24720     {
24721       break_out_comdat_types (comp_unit_die ());
24722 
24723       /* Each new type_unit DIE was added to the limbo die list when created.
24724          Since these have all been added to comdat_type_list, clear the
24725          limbo die list.  */
24726       limbo_die_list = NULL;
24727 
24728       /* For each new comdat type unit, copy declarations for incomplete
24729          types to make the new unit self-contained (i.e., no direct
24730          references to the main compile unit).  */
24731       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24732         copy_decls_for_unworthy_types (ctnode->root_die);
24733       copy_decls_for_unworthy_types (comp_unit_die ());
24734 
24735       /* In the process of copying declarations from one unit to another,
24736          we may have left some declarations behind that are no longer
24737          referenced.  Prune them.  */
24738       prune_unused_types ();
24739     }
24740 
24741   /* Generate separate CUs for each of the include files we've seen.
24742      They will go into limbo_die_list.  */
24743   if (flag_eliminate_dwarf2_dups)
24744     break_out_includes (comp_unit_die ());
24745 
24746   /* Traverse the DIE's and add add sibling attributes to those DIE's
24747      that have children.  */
24748   add_sibling_attributes (comp_unit_die ());
24749   for (node = limbo_die_list; node; node = node->next)
24750     add_sibling_attributes (node->die);
24751   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24752     add_sibling_attributes (ctnode->root_die);
24753 
24754   /* When splitting DWARF info, we put some attributes in the
24755      skeleton compile_unit DIE that remains in the .o, while
24756      most attributes go in the DWO compile_unit_die.  */
24757   if (dwarf_split_debug_info)
24758     main_comp_unit_die = gen_compile_unit_die (NULL);
24759   else
24760     main_comp_unit_die = comp_unit_die ();
24761 
24762   /* Output a terminator label for the .text section.  */
24763   switch_to_section (text_section);
24764   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
24765   if (cold_text_section)
24766     {
24767       switch_to_section (cold_text_section);
24768       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
24769     }
24770 
24771   /* We can only use the low/high_pc attributes if all of the code was
24772      in .text.  */
24773   if (!have_multiple_function_sections
24774       || (dwarf_version < 3 && dwarf_strict))
24775     {
24776       /* Don't add if the CU has no associated code.  */
24777       if (text_section_used)
24778         add_AT_low_high_pc (main_comp_unit_die, text_section_label,
24779                             text_end_label, true);
24780     }
24781   else
24782     {
24783       unsigned fde_idx;
24784       dw_fde_ref fde;
24785       bool range_list_added = false;
24786 
24787       if (text_section_used)
24788         add_ranges_by_labels (main_comp_unit_die, text_section_label,
24789                               text_end_label, &range_list_added, true);
24790       if (cold_text_section_used)
24791         add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
24792                               cold_end_label, &range_list_added, true);
24793 
24794       FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
24795 	{
24796 	  if (DECL_IGNORED_P (fde->decl))
24797 	    continue;
24798 	  if (!fde->in_std_section)
24799             add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
24800                                   fde->dw_fde_end, &range_list_added,
24801                                   true);
24802 	  if (fde->dw_fde_second_begin && !fde->second_in_std_section)
24803             add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
24804                                   fde->dw_fde_second_end, &range_list_added,
24805                                   true);
24806 	}
24807 
24808       if (range_list_added)
24809 	{
24810 	  /* We need to give .debug_loc and .debug_ranges an appropriate
24811 	     "base address".  Use zero so that these addresses become
24812 	     absolute.  Historically, we've emitted the unexpected
24813 	     DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
24814 	     Emit both to give time for other tools to adapt.  */
24815           add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
24816 	  if (! dwarf_strict && dwarf_version < 4)
24817             add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
24818 
24819 	  add_ranges (NULL);
24820 	}
24821     }
24822 
24823   if (debug_info_level >= DINFO_LEVEL_TERSE)
24824     add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
24825 		    debug_line_section_label);
24826 
24827   if (have_macinfo)
24828     add_AT_macptr (comp_unit_die (),
24829 		   dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
24830 		   macinfo_section_label);
24831 
24832   if (dwarf_split_debug_info)
24833     {
24834       /* optimize_location_lists calculates the size of the lists,
24835          so index them first, and assign indices to the entries.
24836          Although optimize_location_lists will remove entries from
24837          the table, it only does so for duplicates, and therefore
24838          only reduces ref_counts to 1.  */
24839       index_location_lists (comp_unit_die ());
24840 
24841       if (addr_index_table != NULL)
24842         {
24843           unsigned int index = 0;
24844           addr_index_table
24845 	    ->traverse_noresize<unsigned int *, index_addr_table_entry>
24846 	    (&index);
24847         }
24848     }
24849 
24850   if (have_location_lists)
24851     optimize_location_lists (comp_unit_die ());
24852 
24853   save_macinfo_strings ();
24854 
24855   if (dwarf_split_debug_info)
24856     {
24857       unsigned int index = 0;
24858 
24859       /* Add attributes common to skeleton compile_units and
24860          type_units.  Because these attributes include strings, it
24861          must be done before freezing the string table.  Top-level
24862          skeleton die attrs are added when the skeleton type unit is
24863          created, so ensure it is created by this point.  */
24864       add_top_level_skeleton_die_attrs (main_comp_unit_die);
24865       debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
24866     }
24867 
24868   /* Output all of the compilation units.  We put the main one last so that
24869      the offsets are available to output_pubnames.  */
24870   for (node = limbo_die_list; node; node = node->next)
24871     output_comp_unit (node->die, 0);
24872 
24873   hash_table<comdat_type_hasher> comdat_type_table (100);
24874   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24875     {
24876       comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
24877 
24878       /* Don't output duplicate types.  */
24879       if (*slot != HTAB_EMPTY_ENTRY)
24880         continue;
24881 
24882       /* Add a pointer to the line table for the main compilation unit
24883          so that the debugger can make sense of DW_AT_decl_file
24884          attributes.  */
24885       if (debug_info_level >= DINFO_LEVEL_TERSE)
24886         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
24887                         (!dwarf_split_debug_info
24888                          ? debug_line_section_label
24889                          : debug_skeleton_line_section_label));
24890 
24891       output_comdat_type_unit (ctnode);
24892       *slot = ctnode;
24893     }
24894 
24895   /* The AT_pubnames attribute needs to go in all skeleton dies, including
24896      both the main_cu and all skeleton TUs.  Making this call unconditional
24897      would end up either adding a second copy of the AT_pubnames attribute, or
24898      requiring a special case in add_top_level_skeleton_die_attrs.  */
24899   if (!dwarf_split_debug_info)
24900     add_AT_pubnames (comp_unit_die ());
24901 
24902   if (dwarf_split_debug_info)
24903     {
24904       int mark;
24905       unsigned char checksum[16];
24906       struct md5_ctx ctx;
24907 
24908       /* Compute a checksum of the comp_unit to use as the dwo_id.  */
24909       md5_init_ctx (&ctx);
24910       mark = 0;
24911       die_checksum (comp_unit_die (), &ctx, &mark);
24912       unmark_all_dies (comp_unit_die ());
24913       md5_finish_ctx (&ctx, checksum);
24914 
24915       /* Use the first 8 bytes of the checksum as the dwo_id,
24916         and add it to both comp-unit DIEs.  */
24917       add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
24918       add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
24919 
24920       /* Add the base offset of the ranges table to the skeleton
24921         comp-unit DIE.  */
24922       if (ranges_table_in_use)
24923         add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
24924                         ranges_section_label);
24925 
24926       switch_to_section (debug_addr_section);
24927       ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
24928       output_addr_table ();
24929     }
24930 
24931   /* Output the main compilation unit if non-empty or if .debug_macinfo
24932      or .debug_macro will be emitted.  */
24933   output_comp_unit (comp_unit_die (), have_macinfo);
24934 
24935   if (dwarf_split_debug_info && info_section_emitted)
24936     output_skeleton_debug_sections (main_comp_unit_die);
24937 
24938   /* Output the abbreviation table.  */
24939   if (abbrev_die_table_in_use != 1)
24940     {
24941       switch_to_section (debug_abbrev_section);
24942       ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
24943       output_abbrev_section ();
24944     }
24945 
24946   /* Output location list section if necessary.  */
24947   if (have_location_lists)
24948     {
24949       /* Output the location lists info.  */
24950       switch_to_section (debug_loc_section);
24951       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
24952       output_location_lists (comp_unit_die ());
24953     }
24954 
24955   output_pubtables ();
24956 
24957   /* Output the address range information if a CU (.debug_info section)
24958      was emitted.  We output an empty table even if we had no functions
24959      to put in it.  This because the consumer has no way to tell the
24960      difference between an empty table that we omitted and failure to
24961      generate a table that would have contained data.  */
24962   if (info_section_emitted)
24963     {
24964       unsigned long aranges_length = size_of_aranges ();
24965 
24966       switch_to_section (debug_aranges_section);
24967       output_aranges (aranges_length);
24968     }
24969 
24970   /* Output ranges section if necessary.  */
24971   if (ranges_table_in_use)
24972     {
24973       switch_to_section (debug_ranges_section);
24974       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
24975       output_ranges ();
24976     }
24977 
24978   /* Have to end the macro section.  */
24979   if (have_macinfo)
24980     {
24981       switch_to_section (debug_macinfo_section);
24982       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
24983       output_macinfo ();
24984       dw2_asm_output_data (1, 0, "End compilation unit");
24985     }
24986 
24987   /* Output the source line correspondence table.  We must do this
24988      even if there is no line information.  Otherwise, on an empty
24989      translation unit, we will generate a present, but empty,
24990      .debug_info section.  IRIX 6.5 `nm' will then complain when
24991      examining the file.  This is done late so that any filenames
24992      used by the debug_info section are marked as 'used'.  */
24993   switch_to_section (debug_line_section);
24994   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
24995   if (! DWARF2_ASM_LINE_DEBUG_INFO)
24996     output_line_info (false);
24997 
24998   if (dwarf_split_debug_info && info_section_emitted)
24999     {
25000       switch_to_section (debug_skeleton_line_section);
25001       ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
25002       output_line_info (true);
25003     }
25004 
25005   /* If we emitted any indirect strings, output the string table too.  */
25006   if (debug_str_hash || skeleton_debug_str_hash)
25007     output_indirect_strings ();
25008 }
25009 
25010 /* Reset all state within dwarf2out.c so that we can rerun the compiler
25011    within the same process.  For use by toplev::finalize.  */
25012 
25013 void
25014 dwarf2out_c_finalize (void)
25015 {
25016   last_var_location_insn = NULL;
25017   cached_next_real_insn = NULL;
25018   used_rtx_array = NULL;
25019   incomplete_types = NULL;
25020   decl_scope_table = NULL;
25021   debug_info_section = NULL;
25022   debug_skeleton_info_section = NULL;
25023   debug_abbrev_section = NULL;
25024   debug_skeleton_abbrev_section = NULL;
25025   debug_aranges_section = NULL;
25026   debug_addr_section = NULL;
25027   debug_macinfo_section = NULL;
25028   debug_line_section = NULL;
25029   debug_skeleton_line_section = NULL;
25030   debug_loc_section = NULL;
25031   debug_pubnames_section = NULL;
25032   debug_pubtypes_section = NULL;
25033   debug_str_section = NULL;
25034   debug_str_dwo_section = NULL;
25035   debug_str_offsets_section = NULL;
25036   debug_ranges_section = NULL;
25037   debug_frame_section = NULL;
25038   fde_vec = NULL;
25039   debug_str_hash = NULL;
25040   skeleton_debug_str_hash = NULL;
25041   dw2_string_counter = 0;
25042   have_multiple_function_sections = false;
25043   text_section_used = false;
25044   cold_text_section_used = false;
25045   cold_text_section = NULL;
25046   current_unit_personality = NULL;
25047 
25048   deferred_locations_list = NULL;
25049 
25050   next_die_offset = 0;
25051   single_comp_unit_die = NULL;
25052   comdat_type_list = NULL;
25053   limbo_die_list = NULL;
25054   deferred_asm_name = NULL;
25055   file_table = NULL;
25056   decl_die_table = NULL;
25057   common_block_die_table = NULL;
25058   decl_loc_table = NULL;
25059   call_arg_locations = NULL;
25060   call_arg_loc_last = NULL;
25061   call_site_count = -1;
25062   tail_call_site_count = -1;
25063   //block_map = NULL;
25064   cached_dw_loc_list_table = NULL;
25065   abbrev_die_table = NULL;
25066   abbrev_die_table_allocated = 0;
25067   abbrev_die_table_in_use = 0;
25068   line_info_label_num = 0;
25069   cur_line_info_table = NULL;
25070   text_section_line_info = NULL;
25071   cold_text_section_line_info = NULL;
25072   separate_line_info = NULL;
25073   info_section_emitted = false;
25074   pubname_table = NULL;
25075   pubtype_table = NULL;
25076   macinfo_table = NULL;
25077   ranges_table = NULL;
25078   ranges_table_allocated = 0;
25079   ranges_table_in_use = 0;
25080   ranges_by_label = 0;
25081   ranges_by_label_allocated = 0;
25082   ranges_by_label_in_use = 0;
25083   have_location_lists = false;
25084   loclabel_num = 0;
25085   poc_label_num = 0;
25086   last_emitted_file = NULL;
25087   label_num = 0;
25088   file_table_last_lookup = NULL;
25089   tmpl_value_parm_die_table = NULL;
25090   generic_type_instances = NULL;
25091   frame_pointer_fb_offset = 0;
25092   frame_pointer_fb_offset_valid = false;
25093   base_types.release ();
25094   XDELETEVEC (producer_string);
25095   producer_string = NULL;
25096 }
25097 
25098 #include "gt-dwarf2out.h"
25099