1 /* Output Dwarf2 format symbol table information from GCC.
2    Copyright (C) 1992-2021 Free Software Foundation, Inc.
3    Contributed by Gary Funck (gary@intrepid.com).
4    Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
5    Extensively modified by Jason Merrill (jason@cygnus.com).
6 
7 This file is part of GCC.
8 
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
13 
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
17 for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3.  If not see
21 <http://www.gnu.org/licenses/>.  */
22 
23 /* TODO: Emit .debug_line header even when there are no functions, since
24 	   the file numbers are used by .debug_info.  Alternately, leave
25 	   out locations for types and decls.
26 	 Avoid talking about ctors and op= for PODs.
27 	 Factor out common prologue sequences into multiple CIEs.  */
28 
29 /* The first part of this file deals with the DWARF 2 frame unwind
30    information, which is also used by the GCC efficient exception handling
31    mechanism.  The second part, controlled only by an #ifdef
32    DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
33    information.  */
34 
35 /* DWARF2 Abbreviation Glossary:
36 
37    CFA = Canonical Frame Address
38 	   a fixed address on the stack which identifies a call frame.
39 	   We define it to be the value of SP just before the call insn.
40 	   The CFA register and offset, which may change during the course
41 	   of the function, are used to calculate its value at runtime.
42 
43    CFI = Call Frame Instruction
44 	   an instruction for the DWARF2 abstract machine
45 
46    CIE = Common Information Entry
47 	   information describing information common to one or more FDEs
48 
49    DIE = Debugging Information Entry
50 
51    FDE = Frame Description Entry
52 	   information describing the stack call frame, in particular,
53 	   how to restore registers
54 
55    DW_CFA_... = DWARF2 CFA call frame instruction
56    DW_TAG_... = DWARF2 DIE tag */
57 
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "target.h"
62 #include "function.h"
63 #include "rtl.h"
64 #include "tree.h"
65 #include "memmodel.h"
66 #include "tm_p.h"
67 #include "stringpool.h"
68 #include "insn-config.h"
69 #include "ira.h"
70 #include "cgraph.h"
71 #include "diagnostic.h"
72 #include "fold-const.h"
73 #include "stor-layout.h"
74 #include "varasm.h"
75 #include "version.h"
76 #include "flags.h"
77 #include "rtlhash.h"
78 #include "reload.h"
79 #include "output.h"
80 #include "expr.h"
81 #include "dwarf2out.h"
82 #include "dwarf2asm.h"
83 #include "toplev.h"
84 #include "md5.h"
85 #include "tree-pretty-print.h"
86 #include "print-rtl.h"
87 #include "debug.h"
88 #include "common/common-target.h"
89 #include "langhooks.h"
90 #include "lra.h"
91 #include "dumpfile.h"
92 #include "opts.h"
93 #include "tree-dfa.h"
94 #include "gdb/gdb-index.h"
95 #include "rtl-iter.h"
96 #include "stringpool.h"
97 #include "attribs.h"
98 #include "file-prefix-map.h" /* remap_debug_filename()  */
99 
100 static void dwarf2out_source_line (unsigned int, unsigned int, const char *,
101 				   int, bool);
102 static rtx_insn *last_var_location_insn;
103 static rtx_insn *cached_next_real_insn;
104 static void dwarf2out_decl (tree);
105 static bool is_redundant_typedef (const_tree);
106 
107 #ifndef XCOFF_DEBUGGING_INFO
108 #define XCOFF_DEBUGGING_INFO 0
109 #endif
110 
111 #ifndef HAVE_XCOFF_DWARF_EXTRAS
112 #define HAVE_XCOFF_DWARF_EXTRAS 0
113 #endif
114 
115 #ifdef VMS_DEBUGGING_INFO
116 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
117 
118 /* Define this macro to be a nonzero value if the directory specifications
119     which are output in the debug info should end with a separator.  */
120 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
121 /* Define this macro to evaluate to a nonzero value if GCC should refrain
122    from generating indirect strings in DWARF2 debug information, for instance
123    if your target is stuck with an old version of GDB that is unable to
124    process them properly or uses VMS Debug.  */
125 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
126 #else
127 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
128 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
129 #endif
130 
131 /* ??? Poison these here until it can be done generically.  They've been
132    totally replaced in this file; make sure it stays that way.  */
133 #undef DWARF2_UNWIND_INFO
134 #undef DWARF2_FRAME_INFO
135 #if (GCC_VERSION >= 3000)
136  #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
137 #endif
138 
139 /* The size of the target's pointer type.  */
140 #ifndef PTR_SIZE
141 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
142 #endif
143 
144 /* Array of RTXes referenced by the debugging information, which therefore
145    must be kept around forever.  */
146 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
147 
148 /* A pointer to the base of a list of incomplete types which might be
149    completed at some later time.  incomplete_types_list needs to be a
150    vec<tree, va_gc> *because we want to tell the garbage collector about
151    it.  */
152 static GTY(()) vec<tree, va_gc> *incomplete_types;
153 
154 /* Pointers to various DWARF2 sections.  */
155 static GTY(()) section *debug_info_section;
156 static GTY(()) section *debug_skeleton_info_section;
157 static GTY(()) section *debug_abbrev_section;
158 static GTY(()) section *debug_skeleton_abbrev_section;
159 static GTY(()) section *debug_aranges_section;
160 static GTY(()) section *debug_addr_section;
161 static GTY(()) section *debug_macinfo_section;
162 static const char *debug_macinfo_section_name;
163 static unsigned macinfo_label_base = 1;
164 static GTY(()) section *debug_line_section;
165 static GTY(()) section *debug_skeleton_line_section;
166 static GTY(()) section *debug_loc_section;
167 static GTY(()) section *debug_pubnames_section;
168 static GTY(()) section *debug_pubtypes_section;
169 static GTY(()) section *debug_str_section;
170 static GTY(()) section *debug_line_str_section;
171 static GTY(()) section *debug_str_dwo_section;
172 static GTY(()) section *debug_str_offsets_section;
173 static GTY(()) section *debug_ranges_section;
174 static GTY(()) section *debug_ranges_dwo_section;
175 static GTY(()) section *debug_frame_section;
176 
177 /* Maximum size (in bytes) of an artificially generated label.  */
178 #define MAX_ARTIFICIAL_LABEL_BYTES	40
179 
180 /* According to the (draft) DWARF 3 specification, the initial length
181    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
182    bytes are 0xffffffff, followed by the length stored in the next 8
183    bytes.
184 
185    However, the SGI/MIPS ABI uses an initial length which is equal to
186    dwarf_offset_size.  It is defined (elsewhere) accordingly.  */
187 
188 #ifndef DWARF_INITIAL_LENGTH_SIZE
189 #define DWARF_INITIAL_LENGTH_SIZE (dwarf_offset_size == 4 ? 4 : 12)
190 #endif
191 
192 #ifndef DWARF_INITIAL_LENGTH_SIZE_STR
193 #define DWARF_INITIAL_LENGTH_SIZE_STR (dwarf_offset_size == 4 ? "-4" : "-12")
194 #endif
195 
196 /* Round SIZE up to the nearest BOUNDARY.  */
197 #define DWARF_ROUND(SIZE,BOUNDARY) \
198   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
199 
200 /* CIE identifier.  */
201 #if HOST_BITS_PER_WIDE_INT >= 64
202 #define DWARF_CIE_ID \
203   (unsigned HOST_WIDE_INT) (dwarf_offset_size == 4 ? DW_CIE_ID : DW64_CIE_ID)
204 #else
205 #define DWARF_CIE_ID DW_CIE_ID
206 #endif
207 
208 
209 /* A vector for a table that contains frame description
210    information for each routine.  */
211 #define NOT_INDEXED (-1U)
212 #define NO_INDEX_ASSIGNED (-2U)
213 
214 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
215 
216 struct GTY((for_user)) indirect_string_node {
217   const char *str;
218   unsigned int refcount;
219   enum dwarf_form form;
220   char *label;
221   unsigned int index;
222 };
223 
224 struct indirect_string_hasher : ggc_ptr_hash<indirect_string_node>
225 {
226   typedef const char *compare_type;
227 
228   static hashval_t hash (indirect_string_node *);
229   static bool equal (indirect_string_node *, const char *);
230 };
231 
232 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
233 
234 static GTY (()) hash_table<indirect_string_hasher> *debug_line_str_hash;
235 
236 /* With split_debug_info, both the comp_dir and dwo_name go in the
237    main object file, rather than the dwo, similar to the force_direct
238    parameter elsewhere but with additional complications:
239 
240    1) The string is needed in both the main object file and the dwo.
241    That is, the comp_dir and dwo_name will appear in both places.
242 
243    2) Strings can use four forms: DW_FORM_string, DW_FORM_strp,
244    DW_FORM_line_strp or DW_FORM_strx/GNU_str_index.
245 
246    3) GCC chooses the form to use late, depending on the size and
247    reference count.
248 
249    Rather than forcing the all debug string handling functions and
250    callers to deal with these complications, simply use a separate,
251    special-cased string table for any attribute that should go in the
252    main object file.  This limits the complexity to just the places
253    that need it.  */
254 
255 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
256 
257 static GTY(()) int dw2_string_counter;
258 
259 /* True if the compilation unit places functions in more than one section.  */
260 static GTY(()) bool have_multiple_function_sections = false;
261 
262 /* Whether the default text and cold text sections have been used at all.  */
263 static GTY(()) bool text_section_used = false;
264 static GTY(()) bool cold_text_section_used = false;
265 
266 /* The default cold text section.  */
267 static GTY(()) section *cold_text_section;
268 
269 /* The DIE for C++14 'auto' in a function return type.  */
270 static GTY(()) dw_die_ref auto_die;
271 
272 /* The DIE for C++14 'decltype(auto)' in a function return type.  */
273 static GTY(()) dw_die_ref decltype_auto_die;
274 
275 /* Forward declarations for functions defined in this file.  */
276 
277 static void output_call_frame_info (int);
278 static void dwarf2out_note_section_used (void);
279 
280 /* Personality decl of current unit.  Used only when assembler does not support
281    personality CFI.  */
282 static GTY(()) rtx current_unit_personality;
283 
284 /* Whether an eh_frame section is required.  */
285 static GTY(()) bool do_eh_frame = false;
286 
287 /* .debug_rnglists next index.  */
288 static unsigned int rnglist_idx;
289 
290 /* Data and reference forms for relocatable data.  */
291 #define DW_FORM_data (dwarf_offset_size == 8 ? DW_FORM_data8 : DW_FORM_data4)
292 #define DW_FORM_ref (dwarf_offset_size == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
293 
294 #ifndef DEBUG_FRAME_SECTION
295 #define DEBUG_FRAME_SECTION	".debug_frame"
296 #endif
297 
298 #ifndef FUNC_BEGIN_LABEL
299 #define FUNC_BEGIN_LABEL	"LFB"
300 #endif
301 
302 #ifndef FUNC_SECOND_SECT_LABEL
303 #define FUNC_SECOND_SECT_LABEL	"LFSB"
304 #endif
305 
306 #ifndef FUNC_END_LABEL
307 #define FUNC_END_LABEL		"LFE"
308 #endif
309 
310 #ifndef PROLOGUE_END_LABEL
311 #define PROLOGUE_END_LABEL	"LPE"
312 #endif
313 
314 #ifndef EPILOGUE_BEGIN_LABEL
315 #define EPILOGUE_BEGIN_LABEL	"LEB"
316 #endif
317 
318 #ifndef FRAME_BEGIN_LABEL
319 #define FRAME_BEGIN_LABEL	"Lframe"
320 #endif
321 #define CIE_AFTER_SIZE_LABEL	"LSCIE"
322 #define CIE_END_LABEL		"LECIE"
323 #define FDE_LABEL		"LSFDE"
324 #define FDE_AFTER_SIZE_LABEL	"LASFDE"
325 #define FDE_END_LABEL		"LEFDE"
326 #define LINE_NUMBER_BEGIN_LABEL	"LSLT"
327 #define LINE_NUMBER_END_LABEL	"LELT"
328 #define LN_PROLOG_AS_LABEL	"LASLTP"
329 #define LN_PROLOG_END_LABEL	"LELTP"
330 #define DIE_LABEL_PREFIX	"DW"
331 
332 /* Match the base name of a file to the base name of a compilation unit. */
333 
334 static int
matches_main_base(const char * path)335 matches_main_base (const char *path)
336 {
337   /* Cache the last query. */
338   static const char *last_path = NULL;
339   static int last_match = 0;
340   if (path != last_path)
341     {
342       const char *base;
343       int length = base_of_path (path, &base);
344       last_path = path;
345       last_match = (length == main_input_baselength
346                     && memcmp (base, main_input_basename, length) == 0);
347     }
348   return last_match;
349 }
350 
351 #ifdef DEBUG_DEBUG_STRUCT
352 
353 static int
dump_struct_debug(tree type,enum debug_info_usage usage,enum debug_struct_file criterion,int generic,int matches,int result)354 dump_struct_debug (tree type, enum debug_info_usage usage,
355 		   enum debug_struct_file criterion, int generic,
356 		   int matches, int result)
357 {
358   /* Find the type name. */
359   tree type_decl = TYPE_STUB_DECL (type);
360   tree t = type_decl;
361   const char *name = 0;
362   if (TREE_CODE (t) == TYPE_DECL)
363     t = DECL_NAME (t);
364   if (t)
365     name = IDENTIFIER_POINTER (t);
366 
367   fprintf (stderr, "	struct %d %s %s %s %s %d %p %s\n",
368 	   criterion,
369            DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
370            matches ? "bas" : "hdr",
371            generic ? "gen" : "ord",
372            usage == DINFO_USAGE_DFN ? ";" :
373              usage == DINFO_USAGE_DIR_USE ? "." : "*",
374            result,
375            (void*) type_decl, name);
376   return result;
377 }
378 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
379   dump_struct_debug (type, usage, criterion, generic, matches, result)
380 
381 #else
382 
383 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
384   (result)
385 
386 #endif
387 
388 /* Get the number of HOST_WIDE_INTs needed to represent the precision
389    of the number.  */
390 
391 static unsigned int
get_full_len(const wide_int & op)392 get_full_len (const wide_int &op)
393 {
394   int prec = wi::get_precision (op);
395   return ((prec + HOST_BITS_PER_WIDE_INT - 1)
396 	  / HOST_BITS_PER_WIDE_INT);
397 }
398 
399 static bool
should_emit_struct_debug(tree type,enum debug_info_usage usage)400 should_emit_struct_debug (tree type, enum debug_info_usage usage)
401 {
402   if (debug_info_level <= DINFO_LEVEL_TERSE)
403     return false;
404 
405   enum debug_struct_file criterion;
406   tree type_decl;
407   bool generic = lang_hooks.types.generic_p (type);
408 
409   if (generic)
410     criterion = debug_struct_generic[usage];
411   else
412     criterion = debug_struct_ordinary[usage];
413 
414   if (criterion == DINFO_STRUCT_FILE_NONE)
415     return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
416   if (criterion == DINFO_STRUCT_FILE_ANY)
417     return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
418 
419   type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
420 
421   if (type_decl != NULL)
422     {
423      if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
424         return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
425 
426       if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
427         return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
428     }
429 
430   return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
431 }
432 
433 /* Switch [BACK] to eh_frame_section.  If we don't have an eh_frame_section,
434    switch to the data section instead, and write out a synthetic start label
435    for collect2 the first time around.  */
436 
437 static void
switch_to_eh_frame_section(bool back ATTRIBUTE_UNUSED)438 switch_to_eh_frame_section (bool back ATTRIBUTE_UNUSED)
439 {
440   if (eh_frame_section == 0)
441     {
442       int flags;
443 
444       if (EH_TABLES_CAN_BE_READ_ONLY)
445 	{
446 	  int fde_encoding;
447 	  int per_encoding;
448 	  int lsda_encoding;
449 
450 	  fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
451 						       /*global=*/0);
452 	  per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
453 						       /*global=*/1);
454 	  lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
455 							/*global=*/0);
456 	  flags = ((! flag_pic
457 		    || ((fde_encoding & 0x70) != DW_EH_PE_absptr
458 			&& (fde_encoding & 0x70) != DW_EH_PE_aligned
459 			&& (per_encoding & 0x70) != DW_EH_PE_absptr
460 			&& (per_encoding & 0x70) != DW_EH_PE_aligned
461 			&& (lsda_encoding & 0x70) != DW_EH_PE_absptr
462 			&& (lsda_encoding & 0x70) != DW_EH_PE_aligned))
463 		   ? 0 : SECTION_WRITE);
464 	}
465       else
466 	flags = SECTION_WRITE;
467 
468 #ifdef EH_FRAME_SECTION_NAME
469       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
470 #else
471       eh_frame_section = ((flags == SECTION_WRITE)
472 			  ? data_section : readonly_data_section);
473 #endif /* EH_FRAME_SECTION_NAME */
474     }
475 
476   switch_to_section (eh_frame_section);
477 
478 #ifdef EH_FRAME_THROUGH_COLLECT2
479   /* We have no special eh_frame section.  Emit special labels to guide
480      collect2.  */
481   if (!back)
482     {
483       tree label = get_file_function_name ("F");
484       ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
485       targetm.asm_out.globalize_label (asm_out_file,
486 					IDENTIFIER_POINTER (label));
487       ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
488     }
489 #endif
490 }
491 
492 /* Switch [BACK] to the eh or debug frame table section, depending on
493    FOR_EH.  */
494 
495 static void
switch_to_frame_table_section(int for_eh,bool back)496 switch_to_frame_table_section (int for_eh, bool back)
497 {
498   if (for_eh)
499     switch_to_eh_frame_section (back);
500   else
501     {
502       if (!debug_frame_section)
503 	debug_frame_section = get_section (DEBUG_FRAME_SECTION,
504 					   SECTION_DEBUG, NULL);
505       switch_to_section (debug_frame_section);
506     }
507 }
508 
509 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
510 
511 enum dw_cfi_oprnd_type
dw_cfi_oprnd1_desc(enum dwarf_call_frame_info cfi)512 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
513 {
514   switch (cfi)
515     {
516     case DW_CFA_nop:
517     case DW_CFA_GNU_window_save:
518     case DW_CFA_remember_state:
519     case DW_CFA_restore_state:
520       return dw_cfi_oprnd_unused;
521 
522     case DW_CFA_set_loc:
523     case DW_CFA_advance_loc1:
524     case DW_CFA_advance_loc2:
525     case DW_CFA_advance_loc4:
526     case DW_CFA_MIPS_advance_loc8:
527       return dw_cfi_oprnd_addr;
528 
529     case DW_CFA_offset:
530     case DW_CFA_offset_extended:
531     case DW_CFA_def_cfa:
532     case DW_CFA_offset_extended_sf:
533     case DW_CFA_def_cfa_sf:
534     case DW_CFA_restore:
535     case DW_CFA_restore_extended:
536     case DW_CFA_undefined:
537     case DW_CFA_same_value:
538     case DW_CFA_def_cfa_register:
539     case DW_CFA_register:
540     case DW_CFA_expression:
541     case DW_CFA_val_expression:
542       return dw_cfi_oprnd_reg_num;
543 
544     case DW_CFA_def_cfa_offset:
545     case DW_CFA_GNU_args_size:
546     case DW_CFA_def_cfa_offset_sf:
547       return dw_cfi_oprnd_offset;
548 
549     case DW_CFA_def_cfa_expression:
550       return dw_cfi_oprnd_loc;
551 
552     default:
553       gcc_unreachable ();
554     }
555 }
556 
557 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
558 
559 enum dw_cfi_oprnd_type
dw_cfi_oprnd2_desc(enum dwarf_call_frame_info cfi)560 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
561 {
562   switch (cfi)
563     {
564     case DW_CFA_def_cfa:
565     case DW_CFA_def_cfa_sf:
566     case DW_CFA_offset:
567     case DW_CFA_offset_extended_sf:
568     case DW_CFA_offset_extended:
569       return dw_cfi_oprnd_offset;
570 
571     case DW_CFA_register:
572       return dw_cfi_oprnd_reg_num;
573 
574     case DW_CFA_expression:
575     case DW_CFA_val_expression:
576       return dw_cfi_oprnd_loc;
577 
578     case DW_CFA_def_cfa_expression:
579       return dw_cfi_oprnd_cfa_loc;
580 
581     default:
582       return dw_cfi_oprnd_unused;
583     }
584 }
585 
586 /* Output one FDE.  */
587 
588 static void
output_fde(dw_fde_ref fde,bool for_eh,bool second,char * section_start_label,int fde_encoding,char * augmentation,bool any_lsda_needed,int lsda_encoding)589 output_fde (dw_fde_ref fde, bool for_eh, bool second,
590 	    char *section_start_label, int fde_encoding, char *augmentation,
591 	    bool any_lsda_needed, int lsda_encoding)
592 {
593   const char *begin, *end;
594   static unsigned int j;
595   char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
596 
597   targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
598 				     /* empty */ 0);
599   targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
600 				  for_eh + j);
601   ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
602   ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
603   if (!XCOFF_DEBUGGING_INFO || for_eh)
604     {
605       if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4 && !for_eh)
606 	dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
607 			     " indicating 64-bit DWARF extension");
608       dw2_asm_output_delta (for_eh ? 4 : dwarf_offset_size, l2, l1,
609 			    "FDE Length");
610     }
611   ASM_OUTPUT_LABEL (asm_out_file, l1);
612 
613   if (for_eh)
614     dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
615   else
616     dw2_asm_output_offset (dwarf_offset_size, section_start_label,
617 			   debug_frame_section, "FDE CIE offset");
618 
619   begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
620   end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
621 
622   if (for_eh)
623     {
624       rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
625       SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
626       dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
627 				       "FDE initial location");
628       dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
629 			    end, begin, "FDE address range");
630     }
631   else
632     {
633       dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
634       dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
635     }
636 
637   if (augmentation[0])
638     {
639       if (any_lsda_needed)
640 	{
641 	  int size = size_of_encoded_value (lsda_encoding);
642 
643 	  if (lsda_encoding == DW_EH_PE_aligned)
644 	    {
645 	      int offset = (  4		/* Length */
646 			    + 4		/* CIE offset */
647 			    + 2 * size_of_encoded_value (fde_encoding)
648 			    + 1		/* Augmentation size */ );
649 	      int pad = -offset & (PTR_SIZE - 1);
650 
651 	      size += pad;
652 	      gcc_assert (size_of_uleb128 (size) == 1);
653 	    }
654 
655 	  dw2_asm_output_data_uleb128 (size, "Augmentation size");
656 
657 	  if (fde->uses_eh_lsda)
658 	    {
659 	      ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
660 					   fde->funcdef_number);
661 	      dw2_asm_output_encoded_addr_rtx (lsda_encoding,
662 					       gen_rtx_SYMBOL_REF (Pmode, l1),
663 					       false,
664 					       "Language Specific Data Area");
665 	    }
666 	  else
667 	    {
668 	      if (lsda_encoding == DW_EH_PE_aligned)
669 		ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
670 	      dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
671 				   "Language Specific Data Area (none)");
672 	    }
673 	}
674       else
675 	dw2_asm_output_data_uleb128 (0, "Augmentation size");
676     }
677 
678   /* Loop through the Call Frame Instructions associated with this FDE.  */
679   fde->dw_fde_current_label = begin;
680   {
681     size_t from, until, i;
682 
683     from = 0;
684     until = vec_safe_length (fde->dw_fde_cfi);
685 
686     if (fde->dw_fde_second_begin == NULL)
687       ;
688     else if (!second)
689       until = fde->dw_fde_switch_cfi_index;
690     else
691       from = fde->dw_fde_switch_cfi_index;
692 
693     for (i = from; i < until; i++)
694       output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
695   }
696 
697   /* If we are to emit a ref/link from function bodies to their frame tables,
698      do it now.  This is typically performed to make sure that tables
699      associated with functions are dragged with them and not discarded in
700      garbage collecting links. We need to do this on a per function basis to
701      cope with -ffunction-sections.  */
702 
703 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
704   /* Switch to the function section, emit the ref to the tables, and
705      switch *back* into the table section.  */
706   switch_to_section (function_section (fde->decl));
707   ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
708   switch_to_frame_table_section (for_eh, true);
709 #endif
710 
711   /* Pad the FDE out to an address sized boundary.  */
712   ASM_OUTPUT_ALIGN (asm_out_file,
713 		    floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
714   ASM_OUTPUT_LABEL (asm_out_file, l2);
715 
716   j += 2;
717 }
718 
719 /* Return true if frame description entry FDE is needed for EH.  */
720 
721 static bool
fde_needed_for_eh_p(dw_fde_ref fde)722 fde_needed_for_eh_p (dw_fde_ref fde)
723 {
724   if (flag_asynchronous_unwind_tables)
725     return true;
726 
727   if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
728     return true;
729 
730   if (fde->uses_eh_lsda)
731     return true;
732 
733   /* If exceptions are enabled, we have collected nothrow info.  */
734   if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
735     return false;
736 
737   return true;
738 }
739 
740 /* Output the call frame information used to record information
741    that relates to calculating the frame pointer, and records the
742    location of saved registers.  */
743 
744 static void
output_call_frame_info(int for_eh)745 output_call_frame_info (int for_eh)
746 {
747   unsigned int i;
748   dw_fde_ref fde;
749   dw_cfi_ref cfi;
750   char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
751   char section_start_label[MAX_ARTIFICIAL_LABEL_BYTES];
752   bool any_lsda_needed = false;
753   char augmentation[6];
754   int augmentation_size;
755   int fde_encoding = DW_EH_PE_absptr;
756   int per_encoding = DW_EH_PE_absptr;
757   int lsda_encoding = DW_EH_PE_absptr;
758   int return_reg;
759   rtx personality = NULL;
760   int dw_cie_version;
761 
762   /* Don't emit a CIE if there won't be any FDEs.  */
763   if (!fde_vec)
764     return;
765 
766   /* Nothing to do if the assembler's doing it all.  */
767   if (dwarf2out_do_cfi_asm ())
768     return;
769 
770   /* If we don't have any functions we'll want to unwind out of, don't emit
771      any EH unwind information.  If we make FDEs linkonce, we may have to
772      emit an empty label for an FDE that wouldn't otherwise be emitted.  We
773      want to avoid having an FDE kept around when the function it refers to
774      is discarded.  Example where this matters: a primary function template
775      in C++ requires EH information, an explicit specialization doesn't.  */
776   if (for_eh)
777     {
778       bool any_eh_needed = false;
779 
780       FOR_EACH_VEC_ELT (*fde_vec, i, fde)
781 	{
782 	  if (fde->uses_eh_lsda)
783 	    any_eh_needed = any_lsda_needed = true;
784 	  else if (fde_needed_for_eh_p (fde))
785 	    any_eh_needed = true;
786 	  else if (TARGET_USES_WEAK_UNWIND_INFO)
787 	    targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
788 	}
789 
790       if (!any_eh_needed)
791 	return;
792     }
793 
794   /* We're going to be generating comments, so turn on app.  */
795   if (flag_debug_asm)
796     app_enable ();
797 
798   /* Switch to the proper frame section, first time.  */
799   switch_to_frame_table_section (for_eh, false);
800 
801   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
802   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
803 
804   /* Output the CIE.  */
805   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
806   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
807   if (!XCOFF_DEBUGGING_INFO || for_eh)
808     {
809       if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4 && !for_eh)
810 	dw2_asm_output_data (4, 0xffffffff,
811 	  "Initial length escape value indicating 64-bit DWARF extension");
812       dw2_asm_output_delta (for_eh ? 4 : dwarf_offset_size, l2, l1,
813 			    "Length of Common Information Entry");
814     }
815   ASM_OUTPUT_LABEL (asm_out_file, l1);
816 
817   /* Now that the CIE pointer is PC-relative for EH,
818      use 0 to identify the CIE.  */
819   dw2_asm_output_data ((for_eh ? 4 : dwarf_offset_size),
820 		       (for_eh ? 0 : DWARF_CIE_ID),
821 		       "CIE Identifier Tag");
822 
823   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
824      use CIE version 1, unless that would produce incorrect results
825      due to overflowing the return register column.  */
826   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
827   dw_cie_version = 1;
828   if (return_reg >= 256 || dwarf_version > 2)
829     dw_cie_version = 3;
830   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
831 
832   augmentation[0] = 0;
833   augmentation_size = 0;
834 
835   personality = current_unit_personality;
836   if (for_eh)
837     {
838       char *p;
839 
840       /* Augmentation:
841 	 z	Indicates that a uleb128 is present to size the
842 		augmentation section.
843 	 L	Indicates the encoding (and thus presence) of
844 		an LSDA pointer in the FDE augmentation.
845 	 R	Indicates a non-default pointer encoding for
846 		FDE code pointers.
847 	 P	Indicates the presence of an encoding + language
848 		personality routine in the CIE augmentation.  */
849 
850       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
851       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
852       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
853 
854       p = augmentation + 1;
855       if (personality)
856 	{
857 	  *p++ = 'P';
858 	  augmentation_size += 1 + size_of_encoded_value (per_encoding);
859 	  assemble_external_libcall (personality);
860 	}
861       if (any_lsda_needed)
862 	{
863 	  *p++ = 'L';
864 	  augmentation_size += 1;
865 	}
866       if (fde_encoding != DW_EH_PE_absptr)
867 	{
868 	  *p++ = 'R';
869 	  augmentation_size += 1;
870 	}
871       if (p > augmentation + 1)
872 	{
873 	  augmentation[0] = 'z';
874 	  *p = '\0';
875 	}
876 
877       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
878       if (personality && per_encoding == DW_EH_PE_aligned)
879 	{
880 	  int offset = (  4		/* Length */
881 			+ 4		/* CIE Id */
882 			+ 1		/* CIE version */
883 			+ strlen (augmentation) + 1	/* Augmentation */
884 			+ size_of_uleb128 (1)		/* Code alignment */
885 			+ size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
886 			+ 1		/* RA column */
887 			+ 1		/* Augmentation size */
888 			+ 1		/* Personality encoding */ );
889 	  int pad = -offset & (PTR_SIZE - 1);
890 
891 	  augmentation_size += pad;
892 
893 	  /* Augmentations should be small, so there's scarce need to
894 	     iterate for a solution.  Die if we exceed one uleb128 byte.  */
895 	  gcc_assert (size_of_uleb128 (augmentation_size) == 1);
896 	}
897     }
898 
899   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
900   if (dw_cie_version >= 4)
901     {
902       dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
903       dw2_asm_output_data (1, 0, "CIE Segment Size");
904     }
905   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
906   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
907 			       "CIE Data Alignment Factor");
908 
909   if (dw_cie_version == 1)
910     dw2_asm_output_data (1, return_reg, "CIE RA Column");
911   else
912     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
913 
914   if (augmentation[0])
915     {
916       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
917       if (personality)
918 	{
919 	  dw2_asm_output_data (1, per_encoding, "Personality (%s)",
920 			       eh_data_format_name (per_encoding));
921 	  dw2_asm_output_encoded_addr_rtx (per_encoding,
922 					   personality,
923 					   true, NULL);
924 	}
925 
926       if (any_lsda_needed)
927 	dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
928 			     eh_data_format_name (lsda_encoding));
929 
930       if (fde_encoding != DW_EH_PE_absptr)
931 	dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
932 			     eh_data_format_name (fde_encoding));
933     }
934 
935   FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
936     output_cfi (cfi, NULL, for_eh);
937 
938   /* Pad the CIE out to an address sized boundary.  */
939   ASM_OUTPUT_ALIGN (asm_out_file,
940 		    floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
941   ASM_OUTPUT_LABEL (asm_out_file, l2);
942 
943   /* Loop through all of the FDE's.  */
944   FOR_EACH_VEC_ELT (*fde_vec, i, fde)
945     {
946       unsigned int k;
947 
948       /* Don't emit EH unwind info for leaf functions that don't need it.  */
949       if (for_eh && !fde_needed_for_eh_p (fde))
950 	continue;
951 
952       for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
953 	output_fde (fde, for_eh, k, section_start_label, fde_encoding,
954 		    augmentation, any_lsda_needed, lsda_encoding);
955     }
956 
957   if (for_eh && targetm.terminate_dw2_eh_frame_info)
958     dw2_asm_output_data (4, 0, "End of Table");
959 
960   /* Turn off app to make assembly quicker.  */
961   if (flag_debug_asm)
962     app_disable ();
963 }
964 
965 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed.  */
966 
967 static void
dwarf2out_do_cfi_startproc(bool second)968 dwarf2out_do_cfi_startproc (bool second)
969 {
970   int enc;
971   rtx ref;
972 
973   fprintf (asm_out_file, "\t.cfi_startproc\n");
974 
975   targetm.asm_out.post_cfi_startproc (asm_out_file, current_function_decl);
976 
977   /* .cfi_personality and .cfi_lsda are only relevant to DWARF2
978      eh unwinders.  */
979   if (targetm_common.except_unwind_info (&global_options) != UI_DWARF2)
980     return;
981 
982   rtx personality = get_personality_function (current_function_decl);
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 	{
995 	  if (targetm.asm_out.make_eh_symbol_indirect != NULL)
996 	    ref = targetm.asm_out.make_eh_symbol_indirect (ref, true);
997 	  else
998 	    ref = dw2_force_const_mem (ref, true);
999 	}
1000 
1001       fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
1002       output_addr_const (asm_out_file, ref);
1003       fputc ('\n', asm_out_file);
1004     }
1005 
1006   if (crtl->uses_eh_lsda)
1007     {
1008       char lab[MAX_ARTIFICIAL_LABEL_BYTES];
1009 
1010       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
1011       ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
1012 				   current_function_funcdef_no);
1013       ref = gen_rtx_SYMBOL_REF (Pmode, lab);
1014       SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
1015 
1016       if (enc & DW_EH_PE_indirect)
1017 	{
1018 	  if (targetm.asm_out.make_eh_symbol_indirect != NULL)
1019 	    ref = targetm.asm_out.make_eh_symbol_indirect (ref, true);
1020 	  else
1021 	    ref = dw2_force_const_mem (ref, true);
1022 	}
1023 
1024       fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
1025       output_addr_const (asm_out_file, ref);
1026       fputc ('\n', asm_out_file);
1027     }
1028 }
1029 
1030 /* Allocate CURRENT_FDE.  Immediately initialize all we can, noting that
1031    this allocation may be done before pass_final.  */
1032 
1033 dw_fde_ref
dwarf2out_alloc_current_fde(void)1034 dwarf2out_alloc_current_fde (void)
1035 {
1036   dw_fde_ref fde;
1037 
1038   fde = ggc_cleared_alloc<dw_fde_node> ();
1039   fde->decl = current_function_decl;
1040   fde->funcdef_number = current_function_funcdef_no;
1041   fde->fde_index = vec_safe_length (fde_vec);
1042   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1043   fde->uses_eh_lsda = crtl->uses_eh_lsda;
1044   fde->nothrow = crtl->nothrow;
1045   fde->drap_reg = INVALID_REGNUM;
1046   fde->vdrap_reg = INVALID_REGNUM;
1047 
1048   /* Record the FDE associated with this function.  */
1049   cfun->fde = fde;
1050   vec_safe_push (fde_vec, fde);
1051 
1052   return fde;
1053 }
1054 
1055 /* Output a marker (i.e. a label) for the beginning of a function, before
1056    the prologue.  */
1057 
1058 void
dwarf2out_begin_prologue(unsigned int line ATTRIBUTE_UNUSED,unsigned int column ATTRIBUTE_UNUSED,const char * file ATTRIBUTE_UNUSED)1059 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1060 			  unsigned int column ATTRIBUTE_UNUSED,
1061 			  const char *file ATTRIBUTE_UNUSED)
1062 {
1063   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1064   char * dup_label;
1065   dw_fde_ref fde;
1066   section *fnsec;
1067   bool do_frame;
1068 
1069   current_function_func_begin_label = NULL;
1070 
1071   do_frame = dwarf2out_do_frame ();
1072 
1073   /* ??? current_function_func_begin_label is also used by except.c for
1074      call-site information.  We must emit this label if it might be used.  */
1075   if (!do_frame
1076       && (!flag_exceptions
1077 	  || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1078     return;
1079 
1080   fnsec = function_section (current_function_decl);
1081   switch_to_section (fnsec);
1082   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1083 			       current_function_funcdef_no);
1084   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1085 			  current_function_funcdef_no);
1086   dup_label = xstrdup (label);
1087   current_function_func_begin_label = dup_label;
1088 
1089   /* We can elide FDE allocation if we're not emitting frame unwind info.  */
1090   if (!do_frame)
1091     return;
1092 
1093   /* Unlike the debug version, the EH version of frame unwind info is a per-
1094      function setting so we need to record whether we need it for the unit.  */
1095   do_eh_frame |= dwarf2out_do_eh_frame ();
1096 
1097   /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1098      emit insns as rtx but bypass the bulk of rest_of_compilation, which
1099      would include pass_dwarf2_frame.  If we've not created the FDE yet,
1100      do so now.  */
1101   fde = cfun->fde;
1102   if (fde == NULL)
1103     fde = dwarf2out_alloc_current_fde ();
1104 
1105   /* Initialize the bits of CURRENT_FDE that were not available earlier.  */
1106   fde->dw_fde_begin = dup_label;
1107   fde->dw_fde_current_label = dup_label;
1108   fde->in_std_section = (fnsec == text_section
1109 			 || (cold_text_section && fnsec == cold_text_section));
1110 
1111   /* We only want to output line number information for the genuine dwarf2
1112      prologue case, not the eh frame case.  */
1113 #ifdef DWARF2_DEBUGGING_INFO
1114   if (file)
1115     dwarf2out_source_line (line, column, file, 0, true);
1116 #endif
1117 
1118   if (dwarf2out_do_cfi_asm ())
1119     dwarf2out_do_cfi_startproc (false);
1120   else
1121     {
1122       rtx personality = get_personality_function (current_function_decl);
1123       if (!current_unit_personality)
1124         current_unit_personality = personality;
1125 
1126       /* We cannot keep a current personality per function as without CFI
1127 	 asm, at the point where we emit the CFI data, there is no current
1128 	 function anymore.  */
1129       if (personality && current_unit_personality != personality)
1130 	sorry ("multiple EH personalities are supported only with assemblers "
1131 	       "supporting %<.cfi_personality%> directive");
1132     }
1133 }
1134 
1135 /* Output a marker (i.e. a label) for the end of the generated code
1136    for a function prologue.  This gets called *after* the prologue code has
1137    been generated.  */
1138 
1139 void
dwarf2out_vms_end_prologue(unsigned int line ATTRIBUTE_UNUSED,const char * file ATTRIBUTE_UNUSED)1140 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1141 			    const char *file ATTRIBUTE_UNUSED)
1142 {
1143   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1144 
1145   /* Output a label to mark the endpoint of the code generated for this
1146      function.  */
1147   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1148 			       current_function_funcdef_no);
1149   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1150 			  current_function_funcdef_no);
1151   cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1152 }
1153 
1154 /* Output a marker (i.e. a label) for the beginning of the generated code
1155    for a function epilogue.  This gets called *before* the prologue code has
1156    been generated.  */
1157 
1158 void
dwarf2out_vms_begin_epilogue(unsigned int line ATTRIBUTE_UNUSED,const char * file ATTRIBUTE_UNUSED)1159 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1160 			  const char *file ATTRIBUTE_UNUSED)
1161 {
1162   dw_fde_ref fde = cfun->fde;
1163   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1164 
1165   if (fde->dw_fde_vms_begin_epilogue)
1166     return;
1167 
1168   /* Output a label to mark the endpoint of the code generated for this
1169      function.  */
1170   ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1171 			       current_function_funcdef_no);
1172   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1173 			  current_function_funcdef_no);
1174   fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1175 }
1176 
1177 /* Output a marker (i.e. a label) for the absolute end of the generated code
1178    for a function definition.  This gets called *after* the epilogue code has
1179    been generated.  */
1180 
1181 void
dwarf2out_end_epilogue(unsigned int line ATTRIBUTE_UNUSED,const char * file ATTRIBUTE_UNUSED)1182 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1183 			const char *file ATTRIBUTE_UNUSED)
1184 {
1185   dw_fde_ref fde;
1186   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1187 
1188   last_var_location_insn = NULL;
1189   cached_next_real_insn = NULL;
1190 
1191   if (dwarf2out_do_cfi_asm ())
1192     fprintf (asm_out_file, "\t.cfi_endproc\n");
1193 
1194   /* Output a label to mark the endpoint of the code generated for this
1195      function.  */
1196   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1197 			       current_function_funcdef_no);
1198   ASM_OUTPUT_LABEL (asm_out_file, label);
1199   fde = cfun->fde;
1200   gcc_assert (fde != NULL);
1201   if (fde->dw_fde_second_begin == NULL)
1202     fde->dw_fde_end = xstrdup (label);
1203 }
1204 
1205 void
dwarf2out_frame_finish(void)1206 dwarf2out_frame_finish (void)
1207 {
1208   /* Output call frame information.  */
1209   if (targetm.debug_unwind_info () == UI_DWARF2)
1210     output_call_frame_info (0);
1211 
1212   /* Output another copy for the unwinder.  */
1213   if (do_eh_frame)
1214     output_call_frame_info (1);
1215 }
1216 
1217 /* Note that the current function section is being used for code.  */
1218 
1219 static void
dwarf2out_note_section_used(void)1220 dwarf2out_note_section_used (void)
1221 {
1222   section *sec = current_function_section ();
1223   if (sec == text_section)
1224     text_section_used = true;
1225   else if (sec == cold_text_section)
1226     cold_text_section_used = true;
1227 }
1228 
1229 static void var_location_switch_text_section (void);
1230 static void set_cur_line_info_table (section *);
1231 
1232 void
dwarf2out_switch_text_section(void)1233 dwarf2out_switch_text_section (void)
1234 {
1235   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1236   section *sect;
1237   dw_fde_ref fde = cfun->fde;
1238 
1239   gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1240 
1241   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_SECOND_SECT_LABEL,
1242 			       current_function_funcdef_no);
1243 
1244   fde->dw_fde_second_begin = ggc_strdup (label);
1245   if (!in_cold_section_p)
1246     {
1247       fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1248       fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1249     }
1250   else
1251     {
1252       fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1253       fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1254     }
1255   have_multiple_function_sections = true;
1256 
1257   /* There is no need to mark used sections when not debugging.  */
1258   if (cold_text_section != NULL)
1259     dwarf2out_note_section_used ();
1260 
1261   if (dwarf2out_do_cfi_asm ())
1262     fprintf (asm_out_file, "\t.cfi_endproc\n");
1263 
1264   /* Now do the real section switch.  */
1265   sect = current_function_section ();
1266   switch_to_section (sect);
1267 
1268   fde->second_in_std_section
1269     = (sect == text_section
1270        || (cold_text_section && sect == cold_text_section));
1271 
1272   if (dwarf2out_do_cfi_asm ())
1273     dwarf2out_do_cfi_startproc (true);
1274 
1275   var_location_switch_text_section ();
1276 
1277   if (cold_text_section != NULL)
1278     set_cur_line_info_table (sect);
1279 }
1280 
1281 /* And now, the subset of the debugging information support code necessary
1282    for emitting location expressions.  */
1283 
1284 /* Data about a single source file.  */
1285 struct GTY((for_user)) dwarf_file_data {
1286   const char * filename;
1287   int emitted_number;
1288 };
1289 
1290 /* Describe an entry into the .debug_addr section.  */
1291 
1292 enum ate_kind {
1293   ate_kind_rtx,
1294   ate_kind_rtx_dtprel,
1295   ate_kind_label
1296 };
1297 
1298 struct GTY((for_user)) addr_table_entry {
1299   enum ate_kind kind;
1300   unsigned int refcount;
1301   unsigned int index;
1302   union addr_table_entry_struct_union
1303     {
1304       rtx GTY ((tag ("0"))) rtl;
1305       char * GTY ((tag ("1"))) label;
1306     }
1307   GTY ((desc ("%1.kind"))) addr;
1308 };
1309 
1310 typedef unsigned int var_loc_view;
1311 
1312 /* Location lists are ranges + location descriptions for that range,
1313    so you can track variables that are in different places over
1314    their entire life.  */
1315 typedef struct GTY(()) dw_loc_list_struct {
1316   dw_loc_list_ref dw_loc_next;
1317   const char *begin; /* Label and addr_entry for start of range */
1318   addr_table_entry *begin_entry;
1319   const char *end;  /* Label for end of range */
1320   addr_table_entry *end_entry;
1321   char *ll_symbol; /* Label for beginning of location list.
1322 		      Only on head of list.  */
1323   char *vl_symbol; /* Label for beginning of view list.  Ditto.  */
1324   const char *section; /* Section this loclist is relative to */
1325   dw_loc_descr_ref expr;
1326   var_loc_view vbegin, vend;
1327   hashval_t hash;
1328   /* True if all addresses in this and subsequent lists are known to be
1329      resolved.  */
1330   bool resolved_addr;
1331   /* True if this list has been replaced by dw_loc_next.  */
1332   bool replaced;
1333   /* True if it has been emitted into .debug_loc* / .debug_loclists*
1334      section.  */
1335   unsigned char emitted : 1;
1336   /* True if hash field is index rather than hash value.  */
1337   unsigned char num_assigned : 1;
1338   /* True if .debug_loclists.dwo offset has been emitted for it already.  */
1339   unsigned char offset_emitted : 1;
1340   /* True if note_variable_value_in_expr has been called on it.  */
1341   unsigned char noted_variable_value : 1;
1342   /* True if the range should be emitted even if begin and end
1343      are the same.  */
1344   bool force;
1345 } dw_loc_list_node;
1346 
1347 static dw_loc_descr_ref int_loc_descriptor (poly_int64);
1348 static dw_loc_descr_ref uint_loc_descriptor (unsigned HOST_WIDE_INT);
1349 
1350 /* Convert a DWARF stack opcode into its string name.  */
1351 
1352 static const char *
dwarf_stack_op_name(unsigned int op)1353 dwarf_stack_op_name (unsigned int op)
1354 {
1355   const char *name = get_DW_OP_name (op);
1356 
1357   if (name != NULL)
1358     return name;
1359 
1360   return "OP_<unknown>";
1361 }
1362 
1363 /* Return TRUE iff we're to output location view lists as a separate
1364    attribute next to the location lists, as an extension compatible
1365    with DWARF 2 and above.  */
1366 
1367 static inline bool
dwarf2out_locviews_in_attribute()1368 dwarf2out_locviews_in_attribute ()
1369 {
1370   return debug_variable_location_views == 1;
1371 }
1372 
1373 /* Return TRUE iff we're to output location view lists as part of the
1374    location lists, as proposed for standardization after DWARF 5.  */
1375 
1376 static inline bool
dwarf2out_locviews_in_loclist()1377 dwarf2out_locviews_in_loclist ()
1378 {
1379 #ifndef DW_LLE_view_pair
1380   return false;
1381 #else
1382   return debug_variable_location_views == -1;
1383 #endif
1384 }
1385 
1386 /* Return a pointer to a newly allocated location description.  Location
1387    descriptions are simple expression terms that can be strung
1388    together to form more complicated location (address) descriptions.  */
1389 
1390 static inline dw_loc_descr_ref
new_loc_descr(enum dwarf_location_atom op,unsigned HOST_WIDE_INT oprnd1,unsigned HOST_WIDE_INT oprnd2)1391 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1392 	       unsigned HOST_WIDE_INT oprnd2)
1393 {
1394   dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1395 
1396   descr->dw_loc_opc = op;
1397   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1398   descr->dw_loc_oprnd1.val_entry = NULL;
1399   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1400   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1401   descr->dw_loc_oprnd2.val_entry = NULL;
1402   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1403 
1404   return descr;
1405 }
1406 
1407 /* Add a location description term to a location description expression.  */
1408 
1409 static inline void
add_loc_descr(dw_loc_descr_ref * list_head,dw_loc_descr_ref descr)1410 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1411 {
1412   dw_loc_descr_ref *d;
1413 
1414   /* Find the end of the chain.  */
1415   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1416     ;
1417 
1418   *d = descr;
1419 }
1420 
1421 /* Compare two location operands for exact equality.  */
1422 
1423 static bool
dw_val_equal_p(dw_val_node * a,dw_val_node * b)1424 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1425 {
1426   if (a->val_class != b->val_class)
1427     return false;
1428   switch (a->val_class)
1429     {
1430     case dw_val_class_none:
1431       return true;
1432     case dw_val_class_addr:
1433       return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1434 
1435     case dw_val_class_offset:
1436     case dw_val_class_unsigned_const:
1437     case dw_val_class_const:
1438     case dw_val_class_unsigned_const_implicit:
1439     case dw_val_class_const_implicit:
1440     case dw_val_class_range_list:
1441       /* These are all HOST_WIDE_INT, signed or unsigned.  */
1442       return a->v.val_unsigned == b->v.val_unsigned;
1443 
1444     case dw_val_class_loc:
1445       return a->v.val_loc == b->v.val_loc;
1446     case dw_val_class_loc_list:
1447       return a->v.val_loc_list == b->v.val_loc_list;
1448     case dw_val_class_view_list:
1449       return a->v.val_view_list == b->v.val_view_list;
1450     case dw_val_class_die_ref:
1451       return a->v.val_die_ref.die == b->v.val_die_ref.die;
1452     case dw_val_class_fde_ref:
1453       return a->v.val_fde_index == b->v.val_fde_index;
1454     case dw_val_class_symview:
1455       return strcmp (a->v.val_symbolic_view, b->v.val_symbolic_view) == 0;
1456     case dw_val_class_lbl_id:
1457     case dw_val_class_lineptr:
1458     case dw_val_class_macptr:
1459     case dw_val_class_loclistsptr:
1460     case dw_val_class_high_pc:
1461       return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1462     case dw_val_class_str:
1463       return a->v.val_str == b->v.val_str;
1464     case dw_val_class_flag:
1465       return a->v.val_flag == b->v.val_flag;
1466     case dw_val_class_file:
1467     case dw_val_class_file_implicit:
1468       return a->v.val_file == b->v.val_file;
1469     case dw_val_class_decl_ref:
1470       return a->v.val_decl_ref == b->v.val_decl_ref;
1471 
1472     case dw_val_class_const_double:
1473       return (a->v.val_double.high == b->v.val_double.high
1474 	      && a->v.val_double.low == b->v.val_double.low);
1475 
1476     case dw_val_class_wide_int:
1477       return *a->v.val_wide == *b->v.val_wide;
1478 
1479     case dw_val_class_vec:
1480       {
1481 	size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1482 	size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1483 
1484 	return (a_len == b_len
1485 		&& !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1486       }
1487 
1488     case dw_val_class_data8:
1489       return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1490 
1491     case dw_val_class_vms_delta:
1492       return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1493 	      && !strcmp (a->v.val_vms_delta.lbl2, b->v.val_vms_delta.lbl2));
1494 
1495     case dw_val_class_discr_value:
1496       return (a->v.val_discr_value.pos == b->v.val_discr_value.pos
1497 	      && a->v.val_discr_value.v.uval == b->v.val_discr_value.v.uval);
1498     case dw_val_class_discr_list:
1499       /* It makes no sense comparing two discriminant value lists.  */
1500       return false;
1501     }
1502   gcc_unreachable ();
1503 }
1504 
1505 /* Compare two location atoms for exact equality.  */
1506 
1507 static bool
loc_descr_equal_p_1(dw_loc_descr_ref a,dw_loc_descr_ref b)1508 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1509 {
1510   if (a->dw_loc_opc != b->dw_loc_opc)
1511     return false;
1512 
1513   /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1514      address size, but since we always allocate cleared storage it
1515      should be zero for other types of locations.  */
1516   if (a->dtprel != b->dtprel)
1517     return false;
1518 
1519   return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1520 	  && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1521 }
1522 
1523 /* Compare two complete location expressions for exact equality.  */
1524 
1525 bool
loc_descr_equal_p(dw_loc_descr_ref a,dw_loc_descr_ref b)1526 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1527 {
1528   while (1)
1529     {
1530       if (a == b)
1531 	return true;
1532       if (a == NULL || b == NULL)
1533 	return false;
1534       if (!loc_descr_equal_p_1 (a, b))
1535 	return false;
1536 
1537       a = a->dw_loc_next;
1538       b = b->dw_loc_next;
1539     }
1540 }
1541 
1542 
1543 /* Add a constant POLY_OFFSET to a location expression.  */
1544 
1545 static void
loc_descr_plus_const(dw_loc_descr_ref * list_head,poly_int64 poly_offset)1546 loc_descr_plus_const (dw_loc_descr_ref *list_head, poly_int64 poly_offset)
1547 {
1548   dw_loc_descr_ref loc;
1549   HOST_WIDE_INT *p;
1550 
1551   gcc_assert (*list_head != NULL);
1552 
1553   if (known_eq (poly_offset, 0))
1554     return;
1555 
1556   /* Find the end of the chain.  */
1557   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1558     ;
1559 
1560   HOST_WIDE_INT offset;
1561   if (!poly_offset.is_constant (&offset))
1562     {
1563       loc->dw_loc_next = int_loc_descriptor (poly_offset);
1564       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_plus, 0, 0));
1565       return;
1566     }
1567 
1568   p = NULL;
1569   if (loc->dw_loc_opc == DW_OP_fbreg
1570       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1571     p = &loc->dw_loc_oprnd1.v.val_int;
1572   else if (loc->dw_loc_opc == DW_OP_bregx)
1573     p = &loc->dw_loc_oprnd2.v.val_int;
1574 
1575   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1576      offset.  Don't optimize if an signed integer overflow would happen.  */
1577   if (p != NULL
1578       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1579 	  || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1580     *p += offset;
1581 
1582   else if (offset > 0)
1583     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1584 
1585   else
1586     {
1587       loc->dw_loc_next
1588 	= uint_loc_descriptor (-(unsigned HOST_WIDE_INT) offset);
1589       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1590     }
1591 }
1592 
1593 /* Return a pointer to a newly allocated location description for
1594    REG and OFFSET.  */
1595 
1596 static inline dw_loc_descr_ref
new_reg_loc_descr(unsigned int reg,poly_int64 offset)1597 new_reg_loc_descr (unsigned int reg, poly_int64 offset)
1598 {
1599   HOST_WIDE_INT const_offset;
1600   if (offset.is_constant (&const_offset))
1601     {
1602       if (reg <= 31)
1603 	return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1604 			      const_offset, 0);
1605       else
1606 	return new_loc_descr (DW_OP_bregx, reg, const_offset);
1607     }
1608   else
1609     {
1610       dw_loc_descr_ref ret = new_reg_loc_descr (reg, 0);
1611       loc_descr_plus_const (&ret, offset);
1612       return ret;
1613     }
1614 }
1615 
1616 /* Add a constant OFFSET to a location list.  */
1617 
1618 static void
loc_list_plus_const(dw_loc_list_ref list_head,poly_int64 offset)1619 loc_list_plus_const (dw_loc_list_ref list_head, poly_int64 offset)
1620 {
1621   dw_loc_list_ref d;
1622   for (d = list_head; d != NULL; d = d->dw_loc_next)
1623     loc_descr_plus_const (&d->expr, offset);
1624 }
1625 
1626 #define DWARF_REF_SIZE	\
1627   (dwarf_version == 2 ? DWARF2_ADDR_SIZE : dwarf_offset_size)
1628 
1629 /* The number of bits that can be encoded by largest DW_FORM_dataN.
1630    In DWARF4 and earlier it is DW_FORM_data8 with 64 bits, in DWARF5
1631    DW_FORM_data16 with 128 bits.  */
1632 #define DWARF_LARGEST_DATA_FORM_BITS \
1633   (dwarf_version >= 5 ? 128 : 64)
1634 
1635 /* Utility inline function for construction of ops that were GNU extension
1636    before DWARF 5.  */
1637 static inline enum dwarf_location_atom
dwarf_OP(enum dwarf_location_atom op)1638 dwarf_OP (enum dwarf_location_atom op)
1639 {
1640   switch (op)
1641     {
1642     case DW_OP_implicit_pointer:
1643       if (dwarf_version < 5)
1644 	return DW_OP_GNU_implicit_pointer;
1645       break;
1646 
1647     case DW_OP_entry_value:
1648       if (dwarf_version < 5)
1649 	return DW_OP_GNU_entry_value;
1650       break;
1651 
1652     case DW_OP_const_type:
1653       if (dwarf_version < 5)
1654 	return DW_OP_GNU_const_type;
1655       break;
1656 
1657     case DW_OP_regval_type:
1658       if (dwarf_version < 5)
1659 	return DW_OP_GNU_regval_type;
1660       break;
1661 
1662     case DW_OP_deref_type:
1663       if (dwarf_version < 5)
1664 	return DW_OP_GNU_deref_type;
1665       break;
1666 
1667     case DW_OP_convert:
1668       if (dwarf_version < 5)
1669 	return DW_OP_GNU_convert;
1670       break;
1671 
1672     case DW_OP_reinterpret:
1673       if (dwarf_version < 5)
1674 	return DW_OP_GNU_reinterpret;
1675       break;
1676 
1677     case DW_OP_addrx:
1678       if (dwarf_version < 5)
1679 	return DW_OP_GNU_addr_index;
1680       break;
1681 
1682     case DW_OP_constx:
1683       if (dwarf_version < 5)
1684 	return DW_OP_GNU_const_index;
1685       break;
1686 
1687     default:
1688       break;
1689     }
1690   return op;
1691 }
1692 
1693 /* Similarly for attributes.  */
1694 static inline enum dwarf_attribute
dwarf_AT(enum dwarf_attribute at)1695 dwarf_AT (enum dwarf_attribute at)
1696 {
1697   switch (at)
1698     {
1699     case DW_AT_call_return_pc:
1700       if (dwarf_version < 5)
1701 	return DW_AT_low_pc;
1702       break;
1703 
1704     case DW_AT_call_tail_call:
1705       if (dwarf_version < 5)
1706 	return DW_AT_GNU_tail_call;
1707       break;
1708 
1709     case DW_AT_call_origin:
1710       if (dwarf_version < 5)
1711 	return DW_AT_abstract_origin;
1712       break;
1713 
1714     case DW_AT_call_target:
1715       if (dwarf_version < 5)
1716 	return DW_AT_GNU_call_site_target;
1717       break;
1718 
1719     case DW_AT_call_target_clobbered:
1720       if (dwarf_version < 5)
1721 	return DW_AT_GNU_call_site_target_clobbered;
1722       break;
1723 
1724     case DW_AT_call_parameter:
1725       if (dwarf_version < 5)
1726 	return DW_AT_abstract_origin;
1727       break;
1728 
1729     case DW_AT_call_value:
1730       if (dwarf_version < 5)
1731 	return DW_AT_GNU_call_site_value;
1732       break;
1733 
1734     case DW_AT_call_data_value:
1735       if (dwarf_version < 5)
1736 	return DW_AT_GNU_call_site_data_value;
1737       break;
1738 
1739     case DW_AT_call_all_calls:
1740       if (dwarf_version < 5)
1741 	return DW_AT_GNU_all_call_sites;
1742       break;
1743 
1744     case DW_AT_call_all_tail_calls:
1745       if (dwarf_version < 5)
1746 	return DW_AT_GNU_all_tail_call_sites;
1747       break;
1748 
1749     case DW_AT_dwo_name:
1750       if (dwarf_version < 5)
1751 	return DW_AT_GNU_dwo_name;
1752       break;
1753 
1754     case DW_AT_addr_base:
1755       if (dwarf_version < 5)
1756 	return DW_AT_GNU_addr_base;
1757       break;
1758 
1759     default:
1760       break;
1761     }
1762   return at;
1763 }
1764 
1765 /* And similarly for tags.  */
1766 static inline enum dwarf_tag
dwarf_TAG(enum dwarf_tag tag)1767 dwarf_TAG (enum dwarf_tag tag)
1768 {
1769   switch (tag)
1770     {
1771     case DW_TAG_call_site:
1772       if (dwarf_version < 5)
1773 	return DW_TAG_GNU_call_site;
1774       break;
1775 
1776     case DW_TAG_call_site_parameter:
1777       if (dwarf_version < 5)
1778 	return DW_TAG_GNU_call_site_parameter;
1779       break;
1780 
1781     default:
1782       break;
1783     }
1784   return tag;
1785 }
1786 
1787 /* And similarly for forms.  */
1788 static inline enum dwarf_form
dwarf_FORM(enum dwarf_form form)1789 dwarf_FORM (enum dwarf_form form)
1790 {
1791   switch (form)
1792     {
1793     case DW_FORM_addrx:
1794       if (dwarf_version < 5)
1795 	return DW_FORM_GNU_addr_index;
1796       break;
1797 
1798     case DW_FORM_strx:
1799       if (dwarf_version < 5)
1800 	return DW_FORM_GNU_str_index;
1801       break;
1802 
1803     default:
1804       break;
1805     }
1806   return form;
1807 }
1808 
1809 static unsigned long int get_base_type_offset (dw_die_ref);
1810 
1811 /* Return the size of a location descriptor.  */
1812 
1813 static unsigned long
size_of_loc_descr(dw_loc_descr_ref loc)1814 size_of_loc_descr (dw_loc_descr_ref loc)
1815 {
1816   unsigned long size = 1;
1817 
1818   switch (loc->dw_loc_opc)
1819     {
1820     case DW_OP_addr:
1821       size += DWARF2_ADDR_SIZE;
1822       break;
1823     case DW_OP_GNU_addr_index:
1824     case DW_OP_addrx:
1825     case DW_OP_GNU_const_index:
1826     case DW_OP_constx:
1827       gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1828       size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1829       break;
1830     case DW_OP_const1u:
1831     case DW_OP_const1s:
1832       size += 1;
1833       break;
1834     case DW_OP_const2u:
1835     case DW_OP_const2s:
1836       size += 2;
1837       break;
1838     case DW_OP_const4u:
1839     case DW_OP_const4s:
1840       size += 4;
1841       break;
1842     case DW_OP_const8u:
1843     case DW_OP_const8s:
1844       size += 8;
1845       break;
1846     case DW_OP_constu:
1847       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1848       break;
1849     case DW_OP_consts:
1850       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1851       break;
1852     case DW_OP_pick:
1853       size += 1;
1854       break;
1855     case DW_OP_plus_uconst:
1856       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1857       break;
1858     case DW_OP_skip:
1859     case DW_OP_bra:
1860       size += 2;
1861       break;
1862     case DW_OP_breg0:
1863     case DW_OP_breg1:
1864     case DW_OP_breg2:
1865     case DW_OP_breg3:
1866     case DW_OP_breg4:
1867     case DW_OP_breg5:
1868     case DW_OP_breg6:
1869     case DW_OP_breg7:
1870     case DW_OP_breg8:
1871     case DW_OP_breg9:
1872     case DW_OP_breg10:
1873     case DW_OP_breg11:
1874     case DW_OP_breg12:
1875     case DW_OP_breg13:
1876     case DW_OP_breg14:
1877     case DW_OP_breg15:
1878     case DW_OP_breg16:
1879     case DW_OP_breg17:
1880     case DW_OP_breg18:
1881     case DW_OP_breg19:
1882     case DW_OP_breg20:
1883     case DW_OP_breg21:
1884     case DW_OP_breg22:
1885     case DW_OP_breg23:
1886     case DW_OP_breg24:
1887     case DW_OP_breg25:
1888     case DW_OP_breg26:
1889     case DW_OP_breg27:
1890     case DW_OP_breg28:
1891     case DW_OP_breg29:
1892     case DW_OP_breg30:
1893     case DW_OP_breg31:
1894       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1895       break;
1896     case DW_OP_regx:
1897       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1898       break;
1899     case DW_OP_fbreg:
1900       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1901       break;
1902     case DW_OP_bregx:
1903       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1904       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1905       break;
1906     case DW_OP_piece:
1907       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1908       break;
1909     case DW_OP_bit_piece:
1910       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1911       size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1912       break;
1913     case DW_OP_deref_size:
1914     case DW_OP_xderef_size:
1915       size += 1;
1916       break;
1917     case DW_OP_call2:
1918       size += 2;
1919       break;
1920     case DW_OP_call4:
1921       size += 4;
1922       break;
1923     case DW_OP_call_ref:
1924     case DW_OP_GNU_variable_value:
1925       size += DWARF_REF_SIZE;
1926       break;
1927     case DW_OP_implicit_value:
1928       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1929 	      + loc->dw_loc_oprnd1.v.val_unsigned;
1930       break;
1931     case DW_OP_implicit_pointer:
1932     case DW_OP_GNU_implicit_pointer:
1933       size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1934       break;
1935     case DW_OP_entry_value:
1936     case DW_OP_GNU_entry_value:
1937       {
1938 	unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1939 	size += size_of_uleb128 (op_size) + op_size;
1940 	break;
1941       }
1942     case DW_OP_const_type:
1943     case DW_OP_GNU_const_type:
1944       {
1945 	unsigned long o
1946 	  = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1947 	size += size_of_uleb128 (o) + 1;
1948 	switch (loc->dw_loc_oprnd2.val_class)
1949 	  {
1950 	  case dw_val_class_vec:
1951 	    size += loc->dw_loc_oprnd2.v.val_vec.length
1952 		    * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1953 	    break;
1954 	  case dw_val_class_const:
1955 	    size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1956 	    break;
1957 	  case dw_val_class_const_double:
1958 	    size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1959 	    break;
1960 	  case dw_val_class_wide_int:
1961 	    size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1962 		     * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1963 	    break;
1964 	  default:
1965 	    gcc_unreachable ();
1966 	  }
1967 	break;
1968       }
1969     case DW_OP_regval_type:
1970     case DW_OP_GNU_regval_type:
1971       {
1972 	unsigned long o
1973 	  = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1974 	size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1975 		+ size_of_uleb128 (o);
1976       }
1977       break;
1978     case DW_OP_deref_type:
1979     case DW_OP_GNU_deref_type:
1980       {
1981 	unsigned long o
1982 	  = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1983 	size += 1 + size_of_uleb128 (o);
1984       }
1985       break;
1986     case DW_OP_convert:
1987     case DW_OP_reinterpret:
1988     case DW_OP_GNU_convert:
1989     case DW_OP_GNU_reinterpret:
1990       if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1991 	size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1992       else
1993 	{
1994 	  unsigned long o
1995 	    = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1996 	  size += size_of_uleb128 (o);
1997 	}
1998       break;
1999     case DW_OP_GNU_parameter_ref:
2000       size += 4;
2001       break;
2002     default:
2003       break;
2004     }
2005 
2006   return size;
2007 }
2008 
2009 /* Return the size of a series of location descriptors.  */
2010 
2011 unsigned long
size_of_locs(dw_loc_descr_ref loc)2012 size_of_locs (dw_loc_descr_ref loc)
2013 {
2014   dw_loc_descr_ref l;
2015   unsigned long size;
2016 
2017   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
2018      field, to avoid writing to a PCH file.  */
2019   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
2020     {
2021       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
2022 	break;
2023       size += size_of_loc_descr (l);
2024     }
2025   if (! l)
2026     return size;
2027 
2028   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
2029     {
2030       l->dw_loc_addr = size;
2031       size += size_of_loc_descr (l);
2032     }
2033 
2034   return size;
2035 }
2036 
2037 /* Return the size of the value in a DW_AT_discr_value attribute.  */
2038 
2039 static int
size_of_discr_value(dw_discr_value * discr_value)2040 size_of_discr_value (dw_discr_value *discr_value)
2041 {
2042   if (discr_value->pos)
2043     return size_of_uleb128 (discr_value->v.uval);
2044   else
2045     return size_of_sleb128 (discr_value->v.sval);
2046 }
2047 
2048 /* Return the size of the value in a DW_AT_discr_list attribute.  */
2049 
2050 static int
size_of_discr_list(dw_discr_list_ref discr_list)2051 size_of_discr_list (dw_discr_list_ref discr_list)
2052 {
2053   int size = 0;
2054 
2055   for (dw_discr_list_ref list = discr_list;
2056        list != NULL;
2057        list = list->dw_discr_next)
2058     {
2059       /* One byte for the discriminant value descriptor, and then one or two
2060 	 LEB128 numbers, depending on whether it's a single case label or a
2061 	 range label.  */
2062       size += 1;
2063       size += size_of_discr_value (&list->dw_discr_lower_bound);
2064       if (list->dw_discr_range != 0)
2065 	size += size_of_discr_value (&list->dw_discr_upper_bound);
2066     }
2067   return size;
2068 }
2069 
2070 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
2071 static void get_ref_die_offset_label (char *, dw_die_ref);
2072 static unsigned long int get_ref_die_offset (dw_die_ref);
2073 
2074 /* Output location description stack opcode's operands (if any).
2075    The for_eh_or_skip parameter controls whether register numbers are
2076    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2077    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2078    info).  This should be suppressed for the cases that have not been converted
2079    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
2080 
2081 static void
output_loc_operands(dw_loc_descr_ref loc,int for_eh_or_skip)2082 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
2083 {
2084   dw_val_ref val1 = &loc->dw_loc_oprnd1;
2085   dw_val_ref val2 = &loc->dw_loc_oprnd2;
2086 
2087   switch (loc->dw_loc_opc)
2088     {
2089 #ifdef DWARF2_DEBUGGING_INFO
2090     case DW_OP_const2u:
2091     case DW_OP_const2s:
2092       dw2_asm_output_data (2, val1->v.val_int, NULL);
2093       break;
2094     case DW_OP_const4u:
2095       if (loc->dtprel)
2096 	{
2097 	  gcc_assert (targetm.asm_out.output_dwarf_dtprel);
2098 	  targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
2099 					       val1->v.val_addr);
2100 	  fputc ('\n', asm_out_file);
2101 	  break;
2102 	}
2103       /* FALLTHRU */
2104     case DW_OP_const4s:
2105       dw2_asm_output_data (4, val1->v.val_int, NULL);
2106       break;
2107     case DW_OP_const8u:
2108       if (loc->dtprel)
2109 	{
2110 	  gcc_assert (targetm.asm_out.output_dwarf_dtprel);
2111 	  targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
2112 					       val1->v.val_addr);
2113 	  fputc ('\n', asm_out_file);
2114 	  break;
2115 	}
2116       /* FALLTHRU */
2117     case DW_OP_const8s:
2118       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2119       dw2_asm_output_data (8, val1->v.val_int, NULL);
2120       break;
2121     case DW_OP_skip:
2122     case DW_OP_bra:
2123       {
2124 	int offset;
2125 
2126 	gcc_assert (val1->val_class == dw_val_class_loc);
2127 	offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2128 
2129 	dw2_asm_output_data (2, offset, NULL);
2130       }
2131       break;
2132     case DW_OP_implicit_value:
2133       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2134       switch (val2->val_class)
2135 	{
2136 	case dw_val_class_const:
2137 	  dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
2138 	  break;
2139 	case dw_val_class_vec:
2140 	  {
2141 	    unsigned int elt_size = val2->v.val_vec.elt_size;
2142 	    unsigned int len = val2->v.val_vec.length;
2143 	    unsigned int i;
2144 	    unsigned char *p;
2145 
2146 	    if (elt_size > sizeof (HOST_WIDE_INT))
2147 	      {
2148 		elt_size /= 2;
2149 		len *= 2;
2150 	      }
2151 	    for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2152 		 i < len;
2153 		 i++, p += elt_size)
2154 	      dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2155 				   "fp or vector constant word %u", i);
2156 	  }
2157 	  break;
2158 	case dw_val_class_const_double:
2159 	  {
2160 	    unsigned HOST_WIDE_INT first, second;
2161 
2162 	    if (WORDS_BIG_ENDIAN)
2163 	      {
2164 		first = val2->v.val_double.high;
2165 		second = val2->v.val_double.low;
2166 	      }
2167 	    else
2168 	      {
2169 		first = val2->v.val_double.low;
2170 		second = val2->v.val_double.high;
2171 	      }
2172 	    dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2173 				 first, NULL);
2174 	    dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2175 				 second, NULL);
2176 	  }
2177 	  break;
2178 	case dw_val_class_wide_int:
2179 	  {
2180 	    int i;
2181 	    int len = get_full_len (*val2->v.val_wide);
2182 	    if (WORDS_BIG_ENDIAN)
2183 	      for (i = len - 1; i >= 0; --i)
2184 		dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2185 				     val2->v.val_wide->elt (i), NULL);
2186 	    else
2187 	      for (i = 0; i < len; ++i)
2188 		dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2189 				     val2->v.val_wide->elt (i), NULL);
2190 	  }
2191 	  break;
2192 	case dw_val_class_addr:
2193 	  gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
2194 	  dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
2195 	  break;
2196 	default:
2197 	  gcc_unreachable ();
2198 	}
2199       break;
2200 #else
2201     case DW_OP_const2u:
2202     case DW_OP_const2s:
2203     case DW_OP_const4u:
2204     case DW_OP_const4s:
2205     case DW_OP_const8u:
2206     case DW_OP_const8s:
2207     case DW_OP_skip:
2208     case DW_OP_bra:
2209     case DW_OP_implicit_value:
2210       /* We currently don't make any attempt to make sure these are
2211 	 aligned properly like we do for the main unwind info, so
2212 	 don't support emitting things larger than a byte if we're
2213 	 only doing unwinding.  */
2214       gcc_unreachable ();
2215 #endif
2216     case DW_OP_const1u:
2217     case DW_OP_const1s:
2218       dw2_asm_output_data (1, val1->v.val_int, NULL);
2219       break;
2220     case DW_OP_constu:
2221       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2222       break;
2223     case DW_OP_consts:
2224       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2225       break;
2226     case DW_OP_pick:
2227       dw2_asm_output_data (1, val1->v.val_int, NULL);
2228       break;
2229     case DW_OP_plus_uconst:
2230       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2231       break;
2232     case DW_OP_breg0:
2233     case DW_OP_breg1:
2234     case DW_OP_breg2:
2235     case DW_OP_breg3:
2236     case DW_OP_breg4:
2237     case DW_OP_breg5:
2238     case DW_OP_breg6:
2239     case DW_OP_breg7:
2240     case DW_OP_breg8:
2241     case DW_OP_breg9:
2242     case DW_OP_breg10:
2243     case DW_OP_breg11:
2244     case DW_OP_breg12:
2245     case DW_OP_breg13:
2246     case DW_OP_breg14:
2247     case DW_OP_breg15:
2248     case DW_OP_breg16:
2249     case DW_OP_breg17:
2250     case DW_OP_breg18:
2251     case DW_OP_breg19:
2252     case DW_OP_breg20:
2253     case DW_OP_breg21:
2254     case DW_OP_breg22:
2255     case DW_OP_breg23:
2256     case DW_OP_breg24:
2257     case DW_OP_breg25:
2258     case DW_OP_breg26:
2259     case DW_OP_breg27:
2260     case DW_OP_breg28:
2261     case DW_OP_breg29:
2262     case DW_OP_breg30:
2263     case DW_OP_breg31:
2264       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2265       break;
2266     case DW_OP_regx:
2267       {
2268 	unsigned r = val1->v.val_unsigned;
2269 	if (for_eh_or_skip >= 0)
2270 	  r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2271 	gcc_assert (size_of_uleb128 (r)
2272 		    == size_of_uleb128 (val1->v.val_unsigned));
2273 	dw2_asm_output_data_uleb128 (r, NULL);
2274       }
2275       break;
2276     case DW_OP_fbreg:
2277       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2278       break;
2279     case DW_OP_bregx:
2280       {
2281 	unsigned r = val1->v.val_unsigned;
2282 	if (for_eh_or_skip >= 0)
2283 	  r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2284 	gcc_assert (size_of_uleb128 (r)
2285 		    == size_of_uleb128 (val1->v.val_unsigned));
2286 	dw2_asm_output_data_uleb128 (r, NULL);
2287 	dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2288       }
2289       break;
2290     case DW_OP_piece:
2291       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2292       break;
2293     case DW_OP_bit_piece:
2294       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2295       dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
2296       break;
2297     case DW_OP_deref_size:
2298     case DW_OP_xderef_size:
2299       dw2_asm_output_data (1, val1->v.val_int, NULL);
2300       break;
2301 
2302     case DW_OP_addr:
2303       if (loc->dtprel)
2304 	{
2305 	  if (targetm.asm_out.output_dwarf_dtprel)
2306 	    {
2307 	      targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2308 						   DWARF2_ADDR_SIZE,
2309 						   val1->v.val_addr);
2310 	      fputc ('\n', asm_out_file);
2311 	    }
2312 	  else
2313 	    gcc_unreachable ();
2314 	}
2315       else
2316 	{
2317 #ifdef DWARF2_DEBUGGING_INFO
2318 	  dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2319 #else
2320 	  gcc_unreachable ();
2321 #endif
2322 	}
2323       break;
2324 
2325     case DW_OP_GNU_addr_index:
2326     case DW_OP_addrx:
2327     case DW_OP_GNU_const_index:
2328     case DW_OP_constx:
2329       gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2330       dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2331                                    "(index into .debug_addr)");
2332       break;
2333 
2334     case DW_OP_call2:
2335     case DW_OP_call4:
2336       {
2337 	unsigned long die_offset
2338 	  = get_ref_die_offset (val1->v.val_die_ref.die);
2339 	/* Make sure the offset has been computed and that we can encode it as
2340 	   an operand.  */
2341 	gcc_assert (die_offset > 0
2342 		    && die_offset <= (loc->dw_loc_opc == DW_OP_call2
2343 				     ? 0xffff
2344 				     : 0xffffffff));
2345 	dw2_asm_output_data ((loc->dw_loc_opc == DW_OP_call2) ? 2 : 4,
2346 			     die_offset, NULL);
2347       }
2348       break;
2349 
2350     case DW_OP_call_ref:
2351     case DW_OP_GNU_variable_value:
2352       {
2353 	char label[MAX_ARTIFICIAL_LABEL_BYTES
2354 		   + HOST_BITS_PER_WIDE_INT / 2 + 2];
2355 	gcc_assert (val1->val_class == dw_val_class_die_ref);
2356 	get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2357 	dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2358       }
2359       break;
2360 
2361     case DW_OP_implicit_pointer:
2362     case DW_OP_GNU_implicit_pointer:
2363       {
2364 	char label[MAX_ARTIFICIAL_LABEL_BYTES
2365 		   + HOST_BITS_PER_WIDE_INT / 2 + 2];
2366 	gcc_assert (val1->val_class == dw_val_class_die_ref);
2367 	get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2368 	dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2369 	dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2370       }
2371       break;
2372 
2373     case DW_OP_entry_value:
2374     case DW_OP_GNU_entry_value:
2375       dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2376       output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2377       break;
2378 
2379     case DW_OP_const_type:
2380     case DW_OP_GNU_const_type:
2381       {
2382 	unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2383 	gcc_assert (o);
2384 	dw2_asm_output_data_uleb128 (o, NULL);
2385 	switch (val2->val_class)
2386 	  {
2387 	  case dw_val_class_const:
2388 	    l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2389 	    dw2_asm_output_data (1, l, NULL);
2390 	    dw2_asm_output_data (l, val2->v.val_int, NULL);
2391 	    break;
2392 	  case dw_val_class_vec:
2393 	    {
2394 	      unsigned int elt_size = val2->v.val_vec.elt_size;
2395 	      unsigned int len = val2->v.val_vec.length;
2396 	      unsigned int i;
2397 	      unsigned char *p;
2398 
2399 	      l = len * elt_size;
2400 	      dw2_asm_output_data (1, l, NULL);
2401 	      if (elt_size > sizeof (HOST_WIDE_INT))
2402 		{
2403 		  elt_size /= 2;
2404 		  len *= 2;
2405 		}
2406 	      for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2407 		   i < len;
2408 		   i++, p += elt_size)
2409 		dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2410 				     "fp or vector constant word %u", i);
2411 	    }
2412 	    break;
2413 	  case dw_val_class_const_double:
2414 	    {
2415 	      unsigned HOST_WIDE_INT first, second;
2416 	      l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2417 
2418 	      dw2_asm_output_data (1, 2 * l, NULL);
2419 	      if (WORDS_BIG_ENDIAN)
2420 		{
2421 		  first = val2->v.val_double.high;
2422 		  second = val2->v.val_double.low;
2423 		}
2424 	      else
2425 		{
2426 		  first = val2->v.val_double.low;
2427 		  second = val2->v.val_double.high;
2428 		}
2429 	      dw2_asm_output_data (l, first, NULL);
2430 	      dw2_asm_output_data (l, second, NULL);
2431 	    }
2432 	    break;
2433 	  case dw_val_class_wide_int:
2434 	    {
2435 	      int i;
2436 	      int len = get_full_len (*val2->v.val_wide);
2437 	      l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2438 
2439 	      dw2_asm_output_data (1, len * l, NULL);
2440 	      if (WORDS_BIG_ENDIAN)
2441 		for (i = len - 1; i >= 0; --i)
2442 		  dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2443 	      else
2444 		for (i = 0; i < len; ++i)
2445 		  dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2446 	    }
2447 	    break;
2448 	  default:
2449 	    gcc_unreachable ();
2450 	  }
2451       }
2452       break;
2453     case DW_OP_regval_type:
2454     case DW_OP_GNU_regval_type:
2455       {
2456 	unsigned r = val1->v.val_unsigned;
2457 	unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2458 	gcc_assert (o);
2459 	if (for_eh_or_skip >= 0)
2460 	  {
2461 	    r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2462 	    gcc_assert (size_of_uleb128 (r)
2463 			== size_of_uleb128 (val1->v.val_unsigned));
2464 	  }
2465 	dw2_asm_output_data_uleb128 (r, NULL);
2466 	dw2_asm_output_data_uleb128 (o, NULL);
2467       }
2468       break;
2469     case DW_OP_deref_type:
2470     case DW_OP_GNU_deref_type:
2471       {
2472 	unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2473 	gcc_assert (o);
2474 	dw2_asm_output_data (1, val1->v.val_int, NULL);
2475 	dw2_asm_output_data_uleb128 (o, NULL);
2476       }
2477       break;
2478     case DW_OP_convert:
2479     case DW_OP_reinterpret:
2480     case DW_OP_GNU_convert:
2481     case DW_OP_GNU_reinterpret:
2482       if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2483 	dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2484       else
2485 	{
2486 	  unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2487 	  gcc_assert (o);
2488 	  dw2_asm_output_data_uleb128 (o, NULL);
2489 	}
2490       break;
2491 
2492     case DW_OP_GNU_parameter_ref:
2493       {
2494 	unsigned long o;
2495 	gcc_assert (val1->val_class == dw_val_class_die_ref);
2496 	o = get_ref_die_offset (val1->v.val_die_ref.die);
2497 	dw2_asm_output_data (4, o, NULL);
2498       }
2499       break;
2500 
2501     default:
2502       /* Other codes have no operands.  */
2503       break;
2504     }
2505 }
2506 
2507 /* Output a sequence of location operations.
2508    The for_eh_or_skip parameter controls whether register numbers are
2509    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2510    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2511    info).  This should be suppressed for the cases that have not been converted
2512    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
2513 
2514 void
output_loc_sequence(dw_loc_descr_ref loc,int for_eh_or_skip)2515 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2516 {
2517   for (; loc != NULL; loc = loc->dw_loc_next)
2518     {
2519       enum dwarf_location_atom opc = loc->dw_loc_opc;
2520       /* Output the opcode.  */
2521       if (for_eh_or_skip >= 0
2522           && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2523 	{
2524 	  unsigned r = (opc - DW_OP_breg0);
2525 	  r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2526 	  gcc_assert (r <= 31);
2527 	  opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2528 	}
2529       else if (for_eh_or_skip >= 0
2530 	       && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2531 	{
2532 	  unsigned r = (opc - DW_OP_reg0);
2533 	  r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2534 	  gcc_assert (r <= 31);
2535 	  opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2536 	}
2537 
2538       dw2_asm_output_data (1, opc,
2539 			     "%s", dwarf_stack_op_name (opc));
2540 
2541       /* Output the operand(s) (if any).  */
2542       output_loc_operands (loc, for_eh_or_skip);
2543     }
2544 }
2545 
2546 /* Output location description stack opcode's operands (if any).
2547    The output is single bytes on a line, suitable for .cfi_escape.  */
2548 
2549 static void
output_loc_operands_raw(dw_loc_descr_ref loc)2550 output_loc_operands_raw (dw_loc_descr_ref loc)
2551 {
2552   dw_val_ref val1 = &loc->dw_loc_oprnd1;
2553   dw_val_ref val2 = &loc->dw_loc_oprnd2;
2554 
2555   switch (loc->dw_loc_opc)
2556     {
2557     case DW_OP_addr:
2558     case DW_OP_GNU_addr_index:
2559     case DW_OP_addrx:
2560     case DW_OP_GNU_const_index:
2561     case DW_OP_constx:
2562     case DW_OP_implicit_value:
2563       /* We cannot output addresses in .cfi_escape, only bytes.  */
2564       gcc_unreachable ();
2565 
2566     case DW_OP_const1u:
2567     case DW_OP_const1s:
2568     case DW_OP_pick:
2569     case DW_OP_deref_size:
2570     case DW_OP_xderef_size:
2571       fputc (',', asm_out_file);
2572       dw2_asm_output_data_raw (1, val1->v.val_int);
2573       break;
2574 
2575     case DW_OP_const2u:
2576     case DW_OP_const2s:
2577       fputc (',', asm_out_file);
2578       dw2_asm_output_data_raw (2, val1->v.val_int);
2579       break;
2580 
2581     case DW_OP_const4u:
2582     case DW_OP_const4s:
2583       fputc (',', asm_out_file);
2584       dw2_asm_output_data_raw (4, val1->v.val_int);
2585       break;
2586 
2587     case DW_OP_const8u:
2588     case DW_OP_const8s:
2589       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2590       fputc (',', asm_out_file);
2591       dw2_asm_output_data_raw (8, val1->v.val_int);
2592       break;
2593 
2594     case DW_OP_skip:
2595     case DW_OP_bra:
2596       {
2597 	int offset;
2598 
2599 	gcc_assert (val1->val_class == dw_val_class_loc);
2600 	offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2601 
2602         fputc (',', asm_out_file);
2603 	dw2_asm_output_data_raw (2, offset);
2604       }
2605       break;
2606 
2607     case DW_OP_regx:
2608       {
2609 	unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2610 	gcc_assert (size_of_uleb128 (r)
2611 		    == size_of_uleb128 (val1->v.val_unsigned));
2612 	fputc (',', asm_out_file);
2613 	dw2_asm_output_data_uleb128_raw (r);
2614       }
2615       break;
2616 
2617     case DW_OP_constu:
2618     case DW_OP_plus_uconst:
2619     case DW_OP_piece:
2620       fputc (',', asm_out_file);
2621       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2622       break;
2623 
2624     case DW_OP_bit_piece:
2625       fputc (',', asm_out_file);
2626       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2627       dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2628       break;
2629 
2630     case DW_OP_consts:
2631     case DW_OP_breg0:
2632     case DW_OP_breg1:
2633     case DW_OP_breg2:
2634     case DW_OP_breg3:
2635     case DW_OP_breg4:
2636     case DW_OP_breg5:
2637     case DW_OP_breg6:
2638     case DW_OP_breg7:
2639     case DW_OP_breg8:
2640     case DW_OP_breg9:
2641     case DW_OP_breg10:
2642     case DW_OP_breg11:
2643     case DW_OP_breg12:
2644     case DW_OP_breg13:
2645     case DW_OP_breg14:
2646     case DW_OP_breg15:
2647     case DW_OP_breg16:
2648     case DW_OP_breg17:
2649     case DW_OP_breg18:
2650     case DW_OP_breg19:
2651     case DW_OP_breg20:
2652     case DW_OP_breg21:
2653     case DW_OP_breg22:
2654     case DW_OP_breg23:
2655     case DW_OP_breg24:
2656     case DW_OP_breg25:
2657     case DW_OP_breg26:
2658     case DW_OP_breg27:
2659     case DW_OP_breg28:
2660     case DW_OP_breg29:
2661     case DW_OP_breg30:
2662     case DW_OP_breg31:
2663     case DW_OP_fbreg:
2664       fputc (',', asm_out_file);
2665       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2666       break;
2667 
2668     case DW_OP_bregx:
2669       {
2670 	unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2671 	gcc_assert (size_of_uleb128 (r)
2672 		    == size_of_uleb128 (val1->v.val_unsigned));
2673 	fputc (',', asm_out_file);
2674 	dw2_asm_output_data_uleb128_raw (r);
2675 	fputc (',', asm_out_file);
2676 	dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2677       }
2678       break;
2679 
2680     case DW_OP_implicit_pointer:
2681     case DW_OP_entry_value:
2682     case DW_OP_const_type:
2683     case DW_OP_regval_type:
2684     case DW_OP_deref_type:
2685     case DW_OP_convert:
2686     case DW_OP_reinterpret:
2687     case DW_OP_GNU_implicit_pointer:
2688     case DW_OP_GNU_entry_value:
2689     case DW_OP_GNU_const_type:
2690     case DW_OP_GNU_regval_type:
2691     case DW_OP_GNU_deref_type:
2692     case DW_OP_GNU_convert:
2693     case DW_OP_GNU_reinterpret:
2694     case DW_OP_GNU_parameter_ref:
2695       gcc_unreachable ();
2696       break;
2697 
2698     default:
2699       /* Other codes have no operands.  */
2700       break;
2701     }
2702 }
2703 
2704 void
output_loc_sequence_raw(dw_loc_descr_ref loc)2705 output_loc_sequence_raw (dw_loc_descr_ref loc)
2706 {
2707   while (1)
2708     {
2709       enum dwarf_location_atom opc = loc->dw_loc_opc;
2710       /* Output the opcode.  */
2711       if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2712 	{
2713 	  unsigned r = (opc - DW_OP_breg0);
2714 	  r = DWARF2_FRAME_REG_OUT (r, 1);
2715 	  gcc_assert (r <= 31);
2716 	  opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2717 	}
2718       else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2719 	{
2720 	  unsigned r = (opc - DW_OP_reg0);
2721 	  r = DWARF2_FRAME_REG_OUT (r, 1);
2722 	  gcc_assert (r <= 31);
2723 	  opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2724 	}
2725       /* Output the opcode.  */
2726       fprintf (asm_out_file, "%#x", opc);
2727       output_loc_operands_raw (loc);
2728 
2729       if (!loc->dw_loc_next)
2730 	break;
2731       loc = loc->dw_loc_next;
2732 
2733       fputc (',', asm_out_file);
2734     }
2735 }
2736 
2737 /* This function builds a dwarf location descriptor sequence from a
2738    dw_cfa_location, adding the given OFFSET to the result of the
2739    expression.  */
2740 
2741 struct dw_loc_descr_node *
build_cfa_loc(dw_cfa_location * cfa,poly_int64 offset)2742 build_cfa_loc (dw_cfa_location *cfa, poly_int64 offset)
2743 {
2744   struct dw_loc_descr_node *head, *tmp;
2745 
2746   offset += cfa->offset;
2747 
2748   if (cfa->indirect)
2749     {
2750       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2751       head->dw_loc_oprnd1.val_class = dw_val_class_const;
2752       head->dw_loc_oprnd1.val_entry = NULL;
2753       tmp = new_loc_descr (DW_OP_deref, 0, 0);
2754       add_loc_descr (&head, tmp);
2755       loc_descr_plus_const (&head, offset);
2756     }
2757   else
2758     head = new_reg_loc_descr (cfa->reg, offset);
2759 
2760   return head;
2761 }
2762 
2763 /* This function builds a dwarf location descriptor sequence for
2764    the address at OFFSET from the CFA when stack is aligned to
2765    ALIGNMENT byte.  */
2766 
2767 struct dw_loc_descr_node *
build_cfa_aligned_loc(dw_cfa_location * cfa,poly_int64 offset,HOST_WIDE_INT alignment)2768 build_cfa_aligned_loc (dw_cfa_location *cfa,
2769 		       poly_int64 offset, HOST_WIDE_INT alignment)
2770 {
2771   struct dw_loc_descr_node *head;
2772   unsigned int dwarf_fp
2773     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2774 
2775   /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
2776   if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2777     {
2778       head = new_reg_loc_descr (dwarf_fp, 0);
2779       add_loc_descr (&head, int_loc_descriptor (alignment));
2780       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2781       loc_descr_plus_const (&head, offset);
2782     }
2783   else
2784     head = new_reg_loc_descr (dwarf_fp, offset);
2785   return head;
2786 }
2787 
2788 /* And now, the support for symbolic debugging information.  */
2789 
2790 /* .debug_str support.  */
2791 
2792 static void dwarf2out_init (const char *);
2793 static void dwarf2out_finish (const char *);
2794 static void dwarf2out_early_finish (const char *);
2795 static void dwarf2out_assembly_start (void);
2796 static void dwarf2out_define (unsigned int, const char *);
2797 static void dwarf2out_undef (unsigned int, const char *);
2798 static void dwarf2out_start_source_file (unsigned, const char *);
2799 static void dwarf2out_end_source_file (unsigned);
2800 static void dwarf2out_function_decl (tree);
2801 static void dwarf2out_begin_block (unsigned, unsigned);
2802 static void dwarf2out_end_block (unsigned, unsigned);
2803 static bool dwarf2out_ignore_block (const_tree);
2804 static void dwarf2out_early_global_decl (tree);
2805 static void dwarf2out_late_global_decl (tree);
2806 static void dwarf2out_type_decl (tree, int);
2807 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool, bool);
2808 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2809 						 dw_die_ref);
2810 static void dwarf2out_abstract_function (tree);
2811 static void dwarf2out_var_location (rtx_insn *);
2812 static void dwarf2out_inline_entry (tree);
2813 static void dwarf2out_size_function (tree);
2814 static void dwarf2out_begin_function (tree);
2815 static void dwarf2out_end_function (unsigned int);
2816 static void dwarf2out_register_main_translation_unit (tree unit);
2817 static void dwarf2out_set_name (tree, tree);
2818 static void dwarf2out_register_external_die (tree decl, const char *sym,
2819 					     unsigned HOST_WIDE_INT off);
2820 static bool dwarf2out_die_ref_for_decl (tree decl, const char **sym,
2821 					unsigned HOST_WIDE_INT *off);
2822 
2823 /* The debug hooks structure.  */
2824 
2825 const struct gcc_debug_hooks dwarf2_debug_hooks =
2826 {
2827   dwarf2out_init,
2828   dwarf2out_finish,
2829   dwarf2out_early_finish,
2830   dwarf2out_assembly_start,
2831   dwarf2out_define,
2832   dwarf2out_undef,
2833   dwarf2out_start_source_file,
2834   dwarf2out_end_source_file,
2835   dwarf2out_begin_block,
2836   dwarf2out_end_block,
2837   dwarf2out_ignore_block,
2838   dwarf2out_source_line,
2839   dwarf2out_begin_prologue,
2840 #if VMS_DEBUGGING_INFO
2841   dwarf2out_vms_end_prologue,
2842   dwarf2out_vms_begin_epilogue,
2843 #else
2844   debug_nothing_int_charstar,
2845   debug_nothing_int_charstar,
2846 #endif
2847   dwarf2out_end_epilogue,
2848   dwarf2out_begin_function,
2849   dwarf2out_end_function,	/* end_function */
2850   dwarf2out_register_main_translation_unit,
2851   dwarf2out_function_decl,	/* function_decl */
2852   dwarf2out_early_global_decl,
2853   dwarf2out_late_global_decl,
2854   dwarf2out_type_decl,		/* type_decl */
2855   dwarf2out_imported_module_or_decl,
2856   dwarf2out_die_ref_for_decl,
2857   dwarf2out_register_external_die,
2858   debug_nothing_tree,		/* deferred_inline_function */
2859   /* The DWARF 2 backend tries to reduce debugging bloat by not
2860      emitting the abstract description of inline functions until
2861      something tries to reference them.  */
2862   dwarf2out_abstract_function,	/* outlining_inline_function */
2863   debug_nothing_rtx_code_label,	/* label */
2864   debug_nothing_int,		/* handle_pch */
2865   dwarf2out_var_location,
2866   dwarf2out_inline_entry,	/* inline_entry */
2867   dwarf2out_size_function,	/* size_function */
2868   dwarf2out_switch_text_section,
2869   dwarf2out_set_name,
2870   1,                            /* start_end_main_source_file */
2871   TYPE_SYMTAB_IS_DIE            /* tree_type_symtab_field */
2872 };
2873 
2874 const struct gcc_debug_hooks dwarf2_lineno_debug_hooks =
2875 {
2876   dwarf2out_init,
2877   debug_nothing_charstar,
2878   debug_nothing_charstar,
2879   dwarf2out_assembly_start,
2880   debug_nothing_int_charstar,
2881   debug_nothing_int_charstar,
2882   debug_nothing_int_charstar,
2883   debug_nothing_int,
2884   debug_nothing_int_int,	         /* begin_block */
2885   debug_nothing_int_int,	         /* end_block */
2886   debug_true_const_tree,	         /* ignore_block */
2887   dwarf2out_source_line,		 /* source_line */
2888   debug_nothing_int_int_charstar,	 /* begin_prologue */
2889   debug_nothing_int_charstar,	         /* end_prologue */
2890   debug_nothing_int_charstar,	         /* begin_epilogue */
2891   debug_nothing_int_charstar,	         /* end_epilogue */
2892   debug_nothing_tree,		         /* begin_function */
2893   debug_nothing_int,		         /* end_function */
2894   debug_nothing_tree,			 /* register_main_translation_unit */
2895   debug_nothing_tree,		         /* function_decl */
2896   debug_nothing_tree,		         /* early_global_decl */
2897   debug_nothing_tree,		         /* late_global_decl */
2898   debug_nothing_tree_int,		 /* type_decl */
2899   debug_nothing_tree_tree_tree_bool_bool,/* imported_module_or_decl */
2900   debug_false_tree_charstarstar_uhwistar,/* die_ref_for_decl */
2901   debug_nothing_tree_charstar_uhwi,      /* register_external_die */
2902   debug_nothing_tree,		         /* deferred_inline_function */
2903   debug_nothing_tree,		         /* outlining_inline_function */
2904   debug_nothing_rtx_code_label,	         /* label */
2905   debug_nothing_int,		         /* handle_pch */
2906   debug_nothing_rtx_insn,	         /* var_location */
2907   debug_nothing_tree,	         	 /* inline_entry */
2908   debug_nothing_tree,			 /* size_function */
2909   debug_nothing_void,                    /* switch_text_section */
2910   debug_nothing_tree_tree,		 /* set_name */
2911   0,                                     /* start_end_main_source_file */
2912   TYPE_SYMTAB_IS_ADDRESS                 /* tree_type_symtab_field */
2913 };
2914 
2915 /* NOTE: In the comments in this file, many references are made to
2916    "Debugging Information Entries".  This term is abbreviated as `DIE'
2917    throughout the remainder of this file.  */
2918 
2919 /* An internal representation of the DWARF output is built, and then
2920    walked to generate the DWARF debugging info.  The walk of the internal
2921    representation is done after the entire program has been compiled.
2922    The types below are used to describe the internal representation.  */
2923 
2924 /* Whether to put type DIEs into their own section .debug_types instead
2925    of making them part of the .debug_info section.  Only supported for
2926    Dwarf V4 or higher and the user didn't disable them through
2927    -fno-debug-types-section.  It is more efficient to put them in a
2928    separate comdat sections since the linker will then be able to
2929    remove duplicates.  But not all tools support .debug_types sections
2930    yet.  For Dwarf V5 or higher .debug_types doesn't exist any more,
2931    it is DW_UT_type unit type in .debug_info section.  For late LTO
2932    debug there should be almost no types emitted so avoid enabling
2933    -fdebug-types-section there.  */
2934 
2935 #define use_debug_types (dwarf_version >= 4 \
2936 			 && flag_debug_types_section \
2937 			 && !in_lto_p)
2938 
2939 /* Various DIE's use offsets relative to the beginning of the
2940    .debug_info section to refer to each other.  */
2941 
2942 typedef long int dw_offset;
2943 
2944 struct comdat_type_node;
2945 
2946 /* The entries in the line_info table more-or-less mirror the opcodes
2947    that are used in the real dwarf line table.  Arrays of these entries
2948    are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2949    supported.  */
2950 
2951 enum dw_line_info_opcode {
2952   /* Emit DW_LNE_set_address; the operand is the label index.  */
2953   LI_set_address,
2954 
2955   /* Emit a row to the matrix with the given line.  This may be done
2956      via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2957      special opcodes.  */
2958   LI_set_line,
2959 
2960   /* Emit a DW_LNS_set_file.  */
2961   LI_set_file,
2962 
2963   /* Emit a DW_LNS_set_column.  */
2964   LI_set_column,
2965 
2966   /* Emit a DW_LNS_negate_stmt; the operand is ignored.  */
2967   LI_negate_stmt,
2968 
2969   /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored.  */
2970   LI_set_prologue_end,
2971   LI_set_epilogue_begin,
2972 
2973   /* Emit a DW_LNE_set_discriminator.  */
2974   LI_set_discriminator,
2975 
2976   /* Output a Fixed Advance PC; the target PC is the label index; the
2977      base PC is the previous LI_adv_address or LI_set_address entry.
2978      We only use this when emitting debug views without assembler
2979      support, at explicit user request.  Ideally, we should only use
2980      it when the offset might be zero but we can't tell: it's the only
2981      way to maybe change the PC without resetting the view number.  */
2982   LI_adv_address
2983 };
2984 
2985 typedef struct GTY(()) dw_line_info_struct {
2986   enum dw_line_info_opcode opcode;
2987   unsigned int val;
2988 } dw_line_info_entry;
2989 
2990 
2991 struct GTY(()) dw_line_info_table {
2992   /* The label that marks the end of this section.  */
2993   const char *end_label;
2994 
2995   /* The values for the last row of the matrix, as collected in the table.
2996      These are used to minimize the changes to the next row.  */
2997   unsigned int file_num;
2998   unsigned int line_num;
2999   unsigned int column_num;
3000   int discrim_num;
3001   bool is_stmt;
3002   bool in_use;
3003 
3004   /* This denotes the NEXT view number.
3005 
3006      If it is 0, it is known that the NEXT view will be the first view
3007      at the given PC.
3008 
3009      If it is -1, we're forcing the view number to be reset, e.g. at a
3010      function entry.
3011 
3012      The meaning of other nonzero values depends on whether we're
3013      computing views internally or leaving it for the assembler to do
3014      so.  If we're emitting them internally, view denotes the view
3015      number since the last known advance of PC.  If we're leaving it
3016      for the assembler, it denotes the LVU label number that we're
3017      going to ask the assembler to assign.  */
3018   var_loc_view view;
3019 
3020   /* This counts the number of symbolic views emitted in this table
3021      since the latest view reset.  Its max value, over all tables,
3022      sets symview_upper_bound.  */
3023   var_loc_view symviews_since_reset;
3024 
3025 #define FORCE_RESET_NEXT_VIEW(x) ((x) = (var_loc_view)-1)
3026 #define RESET_NEXT_VIEW(x) ((x) = (var_loc_view)0)
3027 #define FORCE_RESETTING_VIEW_P(x) ((x) == (var_loc_view)-1)
3028 #define RESETTING_VIEW_P(x) ((x) == (var_loc_view)0 || FORCE_RESETTING_VIEW_P (x))
3029 
3030   vec<dw_line_info_entry, va_gc> *entries;
3031 };
3032 
3033 /* This is an upper bound for view numbers that the assembler may
3034    assign to symbolic views output in this translation.  It is used to
3035    decide how big a field to use to represent view numbers in
3036    symview-classed attributes.  */
3037 
3038 static var_loc_view symview_upper_bound;
3039 
3040 /* If we're keep track of location views and their reset points, and
3041    INSN is a reset point (i.e., it necessarily advances the PC), mark
3042    the next view in TABLE as reset.  */
3043 
3044 static void
maybe_reset_location_view(rtx_insn * insn,dw_line_info_table * table)3045 maybe_reset_location_view (rtx_insn *insn, dw_line_info_table *table)
3046 {
3047   if (!debug_internal_reset_location_views)
3048     return;
3049 
3050   /* Maybe turn (part of?) this test into a default target hook.  */
3051   int reset = 0;
3052 
3053   if (targetm.reset_location_view)
3054     reset = targetm.reset_location_view (insn);
3055 
3056   if (reset)
3057     ;
3058   else if (JUMP_TABLE_DATA_P (insn))
3059     reset = 1;
3060   else if (GET_CODE (insn) == USE
3061 	   || GET_CODE (insn) == CLOBBER
3062 	   || GET_CODE (insn) == ASM_INPUT
3063 	   || asm_noperands (insn) >= 0)
3064     ;
3065   else if (get_attr_min_length (insn) > 0)
3066     reset = 1;
3067 
3068   if (reset > 0 && !RESETTING_VIEW_P (table->view))
3069     RESET_NEXT_VIEW (table->view);
3070 }
3071 
3072 /* Each DIE attribute has a field specifying the attribute kind,
3073    a link to the next attribute in the chain, and an attribute value.
3074    Attributes are typically linked below the DIE they modify.  */
3075 
3076 typedef struct GTY(()) dw_attr_struct {
3077   enum dwarf_attribute dw_attr;
3078   dw_val_node dw_attr_val;
3079 }
3080 dw_attr_node;
3081 
3082 
3083 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
3084    The children of each node form a circular list linked by
3085    die_sib.  die_child points to the node *before* the "first" child node.  */
3086 
3087 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
3088   union die_symbol_or_type_node
3089     {
3090       const char * GTY ((tag ("0"))) die_symbol;
3091       comdat_type_node *GTY ((tag ("1"))) die_type_node;
3092     }
3093   GTY ((desc ("%0.comdat_type_p"))) die_id;
3094   vec<dw_attr_node, va_gc> *die_attr;
3095   dw_die_ref die_parent;
3096   dw_die_ref die_child;
3097   dw_die_ref die_sib;
3098   dw_die_ref die_definition; /* ref from a specification to its definition */
3099   dw_offset die_offset;
3100   unsigned long die_abbrev;
3101   int die_mark;
3102   unsigned int decl_id;
3103   enum dwarf_tag die_tag;
3104   /* Die is used and must not be pruned as unused.  */
3105   BOOL_BITFIELD die_perennial_p : 1;
3106   BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
3107   /* For an external ref to die_symbol if die_offset contains an extra
3108      offset to that symbol.  */
3109   BOOL_BITFIELD with_offset : 1;
3110   /* Whether this DIE was removed from the DIE tree, for example via
3111      prune_unused_types.  We don't consider those present from the
3112      DIE lookup routines.  */
3113   BOOL_BITFIELD removed : 1;
3114   /* Lots of spare bits.  */
3115 }
3116 die_node;
3117 
3118 /* Set to TRUE while dwarf2out_early_global_decl is running.  */
3119 static bool early_dwarf;
3120 static bool early_dwarf_finished;
3121 class set_early_dwarf {
3122 public:
3123   bool saved;
set_early_dwarf()3124   set_early_dwarf () : saved(early_dwarf)
3125     {
3126       gcc_assert (! early_dwarf_finished);
3127       early_dwarf = true;
3128     }
~set_early_dwarf()3129   ~set_early_dwarf () { early_dwarf = saved; }
3130 };
3131 
3132 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
3133 #define FOR_EACH_CHILD(die, c, expr) do {	\
3134   c = die->die_child;				\
3135   if (c) do {					\
3136     c = c->die_sib;				\
3137     expr;					\
3138   } while (c != die->die_child);		\
3139 } while (0)
3140 
3141 /* The pubname structure */
3142 
3143 typedef struct GTY(()) pubname_struct {
3144   dw_die_ref die;
3145   const char *name;
3146 }
3147 pubname_entry;
3148 
3149 
3150 struct GTY(()) dw_ranges {
3151   const char *label;
3152   /* If this is positive, it's a block number, otherwise it's a
3153      bitwise-negated index into dw_ranges_by_label.  */
3154   int num;
3155   /* If idx is equal to DW_RANGES_IDX_SKELETON, it should be emitted
3156      into .debug_rnglists section rather than .debug_rnglists.dwo
3157      for -gsplit-dwarf and DWARF >= 5.  */
3158 #define DW_RANGES_IDX_SKELETON ((1U << 31) - 1)
3159   /* Index for the range list for DW_FORM_rnglistx.  */
3160   unsigned int idx : 31;
3161   /* True if this range might be possibly in a different section
3162      from previous entry.  */
3163   unsigned int maybe_new_sec : 1;
3164   addr_table_entry *begin_entry;
3165   addr_table_entry *end_entry;
3166 };
3167 
3168 /* A structure to hold a macinfo entry.  */
3169 
3170 typedef struct GTY(()) macinfo_struct {
3171   unsigned char code;
3172   unsigned HOST_WIDE_INT lineno;
3173   const char *info;
3174 }
3175 macinfo_entry;
3176 
3177 
3178 struct GTY(()) dw_ranges_by_label {
3179   const char *begin;
3180   const char *end;
3181 };
3182 
3183 /* The comdat type node structure.  */
3184 struct GTY(()) comdat_type_node
3185 {
3186   dw_die_ref root_die;
3187   dw_die_ref type_die;
3188   dw_die_ref skeleton_die;
3189   char signature[DWARF_TYPE_SIGNATURE_SIZE];
3190   comdat_type_node *next;
3191 };
3192 
3193 /* A list of DIEs for which we can't determine ancestry (parent_die
3194    field) just yet.  Later in dwarf2out_finish we will fill in the
3195    missing bits.  */
3196 typedef struct GTY(()) limbo_die_struct {
3197   dw_die_ref die;
3198   /* The tree for which this DIE was created.  We use this to
3199      determine ancestry later.  */
3200   tree created_for;
3201   struct limbo_die_struct *next;
3202 }
3203 limbo_die_node;
3204 
3205 typedef struct skeleton_chain_struct
3206 {
3207   dw_die_ref old_die;
3208   dw_die_ref new_die;
3209   struct skeleton_chain_struct *parent;
3210 }
3211 skeleton_chain_node;
3212 
3213 /* Define a macro which returns nonzero for a TYPE_DECL which was
3214    implicitly generated for a type.
3215 
3216    Note that, unlike the C front-end (which generates a NULL named
3217    TYPE_DECL node for each complete tagged type, each array type,
3218    and each function type node created) the C++ front-end generates
3219    a _named_ TYPE_DECL node for each tagged type node created.
3220    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3221    generate a DW_TAG_typedef DIE for them.  Likewise with the Ada
3222    front-end, but for each type, tagged or not.  */
3223 
3224 #define TYPE_DECL_IS_STUB(decl)				\
3225   (DECL_NAME (decl) == NULL_TREE			\
3226    || (DECL_ARTIFICIAL (decl)				\
3227        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))	\
3228 	   /* This is necessary for stub decls that	\
3229 	      appear in nested inline functions.  */	\
3230 	   || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE	\
3231 	       && (decl_ultimate_origin (decl)		\
3232 		   == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3233 
3234 /* Information concerning the compilation unit's programming
3235    language, and compiler version.  */
3236 
3237 /* Fixed size portion of the DWARF compilation unit header.  */
3238 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3239   (DWARF_INITIAL_LENGTH_SIZE + dwarf_offset_size			\
3240    + (dwarf_version >= 5 ? 4 : 3))
3241 
3242 /* Fixed size portion of the DWARF comdat type unit header.  */
3243 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
3244   (DWARF_COMPILE_UNIT_HEADER_SIZE					\
3245    + DWARF_TYPE_SIGNATURE_SIZE + dwarf_offset_size)
3246 
3247 /* Fixed size portion of the DWARF skeleton compilation unit header.  */
3248 #define DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE \
3249   (DWARF_COMPILE_UNIT_HEADER_SIZE + (dwarf_version >= 5 ? 8 : 0))
3250 
3251 /* Fixed size portion of public names info.  */
3252 #define DWARF_PUBNAMES_HEADER_SIZE (2 * dwarf_offset_size + 2)
3253 
3254 /* Fixed size portion of the address range info.  */
3255 #define DWARF_ARANGES_HEADER_SIZE					\
3256   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + dwarf_offset_size + 4,	\
3257 		DWARF2_ADDR_SIZE * 2)					\
3258    - DWARF_INITIAL_LENGTH_SIZE)
3259 
3260 /* Size of padding portion in the address range info.  It must be
3261    aligned to twice the pointer size.  */
3262 #define DWARF_ARANGES_PAD_SIZE \
3263   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + dwarf_offset_size + 4, \
3264 		DWARF2_ADDR_SIZE * 2)				   \
3265    - (DWARF_INITIAL_LENGTH_SIZE + dwarf_offset_size + 4))
3266 
3267 /* Use assembler line directives if available.  */
3268 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3269 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3270 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3271 #else
3272 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3273 #endif
3274 #endif
3275 
3276 /* Use assembler views in line directives if available.  */
3277 #ifndef DWARF2_ASM_VIEW_DEBUG_INFO
3278 #ifdef HAVE_AS_DWARF2_DEBUG_VIEW
3279 #define DWARF2_ASM_VIEW_DEBUG_INFO 1
3280 #else
3281 #define DWARF2_ASM_VIEW_DEBUG_INFO 0
3282 #endif
3283 #endif
3284 
3285 /* Return true if GCC configure detected assembler support for .loc.  */
3286 
3287 bool
dwarf2out_default_as_loc_support(void)3288 dwarf2out_default_as_loc_support (void)
3289 {
3290   return DWARF2_ASM_LINE_DEBUG_INFO;
3291 #if (GCC_VERSION >= 3000)
3292 # undef DWARF2_ASM_LINE_DEBUG_INFO
3293 # pragma GCC poison DWARF2_ASM_LINE_DEBUG_INFO
3294 #endif
3295 }
3296 
3297 /* Return true if GCC configure detected assembler support for views
3298    in .loc directives.  */
3299 
3300 bool
dwarf2out_default_as_locview_support(void)3301 dwarf2out_default_as_locview_support (void)
3302 {
3303   return DWARF2_ASM_VIEW_DEBUG_INFO;
3304 #if (GCC_VERSION >= 3000)
3305 # undef DWARF2_ASM_VIEW_DEBUG_INFO
3306 # pragma GCC poison DWARF2_ASM_VIEW_DEBUG_INFO
3307 #endif
3308 }
3309 
3310 /* A bit is set in ZERO_VIEW_P if we are using the assembler-supported
3311    view computation, and it refers to a view identifier for which we
3312    will not emit a label because it is known to map to a view number
3313    zero.  We won't allocate the bitmap if we're not using assembler
3314    support for location views, but we have to make the variable
3315    visible for GGC and for code that will be optimized out for lack of
3316    support but that's still parsed and compiled.  We could abstract it
3317    out with macros, but it's not worth it.  */
3318 static GTY(()) bitmap zero_view_p;
3319 
3320 /* Evaluate to TRUE iff N is known to identify the first location view
3321    at its PC.  When not using assembler location view computation,
3322    that must be view number zero.  Otherwise, ZERO_VIEW_P is allocated
3323    and views label numbers recorded in it are the ones known to be
3324    zero.  */
3325 #define ZERO_VIEW_P(N) ((N) == (var_loc_view)0				\
3326 			|| (N) == (var_loc_view)-1			\
3327 			|| (zero_view_p					\
3328 			    && bitmap_bit_p (zero_view_p, (N))))
3329 
3330 /* Return true iff we're to emit .loc directives for the assembler to
3331    generate line number sections.
3332 
3333    When we're not emitting views, all we need from the assembler is
3334    support for .loc directives.
3335 
3336    If we are emitting views, we can only use the assembler's .loc
3337    support if it also supports views.
3338 
3339    When the compiler is emitting the line number programs and
3340    computing view numbers itself, it resets view numbers at known PC
3341    changes and counts from that, and then it emits view numbers as
3342    literal constants in locviewlists.  There are cases in which the
3343    compiler is not sure about PC changes, e.g. when extra alignment is
3344    requested for a label.  In these cases, the compiler may not reset
3345    the view counter, and the potential PC advance in the line number
3346    program will use an opcode that does not reset the view counter
3347    even if the PC actually changes, so that compiler and debug info
3348    consumer can keep view numbers in sync.
3349 
3350    When the compiler defers view computation to the assembler, it
3351    emits symbolic view numbers in locviewlists, with the exception of
3352    views known to be zero (forced resets, or reset after
3353    compiler-visible PC changes): instead of emitting symbols for
3354    these, we emit literal zero and assert the assembler agrees with
3355    the compiler's assessment.  We could use symbolic views everywhere,
3356    instead of special-casing zero views, but then we'd be unable to
3357    optimize out locviewlists that contain only zeros.  */
3358 
3359 static bool
output_asm_line_debug_info(void)3360 output_asm_line_debug_info (void)
3361 {
3362   return (dwarf2out_as_loc_support
3363 	  && (dwarf2out_as_locview_support
3364 	      || !debug_variable_location_views));
3365 }
3366 
3367 static bool asm_outputs_debug_line_str (void);
3368 
3369 /* Minimum line offset in a special line info. opcode.
3370    This value was chosen to give a reasonable range of values.  */
3371 #define DWARF_LINE_BASE  -10
3372 
3373 /* First special line opcode - leave room for the standard opcodes.  */
3374 #define DWARF_LINE_OPCODE_BASE  ((int)DW_LNS_set_isa + 1)
3375 
3376 /* Range of line offsets in a special line info. opcode.  */
3377 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
3378 
3379 /* Flag that indicates the initial value of the is_stmt_start flag.
3380    In the present implementation, we do not mark any lines as
3381    the beginning of a source statement, because that information
3382    is not made available by the GCC front-end.  */
3383 #define	DWARF_LINE_DEFAULT_IS_STMT_START 1
3384 
3385 /* Maximum number of operations per instruction bundle.  */
3386 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
3387 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
3388 #endif
3389 
3390 /* This location is used by calc_die_sizes() to keep track
3391    the offset of each DIE within the .debug_info section.  */
3392 static unsigned long next_die_offset;
3393 
3394 /* Record the root of the DIE's built for the current compilation unit.  */
3395 static GTY(()) dw_die_ref single_comp_unit_die;
3396 
3397 /* A list of type DIEs that have been separated into comdat sections.  */
3398 static GTY(()) comdat_type_node *comdat_type_list;
3399 
3400 /* A list of CU DIEs that have been separated.  */
3401 static GTY(()) limbo_die_node *cu_die_list;
3402 
3403 /* A list of DIEs with a NULL parent waiting to be relocated.  */
3404 static GTY(()) limbo_die_node *limbo_die_list;
3405 
3406 /* A list of DIEs for which we may have to generate
3407    DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set.  */
3408 static GTY(()) limbo_die_node *deferred_asm_name;
3409 
3410 struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
3411 {
3412   typedef const char *compare_type;
3413 
3414   static hashval_t hash (dwarf_file_data *);
3415   static bool equal (dwarf_file_data *, const char *);
3416 };
3417 
3418 /* Filenames referenced by this compilation unit.  */
3419 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
3420 
3421 struct decl_die_hasher : ggc_ptr_hash<die_node>
3422 {
3423   typedef tree compare_type;
3424 
3425   static hashval_t hash (die_node *);
3426   static bool equal (die_node *, tree);
3427 };
3428 /* A hash table of references to DIE's that describe declarations.
3429    The key is a DECL_UID() which is a unique number identifying each decl.  */
3430 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
3431 
3432 struct GTY ((for_user)) variable_value_struct {
3433   unsigned int decl_id;
3434   vec<dw_die_ref, va_gc> *dies;
3435 };
3436 
3437 struct variable_value_hasher : ggc_ptr_hash<variable_value_struct>
3438 {
3439   typedef tree compare_type;
3440 
3441   static hashval_t hash (variable_value_struct *);
3442   static bool equal (variable_value_struct *, tree);
3443 };
3444 /* A hash table of DIEs that contain DW_OP_GNU_variable_value with
3445    dw_val_class_decl_ref class, indexed by FUNCTION_DECLs which is
3446    DECL_CONTEXT of the referenced VAR_DECLs.  */
3447 static GTY (()) hash_table<variable_value_hasher> *variable_value_hash;
3448 
3449 struct block_die_hasher : ggc_ptr_hash<die_struct>
3450 {
3451   static hashval_t hash (die_struct *);
3452   static bool equal (die_struct *, die_struct *);
3453 };
3454 
3455 /* A hash table of references to DIE's that describe COMMON blocks.
3456    The key is DECL_UID() ^ die_parent.  */
3457 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
3458 
3459 typedef struct GTY(()) die_arg_entry_struct {
3460     dw_die_ref die;
3461     tree arg;
3462 } die_arg_entry;
3463 
3464 
3465 /* Node of the variable location list.  */
3466 struct GTY ((chain_next ("%h.next"))) var_loc_node {
3467   /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
3468      EXPR_LIST chain.  For small bitsizes, bitsize is encoded
3469      in mode of the EXPR_LIST node and first EXPR_LIST operand
3470      is either NOTE_INSN_VAR_LOCATION for a piece with a known
3471      location or NULL for padding.  For larger bitsizes,
3472      mode is 0 and first operand is a CONCAT with bitsize
3473      as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
3474      NULL as second operand.  */
3475   rtx GTY (()) loc;
3476   const char * GTY (()) label;
3477   struct var_loc_node * GTY (()) next;
3478   var_loc_view view;
3479 };
3480 
3481 /* Variable location list.  */
3482 struct GTY ((for_user)) var_loc_list_def {
3483   struct var_loc_node * GTY (()) first;
3484 
3485   /* Pointer to the last but one or last element of the
3486      chained list.  If the list is empty, both first and
3487      last are NULL, if the list contains just one node
3488      or the last node certainly is not redundant, it points
3489      to the last node, otherwise points to the last but one.
3490      Do not mark it for GC because it is marked through the chain.  */
3491   struct var_loc_node * GTY ((skip ("%h"))) last;
3492 
3493   /* Pointer to the last element before section switch,
3494      if NULL, either sections weren't switched or first
3495      is after section switch.  */
3496   struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
3497 
3498   /* DECL_UID of the variable decl.  */
3499   unsigned int decl_id;
3500 };
3501 typedef struct var_loc_list_def var_loc_list;
3502 
3503 /* Call argument location list.  */
3504 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
3505   rtx GTY (()) call_arg_loc_note;
3506   const char * GTY (()) label;
3507   tree GTY (()) block;
3508   bool tail_call_p;
3509   rtx GTY (()) symbol_ref;
3510   struct call_arg_loc_node * GTY (()) next;
3511 };
3512 
3513 
3514 struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
3515 {
3516   typedef const_tree compare_type;
3517 
3518   static hashval_t hash (var_loc_list *);
3519   static bool equal (var_loc_list *, const_tree);
3520 };
3521 
3522 /* Table of decl location linked lists.  */
3523 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
3524 
3525 /* Head and tail of call_arg_loc chain.  */
3526 static GTY (()) struct call_arg_loc_node *call_arg_locations;
3527 static struct call_arg_loc_node *call_arg_loc_last;
3528 
3529 /* Number of call sites in the current function.  */
3530 static int call_site_count = -1;
3531 /* Number of tail call sites in the current function.  */
3532 static int tail_call_site_count = -1;
3533 
3534 /* A cached location list.  */
3535 struct GTY ((for_user)) cached_dw_loc_list_def {
3536   /* The DECL_UID of the decl that this entry describes.  */
3537   unsigned int decl_id;
3538 
3539   /* The cached location list.  */
3540   dw_loc_list_ref loc_list;
3541 };
3542 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
3543 
3544 struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
3545 {
3546 
3547   typedef const_tree compare_type;
3548 
3549   static hashval_t hash (cached_dw_loc_list *);
3550   static bool equal (cached_dw_loc_list *, const_tree);
3551 };
3552 
3553 /* Table of cached location lists.  */
3554 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
3555 
3556 /* A vector of references to DIE's that are uniquely identified by their tag,
3557    presence/absence of children DIE's, and list of attribute/value pairs.  */
3558 static GTY(()) vec<dw_die_ref, va_gc> *abbrev_die_table;
3559 
3560 /* A hash map to remember the stack usage for DWARF procedures.  The value
3561    stored is the stack size difference between before the DWARF procedure
3562    invokation and after it returned.  In other words, for a DWARF procedure
3563    that consumes N stack slots and that pushes M ones, this stores M - N.  */
3564 static hash_map<dw_die_ref, int> *dwarf_proc_stack_usage_map;
3565 
3566 /* A global counter for generating labels for line number data.  */
3567 static unsigned int line_info_label_num;
3568 
3569 /* The current table to which we should emit line number information
3570    for the current function.  This will be set up at the beginning of
3571    assembly for the function.  */
3572 static GTY(()) dw_line_info_table *cur_line_info_table;
3573 
3574 /* The two default tables of line number info.  */
3575 static GTY(()) dw_line_info_table *text_section_line_info;
3576 static GTY(()) dw_line_info_table *cold_text_section_line_info;
3577 
3578 /* The set of all non-default tables of line number info.  */
3579 static GTY(()) vec<dw_line_info_table *, va_gc> *separate_line_info;
3580 
3581 /* A flag to tell pubnames/types export if there is an info section to
3582    refer to.  */
3583 static bool info_section_emitted;
3584 
3585 /* A pointer to the base of a table that contains a list of publicly
3586    accessible names.  */
3587 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
3588 
3589 /* A pointer to the base of a table that contains a list of publicly
3590    accessible types.  */
3591 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
3592 
3593 /* A pointer to the base of a table that contains a list of macro
3594    defines/undefines (and file start/end markers).  */
3595 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
3596 
3597 /* True if .debug_macinfo or .debug_macros section is going to be
3598    emitted.  */
3599 #define have_macinfo \
3600   ((!XCOFF_DEBUGGING_INFO || HAVE_XCOFF_DWARF_EXTRAS) \
3601    && debug_info_level >= DINFO_LEVEL_VERBOSE \
3602    && !macinfo_table->is_empty ())
3603 
3604 /* Vector of dies for which we should generate .debug_ranges info.  */
3605 static GTY (()) vec<dw_ranges, va_gc> *ranges_table;
3606 
3607 /* Vector of pairs of labels referenced in ranges_table.  */
3608 static GTY (()) vec<dw_ranges_by_label, va_gc> *ranges_by_label;
3609 
3610 /* Whether we have location lists that need outputting */
3611 static GTY(()) bool have_location_lists;
3612 
3613 /* Unique label counter.  */
3614 static GTY(()) unsigned int loclabel_num;
3615 
3616 /* Unique label counter for point-of-call tables.  */
3617 static GTY(()) unsigned int poc_label_num;
3618 
3619 /* The last file entry emitted by maybe_emit_file().  */
3620 static GTY(()) struct dwarf_file_data * last_emitted_file;
3621 
3622 /* Number of internal labels generated by gen_internal_sym().  */
3623 static GTY(()) int label_num;
3624 
3625 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3626 
3627 /* Instances of generic types for which we need to generate debug
3628    info that describe their generic parameters and arguments. That
3629    generation needs to happen once all types are properly laid out so
3630    we do it at the end of compilation.  */
3631 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3632 
3633 /* Offset from the "steady-state frame pointer" to the frame base,
3634    within the current function.  */
3635 static poly_int64 frame_pointer_fb_offset;
3636 static bool frame_pointer_fb_offset_valid;
3637 
3638 static vec<dw_die_ref> base_types;
3639 
3640 /* Flags to represent a set of attribute classes for attributes that represent
3641    a scalar value (bounds, pointers, ...).  */
3642 enum dw_scalar_form
3643 {
3644   dw_scalar_form_constant = 0x01,
3645   dw_scalar_form_exprloc = 0x02,
3646   dw_scalar_form_reference = 0x04
3647 };
3648 
3649 /* Forward declarations for functions defined in this file.  */
3650 
3651 static int is_pseudo_reg (const_rtx);
3652 static tree type_main_variant (tree);
3653 static int is_tagged_type (const_tree);
3654 static const char *dwarf_tag_name (unsigned);
3655 static const char *dwarf_attr_name (unsigned);
3656 static const char *dwarf_form_name (unsigned);
3657 static tree decl_ultimate_origin (const_tree);
3658 static tree decl_class_context (tree);
3659 static void add_dwarf_attr (dw_die_ref, dw_attr_node *);
3660 static inline enum dw_val_class AT_class (dw_attr_node *);
3661 static inline unsigned int AT_index (dw_attr_node *);
3662 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3663 static inline unsigned AT_flag (dw_attr_node *);
3664 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3665 static inline HOST_WIDE_INT AT_int (dw_attr_node *);
3666 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3667 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_node *);
3668 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3669 			   HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3670 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3671 			       unsigned int, unsigned char *);
3672 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3673 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3674 static inline const char *AT_string (dw_attr_node *);
3675 static enum dwarf_form AT_string_form (dw_attr_node *);
3676 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3677 static void add_AT_specification (dw_die_ref, dw_die_ref);
3678 static inline dw_die_ref AT_ref (dw_attr_node *);
3679 static inline int AT_ref_external (dw_attr_node *);
3680 static inline void set_AT_ref_external (dw_attr_node *, int);
3681 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3682 static inline dw_loc_descr_ref AT_loc (dw_attr_node *);
3683 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3684 			     dw_loc_list_ref);
3685 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3686 static void add_AT_view_list (dw_die_ref, enum dwarf_attribute);
3687 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3688 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3689 static void remove_addr_table_entry (addr_table_entry *);
3690 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3691 static inline rtx AT_addr (dw_attr_node *);
3692 static void add_AT_symview (dw_die_ref, enum dwarf_attribute, const char *);
3693 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3694 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3695 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3696 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3697                                unsigned long, bool);
3698 static inline const char *AT_lbl (dw_attr_node *);
3699 static dw_attr_node *get_AT (dw_die_ref, enum dwarf_attribute);
3700 static const char *get_AT_low_pc (dw_die_ref);
3701 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3702 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3703 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3704 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3705 static bool is_c (void);
3706 static bool is_cxx (void);
3707 static bool is_cxx (const_tree);
3708 static bool is_fortran (void);
3709 static bool is_ada (void);
3710 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3711 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3712 static void add_child_die (dw_die_ref, dw_die_ref);
3713 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3714 static dw_die_ref lookup_type_die (tree);
3715 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3716 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3717 static void equate_type_number_to_die (tree, dw_die_ref);
3718 static dw_die_ref lookup_decl_die (tree);
3719 static var_loc_list *lookup_decl_loc (const_tree);
3720 static void equate_decl_number_to_die (tree, dw_die_ref);
3721 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *, var_loc_view);
3722 static void print_spaces (FILE *);
3723 static void print_die (dw_die_ref, FILE *);
3724 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3725 static void attr_checksum (dw_attr_node *, struct md5_ctx *, int *);
3726 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3727 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3728 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3729 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3730 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_node *,
3731 				   struct md5_ctx *, int *);
3732 struct checksum_attributes;
3733 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3734 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3735 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3736 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3737 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3738 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3739 static int same_attr_p (dw_attr_node *, dw_attr_node *, int *);
3740 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3741 static int is_type_die (dw_die_ref);
3742 static inline bool is_template_instantiation (dw_die_ref);
3743 static int is_declaration_die (dw_die_ref);
3744 static int should_move_die_to_comdat (dw_die_ref);
3745 static dw_die_ref clone_as_declaration (dw_die_ref);
3746 static dw_die_ref clone_die (dw_die_ref);
3747 static dw_die_ref clone_tree (dw_die_ref);
3748 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3749 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3750 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3751 static dw_die_ref generate_skeleton (dw_die_ref);
3752 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3753                                                          dw_die_ref,
3754                                                          dw_die_ref);
3755 static void break_out_comdat_types (dw_die_ref);
3756 static void copy_decls_for_unworthy_types (dw_die_ref);
3757 
3758 static void add_sibling_attributes (dw_die_ref);
3759 static void output_location_lists (dw_die_ref);
3760 static int constant_size (unsigned HOST_WIDE_INT);
3761 static unsigned long size_of_die (dw_die_ref);
3762 static void calc_die_sizes (dw_die_ref);
3763 static void calc_base_type_die_sizes (void);
3764 static void mark_dies (dw_die_ref);
3765 static void unmark_dies (dw_die_ref);
3766 static void unmark_all_dies (dw_die_ref);
3767 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3768 static unsigned long size_of_aranges (void);
3769 static enum dwarf_form value_format (dw_attr_node *);
3770 static void output_value_format (dw_attr_node *);
3771 static void output_abbrev_section (void);
3772 static void output_die_abbrevs (unsigned long, dw_die_ref);
3773 static void output_die (dw_die_ref);
3774 static void output_compilation_unit_header (enum dwarf_unit_type);
3775 static void output_comp_unit (dw_die_ref, int, const unsigned char *);
3776 static void output_comdat_type_unit (comdat_type_node *, bool);
3777 static const char *dwarf2_name (tree, int);
3778 static void add_pubname (tree, dw_die_ref);
3779 static void add_enumerator_pubname (const char *, dw_die_ref);
3780 static void add_pubname_string (const char *, dw_die_ref);
3781 static void add_pubtype (tree, dw_die_ref);
3782 static void output_pubnames (vec<pubname_entry, va_gc> *);
3783 static void output_aranges (void);
3784 static unsigned int add_ranges (const_tree, bool = false);
3785 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3786                                   bool *, bool);
3787 static void output_ranges (void);
3788 static dw_line_info_table *new_line_info_table (void);
3789 static void output_line_info (bool);
3790 static void output_file_names (void);
3791 static dw_die_ref base_type_die (tree, bool);
3792 static int is_base_type (tree);
3793 static dw_die_ref subrange_type_die (tree, tree, tree, tree, dw_die_ref);
3794 static int decl_quals (const_tree);
3795 static dw_die_ref modified_type_die (tree, int, bool, dw_die_ref);
3796 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3797 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3798 static unsigned int dbx_reg_number (const_rtx);
3799 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3800 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3801 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3802 						enum var_init_status);
3803 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3804 						     enum var_init_status);
3805 static dw_loc_descr_ref based_loc_descr (rtx, poly_int64,
3806 					 enum var_init_status);
3807 static int is_based_loc (const_rtx);
3808 static bool resolve_one_addr (rtx *);
3809 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3810 					       enum var_init_status);
3811 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3812 					enum var_init_status);
3813 struct loc_descr_context;
3814 static void add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref);
3815 static void add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list);
3816 static dw_loc_list_ref loc_list_from_tree (tree, int,
3817 					   struct loc_descr_context *);
3818 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3819 						  struct loc_descr_context *);
3820 static tree field_type (const_tree);
3821 static unsigned int simple_type_align_in_bits (const_tree);
3822 static unsigned int simple_decl_align_in_bits (const_tree);
3823 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3824 struct vlr_context;
3825 static dw_loc_descr_ref field_byte_offset (const_tree, struct vlr_context *,
3826 					   HOST_WIDE_INT *);
3827 static void add_AT_location_description	(dw_die_ref, enum dwarf_attribute,
3828 					 dw_loc_list_ref);
3829 static void add_data_member_location_attribute (dw_die_ref, tree,
3830 						struct vlr_context *);
3831 static bool add_const_value_attribute (dw_die_ref, rtx);
3832 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3833 static void insert_wide_int (const wide_int &, unsigned char *, int);
3834 static unsigned insert_float (const_rtx, unsigned char *);
3835 static rtx rtl_for_decl_location (tree);
3836 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool);
3837 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3838 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3839 static void add_name_attribute (dw_die_ref, const char *);
3840 static void add_desc_attribute (dw_die_ref, tree);
3841 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3842 static void add_comp_dir_attribute (dw_die_ref);
3843 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3844 			     struct loc_descr_context *);
3845 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3846 			    struct loc_descr_context *);
3847 static void add_subscript_info (dw_die_ref, tree, bool);
3848 static void add_byte_size_attribute (dw_die_ref, tree);
3849 static void add_alignment_attribute (dw_die_ref, tree);
3850 static void add_bit_offset_attribute (dw_die_ref, tree);
3851 static void add_bit_size_attribute (dw_die_ref, tree);
3852 static void add_prototyped_attribute (dw_die_ref, tree);
3853 static void add_abstract_origin_attribute (dw_die_ref, tree);
3854 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3855 static void add_src_coords_attributes (dw_die_ref, tree);
3856 static void add_name_and_src_coords_attributes (dw_die_ref, tree, bool = false);
3857 static void add_discr_value (dw_die_ref, dw_discr_value *);
3858 static void add_discr_list (dw_die_ref, dw_discr_list_ref);
3859 static inline dw_discr_list_ref AT_discr_list (dw_attr_node *);
3860 static dw_die_ref scope_die_for (tree, dw_die_ref);
3861 static inline int local_scope_p (dw_die_ref);
3862 static inline int class_scope_p (dw_die_ref);
3863 static inline int class_or_namespace_scope_p (dw_die_ref);
3864 static void add_type_attribute (dw_die_ref, tree, int, bool, dw_die_ref);
3865 static void add_calling_convention_attribute (dw_die_ref, tree);
3866 static const char *type_tag (const_tree);
3867 static tree member_declared_type (const_tree);
3868 #if 0
3869 static const char *decl_start_label (tree);
3870 #endif
3871 static void gen_array_type_die (tree, dw_die_ref);
3872 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3873 #if 0
3874 static void gen_entry_point_die (tree, dw_die_ref);
3875 #endif
3876 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3877 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3878 static dw_die_ref gen_formal_parameter_pack_die  (tree, tree, dw_die_ref, tree*);
3879 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3880 static void gen_formal_types_die (tree, dw_die_ref);
3881 static void gen_subprogram_die (tree, dw_die_ref);
3882 static void gen_variable_die (tree, tree, dw_die_ref);
3883 static void gen_const_die (tree, dw_die_ref);
3884 static void gen_label_die (tree, dw_die_ref);
3885 static void gen_lexical_block_die (tree, dw_die_ref);
3886 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3887 static void gen_field_die (tree, struct vlr_context *, dw_die_ref);
3888 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3889 static dw_die_ref gen_compile_unit_die (const char *);
3890 static void gen_inheritance_die (tree, tree, tree, dw_die_ref);
3891 static void gen_member_die (tree, dw_die_ref);
3892 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3893 						enum debug_info_usage);
3894 static void gen_subroutine_type_die (tree, dw_die_ref);
3895 static void gen_typedef_die (tree, dw_die_ref);
3896 static void gen_type_die (tree, dw_die_ref);
3897 static void gen_block_die (tree, dw_die_ref);
3898 static void decls_for_scope (tree, dw_die_ref, bool = true);
3899 static bool is_naming_typedef_decl (const_tree);
3900 static inline dw_die_ref get_context_die (tree);
3901 static void gen_namespace_die (tree, dw_die_ref);
3902 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3903 static dw_die_ref gen_decl_die (tree, tree, struct vlr_context *, dw_die_ref);
3904 static dw_die_ref force_decl_die (tree);
3905 static dw_die_ref force_type_die (tree);
3906 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3907 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3908 static struct dwarf_file_data * lookup_filename (const char *);
3909 static void retry_incomplete_types (void);
3910 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3911 static void gen_generic_params_dies (tree);
3912 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3913 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3914 static void splice_child_die (dw_die_ref, dw_die_ref);
3915 static int file_info_cmp (const void *, const void *);
3916 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *, var_loc_view,
3917 				     const char *, var_loc_view, const char *);
3918 static void output_loc_list (dw_loc_list_ref);
3919 static char *gen_internal_sym (const char *);
3920 static bool want_pubnames (void);
3921 
3922 static void prune_unmark_dies (dw_die_ref);
3923 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3924 static void prune_unused_types_mark (dw_die_ref, int);
3925 static void prune_unused_types_walk (dw_die_ref);
3926 static void prune_unused_types_walk_attribs (dw_die_ref);
3927 static void prune_unused_types_prune (dw_die_ref);
3928 static void prune_unused_types (void);
3929 static int maybe_emit_file (struct dwarf_file_data *fd);
3930 static inline const char *AT_vms_delta1 (dw_attr_node *);
3931 static inline const char *AT_vms_delta2 (dw_attr_node *);
3932 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3933 				     const char *, const char *);
3934 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3935 static void gen_remaining_tmpl_value_param_die_attribute (void);
3936 static bool generic_type_p (tree);
3937 static void schedule_generic_params_dies_gen (tree t);
3938 static void gen_scheduled_generic_parms_dies (void);
3939 static void resolve_variable_values (void);
3940 
3941 static const char *comp_dir_string (void);
3942 
3943 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3944 
3945 /* enum for tracking thread-local variables whose address is really an offset
3946    relative to the TLS pointer, which will need link-time relocation, but will
3947    not need relocation by the DWARF consumer.  */
3948 
3949 enum dtprel_bool
3950 {
3951   dtprel_false = 0,
3952   dtprel_true = 1
3953 };
3954 
3955 /* Return the operator to use for an address of a variable.  For dtprel_true, we
3956    use DW_OP_const*.  For regular variables, which need both link-time
3957    relocation and consumer-level relocation (e.g., to account for shared objects
3958    loaded at a random address), we use DW_OP_addr*.  */
3959 
3960 static inline enum dwarf_location_atom
dw_addr_op(enum dtprel_bool dtprel)3961 dw_addr_op (enum dtprel_bool dtprel)
3962 {
3963   if (dtprel == dtprel_true)
3964     return (dwarf_split_debug_info ? dwarf_OP (DW_OP_constx)
3965             : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3966   else
3967     return dwarf_split_debug_info ? dwarf_OP (DW_OP_addrx) : DW_OP_addr;
3968 }
3969 
3970 /* Return a pointer to a newly allocated address location description.  If
3971    dwarf_split_debug_info is true, then record the address with the appropriate
3972    relocation.  */
3973 static inline dw_loc_descr_ref
new_addr_loc_descr(rtx addr,enum dtprel_bool dtprel)3974 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3975 {
3976   dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3977 
3978   ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3979   ref->dw_loc_oprnd1.v.val_addr = addr;
3980   ref->dtprel = dtprel;
3981   if (dwarf_split_debug_info)
3982     ref->dw_loc_oprnd1.val_entry
3983       = add_addr_table_entry (addr,
3984 			      dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3985   else
3986     ref->dw_loc_oprnd1.val_entry = NULL;
3987 
3988   return ref;
3989 }
3990 
3991 /* Section names used to hold DWARF debugging information.  */
3992 
3993 #ifndef DEBUG_INFO_SECTION
3994 #define DEBUG_INFO_SECTION	".debug_info"
3995 #endif
3996 #ifndef DEBUG_DWO_INFO_SECTION
3997 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3998 #endif
3999 #ifndef DEBUG_LTO_INFO_SECTION
4000 #define DEBUG_LTO_INFO_SECTION	".gnu.debuglto_.debug_info"
4001 #endif
4002 #ifndef DEBUG_LTO_DWO_INFO_SECTION
4003 #define DEBUG_LTO_DWO_INFO_SECTION ".gnu.debuglto_.debug_info.dwo"
4004 #endif
4005 #ifndef DEBUG_ABBREV_SECTION
4006 #define DEBUG_ABBREV_SECTION	".debug_abbrev"
4007 #endif
4008 #ifndef DEBUG_LTO_ABBREV_SECTION
4009 #define DEBUG_LTO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev"
4010 #endif
4011 #ifndef DEBUG_DWO_ABBREV_SECTION
4012 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
4013 #endif
4014 #ifndef DEBUG_LTO_DWO_ABBREV_SECTION
4015 #define DEBUG_LTO_DWO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev.dwo"
4016 #endif
4017 #ifndef DEBUG_ARANGES_SECTION
4018 #define DEBUG_ARANGES_SECTION	".debug_aranges"
4019 #endif
4020 #ifndef DEBUG_ADDR_SECTION
4021 #define DEBUG_ADDR_SECTION     ".debug_addr"
4022 #endif
4023 #ifndef DEBUG_MACINFO_SECTION
4024 #define DEBUG_MACINFO_SECTION     ".debug_macinfo"
4025 #endif
4026 #ifndef DEBUG_LTO_MACINFO_SECTION
4027 #define DEBUG_LTO_MACINFO_SECTION      ".gnu.debuglto_.debug_macinfo"
4028 #endif
4029 #ifndef DEBUG_DWO_MACINFO_SECTION
4030 #define DEBUG_DWO_MACINFO_SECTION      ".debug_macinfo.dwo"
4031 #endif
4032 #ifndef DEBUG_LTO_DWO_MACINFO_SECTION
4033 #define DEBUG_LTO_DWO_MACINFO_SECTION  ".gnu.debuglto_.debug_macinfo.dwo"
4034 #endif
4035 #ifndef DEBUG_MACRO_SECTION
4036 #define DEBUG_MACRO_SECTION	".debug_macro"
4037 #endif
4038 #ifndef DEBUG_LTO_MACRO_SECTION
4039 #define DEBUG_LTO_MACRO_SECTION ".gnu.debuglto_.debug_macro"
4040 #endif
4041 #ifndef DEBUG_DWO_MACRO_SECTION
4042 #define DEBUG_DWO_MACRO_SECTION        ".debug_macro.dwo"
4043 #endif
4044 #ifndef DEBUG_LTO_DWO_MACRO_SECTION
4045 #define DEBUG_LTO_DWO_MACRO_SECTION    ".gnu.debuglto_.debug_macro.dwo"
4046 #endif
4047 #ifndef DEBUG_LINE_SECTION
4048 #define DEBUG_LINE_SECTION	".debug_line"
4049 #endif
4050 #ifndef DEBUG_LTO_LINE_SECTION
4051 #define DEBUG_LTO_LINE_SECTION ".gnu.debuglto_.debug_line"
4052 #endif
4053 #ifndef DEBUG_DWO_LINE_SECTION
4054 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
4055 #endif
4056 #ifndef DEBUG_LTO_DWO_LINE_SECTION
4057 #define DEBUG_LTO_DWO_LINE_SECTION ".gnu.debuglto_.debug_line.dwo"
4058 #endif
4059 #ifndef DEBUG_LOC_SECTION
4060 #define DEBUG_LOC_SECTION	".debug_loc"
4061 #endif
4062 #ifndef DEBUG_DWO_LOC_SECTION
4063 #define DEBUG_DWO_LOC_SECTION  ".debug_loc.dwo"
4064 #endif
4065 #ifndef DEBUG_LOCLISTS_SECTION
4066 #define DEBUG_LOCLISTS_SECTION	".debug_loclists"
4067 #endif
4068 #ifndef DEBUG_DWO_LOCLISTS_SECTION
4069 #define DEBUG_DWO_LOCLISTS_SECTION  ".debug_loclists.dwo"
4070 #endif
4071 #ifndef DEBUG_PUBNAMES_SECTION
4072 #define DEBUG_PUBNAMES_SECTION	\
4073   ((debug_generate_pub_sections == 2) \
4074    ? ".debug_gnu_pubnames" : ".debug_pubnames")
4075 #endif
4076 #ifndef DEBUG_PUBTYPES_SECTION
4077 #define DEBUG_PUBTYPES_SECTION	\
4078   ((debug_generate_pub_sections == 2) \
4079    ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
4080 #endif
4081 #ifndef DEBUG_STR_OFFSETS_SECTION
4082 #define DEBUG_STR_OFFSETS_SECTION ".debug_str_offsets"
4083 #endif
4084 #ifndef DEBUG_DWO_STR_OFFSETS_SECTION
4085 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
4086 #endif
4087 #ifndef DEBUG_LTO_DWO_STR_OFFSETS_SECTION
4088 #define DEBUG_LTO_DWO_STR_OFFSETS_SECTION ".gnu.debuglto_.debug_str_offsets.dwo"
4089 #endif
4090 #ifndef DEBUG_STR_SECTION
4091 #define DEBUG_STR_SECTION  ".debug_str"
4092 #endif
4093 #ifndef DEBUG_LTO_STR_SECTION
4094 #define DEBUG_LTO_STR_SECTION ".gnu.debuglto_.debug_str"
4095 #endif
4096 #ifndef DEBUG_STR_DWO_SECTION
4097 #define DEBUG_STR_DWO_SECTION   ".debug_str.dwo"
4098 #endif
4099 #ifndef DEBUG_LTO_STR_DWO_SECTION
4100 #define DEBUG_LTO_STR_DWO_SECTION ".gnu.debuglto_.debug_str.dwo"
4101 #endif
4102 #ifndef DEBUG_RANGES_SECTION
4103 #define DEBUG_RANGES_SECTION	".debug_ranges"
4104 #endif
4105 #ifndef DEBUG_RNGLISTS_SECTION
4106 #define DEBUG_RNGLISTS_SECTION	".debug_rnglists"
4107 #endif
4108 #ifndef DEBUG_DWO_RNGLISTS_SECTION
4109 #define DEBUG_DWO_RNGLISTS_SECTION	".debug_rnglists.dwo"
4110 #endif
4111 #ifndef DEBUG_LINE_STR_SECTION
4112 #define DEBUG_LINE_STR_SECTION  ".debug_line_str"
4113 #endif
4114 #ifndef DEBUG_LTO_LINE_STR_SECTION
4115 #define DEBUG_LTO_LINE_STR_SECTION  ".gnu.debuglto_.debug_line_str"
4116 #endif
4117 
4118 /* Standard ELF section names for compiled code and data.  */
4119 #ifndef TEXT_SECTION_NAME
4120 #define TEXT_SECTION_NAME	".text"
4121 #endif
4122 
4123 /* Section flags for .debug_str section.  */
4124 #define DEBUG_STR_SECTION_FLAGS                                 \
4125   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
4126    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
4127    : SECTION_DEBUG)
4128 
4129 /* Section flags for .debug_str.dwo section.  */
4130 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
4131 
4132 /* Attribute used to refer to the macro section.  */
4133 #define DEBUG_MACRO_ATTRIBUTE (dwarf_version >= 5 ? DW_AT_macros \
4134 		   : dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros)
4135 
4136 /* Labels we insert at beginning sections we can reference instead of
4137    the section names themselves.  */
4138 
4139 #ifndef TEXT_SECTION_LABEL
4140 #define TEXT_SECTION_LABEL                 "Ltext"
4141 #endif
4142 #ifndef COLD_TEXT_SECTION_LABEL
4143 #define COLD_TEXT_SECTION_LABEL             "Ltext_cold"
4144 #endif
4145 #ifndef DEBUG_LINE_SECTION_LABEL
4146 #define DEBUG_LINE_SECTION_LABEL           "Ldebug_line"
4147 #endif
4148 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
4149 #define DEBUG_SKELETON_LINE_SECTION_LABEL   "Lskeleton_debug_line"
4150 #endif
4151 #ifndef DEBUG_INFO_SECTION_LABEL
4152 #define DEBUG_INFO_SECTION_LABEL           "Ldebug_info"
4153 #endif
4154 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
4155 #define DEBUG_SKELETON_INFO_SECTION_LABEL   "Lskeleton_debug_info"
4156 #endif
4157 #ifndef DEBUG_ABBREV_SECTION_LABEL
4158 #define DEBUG_ABBREV_SECTION_LABEL         "Ldebug_abbrev"
4159 #endif
4160 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
4161 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
4162 #endif
4163 #ifndef DEBUG_ADDR_SECTION_LABEL
4164 #define DEBUG_ADDR_SECTION_LABEL           "Ldebug_addr"
4165 #endif
4166 #ifndef DEBUG_LOC_SECTION_LABEL
4167 #define DEBUG_LOC_SECTION_LABEL                    "Ldebug_loc"
4168 #endif
4169 #ifndef DEBUG_RANGES_SECTION_LABEL
4170 #define DEBUG_RANGES_SECTION_LABEL         "Ldebug_ranges"
4171 #endif
4172 #ifndef DEBUG_MACINFO_SECTION_LABEL
4173 #define DEBUG_MACINFO_SECTION_LABEL         "Ldebug_macinfo"
4174 #endif
4175 #ifndef DEBUG_MACRO_SECTION_LABEL
4176 #define DEBUG_MACRO_SECTION_LABEL          "Ldebug_macro"
4177 #endif
4178 #define SKELETON_COMP_DIE_ABBREV 1
4179 #define SKELETON_TYPE_DIE_ABBREV 2
4180 
4181 /* Definitions of defaults for formats and names of various special
4182    (artificial) labels which may be generated within this file (when the -g
4183    options is used and DWARF2_DEBUGGING_INFO is in effect.
4184    If necessary, these may be overridden from within the tm.h file, but
4185    typically, overriding these defaults is unnecessary.  */
4186 
4187 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4188 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4189 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4190 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4191 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4192 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4193 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4194 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4195 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4196 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4197 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4198 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4199 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4200 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4201 static char ranges_base_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4202 
4203 #ifndef TEXT_END_LABEL
4204 #define TEXT_END_LABEL		"Letext"
4205 #endif
4206 #ifndef COLD_END_LABEL
4207 #define COLD_END_LABEL          "Letext_cold"
4208 #endif
4209 #ifndef BLOCK_BEGIN_LABEL
4210 #define BLOCK_BEGIN_LABEL	"LBB"
4211 #endif
4212 #ifndef BLOCK_INLINE_ENTRY_LABEL
4213 #define BLOCK_INLINE_ENTRY_LABEL "LBI"
4214 #endif
4215 #ifndef BLOCK_END_LABEL
4216 #define BLOCK_END_LABEL		"LBE"
4217 #endif
4218 #ifndef LINE_CODE_LABEL
4219 #define LINE_CODE_LABEL		"LM"
4220 #endif
4221 
4222 
4223 /* Return the root of the DIE's built for the current compilation unit.  */
4224 static dw_die_ref
comp_unit_die(void)4225 comp_unit_die (void)
4226 {
4227   if (!single_comp_unit_die)
4228     single_comp_unit_die = gen_compile_unit_die (NULL);
4229   return single_comp_unit_die;
4230 }
4231 
4232 /* We allow a language front-end to designate a function that is to be
4233    called to "demangle" any name before it is put into a DIE.  */
4234 
4235 static const char *(*demangle_name_func) (const char *);
4236 
4237 void
dwarf2out_set_demangle_name_func(const char * (* func)(const char *))4238 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
4239 {
4240   demangle_name_func = func;
4241 }
4242 
4243 /* Test if rtl node points to a pseudo register.  */
4244 
4245 static inline int
is_pseudo_reg(const_rtx rtl)4246 is_pseudo_reg (const_rtx rtl)
4247 {
4248   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
4249 	  || (GET_CODE (rtl) == SUBREG
4250 	      && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
4251 }
4252 
4253 /* Return a reference to a type, with its const and volatile qualifiers
4254    removed.  */
4255 
4256 static inline tree
type_main_variant(tree type)4257 type_main_variant (tree type)
4258 {
4259   type = TYPE_MAIN_VARIANT (type);
4260 
4261   /* ??? There really should be only one main variant among any group of
4262      variants of a given type (and all of the MAIN_VARIANT values for all
4263      members of the group should point to that one type) but sometimes the C
4264      front-end messes this up for array types, so we work around that bug
4265      here.  */
4266   if (TREE_CODE (type) == ARRAY_TYPE)
4267     while (type != TYPE_MAIN_VARIANT (type))
4268       type = TYPE_MAIN_VARIANT (type);
4269 
4270   return type;
4271 }
4272 
4273 /* Return nonzero if the given type node represents a tagged type.  */
4274 
4275 static inline int
is_tagged_type(const_tree type)4276 is_tagged_type (const_tree type)
4277 {
4278   enum tree_code code = TREE_CODE (type);
4279 
4280   return (code == RECORD_TYPE || code == UNION_TYPE
4281 	  || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4282 }
4283 
4284 /* Set label to debug_info_section_label + die_offset of a DIE reference.  */
4285 
4286 static void
get_ref_die_offset_label(char * label,dw_die_ref ref)4287 get_ref_die_offset_label (char *label, dw_die_ref ref)
4288 {
4289   sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
4290 }
4291 
4292 /* Return die_offset of a DIE reference to a base type.  */
4293 
4294 static unsigned long int
get_base_type_offset(dw_die_ref ref)4295 get_base_type_offset (dw_die_ref ref)
4296 {
4297   if (ref->die_offset)
4298     return ref->die_offset;
4299   if (comp_unit_die ()->die_abbrev)
4300     {
4301       calc_base_type_die_sizes ();
4302       gcc_assert (ref->die_offset);
4303     }
4304   return ref->die_offset;
4305 }
4306 
4307 /* Return die_offset of a DIE reference other than base type.  */
4308 
4309 static unsigned long int
get_ref_die_offset(dw_die_ref ref)4310 get_ref_die_offset (dw_die_ref ref)
4311 {
4312   gcc_assert (ref->die_offset);
4313   return ref->die_offset;
4314 }
4315 
4316 /* Convert a DIE tag into its string name.  */
4317 
4318 static const char *
dwarf_tag_name(unsigned int tag)4319 dwarf_tag_name (unsigned int tag)
4320 {
4321   const char *name = get_DW_TAG_name (tag);
4322 
4323   if (name != NULL)
4324     return name;
4325 
4326   return "DW_TAG_<unknown>";
4327 }
4328 
4329 /* Convert a DWARF attribute code into its string name.  */
4330 
4331 static const char *
dwarf_attr_name(unsigned int attr)4332 dwarf_attr_name (unsigned int attr)
4333 {
4334   const char *name;
4335 
4336   switch (attr)
4337     {
4338 #if VMS_DEBUGGING_INFO
4339     case DW_AT_HP_prologue:
4340       return "DW_AT_HP_prologue";
4341 #else
4342     case DW_AT_MIPS_loop_unroll_factor:
4343       return "DW_AT_MIPS_loop_unroll_factor";
4344 #endif
4345 
4346 #if VMS_DEBUGGING_INFO
4347     case DW_AT_HP_epilogue:
4348       return "DW_AT_HP_epilogue";
4349 #else
4350     case DW_AT_MIPS_stride:
4351       return "DW_AT_MIPS_stride";
4352 #endif
4353     }
4354 
4355   name = get_DW_AT_name (attr);
4356 
4357   if (name != NULL)
4358     return name;
4359 
4360   return "DW_AT_<unknown>";
4361 }
4362 
4363 /* Convert a DWARF value form code into its string name.  */
4364 
4365 static const char *
dwarf_form_name(unsigned int form)4366 dwarf_form_name (unsigned int form)
4367 {
4368   const char *name = get_DW_FORM_name (form);
4369 
4370   if (name != NULL)
4371     return name;
4372 
4373   return "DW_FORM_<unknown>";
4374 }
4375 
4376 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
4377    instance of an inlined instance of a decl which is local to an inline
4378    function, so we have to trace all of the way back through the origin chain
4379    to find out what sort of node actually served as the original seed for the
4380    given block.  */
4381 
4382 static tree
decl_ultimate_origin(const_tree decl)4383 decl_ultimate_origin (const_tree decl)
4384 {
4385   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4386     return NULL_TREE;
4387 
4388   /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
4389      we're trying to output the abstract instance of this function.  */
4390   if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4391     return NULL_TREE;
4392 
4393   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4394      most distant ancestor, this should never happen.  */
4395   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4396 
4397   return DECL_ABSTRACT_ORIGIN (decl);
4398 }
4399 
4400 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
4401    of a virtual function may refer to a base class, so we check the 'this'
4402    parameter.  */
4403 
4404 static tree
decl_class_context(tree decl)4405 decl_class_context (tree decl)
4406 {
4407   tree context = NULL_TREE;
4408 
4409   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4410     context = DECL_CONTEXT (decl);
4411   else
4412     context = TYPE_MAIN_VARIANT
4413       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4414 
4415   if (context && !TYPE_P (context))
4416     context = NULL_TREE;
4417 
4418   return context;
4419 }
4420 
4421 /* Add an attribute/value pair to a DIE.  */
4422 
4423 static inline void
add_dwarf_attr(dw_die_ref die,dw_attr_node * attr)4424 add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
4425 {
4426   /* Maybe this should be an assert?  */
4427   if (die == NULL)
4428     return;
4429 
4430   if (flag_checking)
4431     {
4432       /* Check we do not add duplicate attrs.  Can't use get_AT here
4433          because that recurses to the specification/abstract origin DIE.  */
4434       dw_attr_node *a;
4435       unsigned ix;
4436       FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4437 	gcc_assert (a->dw_attr != attr->dw_attr);
4438     }
4439 
4440   vec_safe_reserve (die->die_attr, 1);
4441   vec_safe_push (die->die_attr, *attr);
4442 }
4443 
4444 static inline enum dw_val_class
AT_class(dw_attr_node * a)4445 AT_class (dw_attr_node *a)
4446 {
4447   return a->dw_attr_val.val_class;
4448 }
4449 
4450 /* Return the index for any attribute that will be referenced with a
4451    DW_FORM_addrx/GNU_addr_index or DW_FORM_strx/GNU_str_index.  String
4452    indices are stored in dw_attr_val.v.val_str for reference counting
4453    pruning.  */
4454 
4455 static inline unsigned int
AT_index(dw_attr_node * a)4456 AT_index (dw_attr_node *a)
4457 {
4458   if (AT_class (a) == dw_val_class_str)
4459     return a->dw_attr_val.v.val_str->index;
4460   else if (a->dw_attr_val.val_entry != NULL)
4461     return a->dw_attr_val.val_entry->index;
4462   return NOT_INDEXED;
4463 }
4464 
4465 /* Add a flag value attribute to a DIE.  */
4466 
4467 static inline void
add_AT_flag(dw_die_ref die,enum dwarf_attribute attr_kind,unsigned int flag)4468 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4469 {
4470   dw_attr_node attr;
4471 
4472   attr.dw_attr = attr_kind;
4473   attr.dw_attr_val.val_class = dw_val_class_flag;
4474   attr.dw_attr_val.val_entry = NULL;
4475   attr.dw_attr_val.v.val_flag = flag;
4476   add_dwarf_attr (die, &attr);
4477 }
4478 
4479 static inline unsigned
AT_flag(dw_attr_node * a)4480 AT_flag (dw_attr_node *a)
4481 {
4482   gcc_assert (a && AT_class (a) == dw_val_class_flag);
4483   return a->dw_attr_val.v.val_flag;
4484 }
4485 
4486 /* Add a signed integer attribute value to a DIE.  */
4487 
4488 static inline void
add_AT_int(dw_die_ref die,enum dwarf_attribute attr_kind,HOST_WIDE_INT int_val)4489 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4490 {
4491   dw_attr_node attr;
4492 
4493   attr.dw_attr = attr_kind;
4494   attr.dw_attr_val.val_class = dw_val_class_const;
4495   attr.dw_attr_val.val_entry = NULL;
4496   attr.dw_attr_val.v.val_int = int_val;
4497   add_dwarf_attr (die, &attr);
4498 }
4499 
4500 static inline HOST_WIDE_INT
AT_int(dw_attr_node * a)4501 AT_int (dw_attr_node *a)
4502 {
4503   gcc_assert (a && (AT_class (a) == dw_val_class_const
4504 		    || AT_class (a) == dw_val_class_const_implicit));
4505   return a->dw_attr_val.v.val_int;
4506 }
4507 
4508 /* Add an unsigned integer attribute value to a DIE.  */
4509 
4510 static inline void
add_AT_unsigned(dw_die_ref die,enum dwarf_attribute attr_kind,unsigned HOST_WIDE_INT unsigned_val)4511 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4512 		 unsigned HOST_WIDE_INT unsigned_val)
4513 {
4514   dw_attr_node attr;
4515 
4516   attr.dw_attr = attr_kind;
4517   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4518   attr.dw_attr_val.val_entry = NULL;
4519   attr.dw_attr_val.v.val_unsigned = unsigned_val;
4520   add_dwarf_attr (die, &attr);
4521 }
4522 
4523 static inline unsigned HOST_WIDE_INT
AT_unsigned(dw_attr_node * a)4524 AT_unsigned (dw_attr_node *a)
4525 {
4526   gcc_assert (a && (AT_class (a) == dw_val_class_unsigned_const
4527 		    || AT_class (a) == dw_val_class_unsigned_const_implicit));
4528   return a->dw_attr_val.v.val_unsigned;
4529 }
4530 
4531 /* Add an unsigned wide integer attribute value to a DIE.  */
4532 
4533 static inline void
add_AT_wide(dw_die_ref die,enum dwarf_attribute attr_kind,const wide_int & w)4534 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
4535 	     const wide_int& w)
4536 {
4537   dw_attr_node attr;
4538 
4539   attr.dw_attr = attr_kind;
4540   attr.dw_attr_val.val_class = dw_val_class_wide_int;
4541   attr.dw_attr_val.val_entry = NULL;
4542   attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
4543   *attr.dw_attr_val.v.val_wide = w;
4544   add_dwarf_attr (die, &attr);
4545 }
4546 
4547 /* Add an unsigned double integer attribute value to a DIE.  */
4548 
4549 static inline void
add_AT_double(dw_die_ref die,enum dwarf_attribute attr_kind,HOST_WIDE_INT high,unsigned HOST_WIDE_INT low)4550 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
4551 	       HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
4552 {
4553   dw_attr_node attr;
4554 
4555   attr.dw_attr = attr_kind;
4556   attr.dw_attr_val.val_class = dw_val_class_const_double;
4557   attr.dw_attr_val.val_entry = NULL;
4558   attr.dw_attr_val.v.val_double.high = high;
4559   attr.dw_attr_val.v.val_double.low = low;
4560   add_dwarf_attr (die, &attr);
4561 }
4562 
4563 /* Add a floating point attribute value to a DIE and return it.  */
4564 
4565 static inline void
add_AT_vec(dw_die_ref die,enum dwarf_attribute attr_kind,unsigned int length,unsigned int elt_size,unsigned char * array)4566 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4567 	    unsigned int length, unsigned int elt_size, unsigned char *array)
4568 {
4569   dw_attr_node attr;
4570 
4571   attr.dw_attr = attr_kind;
4572   attr.dw_attr_val.val_class = dw_val_class_vec;
4573   attr.dw_attr_val.val_entry = NULL;
4574   attr.dw_attr_val.v.val_vec.length = length;
4575   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4576   attr.dw_attr_val.v.val_vec.array = array;
4577   add_dwarf_attr (die, &attr);
4578 }
4579 
4580 /* Add an 8-byte data attribute value to a DIE.  */
4581 
4582 static inline void
add_AT_data8(dw_die_ref die,enum dwarf_attribute attr_kind,unsigned char data8[8])4583 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4584               unsigned char data8[8])
4585 {
4586   dw_attr_node attr;
4587 
4588   attr.dw_attr = attr_kind;
4589   attr.dw_attr_val.val_class = dw_val_class_data8;
4590   attr.dw_attr_val.val_entry = NULL;
4591   memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4592   add_dwarf_attr (die, &attr);
4593 }
4594 
4595 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE.  When using
4596    dwarf_split_debug_info, address attributes in dies destined for the
4597    final executable have force_direct set to avoid using indexed
4598    references.  */
4599 
4600 static inline void
add_AT_low_high_pc(dw_die_ref die,const char * lbl_low,const char * lbl_high,bool force_direct)4601 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
4602                     bool force_direct)
4603 {
4604   dw_attr_node attr;
4605   char * lbl_id;
4606 
4607   lbl_id = xstrdup (lbl_low);
4608   attr.dw_attr = DW_AT_low_pc;
4609   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4610   attr.dw_attr_val.v.val_lbl_id = lbl_id;
4611   if (dwarf_split_debug_info && !force_direct)
4612     attr.dw_attr_val.val_entry
4613       = add_addr_table_entry (lbl_id, ate_kind_label);
4614   else
4615     attr.dw_attr_val.val_entry = NULL;
4616   add_dwarf_attr (die, &attr);
4617 
4618   attr.dw_attr = DW_AT_high_pc;
4619   if (dwarf_version < 4)
4620     attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4621   else
4622     attr.dw_attr_val.val_class = dw_val_class_high_pc;
4623   lbl_id = xstrdup (lbl_high);
4624   attr.dw_attr_val.v.val_lbl_id = lbl_id;
4625   if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
4626       && dwarf_split_debug_info && !force_direct)
4627     attr.dw_attr_val.val_entry
4628       = add_addr_table_entry (lbl_id, ate_kind_label);
4629   else
4630     attr.dw_attr_val.val_entry = NULL;
4631   add_dwarf_attr (die, &attr);
4632 }
4633 
4634 /* Hash and equality functions for debug_str_hash.  */
4635 
4636 hashval_t
hash(indirect_string_node * x)4637 indirect_string_hasher::hash (indirect_string_node *x)
4638 {
4639   return htab_hash_string (x->str);
4640 }
4641 
4642 bool
equal(indirect_string_node * x1,const char * x2)4643 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4644 {
4645   return strcmp (x1->str, x2) == 0;
4646 }
4647 
4648 /* Add STR to the given string hash table.  */
4649 
4650 static struct indirect_string_node *
4651 find_AT_string_in_table (const char *str,
4652 			 hash_table<indirect_string_hasher> *table,
4653 			 enum insert_option insert = INSERT)
4654 {
4655   struct indirect_string_node *node;
4656 
4657   indirect_string_node **slot
4658     = table->find_slot_with_hash (str, htab_hash_string (str), insert);
4659   if (*slot == NULL)
4660     {
4661       node = ggc_cleared_alloc<indirect_string_node> ();
4662       node->str = ggc_strdup (str);
4663       *slot = node;
4664     }
4665   else
4666     node = *slot;
4667 
4668   node->refcount++;
4669   return node;
4670 }
4671 
4672 /* Add STR to the indirect string hash table.  */
4673 
4674 static struct indirect_string_node *
4675 find_AT_string (const char *str, enum insert_option insert = INSERT)
4676 {
4677   if (! debug_str_hash)
4678     debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4679 
4680   return find_AT_string_in_table (str, debug_str_hash, insert);
4681 }
4682 
4683 /* Add a string attribute value to a DIE.  */
4684 
4685 static inline void
add_AT_string(dw_die_ref die,enum dwarf_attribute attr_kind,const char * str)4686 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4687 {
4688   dw_attr_node attr;
4689   struct indirect_string_node *node;
4690 
4691   node = find_AT_string (str);
4692 
4693   attr.dw_attr = attr_kind;
4694   attr.dw_attr_val.val_class = dw_val_class_str;
4695   attr.dw_attr_val.val_entry = NULL;
4696   attr.dw_attr_val.v.val_str = node;
4697   add_dwarf_attr (die, &attr);
4698 }
4699 
4700 static inline const char *
AT_string(dw_attr_node * a)4701 AT_string (dw_attr_node *a)
4702 {
4703   gcc_assert (a && AT_class (a) == dw_val_class_str);
4704   return a->dw_attr_val.v.val_str->str;
4705 }
4706 
4707 /* Call this function directly to bypass AT_string_form's logic to put
4708    the string inline in the die. */
4709 
4710 static void
set_indirect_string(struct indirect_string_node * node)4711 set_indirect_string (struct indirect_string_node *node)
4712 {
4713   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4714   /* Already indirect is a no op.  */
4715   if (node->form == DW_FORM_strp
4716       || node->form == DW_FORM_line_strp
4717       || node->form == dwarf_FORM (DW_FORM_strx))
4718     {
4719       gcc_assert (node->label);
4720       return;
4721     }
4722   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4723   ++dw2_string_counter;
4724   node->label = xstrdup (label);
4725 
4726   if (!dwarf_split_debug_info)
4727     {
4728       node->form = DW_FORM_strp;
4729       node->index = NOT_INDEXED;
4730     }
4731   else
4732     {
4733       node->form = dwarf_FORM (DW_FORM_strx);
4734       node->index = NO_INDEX_ASSIGNED;
4735     }
4736 }
4737 
4738 /* A helper function for dwarf2out_finish, called to reset indirect
4739    string decisions done for early LTO dwarf output before fat object
4740    dwarf output.  */
4741 
4742 int
reset_indirect_string(indirect_string_node ** h,void *)4743 reset_indirect_string (indirect_string_node **h, void *)
4744 {
4745   struct indirect_string_node *node = *h;
4746   if (node->form == DW_FORM_strp
4747       || node->form == DW_FORM_line_strp
4748       || node->form == dwarf_FORM (DW_FORM_strx))
4749     {
4750       free (node->label);
4751       node->label = NULL;
4752       node->form = (dwarf_form) 0;
4753       node->index = 0;
4754     }
4755   return 1;
4756 }
4757 
4758 /* Add a string representing a file or filepath attribute value to a DIE.  */
4759 
4760 static inline void
add_filepath_AT_string(dw_die_ref die,enum dwarf_attribute attr_kind,const char * str)4761 add_filepath_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
4762 			const char *str)
4763 {
4764   if (! asm_outputs_debug_line_str ())
4765     add_AT_string (die, attr_kind, str);
4766   else
4767     {
4768       dw_attr_node attr;
4769       struct indirect_string_node *node;
4770 
4771       if (!debug_line_str_hash)
4772 	debug_line_str_hash
4773 	  = hash_table<indirect_string_hasher>::create_ggc (10);
4774 
4775       node = find_AT_string_in_table (str, debug_line_str_hash);
4776       set_indirect_string (node);
4777       node->form = DW_FORM_line_strp;
4778 
4779       attr.dw_attr = attr_kind;
4780       attr.dw_attr_val.val_class = dw_val_class_str;
4781       attr.dw_attr_val.val_entry = NULL;
4782       attr.dw_attr_val.v.val_str = node;
4783       add_dwarf_attr (die, &attr);
4784     }
4785 }
4786 
4787 /* Find out whether a string should be output inline in DIE
4788    or out-of-line in .debug_str section.  */
4789 
4790 static enum dwarf_form
find_string_form(struct indirect_string_node * node)4791 find_string_form (struct indirect_string_node *node)
4792 {
4793   unsigned int len;
4794 
4795   if (node->form)
4796     return node->form;
4797 
4798   len = strlen (node->str) + 1;
4799 
4800   /* If the string is shorter or equal to the size of the reference, it is
4801      always better to put it inline.  */
4802   if (len <= (unsigned) dwarf_offset_size || node->refcount == 0)
4803     return node->form = DW_FORM_string;
4804 
4805   /* If we cannot expect the linker to merge strings in .debug_str
4806      section, only put it into .debug_str if it is worth even in this
4807      single module.  */
4808   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4809       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4810 	  && (len - dwarf_offset_size) * node->refcount <= len))
4811     return node->form = DW_FORM_string;
4812 
4813   set_indirect_string (node);
4814 
4815   return node->form;
4816 }
4817 
4818 /* Find out whether the string referenced from the attribute should be
4819    output inline in DIE or out-of-line in .debug_str section.  */
4820 
4821 static enum dwarf_form
AT_string_form(dw_attr_node * a)4822 AT_string_form (dw_attr_node *a)
4823 {
4824   gcc_assert (a && AT_class (a) == dw_val_class_str);
4825   return find_string_form (a->dw_attr_val.v.val_str);
4826 }
4827 
4828 /* Add a DIE reference attribute value to a DIE.  */
4829 
4830 static inline void
add_AT_die_ref(dw_die_ref die,enum dwarf_attribute attr_kind,dw_die_ref targ_die)4831 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4832 {
4833   dw_attr_node attr;
4834   gcc_checking_assert (targ_die != NULL);
4835 
4836   /* With LTO we can end up trying to reference something we didn't create
4837      a DIE for.  Avoid crashing later on a NULL referenced DIE.  */
4838   if (targ_die == NULL)
4839     return;
4840 
4841   attr.dw_attr = attr_kind;
4842   attr.dw_attr_val.val_class = dw_val_class_die_ref;
4843   attr.dw_attr_val.val_entry = NULL;
4844   attr.dw_attr_val.v.val_die_ref.die = targ_die;
4845   attr.dw_attr_val.v.val_die_ref.external = 0;
4846   add_dwarf_attr (die, &attr);
4847 }
4848 
4849 /* Change DIE reference REF to point to NEW_DIE instead.  */
4850 
4851 static inline void
change_AT_die_ref(dw_attr_node * ref,dw_die_ref new_die)4852 change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4853 {
4854   gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4855   ref->dw_attr_val.v.val_die_ref.die = new_die;
4856   ref->dw_attr_val.v.val_die_ref.external = 0;
4857 }
4858 
4859 /* Add an AT_specification attribute to a DIE, and also make the back
4860    pointer from the specification to the definition.  */
4861 
4862 static inline void
add_AT_specification(dw_die_ref die,dw_die_ref targ_die)4863 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4864 {
4865   add_AT_die_ref (die, DW_AT_specification, targ_die);
4866   gcc_assert (!targ_die->die_definition);
4867   targ_die->die_definition = die;
4868 }
4869 
4870 static inline dw_die_ref
AT_ref(dw_attr_node * a)4871 AT_ref (dw_attr_node *a)
4872 {
4873   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4874   return a->dw_attr_val.v.val_die_ref.die;
4875 }
4876 
4877 static inline int
AT_ref_external(dw_attr_node * a)4878 AT_ref_external (dw_attr_node *a)
4879 {
4880   if (a && AT_class (a) == dw_val_class_die_ref)
4881     return a->dw_attr_val.v.val_die_ref.external;
4882 
4883   return 0;
4884 }
4885 
4886 static inline void
set_AT_ref_external(dw_attr_node * a,int i)4887 set_AT_ref_external (dw_attr_node *a, int i)
4888 {
4889   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4890   a->dw_attr_val.v.val_die_ref.external = i;
4891 }
4892 
4893 /* Add a location description attribute value to a DIE.  */
4894 
4895 static inline void
add_AT_loc(dw_die_ref die,enum dwarf_attribute attr_kind,dw_loc_descr_ref loc)4896 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4897 {
4898   dw_attr_node attr;
4899 
4900   attr.dw_attr = attr_kind;
4901   attr.dw_attr_val.val_class = dw_val_class_loc;
4902   attr.dw_attr_val.val_entry = NULL;
4903   attr.dw_attr_val.v.val_loc = loc;
4904   add_dwarf_attr (die, &attr);
4905 }
4906 
4907 static inline dw_loc_descr_ref
AT_loc(dw_attr_node * a)4908 AT_loc (dw_attr_node *a)
4909 {
4910   gcc_assert (a && AT_class (a) == dw_val_class_loc);
4911   return a->dw_attr_val.v.val_loc;
4912 }
4913 
4914 static inline void
add_AT_loc_list(dw_die_ref die,enum dwarf_attribute attr_kind,dw_loc_list_ref loc_list)4915 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4916 {
4917   dw_attr_node attr;
4918 
4919   if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4920     return;
4921 
4922   attr.dw_attr = attr_kind;
4923   attr.dw_attr_val.val_class = dw_val_class_loc_list;
4924   attr.dw_attr_val.val_entry = NULL;
4925   attr.dw_attr_val.v.val_loc_list = loc_list;
4926   add_dwarf_attr (die, &attr);
4927   have_location_lists = true;
4928 }
4929 
4930 static inline dw_loc_list_ref
AT_loc_list(dw_attr_node * a)4931 AT_loc_list (dw_attr_node *a)
4932 {
4933   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4934   return a->dw_attr_val.v.val_loc_list;
4935 }
4936 
4937 /* Add a view list attribute to DIE.  It must have a DW_AT_location
4938    attribute, because the view list complements the location list.  */
4939 
4940 static inline void
add_AT_view_list(dw_die_ref die,enum dwarf_attribute attr_kind)4941 add_AT_view_list (dw_die_ref die, enum dwarf_attribute attr_kind)
4942 {
4943   dw_attr_node attr;
4944 
4945   if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4946     return;
4947 
4948   attr.dw_attr = attr_kind;
4949   attr.dw_attr_val.val_class = dw_val_class_view_list;
4950   attr.dw_attr_val.val_entry = NULL;
4951   attr.dw_attr_val.v.val_view_list = die;
4952   add_dwarf_attr (die, &attr);
4953   gcc_checking_assert (get_AT (die, DW_AT_location));
4954   gcc_assert (have_location_lists);
4955 }
4956 
4957 /* Return a pointer to the location list referenced by the attribute.
4958    If the named attribute is a view list, look up the corresponding
4959    DW_AT_location attribute and return its location list.  */
4960 
4961 static inline dw_loc_list_ref *
AT_loc_list_ptr(dw_attr_node * a)4962 AT_loc_list_ptr (dw_attr_node *a)
4963 {
4964   gcc_assert (a);
4965   switch (AT_class (a))
4966     {
4967     case dw_val_class_loc_list:
4968       return &a->dw_attr_val.v.val_loc_list;
4969     case dw_val_class_view_list:
4970       {
4971 	dw_attr_node *l;
4972 	l = get_AT (a->dw_attr_val.v.val_view_list, DW_AT_location);
4973 	if (!l)
4974 	  return NULL;
4975 	gcc_checking_assert (l + 1 == a);
4976 	return AT_loc_list_ptr (l);
4977       }
4978     default:
4979       gcc_unreachable ();
4980     }
4981 }
4982 
4983 /* Return the location attribute value associated with a view list
4984    attribute value.  */
4985 
4986 static inline dw_val_node *
view_list_to_loc_list_val_node(dw_val_node * val)4987 view_list_to_loc_list_val_node (dw_val_node *val)
4988 {
4989   gcc_assert (val->val_class == dw_val_class_view_list);
4990   dw_attr_node *loc = get_AT (val->v.val_view_list, DW_AT_location);
4991   if (!loc)
4992     return NULL;
4993   gcc_checking_assert (&(loc + 1)->dw_attr_val == val);
4994   gcc_assert (AT_class (loc) == dw_val_class_loc_list);
4995   return &loc->dw_attr_val;
4996 }
4997 
4998 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
4999 {
5000   static hashval_t hash (addr_table_entry *);
5001   static bool equal (addr_table_entry *, addr_table_entry *);
5002 };
5003 
5004 /* Table of entries into the .debug_addr section.  */
5005 
5006 static GTY (()) hash_table<addr_hasher> *addr_index_table;
5007 
5008 /* Hash an address_table_entry.  */
5009 
5010 hashval_t
hash(addr_table_entry * a)5011 addr_hasher::hash (addr_table_entry *a)
5012 {
5013   inchash::hash hstate;
5014   switch (a->kind)
5015     {
5016       case ate_kind_rtx:
5017 	hstate.add_int (0);
5018 	break;
5019       case ate_kind_rtx_dtprel:
5020 	hstate.add_int (1);
5021 	break;
5022       case ate_kind_label:
5023         return htab_hash_string (a->addr.label);
5024       default:
5025         gcc_unreachable ();
5026     }
5027   inchash::add_rtx (a->addr.rtl, hstate);
5028   return hstate.end ();
5029 }
5030 
5031 /* Determine equality for two address_table_entries.  */
5032 
5033 bool
equal(addr_table_entry * a1,addr_table_entry * a2)5034 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
5035 {
5036   if (a1->kind != a2->kind)
5037     return 0;
5038   switch (a1->kind)
5039     {
5040       case ate_kind_rtx:
5041       case ate_kind_rtx_dtprel:
5042         return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
5043       case ate_kind_label:
5044         return strcmp (a1->addr.label, a2->addr.label) == 0;
5045       default:
5046         gcc_unreachable ();
5047     }
5048 }
5049 
5050 /* Initialize an addr_table_entry.  */
5051 
5052 void
init_addr_table_entry(addr_table_entry * e,enum ate_kind kind,void * addr)5053 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
5054 {
5055   e->kind = kind;
5056   switch (kind)
5057     {
5058       case ate_kind_rtx:
5059       case ate_kind_rtx_dtprel:
5060         e->addr.rtl = (rtx) addr;
5061         break;
5062       case ate_kind_label:
5063         e->addr.label = (char *) addr;
5064         break;
5065     }
5066   e->refcount = 0;
5067   e->index = NO_INDEX_ASSIGNED;
5068 }
5069 
5070 /* Add attr to the address table entry to the table.  Defer setting an
5071    index until output time.  */
5072 
5073 static addr_table_entry *
add_addr_table_entry(void * addr,enum ate_kind kind)5074 add_addr_table_entry (void *addr, enum ate_kind kind)
5075 {
5076   addr_table_entry *node;
5077   addr_table_entry finder;
5078 
5079   gcc_assert (dwarf_split_debug_info);
5080   if (! addr_index_table)
5081     addr_index_table = hash_table<addr_hasher>::create_ggc (10);
5082   init_addr_table_entry (&finder, kind, addr);
5083   addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
5084 
5085   if (*slot == HTAB_EMPTY_ENTRY)
5086     {
5087       node = ggc_cleared_alloc<addr_table_entry> ();
5088       init_addr_table_entry (node, kind, addr);
5089       *slot = node;
5090     }
5091   else
5092     node = *slot;
5093 
5094   node->refcount++;
5095   return node;
5096 }
5097 
5098 /* Remove an entry from the addr table by decrementing its refcount.
5099    Strictly, decrementing the refcount would be enough, but the
5100    assertion that the entry is actually in the table has found
5101    bugs.  */
5102 
5103 static void
remove_addr_table_entry(addr_table_entry * entry)5104 remove_addr_table_entry (addr_table_entry *entry)
5105 {
5106   gcc_assert (dwarf_split_debug_info && addr_index_table);
5107   /* After an index is assigned, the table is frozen.  */
5108   gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
5109   entry->refcount--;
5110 }
5111 
5112 /* Given a location list, remove all addresses it refers to from the
5113    address_table.  */
5114 
5115 static void
remove_loc_list_addr_table_entries(dw_loc_descr_ref descr)5116 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
5117 {
5118   for (; descr; descr = descr->dw_loc_next)
5119     if (descr->dw_loc_oprnd1.val_entry != NULL)
5120       {
5121         gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
5122         remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
5123       }
5124 }
5125 
5126 /* A helper function for dwarf2out_finish called through
5127    htab_traverse.  Assign an addr_table_entry its index.  All entries
5128    must be collected into the table when this function is called,
5129    because the indexing code relies on htab_traverse to traverse nodes
5130    in the same order for each run. */
5131 
5132 int
index_addr_table_entry(addr_table_entry ** h,unsigned int * index)5133 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
5134 {
5135   addr_table_entry *node = *h;
5136 
5137   /* Don't index unreferenced nodes.  */
5138   if (node->refcount == 0)
5139     return 1;
5140 
5141   gcc_assert (node->index == NO_INDEX_ASSIGNED);
5142   node->index = *index;
5143   *index += 1;
5144 
5145   return 1;
5146 }
5147 
5148 /* Add an address constant attribute value to a DIE.  When using
5149    dwarf_split_debug_info, address attributes in dies destined for the
5150    final executable should be direct references--setting the parameter
5151    force_direct ensures this behavior.  */
5152 
5153 static inline void
add_AT_addr(dw_die_ref die,enum dwarf_attribute attr_kind,rtx addr,bool force_direct)5154 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
5155              bool force_direct)
5156 {
5157   dw_attr_node attr;
5158 
5159   attr.dw_attr = attr_kind;
5160   attr.dw_attr_val.val_class = dw_val_class_addr;
5161   attr.dw_attr_val.v.val_addr = addr;
5162   if (dwarf_split_debug_info && !force_direct)
5163     attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
5164   else
5165     attr.dw_attr_val.val_entry = NULL;
5166   add_dwarf_attr (die, &attr);
5167 }
5168 
5169 /* Get the RTX from to an address DIE attribute.  */
5170 
5171 static inline rtx
AT_addr(dw_attr_node * a)5172 AT_addr (dw_attr_node *a)
5173 {
5174   gcc_assert (a && AT_class (a) == dw_val_class_addr);
5175   return a->dw_attr_val.v.val_addr;
5176 }
5177 
5178 /* Add a file attribute value to a DIE.  */
5179 
5180 static inline void
add_AT_file(dw_die_ref die,enum dwarf_attribute attr_kind,struct dwarf_file_data * fd)5181 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
5182 	     struct dwarf_file_data *fd)
5183 {
5184   dw_attr_node attr;
5185 
5186   attr.dw_attr = attr_kind;
5187   attr.dw_attr_val.val_class = dw_val_class_file;
5188   attr.dw_attr_val.val_entry = NULL;
5189   attr.dw_attr_val.v.val_file = fd;
5190   add_dwarf_attr (die, &attr);
5191 }
5192 
5193 /* Get the dwarf_file_data from a file DIE attribute.  */
5194 
5195 static inline struct dwarf_file_data *
AT_file(dw_attr_node * a)5196 AT_file (dw_attr_node *a)
5197 {
5198   gcc_assert (a && (AT_class (a) == dw_val_class_file
5199 		    || AT_class (a) == dw_val_class_file_implicit));
5200   return a->dw_attr_val.v.val_file;
5201 }
5202 
5203 /* Add a vms delta attribute value to a DIE.  */
5204 
5205 static inline void
add_AT_vms_delta(dw_die_ref die,enum dwarf_attribute attr_kind,const char * lbl1,const char * lbl2)5206 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
5207 		  const char *lbl1, const char *lbl2)
5208 {
5209   dw_attr_node attr;
5210 
5211   attr.dw_attr = attr_kind;
5212   attr.dw_attr_val.val_class = dw_val_class_vms_delta;
5213   attr.dw_attr_val.val_entry = NULL;
5214   attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
5215   attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
5216   add_dwarf_attr (die, &attr);
5217 }
5218 
5219 /* Add a symbolic view identifier attribute value to a DIE.  */
5220 
5221 static inline void
add_AT_symview(dw_die_ref die,enum dwarf_attribute attr_kind,const char * view_label)5222 add_AT_symview (dw_die_ref die, enum dwarf_attribute attr_kind,
5223                const char *view_label)
5224 {
5225   dw_attr_node attr;
5226 
5227   attr.dw_attr = attr_kind;
5228   attr.dw_attr_val.val_class = dw_val_class_symview;
5229   attr.dw_attr_val.val_entry = NULL;
5230   attr.dw_attr_val.v.val_symbolic_view = xstrdup (view_label);
5231   add_dwarf_attr (die, &attr);
5232 }
5233 
5234 /* Add a label identifier attribute value to a DIE.  */
5235 
5236 static inline void
add_AT_lbl_id(dw_die_ref die,enum dwarf_attribute attr_kind,const char * lbl_id)5237 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
5238                const char *lbl_id)
5239 {
5240   dw_attr_node attr;
5241 
5242   attr.dw_attr = attr_kind;
5243   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
5244   attr.dw_attr_val.val_entry = NULL;
5245   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
5246   if (dwarf_split_debug_info)
5247     attr.dw_attr_val.val_entry
5248         = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
5249                                 ate_kind_label);
5250   add_dwarf_attr (die, &attr);
5251 }
5252 
5253 /* Add a section offset attribute value to a DIE, an offset into the
5254    debug_line section.  */
5255 
5256 static inline void
add_AT_lineptr(dw_die_ref die,enum dwarf_attribute attr_kind,const char * label)5257 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5258 		const char *label)
5259 {
5260   dw_attr_node attr;
5261 
5262   attr.dw_attr = attr_kind;
5263   attr.dw_attr_val.val_class = dw_val_class_lineptr;
5264   attr.dw_attr_val.val_entry = NULL;
5265   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5266   add_dwarf_attr (die, &attr);
5267 }
5268 
5269 /* Add a section offset attribute value to a DIE, an offset into the
5270    debug_macinfo section.  */
5271 
5272 static inline void
add_AT_macptr(dw_die_ref die,enum dwarf_attribute attr_kind,const char * label)5273 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5274 	       const char *label)
5275 {
5276   dw_attr_node attr;
5277 
5278   attr.dw_attr = attr_kind;
5279   attr.dw_attr_val.val_class = dw_val_class_macptr;
5280   attr.dw_attr_val.val_entry = NULL;
5281   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5282   add_dwarf_attr (die, &attr);
5283 }
5284 
5285 /* Add a range_list attribute value to a DIE.  When using
5286    dwarf_split_debug_info, address attributes in dies destined for the
5287    final executable should be direct references--setting the parameter
5288    force_direct ensures this behavior.  */
5289 
5290 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
5291 #define RELOCATED_OFFSET (NULL)
5292 
5293 static void
add_AT_range_list(dw_die_ref die,enum dwarf_attribute attr_kind,long unsigned int offset,bool force_direct)5294 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5295                    long unsigned int offset, bool force_direct)
5296 {
5297   dw_attr_node attr;
5298 
5299   attr.dw_attr = attr_kind;
5300   attr.dw_attr_val.val_class = dw_val_class_range_list;
5301   /* For the range_list attribute, use val_entry to store whether the
5302      offset should follow split-debug-info or normal semantics.  This
5303      value is read in output_range_list_offset.  */
5304   if (dwarf_split_debug_info && !force_direct)
5305     attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
5306   else
5307     attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
5308   attr.dw_attr_val.v.val_offset = offset;
5309   add_dwarf_attr (die, &attr);
5310 }
5311 
5312 /* Return the start label of a delta attribute.  */
5313 
5314 static inline const char *
AT_vms_delta1(dw_attr_node * a)5315 AT_vms_delta1 (dw_attr_node *a)
5316 {
5317   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
5318   return a->dw_attr_val.v.val_vms_delta.lbl1;
5319 }
5320 
5321 /* Return the end label of a delta attribute.  */
5322 
5323 static inline const char *
AT_vms_delta2(dw_attr_node * a)5324 AT_vms_delta2 (dw_attr_node *a)
5325 {
5326   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
5327   return a->dw_attr_val.v.val_vms_delta.lbl2;
5328 }
5329 
5330 static inline const char *
AT_lbl(dw_attr_node * a)5331 AT_lbl (dw_attr_node *a)
5332 {
5333   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
5334 		    || AT_class (a) == dw_val_class_lineptr
5335 		    || AT_class (a) == dw_val_class_macptr
5336 		    || AT_class (a) == dw_val_class_loclistsptr
5337 		    || AT_class (a) == dw_val_class_high_pc));
5338   return a->dw_attr_val.v.val_lbl_id;
5339 }
5340 
5341 /* Get the attribute of type attr_kind.  */
5342 
5343 static dw_attr_node *
get_AT(dw_die_ref die,enum dwarf_attribute attr_kind)5344 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5345 {
5346   dw_attr_node *a;
5347   unsigned ix;
5348   dw_die_ref spec = NULL;
5349 
5350   if (! die)
5351     return NULL;
5352 
5353   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5354     if (a->dw_attr == attr_kind)
5355       return a;
5356     else if (a->dw_attr == DW_AT_specification
5357 	     || a->dw_attr == DW_AT_abstract_origin)
5358       spec = AT_ref (a);
5359 
5360   if (spec)
5361     return get_AT (spec, attr_kind);
5362 
5363   return NULL;
5364 }
5365 
5366 /* Returns the parent of the declaration of DIE.  */
5367 
5368 static dw_die_ref
get_die_parent(dw_die_ref die)5369 get_die_parent (dw_die_ref die)
5370 {
5371   dw_die_ref t;
5372 
5373   if (!die)
5374     return NULL;
5375 
5376   if ((t = get_AT_ref (die, DW_AT_abstract_origin))
5377       || (t = get_AT_ref (die, DW_AT_specification)))
5378     die = t;
5379 
5380   return die->die_parent;
5381 }
5382 
5383 /* Return the "low pc" attribute value, typically associated with a subprogram
5384    DIE.  Return null if the "low pc" attribute is either not present, or if it
5385    cannot be represented as an assembler label identifier.  */
5386 
5387 static inline const char *
get_AT_low_pc(dw_die_ref die)5388 get_AT_low_pc (dw_die_ref die)
5389 {
5390   dw_attr_node *a = get_AT (die, DW_AT_low_pc);
5391 
5392   return a ? AT_lbl (a) : NULL;
5393 }
5394 
5395 /* Return the value of the string attribute designated by ATTR_KIND, or
5396    NULL if it is not present.  */
5397 
5398 static inline const char *
get_AT_string(dw_die_ref die,enum dwarf_attribute attr_kind)5399 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5400 {
5401   dw_attr_node *a = get_AT (die, attr_kind);
5402 
5403   return a ? AT_string (a) : NULL;
5404 }
5405 
5406 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5407    if it is not present.  */
5408 
5409 static inline int
get_AT_flag(dw_die_ref die,enum dwarf_attribute attr_kind)5410 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5411 {
5412   dw_attr_node *a = get_AT (die, attr_kind);
5413 
5414   return a ? AT_flag (a) : 0;
5415 }
5416 
5417 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5418    if it is not present.  */
5419 
5420 static inline unsigned
get_AT_unsigned(dw_die_ref die,enum dwarf_attribute attr_kind)5421 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5422 {
5423   dw_attr_node *a = get_AT (die, attr_kind);
5424 
5425   return a ? AT_unsigned (a) : 0;
5426 }
5427 
5428 static inline dw_die_ref
get_AT_ref(dw_die_ref die,enum dwarf_attribute attr_kind)5429 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5430 {
5431   dw_attr_node *a = get_AT (die, attr_kind);
5432 
5433   return a ? AT_ref (a) : NULL;
5434 }
5435 
5436 static inline struct dwarf_file_data *
get_AT_file(dw_die_ref die,enum dwarf_attribute attr_kind)5437 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5438 {
5439   dw_attr_node *a = get_AT (die, attr_kind);
5440 
5441   return a ? AT_file (a) : NULL;
5442 }
5443 
5444 /* Return TRUE if the language is C.  */
5445 
5446 static inline bool
is_c(void)5447 is_c (void)
5448 {
5449   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5450 
5451   return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_C99
5452 	  || lang == DW_LANG_C11 || lang == DW_LANG_ObjC);
5453 
5454 
5455 }
5456 
5457 /* Return TRUE if the language is C++.  */
5458 
5459 static inline bool
is_cxx(void)5460 is_cxx (void)
5461 {
5462   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5463 
5464   return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
5465 	  || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
5466 }
5467 
5468 /* Return TRUE if DECL was created by the C++ frontend.  */
5469 
5470 static bool
is_cxx(const_tree decl)5471 is_cxx (const_tree decl)
5472 {
5473   if (in_lto_p)
5474     {
5475       const_tree context = get_ultimate_context (decl);
5476       if (context && TRANSLATION_UNIT_LANGUAGE (context))
5477 	return strncmp (TRANSLATION_UNIT_LANGUAGE (context), "GNU C++", 7) == 0;
5478     }
5479   return is_cxx ();
5480 }
5481 
5482 /* Return TRUE if the language is Fortran.  */
5483 
5484 static inline bool
is_fortran(void)5485 is_fortran (void)
5486 {
5487   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5488 
5489   return (lang == DW_LANG_Fortran77
5490 	  || lang == DW_LANG_Fortran90
5491 	  || lang == DW_LANG_Fortran95
5492 	  || lang == DW_LANG_Fortran03
5493 	  || lang == DW_LANG_Fortran08);
5494 }
5495 
5496 static inline bool
is_fortran(const_tree decl)5497 is_fortran (const_tree decl)
5498 {
5499   if (in_lto_p)
5500     {
5501       const_tree context = get_ultimate_context (decl);
5502       if (context && TRANSLATION_UNIT_LANGUAGE (context))
5503 	return (strncmp (TRANSLATION_UNIT_LANGUAGE (context),
5504 			 "GNU Fortran", 11) == 0
5505 		|| strcmp (TRANSLATION_UNIT_LANGUAGE (context),
5506 			   "GNU F77") == 0);
5507     }
5508   return is_fortran ();
5509 }
5510 
5511 /* Return TRUE if the language is Ada.  */
5512 
5513 static inline bool
is_ada(void)5514 is_ada (void)
5515 {
5516   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5517 
5518   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5519 }
5520 
5521 /* Return TRUE if the language is D.  */
5522 
5523 static inline bool
is_dlang(void)5524 is_dlang (void)
5525 {
5526   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5527 
5528   return lang == DW_LANG_D;
5529 }
5530 
5531 /* Remove the specified attribute if present.  Return TRUE if removal
5532    was successful.  */
5533 
5534 static bool
remove_AT(dw_die_ref die,enum dwarf_attribute attr_kind)5535 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5536 {
5537   dw_attr_node *a;
5538   unsigned ix;
5539 
5540   if (! die)
5541     return false;
5542 
5543   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5544     if (a->dw_attr == attr_kind)
5545       {
5546 	if (AT_class (a) == dw_val_class_str)
5547 	  if (a->dw_attr_val.v.val_str->refcount)
5548 	    a->dw_attr_val.v.val_str->refcount--;
5549 
5550 	/* vec::ordered_remove should help reduce the number of abbrevs
5551 	   that are needed.  */
5552 	die->die_attr->ordered_remove (ix);
5553 	return true;
5554       }
5555   return false;
5556 }
5557 
5558 /* Remove CHILD from its parent.  PREV must have the property that
5559    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
5560 
5561 static void
remove_child_with_prev(dw_die_ref child,dw_die_ref prev)5562 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5563 {
5564   gcc_assert (child->die_parent == prev->die_parent);
5565   gcc_assert (prev->die_sib == child);
5566   if (prev == child)
5567     {
5568       gcc_assert (child->die_parent->die_child == child);
5569       prev = NULL;
5570     }
5571   else
5572     prev->die_sib = child->die_sib;
5573   if (child->die_parent->die_child == child)
5574     child->die_parent->die_child = prev;
5575   child->die_sib = NULL;
5576 }
5577 
5578 /* Replace OLD_CHILD with NEW_CHILD.  PREV must have the property that
5579    PREV->DIE_SIB == OLD_CHILD.  Does not alter OLD_CHILD.  */
5580 
5581 static void
replace_child(dw_die_ref old_child,dw_die_ref new_child,dw_die_ref prev)5582 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
5583 {
5584   dw_die_ref parent = old_child->die_parent;
5585 
5586   gcc_assert (parent == prev->die_parent);
5587   gcc_assert (prev->die_sib == old_child);
5588 
5589   new_child->die_parent = parent;
5590   if (prev == old_child)
5591     {
5592       gcc_assert (parent->die_child == old_child);
5593       new_child->die_sib = new_child;
5594     }
5595   else
5596     {
5597       prev->die_sib = new_child;
5598       new_child->die_sib = old_child->die_sib;
5599     }
5600   if (old_child->die_parent->die_child == old_child)
5601     old_child->die_parent->die_child = new_child;
5602   old_child->die_sib = NULL;
5603 }
5604 
5605 /* Move all children from OLD_PARENT to NEW_PARENT.  */
5606 
5607 static void
move_all_children(dw_die_ref old_parent,dw_die_ref new_parent)5608 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
5609 {
5610   dw_die_ref c;
5611   new_parent->die_child = old_parent->die_child;
5612   old_parent->die_child = NULL;
5613   FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
5614 }
5615 
5616 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
5617    matches TAG.  */
5618 
5619 static void
remove_child_TAG(dw_die_ref die,enum dwarf_tag tag)5620 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5621 {
5622   dw_die_ref c;
5623 
5624   c = die->die_child;
5625   if (c) do {
5626     dw_die_ref prev = c;
5627     c = c->die_sib;
5628     while (c->die_tag == tag)
5629       {
5630 	remove_child_with_prev (c, prev);
5631 	c->die_parent = NULL;
5632 	/* Might have removed every child.  */
5633 	if (die->die_child == NULL)
5634 	  return;
5635 	c = prev->die_sib;
5636       }
5637   } while (c != die->die_child);
5638 }
5639 
5640 /* Add a CHILD_DIE as the last child of DIE.  */
5641 
5642 static void
add_child_die(dw_die_ref die,dw_die_ref child_die)5643 add_child_die (dw_die_ref die, dw_die_ref child_die)
5644 {
5645   /* FIXME this should probably be an assert.  */
5646   if (! die || ! child_die)
5647     return;
5648   gcc_assert (die != child_die);
5649 
5650   child_die->die_parent = die;
5651   if (die->die_child)
5652     {
5653       child_die->die_sib = die->die_child->die_sib;
5654       die->die_child->die_sib = child_die;
5655     }
5656   else
5657     child_die->die_sib = child_die;
5658   die->die_child = child_die;
5659 }
5660 
5661 /* Like add_child_die, but put CHILD_DIE after AFTER_DIE.  */
5662 
5663 static void
add_child_die_after(dw_die_ref die,dw_die_ref child_die,dw_die_ref after_die)5664 add_child_die_after (dw_die_ref die, dw_die_ref child_die,
5665 		     dw_die_ref after_die)
5666 {
5667   gcc_assert (die
5668 	      && child_die
5669 	      && after_die
5670 	      && die->die_child
5671 	      && die != child_die);
5672 
5673   child_die->die_parent = die;
5674   child_die->die_sib = after_die->die_sib;
5675   after_die->die_sib = child_die;
5676   if (die->die_child == after_die)
5677     die->die_child = child_die;
5678 }
5679 
5680 /* Unassociate CHILD from its parent, and make its parent be
5681    NEW_PARENT.  */
5682 
5683 static void
reparent_child(dw_die_ref child,dw_die_ref new_parent)5684 reparent_child (dw_die_ref child, dw_die_ref new_parent)
5685 {
5686   for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
5687     if (p->die_sib == child)
5688       {
5689 	remove_child_with_prev (child, p);
5690 	break;
5691       }
5692   add_child_die (new_parent, child);
5693 }
5694 
5695 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5696    is the specification, to the end of PARENT's list of children.
5697    This is done by removing and re-adding it.  */
5698 
5699 static void
splice_child_die(dw_die_ref parent,dw_die_ref child)5700 splice_child_die (dw_die_ref parent, dw_die_ref child)
5701 {
5702   /* We want the declaration DIE from inside the class, not the
5703      specification DIE at toplevel.  */
5704   if (child->die_parent != parent)
5705     {
5706       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5707 
5708       if (tmp)
5709 	child = tmp;
5710     }
5711 
5712   gcc_assert (child->die_parent == parent
5713 	      || (child->die_parent
5714 		  == get_AT_ref (parent, DW_AT_specification)));
5715 
5716   reparent_child (child, parent);
5717 }
5718 
5719 /* Create and return a new die with TAG_VALUE as tag.  */
5720 
5721 static inline dw_die_ref
new_die_raw(enum dwarf_tag tag_value)5722 new_die_raw (enum dwarf_tag tag_value)
5723 {
5724   dw_die_ref die = ggc_cleared_alloc<die_node> ();
5725   die->die_tag = tag_value;
5726   return die;
5727 }
5728 
5729 /* Create and return a new die with a parent of PARENT_DIE.  If
5730    PARENT_DIE is NULL, the new DIE is placed in limbo and an
5731    associated tree T must be supplied to determine parenthood
5732    later.  */
5733 
5734 static inline dw_die_ref
new_die(enum dwarf_tag tag_value,dw_die_ref parent_die,tree t)5735 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5736 {
5737   dw_die_ref die = new_die_raw (tag_value);
5738 
5739   if (parent_die != NULL)
5740     add_child_die (parent_die, die);
5741   else
5742     {
5743       limbo_die_node *limbo_node;
5744 
5745       /* No DIEs created after early dwarf should end up in limbo,
5746 	 because the limbo list should not persist past LTO
5747 	 streaming.  */
5748       if (tag_value != DW_TAG_compile_unit
5749 	  /* These are allowed because they're generated while
5750 	     breaking out COMDAT units late.  */
5751 	  && tag_value != DW_TAG_type_unit
5752 	  && tag_value != DW_TAG_skeleton_unit
5753 	  && !early_dwarf
5754 	  /* Allow nested functions to live in limbo because they will
5755 	     only temporarily live there, as decls_for_scope will fix
5756 	     them up.  */
5757 	  && (TREE_CODE (t) != FUNCTION_DECL
5758 	      || !decl_function_context (t))
5759 	  /* Same as nested functions above but for types.  Types that
5760 	     are local to a function will be fixed in
5761 	     decls_for_scope.  */
5762 	  && (!RECORD_OR_UNION_TYPE_P (t)
5763 	      || !TYPE_CONTEXT (t)
5764 	      || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
5765 	  /* FIXME debug-early: Allow late limbo DIE creation for LTO,
5766 	     especially in the ltrans stage, but once we implement LTO
5767 	     dwarf streaming, we should remove this exception.  */
5768 	  && !in_lto_p)
5769 	{
5770 	  fprintf (stderr, "symbol ended up in limbo too late:");
5771 	  debug_generic_stmt (t);
5772 	  gcc_unreachable ();
5773 	}
5774 
5775       limbo_node = ggc_cleared_alloc<limbo_die_node> ();
5776       limbo_node->die = die;
5777       limbo_node->created_for = t;
5778       limbo_node->next = limbo_die_list;
5779       limbo_die_list = limbo_node;
5780     }
5781 
5782   return die;
5783 }
5784 
5785 /* Return the DIE associated with the given type specifier.  */
5786 
5787 static inline dw_die_ref
lookup_type_die(tree type)5788 lookup_type_die (tree type)
5789 {
5790   dw_die_ref die = TYPE_SYMTAB_DIE (type);
5791   if (die && die->removed)
5792     {
5793       TYPE_SYMTAB_DIE (type) = NULL;
5794       return NULL;
5795     }
5796   return die;
5797 }
5798 
5799 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5800    anonymous type named by the typedef TYPE_DIE, return the DIE of the
5801    anonymous type instead the one of the naming typedef.  */
5802 
5803 static inline dw_die_ref
strip_naming_typedef(tree type,dw_die_ref type_die)5804 strip_naming_typedef (tree type, dw_die_ref type_die)
5805 {
5806   if (type
5807       && TREE_CODE (type) == RECORD_TYPE
5808       && type_die
5809       && type_die->die_tag == DW_TAG_typedef
5810       && is_naming_typedef_decl (TYPE_NAME (type)))
5811     type_die = get_AT_ref (type_die, DW_AT_type);
5812   return type_die;
5813 }
5814 
5815 /* Like lookup_type_die, but if type is an anonymous type named by a
5816    typedef[1], return the DIE of the anonymous type instead the one of
5817    the naming typedef.  This is because in gen_typedef_die, we did
5818    equate the anonymous struct named by the typedef with the DIE of
5819    the naming typedef. So by default, lookup_type_die on an anonymous
5820    struct yields the DIE of the naming typedef.
5821 
5822    [1]: Read the comment of is_naming_typedef_decl to learn about what
5823    a naming typedef is.  */
5824 
5825 static inline dw_die_ref
lookup_type_die_strip_naming_typedef(tree type)5826 lookup_type_die_strip_naming_typedef (tree type)
5827 {
5828   dw_die_ref die = lookup_type_die (type);
5829   return strip_naming_typedef (type, die);
5830 }
5831 
5832 /* Equate a DIE to a given type specifier.  */
5833 
5834 static inline void
equate_type_number_to_die(tree type,dw_die_ref type_die)5835 equate_type_number_to_die (tree type, dw_die_ref type_die)
5836 {
5837   TYPE_SYMTAB_DIE (type) = type_die;
5838 }
5839 
5840 static dw_die_ref maybe_create_die_with_external_ref (tree);
5841 struct GTY(()) sym_off_pair
5842 {
5843   const char * GTY((skip)) sym;
5844   unsigned HOST_WIDE_INT off;
5845 };
5846 static GTY(()) hash_map<tree, sym_off_pair> *external_die_map;
5847 
5848 /* Returns a hash value for X (which really is a die_struct).  */
5849 
5850 inline hashval_t
hash(die_node * x)5851 decl_die_hasher::hash (die_node *x)
5852 {
5853   return (hashval_t) x->decl_id;
5854 }
5855 
5856 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
5857 
5858 inline bool
equal(die_node * x,tree y)5859 decl_die_hasher::equal (die_node *x, tree y)
5860 {
5861   return (x->decl_id == DECL_UID (y));
5862 }
5863 
5864 /* Return the DIE associated with a given declaration.  */
5865 
5866 static inline dw_die_ref
lookup_decl_die(tree decl)5867 lookup_decl_die (tree decl)
5868 {
5869   dw_die_ref *die = decl_die_table->find_slot_with_hash (decl, DECL_UID (decl),
5870 							 NO_INSERT);
5871   if (!die)
5872     {
5873       if (in_lto_p)
5874 	return maybe_create_die_with_external_ref (decl);
5875       return NULL;
5876     }
5877   if ((*die)->removed)
5878     {
5879       decl_die_table->clear_slot (die);
5880       return NULL;
5881     }
5882   return *die;
5883 }
5884 
5885 
5886 /* Return the DIE associated with BLOCK.  */
5887 
5888 static inline dw_die_ref
lookup_block_die(tree block)5889 lookup_block_die (tree block)
5890 {
5891   dw_die_ref die = BLOCK_DIE (block);
5892   if (!die && in_lto_p)
5893     return maybe_create_die_with_external_ref (block);
5894   return die;
5895 }
5896 
5897 /* Associate DIE with BLOCK.  */
5898 
5899 static inline void
equate_block_to_die(tree block,dw_die_ref die)5900 equate_block_to_die (tree block, dw_die_ref die)
5901 {
5902   BLOCK_DIE (block) = die;
5903 }
5904 #undef BLOCK_DIE
5905 
5906 
5907 /* For DECL which might have early dwarf output query a SYMBOL + OFFSET
5908    style reference.  Return true if we found one refering to a DIE for
5909    DECL, otherwise return false.  */
5910 
5911 static bool
dwarf2out_die_ref_for_decl(tree decl,const char ** sym,unsigned HOST_WIDE_INT * off)5912 dwarf2out_die_ref_for_decl (tree decl, const char **sym,
5913 			    unsigned HOST_WIDE_INT *off)
5914 {
5915   dw_die_ref die;
5916 
5917   if (in_lto_p)
5918     {
5919       /* During WPA stage and incremental linking we use a hash-map
5920 	 to store the decl <-> label + offset map.  */
5921       if (!external_die_map)
5922 	return false;
5923       sym_off_pair *desc = external_die_map->get (decl);
5924       if (!desc)
5925 	return false;
5926       *sym = desc->sym;
5927       *off = desc->off;
5928       return true;
5929     }
5930 
5931   if (TREE_CODE (decl) == BLOCK)
5932     die = lookup_block_die (decl);
5933   else
5934     die = lookup_decl_die (decl);
5935   if (!die)
5936     return false;
5937 
5938   /* Similar to get_ref_die_offset_label, but using the "correct"
5939      label.  */
5940   *off = die->die_offset;
5941   while (die->die_parent)
5942     die = die->die_parent;
5943   /* For the containing CU DIE we compute a die_symbol in
5944      compute_comp_unit_symbol.  */
5945   gcc_assert (die->die_tag == DW_TAG_compile_unit
5946 	      && die->die_id.die_symbol != NULL);
5947   *sym = die->die_id.die_symbol;
5948   return true;
5949 }
5950 
5951 /* Add a reference of kind ATTR_KIND to a DIE at SYMBOL + OFFSET to DIE.  */
5952 
5953 static void
add_AT_external_die_ref(dw_die_ref die,enum dwarf_attribute attr_kind,const char * symbol,HOST_WIDE_INT offset)5954 add_AT_external_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind,
5955 			 const char *symbol, HOST_WIDE_INT offset)
5956 {
5957   /* Create a fake DIE that contains the reference.  Don't use
5958      new_die because we don't want to end up in the limbo list.  */
5959   /* ???  We probably want to share these, thus put a ref to the DIE
5960      we create here to the external_die_map entry.  */
5961   dw_die_ref ref = new_die_raw (die->die_tag);
5962   ref->die_id.die_symbol = symbol;
5963   ref->die_offset = offset;
5964   ref->with_offset = 1;
5965   add_AT_die_ref (die, attr_kind, ref);
5966 }
5967 
5968 /* Create a DIE for DECL if required and add a reference to a DIE
5969    at SYMBOL + OFFSET which contains attributes dumped early.  */
5970 
5971 static void
dwarf2out_register_external_die(tree decl,const char * sym,unsigned HOST_WIDE_INT off)5972 dwarf2out_register_external_die (tree decl, const char *sym,
5973 				 unsigned HOST_WIDE_INT off)
5974 {
5975   if (debug_info_level == DINFO_LEVEL_NONE)
5976     return;
5977 
5978   if (!external_die_map)
5979     external_die_map = hash_map<tree, sym_off_pair>::create_ggc (1000);
5980   gcc_checking_assert (!external_die_map->get (decl));
5981   sym_off_pair p = { IDENTIFIER_POINTER (get_identifier (sym)), off };
5982   external_die_map->put (decl, p);
5983 }
5984 
5985 /* If we have a registered external DIE for DECL return a new DIE for
5986    the concrete instance with an appropriate abstract origin.  */
5987 
5988 static dw_die_ref
maybe_create_die_with_external_ref(tree decl)5989 maybe_create_die_with_external_ref (tree decl)
5990 {
5991   if (!external_die_map)
5992     return NULL;
5993   sym_off_pair *desc = external_die_map->get (decl);
5994   if (!desc)
5995     return NULL;
5996 
5997   const char *sym = desc->sym;
5998   unsigned HOST_WIDE_INT off = desc->off;
5999   external_die_map->remove (decl);
6000 
6001   in_lto_p = false;
6002   dw_die_ref die = (TREE_CODE (decl) == BLOCK
6003 		    ? lookup_block_die (decl) : lookup_decl_die (decl));
6004   gcc_assert (!die);
6005   in_lto_p = true;
6006 
6007   tree ctx;
6008   dw_die_ref parent = NULL;
6009   /* Need to lookup a DIE for the decls context - the containing
6010      function or translation unit.  */
6011   if (TREE_CODE (decl) == BLOCK)
6012     {
6013       ctx = BLOCK_SUPERCONTEXT (decl);
6014       /* ???  We do not output DIEs for all scopes thus skip as
6015 	 many DIEs as needed.  */
6016       while (TREE_CODE (ctx) == BLOCK
6017 	     && !lookup_block_die (ctx))
6018 	ctx = BLOCK_SUPERCONTEXT (ctx);
6019     }
6020   else
6021     ctx = DECL_CONTEXT (decl);
6022   /* Peel types in the context stack.  */
6023   while (ctx && TYPE_P (ctx))
6024     ctx = TYPE_CONTEXT (ctx);
6025   /* Likewise namespaces in case we do not want to emit DIEs for them.  */
6026   if (debug_info_level <= DINFO_LEVEL_TERSE)
6027     while (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
6028       ctx = DECL_CONTEXT (ctx);
6029   if (ctx)
6030     {
6031       if (TREE_CODE (ctx) == BLOCK)
6032 	parent = lookup_block_die (ctx);
6033       else if (TREE_CODE (ctx) == TRANSLATION_UNIT_DECL
6034 	       /* Keep the 1:1 association during WPA.  */
6035 	       && !flag_wpa
6036 	       && flag_incremental_link != INCREMENTAL_LINK_LTO)
6037 	/* Otherwise all late annotations go to the main CU which
6038 	   imports the original CUs.  */
6039 	parent = comp_unit_die ();
6040       else if (TREE_CODE (ctx) == FUNCTION_DECL
6041 	       && TREE_CODE (decl) != FUNCTION_DECL
6042 	       && TREE_CODE (decl) != PARM_DECL
6043 	       && TREE_CODE (decl) != RESULT_DECL
6044 	       && TREE_CODE (decl) != BLOCK)
6045 	/* Leave function local entities parent determination to when
6046 	   we process scope vars.  */
6047 	;
6048       else
6049 	parent = lookup_decl_die (ctx);
6050     }
6051   else
6052     /* In some cases the FEs fail to set DECL_CONTEXT properly.
6053        Handle this case gracefully by globalizing stuff.  */
6054     parent = comp_unit_die ();
6055   /* Create a DIE "stub".  */
6056   switch (TREE_CODE (decl))
6057     {
6058     case TRANSLATION_UNIT_DECL:
6059       {
6060 	die = comp_unit_die ();
6061 	/* We re-target all CU decls to the LTRANS CU DIE, so no need
6062 	   to create a DIE for the original CUs.  */
6063 	return die;
6064       }
6065     case NAMESPACE_DECL:
6066       if (is_fortran (decl))
6067 	die = new_die (DW_TAG_module, parent, decl);
6068       else
6069 	die = new_die (DW_TAG_namespace, parent, decl);
6070       break;
6071     case FUNCTION_DECL:
6072       die = new_die (DW_TAG_subprogram, parent, decl);
6073       break;
6074     case VAR_DECL:
6075       die = new_die (DW_TAG_variable, parent, decl);
6076       break;
6077     case RESULT_DECL:
6078       die = new_die (DW_TAG_variable, parent, decl);
6079       break;
6080     case PARM_DECL:
6081       die = new_die (DW_TAG_formal_parameter, parent, decl);
6082       break;
6083     case CONST_DECL:
6084       die = new_die (DW_TAG_constant, parent, decl);
6085       break;
6086     case LABEL_DECL:
6087       die = new_die (DW_TAG_label, parent, decl);
6088       break;
6089     case BLOCK:
6090       die = new_die (DW_TAG_lexical_block, parent, decl);
6091       break;
6092     default:
6093       gcc_unreachable ();
6094     }
6095   if (TREE_CODE (decl) == BLOCK)
6096     equate_block_to_die (decl, die);
6097   else
6098     equate_decl_number_to_die (decl, die);
6099 
6100   add_desc_attribute (die, decl);
6101 
6102   /* Add a reference to the DIE providing early debug at $sym + off.  */
6103   add_AT_external_die_ref (die, DW_AT_abstract_origin, sym, off);
6104 
6105   return die;
6106 }
6107 
6108 /* Returns a hash value for X (which really is a var_loc_list).  */
6109 
6110 inline hashval_t
hash(var_loc_list * x)6111 decl_loc_hasher::hash (var_loc_list *x)
6112 {
6113   return (hashval_t) x->decl_id;
6114 }
6115 
6116 /* Return nonzero if decl_id of var_loc_list X is the same as
6117    UID of decl *Y.  */
6118 
6119 inline bool
equal(var_loc_list * x,const_tree y)6120 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
6121 {
6122   return (x->decl_id == DECL_UID (y));
6123 }
6124 
6125 /* Return the var_loc list associated with a given declaration.  */
6126 
6127 static inline var_loc_list *
lookup_decl_loc(const_tree decl)6128 lookup_decl_loc (const_tree decl)
6129 {
6130   if (!decl_loc_table)
6131     return NULL;
6132   return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
6133 }
6134 
6135 /* Returns a hash value for X (which really is a cached_dw_loc_list_list).  */
6136 
6137 inline hashval_t
hash(cached_dw_loc_list * x)6138 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
6139 {
6140   return (hashval_t) x->decl_id;
6141 }
6142 
6143 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
6144    UID of decl *Y.  */
6145 
6146 inline bool
equal(cached_dw_loc_list * x,const_tree y)6147 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
6148 {
6149   return (x->decl_id == DECL_UID (y));
6150 }
6151 
6152 /* Equate a DIE to a particular declaration.  */
6153 
6154 static void
equate_decl_number_to_die(tree decl,dw_die_ref decl_die)6155 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
6156 {
6157   unsigned int decl_id = DECL_UID (decl);
6158 
6159   *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
6160   decl_die->decl_id = decl_id;
6161 }
6162 
6163 /* Return how many bits covers PIECE EXPR_LIST.  */
6164 
6165 static HOST_WIDE_INT
decl_piece_bitsize(rtx piece)6166 decl_piece_bitsize (rtx piece)
6167 {
6168   int ret = (int) GET_MODE (piece);
6169   if (ret)
6170     return ret;
6171   gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
6172 	      && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
6173   return INTVAL (XEXP (XEXP (piece, 0), 0));
6174 }
6175 
6176 /* Return pointer to the location of location note in PIECE EXPR_LIST.  */
6177 
6178 static rtx *
decl_piece_varloc_ptr(rtx piece)6179 decl_piece_varloc_ptr (rtx piece)
6180 {
6181   if ((int) GET_MODE (piece))
6182     return &XEXP (piece, 0);
6183   else
6184     return &XEXP (XEXP (piece, 0), 1);
6185 }
6186 
6187 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
6188    Next is the chain of following piece nodes.  */
6189 
6190 static rtx_expr_list *
decl_piece_node(rtx loc_note,HOST_WIDE_INT bitsize,rtx next)6191 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
6192 {
6193   if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
6194     return alloc_EXPR_LIST (bitsize, loc_note, next);
6195   else
6196     return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
6197 					       GEN_INT (bitsize),
6198 					       loc_note), next);
6199 }
6200 
6201 /* Return rtx that should be stored into loc field for
6202    LOC_NOTE and BITPOS/BITSIZE.  */
6203 
6204 static rtx
construct_piece_list(rtx loc_note,HOST_WIDE_INT bitpos,HOST_WIDE_INT bitsize)6205 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
6206 		      HOST_WIDE_INT bitsize)
6207 {
6208   if (bitsize != -1)
6209     {
6210       loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
6211       if (bitpos != 0)
6212 	loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
6213     }
6214   return loc_note;
6215 }
6216 
6217 /* This function either modifies location piece list *DEST in
6218    place (if SRC and INNER is NULL), or copies location piece list
6219    *SRC to *DEST while modifying it.  Location BITPOS is modified
6220    to contain LOC_NOTE, any pieces overlapping it are removed resp.
6221    not copied and if needed some padding around it is added.
6222    When modifying in place, DEST should point to EXPR_LIST where
6223    earlier pieces cover PIECE_BITPOS bits, when copying SRC points
6224    to the start of the whole list and INNER points to the EXPR_LIST
6225    where earlier pieces cover PIECE_BITPOS bits.  */
6226 
6227 static void
adjust_piece_list(rtx * dest,rtx * src,rtx * inner,HOST_WIDE_INT bitpos,HOST_WIDE_INT piece_bitpos,HOST_WIDE_INT bitsize,rtx loc_note)6228 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
6229 		   HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
6230 		   HOST_WIDE_INT bitsize, rtx loc_note)
6231 {
6232   HOST_WIDE_INT diff;
6233   bool copy = inner != NULL;
6234 
6235   if (copy)
6236     {
6237       /* First copy all nodes preceding the current bitpos.  */
6238       while (src != inner)
6239 	{
6240 	  *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
6241 				   decl_piece_bitsize (*src), NULL_RTX);
6242 	  dest = &XEXP (*dest, 1);
6243 	  src = &XEXP (*src, 1);
6244 	}
6245     }
6246   /* Add padding if needed.  */
6247   if (bitpos != piece_bitpos)
6248     {
6249       *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
6250 			       copy ? NULL_RTX : *dest);
6251       dest = &XEXP (*dest, 1);
6252     }
6253   else if (*dest && decl_piece_bitsize (*dest) == bitsize)
6254     {
6255       gcc_assert (!copy);
6256       /* A piece with correct bitpos and bitsize already exist,
6257 	 just update the location for it and return.  */
6258       *decl_piece_varloc_ptr (*dest) = loc_note;
6259       return;
6260     }
6261   /* Add the piece that changed.  */
6262   *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
6263   dest = &XEXP (*dest, 1);
6264   /* Skip over pieces that overlap it.  */
6265   diff = bitpos - piece_bitpos + bitsize;
6266   if (!copy)
6267     src = dest;
6268   while (diff > 0 && *src)
6269     {
6270       rtx piece = *src;
6271       diff -= decl_piece_bitsize (piece);
6272       if (copy)
6273 	src = &XEXP (piece, 1);
6274       else
6275 	{
6276 	  *src = XEXP (piece, 1);
6277 	  free_EXPR_LIST_node (piece);
6278 	}
6279     }
6280   /* Add padding if needed.  */
6281   if (diff < 0 && *src)
6282     {
6283       if (!copy)
6284 	dest = src;
6285       *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
6286       dest = &XEXP (*dest, 1);
6287     }
6288   if (!copy)
6289     return;
6290   /* Finally copy all nodes following it.  */
6291   while (*src)
6292     {
6293       *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
6294 			       decl_piece_bitsize (*src), NULL_RTX);
6295       dest = &XEXP (*dest, 1);
6296       src = &XEXP (*src, 1);
6297     }
6298 }
6299 
6300 /* Add a variable location node to the linked list for DECL.  */
6301 
6302 static struct var_loc_node *
add_var_loc_to_decl(tree decl,rtx loc_note,const char * label,var_loc_view view)6303 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label, var_loc_view view)
6304 {
6305   unsigned int decl_id;
6306   var_loc_list *temp;
6307   struct var_loc_node *loc = NULL;
6308   HOST_WIDE_INT bitsize = -1, bitpos = -1;
6309 
6310   if (VAR_P (decl) && DECL_HAS_DEBUG_EXPR_P (decl))
6311     {
6312       tree realdecl = DECL_DEBUG_EXPR (decl);
6313       if (handled_component_p (realdecl)
6314 	  || (TREE_CODE (realdecl) == MEM_REF
6315 	      && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
6316 	{
6317 	  bool reverse;
6318 	  tree innerdecl = get_ref_base_and_extent_hwi (realdecl, &bitpos,
6319 							&bitsize, &reverse);
6320 	  if (!innerdecl
6321 	      || !DECL_P (innerdecl)
6322 	      || DECL_IGNORED_P (innerdecl)
6323 	      || TREE_STATIC (innerdecl)
6324 	      || bitsize == 0
6325 	      || bitpos + bitsize > 256)
6326 	    return NULL;
6327 	  decl = innerdecl;
6328 	}
6329     }
6330 
6331   decl_id = DECL_UID (decl);
6332   var_loc_list **slot
6333     = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
6334   if (*slot == NULL)
6335     {
6336       temp = ggc_cleared_alloc<var_loc_list> ();
6337       temp->decl_id = decl_id;
6338       *slot = temp;
6339     }
6340   else
6341     temp = *slot;
6342 
6343   /* For PARM_DECLs try to keep around the original incoming value,
6344      even if that means we'll emit a zero-range .debug_loc entry.  */
6345   if (temp->last
6346       && temp->first == temp->last
6347       && TREE_CODE (decl) == PARM_DECL
6348       && NOTE_P (temp->first->loc)
6349       && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
6350       && DECL_INCOMING_RTL (decl)
6351       && NOTE_VAR_LOCATION_LOC (temp->first->loc)
6352       && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
6353 	 == GET_CODE (DECL_INCOMING_RTL (decl))
6354       && prev_real_insn (as_a<rtx_insn *> (temp->first->loc)) == NULL_RTX
6355       && (bitsize != -1
6356 	  || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
6357 			   NOTE_VAR_LOCATION_LOC (loc_note))
6358 	  || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
6359 	      != NOTE_VAR_LOCATION_STATUS (loc_note))))
6360     {
6361       loc = ggc_cleared_alloc<var_loc_node> ();
6362       temp->first->next = loc;
6363       temp->last = loc;
6364       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6365     }
6366   else if (temp->last)
6367     {
6368       struct var_loc_node *last = temp->last, *unused = NULL;
6369       rtx *piece_loc = NULL, last_loc_note;
6370       HOST_WIDE_INT piece_bitpos = 0;
6371       if (last->next)
6372 	{
6373 	  last = last->next;
6374 	  gcc_assert (last->next == NULL);
6375 	}
6376       if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
6377 	{
6378 	  piece_loc = &last->loc;
6379 	  do
6380 	    {
6381 	      HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
6382 	      if (piece_bitpos + cur_bitsize > bitpos)
6383 		break;
6384 	      piece_bitpos += cur_bitsize;
6385 	      piece_loc = &XEXP (*piece_loc, 1);
6386 	    }
6387 	  while (*piece_loc);
6388 	}
6389       /* TEMP->LAST here is either pointer to the last but one or
6390 	 last element in the chained list, LAST is pointer to the
6391 	 last element.  */
6392       if (label && strcmp (last->label, label) == 0 && last->view == view)
6393 	{
6394 	  /* For SRA optimized variables if there weren't any real
6395 	     insns since last note, just modify the last node.  */
6396 	  if (piece_loc != NULL)
6397 	    {
6398 	      adjust_piece_list (piece_loc, NULL, NULL,
6399 				 bitpos, piece_bitpos, bitsize, loc_note);
6400 	      return NULL;
6401 	    }
6402 	  /* If the last note doesn't cover any instructions, remove it.  */
6403 	  if (temp->last != last)
6404 	    {
6405 	      temp->last->next = NULL;
6406 	      unused = last;
6407 	      last = temp->last;
6408 	      gcc_assert (strcmp (last->label, label) != 0 || last->view != view);
6409 	    }
6410 	  else
6411 	    {
6412 	      gcc_assert (temp->first == temp->last
6413 			  || (temp->first->next == temp->last
6414 			      && TREE_CODE (decl) == PARM_DECL));
6415 	      memset (temp->last, '\0', sizeof (*temp->last));
6416 	      temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
6417 	      return temp->last;
6418 	    }
6419 	}
6420       if (bitsize == -1 && NOTE_P (last->loc))
6421 	last_loc_note = last->loc;
6422       else if (piece_loc != NULL
6423 	       && *piece_loc != NULL_RTX
6424 	       && piece_bitpos == bitpos
6425 	       && decl_piece_bitsize (*piece_loc) == bitsize)
6426 	last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
6427       else
6428 	last_loc_note = NULL_RTX;
6429       /* If the current location is the same as the end of the list,
6430 	 and either both or neither of the locations is uninitialized,
6431 	 we have nothing to do.  */
6432       if (last_loc_note == NULL_RTX
6433 	  || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
6434 			    NOTE_VAR_LOCATION_LOC (loc_note)))
6435 	  || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6436 	       != NOTE_VAR_LOCATION_STATUS (loc_note))
6437 	      && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6438 		   == VAR_INIT_STATUS_UNINITIALIZED)
6439 		  || (NOTE_VAR_LOCATION_STATUS (loc_note)
6440 		      == VAR_INIT_STATUS_UNINITIALIZED))))
6441 	{
6442 	  /* Add LOC to the end of list and update LAST.  If the last
6443 	     element of the list has been removed above, reuse its
6444 	     memory for the new node, otherwise allocate a new one.  */
6445 	  if (unused)
6446 	    {
6447 	      loc = unused;
6448 	      memset (loc, '\0', sizeof (*loc));
6449 	    }
6450 	  else
6451 	    loc = ggc_cleared_alloc<var_loc_node> ();
6452 	  if (bitsize == -1 || piece_loc == NULL)
6453 	    loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6454 	  else
6455 	    adjust_piece_list (&loc->loc, &last->loc, piece_loc,
6456 			       bitpos, piece_bitpos, bitsize, loc_note);
6457 	  last->next = loc;
6458 	  /* Ensure TEMP->LAST will point either to the new last but one
6459 	     element of the chain, or to the last element in it.  */
6460 	  if (last != temp->last)
6461 	    temp->last = last;
6462 	}
6463       else if (unused)
6464 	ggc_free (unused);
6465     }
6466   else
6467     {
6468       loc = ggc_cleared_alloc<var_loc_node> ();
6469       temp->first = loc;
6470       temp->last = loc;
6471       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6472     }
6473   return loc;
6474 }
6475 
6476 /* Keep track of the number of spaces used to indent the
6477    output of the debugging routines that print the structure of
6478    the DIE internal representation.  */
6479 static int print_indent;
6480 
6481 /* Indent the line the number of spaces given by print_indent.  */
6482 
6483 static inline void
print_spaces(FILE * outfile)6484 print_spaces (FILE *outfile)
6485 {
6486   fprintf (outfile, "%*s", print_indent, "");
6487 }
6488 
6489 /* Print a type signature in hex.  */
6490 
6491 static inline void
print_signature(FILE * outfile,char * sig)6492 print_signature (FILE *outfile, char *sig)
6493 {
6494   int i;
6495 
6496   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
6497     fprintf (outfile, "%02x", sig[i] & 0xff);
6498 }
6499 
6500 static inline void
print_discr_value(FILE * outfile,dw_discr_value * discr_value)6501 print_discr_value (FILE *outfile, dw_discr_value *discr_value)
6502 {
6503   if (discr_value->pos)
6504     fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, discr_value->v.sval);
6505   else
6506     fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, discr_value->v.uval);
6507 }
6508 
6509 static void print_loc_descr (dw_loc_descr_ref, FILE *);
6510 
6511 /* Print the value associated to the VAL DWARF value node to OUTFILE.  If
6512    RECURSE, output location descriptor operations.  */
6513 
6514 static void
print_dw_val(dw_val_node * val,bool recurse,FILE * outfile)6515 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
6516 {
6517   switch (val->val_class)
6518     {
6519     case dw_val_class_addr:
6520       fprintf (outfile, "address");
6521       break;
6522     case dw_val_class_offset:
6523       fprintf (outfile, "offset");
6524       break;
6525     case dw_val_class_loc:
6526       fprintf (outfile, "location descriptor");
6527       if (val->v.val_loc == NULL)
6528 	fprintf (outfile, " -> <null>\n");
6529       else if (recurse)
6530 	{
6531 	  fprintf (outfile, ":\n");
6532 	  print_indent += 4;
6533 	  print_loc_descr (val->v.val_loc, outfile);
6534 	  print_indent -= 4;
6535 	}
6536       else
6537 	{
6538 	  if (flag_dump_noaddr || flag_dump_unnumbered)
6539 	    fprintf (outfile, " #\n");
6540 	  else
6541 	    fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
6542 	}
6543       break;
6544     case dw_val_class_loc_list:
6545       fprintf (outfile, "location list -> label:%s",
6546 	       val->v.val_loc_list->ll_symbol);
6547       break;
6548     case dw_val_class_view_list:
6549       val = view_list_to_loc_list_val_node (val);
6550       fprintf (outfile, "location list with views -> labels:%s and %s",
6551 	       val->v.val_loc_list->ll_symbol,
6552 	       val->v.val_loc_list->vl_symbol);
6553       break;
6554     case dw_val_class_range_list:
6555       fprintf (outfile, "range list");
6556       break;
6557     case dw_val_class_const:
6558     case dw_val_class_const_implicit:
6559       fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
6560       break;
6561     case dw_val_class_unsigned_const:
6562     case dw_val_class_unsigned_const_implicit:
6563       fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
6564       break;
6565     case dw_val_class_const_double:
6566       fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
6567 			HOST_WIDE_INT_PRINT_UNSIGNED")",
6568 	       val->v.val_double.high,
6569 	       val->v.val_double.low);
6570       break;
6571     case dw_val_class_wide_int:
6572       {
6573 	int i = val->v.val_wide->get_len ();
6574 	fprintf (outfile, "constant (");
6575 	gcc_assert (i > 0);
6576 	if (val->v.val_wide->elt (i - 1) == 0)
6577 	  fprintf (outfile, "0x");
6578 	fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
6579 		 val->v.val_wide->elt (--i));
6580 	while (--i >= 0)
6581 	  fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
6582 		   val->v.val_wide->elt (i));
6583 	fprintf (outfile, ")");
6584 	break;
6585       }
6586     case dw_val_class_vec:
6587       fprintf (outfile, "floating-point or vector constant");
6588       break;
6589     case dw_val_class_flag:
6590       fprintf (outfile, "%u", val->v.val_flag);
6591       break;
6592     case dw_val_class_die_ref:
6593       if (val->v.val_die_ref.die != NULL)
6594 	{
6595 	  dw_die_ref die = val->v.val_die_ref.die;
6596 
6597 	  if (die->comdat_type_p)
6598 	    {
6599 	      fprintf (outfile, "die -> signature: ");
6600 	      print_signature (outfile,
6601 			       die->die_id.die_type_node->signature);
6602 	    }
6603 	  else if (die->die_id.die_symbol)
6604 	    {
6605 	      fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
6606 	      if (die->with_offset)
6607 		fprintf (outfile, " + %ld", die->die_offset);
6608 	    }
6609 	  else
6610 	    fprintf (outfile, "die -> %ld", die->die_offset);
6611 	  if (flag_dump_noaddr || flag_dump_unnumbered)
6612 	    fprintf (outfile, " #");
6613 	  else
6614 	    fprintf (outfile, " (%p)", (void *) die);
6615 	}
6616       else
6617 	fprintf (outfile, "die -> <null>");
6618       break;
6619     case dw_val_class_vms_delta:
6620       fprintf (outfile, "delta: @slotcount(%s-%s)",
6621 	       val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
6622       break;
6623     case dw_val_class_symview:
6624       fprintf (outfile, "view: %s", val->v.val_symbolic_view);
6625       break;
6626     case dw_val_class_lbl_id:
6627     case dw_val_class_lineptr:
6628     case dw_val_class_macptr:
6629     case dw_val_class_loclistsptr:
6630     case dw_val_class_high_pc:
6631       fprintf (outfile, "label: %s", val->v.val_lbl_id);
6632       break;
6633     case dw_val_class_str:
6634       if (val->v.val_str->str != NULL)
6635 	fprintf (outfile, "\"%s\"", val->v.val_str->str);
6636       else
6637 	fprintf (outfile, "<null>");
6638       break;
6639     case dw_val_class_file:
6640     case dw_val_class_file_implicit:
6641       fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
6642 	       val->v.val_file->emitted_number);
6643       break;
6644     case dw_val_class_data8:
6645       {
6646 	int i;
6647 
6648 	for (i = 0; i < 8; i++)
6649 	  fprintf (outfile, "%02x", val->v.val_data8[i]);
6650 	break;
6651       }
6652     case dw_val_class_discr_value:
6653       print_discr_value (outfile, &val->v.val_discr_value);
6654       break;
6655     case dw_val_class_discr_list:
6656       for (dw_discr_list_ref node = val->v.val_discr_list;
6657 	   node != NULL;
6658 	   node = node->dw_discr_next)
6659 	{
6660 	  if (node->dw_discr_range)
6661 	    {
6662 	      fprintf (outfile, " .. ");
6663 	      print_discr_value (outfile, &node->dw_discr_lower_bound);
6664 	      print_discr_value (outfile, &node->dw_discr_upper_bound);
6665 	    }
6666 	  else
6667 	    print_discr_value (outfile, &node->dw_discr_lower_bound);
6668 
6669 	  if (node->dw_discr_next != NULL)
6670 	    fprintf (outfile, " | ");
6671 	}
6672     default:
6673       break;
6674     }
6675 }
6676 
6677 /* Likewise, for a DIE attribute.  */
6678 
6679 static void
print_attribute(dw_attr_node * a,bool recurse,FILE * outfile)6680 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
6681 {
6682   print_dw_val (&a->dw_attr_val, recurse, outfile);
6683 }
6684 
6685 
6686 /* Print the list of operands in the LOC location description to OUTFILE.  This
6687    routine is a debugging aid only.  */
6688 
6689 static void
print_loc_descr(dw_loc_descr_ref loc,FILE * outfile)6690 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
6691 {
6692   dw_loc_descr_ref l = loc;
6693 
6694   if (loc == NULL)
6695     {
6696       print_spaces (outfile);
6697       fprintf (outfile, "<null>\n");
6698       return;
6699     }
6700 
6701   for (l = loc; l != NULL; l = l->dw_loc_next)
6702     {
6703       print_spaces (outfile);
6704       if (flag_dump_noaddr || flag_dump_unnumbered)
6705 	fprintf (outfile, "#");
6706       else
6707 	fprintf (outfile, "(%p)", (void *) l);
6708       fprintf (outfile, " %s",
6709 	       dwarf_stack_op_name (l->dw_loc_opc));
6710       if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
6711 	{
6712 	  fprintf (outfile, " ");
6713 	  print_dw_val (&l->dw_loc_oprnd1, false, outfile);
6714 	}
6715       if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
6716 	{
6717 	  fprintf (outfile, ", ");
6718 	  print_dw_val (&l->dw_loc_oprnd2, false, outfile);
6719 	}
6720       fprintf (outfile, "\n");
6721     }
6722 }
6723 
6724 /* Print the information associated with a given DIE, and its children.
6725    This routine is a debugging aid only.  */
6726 
6727 static void
print_die(dw_die_ref die,FILE * outfile)6728 print_die (dw_die_ref die, FILE *outfile)
6729 {
6730   dw_attr_node *a;
6731   dw_die_ref c;
6732   unsigned ix;
6733 
6734   print_spaces (outfile);
6735   fprintf (outfile, "DIE %4ld: %s ",
6736 	   die->die_offset, dwarf_tag_name (die->die_tag));
6737   if (flag_dump_noaddr || flag_dump_unnumbered)
6738     fprintf (outfile, "#\n");
6739   else
6740     fprintf (outfile, "(%p)\n", (void*) die);
6741   print_spaces (outfile);
6742   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
6743   fprintf (outfile, " offset: %ld", die->die_offset);
6744   fprintf (outfile, " mark: %d\n", die->die_mark);
6745 
6746   if (die->comdat_type_p)
6747     {
6748       print_spaces (outfile);
6749       fprintf (outfile, "  signature: ");
6750       print_signature (outfile, die->die_id.die_type_node->signature);
6751       fprintf (outfile, "\n");
6752     }
6753 
6754   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6755     {
6756       print_spaces (outfile);
6757       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
6758 
6759       print_attribute (a, true, outfile);
6760       fprintf (outfile, "\n");
6761     }
6762 
6763   if (die->die_child != NULL)
6764     {
6765       print_indent += 4;
6766       FOR_EACH_CHILD (die, c, print_die (c, outfile));
6767       print_indent -= 4;
6768     }
6769   if (print_indent == 0)
6770     fprintf (outfile, "\n");
6771 }
6772 
6773 /* Print the list of operations in the LOC location description.  */
6774 
6775 DEBUG_FUNCTION void
debug_dwarf_loc_descr(dw_loc_descr_ref loc)6776 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
6777 {
6778   print_loc_descr (loc, stderr);
6779 }
6780 
6781 /* Print the information collected for a given DIE.  */
6782 
6783 DEBUG_FUNCTION void
debug_dwarf_die(dw_die_ref die)6784 debug_dwarf_die (dw_die_ref die)
6785 {
6786   print_die (die, stderr);
6787 }
6788 
6789 DEBUG_FUNCTION void
debug(die_struct & ref)6790 debug (die_struct &ref)
6791 {
6792   print_die (&ref, stderr);
6793 }
6794 
6795 DEBUG_FUNCTION void
debug(die_struct * ptr)6796 debug (die_struct *ptr)
6797 {
6798   if (ptr)
6799     debug (*ptr);
6800   else
6801     fprintf (stderr, "<nil>\n");
6802 }
6803 
6804 
6805 /* Print all DWARF information collected for the compilation unit.
6806    This routine is a debugging aid only.  */
6807 
6808 DEBUG_FUNCTION void
debug_dwarf(void)6809 debug_dwarf (void)
6810 {
6811   print_indent = 0;
6812   print_die (comp_unit_die (), stderr);
6813 }
6814 
6815 /* Verify the DIE tree structure.  */
6816 
6817 DEBUG_FUNCTION void
verify_die(dw_die_ref die)6818 verify_die (dw_die_ref die)
6819 {
6820   gcc_assert (!die->die_mark);
6821   if (die->die_parent == NULL
6822       && die->die_sib == NULL)
6823     return;
6824   /* Verify the die_sib list is cyclic.  */
6825   dw_die_ref x = die;
6826   do
6827     {
6828       x->die_mark = 1;
6829       x = x->die_sib;
6830     }
6831   while (x && !x->die_mark);
6832   gcc_assert (x == die);
6833   x = die;
6834   do
6835     {
6836       /* Verify all dies have the same parent.  */
6837       gcc_assert (x->die_parent == die->die_parent);
6838       if (x->die_child)
6839 	{
6840 	  /* Verify the child has the proper parent and recurse.  */
6841 	  gcc_assert (x->die_child->die_parent == x);
6842 	  verify_die (x->die_child);
6843 	}
6844       x->die_mark = 0;
6845       x = x->die_sib;
6846     }
6847   while (x && x->die_mark);
6848 }
6849 
6850 /* Sanity checks on DIEs.  */
6851 
6852 static void
check_die(dw_die_ref die)6853 check_die (dw_die_ref die)
6854 {
6855   unsigned ix;
6856   dw_attr_node *a;
6857   bool inline_found = false;
6858   int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
6859   int n_decl_line = 0, n_decl_column = 0, n_decl_file = 0;
6860   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6861     {
6862       switch (a->dw_attr)
6863 	{
6864 	case DW_AT_inline:
6865 	  if (a->dw_attr_val.v.val_unsigned)
6866 	    inline_found = true;
6867 	  break;
6868 	case DW_AT_location:
6869 	  ++n_location;
6870 	  break;
6871 	case DW_AT_low_pc:
6872 	  ++n_low_pc;
6873 	  break;
6874 	case DW_AT_high_pc:
6875 	  ++n_high_pc;
6876 	  break;
6877 	case DW_AT_artificial:
6878 	  ++n_artificial;
6879 	  break;
6880         case DW_AT_decl_column:
6881 	  ++n_decl_column;
6882 	  break;
6883 	case DW_AT_decl_line:
6884 	  ++n_decl_line;
6885 	  break;
6886 	case DW_AT_decl_file:
6887 	  ++n_decl_file;
6888 	  break;
6889 	default:
6890 	  break;
6891 	}
6892     }
6893   if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
6894       || n_decl_column > 1 || n_decl_line > 1 || n_decl_file > 1)
6895     {
6896       fprintf (stderr, "Duplicate attributes in DIE:\n");
6897       debug_dwarf_die (die);
6898       gcc_unreachable ();
6899     }
6900   if (inline_found)
6901     {
6902       /* A debugging information entry that is a member of an abstract
6903 	 instance tree [that has DW_AT_inline] should not contain any
6904 	 attributes which describe aspects of the subroutine which vary
6905 	 between distinct inlined expansions or distinct out-of-line
6906 	 expansions.  */
6907       FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6908 	gcc_assert (a->dw_attr != DW_AT_low_pc
6909 		    && a->dw_attr != DW_AT_high_pc
6910 		    && a->dw_attr != DW_AT_location
6911 		    && a->dw_attr != DW_AT_frame_base
6912 		    && a->dw_attr != DW_AT_call_all_calls
6913 		    && a->dw_attr != DW_AT_GNU_all_call_sites);
6914     }
6915 }
6916 
6917 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6918 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6919 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6920 
6921 /* Calculate the checksum of a location expression.  */
6922 
6923 static inline void
loc_checksum(dw_loc_descr_ref loc,struct md5_ctx * ctx)6924 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6925 {
6926   int tem;
6927   inchash::hash hstate;
6928   hashval_t hash;
6929 
6930   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
6931   CHECKSUM (tem);
6932   hash_loc_operands (loc, hstate);
6933   hash = hstate.end();
6934   CHECKSUM (hash);
6935 }
6936 
6937 /* Calculate the checksum of an attribute.  */
6938 
6939 static void
attr_checksum(dw_attr_node * at,struct md5_ctx * ctx,int * mark)6940 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
6941 {
6942   dw_loc_descr_ref loc;
6943   rtx r;
6944 
6945   CHECKSUM (at->dw_attr);
6946 
6947   /* We don't care that this was compiled with a different compiler
6948      snapshot; if the output is the same, that's what matters.  */
6949   if (at->dw_attr == DW_AT_producer)
6950     return;
6951 
6952   switch (AT_class (at))
6953     {
6954     case dw_val_class_const:
6955     case dw_val_class_const_implicit:
6956       CHECKSUM (at->dw_attr_val.v.val_int);
6957       break;
6958     case dw_val_class_unsigned_const:
6959     case dw_val_class_unsigned_const_implicit:
6960       CHECKSUM (at->dw_attr_val.v.val_unsigned);
6961       break;
6962     case dw_val_class_const_double:
6963       CHECKSUM (at->dw_attr_val.v.val_double);
6964       break;
6965     case dw_val_class_wide_int:
6966       CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6967 		      get_full_len (*at->dw_attr_val.v.val_wide)
6968 		      * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6969       break;
6970     case dw_val_class_vec:
6971       CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6972 		      (at->dw_attr_val.v.val_vec.length
6973 		       * at->dw_attr_val.v.val_vec.elt_size));
6974       break;
6975     case dw_val_class_flag:
6976       CHECKSUM (at->dw_attr_val.v.val_flag);
6977       break;
6978     case dw_val_class_str:
6979       CHECKSUM_STRING (AT_string (at));
6980       break;
6981 
6982     case dw_val_class_addr:
6983       r = AT_addr (at);
6984       gcc_assert (GET_CODE (r) == SYMBOL_REF);
6985       CHECKSUM_STRING (XSTR (r, 0));
6986       break;
6987 
6988     case dw_val_class_offset:
6989       CHECKSUM (at->dw_attr_val.v.val_offset);
6990       break;
6991 
6992     case dw_val_class_loc:
6993       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6994 	loc_checksum (loc, ctx);
6995       break;
6996 
6997     case dw_val_class_die_ref:
6998       die_checksum (AT_ref (at), ctx, mark);
6999       break;
7000 
7001     case dw_val_class_fde_ref:
7002     case dw_val_class_vms_delta:
7003     case dw_val_class_symview:
7004     case dw_val_class_lbl_id:
7005     case dw_val_class_lineptr:
7006     case dw_val_class_macptr:
7007     case dw_val_class_loclistsptr:
7008     case dw_val_class_high_pc:
7009       break;
7010 
7011     case dw_val_class_file:
7012     case dw_val_class_file_implicit:
7013       CHECKSUM_STRING (AT_file (at)->filename);
7014       break;
7015 
7016     case dw_val_class_data8:
7017       CHECKSUM (at->dw_attr_val.v.val_data8);
7018       break;
7019 
7020     default:
7021       break;
7022     }
7023 }
7024 
7025 /* Calculate the checksum of a DIE.  */
7026 
7027 static void
die_checksum(dw_die_ref die,struct md5_ctx * ctx,int * mark)7028 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7029 {
7030   dw_die_ref c;
7031   dw_attr_node *a;
7032   unsigned ix;
7033 
7034   /* To avoid infinite recursion.  */
7035   if (die->die_mark)
7036     {
7037       CHECKSUM (die->die_mark);
7038       return;
7039     }
7040   die->die_mark = ++(*mark);
7041 
7042   CHECKSUM (die->die_tag);
7043 
7044   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7045     attr_checksum (a, ctx, mark);
7046 
7047   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
7048 }
7049 
7050 #undef CHECKSUM
7051 #undef CHECKSUM_BLOCK
7052 #undef CHECKSUM_STRING
7053 
7054 /* For DWARF-4 types, include the trailing NULL when checksumming strings.  */
7055 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
7056 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
7057 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
7058 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
7059 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
7060 #define CHECKSUM_ATTR(FOO) \
7061   if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
7062 
7063 /* Calculate the checksum of a number in signed LEB128 format.  */
7064 
7065 static void
checksum_sleb128(HOST_WIDE_INT value,struct md5_ctx * ctx)7066 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
7067 {
7068   unsigned char byte;
7069   bool more;
7070 
7071   while (1)
7072     {
7073       byte = (value & 0x7f);
7074       value >>= 7;
7075       more = !((value == 0 && (byte & 0x40) == 0)
7076 		|| (value == -1 && (byte & 0x40) != 0));
7077       if (more)
7078 	byte |= 0x80;
7079       CHECKSUM (byte);
7080       if (!more)
7081 	break;
7082     }
7083 }
7084 
7085 /* Calculate the checksum of a number in unsigned LEB128 format.  */
7086 
7087 static void
checksum_uleb128(unsigned HOST_WIDE_INT value,struct md5_ctx * ctx)7088 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
7089 {
7090   while (1)
7091     {
7092       unsigned char byte = (value & 0x7f);
7093       value >>= 7;
7094       if (value != 0)
7095 	/* More bytes to follow.  */
7096 	byte |= 0x80;
7097       CHECKSUM (byte);
7098       if (value == 0)
7099 	break;
7100     }
7101 }
7102 
7103 /* Checksum the context of the DIE.  This adds the names of any
7104    surrounding namespaces or structures to the checksum.  */
7105 
7106 static void
checksum_die_context(dw_die_ref die,struct md5_ctx * ctx)7107 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
7108 {
7109   const char *name;
7110   dw_die_ref spec;
7111   int tag = die->die_tag;
7112 
7113   if (tag != DW_TAG_namespace
7114       && tag != DW_TAG_structure_type
7115       && tag != DW_TAG_class_type)
7116     return;
7117 
7118   name = get_AT_string (die, DW_AT_name);
7119 
7120   spec = get_AT_ref (die, DW_AT_specification);
7121   if (spec != NULL)
7122     die = spec;
7123 
7124   if (die->die_parent != NULL)
7125     checksum_die_context (die->die_parent, ctx);
7126 
7127   CHECKSUM_ULEB128 ('C');
7128   CHECKSUM_ULEB128 (tag);
7129   if (name != NULL)
7130     CHECKSUM_STRING (name);
7131 }
7132 
7133 /* Calculate the checksum of a location expression.  */
7134 
7135 static inline void
loc_checksum_ordered(dw_loc_descr_ref loc,struct md5_ctx * ctx)7136 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
7137 {
7138   /* Special case for lone DW_OP_plus_uconst: checksum as if the location
7139      were emitted as a DW_FORM_sdata instead of a location expression.  */
7140   if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
7141     {
7142       CHECKSUM_ULEB128 (DW_FORM_sdata);
7143       CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
7144       return;
7145     }
7146 
7147   /* Otherwise, just checksum the raw location expression.  */
7148   while (loc != NULL)
7149     {
7150       inchash::hash hstate;
7151       hashval_t hash;
7152 
7153       CHECKSUM_ULEB128 (loc->dtprel);
7154       CHECKSUM_ULEB128 (loc->dw_loc_opc);
7155       hash_loc_operands (loc, hstate);
7156       hash = hstate.end ();
7157       CHECKSUM (hash);
7158       loc = loc->dw_loc_next;
7159     }
7160 }
7161 
7162 /* Calculate the checksum of an attribute.  */
7163 
7164 static void
attr_checksum_ordered(enum dwarf_tag tag,dw_attr_node * at,struct md5_ctx * ctx,int * mark)7165 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
7166 		       struct md5_ctx *ctx, int *mark)
7167 {
7168   dw_loc_descr_ref loc;
7169   rtx r;
7170 
7171   if (AT_class (at) == dw_val_class_die_ref)
7172     {
7173       dw_die_ref target_die = AT_ref (at);
7174 
7175       /* For pointer and reference types, we checksum only the (qualified)
7176 	 name of the target type (if there is a name).  For friend entries,
7177 	 we checksum only the (qualified) name of the target type or function.
7178 	 This allows the checksum to remain the same whether the target type
7179 	 is complete or not.  */
7180       if ((at->dw_attr == DW_AT_type
7181 	   && (tag == DW_TAG_pointer_type
7182 	       || tag == DW_TAG_reference_type
7183 	       || tag == DW_TAG_rvalue_reference_type
7184 	       || tag == DW_TAG_ptr_to_member_type))
7185 	  || (at->dw_attr == DW_AT_friend
7186 	      && tag == DW_TAG_friend))
7187 	{
7188 	  dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
7189 
7190 	  if (name_attr != NULL)
7191 	    {
7192 	      dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
7193 
7194 	      if (decl == NULL)
7195 		decl = target_die;
7196 	      CHECKSUM_ULEB128 ('N');
7197 	      CHECKSUM_ULEB128 (at->dw_attr);
7198 	      if (decl->die_parent != NULL)
7199 		checksum_die_context (decl->die_parent, ctx);
7200 	      CHECKSUM_ULEB128 ('E');
7201 	      CHECKSUM_STRING (AT_string (name_attr));
7202 	      return;
7203 	    }
7204 	}
7205 
7206       /* For all other references to another DIE, we check to see if the
7207          target DIE has already been visited.  If it has, we emit a
7208          backward reference; if not, we descend recursively.  */
7209       if (target_die->die_mark > 0)
7210         {
7211 	  CHECKSUM_ULEB128 ('R');
7212 	  CHECKSUM_ULEB128 (at->dw_attr);
7213 	  CHECKSUM_ULEB128 (target_die->die_mark);
7214         }
7215       else
7216         {
7217 	  dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
7218 
7219 	  if (decl == NULL)
7220 	    decl = target_die;
7221 	  target_die->die_mark = ++(*mark);
7222 	  CHECKSUM_ULEB128 ('T');
7223 	  CHECKSUM_ULEB128 (at->dw_attr);
7224 	  if (decl->die_parent != NULL)
7225 	    checksum_die_context (decl->die_parent, ctx);
7226 	  die_checksum_ordered (target_die, ctx, mark);
7227         }
7228       return;
7229     }
7230 
7231   CHECKSUM_ULEB128 ('A');
7232   CHECKSUM_ULEB128 (at->dw_attr);
7233 
7234   switch (AT_class (at))
7235     {
7236     case dw_val_class_const:
7237     case dw_val_class_const_implicit:
7238       CHECKSUM_ULEB128 (DW_FORM_sdata);
7239       CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
7240       break;
7241 
7242     case dw_val_class_unsigned_const:
7243     case dw_val_class_unsigned_const_implicit:
7244       CHECKSUM_ULEB128 (DW_FORM_sdata);
7245       CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
7246       break;
7247 
7248     case dw_val_class_const_double:
7249       CHECKSUM_ULEB128 (DW_FORM_block);
7250       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
7251       CHECKSUM (at->dw_attr_val.v.val_double);
7252       break;
7253 
7254     case dw_val_class_wide_int:
7255       CHECKSUM_ULEB128 (DW_FORM_block);
7256       CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
7257 			* HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
7258       CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
7259 		      get_full_len (*at->dw_attr_val.v.val_wide)
7260 		      * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
7261       break;
7262 
7263     case dw_val_class_vec:
7264       CHECKSUM_ULEB128 (DW_FORM_block);
7265       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
7266 			* at->dw_attr_val.v.val_vec.elt_size);
7267       CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
7268 		      (at->dw_attr_val.v.val_vec.length
7269 		       * at->dw_attr_val.v.val_vec.elt_size));
7270       break;
7271 
7272     case dw_val_class_flag:
7273       CHECKSUM_ULEB128 (DW_FORM_flag);
7274       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
7275       break;
7276 
7277     case dw_val_class_str:
7278       CHECKSUM_ULEB128 (DW_FORM_string);
7279       CHECKSUM_STRING (AT_string (at));
7280       break;
7281 
7282     case dw_val_class_addr:
7283       r = AT_addr (at);
7284       gcc_assert (GET_CODE (r) == SYMBOL_REF);
7285       CHECKSUM_ULEB128 (DW_FORM_string);
7286       CHECKSUM_STRING (XSTR (r, 0));
7287       break;
7288 
7289     case dw_val_class_offset:
7290       CHECKSUM_ULEB128 (DW_FORM_sdata);
7291       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
7292       break;
7293 
7294     case dw_val_class_loc:
7295       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
7296 	loc_checksum_ordered (loc, ctx);
7297       break;
7298 
7299     case dw_val_class_fde_ref:
7300     case dw_val_class_symview:
7301     case dw_val_class_lbl_id:
7302     case dw_val_class_lineptr:
7303     case dw_val_class_macptr:
7304     case dw_val_class_loclistsptr:
7305     case dw_val_class_high_pc:
7306       break;
7307 
7308     case dw_val_class_file:
7309     case dw_val_class_file_implicit:
7310       CHECKSUM_ULEB128 (DW_FORM_string);
7311       CHECKSUM_STRING (AT_file (at)->filename);
7312       break;
7313 
7314     case dw_val_class_data8:
7315       CHECKSUM (at->dw_attr_val.v.val_data8);
7316       break;
7317 
7318     default:
7319       break;
7320     }
7321 }
7322 
7323 struct checksum_attributes
7324 {
7325   dw_attr_node *at_name;
7326   dw_attr_node *at_type;
7327   dw_attr_node *at_friend;
7328   dw_attr_node *at_accessibility;
7329   dw_attr_node *at_address_class;
7330   dw_attr_node *at_alignment;
7331   dw_attr_node *at_allocated;
7332   dw_attr_node *at_artificial;
7333   dw_attr_node *at_associated;
7334   dw_attr_node *at_binary_scale;
7335   dw_attr_node *at_bit_offset;
7336   dw_attr_node *at_bit_size;
7337   dw_attr_node *at_bit_stride;
7338   dw_attr_node *at_byte_size;
7339   dw_attr_node *at_byte_stride;
7340   dw_attr_node *at_const_value;
7341   dw_attr_node *at_containing_type;
7342   dw_attr_node *at_count;
7343   dw_attr_node *at_data_location;
7344   dw_attr_node *at_data_member_location;
7345   dw_attr_node *at_decimal_scale;
7346   dw_attr_node *at_decimal_sign;
7347   dw_attr_node *at_default_value;
7348   dw_attr_node *at_digit_count;
7349   dw_attr_node *at_discr;
7350   dw_attr_node *at_discr_list;
7351   dw_attr_node *at_discr_value;
7352   dw_attr_node *at_encoding;
7353   dw_attr_node *at_endianity;
7354   dw_attr_node *at_explicit;
7355   dw_attr_node *at_is_optional;
7356   dw_attr_node *at_location;
7357   dw_attr_node *at_lower_bound;
7358   dw_attr_node *at_mutable;
7359   dw_attr_node *at_ordering;
7360   dw_attr_node *at_picture_string;
7361   dw_attr_node *at_prototyped;
7362   dw_attr_node *at_small;
7363   dw_attr_node *at_segment;
7364   dw_attr_node *at_string_length;
7365   dw_attr_node *at_string_length_bit_size;
7366   dw_attr_node *at_string_length_byte_size;
7367   dw_attr_node *at_threads_scaled;
7368   dw_attr_node *at_upper_bound;
7369   dw_attr_node *at_use_location;
7370   dw_attr_node *at_use_UTF8;
7371   dw_attr_node *at_variable_parameter;
7372   dw_attr_node *at_virtuality;
7373   dw_attr_node *at_visibility;
7374   dw_attr_node *at_vtable_elem_location;
7375 };
7376 
7377 /* Collect the attributes that we will want to use for the checksum.  */
7378 
7379 static void
collect_checksum_attributes(struct checksum_attributes * attrs,dw_die_ref die)7380 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
7381 {
7382   dw_attr_node *a;
7383   unsigned ix;
7384 
7385   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7386     {
7387       switch (a->dw_attr)
7388         {
7389         case DW_AT_name:
7390           attrs->at_name = a;
7391           break;
7392         case DW_AT_type:
7393           attrs->at_type = a;
7394           break;
7395         case DW_AT_friend:
7396           attrs->at_friend = a;
7397           break;
7398         case DW_AT_accessibility:
7399           attrs->at_accessibility = a;
7400           break;
7401         case DW_AT_address_class:
7402           attrs->at_address_class = a;
7403           break;
7404 	case DW_AT_alignment:
7405 	  attrs->at_alignment = a;
7406 	  break;
7407         case DW_AT_allocated:
7408           attrs->at_allocated = a;
7409           break;
7410         case DW_AT_artificial:
7411           attrs->at_artificial = a;
7412           break;
7413         case DW_AT_associated:
7414           attrs->at_associated = a;
7415           break;
7416         case DW_AT_binary_scale:
7417           attrs->at_binary_scale = a;
7418           break;
7419         case DW_AT_bit_offset:
7420           attrs->at_bit_offset = a;
7421           break;
7422         case DW_AT_bit_size:
7423           attrs->at_bit_size = a;
7424           break;
7425         case DW_AT_bit_stride:
7426           attrs->at_bit_stride = a;
7427           break;
7428         case DW_AT_byte_size:
7429           attrs->at_byte_size = a;
7430           break;
7431         case DW_AT_byte_stride:
7432           attrs->at_byte_stride = a;
7433           break;
7434         case DW_AT_const_value:
7435           attrs->at_const_value = a;
7436           break;
7437         case DW_AT_containing_type:
7438           attrs->at_containing_type = a;
7439           break;
7440         case DW_AT_count:
7441           attrs->at_count = a;
7442           break;
7443         case DW_AT_data_location:
7444           attrs->at_data_location = a;
7445           break;
7446         case DW_AT_data_member_location:
7447           attrs->at_data_member_location = a;
7448           break;
7449         case DW_AT_decimal_scale:
7450           attrs->at_decimal_scale = a;
7451           break;
7452         case DW_AT_decimal_sign:
7453           attrs->at_decimal_sign = a;
7454           break;
7455         case DW_AT_default_value:
7456           attrs->at_default_value = a;
7457           break;
7458         case DW_AT_digit_count:
7459           attrs->at_digit_count = a;
7460           break;
7461         case DW_AT_discr:
7462           attrs->at_discr = a;
7463           break;
7464         case DW_AT_discr_list:
7465           attrs->at_discr_list = a;
7466           break;
7467         case DW_AT_discr_value:
7468           attrs->at_discr_value = a;
7469           break;
7470         case DW_AT_encoding:
7471           attrs->at_encoding = a;
7472           break;
7473         case DW_AT_endianity:
7474           attrs->at_endianity = a;
7475           break;
7476         case DW_AT_explicit:
7477           attrs->at_explicit = a;
7478           break;
7479         case DW_AT_is_optional:
7480           attrs->at_is_optional = a;
7481           break;
7482         case DW_AT_location:
7483           attrs->at_location = a;
7484           break;
7485         case DW_AT_lower_bound:
7486           attrs->at_lower_bound = a;
7487           break;
7488         case DW_AT_mutable:
7489           attrs->at_mutable = a;
7490           break;
7491         case DW_AT_ordering:
7492           attrs->at_ordering = a;
7493           break;
7494         case DW_AT_picture_string:
7495           attrs->at_picture_string = a;
7496           break;
7497         case DW_AT_prototyped:
7498           attrs->at_prototyped = a;
7499           break;
7500         case DW_AT_small:
7501           attrs->at_small = a;
7502           break;
7503         case DW_AT_segment:
7504           attrs->at_segment = a;
7505           break;
7506         case DW_AT_string_length:
7507           attrs->at_string_length = a;
7508           break;
7509 	case DW_AT_string_length_bit_size:
7510 	  attrs->at_string_length_bit_size = a;
7511 	  break;
7512 	case DW_AT_string_length_byte_size:
7513 	  attrs->at_string_length_byte_size = a;
7514 	  break;
7515         case DW_AT_threads_scaled:
7516           attrs->at_threads_scaled = a;
7517           break;
7518         case DW_AT_upper_bound:
7519           attrs->at_upper_bound = a;
7520           break;
7521         case DW_AT_use_location:
7522           attrs->at_use_location = a;
7523           break;
7524         case DW_AT_use_UTF8:
7525           attrs->at_use_UTF8 = a;
7526           break;
7527         case DW_AT_variable_parameter:
7528           attrs->at_variable_parameter = a;
7529           break;
7530         case DW_AT_virtuality:
7531           attrs->at_virtuality = a;
7532           break;
7533         case DW_AT_visibility:
7534           attrs->at_visibility = a;
7535           break;
7536         case DW_AT_vtable_elem_location:
7537           attrs->at_vtable_elem_location = a;
7538           break;
7539         default:
7540           break;
7541         }
7542     }
7543 }
7544 
7545 /* Calculate the checksum of a DIE, using an ordered subset of attributes.  */
7546 
7547 static void
die_checksum_ordered(dw_die_ref die,struct md5_ctx * ctx,int * mark)7548 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7549 {
7550   dw_die_ref c;
7551   dw_die_ref decl;
7552   struct checksum_attributes attrs;
7553 
7554   CHECKSUM_ULEB128 ('D');
7555   CHECKSUM_ULEB128 (die->die_tag);
7556 
7557   memset (&attrs, 0, sizeof (attrs));
7558 
7559   decl = get_AT_ref (die, DW_AT_specification);
7560   if (decl != NULL)
7561     collect_checksum_attributes (&attrs, decl);
7562   collect_checksum_attributes (&attrs, die);
7563 
7564   CHECKSUM_ATTR (attrs.at_name);
7565   CHECKSUM_ATTR (attrs.at_accessibility);
7566   CHECKSUM_ATTR (attrs.at_address_class);
7567   CHECKSUM_ATTR (attrs.at_allocated);
7568   CHECKSUM_ATTR (attrs.at_artificial);
7569   CHECKSUM_ATTR (attrs.at_associated);
7570   CHECKSUM_ATTR (attrs.at_binary_scale);
7571   CHECKSUM_ATTR (attrs.at_bit_offset);
7572   CHECKSUM_ATTR (attrs.at_bit_size);
7573   CHECKSUM_ATTR (attrs.at_bit_stride);
7574   CHECKSUM_ATTR (attrs.at_byte_size);
7575   CHECKSUM_ATTR (attrs.at_byte_stride);
7576   CHECKSUM_ATTR (attrs.at_const_value);
7577   CHECKSUM_ATTR (attrs.at_containing_type);
7578   CHECKSUM_ATTR (attrs.at_count);
7579   CHECKSUM_ATTR (attrs.at_data_location);
7580   CHECKSUM_ATTR (attrs.at_data_member_location);
7581   CHECKSUM_ATTR (attrs.at_decimal_scale);
7582   CHECKSUM_ATTR (attrs.at_decimal_sign);
7583   CHECKSUM_ATTR (attrs.at_default_value);
7584   CHECKSUM_ATTR (attrs.at_digit_count);
7585   CHECKSUM_ATTR (attrs.at_discr);
7586   CHECKSUM_ATTR (attrs.at_discr_list);
7587   CHECKSUM_ATTR (attrs.at_discr_value);
7588   CHECKSUM_ATTR (attrs.at_encoding);
7589   CHECKSUM_ATTR (attrs.at_endianity);
7590   CHECKSUM_ATTR (attrs.at_explicit);
7591   CHECKSUM_ATTR (attrs.at_is_optional);
7592   CHECKSUM_ATTR (attrs.at_location);
7593   CHECKSUM_ATTR (attrs.at_lower_bound);
7594   CHECKSUM_ATTR (attrs.at_mutable);
7595   CHECKSUM_ATTR (attrs.at_ordering);
7596   CHECKSUM_ATTR (attrs.at_picture_string);
7597   CHECKSUM_ATTR (attrs.at_prototyped);
7598   CHECKSUM_ATTR (attrs.at_small);
7599   CHECKSUM_ATTR (attrs.at_segment);
7600   CHECKSUM_ATTR (attrs.at_string_length);
7601   CHECKSUM_ATTR (attrs.at_string_length_bit_size);
7602   CHECKSUM_ATTR (attrs.at_string_length_byte_size);
7603   CHECKSUM_ATTR (attrs.at_threads_scaled);
7604   CHECKSUM_ATTR (attrs.at_upper_bound);
7605   CHECKSUM_ATTR (attrs.at_use_location);
7606   CHECKSUM_ATTR (attrs.at_use_UTF8);
7607   CHECKSUM_ATTR (attrs.at_variable_parameter);
7608   CHECKSUM_ATTR (attrs.at_virtuality);
7609   CHECKSUM_ATTR (attrs.at_visibility);
7610   CHECKSUM_ATTR (attrs.at_vtable_elem_location);
7611   CHECKSUM_ATTR (attrs.at_type);
7612   CHECKSUM_ATTR (attrs.at_friend);
7613   CHECKSUM_ATTR (attrs.at_alignment);
7614 
7615   /* Checksum the child DIEs.  */
7616   c = die->die_child;
7617   if (c) do {
7618     dw_attr_node *name_attr;
7619 
7620     c = c->die_sib;
7621     name_attr = get_AT (c, DW_AT_name);
7622     if (is_template_instantiation (c))
7623       {
7624 	/* Ignore instantiations of member type and function templates.  */
7625       }
7626     else if (name_attr != NULL
7627 	     && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
7628       {
7629 	/* Use a shallow checksum for named nested types and member
7630 	   functions.  */
7631         CHECKSUM_ULEB128 ('S');
7632         CHECKSUM_ULEB128 (c->die_tag);
7633         CHECKSUM_STRING (AT_string (name_attr));
7634       }
7635     else
7636       {
7637 	/* Use a deep checksum for other children.  */
7638         /* Mark this DIE so it gets processed when unmarking.  */
7639         if (c->die_mark == 0)
7640           c->die_mark = -1;
7641         die_checksum_ordered (c, ctx, mark);
7642       }
7643   } while (c != die->die_child);
7644 
7645   CHECKSUM_ULEB128 (0);
7646 }
7647 
7648 /* Add a type name and tag to a hash.  */
7649 static void
die_odr_checksum(int tag,const char * name,md5_ctx * ctx)7650 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
7651 {
7652   CHECKSUM_ULEB128 (tag);
7653   CHECKSUM_STRING (name);
7654 }
7655 
7656 #undef CHECKSUM
7657 #undef CHECKSUM_STRING
7658 #undef CHECKSUM_ATTR
7659 #undef CHECKSUM_LEB128
7660 #undef CHECKSUM_ULEB128
7661 
7662 /* Generate the type signature for DIE.  This is computed by generating an
7663    MD5 checksum over the DIE's tag, its relevant attributes, and its
7664    children.  Attributes that are references to other DIEs are processed
7665    by recursion, using the MARK field to prevent infinite recursion.
7666    If the DIE is nested inside a namespace or another type, we also
7667    need to include that context in the signature.  The lower 64 bits
7668    of the resulting MD5 checksum comprise the signature.  */
7669 
7670 static void
generate_type_signature(dw_die_ref die,comdat_type_node * type_node)7671 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
7672 {
7673   int mark;
7674   const char *name;
7675   unsigned char checksum[16];
7676   struct md5_ctx ctx;
7677   dw_die_ref decl;
7678   dw_die_ref parent;
7679 
7680   name = get_AT_string (die, DW_AT_name);
7681   decl = get_AT_ref (die, DW_AT_specification);
7682   parent = get_die_parent (die);
7683 
7684   /* First, compute a signature for just the type name (and its surrounding
7685      context, if any.  This is stored in the type unit DIE for link-time
7686      ODR (one-definition rule) checking.  */
7687 
7688   if (is_cxx () && name != NULL)
7689     {
7690       md5_init_ctx (&ctx);
7691 
7692       /* Checksum the names of surrounding namespaces and structures.  */
7693       if (parent != NULL)
7694         checksum_die_context (parent, &ctx);
7695 
7696       /* Checksum the current DIE. */
7697       die_odr_checksum (die->die_tag, name, &ctx);
7698       md5_finish_ctx (&ctx, checksum);
7699 
7700       add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
7701     }
7702 
7703   /* Next, compute the complete type signature.  */
7704 
7705   md5_init_ctx (&ctx);
7706   mark = 1;
7707   die->die_mark = mark;
7708 
7709   /* Checksum the names of surrounding namespaces and structures.  */
7710   if (parent != NULL)
7711     checksum_die_context (parent, &ctx);
7712 
7713   /* Checksum the DIE and its children.  */
7714   die_checksum_ordered (die, &ctx, &mark);
7715   unmark_all_dies (die);
7716   md5_finish_ctx (&ctx, checksum);
7717 
7718   /* Store the signature in the type node and link the type DIE and the
7719      type node together.  */
7720   memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
7721           DWARF_TYPE_SIGNATURE_SIZE);
7722   die->comdat_type_p = true;
7723   die->die_id.die_type_node = type_node;
7724   type_node->type_die = die;
7725 
7726   /* If the DIE is a specification, link its declaration to the type node
7727      as well.  */
7728   if (decl != NULL)
7729     {
7730       decl->comdat_type_p = true;
7731       decl->die_id.die_type_node = type_node;
7732     }
7733 }
7734 
7735 /* Do the location expressions look same?  */
7736 static inline int
same_loc_p(dw_loc_descr_ref loc1,dw_loc_descr_ref loc2,int * mark)7737 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7738 {
7739   return loc1->dw_loc_opc == loc2->dw_loc_opc
7740 	 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7741 	 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7742 }
7743 
7744 /* Do the values look the same?  */
7745 static int
same_dw_val_p(const dw_val_node * v1,const dw_val_node * v2,int * mark)7746 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7747 {
7748   dw_loc_descr_ref loc1, loc2;
7749   rtx r1, r2;
7750 
7751   if (v1->val_class != v2->val_class)
7752     return 0;
7753 
7754   switch (v1->val_class)
7755     {
7756     case dw_val_class_const:
7757     case dw_val_class_const_implicit:
7758       return v1->v.val_int == v2->v.val_int;
7759     case dw_val_class_unsigned_const:
7760     case dw_val_class_unsigned_const_implicit:
7761       return v1->v.val_unsigned == v2->v.val_unsigned;
7762     case dw_val_class_const_double:
7763       return v1->v.val_double.high == v2->v.val_double.high
7764 	     && v1->v.val_double.low == v2->v.val_double.low;
7765     case dw_val_class_wide_int:
7766       return *v1->v.val_wide == *v2->v.val_wide;
7767     case dw_val_class_vec:
7768       if (v1->v.val_vec.length != v2->v.val_vec.length
7769 	  || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7770 	return 0;
7771       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7772 		  v1->v.val_vec.length * v1->v.val_vec.elt_size))
7773 	return 0;
7774       return 1;
7775     case dw_val_class_flag:
7776       return v1->v.val_flag == v2->v.val_flag;
7777     case dw_val_class_str:
7778       return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
7779 
7780     case dw_val_class_addr:
7781       r1 = v1->v.val_addr;
7782       r2 = v2->v.val_addr;
7783       if (GET_CODE (r1) != GET_CODE (r2))
7784 	return 0;
7785       return !rtx_equal_p (r1, r2);
7786 
7787     case dw_val_class_offset:
7788       return v1->v.val_offset == v2->v.val_offset;
7789 
7790     case dw_val_class_loc:
7791       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7792 	   loc1 && loc2;
7793 	   loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7794 	if (!same_loc_p (loc1, loc2, mark))
7795 	  return 0;
7796       return !loc1 && !loc2;
7797 
7798     case dw_val_class_die_ref:
7799       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7800 
7801     case dw_val_class_symview:
7802       return strcmp (v1->v.val_symbolic_view, v2->v.val_symbolic_view) == 0;
7803 
7804     case dw_val_class_fde_ref:
7805     case dw_val_class_vms_delta:
7806     case dw_val_class_lbl_id:
7807     case dw_val_class_lineptr:
7808     case dw_val_class_macptr:
7809     case dw_val_class_loclistsptr:
7810     case dw_val_class_high_pc:
7811       return 1;
7812 
7813     case dw_val_class_file:
7814     case dw_val_class_file_implicit:
7815       return v1->v.val_file == v2->v.val_file;
7816 
7817     case dw_val_class_data8:
7818       return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
7819 
7820     default:
7821       return 1;
7822     }
7823 }
7824 
7825 /* Do the attributes look the same?  */
7826 
7827 static int
same_attr_p(dw_attr_node * at1,dw_attr_node * at2,int * mark)7828 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
7829 {
7830   if (at1->dw_attr != at2->dw_attr)
7831     return 0;
7832 
7833   /* We don't care that this was compiled with a different compiler
7834      snapshot; if the output is the same, that's what matters. */
7835   if (at1->dw_attr == DW_AT_producer)
7836     return 1;
7837 
7838   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7839 }
7840 
7841 /* Do the dies look the same?  */
7842 
7843 static int
same_die_p(dw_die_ref die1,dw_die_ref die2,int * mark)7844 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7845 {
7846   dw_die_ref c1, c2;
7847   dw_attr_node *a1;
7848   unsigned ix;
7849 
7850   /* To avoid infinite recursion.  */
7851   if (die1->die_mark)
7852     return die1->die_mark == die2->die_mark;
7853   die1->die_mark = die2->die_mark = ++(*mark);
7854 
7855   if (die1->die_tag != die2->die_tag)
7856     return 0;
7857 
7858   if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
7859     return 0;
7860 
7861   FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
7862     if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
7863       return 0;
7864 
7865   c1 = die1->die_child;
7866   c2 = die2->die_child;
7867   if (! c1)
7868     {
7869       if (c2)
7870 	return 0;
7871     }
7872   else
7873     for (;;)
7874       {
7875 	if (!same_die_p (c1, c2, mark))
7876 	  return 0;
7877 	c1 = c1->die_sib;
7878 	c2 = c2->die_sib;
7879 	if (c1 == die1->die_child)
7880 	  {
7881 	    if (c2 == die2->die_child)
7882 	      break;
7883 	    else
7884 	      return 0;
7885 	  }
7886     }
7887 
7888   return 1;
7889 }
7890 
7891 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7892    children, and set die_symbol.  */
7893 
7894 static void
compute_comp_unit_symbol(dw_die_ref unit_die)7895 compute_comp_unit_symbol (dw_die_ref unit_die)
7896 {
7897   const char *die_name = get_AT_string (unit_die, DW_AT_name);
7898   const char *base = die_name ? lbasename (die_name) : "anonymous";
7899   char *name = XALLOCAVEC (char, strlen (base) + 64);
7900   char *p;
7901   int i, mark;
7902   unsigned char checksum[16];
7903   struct md5_ctx ctx;
7904 
7905   /* Compute the checksum of the DIE, then append part of it as hex digits to
7906      the name filename of the unit.  */
7907 
7908   md5_init_ctx (&ctx);
7909   mark = 0;
7910   die_checksum (unit_die, &ctx, &mark);
7911   unmark_all_dies (unit_die);
7912   md5_finish_ctx (&ctx, checksum);
7913 
7914   /* When we this for comp_unit_die () we have a DW_AT_name that might
7915      not start with a letter but with anything valid for filenames and
7916      clean_symbol_name doesn't fix that up.  Prepend 'g' if the first
7917      character is not a letter.  */
7918   sprintf (name, "%s%s.", ISALPHA (*base) ? "" : "g", base);
7919   clean_symbol_name (name);
7920 
7921   p = name + strlen (name);
7922   for (i = 0; i < 4; i++)
7923     {
7924       sprintf (p, "%.2x", checksum[i]);
7925       p += 2;
7926     }
7927 
7928   unit_die->die_id.die_symbol = xstrdup (name);
7929 }
7930 
7931 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
7932 
7933 static int
is_type_die(dw_die_ref die)7934 is_type_die (dw_die_ref die)
7935 {
7936   switch (die->die_tag)
7937     {
7938     case DW_TAG_array_type:
7939     case DW_TAG_class_type:
7940     case DW_TAG_interface_type:
7941     case DW_TAG_enumeration_type:
7942     case DW_TAG_pointer_type:
7943     case DW_TAG_reference_type:
7944     case DW_TAG_rvalue_reference_type:
7945     case DW_TAG_string_type:
7946     case DW_TAG_structure_type:
7947     case DW_TAG_subroutine_type:
7948     case DW_TAG_union_type:
7949     case DW_TAG_ptr_to_member_type:
7950     case DW_TAG_set_type:
7951     case DW_TAG_subrange_type:
7952     case DW_TAG_base_type:
7953     case DW_TAG_const_type:
7954     case DW_TAG_file_type:
7955     case DW_TAG_packed_type:
7956     case DW_TAG_volatile_type:
7957     case DW_TAG_typedef:
7958       return 1;
7959     default:
7960       return 0;
7961     }
7962 }
7963 
7964 /* Returns true iff C is a compile-unit DIE.  */
7965 
7966 static inline bool
is_cu_die(dw_die_ref c)7967 is_cu_die (dw_die_ref c)
7968 {
7969   return c && (c->die_tag == DW_TAG_compile_unit
7970 	       || c->die_tag == DW_TAG_skeleton_unit);
7971 }
7972 
7973 /* Returns true iff C is a unit DIE of some sort.  */
7974 
7975 static inline bool
is_unit_die(dw_die_ref c)7976 is_unit_die (dw_die_ref c)
7977 {
7978   return c && (c->die_tag == DW_TAG_compile_unit
7979 	       || c->die_tag == DW_TAG_partial_unit
7980 	       || c->die_tag == DW_TAG_type_unit
7981 	       || c->die_tag == DW_TAG_skeleton_unit);
7982 }
7983 
7984 /* Returns true iff C is a namespace DIE.  */
7985 
7986 static inline bool
is_namespace_die(dw_die_ref c)7987 is_namespace_die (dw_die_ref c)
7988 {
7989   return c && c->die_tag == DW_TAG_namespace;
7990 }
7991 
7992 /* Return non-zero if this DIE is a template parameter.  */
7993 
7994 static inline bool
is_template_parameter(dw_die_ref die)7995 is_template_parameter (dw_die_ref die)
7996 {
7997   switch (die->die_tag)
7998     {
7999     case DW_TAG_template_type_param:
8000     case DW_TAG_template_value_param:
8001     case DW_TAG_GNU_template_template_param:
8002     case DW_TAG_GNU_template_parameter_pack:
8003       return true;
8004     default:
8005       return false;
8006     }
8007 }
8008 
8009 /* Return non-zero if this DIE represents a template instantiation.  */
8010 
8011 static inline bool
is_template_instantiation(dw_die_ref die)8012 is_template_instantiation (dw_die_ref die)
8013 {
8014   dw_die_ref c;
8015 
8016   if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
8017     return false;
8018   FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
8019   return false;
8020 }
8021 
8022 static char *
gen_internal_sym(const char * prefix)8023 gen_internal_sym (const char *prefix)
8024 {
8025   char buf[MAX_ARTIFICIAL_LABEL_BYTES];
8026 
8027   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
8028   return xstrdup (buf);
8029 }
8030 
8031 /* Return non-zero if this DIE is a declaration.  */
8032 
8033 static int
is_declaration_die(dw_die_ref die)8034 is_declaration_die (dw_die_ref die)
8035 {
8036   dw_attr_node *a;
8037   unsigned ix;
8038 
8039   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8040     if (a->dw_attr == DW_AT_declaration)
8041       return 1;
8042 
8043   return 0;
8044 }
8045 
8046 /* Return non-zero if this DIE is nested inside a subprogram.  */
8047 
8048 static int
is_nested_in_subprogram(dw_die_ref die)8049 is_nested_in_subprogram (dw_die_ref die)
8050 {
8051   dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
8052 
8053   if (decl == NULL)
8054     decl = die;
8055   return local_scope_p (decl);
8056 }
8057 
8058 /* Return non-zero if this DIE contains a defining declaration of a
8059    subprogram.  */
8060 
8061 static int
contains_subprogram_definition(dw_die_ref die)8062 contains_subprogram_definition (dw_die_ref die)
8063 {
8064   dw_die_ref c;
8065 
8066   if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
8067     return 1;
8068   FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
8069   return 0;
8070 }
8071 
8072 /* Return non-zero if this is a type DIE that should be moved to a
8073    COMDAT .debug_types section or .debug_info section with DW_UT_*type
8074    unit type.  */
8075 
8076 static int
should_move_die_to_comdat(dw_die_ref die)8077 should_move_die_to_comdat (dw_die_ref die)
8078 {
8079   switch (die->die_tag)
8080     {
8081     case DW_TAG_class_type:
8082     case DW_TAG_structure_type:
8083     case DW_TAG_enumeration_type:
8084     case DW_TAG_union_type:
8085       /* Don't move declarations, inlined instances, types nested in a
8086 	 subprogram, or types that contain subprogram definitions.  */
8087       if (is_declaration_die (die)
8088           || get_AT (die, DW_AT_abstract_origin)
8089           || is_nested_in_subprogram (die)
8090           || contains_subprogram_definition (die))
8091         return 0;
8092       return 1;
8093     case DW_TAG_array_type:
8094     case DW_TAG_interface_type:
8095     case DW_TAG_pointer_type:
8096     case DW_TAG_reference_type:
8097     case DW_TAG_rvalue_reference_type:
8098     case DW_TAG_string_type:
8099     case DW_TAG_subroutine_type:
8100     case DW_TAG_ptr_to_member_type:
8101     case DW_TAG_set_type:
8102     case DW_TAG_subrange_type:
8103     case DW_TAG_base_type:
8104     case DW_TAG_const_type:
8105     case DW_TAG_file_type:
8106     case DW_TAG_packed_type:
8107     case DW_TAG_volatile_type:
8108     case DW_TAG_typedef:
8109     default:
8110       return 0;
8111     }
8112 }
8113 
8114 /* Make a clone of DIE.  */
8115 
8116 static dw_die_ref
clone_die(dw_die_ref die)8117 clone_die (dw_die_ref die)
8118 {
8119   dw_die_ref clone = new_die_raw (die->die_tag);
8120   dw_attr_node *a;
8121   unsigned ix;
8122 
8123   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8124     add_dwarf_attr (clone, a);
8125 
8126   return clone;
8127 }
8128 
8129 /* Make a clone of the tree rooted at DIE.  */
8130 
8131 static dw_die_ref
clone_tree(dw_die_ref die)8132 clone_tree (dw_die_ref die)
8133 {
8134   dw_die_ref c;
8135   dw_die_ref clone = clone_die (die);
8136 
8137   FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
8138 
8139   return clone;
8140 }
8141 
8142 /* Make a clone of DIE as a declaration.  */
8143 
8144 static dw_die_ref
clone_as_declaration(dw_die_ref die)8145 clone_as_declaration (dw_die_ref die)
8146 {
8147   dw_die_ref clone;
8148   dw_die_ref decl;
8149   dw_attr_node *a;
8150   unsigned ix;
8151 
8152   /* If the DIE is already a declaration, just clone it.  */
8153   if (is_declaration_die (die))
8154     return clone_die (die);
8155 
8156   /* If the DIE is a specification, just clone its declaration DIE.  */
8157   decl = get_AT_ref (die, DW_AT_specification);
8158   if (decl != NULL)
8159     {
8160       clone = clone_die (decl);
8161       if (die->comdat_type_p)
8162 	add_AT_die_ref (clone, DW_AT_signature, die);
8163       return clone;
8164     }
8165 
8166   clone = new_die_raw (die->die_tag);
8167 
8168   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8169     {
8170       /* We don't want to copy over all attributes.
8171          For example we don't want DW_AT_byte_size because otherwise we will no
8172          longer have a declaration and GDB will treat it as a definition.  */
8173 
8174       switch (a->dw_attr)
8175         {
8176         case DW_AT_abstract_origin:
8177         case DW_AT_artificial:
8178         case DW_AT_containing_type:
8179         case DW_AT_external:
8180         case DW_AT_name:
8181         case DW_AT_type:
8182         case DW_AT_virtuality:
8183         case DW_AT_linkage_name:
8184         case DW_AT_MIPS_linkage_name:
8185           add_dwarf_attr (clone, a);
8186           break;
8187         case DW_AT_byte_size:
8188 	case DW_AT_alignment:
8189         default:
8190           break;
8191         }
8192     }
8193 
8194   if (die->comdat_type_p)
8195     add_AT_die_ref (clone, DW_AT_signature, die);
8196 
8197   add_AT_flag (clone, DW_AT_declaration, 1);
8198   return clone;
8199 }
8200 
8201 
8202 /* Structure to map a DIE in one CU to its copy in a comdat type unit.  */
8203 
8204 struct decl_table_entry
8205 {
8206   dw_die_ref orig;
8207   dw_die_ref copy;
8208 };
8209 
8210 /* Helpers to manipulate hash table of copied declarations.  */
8211 
8212 /* Hashtable helpers.  */
8213 
8214 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
8215 {
8216   typedef die_struct *compare_type;
8217   static inline hashval_t hash (const decl_table_entry *);
8218   static inline bool equal (const decl_table_entry *, const die_struct *);
8219 };
8220 
8221 inline hashval_t
hash(const decl_table_entry * entry)8222 decl_table_entry_hasher::hash (const decl_table_entry *entry)
8223 {
8224   return htab_hash_pointer (entry->orig);
8225 }
8226 
8227 inline bool
equal(const decl_table_entry * entry1,const die_struct * entry2)8228 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
8229 				const die_struct *entry2)
8230 {
8231   return entry1->orig == entry2;
8232 }
8233 
8234 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
8235 
8236 /* Copy DIE and its ancestors, up to, but not including, the compile unit
8237    or type unit entry, to a new tree.  Adds the new tree to UNIT and returns
8238    a pointer to the copy of DIE.  If DECL_TABLE is provided, it is used
8239    to check if the ancestor has already been copied into UNIT.  */
8240 
8241 static dw_die_ref
copy_ancestor_tree(dw_die_ref unit,dw_die_ref die,decl_hash_type * decl_table)8242 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
8243 		    decl_hash_type *decl_table)
8244 {
8245   dw_die_ref parent = die->die_parent;
8246   dw_die_ref new_parent = unit;
8247   dw_die_ref copy;
8248   decl_table_entry **slot = NULL;
8249   struct decl_table_entry *entry = NULL;
8250 
8251   /* If DIE refers to a stub unfold that so we get the appropriate
8252      DIE registered as orig in decl_table.  */
8253   if (dw_die_ref c = get_AT_ref (die, DW_AT_signature))
8254     die = c;
8255 
8256   if (decl_table)
8257     {
8258       /* Check if the entry has already been copied to UNIT.  */
8259       slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
8260 					      INSERT);
8261       if (*slot != HTAB_EMPTY_ENTRY)
8262         {
8263           entry = *slot;
8264           return entry->copy;
8265         }
8266 
8267       /* Record in DECL_TABLE that DIE has been copied to UNIT.  */
8268       entry = XCNEW (struct decl_table_entry);
8269       entry->orig = die;
8270       entry->copy = NULL;
8271       *slot = entry;
8272     }
8273 
8274   if (parent != NULL)
8275     {
8276       dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
8277       if (spec != NULL)
8278         parent = spec;
8279       if (!is_unit_die (parent))
8280         new_parent = copy_ancestor_tree (unit, parent, decl_table);
8281     }
8282 
8283   copy = clone_as_declaration (die);
8284   add_child_die (new_parent, copy);
8285 
8286   if (decl_table)
8287     {
8288       /* Record the pointer to the copy.  */
8289       entry->copy = copy;
8290     }
8291 
8292   return copy;
8293 }
8294 /* Copy the declaration context to the new type unit DIE.  This includes
8295    any surrounding namespace or type declarations.  If the DIE has an
8296    AT_specification attribute, it also includes attributes and children
8297    attached to the specification, and returns a pointer to the original
8298    parent of the declaration DIE.  Returns NULL otherwise.  */
8299 
8300 static dw_die_ref
copy_declaration_context(dw_die_ref unit,dw_die_ref die)8301 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
8302 {
8303   dw_die_ref decl;
8304   dw_die_ref new_decl;
8305   dw_die_ref orig_parent = NULL;
8306 
8307   decl = get_AT_ref (die, DW_AT_specification);
8308   if (decl == NULL)
8309     decl = die;
8310   else
8311     {
8312       unsigned ix;
8313       dw_die_ref c;
8314       dw_attr_node *a;
8315 
8316       /* The original DIE will be changed to a declaration, and must
8317          be moved to be a child of the original declaration DIE.  */
8318       orig_parent = decl->die_parent;
8319 
8320       /* Copy the type node pointer from the new DIE to the original
8321          declaration DIE so we can forward references later.  */
8322       decl->comdat_type_p = true;
8323       decl->die_id.die_type_node = die->die_id.die_type_node;
8324 
8325       remove_AT (die, DW_AT_specification);
8326 
8327       FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
8328         {
8329           if (a->dw_attr != DW_AT_name
8330               && a->dw_attr != DW_AT_declaration
8331               && a->dw_attr != DW_AT_external)
8332             add_dwarf_attr (die, a);
8333         }
8334 
8335       FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
8336     }
8337 
8338   if (decl->die_parent != NULL
8339       && !is_unit_die (decl->die_parent))
8340     {
8341       new_decl = copy_ancestor_tree (unit, decl, NULL);
8342       if (new_decl != NULL)
8343         {
8344           remove_AT (new_decl, DW_AT_signature);
8345           add_AT_specification (die, new_decl);
8346         }
8347     }
8348 
8349   return orig_parent;
8350 }
8351 
8352 /* Generate the skeleton ancestor tree for the given NODE, then clone
8353    the DIE and add the clone into the tree.  */
8354 
8355 static void
generate_skeleton_ancestor_tree(skeleton_chain_node * node)8356 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
8357 {
8358   if (node->new_die != NULL)
8359     return;
8360 
8361   node->new_die = clone_as_declaration (node->old_die);
8362 
8363   if (node->parent != NULL)
8364     {
8365       generate_skeleton_ancestor_tree (node->parent);
8366       add_child_die (node->parent->new_die, node->new_die);
8367     }
8368 }
8369 
8370 /* Generate a skeleton tree of DIEs containing any declarations that are
8371    found in the original tree.  We traverse the tree looking for declaration
8372    DIEs, and construct the skeleton from the bottom up whenever we find one.  */
8373 
8374 static void
generate_skeleton_bottom_up(skeleton_chain_node * parent)8375 generate_skeleton_bottom_up (skeleton_chain_node *parent)
8376 {
8377   skeleton_chain_node node;
8378   dw_die_ref c;
8379   dw_die_ref first;
8380   dw_die_ref prev = NULL;
8381   dw_die_ref next = NULL;
8382 
8383   node.parent = parent;
8384 
8385   first = c = parent->old_die->die_child;
8386   if (c)
8387     next = c->die_sib;
8388   if (c) do {
8389     if (prev == NULL || prev->die_sib == c)
8390       prev = c;
8391     c = next;
8392     next = (c == first ? NULL : c->die_sib);
8393     node.old_die = c;
8394     node.new_die = NULL;
8395     if (is_declaration_die (c))
8396       {
8397 	if (is_template_instantiation (c))
8398 	  {
8399 	    /* Instantiated templates do not need to be cloned into the
8400 	       type unit.  Just move the DIE and its children back to
8401 	       the skeleton tree (in the main CU).  */
8402 	    remove_child_with_prev (c, prev);
8403 	    add_child_die (parent->new_die, c);
8404 	    c = prev;
8405 	  }
8406 	else if (c->comdat_type_p)
8407 	  {
8408 	    /* This is the skeleton of earlier break_out_comdat_types
8409 	       type.  Clone the existing DIE, but keep the children
8410 	       under the original (which is in the main CU).  */
8411 	    dw_die_ref clone = clone_die (c);
8412 
8413 	    replace_child (c, clone, prev);
8414 	    generate_skeleton_ancestor_tree (parent);
8415 	    add_child_die (parent->new_die, c);
8416 	    c = clone;
8417 	    continue;
8418 	  }
8419 	else
8420 	  {
8421 	    /* Clone the existing DIE, move the original to the skeleton
8422 	       tree (which is in the main CU), and put the clone, with
8423 	       all the original's children, where the original came from
8424 	       (which is about to be moved to the type unit).  */
8425 	    dw_die_ref clone = clone_die (c);
8426 	    move_all_children (c, clone);
8427 
8428 	    /* If the original has a DW_AT_object_pointer attribute,
8429 	       it would now point to a child DIE just moved to the
8430 	       cloned tree, so we need to remove that attribute from
8431 	       the original.  */
8432 	    remove_AT (c, DW_AT_object_pointer);
8433 
8434 	    replace_child (c, clone, prev);
8435 	    generate_skeleton_ancestor_tree (parent);
8436 	    add_child_die (parent->new_die, c);
8437 	    node.old_die = clone;
8438 	    node.new_die = c;
8439 	    c = clone;
8440 	  }
8441       }
8442     generate_skeleton_bottom_up (&node);
8443   } while (next != NULL);
8444 }
8445 
8446 /* Wrapper function for generate_skeleton_bottom_up.  */
8447 
8448 static dw_die_ref
generate_skeleton(dw_die_ref die)8449 generate_skeleton (dw_die_ref die)
8450 {
8451   skeleton_chain_node node;
8452 
8453   node.old_die = die;
8454   node.new_die = NULL;
8455   node.parent = NULL;
8456 
8457   /* If this type definition is nested inside another type,
8458      and is not an instantiation of a template, always leave
8459      at least a declaration in its place.  */
8460   if (die->die_parent != NULL
8461       && is_type_die (die->die_parent)
8462       && !is_template_instantiation (die))
8463     node.new_die = clone_as_declaration (die);
8464 
8465   generate_skeleton_bottom_up (&node);
8466   return node.new_die;
8467 }
8468 
8469 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
8470    declaration.  The original DIE is moved to a new compile unit so that
8471    existing references to it follow it to the new location.  If any of the
8472    original DIE's descendants is a declaration, we need to replace the
8473    original DIE with a skeleton tree and move the declarations back into the
8474    skeleton tree.  */
8475 
8476 static dw_die_ref
remove_child_or_replace_with_skeleton(dw_die_ref unit,dw_die_ref child,dw_die_ref prev)8477 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
8478 				       dw_die_ref prev)
8479 {
8480   dw_die_ref skeleton, orig_parent;
8481 
8482   /* Copy the declaration context to the type unit DIE.  If the returned
8483      ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
8484      that DIE.  */
8485   orig_parent = copy_declaration_context (unit, child);
8486 
8487   skeleton = generate_skeleton (child);
8488   if (skeleton == NULL)
8489     remove_child_with_prev (child, prev);
8490   else
8491     {
8492       skeleton->comdat_type_p = true;
8493       skeleton->die_id.die_type_node = child->die_id.die_type_node;
8494 
8495       /* If the original DIE was a specification, we need to put
8496          the skeleton under the parent DIE of the declaration.
8497 	 This leaves the original declaration in the tree, but
8498 	 it will be pruned later since there are no longer any
8499 	 references to it.  */
8500       if (orig_parent != NULL)
8501 	{
8502 	  remove_child_with_prev (child, prev);
8503 	  add_child_die (orig_parent, skeleton);
8504 	}
8505       else
8506 	replace_child (child, skeleton, prev);
8507     }
8508 
8509   return skeleton;
8510 }
8511 
8512 static void
8513 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8514 			       comdat_type_node *type_node,
8515 			       hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs);
8516 
8517 /* Helper for copy_dwarf_procs_ref_in_dies.  Make a copy of the DIE DWARF
8518    procedure, put it under TYPE_NODE and return the copy.  Continue looking for
8519    DWARF procedure references in the DW_AT_location attribute.  */
8520 
8521 static dw_die_ref
copy_dwarf_procedure(dw_die_ref die,comdat_type_node * type_node,hash_map<dw_die_ref,dw_die_ref> & copied_dwarf_procs)8522 copy_dwarf_procedure (dw_die_ref die,
8523 		      comdat_type_node *type_node,
8524 		      hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8525 {
8526   gcc_assert (die->die_tag == DW_TAG_dwarf_procedure);
8527 
8528   /* DWARF procedures are not supposed to have children...  */
8529   gcc_assert (die->die_child == NULL);
8530 
8531   /* ... and they are supposed to have only one attribute: DW_AT_location.  */
8532   gcc_assert (vec_safe_length (die->die_attr) == 1
8533 	      && ((*die->die_attr)[0].dw_attr == DW_AT_location));
8534 
8535   /* Do not copy more than once DWARF procedures.  */
8536   bool existed;
8537   dw_die_ref &die_copy = copied_dwarf_procs.get_or_insert (die, &existed);
8538   if (existed)
8539     return die_copy;
8540 
8541   die_copy = clone_die (die);
8542   add_child_die (type_node->root_die, die_copy);
8543   copy_dwarf_procs_ref_in_attrs (die_copy, type_node, copied_dwarf_procs);
8544   return die_copy;
8545 }
8546 
8547 /* Helper for copy_dwarf_procs_ref_in_dies.  Look for references to DWARF
8548    procedures in DIE's attributes.  */
8549 
8550 static void
copy_dwarf_procs_ref_in_attrs(dw_die_ref die,comdat_type_node * type_node,hash_map<dw_die_ref,dw_die_ref> & copied_dwarf_procs)8551 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8552 			       comdat_type_node *type_node,
8553 			       hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8554 {
8555   dw_attr_node *a;
8556   unsigned i;
8557 
8558   FOR_EACH_VEC_SAFE_ELT (die->die_attr, i, a)
8559     {
8560       dw_loc_descr_ref loc;
8561 
8562       if (a->dw_attr_val.val_class != dw_val_class_loc)
8563 	continue;
8564 
8565       for (loc = a->dw_attr_val.v.val_loc; loc != NULL; loc = loc->dw_loc_next)
8566 	{
8567 	  switch (loc->dw_loc_opc)
8568 	    {
8569 	    case DW_OP_call2:
8570 	    case DW_OP_call4:
8571 	    case DW_OP_call_ref:
8572 	      gcc_assert (loc->dw_loc_oprnd1.val_class
8573 			  == dw_val_class_die_ref);
8574 	      loc->dw_loc_oprnd1.v.val_die_ref.die
8575 	        = copy_dwarf_procedure (loc->dw_loc_oprnd1.v.val_die_ref.die,
8576 					type_node,
8577 					copied_dwarf_procs);
8578 
8579 	    default:
8580 	      break;
8581 	    }
8582 	}
8583     }
8584 }
8585 
8586 /* Copy DWARF procedures that are referenced by the DIE tree to TREE_NODE and
8587    rewrite references to point to the copies.
8588 
8589    References are looked for in DIE's attributes and recursively in all its
8590    children attributes that are location descriptions. COPIED_DWARF_PROCS is a
8591    mapping from old DWARF procedures to their copy. It is used not to copy
8592    twice the same DWARF procedure under TYPE_NODE.  */
8593 
8594 static void
copy_dwarf_procs_ref_in_dies(dw_die_ref die,comdat_type_node * type_node,hash_map<dw_die_ref,dw_die_ref> & copied_dwarf_procs)8595 copy_dwarf_procs_ref_in_dies (dw_die_ref die,
8596 			      comdat_type_node *type_node,
8597 			      hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8598 {
8599   dw_die_ref c;
8600 
8601   copy_dwarf_procs_ref_in_attrs (die, type_node, copied_dwarf_procs);
8602   FOR_EACH_CHILD (die, c, copy_dwarf_procs_ref_in_dies (c,
8603 							type_node,
8604 							copied_dwarf_procs));
8605 }
8606 
8607 /* Traverse the DIE and set up additional .debug_types or .debug_info
8608    DW_UT_*type sections for each type worthy of being placed in a COMDAT
8609    section.  */
8610 
8611 static void
break_out_comdat_types(dw_die_ref die)8612 break_out_comdat_types (dw_die_ref die)
8613 {
8614   dw_die_ref c;
8615   dw_die_ref first;
8616   dw_die_ref prev = NULL;
8617   dw_die_ref next = NULL;
8618   dw_die_ref unit = NULL;
8619 
8620   first = c = die->die_child;
8621   if (c)
8622     next = c->die_sib;
8623   if (c) do {
8624     if (prev == NULL || prev->die_sib == c)
8625       prev = c;
8626     c = next;
8627     next = (c == first ? NULL : c->die_sib);
8628     if (should_move_die_to_comdat (c))
8629       {
8630         dw_die_ref replacement;
8631 	comdat_type_node *type_node;
8632 
8633         /* Break out nested types into their own type units.  */
8634         break_out_comdat_types (c);
8635 
8636         /* Create a new type unit DIE as the root for the new tree.  */
8637         unit = new_die (DW_TAG_type_unit, NULL, NULL);
8638         add_AT_unsigned (unit, DW_AT_language,
8639                          get_AT_unsigned (comp_unit_die (), DW_AT_language));
8640 
8641 	/* Add the new unit's type DIE into the comdat type list.  */
8642         type_node = ggc_cleared_alloc<comdat_type_node> ();
8643         type_node->root_die = unit;
8644         type_node->next = comdat_type_list;
8645         comdat_type_list = type_node;
8646 
8647         /* Generate the type signature.  */
8648         generate_type_signature (c, type_node);
8649 
8650         /* Copy the declaration context, attributes, and children of the
8651            declaration into the new type unit DIE, then remove this DIE
8652 	   from the main CU (or replace it with a skeleton if necessary).  */
8653 	replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
8654 	type_node->skeleton_die = replacement;
8655 
8656         /* Add the DIE to the new compunit.  */
8657 	add_child_die (unit, c);
8658 
8659 	/* Types can reference DWARF procedures for type size or data location
8660 	   expressions.  Calls in DWARF expressions cannot target procedures
8661 	   that are not in the same section.  So we must copy DWARF procedures
8662 	   along with this type and then rewrite references to them.  */
8663 	hash_map<dw_die_ref, dw_die_ref> copied_dwarf_procs;
8664 	copy_dwarf_procs_ref_in_dies (c, type_node, copied_dwarf_procs);
8665 
8666         if (replacement != NULL)
8667           c = replacement;
8668       }
8669     else if (c->die_tag == DW_TAG_namespace
8670              || c->die_tag == DW_TAG_class_type
8671              || c->die_tag == DW_TAG_structure_type
8672              || c->die_tag == DW_TAG_union_type)
8673       {
8674         /* Look for nested types that can be broken out.  */
8675         break_out_comdat_types (c);
8676       }
8677   } while (next != NULL);
8678 }
8679 
8680 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
8681    Enter all the cloned children into the hash table decl_table.  */
8682 
8683 static dw_die_ref
clone_tree_partial(dw_die_ref die,decl_hash_type * decl_table)8684 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
8685 {
8686   dw_die_ref c;
8687   dw_die_ref clone;
8688   struct decl_table_entry *entry;
8689   decl_table_entry **slot;
8690 
8691   if (die->die_tag == DW_TAG_subprogram)
8692     clone = clone_as_declaration (die);
8693   else
8694     clone = clone_die (die);
8695 
8696   slot = decl_table->find_slot_with_hash (die,
8697 					  htab_hash_pointer (die), INSERT);
8698 
8699   /* Assert that DIE isn't in the hash table yet.  If it would be there
8700      before, the ancestors would be necessarily there as well, therefore
8701      clone_tree_partial wouldn't be called.  */
8702   gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8703 
8704   entry = XCNEW (struct decl_table_entry);
8705   entry->orig = die;
8706   entry->copy = clone;
8707   *slot = entry;
8708 
8709   if (die->die_tag != DW_TAG_subprogram)
8710     FOR_EACH_CHILD (die, c,
8711 		    add_child_die (clone, clone_tree_partial (c, decl_table)));
8712 
8713   return clone;
8714 }
8715 
8716 /* Walk the DIE and its children, looking for references to incomplete
8717    or trivial types that are unmarked (i.e., that are not in the current
8718    type_unit).  */
8719 
8720 static void
copy_decls_walk(dw_die_ref unit,dw_die_ref die,decl_hash_type * decl_table)8721 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
8722 {
8723   dw_die_ref c;
8724   dw_attr_node *a;
8725   unsigned ix;
8726 
8727   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8728     {
8729       if (AT_class (a) == dw_val_class_die_ref)
8730         {
8731           dw_die_ref targ = AT_ref (a);
8732           decl_table_entry **slot;
8733           struct decl_table_entry *entry;
8734 
8735           if (targ->die_mark != 0 || targ->comdat_type_p)
8736             continue;
8737 
8738           slot = decl_table->find_slot_with_hash (targ,
8739 						  htab_hash_pointer (targ),
8740 						  INSERT);
8741 
8742           if (*slot != HTAB_EMPTY_ENTRY)
8743             {
8744               /* TARG has already been copied, so we just need to
8745                  modify the reference to point to the copy.  */
8746               entry = *slot;
8747               a->dw_attr_val.v.val_die_ref.die = entry->copy;
8748             }
8749           else
8750             {
8751               dw_die_ref parent = unit;
8752 	      dw_die_ref copy = clone_die (targ);
8753 
8754               /* Record in DECL_TABLE that TARG has been copied.
8755                  Need to do this now, before the recursive call,
8756                  because DECL_TABLE may be expanded and SLOT
8757                  would no longer be a valid pointer.  */
8758               entry = XCNEW (struct decl_table_entry);
8759               entry->orig = targ;
8760               entry->copy = copy;
8761               *slot = entry;
8762 
8763 	      /* If TARG is not a declaration DIE, we need to copy its
8764 	         children.  */
8765 	      if (!is_declaration_die (targ))
8766 		{
8767 		  FOR_EACH_CHILD (
8768 		      targ, c,
8769 		      add_child_die (copy,
8770 				     clone_tree_partial (c, decl_table)));
8771 		}
8772 
8773               /* Make sure the cloned tree is marked as part of the
8774                  type unit.  */
8775               mark_dies (copy);
8776 
8777               /* If TARG has surrounding context, copy its ancestor tree
8778                  into the new type unit.  */
8779               if (targ->die_parent != NULL
8780 		  && !is_unit_die (targ->die_parent))
8781                 parent = copy_ancestor_tree (unit, targ->die_parent,
8782                                              decl_table);
8783 
8784               add_child_die (parent, copy);
8785               a->dw_attr_val.v.val_die_ref.die = copy;
8786 
8787               /* Make sure the newly-copied DIE is walked.  If it was
8788                  installed in a previously-added context, it won't
8789                  get visited otherwise.  */
8790               if (parent != unit)
8791 		{
8792 		  /* Find the highest point of the newly-added tree,
8793 		     mark each node along the way, and walk from there.  */
8794 		  parent->die_mark = 1;
8795 		  while (parent->die_parent
8796 		  	 && parent->die_parent->die_mark == 0)
8797 		    {
8798 		      parent = parent->die_parent;
8799 		      parent->die_mark = 1;
8800 		    }
8801 		  copy_decls_walk (unit, parent, decl_table);
8802 		}
8803             }
8804         }
8805     }
8806 
8807   FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8808 }
8809 
8810 /* Collect skeleton dies in DIE created by break_out_comdat_types already
8811    and record them in DECL_TABLE.  */
8812 
8813 static void
collect_skeleton_dies(dw_die_ref die,decl_hash_type * decl_table)8814 collect_skeleton_dies (dw_die_ref die, decl_hash_type *decl_table)
8815 {
8816   dw_die_ref c;
8817 
8818   if (dw_attr_node *a = get_AT (die, DW_AT_signature))
8819     {
8820       dw_die_ref targ = AT_ref (a);
8821       gcc_assert (targ->die_mark == 0 && targ->comdat_type_p);
8822       decl_table_entry **slot
8823         = decl_table->find_slot_with_hash (targ,
8824 					   htab_hash_pointer (targ),
8825 					   INSERT);
8826       gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8827       /* Record in DECL_TABLE that TARG has been already copied
8828 	 by remove_child_or_replace_with_skeleton.  */
8829       decl_table_entry *entry = XCNEW (struct decl_table_entry);
8830       entry->orig = targ;
8831       entry->copy = die;
8832       *slot = entry;
8833     }
8834   FOR_EACH_CHILD (die, c, collect_skeleton_dies (c, decl_table));
8835 }
8836 
8837 /* Copy declarations for "unworthy" types into the new comdat section.
8838    Incomplete types, modified types, and certain other types aren't broken
8839    out into comdat sections of their own, so they don't have a signature,
8840    and we need to copy the declaration into the same section so that we
8841    don't have an external reference.  */
8842 
8843 static void
copy_decls_for_unworthy_types(dw_die_ref unit)8844 copy_decls_for_unworthy_types (dw_die_ref unit)
8845 {
8846   mark_dies (unit);
8847   decl_hash_type decl_table (10);
8848   collect_skeleton_dies (unit, &decl_table);
8849   copy_decls_walk (unit, unit, &decl_table);
8850   unmark_dies (unit);
8851 }
8852 
8853 /* Traverse the DIE and add a sibling attribute if it may have the
8854    effect of speeding up access to siblings.  To save some space,
8855    avoid generating sibling attributes for DIE's without children.  */
8856 
8857 static void
add_sibling_attributes(dw_die_ref die)8858 add_sibling_attributes (dw_die_ref die)
8859 {
8860   dw_die_ref c;
8861 
8862   if (! die->die_child)
8863     return;
8864 
8865   if (die->die_parent && die != die->die_parent->die_child)
8866     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8867 
8868   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8869 }
8870 
8871 /* Output all location lists for the DIE and its children.  */
8872 
8873 static void
output_location_lists(dw_die_ref die)8874 output_location_lists (dw_die_ref die)
8875 {
8876   dw_die_ref c;
8877   dw_attr_node *a;
8878   unsigned ix;
8879 
8880   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8881     if (AT_class (a) == dw_val_class_loc_list)
8882       output_loc_list (AT_loc_list (a));
8883 
8884   FOR_EACH_CHILD (die, c, output_location_lists (c));
8885 }
8886 
8887 /* During assign_location_list_indexes and output_loclists_offset the
8888    current index, after it the number of assigned indexes (i.e. how
8889    large the .debug_loclists* offset table should be).  */
8890 static unsigned int loc_list_idx;
8891 
8892 /* Output all location list offsets for the DIE and its children.  */
8893 
8894 static void
output_loclists_offsets(dw_die_ref die)8895 output_loclists_offsets (dw_die_ref die)
8896 {
8897   dw_die_ref c;
8898   dw_attr_node *a;
8899   unsigned ix;
8900 
8901   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8902     if (AT_class (a) == dw_val_class_loc_list)
8903       {
8904 	dw_loc_list_ref l = AT_loc_list (a);
8905 	if (l->offset_emitted)
8906 	  continue;
8907 	dw2_asm_output_delta (dwarf_offset_size, l->ll_symbol,
8908 			      loc_section_label, NULL);
8909 	gcc_assert (l->hash == loc_list_idx);
8910 	loc_list_idx++;
8911 	l->offset_emitted = true;
8912       }
8913 
8914   FOR_EACH_CHILD (die, c, output_loclists_offsets (c));
8915 }
8916 
8917 /* Recursively set indexes of location lists.  */
8918 
8919 static void
assign_location_list_indexes(dw_die_ref die)8920 assign_location_list_indexes (dw_die_ref die)
8921 {
8922   dw_die_ref c;
8923   dw_attr_node *a;
8924   unsigned ix;
8925 
8926   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8927     if (AT_class (a) == dw_val_class_loc_list)
8928       {
8929 	dw_loc_list_ref list = AT_loc_list (a);
8930 	if (!list->num_assigned)
8931 	  {
8932 	    list->num_assigned = true;
8933 	    list->hash = loc_list_idx++;
8934 	  }
8935       }
8936 
8937   FOR_EACH_CHILD (die, c, assign_location_list_indexes (c));
8938 }
8939 
8940 /* We want to limit the number of external references, because they are
8941    larger than local references: a relocation takes multiple words, and
8942    even a sig8 reference is always eight bytes, whereas a local reference
8943    can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
8944    So if we encounter multiple external references to the same type DIE, we
8945    make a local typedef stub for it and redirect all references there.
8946 
8947    This is the element of the hash table for keeping track of these
8948    references.  */
8949 
8950 struct external_ref
8951 {
8952   dw_die_ref type;
8953   dw_die_ref stub;
8954   unsigned n_refs;
8955 };
8956 
8957 /* Hashtable helpers.  */
8958 
8959 struct external_ref_hasher : free_ptr_hash <external_ref>
8960 {
8961   static inline hashval_t hash (const external_ref *);
8962   static inline bool equal (const external_ref *, const external_ref *);
8963 };
8964 
8965 inline hashval_t
hash(const external_ref * r)8966 external_ref_hasher::hash (const external_ref *r)
8967 {
8968   dw_die_ref die = r->type;
8969   hashval_t h = 0;
8970 
8971   /* We can't use the address of the DIE for hashing, because
8972      that will make the order of the stub DIEs non-deterministic.  */
8973   if (! die->comdat_type_p)
8974     /* We have a symbol; use it to compute a hash.  */
8975     h = htab_hash_string (die->die_id.die_symbol);
8976   else
8977     {
8978       /* We have a type signature; use a subset of the bits as the hash.
8979 	 The 8-byte signature is at least as large as hashval_t.  */
8980       comdat_type_node *type_node = die->die_id.die_type_node;
8981       memcpy (&h, type_node->signature, sizeof (h));
8982     }
8983   return h;
8984 }
8985 
8986 inline bool
equal(const external_ref * r1,const external_ref * r2)8987 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
8988 {
8989   return r1->type == r2->type;
8990 }
8991 
8992 typedef hash_table<external_ref_hasher> external_ref_hash_type;
8993 
8994 /* Return a pointer to the external_ref for references to DIE.  */
8995 
8996 static struct external_ref *
lookup_external_ref(external_ref_hash_type * map,dw_die_ref die)8997 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
8998 {
8999   struct external_ref ref, *ref_p;
9000   external_ref **slot;
9001 
9002   ref.type = die;
9003   slot = map->find_slot (&ref, INSERT);
9004   if (*slot != HTAB_EMPTY_ENTRY)
9005     return *slot;
9006 
9007   ref_p = XCNEW (struct external_ref);
9008   ref_p->type = die;
9009   *slot = ref_p;
9010   return ref_p;
9011 }
9012 
9013 /* Subroutine of optimize_external_refs, below.
9014 
9015    If we see a type skeleton, record it as our stub.  If we see external
9016    references, remember how many we've seen.  */
9017 
9018 static void
optimize_external_refs_1(dw_die_ref die,external_ref_hash_type * map)9019 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
9020 {
9021   dw_die_ref c;
9022   dw_attr_node *a;
9023   unsigned ix;
9024   struct external_ref *ref_p;
9025 
9026   if (is_type_die (die)
9027       && (c = get_AT_ref (die, DW_AT_signature)))
9028     {
9029       /* This is a local skeleton; use it for local references.  */
9030       ref_p = lookup_external_ref (map, c);
9031       ref_p->stub = die;
9032     }
9033 
9034   /* Scan the DIE references, and remember any that refer to DIEs from
9035      other CUs (i.e. those which are not marked).  */
9036   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9037     if (AT_class (a) == dw_val_class_die_ref
9038 	&& (c = AT_ref (a))->die_mark == 0
9039 	&& is_type_die (c))
9040       {
9041 	ref_p = lookup_external_ref (map, c);
9042 	ref_p->n_refs++;
9043       }
9044 
9045   FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
9046 }
9047 
9048 /* htab_traverse callback function for optimize_external_refs, below.  SLOT
9049    points to an external_ref, DATA is the CU we're processing.  If we don't
9050    already have a local stub, and we have multiple refs, build a stub.  */
9051 
9052 int
dwarf2_build_local_stub(external_ref ** slot,dw_die_ref data)9053 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
9054 {
9055   struct external_ref *ref_p = *slot;
9056 
9057   if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
9058     {
9059       /* We have multiple references to this type, so build a small stub.
9060 	 Both of these forms are a bit dodgy from the perspective of the
9061 	 DWARF standard, since technically they should have names.  */
9062       dw_die_ref cu = data;
9063       dw_die_ref type = ref_p->type;
9064       dw_die_ref stub = NULL;
9065 
9066       if (type->comdat_type_p)
9067 	{
9068 	  /* If we refer to this type via sig8, use AT_signature.  */
9069 	  stub = new_die (type->die_tag, cu, NULL_TREE);
9070 	  add_AT_die_ref (stub, DW_AT_signature, type);
9071 	}
9072       else
9073 	{
9074 	  /* Otherwise, use a typedef with no name.  */
9075 	  stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
9076 	  add_AT_die_ref (stub, DW_AT_type, type);
9077 	}
9078 
9079       stub->die_mark++;
9080       ref_p->stub = stub;
9081     }
9082   return 1;
9083 }
9084 
9085 /* DIE is a unit; look through all the DIE references to see if there are
9086    any external references to types, and if so, create local stubs for
9087    them which will be applied in build_abbrev_table.  This is useful because
9088    references to local DIEs are smaller.  */
9089 
9090 static external_ref_hash_type *
optimize_external_refs(dw_die_ref die)9091 optimize_external_refs (dw_die_ref die)
9092 {
9093   external_ref_hash_type *map = new external_ref_hash_type (10);
9094   optimize_external_refs_1 (die, map);
9095   map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
9096   return map;
9097 }
9098 
9099 /* The following 3 variables are temporaries that are computed only during the
9100    build_abbrev_table call and used and released during the following
9101    optimize_abbrev_table call.  */
9102 
9103 /* First abbrev_id that can be optimized based on usage.  */
9104 static unsigned int abbrev_opt_start;
9105 
9106 /* Maximum abbrev_id of a base type plus one (we can't optimize DIEs with
9107    abbrev_id smaller than this, because they must be already sized
9108    during build_abbrev_table).  */
9109 static unsigned int abbrev_opt_base_type_end;
9110 
9111 /* Vector of usage counts during build_abbrev_table.  Indexed by
9112    abbrev_id - abbrev_opt_start.  */
9113 static vec<unsigned int> abbrev_usage_count;
9114 
9115 /* Vector of all DIEs added with die_abbrev >= abbrev_opt_start.  */
9116 static vec<dw_die_ref> sorted_abbrev_dies;
9117 
9118 /* The format of each DIE (and its attribute value pairs) is encoded in an
9119    abbreviation table.  This routine builds the abbreviation table and assigns
9120    a unique abbreviation id for each abbreviation entry.  The children of each
9121    die are visited recursively.  */
9122 
9123 static void
build_abbrev_table(dw_die_ref die,external_ref_hash_type * extern_map)9124 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
9125 {
9126   unsigned int abbrev_id = 0;
9127   dw_die_ref c;
9128   dw_attr_node *a;
9129   unsigned ix;
9130   dw_die_ref abbrev;
9131 
9132   /* Scan the DIE references, and replace any that refer to
9133      DIEs from other CUs (i.e. those which are not marked) with
9134      the local stubs we built in optimize_external_refs.  */
9135   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9136     if (AT_class (a) == dw_val_class_die_ref
9137 	&& (c = AT_ref (a))->die_mark == 0)
9138       {
9139 	struct external_ref *ref_p;
9140 	gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
9141 
9142 	if (is_type_die (c)
9143 	    && (ref_p = lookup_external_ref (extern_map, c))
9144 	    && ref_p->stub && ref_p->stub != die)
9145 	  {
9146 	    gcc_assert (a->dw_attr != DW_AT_signature);
9147 	    change_AT_die_ref (a, ref_p->stub);
9148 	  }
9149 	else
9150 	  /* We aren't changing this reference, so mark it external.  */
9151 	  set_AT_ref_external (a, 1);
9152       }
9153 
9154   FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
9155     {
9156       dw_attr_node *die_a, *abbrev_a;
9157       unsigned ix;
9158       bool ok = true;
9159 
9160       if (abbrev_id == 0)
9161 	continue;
9162       if (abbrev->die_tag != die->die_tag)
9163 	continue;
9164       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
9165 	continue;
9166 
9167       if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
9168 	continue;
9169 
9170       FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
9171 	{
9172 	  abbrev_a = &(*abbrev->die_attr)[ix];
9173 	  if ((abbrev_a->dw_attr != die_a->dw_attr)
9174 	      || (value_format (abbrev_a) != value_format (die_a)))
9175 	    {
9176 	      ok = false;
9177 	      break;
9178 	    }
9179 	}
9180       if (ok)
9181 	break;
9182     }
9183 
9184   if (abbrev_id >= vec_safe_length (abbrev_die_table))
9185     {
9186       vec_safe_push (abbrev_die_table, die);
9187       if (abbrev_opt_start)
9188 	abbrev_usage_count.safe_push (0);
9189     }
9190   if (abbrev_opt_start && abbrev_id >= abbrev_opt_start)
9191     {
9192       abbrev_usage_count[abbrev_id - abbrev_opt_start]++;
9193       sorted_abbrev_dies.safe_push (die);
9194     }
9195 
9196   die->die_abbrev = abbrev_id;
9197   FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
9198 }
9199 
9200 /* Callback function for sorted_abbrev_dies vector sorting.  We sort
9201    by die_abbrev's usage count, from the most commonly used
9202    abbreviation to the least.  */
9203 
9204 static int
die_abbrev_cmp(const void * p1,const void * p2)9205 die_abbrev_cmp (const void *p1, const void *p2)
9206 {
9207   dw_die_ref die1 = *(const dw_die_ref *) p1;
9208   dw_die_ref die2 = *(const dw_die_ref *) p2;
9209 
9210   gcc_checking_assert (die1->die_abbrev >= abbrev_opt_start);
9211   gcc_checking_assert (die2->die_abbrev >= abbrev_opt_start);
9212 
9213   if (die1->die_abbrev >= abbrev_opt_base_type_end
9214       && die2->die_abbrev >= abbrev_opt_base_type_end)
9215     {
9216       if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
9217 	  > abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
9218 	return -1;
9219       if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
9220 	  < abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
9221 	return 1;
9222     }
9223 
9224   /* Stabilize the sort.  */
9225   if (die1->die_abbrev < die2->die_abbrev)
9226     return -1;
9227   if (die1->die_abbrev > die2->die_abbrev)
9228     return 1;
9229 
9230   return 0;
9231 }
9232 
9233 /* Convert dw_val_class_const and dw_val_class_unsigned_const class attributes
9234    of DIEs in between sorted_abbrev_dies[first_id] and abbrev_dies[end_id - 1]
9235    into dw_val_class_const_implicit or
9236    dw_val_class_unsigned_const_implicit.  */
9237 
9238 static void
optimize_implicit_const(unsigned int first_id,unsigned int end,vec<bool> & implicit_consts)9239 optimize_implicit_const (unsigned int first_id, unsigned int end,
9240 			 vec<bool> &implicit_consts)
9241 {
9242   /* It never makes sense if there is just one DIE using the abbreviation.  */
9243   if (end < first_id + 2)
9244     return;
9245 
9246   dw_attr_node *a;
9247   unsigned ix, i;
9248   dw_die_ref die = sorted_abbrev_dies[first_id];
9249   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9250     if (implicit_consts[ix])
9251       {
9252 	enum dw_val_class new_class = dw_val_class_none;
9253 	switch (AT_class (a))
9254 	  {
9255 	  case dw_val_class_unsigned_const:
9256 	    if ((HOST_WIDE_INT) AT_unsigned (a) < 0)
9257 	      continue;
9258 
9259 	    /* The .debug_abbrev section will grow by
9260 	       size_of_sleb128 (AT_unsigned (a)) and we avoid the constants
9261 	       in all the DIEs using that abbreviation.  */
9262 	    if (constant_size (AT_unsigned (a)) * (end - first_id)
9263 		<= (unsigned) size_of_sleb128 (AT_unsigned (a)))
9264 	      continue;
9265 
9266 	    new_class = dw_val_class_unsigned_const_implicit;
9267 	    break;
9268 
9269 	  case dw_val_class_const:
9270 	    new_class = dw_val_class_const_implicit;
9271 	    break;
9272 
9273 	  case dw_val_class_file:
9274 	    new_class = dw_val_class_file_implicit;
9275 	    break;
9276 
9277 	  default:
9278 	    continue;
9279 	  }
9280 	for (i = first_id; i < end; i++)
9281 	  (*sorted_abbrev_dies[i]->die_attr)[ix].dw_attr_val.val_class
9282 	    = new_class;
9283       }
9284 }
9285 
9286 /* Attempt to optimize abbreviation table from abbrev_opt_start
9287    abbreviation above.  */
9288 
9289 static void
optimize_abbrev_table(void)9290 optimize_abbrev_table (void)
9291 {
9292   if (abbrev_opt_start
9293       && vec_safe_length (abbrev_die_table) > abbrev_opt_start
9294       && (dwarf_version >= 5 || vec_safe_length (abbrev_die_table) > 127))
9295     {
9296       auto_vec<bool, 32> implicit_consts;
9297       sorted_abbrev_dies.qsort (die_abbrev_cmp);
9298 
9299       unsigned int abbrev_id = abbrev_opt_start - 1;
9300       unsigned int first_id = ~0U;
9301       unsigned int last_abbrev_id = 0;
9302       unsigned int i;
9303       dw_die_ref die;
9304       if (abbrev_opt_base_type_end > abbrev_opt_start)
9305 	abbrev_id = abbrev_opt_base_type_end - 1;
9306       /* Reassign abbreviation ids from abbrev_opt_start above, so that
9307 	 most commonly used abbreviations come first.  */
9308       FOR_EACH_VEC_ELT (sorted_abbrev_dies, i, die)
9309 	{
9310 	  dw_attr_node *a;
9311 	  unsigned ix;
9312 
9313 	  /* If calc_base_type_die_sizes has been called, the CU and
9314 	     base types after it can't be optimized, because we've already
9315 	     calculated their DIE offsets.  We've sorted them first.  */
9316 	  if (die->die_abbrev < abbrev_opt_base_type_end)
9317 	    continue;
9318 	  if (die->die_abbrev != last_abbrev_id)
9319 	    {
9320 	      last_abbrev_id = die->die_abbrev;
9321 	      if (dwarf_version >= 5 && first_id != ~0U)
9322 		optimize_implicit_const (first_id, i, implicit_consts);
9323 	      abbrev_id++;
9324 	      (*abbrev_die_table)[abbrev_id] = die;
9325 	      if (dwarf_version >= 5)
9326 		{
9327 		  first_id = i;
9328 		  implicit_consts.truncate (0);
9329 
9330 		  FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9331 		    switch (AT_class (a))
9332 		      {
9333 		      case dw_val_class_const:
9334 		      case dw_val_class_unsigned_const:
9335 		      case dw_val_class_file:
9336 			implicit_consts.safe_push (true);
9337 			break;
9338 		      default:
9339 			implicit_consts.safe_push (false);
9340 			break;
9341 		      }
9342 		}
9343 	    }
9344 	  else if (dwarf_version >= 5)
9345 	    {
9346 	      FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9347 		if (!implicit_consts[ix])
9348 		  continue;
9349 		else
9350 		  {
9351 		    dw_attr_node *other_a
9352 		      = &(*(*abbrev_die_table)[abbrev_id]->die_attr)[ix];
9353 		    if (!dw_val_equal_p (&a->dw_attr_val,
9354 					 &other_a->dw_attr_val))
9355 		      implicit_consts[ix] = false;
9356 		  }
9357 	    }
9358 	  die->die_abbrev = abbrev_id;
9359 	}
9360       gcc_assert (abbrev_id == vec_safe_length (abbrev_die_table) - 1);
9361       if (dwarf_version >= 5 && first_id != ~0U)
9362 	optimize_implicit_const (first_id, i, implicit_consts);
9363     }
9364 
9365   abbrev_opt_start = 0;
9366   abbrev_opt_base_type_end = 0;
9367   abbrev_usage_count.release ();
9368   sorted_abbrev_dies.release ();
9369 }
9370 
9371 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
9372 
9373 static int
constant_size(unsigned HOST_WIDE_INT value)9374 constant_size (unsigned HOST_WIDE_INT value)
9375 {
9376   int log;
9377 
9378   if (value == 0)
9379     log = 0;
9380   else
9381     log = floor_log2 (value);
9382 
9383   log = log / 8;
9384   log = 1 << (floor_log2 (log) + 1);
9385 
9386   return log;
9387 }
9388 
9389 /* Return the size of a DIE as it is represented in the
9390    .debug_info section.  */
9391 
9392 static unsigned long
size_of_die(dw_die_ref die)9393 size_of_die (dw_die_ref die)
9394 {
9395   unsigned long size = 0;
9396   dw_attr_node *a;
9397   unsigned ix;
9398   enum dwarf_form form;
9399 
9400   size += size_of_uleb128 (die->die_abbrev);
9401   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9402     {
9403       switch (AT_class (a))
9404 	{
9405 	case dw_val_class_addr:
9406           if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9407             {
9408               gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9409               size += size_of_uleb128 (AT_index (a));
9410             }
9411           else
9412             size += DWARF2_ADDR_SIZE;
9413 	  break;
9414 	case dw_val_class_offset:
9415 	  size += dwarf_offset_size;
9416 	  break;
9417 	case dw_val_class_loc:
9418 	  {
9419 	    unsigned long lsize = size_of_locs (AT_loc (a));
9420 
9421 	    /* Block length.  */
9422 	    if (dwarf_version >= 4)
9423 	      size += size_of_uleb128 (lsize);
9424 	    else
9425 	      size += constant_size (lsize);
9426 	    size += lsize;
9427 	  }
9428 	  break;
9429 	case dw_val_class_loc_list:
9430 	  if (dwarf_split_debug_info && dwarf_version >= 5)
9431 	    {
9432 	      gcc_assert (AT_loc_list (a)->num_assigned);
9433 	      size += size_of_uleb128 (AT_loc_list (a)->hash);
9434 	    }
9435           else
9436             size += dwarf_offset_size;
9437 	  break;
9438 	case dw_val_class_view_list:
9439 	  size += dwarf_offset_size;
9440 	  break;
9441 	case dw_val_class_range_list:
9442 	  if (value_format (a) == DW_FORM_rnglistx)
9443 	    {
9444 	      gcc_assert (rnglist_idx);
9445 	      dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9446 	      size += size_of_uleb128 (r->idx);
9447 	    }
9448 	  else
9449 	    size += dwarf_offset_size;
9450 	  break;
9451 	case dw_val_class_const:
9452 	  size += size_of_sleb128 (AT_int (a));
9453 	  break;
9454 	case dw_val_class_unsigned_const:
9455 	  {
9456 	    int csize = constant_size (AT_unsigned (a));
9457 	    if (dwarf_version == 3
9458 		&& a->dw_attr == DW_AT_data_member_location
9459 		&& csize >= 4)
9460 	      size += size_of_uleb128 (AT_unsigned (a));
9461 	    else
9462 	      size += csize;
9463 	  }
9464 	  break;
9465 	case dw_val_class_symview:
9466 	  if (symview_upper_bound <= 0xff)
9467 	    size += 1;
9468 	  else if (symview_upper_bound <= 0xffff)
9469 	    size += 2;
9470 	  else if (symview_upper_bound <= 0xffffffff)
9471 	    size += 4;
9472 	  else
9473 	    size += 8;
9474 	  break;
9475 	case dw_val_class_const_implicit:
9476 	case dw_val_class_unsigned_const_implicit:
9477 	case dw_val_class_file_implicit:
9478 	  /* These occupy no size in the DIE, just an extra sleb128 in
9479 	     .debug_abbrev.  */
9480 	  break;
9481 	case dw_val_class_const_double:
9482 	  size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
9483 	  if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
9484 	    size++; /* block */
9485 	  break;
9486 	case dw_val_class_wide_int:
9487 	  size += (get_full_len (*a->dw_attr_val.v.val_wide)
9488 		   * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
9489 	  if (get_full_len (*a->dw_attr_val.v.val_wide)
9490 	      * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
9491 	    size++; /* block */
9492 	  break;
9493 	case dw_val_class_vec:
9494 	  size += constant_size (a->dw_attr_val.v.val_vec.length
9495 				 * a->dw_attr_val.v.val_vec.elt_size)
9496 		  + a->dw_attr_val.v.val_vec.length
9497 		    * a->dw_attr_val.v.val_vec.elt_size; /* block */
9498 	  break;
9499 	case dw_val_class_flag:
9500 	  if (dwarf_version >= 4)
9501 	    /* Currently all add_AT_flag calls pass in 1 as last argument,
9502 	       so DW_FORM_flag_present can be used.  If that ever changes,
9503 	       we'll need to use DW_FORM_flag and have some optimization
9504 	       in build_abbrev_table that will change those to
9505 	       DW_FORM_flag_present if it is set to 1 in all DIEs using
9506 	       the same abbrev entry.  */
9507 	    gcc_assert (a->dw_attr_val.v.val_flag == 1);
9508 	  else
9509 	    size += 1;
9510 	  break;
9511 	case dw_val_class_die_ref:
9512 	  if (AT_ref_external (a))
9513 	    {
9514 	      /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
9515 		 we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
9516 		 is sized by target address length, whereas in DWARF3
9517 		 it's always sized as an offset.  */
9518 	      if (AT_ref (a)->comdat_type_p)
9519 		size += DWARF_TYPE_SIGNATURE_SIZE;
9520 	      else if (dwarf_version == 2)
9521 		size += DWARF2_ADDR_SIZE;
9522 	      else
9523 		size += dwarf_offset_size;
9524 	    }
9525 	  else
9526 	    size += dwarf_offset_size;
9527 	  break;
9528 	case dw_val_class_fde_ref:
9529 	  size += dwarf_offset_size;
9530 	  break;
9531 	case dw_val_class_lbl_id:
9532           if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9533             {
9534               gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9535               size += size_of_uleb128 (AT_index (a));
9536             }
9537           else
9538             size += DWARF2_ADDR_SIZE;
9539 	  break;
9540 	case dw_val_class_lineptr:
9541 	case dw_val_class_macptr:
9542 	case dw_val_class_loclistsptr:
9543 	  size += dwarf_offset_size;
9544 	  break;
9545 	case dw_val_class_str:
9546           form = AT_string_form (a);
9547 	  if (form == DW_FORM_strp || form == DW_FORM_line_strp)
9548 	    size += dwarf_offset_size;
9549 	  else if (form == dwarf_FORM (DW_FORM_strx))
9550 	    size += size_of_uleb128 (AT_index (a));
9551 	  else
9552 	    size += strlen (a->dw_attr_val.v.val_str->str) + 1;
9553 	  break;
9554 	case dw_val_class_file:
9555 	  size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
9556 	  break;
9557 	case dw_val_class_data8:
9558 	  size += 8;
9559 	  break;
9560 	case dw_val_class_vms_delta:
9561 	  size += dwarf_offset_size;
9562 	  break;
9563 	case dw_val_class_high_pc:
9564 	  size += DWARF2_ADDR_SIZE;
9565 	  break;
9566 	case dw_val_class_discr_value:
9567 	  size += size_of_discr_value (&a->dw_attr_val.v.val_discr_value);
9568 	  break;
9569 	case dw_val_class_discr_list:
9570 	    {
9571 	      unsigned block_size = size_of_discr_list (AT_discr_list (a));
9572 
9573 	      /* This is a block, so we have the block length and then its
9574 		 data.  */
9575 	      size += constant_size (block_size) + block_size;
9576 	    }
9577 	  break;
9578 	default:
9579 	  gcc_unreachable ();
9580 	}
9581     }
9582 
9583   return size;
9584 }
9585 
9586 /* Size the debugging information associated with a given DIE.  Visits the
9587    DIE's children recursively.  Updates the global variable next_die_offset, on
9588    each time through.  Uses the current value of next_die_offset to update the
9589    die_offset field in each DIE.  */
9590 
9591 static void
calc_die_sizes(dw_die_ref die)9592 calc_die_sizes (dw_die_ref die)
9593 {
9594   dw_die_ref c;
9595 
9596   gcc_assert (die->die_offset == 0
9597 	      || (unsigned long int) die->die_offset == next_die_offset);
9598   die->die_offset = next_die_offset;
9599   next_die_offset += size_of_die (die);
9600 
9601   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
9602 
9603   if (die->die_child != NULL)
9604     /* Count the null byte used to terminate sibling lists.  */
9605     next_die_offset += 1;
9606 }
9607 
9608 /* Size just the base type children at the start of the CU.
9609    This is needed because build_abbrev needs to size locs
9610    and sizing of type based stack ops needs to know die_offset
9611    values for the base types.  */
9612 
9613 static void
calc_base_type_die_sizes(void)9614 calc_base_type_die_sizes (void)
9615 {
9616   unsigned long die_offset = (dwarf_split_debug_info
9617 			      ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
9618 			      : DWARF_COMPILE_UNIT_HEADER_SIZE);
9619   unsigned int i;
9620   dw_die_ref base_type;
9621 #if ENABLE_ASSERT_CHECKING
9622   dw_die_ref prev = comp_unit_die ()->die_child;
9623 #endif
9624 
9625   die_offset += size_of_die (comp_unit_die ());
9626   for (i = 0; base_types.iterate (i, &base_type); i++)
9627     {
9628 #if ENABLE_ASSERT_CHECKING
9629       gcc_assert (base_type->die_offset == 0
9630 		  && prev->die_sib == base_type
9631 		  && base_type->die_child == NULL
9632 		  && base_type->die_abbrev);
9633       prev = base_type;
9634 #endif
9635       if (abbrev_opt_start
9636 	  && base_type->die_abbrev >= abbrev_opt_base_type_end)
9637 	abbrev_opt_base_type_end = base_type->die_abbrev + 1;
9638       base_type->die_offset = die_offset;
9639       die_offset += size_of_die (base_type);
9640     }
9641 }
9642 
9643 /* Set the marks for a die and its children.  We do this so
9644    that we know whether or not a reference needs to use FORM_ref_addr; only
9645    DIEs in the same CU will be marked.  We used to clear out the offset
9646    and use that as the flag, but ran into ordering problems.  */
9647 
9648 static void
mark_dies(dw_die_ref die)9649 mark_dies (dw_die_ref die)
9650 {
9651   dw_die_ref c;
9652 
9653   gcc_assert (!die->die_mark);
9654 
9655   die->die_mark = 1;
9656   FOR_EACH_CHILD (die, c, mark_dies (c));
9657 }
9658 
9659 /* Clear the marks for a die and its children.  */
9660 
9661 static void
unmark_dies(dw_die_ref die)9662 unmark_dies (dw_die_ref die)
9663 {
9664   dw_die_ref c;
9665 
9666   if (! use_debug_types)
9667     gcc_assert (die->die_mark);
9668 
9669   die->die_mark = 0;
9670   FOR_EACH_CHILD (die, c, unmark_dies (c));
9671 }
9672 
9673 /* Clear the marks for a die, its children and referred dies.  */
9674 
9675 static void
unmark_all_dies(dw_die_ref die)9676 unmark_all_dies (dw_die_ref die)
9677 {
9678   dw_die_ref c;
9679   dw_attr_node *a;
9680   unsigned ix;
9681 
9682   if (!die->die_mark)
9683     return;
9684   die->die_mark = 0;
9685 
9686   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
9687 
9688   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9689     if (AT_class (a) == dw_val_class_die_ref)
9690       unmark_all_dies (AT_ref (a));
9691 }
9692 
9693 /* Calculate if the entry should appear in the final output file.  It may be
9694    from a pruned a type.  */
9695 
9696 static bool
include_pubname_in_output(vec<pubname_entry,va_gc> * table,pubname_entry * p)9697 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
9698 {
9699   /* By limiting gnu pubnames to definitions only, gold can generate a
9700      gdb index without entries for declarations, which don't include
9701      enough information to be useful.  */
9702   if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
9703     return false;
9704 
9705   if (table == pubname_table)
9706     {
9707       /* Enumerator names are part of the pubname table, but the
9708          parent DW_TAG_enumeration_type die may have been pruned.
9709          Don't output them if that is the case.  */
9710       if (p->die->die_tag == DW_TAG_enumerator &&
9711           (p->die->die_parent == NULL
9712            || !p->die->die_parent->die_perennial_p))
9713         return false;
9714 
9715       /* Everything else in the pubname table is included.  */
9716       return true;
9717     }
9718 
9719   /* The pubtypes table shouldn't include types that have been
9720      pruned.  */
9721   return (p->die->die_offset != 0
9722           || !flag_eliminate_unused_debug_types);
9723 }
9724 
9725 /* Return the size of the .debug_pubnames or .debug_pubtypes table
9726    generated for the compilation unit.  */
9727 
9728 static unsigned long
size_of_pubnames(vec<pubname_entry,va_gc> * names)9729 size_of_pubnames (vec<pubname_entry, va_gc> *names)
9730 {
9731   unsigned long size;
9732   unsigned i;
9733   pubname_entry *p;
9734   int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
9735 
9736   size = DWARF_PUBNAMES_HEADER_SIZE;
9737   FOR_EACH_VEC_ELT (*names, i, p)
9738     if (include_pubname_in_output (names, p))
9739       size += strlen (p->name) + dwarf_offset_size + 1 + space_for_flags;
9740 
9741   size += dwarf_offset_size;
9742   return size;
9743 }
9744 
9745 /* Return the size of the information in the .debug_aranges section.  */
9746 
9747 static unsigned long
size_of_aranges(void)9748 size_of_aranges (void)
9749 {
9750   unsigned long size;
9751 
9752   size = DWARF_ARANGES_HEADER_SIZE;
9753 
9754   /* Count the address/length pair for this compilation unit.  */
9755   if (text_section_used)
9756     size += 2 * DWARF2_ADDR_SIZE;
9757   if (cold_text_section_used)
9758     size += 2 * DWARF2_ADDR_SIZE;
9759   if (have_multiple_function_sections)
9760     {
9761       unsigned fde_idx;
9762       dw_fde_ref fde;
9763 
9764       FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9765 	{
9766 	  if (DECL_IGNORED_P (fde->decl))
9767 	    continue;
9768 	  if (!fde->in_std_section)
9769 	    size += 2 * DWARF2_ADDR_SIZE;
9770 	  if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9771 	    size += 2 * DWARF2_ADDR_SIZE;
9772 	}
9773     }
9774 
9775   /* Count the two zero words used to terminated the address range table.  */
9776   size += 2 * DWARF2_ADDR_SIZE;
9777   return size;
9778 }
9779 
9780 /* Select the encoding of an attribute value.  */
9781 
9782 static enum dwarf_form
value_format(dw_attr_node * a)9783 value_format (dw_attr_node *a)
9784 {
9785   switch (AT_class (a))
9786     {
9787     case dw_val_class_addr:
9788       /* Only very few attributes allow DW_FORM_addr.  */
9789       switch (a->dw_attr)
9790 	{
9791 	case DW_AT_low_pc:
9792 	case DW_AT_high_pc:
9793 	case DW_AT_entry_pc:
9794 	case DW_AT_trampoline:
9795           return (AT_index (a) == NOT_INDEXED
9796                   ? DW_FORM_addr : dwarf_FORM (DW_FORM_addrx));
9797 	default:
9798 	  break;
9799 	}
9800       switch (DWARF2_ADDR_SIZE)
9801 	{
9802 	case 1:
9803 	  return DW_FORM_data1;
9804 	case 2:
9805 	  return DW_FORM_data2;
9806 	case 4:
9807 	  return DW_FORM_data4;
9808 	case 8:
9809 	  return DW_FORM_data8;
9810 	default:
9811 	  gcc_unreachable ();
9812 	}
9813     case dw_val_class_loc_list:
9814       if (dwarf_split_debug_info
9815 	  && dwarf_version >= 5
9816 	  && AT_loc_list (a)->num_assigned)
9817 	return DW_FORM_loclistx;
9818       /* FALLTHRU */
9819     case dw_val_class_view_list:
9820     case dw_val_class_range_list:
9821       /* For range lists in DWARF 5, use DW_FORM_rnglistx from .debug_info.dwo
9822 	 but in .debug_info use DW_FORM_sec_offset, which is shorter if we
9823 	 care about sizes of .debug* sections in shared libraries and
9824 	 executables and don't take into account relocations that affect just
9825 	 relocatable objects - for DW_FORM_rnglistx we'd have to emit offset
9826 	 table in the .debug_rnglists section.  */
9827       if (dwarf_split_debug_info
9828 	  && dwarf_version >= 5
9829 	  && AT_class (a) == dw_val_class_range_list
9830 	  && rnglist_idx
9831 	  && a->dw_attr_val.val_entry != RELOCATED_OFFSET)
9832 	return DW_FORM_rnglistx;
9833       if (dwarf_version >= 4)
9834 	return DW_FORM_sec_offset;
9835       /* FALLTHRU */
9836     case dw_val_class_vms_delta:
9837     case dw_val_class_offset:
9838       switch (dwarf_offset_size)
9839 	{
9840 	case 4:
9841 	  return DW_FORM_data4;
9842 	case 8:
9843 	  return DW_FORM_data8;
9844 	default:
9845 	  gcc_unreachable ();
9846 	}
9847     case dw_val_class_loc:
9848       if (dwarf_version >= 4)
9849 	return DW_FORM_exprloc;
9850       switch (constant_size (size_of_locs (AT_loc (a))))
9851 	{
9852 	case 1:
9853 	  return DW_FORM_block1;
9854 	case 2:
9855 	  return DW_FORM_block2;
9856 	case 4:
9857 	  return DW_FORM_block4;
9858 	default:
9859 	  gcc_unreachable ();
9860 	}
9861     case dw_val_class_const:
9862       return DW_FORM_sdata;
9863     case dw_val_class_unsigned_const:
9864       switch (constant_size (AT_unsigned (a)))
9865 	{
9866 	case 1:
9867 	  return DW_FORM_data1;
9868 	case 2:
9869 	  return DW_FORM_data2;
9870 	case 4:
9871 	  /* In DWARF3 DW_AT_data_member_location with
9872 	     DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
9873 	     constant, so we need to use DW_FORM_udata if we need
9874 	     a large constant.  */
9875 	  if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9876 	    return DW_FORM_udata;
9877 	  return DW_FORM_data4;
9878 	case 8:
9879 	  if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9880 	    return DW_FORM_udata;
9881 	  return DW_FORM_data8;
9882 	default:
9883 	  gcc_unreachable ();
9884 	}
9885     case dw_val_class_const_implicit:
9886     case dw_val_class_unsigned_const_implicit:
9887     case dw_val_class_file_implicit:
9888       return DW_FORM_implicit_const;
9889     case dw_val_class_const_double:
9890       switch (HOST_BITS_PER_WIDE_INT)
9891 	{
9892 	case 8:
9893 	  return DW_FORM_data2;
9894 	case 16:
9895 	  return DW_FORM_data4;
9896 	case 32:
9897 	  return DW_FORM_data8;
9898 	case 64:
9899 	  if (dwarf_version >= 5)
9900 	    return DW_FORM_data16;
9901 	  /* FALLTHRU */
9902 	default:
9903 	  return DW_FORM_block1;
9904 	}
9905     case dw_val_class_wide_int:
9906       switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
9907 	{
9908 	case 8:
9909 	  return DW_FORM_data1;
9910 	case 16:
9911 	  return DW_FORM_data2;
9912 	case 32:
9913 	  return DW_FORM_data4;
9914 	case 64:
9915 	  return DW_FORM_data8;
9916 	case 128:
9917 	  if (dwarf_version >= 5)
9918 	    return DW_FORM_data16;
9919 	  /* FALLTHRU */
9920 	default:
9921 	  return DW_FORM_block1;
9922 	}
9923     case dw_val_class_symview:
9924       /* ??? We might use uleb128, but then we'd have to compute
9925 	 .debug_info offsets in the assembler.  */
9926       if (symview_upper_bound <= 0xff)
9927 	return DW_FORM_data1;
9928       else if (symview_upper_bound <= 0xffff)
9929 	return DW_FORM_data2;
9930       else if (symview_upper_bound <= 0xffffffff)
9931 	return DW_FORM_data4;
9932       else
9933 	return DW_FORM_data8;
9934     case dw_val_class_vec:
9935       switch (constant_size (a->dw_attr_val.v.val_vec.length
9936 			     * a->dw_attr_val.v.val_vec.elt_size))
9937 	{
9938 	case 1:
9939 	  return DW_FORM_block1;
9940 	case 2:
9941 	  return DW_FORM_block2;
9942 	case 4:
9943 	  return DW_FORM_block4;
9944 	default:
9945 	  gcc_unreachable ();
9946 	}
9947     case dw_val_class_flag:
9948       if (dwarf_version >= 4)
9949 	{
9950 	  /* Currently all add_AT_flag calls pass in 1 as last argument,
9951 	     so DW_FORM_flag_present can be used.  If that ever changes,
9952 	     we'll need to use DW_FORM_flag and have some optimization
9953 	     in build_abbrev_table that will change those to
9954 	     DW_FORM_flag_present if it is set to 1 in all DIEs using
9955 	     the same abbrev entry.  */
9956 	  gcc_assert (a->dw_attr_val.v.val_flag == 1);
9957 	  return DW_FORM_flag_present;
9958 	}
9959       return DW_FORM_flag;
9960     case dw_val_class_die_ref:
9961       if (AT_ref_external (a))
9962 	{
9963 	  if (AT_ref (a)->comdat_type_p)
9964 	    return DW_FORM_ref_sig8;
9965 	  else
9966 	    return DW_FORM_ref_addr;
9967 	}
9968       else
9969 	return DW_FORM_ref;
9970     case dw_val_class_fde_ref:
9971       return DW_FORM_data;
9972     case dw_val_class_lbl_id:
9973       return (AT_index (a) == NOT_INDEXED
9974               ? DW_FORM_addr : dwarf_FORM (DW_FORM_addrx));
9975     case dw_val_class_lineptr:
9976     case dw_val_class_macptr:
9977     case dw_val_class_loclistsptr:
9978       return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
9979     case dw_val_class_str:
9980       return AT_string_form (a);
9981     case dw_val_class_file:
9982       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
9983 	{
9984 	case 1:
9985 	  return DW_FORM_data1;
9986 	case 2:
9987 	  return DW_FORM_data2;
9988 	case 4:
9989 	  return DW_FORM_data4;
9990 	default:
9991 	  gcc_unreachable ();
9992 	}
9993 
9994     case dw_val_class_data8:
9995       return DW_FORM_data8;
9996 
9997     case dw_val_class_high_pc:
9998       switch (DWARF2_ADDR_SIZE)
9999 	{
10000 	case 1:
10001 	  return DW_FORM_data1;
10002 	case 2:
10003 	  return DW_FORM_data2;
10004 	case 4:
10005 	  return DW_FORM_data4;
10006 	case 8:
10007 	  return DW_FORM_data8;
10008 	default:
10009 	  gcc_unreachable ();
10010 	}
10011 
10012     case dw_val_class_discr_value:
10013       return (a->dw_attr_val.v.val_discr_value.pos
10014 	      ? DW_FORM_udata
10015 	      : DW_FORM_sdata);
10016     case dw_val_class_discr_list:
10017       switch (constant_size (size_of_discr_list (AT_discr_list (a))))
10018 	{
10019 	case 1:
10020 	  return DW_FORM_block1;
10021 	case 2:
10022 	  return DW_FORM_block2;
10023 	case 4:
10024 	  return DW_FORM_block4;
10025 	default:
10026 	  gcc_unreachable ();
10027 	}
10028 
10029     default:
10030       gcc_unreachable ();
10031     }
10032 }
10033 
10034 /* Output the encoding of an attribute value.  */
10035 
10036 static void
output_value_format(dw_attr_node * a)10037 output_value_format (dw_attr_node *a)
10038 {
10039   enum dwarf_form form = value_format (a);
10040 
10041   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
10042 }
10043 
10044 /* Given a die and id, produce the appropriate abbreviations.  */
10045 
10046 static void
output_die_abbrevs(unsigned long abbrev_id,dw_die_ref abbrev)10047 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
10048 {
10049   unsigned ix;
10050   dw_attr_node *a_attr;
10051 
10052   dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
10053   dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
10054                                dwarf_tag_name (abbrev->die_tag));
10055 
10056   if (abbrev->die_child != NULL)
10057     dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
10058   else
10059     dw2_asm_output_data (1, DW_children_no, "DW_children_no");
10060 
10061   for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
10062     {
10063       dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
10064                                    dwarf_attr_name (a_attr->dw_attr));
10065       output_value_format (a_attr);
10066       if (value_format (a_attr) == DW_FORM_implicit_const)
10067 	{
10068 	  if (AT_class (a_attr) == dw_val_class_file_implicit)
10069 	    {
10070 	      int f = maybe_emit_file (a_attr->dw_attr_val.v.val_file);
10071 	      const char *filename = a_attr->dw_attr_val.v.val_file->filename;
10072 	      dw2_asm_output_data_sleb128 (f, "(%s)", filename);
10073 	    }
10074 	  else
10075 	    dw2_asm_output_data_sleb128 (a_attr->dw_attr_val.v.val_int, NULL);
10076 	}
10077     }
10078 
10079   dw2_asm_output_data (1, 0, NULL);
10080   dw2_asm_output_data (1, 0, NULL);
10081 }
10082 
10083 
10084 /* Output the .debug_abbrev section which defines the DIE abbreviation
10085    table.  */
10086 
10087 static void
output_abbrev_section(void)10088 output_abbrev_section (void)
10089 {
10090   unsigned int abbrev_id;
10091   dw_die_ref abbrev;
10092 
10093   FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
10094     if (abbrev_id != 0)
10095       output_die_abbrevs (abbrev_id, abbrev);
10096 
10097   /* Terminate the table.  */
10098   dw2_asm_output_data (1, 0, NULL);
10099 }
10100 
10101 /* Return a new location list, given the begin and end range, and the
10102    expression.  */
10103 
10104 static inline dw_loc_list_ref
new_loc_list(dw_loc_descr_ref expr,const char * begin,var_loc_view vbegin,const char * end,var_loc_view vend,const char * section)10105 new_loc_list (dw_loc_descr_ref expr, const char *begin, var_loc_view vbegin,
10106 	      const char *end, var_loc_view vend,
10107 	      const char *section)
10108 {
10109   dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
10110 
10111   retlist->begin = begin;
10112   retlist->begin_entry = NULL;
10113   retlist->end = end;
10114   retlist->end_entry = NULL;
10115   retlist->expr = expr;
10116   retlist->section = section;
10117   retlist->vbegin = vbegin;
10118   retlist->vend = vend;
10119 
10120   return retlist;
10121 }
10122 
10123 /* Return true iff there's any nonzero view number in the loc list.
10124 
10125    ??? When views are not enabled, we'll often extend a single range
10126    to the entire function, so that we emit a single location
10127    expression rather than a location list.  With views, even with a
10128    single range, we'll output a list if start or end have a nonzero
10129    view.  If we change this, we may want to stop splitting a single
10130    range in dw_loc_list just because of a nonzero view, even if it
10131    straddles across hot/cold partitions.  */
10132 
10133 static bool
loc_list_has_views(dw_loc_list_ref list)10134 loc_list_has_views (dw_loc_list_ref list)
10135 {
10136   if (!debug_variable_location_views)
10137     return false;
10138 
10139   for (dw_loc_list_ref loc = list;
10140        loc != NULL; loc = loc->dw_loc_next)
10141     if (!ZERO_VIEW_P (loc->vbegin) || !ZERO_VIEW_P (loc->vend))
10142       return true;
10143 
10144   return false;
10145 }
10146 
10147 /* Generate a new internal symbol for this location list node, if it
10148    hasn't got one yet.  */
10149 
10150 static inline void
gen_llsym(dw_loc_list_ref list)10151 gen_llsym (dw_loc_list_ref list)
10152 {
10153   gcc_assert (!list->ll_symbol);
10154   list->ll_symbol = gen_internal_sym ("LLST");
10155 
10156   if (!loc_list_has_views (list))
10157     return;
10158 
10159   if (dwarf2out_locviews_in_attribute ())
10160     {
10161       /* Use the same label_num for the view list.  */
10162       label_num--;
10163       list->vl_symbol = gen_internal_sym ("LVUS");
10164     }
10165   else
10166     list->vl_symbol = list->ll_symbol;
10167 }
10168 
10169 /* Generate a symbol for the list, but only if we really want to emit
10170    it as a list.  */
10171 
10172 static inline void
maybe_gen_llsym(dw_loc_list_ref list)10173 maybe_gen_llsym (dw_loc_list_ref list)
10174 {
10175   if (!list || (!list->dw_loc_next && !loc_list_has_views (list)))
10176     return;
10177 
10178   gen_llsym (list);
10179 }
10180 
10181 /* Determine whether or not to skip loc_list entry CURR.  If SIZEP is
10182    NULL, don't consider size of the location expression.  If we're not
10183    to skip it, and SIZEP is non-null, store the size of CURR->expr's
10184    representation in *SIZEP.  */
10185 
10186 static bool
10187 skip_loc_list_entry (dw_loc_list_ref curr, unsigned long *sizep = NULL)
10188 {
10189   /* Don't output an entry that starts and ends at the same address.  */
10190   if (strcmp (curr->begin, curr->end) == 0
10191       && curr->vbegin == curr->vend && !curr->force)
10192     return true;
10193 
10194   if (!sizep)
10195     return false;
10196 
10197   unsigned long size = size_of_locs (curr->expr);
10198 
10199   /* If the expression is too large, drop it on the floor.  We could
10200      perhaps put it into DW_TAG_dwarf_procedure and refer to that
10201      in the expression, but >= 64KB expressions for a single value
10202      in a single range are unlikely very useful.  */
10203   if (dwarf_version < 5 && size > 0xffff)
10204     return true;
10205 
10206   *sizep = size;
10207 
10208   return false;
10209 }
10210 
10211 /* Output a view pair loclist entry for CURR, if it requires one.  */
10212 
10213 static void
dwarf2out_maybe_output_loclist_view_pair(dw_loc_list_ref curr)10214 dwarf2out_maybe_output_loclist_view_pair (dw_loc_list_ref curr)
10215 {
10216   if (!dwarf2out_locviews_in_loclist ())
10217     return;
10218 
10219   if (ZERO_VIEW_P (curr->vbegin) && ZERO_VIEW_P (curr->vend))
10220     return;
10221 
10222 #ifdef DW_LLE_view_pair
10223   dw2_asm_output_data (1, DW_LLE_view_pair, "DW_LLE_view_pair");
10224 
10225   if (dwarf2out_as_locview_support)
10226     {
10227       if (ZERO_VIEW_P (curr->vbegin))
10228 	dw2_asm_output_data_uleb128 (0, "Location view begin");
10229       else
10230 	{
10231 	  char label[MAX_ARTIFICIAL_LABEL_BYTES];
10232 	  ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vbegin);
10233 	  dw2_asm_output_symname_uleb128 (label, "Location view begin");
10234 	}
10235 
10236       if (ZERO_VIEW_P (curr->vend))
10237 	dw2_asm_output_data_uleb128 (0, "Location view end");
10238       else
10239 	{
10240 	  char label[MAX_ARTIFICIAL_LABEL_BYTES];
10241 	  ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vend);
10242 	  dw2_asm_output_symname_uleb128 (label, "Location view end");
10243 	}
10244     }
10245   else
10246     {
10247       dw2_asm_output_data_uleb128 (curr->vbegin, "Location view begin");
10248       dw2_asm_output_data_uleb128 (curr->vend, "Location view end");
10249     }
10250 #endif /* DW_LLE_view_pair */
10251 
10252   return;
10253 }
10254 
10255 /* Output the location list given to us.  */
10256 
10257 static void
output_loc_list(dw_loc_list_ref list_head)10258 output_loc_list (dw_loc_list_ref list_head)
10259 {
10260   int vcount = 0, lcount = 0;
10261 
10262   if (list_head->emitted)
10263     return;
10264   list_head->emitted = true;
10265 
10266   if (list_head->vl_symbol && dwarf2out_locviews_in_attribute ())
10267     {
10268       ASM_OUTPUT_LABEL (asm_out_file, list_head->vl_symbol);
10269 
10270       for (dw_loc_list_ref curr = list_head; curr != NULL;
10271 	   curr = curr->dw_loc_next)
10272 	{
10273 	  unsigned long size;
10274 
10275 	  if (skip_loc_list_entry (curr, &size))
10276 	    continue;
10277 
10278 	  vcount++;
10279 
10280 	  /* ?? dwarf_split_debug_info?  */
10281 	  if (dwarf2out_as_locview_support)
10282 	    {
10283 	      char label[MAX_ARTIFICIAL_LABEL_BYTES];
10284 
10285 	      if (!ZERO_VIEW_P (curr->vbegin))
10286 		{
10287 		  ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vbegin);
10288 		  dw2_asm_output_symname_uleb128 (label,
10289 						  "View list begin (%s)",
10290 						  list_head->vl_symbol);
10291 		}
10292 	      else
10293 		dw2_asm_output_data_uleb128 (0,
10294 					     "View list begin (%s)",
10295 					     list_head->vl_symbol);
10296 
10297 	      if (!ZERO_VIEW_P (curr->vend))
10298 		{
10299 		  ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vend);
10300 		  dw2_asm_output_symname_uleb128 (label,
10301 						  "View list end (%s)",
10302 						  list_head->vl_symbol);
10303 		}
10304 	      else
10305 		dw2_asm_output_data_uleb128 (0,
10306 					     "View list end (%s)",
10307 					     list_head->vl_symbol);
10308 	    }
10309 	  else
10310 	    {
10311 	      dw2_asm_output_data_uleb128 (curr->vbegin,
10312 					   "View list begin (%s)",
10313 					   list_head->vl_symbol);
10314 	      dw2_asm_output_data_uleb128 (curr->vend,
10315 					   "View list end (%s)",
10316 					   list_head->vl_symbol);
10317 	    }
10318 	}
10319     }
10320 
10321   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
10322 
10323   const char *last_section = NULL;
10324   const char *base_label = NULL;
10325 
10326   /* Walk the location list, and output each range + expression.  */
10327   for (dw_loc_list_ref curr = list_head; curr != NULL;
10328        curr = curr->dw_loc_next)
10329     {
10330       unsigned long size;
10331 
10332       /* Skip this entry?  If we skip it here, we must skip it in the
10333 	 view list above as well. */
10334       if (skip_loc_list_entry (curr, &size))
10335 	continue;
10336 
10337       lcount++;
10338 
10339       if (dwarf_version >= 5)
10340 	{
10341 	  if (dwarf_split_debug_info && HAVE_AS_LEB128)
10342 	    {
10343 	      dwarf2out_maybe_output_loclist_view_pair (curr);
10344 	      /* For -gsplit-dwarf, emit DW_LLE_startx_length, which has
10345 		 uleb128 index into .debug_addr and uleb128 length.  */
10346 	      dw2_asm_output_data (1, DW_LLE_startx_length,
10347 				   "DW_LLE_startx_length (%s)",
10348 				   list_head->ll_symbol);
10349 	      dw2_asm_output_data_uleb128 (curr->begin_entry->index,
10350 					   "Location list range start index "
10351 					   "(%s)", curr->begin);
10352 	      dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
10353 					    "Location list length (%s)",
10354 					    list_head->ll_symbol);
10355 	    }
10356 	  else if (dwarf_split_debug_info)
10357 	    {
10358 	      dwarf2out_maybe_output_loclist_view_pair (curr);
10359 	      /* For -gsplit-dwarf without usable .uleb128 support, emit
10360 		 DW_LLE_startx_endx, which has two uleb128 indexes into
10361 		 .debug_addr.  */
10362 	      dw2_asm_output_data (1, DW_LLE_startx_endx,
10363 				   "DW_LLE_startx_endx (%s)",
10364 				   list_head->ll_symbol);
10365 	      dw2_asm_output_data_uleb128 (curr->begin_entry->index,
10366 					   "Location list range start index "
10367 					   "(%s)", curr->begin);
10368 	      dw2_asm_output_data_uleb128 (curr->end_entry->index,
10369 					   "Location list range end index "
10370 					   "(%s)", curr->end);
10371 	    }
10372 	  else if (!have_multiple_function_sections && HAVE_AS_LEB128)
10373 	    {
10374 	      dwarf2out_maybe_output_loclist_view_pair (curr);
10375 	      /* If all code is in .text section, the base address is
10376 		 already provided by the CU attributes.  Use
10377 		 DW_LLE_offset_pair where both addresses are uleb128 encoded
10378 		 offsets against that base.  */
10379 	      dw2_asm_output_data (1, DW_LLE_offset_pair,
10380 				   "DW_LLE_offset_pair (%s)",
10381 				   list_head->ll_symbol);
10382 	      dw2_asm_output_delta_uleb128 (curr->begin, curr->section,
10383 					    "Location list begin address (%s)",
10384 					    list_head->ll_symbol);
10385 	      dw2_asm_output_delta_uleb128 (curr->end, curr->section,
10386 					    "Location list end address (%s)",
10387 					    list_head->ll_symbol);
10388 	    }
10389 	  else if (HAVE_AS_LEB128)
10390 	    {
10391 	      /* Otherwise, find out how many consecutive entries could share
10392 		 the same base entry.  If just one, emit DW_LLE_start_length,
10393 		 otherwise emit DW_LLE_base_address for the base address
10394 		 followed by a series of DW_LLE_offset_pair.  */
10395 	      if (last_section == NULL || curr->section != last_section)
10396 		{
10397 		  dw_loc_list_ref curr2;
10398 		  for (curr2 = curr->dw_loc_next; curr2 != NULL;
10399 		       curr2 = curr2->dw_loc_next)
10400 		    {
10401 		      if (strcmp (curr2->begin, curr2->end) == 0
10402 			  && !curr2->force)
10403 			continue;
10404 		      break;
10405 		    }
10406 		  if (curr2 == NULL || curr->section != curr2->section)
10407 		    last_section = NULL;
10408 		  else
10409 		    {
10410 		      last_section = curr->section;
10411 		      base_label = curr->begin;
10412 		      dw2_asm_output_data (1, DW_LLE_base_address,
10413 					   "DW_LLE_base_address (%s)",
10414 					   list_head->ll_symbol);
10415 		      dw2_asm_output_addr (DWARF2_ADDR_SIZE, base_label,
10416 					   "Base address (%s)",
10417 					   list_head->ll_symbol);
10418 		    }
10419 		}
10420 	      /* Only one entry with the same base address.  Use
10421 		 DW_LLE_start_length with absolute address and uleb128
10422 		 length.  */
10423 	      if (last_section == NULL)
10424 		{
10425 		  dwarf2out_maybe_output_loclist_view_pair (curr);
10426 		  dw2_asm_output_data (1, DW_LLE_start_length,
10427 				       "DW_LLE_start_length (%s)",
10428 				       list_head->ll_symbol);
10429 		  dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10430 				       "Location list begin address (%s)",
10431 				       list_head->ll_symbol);
10432 		  dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
10433 						"Location list length "
10434 						"(%s)", list_head->ll_symbol);
10435 		}
10436 	      /* Otherwise emit DW_LLE_offset_pair, relative to above emitted
10437 		 DW_LLE_base_address.  */
10438 	      else
10439 		{
10440 		  dwarf2out_maybe_output_loclist_view_pair (curr);
10441 		  dw2_asm_output_data (1, DW_LLE_offset_pair,
10442 				       "DW_LLE_offset_pair (%s)",
10443 				       list_head->ll_symbol);
10444 		  dw2_asm_output_delta_uleb128 (curr->begin, base_label,
10445 						"Location list begin address "
10446 						"(%s)", list_head->ll_symbol);
10447 		  dw2_asm_output_delta_uleb128 (curr->end, base_label,
10448 						"Location list end address "
10449 						"(%s)", list_head->ll_symbol);
10450 		}
10451 	    }
10452 	  /* The assembler does not support .uleb128 directive.  Emit
10453 	     DW_LLE_start_end with a pair of absolute addresses.  */
10454 	  else
10455 	    {
10456 	      dwarf2out_maybe_output_loclist_view_pair (curr);
10457 	      dw2_asm_output_data (1, DW_LLE_start_end,
10458 				   "DW_LLE_start_end (%s)",
10459 				   list_head->ll_symbol);
10460 	      dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10461 				   "Location list begin address (%s)",
10462 				   list_head->ll_symbol);
10463 	      dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10464 				   "Location list end address (%s)",
10465 				   list_head->ll_symbol);
10466 	    }
10467 	}
10468       else if (dwarf_split_debug_info)
10469 	{
10470 	  /* For -gsplit-dwarf -gdwarf-{2,3,4} emit index into .debug_addr
10471 	     and 4 byte length.  */
10472 	  dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
10473 			       "Location list start/length entry (%s)",
10474 			       list_head->ll_symbol);
10475 	  dw2_asm_output_data_uleb128 (curr->begin_entry->index,
10476 				       "Location list range start index (%s)",
10477 				       curr->begin);
10478 	  /* The length field is 4 bytes.  If we ever need to support
10479 	     an 8-byte length, we can add a new DW_LLE code or fall back
10480 	     to DW_LLE_GNU_start_end_entry.  */
10481 	  dw2_asm_output_delta (4, curr->end, curr->begin,
10482 				"Location list range length (%s)",
10483 				list_head->ll_symbol);
10484 	}
10485       else if (!have_multiple_function_sections)
10486 	{
10487 	  /* Pair of relative addresses against start of text section.  */
10488 	  dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
10489 				"Location list begin address (%s)",
10490 				list_head->ll_symbol);
10491 	  dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
10492 				"Location list end address (%s)",
10493 				list_head->ll_symbol);
10494 	}
10495       else
10496 	{
10497 	  /* Pair of absolute addresses.  */
10498 	  dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10499 			       "Location list begin address (%s)",
10500 			       list_head->ll_symbol);
10501 	  dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10502 			       "Location list end address (%s)",
10503 			       list_head->ll_symbol);
10504 	}
10505 
10506       /* Output the block length for this list of location operations.  */
10507       if (dwarf_version >= 5)
10508 	dw2_asm_output_data_uleb128 (size, "Location expression size");
10509       else
10510 	{
10511 	  gcc_assert (size <= 0xffff);
10512 	  dw2_asm_output_data (2, size, "Location expression size");
10513 	}
10514 
10515       output_loc_sequence (curr->expr, -1);
10516     }
10517 
10518   /* And finally list termination.  */
10519   if (dwarf_version >= 5)
10520     dw2_asm_output_data (1, DW_LLE_end_of_list,
10521 			 "DW_LLE_end_of_list (%s)", list_head->ll_symbol);
10522   else if (dwarf_split_debug_info)
10523     dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
10524 			 "Location list terminator (%s)",
10525 			 list_head->ll_symbol);
10526   else
10527     {
10528       dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10529 			   "Location list terminator begin (%s)",
10530 			   list_head->ll_symbol);
10531       dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10532 			   "Location list terminator end (%s)",
10533 			   list_head->ll_symbol);
10534     }
10535 
10536   gcc_assert (!list_head->vl_symbol
10537 	      || vcount == lcount * (dwarf2out_locviews_in_attribute () ? 1 : 0));
10538 }
10539 
10540 /* Output a range_list offset into the .debug_ranges or .debug_rnglists
10541    section.  Emit a relocated reference if val_entry is NULL, otherwise,
10542    emit an indirect reference.  */
10543 
10544 static void
output_range_list_offset(dw_attr_node * a)10545 output_range_list_offset (dw_attr_node *a)
10546 {
10547   const char *name = dwarf_attr_name (a->dw_attr);
10548 
10549   if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
10550     {
10551       if (dwarf_version >= 5)
10552 	{
10553 	  dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
10554 	  dw2_asm_output_offset (dwarf_offset_size, r->label,
10555 				 debug_ranges_section, "%s", name);
10556 	}
10557       else
10558 	{
10559 	  char *p = strchr (ranges_section_label, '\0');
10560 	  sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
10561 		   a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE);
10562 	  dw2_asm_output_offset (dwarf_offset_size, ranges_section_label,
10563 				 debug_ranges_section, "%s", name);
10564 	  *p = '\0';
10565 	}
10566     }
10567   else if (dwarf_version >= 5)
10568     {
10569       dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
10570       gcc_assert (rnglist_idx);
10571       dw2_asm_output_data_uleb128 (r->idx, "%s", name);
10572     }
10573   else
10574     dw2_asm_output_data (dwarf_offset_size,
10575 			 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE,
10576                          "%s (offset from %s)", name, ranges_section_label);
10577 }
10578 
10579 /* Output the offset into the debug_loc section.  */
10580 
10581 static void
output_loc_list_offset(dw_attr_node * a)10582 output_loc_list_offset (dw_attr_node *a)
10583 {
10584   char *sym = AT_loc_list (a)->ll_symbol;
10585 
10586   gcc_assert (sym);
10587   if (!dwarf_split_debug_info)
10588     dw2_asm_output_offset (dwarf_offset_size, sym, debug_loc_section,
10589                            "%s", dwarf_attr_name (a->dw_attr));
10590   else if (dwarf_version >= 5)
10591     {
10592       gcc_assert (AT_loc_list (a)->num_assigned);
10593       dw2_asm_output_data_uleb128 (AT_loc_list (a)->hash, "%s (%s)",
10594 				   dwarf_attr_name (a->dw_attr),
10595 				   sym);
10596     }
10597   else
10598     dw2_asm_output_delta (dwarf_offset_size, sym, loc_section_label,
10599 			  "%s", dwarf_attr_name (a->dw_attr));
10600 }
10601 
10602 /* Output the offset into the debug_loc section.  */
10603 
10604 static void
output_view_list_offset(dw_attr_node * a)10605 output_view_list_offset (dw_attr_node *a)
10606 {
10607   char *sym = (*AT_loc_list_ptr (a))->vl_symbol;
10608 
10609   gcc_assert (sym);
10610   if (dwarf_split_debug_info)
10611     dw2_asm_output_delta (dwarf_offset_size, sym, loc_section_label,
10612                           "%s", dwarf_attr_name (a->dw_attr));
10613   else
10614     dw2_asm_output_offset (dwarf_offset_size, sym, debug_loc_section,
10615                            "%s", dwarf_attr_name (a->dw_attr));
10616 }
10617 
10618 /* Output an attribute's index or value appropriately.  */
10619 
10620 static void
output_attr_index_or_value(dw_attr_node * a)10621 output_attr_index_or_value (dw_attr_node *a)
10622 {
10623   const char *name = dwarf_attr_name (a->dw_attr);
10624 
10625   if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
10626     {
10627       dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
10628       return;
10629     }
10630   switch (AT_class (a))
10631     {
10632     case dw_val_class_addr:
10633       dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
10634       break;
10635     case dw_val_class_high_pc:
10636     case dw_val_class_lbl_id:
10637       dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
10638       break;
10639     default:
10640       gcc_unreachable ();
10641     }
10642 }
10643 
10644 /* Output a type signature.  */
10645 
10646 static inline void
output_signature(const char * sig,const char * name)10647 output_signature (const char *sig, const char *name)
10648 {
10649   int i;
10650 
10651   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10652     dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
10653 }
10654 
10655 /* Output a discriminant value.  */
10656 
10657 static inline void
output_discr_value(dw_discr_value * discr_value,const char * name)10658 output_discr_value (dw_discr_value *discr_value, const char *name)
10659 {
10660   if (discr_value->pos)
10661     dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
10662   else
10663     dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
10664 }
10665 
10666 /* Output the DIE and its attributes.  Called recursively to generate
10667    the definitions of each child DIE.  */
10668 
10669 static void
output_die(dw_die_ref die)10670 output_die (dw_die_ref die)
10671 {
10672   dw_attr_node *a;
10673   dw_die_ref c;
10674   unsigned long size;
10675   unsigned ix;
10676 
10677   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
10678 			       (unsigned long)die->die_offset,
10679 			       dwarf_tag_name (die->die_tag));
10680 
10681   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
10682     {
10683       const char *name = dwarf_attr_name (a->dw_attr);
10684 
10685       switch (AT_class (a))
10686 	{
10687 	case dw_val_class_addr:
10688           output_attr_index_or_value (a);
10689 	  break;
10690 
10691 	case dw_val_class_offset:
10692 	  dw2_asm_output_data (dwarf_offset_size, a->dw_attr_val.v.val_offset,
10693 			       "%s", name);
10694 	  break;
10695 
10696 	case dw_val_class_range_list:
10697           output_range_list_offset (a);
10698 	  break;
10699 
10700 	case dw_val_class_loc:
10701 	  size = size_of_locs (AT_loc (a));
10702 
10703 	  /* Output the block length for this list of location operations.  */
10704 	  if (dwarf_version >= 4)
10705 	    dw2_asm_output_data_uleb128 (size, "%s", name);
10706 	  else
10707 	    dw2_asm_output_data (constant_size (size), size, "%s", name);
10708 
10709 	  output_loc_sequence (AT_loc (a), -1);
10710 	  break;
10711 
10712 	case dw_val_class_const:
10713 	  /* ??? It would be slightly more efficient to use a scheme like is
10714 	     used for unsigned constants below, but gdb 4.x does not sign
10715 	     extend.  Gdb 5.x does sign extend.  */
10716 	  dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
10717 	  break;
10718 
10719 	case dw_val_class_unsigned_const:
10720 	  {
10721 	    int csize = constant_size (AT_unsigned (a));
10722 	    if (dwarf_version == 3
10723 		&& a->dw_attr == DW_AT_data_member_location
10724 		&& csize >= 4)
10725 	      dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
10726 	    else
10727 	      dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
10728 	  }
10729 	  break;
10730 
10731 	case dw_val_class_symview:
10732 	  {
10733 	    int vsize;
10734 	    if (symview_upper_bound <= 0xff)
10735 	      vsize = 1;
10736 	    else if (symview_upper_bound <= 0xffff)
10737 	      vsize = 2;
10738 	    else if (symview_upper_bound <= 0xffffffff)
10739 	      vsize = 4;
10740 	    else
10741 	      vsize = 8;
10742 	    dw2_asm_output_addr (vsize, a->dw_attr_val.v.val_symbolic_view,
10743 				 "%s", name);
10744 	  }
10745 	  break;
10746 
10747 	case dw_val_class_const_implicit:
10748 	  if (flag_debug_asm)
10749 	    fprintf (asm_out_file, "\t\t\t%s %s ("
10750 				   HOST_WIDE_INT_PRINT_DEC ")\n",
10751 		     ASM_COMMENT_START, name, AT_int (a));
10752 	  break;
10753 
10754 	case dw_val_class_unsigned_const_implicit:
10755 	  if (flag_debug_asm)
10756 	    fprintf (asm_out_file, "\t\t\t%s %s ("
10757 				   HOST_WIDE_INT_PRINT_HEX ")\n",
10758 		     ASM_COMMENT_START, name, AT_unsigned (a));
10759 	  break;
10760 
10761 	case dw_val_class_const_double:
10762 	  {
10763 	    unsigned HOST_WIDE_INT first, second;
10764 
10765 	    if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
10766 	      dw2_asm_output_data (1,
10767 				   HOST_BITS_PER_DOUBLE_INT
10768 				   / HOST_BITS_PER_CHAR,
10769 				   NULL);
10770 
10771 	    if (WORDS_BIG_ENDIAN)
10772 	      {
10773 		first = a->dw_attr_val.v.val_double.high;
10774 		second = a->dw_attr_val.v.val_double.low;
10775 	      }
10776 	    else
10777 	      {
10778 		first = a->dw_attr_val.v.val_double.low;
10779 		second = a->dw_attr_val.v.val_double.high;
10780 	      }
10781 
10782 	    dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10783                                  first, "%s", name);
10784 	    dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10785 				 second, NULL);
10786 	  }
10787 	  break;
10788 
10789 	case dw_val_class_wide_int:
10790 	  {
10791 	    int i;
10792 	    int len = get_full_len (*a->dw_attr_val.v.val_wide);
10793 	    int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10794 	    if (len * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
10795 	      dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide)
10796 				      * l, NULL);
10797 
10798 	    if (WORDS_BIG_ENDIAN)
10799 	      for (i = len - 1; i >= 0; --i)
10800 		{
10801 		  dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10802 				       "%s", name);
10803 		  name = "";
10804 		}
10805 	    else
10806 	      for (i = 0; i < len; ++i)
10807 		{
10808 		  dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10809 				       "%s", name);
10810 		  name = "";
10811 		}
10812 	  }
10813 	  break;
10814 
10815 	case dw_val_class_vec:
10816 	  {
10817 	    unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
10818 	    unsigned int len = a->dw_attr_val.v.val_vec.length;
10819 	    unsigned int i;
10820 	    unsigned char *p;
10821 
10822 	    dw2_asm_output_data (constant_size (len * elt_size),
10823 				 len * elt_size, "%s", name);
10824 	    if (elt_size > sizeof (HOST_WIDE_INT))
10825 	      {
10826 		elt_size /= 2;
10827 		len *= 2;
10828 	      }
10829 	    for (i = 0, p = (unsigned char *) a->dw_attr_val.v.val_vec.array;
10830 		 i < len;
10831 		 i++, p += elt_size)
10832 	      dw2_asm_output_data (elt_size, extract_int (p, elt_size),
10833 				   "fp or vector constant word %u", i);
10834 	    break;
10835 	  }
10836 
10837 	case dw_val_class_flag:
10838 	  if (dwarf_version >= 4)
10839 	    {
10840 	      /* Currently all add_AT_flag calls pass in 1 as last argument,
10841 		 so DW_FORM_flag_present can be used.  If that ever changes,
10842 		 we'll need to use DW_FORM_flag and have some optimization
10843 		 in build_abbrev_table that will change those to
10844 		 DW_FORM_flag_present if it is set to 1 in all DIEs using
10845 		 the same abbrev entry.  */
10846 	      gcc_assert (AT_flag (a) == 1);
10847 	      if (flag_debug_asm)
10848 		fprintf (asm_out_file, "\t\t\t%s %s\n",
10849 			 ASM_COMMENT_START, name);
10850 	      break;
10851 	    }
10852 	  dw2_asm_output_data (1, AT_flag (a), "%s", name);
10853 	  break;
10854 
10855 	case dw_val_class_loc_list:
10856 	  output_loc_list_offset (a);
10857 	  break;
10858 
10859 	case dw_val_class_view_list:
10860 	  output_view_list_offset (a);
10861 	  break;
10862 
10863 	case dw_val_class_die_ref:
10864 	  if (AT_ref_external (a))
10865 	    {
10866 	      if (AT_ref (a)->comdat_type_p)
10867 	        {
10868 		  comdat_type_node *type_node
10869 		    = AT_ref (a)->die_id.die_type_node;
10870 
10871 	          gcc_assert (type_node);
10872 	          output_signature (type_node->signature, name);
10873 	        }
10874 	      else
10875 	        {
10876 		  const char *sym = AT_ref (a)->die_id.die_symbol;
10877 		  int size;
10878 
10879 		  gcc_assert (sym);
10880 		  /* In DWARF2, DW_FORM_ref_addr is sized by target address
10881 		     length, whereas in DWARF3 it's always sized as an
10882 		     offset.  */
10883 		  if (dwarf_version == 2)
10884 		    size = DWARF2_ADDR_SIZE;
10885 		  else
10886 		    size = dwarf_offset_size;
10887 		  /* ???  We cannot unconditionally output die_offset if
10888 		     non-zero - others might create references to those
10889 		     DIEs via symbols.
10890 		     And we do not clear its DIE offset after outputting it
10891 		     (and the label refers to the actual DIEs, not the
10892 		     DWARF CU unit header which is when using label + offset
10893 		     would be the correct thing to do).
10894 		     ???  This is the reason for the with_offset flag.  */
10895 		  if (AT_ref (a)->with_offset)
10896 		    dw2_asm_output_offset (size, sym, AT_ref (a)->die_offset,
10897 					   debug_info_section, "%s", name);
10898 		  else
10899 		    dw2_asm_output_offset (size, sym, debug_info_section, "%s",
10900 					   name);
10901 		}
10902 	    }
10903 	  else
10904 	    {
10905 	      gcc_assert (AT_ref (a)->die_offset);
10906 	      dw2_asm_output_data (dwarf_offset_size, AT_ref (a)->die_offset,
10907 				   "%s", name);
10908 	    }
10909 	  break;
10910 
10911 	case dw_val_class_fde_ref:
10912 	  {
10913 	    char l1[MAX_ARTIFICIAL_LABEL_BYTES];
10914 
10915 	    ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
10916 					 a->dw_attr_val.v.val_fde_index * 2);
10917 	    dw2_asm_output_offset (dwarf_offset_size, l1, debug_frame_section,
10918 				   "%s", name);
10919 	  }
10920 	  break;
10921 
10922 	case dw_val_class_vms_delta:
10923 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
10924 	  dw2_asm_output_vms_delta (dwarf_offset_size,
10925 				    AT_vms_delta2 (a), AT_vms_delta1 (a),
10926 				    "%s", name);
10927 #else
10928 	  dw2_asm_output_delta (dwarf_offset_size,
10929 				AT_vms_delta2 (a), AT_vms_delta1 (a),
10930 				"%s", name);
10931 #endif
10932 	  break;
10933 
10934 	case dw_val_class_lbl_id:
10935 	  output_attr_index_or_value (a);
10936 	  break;
10937 
10938 	case dw_val_class_lineptr:
10939 	  dw2_asm_output_offset (dwarf_offset_size, AT_lbl (a),
10940 				 debug_line_section, "%s", name);
10941 	  break;
10942 
10943 	case dw_val_class_macptr:
10944 	  dw2_asm_output_offset (dwarf_offset_size, AT_lbl (a),
10945 				 debug_macinfo_section, "%s", name);
10946 	  break;
10947 
10948 	case dw_val_class_loclistsptr:
10949 	  dw2_asm_output_offset (dwarf_offset_size, AT_lbl (a),
10950 				 debug_loc_section, "%s", name);
10951 	  break;
10952 
10953 	case dw_val_class_str:
10954           if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
10955             dw2_asm_output_offset (dwarf_offset_size,
10956                                    a->dw_attr_val.v.val_str->label,
10957                                    debug_str_section,
10958                                    "%s: \"%s\"", name, AT_string (a));
10959 	  else if (a->dw_attr_val.v.val_str->form == DW_FORM_line_strp)
10960 	    dw2_asm_output_offset (dwarf_offset_size,
10961 				   a->dw_attr_val.v.val_str->label,
10962 				   debug_line_str_section,
10963 				   "%s: \"%s\"", name, AT_string (a));
10964           else if (a->dw_attr_val.v.val_str->form == dwarf_FORM (DW_FORM_strx))
10965             dw2_asm_output_data_uleb128 (AT_index (a),
10966                                          "%s: \"%s\"", name, AT_string (a));
10967           else
10968 	    dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
10969 	  break;
10970 
10971 	case dw_val_class_file:
10972 	  {
10973 	    int f = maybe_emit_file (a->dw_attr_val.v.val_file);
10974 
10975 	    dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
10976 				 a->dw_attr_val.v.val_file->filename);
10977 	    break;
10978 	  }
10979 
10980 	case dw_val_class_file_implicit:
10981 	  if (flag_debug_asm)
10982 	    fprintf (asm_out_file, "\t\t\t%s %s (%d, %s)\n",
10983 		     ASM_COMMENT_START, name,
10984 		     maybe_emit_file (a->dw_attr_val.v.val_file),
10985 		     a->dw_attr_val.v.val_file->filename);
10986 	  break;
10987 
10988 	case dw_val_class_data8:
10989 	  {
10990 	    int i;
10991 
10992 	    for (i = 0; i < 8; i++)
10993 	      dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
10994 				   i == 0 ? "%s" : NULL, name);
10995 	    break;
10996 	  }
10997 
10998 	case dw_val_class_high_pc:
10999 	  dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
11000 				get_AT_low_pc (die), "DW_AT_high_pc");
11001 	  break;
11002 
11003 	case dw_val_class_discr_value:
11004 	  output_discr_value (&a->dw_attr_val.v.val_discr_value, name);
11005 	  break;
11006 
11007 	case dw_val_class_discr_list:
11008 	  {
11009 	    dw_discr_list_ref list = AT_discr_list (a);
11010 	    const int size = size_of_discr_list (list);
11011 
11012 	    /* This is a block, so output its length first.  */
11013 	    dw2_asm_output_data (constant_size (size), size,
11014 				 "%s: block size", name);
11015 
11016 	    for (; list != NULL; list = list->dw_discr_next)
11017 	      {
11018 		/* One byte for the discriminant value descriptor, and then as
11019 		   many LEB128 numbers as required.  */
11020 		if (list->dw_discr_range)
11021 		  dw2_asm_output_data (1, DW_DSC_range,
11022 				       "%s: DW_DSC_range", name);
11023 		else
11024 		  dw2_asm_output_data (1, DW_DSC_label,
11025 				       "%s: DW_DSC_label", name);
11026 
11027 		output_discr_value (&list->dw_discr_lower_bound, name);
11028 		if (list->dw_discr_range)
11029 		  output_discr_value (&list->dw_discr_upper_bound, name);
11030 	      }
11031 	    break;
11032 	  }
11033 
11034 	default:
11035 	  gcc_unreachable ();
11036 	}
11037     }
11038 
11039   FOR_EACH_CHILD (die, c, output_die (c));
11040 
11041   /* Add null byte to terminate sibling list.  */
11042   if (die->die_child != NULL)
11043     dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
11044 			 (unsigned long) die->die_offset);
11045 }
11046 
11047 /* Output the dwarf version number.  */
11048 
11049 static void
output_dwarf_version()11050 output_dwarf_version ()
11051 {
11052   /* ??? For now, if -gdwarf-6 is specified, we output version 5 with
11053      views in loclist.  That will change eventually.  */
11054   if (dwarf_version == 6)
11055     {
11056       static bool once;
11057       if (!once)
11058 	{
11059 	  warning (0, "%<-gdwarf-6%> is output as version 5 with "
11060 		   "incompatibilities");
11061 	  once = true;
11062 	}
11063       dw2_asm_output_data (2, 5, "DWARF version number");
11064     }
11065   else
11066     dw2_asm_output_data (2, dwarf_version, "DWARF version number");
11067 }
11068 
11069 /* Output the compilation unit that appears at the beginning of the
11070    .debug_info section, and precedes the DIE descriptions.  */
11071 
11072 static void
output_compilation_unit_header(enum dwarf_unit_type ut)11073 output_compilation_unit_header (enum dwarf_unit_type ut)
11074 {
11075   if (!XCOFF_DEBUGGING_INFO)
11076     {
11077       if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
11078 	dw2_asm_output_data (4, 0xffffffff,
11079 	  "Initial length escape value indicating 64-bit DWARF extension");
11080       dw2_asm_output_data (dwarf_offset_size,
11081 			   next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
11082 			   "Length of Compilation Unit Info");
11083     }
11084 
11085   output_dwarf_version ();
11086   if (dwarf_version >= 5)
11087     {
11088       const char *name;
11089       switch (ut)
11090 	{
11091 	case DW_UT_compile: name = "DW_UT_compile"; break;
11092 	case DW_UT_type: name = "DW_UT_type"; break;
11093 	case DW_UT_split_compile: name = "DW_UT_split_compile"; break;
11094 	case DW_UT_split_type: name = "DW_UT_split_type"; break;
11095 	default: gcc_unreachable ();
11096 	}
11097       dw2_asm_output_data (1, ut, "%s", name);
11098       dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11099     }
11100   dw2_asm_output_offset (dwarf_offset_size, abbrev_section_label,
11101 			 debug_abbrev_section,
11102 			 "Offset Into Abbrev. Section");
11103   if (dwarf_version < 5)
11104     dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11105 }
11106 
11107 /* Output the compilation unit DIE and its children.  */
11108 
11109 static void
output_comp_unit(dw_die_ref die,int output_if_empty,const unsigned char * dwo_id)11110 output_comp_unit (dw_die_ref die, int output_if_empty,
11111 		  const unsigned char *dwo_id)
11112 {
11113   const char *secname, *oldsym;
11114   char *tmp;
11115 
11116   /* Unless we are outputting main CU, we may throw away empty ones.  */
11117   if (!output_if_empty && die->die_child == NULL)
11118     return;
11119 
11120   /* Even if there are no children of this DIE, we must output the information
11121      about the compilation unit.  Otherwise, on an empty translation unit, we
11122      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
11123      will then complain when examining the file.  First mark all the DIEs in
11124      this CU so we know which get local refs.  */
11125   mark_dies (die);
11126 
11127   external_ref_hash_type *extern_map = optimize_external_refs (die);
11128 
11129   /* For now, optimize only the main CU, in order to optimize the rest
11130      we'd need to see all of them earlier.  Leave the rest for post-linking
11131      tools like DWZ.  */
11132   if (die == comp_unit_die ())
11133     abbrev_opt_start = vec_safe_length (abbrev_die_table);
11134 
11135   build_abbrev_table (die, extern_map);
11136 
11137   optimize_abbrev_table ();
11138 
11139   delete extern_map;
11140 
11141   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
11142   next_die_offset = (dwo_id
11143 		     ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
11144 		     : DWARF_COMPILE_UNIT_HEADER_SIZE);
11145   calc_die_sizes (die);
11146 
11147   oldsym = die->die_id.die_symbol;
11148   if (oldsym && die->comdat_type_p)
11149     {
11150       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
11151 
11152       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
11153       secname = tmp;
11154       die->die_id.die_symbol = NULL;
11155       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11156     }
11157   else
11158     {
11159       switch_to_section (debug_info_section);
11160       ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
11161       info_section_emitted = true;
11162     }
11163 
11164   /* For LTO cross unit DIE refs we want a symbol on the start of the
11165      debuginfo section, not on the CU DIE.  */
11166   if ((flag_generate_lto || flag_generate_offload) && oldsym)
11167     {
11168       /* ???  No way to get visibility assembled without a decl.  */
11169       tree decl = build_decl (UNKNOWN_LOCATION, VAR_DECL,
11170 			      get_identifier (oldsym), char_type_node);
11171       TREE_PUBLIC (decl) = true;
11172       TREE_STATIC (decl) = true;
11173       DECL_ARTIFICIAL (decl) = true;
11174       DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
11175       DECL_VISIBILITY_SPECIFIED (decl) = true;
11176       targetm.asm_out.assemble_visibility (decl, VISIBILITY_HIDDEN);
11177 #ifdef ASM_WEAKEN_LABEL
11178       /* We prefer a .weak because that handles duplicates from duplicate
11179          archive members in a graceful way.  */
11180       ASM_WEAKEN_LABEL (asm_out_file, oldsym);
11181 #else
11182       targetm.asm_out.globalize_label (asm_out_file, oldsym);
11183 #endif
11184       ASM_OUTPUT_LABEL (asm_out_file, oldsym);
11185     }
11186 
11187   /* Output debugging information.  */
11188   output_compilation_unit_header (dwo_id
11189 				  ? DW_UT_split_compile : DW_UT_compile);
11190   if (dwarf_version >= 5)
11191     {
11192       if (dwo_id != NULL)
11193 	for (int i = 0; i < 8; i++)
11194 	  dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
11195     }
11196   output_die (die);
11197 
11198   /* Leave the marks on the main CU, so we can check them in
11199      output_pubnames.  */
11200   if (oldsym)
11201     {
11202       unmark_dies (die);
11203       die->die_id.die_symbol = oldsym;
11204     }
11205 }
11206 
11207 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
11208    and .debug_pubtypes.  This is configured per-target, but can be
11209    overridden by the -gpubnames or -gno-pubnames options.  */
11210 
11211 static inline bool
want_pubnames(void)11212 want_pubnames (void)
11213 {
11214   if (debug_info_level <= DINFO_LEVEL_TERSE
11215       /* Names and types go to the early debug part only.  */
11216       || in_lto_p)
11217     return false;
11218   if (debug_generate_pub_sections != -1)
11219     return debug_generate_pub_sections;
11220   return targetm.want_debug_pub_sections;
11221 }
11222 
11223 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes.  */
11224 
11225 static void
add_AT_pubnames(dw_die_ref die)11226 add_AT_pubnames (dw_die_ref die)
11227 {
11228   if (want_pubnames ())
11229     add_AT_flag (die, DW_AT_GNU_pubnames, 1);
11230 }
11231 
11232 /* Add a string attribute value to a skeleton DIE.  */
11233 
11234 static inline void
add_skeleton_AT_string(dw_die_ref die,enum dwarf_attribute attr_kind,const char * str)11235 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
11236                         const char *str)
11237 {
11238   dw_attr_node attr;
11239   struct indirect_string_node *node;
11240 
11241   if (! skeleton_debug_str_hash)
11242     skeleton_debug_str_hash
11243       = hash_table<indirect_string_hasher>::create_ggc (10);
11244 
11245   node = find_AT_string_in_table (str, skeleton_debug_str_hash);
11246   find_string_form (node);
11247   if (node->form == dwarf_FORM (DW_FORM_strx))
11248     node->form = DW_FORM_strp;
11249 
11250   attr.dw_attr = attr_kind;
11251   attr.dw_attr_val.val_class = dw_val_class_str;
11252   attr.dw_attr_val.val_entry = NULL;
11253   attr.dw_attr_val.v.val_str = node;
11254   add_dwarf_attr (die, &attr);
11255 }
11256 
11257 /* Helper function to generate top-level dies for skeleton debug_info and
11258    debug_types.  */
11259 
11260 static void
add_top_level_skeleton_die_attrs(dw_die_ref die)11261 add_top_level_skeleton_die_attrs (dw_die_ref die)
11262 {
11263   const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
11264   const char *comp_dir = comp_dir_string ();
11265 
11266   add_skeleton_AT_string (die, dwarf_AT (DW_AT_dwo_name), dwo_file_name);
11267   if (comp_dir != NULL)
11268     add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
11269   add_AT_pubnames (die);
11270   if (addr_index_table != NULL && addr_index_table->size () > 0)
11271     add_AT_lineptr (die, dwarf_AT (DW_AT_addr_base), debug_addr_section_label);
11272 }
11273 
11274 /* Output skeleton debug sections that point to the dwo file.  */
11275 
11276 static void
output_skeleton_debug_sections(dw_die_ref comp_unit,const unsigned char * dwo_id)11277 output_skeleton_debug_sections (dw_die_ref comp_unit,
11278 				const unsigned char *dwo_id)
11279 {
11280   /* These attributes will be found in the full debug_info section.  */
11281   remove_AT (comp_unit, DW_AT_producer);
11282   remove_AT (comp_unit, DW_AT_language);
11283 
11284   switch_to_section (debug_skeleton_info_section);
11285   ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
11286 
11287   /* Produce the skeleton compilation-unit header.  This one differs enough from
11288      a normal CU header that it's better not to call output_compilation_unit
11289      header.  */
11290   if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
11291     dw2_asm_output_data (4, 0xffffffff,
11292 			 "Initial length escape value indicating 64-bit "
11293 			 "DWARF extension");
11294 
11295   dw2_asm_output_data (dwarf_offset_size,
11296 		       DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
11297                        - DWARF_INITIAL_LENGTH_SIZE
11298                        + size_of_die (comp_unit),
11299                       "Length of Compilation Unit Info");
11300   output_dwarf_version ();
11301   if (dwarf_version >= 5)
11302     {
11303       dw2_asm_output_data (1, DW_UT_skeleton, "DW_UT_skeleton");
11304       dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11305     }
11306   dw2_asm_output_offset (dwarf_offset_size, debug_skeleton_abbrev_section_label,
11307 			 debug_skeleton_abbrev_section,
11308                          "Offset Into Abbrev. Section");
11309   if (dwarf_version < 5)
11310     dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11311   else
11312     for (int i = 0; i < 8; i++)
11313       dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
11314 
11315   comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
11316   output_die (comp_unit);
11317 
11318   /* Build the skeleton debug_abbrev section.  */
11319   switch_to_section (debug_skeleton_abbrev_section);
11320   ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
11321 
11322   output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
11323 
11324   dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
11325 }
11326 
11327 /* Output a comdat type unit DIE and its children.  */
11328 
11329 static void
output_comdat_type_unit(comdat_type_node * node,bool early_lto_debug ATTRIBUTE_UNUSED)11330 output_comdat_type_unit (comdat_type_node *node,
11331 			 bool early_lto_debug ATTRIBUTE_UNUSED)
11332 {
11333   const char *secname;
11334   char *tmp;
11335   int i;
11336 #if defined (OBJECT_FORMAT_ELF)
11337   tree comdat_key;
11338 #endif
11339 
11340   /* First mark all the DIEs in this CU so we know which get local refs.  */
11341   mark_dies (node->root_die);
11342 
11343   external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
11344 
11345   build_abbrev_table (node->root_die, extern_map);
11346 
11347   delete extern_map;
11348   extern_map = NULL;
11349 
11350   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
11351   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
11352   calc_die_sizes (node->root_die);
11353 
11354 #if defined (OBJECT_FORMAT_ELF)
11355   if (dwarf_version >= 5)
11356     {
11357       if (!dwarf_split_debug_info)
11358 	secname = early_lto_debug ? DEBUG_LTO_INFO_SECTION : DEBUG_INFO_SECTION;
11359       else
11360 	secname = (early_lto_debug
11361 		   ? DEBUG_LTO_DWO_INFO_SECTION : DEBUG_DWO_INFO_SECTION);
11362     }
11363   else if (!dwarf_split_debug_info)
11364     secname = early_lto_debug ? ".gnu.debuglto_.debug_types" : ".debug_types";
11365   else
11366     secname = (early_lto_debug
11367 	       ? ".gnu.debuglto_.debug_types.dwo" : ".debug_types.dwo");
11368 
11369   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11370   sprintf (tmp, dwarf_version >= 5 ? "wi." : "wt.");
11371   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11372     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
11373   comdat_key = get_identifier (tmp);
11374   targetm.asm_out.named_section (secname,
11375                                  SECTION_DEBUG | SECTION_LINKONCE,
11376                                  comdat_key);
11377 #else
11378   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11379   sprintf (tmp, (dwarf_version >= 5
11380 		 ? ".gnu.linkonce.wi." : ".gnu.linkonce.wt."));
11381   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11382     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
11383   secname = tmp;
11384   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11385 #endif
11386 
11387   /* Output debugging information.  */
11388   output_compilation_unit_header (dwarf_split_debug_info
11389 				  ? DW_UT_split_type : DW_UT_type);
11390   output_signature (node->signature, "Type Signature");
11391   dw2_asm_output_data (dwarf_offset_size, node->type_die->die_offset,
11392 		       "Offset to Type DIE");
11393   output_die (node->root_die);
11394 
11395   unmark_dies (node->root_die);
11396 }
11397 
11398 /* Return the DWARF2/3 pubname associated with a decl.  */
11399 
11400 static const char *
dwarf2_name(tree decl,int scope)11401 dwarf2_name (tree decl, int scope)
11402 {
11403   if (DECL_NAMELESS (decl))
11404     return NULL;
11405   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
11406 }
11407 
11408 /* Add a new entry to .debug_pubnames if appropriate.  */
11409 
11410 static void
add_pubname_string(const char * str,dw_die_ref die)11411 add_pubname_string (const char *str, dw_die_ref die)
11412 {
11413   pubname_entry e;
11414 
11415   e.die = die;
11416   e.name = xstrdup (str);
11417   vec_safe_push (pubname_table, e);
11418 }
11419 
11420 static void
add_pubname(tree decl,dw_die_ref die)11421 add_pubname (tree decl, dw_die_ref die)
11422 {
11423   if (!want_pubnames ())
11424     return;
11425 
11426   /* Don't add items to the table when we expect that the consumer will have
11427      just read the enclosing die.  For example, if the consumer is looking at a
11428      class_member, it will either be inside the class already, or will have just
11429      looked up the class to find the member.  Either way, searching the class is
11430      faster than searching the index.  */
11431   if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
11432       || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
11433     {
11434       const char *name = dwarf2_name (decl, 1);
11435 
11436       if (name)
11437 	add_pubname_string (name, die);
11438     }
11439 }
11440 
11441 /* Add an enumerator to the pubnames section.  */
11442 
11443 static void
add_enumerator_pubname(const char * scope_name,dw_die_ref die)11444 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
11445 {
11446   pubname_entry e;
11447 
11448   gcc_assert (scope_name);
11449   e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
11450   e.die = die;
11451   vec_safe_push (pubname_table, e);
11452 }
11453 
11454 /* Add a new entry to .debug_pubtypes if appropriate.  */
11455 
11456 static void
add_pubtype(tree decl,dw_die_ref die)11457 add_pubtype (tree decl, dw_die_ref die)
11458 {
11459   pubname_entry e;
11460 
11461   if (!want_pubnames ())
11462     return;
11463 
11464   if ((TREE_PUBLIC (decl)
11465        || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
11466       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
11467     {
11468       tree scope = NULL;
11469       const char *scope_name = "";
11470       const char *sep = is_cxx () ? "::" : ".";
11471       const char *name;
11472 
11473       scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
11474       if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
11475         {
11476           scope_name = lang_hooks.dwarf_name (scope, 1);
11477           if (scope_name != NULL && scope_name[0] != '\0')
11478             scope_name = concat (scope_name, sep, NULL);
11479           else
11480             scope_name = "";
11481 	}
11482 
11483       if (TYPE_P (decl))
11484         name = type_tag (decl);
11485       else
11486         name = lang_hooks.dwarf_name (decl, 1);
11487 
11488       /* If we don't have a name for the type, there's no point in adding
11489 	 it to the table.  */
11490       if (name != NULL && name[0] != '\0')
11491         {
11492           e.die = die;
11493           e.name = concat (scope_name, name, NULL);
11494           vec_safe_push (pubtype_table, e);
11495         }
11496 
11497       /* Although it might be more consistent to add the pubinfo for the
11498          enumerators as their dies are created, they should only be added if the
11499          enum type meets the criteria above.  So rather than re-check the parent
11500          enum type whenever an enumerator die is created, just output them all
11501          here.  This isn't protected by the name conditional because anonymous
11502          enums don't have names.  */
11503       if (die->die_tag == DW_TAG_enumeration_type)
11504         {
11505           dw_die_ref c;
11506 
11507           FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
11508         }
11509     }
11510 }
11511 
11512 /* Output a single entry in the pubnames table.  */
11513 
11514 static void
output_pubname(dw_offset die_offset,pubname_entry * entry)11515 output_pubname (dw_offset die_offset, pubname_entry *entry)
11516 {
11517   dw_die_ref die = entry->die;
11518   int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
11519 
11520   dw2_asm_output_data (dwarf_offset_size, die_offset, "DIE offset");
11521 
11522   if (debug_generate_pub_sections == 2)
11523     {
11524       /* This logic follows gdb's method for determining the value of the flag
11525          byte.  */
11526       uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
11527       switch (die->die_tag)
11528       {
11529         case DW_TAG_typedef:
11530         case DW_TAG_base_type:
11531         case DW_TAG_subrange_type:
11532           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11533           GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11534           break;
11535         case DW_TAG_enumerator:
11536           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11537                                           GDB_INDEX_SYMBOL_KIND_VARIABLE);
11538 	  if (!is_cxx ())
11539 	    GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11540           break;
11541         case DW_TAG_subprogram:
11542           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11543                                           GDB_INDEX_SYMBOL_KIND_FUNCTION);
11544           if (!is_ada ())
11545             GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11546           break;
11547         case DW_TAG_constant:
11548           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11549                                           GDB_INDEX_SYMBOL_KIND_VARIABLE);
11550           GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11551           break;
11552         case DW_TAG_variable:
11553           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11554                                           GDB_INDEX_SYMBOL_KIND_VARIABLE);
11555           GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11556           break;
11557         case DW_TAG_namespace:
11558         case DW_TAG_imported_declaration:
11559           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11560           break;
11561         case DW_TAG_class_type:
11562         case DW_TAG_interface_type:
11563         case DW_TAG_structure_type:
11564         case DW_TAG_union_type:
11565         case DW_TAG_enumeration_type:
11566           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11567 	  if (!is_cxx ())
11568 	    GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11569           break;
11570         default:
11571           /* An unusual tag.  Leave the flag-byte empty.  */
11572           break;
11573       }
11574       dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
11575                            "GDB-index flags");
11576     }
11577 
11578   dw2_asm_output_nstring (entry->name, -1, "external name");
11579 }
11580 
11581 
11582 /* Output the public names table used to speed up access to externally
11583    visible names; or the public types table used to find type definitions.  */
11584 
11585 static void
output_pubnames(vec<pubname_entry,va_gc> * names)11586 output_pubnames (vec<pubname_entry, va_gc> *names)
11587 {
11588   unsigned i;
11589   unsigned long pubnames_length = size_of_pubnames (names);
11590   pubname_entry *pub;
11591 
11592   if (!XCOFF_DEBUGGING_INFO)
11593     {
11594       if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
11595 	dw2_asm_output_data (4, 0xffffffff,
11596 	  "Initial length escape value indicating 64-bit DWARF extension");
11597       dw2_asm_output_data (dwarf_offset_size, pubnames_length,
11598 			   "Pub Info Length");
11599     }
11600 
11601   /* Version number for pubnames/pubtypes is independent of dwarf version.  */
11602   dw2_asm_output_data (2, 2, "DWARF pubnames/pubtypes version");
11603 
11604   if (dwarf_split_debug_info)
11605     dw2_asm_output_offset (dwarf_offset_size, debug_skeleton_info_section_label,
11606                            debug_skeleton_info_section,
11607                            "Offset of Compilation Unit Info");
11608   else
11609     dw2_asm_output_offset (dwarf_offset_size, debug_info_section_label,
11610                            debug_info_section,
11611                            "Offset of Compilation Unit Info");
11612   dw2_asm_output_data (dwarf_offset_size, next_die_offset,
11613 		       "Compilation Unit Length");
11614 
11615   FOR_EACH_VEC_ELT (*names, i, pub)
11616     {
11617       if (include_pubname_in_output (names, pub))
11618 	{
11619 	  dw_offset die_offset = pub->die->die_offset;
11620 
11621           /* We shouldn't see pubnames for DIEs outside of the main CU.  */
11622           if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
11623             gcc_assert (pub->die->die_mark);
11624 
11625 	  /* If we're putting types in their own .debug_types sections,
11626 	     the .debug_pubtypes table will still point to the compile
11627 	     unit (not the type unit), so we want to use the offset of
11628 	     the skeleton DIE (if there is one).  */
11629 	  if (pub->die->comdat_type_p && names == pubtype_table)
11630 	    {
11631 	      comdat_type_node *type_node = pub->die->die_id.die_type_node;
11632 
11633 	      if (type_node != NULL)
11634 	        die_offset = (type_node->skeleton_die != NULL
11635 			      ? type_node->skeleton_die->die_offset
11636 			      : comp_unit_die ()->die_offset);
11637 	    }
11638 
11639           output_pubname (die_offset, pub);
11640 	}
11641     }
11642 
11643   dw2_asm_output_data (dwarf_offset_size, 0, NULL);
11644 }
11645 
11646 /* Output public names and types tables if necessary.  */
11647 
11648 static void
output_pubtables(void)11649 output_pubtables (void)
11650 {
11651   if (!want_pubnames () || !info_section_emitted)
11652     return;
11653 
11654   switch_to_section (debug_pubnames_section);
11655   output_pubnames (pubname_table);
11656   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
11657      It shouldn't hurt to emit it always, since pure DWARF2 consumers
11658      simply won't look for the section.  */
11659   switch_to_section (debug_pubtypes_section);
11660   output_pubnames (pubtype_table);
11661 }
11662 
11663 
11664 /* Output the information that goes into the .debug_aranges table.
11665    Namely, define the beginning and ending address range of the
11666    text section generated for this compilation unit.  */
11667 
11668 static void
output_aranges(void)11669 output_aranges (void)
11670 {
11671   unsigned i;
11672   unsigned long aranges_length = size_of_aranges ();
11673 
11674   if (!XCOFF_DEBUGGING_INFO)
11675     {
11676       if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
11677 	dw2_asm_output_data (4, 0xffffffff,
11678 	  "Initial length escape value indicating 64-bit DWARF extension");
11679       dw2_asm_output_data (dwarf_offset_size, aranges_length,
11680 			   "Length of Address Ranges Info");
11681     }
11682 
11683   /* Version number for aranges is still 2, even up to DWARF5.  */
11684   dw2_asm_output_data (2, 2, "DWARF aranges version");
11685   if (dwarf_split_debug_info)
11686     dw2_asm_output_offset (dwarf_offset_size, debug_skeleton_info_section_label,
11687                            debug_skeleton_info_section,
11688                            "Offset of Compilation Unit Info");
11689   else
11690     dw2_asm_output_offset (dwarf_offset_size, debug_info_section_label,
11691                            debug_info_section,
11692                            "Offset of Compilation Unit Info");
11693   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
11694   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
11695 
11696   /* We need to align to twice the pointer size here.  */
11697   if (DWARF_ARANGES_PAD_SIZE)
11698     {
11699       /* Pad using a 2 byte words so that padding is correct for any
11700 	 pointer size.  */
11701       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
11702 			   2 * DWARF2_ADDR_SIZE);
11703       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
11704 	dw2_asm_output_data (2, 0, NULL);
11705     }
11706 
11707   /* It is necessary not to output these entries if the sections were
11708      not used; if the sections were not used, the length will be 0 and
11709      the address may end up as 0 if the section is discarded by ld
11710      --gc-sections, leaving an invalid (0, 0) entry that can be
11711      confused with the terminator.  */
11712   if (text_section_used)
11713     {
11714       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
11715       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
11716 			    text_section_label, "Length");
11717     }
11718   if (cold_text_section_used)
11719     {
11720       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
11721 			   "Address");
11722       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
11723 			    cold_text_section_label, "Length");
11724     }
11725 
11726   if (have_multiple_function_sections)
11727     {
11728       unsigned fde_idx;
11729       dw_fde_ref fde;
11730 
11731       FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
11732 	{
11733 	  if (DECL_IGNORED_P (fde->decl))
11734 	    continue;
11735 	  if (!fde->in_std_section)
11736 	    {
11737 	      dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
11738 				   "Address");
11739 	      dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
11740 				    fde->dw_fde_begin, "Length");
11741 	    }
11742 	  if (fde->dw_fde_second_begin && !fde->second_in_std_section)
11743 	    {
11744 	      dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
11745 				   "Address");
11746 	      dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
11747 				    fde->dw_fde_second_begin, "Length");
11748 	    }
11749 	}
11750     }
11751 
11752   /* Output the terminator words.  */
11753   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11754   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11755 }
11756 
11757 /* Add a new entry to .debug_ranges.  Return its index into
11758    ranges_table vector.  */
11759 
11760 static unsigned int
add_ranges_num(int num,bool maybe_new_sec)11761 add_ranges_num (int num, bool maybe_new_sec)
11762 {
11763   dw_ranges r = { NULL, num, 0, maybe_new_sec, NULL, NULL };
11764   vec_safe_push (ranges_table, r);
11765   return vec_safe_length (ranges_table) - 1;
11766 }
11767 
11768 /* Add a new entry to .debug_ranges corresponding to a block, or a
11769    range terminator if BLOCK is NULL.  MAYBE_NEW_SEC is true if
11770    this entry might be in a different section from previous range.  */
11771 
11772 static unsigned int
add_ranges(const_tree block,bool maybe_new_sec)11773 add_ranges (const_tree block, bool maybe_new_sec)
11774 {
11775   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0, maybe_new_sec);
11776 }
11777 
11778 /* Note that (*rnglist_table)[offset] is either a head of a rnglist
11779    chain, or middle entry of a chain that will be directly referred to.  */
11780 
11781 static void
note_rnglist_head(unsigned int offset)11782 note_rnglist_head (unsigned int offset)
11783 {
11784   if (dwarf_version < 5 || (*ranges_table)[offset].label)
11785     return;
11786   (*ranges_table)[offset].label = gen_internal_sym ("LLRL");
11787 }
11788 
11789 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
11790    When using dwarf_split_debug_info, address attributes in dies destined
11791    for the final executable should be direct references--setting the
11792    parameter force_direct ensures this behavior.  */
11793 
11794 static void
add_ranges_by_labels(dw_die_ref die,const char * begin,const char * end,bool * added,bool force_direct)11795 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11796                       bool *added, bool force_direct)
11797 {
11798   unsigned int in_use = vec_safe_length (ranges_by_label);
11799   unsigned int offset;
11800   dw_ranges_by_label rbl = { begin, end };
11801   vec_safe_push (ranges_by_label, rbl);
11802   offset = add_ranges_num (-(int)in_use - 1, true);
11803   if (!*added)
11804     {
11805       add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
11806       *added = true;
11807       note_rnglist_head (offset);
11808       if (dwarf_split_debug_info && force_direct)
11809 	(*ranges_table)[offset].idx = DW_RANGES_IDX_SKELETON;
11810     }
11811 }
11812 
11813 /* Emit .debug_ranges section.  */
11814 
11815 static void
output_ranges(void)11816 output_ranges (void)
11817 {
11818   unsigned i;
11819   static const char *const start_fmt = "Offset %#x";
11820   const char *fmt = start_fmt;
11821   dw_ranges *r;
11822 
11823   switch_to_section (debug_ranges_section);
11824   ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11825   FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11826     {
11827       int block_num = r->num;
11828 
11829       if (block_num > 0)
11830 	{
11831 	  char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11832 	  char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11833 
11834 	  ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11835 	  ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11836 
11837 	  /* If all code is in the text section, then the compilation
11838 	     unit base address defaults to DW_AT_low_pc, which is the
11839 	     base of the text section.  */
11840 	  if (!have_multiple_function_sections)
11841 	    {
11842 	      dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11843 				    text_section_label,
11844 				    fmt, i * 2 * DWARF2_ADDR_SIZE);
11845 	      dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11846 				    text_section_label, NULL);
11847 	    }
11848 
11849 	  /* Otherwise, the compilation unit base address is zero,
11850 	     which allows us to use absolute addresses, and not worry
11851 	     about whether the target supports cross-section
11852 	     arithmetic.  */
11853 	  else
11854 	    {
11855 	      dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11856 				   fmt, i * 2 * DWARF2_ADDR_SIZE);
11857 	      dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11858 	    }
11859 
11860 	  fmt = NULL;
11861 	}
11862 
11863       /* Negative block_num stands for an index into ranges_by_label.  */
11864       else if (block_num < 0)
11865 	{
11866 	  int lab_idx = - block_num - 1;
11867 
11868 	  if (!have_multiple_function_sections)
11869 	    {
11870 	      gcc_unreachable ();
11871 #if 0
11872 	      /* If we ever use add_ranges_by_labels () for a single
11873 		 function section, all we have to do is to take out
11874 		 the #if 0 above.  */
11875 	      dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11876 				    (*ranges_by_label)[lab_idx].begin,
11877 				    text_section_label,
11878 				    fmt, i * 2 * DWARF2_ADDR_SIZE);
11879 	      dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11880 				    (*ranges_by_label)[lab_idx].end,
11881 				    text_section_label, NULL);
11882 #endif
11883 	    }
11884 	  else
11885 	    {
11886 	      dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11887 				   (*ranges_by_label)[lab_idx].begin,
11888 				   fmt, i * 2 * DWARF2_ADDR_SIZE);
11889 	      dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11890 				   (*ranges_by_label)[lab_idx].end,
11891 				   NULL);
11892 	    }
11893 	}
11894       else
11895 	{
11896 	  dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11897 	  dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11898 	  fmt = start_fmt;
11899 	}
11900     }
11901 }
11902 
11903 /* Non-zero if .debug_line_str should be used for .debug_line section
11904    strings or strings that are likely shareable with those.  */
11905 #define DWARF5_USE_DEBUG_LINE_STR \
11906   (!DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET		\
11907    && (DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) != 0		\
11908    /* FIXME: there is no .debug_line_str.dwo section,		\
11909       for -gsplit-dwarf we should use DW_FORM_strx instead.  */	\
11910    && !dwarf_split_debug_info)
11911 
11912 
11913 /* Returns TRUE if we are outputting DWARF5 and the assembler supports
11914    DWARF5 .debug_line tables using .debug_line_str or we generate
11915    it ourselves, except for split-dwarf which doesn't have a
11916    .debug_line_str.  */
11917 static bool
asm_outputs_debug_line_str(void)11918 asm_outputs_debug_line_str (void)
11919 {
11920   if (dwarf_version >= 5
11921       && ! output_asm_line_debug_info ()
11922       && DWARF5_USE_DEBUG_LINE_STR)
11923     return true;
11924   else
11925     {
11926 #if defined(HAVE_AS_GDWARF_5_DEBUG_FLAG) && defined(HAVE_AS_WORKING_DWARF_N_FLAG)
11927       return !dwarf_split_debug_info && dwarf_version >= 5;
11928 #else
11929       return false;
11930 #endif
11931     }
11932 }
11933 
11934 /* Return true if it is beneficial to use DW_RLE_base_address{,x}.
11935    I is index of the following range.  */
11936 
11937 static bool
use_distinct_base_address_for_range(unsigned int i)11938 use_distinct_base_address_for_range (unsigned int i)
11939 {
11940   if (i >= vec_safe_length (ranges_table))
11941     return false;
11942 
11943   dw_ranges *r2 = &(*ranges_table)[i];
11944   /* Use DW_RLE_base_address{,x} if there is a next range in the
11945      range list and is guaranteed to be in the same section.  */
11946   return r2->num != 0 && r2->label == NULL && !r2->maybe_new_sec;
11947 }
11948 
11949 /* Assign .debug_rnglists indexes and unique indexes into the debug_addr
11950    section when needed.  */
11951 
11952 static void
index_rnglists(void)11953 index_rnglists (void)
11954 {
11955   unsigned i;
11956   dw_ranges *r;
11957   bool base = false;
11958 
11959   FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11960     {
11961       if (r->label && r->idx != DW_RANGES_IDX_SKELETON)
11962 	r->idx = rnglist_idx++;
11963 
11964       if (!have_multiple_function_sections)
11965 	continue;
11966       int block_num = r->num;
11967       if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
11968 	base = false;
11969       if (block_num > 0)
11970 	{
11971 	  char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11972 	  char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11973 
11974 	  ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11975 	  ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11976 
11977 	  if (HAVE_AS_LEB128)
11978 	    {
11979 	      if (!base && use_distinct_base_address_for_range (i + 1))
11980 		{
11981 		  r->begin_entry = add_addr_table_entry (xstrdup (blabel),
11982 							 ate_kind_label);
11983 		  base = true;
11984 		}
11985 	      if (base)
11986 		/* If we have a base, no need for further
11987 		   begin_entry/end_entry, as DW_RLE_offset_pair will be
11988 		   used.  */
11989 		continue;
11990 	      r->begin_entry
11991 		= add_addr_table_entry (xstrdup (blabel), ate_kind_label);
11992 	      /* No need for end_entry, DW_RLE_start{,x}_length will use
11993 		 length as opposed to a pair of addresses.  */
11994 	    }
11995 	  else
11996 	    {
11997 	      r->begin_entry
11998 		= add_addr_table_entry (xstrdup (blabel), ate_kind_label);
11999 	      r->end_entry
12000 		= add_addr_table_entry (xstrdup (elabel), ate_kind_label);
12001 	    }
12002 	}
12003 
12004       /* Negative block_num stands for an index into ranges_by_label.  */
12005       else if (block_num < 0)
12006 	{
12007 	  int lab_idx = - block_num - 1;
12008 	  const char *blabel = (*ranges_by_label)[lab_idx].begin;
12009 	  const char *elabel = (*ranges_by_label)[lab_idx].end;
12010 
12011 	  r->begin_entry
12012 	    = add_addr_table_entry (xstrdup (blabel), ate_kind_label);
12013 	  if (!HAVE_AS_LEB128)
12014 	    r->end_entry
12015 	      = add_addr_table_entry (xstrdup (elabel), ate_kind_label);
12016 	}
12017     }
12018 }
12019 
12020 /* Emit .debug_rnglists or (when DWO is true) .debug_rnglists.dwo section.  */
12021 
12022 static bool
output_rnglists(unsigned generation,bool dwo)12023 output_rnglists (unsigned generation, bool dwo)
12024 {
12025   unsigned i;
12026   dw_ranges *r;
12027   char l1[MAX_ARTIFICIAL_LABEL_BYTES];
12028   char l2[MAX_ARTIFICIAL_LABEL_BYTES];
12029   char basebuf[MAX_ARTIFICIAL_LABEL_BYTES];
12030 
12031   if (dwo)
12032     switch_to_section (debug_ranges_dwo_section);
12033   else
12034     {
12035       switch_to_section (debug_ranges_section);
12036       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
12037     }
12038   /* There are up to 4 unique ranges labels per generation.
12039      See also init_sections_and_labels.  */
12040   ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_RANGES_SECTION_LABEL,
12041 			       2 + 2 * dwo + generation * 6);
12042   ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_RANGES_SECTION_LABEL,
12043 			       3 + 2 * dwo + generation * 6);
12044   if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
12045     dw2_asm_output_data (4, 0xffffffff,
12046 			 "Initial length escape value indicating "
12047 			 "64-bit DWARF extension");
12048   dw2_asm_output_delta (dwarf_offset_size, l2, l1,
12049 			"Length of Range Lists");
12050   ASM_OUTPUT_LABEL (asm_out_file, l1);
12051   output_dwarf_version ();
12052   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
12053   dw2_asm_output_data (1, 0, "Segment Size");
12054   /* Emit the offset table only for -gsplit-dwarf.  If we don't care
12055      about relocation sizes and primarily care about the size of .debug*
12056      sections in linked shared libraries and executables, then
12057      the offset table plus corresponding DW_FORM_rnglistx uleb128 indexes
12058      into it are usually larger than just DW_FORM_sec_offset offsets
12059      into the .debug_rnglists section.  */
12060   dw2_asm_output_data (4, dwo ? rnglist_idx : 0,
12061 		       "Offset Entry Count");
12062   if (dwo)
12063     {
12064       ASM_OUTPUT_LABEL (asm_out_file, ranges_base_label);
12065       FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
12066 	if (r->label && r->idx != DW_RANGES_IDX_SKELETON)
12067 	  dw2_asm_output_delta (dwarf_offset_size, r->label,
12068 				ranges_base_label, NULL);
12069     }
12070 
12071   const char *lab = "";
12072   const char *base = NULL;
12073   bool skipping = false;
12074   bool ret = false;
12075   FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
12076     {
12077       int block_num = r->num;
12078 
12079       if (r->label)
12080 	{
12081 	  if (dwarf_split_debug_info
12082 	      && (r->idx == DW_RANGES_IDX_SKELETON) == dwo)
12083 	    {
12084 	      ret = true;
12085 	      skipping = true;
12086 	      continue;
12087 	    }
12088 	  ASM_OUTPUT_LABEL (asm_out_file, r->label);
12089 	  lab = r->label;
12090 	}
12091       if (skipping)
12092 	{
12093 	  if (block_num == 0)
12094 	    skipping = false;
12095 	  continue;
12096 	}
12097       if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
12098 	base = NULL;
12099       if (block_num > 0)
12100 	{
12101 	  char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
12102 	  char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
12103 
12104 	  ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
12105 	  ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
12106 
12107 	  if (HAVE_AS_LEB128)
12108 	    {
12109 	      /* If all code is in the text section, then the compilation
12110 		 unit base address defaults to DW_AT_low_pc, which is the
12111 		 base of the text section.  */
12112 	      if (!have_multiple_function_sections)
12113 		{
12114 		  dw2_asm_output_data (1, DW_RLE_offset_pair,
12115 				       "DW_RLE_offset_pair (%s)", lab);
12116 		  dw2_asm_output_delta_uleb128 (blabel, text_section_label,
12117 						"Range begin address (%s)", lab);
12118 		  dw2_asm_output_delta_uleb128 (elabel, text_section_label,
12119 						"Range end address (%s)", lab);
12120 		  continue;
12121 		}
12122 	      if (base == NULL && use_distinct_base_address_for_range (i + 1))
12123 		{
12124 		  if (dwarf_split_debug_info)
12125 		    {
12126 		      dw2_asm_output_data (1, DW_RLE_base_addressx,
12127 					   "DW_RLE_base_addressx (%s)", lab);
12128 		      dw2_asm_output_data_uleb128 (r->begin_entry->index,
12129 						   "Base address index (%s)",
12130 						   blabel);
12131 		    }
12132 		  else
12133 		    {
12134 		      dw2_asm_output_data (1, DW_RLE_base_address,
12135 					   "DW_RLE_base_address (%s)", lab);
12136 		      dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12137 					   "Base address (%s)", lab);
12138 		    }
12139 		  strcpy (basebuf, blabel);
12140 		  base = basebuf;
12141 		}
12142 	      if (base)
12143 		{
12144 		  dw2_asm_output_data (1, DW_RLE_offset_pair,
12145 				       "DW_RLE_offset_pair (%s)", lab);
12146 		  dw2_asm_output_delta_uleb128 (blabel, base,
12147 						"Range begin address (%s)", lab);
12148 		  dw2_asm_output_delta_uleb128 (elabel, base,
12149 						"Range end address (%s)", lab);
12150 		  continue;
12151 		}
12152 	      if (dwarf_split_debug_info)
12153 		{
12154 		  dw2_asm_output_data (1, DW_RLE_startx_length,
12155 				       "DW_RLE_startx_length (%s)", lab);
12156 		  dw2_asm_output_data_uleb128 (r->begin_entry->index,
12157 					       "Range begin address index "
12158 					       "(%s)", blabel);
12159 		}
12160 	      else
12161 		{
12162 		  dw2_asm_output_data (1, DW_RLE_start_length,
12163 				       "DW_RLE_start_length (%s)", lab);
12164 		  dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12165 				       "Range begin address (%s)", lab);
12166 		}
12167 	      dw2_asm_output_delta_uleb128 (elabel, blabel,
12168 					    "Range length (%s)", lab);
12169 	    }
12170 	  else if (dwarf_split_debug_info)
12171 	    {
12172 	      dw2_asm_output_data (1, DW_RLE_startx_endx,
12173 				   "DW_RLE_startx_endx (%s)", lab);
12174 	      dw2_asm_output_data_uleb128 (r->begin_entry->index,
12175 					   "Range begin address index "
12176 					   "(%s)", blabel);
12177 	      dw2_asm_output_data_uleb128 (r->end_entry->index,
12178 					   "Range end address index "
12179 					   "(%s)", elabel);
12180 	    }
12181 	  else
12182 	    {
12183 	      dw2_asm_output_data (1, DW_RLE_start_end,
12184 				   "DW_RLE_start_end (%s)", lab);
12185 	      dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12186 				   "Range begin address (%s)", lab);
12187 	      dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
12188 				   "Range end address (%s)", lab);
12189 	    }
12190 	}
12191 
12192       /* Negative block_num stands for an index into ranges_by_label.  */
12193       else if (block_num < 0)
12194 	{
12195 	  int lab_idx = - block_num - 1;
12196 	  const char *blabel = (*ranges_by_label)[lab_idx].begin;
12197 	  const char *elabel = (*ranges_by_label)[lab_idx].end;
12198 
12199 	  if (!have_multiple_function_sections)
12200 	    gcc_unreachable ();
12201 	  if (HAVE_AS_LEB128)
12202 	    {
12203 	      if (dwarf_split_debug_info)
12204 		{
12205 		  dw2_asm_output_data (1, DW_RLE_startx_length,
12206 				       "DW_RLE_startx_length (%s)", lab);
12207 		  dw2_asm_output_data_uleb128 (r->begin_entry->index,
12208 					       "Range begin address index "
12209 					       "(%s)", blabel);
12210 		}
12211 	      else
12212 		{
12213 		  dw2_asm_output_data (1, DW_RLE_start_length,
12214 				       "DW_RLE_start_length (%s)", lab);
12215 		  dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12216 				       "Range begin address (%s)", lab);
12217 		}
12218 	      dw2_asm_output_delta_uleb128 (elabel, blabel,
12219 					    "Range length (%s)", lab);
12220 	    }
12221 	  else if (dwarf_split_debug_info)
12222 	    {
12223 	      dw2_asm_output_data (1, DW_RLE_startx_endx,
12224 				   "DW_RLE_startx_endx (%s)", lab);
12225 	      dw2_asm_output_data_uleb128 (r->begin_entry->index,
12226 					   "Range begin address index "
12227 					   "(%s)", blabel);
12228 	      dw2_asm_output_data_uleb128 (r->end_entry->index,
12229 					   "Range end address index "
12230 					   "(%s)", elabel);
12231 	    }
12232 	  else
12233 	    {
12234 	      dw2_asm_output_data (1, DW_RLE_start_end,
12235 				   "DW_RLE_start_end (%s)", lab);
12236 	      dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12237 				   "Range begin address (%s)", lab);
12238 	      dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
12239 				   "Range end address (%s)", lab);
12240 	    }
12241 	}
12242       else
12243 	dw2_asm_output_data (1, DW_RLE_end_of_list,
12244 			     "DW_RLE_end_of_list (%s)", lab);
12245     }
12246   ASM_OUTPUT_LABEL (asm_out_file, l2);
12247   return ret;
12248 }
12249 
12250 /* Data structure containing information about input files.  */
12251 struct file_info
12252 {
12253   const char *path;	/* Complete file name.  */
12254   const char *fname;	/* File name part.  */
12255   int length;		/* Length of entire string.  */
12256   struct dwarf_file_data * file_idx;	/* Index in input file table.  */
12257   int dir_idx;		/* Index in directory table.  */
12258 };
12259 
12260 /* Data structure containing information about directories with source
12261    files.  */
12262 struct dir_info
12263 {
12264   const char *path;	/* Path including directory name.  */
12265   int length;		/* Path length.  */
12266   int prefix;		/* Index of directory entry which is a prefix.  */
12267   int count;		/* Number of files in this directory.  */
12268   int dir_idx;		/* Index of directory used as base.  */
12269 };
12270 
12271 /* Callback function for file_info comparison.  We sort by looking at
12272    the directories in the path.  */
12273 
12274 static int
file_info_cmp(const void * p1,const void * p2)12275 file_info_cmp (const void *p1, const void *p2)
12276 {
12277   const struct file_info *const s1 = (const struct file_info *) p1;
12278   const struct file_info *const s2 = (const struct file_info *) p2;
12279   const unsigned char *cp1;
12280   const unsigned char *cp2;
12281 
12282   /* Take care of file names without directories.  We need to make sure that
12283      we return consistent values to qsort since some will get confused if
12284      we return the same value when identical operands are passed in opposite
12285      orders.  So if neither has a directory, return 0 and otherwise return
12286      1 or -1 depending on which one has the directory.  We want the one with
12287      the directory to sort after the one without, so all no directory files
12288      are at the start (normally only the compilation unit file).  */
12289   if ((s1->path == s1->fname || s2->path == s2->fname))
12290     return (s2->path == s2->fname) - (s1->path == s1->fname);
12291 
12292   cp1 = (const unsigned char *) s1->path;
12293   cp2 = (const unsigned char *) s2->path;
12294 
12295   while (1)
12296     {
12297       ++cp1;
12298       ++cp2;
12299       /* Reached the end of the first path?  If so, handle like above,
12300 	 but now we want longer directory prefixes before shorter ones.  */
12301       if ((cp1 == (const unsigned char *) s1->fname)
12302 	  || (cp2 == (const unsigned char *) s2->fname))
12303 	return ((cp1 == (const unsigned char *) s1->fname)
12304 		- (cp2 == (const unsigned char *) s2->fname));
12305 
12306       /* Character of current path component the same?  */
12307       else if (*cp1 != *cp2)
12308 	return *cp1 - *cp2;
12309     }
12310 }
12311 
12312 struct file_name_acquire_data
12313 {
12314   struct file_info *files;
12315   int used_files;
12316   int max_files;
12317 };
12318 
12319 /* Traversal function for the hash table.  */
12320 
12321 int
file_name_acquire(dwarf_file_data ** slot,file_name_acquire_data * fnad)12322 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
12323 {
12324   struct dwarf_file_data *d = *slot;
12325   struct file_info *fi;
12326   const char *f;
12327 
12328   gcc_assert (fnad->max_files >= d->emitted_number);
12329 
12330   if (! d->emitted_number)
12331     return 1;
12332 
12333   gcc_assert (fnad->max_files != fnad->used_files);
12334 
12335   fi = fnad->files + fnad->used_files++;
12336 
12337   f = remap_debug_filename (d->filename);
12338 
12339   /* Skip all leading "./".  */
12340   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
12341     f += 2;
12342 
12343   /* Create a new array entry.  */
12344   fi->path = f;
12345   fi->length = strlen (f);
12346   fi->file_idx = d;
12347 
12348   /* Search for the file name part.  */
12349   f = strrchr (f, DIR_SEPARATOR);
12350 #if defined (DIR_SEPARATOR_2)
12351   {
12352     const char *g = strrchr (fi->path, DIR_SEPARATOR_2);
12353 
12354     if (g != NULL)
12355       {
12356 	if (f == NULL || f < g)
12357 	  f = g;
12358       }
12359   }
12360 #endif
12361 
12362   fi->fname = f == NULL ? fi->path : f + 1;
12363   return 1;
12364 }
12365 
12366 /* Helper function for output_file_names.  Emit a FORM encoded
12367    string STR, with assembly comment start ENTRY_KIND and
12368    index IDX */
12369 
12370 static void
output_line_string(enum dwarf_form form,const char * str,const char * entry_kind,unsigned int idx)12371 output_line_string (enum dwarf_form form, const char *str,
12372 		    const char *entry_kind, unsigned int idx)
12373 {
12374   switch (form)
12375     {
12376     case DW_FORM_string:
12377       dw2_asm_output_nstring (str, -1, "%s: %#x", entry_kind, idx);
12378       break;
12379     case DW_FORM_line_strp:
12380       if (!debug_line_str_hash)
12381 	debug_line_str_hash
12382 	  = hash_table<indirect_string_hasher>::create_ggc (10);
12383 
12384       struct indirect_string_node *node;
12385       node = find_AT_string_in_table (str, debug_line_str_hash);
12386       set_indirect_string (node);
12387       node->form = form;
12388       dw2_asm_output_offset (dwarf_offset_size, node->label,
12389 			     debug_line_str_section, "%s: %#x: \"%s\"",
12390 			     entry_kind, 0, node->str);
12391       break;
12392     default:
12393       gcc_unreachable ();
12394     }
12395 }
12396 
12397 /* Output the directory table and the file name table.  We try to minimize
12398    the total amount of memory needed.  A heuristic is used to avoid large
12399    slowdowns with many input files.  */
12400 
12401 static void
output_file_names(void)12402 output_file_names (void)
12403 {
12404   struct file_name_acquire_data fnad;
12405   int numfiles;
12406   struct file_info *files;
12407   struct dir_info *dirs;
12408   int *saved;
12409   int *savehere;
12410   int *backmap;
12411   int ndirs;
12412   int idx_offset;
12413   int i;
12414 
12415   if (!last_emitted_file)
12416     {
12417       if (dwarf_version >= 5)
12418 	{
12419 	  const char *comp_dir = comp_dir_string ();
12420 	  if (comp_dir == NULL)
12421 	    comp_dir = "";
12422 	  dw2_asm_output_data (1, 1, "Directory entry format count");
12423 	  enum dwarf_form str_form = DW_FORM_string;
12424 	  if (DWARF5_USE_DEBUG_LINE_STR)
12425 	    str_form = DW_FORM_line_strp;
12426 	  dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12427 	  dw2_asm_output_data_uleb128 (str_form, "%s",
12428 				       get_DW_FORM_name (str_form));
12429 	  dw2_asm_output_data_uleb128 (1, "Directories count");
12430 	  if (str_form == DW_FORM_string)
12431 	    dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
12432 	  else
12433 	    output_line_string (str_form, comp_dir, "Directory Entry", 0);
12434 	  const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
12435 	  if (filename0 == NULL)
12436 	    filename0 = "";
12437 #ifdef VMS_DEBUGGING_INFO
12438 	  dw2_asm_output_data (1, 4, "File name entry format count");
12439 #else
12440 	  dw2_asm_output_data (1, 2, "File name entry format count");
12441 #endif
12442 	  dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12443 	  dw2_asm_output_data_uleb128 (str_form, "%s",
12444 				       get_DW_FORM_name (str_form));
12445 	  dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
12446 				       "DW_LNCT_directory_index");
12447 	  dw2_asm_output_data_uleb128 (DW_FORM_data1, "%s",
12448 				       get_DW_FORM_name (DW_FORM_data1));
12449 #ifdef VMS_DEBUGGING_INFO
12450 	  dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
12451 	  dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12452 	  dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
12453 	  dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12454 #endif
12455 	  dw2_asm_output_data_uleb128 (1, "File names count");
12456 
12457 	  output_line_string (str_form, filename0, "File Entry", 0);
12458 	  dw2_asm_output_data (1, 0, NULL);
12459 #ifdef VMS_DEBUGGING_INFO
12460 	  dw2_asm_output_data_uleb128 (0, NULL);
12461 	  dw2_asm_output_data_uleb128 (0, NULL);
12462 #endif
12463 	}
12464       else
12465 	{
12466 	  dw2_asm_output_data (1, 0, "End directory table");
12467 	  dw2_asm_output_data (1, 0, "End file name table");
12468 	}
12469       return;
12470     }
12471 
12472   numfiles = last_emitted_file->emitted_number;
12473 
12474   /* Allocate the various arrays we need.  */
12475   files = XALLOCAVEC (struct file_info, numfiles);
12476   dirs = XALLOCAVEC (struct dir_info, numfiles);
12477 
12478   fnad.files = files;
12479   fnad.used_files = 0;
12480   fnad.max_files = numfiles;
12481   file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
12482   gcc_assert (fnad.used_files == fnad.max_files);
12483 
12484   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
12485 
12486   /* Find all the different directories used.  */
12487   dirs[0].path = files[0].path;
12488   dirs[0].length = files[0].fname - files[0].path;
12489   dirs[0].prefix = -1;
12490   dirs[0].count = 1;
12491   dirs[0].dir_idx = 0;
12492   files[0].dir_idx = 0;
12493   ndirs = 1;
12494 
12495   for (i = 1; i < numfiles; i++)
12496     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
12497 	&& memcmp (dirs[ndirs - 1].path, files[i].path,
12498 		   dirs[ndirs - 1].length) == 0)
12499       {
12500 	/* Same directory as last entry.  */
12501 	files[i].dir_idx = ndirs - 1;
12502 	++dirs[ndirs - 1].count;
12503       }
12504     else
12505       {
12506 	int j;
12507 
12508 	/* This is a new directory.  */
12509 	dirs[ndirs].path = files[i].path;
12510 	dirs[ndirs].length = files[i].fname - files[i].path;
12511 	dirs[ndirs].count = 1;
12512 	dirs[ndirs].dir_idx = ndirs;
12513 	files[i].dir_idx = ndirs;
12514 
12515 	/* Search for a prefix.  */
12516 	dirs[ndirs].prefix = -1;
12517 	for (j = 0; j < ndirs; j++)
12518 	  if (dirs[j].length < dirs[ndirs].length
12519 	      && dirs[j].length > 1
12520 	      && (dirs[ndirs].prefix == -1
12521 		  || dirs[j].length > dirs[dirs[ndirs].prefix].length)
12522 	      && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
12523 	    dirs[ndirs].prefix = j;
12524 
12525 	++ndirs;
12526       }
12527 
12528   /* Now to the actual work.  We have to find a subset of the directories which
12529      allow expressing the file name using references to the directory table
12530      with the least amount of characters.  We do not do an exhaustive search
12531      where we would have to check out every combination of every single
12532      possible prefix.  Instead we use a heuristic which provides nearly optimal
12533      results in most cases and never is much off.  */
12534   saved = XALLOCAVEC (int, ndirs);
12535   savehere = XALLOCAVEC (int, ndirs);
12536 
12537   memset (saved, '\0', ndirs * sizeof (saved[0]));
12538   for (i = 0; i < ndirs; i++)
12539     {
12540       int j;
12541       int total;
12542 
12543       /* We can always save some space for the current directory.  But this
12544 	 does not mean it will be enough to justify adding the directory.  */
12545       savehere[i] = dirs[i].length;
12546       total = (savehere[i] - saved[i]) * dirs[i].count;
12547 
12548       for (j = i + 1; j < ndirs; j++)
12549 	{
12550 	  savehere[j] = 0;
12551 	  if (saved[j] < dirs[i].length)
12552 	    {
12553 	      /* Determine whether the dirs[i] path is a prefix of the
12554 		 dirs[j] path.  */
12555 	      int k;
12556 
12557 	      k = dirs[j].prefix;
12558 	      while (k != -1 && k != (int) i)
12559 		k = dirs[k].prefix;
12560 
12561 	      if (k == (int) i)
12562 		{
12563 		  /* Yes it is.  We can possibly save some memory by
12564 		     writing the filenames in dirs[j] relative to
12565 		     dirs[i].  */
12566 		  savehere[j] = dirs[i].length;
12567 		  total += (savehere[j] - saved[j]) * dirs[j].count;
12568 		}
12569 	    }
12570 	}
12571 
12572       /* Check whether we can save enough to justify adding the dirs[i]
12573 	 directory.  */
12574       if (total > dirs[i].length + 1)
12575 	{
12576 	  /* It's worthwhile adding.  */
12577 	  for (j = i; j < ndirs; j++)
12578 	    if (savehere[j] > 0)
12579 	      {
12580 		/* Remember how much we saved for this directory so far.  */
12581 		saved[j] = savehere[j];
12582 
12583 		/* Remember the prefix directory.  */
12584 		dirs[j].dir_idx = i;
12585 	      }
12586 	}
12587     }
12588 
12589   /* Emit the directory name table.  */
12590   idx_offset = dirs[0].length > 0 ? 1 : 0;
12591   enum dwarf_form str_form = DW_FORM_string;
12592   enum dwarf_form idx_form = DW_FORM_udata;
12593   if (dwarf_version >= 5)
12594     {
12595       const char *comp_dir = comp_dir_string ();
12596       if (comp_dir == NULL)
12597 	comp_dir = "";
12598       dw2_asm_output_data (1, 1, "Directory entry format count");
12599       if (DWARF5_USE_DEBUG_LINE_STR)
12600 	str_form = DW_FORM_line_strp;
12601       dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12602       dw2_asm_output_data_uleb128 (str_form, "%s",
12603 				   get_DW_FORM_name (str_form));
12604       dw2_asm_output_data_uleb128 (ndirs + idx_offset, "Directories count");
12605       if (str_form == DW_FORM_string)
12606 	{
12607 	  dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
12608 	  for (i = 1 - idx_offset; i < ndirs; i++)
12609 	    dw2_asm_output_nstring (dirs[i].path,
12610 				    dirs[i].length
12611 				    - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12612 				    "Directory Entry: %#x", i + idx_offset);
12613 	}
12614       else
12615 	{
12616 	  output_line_string (str_form, comp_dir, "Directory Entry", 0);
12617 	  for (i = 1 - idx_offset; i < ndirs; i++)
12618 	    {
12619 	      const char *str
12620 		= ggc_alloc_string (dirs[i].path,
12621 				    dirs[i].length
12622 				    - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR);
12623 	      output_line_string (str_form, str, "Directory Entry",
12624 				  (unsigned) i + idx_offset);
12625 	    }
12626 	}
12627     }
12628   else
12629     {
12630       for (i = 1 - idx_offset; i < ndirs; i++)
12631 	dw2_asm_output_nstring (dirs[i].path,
12632 				dirs[i].length
12633 				- !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12634 				"Directory Entry: %#x", i + idx_offset);
12635 
12636       dw2_asm_output_data (1, 0, "End directory table");
12637     }
12638 
12639   /* We have to emit them in the order of emitted_number since that's
12640      used in the debug info generation.  To do this efficiently we
12641      generate a back-mapping of the indices first.  */
12642   backmap = XALLOCAVEC (int, numfiles);
12643   for (i = 0; i < numfiles; i++)
12644     backmap[files[i].file_idx->emitted_number - 1] = i;
12645 
12646   if (dwarf_version >= 5)
12647     {
12648       const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
12649       if (filename0 == NULL)
12650 	filename0 = "";
12651       /* DW_LNCT_directory_index can use DW_FORM_udata, DW_FORM_data1 and
12652 	 DW_FORM_data2.  Choose one based on the number of directories
12653 	 and how much space would they occupy in each encoding.
12654 	 If we have at most 256 directories, all indexes fit into
12655 	 a single byte, so DW_FORM_data1 is most compact (if there
12656 	 are at most 128 directories, DW_FORM_udata would be as
12657 	 compact as that, but not shorter and slower to decode).  */
12658       if (ndirs + idx_offset <= 256)
12659 	idx_form = DW_FORM_data1;
12660       /* If there are more than 65536 directories, we have to use
12661 	 DW_FORM_udata, DW_FORM_data2 can't refer to them.
12662 	 Otherwise, compute what space would occupy if all the indexes
12663 	 used DW_FORM_udata - sum - and compare that to how large would
12664 	 be DW_FORM_data2 encoding, and pick the more efficient one.  */
12665       else if (ndirs + idx_offset <= 65536)
12666 	{
12667 	  unsigned HOST_WIDE_INT sum = 1;
12668 	  for (i = 0; i < numfiles; i++)
12669 	    {
12670 	      int file_idx = backmap[i];
12671 	      int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12672 	      sum += size_of_uleb128 (dir_idx);
12673 	    }
12674 	  if (sum >= HOST_WIDE_INT_UC (2) * (numfiles + 1))
12675 	    idx_form = DW_FORM_data2;
12676 	}
12677 #ifdef VMS_DEBUGGING_INFO
12678       dw2_asm_output_data (1, 4, "File name entry format count");
12679 #else
12680       dw2_asm_output_data (1, 2, "File name entry format count");
12681 #endif
12682       dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12683       dw2_asm_output_data_uleb128 (str_form, "%s",
12684 				   get_DW_FORM_name (str_form));
12685       dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
12686 				   "DW_LNCT_directory_index");
12687       dw2_asm_output_data_uleb128 (idx_form, "%s",
12688 				   get_DW_FORM_name (idx_form));
12689 #ifdef VMS_DEBUGGING_INFO
12690       dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
12691       dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12692       dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
12693       dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12694 #endif
12695       dw2_asm_output_data_uleb128 (numfiles + 1, "File names count");
12696 
12697       output_line_string (str_form, filename0, "File Entry", 0);
12698 
12699       /* Include directory index.  */
12700       if (idx_form != DW_FORM_udata)
12701 	dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12702 			     0, NULL);
12703       else
12704 	dw2_asm_output_data_uleb128 (0, NULL);
12705 
12706 #ifdef VMS_DEBUGGING_INFO
12707       dw2_asm_output_data_uleb128 (0, NULL);
12708       dw2_asm_output_data_uleb128 (0, NULL);
12709 #endif
12710     }
12711 
12712   /* Now write all the file names.  */
12713   for (i = 0; i < numfiles; i++)
12714     {
12715       int file_idx = backmap[i];
12716       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12717 
12718 #ifdef VMS_DEBUGGING_INFO
12719 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
12720 
12721       /* Setting these fields can lead to debugger miscomparisons,
12722          but VMS Debug requires them to be set correctly.  */
12723 
12724       int ver;
12725       long long cdt;
12726       long siz;
12727       int maxfilelen = (strlen (files[file_idx].path)
12728 			+ dirs[dir_idx].length
12729 			+ MAX_VMS_VERSION_LEN + 1);
12730       char *filebuf = XALLOCAVEC (char, maxfilelen);
12731 
12732       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
12733       snprintf (filebuf, maxfilelen, "%s;%d",
12734 	        files[file_idx].path + dirs[dir_idx].length, ver);
12735 
12736       output_line_string (str_form, filebuf, "File Entry", (unsigned) i + 1);
12737 
12738       /* Include directory index.  */
12739       if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
12740 	dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12741 			     dir_idx + idx_offset, NULL);
12742       else
12743 	dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12744 
12745       /* Modification time.  */
12746       dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
12747 							 &cdt, 0, 0, 0) == 0)
12748 				   ? cdt : 0, NULL);
12749 
12750       /* File length in bytes.  */
12751       dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
12752 							 0, &siz, 0, 0) == 0)
12753 				   ? siz : 0, NULL);
12754 #else
12755       output_line_string (str_form,
12756 			  files[file_idx].path + dirs[dir_idx].length,
12757 			  "File Entry", (unsigned) i + 1);
12758 
12759       /* Include directory index.  */
12760       if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
12761 	dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12762 			     dir_idx + idx_offset, NULL);
12763       else
12764 	dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12765 
12766       if (dwarf_version >= 5)
12767 	continue;
12768 
12769       /* Modification time.  */
12770       dw2_asm_output_data_uleb128 (0, NULL);
12771 
12772       /* File length in bytes.  */
12773       dw2_asm_output_data_uleb128 (0, NULL);
12774 #endif /* VMS_DEBUGGING_INFO */
12775     }
12776 
12777   if (dwarf_version < 5)
12778     dw2_asm_output_data (1, 0, "End file name table");
12779 }
12780 
12781 
12782 /* Output one line number table into the .debug_line section.  */
12783 
12784 static void
output_one_line_info_table(dw_line_info_table * table)12785 output_one_line_info_table (dw_line_info_table *table)
12786 {
12787   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12788   unsigned int current_line = 1;
12789   bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
12790   dw_line_info_entry *ent, *prev_addr;
12791   size_t i;
12792   unsigned int view;
12793 
12794   view = 0;
12795 
12796   FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
12797     {
12798       switch (ent->opcode)
12799 	{
12800 	case LI_set_address:
12801 	  /* ??? Unfortunately, we have little choice here currently, and
12802 	     must always use the most general form.  GCC does not know the
12803 	     address delta itself, so we can't use DW_LNS_advance_pc.  Many
12804 	     ports do have length attributes which will give an upper bound
12805 	     on the address range.  We could perhaps use length attributes
12806 	     to determine when it is safe to use DW_LNS_fixed_advance_pc.  */
12807 	  ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
12808 
12809 	  view = 0;
12810 
12811 	  /* This can handle any delta.  This takes
12812 	     4+DWARF2_ADDR_SIZE bytes.  */
12813 	  dw2_asm_output_data (1, 0, "set address %s%s", line_label,
12814 			       debug_variable_location_views
12815 			       ? ", reset view to 0" : "");
12816 	  dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12817 	  dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12818 	  dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12819 
12820 	  prev_addr = ent;
12821 	  break;
12822 
12823 	case LI_adv_address:
12824 	  {
12825 	    ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
12826 	    char prev_label[MAX_ARTIFICIAL_LABEL_BYTES];
12827 	    ASM_GENERATE_INTERNAL_LABEL (prev_label, LINE_CODE_LABEL, prev_addr->val);
12828 
12829 	    view++;
12830 
12831 	    dw2_asm_output_data (1, DW_LNS_fixed_advance_pc, "fixed advance PC, increment view to %i", view);
12832 	    dw2_asm_output_delta (2, line_label, prev_label,
12833 				  "from %s to %s", prev_label, line_label);
12834 
12835 	    prev_addr = ent;
12836 	    break;
12837 	  }
12838 
12839 	case LI_set_line:
12840 	  if (ent->val == current_line)
12841 	    {
12842 	      /* We still need to start a new row, so output a copy insn.  */
12843 	      dw2_asm_output_data (1, DW_LNS_copy,
12844 				   "copy line %u", current_line);
12845 	    }
12846 	  else
12847 	    {
12848 	      int line_offset = ent->val - current_line;
12849 	      int line_delta = line_offset - DWARF_LINE_BASE;
12850 
12851 	      current_line = ent->val;
12852 	      if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12853 		{
12854 		  /* This can handle deltas from -10 to 234, using the current
12855 		     definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
12856 		     This takes 1 byte.  */
12857 		  dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12858 				       "line %u", current_line);
12859 		}
12860 	      else
12861 		{
12862 		  /* This can handle any delta.  This takes at least 4 bytes,
12863 		     depending on the value being encoded.  */
12864 		  dw2_asm_output_data (1, DW_LNS_advance_line,
12865 				       "advance to line %u", current_line);
12866 		  dw2_asm_output_data_sleb128 (line_offset, NULL);
12867 		  dw2_asm_output_data (1, DW_LNS_copy, NULL);
12868 		}
12869 	    }
12870 	  break;
12871 
12872 	case LI_set_file:
12873 	  dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
12874 	  dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
12875 	  break;
12876 
12877 	case LI_set_column:
12878 	  dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
12879 	  dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
12880 	  break;
12881 
12882 	case LI_negate_stmt:
12883 	  current_is_stmt = !current_is_stmt;
12884 	  dw2_asm_output_data (1, DW_LNS_negate_stmt,
12885 			       "is_stmt %d", current_is_stmt);
12886 	  break;
12887 
12888 	case LI_set_prologue_end:
12889 	  dw2_asm_output_data (1, DW_LNS_set_prologue_end,
12890 			       "set prologue end");
12891 	  break;
12892 
12893 	case LI_set_epilogue_begin:
12894 	  dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
12895 			       "set epilogue begin");
12896 	  break;
12897 
12898 	case LI_set_discriminator:
12899 	  dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
12900 	  dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
12901 	  dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
12902 	  dw2_asm_output_data_uleb128 (ent->val, NULL);
12903 	  break;
12904 	}
12905     }
12906 
12907   /* Emit debug info for the address of the end of the table.  */
12908   dw2_asm_output_data (1, 0, "set address %s", table->end_label);
12909   dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12910   dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12911   dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
12912 
12913   dw2_asm_output_data (1, 0, "end sequence");
12914   dw2_asm_output_data_uleb128 (1, NULL);
12915   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12916 }
12917 
12918 static unsigned int output_line_info_generation;
12919 
12920 /* Output the source line number correspondence information.  This
12921    information goes into the .debug_line section.  */
12922 
12923 static void
output_line_info(bool prologue_only)12924 output_line_info (bool prologue_only)
12925 {
12926   char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
12927   char p1[MAX_ARTIFICIAL_LABEL_BYTES], p2[MAX_ARTIFICIAL_LABEL_BYTES];
12928   bool saw_one = false;
12929   int opc;
12930 
12931   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL,
12932 			       output_line_info_generation);
12933   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL,
12934 			       output_line_info_generation);
12935   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL,
12936 			       output_line_info_generation);
12937   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL,
12938 			       output_line_info_generation++);
12939 
12940   if (!XCOFF_DEBUGGING_INFO)
12941     {
12942       if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
12943 	dw2_asm_output_data (4, 0xffffffff,
12944 	  "Initial length escape value indicating 64-bit DWARF extension");
12945       dw2_asm_output_delta (dwarf_offset_size, l2, l1,
12946 			    "Length of Source Line Info");
12947     }
12948 
12949   ASM_OUTPUT_LABEL (asm_out_file, l1);
12950 
12951   output_dwarf_version ();
12952   if (dwarf_version >= 5)
12953     {
12954       dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
12955       dw2_asm_output_data (1, 0, "Segment Size");
12956     }
12957   dw2_asm_output_delta (dwarf_offset_size, p2, p1, "Prolog Length");
12958   ASM_OUTPUT_LABEL (asm_out_file, p1);
12959 
12960   /* Define the architecture-dependent minimum instruction length (in bytes).
12961      In this implementation of DWARF, this field is used for information
12962      purposes only.  Since GCC generates assembly language, we have no
12963      a priori knowledge of how many instruction bytes are generated for each
12964      source line, and therefore can use only the DW_LNE_set_address and
12965      DW_LNS_fixed_advance_pc line information commands.  Accordingly, we fix
12966      this as '1', which is "correct enough" for all architectures,
12967      and don't let the target override.  */
12968   dw2_asm_output_data (1, 1, "Minimum Instruction Length");
12969 
12970   if (dwarf_version >= 4)
12971     dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
12972 			 "Maximum Operations Per Instruction");
12973   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
12974 		       "Default is_stmt_start flag");
12975   dw2_asm_output_data (1, DWARF_LINE_BASE,
12976 		       "Line Base Value (Special Opcodes)");
12977   dw2_asm_output_data (1, DWARF_LINE_RANGE,
12978 		       "Line Range Value (Special Opcodes)");
12979   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
12980 		       "Special Opcode Base");
12981 
12982   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
12983     {
12984       int n_op_args;
12985       switch (opc)
12986 	{
12987 	case DW_LNS_advance_pc:
12988 	case DW_LNS_advance_line:
12989 	case DW_LNS_set_file:
12990 	case DW_LNS_set_column:
12991 	case DW_LNS_fixed_advance_pc:
12992 	case DW_LNS_set_isa:
12993 	  n_op_args = 1;
12994 	  break;
12995 	default:
12996 	  n_op_args = 0;
12997 	  break;
12998 	}
12999 
13000       dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
13001 			   opc, n_op_args);
13002     }
13003 
13004   /* Write out the information about the files we use.  */
13005   output_file_names ();
13006   ASM_OUTPUT_LABEL (asm_out_file, p2);
13007   if (prologue_only)
13008     {
13009       /* Output the marker for the end of the line number info.  */
13010       ASM_OUTPUT_LABEL (asm_out_file, l2);
13011       return;
13012     }
13013 
13014   if (separate_line_info)
13015     {
13016       dw_line_info_table *table;
13017       size_t i;
13018 
13019       FOR_EACH_VEC_ELT (*separate_line_info, i, table)
13020 	if (table->in_use)
13021 	  {
13022 	    output_one_line_info_table (table);
13023 	    saw_one = true;
13024 	  }
13025     }
13026   if (cold_text_section_line_info && cold_text_section_line_info->in_use)
13027     {
13028       output_one_line_info_table (cold_text_section_line_info);
13029       saw_one = true;
13030     }
13031 
13032   /* ??? Some Darwin linkers crash on a .debug_line section with no
13033      sequences.  Further, merely a DW_LNE_end_sequence entry is not
13034      sufficient -- the address column must also be initialized.
13035      Make sure to output at least one set_address/end_sequence pair,
13036      choosing .text since that section is always present.  */
13037   if (text_section_line_info->in_use || !saw_one)
13038     output_one_line_info_table (text_section_line_info);
13039 
13040   /* Output the marker for the end of the line number info.  */
13041   ASM_OUTPUT_LABEL (asm_out_file, l2);
13042 }
13043 
13044 /* Return true if DW_AT_endianity should be emitted according to REVERSE.  */
13045 
13046 static inline bool
need_endianity_attribute_p(bool reverse)13047 need_endianity_attribute_p (bool reverse)
13048 {
13049   return reverse && (dwarf_version >= 3 || !dwarf_strict);
13050 }
13051 
13052 /* Given a pointer to a tree node for some base type, return a pointer to
13053    a DIE that describes the given type.  REVERSE is true if the type is
13054    to be interpreted in the reverse storage order wrt the target order.
13055 
13056    This routine must only be called for GCC type nodes that correspond to
13057    Dwarf base (fundamental) types.  */
13058 
13059 static dw_die_ref
base_type_die(tree type,bool reverse)13060 base_type_die (tree type, bool reverse)
13061 {
13062   dw_die_ref base_type_result;
13063   enum dwarf_type encoding;
13064   bool fpt_used = false;
13065   struct fixed_point_type_info fpt_info;
13066   tree type_bias = NULL_TREE;
13067 
13068   /* If this is a subtype that should not be emitted as a subrange type,
13069      use the base type.  See subrange_type_for_debug_p.  */
13070   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
13071     type = TREE_TYPE (type);
13072 
13073   switch (TREE_CODE (type))
13074     {
13075     case INTEGER_TYPE:
13076       if ((dwarf_version >= 4 || !dwarf_strict)
13077 	  && TYPE_NAME (type)
13078 	  && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
13079 	  && DECL_IS_UNDECLARED_BUILTIN (TYPE_NAME (type))
13080 	  && DECL_NAME (TYPE_NAME (type)))
13081 	{
13082 	  const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
13083 	  if (strcmp (name, "char16_t") == 0
13084 	      || strcmp (name, "char32_t") == 0)
13085 	    {
13086 	      encoding = DW_ATE_UTF;
13087 	      break;
13088 	    }
13089 	}
13090       if ((dwarf_version >= 3 || !dwarf_strict)
13091 	  && lang_hooks.types.get_fixed_point_type_info)
13092 	{
13093 	  memset (&fpt_info, 0, sizeof (fpt_info));
13094 	  if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
13095 	    {
13096 	      fpt_used = true;
13097 	      encoding = ((TYPE_UNSIGNED (type))
13098 			  ? DW_ATE_unsigned_fixed
13099 			  : DW_ATE_signed_fixed);
13100 	      break;
13101 	    }
13102 	}
13103       if (TYPE_STRING_FLAG (type))
13104 	{
13105 	  if (TYPE_UNSIGNED (type))
13106 	    encoding = DW_ATE_unsigned_char;
13107 	  else
13108 	    encoding = DW_ATE_signed_char;
13109 	}
13110       else if (TYPE_UNSIGNED (type))
13111 	encoding = DW_ATE_unsigned;
13112       else
13113 	encoding = DW_ATE_signed;
13114 
13115       if (!dwarf_strict
13116 	  && lang_hooks.types.get_type_bias)
13117 	type_bias = lang_hooks.types.get_type_bias (type);
13118       break;
13119 
13120     case REAL_TYPE:
13121       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
13122 	{
13123 	  if (dwarf_version >= 3 || !dwarf_strict)
13124 	    encoding = DW_ATE_decimal_float;
13125 	  else
13126 	    encoding = DW_ATE_lo_user;
13127 	}
13128       else
13129 	encoding = DW_ATE_float;
13130       break;
13131 
13132     case FIXED_POINT_TYPE:
13133       if (!(dwarf_version >= 3 || !dwarf_strict))
13134 	encoding = DW_ATE_lo_user;
13135       else if (TYPE_UNSIGNED (type))
13136 	encoding = DW_ATE_unsigned_fixed;
13137       else
13138 	encoding = DW_ATE_signed_fixed;
13139       break;
13140 
13141       /* Dwarf2 doesn't know anything about complex ints, so use
13142 	 a user defined type for it.  */
13143     case COMPLEX_TYPE:
13144       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
13145 	encoding = DW_ATE_complex_float;
13146       else
13147 	encoding = DW_ATE_lo_user;
13148       break;
13149 
13150     case BOOLEAN_TYPE:
13151       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
13152       encoding = DW_ATE_boolean;
13153       break;
13154 
13155     default:
13156       /* No other TREE_CODEs are Dwarf fundamental types.  */
13157       gcc_unreachable ();
13158     }
13159 
13160   base_type_result = new_die_raw (DW_TAG_base_type);
13161 
13162   add_AT_unsigned (base_type_result, DW_AT_byte_size,
13163 		   int_size_in_bytes (type));
13164   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
13165 
13166   if (need_endianity_attribute_p (reverse))
13167     add_AT_unsigned (base_type_result, DW_AT_endianity,
13168 		     BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
13169 
13170   add_alignment_attribute (base_type_result, type);
13171 
13172   if (fpt_used)
13173     {
13174       switch (fpt_info.scale_factor_kind)
13175 	{
13176 	case fixed_point_scale_factor_binary:
13177 	  add_AT_int (base_type_result, DW_AT_binary_scale,
13178 		      fpt_info.scale_factor.binary);
13179 	  break;
13180 
13181 	case fixed_point_scale_factor_decimal:
13182 	  add_AT_int (base_type_result, DW_AT_decimal_scale,
13183 		      fpt_info.scale_factor.decimal);
13184 	  break;
13185 
13186 	case fixed_point_scale_factor_arbitrary:
13187 	  /* Arbitrary scale factors cannot be described in standard DWARF.  */
13188 	  if (!dwarf_strict)
13189 	    {
13190 	      /* Describe the scale factor as a rational constant.  */
13191 	      const dw_die_ref scale_factor
13192 		= new_die (DW_TAG_constant, comp_unit_die (), type);
13193 
13194 	      add_scalar_info (scale_factor, DW_AT_GNU_numerator,
13195 			       fpt_info.scale_factor.arbitrary.numerator,
13196 			       dw_scalar_form_constant, NULL);
13197 	      add_scalar_info (scale_factor, DW_AT_GNU_denominator,
13198 			       fpt_info.scale_factor.arbitrary.denominator,
13199 			       dw_scalar_form_constant, NULL);
13200 
13201 	      add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
13202 	    }
13203 	  break;
13204 
13205 	default:
13206 	  gcc_unreachable ();
13207 	}
13208     }
13209 
13210   if (type_bias)
13211     add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
13212 		     dw_scalar_form_constant
13213 		     | dw_scalar_form_exprloc
13214 		     | dw_scalar_form_reference,
13215 		     NULL);
13216 
13217   return base_type_result;
13218 }
13219 
13220 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
13221    named 'auto' in its type: return true for it, false otherwise.  */
13222 
13223 static inline bool
is_cxx_auto(tree type)13224 is_cxx_auto (tree type)
13225 {
13226   if (is_cxx ())
13227     {
13228       tree name = TYPE_IDENTIFIER (type);
13229       if (name == get_identifier ("auto")
13230 	  || name == get_identifier ("decltype(auto)"))
13231 	return true;
13232     }
13233   return false;
13234 }
13235 
13236 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
13237    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
13238 
13239 static inline int
is_base_type(tree type)13240 is_base_type (tree type)
13241 {
13242   switch (TREE_CODE (type))
13243     {
13244     case INTEGER_TYPE:
13245     case REAL_TYPE:
13246     case FIXED_POINT_TYPE:
13247     case COMPLEX_TYPE:
13248     case BOOLEAN_TYPE:
13249       return 1;
13250 
13251     case VOID_TYPE:
13252     case OPAQUE_TYPE:
13253     case ARRAY_TYPE:
13254     case RECORD_TYPE:
13255     case UNION_TYPE:
13256     case QUAL_UNION_TYPE:
13257     case ENUMERAL_TYPE:
13258     case FUNCTION_TYPE:
13259     case METHOD_TYPE:
13260     case POINTER_TYPE:
13261     case REFERENCE_TYPE:
13262     case NULLPTR_TYPE:
13263     case OFFSET_TYPE:
13264     case LANG_TYPE:
13265     case VECTOR_TYPE:
13266       return 0;
13267 
13268     default:
13269       if (is_cxx_auto (type))
13270 	return 0;
13271       gcc_unreachable ();
13272     }
13273 
13274   return 0;
13275 }
13276 
13277 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
13278    node, return the size in bits for the type if it is a constant, or else
13279    return the alignment for the type if the type's size is not constant, or
13280    else return BITS_PER_WORD if the type actually turns out to be an
13281    ERROR_MARK node.  */
13282 
13283 static inline unsigned HOST_WIDE_INT
simple_type_size_in_bits(const_tree type)13284 simple_type_size_in_bits (const_tree type)
13285 {
13286   if (TREE_CODE (type) == ERROR_MARK)
13287     return BITS_PER_WORD;
13288   else if (TYPE_SIZE (type) == NULL_TREE)
13289     return 0;
13290   else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
13291     return tree_to_uhwi (TYPE_SIZE (type));
13292   else
13293     return TYPE_ALIGN (type);
13294 }
13295 
13296 /* Similarly, but return an offset_int instead of UHWI.  */
13297 
13298 static inline offset_int
offset_int_type_size_in_bits(const_tree type)13299 offset_int_type_size_in_bits (const_tree type)
13300 {
13301   if (TREE_CODE (type) == ERROR_MARK)
13302     return BITS_PER_WORD;
13303   else if (TYPE_SIZE (type) == NULL_TREE)
13304     return 0;
13305   else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
13306     return wi::to_offset (TYPE_SIZE (type));
13307   else
13308     return TYPE_ALIGN (type);
13309 }
13310 
13311 /*  Given a pointer to a tree node for a subrange type, return a pointer
13312     to a DIE that describes the given type.  */
13313 
13314 static dw_die_ref
subrange_type_die(tree type,tree low,tree high,tree bias,dw_die_ref context_die)13315 subrange_type_die (tree type, tree low, tree high, tree bias,
13316 		   dw_die_ref context_die)
13317 {
13318   dw_die_ref subrange_die;
13319   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
13320 
13321   if (context_die == NULL)
13322     context_die = comp_unit_die ();
13323 
13324   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
13325 
13326   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
13327     {
13328       /* The size of the subrange type and its base type do not match,
13329 	 so we need to generate a size attribute for the subrange type.  */
13330       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
13331     }
13332 
13333   add_alignment_attribute (subrange_die, type);
13334 
13335   if (low)
13336     add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
13337   if (high)
13338     add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
13339   if (bias && !dwarf_strict)
13340     add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
13341 		     dw_scalar_form_constant
13342 		     | dw_scalar_form_exprloc
13343 		     | dw_scalar_form_reference,
13344 		     NULL);
13345 
13346   return subrange_die;
13347 }
13348 
13349 /* Returns the (const and/or volatile) cv_qualifiers associated with
13350    the decl node.  This will normally be augmented with the
13351    cv_qualifiers of the underlying type in add_type_attribute.  */
13352 
13353 static int
decl_quals(const_tree decl)13354 decl_quals (const_tree decl)
13355 {
13356   return ((TREE_READONLY (decl)
13357 	   /* The C++ front-end correctly marks reference-typed
13358 	      variables as readonly, but from a language (and debug
13359 	      info) standpoint they are not const-qualified.  */
13360 	   && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
13361 	   ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
13362 	  | (TREE_THIS_VOLATILE (decl)
13363 	     ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
13364 }
13365 
13366 /* Determine the TYPE whose qualifiers match the largest strict subset
13367    of the given TYPE_QUALS, and return its qualifiers.  Ignore all
13368    qualifiers outside QUAL_MASK.  */
13369 
13370 static int
get_nearest_type_subqualifiers(tree type,int type_quals,int qual_mask)13371 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
13372 {
13373   tree t;
13374   int best_rank = 0, best_qual = 0, max_rank;
13375 
13376   type_quals &= qual_mask;
13377   max_rank = popcount_hwi (type_quals) - 1;
13378 
13379   for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
13380        t = TYPE_NEXT_VARIANT (t))
13381     {
13382       int q = TYPE_QUALS (t) & qual_mask;
13383 
13384       if ((q & type_quals) == q && q != type_quals
13385 	  && check_base_type (t, type))
13386 	{
13387 	  int rank = popcount_hwi (q);
13388 
13389 	  if (rank > best_rank)
13390 	    {
13391 	      best_rank = rank;
13392 	      best_qual = q;
13393 	    }
13394 	}
13395     }
13396 
13397   return best_qual;
13398 }
13399 
13400 struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
13401 static const dwarf_qual_info_t dwarf_qual_info[] =
13402 {
13403   { TYPE_QUAL_CONST, DW_TAG_const_type },
13404   { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
13405   { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
13406   { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
13407 };
13408 static const unsigned int dwarf_qual_info_size
13409   = sizeof (dwarf_qual_info) / sizeof (dwarf_qual_info[0]);
13410 
13411 /* If DIE is a qualified DIE of some base DIE with the same parent,
13412    return the base DIE, otherwise return NULL.  Set MASK to the
13413    qualifiers added compared to the returned DIE.  */
13414 
13415 static dw_die_ref
qualified_die_p(dw_die_ref die,int * mask,unsigned int depth)13416 qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
13417 {
13418   unsigned int i;
13419   for (i = 0; i < dwarf_qual_info_size; i++)
13420     if (die->die_tag == dwarf_qual_info[i].t)
13421       break;
13422   if (i == dwarf_qual_info_size)
13423     return NULL;
13424   if (vec_safe_length (die->die_attr) != 1)
13425     return NULL;
13426   dw_die_ref type = get_AT_ref (die, DW_AT_type);
13427   if (type == NULL || type->die_parent != die->die_parent)
13428     return NULL;
13429   *mask |= dwarf_qual_info[i].q;
13430   if (depth)
13431     {
13432       dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
13433       if (ret)
13434 	return ret;
13435     }
13436   return type;
13437 }
13438 
13439 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
13440    entry that chains the modifiers specified by CV_QUALS in front of the
13441    given type.  REVERSE is true if the type is to be interpreted in the
13442    reverse storage order wrt the target order.  */
13443 
13444 static dw_die_ref
modified_type_die(tree type,int cv_quals,bool reverse,dw_die_ref context_die)13445 modified_type_die (tree type, int cv_quals, bool reverse,
13446 		   dw_die_ref context_die)
13447 {
13448   enum tree_code code = TREE_CODE (type);
13449   dw_die_ref mod_type_die;
13450   dw_die_ref sub_die = NULL;
13451   tree item_type = NULL;
13452   tree qualified_type;
13453   tree name, low, high;
13454   dw_die_ref mod_scope;
13455   /* Only these cv-qualifiers are currently handled.  */
13456   const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
13457 			    | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC |
13458 			    ENCODE_QUAL_ADDR_SPACE(~0U));
13459   const bool reverse_base_type
13460     = need_endianity_attribute_p (reverse) && is_base_type (type);
13461 
13462   if (code == ERROR_MARK)
13463     return NULL;
13464 
13465   if (lang_hooks.types.get_debug_type)
13466     {
13467       tree debug_type = lang_hooks.types.get_debug_type (type);
13468 
13469       if (debug_type != NULL_TREE && debug_type != type)
13470 	return modified_type_die (debug_type, cv_quals, reverse, context_die);
13471     }
13472 
13473   cv_quals &= cv_qual_mask;
13474 
13475   /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
13476      tag modifier (and not an attribute) old consumers won't be able
13477      to handle it.  */
13478   if (dwarf_version < 3)
13479     cv_quals &= ~TYPE_QUAL_RESTRICT;
13480 
13481   /* Likewise for DW_TAG_atomic_type for DWARFv5.  */
13482   if (dwarf_version < 5)
13483     cv_quals &= ~TYPE_QUAL_ATOMIC;
13484 
13485   /* See if we already have the appropriately qualified variant of
13486      this type.  */
13487   qualified_type = get_qualified_type (type, cv_quals);
13488 
13489   if (qualified_type == sizetype)
13490     {
13491       /* Try not to expose the internal sizetype type's name.  */
13492       if (TYPE_NAME (qualified_type)
13493 	  && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
13494 	{
13495 	  tree t = TREE_TYPE (TYPE_NAME (qualified_type));
13496 
13497 	  gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
13498 			       && (TYPE_PRECISION (t)
13499 				   == TYPE_PRECISION (qualified_type))
13500 			       && (TYPE_UNSIGNED (t)
13501 				   == TYPE_UNSIGNED (qualified_type)));
13502 	  qualified_type = t;
13503 	}
13504       else if (qualified_type == sizetype
13505 	       && TREE_CODE (sizetype) == TREE_CODE (size_type_node)
13506 	       && TYPE_PRECISION (sizetype) == TYPE_PRECISION (size_type_node)
13507 	       && TYPE_UNSIGNED (sizetype) == TYPE_UNSIGNED (size_type_node))
13508 	qualified_type = size_type_node;
13509       if (type == sizetype)
13510 	type = qualified_type;
13511     }
13512 
13513   /* If we do, then we can just use its DIE, if it exists.  */
13514   if (qualified_type)
13515     {
13516       mod_type_die = lookup_type_die (qualified_type);
13517 
13518       /* DW_AT_endianity doesn't come from a qualifier on the type, so it is
13519 	 dealt with specially: the DIE with the attribute, if it exists, is
13520 	 placed immediately after the regular DIE for the same base type.  */
13521       if (mod_type_die
13522 	  && (!reverse_base_type
13523 	      || ((mod_type_die = mod_type_die->die_sib) != NULL
13524 		  && get_AT_unsigned (mod_type_die, DW_AT_endianity))))
13525 	return mod_type_die;
13526     }
13527 
13528   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
13529 
13530   /* Handle C typedef types.  */
13531   if (name
13532       && TREE_CODE (name) == TYPE_DECL
13533       && DECL_ORIGINAL_TYPE (name)
13534       && !DECL_ARTIFICIAL (name))
13535     {
13536       tree dtype = TREE_TYPE (name);
13537 
13538       /* Skip the typedef for base types with DW_AT_endianity, no big deal.  */
13539       if (qualified_type == dtype && !reverse_base_type)
13540 	{
13541 	  tree origin = decl_ultimate_origin (name);
13542 
13543 	  /* Typedef variants that have an abstract origin don't get their own
13544 	     type DIE (see gen_typedef_die), so fall back on the ultimate
13545 	     abstract origin instead.  */
13546 	  if (origin != NULL && origin != name)
13547 	    return modified_type_die (TREE_TYPE (origin), cv_quals, reverse,
13548 				      context_die);
13549 
13550 	  /* For a named type, use the typedef.  */
13551 	  gen_type_die (qualified_type, context_die);
13552 	  return lookup_type_die (qualified_type);
13553 	}
13554       else
13555 	{
13556 	  int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
13557 	  dquals &= cv_qual_mask;
13558 	  if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
13559 	      || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
13560 	    /* cv-unqualified version of named type.  Just use
13561 	       the unnamed type to which it refers.  */
13562 	    return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
13563 				      reverse, context_die);
13564 	  /* Else cv-qualified version of named type; fall through.  */
13565 	}
13566     }
13567 
13568   mod_scope = scope_die_for (type, context_die);
13569 
13570   if (cv_quals)
13571     {
13572       int sub_quals = 0, first_quals = 0;
13573       unsigned i;
13574       dw_die_ref first = NULL, last = NULL;
13575 
13576       /* Determine a lesser qualified type that most closely matches
13577 	 this one.  Then generate DW_TAG_* entries for the remaining
13578 	 qualifiers.  */
13579       sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
13580 						  cv_qual_mask);
13581       if (sub_quals && use_debug_types)
13582 	{
13583 	  bool needed = false;
13584 	  /* If emitting type units, make sure the order of qualifiers
13585 	     is canonical.  Thus, start from unqualified type if
13586 	     an earlier qualifier is missing in sub_quals, but some later
13587 	     one is present there.  */
13588 	  for (i = 0; i < dwarf_qual_info_size; i++)
13589 	    if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
13590 	      needed = true;
13591 	    else if (needed && (dwarf_qual_info[i].q & cv_quals))
13592 	      {
13593 		sub_quals = 0;
13594 		break;
13595 	      }
13596 	}
13597       mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
13598       if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
13599 	{
13600 	  /* As not all intermediate qualified DIEs have corresponding
13601 	     tree types, ensure that qualified DIEs in the same scope
13602 	     as their DW_AT_type are emitted after their DW_AT_type,
13603 	     only with other qualified DIEs for the same type possibly
13604 	     in between them.  Determine the range of such qualified
13605 	     DIEs now (first being the base type, last being corresponding
13606 	     last qualified DIE for it).  */
13607 	  unsigned int count = 0;
13608 	  first = qualified_die_p (mod_type_die, &first_quals,
13609 				   dwarf_qual_info_size);
13610 	  if (first == NULL)
13611 	    first = mod_type_die;
13612 	  gcc_assert ((first_quals & ~sub_quals) == 0);
13613 	  for (count = 0, last = first;
13614 	       count < (1U << dwarf_qual_info_size);
13615 	       count++, last = last->die_sib)
13616 	    {
13617 	      int quals = 0;
13618 	      if (last == mod_scope->die_child)
13619 		break;
13620 	      if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
13621 		  != first)
13622 		break;
13623 	    }
13624 	}
13625 
13626       for (i = 0; i < dwarf_qual_info_size; i++)
13627 	if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
13628 	  {
13629 	    dw_die_ref d;
13630 	    if (first && first != last)
13631 	      {
13632 		for (d = first->die_sib; ; d = d->die_sib)
13633 		  {
13634 		    int quals = 0;
13635 		    qualified_die_p (d, &quals, dwarf_qual_info_size);
13636 		    if (quals == (first_quals | dwarf_qual_info[i].q))
13637 		      break;
13638 		    if (d == last)
13639 		      {
13640 			d = NULL;
13641 			break;
13642 		      }
13643 		  }
13644 		if (d)
13645 		  {
13646 		    mod_type_die = d;
13647 		    continue;
13648 		  }
13649 	      }
13650 	    if (first)
13651 	      {
13652 		d = new_die_raw (dwarf_qual_info[i].t);
13653 		add_child_die_after (mod_scope, d, last);
13654 		last = d;
13655 	      }
13656 	    else
13657 	      d = new_die (dwarf_qual_info[i].t, mod_scope, type);
13658 	    if (mod_type_die)
13659 	      add_AT_die_ref (d, DW_AT_type, mod_type_die);
13660 	    mod_type_die = d;
13661 	    first_quals |= dwarf_qual_info[i].q;
13662 	  }
13663     }
13664   else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
13665     {
13666       dwarf_tag tag = DW_TAG_pointer_type;
13667       if (code == REFERENCE_TYPE)
13668 	{
13669 	  if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
13670 	    tag = DW_TAG_rvalue_reference_type;
13671 	  else
13672 	    tag = DW_TAG_reference_type;
13673 	}
13674       mod_type_die = new_die (tag, mod_scope, type);
13675 
13676       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
13677 		       simple_type_size_in_bits (type) / BITS_PER_UNIT);
13678       add_alignment_attribute (mod_type_die, type);
13679       item_type = TREE_TYPE (type);
13680 
13681       addr_space_t as = TYPE_ADDR_SPACE (item_type);
13682       if (!ADDR_SPACE_GENERIC_P (as))
13683 	{
13684 	  int action = targetm.addr_space.debug (as);
13685 	  if (action >= 0)
13686 	    {
13687 	      /* Positive values indicate an address_class.  */
13688 	      add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
13689 	    }
13690 	  else
13691 	    {
13692 	      /* Negative values indicate an (inverted) segment base reg.  */
13693 	      dw_loc_descr_ref d
13694 		= one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
13695 	      add_AT_loc (mod_type_die, DW_AT_segment, d);
13696 	    }
13697 	}
13698     }
13699   else if (code == INTEGER_TYPE
13700 	   && TREE_TYPE (type) != NULL_TREE
13701 	   && subrange_type_for_debug_p (type, &low, &high))
13702     {
13703       tree bias = NULL_TREE;
13704       if (lang_hooks.types.get_type_bias)
13705 	bias = lang_hooks.types.get_type_bias (type);
13706       mod_type_die = subrange_type_die (type, low, high, bias, context_die);
13707       item_type = TREE_TYPE (type);
13708     }
13709   else if (is_base_type (type))
13710     {
13711       mod_type_die = base_type_die (type, reverse);
13712 
13713       /* The DIE with DW_AT_endianity is placed right after the naked DIE.  */
13714       if (reverse_base_type)
13715 	{
13716 	  dw_die_ref after_die
13717 	    = modified_type_die (type, cv_quals, false, context_die);
13718 	  add_child_die_after (comp_unit_die (), mod_type_die, after_die);
13719 	}
13720       else
13721 	add_child_die (comp_unit_die (), mod_type_die);
13722 
13723       add_pubtype (type, mod_type_die);
13724     }
13725   else
13726     {
13727       gen_type_die (type, context_die);
13728 
13729       /* We have to get the type_main_variant here (and pass that to the
13730 	 `lookup_type_die' routine) because the ..._TYPE node we have
13731 	 might simply be a *copy* of some original type node (where the
13732 	 copy was created to help us keep track of typedef names) and
13733 	 that copy might have a different TYPE_UID from the original
13734 	 ..._TYPE node.  */
13735       if (TREE_CODE (type) == FUNCTION_TYPE
13736 	  || TREE_CODE (type) == METHOD_TYPE)
13737 	{
13738 	  /* For function/method types, can't just use type_main_variant here,
13739 	     because that can have different ref-qualifiers for C++,
13740 	     but try to canonicalize.  */
13741 	  tree main = TYPE_MAIN_VARIANT (type);
13742 	  for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
13743 	    if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
13744 		&& check_base_type (t, main)
13745 		&& check_lang_type (t, type))
13746 	      return lookup_type_die (t);
13747 	  return lookup_type_die (type);
13748 	}
13749       else if (TREE_CODE (type) != VECTOR_TYPE
13750 	       && TREE_CODE (type) != ARRAY_TYPE)
13751 	return lookup_type_die (type_main_variant (type));
13752       else
13753 	/* Vectors have the debugging information in the type,
13754 	   not the main variant.  */
13755 	return lookup_type_die (type);
13756     }
13757 
13758   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
13759      don't output a DW_TAG_typedef, since there isn't one in the
13760      user's program; just attach a DW_AT_name to the type.
13761      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
13762      if the base type already has the same name.  */
13763   if (name
13764       && ((TREE_CODE (name) != TYPE_DECL
13765 	   && (qualified_type == TYPE_MAIN_VARIANT (type)
13766 	       || (cv_quals == TYPE_UNQUALIFIED)))
13767 	  || (TREE_CODE (name) == TYPE_DECL
13768 	      && TREE_TYPE (name) == qualified_type
13769 	      && DECL_NAME (name))))
13770     {
13771       if (TREE_CODE (name) == TYPE_DECL)
13772 	/* Could just call add_name_and_src_coords_attributes here,
13773 	   but since this is a builtin type it doesn't have any
13774 	   useful source coordinates anyway.  */
13775 	name = DECL_NAME (name);
13776       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
13777     }
13778   /* This probably indicates a bug.  */
13779   else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
13780     {
13781       name = TYPE_IDENTIFIER (type);
13782       add_name_attribute (mod_type_die,
13783 			  name ? IDENTIFIER_POINTER (name) : "__unknown__");
13784     }
13785 
13786   if (qualified_type && !reverse_base_type)
13787     equate_type_number_to_die (qualified_type, mod_type_die);
13788 
13789   if (item_type)
13790     /* We must do this after the equate_type_number_to_die call, in case
13791        this is a recursive type.  This ensures that the modified_type_die
13792        recursion will terminate even if the type is recursive.  Recursive
13793        types are possible in Ada.  */
13794     sub_die = modified_type_die (item_type,
13795 				 TYPE_QUALS_NO_ADDR_SPACE (item_type),
13796 				 reverse,
13797 				 context_die);
13798 
13799   if (sub_die != NULL)
13800     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
13801 
13802   add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
13803   if (TYPE_ARTIFICIAL (type))
13804     add_AT_flag (mod_type_die, DW_AT_artificial, 1);
13805 
13806   return mod_type_die;
13807 }
13808 
13809 /* Generate DIEs for the generic parameters of T.
13810    T must be either a generic type or a generic function.
13811    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
13812 
13813 static void
gen_generic_params_dies(tree t)13814 gen_generic_params_dies (tree t)
13815 {
13816   tree parms, args;
13817   int parms_num, i;
13818   dw_die_ref die = NULL;
13819   int non_default;
13820 
13821   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
13822     return;
13823 
13824   if (TYPE_P (t))
13825     die = lookup_type_die (t);
13826   else if (DECL_P (t))
13827     die = lookup_decl_die (t);
13828 
13829   gcc_assert (die);
13830 
13831   parms = lang_hooks.get_innermost_generic_parms (t);
13832   if (!parms)
13833     /* T has no generic parameter. It means T is neither a generic type
13834        or function. End of story.  */
13835     return;
13836 
13837   parms_num = TREE_VEC_LENGTH (parms);
13838   args = lang_hooks.get_innermost_generic_args (t);
13839   if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
13840     non_default = int_cst_value (TREE_CHAIN (args));
13841   else
13842     non_default = TREE_VEC_LENGTH (args);
13843   for (i = 0; i < parms_num; i++)
13844     {
13845       tree parm, arg, arg_pack_elems;
13846       dw_die_ref parm_die;
13847 
13848       parm = TREE_VEC_ELT (parms, i);
13849       arg = TREE_VEC_ELT (args, i);
13850       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
13851       gcc_assert (parm && TREE_VALUE (parm) && arg);
13852 
13853       if (parm && TREE_VALUE (parm) && arg)
13854 	{
13855 	  /* If PARM represents a template parameter pack,
13856 	     emit a DW_TAG_GNU_template_parameter_pack DIE, followed
13857 	     by DW_TAG_template_*_parameter DIEs for the argument
13858 	     pack elements of ARG. Note that ARG would then be
13859 	     an argument pack.  */
13860 	  if (arg_pack_elems)
13861 	    parm_die = template_parameter_pack_die (TREE_VALUE (parm),
13862 						    arg_pack_elems,
13863 						    die);
13864 	  else
13865 	    parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
13866 					      true /* emit name */, die);
13867 	  if (i >= non_default)
13868 	    add_AT_flag (parm_die, DW_AT_default_value, 1);
13869 	}
13870     }
13871 }
13872 
13873 /* Create and return a DIE for PARM which should be
13874    the representation of a generic type parameter.
13875    For instance, in the C++ front end, PARM would be a template parameter.
13876    ARG is the argument to PARM.
13877    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
13878    name of the PARM.
13879    PARENT_DIE is the parent DIE which the new created DIE should be added to,
13880    as a child node.  */
13881 
13882 static dw_die_ref
generic_parameter_die(tree parm,tree arg,bool emit_name_p,dw_die_ref parent_die)13883 generic_parameter_die (tree parm, tree arg,
13884 		       bool emit_name_p,
13885 		       dw_die_ref parent_die)
13886 {
13887   dw_die_ref tmpl_die = NULL;
13888   const char *name = NULL;
13889 
13890   /* C++20 accepts class literals as template parameters, and var
13891      decls with initializers represent them.  The VAR_DECLs would be
13892      rejected, but we can take the DECL_INITIAL constructor and
13893      attempt to expand it.  */
13894   if (arg && VAR_P (arg))
13895     arg = DECL_INITIAL (arg);
13896 
13897   if (!parm || !DECL_NAME (parm) || !arg)
13898     return NULL;
13899 
13900   /* We support non-type generic parameters and arguments,
13901      type generic parameters and arguments, as well as
13902      generic generic parameters (a.k.a. template template parameters in C++)
13903      and arguments.  */
13904   if (TREE_CODE (parm) == PARM_DECL)
13905     /* PARM is a nontype generic parameter  */
13906     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
13907   else if (TREE_CODE (parm) == TYPE_DECL)
13908     /* PARM is a type generic parameter.  */
13909     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
13910   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13911     /* PARM is a generic generic parameter.
13912        Its DIE is a GNU extension. It shall have a
13913        DW_AT_name attribute to represent the name of the template template
13914        parameter, and a DW_AT_GNU_template_name attribute to represent the
13915        name of the template template argument.  */
13916     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
13917 			parent_die, parm);
13918   else
13919     gcc_unreachable ();
13920 
13921   if (tmpl_die)
13922     {
13923       tree tmpl_type;
13924 
13925       /* If PARM is a generic parameter pack, it means we are
13926          emitting debug info for a template argument pack element.
13927 	 In other terms, ARG is a template argument pack element.
13928 	 In that case, we don't emit any DW_AT_name attribute for
13929 	 the die.  */
13930       if (emit_name_p)
13931 	{
13932 	  name = IDENTIFIER_POINTER (DECL_NAME (parm));
13933 	  gcc_assert (name);
13934 	  add_AT_string (tmpl_die, DW_AT_name, name);
13935 	}
13936 
13937       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13938 	{
13939 	  /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
13940 	     TMPL_DIE should have a child DW_AT_type attribute that is set
13941 	     to the type of the argument to PARM, which is ARG.
13942 	     If PARM is a type generic parameter, TMPL_DIE should have a
13943 	     child DW_AT_type that is set to ARG.  */
13944 	  tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
13945 	  add_type_attribute (tmpl_die, tmpl_type,
13946 			      (TREE_THIS_VOLATILE (tmpl_type)
13947 			       ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
13948 			      false, parent_die);
13949 	}
13950       else
13951 	{
13952 	  /* So TMPL_DIE is a DIE representing a
13953 	     a generic generic template parameter, a.k.a template template
13954 	     parameter in C++ and arg is a template.  */
13955 
13956 	  /* The DW_AT_GNU_template_name attribute of the DIE must be set
13957 	     to the name of the argument.  */
13958 	  name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
13959 	  if (name)
13960 	    add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
13961 	}
13962 
13963       if (TREE_CODE (parm) == PARM_DECL)
13964 	/* So PARM is a non-type generic parameter.
13965 	   DWARF3 5.6.8 says we must set a DW_AT_const_value child
13966 	   attribute of TMPL_DIE which value represents the value
13967 	   of ARG.
13968 	   We must be careful here:
13969 	   The value of ARG might reference some function decls.
13970 	   We might currently be emitting debug info for a generic
13971 	   type and types are emitted before function decls, we don't
13972 	   know if the function decls referenced by ARG will actually be
13973 	   emitted after cgraph computations.
13974 	   So must defer the generation of the DW_AT_const_value to
13975 	   after cgraph is ready.  */
13976 	append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
13977     }
13978 
13979   return tmpl_die;
13980 }
13981 
13982 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
13983    PARM_PACK must be a template parameter pack. The returned DIE
13984    will be child DIE of PARENT_DIE.  */
13985 
13986 static dw_die_ref
template_parameter_pack_die(tree parm_pack,tree parm_pack_args,dw_die_ref parent_die)13987 template_parameter_pack_die (tree parm_pack,
13988 			     tree parm_pack_args,
13989 			     dw_die_ref parent_die)
13990 {
13991   dw_die_ref die;
13992   int j;
13993 
13994   gcc_assert (parent_die && parm_pack);
13995 
13996   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
13997   add_name_and_src_coords_attributes (die, parm_pack);
13998   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
13999     generic_parameter_die (parm_pack,
14000 			   TREE_VEC_ELT (parm_pack_args, j),
14001 			   false /* Don't emit DW_AT_name */,
14002 			   die);
14003   return die;
14004 }
14005 
14006 /* Return the DBX register number described by a given RTL node.  */
14007 
14008 static unsigned int
dbx_reg_number(const_rtx rtl)14009 dbx_reg_number (const_rtx rtl)
14010 {
14011   unsigned regno = REGNO (rtl);
14012 
14013   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
14014 
14015 #ifdef LEAF_REG_REMAP
14016   if (crtl->uses_only_leaf_regs)
14017     {
14018       int leaf_reg = LEAF_REG_REMAP (regno);
14019       if (leaf_reg != -1)
14020 	regno = (unsigned) leaf_reg;
14021     }
14022 #endif
14023 
14024   regno = DBX_REGISTER_NUMBER (regno);
14025   gcc_assert (regno != INVALID_REGNUM);
14026   return regno;
14027 }
14028 
14029 /* Optionally add a DW_OP_piece term to a location description expression.
14030    DW_OP_piece is only added if the location description expression already
14031    doesn't end with DW_OP_piece.  */
14032 
14033 static void
add_loc_descr_op_piece(dw_loc_descr_ref * list_head,int size)14034 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
14035 {
14036   dw_loc_descr_ref loc;
14037 
14038   if (*list_head != NULL)
14039     {
14040       /* Find the end of the chain.  */
14041       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
14042 	;
14043 
14044       if (loc->dw_loc_opc != DW_OP_piece)
14045 	loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
14046     }
14047 }
14048 
14049 /* Return a location descriptor that designates a machine register or
14050    zero if there is none.  */
14051 
14052 static dw_loc_descr_ref
reg_loc_descriptor(rtx rtl,enum var_init_status initialized)14053 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
14054 {
14055   rtx regs;
14056 
14057   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
14058     return 0;
14059 
14060   /* We only use "frame base" when we're sure we're talking about the
14061      post-prologue local stack frame.  We do this by *not* running
14062      register elimination until this point, and recognizing the special
14063      argument pointer and soft frame pointer rtx's.
14064      Use DW_OP_fbreg offset DW_OP_stack_value in this case.  */
14065   if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
14066       && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
14067     {
14068       dw_loc_descr_ref result = NULL;
14069 
14070       if (dwarf_version >= 4 || !dwarf_strict)
14071 	{
14072 	  result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
14073 				       initialized);
14074 	  if (result)
14075 	    add_loc_descr (&result,
14076 			   new_loc_descr (DW_OP_stack_value, 0, 0));
14077 	}
14078       return result;
14079     }
14080 
14081   regs = targetm.dwarf_register_span (rtl);
14082 
14083   if (REG_NREGS (rtl) > 1 || regs)
14084     return multiple_reg_loc_descriptor (rtl, regs, initialized);
14085   else
14086     {
14087       unsigned int dbx_regnum = dbx_reg_number (rtl);
14088       if (dbx_regnum == IGNORED_DWARF_REGNUM)
14089 	return 0;
14090       return one_reg_loc_descriptor (dbx_regnum, initialized);
14091     }
14092 }
14093 
14094 /* Return a location descriptor that designates a machine register for
14095    a given hard register number.  */
14096 
14097 static dw_loc_descr_ref
one_reg_loc_descriptor(unsigned int regno,enum var_init_status initialized)14098 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
14099 {
14100   dw_loc_descr_ref reg_loc_descr;
14101 
14102   if (regno <= 31)
14103     reg_loc_descr
14104       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
14105   else
14106     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
14107 
14108   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14109     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14110 
14111   return reg_loc_descr;
14112 }
14113 
14114 /* Given an RTL of a register, return a location descriptor that
14115    designates a value that spans more than one register.  */
14116 
14117 static dw_loc_descr_ref
multiple_reg_loc_descriptor(rtx rtl,rtx regs,enum var_init_status initialized)14118 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
14119 			     enum var_init_status initialized)
14120 {
14121   int size, i;
14122   dw_loc_descr_ref loc_result = NULL;
14123 
14124   /* Simple, contiguous registers.  */
14125   if (regs == NULL_RTX)
14126     {
14127       unsigned reg = REGNO (rtl);
14128       int nregs;
14129 
14130 #ifdef LEAF_REG_REMAP
14131       if (crtl->uses_only_leaf_regs)
14132 	{
14133 	  int leaf_reg = LEAF_REG_REMAP (reg);
14134 	  if (leaf_reg != -1)
14135 	    reg = (unsigned) leaf_reg;
14136 	}
14137 #endif
14138 
14139       gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
14140       nregs = REG_NREGS (rtl);
14141 
14142       /* At present we only track constant-sized pieces.  */
14143       if (!GET_MODE_SIZE (GET_MODE (rtl)).is_constant (&size))
14144 	return NULL;
14145       size /= nregs;
14146 
14147       loc_result = NULL;
14148       while (nregs--)
14149 	{
14150 	  dw_loc_descr_ref t;
14151 
14152 	  t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
14153 				      VAR_INIT_STATUS_INITIALIZED);
14154 	  add_loc_descr (&loc_result, t);
14155 	  add_loc_descr_op_piece (&loc_result, size);
14156 	  ++reg;
14157 	}
14158       return loc_result;
14159     }
14160 
14161   /* Now onto stupid register sets in non contiguous locations.  */
14162 
14163   gcc_assert (GET_CODE (regs) == PARALLEL);
14164 
14165   /* At present we only track constant-sized pieces.  */
14166   if (!GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0))).is_constant (&size))
14167     return NULL;
14168   loc_result = NULL;
14169 
14170   for (i = 0; i < XVECLEN (regs, 0); ++i)
14171     {
14172       dw_loc_descr_ref t;
14173 
14174       t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
14175 				  VAR_INIT_STATUS_INITIALIZED);
14176       add_loc_descr (&loc_result, t);
14177       add_loc_descr_op_piece (&loc_result, size);
14178     }
14179 
14180   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14181     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14182   return loc_result;
14183 }
14184 
14185 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
14186 
14187 /* Return a location descriptor that designates a constant i,
14188    as a compound operation from constant (i >> shift), constant shift
14189    and DW_OP_shl.  */
14190 
14191 static dw_loc_descr_ref
int_shift_loc_descriptor(HOST_WIDE_INT i,int shift)14192 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
14193 {
14194   dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
14195   add_loc_descr (&ret, int_loc_descriptor (shift));
14196   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14197   return ret;
14198 }
14199 
14200 /* Return a location descriptor that designates constant POLY_I.  */
14201 
14202 static dw_loc_descr_ref
int_loc_descriptor(poly_int64 poly_i)14203 int_loc_descriptor (poly_int64 poly_i)
14204 {
14205   enum dwarf_location_atom op;
14206 
14207   HOST_WIDE_INT i;
14208   if (!poly_i.is_constant (&i))
14209     {
14210       /* Create location descriptions for the non-constant part and
14211 	 add any constant offset at the end.  */
14212       dw_loc_descr_ref ret = NULL;
14213       HOST_WIDE_INT constant = poly_i.coeffs[0];
14214       for (unsigned int j = 1; j < NUM_POLY_INT_COEFFS; ++j)
14215 	{
14216 	  HOST_WIDE_INT coeff = poly_i.coeffs[j];
14217 	  if (coeff != 0)
14218 	    {
14219 	      dw_loc_descr_ref start = ret;
14220 	      unsigned int factor;
14221 	      int bias;
14222 	      unsigned int regno = targetm.dwarf_poly_indeterminate_value
14223 		(j, &factor, &bias);
14224 
14225 	      /* Add COEFF * ((REGNO / FACTOR) - BIAS) to the value:
14226 		 add COEFF * (REGNO / FACTOR) now and subtract
14227 		 COEFF * BIAS from the final constant part.  */
14228 	      constant -= coeff * bias;
14229 	      add_loc_descr (&ret, new_reg_loc_descr (regno, 0));
14230 	      if (coeff % factor == 0)
14231 		coeff /= factor;
14232 	      else
14233 		{
14234 		  int amount = exact_log2 (factor);
14235 		  gcc_assert (amount >= 0);
14236 		  add_loc_descr (&ret, int_loc_descriptor (amount));
14237 		  add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14238 		}
14239 	      if (coeff != 1)
14240 		{
14241 		  add_loc_descr (&ret, int_loc_descriptor (coeff));
14242 		  add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
14243 		}
14244 	      if (start)
14245 		add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
14246 	    }
14247 	}
14248       loc_descr_plus_const (&ret, constant);
14249       return ret;
14250     }
14251 
14252   /* Pick the smallest representation of a constant, rather than just
14253      defaulting to the LEB encoding.  */
14254   if (i >= 0)
14255     {
14256       int clz = clz_hwi (i);
14257       int ctz = ctz_hwi (i);
14258       if (i <= 31)
14259 	op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
14260       else if (i <= 0xff)
14261 	op = DW_OP_const1u;
14262       else if (i <= 0xffff)
14263 	op = DW_OP_const2u;
14264       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
14265 	       && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
14266 	/* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
14267 	   DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
14268 	   while DW_OP_const4u is 5 bytes.  */
14269 	return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
14270       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14271 	       && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
14272 	/* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
14273 	   while DW_OP_const4u is 5 bytes.  */
14274 	return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
14275 
14276       else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
14277 	       && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
14278 		  <= 4)
14279 	{
14280 	  /* As i >= 2**31, the double cast above will yield a negative number.
14281 	     Since wrapping is defined in DWARF expressions we can output big
14282 	     positive integers as small negative ones, regardless of the size
14283 	     of host wide ints.
14284 
14285 	     Here, since the evaluator will handle 32-bit values and since i >=
14286 	     2**31, we know it's going to be interpreted as a negative literal:
14287 	     store it this way if we can do better than 5 bytes this way.  */
14288 	  return int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
14289 	}
14290       else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14291 	op = DW_OP_const4u;
14292 
14293       /* Past this point, i >= 0x100000000 and thus DW_OP_constu will take at
14294 	 least 6 bytes: see if we can do better before falling back to it.  */
14295       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14296 	       && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
14297 	/* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes.  */
14298 	return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
14299       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
14300 	       && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
14301 		  >= HOST_BITS_PER_WIDE_INT)
14302 	/* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
14303 	   DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes.  */
14304 	return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
14305       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
14306 	       && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
14307 	       && size_of_uleb128 (i) > 6)
14308 	/* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes.  */
14309 	return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
14310       else
14311 	op = DW_OP_constu;
14312     }
14313   else
14314     {
14315       if (i >= -0x80)
14316 	op = DW_OP_const1s;
14317       else if (i >= -0x8000)
14318 	op = DW_OP_const2s;
14319       else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
14320 	{
14321 	  if (size_of_int_loc_descriptor (i) < 5)
14322 	    {
14323 	      dw_loc_descr_ref ret = int_loc_descriptor (-i);
14324 	      add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14325 	      return ret;
14326 	    }
14327 	  op = DW_OP_const4s;
14328 	}
14329       else
14330 	{
14331 	  if (size_of_int_loc_descriptor (i)
14332 	      < (unsigned long) 1 + size_of_sleb128 (i))
14333 	    {
14334 	      dw_loc_descr_ref ret = int_loc_descriptor (-i);
14335 	      add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14336 	      return ret;
14337 	    }
14338 	  op = DW_OP_consts;
14339 	}
14340     }
14341 
14342   return new_loc_descr (op, i, 0);
14343 }
14344 
14345 /* Likewise, for unsigned constants.  */
14346 
14347 static dw_loc_descr_ref
uint_loc_descriptor(unsigned HOST_WIDE_INT i)14348 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
14349 {
14350   const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
14351   const unsigned HOST_WIDE_INT max_uint
14352     = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
14353 
14354   /* If possible, use the clever signed constants handling.  */
14355   if (i <= max_int)
14356     return int_loc_descriptor ((HOST_WIDE_INT) i);
14357 
14358   /* Here, we are left with positive numbers that cannot be represented as
14359      HOST_WIDE_INT, i.e.:
14360          max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
14361 
14362      Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
14363      whereas may be better to output a negative integer: thanks to integer
14364      wrapping, we know that:
14365          x = x - 2 ** DWARF2_ADDR_SIZE
14366 	   = x - 2 * (max (HOST_WIDE_INT) + 1)
14367      So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
14368      small negative integers.  Let's try that in cases it will clearly improve
14369      the encoding: there is no gain turning DW_OP_const4u into
14370      DW_OP_const4s.  */
14371   if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
14372       && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
14373 	  || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
14374     {
14375       const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
14376 
14377       /* Now, -1 <  first_shift <= max (HOST_WIDE_INT)
14378 	 i.e.  0 <= first_shift <= max (HOST_WIDE_INT).  */
14379       const HOST_WIDE_INT second_shift
14380         = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
14381 
14382       /* So we finally have:
14383 	      -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
14384 	 i.e.  min (HOST_WIDE_INT)     <= second_shift <  0.  */
14385       return int_loc_descriptor (second_shift);
14386     }
14387 
14388   /* Last chance: fallback to a simple constant operation.  */
14389   return new_loc_descr
14390      ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14391       ? DW_OP_const4u
14392       : DW_OP_const8u,
14393       i, 0);
14394 }
14395 
14396 /* Generate and return a location description that computes the unsigned
14397    comparison of the two stack top entries (a OP b where b is the top-most
14398    entry and a is the second one).  The KIND of comparison can be LT_EXPR,
14399    LE_EXPR, GT_EXPR or GE_EXPR.  */
14400 
14401 static dw_loc_descr_ref
uint_comparison_loc_list(enum tree_code kind)14402 uint_comparison_loc_list (enum tree_code kind)
14403 {
14404   enum dwarf_location_atom op, flip_op;
14405   dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
14406 
14407   switch (kind)
14408     {
14409     case LT_EXPR:
14410       op = DW_OP_lt;
14411       break;
14412     case LE_EXPR:
14413       op = DW_OP_le;
14414       break;
14415     case GT_EXPR:
14416       op = DW_OP_gt;
14417       break;
14418     case GE_EXPR:
14419       op = DW_OP_ge;
14420       break;
14421     default:
14422       gcc_unreachable ();
14423     }
14424 
14425   bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14426   jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
14427 
14428   /* Until DWARFv4, operations all work on signed integers.  It is nevertheless
14429      possible to perform unsigned comparisons: we just have to distinguish
14430      three cases:
14431 
14432        1. when a and b have the same sign (as signed integers); then we should
14433 	  return: a OP(signed) b;
14434 
14435        2. when a is a negative signed integer while b is a positive one, then a
14436 	  is a greater unsigned integer than b; likewise when a and b's roles
14437 	  are flipped.
14438 
14439      So first, compare the sign of the two operands.  */
14440   ret = new_loc_descr (DW_OP_over, 0, 0);
14441   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14442   add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
14443   /* If they have different signs (i.e. they have different sign bits), then
14444      the stack top value has now the sign bit set and thus it's smaller than
14445      zero.  */
14446   add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
14447   add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
14448   add_loc_descr (&ret, bra_node);
14449 
14450   /* We are in case 1.  At this point, we know both operands have the same
14451      sign, to it's safe to use the built-in signed comparison.  */
14452   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14453   add_loc_descr (&ret, jmp_node);
14454 
14455   /* We are in case 2.  Here, we know both operands do not have the same sign,
14456      so we have to flip the signed comparison.  */
14457   flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
14458   tmp = new_loc_descr (flip_op, 0, 0);
14459   bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14460   bra_node->dw_loc_oprnd1.v.val_loc = tmp;
14461   add_loc_descr (&ret, tmp);
14462 
14463   /* This dummy operation is necessary to make the two branches join.  */
14464   tmp = new_loc_descr (DW_OP_nop, 0, 0);
14465   jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14466   jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
14467   add_loc_descr (&ret, tmp);
14468 
14469   return ret;
14470 }
14471 
14472 /* Likewise, but takes the location description lists (might be destructive on
14473    them).  Return NULL if either is NULL or if concatenation fails.  */
14474 
14475 static dw_loc_list_ref
loc_list_from_uint_comparison(dw_loc_list_ref left,dw_loc_list_ref right,enum tree_code kind)14476 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
14477 			       enum tree_code kind)
14478 {
14479   if (left == NULL || right == NULL)
14480     return NULL;
14481 
14482   add_loc_list (&left, right);
14483   if (left == NULL)
14484     return NULL;
14485 
14486   add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
14487   return left;
14488 }
14489 
14490 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
14491    without actually allocating it.  */
14492 
14493 static unsigned long
size_of_int_shift_loc_descriptor(HOST_WIDE_INT i,int shift)14494 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
14495 {
14496   return size_of_int_loc_descriptor (i >> shift)
14497 	 + size_of_int_loc_descriptor (shift)
14498 	 + 1;
14499 }
14500 
14501 /* Return size_of_locs (int_loc_descriptor (i)) without
14502    actually allocating it.  */
14503 
14504 static unsigned long
size_of_int_loc_descriptor(HOST_WIDE_INT i)14505 size_of_int_loc_descriptor (HOST_WIDE_INT i)
14506 {
14507   unsigned long s;
14508 
14509   if (i >= 0)
14510     {
14511       int clz, ctz;
14512       if (i <= 31)
14513 	return 1;
14514       else if (i <= 0xff)
14515 	return 2;
14516       else if (i <= 0xffff)
14517 	return 3;
14518       clz = clz_hwi (i);
14519       ctz = ctz_hwi (i);
14520       if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
14521 	  && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
14522 	return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14523 						    - clz - 5);
14524       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14525 	       && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
14526 	return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14527 						    - clz - 8);
14528       else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
14529 	       && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
14530 		  <= 4)
14531 	return size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
14532       else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14533 	return 5;
14534       s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
14535       if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14536 	  && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
14537 	return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14538 						    - clz - 8);
14539       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
14540 	       && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
14541 	return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14542 						    - clz - 16);
14543       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
14544 	       && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
14545 	       && s > 6)
14546 	return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14547 						    - clz - 32);
14548       else
14549 	return 1 + s;
14550     }
14551   else
14552     {
14553       if (i >= -0x80)
14554 	return 2;
14555       else if (i >= -0x8000)
14556 	return 3;
14557       else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
14558 	{
14559 	  if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
14560 	    {
14561 	      s = size_of_int_loc_descriptor (-i) + 1;
14562 	      if (s < 5)
14563 		return s;
14564 	    }
14565 	  return 5;
14566 	}
14567       else
14568 	{
14569 	  unsigned long r = 1 + size_of_sleb128 (i);
14570 	  if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
14571 	    {
14572 	      s = size_of_int_loc_descriptor (-i) + 1;
14573 	      if (s < r)
14574 		return s;
14575 	    }
14576 	  return r;
14577 	}
14578     }
14579 }
14580 
14581 /* Return loc description representing "address" of integer value.
14582    This can appear only as toplevel expression.  */
14583 
14584 static dw_loc_descr_ref
address_of_int_loc_descriptor(int size,HOST_WIDE_INT i)14585 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
14586 {
14587   int litsize;
14588   dw_loc_descr_ref loc_result = NULL;
14589 
14590   if (!(dwarf_version >= 4 || !dwarf_strict))
14591     return NULL;
14592 
14593   litsize = size_of_int_loc_descriptor (i);
14594   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
14595      is more compact.  For DW_OP_stack_value we need:
14596      litsize + 1 (DW_OP_stack_value)
14597      and for DW_OP_implicit_value:
14598      1 (DW_OP_implicit_value) + 1 (length) + size.  */
14599   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
14600     {
14601       loc_result = int_loc_descriptor (i);
14602       add_loc_descr (&loc_result,
14603 		     new_loc_descr (DW_OP_stack_value, 0, 0));
14604       return loc_result;
14605     }
14606 
14607   loc_result = new_loc_descr (DW_OP_implicit_value,
14608 			      size, 0);
14609   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
14610   loc_result->dw_loc_oprnd2.v.val_int = i;
14611   return loc_result;
14612 }
14613 
14614 /* Return a location descriptor that designates a base+offset location.  */
14615 
14616 static dw_loc_descr_ref
based_loc_descr(rtx reg,poly_int64 offset,enum var_init_status initialized)14617 based_loc_descr (rtx reg, poly_int64 offset,
14618 		 enum var_init_status initialized)
14619 {
14620   unsigned int regno;
14621   dw_loc_descr_ref result;
14622   dw_fde_ref fde = cfun->fde;
14623 
14624   /* We only use "frame base" when we're sure we're talking about the
14625      post-prologue local stack frame.  We do this by *not* running
14626      register elimination until this point, and recognizing the special
14627      argument pointer and soft frame pointer rtx's.  */
14628   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
14629     {
14630       rtx elim = (ira_use_lra_p
14631 		  ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
14632 		  : eliminate_regs (reg, VOIDmode, NULL_RTX));
14633 
14634       if (elim != reg)
14635 	{
14636 	  /* Allow hard frame pointer here even if frame pointer
14637 	    isn't used since hard frame pointer is encoded with
14638 	    DW_OP_fbreg which uses the DW_AT_frame_base attribute,
14639 	    not hard frame pointer directly.  */
14640 	  elim = strip_offset_and_add (elim, &offset);
14641 	  gcc_assert (elim == hard_frame_pointer_rtx
14642 		      || elim == stack_pointer_rtx);
14643 
14644 	  /* If drap register is used to align stack, use frame
14645 	     pointer + offset to access stack variables.  If stack
14646 	     is aligned without drap, use stack pointer + offset to
14647 	     access stack variables.  */
14648 	  if (crtl->stack_realign_tried
14649 	      && reg == frame_pointer_rtx)
14650 	    {
14651 	      int base_reg
14652 		= DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
14653 				      ? HARD_FRAME_POINTER_REGNUM
14654 				      : REGNO (elim));
14655 	      return new_reg_loc_descr (base_reg, offset);
14656 	    }
14657 
14658 	  gcc_assert (frame_pointer_fb_offset_valid);
14659 	  offset += frame_pointer_fb_offset;
14660 	  HOST_WIDE_INT const_offset;
14661 	  if (offset.is_constant (&const_offset))
14662 	    return new_loc_descr (DW_OP_fbreg, const_offset, 0);
14663 	  else
14664 	    {
14665 	      dw_loc_descr_ref ret = new_loc_descr (DW_OP_fbreg, 0, 0);
14666 	      loc_descr_plus_const (&ret, offset);
14667 	      return ret;
14668 	    }
14669 	}
14670     }
14671 
14672   regno = REGNO (reg);
14673 #ifdef LEAF_REG_REMAP
14674   if (crtl->uses_only_leaf_regs)
14675     {
14676       int leaf_reg = LEAF_REG_REMAP (regno);
14677       if (leaf_reg != -1)
14678 	regno = (unsigned) leaf_reg;
14679     }
14680 #endif
14681   regno = DWARF_FRAME_REGNUM (regno);
14682 
14683   HOST_WIDE_INT const_offset;
14684   if (!optimize && fde
14685       && (fde->drap_reg == regno || fde->vdrap_reg == regno)
14686       && offset.is_constant (&const_offset))
14687     {
14688       /* Use cfa+offset to represent the location of arguments passed
14689 	 on the stack when drap is used to align stack.
14690 	 Only do this when not optimizing, for optimized code var-tracking
14691 	 is supposed to track where the arguments live and the register
14692 	 used as vdrap or drap in some spot might be used for something
14693 	 else in other part of the routine.  */
14694       return new_loc_descr (DW_OP_fbreg, const_offset, 0);
14695     }
14696 
14697   result = new_reg_loc_descr (regno, offset);
14698 
14699   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14700     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14701 
14702   return result;
14703 }
14704 
14705 /* Return true if this RTL expression describes a base+offset calculation.  */
14706 
14707 static inline int
is_based_loc(const_rtx rtl)14708 is_based_loc (const_rtx rtl)
14709 {
14710   return (GET_CODE (rtl) == PLUS
14711 	  && ((REG_P (XEXP (rtl, 0))
14712 	       && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
14713 	       && CONST_INT_P (XEXP (rtl, 1)))));
14714 }
14715 
14716 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
14717    failed.  */
14718 
14719 static dw_loc_descr_ref
tls_mem_loc_descriptor(rtx mem)14720 tls_mem_loc_descriptor (rtx mem)
14721 {
14722   tree base;
14723   dw_loc_descr_ref loc_result;
14724 
14725   if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
14726     return NULL;
14727 
14728   base = get_base_address (MEM_EXPR (mem));
14729   if (base == NULL
14730       || !VAR_P (base)
14731       || !DECL_THREAD_LOCAL_P (base))
14732     return NULL;
14733 
14734   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
14735   if (loc_result == NULL)
14736     return NULL;
14737 
14738   if (maybe_ne (MEM_OFFSET (mem), 0))
14739     loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
14740 
14741   return loc_result;
14742 }
14743 
14744 /* Output debug info about reason why we failed to expand expression as dwarf
14745    expression.  */
14746 
14747 static void
expansion_failed(tree expr,rtx rtl,char const * reason)14748 expansion_failed (tree expr, rtx rtl, char const *reason)
14749 {
14750   if (dump_file && (dump_flags & TDF_DETAILS))
14751     {
14752       fprintf (dump_file, "Failed to expand as dwarf: ");
14753       if (expr)
14754 	print_generic_expr (dump_file, expr, dump_flags);
14755       if (rtl)
14756 	{
14757 	  fprintf (dump_file, "\n");
14758 	  print_rtl (dump_file, rtl);
14759 	}
14760       fprintf (dump_file, "\nReason: %s\n", reason);
14761     }
14762 }
14763 
14764 /* Helper function for const_ok_for_output.  */
14765 
14766 static bool
const_ok_for_output_1(rtx rtl)14767 const_ok_for_output_1 (rtx rtl)
14768 {
14769   if (targetm.const_not_ok_for_debug_p (rtl))
14770     {
14771       if (GET_CODE (rtl) != UNSPEC)
14772 	{
14773 	  expansion_failed (NULL_TREE, rtl,
14774 			    "Expression rejected for debug by the backend.\n");
14775 	  return false;
14776 	}
14777 
14778       /* If delegitimize_address couldn't do anything with the UNSPEC, and
14779 	 the target hook doesn't explicitly allow it in debug info, assume
14780 	 we can't express it in the debug info.  */
14781       /* Don't complain about TLS UNSPECs, those are just too hard to
14782 	 delegitimize.  Note this could be a non-decl SYMBOL_REF such as
14783 	 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
14784 	 rather than DECL_THREAD_LOCAL_P is not just an optimization.  */
14785       if (flag_checking
14786 	  && (XVECLEN (rtl, 0) == 0
14787 	      || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
14788 	      || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
14789 	inform (current_function_decl
14790 		? DECL_SOURCE_LOCATION (current_function_decl)
14791 		: UNKNOWN_LOCATION,
14792 #if NUM_UNSPEC_VALUES > 0
14793 		"non-delegitimized UNSPEC %s (%d) found in variable location",
14794 		((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
14795 		 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
14796 #else
14797 		"non-delegitimized UNSPEC %d found in variable location",
14798 #endif
14799 		XINT (rtl, 1));
14800       expansion_failed (NULL_TREE, rtl,
14801 			"UNSPEC hasn't been delegitimized.\n");
14802       return false;
14803     }
14804 
14805   if (CONST_POLY_INT_P (rtl))
14806     return false;
14807 
14808   /* FIXME: Refer to PR60655. It is possible for simplification
14809      of rtl expressions in var tracking to produce such expressions.
14810      We should really identify / validate expressions
14811      enclosed in CONST that can be handled by assemblers on various
14812      targets and only handle legitimate cases here.  */
14813   switch (GET_CODE (rtl))
14814     {
14815     case SYMBOL_REF:
14816       break;
14817     case NOT:
14818     case NEG:
14819       return false;
14820     case PLUS:
14821       {
14822 	/* Make sure SYMBOL_REFs/UNSPECs are at most in one of the
14823 	   operands.  */
14824 	subrtx_var_iterator::array_type array;
14825 	bool first = false;
14826 	FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
14827 	  if (SYMBOL_REF_P (*iter)
14828 	      || LABEL_P (*iter)
14829 	      || GET_CODE (*iter) == UNSPEC)
14830 	    {
14831 	      first = true;
14832 	      break;
14833 	    }
14834 	if (!first)
14835 	  return true;
14836 	FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 1), ALL)
14837 	  if (SYMBOL_REF_P (*iter)
14838 	      || LABEL_P (*iter)
14839 	      || GET_CODE (*iter) == UNSPEC)
14840 	    return false;
14841 	return true;
14842       }
14843     case MINUS:
14844       {
14845 	/* Disallow negation of SYMBOL_REFs or UNSPECs when they
14846 	   appear in the second operand of MINUS.  */
14847 	subrtx_var_iterator::array_type array;
14848 	FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 1), ALL)
14849 	  if (SYMBOL_REF_P (*iter)
14850 	      || LABEL_P (*iter)
14851 	      || GET_CODE (*iter) == UNSPEC)
14852 	    return false;
14853 	return true;
14854       }
14855     default:
14856       return true;
14857     }
14858 
14859   if (CONSTANT_POOL_ADDRESS_P (rtl))
14860     {
14861       bool marked;
14862       get_pool_constant_mark (rtl, &marked);
14863       /* If all references to this pool constant were optimized away,
14864 	 it was not output and thus we can't represent it.  */
14865       if (!marked)
14866 	{
14867 	  expansion_failed (NULL_TREE, rtl,
14868 			    "Constant was removed from constant pool.\n");
14869 	  return false;
14870 	}
14871     }
14872 
14873   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
14874     return false;
14875 
14876   /* Avoid references to external symbols in debug info, on several targets
14877      the linker might even refuse to link when linking a shared library,
14878      and in many other cases the relocations for .debug_info/.debug_loc are
14879      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
14880      to be defined within the same shared library or executable are fine.  */
14881   if (SYMBOL_REF_EXTERNAL_P (rtl))
14882     {
14883       tree decl = SYMBOL_REF_DECL (rtl);
14884 
14885       if (decl == NULL || !targetm.binds_local_p (decl))
14886 	{
14887 	  expansion_failed (NULL_TREE, rtl,
14888 			    "Symbol not defined in current TU.\n");
14889 	  return false;
14890 	}
14891     }
14892 
14893   return true;
14894 }
14895 
14896 /* Return true if constant RTL can be emitted in DW_OP_addr or
14897    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
14898    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
14899 
14900 static bool
const_ok_for_output(rtx rtl)14901 const_ok_for_output (rtx rtl)
14902 {
14903   if (GET_CODE (rtl) == SYMBOL_REF)
14904     return const_ok_for_output_1 (rtl);
14905 
14906   if (GET_CODE (rtl) == CONST)
14907     {
14908       subrtx_var_iterator::array_type array;
14909       FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
14910 	if (!const_ok_for_output_1 (*iter))
14911 	  return false;
14912       return true;
14913     }
14914 
14915   return true;
14916 }
14917 
14918 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
14919    if possible, NULL otherwise.  */
14920 
14921 static dw_die_ref
base_type_for_mode(machine_mode mode,bool unsignedp)14922 base_type_for_mode (machine_mode mode, bool unsignedp)
14923 {
14924   dw_die_ref type_die;
14925   tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
14926 
14927   if (type == NULL)
14928     return NULL;
14929   switch (TREE_CODE (type))
14930     {
14931     case INTEGER_TYPE:
14932     case REAL_TYPE:
14933       break;
14934     default:
14935       return NULL;
14936     }
14937   type_die = lookup_type_die (type);
14938   if (!type_die)
14939     type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
14940 				  comp_unit_die ());
14941   if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
14942     return NULL;
14943   return type_die;
14944 }
14945 
14946 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
14947    type matching MODE, or, if MODE is narrower than or as wide as
14948    DWARF2_ADDR_SIZE, untyped.  Return NULL if the conversion is not
14949    possible.  */
14950 
14951 static dw_loc_descr_ref
convert_descriptor_to_mode(scalar_int_mode mode,dw_loc_descr_ref op)14952 convert_descriptor_to_mode (scalar_int_mode mode, dw_loc_descr_ref op)
14953 {
14954   machine_mode outer_mode = mode;
14955   dw_die_ref type_die;
14956   dw_loc_descr_ref cvt;
14957 
14958   if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14959     {
14960       add_loc_descr (&op, new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
14961       return op;
14962     }
14963   type_die = base_type_for_mode (outer_mode, 1);
14964   if (type_die == NULL)
14965     return NULL;
14966   cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14967   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14968   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14969   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14970   add_loc_descr (&op, cvt);
14971   return op;
14972 }
14973 
14974 /* Return location descriptor for comparison OP with operands OP0 and OP1.  */
14975 
14976 static dw_loc_descr_ref
compare_loc_descriptor(enum dwarf_location_atom op,dw_loc_descr_ref op0,dw_loc_descr_ref op1)14977 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
14978 			dw_loc_descr_ref op1)
14979 {
14980   dw_loc_descr_ref ret = op0;
14981   add_loc_descr (&ret, op1);
14982   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14983   if (STORE_FLAG_VALUE != 1)
14984     {
14985       add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
14986       add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
14987     }
14988   return ret;
14989 }
14990 
14991 /* Subroutine of scompare_loc_descriptor for the case in which we're
14992    comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
14993    and in which OP_MODE is bigger than DWARF2_ADDR_SIZE.  */
14994 
14995 static dw_loc_descr_ref
scompare_loc_descriptor_wide(enum dwarf_location_atom op,scalar_int_mode op_mode,dw_loc_descr_ref op0,dw_loc_descr_ref op1)14996 scompare_loc_descriptor_wide (enum dwarf_location_atom op,
14997 			      scalar_int_mode op_mode,
14998 			      dw_loc_descr_ref op0, dw_loc_descr_ref op1)
14999 {
15000   dw_die_ref type_die = base_type_for_mode (op_mode, 0);
15001   dw_loc_descr_ref cvt;
15002 
15003   if (type_die == NULL)
15004     return NULL;
15005   cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15006   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15007   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15008   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15009   add_loc_descr (&op0, cvt);
15010   cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15011   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15012   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15013   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15014   add_loc_descr (&op1, cvt);
15015   return compare_loc_descriptor (op, op0, op1);
15016 }
15017 
15018 /* Subroutine of scompare_loc_descriptor for the case in which we're
15019    comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
15020    and in which OP_MODE is smaller than DWARF2_ADDR_SIZE.  */
15021 
15022 static dw_loc_descr_ref
scompare_loc_descriptor_narrow(enum dwarf_location_atom op,rtx rtl,scalar_int_mode op_mode,dw_loc_descr_ref op0,dw_loc_descr_ref op1)15023 scompare_loc_descriptor_narrow (enum dwarf_location_atom op, rtx rtl,
15024 				scalar_int_mode op_mode,
15025 				dw_loc_descr_ref op0, dw_loc_descr_ref op1)
15026 {
15027   int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
15028   /* For eq/ne, if the operands are known to be zero-extended,
15029      there is no need to do the fancy shifting up.  */
15030   if (op == DW_OP_eq || op == DW_OP_ne)
15031     {
15032       dw_loc_descr_ref last0, last1;
15033       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
15034 	;
15035       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
15036 	;
15037       /* deref_size zero extends, and for constants we can check
15038 	 whether they are zero extended or not.  */
15039       if (((last0->dw_loc_opc == DW_OP_deref_size
15040 	    && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
15041 	   || (CONST_INT_P (XEXP (rtl, 0))
15042 	       && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
15043 		  == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
15044 	  && ((last1->dw_loc_opc == DW_OP_deref_size
15045 	       && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
15046 	      || (CONST_INT_P (XEXP (rtl, 1))
15047 		  && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
15048 		     == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
15049 	return compare_loc_descriptor (op, op0, op1);
15050 
15051       /* EQ/NE comparison against constant in narrower type than
15052 	 DWARF2_ADDR_SIZE can be performed either as
15053 	 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
15054 	 DW_OP_{eq,ne}
15055 	 or
15056 	 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
15057 	 DW_OP_{eq,ne}.  Pick whatever is shorter.  */
15058       if (CONST_INT_P (XEXP (rtl, 1))
15059 	  && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
15060 	  && (size_of_int_loc_descriptor (shift) + 1
15061 	      + size_of_int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift)
15062 	      >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
15063 		 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
15064 					       & GET_MODE_MASK (op_mode))))
15065 	{
15066 	  add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
15067 	  add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
15068 	  op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
15069 				    & GET_MODE_MASK (op_mode));
15070 	  return compare_loc_descriptor (op, op0, op1);
15071 	}
15072     }
15073   add_loc_descr (&op0, int_loc_descriptor (shift));
15074   add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
15075   if (CONST_INT_P (XEXP (rtl, 1)))
15076     op1 = int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift);
15077   else
15078     {
15079       add_loc_descr (&op1, int_loc_descriptor (shift));
15080       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
15081     }
15082   return compare_loc_descriptor (op, op0, op1);
15083 }
15084 
15085 /* Return location descriptor for unsigned comparison OP RTL.  */
15086 
15087 static dw_loc_descr_ref
scompare_loc_descriptor(enum dwarf_location_atom op,rtx rtl,machine_mode mem_mode)15088 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
15089 			 machine_mode mem_mode)
15090 {
15091   machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
15092   dw_loc_descr_ref op0, op1;
15093 
15094   if (op_mode == VOIDmode)
15095     op_mode = GET_MODE (XEXP (rtl, 1));
15096   if (op_mode == VOIDmode)
15097     return NULL;
15098 
15099   scalar_int_mode int_op_mode;
15100   if (dwarf_strict
15101       && dwarf_version < 5
15102       && (!is_a <scalar_int_mode> (op_mode, &int_op_mode)
15103 	  || GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE))
15104     return NULL;
15105 
15106   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
15107 			    VAR_INIT_STATUS_INITIALIZED);
15108   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
15109 			    VAR_INIT_STATUS_INITIALIZED);
15110 
15111   if (op0 == NULL || op1 == NULL)
15112     return NULL;
15113 
15114   if (is_a <scalar_int_mode> (op_mode, &int_op_mode))
15115     {
15116       if (GET_MODE_SIZE (int_op_mode) < DWARF2_ADDR_SIZE)
15117 	return scompare_loc_descriptor_narrow (op, rtl, int_op_mode, op0, op1);
15118 
15119       if (GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE)
15120 	return scompare_loc_descriptor_wide (op, int_op_mode, op0, op1);
15121     }
15122   return compare_loc_descriptor (op, op0, op1);
15123 }
15124 
15125 /* Return location descriptor for unsigned comparison OP RTL.  */
15126 
15127 static dw_loc_descr_ref
ucompare_loc_descriptor(enum dwarf_location_atom op,rtx rtl,machine_mode mem_mode)15128 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
15129 			 machine_mode mem_mode)
15130 {
15131   dw_loc_descr_ref op0, op1;
15132 
15133   machine_mode test_op_mode = GET_MODE (XEXP (rtl, 0));
15134   if (test_op_mode == VOIDmode)
15135     test_op_mode = GET_MODE (XEXP (rtl, 1));
15136 
15137   scalar_int_mode op_mode;
15138   if (!is_a <scalar_int_mode> (test_op_mode, &op_mode))
15139     return NULL;
15140 
15141   if (dwarf_strict
15142       && dwarf_version < 5
15143       && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
15144     return NULL;
15145 
15146   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
15147 			    VAR_INIT_STATUS_INITIALIZED);
15148   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
15149 			    VAR_INIT_STATUS_INITIALIZED);
15150 
15151   if (op0 == NULL || op1 == NULL)
15152     return NULL;
15153 
15154   if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
15155     {
15156       HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
15157       dw_loc_descr_ref last0, last1;
15158       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
15159 	;
15160       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
15161 	;
15162       if (CONST_INT_P (XEXP (rtl, 0)))
15163 	op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
15164       /* deref_size zero extends, so no need to mask it again.  */
15165       else if (last0->dw_loc_opc != DW_OP_deref_size
15166 	       || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
15167 	{
15168 	  add_loc_descr (&op0, int_loc_descriptor (mask));
15169 	  add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
15170 	}
15171       if (CONST_INT_P (XEXP (rtl, 1)))
15172 	op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
15173       /* deref_size zero extends, so no need to mask it again.  */
15174       else if (last1->dw_loc_opc != DW_OP_deref_size
15175 	       || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
15176 	{
15177 	  add_loc_descr (&op1, int_loc_descriptor (mask));
15178 	  add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
15179 	}
15180     }
15181   else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
15182     {
15183       HOST_WIDE_INT bias = 1;
15184       bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
15185       add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
15186       if (CONST_INT_P (XEXP (rtl, 1)))
15187 	op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
15188 				  + INTVAL (XEXP (rtl, 1)));
15189       else
15190 	add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
15191 					    bias, 0));
15192     }
15193   return compare_loc_descriptor (op, op0, op1);
15194 }
15195 
15196 /* Return location descriptor for {U,S}{MIN,MAX}.  */
15197 
15198 static dw_loc_descr_ref
minmax_loc_descriptor(rtx rtl,machine_mode mode,machine_mode mem_mode)15199 minmax_loc_descriptor (rtx rtl, machine_mode mode,
15200 		       machine_mode mem_mode)
15201 {
15202   enum dwarf_location_atom op;
15203   dw_loc_descr_ref op0, op1, ret;
15204   dw_loc_descr_ref bra_node, drop_node;
15205 
15206   scalar_int_mode int_mode;
15207   if (dwarf_strict
15208       && dwarf_version < 5
15209       && (!is_a <scalar_int_mode> (mode, &int_mode)
15210 	  || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE))
15211     return NULL;
15212 
15213   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15214 			    VAR_INIT_STATUS_INITIALIZED);
15215   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15216 			    VAR_INIT_STATUS_INITIALIZED);
15217 
15218   if (op0 == NULL || op1 == NULL)
15219     return NULL;
15220 
15221   add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
15222   add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
15223   add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
15224   if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
15225     {
15226       /* Checked by the caller.  */
15227       int_mode = as_a <scalar_int_mode> (mode);
15228       if (GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
15229 	{
15230 	  HOST_WIDE_INT mask = GET_MODE_MASK (int_mode);
15231 	  add_loc_descr (&op0, int_loc_descriptor (mask));
15232 	  add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
15233 	  add_loc_descr (&op1, int_loc_descriptor (mask));
15234 	  add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
15235 	}
15236       else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
15237 	{
15238 	  HOST_WIDE_INT bias = 1;
15239 	  bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
15240 	  add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
15241 	  add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
15242 	}
15243     }
15244   else if (is_a <scalar_int_mode> (mode, &int_mode)
15245 	   && GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
15246     {
15247       int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (int_mode)) * BITS_PER_UNIT;
15248       add_loc_descr (&op0, int_loc_descriptor (shift));
15249       add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
15250       add_loc_descr (&op1, int_loc_descriptor (shift));
15251       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
15252     }
15253   else if (is_a <scalar_int_mode> (mode, &int_mode)
15254 	   && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15255     {
15256       dw_die_ref type_die = base_type_for_mode (int_mode, 0);
15257       dw_loc_descr_ref cvt;
15258       if (type_die == NULL)
15259 	return NULL;
15260       cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15261       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15262       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15263       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15264       add_loc_descr (&op0, cvt);
15265       cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15266       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15267       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15268       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15269       add_loc_descr (&op1, cvt);
15270     }
15271 
15272   if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
15273     op = DW_OP_lt;
15274   else
15275     op = DW_OP_gt;
15276   ret = op0;
15277   add_loc_descr (&ret, op1);
15278   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
15279   bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15280   add_loc_descr (&ret, bra_node);
15281   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15282   drop_node = new_loc_descr (DW_OP_drop, 0, 0);
15283   add_loc_descr (&ret, drop_node);
15284   bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15285   bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
15286   if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
15287       && is_a <scalar_int_mode> (mode, &int_mode)
15288       && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15289     ret = convert_descriptor_to_mode (int_mode, ret);
15290   return ret;
15291 }
15292 
15293 /* Helper function for mem_loc_descriptor.  Perform OP binary op,
15294    but after converting arguments to type_die, afterwards
15295    convert back to unsigned.  */
15296 
15297 static dw_loc_descr_ref
typed_binop(enum dwarf_location_atom op,rtx rtl,dw_die_ref type_die,scalar_int_mode mode,machine_mode mem_mode)15298 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
15299 	     scalar_int_mode mode, machine_mode mem_mode)
15300 {
15301   dw_loc_descr_ref cvt, op0, op1;
15302 
15303   if (type_die == NULL)
15304     return NULL;
15305   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15306 			    VAR_INIT_STATUS_INITIALIZED);
15307   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15308 			    VAR_INIT_STATUS_INITIALIZED);
15309   if (op0 == NULL || op1 == NULL)
15310     return NULL;
15311   cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15312   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15313   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15314   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15315   add_loc_descr (&op0, cvt);
15316   cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15317   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15318   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15319   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15320   add_loc_descr (&op1, cvt);
15321   add_loc_descr (&op0, op1);
15322   add_loc_descr (&op0, new_loc_descr (op, 0, 0));
15323   return convert_descriptor_to_mode (mode, op0);
15324 }
15325 
15326 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
15327    const0 is DW_OP_lit0 or corresponding typed constant,
15328    const1 is DW_OP_lit1 or corresponding typed constant
15329    and constMSB is constant with just the MSB bit set
15330    for the mode):
15331        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
15332    L1: const0 DW_OP_swap
15333    L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
15334        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
15335    L3: DW_OP_drop
15336    L4: DW_OP_nop
15337 
15338    CTZ is similar:
15339        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
15340    L1: const0 DW_OP_swap
15341    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
15342        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
15343    L3: DW_OP_drop
15344    L4: DW_OP_nop
15345 
15346    FFS is similar:
15347        DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
15348    L1: const1 DW_OP_swap
15349    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
15350        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
15351    L3: DW_OP_drop
15352    L4: DW_OP_nop  */
15353 
15354 static dw_loc_descr_ref
clz_loc_descriptor(rtx rtl,scalar_int_mode mode,machine_mode mem_mode)15355 clz_loc_descriptor (rtx rtl, scalar_int_mode mode,
15356 		    machine_mode mem_mode)
15357 {
15358   dw_loc_descr_ref op0, ret, tmp;
15359   HOST_WIDE_INT valv;
15360   dw_loc_descr_ref l1jump, l1label;
15361   dw_loc_descr_ref l2jump, l2label;
15362   dw_loc_descr_ref l3jump, l3label;
15363   dw_loc_descr_ref l4jump, l4label;
15364   rtx msb;
15365 
15366   if (GET_MODE (XEXP (rtl, 0)) != mode)
15367     return NULL;
15368 
15369   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15370 			    VAR_INIT_STATUS_INITIALIZED);
15371   if (op0 == NULL)
15372     return NULL;
15373   ret = op0;
15374   if (GET_CODE (rtl) == CLZ)
15375     {
15376       if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
15377 	valv = GET_MODE_BITSIZE (mode);
15378     }
15379   else if (GET_CODE (rtl) == FFS)
15380     valv = 0;
15381   else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
15382     valv = GET_MODE_BITSIZE (mode);
15383   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15384   l1jump = new_loc_descr (DW_OP_bra, 0, 0);
15385   add_loc_descr (&ret, l1jump);
15386   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
15387   tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
15388 			    VAR_INIT_STATUS_INITIALIZED);
15389   if (tmp == NULL)
15390     return NULL;
15391   add_loc_descr (&ret, tmp);
15392   l4jump = new_loc_descr (DW_OP_skip, 0, 0);
15393   add_loc_descr (&ret, l4jump);
15394   l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
15395 				? const1_rtx : const0_rtx,
15396 				mode, mem_mode,
15397 				VAR_INIT_STATUS_INITIALIZED);
15398   if (l1label == NULL)
15399     return NULL;
15400   add_loc_descr (&ret, l1label);
15401   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15402   l2label = new_loc_descr (DW_OP_dup, 0, 0);
15403   add_loc_descr (&ret, l2label);
15404   if (GET_CODE (rtl) != CLZ)
15405     msb = const1_rtx;
15406   else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
15407     msb = GEN_INT (HOST_WIDE_INT_1U
15408 		   << (GET_MODE_BITSIZE (mode) - 1));
15409   else
15410     msb = immed_wide_int_const
15411       (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
15412 			    GET_MODE_PRECISION (mode)), mode);
15413   if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
15414     tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
15415 			 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
15416 			 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
15417   else
15418     tmp = mem_loc_descriptor (msb, mode, mem_mode,
15419 			      VAR_INIT_STATUS_INITIALIZED);
15420   if (tmp == NULL)
15421     return NULL;
15422   add_loc_descr (&ret, tmp);
15423   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15424   l3jump = new_loc_descr (DW_OP_bra, 0, 0);
15425   add_loc_descr (&ret, l3jump);
15426   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15427 			    VAR_INIT_STATUS_INITIALIZED);
15428   if (tmp == NULL)
15429     return NULL;
15430   add_loc_descr (&ret, tmp);
15431   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
15432 				      ? DW_OP_shl : DW_OP_shr, 0, 0));
15433   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15434   add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
15435   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15436   l2jump = new_loc_descr (DW_OP_skip, 0, 0);
15437   add_loc_descr (&ret, l2jump);
15438   l3label = new_loc_descr (DW_OP_drop, 0, 0);
15439   add_loc_descr (&ret, l3label);
15440   l4label = new_loc_descr (DW_OP_nop, 0, 0);
15441   add_loc_descr (&ret, l4label);
15442   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15443   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15444   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15445   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15446   l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15447   l3jump->dw_loc_oprnd1.v.val_loc = l3label;
15448   l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15449   l4jump->dw_loc_oprnd1.v.val_loc = l4label;
15450   return ret;
15451 }
15452 
15453 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
15454    const1 is DW_OP_lit1 or corresponding typed constant):
15455        const0 DW_OP_swap
15456    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
15457        DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
15458    L2: DW_OP_drop
15459 
15460    PARITY is similar:
15461    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
15462        DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
15463    L2: DW_OP_drop  */
15464 
15465 static dw_loc_descr_ref
popcount_loc_descriptor(rtx rtl,scalar_int_mode mode,machine_mode mem_mode)15466 popcount_loc_descriptor (rtx rtl, scalar_int_mode mode,
15467 			 machine_mode mem_mode)
15468 {
15469   dw_loc_descr_ref op0, ret, tmp;
15470   dw_loc_descr_ref l1jump, l1label;
15471   dw_loc_descr_ref l2jump, l2label;
15472 
15473   if (GET_MODE (XEXP (rtl, 0)) != mode)
15474     return NULL;
15475 
15476   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15477 			    VAR_INIT_STATUS_INITIALIZED);
15478   if (op0 == NULL)
15479     return NULL;
15480   ret = op0;
15481   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15482 			    VAR_INIT_STATUS_INITIALIZED);
15483   if (tmp == NULL)
15484     return NULL;
15485   add_loc_descr (&ret, tmp);
15486   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15487   l1label = new_loc_descr (DW_OP_dup, 0, 0);
15488   add_loc_descr (&ret, l1label);
15489   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
15490   add_loc_descr (&ret, l2jump);
15491   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15492   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
15493   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15494 			    VAR_INIT_STATUS_INITIALIZED);
15495   if (tmp == NULL)
15496     return NULL;
15497   add_loc_descr (&ret, tmp);
15498   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15499   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
15500 				      ? DW_OP_plus : DW_OP_xor, 0, 0));
15501   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15502   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15503 			    VAR_INIT_STATUS_INITIALIZED);
15504   add_loc_descr (&ret, tmp);
15505   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15506   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
15507   add_loc_descr (&ret, l1jump);
15508   l2label = new_loc_descr (DW_OP_drop, 0, 0);
15509   add_loc_descr (&ret, l2label);
15510   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15511   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15512   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15513   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15514   return ret;
15515 }
15516 
15517 /* BSWAP (constS is initial shift count, either 56 or 24):
15518        constS const0
15519    L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
15520        const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
15521        DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
15522        DW_OP_minus DW_OP_swap DW_OP_skip <L1>
15523    L2: DW_OP_drop DW_OP_swap DW_OP_drop  */
15524 
15525 static dw_loc_descr_ref
bswap_loc_descriptor(rtx rtl,scalar_int_mode mode,machine_mode mem_mode)15526 bswap_loc_descriptor (rtx rtl, scalar_int_mode mode,
15527 		      machine_mode mem_mode)
15528 {
15529   dw_loc_descr_ref op0, ret, tmp;
15530   dw_loc_descr_ref l1jump, l1label;
15531   dw_loc_descr_ref l2jump, l2label;
15532 
15533   if (BITS_PER_UNIT != 8
15534       || (GET_MODE_BITSIZE (mode) != 32
15535 	  && GET_MODE_BITSIZE (mode) != 64))
15536     return NULL;
15537 
15538   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15539 			    VAR_INIT_STATUS_INITIALIZED);
15540   if (op0 == NULL)
15541     return NULL;
15542 
15543   ret = op0;
15544   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
15545 			    mode, mem_mode,
15546 			    VAR_INIT_STATUS_INITIALIZED);
15547   if (tmp == NULL)
15548     return NULL;
15549   add_loc_descr (&ret, tmp);
15550   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15551 			    VAR_INIT_STATUS_INITIALIZED);
15552   if (tmp == NULL)
15553     return NULL;
15554   add_loc_descr (&ret, tmp);
15555   l1label = new_loc_descr (DW_OP_pick, 2, 0);
15556   add_loc_descr (&ret, l1label);
15557   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
15558 			    mode, mem_mode,
15559 			    VAR_INIT_STATUS_INITIALIZED);
15560   add_loc_descr (&ret, tmp);
15561   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
15562   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
15563   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15564   tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
15565 			    VAR_INIT_STATUS_INITIALIZED);
15566   if (tmp == NULL)
15567     return NULL;
15568   add_loc_descr (&ret, tmp);
15569   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15570   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
15571   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
15572   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
15573   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15574   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15575   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15576 			    VAR_INIT_STATUS_INITIALIZED);
15577   add_loc_descr (&ret, tmp);
15578   add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
15579   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
15580   add_loc_descr (&ret, l2jump);
15581   tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
15582 			    VAR_INIT_STATUS_INITIALIZED);
15583   add_loc_descr (&ret, tmp);
15584   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
15585   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15586   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
15587   add_loc_descr (&ret, l1jump);
15588   l2label = new_loc_descr (DW_OP_drop, 0, 0);
15589   add_loc_descr (&ret, l2label);
15590   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15591   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
15592   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15593   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15594   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15595   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15596   return ret;
15597 }
15598 
15599 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
15600    DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
15601    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
15602    DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
15603 
15604    ROTATERT is similar:
15605    DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
15606    DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
15607    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or  */
15608 
15609 static dw_loc_descr_ref
rotate_loc_descriptor(rtx rtl,scalar_int_mode mode,machine_mode mem_mode)15610 rotate_loc_descriptor (rtx rtl, scalar_int_mode mode,
15611 		       machine_mode mem_mode)
15612 {
15613   rtx rtlop1 = XEXP (rtl, 1);
15614   dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
15615   int i;
15616 
15617   if (is_narrower_int_mode (GET_MODE (rtlop1), mode))
15618     rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
15619   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15620 			    VAR_INIT_STATUS_INITIALIZED);
15621   op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
15622 			    VAR_INIT_STATUS_INITIALIZED);
15623   if (op0 == NULL || op1 == NULL)
15624     return NULL;
15625   if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
15626     for (i = 0; i < 2; i++)
15627       {
15628 	if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
15629 	  mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
15630 					mode, mem_mode,
15631 					VAR_INIT_STATUS_INITIALIZED);
15632 	else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
15633 	  mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
15634 				   ? DW_OP_const4u
15635 				   : HOST_BITS_PER_WIDE_INT == 64
15636 				   ? DW_OP_const8u : DW_OP_constu,
15637 				   GET_MODE_MASK (mode), 0);
15638 	else
15639 	  mask[i] = NULL;
15640 	if (mask[i] == NULL)
15641 	  return NULL;
15642 	add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
15643       }
15644   ret = op0;
15645   add_loc_descr (&ret, op1);
15646   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
15647   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
15648   if (GET_CODE (rtl) == ROTATERT)
15649     {
15650       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
15651       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
15652 					  GET_MODE_BITSIZE (mode), 0));
15653     }
15654   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
15655   if (mask[0] != NULL)
15656     add_loc_descr (&ret, mask[0]);
15657   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
15658   if (mask[1] != NULL)
15659     {
15660       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15661       add_loc_descr (&ret, mask[1]);
15662       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15663     }
15664   if (GET_CODE (rtl) == ROTATE)
15665     {
15666       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
15667       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
15668 					  GET_MODE_BITSIZE (mode), 0));
15669     }
15670   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15671   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
15672   return ret;
15673 }
15674 
15675 /* Helper function for mem_loc_descriptor.  Return DW_OP_GNU_parameter_ref
15676    for DEBUG_PARAMETER_REF RTL.  */
15677 
15678 static dw_loc_descr_ref
parameter_ref_descriptor(rtx rtl)15679 parameter_ref_descriptor (rtx rtl)
15680 {
15681   dw_loc_descr_ref ret;
15682   dw_die_ref ref;
15683 
15684   if (dwarf_strict)
15685     return NULL;
15686   gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
15687   /* With LTO during LTRANS we get the late DIE that refers to the early
15688      DIE, thus we add another indirection here.  This seems to confuse
15689      gdb enough to make gcc.dg/guality/pr68860-1.c FAIL with LTO.  */
15690   ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
15691   ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
15692   if (ref)
15693     {
15694       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15695       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
15696       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
15697     }
15698   else
15699     {
15700       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
15701       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
15702     }
15703   return ret;
15704 }
15705 
15706 /* The following routine converts the RTL for a variable or parameter
15707    (resident in memory) into an equivalent Dwarf representation of a
15708    mechanism for getting the address of that same variable onto the top of a
15709    hypothetical "address evaluation" stack.
15710 
15711    When creating memory location descriptors, we are effectively transforming
15712    the RTL for a memory-resident object into its Dwarf postfix expression
15713    equivalent.  This routine recursively descends an RTL tree, turning
15714    it into Dwarf postfix code as it goes.
15715 
15716    MODE is the mode that should be assumed for the rtl if it is VOIDmode.
15717 
15718    MEM_MODE is the mode of the memory reference, needed to handle some
15719    autoincrement addressing modes.
15720 
15721    Return 0 if we can't represent the location.  */
15722 
15723 dw_loc_descr_ref
mem_loc_descriptor(rtx rtl,machine_mode mode,machine_mode mem_mode,enum var_init_status initialized)15724 mem_loc_descriptor (rtx rtl, machine_mode mode,
15725 		    machine_mode mem_mode,
15726 		    enum var_init_status initialized)
15727 {
15728   dw_loc_descr_ref mem_loc_result = NULL;
15729   enum dwarf_location_atom op;
15730   dw_loc_descr_ref op0, op1;
15731   rtx inner = NULL_RTX;
15732   poly_int64 offset;
15733 
15734   if (mode == VOIDmode)
15735     mode = GET_MODE (rtl);
15736 
15737   /* Note that for a dynamically sized array, the location we will generate a
15738      description of here will be the lowest numbered location which is
15739      actually within the array.  That's *not* necessarily the same as the
15740      zeroth element of the array.  */
15741 
15742   rtl = targetm.delegitimize_address (rtl);
15743 
15744   if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
15745     return NULL;
15746 
15747   scalar_int_mode int_mode = BImode, inner_mode, op1_mode;
15748   switch (GET_CODE (rtl))
15749     {
15750     case POST_INC:
15751     case POST_DEC:
15752     case POST_MODIFY:
15753       return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
15754 
15755     case SUBREG:
15756       /* The case of a subreg may arise when we have a local (register)
15757 	 variable or a formal (register) parameter which doesn't quite fill
15758 	 up an entire register.  For now, just assume that it is
15759 	 legitimate to make the Dwarf info refer to the whole register which
15760 	 contains the given subreg.  */
15761       if (!subreg_lowpart_p (rtl))
15762 	break;
15763       inner = SUBREG_REG (rtl);
15764       /* FALLTHRU */
15765     case TRUNCATE:
15766       if (inner == NULL_RTX)
15767         inner = XEXP (rtl, 0);
15768       if (is_a <scalar_int_mode> (mode, &int_mode)
15769 	  && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
15770 	  && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15771 #ifdef POINTERS_EXTEND_UNSIGNED
15772 	      || (int_mode == Pmode && mem_mode != VOIDmode)
15773 #endif
15774 	     )
15775 	  && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE)
15776 	{
15777 	  mem_loc_result = mem_loc_descriptor (inner,
15778 					       inner_mode,
15779 					       mem_mode, initialized);
15780 	  break;
15781 	}
15782       if (dwarf_strict && dwarf_version < 5)
15783 	break;
15784       if (is_a <scalar_int_mode> (mode, &int_mode)
15785 	  && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
15786 	  ? GET_MODE_SIZE (int_mode) <= GET_MODE_SIZE (inner_mode)
15787 	  : known_eq (GET_MODE_SIZE (mode), GET_MODE_SIZE (GET_MODE (inner))))
15788 	{
15789 	  dw_die_ref type_die;
15790 	  dw_loc_descr_ref cvt;
15791 
15792 	  mem_loc_result = mem_loc_descriptor (inner,
15793 					       GET_MODE (inner),
15794 					       mem_mode, initialized);
15795 	  if (mem_loc_result == NULL)
15796 	    break;
15797 	  type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15798 	  if (type_die == NULL)
15799 	    {
15800 	      mem_loc_result = NULL;
15801 	      break;
15802 	    }
15803 	  if (maybe_ne (GET_MODE_SIZE (mode), GET_MODE_SIZE (GET_MODE (inner))))
15804 	    cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15805 	  else
15806 	    cvt = new_loc_descr (dwarf_OP (DW_OP_reinterpret), 0, 0);
15807 	  cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15808 	  cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15809 	  cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15810 	  add_loc_descr (&mem_loc_result, cvt);
15811 	  if (is_a <scalar_int_mode> (mode, &int_mode)
15812 	      && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
15813 	    {
15814 	      /* Convert it to untyped afterwards.  */
15815 	      cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15816 	      add_loc_descr (&mem_loc_result, cvt);
15817 	    }
15818 	}
15819       break;
15820 
15821     case REG:
15822       if (!is_a <scalar_int_mode> (mode, &int_mode)
15823 	  || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
15824 	      && rtl != arg_pointer_rtx
15825 	      && rtl != frame_pointer_rtx
15826 #ifdef POINTERS_EXTEND_UNSIGNED
15827 	      && (int_mode != Pmode || mem_mode == VOIDmode)
15828 #endif
15829 	      ))
15830 	{
15831 	  dw_die_ref type_die;
15832 	  unsigned int dbx_regnum;
15833 
15834 	  if (dwarf_strict && dwarf_version < 5)
15835 	    break;
15836 	  if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
15837 	    break;
15838 	  type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15839 	  if (type_die == NULL)
15840 	    break;
15841 
15842 	  dbx_regnum = dbx_reg_number (rtl);
15843 	  if (dbx_regnum == IGNORED_DWARF_REGNUM)
15844 	    break;
15845 	  mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_regval_type),
15846 					  dbx_regnum, 0);
15847 	  mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
15848 	  mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
15849 	  mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
15850 	  break;
15851 	}
15852       /* Whenever a register number forms a part of the description of the
15853 	 method for calculating the (dynamic) address of a memory resident
15854 	 object, DWARF rules require the register number be referred to as
15855 	 a "base register".  This distinction is not based in any way upon
15856 	 what category of register the hardware believes the given register
15857 	 belongs to.  This is strictly DWARF terminology we're dealing with
15858 	 here. Note that in cases where the location of a memory-resident
15859 	 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
15860 	 OP_CONST (0)) the actual DWARF location descriptor that we generate
15861 	 may just be OP_BASEREG (basereg).  This may look deceptively like
15862 	 the object in question was allocated to a register (rather than in
15863 	 memory) so DWARF consumers need to be aware of the subtle
15864 	 distinction between OP_REG and OP_BASEREG.  */
15865       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
15866 	mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
15867       else if (stack_realign_drap
15868 	       && crtl->drap_reg
15869 	       && crtl->args.internal_arg_pointer == rtl
15870 	       && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
15871 	{
15872 	  /* If RTL is internal_arg_pointer, which has been optimized
15873 	     out, use DRAP instead.  */
15874 	  mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
15875 					    VAR_INIT_STATUS_INITIALIZED);
15876 	}
15877       break;
15878 
15879     case SIGN_EXTEND:
15880     case ZERO_EXTEND:
15881       if (!is_a <scalar_int_mode> (mode, &int_mode)
15882 	  || !is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode))
15883 	break;
15884       op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
15885 				mem_mode, VAR_INIT_STATUS_INITIALIZED);
15886       if (op0 == 0)
15887 	break;
15888       else if (GET_CODE (rtl) == ZERO_EXTEND
15889 	       && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15890 	       && GET_MODE_BITSIZE (inner_mode) < HOST_BITS_PER_WIDE_INT
15891 	       /* If DW_OP_const{1,2,4}u won't be used, it is shorter
15892 		  to expand zero extend as two shifts instead of
15893 		  masking.  */
15894 	       && GET_MODE_SIZE (inner_mode) <= 4)
15895 	{
15896 	  mem_loc_result = op0;
15897 	  add_loc_descr (&mem_loc_result,
15898 			 int_loc_descriptor (GET_MODE_MASK (inner_mode)));
15899 	  add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
15900 	}
15901       else if (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
15902 	{
15903 	  int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (inner_mode);
15904 	  shift *= BITS_PER_UNIT;
15905 	  if (GET_CODE (rtl) == SIGN_EXTEND)
15906 	    op = DW_OP_shra;
15907 	  else
15908 	    op = DW_OP_shr;
15909 	  mem_loc_result = op0;
15910 	  add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
15911 	  add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
15912 	  add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
15913 	  add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15914 	}
15915       else if (!dwarf_strict || dwarf_version >= 5)
15916 	{
15917 	  dw_die_ref type_die1, type_die2;
15918 	  dw_loc_descr_ref cvt;
15919 
15920 	  type_die1 = base_type_for_mode (inner_mode,
15921 					  GET_CODE (rtl) == ZERO_EXTEND);
15922 	  if (type_die1 == NULL)
15923 	    break;
15924 	  type_die2 = base_type_for_mode (int_mode, 1);
15925 	  if (type_die2 == NULL)
15926 	    break;
15927 	  mem_loc_result = op0;
15928 	  cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15929 	  cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15930 	  cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
15931 	  cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15932 	  add_loc_descr (&mem_loc_result, cvt);
15933 	  cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15934 	  cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15935 	  cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
15936 	  cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15937 	  add_loc_descr (&mem_loc_result, cvt);
15938 	}
15939       break;
15940 
15941     case MEM:
15942       {
15943 	rtx new_rtl = avoid_constant_pool_reference (rtl);
15944 	if (new_rtl != rtl)
15945 	  {
15946 	    mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
15947 						 initialized);
15948 	    if (mem_loc_result != NULL)
15949 	      return mem_loc_result;
15950 	  }
15951       }
15952       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
15953 					   get_address_mode (rtl), mode,
15954 					   VAR_INIT_STATUS_INITIALIZED);
15955       if (mem_loc_result == NULL)
15956 	mem_loc_result = tls_mem_loc_descriptor (rtl);
15957       if (mem_loc_result != NULL)
15958 	{
15959 	  if (!is_a <scalar_int_mode> (mode, &int_mode)
15960 	      || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15961 	    {
15962 	      dw_die_ref type_die;
15963 	      dw_loc_descr_ref deref;
15964 	      HOST_WIDE_INT size;
15965 
15966 	      if (dwarf_strict && dwarf_version < 5)
15967 		return NULL;
15968 	      if (!GET_MODE_SIZE (mode).is_constant (&size))
15969 		return NULL;
15970 	      type_die
15971 		= base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15972 	      if (type_die == NULL)
15973 		return NULL;
15974 	      deref = new_loc_descr (dwarf_OP (DW_OP_deref_type), size, 0);
15975 	      deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
15976 	      deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
15977 	      deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
15978 	      add_loc_descr (&mem_loc_result, deref);
15979 	    }
15980 	  else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
15981 	    add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
15982 	  else
15983 	    add_loc_descr (&mem_loc_result,
15984 			   new_loc_descr (DW_OP_deref_size,
15985 					  GET_MODE_SIZE (int_mode), 0));
15986 	}
15987       break;
15988 
15989     case LO_SUM:
15990       return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
15991 
15992     case LABEL_REF:
15993       /* Some ports can transform a symbol ref into a label ref, because
15994 	 the symbol ref is too far away and has to be dumped into a constant
15995 	 pool.  */
15996     case CONST:
15997     case SYMBOL_REF:
15998     case UNSPEC:
15999       if (!is_a <scalar_int_mode> (mode, &int_mode)
16000 	  || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
16001 #ifdef POINTERS_EXTEND_UNSIGNED
16002 	      && (int_mode != Pmode || mem_mode == VOIDmode)
16003 #endif
16004 	      ))
16005 	break;
16006 
16007       if (GET_CODE (rtl) == UNSPEC)
16008 	{
16009 	  /* If delegitimize_address couldn't do anything with the UNSPEC, we
16010 	     can't express it in the debug info.  This can happen e.g. with some
16011 	     TLS UNSPECs.  Allow UNSPECs formerly from CONST that the backend
16012 	     approves.  */
16013 	  bool not_ok = false;
16014 	  subrtx_var_iterator::array_type array;
16015 	  FOR_EACH_SUBRTX_VAR (iter, array, rtl, ALL)
16016 	    if (*iter != rtl && !CONSTANT_P (*iter))
16017 	      {
16018 		not_ok = true;
16019 		break;
16020 	      }
16021 
16022 	  if (not_ok)
16023 	    break;
16024 
16025 	  FOR_EACH_SUBRTX_VAR (iter, array, rtl, ALL)
16026 	    if (!const_ok_for_output_1 (*iter))
16027 	      {
16028 		not_ok = true;
16029 		break;
16030 	      }
16031 
16032 	  if (not_ok)
16033 	    break;
16034 
16035 	  rtl = gen_rtx_CONST (GET_MODE (rtl), rtl);
16036 	  goto symref;
16037 	}
16038 
16039       if (GET_CODE (rtl) == SYMBOL_REF
16040 	  && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
16041 	{
16042 	  dw_loc_descr_ref temp;
16043 
16044 	  /* If this is not defined, we have no way to emit the data.  */
16045 	  if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
16046 	    break;
16047 
16048           temp = new_addr_loc_descr (rtl, dtprel_true);
16049 
16050 	  /* We check for DWARF 5 here because gdb did not implement
16051 	     DW_OP_form_tls_address until after 7.12.  */
16052 	  mem_loc_result = new_loc_descr ((dwarf_version >= 5
16053 					   ? DW_OP_form_tls_address
16054 					   : DW_OP_GNU_push_tls_address),
16055 					  0, 0);
16056 	  add_loc_descr (&mem_loc_result, temp);
16057 
16058 	  break;
16059 	}
16060 
16061       if (!const_ok_for_output (rtl))
16062 	{
16063 	  if (GET_CODE (rtl) == CONST)
16064 	    switch (GET_CODE (XEXP (rtl, 0)))
16065 	      {
16066 	      case NOT:
16067 		op = DW_OP_not;
16068 		goto try_const_unop;
16069 	      case NEG:
16070 		op = DW_OP_neg;
16071 		goto try_const_unop;
16072 	      try_const_unop:
16073 		rtx arg;
16074 		arg = XEXP (XEXP (rtl, 0), 0);
16075 		if (!CONSTANT_P (arg))
16076 		  arg = gen_rtx_CONST (int_mode, arg);
16077 		op0 = mem_loc_descriptor (arg, int_mode, mem_mode,
16078 					  initialized);
16079 		if (op0)
16080 		  {
16081 		    mem_loc_result = op0;
16082 		    add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16083 		  }
16084 		break;
16085 	      default:
16086 		mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), int_mode,
16087 						     mem_mode, initialized);
16088 		break;
16089 	      }
16090 	  break;
16091 	}
16092 
16093     symref:
16094       mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
16095       vec_safe_push (used_rtx_array, rtl);
16096       break;
16097 
16098     case CONCAT:
16099     case CONCATN:
16100     case VAR_LOCATION:
16101     case DEBUG_IMPLICIT_PTR:
16102       expansion_failed (NULL_TREE, rtl,
16103 			"CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
16104       return 0;
16105 
16106     case ENTRY_VALUE:
16107       if (dwarf_strict && dwarf_version < 5)
16108 	return NULL;
16109       if (REG_P (ENTRY_VALUE_EXP (rtl)))
16110 	{
16111 	  if (!is_a <scalar_int_mode> (mode, &int_mode)
16112 	      || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
16113 	    op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
16114 				      VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16115 	  else
16116 	    {
16117               unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
16118 	      if (dbx_regnum == IGNORED_DWARF_REGNUM)
16119 		return NULL;
16120 	      op0 = one_reg_loc_descriptor (dbx_regnum,
16121 					    VAR_INIT_STATUS_INITIALIZED);
16122 	    }
16123 	}
16124       else if (MEM_P (ENTRY_VALUE_EXP (rtl))
16125 	       && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
16126 	{
16127 	  op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
16128 				    VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16129 	  if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
16130 	    return NULL;
16131 	}
16132       else
16133 	gcc_unreachable ();
16134       if (op0 == NULL)
16135 	return NULL;
16136       mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_entry_value), 0, 0);
16137       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
16138       mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
16139       break;
16140 
16141     case DEBUG_PARAMETER_REF:
16142       mem_loc_result = parameter_ref_descriptor (rtl);
16143       break;
16144 
16145     case PRE_MODIFY:
16146       /* Extract the PLUS expression nested inside and fall into
16147 	 PLUS code below.  */
16148       rtl = XEXP (rtl, 1);
16149       goto plus;
16150 
16151     case PRE_INC:
16152     case PRE_DEC:
16153       /* Turn these into a PLUS expression and fall into the PLUS code
16154 	 below.  */
16155       rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
16156 			  gen_int_mode (GET_CODE (rtl) == PRE_INC
16157 					? GET_MODE_UNIT_SIZE (mem_mode)
16158 					: -GET_MODE_UNIT_SIZE (mem_mode),
16159 					mode));
16160 
16161       /* fall through */
16162 
16163     case PLUS:
16164     plus:
16165       if (is_based_loc (rtl)
16166 	  && is_a <scalar_int_mode> (mode, &int_mode)
16167 	  && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16168 	      || XEXP (rtl, 0) == arg_pointer_rtx
16169 	      || XEXP (rtl, 0) == frame_pointer_rtx))
16170 	mem_loc_result = based_loc_descr (XEXP (rtl, 0),
16171 					  INTVAL (XEXP (rtl, 1)),
16172 					  VAR_INIT_STATUS_INITIALIZED);
16173       else
16174 	{
16175 	  mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16176 					       VAR_INIT_STATUS_INITIALIZED);
16177 	  if (mem_loc_result == 0)
16178 	    break;
16179 
16180 	  if (CONST_INT_P (XEXP (rtl, 1))
16181 	      && (GET_MODE_SIZE (as_a <scalar_int_mode> (mode))
16182 		  <= DWARF2_ADDR_SIZE))
16183 	    loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
16184 	  else
16185 	    {
16186 	      op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16187 					VAR_INIT_STATUS_INITIALIZED);
16188 	      if (op1 == 0)
16189 		return NULL;
16190 	      add_loc_descr (&mem_loc_result, op1);
16191 	      add_loc_descr (&mem_loc_result,
16192 			     new_loc_descr (DW_OP_plus, 0, 0));
16193 	    }
16194 	}
16195       break;
16196 
16197     /* If a pseudo-reg is optimized away, it is possible for it to
16198        be replaced with a MEM containing a multiply or shift.  */
16199     case MINUS:
16200       op = DW_OP_minus;
16201       goto do_binop;
16202 
16203     case MULT:
16204       op = DW_OP_mul;
16205       goto do_binop;
16206 
16207     case DIV:
16208       if ((!dwarf_strict || dwarf_version >= 5)
16209 	  && is_a <scalar_int_mode> (mode, &int_mode)
16210 	  && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
16211 	{
16212 	  mem_loc_result = typed_binop (DW_OP_div, rtl,
16213 					base_type_for_mode (mode, 0),
16214 					int_mode, mem_mode);
16215 	  break;
16216 	}
16217       op = DW_OP_div;
16218       goto do_binop;
16219 
16220     case UMOD:
16221       op = DW_OP_mod;
16222       goto do_binop;
16223 
16224     case ASHIFT:
16225       op = DW_OP_shl;
16226       goto do_shift;
16227 
16228     case ASHIFTRT:
16229       op = DW_OP_shra;
16230       goto do_shift;
16231 
16232     case LSHIFTRT:
16233       op = DW_OP_shr;
16234       goto do_shift;
16235 
16236     do_shift:
16237       if (!is_a <scalar_int_mode> (mode, &int_mode))
16238 	break;
16239       op0 = mem_loc_descriptor (XEXP (rtl, 0), int_mode, mem_mode,
16240 				VAR_INIT_STATUS_INITIALIZED);
16241       {
16242 	rtx rtlop1 = XEXP (rtl, 1);
16243 	if (is_a <scalar_int_mode> (GET_MODE (rtlop1), &op1_mode)
16244 	    && GET_MODE_BITSIZE (op1_mode) < GET_MODE_BITSIZE (int_mode))
16245 	  rtlop1 = gen_rtx_ZERO_EXTEND (int_mode, rtlop1);
16246 	op1 = mem_loc_descriptor (rtlop1, int_mode, mem_mode,
16247 				  VAR_INIT_STATUS_INITIALIZED);
16248       }
16249 
16250       if (op0 == 0 || op1 == 0)
16251 	break;
16252 
16253       mem_loc_result = op0;
16254       add_loc_descr (&mem_loc_result, op1);
16255       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16256       break;
16257 
16258     case AND:
16259       op = DW_OP_and;
16260       goto do_binop;
16261 
16262     case IOR:
16263       op = DW_OP_or;
16264       goto do_binop;
16265 
16266     case XOR:
16267       op = DW_OP_xor;
16268       goto do_binop;
16269 
16270     do_binop:
16271       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16272 				VAR_INIT_STATUS_INITIALIZED);
16273       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16274 				VAR_INIT_STATUS_INITIALIZED);
16275 
16276       if (op0 == 0 || op1 == 0)
16277 	break;
16278 
16279       mem_loc_result = op0;
16280       add_loc_descr (&mem_loc_result, op1);
16281       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16282       break;
16283 
16284     case MOD:
16285       if ((!dwarf_strict || dwarf_version >= 5)
16286 	  && is_a <scalar_int_mode> (mode, &int_mode)
16287 	  && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
16288 	{
16289 	  mem_loc_result = typed_binop (DW_OP_mod, rtl,
16290 					base_type_for_mode (mode, 0),
16291 					int_mode, mem_mode);
16292 	  break;
16293 	}
16294 
16295       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16296 				VAR_INIT_STATUS_INITIALIZED);
16297       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16298 				VAR_INIT_STATUS_INITIALIZED);
16299 
16300       if (op0 == 0 || op1 == 0)
16301 	break;
16302 
16303       mem_loc_result = op0;
16304       add_loc_descr (&mem_loc_result, op1);
16305       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
16306       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
16307       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
16308       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
16309       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
16310       break;
16311 
16312     case UDIV:
16313       if ((!dwarf_strict || dwarf_version >= 5)
16314 	  && is_a <scalar_int_mode> (mode, &int_mode))
16315 	{
16316 	  if (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
16317 	    {
16318 	      op = DW_OP_div;
16319 	      goto do_binop;
16320 	    }
16321 	  mem_loc_result = typed_binop (DW_OP_div, rtl,
16322 					base_type_for_mode (int_mode, 1),
16323 					int_mode, mem_mode);
16324 	}
16325       break;
16326 
16327     case NOT:
16328       op = DW_OP_not;
16329       goto do_unop;
16330 
16331     case ABS:
16332       op = DW_OP_abs;
16333       goto do_unop;
16334 
16335     case NEG:
16336       op = DW_OP_neg;
16337       goto do_unop;
16338 
16339     do_unop:
16340       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16341 				VAR_INIT_STATUS_INITIALIZED);
16342 
16343       if (op0 == 0)
16344 	break;
16345 
16346       mem_loc_result = op0;
16347       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16348       break;
16349 
16350     case CONST_INT:
16351       if (!is_a <scalar_int_mode> (mode, &int_mode)
16352 	  || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16353 #ifdef POINTERS_EXTEND_UNSIGNED
16354 	  || (int_mode == Pmode
16355 	      && mem_mode != VOIDmode
16356 	      && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
16357 #endif
16358 	  )
16359 	{
16360 	  mem_loc_result = int_loc_descriptor (INTVAL (rtl));
16361 	  break;
16362 	}
16363       if ((!dwarf_strict || dwarf_version >= 5)
16364 	  && (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT
16365 	      || GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_DOUBLE_INT))
16366 	{
16367 	  dw_die_ref type_die = base_type_for_mode (int_mode, 1);
16368 	  scalar_int_mode amode;
16369 	  if (type_die == NULL)
16370 	    return NULL;
16371 	  if (INTVAL (rtl) >= 0
16372 	      && (int_mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT, 0)
16373 		  .exists (&amode))
16374 	      && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
16375 	      /* const DW_OP_convert <XXX> vs.
16376 		 DW_OP_const_type <XXX, 1, const>.  */
16377 	      && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
16378 		 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (int_mode))
16379 	    {
16380 	      mem_loc_result = int_loc_descriptor (INTVAL (rtl));
16381 	      op0 = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16382 	      op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16383 	      op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16384 	      op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
16385 	      add_loc_descr (&mem_loc_result, op0);
16386 	      return mem_loc_result;
16387 	    }
16388 	  mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0,
16389 					  INTVAL (rtl));
16390 	  mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16391 	  mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16392 	  mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
16393 	  if (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT)
16394 	    mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
16395 	  else
16396 	    {
16397 	      mem_loc_result->dw_loc_oprnd2.val_class
16398 		= dw_val_class_const_double;
16399 	      mem_loc_result->dw_loc_oprnd2.v.val_double
16400 		= double_int::from_shwi (INTVAL (rtl));
16401 	    }
16402 	}
16403       break;
16404 
16405     case CONST_DOUBLE:
16406       if (!dwarf_strict || dwarf_version >= 5)
16407 	{
16408 	  dw_die_ref type_die;
16409 
16410 	  /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
16411 	     CONST_DOUBLE rtx could represent either a large integer
16412 	     or a floating-point constant.  If TARGET_SUPPORTS_WIDE_INT != 0,
16413 	     the value is always a floating point constant.
16414 
16415 	     When it is an integer, a CONST_DOUBLE is used whenever
16416 	     the constant requires 2 HWIs to be adequately represented.
16417 	     We output CONST_DOUBLEs as blocks.  */
16418 	  if (mode == VOIDmode
16419 	      || (GET_MODE (rtl) == VOIDmode
16420 		  && maybe_ne (GET_MODE_BITSIZE (mode),
16421 			       HOST_BITS_PER_DOUBLE_INT)))
16422 	    break;
16423 	  type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16424 	  if (type_die == NULL)
16425 	    return NULL;
16426 	  mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
16427 	  mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16428 	  mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16429 	  mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
16430 #if TARGET_SUPPORTS_WIDE_INT == 0
16431 	  if (!SCALAR_FLOAT_MODE_P (mode))
16432 	    {
16433 	      mem_loc_result->dw_loc_oprnd2.val_class
16434 		= dw_val_class_const_double;
16435 	      mem_loc_result->dw_loc_oprnd2.v.val_double
16436 		= rtx_to_double_int (rtl);
16437 	    }
16438 	  else
16439 #endif
16440 	    {
16441 	      scalar_float_mode float_mode = as_a <scalar_float_mode> (mode);
16442 	      unsigned int length = GET_MODE_SIZE (float_mode);
16443 	      unsigned char *array = ggc_vec_alloc<unsigned char> (length);
16444 	      unsigned int elt_size = insert_float (rtl, array);
16445 
16446 	      mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
16447 	      mem_loc_result->dw_loc_oprnd2.v.val_vec.length
16448 		= length / elt_size;
16449 	      mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
16450 	      mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
16451 	    }
16452 	}
16453       break;
16454 
16455     case CONST_WIDE_INT:
16456       if (!dwarf_strict || dwarf_version >= 5)
16457 	{
16458 	  dw_die_ref type_die;
16459 
16460 	  type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16461 	  if (type_die == NULL)
16462 	    return NULL;
16463 	  mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
16464 	  mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16465 	  mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16466 	  mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
16467 	  mem_loc_result->dw_loc_oprnd2.val_class
16468 	    = dw_val_class_wide_int;
16469 	  mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
16470 	  *mem_loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
16471 	}
16472       break;
16473 
16474     case CONST_POLY_INT:
16475       mem_loc_result = int_loc_descriptor (rtx_to_poly_int64 (rtl));
16476       break;
16477 
16478     case EQ:
16479       mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
16480       break;
16481 
16482     case GE:
16483       mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
16484       break;
16485 
16486     case GT:
16487       mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
16488       break;
16489 
16490     case LE:
16491       mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
16492       break;
16493 
16494     case LT:
16495       mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
16496       break;
16497 
16498     case NE:
16499       mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
16500       break;
16501 
16502     case GEU:
16503       mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
16504       break;
16505 
16506     case GTU:
16507       mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
16508       break;
16509 
16510     case LEU:
16511       mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
16512       break;
16513 
16514     case LTU:
16515       mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
16516       break;
16517 
16518     case UMIN:
16519     case UMAX:
16520       if (!SCALAR_INT_MODE_P (mode))
16521 	break;
16522       /* FALLTHRU */
16523     case SMIN:
16524     case SMAX:
16525       mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
16526       break;
16527 
16528     case ZERO_EXTRACT:
16529     case SIGN_EXTRACT:
16530       if (CONST_INT_P (XEXP (rtl, 1))
16531 	  && CONST_INT_P (XEXP (rtl, 2))
16532 	  && is_a <scalar_int_mode> (mode, &int_mode)
16533 	  && is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode)
16534 	  && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16535 	  && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE
16536 	  && ((unsigned) INTVAL (XEXP (rtl, 1))
16537 	      + (unsigned) INTVAL (XEXP (rtl, 2))
16538 	      <= GET_MODE_BITSIZE (int_mode)))
16539 	{
16540 	  int shift, size;
16541 	  op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
16542 				    mem_mode, VAR_INIT_STATUS_INITIALIZED);
16543 	  if (op0 == 0)
16544 	    break;
16545 	  if (GET_CODE (rtl) == SIGN_EXTRACT)
16546 	    op = DW_OP_shra;
16547 	  else
16548 	    op = DW_OP_shr;
16549 	  mem_loc_result = op0;
16550 	  size = INTVAL (XEXP (rtl, 1));
16551 	  shift = INTVAL (XEXP (rtl, 2));
16552 	  if (BITS_BIG_ENDIAN)
16553 	    shift = GET_MODE_BITSIZE (inner_mode) - shift - size;
16554 	  if (shift + size != (int) DWARF2_ADDR_SIZE)
16555 	    {
16556 	      add_loc_descr (&mem_loc_result,
16557 			     int_loc_descriptor (DWARF2_ADDR_SIZE
16558 						 - shift - size));
16559 	      add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
16560 	    }
16561 	  if (size != (int) DWARF2_ADDR_SIZE)
16562 	    {
16563 	      add_loc_descr (&mem_loc_result,
16564 			     int_loc_descriptor (DWARF2_ADDR_SIZE - size));
16565 	      add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16566 	    }
16567 	}
16568       break;
16569 
16570     case IF_THEN_ELSE:
16571       {
16572 	dw_loc_descr_ref op2, bra_node, drop_node;
16573 	op0 = mem_loc_descriptor (XEXP (rtl, 0),
16574 				  GET_MODE (XEXP (rtl, 0)) == VOIDmode
16575 				  ? word_mode : GET_MODE (XEXP (rtl, 0)),
16576 				  mem_mode, VAR_INIT_STATUS_INITIALIZED);
16577 	op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16578 				  VAR_INIT_STATUS_INITIALIZED);
16579 	op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
16580 				  VAR_INIT_STATUS_INITIALIZED);
16581 	if (op0 == NULL || op1 == NULL || op2 == NULL)
16582 	  break;
16583 
16584 	mem_loc_result = op1;
16585 	add_loc_descr (&mem_loc_result, op2);
16586 	add_loc_descr (&mem_loc_result, op0);
16587 	bra_node = new_loc_descr (DW_OP_bra, 0, 0);
16588 	add_loc_descr (&mem_loc_result, bra_node);
16589 	add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
16590 	drop_node = new_loc_descr (DW_OP_drop, 0, 0);
16591 	add_loc_descr (&mem_loc_result, drop_node);
16592 	bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
16593 	bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
16594       }
16595       break;
16596 
16597     case FLOAT_EXTEND:
16598     case FLOAT_TRUNCATE:
16599     case FLOAT:
16600     case UNSIGNED_FLOAT:
16601     case FIX:
16602     case UNSIGNED_FIX:
16603       if (!dwarf_strict || dwarf_version >= 5)
16604 	{
16605 	  dw_die_ref type_die;
16606 	  dw_loc_descr_ref cvt;
16607 
16608 	  op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
16609 				    mem_mode, VAR_INIT_STATUS_INITIALIZED);
16610 	  if (op0 == NULL)
16611 	    break;
16612 	  if (is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &int_mode)
16613 	      && (GET_CODE (rtl) == FLOAT
16614 		  || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE))
16615 	    {
16616 	      type_die = base_type_for_mode (int_mode,
16617 					     GET_CODE (rtl) == UNSIGNED_FLOAT);
16618 	      if (type_die == NULL)
16619 		break;
16620 	      cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16621 	      cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16622 	      cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16623 	      cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16624 	      add_loc_descr (&op0, cvt);
16625 	    }
16626 	  type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
16627 	  if (type_die == NULL)
16628 	    break;
16629 	  cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16630 	  cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16631 	  cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16632 	  cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16633 	  add_loc_descr (&op0, cvt);
16634 	  if (is_a <scalar_int_mode> (mode, &int_mode)
16635 	      && (GET_CODE (rtl) == FIX
16636 		  || GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE))
16637 	    {
16638 	      op0 = convert_descriptor_to_mode (int_mode, op0);
16639 	      if (op0 == NULL)
16640 		break;
16641 	    }
16642 	  mem_loc_result = op0;
16643 	}
16644       break;
16645 
16646     case CLZ:
16647     case CTZ:
16648     case FFS:
16649       if (is_a <scalar_int_mode> (mode, &int_mode))
16650 	mem_loc_result = clz_loc_descriptor (rtl, int_mode, mem_mode);
16651       break;
16652 
16653     case POPCOUNT:
16654     case PARITY:
16655       if (is_a <scalar_int_mode> (mode, &int_mode))
16656 	mem_loc_result = popcount_loc_descriptor (rtl, int_mode, mem_mode);
16657       break;
16658 
16659     case BSWAP:
16660       if (is_a <scalar_int_mode> (mode, &int_mode))
16661 	mem_loc_result = bswap_loc_descriptor (rtl, int_mode, mem_mode);
16662       break;
16663 
16664     case ROTATE:
16665     case ROTATERT:
16666       if (is_a <scalar_int_mode> (mode, &int_mode))
16667 	mem_loc_result = rotate_loc_descriptor (rtl, int_mode, mem_mode);
16668       break;
16669 
16670     case COMPARE:
16671       /* In theory, we could implement the above.  */
16672       /* DWARF cannot represent the unsigned compare operations
16673 	 natively.  */
16674     case SS_MULT:
16675     case US_MULT:
16676     case SS_DIV:
16677     case US_DIV:
16678     case SS_PLUS:
16679     case US_PLUS:
16680     case SS_MINUS:
16681     case US_MINUS:
16682     case SS_NEG:
16683     case US_NEG:
16684     case SS_ABS:
16685     case SS_ASHIFT:
16686     case US_ASHIFT:
16687     case SS_TRUNCATE:
16688     case US_TRUNCATE:
16689     case UNORDERED:
16690     case ORDERED:
16691     case UNEQ:
16692     case UNGE:
16693     case UNGT:
16694     case UNLE:
16695     case UNLT:
16696     case LTGT:
16697     case FRACT_CONVERT:
16698     case UNSIGNED_FRACT_CONVERT:
16699     case SAT_FRACT:
16700     case UNSIGNED_SAT_FRACT:
16701     case SQRT:
16702     case ASM_OPERANDS:
16703     case VEC_MERGE:
16704     case VEC_SELECT:
16705     case VEC_CONCAT:
16706     case VEC_DUPLICATE:
16707     case VEC_SERIES:
16708     case HIGH:
16709     case FMA:
16710     case STRICT_LOW_PART:
16711     case CONST_VECTOR:
16712     case CONST_FIXED:
16713     case CLRSB:
16714     case CLOBBER:
16715       break;
16716 
16717     case CONST_STRING:
16718       resolve_one_addr (&rtl);
16719       goto symref;
16720 
16721     /* RTL sequences inside PARALLEL record a series of DWARF operations for
16722        the expression.  An UNSPEC rtx represents a raw DWARF operation,
16723        new_loc_descr is called for it to build the operation directly.
16724        Otherwise mem_loc_descriptor is called recursively.  */
16725     case PARALLEL:
16726       {
16727 	int index = 0;
16728 	dw_loc_descr_ref exp_result = NULL;
16729 
16730 	for (; index < XVECLEN (rtl, 0); index++)
16731 	  {
16732 	    rtx elem = XVECEXP (rtl, 0, index);
16733 	    if (GET_CODE (elem) == UNSPEC)
16734 	      {
16735 		/* Each DWARF operation UNSPEC contain two operands, if
16736 		   one operand is not used for the operation, const0_rtx is
16737 		   passed.  */
16738 		gcc_assert (XVECLEN (elem, 0) == 2);
16739 
16740 		HOST_WIDE_INT dw_op = XINT (elem, 1);
16741 		HOST_WIDE_INT oprnd1 = INTVAL (XVECEXP (elem, 0, 0));
16742 		HOST_WIDE_INT oprnd2 = INTVAL (XVECEXP (elem, 0, 1));
16743 		exp_result
16744 		  = new_loc_descr ((enum dwarf_location_atom) dw_op, oprnd1,
16745 				   oprnd2);
16746 	      }
16747 	    else
16748 	      exp_result
16749 		= mem_loc_descriptor (elem, mode, mem_mode,
16750 				      VAR_INIT_STATUS_INITIALIZED);
16751 
16752 	    if (!mem_loc_result)
16753 	      mem_loc_result = exp_result;
16754 	    else
16755 	      add_loc_descr (&mem_loc_result, exp_result);
16756 	  }
16757 
16758 	break;
16759       }
16760 
16761     default:
16762       if (flag_checking)
16763 	{
16764 	  print_rtl (stderr, rtl);
16765 	  gcc_unreachable ();
16766 	}
16767       break;
16768     }
16769 
16770   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
16771     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
16772 
16773   return mem_loc_result;
16774 }
16775 
16776 /* Return a descriptor that describes the concatenation of two locations.
16777    This is typically a complex variable.  */
16778 
16779 static dw_loc_descr_ref
concat_loc_descriptor(rtx x0,rtx x1,enum var_init_status initialized)16780 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
16781 {
16782   /* At present we only track constant-sized pieces.  */
16783   unsigned int size0, size1;
16784   if (!GET_MODE_SIZE (GET_MODE (x0)).is_constant (&size0)
16785       || !GET_MODE_SIZE (GET_MODE (x1)).is_constant (&size1))
16786     return 0;
16787 
16788   dw_loc_descr_ref cc_loc_result = NULL;
16789   dw_loc_descr_ref x0_ref
16790     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16791   dw_loc_descr_ref x1_ref
16792     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16793 
16794   if (x0_ref == 0 || x1_ref == 0)
16795     return 0;
16796 
16797   cc_loc_result = x0_ref;
16798   add_loc_descr_op_piece (&cc_loc_result, size0);
16799 
16800   add_loc_descr (&cc_loc_result, x1_ref);
16801   add_loc_descr_op_piece (&cc_loc_result, size1);
16802 
16803   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
16804     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
16805 
16806   return cc_loc_result;
16807 }
16808 
16809 /* Return a descriptor that describes the concatenation of N
16810    locations.  */
16811 
16812 static dw_loc_descr_ref
concatn_loc_descriptor(rtx concatn,enum var_init_status initialized)16813 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
16814 {
16815   unsigned int i;
16816   dw_loc_descr_ref cc_loc_result = NULL;
16817   unsigned int n = XVECLEN (concatn, 0);
16818   unsigned int size;
16819 
16820   for (i = 0; i < n; ++i)
16821     {
16822       dw_loc_descr_ref ref;
16823       rtx x = XVECEXP (concatn, 0, i);
16824 
16825       /* At present we only track constant-sized pieces.  */
16826       if (!GET_MODE_SIZE (GET_MODE (x)).is_constant (&size))
16827 	return NULL;
16828 
16829       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16830       if (ref == NULL)
16831 	return NULL;
16832 
16833       add_loc_descr (&cc_loc_result, ref);
16834       add_loc_descr_op_piece (&cc_loc_result, size);
16835     }
16836 
16837   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
16838     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
16839 
16840   return cc_loc_result;
16841 }
16842 
16843 /* Helper function for loc_descriptor.  Return DW_OP_implicit_pointer
16844    for DEBUG_IMPLICIT_PTR RTL.  */
16845 
16846 static dw_loc_descr_ref
implicit_ptr_descriptor(rtx rtl,HOST_WIDE_INT offset)16847 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
16848 {
16849   dw_loc_descr_ref ret;
16850   dw_die_ref ref;
16851 
16852   if (dwarf_strict && dwarf_version < 5)
16853     return NULL;
16854   gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
16855 	      || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
16856 	      || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
16857   ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
16858   ret = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
16859   ret->dw_loc_oprnd2.val_class = dw_val_class_const;
16860   if (ref)
16861     {
16862       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16863       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
16864       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
16865     }
16866   else
16867     {
16868       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
16869       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
16870     }
16871   return ret;
16872 }
16873 
16874 /* Output a proper Dwarf location descriptor for a variable or parameter
16875    which is either allocated in a register or in a memory location.  For a
16876    register, we just generate an OP_REG and the register number.  For a
16877    memory location we provide a Dwarf postfix expression describing how to
16878    generate the (dynamic) address of the object onto the address stack.
16879 
16880    MODE is mode of the decl if this loc_descriptor is going to be used in
16881    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
16882    allowed, VOIDmode otherwise.
16883 
16884    If we don't know how to describe it, return 0.  */
16885 
16886 static dw_loc_descr_ref
loc_descriptor(rtx rtl,machine_mode mode,enum var_init_status initialized)16887 loc_descriptor (rtx rtl, machine_mode mode,
16888 		enum var_init_status initialized)
16889 {
16890   dw_loc_descr_ref loc_result = NULL;
16891   scalar_int_mode int_mode;
16892 
16893   switch (GET_CODE (rtl))
16894     {
16895     case SUBREG:
16896       /* The case of a subreg may arise when we have a local (register)
16897 	 variable or a formal (register) parameter which doesn't quite fill
16898 	 up an entire register.  For now, just assume that it is
16899 	 legitimate to make the Dwarf info refer to the whole register which
16900 	 contains the given subreg.  */
16901       if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
16902 	loc_result = loc_descriptor (SUBREG_REG (rtl),
16903 				     GET_MODE (SUBREG_REG (rtl)), initialized);
16904       else
16905 	goto do_default;
16906       break;
16907 
16908     case REG:
16909       loc_result = reg_loc_descriptor (rtl, initialized);
16910       break;
16911 
16912     case MEM:
16913       loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
16914 				       GET_MODE (rtl), initialized);
16915       if (loc_result == NULL)
16916 	loc_result = tls_mem_loc_descriptor (rtl);
16917       if (loc_result == NULL)
16918 	{
16919 	  rtx new_rtl = avoid_constant_pool_reference (rtl);
16920 	  if (new_rtl != rtl)
16921 	    loc_result = loc_descriptor (new_rtl, mode, initialized);
16922 	}
16923       break;
16924 
16925     case CONCAT:
16926       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
16927 					  initialized);
16928       break;
16929 
16930     case CONCATN:
16931       loc_result = concatn_loc_descriptor (rtl, initialized);
16932       break;
16933 
16934     case VAR_LOCATION:
16935       /* Single part.  */
16936       if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
16937 	{
16938 	  rtx loc = PAT_VAR_LOCATION_LOC (rtl);
16939 	  if (GET_CODE (loc) == EXPR_LIST)
16940 	    loc = XEXP (loc, 0);
16941 	  loc_result = loc_descriptor (loc, mode, initialized);
16942 	  break;
16943 	}
16944 
16945       rtl = XEXP (rtl, 1);
16946       /* FALLTHRU */
16947 
16948     case PARALLEL:
16949       {
16950 	rtvec par_elems = XVEC (rtl, 0);
16951 	int num_elem = GET_NUM_ELEM (par_elems);
16952 	machine_mode mode;
16953 	int i, size;
16954 
16955 	/* Create the first one, so we have something to add to.  */
16956 	loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
16957 				     VOIDmode, initialized);
16958 	if (loc_result == NULL)
16959 	  return NULL;
16960 	mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
16961 	/* At present we only track constant-sized pieces.  */
16962 	if (!GET_MODE_SIZE (mode).is_constant (&size))
16963 	  return NULL;
16964 	add_loc_descr_op_piece (&loc_result, size);
16965 	for (i = 1; i < num_elem; i++)
16966 	  {
16967 	    dw_loc_descr_ref temp;
16968 
16969 	    temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
16970 				   VOIDmode, initialized);
16971 	    if (temp == NULL)
16972 	      return NULL;
16973 	    add_loc_descr (&loc_result, temp);
16974 	    mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
16975 	    /* At present we only track constant-sized pieces.  */
16976 	    if (!GET_MODE_SIZE (mode).is_constant (&size))
16977 	      return NULL;
16978 	    add_loc_descr_op_piece (&loc_result, size);
16979 	  }
16980       }
16981       break;
16982 
16983     case CONST_INT:
16984       if (mode != VOIDmode && mode != BLKmode)
16985 	{
16986 	  int_mode = as_a <scalar_int_mode> (mode);
16987 	  loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (int_mode),
16988 						      INTVAL (rtl));
16989 	}
16990       break;
16991 
16992     case CONST_DOUBLE:
16993       if (mode == VOIDmode)
16994 	mode = GET_MODE (rtl);
16995 
16996       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
16997 	{
16998 	  gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
16999 
17000 	  /* Note that a CONST_DOUBLE rtx could represent either an integer
17001 	     or a floating-point constant.  A CONST_DOUBLE is used whenever
17002 	     the constant requires more than one word in order to be
17003 	     adequately represented.  We output CONST_DOUBLEs as blocks.  */
17004 	  scalar_mode smode = as_a <scalar_mode> (mode);
17005 	  loc_result = new_loc_descr (DW_OP_implicit_value,
17006 				      GET_MODE_SIZE (smode), 0);
17007 #if TARGET_SUPPORTS_WIDE_INT == 0
17008 	  if (!SCALAR_FLOAT_MODE_P (smode))
17009 	    {
17010 	      loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
17011 	      loc_result->dw_loc_oprnd2.v.val_double
17012 	        = rtx_to_double_int (rtl);
17013 	    }
17014 	  else
17015 #endif
17016 	    {
17017 	      unsigned int length = GET_MODE_SIZE (smode);
17018 	      unsigned char *array = ggc_vec_alloc<unsigned char> (length);
17019 	      unsigned int elt_size = insert_float (rtl, array);
17020 
17021 	      loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
17022 	      loc_result->dw_loc_oprnd2.v.val_vec.length = length / elt_size;
17023 	      loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
17024 	      loc_result->dw_loc_oprnd2.v.val_vec.array = array;
17025 	    }
17026 	}
17027       break;
17028 
17029     case CONST_WIDE_INT:
17030       if (mode == VOIDmode)
17031 	mode = GET_MODE (rtl);
17032 
17033       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
17034 	{
17035 	  int_mode = as_a <scalar_int_mode> (mode);
17036 	  loc_result = new_loc_descr (DW_OP_implicit_value,
17037 				      GET_MODE_SIZE (int_mode), 0);
17038 	  loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
17039 	  loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
17040 	  *loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, int_mode);
17041 	}
17042       break;
17043 
17044     case CONST_VECTOR:
17045       if (mode == VOIDmode)
17046 	mode = GET_MODE (rtl);
17047 
17048       if (mode != VOIDmode
17049 	  /* The combination of a length and byte elt_size doesn't extend
17050 	     naturally to boolean vectors, where several elements are packed
17051 	     into the same byte.  */
17052 	  && GET_MODE_CLASS (mode) != MODE_VECTOR_BOOL
17053 	  && (dwarf_version >= 4 || !dwarf_strict))
17054 	{
17055 	  unsigned int length;
17056 	  if (!CONST_VECTOR_NUNITS (rtl).is_constant (&length))
17057 	    return NULL;
17058 
17059 	  unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
17060 	  unsigned char *array
17061 	    = ggc_vec_alloc<unsigned char> (length * elt_size);
17062 	  unsigned int i;
17063 	  unsigned char *p;
17064 	  machine_mode imode = GET_MODE_INNER (mode);
17065 
17066 	  gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
17067 	  switch (GET_MODE_CLASS (mode))
17068 	    {
17069 	    case MODE_VECTOR_INT:
17070 	      for (i = 0, p = array; i < length; i++, p += elt_size)
17071 		{
17072 		  rtx elt = CONST_VECTOR_ELT (rtl, i);
17073 		  insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
17074 		}
17075 	      break;
17076 
17077 	    case MODE_VECTOR_FLOAT:
17078 	      for (i = 0, p = array; i < length; i++, p += elt_size)
17079 		{
17080 		  rtx elt = CONST_VECTOR_ELT (rtl, i);
17081 		  insert_float (elt, p);
17082 		}
17083 	      break;
17084 
17085 	    default:
17086 	      gcc_unreachable ();
17087 	    }
17088 
17089 	  loc_result = new_loc_descr (DW_OP_implicit_value,
17090 				      length * elt_size, 0);
17091 	  loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
17092 	  loc_result->dw_loc_oprnd2.v.val_vec.length = length;
17093 	  loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
17094 	  loc_result->dw_loc_oprnd2.v.val_vec.array = array;
17095 	}
17096       break;
17097 
17098     case CONST:
17099       if (mode == VOIDmode
17100 	  || CONST_SCALAR_INT_P (XEXP (rtl, 0))
17101 	  || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
17102 	  || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
17103 	{
17104 	  loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
17105 	  break;
17106 	}
17107       /* FALLTHROUGH */
17108     case SYMBOL_REF:
17109       if (!const_ok_for_output (rtl))
17110 	break;
17111       /* FALLTHROUGH */
17112     case LABEL_REF:
17113       if (is_a <scalar_int_mode> (mode, &int_mode)
17114 	  && GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE
17115 	  && (dwarf_version >= 4 || !dwarf_strict))
17116 	{
17117          loc_result = new_addr_loc_descr (rtl, dtprel_false);
17118 	  add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
17119 	  vec_safe_push (used_rtx_array, rtl);
17120 	}
17121       break;
17122 
17123     case DEBUG_IMPLICIT_PTR:
17124       loc_result = implicit_ptr_descriptor (rtl, 0);
17125       break;
17126 
17127     case PLUS:
17128       if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
17129 	  && CONST_INT_P (XEXP (rtl, 1)))
17130 	{
17131 	  loc_result
17132 	    = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
17133 	  break;
17134 	}
17135       /* FALLTHRU */
17136     do_default:
17137     default:
17138       if ((is_a <scalar_int_mode> (mode, &int_mode)
17139 	   && GET_MODE (rtl) == int_mode
17140 	   && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
17141 	   && dwarf_version >= 4)
17142 	  || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
17143 	{
17144 	  /* Value expression.  */
17145 	  loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
17146 	  if (loc_result)
17147 	    add_loc_descr (&loc_result,
17148 			   new_loc_descr (DW_OP_stack_value, 0, 0));
17149 	}
17150       break;
17151     }
17152 
17153   return loc_result;
17154 }
17155 
17156 /* We need to figure out what section we should use as the base for the
17157    address ranges where a given location is valid.
17158    1. If this particular DECL has a section associated with it, use that.
17159    2. If this function has a section associated with it, use that.
17160    3. Otherwise, use the text section.
17161    XXX: If you split a variable across multiple sections, we won't notice.  */
17162 
17163 static const char *
secname_for_decl(const_tree decl)17164 secname_for_decl (const_tree decl)
17165 {
17166   const char *secname;
17167 
17168   if (VAR_OR_FUNCTION_DECL_P (decl)
17169       && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
17170       && DECL_SECTION_NAME (decl))
17171     secname = DECL_SECTION_NAME (decl);
17172   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
17173     {
17174       if (in_cold_section_p)
17175 	{
17176 	  section *sec = current_function_section ();
17177 	  if (sec->common.flags & SECTION_NAMED)
17178 	    return sec->named.name;
17179 	}
17180       secname = DECL_SECTION_NAME (current_function_decl);
17181     }
17182   else if (cfun && in_cold_section_p)
17183     secname = crtl->subsections.cold_section_label;
17184   else
17185     secname = text_section_label;
17186 
17187   return secname;
17188 }
17189 
17190 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
17191 
17192 static bool
decl_by_reference_p(tree decl)17193 decl_by_reference_p (tree decl)
17194 {
17195   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
17196   	   || VAR_P (decl))
17197 	  && DECL_BY_REFERENCE (decl));
17198 }
17199 
17200 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
17201    for VARLOC.  */
17202 
17203 static dw_loc_descr_ref
dw_loc_list_1(tree loc,rtx varloc,int want_address,enum var_init_status initialized)17204 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
17205 	       enum var_init_status initialized)
17206 {
17207   int have_address = 0;
17208   dw_loc_descr_ref descr;
17209   machine_mode mode;
17210 
17211   if (want_address != 2)
17212     {
17213       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
17214       /* Single part.  */
17215       if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
17216 	{
17217 	  varloc = PAT_VAR_LOCATION_LOC (varloc);
17218 	  if (GET_CODE (varloc) == EXPR_LIST)
17219 	    varloc = XEXP (varloc, 0);
17220 	  mode = GET_MODE (varloc);
17221 	  if (MEM_P (varloc))
17222 	    {
17223 	      rtx addr = XEXP (varloc, 0);
17224 	      descr = mem_loc_descriptor (addr, get_address_mode (varloc),
17225 					  mode, initialized);
17226 	      if (descr)
17227 		have_address = 1;
17228 	      else
17229 		{
17230 		  rtx x = avoid_constant_pool_reference (varloc);
17231 		  if (x != varloc)
17232 		    descr = mem_loc_descriptor (x, mode, VOIDmode,
17233 						initialized);
17234 		}
17235 	    }
17236 	  else
17237 	    descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
17238 	}
17239       else
17240 	return 0;
17241     }
17242   else
17243     {
17244       if (GET_CODE (varloc) == VAR_LOCATION)
17245 	mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
17246       else
17247 	mode = DECL_MODE (loc);
17248       descr = loc_descriptor (varloc, mode, initialized);
17249       have_address = 1;
17250     }
17251 
17252   if (!descr)
17253     return 0;
17254 
17255   if (want_address == 2 && !have_address
17256       && (dwarf_version >= 4 || !dwarf_strict))
17257     {
17258       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
17259 	{
17260 	  expansion_failed (loc, NULL_RTX,
17261 			    "DWARF address size mismatch");
17262 	  return 0;
17263 	}
17264       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
17265       have_address = 1;
17266     }
17267   /* Show if we can't fill the request for an address.  */
17268   if (want_address && !have_address)
17269     {
17270       expansion_failed (loc, NULL_RTX,
17271 			"Want address and only have value");
17272       return 0;
17273     }
17274 
17275   /* If we've got an address and don't want one, dereference.  */
17276   if (!want_address && have_address)
17277     {
17278       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
17279       enum dwarf_location_atom op;
17280 
17281       if (size > DWARF2_ADDR_SIZE || size == -1)
17282 	{
17283 	  expansion_failed (loc, NULL_RTX,
17284 			    "DWARF address size mismatch");
17285 	  return 0;
17286 	}
17287       else if (size == DWARF2_ADDR_SIZE)
17288 	op = DW_OP_deref;
17289       else
17290 	op = DW_OP_deref_size;
17291 
17292       add_loc_descr (&descr, new_loc_descr (op, size, 0));
17293     }
17294 
17295   return descr;
17296 }
17297 
17298 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
17299    if it is not possible.  */
17300 
17301 static dw_loc_descr_ref
new_loc_descr_op_bit_piece(HOST_WIDE_INT bitsize,HOST_WIDE_INT offset)17302 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
17303 {
17304   if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
17305     return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
17306   else if (dwarf_version >= 3 || !dwarf_strict)
17307     return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
17308   else
17309     return NULL;
17310 }
17311 
17312 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
17313    for VAR_LOC_NOTE for variable DECL that has been optimized by SRA.  */
17314 
17315 static dw_loc_descr_ref
dw_sra_loc_expr(tree decl,rtx loc)17316 dw_sra_loc_expr (tree decl, rtx loc)
17317 {
17318   rtx p;
17319   unsigned HOST_WIDE_INT padsize = 0;
17320   dw_loc_descr_ref descr, *descr_tail;
17321   unsigned HOST_WIDE_INT decl_size;
17322   rtx varloc;
17323   enum var_init_status initialized;
17324 
17325   if (DECL_SIZE (decl) == NULL
17326       || !tree_fits_uhwi_p (DECL_SIZE (decl)))
17327     return NULL;
17328 
17329   decl_size = tree_to_uhwi (DECL_SIZE (decl));
17330   descr = NULL;
17331   descr_tail = &descr;
17332 
17333   for (p = loc; p; p = XEXP (p, 1))
17334     {
17335       unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
17336       rtx loc_note = *decl_piece_varloc_ptr (p);
17337       dw_loc_descr_ref cur_descr;
17338       dw_loc_descr_ref *tail, last = NULL;
17339       unsigned HOST_WIDE_INT opsize = 0;
17340 
17341       if (loc_note == NULL_RTX
17342 	  || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
17343 	{
17344 	  padsize += bitsize;
17345 	  continue;
17346 	}
17347       initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
17348       varloc = NOTE_VAR_LOCATION (loc_note);
17349       cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
17350       if (cur_descr == NULL)
17351 	{
17352 	  padsize += bitsize;
17353 	  continue;
17354 	}
17355 
17356       /* Check that cur_descr either doesn't use
17357 	 DW_OP_*piece operations, or their sum is equal
17358 	 to bitsize.  Otherwise we can't embed it.  */
17359       for (tail = &cur_descr; *tail != NULL;
17360 	   tail = &(*tail)->dw_loc_next)
17361 	if ((*tail)->dw_loc_opc == DW_OP_piece)
17362 	  {
17363 	    opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
17364 		      * BITS_PER_UNIT;
17365 	    last = *tail;
17366 	  }
17367 	else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
17368 	  {
17369 	    opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
17370 	    last = *tail;
17371 	  }
17372 
17373       if (last != NULL && opsize != bitsize)
17374 	{
17375 	  padsize += bitsize;
17376 	  /* Discard the current piece of the descriptor and release any
17377 	     addr_table entries it uses.  */
17378 	  remove_loc_list_addr_table_entries (cur_descr);
17379 	  continue;
17380 	}
17381 
17382       /* If there is a hole, add DW_OP_*piece after empty DWARF
17383 	 expression, which means that those bits are optimized out.  */
17384       if (padsize)
17385 	{
17386 	  if (padsize > decl_size)
17387 	    {
17388 	      remove_loc_list_addr_table_entries (cur_descr);
17389 	      goto discard_descr;
17390 	    }
17391 	  decl_size -= padsize;
17392 	  *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
17393 	  if (*descr_tail == NULL)
17394 	    {
17395 	      remove_loc_list_addr_table_entries (cur_descr);
17396 	      goto discard_descr;
17397 	    }
17398 	  descr_tail = &(*descr_tail)->dw_loc_next;
17399 	  padsize = 0;
17400 	}
17401       *descr_tail = cur_descr;
17402       descr_tail = tail;
17403       if (bitsize > decl_size)
17404 	goto discard_descr;
17405       decl_size -= bitsize;
17406       if (last == NULL)
17407 	{
17408 	  HOST_WIDE_INT offset = 0;
17409 	  if (GET_CODE (varloc) == VAR_LOCATION
17410 	      && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
17411 	    {
17412 	      varloc = PAT_VAR_LOCATION_LOC (varloc);
17413 	      if (GET_CODE (varloc) == EXPR_LIST)
17414 		varloc = XEXP (varloc, 0);
17415 	    }
17416 	  do
17417 	    {
17418 	      if (GET_CODE (varloc) == CONST
17419 		  || GET_CODE (varloc) == SIGN_EXTEND
17420 		  || GET_CODE (varloc) == ZERO_EXTEND)
17421 		varloc = XEXP (varloc, 0);
17422 	      else if (GET_CODE (varloc) == SUBREG)
17423 		varloc = SUBREG_REG (varloc);
17424 	      else
17425 		break;
17426 	    }
17427 	  while (1);
17428 	  /* DW_OP_bit_size offset should be zero for register
17429 	     or implicit location descriptions and empty location
17430 	     descriptions, but for memory addresses needs big endian
17431 	     adjustment.  */
17432 	  if (MEM_P (varloc))
17433 	    {
17434 	      unsigned HOST_WIDE_INT memsize;
17435 	      if (!poly_uint64 (MEM_SIZE (varloc)).is_constant (&memsize))
17436 		goto discard_descr;
17437 	      memsize *= BITS_PER_UNIT;
17438 	      if (memsize != bitsize)
17439 		{
17440 		  if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
17441 		      && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
17442 		    goto discard_descr;
17443 		  if (memsize < bitsize)
17444 		    goto discard_descr;
17445 		  if (BITS_BIG_ENDIAN)
17446 		    offset = memsize - bitsize;
17447 		}
17448 	    }
17449 
17450 	  *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
17451 	  if (*descr_tail == NULL)
17452 	    goto discard_descr;
17453 	  descr_tail = &(*descr_tail)->dw_loc_next;
17454 	}
17455     }
17456 
17457   /* If there were any non-empty expressions, add padding till the end of
17458      the decl.  */
17459   if (descr != NULL && decl_size != 0)
17460     {
17461       *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
17462       if (*descr_tail == NULL)
17463 	goto discard_descr;
17464     }
17465   return descr;
17466 
17467 discard_descr:
17468   /* Discard the descriptor and release any addr_table entries it uses.  */
17469   remove_loc_list_addr_table_entries (descr);
17470   return NULL;
17471 }
17472 
17473 /* Return the dwarf representation of the location list LOC_LIST of
17474    DECL.  WANT_ADDRESS has the same meaning as in loc_list_from_tree
17475    function.  */
17476 
17477 static dw_loc_list_ref
dw_loc_list(var_loc_list * loc_list,tree decl,int want_address)17478 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
17479 {
17480   const char *endname, *secname;
17481   var_loc_view endview;
17482   rtx varloc;
17483   enum var_init_status initialized;
17484   struct var_loc_node *node;
17485   dw_loc_descr_ref descr;
17486   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
17487   dw_loc_list_ref list = NULL;
17488   dw_loc_list_ref *listp = &list;
17489 
17490   /* Now that we know what section we are using for a base,
17491      actually construct the list of locations.
17492      The first location information is what is passed to the
17493      function that creates the location list, and the remaining
17494      locations just get added on to that list.
17495      Note that we only know the start address for a location
17496      (IE location changes), so to build the range, we use
17497      the range [current location start, next location start].
17498      This means we have to special case the last node, and generate
17499      a range of [last location start, end of function label].  */
17500 
17501   if (cfun && crtl->has_bb_partition)
17502     {
17503       bool save_in_cold_section_p = in_cold_section_p;
17504       in_cold_section_p = first_function_block_is_cold;
17505       if (loc_list->last_before_switch == NULL)
17506 	in_cold_section_p = !in_cold_section_p;
17507       secname = secname_for_decl (decl);
17508       in_cold_section_p = save_in_cold_section_p;
17509     }
17510   else
17511     secname = secname_for_decl (decl);
17512 
17513   for (node = loc_list->first; node; node = node->next)
17514     {
17515       bool range_across_switch = false;
17516       if (GET_CODE (node->loc) == EXPR_LIST
17517 	  || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
17518 	{
17519 	  if (GET_CODE (node->loc) == EXPR_LIST)
17520 	    {
17521 	      descr = NULL;
17522 	      /* This requires DW_OP_{,bit_}piece, which is not usable
17523 		 inside DWARF expressions.  */
17524 	      if (want_address == 2)
17525 		descr = dw_sra_loc_expr (decl, node->loc);
17526 	    }
17527 	  else
17528 	    {
17529 	      initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
17530 	      varloc = NOTE_VAR_LOCATION (node->loc);
17531 	      descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
17532 	    }
17533 	  if (descr)
17534 	    {
17535 	      /* If section switch happens in between node->label
17536 		 and node->next->label (or end of function) and
17537 		 we can't emit it as a single entry list,
17538 		 emit two ranges, first one ending at the end
17539 		 of first partition and second one starting at the
17540 		 beginning of second partition.  */
17541 	      if (node == loc_list->last_before_switch
17542 		  && (node != loc_list->first || loc_list->first->next
17543 		      /* If we are to emit a view number, we will emit
17544 			 a loclist rather than a single location
17545 			 expression for the entire function (see
17546 			 loc_list_has_views), so we have to split the
17547 			 range that straddles across partitions.  */
17548 		      || !ZERO_VIEW_P (node->view))
17549 		  && current_function_decl)
17550 		{
17551 		  endname = cfun->fde->dw_fde_end;
17552 		  endview = 0;
17553 		  range_across_switch = true;
17554 		}
17555 	      /* The variable has a location between NODE->LABEL and
17556 		 NODE->NEXT->LABEL.  */
17557 	      else if (node->next)
17558 		endname = node->next->label, endview = node->next->view;
17559 	      /* If the variable has a location at the last label
17560 		 it keeps its location until the end of function.  */
17561 	      else if (!current_function_decl)
17562 		endname = text_end_label, endview = 0;
17563 	      else
17564 		{
17565 		  ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
17566 					       current_function_funcdef_no);
17567 		  endname = ggc_strdup (label_id);
17568 		  endview = 0;
17569 		}
17570 
17571 	      *listp = new_loc_list (descr, node->label, node->view,
17572 				     endname, endview, secname);
17573 	      if (TREE_CODE (decl) == PARM_DECL
17574 		  && node == loc_list->first
17575 		  && NOTE_P (node->loc)
17576 		  && strcmp (node->label, endname) == 0)
17577 		(*listp)->force = true;
17578 	      listp = &(*listp)->dw_loc_next;
17579 	    }
17580 	}
17581 
17582       if (cfun
17583 	  && crtl->has_bb_partition
17584 	  && node == loc_list->last_before_switch)
17585 	{
17586 	  bool save_in_cold_section_p = in_cold_section_p;
17587 	  in_cold_section_p = !first_function_block_is_cold;
17588 	  secname = secname_for_decl (decl);
17589 	  in_cold_section_p = save_in_cold_section_p;
17590 	}
17591 
17592       if (range_across_switch)
17593 	{
17594 	  if (GET_CODE (node->loc) == EXPR_LIST)
17595 	    descr = dw_sra_loc_expr (decl, node->loc);
17596 	  else
17597 	    {
17598 	      initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
17599 	      varloc = NOTE_VAR_LOCATION (node->loc);
17600 	      descr = dw_loc_list_1 (decl, varloc, want_address,
17601 				     initialized);
17602 	    }
17603 	  gcc_assert (descr);
17604 	  /* The variable has a location between NODE->LABEL and
17605 	     NODE->NEXT->LABEL.  */
17606 	  if (node->next)
17607 	    endname = node->next->label, endview = node->next->view;
17608 	  else
17609 	    endname = cfun->fde->dw_fde_second_end, endview = 0;
17610 	  *listp = new_loc_list (descr, cfun->fde->dw_fde_second_begin, 0,
17611 				 endname, endview, secname);
17612 	  listp = &(*listp)->dw_loc_next;
17613 	}
17614     }
17615 
17616   /* Try to avoid the overhead of a location list emitting a location
17617      expression instead, but only if we didn't have more than one
17618      location entry in the first place.  If some entries were not
17619      representable, we don't want to pretend a single entry that was
17620      applies to the entire scope in which the variable is
17621      available.  */
17622   if (list && loc_list->first->next)
17623     gen_llsym (list);
17624   else
17625     maybe_gen_llsym (list);
17626 
17627   return list;
17628 }
17629 
17630 /* Return if the loc_list has only single element and thus can be represented
17631    as location description.   */
17632 
17633 static bool
single_element_loc_list_p(dw_loc_list_ref list)17634 single_element_loc_list_p (dw_loc_list_ref list)
17635 {
17636   gcc_assert (!list->dw_loc_next || list->ll_symbol);
17637   return !list->ll_symbol;
17638 }
17639 
17640 /* Duplicate a single element of location list.  */
17641 
17642 static inline dw_loc_descr_ref
copy_loc_descr(dw_loc_descr_ref ref)17643 copy_loc_descr (dw_loc_descr_ref ref)
17644 {
17645   dw_loc_descr_ref copy = ggc_alloc<dw_loc_descr_node> ();
17646   memcpy (copy, ref, sizeof (dw_loc_descr_node));
17647   return copy;
17648 }
17649 
17650 /* To each location in list LIST append loc descr REF.  */
17651 
17652 static void
add_loc_descr_to_each(dw_loc_list_ref list,dw_loc_descr_ref ref)17653 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
17654 {
17655   dw_loc_descr_ref copy;
17656   add_loc_descr (&list->expr, ref);
17657   list = list->dw_loc_next;
17658   while (list)
17659     {
17660       copy = copy_loc_descr (ref);
17661       add_loc_descr (&list->expr, copy);
17662       while (copy->dw_loc_next)
17663 	copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
17664       list = list->dw_loc_next;
17665     }
17666 }
17667 
17668 /* To each location in list LIST prepend loc descr REF.  */
17669 
17670 static void
prepend_loc_descr_to_each(dw_loc_list_ref list,dw_loc_descr_ref ref)17671 prepend_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
17672 {
17673   dw_loc_descr_ref copy;
17674   dw_loc_descr_ref ref_end = list->expr;
17675   add_loc_descr (&ref, list->expr);
17676   list->expr = ref;
17677   list = list->dw_loc_next;
17678   while (list)
17679     {
17680       dw_loc_descr_ref end = list->expr;
17681       list->expr = copy = copy_loc_descr (ref);
17682       while (copy->dw_loc_next != ref_end)
17683 	copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
17684       copy->dw_loc_next = end;
17685       list = list->dw_loc_next;
17686     }
17687 }
17688 
17689 /* Given two lists RET and LIST
17690    produce location list that is result of adding expression in LIST
17691    to expression in RET on each position in program.
17692    Might be destructive on both RET and LIST.
17693 
17694    TODO: We handle only simple cases of RET or LIST having at most one
17695    element.  General case would involve sorting the lists in program order
17696    and merging them that will need some additional work.
17697    Adding that will improve quality of debug info especially for SRA-ed
17698    structures.  */
17699 
17700 static void
add_loc_list(dw_loc_list_ref * ret,dw_loc_list_ref list)17701 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
17702 {
17703   if (!list)
17704     return;
17705   if (!*ret)
17706     {
17707       *ret = list;
17708       return;
17709     }
17710   if (!list->dw_loc_next)
17711     {
17712       add_loc_descr_to_each (*ret, list->expr);
17713       return;
17714     }
17715   if (!(*ret)->dw_loc_next)
17716     {
17717       prepend_loc_descr_to_each (list, (*ret)->expr);
17718       *ret = list;
17719       return;
17720     }
17721   expansion_failed (NULL_TREE, NULL_RTX,
17722 		    "Don't know how to merge two non-trivial"
17723 		    " location lists.\n");
17724   *ret = NULL;
17725   return;
17726 }
17727 
17728 /* LOC is constant expression.  Try a luck, look it up in constant
17729    pool and return its loc_descr of its address.  */
17730 
17731 static dw_loc_descr_ref
cst_pool_loc_descr(tree loc)17732 cst_pool_loc_descr (tree loc)
17733 {
17734   /* Get an RTL for this, if something has been emitted.  */
17735   rtx rtl = lookup_constant_def (loc);
17736 
17737   if (!rtl || !MEM_P (rtl))
17738     {
17739       gcc_assert (!rtl);
17740       return 0;
17741     }
17742   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
17743 
17744   /* TODO: We might get more coverage if we was actually delaying expansion
17745      of all expressions till end of compilation when constant pools are fully
17746      populated.  */
17747   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
17748     {
17749       expansion_failed (loc, NULL_RTX,
17750 			"CST value in contant pool but not marked.");
17751       return 0;
17752     }
17753   return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
17754 			     GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
17755 }
17756 
17757 /* Return dw_loc_list representing address of addr_expr LOC
17758    by looking for inner INDIRECT_REF expression and turning
17759    it into simple arithmetics.
17760 
17761    See loc_list_from_tree for the meaning of CONTEXT.  */
17762 
17763 static dw_loc_list_ref
loc_list_for_address_of_addr_expr_of_indirect_ref(tree loc,bool toplev,loc_descr_context * context)17764 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
17765 						   loc_descr_context *context)
17766 {
17767   tree obj, offset;
17768   poly_int64 bitsize, bitpos, bytepos;
17769   machine_mode mode;
17770   int unsignedp, reversep, volatilep = 0;
17771   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
17772 
17773   obj = get_inner_reference (TREE_OPERAND (loc, 0),
17774 			     &bitsize, &bitpos, &offset, &mode,
17775 			     &unsignedp, &reversep, &volatilep);
17776   STRIP_NOPS (obj);
17777   if (!multiple_p (bitpos, BITS_PER_UNIT, &bytepos))
17778     {
17779       expansion_failed (loc, NULL_RTX, "bitfield access");
17780       return 0;
17781     }
17782   if (!INDIRECT_REF_P (obj))
17783     {
17784       expansion_failed (obj,
17785 			NULL_RTX, "no indirect ref in inner refrence");
17786       return 0;
17787     }
17788   if (!offset && known_eq (bitpos, 0))
17789     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
17790 				   context);
17791   else if (toplev
17792 	   && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
17793 	   && (dwarf_version >= 4 || !dwarf_strict))
17794     {
17795       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
17796       if (!list_ret)
17797 	return 0;
17798       if (offset)
17799 	{
17800 	  /* Variable offset.  */
17801 	  list_ret1 = loc_list_from_tree (offset, 0, context);
17802 	  if (list_ret1 == 0)
17803 	    return 0;
17804 	  add_loc_list (&list_ret, list_ret1);
17805 	  if (!list_ret)
17806 	    return 0;
17807 	  add_loc_descr_to_each (list_ret,
17808 				 new_loc_descr (DW_OP_plus, 0, 0));
17809 	}
17810       HOST_WIDE_INT value;
17811       if (bytepos.is_constant (&value) && value > 0)
17812 	add_loc_descr_to_each (list_ret,
17813 			       new_loc_descr (DW_OP_plus_uconst, value, 0));
17814       else if (maybe_ne (bytepos, 0))
17815 	loc_list_plus_const (list_ret, bytepos);
17816       add_loc_descr_to_each (list_ret,
17817 			     new_loc_descr (DW_OP_stack_value, 0, 0));
17818     }
17819   return list_ret;
17820 }
17821 
17822 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
17823    all operations from LOC are nops, move to the last one.  Insert in NOPS all
17824    operations that are skipped.  */
17825 
17826 static void
loc_descr_to_next_no_nop(dw_loc_descr_ref & loc,hash_set<dw_loc_descr_ref> & nops)17827 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
17828 			  hash_set<dw_loc_descr_ref> &nops)
17829 {
17830   while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
17831     {
17832       nops.add (loc);
17833       loc = loc->dw_loc_next;
17834     }
17835 }
17836 
17837 /* Helper for loc_descr_without_nops: free the location description operation
17838    P.  */
17839 
17840 bool
free_loc_descr(const dw_loc_descr_ref & loc,void * data ATTRIBUTE_UNUSED)17841 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
17842 {
17843   ggc_free (loc);
17844   return true;
17845 }
17846 
17847 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
17848    finishes LOC.  */
17849 
17850 static void
loc_descr_without_nops(dw_loc_descr_ref & loc)17851 loc_descr_without_nops (dw_loc_descr_ref &loc)
17852 {
17853   if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
17854     return;
17855 
17856   /* Set of all DW_OP_nop operations we remove.  */
17857   hash_set<dw_loc_descr_ref> nops;
17858 
17859   /* First, strip all prefix NOP operations in order to keep the head of the
17860      operations list.  */
17861   loc_descr_to_next_no_nop (loc, nops);
17862 
17863   for (dw_loc_descr_ref cur = loc; cur != NULL;)
17864     {
17865       /* For control flow operations: strip "prefix" nops in destination
17866 	 labels.  */
17867       if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
17868 	loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
17869       if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
17870 	loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
17871 
17872       /* Do the same for the operations that follow, then move to the next
17873 	 iteration.  */
17874       if (cur->dw_loc_next != NULL)
17875 	loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
17876       cur = cur->dw_loc_next;
17877     }
17878 
17879   nops.traverse<void *, free_loc_descr> (NULL);
17880 }
17881 
17882 
17883 struct dwarf_procedure_info;
17884 
17885 /* Helper structure for location descriptions generation.  */
17886 struct loc_descr_context
17887 {
17888   /* The type that is implicitly referenced by DW_OP_push_object_address, or
17889      NULL_TREE if DW_OP_push_object_address in invalid for this location
17890      description.  This is used when processing PLACEHOLDER_EXPR nodes.  */
17891   tree context_type;
17892   /* The ..._DECL node that should be translated as a
17893      DW_OP_push_object_address operation.  */
17894   tree base_decl;
17895   /* Information about the DWARF procedure we are currently generating. NULL if
17896      we are not generating a DWARF procedure.  */
17897   struct dwarf_procedure_info *dpi;
17898   /* True if integral PLACEHOLDER_EXPR stands for the first argument passed
17899      by consumer.  Used for DW_TAG_generic_subrange attributes.  */
17900   bool placeholder_arg;
17901   /* True if PLACEHOLDER_EXPR has been seen.  */
17902   bool placeholder_seen;
17903 };
17904 
17905 /* DWARF procedures generation
17906 
17907    DWARF expressions (aka. location descriptions) are used to encode variable
17908    things such as sizes or offsets.  Such computations can have redundant parts
17909    that can be factorized in order to reduce the size of the output debug
17910    information.  This is the whole point of DWARF procedures.
17911 
17912    Thanks to stor-layout.c, size and offset expressions in GENERIC trees are
17913    already factorized into functions ("size functions") in order to handle very
17914    big and complex types.  Such functions are quite simple: they have integral
17915    arguments, they return an integral result and their body contains only a
17916    return statement with arithmetic expressions.  This is the only kind of
17917    function we are interested in translating into DWARF procedures, here.
17918 
17919    DWARF expressions and DWARF procedure are executed using a stack, so we have
17920    to define some calling convention for them to interact.  Let's say that:
17921 
17922    - Before calling a DWARF procedure, DWARF expressions must push on the stack
17923      all arguments in reverse order (right-to-left) so that when the DWARF
17924      procedure execution starts, the first argument is the top of the stack.
17925 
17926    - Then, when returning, the DWARF procedure must have consumed all arguments
17927      on the stack, must have pushed the result and touched nothing else.
17928 
17929    - Each integral argument and the result are integral types can be hold in a
17930      single stack slot.
17931 
17932    - We call "frame offset" the number of stack slots that are "under DWARF
17933      procedure control": it includes the arguments slots, the temporaries and
17934      the result slot. Thus, it is equal to the number of arguments when the
17935      procedure execution starts and must be equal to one (the result) when it
17936      returns.  */
17937 
17938 /* Helper structure used when generating operations for a DWARF procedure.  */
17939 struct dwarf_procedure_info
17940 {
17941   /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
17942      currently translated.  */
17943   tree fndecl;
17944   /* The number of arguments FNDECL takes.  */
17945   unsigned args_count;
17946 };
17947 
17948 /* Return a pointer to a newly created DIE node for a DWARF procedure.  Add
17949    LOCATION as its DW_AT_location attribute.  If FNDECL is not NULL_TREE,
17950    equate it to this DIE.  */
17951 
17952 static dw_die_ref
new_dwarf_proc_die(dw_loc_descr_ref location,tree fndecl,dw_die_ref parent_die)17953 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
17954 		    dw_die_ref parent_die)
17955 {
17956   dw_die_ref dwarf_proc_die;
17957 
17958   if ((dwarf_version < 3 && dwarf_strict)
17959       || location == NULL)
17960     return NULL;
17961 
17962   dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
17963   if (fndecl)
17964     equate_decl_number_to_die (fndecl, dwarf_proc_die);
17965   add_AT_loc (dwarf_proc_die, DW_AT_location, location);
17966   return dwarf_proc_die;
17967 }
17968 
17969 /* Return whether TYPE is a supported type as a DWARF procedure argument
17970    type or return type (we handle only scalar types and pointer types that
17971    aren't wider than the DWARF expression evaluation stack.  */
17972 
17973 static bool
is_handled_procedure_type(tree type)17974 is_handled_procedure_type (tree type)
17975 {
17976   return ((INTEGRAL_TYPE_P (type)
17977 	   || TREE_CODE (type) == OFFSET_TYPE
17978 	   || TREE_CODE (type) == POINTER_TYPE)
17979 	  && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
17980 }
17981 
17982 /* Helper for resolve_args_picking: do the same but stop when coming across
17983    visited nodes.  For each node we visit, register in FRAME_OFFSETS the frame
17984    offset *before* evaluating the corresponding operation.  */
17985 
17986 static bool
resolve_args_picking_1(dw_loc_descr_ref loc,unsigned initial_frame_offset,struct dwarf_procedure_info * dpi,hash_map<dw_loc_descr_ref,unsigned> & frame_offsets)17987 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
17988 			struct dwarf_procedure_info *dpi,
17989 			hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
17990 {
17991   /* The "frame_offset" identifier is already used to name a macro... */
17992   unsigned frame_offset_ = initial_frame_offset;
17993   dw_loc_descr_ref l;
17994 
17995   for (l = loc; l != NULL;)
17996     {
17997       bool existed;
17998       unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
17999 
18000       /* If we already met this node, there is nothing to compute anymore.  */
18001       if (existed)
18002 	{
18003 	  /* Make sure that the stack size is consistent wherever the execution
18004 	     flow comes from.  */
18005 	  gcc_assert ((unsigned) l_frame_offset == frame_offset_);
18006 	  break;
18007 	}
18008       l_frame_offset = frame_offset_;
18009 
18010       /* If needed, relocate the picking offset with respect to the frame
18011 	 offset. */
18012       if (l->frame_offset_rel)
18013 	{
18014 	  unsigned HOST_WIDE_INT off;
18015 	  switch (l->dw_loc_opc)
18016 	    {
18017 	    case DW_OP_pick:
18018 	      off = l->dw_loc_oprnd1.v.val_unsigned;
18019 	      break;
18020 	    case DW_OP_dup:
18021 	      off = 0;
18022 	      break;
18023 	    case DW_OP_over:
18024 	      off = 1;
18025 	      break;
18026 	    default:
18027 	      gcc_unreachable ();
18028 	    }
18029 	  /* frame_offset_ is the size of the current stack frame, including
18030 	     incoming arguments. Besides, the arguments are pushed
18031 	     right-to-left.  Thus, in order to access the Nth argument from
18032 	     this operation node, the picking has to skip temporaries *plus*
18033 	     one stack slot per argument (0 for the first one, 1 for the second
18034 	     one, etc.).
18035 
18036 	     The targetted argument number (N) is already set as the operand,
18037 	     and the number of temporaries can be computed with:
18038 	       frame_offsets_ - dpi->args_count */
18039 	  off += frame_offset_ - dpi->args_count;
18040 
18041 	  /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)...  */
18042 	  if (off > 255)
18043 	    return false;
18044 
18045 	  if (off == 0)
18046 	    {
18047 	      l->dw_loc_opc = DW_OP_dup;
18048 	      l->dw_loc_oprnd1.v.val_unsigned = 0;
18049 	    }
18050 	  else if (off == 1)
18051 	    {
18052 	      l->dw_loc_opc = DW_OP_over;
18053 	      l->dw_loc_oprnd1.v.val_unsigned = 0;
18054 	    }
18055 	  else
18056 	    {
18057 	      l->dw_loc_opc = DW_OP_pick;
18058 	      l->dw_loc_oprnd1.v.val_unsigned = off;
18059 	    }
18060 	}
18061 
18062       /* Update frame_offset according to the effect the current operation has
18063 	 on the stack.  */
18064       switch (l->dw_loc_opc)
18065 	{
18066 	case DW_OP_deref:
18067 	case DW_OP_swap:
18068 	case DW_OP_rot:
18069 	case DW_OP_abs:
18070 	case DW_OP_neg:
18071 	case DW_OP_not:
18072 	case DW_OP_plus_uconst:
18073 	case DW_OP_skip:
18074 	case DW_OP_reg0:
18075 	case DW_OP_reg1:
18076 	case DW_OP_reg2:
18077 	case DW_OP_reg3:
18078 	case DW_OP_reg4:
18079 	case DW_OP_reg5:
18080 	case DW_OP_reg6:
18081 	case DW_OP_reg7:
18082 	case DW_OP_reg8:
18083 	case DW_OP_reg9:
18084 	case DW_OP_reg10:
18085 	case DW_OP_reg11:
18086 	case DW_OP_reg12:
18087 	case DW_OP_reg13:
18088 	case DW_OP_reg14:
18089 	case DW_OP_reg15:
18090 	case DW_OP_reg16:
18091 	case DW_OP_reg17:
18092 	case DW_OP_reg18:
18093 	case DW_OP_reg19:
18094 	case DW_OP_reg20:
18095 	case DW_OP_reg21:
18096 	case DW_OP_reg22:
18097 	case DW_OP_reg23:
18098 	case DW_OP_reg24:
18099 	case DW_OP_reg25:
18100 	case DW_OP_reg26:
18101 	case DW_OP_reg27:
18102 	case DW_OP_reg28:
18103 	case DW_OP_reg29:
18104 	case DW_OP_reg30:
18105 	case DW_OP_reg31:
18106 	case DW_OP_bregx:
18107 	case DW_OP_piece:
18108 	case DW_OP_deref_size:
18109 	case DW_OP_nop:
18110 	case DW_OP_bit_piece:
18111 	case DW_OP_implicit_value:
18112 	case DW_OP_stack_value:
18113 	  break;
18114 
18115 	case DW_OP_addr:
18116 	case DW_OP_const1u:
18117 	case DW_OP_const1s:
18118 	case DW_OP_const2u:
18119 	case DW_OP_const2s:
18120 	case DW_OP_const4u:
18121 	case DW_OP_const4s:
18122 	case DW_OP_const8u:
18123 	case DW_OP_const8s:
18124 	case DW_OP_constu:
18125 	case DW_OP_consts:
18126 	case DW_OP_dup:
18127 	case DW_OP_over:
18128 	case DW_OP_pick:
18129 	case DW_OP_lit0:
18130 	case DW_OP_lit1:
18131 	case DW_OP_lit2:
18132 	case DW_OP_lit3:
18133 	case DW_OP_lit4:
18134 	case DW_OP_lit5:
18135 	case DW_OP_lit6:
18136 	case DW_OP_lit7:
18137 	case DW_OP_lit8:
18138 	case DW_OP_lit9:
18139 	case DW_OP_lit10:
18140 	case DW_OP_lit11:
18141 	case DW_OP_lit12:
18142 	case DW_OP_lit13:
18143 	case DW_OP_lit14:
18144 	case DW_OP_lit15:
18145 	case DW_OP_lit16:
18146 	case DW_OP_lit17:
18147 	case DW_OP_lit18:
18148 	case DW_OP_lit19:
18149 	case DW_OP_lit20:
18150 	case DW_OP_lit21:
18151 	case DW_OP_lit22:
18152 	case DW_OP_lit23:
18153 	case DW_OP_lit24:
18154 	case DW_OP_lit25:
18155 	case DW_OP_lit26:
18156 	case DW_OP_lit27:
18157 	case DW_OP_lit28:
18158 	case DW_OP_lit29:
18159 	case DW_OP_lit30:
18160 	case DW_OP_lit31:
18161 	case DW_OP_breg0:
18162 	case DW_OP_breg1:
18163 	case DW_OP_breg2:
18164 	case DW_OP_breg3:
18165 	case DW_OP_breg4:
18166 	case DW_OP_breg5:
18167 	case DW_OP_breg6:
18168 	case DW_OP_breg7:
18169 	case DW_OP_breg8:
18170 	case DW_OP_breg9:
18171 	case DW_OP_breg10:
18172 	case DW_OP_breg11:
18173 	case DW_OP_breg12:
18174 	case DW_OP_breg13:
18175 	case DW_OP_breg14:
18176 	case DW_OP_breg15:
18177 	case DW_OP_breg16:
18178 	case DW_OP_breg17:
18179 	case DW_OP_breg18:
18180 	case DW_OP_breg19:
18181 	case DW_OP_breg20:
18182 	case DW_OP_breg21:
18183 	case DW_OP_breg22:
18184 	case DW_OP_breg23:
18185 	case DW_OP_breg24:
18186 	case DW_OP_breg25:
18187 	case DW_OP_breg26:
18188 	case DW_OP_breg27:
18189 	case DW_OP_breg28:
18190 	case DW_OP_breg29:
18191 	case DW_OP_breg30:
18192 	case DW_OP_breg31:
18193 	case DW_OP_fbreg:
18194 	case DW_OP_push_object_address:
18195 	case DW_OP_call_frame_cfa:
18196 	case DW_OP_GNU_variable_value:
18197 	case DW_OP_GNU_addr_index:
18198 	case DW_OP_GNU_const_index:
18199 	  ++frame_offset_;
18200 	  break;
18201 
18202 	case DW_OP_drop:
18203 	case DW_OP_xderef:
18204 	case DW_OP_and:
18205 	case DW_OP_div:
18206 	case DW_OP_minus:
18207 	case DW_OP_mod:
18208 	case DW_OP_mul:
18209 	case DW_OP_or:
18210 	case DW_OP_plus:
18211 	case DW_OP_shl:
18212 	case DW_OP_shr:
18213 	case DW_OP_shra:
18214 	case DW_OP_xor:
18215 	case DW_OP_bra:
18216 	case DW_OP_eq:
18217 	case DW_OP_ge:
18218 	case DW_OP_gt:
18219 	case DW_OP_le:
18220 	case DW_OP_lt:
18221 	case DW_OP_ne:
18222 	case DW_OP_regx:
18223 	case DW_OP_xderef_size:
18224 	  --frame_offset_;
18225 	  break;
18226 
18227 	case DW_OP_call2:
18228 	case DW_OP_call4:
18229 	case DW_OP_call_ref:
18230 	  {
18231 	    dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
18232 	    int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
18233 
18234 	    if (stack_usage == NULL)
18235 	      return false;
18236 	    frame_offset_ += *stack_usage;
18237 	    break;
18238 	  }
18239 
18240 	case DW_OP_implicit_pointer:
18241 	case DW_OP_entry_value:
18242 	case DW_OP_const_type:
18243 	case DW_OP_regval_type:
18244 	case DW_OP_deref_type:
18245 	case DW_OP_convert:
18246 	case DW_OP_reinterpret:
18247 	case DW_OP_form_tls_address:
18248 	case DW_OP_GNU_push_tls_address:
18249 	case DW_OP_GNU_uninit:
18250 	case DW_OP_GNU_encoded_addr:
18251 	case DW_OP_GNU_implicit_pointer:
18252 	case DW_OP_GNU_entry_value:
18253 	case DW_OP_GNU_const_type:
18254 	case DW_OP_GNU_regval_type:
18255 	case DW_OP_GNU_deref_type:
18256 	case DW_OP_GNU_convert:
18257 	case DW_OP_GNU_reinterpret:
18258 	case DW_OP_GNU_parameter_ref:
18259 	  /* loc_list_from_tree will probably not output these operations for
18260 	     size functions, so assume they will not appear here.  */
18261 	  /* Fall through...  */
18262 
18263 	default:
18264 	  gcc_unreachable ();
18265 	}
18266 
18267       /* Now, follow the control flow (except subroutine calls).  */
18268       switch (l->dw_loc_opc)
18269 	{
18270 	case DW_OP_bra:
18271 	  if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
18272 				       frame_offsets))
18273 	    return false;
18274 	  /* Fall through. */
18275 
18276 	case DW_OP_skip:
18277 	  l = l->dw_loc_oprnd1.v.val_loc;
18278 	  break;
18279 
18280 	case DW_OP_stack_value:
18281 	  return true;
18282 
18283 	default:
18284 	  l = l->dw_loc_next;
18285 	  break;
18286 	}
18287     }
18288 
18289   return true;
18290 }
18291 
18292 /* Make a DFS over operations reachable through LOC (i.e. follow branch
18293    operations) in order to resolve the operand of DW_OP_pick operations that
18294    target DWARF procedure arguments (DPI).  INITIAL_FRAME_OFFSET is the frame
18295    offset *before* LOC is executed.  Return if all relocations were
18296    successful.  */
18297 
18298 static bool
resolve_args_picking(dw_loc_descr_ref loc,unsigned initial_frame_offset,struct dwarf_procedure_info * dpi)18299 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
18300 		      struct dwarf_procedure_info *dpi)
18301 {
18302   /* Associate to all visited operations the frame offset *before* evaluating
18303      this operation.  */
18304   hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
18305 
18306   return resolve_args_picking_1 (loc, initial_frame_offset, dpi,
18307 				 frame_offsets);
18308 }
18309 
18310 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
18311    Return NULL if it is not possible.  */
18312 
18313 static dw_die_ref
function_to_dwarf_procedure(tree fndecl)18314 function_to_dwarf_procedure (tree fndecl)
18315 {
18316   struct loc_descr_context ctx;
18317   struct dwarf_procedure_info dpi;
18318   dw_die_ref dwarf_proc_die;
18319   tree tree_body = DECL_SAVED_TREE (fndecl);
18320   dw_loc_descr_ref loc_body, epilogue;
18321 
18322   tree cursor;
18323   unsigned i;
18324 
18325   /* Do not generate multiple DWARF procedures for the same function
18326      declaration.  */
18327   dwarf_proc_die = lookup_decl_die (fndecl);
18328   if (dwarf_proc_die != NULL)
18329     return dwarf_proc_die;
18330 
18331   /* DWARF procedures are available starting with the DWARFv3 standard.  */
18332   if (dwarf_version < 3 && dwarf_strict)
18333     return NULL;
18334 
18335   /* We handle only functions for which we still have a body, that return a
18336      supported type and that takes arguments with supported types.  Note that
18337      there is no point translating functions that return nothing.  */
18338   if (tree_body == NULL_TREE
18339       || DECL_RESULT (fndecl) == NULL_TREE
18340       || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
18341     return NULL;
18342 
18343   for (cursor = DECL_ARGUMENTS (fndecl);
18344        cursor != NULL_TREE;
18345        cursor = TREE_CHAIN (cursor))
18346     if (!is_handled_procedure_type (TREE_TYPE (cursor)))
18347       return NULL;
18348 
18349   /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)).  */
18350   if (TREE_CODE (tree_body) != RETURN_EXPR)
18351     return NULL;
18352   tree_body = TREE_OPERAND (tree_body, 0);
18353   if (TREE_CODE (tree_body) != MODIFY_EXPR
18354       || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
18355     return NULL;
18356   tree_body = TREE_OPERAND (tree_body, 1);
18357 
18358   /* Try to translate the body expression itself.  Note that this will probably
18359      cause an infinite recursion if its call graph has a cycle.  This is very
18360      unlikely for size functions, however, so don't bother with such things at
18361      the moment.  */
18362   ctx.context_type = NULL_TREE;
18363   ctx.base_decl = NULL_TREE;
18364   ctx.dpi = &dpi;
18365   ctx.placeholder_arg = false;
18366   ctx.placeholder_seen = false;
18367   dpi.fndecl = fndecl;
18368   dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
18369   loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
18370   if (!loc_body)
18371     return NULL;
18372 
18373   /* After evaluating all operands in "loc_body", we should still have on the
18374      stack all arguments plus the desired function result (top of the stack).
18375      Generate code in order to keep only the result in our stack frame.  */
18376   epilogue = NULL;
18377   for (i = 0; i < dpi.args_count; ++i)
18378     {
18379       dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
18380       op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
18381       op_couple->dw_loc_next->dw_loc_next = epilogue;
18382       epilogue = op_couple;
18383     }
18384   add_loc_descr (&loc_body, epilogue);
18385   if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
18386     return NULL;
18387 
18388   /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
18389      because they are considered useful.  Now there is an epilogue, they are
18390      not anymore, so give it another try.   */
18391   loc_descr_without_nops (loc_body);
18392 
18393   /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
18394      a DW_TAG_dwarf_procedure, so we may have a conflict, here.  It's unlikely,
18395      though, given that size functions do not come from source, so they should
18396      not have a dedicated DW_TAG_subprogram DIE.  */
18397   dwarf_proc_die
18398     = new_dwarf_proc_die (loc_body, fndecl,
18399 			  get_context_die (DECL_CONTEXT (fndecl)));
18400 
18401   /* The called DWARF procedure consumes one stack slot per argument and
18402      returns one stack slot.  */
18403   dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
18404 
18405   return dwarf_proc_die;
18406 }
18407 
18408 
18409 /* Generate Dwarf location list representing LOC.
18410    If WANT_ADDRESS is false, expression computing LOC will be computed
18411    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
18412    if WANT_ADDRESS is 2, expression computing address useable in location
18413      will be returned (i.e. DW_OP_reg can be used
18414      to refer to register values).
18415 
18416    CONTEXT provides information to customize the location descriptions
18417    generation.  Its context_type field specifies what type is implicitly
18418    referenced by DW_OP_push_object_address.  If it is NULL_TREE, this operation
18419    will not be generated.
18420 
18421    Its DPI field determines whether we are generating a DWARF expression for a
18422    DWARF procedure, so PARM_DECL references are processed specifically.
18423 
18424    If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
18425    and dpi fields were null.  */
18426 
18427 static dw_loc_list_ref
loc_list_from_tree_1(tree loc,int want_address,struct loc_descr_context * context)18428 loc_list_from_tree_1 (tree loc, int want_address,
18429 		      struct loc_descr_context *context)
18430 {
18431   dw_loc_descr_ref ret = NULL, ret1 = NULL;
18432   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
18433   int have_address = 0;
18434   enum dwarf_location_atom op;
18435 
18436   /* ??? Most of the time we do not take proper care for sign/zero
18437      extending the values properly.  Hopefully this won't be a real
18438      problem...  */
18439 
18440   if (context != NULL
18441       && context->base_decl == loc
18442       && want_address == 0)
18443     {
18444       if (dwarf_version >= 3 || !dwarf_strict)
18445 	return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
18446 			     NULL, 0, NULL, 0, NULL);
18447       else
18448 	return NULL;
18449     }
18450 
18451   switch (TREE_CODE (loc))
18452     {
18453     case ERROR_MARK:
18454       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
18455       return 0;
18456 
18457     case PLACEHOLDER_EXPR:
18458       /* This case involves extracting fields from an object to determine the
18459 	 position of other fields. It is supposed to appear only as the first
18460 	 operand of COMPONENT_REF nodes and to reference precisely the type
18461 	 that the context allows or its enclosing type.  */
18462       if (context != NULL
18463 	  && (TREE_TYPE (loc) == context->context_type
18464 	      || TREE_TYPE (loc) == TYPE_CONTEXT (context->context_type))
18465 	  && want_address >= 1)
18466 	{
18467 	  if (dwarf_version >= 3 || !dwarf_strict)
18468 	    {
18469 	      ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
18470 	      have_address = 1;
18471 	      break;
18472 	    }
18473 	  else
18474 	    return NULL;
18475 	}
18476       /* For DW_TAG_generic_subrange attributes, PLACEHOLDER_EXPR stands for
18477 	 the single argument passed by consumer.  */
18478       else if (context != NULL
18479 	       && context->placeholder_arg
18480 	       && INTEGRAL_TYPE_P (TREE_TYPE (loc))
18481 	       && want_address == 0)
18482 	{
18483 	  ret = new_loc_descr (DW_OP_pick, 0, 0);
18484 	  ret->frame_offset_rel = 1;
18485 	  context->placeholder_seen = true;
18486 	  break;
18487 	}
18488       else
18489 	expansion_failed (loc, NULL_RTX,
18490 			  "PLACEHOLDER_EXPR for an unexpected type");
18491       break;
18492 
18493     case CALL_EXPR:
18494 	{
18495 	  const int nargs = call_expr_nargs (loc);
18496 	  tree callee = get_callee_fndecl (loc);
18497 	  int i;
18498 	  dw_die_ref dwarf_proc;
18499 
18500 	  if (callee == NULL_TREE)
18501 	    goto call_expansion_failed;
18502 
18503 	  /* We handle only functions that return an integer.  */
18504 	  if (!is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee))))
18505 	    goto call_expansion_failed;
18506 
18507 	  dwarf_proc = function_to_dwarf_procedure (callee);
18508 	  if (dwarf_proc == NULL)
18509 	    goto call_expansion_failed;
18510 
18511 	  /* Evaluate arguments right-to-left so that the first argument will
18512 	     be the top-most one on the stack.  */
18513 	  for (i = nargs - 1; i >= 0; --i)
18514 	    {
18515 	      dw_loc_descr_ref loc_descr
18516 	        = loc_descriptor_from_tree (CALL_EXPR_ARG (loc, i), 0,
18517 					    context);
18518 
18519 	      if (loc_descr == NULL)
18520 		goto call_expansion_failed;
18521 
18522 	      add_loc_descr (&ret, loc_descr);
18523 	    }
18524 
18525 	  ret1 = new_loc_descr (DW_OP_call4, 0, 0);
18526 	  ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18527 	  ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
18528 	  ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
18529 	  add_loc_descr (&ret, ret1);
18530 	  break;
18531 
18532 	call_expansion_failed:
18533 	  expansion_failed (loc, NULL_RTX, "CALL_EXPR");
18534 	  /* There are no opcodes for these operations.  */
18535 	  return 0;
18536 	}
18537 
18538     case PREINCREMENT_EXPR:
18539     case PREDECREMENT_EXPR:
18540     case POSTINCREMENT_EXPR:
18541     case POSTDECREMENT_EXPR:
18542       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
18543       /* There are no opcodes for these operations.  */
18544       return 0;
18545 
18546     case ADDR_EXPR:
18547       /* If we already want an address, see if there is INDIRECT_REF inside
18548          e.g. for &this->field.  */
18549       if (want_address)
18550 	{
18551 	  list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
18552 		       (loc, want_address == 2, context);
18553 	  if (list_ret)
18554 	    have_address = 1;
18555 	  else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
18556 	  	   && (ret = cst_pool_loc_descr (loc)))
18557 	    have_address = 1;
18558 	}
18559         /* Otherwise, process the argument and look for the address.  */
18560       if (!list_ret && !ret)
18561         list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
18562       else
18563 	{
18564 	  if (want_address)
18565 	    expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
18566 	  return NULL;
18567 	}
18568       break;
18569 
18570     case VAR_DECL:
18571       if (DECL_THREAD_LOCAL_P (loc))
18572 	{
18573 	  rtx rtl;
18574          enum dwarf_location_atom tls_op;
18575          enum dtprel_bool dtprel = dtprel_false;
18576 
18577 	  if (targetm.have_tls)
18578 	    {
18579 	      /* If this is not defined, we have no way to emit the
18580 		 data.  */
18581 	      if (!targetm.asm_out.output_dwarf_dtprel)
18582 		return 0;
18583 
18584 	       /* The way DW_OP_GNU_push_tls_address is specified, we
18585 	     	  can only look up addresses of objects in the current
18586 	     	  module.  We used DW_OP_addr as first op, but that's
18587 		  wrong, because DW_OP_addr is relocated by the debug
18588 		  info consumer, while DW_OP_GNU_push_tls_address
18589 		  operand shouldn't be.  */
18590 	      if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
18591 		return 0;
18592 	      dtprel = dtprel_true;
18593 	      /* We check for DWARF 5 here because gdb did not implement
18594 		 DW_OP_form_tls_address until after 7.12.  */
18595 	      tls_op = (dwarf_version >= 5 ? DW_OP_form_tls_address
18596 			: DW_OP_GNU_push_tls_address);
18597 	    }
18598 	  else
18599 	    {
18600 	      if (!targetm.emutls.debug_form_tls_address
18601 		  || !(dwarf_version >= 3 || !dwarf_strict))
18602 		return 0;
18603 	      /* We stuffed the control variable into the DECL_VALUE_EXPR
18604 		 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
18605 		 no longer appear in gimple code.  We used the control
18606 		 variable in specific so that we could pick it up here.  */
18607 	      loc = DECL_VALUE_EXPR (loc);
18608               tls_op = DW_OP_form_tls_address;
18609 	    }
18610 
18611 	  rtl = rtl_for_decl_location (loc);
18612 	  if (rtl == NULL_RTX)
18613 	    return 0;
18614 
18615 	  if (!MEM_P (rtl))
18616 	    return 0;
18617 	  rtl = XEXP (rtl, 0);
18618 	  if (! CONSTANT_P (rtl))
18619 	    return 0;
18620 
18621           ret = new_addr_loc_descr (rtl, dtprel);
18622           ret1 = new_loc_descr (tls_op, 0, 0);
18623 	  add_loc_descr (&ret, ret1);
18624 
18625 	  have_address = 1;
18626 	  break;
18627 	}
18628       /* FALLTHRU */
18629 
18630     case PARM_DECL:
18631       if (context != NULL && context->dpi != NULL
18632 	  && DECL_CONTEXT (loc) == context->dpi->fndecl)
18633 	{
18634 	  /* We are generating code for a DWARF procedure and we want to access
18635 	     one of its arguments: find the appropriate argument offset and let
18636 	     the resolve_args_picking pass compute the offset that complies
18637 	     with the stack frame size.  */
18638 	  unsigned i = 0;
18639 	  tree cursor;
18640 
18641 	  for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
18642 	       cursor != NULL_TREE && cursor != loc;
18643 	       cursor = TREE_CHAIN (cursor), ++i)
18644 	    ;
18645 	  /* If we are translating a DWARF procedure, all referenced parameters
18646 	     must belong to the current function.  */
18647 	  gcc_assert (cursor != NULL_TREE);
18648 
18649 	  ret = new_loc_descr (DW_OP_pick, i, 0);
18650 	  ret->frame_offset_rel = 1;
18651 	  break;
18652 	}
18653       /* FALLTHRU */
18654 
18655     case RESULT_DECL:
18656       if (DECL_HAS_VALUE_EXPR_P (loc))
18657 	return loc_list_from_tree_1 (DECL_VALUE_EXPR (loc),
18658 				     want_address, context);
18659       /* FALLTHRU */
18660 
18661     case FUNCTION_DECL:
18662       {
18663 	rtx rtl;
18664 	var_loc_list *loc_list = lookup_decl_loc (loc);
18665 
18666 	if (loc_list && loc_list->first)
18667 	  {
18668 	    list_ret = dw_loc_list (loc_list, loc, want_address);
18669 	    have_address = want_address != 0;
18670 	    break;
18671 	  }
18672 	rtl = rtl_for_decl_location (loc);
18673 	if (rtl == NULL_RTX)
18674 	  {
18675 	    if (TREE_CODE (loc) != FUNCTION_DECL
18676 		&& early_dwarf
18677 		&& current_function_decl
18678 		&& want_address != 1
18679 		&& ! DECL_IGNORED_P (loc)
18680 		&& (INTEGRAL_TYPE_P (TREE_TYPE (loc))
18681 		    || POINTER_TYPE_P (TREE_TYPE (loc)))
18682 		&& DECL_CONTEXT (loc) == current_function_decl
18683 		&& (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (loc)))
18684 		    <= DWARF2_ADDR_SIZE))
18685 	      {
18686 		dw_die_ref ref = lookup_decl_die (loc);
18687 		ret = new_loc_descr (DW_OP_GNU_variable_value, 0, 0);
18688 		if (ref)
18689 		  {
18690 		    ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18691 		    ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
18692 		    ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
18693 		  }
18694 		else
18695 		  {
18696 		    ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
18697 		    ret->dw_loc_oprnd1.v.val_decl_ref = loc;
18698 		  }
18699 		break;
18700 	      }
18701 	    expansion_failed (loc, NULL_RTX, "DECL has no RTL");
18702 	    return 0;
18703 	  }
18704 	else if (CONST_INT_P (rtl))
18705 	  {
18706 	    HOST_WIDE_INT val = INTVAL (rtl);
18707 	    if (TYPE_UNSIGNED (TREE_TYPE (loc)))
18708 	      val &= GET_MODE_MASK (DECL_MODE (loc));
18709 	    ret = int_loc_descriptor (val);
18710 	  }
18711 	else if (GET_CODE (rtl) == CONST_STRING)
18712 	  {
18713 	    expansion_failed (loc, NULL_RTX, "CONST_STRING");
18714 	    return 0;
18715 	  }
18716 	else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
18717           ret = new_addr_loc_descr (rtl, dtprel_false);
18718 	else
18719 	  {
18720 	    machine_mode mode, mem_mode;
18721 
18722 	    /* Certain constructs can only be represented at top-level.  */
18723 	    if (want_address == 2)
18724 	      {
18725 		ret = loc_descriptor (rtl, VOIDmode,
18726 				      VAR_INIT_STATUS_INITIALIZED);
18727 		have_address = 1;
18728 	      }
18729 	    else
18730 	      {
18731 		mode = GET_MODE (rtl);
18732 		mem_mode = VOIDmode;
18733 		if (MEM_P (rtl))
18734 		  {
18735 		    mem_mode = mode;
18736 		    mode = get_address_mode (rtl);
18737 		    rtl = XEXP (rtl, 0);
18738 		    have_address = 1;
18739 		  }
18740 		ret = mem_loc_descriptor (rtl, mode, mem_mode,
18741 					  VAR_INIT_STATUS_INITIALIZED);
18742 	      }
18743 	    if (!ret)
18744 	      expansion_failed (loc, rtl,
18745 				"failed to produce loc descriptor for rtl");
18746 	  }
18747       }
18748       break;
18749 
18750     case MEM_REF:
18751       if (!integer_zerop (TREE_OPERAND (loc, 1)))
18752 	{
18753 	  have_address = 1;
18754 	  goto do_plus;
18755 	}
18756       /* Fallthru.  */
18757     case INDIRECT_REF:
18758       list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18759       have_address = 1;
18760       break;
18761 
18762     case TARGET_MEM_REF:
18763     case SSA_NAME:
18764     case DEBUG_EXPR_DECL:
18765       return NULL;
18766 
18767     case COMPOUND_EXPR:
18768       return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
18769 				   context);
18770 
18771     CASE_CONVERT:
18772     case VIEW_CONVERT_EXPR:
18773     case SAVE_EXPR:
18774     case MODIFY_EXPR:
18775     case NON_LVALUE_EXPR:
18776       return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
18777 				   context);
18778 
18779     case COMPONENT_REF:
18780     case BIT_FIELD_REF:
18781     case ARRAY_REF:
18782     case ARRAY_RANGE_REF:
18783     case REALPART_EXPR:
18784     case IMAGPART_EXPR:
18785       {
18786 	tree obj, offset;
18787 	poly_int64 bitsize, bitpos, bytepos;
18788 	machine_mode mode;
18789 	int unsignedp, reversep, volatilep = 0;
18790 
18791 	obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
18792 				   &unsignedp, &reversep, &volatilep);
18793 
18794 	gcc_assert (obj != loc);
18795 
18796 	list_ret = loc_list_from_tree_1 (obj,
18797 					 want_address == 2
18798 					 && known_eq (bitpos, 0)
18799 					 && !offset ? 2 : 1,
18800 					 context);
18801 	/* TODO: We can extract value of the small expression via shifting even
18802 	   for nonzero bitpos.  */
18803 	if (list_ret == 0)
18804 	  return 0;
18805 	if (!multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
18806 	    || !multiple_p (bitsize, BITS_PER_UNIT))
18807 	  {
18808 	    expansion_failed (loc, NULL_RTX,
18809 			      "bitfield access");
18810 	    return 0;
18811 	  }
18812 
18813 	if (offset != NULL_TREE)
18814 	  {
18815 	    /* Variable offset.  */
18816 	    list_ret1 = loc_list_from_tree_1 (offset, 0, context);
18817 	    if (list_ret1 == 0)
18818 	      return 0;
18819 	    add_loc_list (&list_ret, list_ret1);
18820 	    if (!list_ret)
18821 	      return 0;
18822 	    add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
18823 	  }
18824 
18825 	HOST_WIDE_INT value;
18826 	if (bytepos.is_constant (&value) && value > 0)
18827 	  add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst,
18828 							  value, 0));
18829 	else if (maybe_ne (bytepos, 0))
18830 	  loc_list_plus_const (list_ret, bytepos);
18831 
18832 	have_address = 1;
18833 	break;
18834       }
18835 
18836     case INTEGER_CST:
18837       if ((want_address || !tree_fits_shwi_p (loc))
18838 	  && (ret = cst_pool_loc_descr (loc)))
18839 	have_address = 1;
18840       else if (want_address == 2
18841 	       && tree_fits_shwi_p (loc)
18842 	       && (ret = address_of_int_loc_descriptor
18843 	       		   (int_size_in_bytes (TREE_TYPE (loc)),
18844 	       		    tree_to_shwi (loc))))
18845 	have_address = 1;
18846       else if (tree_fits_shwi_p (loc))
18847 	ret = int_loc_descriptor (tree_to_shwi (loc));
18848       else if (tree_fits_uhwi_p (loc))
18849 	ret = uint_loc_descriptor (tree_to_uhwi (loc));
18850       else
18851 	{
18852 	  expansion_failed (loc, NULL_RTX,
18853 			    "Integer operand is not host integer");
18854 	  return 0;
18855 	}
18856       break;
18857 
18858     case POLY_INT_CST:
18859       {
18860 	if (want_address)
18861 	  {
18862 	    expansion_failed (loc, NULL_RTX,
18863 			      "constant address with a runtime component");
18864 	    return 0;
18865 	  }
18866 	poly_int64 value;
18867 	if (!poly_int_tree_p (loc, &value))
18868 	  {
18869 	    expansion_failed (loc, NULL_RTX, "constant too big");
18870 	    return 0;
18871 	  }
18872 	ret = int_loc_descriptor (value);
18873       }
18874       break;
18875 
18876     case CONSTRUCTOR:
18877     case REAL_CST:
18878     case STRING_CST:
18879     case COMPLEX_CST:
18880       if ((ret = cst_pool_loc_descr (loc)))
18881 	have_address = 1;
18882       else if (TREE_CODE (loc) == CONSTRUCTOR)
18883 	{
18884 	  tree type = TREE_TYPE (loc);
18885 	  unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
18886 	  unsigned HOST_WIDE_INT offset = 0;
18887 	  unsigned HOST_WIDE_INT cnt;
18888 	  constructor_elt *ce;
18889 
18890 	  if (TREE_CODE (type) == RECORD_TYPE)
18891 	    {
18892 	      /* This is very limited, but it's enough to output
18893 		 pointers to member functions, as long as the
18894 		 referenced function is defined in the current
18895 		 translation unit.  */
18896 	      FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
18897 		{
18898 		  tree val = ce->value;
18899 
18900 		  tree field = ce->index;
18901 
18902 		  if (val)
18903 		    STRIP_NOPS (val);
18904 
18905 		  if (!field || DECL_BIT_FIELD (field))
18906 		    {
18907 		      expansion_failed (loc, NULL_RTX,
18908 					"bitfield in record type constructor");
18909 		      size = offset = (unsigned HOST_WIDE_INT)-1;
18910 		      ret = NULL;
18911 		      break;
18912 		    }
18913 
18914 		  HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
18915 		  unsigned HOST_WIDE_INT pos = int_byte_position (field);
18916 		  gcc_assert (pos + fieldsize <= size);
18917 		  if (pos < offset)
18918 		    {
18919 		      expansion_failed (loc, NULL_RTX,
18920 					"out-of-order fields in record constructor");
18921 		      size = offset = (unsigned HOST_WIDE_INT)-1;
18922 		      ret = NULL;
18923 		      break;
18924 		    }
18925 		  if (pos > offset)
18926 		    {
18927 		      ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
18928 		      add_loc_descr (&ret, ret1);
18929 		      offset = pos;
18930 		    }
18931 		  if (val && fieldsize != 0)
18932 		    {
18933 		      ret1 = loc_descriptor_from_tree (val, want_address, context);
18934 		      if (!ret1)
18935 			{
18936 			  expansion_failed (loc, NULL_RTX,
18937 					    "unsupported expression in field");
18938 			  size = offset = (unsigned HOST_WIDE_INT)-1;
18939 			  ret = NULL;
18940 			  break;
18941 			}
18942 		      add_loc_descr (&ret, ret1);
18943 		    }
18944 		  if (fieldsize)
18945 		    {
18946 		      ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
18947 		      add_loc_descr (&ret, ret1);
18948 		      offset = pos + fieldsize;
18949 		    }
18950 		}
18951 
18952 	      if (offset != size)
18953 		{
18954 		  ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
18955 		  add_loc_descr (&ret, ret1);
18956 		  offset = size;
18957 		}
18958 
18959 	      have_address = !!want_address;
18960 	    }
18961 	  else
18962 	    expansion_failed (loc, NULL_RTX,
18963 			      "constructor of non-record type");
18964 	}
18965       else
18966       /* We can construct small constants here using int_loc_descriptor.  */
18967 	expansion_failed (loc, NULL_RTX,
18968 			  "constructor or constant not in constant pool");
18969       break;
18970 
18971     case TRUTH_AND_EXPR:
18972     case TRUTH_ANDIF_EXPR:
18973     case BIT_AND_EXPR:
18974       op = DW_OP_and;
18975       goto do_binop;
18976 
18977     case TRUTH_XOR_EXPR:
18978     case BIT_XOR_EXPR:
18979       op = DW_OP_xor;
18980       goto do_binop;
18981 
18982     case TRUTH_OR_EXPR:
18983     case TRUTH_ORIF_EXPR:
18984     case BIT_IOR_EXPR:
18985       op = DW_OP_or;
18986       goto do_binop;
18987 
18988     case FLOOR_DIV_EXPR:
18989     case CEIL_DIV_EXPR:
18990     case ROUND_DIV_EXPR:
18991     case TRUNC_DIV_EXPR:
18992     case EXACT_DIV_EXPR:
18993       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
18994 	return 0;
18995       op = DW_OP_div;
18996       goto do_binop;
18997 
18998     case MINUS_EXPR:
18999       op = DW_OP_minus;
19000       goto do_binop;
19001 
19002     case FLOOR_MOD_EXPR:
19003     case CEIL_MOD_EXPR:
19004     case ROUND_MOD_EXPR:
19005     case TRUNC_MOD_EXPR:
19006       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
19007 	{
19008 	  op = DW_OP_mod;
19009 	  goto do_binop;
19010 	}
19011       list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19012       list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
19013       if (list_ret == 0 || list_ret1 == 0)
19014 	return 0;
19015 
19016       add_loc_list (&list_ret, list_ret1);
19017       if (list_ret == 0)
19018 	return 0;
19019       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
19020       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
19021       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
19022       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
19023       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
19024       break;
19025 
19026     case MULT_EXPR:
19027       op = DW_OP_mul;
19028       goto do_binop;
19029 
19030     case LSHIFT_EXPR:
19031       op = DW_OP_shl;
19032       goto do_binop;
19033 
19034     case RSHIFT_EXPR:
19035       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
19036       goto do_binop;
19037 
19038     case POINTER_PLUS_EXPR:
19039     case PLUS_EXPR:
19040     do_plus:
19041       if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
19042 	{
19043 	  /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
19044 	     smarter to encode their opposite.  The DW_OP_plus_uconst operation
19045 	     takes 1 + X bytes, X being the size of the ULEB128 addend.  On the
19046 	     other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
19047 	     bytes, Y being the size of the operation that pushes the opposite
19048 	     of the addend.  So let's choose the smallest representation.  */
19049 	  const tree tree_addend = TREE_OPERAND (loc, 1);
19050 	  offset_int wi_addend;
19051 	  HOST_WIDE_INT shwi_addend;
19052 	  dw_loc_descr_ref loc_naddend;
19053 
19054 	  list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19055 	  if (list_ret == 0)
19056 	    return 0;
19057 
19058 	  /* Try to get the literal to push.  It is the opposite of the addend,
19059 	     so as we rely on wrapping during DWARF evaluation, first decode
19060 	     the literal as a "DWARF-sized" signed number.  */
19061 	  wi_addend = wi::to_offset (tree_addend);
19062 	  wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
19063 	  shwi_addend = wi_addend.to_shwi ();
19064 	  loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
19065 			? int_loc_descriptor (-shwi_addend)
19066 			: NULL;
19067 
19068 	  if (loc_naddend != NULL
19069 	      && ((unsigned) size_of_uleb128 (shwi_addend)
19070 	          > size_of_loc_descr (loc_naddend)))
19071 	    {
19072 	      add_loc_descr_to_each (list_ret, loc_naddend);
19073 	      add_loc_descr_to_each (list_ret,
19074 				     new_loc_descr (DW_OP_minus, 0, 0));
19075 	    }
19076 	  else
19077 	    {
19078 	      for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
19079 		{
19080 		  loc_naddend = loc_cur;
19081 		  loc_cur = loc_cur->dw_loc_next;
19082 		  ggc_free (loc_naddend);
19083 		}
19084 	      loc_list_plus_const (list_ret, wi_addend.to_shwi ());
19085 	    }
19086 	  break;
19087 	}
19088 
19089       op = DW_OP_plus;
19090       goto do_binop;
19091 
19092     case LE_EXPR:
19093       op = DW_OP_le;
19094       goto do_comp_binop;
19095 
19096     case GE_EXPR:
19097       op = DW_OP_ge;
19098       goto do_comp_binop;
19099 
19100     case LT_EXPR:
19101       op = DW_OP_lt;
19102       goto do_comp_binop;
19103 
19104     case GT_EXPR:
19105       op = DW_OP_gt;
19106       goto do_comp_binop;
19107 
19108     do_comp_binop:
19109       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
19110 	{
19111 	  list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
19112 	  list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
19113 	  list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
19114 						    TREE_CODE (loc));
19115 	  break;
19116 	}
19117       else
19118 	goto do_binop;
19119 
19120     case EQ_EXPR:
19121       op = DW_OP_eq;
19122       goto do_binop;
19123 
19124     case NE_EXPR:
19125       op = DW_OP_ne;
19126       goto do_binop;
19127 
19128     do_binop:
19129       list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19130       list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
19131       if (list_ret == 0 || list_ret1 == 0)
19132 	return 0;
19133 
19134       add_loc_list (&list_ret, list_ret1);
19135       if (list_ret == 0)
19136 	return 0;
19137       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
19138       break;
19139 
19140     case TRUTH_NOT_EXPR:
19141     case BIT_NOT_EXPR:
19142       op = DW_OP_not;
19143       goto do_unop;
19144 
19145     case ABS_EXPR:
19146       op = DW_OP_abs;
19147       goto do_unop;
19148 
19149     case NEGATE_EXPR:
19150       op = DW_OP_neg;
19151       goto do_unop;
19152 
19153     do_unop:
19154       list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19155       if (list_ret == 0)
19156 	return 0;
19157 
19158       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
19159       break;
19160 
19161     case MIN_EXPR:
19162     case MAX_EXPR:
19163       {
19164 	const enum tree_code code =
19165 	  TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
19166 
19167 	loc = build3 (COND_EXPR, TREE_TYPE (loc),
19168 		      build2 (code, integer_type_node,
19169 			      TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
19170 		      TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
19171       }
19172 
19173       /* fall through */
19174 
19175     case COND_EXPR:
19176       {
19177 	dw_loc_descr_ref lhs
19178 	  = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
19179 	dw_loc_list_ref rhs
19180 	  = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
19181 	dw_loc_descr_ref bra_node, jump_node, tmp;
19182 
19183 	list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19184 	if (list_ret == 0 || lhs == 0 || rhs == 0)
19185 	  return 0;
19186 
19187 	bra_node = new_loc_descr (DW_OP_bra, 0, 0);
19188 	add_loc_descr_to_each (list_ret, bra_node);
19189 
19190 	add_loc_list (&list_ret, rhs);
19191 	jump_node = new_loc_descr (DW_OP_skip, 0, 0);
19192 	add_loc_descr_to_each (list_ret, jump_node);
19193 
19194 	add_loc_descr_to_each (list_ret, lhs);
19195 	bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
19196 	bra_node->dw_loc_oprnd1.v.val_loc = lhs;
19197 
19198 	/* ??? Need a node to point the skip at.  Use a nop.  */
19199 	tmp = new_loc_descr (DW_OP_nop, 0, 0);
19200 	add_loc_descr_to_each (list_ret, tmp);
19201 	jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
19202 	jump_node->dw_loc_oprnd1.v.val_loc = tmp;
19203       }
19204       break;
19205 
19206     case FIX_TRUNC_EXPR:
19207       return 0;
19208 
19209     case COMPOUND_LITERAL_EXPR:
19210       return loc_list_from_tree_1 (COMPOUND_LITERAL_EXPR_DECL (loc),
19211 				   0, context);
19212 
19213     default:
19214       /* Leave front-end specific codes as simply unknown.  This comes
19215 	 up, for instance, with the C STMT_EXPR.  */
19216       if ((unsigned int) TREE_CODE (loc)
19217 	  >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
19218 	{
19219 	  expansion_failed (loc, NULL_RTX,
19220 			    "language specific tree node");
19221 	  return 0;
19222 	}
19223 
19224       /* Otherwise this is a generic code; we should just lists all of
19225 	 these explicitly.  We forgot one.  */
19226       if (flag_checking)
19227 	gcc_unreachable ();
19228 
19229       /* In a release build, we want to degrade gracefully: better to
19230 	 generate incomplete debugging information than to crash.  */
19231       return NULL;
19232     }
19233 
19234   if (!ret && !list_ret)
19235     return 0;
19236 
19237   if (want_address == 2 && !have_address
19238       && (dwarf_version >= 4 || !dwarf_strict))
19239     {
19240       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
19241 	{
19242 	  expansion_failed (loc, NULL_RTX,
19243 			    "DWARF address size mismatch");
19244 	  return 0;
19245 	}
19246       if (ret)
19247 	add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
19248       else
19249 	add_loc_descr_to_each (list_ret,
19250 			       new_loc_descr (DW_OP_stack_value, 0, 0));
19251       have_address = 1;
19252     }
19253   /* Show if we can't fill the request for an address.  */
19254   if (want_address && !have_address)
19255     {
19256       expansion_failed (loc, NULL_RTX,
19257 			"Want address and only have value");
19258       return 0;
19259     }
19260 
19261   gcc_assert (!ret || !list_ret);
19262 
19263   /* If we've got an address and don't want one, dereference.  */
19264   if (!want_address && have_address)
19265     {
19266       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
19267 
19268       if (size > DWARF2_ADDR_SIZE || size == -1)
19269 	{
19270 	  expansion_failed (loc, NULL_RTX,
19271 			    "DWARF address size mismatch");
19272 	  return 0;
19273 	}
19274       else if (size == DWARF2_ADDR_SIZE)
19275 	op = DW_OP_deref;
19276       else
19277 	op = DW_OP_deref_size;
19278 
19279       if (ret)
19280 	add_loc_descr (&ret, new_loc_descr (op, size, 0));
19281       else
19282 	add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
19283     }
19284   if (ret)
19285     list_ret = new_loc_list (ret, NULL, 0, NULL, 0, NULL);
19286 
19287   return list_ret;
19288 }
19289 
19290 /* Likewise, but strip useless DW_OP_nop operations in the resulting
19291    expressions.  */
19292 
19293 static dw_loc_list_ref
loc_list_from_tree(tree loc,int want_address,struct loc_descr_context * context)19294 loc_list_from_tree (tree loc, int want_address,
19295 		    struct loc_descr_context *context)
19296 {
19297   dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
19298 
19299   for (dw_loc_list_ref loc_cur = result;
19300        loc_cur != NULL; loc_cur = loc_cur->dw_loc_next)
19301     loc_descr_without_nops (loc_cur->expr);
19302   return result;
19303 }
19304 
19305 /* Same as above but return only single location expression.  */
19306 static dw_loc_descr_ref
loc_descriptor_from_tree(tree loc,int want_address,struct loc_descr_context * context)19307 loc_descriptor_from_tree (tree loc, int want_address,
19308 			  struct loc_descr_context *context)
19309 {
19310   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
19311   if (!ret)
19312     return NULL;
19313   if (ret->dw_loc_next)
19314     {
19315       expansion_failed (loc, NULL_RTX,
19316 			"Location list where only loc descriptor needed");
19317       return NULL;
19318     }
19319   return ret->expr;
19320 }
19321 
19322 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
19323    pointer to the declared type for the relevant field variable, or return
19324    `integer_type_node' if the given node turns out to be an
19325    ERROR_MARK node.  */
19326 
19327 static inline tree
field_type(const_tree decl)19328 field_type (const_tree decl)
19329 {
19330   tree type;
19331 
19332   if (TREE_CODE (decl) == ERROR_MARK)
19333     return integer_type_node;
19334 
19335   type = DECL_BIT_FIELD_TYPE (decl);
19336   if (type == NULL_TREE)
19337     type = TREE_TYPE (decl);
19338 
19339   return type;
19340 }
19341 
19342 /* Given a pointer to a tree node, return the alignment in bits for
19343    it, or else return BITS_PER_WORD if the node actually turns out to
19344    be an ERROR_MARK node.  */
19345 
19346 static inline unsigned
simple_type_align_in_bits(const_tree type)19347 simple_type_align_in_bits (const_tree type)
19348 {
19349   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
19350 }
19351 
19352 static inline unsigned
simple_decl_align_in_bits(const_tree decl)19353 simple_decl_align_in_bits (const_tree decl)
19354 {
19355   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
19356 }
19357 
19358 /* Return the result of rounding T up to ALIGN.  */
19359 
19360 static inline offset_int
round_up_to_align(const offset_int & t,unsigned int align)19361 round_up_to_align (const offset_int &t, unsigned int align)
19362 {
19363   return wi::udiv_trunc (t + align - 1, align) * align;
19364 }
19365 
19366 /* Compute the size of TYPE in bytes.  If possible, return NULL and store the
19367    size as an integer constant in CST_SIZE.  Otherwise, if possible, return a
19368    DWARF expression that computes the size.  Return NULL and set CST_SIZE to -1
19369    if we fail to return the size in one of these two forms.  */
19370 
19371 static dw_loc_descr_ref
type_byte_size(const_tree type,HOST_WIDE_INT * cst_size)19372 type_byte_size (const_tree type, HOST_WIDE_INT *cst_size)
19373 {
19374   tree tree_size;
19375   struct loc_descr_context ctx;
19376 
19377   /* Return a constant integer in priority, if possible.  */
19378   *cst_size = int_size_in_bytes (type);
19379   if (*cst_size != -1)
19380     return NULL;
19381 
19382   ctx.context_type = const_cast<tree> (type);
19383   ctx.base_decl = NULL_TREE;
19384   ctx.dpi = NULL;
19385   ctx.placeholder_arg = false;
19386   ctx.placeholder_seen = false;
19387 
19388   type = TYPE_MAIN_VARIANT (type);
19389   tree_size = TYPE_SIZE_UNIT (type);
19390   return ((tree_size != NULL_TREE)
19391 	  ? loc_descriptor_from_tree (tree_size, 0, &ctx)
19392 	  : NULL);
19393 }
19394 
19395 /* Helper structure for RECORD_TYPE processing.  */
19396 struct vlr_context
19397 {
19398   /* Root RECORD_TYPE.  It is needed to generate data member location
19399      descriptions in variable-length records (VLR), but also to cope with
19400      variants, which are composed of nested structures multiplexed with
19401      QUAL_UNION_TYPE nodes.  Each time such a structure is passed to a
19402      function processing a FIELD_DECL, it is required to be non null.  */
19403   tree struct_type;
19404 
19405   /* When generating a variant part in a RECORD_TYPE (i.e. a nested
19406      QUAL_UNION_TYPE), this holds an expression that computes the offset for
19407      this variant part as part of the root record (in storage units).  For
19408      regular records, it must be NULL_TREE.  */
19409   tree variant_part_offset;
19410 };
19411 
19412 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
19413    addressed byte of the "containing object" for the given FIELD_DECL. If
19414    possible, return a native constant through CST_OFFSET (in which case NULL is
19415    returned); otherwise return a DWARF expression that computes the offset.
19416 
19417    Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
19418    that offset is, either because the argument turns out to be a pointer to an
19419    ERROR_MARK node, or because the offset expression is too complex for us.
19420 
19421    CTX is required: see the comment for VLR_CONTEXT.  */
19422 
19423 static dw_loc_descr_ref
field_byte_offset(const_tree decl,struct vlr_context * ctx,HOST_WIDE_INT * cst_offset)19424 field_byte_offset (const_tree decl, struct vlr_context *ctx,
19425 		   HOST_WIDE_INT *cst_offset)
19426 {
19427   tree tree_result;
19428   dw_loc_list_ref loc_result;
19429 
19430   *cst_offset = 0;
19431 
19432   if (TREE_CODE (decl) == ERROR_MARK)
19433     return NULL;
19434   else
19435     gcc_assert (TREE_CODE (decl) == FIELD_DECL);
19436 
19437   /* We cannot handle variable bit offsets at the moment, so abort if it's the
19438      case.  */
19439   if (TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST)
19440     return NULL;
19441 
19442   /* We used to handle only constant offsets in all cases.  Now, we handle
19443      properly dynamic byte offsets only when PCC bitfield type doesn't
19444      matter.  */
19445   if (PCC_BITFIELD_TYPE_MATTERS
19446       && TREE_CODE (DECL_FIELD_OFFSET (decl)) == INTEGER_CST)
19447     {
19448       offset_int object_offset_in_bits;
19449       offset_int object_offset_in_bytes;
19450       offset_int bitpos_int;
19451       tree type;
19452       tree field_size_tree;
19453       offset_int deepest_bitpos;
19454       offset_int field_size_in_bits;
19455       unsigned int type_align_in_bits;
19456       unsigned int decl_align_in_bits;
19457       offset_int type_size_in_bits;
19458 
19459       bitpos_int = wi::to_offset (bit_position (decl));
19460       type = field_type (decl);
19461       type_size_in_bits = offset_int_type_size_in_bits (type);
19462       type_align_in_bits = simple_type_align_in_bits (type);
19463 
19464       field_size_tree = DECL_SIZE (decl);
19465 
19466       /* The size could be unspecified if there was an error, or for
19467 	 a flexible array member.  */
19468       if (!field_size_tree)
19469 	field_size_tree = bitsize_zero_node;
19470 
19471       /* If the size of the field is not constant, use the type size.  */
19472       if (TREE_CODE (field_size_tree) == INTEGER_CST)
19473 	field_size_in_bits = wi::to_offset (field_size_tree);
19474       else
19475 	field_size_in_bits = type_size_in_bits;
19476 
19477       decl_align_in_bits = simple_decl_align_in_bits (decl);
19478 
19479       /* The GCC front-end doesn't make any attempt to keep track of the
19480 	 starting bit offset (relative to the start of the containing
19481 	 structure type) of the hypothetical "containing object" for a
19482 	 bit-field.  Thus, when computing the byte offset value for the
19483 	 start of the "containing object" of a bit-field, we must deduce
19484 	 this information on our own. This can be rather tricky to do in
19485 	 some cases.  For example, handling the following structure type
19486 	 definition when compiling for an i386/i486 target (which only
19487 	 aligns long long's to 32-bit boundaries) can be very tricky:
19488 
19489 	 struct S { int field1; long long field2:31; };
19490 
19491 	 Fortunately, there is a simple rule-of-thumb which can be used
19492 	 in such cases.  When compiling for an i386/i486, GCC will
19493 	 allocate 8 bytes for the structure shown above.  It decides to
19494 	 do this based upon one simple rule for bit-field allocation.
19495 	 GCC allocates each "containing object" for each bit-field at
19496 	 the first (i.e. lowest addressed) legitimate alignment boundary
19497 	 (based upon the required minimum alignment for the declared
19498 	 type of the field) which it can possibly use, subject to the
19499 	 condition that there is still enough available space remaining
19500 	 in the containing object (when allocated at the selected point)
19501 	 to fully accommodate all of the bits of the bit-field itself.
19502 
19503 	 This simple rule makes it obvious why GCC allocates 8 bytes for
19504 	 each object of the structure type shown above.  When looking
19505 	 for a place to allocate the "containing object" for `field2',
19506 	 the compiler simply tries to allocate a 64-bit "containing
19507 	 object" at each successive 32-bit boundary (starting at zero)
19508 	 until it finds a place to allocate that 64- bit field such that
19509 	 at least 31 contiguous (and previously unallocated) bits remain
19510 	 within that selected 64 bit field.  (As it turns out, for the
19511 	 example above, the compiler finds it is OK to allocate the
19512 	 "containing object" 64-bit field at bit-offset zero within the
19513 	 structure type.)
19514 
19515 	 Here we attempt to work backwards from the limited set of facts
19516 	 we're given, and we try to deduce from those facts, where GCC
19517 	 must have believed that the containing object started (within
19518 	 the structure type). The value we deduce is then used (by the
19519 	 callers of this routine) to generate DW_AT_location and
19520 	 DW_AT_bit_offset attributes for fields (both bit-fields and, in
19521 	 the case of DW_AT_location, regular fields as well).  */
19522 
19523       /* Figure out the bit-distance from the start of the structure to
19524 	 the "deepest" bit of the bit-field.  */
19525       deepest_bitpos = bitpos_int + field_size_in_bits;
19526 
19527       /* This is the tricky part.  Use some fancy footwork to deduce
19528 	 where the lowest addressed bit of the containing object must
19529 	 be.  */
19530       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
19531 
19532       /* Round up to type_align by default.  This works best for
19533 	 bitfields.  */
19534       object_offset_in_bits
19535 	= round_up_to_align (object_offset_in_bits, type_align_in_bits);
19536 
19537       if (wi::gtu_p (object_offset_in_bits, bitpos_int))
19538 	{
19539 	  object_offset_in_bits = deepest_bitpos - type_size_in_bits;
19540 
19541 	  /* Round up to decl_align instead.  */
19542 	  object_offset_in_bits
19543 	    = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
19544 	}
19545 
19546       object_offset_in_bytes
19547 	= wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
19548       if (ctx->variant_part_offset == NULL_TREE)
19549 	{
19550 	  *cst_offset = object_offset_in_bytes.to_shwi ();
19551 	  return NULL;
19552 	}
19553       tree_result = wide_int_to_tree (sizetype, object_offset_in_bytes);
19554     }
19555   else
19556     tree_result = byte_position (decl);
19557 
19558   if (ctx->variant_part_offset != NULL_TREE)
19559     tree_result = fold_build2 (PLUS_EXPR, TREE_TYPE (tree_result),
19560 			       ctx->variant_part_offset, tree_result);
19561 
19562   /* If the byte offset is a constant, it's simplier to handle a native
19563      constant rather than a DWARF expression.  */
19564   if (TREE_CODE (tree_result) == INTEGER_CST)
19565     {
19566       *cst_offset = wi::to_offset (tree_result).to_shwi ();
19567       return NULL;
19568     }
19569   struct loc_descr_context loc_ctx = {
19570     ctx->struct_type, /* context_type */
19571     NULL_TREE,	      /* base_decl */
19572     NULL,	      /* dpi */
19573     false,	      /* placeholder_arg */
19574     false	      /* placeholder_seen */
19575   };
19576   loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
19577 
19578   /* We want a DWARF expression: abort if we only have a location list with
19579      multiple elements.  */
19580   if (!loc_result || !single_element_loc_list_p (loc_result))
19581     return NULL;
19582   else
19583     return loc_result->expr;
19584 }
19585 
19586 /* The following routines define various Dwarf attributes and any data
19587    associated with them.  */
19588 
19589 /* Add a location description attribute value to a DIE.
19590 
19591    This emits location attributes suitable for whole variables and
19592    whole parameters.  Note that the location attributes for struct fields are
19593    generated by the routine `data_member_location_attribute' below.  */
19594 
19595 static inline void
add_AT_location_description(dw_die_ref die,enum dwarf_attribute attr_kind,dw_loc_list_ref descr)19596 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
19597 			     dw_loc_list_ref descr)
19598 {
19599   bool check_no_locviews = true;
19600   if (descr == 0)
19601     return;
19602   if (single_element_loc_list_p (descr))
19603     add_AT_loc (die, attr_kind, descr->expr);
19604   else
19605     {
19606       add_AT_loc_list (die, attr_kind, descr);
19607       gcc_assert (descr->ll_symbol);
19608       if (attr_kind == DW_AT_location && descr->vl_symbol
19609 	  && dwarf2out_locviews_in_attribute ())
19610 	{
19611 	  add_AT_view_list (die, DW_AT_GNU_locviews);
19612 	  check_no_locviews = false;
19613 	}
19614     }
19615 
19616   if (check_no_locviews)
19617     gcc_assert (!get_AT (die, DW_AT_GNU_locviews));
19618 }
19619 
19620 /* Add DW_AT_accessibility attribute to DIE if needed.  */
19621 
19622 static void
add_accessibility_attribute(dw_die_ref die,tree decl)19623 add_accessibility_attribute (dw_die_ref die, tree decl)
19624 {
19625   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19626      children, otherwise the default is DW_ACCESS_public.  In DWARF2
19627      the default has always been DW_ACCESS_public.  */
19628   if (TREE_PROTECTED (decl))
19629     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19630   else if (TREE_PRIVATE (decl))
19631     {
19632       if (dwarf_version == 2
19633 	  || die->die_parent == NULL
19634 	  || die->die_parent->die_tag != DW_TAG_class_type)
19635 	add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19636     }
19637   else if (dwarf_version > 2
19638 	   && die->die_parent
19639 	   && die->die_parent->die_tag == DW_TAG_class_type)
19640     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19641 }
19642 
19643 /* Attach the specialized form of location attribute used for data members of
19644    struct and union types.  In the special case of a FIELD_DECL node which
19645    represents a bit-field, the "offset" part of this special location
19646    descriptor must indicate the distance in bytes from the lowest-addressed
19647    byte of the containing struct or union type to the lowest-addressed byte of
19648    the "containing object" for the bit-field.  (See the `field_byte_offset'
19649    function above).
19650 
19651    For any given bit-field, the "containing object" is a hypothetical object
19652    (of some integral or enum type) within which the given bit-field lives.  The
19653    type of this hypothetical "containing object" is always the same as the
19654    declared type of the individual bit-field itself (for GCC anyway... the
19655    DWARF spec doesn't actually mandate this).  Note that it is the size (in
19656    bytes) of the hypothetical "containing object" which will be given in the
19657    DW_AT_byte_size attribute for this bit-field.  (See the
19658    `byte_size_attribute' function below.)  It is also used when calculating the
19659    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
19660    function below.)
19661 
19662    CTX is required: see the comment for VLR_CONTEXT.  */
19663 
19664 static void
add_data_member_location_attribute(dw_die_ref die,tree decl,struct vlr_context * ctx)19665 add_data_member_location_attribute (dw_die_ref die,
19666 				    tree decl,
19667 				    struct vlr_context *ctx)
19668 {
19669   HOST_WIDE_INT offset;
19670   dw_loc_descr_ref loc_descr = 0;
19671 
19672   if (TREE_CODE (decl) == TREE_BINFO)
19673     {
19674       /* We're working on the TAG_inheritance for a base class.  */
19675       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
19676 	{
19677 	  /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
19678 	     aren't at a fixed offset from all (sub)objects of the same
19679 	     type.  We need to extract the appropriate offset from our
19680 	     vtable.  The following dwarf expression means
19681 
19682 	       BaseAddr = ObAddr + *((*ObAddr) - Offset)
19683 
19684 	     This is specific to the V3 ABI, of course.  */
19685 
19686 	  dw_loc_descr_ref tmp;
19687 
19688 	  /* Make a copy of the object address.  */
19689 	  tmp = new_loc_descr (DW_OP_dup, 0, 0);
19690 	  add_loc_descr (&loc_descr, tmp);
19691 
19692 	  /* Extract the vtable address.  */
19693 	  tmp = new_loc_descr (DW_OP_deref, 0, 0);
19694 	  add_loc_descr (&loc_descr, tmp);
19695 
19696 	  /* Calculate the address of the offset.  */
19697 	  offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
19698 	  gcc_assert (offset < 0);
19699 
19700 	  tmp = int_loc_descriptor (-offset);
19701 	  add_loc_descr (&loc_descr, tmp);
19702 	  tmp = new_loc_descr (DW_OP_minus, 0, 0);
19703 	  add_loc_descr (&loc_descr, tmp);
19704 
19705 	  /* Extract the offset.  */
19706 	  tmp = new_loc_descr (DW_OP_deref, 0, 0);
19707 	  add_loc_descr (&loc_descr, tmp);
19708 
19709 	  /* Add it to the object address.  */
19710 	  tmp = new_loc_descr (DW_OP_plus, 0, 0);
19711 	  add_loc_descr (&loc_descr, tmp);
19712 	}
19713       else
19714 	offset = tree_to_shwi (BINFO_OFFSET (decl));
19715     }
19716   else
19717     {
19718       loc_descr = field_byte_offset (decl, ctx, &offset);
19719 
19720       /* If loc_descr is available then we know the field offset is dynamic.
19721 	 However, GDB does not handle dynamic field offsets very well at the
19722 	 moment.  */
19723       if (loc_descr != NULL && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
19724 	{
19725 	  loc_descr = NULL;
19726 	  offset = 0;
19727 	}
19728 
19729       /* Data member location evalutation starts with the base address on the
19730 	 stack.  Compute the field offset and add it to this base address.  */
19731       else if (loc_descr != NULL)
19732 	add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
19733     }
19734 
19735   if (! loc_descr)
19736     {
19737       /* While DW_AT_data_bit_offset has been added already in DWARF4,
19738 	 e.g. GDB only added support to it in November 2016.  For DWARF5
19739 	 we need newer debug info consumers anyway.  We might change this
19740 	 to dwarf_version >= 4 once most consumers catched up.  */
19741       if (dwarf_version >= 5
19742 	  && TREE_CODE (decl) == FIELD_DECL
19743 	  && DECL_BIT_FIELD_TYPE (decl)
19744 	  && (ctx->variant_part_offset == NULL_TREE
19745 	      || TREE_CODE (ctx->variant_part_offset) == INTEGER_CST))
19746 	{
19747 	  tree off = bit_position (decl);
19748 	  if (ctx->variant_part_offset)
19749 	    off = bit_from_pos (ctx->variant_part_offset, off);
19750 	  if (tree_fits_uhwi_p (off) && get_AT (die, DW_AT_bit_size))
19751 	    {
19752 	      remove_AT (die, DW_AT_byte_size);
19753 	      remove_AT (die, DW_AT_bit_offset);
19754 	      add_AT_unsigned (die, DW_AT_data_bit_offset, tree_to_uhwi (off));
19755 	      return;
19756 	    }
19757 	}
19758       if (dwarf_version > 2)
19759 	{
19760 	  /* Don't need to output a location expression, just the constant. */
19761 	  if (offset < 0)
19762 	    add_AT_int (die, DW_AT_data_member_location, offset);
19763 	  else
19764 	    add_AT_unsigned (die, DW_AT_data_member_location, offset);
19765 	  return;
19766 	}
19767       else
19768 	{
19769 	  enum dwarf_location_atom op;
19770 
19771 	  /* The DWARF2 standard says that we should assume that the structure
19772 	     address is already on the stack, so we can specify a structure
19773 	     field address by using DW_OP_plus_uconst.  */
19774 	  op = DW_OP_plus_uconst;
19775 	  loc_descr = new_loc_descr (op, offset, 0);
19776 	}
19777     }
19778 
19779   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
19780 }
19781 
19782 /* Writes integer values to dw_vec_const array.  */
19783 
19784 static void
insert_int(HOST_WIDE_INT val,unsigned int size,unsigned char * dest)19785 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
19786 {
19787   while (size != 0)
19788     {
19789       *dest++ = val & 0xff;
19790       val >>= 8;
19791       --size;
19792     }
19793 }
19794 
19795 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
19796 
19797 static HOST_WIDE_INT
extract_int(const unsigned char * src,unsigned int size)19798 extract_int (const unsigned char *src, unsigned int size)
19799 {
19800   HOST_WIDE_INT val = 0;
19801 
19802   src += size;
19803   while (size != 0)
19804     {
19805       val <<= 8;
19806       val |= *--src & 0xff;
19807       --size;
19808     }
19809   return val;
19810 }
19811 
19812 /* Writes wide_int values to dw_vec_const array.  */
19813 
19814 static void
insert_wide_int(const wide_int & val,unsigned char * dest,int elt_size)19815 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
19816 {
19817   int i;
19818 
19819   if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
19820     {
19821       insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
19822       return;
19823     }
19824 
19825   /* We'd have to extend this code to support odd sizes.  */
19826   gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
19827 
19828   int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
19829 
19830   if (WORDS_BIG_ENDIAN)
19831     for (i = n - 1; i >= 0; i--)
19832       {
19833 	insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
19834 	dest += sizeof (HOST_WIDE_INT);
19835       }
19836   else
19837     for (i = 0; i < n; i++)
19838       {
19839 	insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
19840 	dest += sizeof (HOST_WIDE_INT);
19841       }
19842 }
19843 
19844 /* Writes floating point values to dw_vec_const array.  */
19845 
19846 static unsigned
insert_float(const_rtx rtl,unsigned char * array)19847 insert_float (const_rtx rtl, unsigned char *array)
19848 {
19849   long val[4];
19850   int i;
19851   scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
19852 
19853   real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), mode);
19854 
19855   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
19856   if (GET_MODE_SIZE (mode) < 4)
19857     {
19858       gcc_assert (GET_MODE_SIZE (mode) == 2);
19859       insert_int (val[0], 2, array);
19860       return 2;
19861     }
19862 
19863   for (i = 0; i < GET_MODE_SIZE (mode) / 4; i++)
19864     {
19865       insert_int (val[i], 4, array);
19866       array += 4;
19867     }
19868   return 4;
19869 }
19870 
19871 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
19872    does not have a "location" either in memory or in a register.  These
19873    things can arise in GNU C when a constant is passed as an actual parameter
19874    to an inlined function.  They can also arise in C++ where declared
19875    constants do not necessarily get memory "homes".  */
19876 
19877 static bool
add_const_value_attribute(dw_die_ref die,rtx rtl)19878 add_const_value_attribute (dw_die_ref die, rtx rtl)
19879 {
19880   switch (GET_CODE (rtl))
19881     {
19882     case CONST_INT:
19883       {
19884 	HOST_WIDE_INT val = INTVAL (rtl);
19885 
19886 	if (val < 0)
19887 	  add_AT_int (die, DW_AT_const_value, val);
19888 	else
19889 	  add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
19890       }
19891       return true;
19892 
19893     case CONST_WIDE_INT:
19894       {
19895 	wide_int w1 = rtx_mode_t (rtl, MAX_MODE_INT);
19896 	unsigned int prec = MIN (wi::min_precision (w1, UNSIGNED),
19897 				 (unsigned int) CONST_WIDE_INT_NUNITS (rtl)
19898 				 * HOST_BITS_PER_WIDE_INT);
19899 	wide_int w = wide_int::from (w1, prec, UNSIGNED);
19900 	add_AT_wide (die, DW_AT_const_value, w);
19901       }
19902       return true;
19903 
19904     case CONST_DOUBLE:
19905       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
19906 	 floating-point constant.  A CONST_DOUBLE is used whenever the
19907 	 constant requires more than one word in order to be adequately
19908 	 represented.  */
19909       if (TARGET_SUPPORTS_WIDE_INT == 0
19910 	  && !SCALAR_FLOAT_MODE_P (GET_MODE (rtl)))
19911 	add_AT_double (die, DW_AT_const_value,
19912 		       CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
19913       else
19914 	{
19915 	  scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
19916 	  unsigned int length = GET_MODE_SIZE (mode);
19917 	  unsigned char *array = ggc_vec_alloc<unsigned char> (length);
19918 	  unsigned int elt_size = insert_float (rtl, array);
19919 
19920 	  add_AT_vec (die, DW_AT_const_value, length / elt_size, elt_size,
19921 		      array);
19922 	}
19923       return true;
19924 
19925     case CONST_VECTOR:
19926       {
19927 	unsigned int length;
19928 	if (!CONST_VECTOR_NUNITS (rtl).is_constant (&length))
19929 	  return false;
19930 
19931 	machine_mode mode = GET_MODE (rtl);
19932 	/* The combination of a length and byte elt_size doesn't extend
19933 	   naturally to boolean vectors, where several elements are packed
19934 	   into the same byte.  */
19935 	if (GET_MODE_CLASS (mode) == MODE_VECTOR_BOOL)
19936 	  return false;
19937 
19938 	unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
19939 	unsigned char *array
19940 	  = ggc_vec_alloc<unsigned char> (length * elt_size);
19941 	unsigned int i;
19942 	unsigned char *p;
19943 	machine_mode imode = GET_MODE_INNER (mode);
19944 
19945 	switch (GET_MODE_CLASS (mode))
19946 	  {
19947 	  case MODE_VECTOR_INT:
19948 	    for (i = 0, p = array; i < length; i++, p += elt_size)
19949 	      {
19950 		rtx elt = CONST_VECTOR_ELT (rtl, i);
19951 		insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
19952 	      }
19953 	    break;
19954 
19955 	  case MODE_VECTOR_FLOAT:
19956 	    for (i = 0, p = array; i < length; i++, p += elt_size)
19957 	      {
19958 		rtx elt = CONST_VECTOR_ELT (rtl, i);
19959 		insert_float (elt, p);
19960 	      }
19961 	    break;
19962 
19963 	  default:
19964 	    gcc_unreachable ();
19965 	  }
19966 
19967 	add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
19968       }
19969       return true;
19970 
19971     case CONST_STRING:
19972       if (dwarf_version >= 4 || !dwarf_strict)
19973 	{
19974 	  dw_loc_descr_ref loc_result;
19975 	  resolve_one_addr (&rtl);
19976 	rtl_addr:
19977           loc_result = new_addr_loc_descr (rtl, dtprel_false);
19978 	  add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
19979 	  add_AT_loc (die, DW_AT_location, loc_result);
19980 	  vec_safe_push (used_rtx_array, rtl);
19981 	  return true;
19982 	}
19983       return false;
19984 
19985     case CONST:
19986       if (CONSTANT_P (XEXP (rtl, 0)))
19987 	return add_const_value_attribute (die, XEXP (rtl, 0));
19988       /* FALLTHROUGH */
19989     case SYMBOL_REF:
19990       if (!const_ok_for_output (rtl))
19991 	return false;
19992       /* FALLTHROUGH */
19993     case LABEL_REF:
19994       if (dwarf_version >= 4 || !dwarf_strict)
19995 	goto rtl_addr;
19996       return false;
19997 
19998     case PLUS:
19999       /* In cases where an inlined instance of an inline function is passed
20000 	 the address of an `auto' variable (which is local to the caller) we
20001 	 can get a situation where the DECL_RTL of the artificial local
20002 	 variable (for the inlining) which acts as a stand-in for the
20003 	 corresponding formal parameter (of the inline function) will look
20004 	 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
20005 	 exactly a compile-time constant expression, but it isn't the address
20006 	 of the (artificial) local variable either.  Rather, it represents the
20007 	 *value* which the artificial local variable always has during its
20008 	 lifetime.  We currently have no way to represent such quasi-constant
20009 	 values in Dwarf, so for now we just punt and generate nothing.  */
20010       return false;
20011 
20012     case HIGH:
20013     case CONST_FIXED:
20014     case MINUS:
20015     case SIGN_EXTEND:
20016     case ZERO_EXTEND:
20017     case CONST_POLY_INT:
20018       return false;
20019 
20020     case MEM:
20021       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
20022 	  && MEM_READONLY_P (rtl)
20023 	  && GET_MODE (rtl) == BLKmode)
20024 	{
20025 	  add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
20026 	  return true;
20027 	}
20028       return false;
20029 
20030     default:
20031       /* No other kinds of rtx should be possible here.  */
20032       gcc_unreachable ();
20033     }
20034   return false;
20035 }
20036 
20037 /* Determine whether the evaluation of EXPR references any variables
20038    or functions which aren't otherwise used (and therefore may not be
20039    output).  */
20040 static tree
reference_to_unused(tree * tp,int * walk_subtrees,void * data ATTRIBUTE_UNUSED)20041 reference_to_unused (tree * tp, int * walk_subtrees,
20042 		     void * data ATTRIBUTE_UNUSED)
20043 {
20044   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
20045     *walk_subtrees = 0;
20046 
20047   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
20048       && ! TREE_ASM_WRITTEN (*tp))
20049     return *tp;
20050   /* ???  The C++ FE emits debug information for using decls, so
20051      putting gcc_unreachable here falls over.  See PR31899.  For now
20052      be conservative.  */
20053   else if (!symtab->global_info_ready && VAR_P (*tp))
20054     return *tp;
20055   else if (VAR_P (*tp))
20056     {
20057       varpool_node *node = varpool_node::get (*tp);
20058       if (!node || !node->definition)
20059 	return *tp;
20060     }
20061   else if (TREE_CODE (*tp) == FUNCTION_DECL
20062 	   && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
20063     {
20064       /* The call graph machinery must have finished analyzing,
20065          optimizing and gimplifying the CU by now.
20066 	 So if *TP has no call graph node associated
20067 	 to it, it means *TP will not be emitted.  */
20068       if (!symtab->global_info_ready || !cgraph_node::get (*tp))
20069 	return *tp;
20070     }
20071   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
20072     return *tp;
20073 
20074   return NULL_TREE;
20075 }
20076 
20077 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
20078    for use in a later add_const_value_attribute call.  */
20079 
20080 static rtx
rtl_for_decl_init(tree init,tree type)20081 rtl_for_decl_init (tree init, tree type)
20082 {
20083   rtx rtl = NULL_RTX;
20084 
20085   STRIP_NOPS (init);
20086 
20087   /* If a variable is initialized with a string constant without embedded
20088      zeros, build CONST_STRING.  */
20089   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
20090     {
20091       tree enttype = TREE_TYPE (type);
20092       tree domain = TYPE_DOMAIN (type);
20093       scalar_int_mode mode;
20094 
20095       if (is_int_mode (TYPE_MODE (enttype), &mode)
20096 	  && GET_MODE_SIZE (mode) == 1
20097 	  && domain
20098 	  && TYPE_MAX_VALUE (domain)
20099 	  && TREE_CODE (TYPE_MAX_VALUE (domain)) == INTEGER_CST
20100 	  && integer_zerop (TYPE_MIN_VALUE (domain))
20101 	  && compare_tree_int (TYPE_MAX_VALUE (domain),
20102 			       TREE_STRING_LENGTH (init) - 1) == 0
20103 	  && ((size_t) TREE_STRING_LENGTH (init)
20104 	      == strlen (TREE_STRING_POINTER (init)) + 1))
20105 	{
20106 	  rtl = gen_rtx_CONST_STRING (VOIDmode,
20107 				      ggc_strdup (TREE_STRING_POINTER (init)));
20108 	  rtl = gen_rtx_MEM (BLKmode, rtl);
20109 	  MEM_READONLY_P (rtl) = 1;
20110 	}
20111     }
20112   /* Other aggregates, and complex values, could be represented using
20113      CONCAT: FIXME!  */
20114   else if (AGGREGATE_TYPE_P (type)
20115 	   || (TREE_CODE (init) == VIEW_CONVERT_EXPR
20116 	       && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
20117 	   || TREE_CODE (type) == COMPLEX_TYPE)
20118     ;
20119   /* Vectors only work if their mode is supported by the target.
20120      FIXME: generic vectors ought to work too.  */
20121   else if (TREE_CODE (type) == VECTOR_TYPE
20122 	   && !VECTOR_MODE_P (TYPE_MODE (type)))
20123     ;
20124   /* If the initializer is something that we know will expand into an
20125      immediate RTL constant, expand it now.  We must be careful not to
20126      reference variables which won't be output.  */
20127   else if (initializer_constant_valid_p (init, type)
20128 	   && ! walk_tree (&init, reference_to_unused, NULL, NULL))
20129     {
20130       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
20131 	 possible.  */
20132       if (TREE_CODE (type) == VECTOR_TYPE)
20133 	switch (TREE_CODE (init))
20134 	  {
20135 	  case VECTOR_CST:
20136 	    break;
20137 	  case CONSTRUCTOR:
20138 	    if (TREE_CONSTANT (init))
20139 	      {
20140 		vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
20141 		bool constant_p = true;
20142 		tree value;
20143 		unsigned HOST_WIDE_INT ix;
20144 
20145 		/* Even when ctor is constant, it might contain non-*_CST
20146 		   elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
20147 		   belong into VECTOR_CST nodes.  */
20148 		FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
20149 		  if (!CONSTANT_CLASS_P (value))
20150 		    {
20151 		      constant_p = false;
20152 		      break;
20153 		    }
20154 
20155 		if (constant_p)
20156 		  {
20157 		    init = build_vector_from_ctor (type, elts);
20158 		    break;
20159 		  }
20160 	      }
20161 	    /* FALLTHRU */
20162 
20163 	  default:
20164 	    return NULL;
20165 	  }
20166 
20167       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
20168 
20169       /* If expand_expr returns a MEM, it wasn't immediate.  */
20170       gcc_assert (!rtl || !MEM_P (rtl));
20171     }
20172 
20173   return rtl;
20174 }
20175 
20176 /* Generate RTL for the variable DECL to represent its location.  */
20177 
20178 static rtx
rtl_for_decl_location(tree decl)20179 rtl_for_decl_location (tree decl)
20180 {
20181   rtx rtl;
20182 
20183   /* Here we have to decide where we are going to say the parameter "lives"
20184      (as far as the debugger is concerned).  We only have a couple of
20185      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
20186 
20187      DECL_RTL normally indicates where the parameter lives during most of the
20188      activation of the function.  If optimization is enabled however, this
20189      could be either NULL or else a pseudo-reg.  Both of those cases indicate
20190      that the parameter doesn't really live anywhere (as far as the code
20191      generation parts of GCC are concerned) during most of the function's
20192      activation.  That will happen (for example) if the parameter is never
20193      referenced within the function.
20194 
20195      We could just generate a location descriptor here for all non-NULL
20196      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
20197      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
20198      where DECL_RTL is NULL or is a pseudo-reg.
20199 
20200      Note however that we can only get away with using DECL_INCOMING_RTL as
20201      a backup substitute for DECL_RTL in certain limited cases.  In cases
20202      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
20203      we can be sure that the parameter was passed using the same type as it is
20204      declared to have within the function, and that its DECL_INCOMING_RTL
20205      points us to a place where a value of that type is passed.
20206 
20207      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
20208      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
20209      because in these cases DECL_INCOMING_RTL points us to a value of some
20210      type which is *different* from the type of the parameter itself.  Thus,
20211      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
20212      such cases, the debugger would end up (for example) trying to fetch a
20213      `float' from a place which actually contains the first part of a
20214      `double'.  That would lead to really incorrect and confusing
20215      output at debug-time.
20216 
20217      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
20218      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
20219      are a couple of exceptions however.  On little-endian machines we can
20220      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
20221      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
20222      an integral type that is smaller than TREE_TYPE (decl). These cases arise
20223      when (on a little-endian machine) a non-prototyped function has a
20224      parameter declared to be of type `short' or `char'.  In such cases,
20225      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
20226      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
20227      passed `int' value.  If the debugger then uses that address to fetch
20228      a `short' or a `char' (on a little-endian machine) the result will be
20229      the correct data, so we allow for such exceptional cases below.
20230 
20231      Note that our goal here is to describe the place where the given formal
20232      parameter lives during most of the function's activation (i.e. between the
20233      end of the prologue and the start of the epilogue).  We'll do that as best
20234      as we can. Note however that if the given formal parameter is modified
20235      sometime during the execution of the function, then a stack backtrace (at
20236      debug-time) will show the function as having been called with the *new*
20237      value rather than the value which was originally passed in.  This happens
20238      rarely enough that it is not a major problem, but it *is* a problem, and
20239      I'd like to fix it.
20240 
20241      A future version of dwarf2out.c may generate two additional attributes for
20242      any given DW_TAG_formal_parameter DIE which will describe the "passed
20243      type" and the "passed location" for the given formal parameter in addition
20244      to the attributes we now generate to indicate the "declared type" and the
20245      "active location" for each parameter.  This additional set of attributes
20246      could be used by debuggers for stack backtraces. Separately, note that
20247      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
20248      This happens (for example) for inlined-instances of inline function formal
20249      parameters which are never referenced.  This really shouldn't be
20250      happening.  All PARM_DECL nodes should get valid non-NULL
20251      DECL_INCOMING_RTL values.  FIXME.  */
20252 
20253   /* Use DECL_RTL as the "location" unless we find something better.  */
20254   rtl = DECL_RTL_IF_SET (decl);
20255 
20256   /* When generating abstract instances, ignore everything except
20257      constants, symbols living in memory, and symbols living in
20258      fixed registers.  */
20259   if (! reload_completed)
20260     {
20261       if (rtl
20262 	  && (CONSTANT_P (rtl)
20263 	      || (MEM_P (rtl)
20264 	          && CONSTANT_P (XEXP (rtl, 0)))
20265 	      || (REG_P (rtl)
20266 	          && VAR_P (decl)
20267 		  && TREE_STATIC (decl))))
20268 	{
20269 	  rtl = targetm.delegitimize_address (rtl);
20270 	  return rtl;
20271 	}
20272       rtl = NULL_RTX;
20273     }
20274   else if (TREE_CODE (decl) == PARM_DECL)
20275     {
20276       if (rtl == NULL_RTX
20277 	  || is_pseudo_reg (rtl)
20278 	  || (MEM_P (rtl)
20279 	      && is_pseudo_reg (XEXP (rtl, 0))
20280 	      && DECL_INCOMING_RTL (decl)
20281 	      && MEM_P (DECL_INCOMING_RTL (decl))
20282 	      && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
20283 	{
20284 	  tree declared_type = TREE_TYPE (decl);
20285 	  tree passed_type = DECL_ARG_TYPE (decl);
20286 	  machine_mode dmode = TYPE_MODE (declared_type);
20287 	  machine_mode pmode = TYPE_MODE (passed_type);
20288 
20289 	  /* This decl represents a formal parameter which was optimized out.
20290 	     Note that DECL_INCOMING_RTL may be NULL in here, but we handle
20291 	     all cases where (rtl == NULL_RTX) just below.  */
20292 	  if (dmode == pmode)
20293 	    rtl = DECL_INCOMING_RTL (decl);
20294 	  else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
20295 		   && SCALAR_INT_MODE_P (dmode)
20296 		   && known_le (GET_MODE_SIZE (dmode), GET_MODE_SIZE (pmode))
20297 		   && DECL_INCOMING_RTL (decl))
20298 	    {
20299 	      rtx inc = DECL_INCOMING_RTL (decl);
20300 	      if (REG_P (inc))
20301 		rtl = inc;
20302 	      else if (MEM_P (inc))
20303 		{
20304 		  if (BYTES_BIG_ENDIAN)
20305 		    rtl = adjust_address_nv (inc, dmode,
20306 					     GET_MODE_SIZE (pmode)
20307 					     - GET_MODE_SIZE (dmode));
20308 		  else
20309 		    rtl = inc;
20310 		}
20311 	    }
20312 	}
20313 
20314       /* If the parm was passed in registers, but lives on the stack, then
20315 	 make a big endian correction if the mode of the type of the
20316 	 parameter is not the same as the mode of the rtl.  */
20317       /* ??? This is the same series of checks that are made in dbxout.c before
20318 	 we reach the big endian correction code there.  It isn't clear if all
20319 	 of these checks are necessary here, but keeping them all is the safe
20320 	 thing to do.  */
20321       else if (MEM_P (rtl)
20322 	       && XEXP (rtl, 0) != const0_rtx
20323 	       && ! CONSTANT_P (XEXP (rtl, 0))
20324 	       /* Not passed in memory.  */
20325 	       && !MEM_P (DECL_INCOMING_RTL (decl))
20326 	       /* Not passed by invisible reference.  */
20327 	       && (!REG_P (XEXP (rtl, 0))
20328 		   || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
20329 		   || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
20330 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
20331 		   || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
20332 #endif
20333 		     )
20334 	       /* Big endian correction check.  */
20335 	       && BYTES_BIG_ENDIAN
20336 	       && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
20337 	       && known_lt (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))),
20338 			    UNITS_PER_WORD))
20339 	{
20340 	  machine_mode addr_mode = get_address_mode (rtl);
20341 	  poly_int64 offset = (UNITS_PER_WORD
20342 			       - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
20343 
20344 	  rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
20345 			     plus_constant (addr_mode, XEXP (rtl, 0), offset));
20346 	}
20347     }
20348   else if (VAR_P (decl)
20349 	   && rtl
20350 	   && MEM_P (rtl)
20351 	   && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl)))
20352     {
20353       machine_mode addr_mode = get_address_mode (rtl);
20354       poly_int64 offset = byte_lowpart_offset (TYPE_MODE (TREE_TYPE (decl)),
20355 					       GET_MODE (rtl));
20356 
20357       /* If a variable is declared "register" yet is smaller than
20358 	 a register, then if we store the variable to memory, it
20359 	 looks like we're storing a register-sized value, when in
20360 	 fact we are not.  We need to adjust the offset of the
20361 	 storage location to reflect the actual value's bytes,
20362 	 else gdb will not be able to display it.  */
20363       if (maybe_ne (offset, 0))
20364 	rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
20365 			   plus_constant (addr_mode, XEXP (rtl, 0), offset));
20366     }
20367 
20368   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
20369      and will have been substituted directly into all expressions that use it.
20370      C does not have such a concept, but C++ and other languages do.  */
20371   if (!rtl && VAR_P (decl) && DECL_INITIAL (decl))
20372     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
20373 
20374   if (rtl)
20375     rtl = targetm.delegitimize_address (rtl);
20376 
20377   /* If we don't look past the constant pool, we risk emitting a
20378      reference to a constant pool entry that isn't referenced from
20379      code, and thus is not emitted.  */
20380   if (rtl)
20381     rtl = avoid_constant_pool_reference (rtl);
20382 
20383   /* Try harder to get a rtl.  If this symbol ends up not being emitted
20384      in the current CU, resolve_addr will remove the expression referencing
20385      it.  */
20386   if (rtl == NULL_RTX
20387       && !(early_dwarf && (flag_generate_lto || flag_generate_offload))
20388       && VAR_P (decl)
20389       && !DECL_EXTERNAL (decl)
20390       && TREE_STATIC (decl)
20391       && DECL_NAME (decl)
20392       && !DECL_HARD_REGISTER (decl)
20393       && DECL_MODE (decl) != VOIDmode)
20394     {
20395       rtl = make_decl_rtl_for_debug (decl);
20396       if (!MEM_P (rtl)
20397 	  || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
20398 	  || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
20399 	rtl = NULL_RTX;
20400     }
20401 
20402   return rtl;
20403 }
20404 
20405 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
20406    returned.  If so, the decl for the COMMON block is returned, and the
20407    value is the offset into the common block for the symbol.  */
20408 
20409 static tree
fortran_common(tree decl,HOST_WIDE_INT * value)20410 fortran_common (tree decl, HOST_WIDE_INT *value)
20411 {
20412   tree val_expr, cvar;
20413   machine_mode mode;
20414   poly_int64 bitsize, bitpos;
20415   tree offset;
20416   HOST_WIDE_INT cbitpos;
20417   int unsignedp, reversep, volatilep = 0;
20418 
20419   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
20420      it does not have a value (the offset into the common area), or if it
20421      is thread local (as opposed to global) then it isn't common, and shouldn't
20422      be handled as such.  */
20423   if (!VAR_P (decl)
20424       || !TREE_STATIC (decl)
20425       || !DECL_HAS_VALUE_EXPR_P (decl)
20426       || !is_fortran ())
20427     return NULL_TREE;
20428 
20429   val_expr = DECL_VALUE_EXPR (decl);
20430   if (TREE_CODE (val_expr) != COMPONENT_REF)
20431     return NULL_TREE;
20432 
20433   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
20434 			      &unsignedp, &reversep, &volatilep);
20435 
20436   if (cvar == NULL_TREE
20437       || !VAR_P (cvar)
20438       || DECL_ARTIFICIAL (cvar)
20439       || !TREE_PUBLIC (cvar)
20440       /* We don't expect to have to cope with variable offsets,
20441 	 since at present all static data must have a constant size.  */
20442       || !bitpos.is_constant (&cbitpos))
20443     return NULL_TREE;
20444 
20445   *value = 0;
20446   if (offset != NULL)
20447     {
20448       if (!tree_fits_shwi_p (offset))
20449 	return NULL_TREE;
20450       *value = tree_to_shwi (offset);
20451     }
20452   if (cbitpos != 0)
20453     *value += cbitpos / BITS_PER_UNIT;
20454 
20455   return cvar;
20456 }
20457 
20458 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
20459    data attribute for a variable or a parameter.  We generate the
20460    DW_AT_const_value attribute only in those cases where the given variable
20461    or parameter does not have a true "location" either in memory or in a
20462    register.  This can happen (for example) when a constant is passed as an
20463    actual argument in a call to an inline function.  (It's possible that
20464    these things can crop up in other ways also.)  Note that one type of
20465    constant value which can be passed into an inlined function is a constant
20466    pointer.  This can happen for example if an actual argument in an inlined
20467    function call evaluates to a compile-time constant address.
20468 
20469    CACHE_P is true if it is worth caching the location list for DECL,
20470    so that future calls can reuse it rather than regenerate it from scratch.
20471    This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
20472    since we will need to refer to them each time the function is inlined.  */
20473 
20474 static bool
add_location_or_const_value_attribute(dw_die_ref die,tree decl,bool cache_p)20475 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
20476 {
20477   rtx rtl;
20478   dw_loc_list_ref list;
20479   var_loc_list *loc_list;
20480   cached_dw_loc_list *cache;
20481 
20482   if (early_dwarf)
20483     return false;
20484 
20485   if (TREE_CODE (decl) == ERROR_MARK)
20486     return false;
20487 
20488   if (get_AT (die, DW_AT_location)
20489       || get_AT (die, DW_AT_const_value))
20490     return true;
20491 
20492   gcc_assert (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
20493 	      || TREE_CODE (decl) == RESULT_DECL);
20494 
20495   /* Try to get some constant RTL for this decl, and use that as the value of
20496      the location.  */
20497 
20498   rtl = rtl_for_decl_location (decl);
20499   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
20500       && add_const_value_attribute (die, rtl))
20501     return true;
20502 
20503   /* See if we have single element location list that is equivalent to
20504      a constant value.  That way we are better to use add_const_value_attribute
20505      rather than expanding constant value equivalent.  */
20506   loc_list = lookup_decl_loc (decl);
20507   if (loc_list
20508       && loc_list->first
20509       && loc_list->first->next == NULL
20510       && NOTE_P (loc_list->first->loc)
20511       && NOTE_VAR_LOCATION (loc_list->first->loc)
20512       && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
20513     {
20514       struct var_loc_node *node;
20515 
20516       node = loc_list->first;
20517       rtl = NOTE_VAR_LOCATION_LOC (node->loc);
20518       if (GET_CODE (rtl) == EXPR_LIST)
20519 	rtl = XEXP (rtl, 0);
20520       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
20521 	  && add_const_value_attribute (die, rtl))
20522 	 return true;
20523     }
20524   /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
20525      list several times.  See if we've already cached the contents.  */
20526   list = NULL;
20527   if (loc_list == NULL || cached_dw_loc_list_table == NULL)
20528     cache_p = false;
20529   if (cache_p)
20530     {
20531       cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
20532       if (cache)
20533 	list = cache->loc_list;
20534     }
20535   if (list == NULL)
20536     {
20537       list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
20538 				 NULL);
20539       /* It is usually worth caching this result if the decl is from
20540 	 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements.  */
20541       if (cache_p && list && list->dw_loc_next)
20542 	{
20543 	  cached_dw_loc_list **slot
20544 	    = cached_dw_loc_list_table->find_slot_with_hash (decl,
20545 							     DECL_UID (decl),
20546 							     INSERT);
20547 	  cache = ggc_cleared_alloc<cached_dw_loc_list> ();
20548 	  cache->decl_id = DECL_UID (decl);
20549 	  cache->loc_list = list;
20550 	  *slot = cache;
20551 	}
20552     }
20553   if (list)
20554     {
20555       add_AT_location_description (die, DW_AT_location, list);
20556       return true;
20557     }
20558   /* None of that worked, so it must not really have a location;
20559      try adding a constant value attribute from the DECL_INITIAL.  */
20560   return tree_add_const_value_attribute_for_decl (die, decl);
20561 }
20562 
20563 /* Attach a DW_AT_const_value attribute to DIE. The value of the
20564    attribute is the const value T.  */
20565 
20566 static bool
tree_add_const_value_attribute(dw_die_ref die,tree t)20567 tree_add_const_value_attribute (dw_die_ref die, tree t)
20568 {
20569   tree init;
20570   tree type = TREE_TYPE (t);
20571   rtx rtl;
20572 
20573   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
20574     return false;
20575 
20576   init = t;
20577   gcc_assert (!DECL_P (init));
20578 
20579   if (TREE_CODE (init) == INTEGER_CST)
20580     {
20581       if (tree_fits_uhwi_p (init))
20582 	{
20583 	  add_AT_unsigned (die, DW_AT_const_value, tree_to_uhwi (init));
20584 	  return true;
20585 	}
20586       if (tree_fits_shwi_p (init))
20587 	{
20588 	  add_AT_int (die, DW_AT_const_value, tree_to_shwi (init));
20589 	  return true;
20590 	}
20591     }
20592   /* Generate the RTL even if early_dwarf to force mangling of all refered to
20593      symbols.  */
20594   rtl = rtl_for_decl_init (init, type);
20595   if (rtl && !early_dwarf)
20596     return add_const_value_attribute (die, rtl);
20597   /* If the host and target are sane, try harder.  */
20598   if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
20599       && initializer_constant_valid_p (init, type))
20600     {
20601       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
20602       if (size > 0 && (int) size == size)
20603 	{
20604 	  unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
20605 
20606 	  if (native_encode_initializer (init, array, size) == size)
20607 	    {
20608 	      add_AT_vec (die, DW_AT_const_value, size, 1, array);
20609 	      return true;
20610 	    }
20611 	  ggc_free (array);
20612 	}
20613     }
20614   return false;
20615 }
20616 
20617 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
20618    attribute is the const value of T, where T is an integral constant
20619    variable with static storage duration
20620    (so it can't be a PARM_DECL or a RESULT_DECL).  */
20621 
20622 static bool
tree_add_const_value_attribute_for_decl(dw_die_ref var_die,tree decl)20623 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
20624 {
20625 
20626   if (!decl
20627       || (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
20628       || (VAR_P (decl) && !TREE_STATIC (decl)))
20629     return false;
20630 
20631   if (TREE_READONLY (decl)
20632       && ! TREE_THIS_VOLATILE (decl)
20633       && DECL_INITIAL (decl))
20634     /* OK */;
20635   else
20636     return false;
20637 
20638   /* Don't add DW_AT_const_value if abstract origin already has one.  */
20639   if (get_AT (var_die, DW_AT_const_value))
20640     return false;
20641 
20642   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
20643 }
20644 
20645 /* Convert the CFI instructions for the current function into a
20646    location list.  This is used for DW_AT_frame_base when we targeting
20647    a dwarf2 consumer that does not support the dwarf3
20648    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
20649    expressions.  */
20650 
20651 static dw_loc_list_ref
convert_cfa_to_fb_loc_list(HOST_WIDE_INT offset)20652 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
20653 {
20654   int ix;
20655   dw_fde_ref fde;
20656   dw_loc_list_ref list, *list_tail;
20657   dw_cfi_ref cfi;
20658   dw_cfa_location last_cfa, next_cfa;
20659   const char *start_label, *last_label, *section;
20660   dw_cfa_location remember;
20661 
20662   fde = cfun->fde;
20663   gcc_assert (fde != NULL);
20664 
20665   section = secname_for_decl (current_function_decl);
20666   list_tail = &list;
20667   list = NULL;
20668 
20669   memset (&next_cfa, 0, sizeof (next_cfa));
20670   next_cfa.reg = INVALID_REGNUM;
20671   remember = next_cfa;
20672 
20673   start_label = fde->dw_fde_begin;
20674 
20675   /* ??? Bald assumption that the CIE opcode list does not contain
20676      advance opcodes.  */
20677   FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
20678     lookup_cfa_1 (cfi, &next_cfa, &remember);
20679 
20680   last_cfa = next_cfa;
20681   last_label = start_label;
20682 
20683   if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
20684     {
20685       /* If the first partition contained no CFI adjustments, the
20686 	 CIE opcodes apply to the whole first partition.  */
20687       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20688 				 fde->dw_fde_begin, 0, fde->dw_fde_end, 0, section);
20689       list_tail =&(*list_tail)->dw_loc_next;
20690       start_label = last_label = fde->dw_fde_second_begin;
20691     }
20692 
20693   FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
20694     {
20695       switch (cfi->dw_cfi_opc)
20696 	{
20697 	case DW_CFA_set_loc:
20698 	case DW_CFA_advance_loc1:
20699 	case DW_CFA_advance_loc2:
20700 	case DW_CFA_advance_loc4:
20701 	  if (!cfa_equal_p (&last_cfa, &next_cfa))
20702 	    {
20703 	      *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20704 					 start_label, 0, last_label, 0, section);
20705 
20706 	      list_tail = &(*list_tail)->dw_loc_next;
20707 	      last_cfa = next_cfa;
20708 	      start_label = last_label;
20709 	    }
20710 	  last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
20711 	  break;
20712 
20713 	case DW_CFA_advance_loc:
20714 	  /* The encoding is complex enough that we should never emit this.  */
20715 	  gcc_unreachable ();
20716 
20717 	default:
20718 	  lookup_cfa_1 (cfi, &next_cfa, &remember);
20719 	  break;
20720 	}
20721       if (ix + 1 == fde->dw_fde_switch_cfi_index)
20722 	{
20723 	  if (!cfa_equal_p (&last_cfa, &next_cfa))
20724 	    {
20725 	      *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20726 					 start_label, 0, last_label, 0, section);
20727 
20728 	      list_tail = &(*list_tail)->dw_loc_next;
20729 	      last_cfa = next_cfa;
20730 	      start_label = last_label;
20731 	    }
20732 	  *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20733 				     start_label, 0, fde->dw_fde_end, 0, section);
20734 	  list_tail = &(*list_tail)->dw_loc_next;
20735 	  start_label = last_label = fde->dw_fde_second_begin;
20736 	}
20737     }
20738 
20739   if (!cfa_equal_p (&last_cfa, &next_cfa))
20740     {
20741       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20742 				 start_label, 0, last_label, 0, section);
20743       list_tail = &(*list_tail)->dw_loc_next;
20744       start_label = last_label;
20745     }
20746 
20747   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
20748 			     start_label, 0,
20749 			     fde->dw_fde_second_begin
20750 			     ? fde->dw_fde_second_end : fde->dw_fde_end, 0,
20751 			     section);
20752 
20753   maybe_gen_llsym (list);
20754 
20755   return list;
20756 }
20757 
20758 /* Compute a displacement from the "steady-state frame pointer" to the
20759    frame base (often the same as the CFA), and store it in
20760    frame_pointer_fb_offset.  OFFSET is added to the displacement
20761    before the latter is negated.  */
20762 
20763 static void
compute_frame_pointer_to_fb_displacement(poly_int64 offset)20764 compute_frame_pointer_to_fb_displacement (poly_int64 offset)
20765 {
20766   rtx reg, elim;
20767 
20768 #ifdef FRAME_POINTER_CFA_OFFSET
20769   reg = frame_pointer_rtx;
20770   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
20771 #else
20772   reg = arg_pointer_rtx;
20773   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
20774 #endif
20775 
20776   elim = (ira_use_lra_p
20777 	  ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
20778 	  : eliminate_regs (reg, VOIDmode, NULL_RTX));
20779   elim = strip_offset_and_add (elim, &offset);
20780 
20781   frame_pointer_fb_offset = -offset;
20782 
20783   /* ??? AVR doesn't set up valid eliminations when there is no stack frame
20784      in which to eliminate.  This is because it's stack pointer isn't
20785      directly accessible as a register within the ISA.  To work around
20786      this, assume that while we cannot provide a proper value for
20787      frame_pointer_fb_offset, we won't need one either.  We can use
20788      hard frame pointer in debug info even if frame pointer isn't used
20789      since hard frame pointer in debug info is encoded with DW_OP_fbreg
20790      which uses the DW_AT_frame_base attribute, not hard frame pointer
20791      directly.  */
20792   frame_pointer_fb_offset_valid
20793     = (elim == hard_frame_pointer_rtx || elim == stack_pointer_rtx);
20794 }
20795 
20796 /* Generate a DW_AT_name attribute given some string value to be included as
20797    the value of the attribute.  */
20798 
20799 static void
add_name_attribute(dw_die_ref die,const char * name_string)20800 add_name_attribute (dw_die_ref die, const char *name_string)
20801 {
20802   if (name_string != NULL && *name_string != 0)
20803     {
20804       if (demangle_name_func)
20805 	name_string = (*demangle_name_func) (name_string);
20806 
20807       add_AT_string (die, DW_AT_name, name_string);
20808     }
20809 }
20810 
20811 /* Generate a DW_AT_name attribute given some string value representing a
20812    file or filepath to be included as value of the attribute.  */
20813 static void
add_filename_attribute(dw_die_ref die,const char * name_string)20814 add_filename_attribute (dw_die_ref die, const char *name_string)
20815 {
20816   if (name_string != NULL && *name_string != 0)
20817     add_filepath_AT_string (die, DW_AT_name, name_string);
20818 }
20819 
20820 /* Generate a DW_AT_description attribute given some string value to be included
20821    as the value of the attribute.  */
20822 
20823 static void
add_desc_attribute(dw_die_ref die,const char * name_string)20824 add_desc_attribute (dw_die_ref die, const char *name_string)
20825 {
20826   if (!flag_describe_dies || (dwarf_version < 3 && dwarf_strict))
20827     return;
20828 
20829   if (name_string == NULL || *name_string == 0)
20830     return;
20831 
20832   if (demangle_name_func)
20833     name_string = (*demangle_name_func) (name_string);
20834 
20835   add_AT_string (die, DW_AT_description, name_string);
20836 }
20837 
20838 /* Generate a DW_AT_description attribute given some decl to be included
20839    as the value of the attribute.  */
20840 
20841 static void
add_desc_attribute(dw_die_ref die,tree decl)20842 add_desc_attribute (dw_die_ref die, tree decl)
20843 {
20844   tree decl_name;
20845 
20846   if (!flag_describe_dies || (dwarf_version < 3 && dwarf_strict))
20847     return;
20848 
20849   if (decl == NULL_TREE || !DECL_P (decl))
20850     return;
20851   decl_name = DECL_NAME (decl);
20852 
20853   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
20854     {
20855       const char *name = dwarf2_name (decl, 0);
20856       add_desc_attribute (die, name ? name : IDENTIFIER_POINTER (decl_name));
20857     }
20858   else
20859     {
20860       char *desc = print_generic_expr_to_str (decl);
20861       add_desc_attribute (die, desc);
20862       free (desc);
20863     }
20864 }
20865 
20866 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
20867    DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
20868    of TYPE accordingly.
20869 
20870    ??? This is a temporary measure until after we're able to generate
20871    regular DWARF for the complex Ada type system.  */
20872 
20873 static void
add_gnat_descriptive_type_attribute(dw_die_ref die,tree type,dw_die_ref context_die)20874 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
20875 				     dw_die_ref context_die)
20876 {
20877   tree dtype;
20878   dw_die_ref dtype_die;
20879 
20880   if (!lang_hooks.types.descriptive_type)
20881     return;
20882 
20883   dtype = lang_hooks.types.descriptive_type (type);
20884   if (!dtype)
20885     return;
20886 
20887   dtype_die = lookup_type_die (dtype);
20888   if (!dtype_die)
20889     {
20890       gen_type_die (dtype, context_die);
20891       dtype_die = lookup_type_die (dtype);
20892       gcc_assert (dtype_die);
20893     }
20894 
20895   add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
20896 }
20897 
20898 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir.  */
20899 
20900 static const char *
comp_dir_string(void)20901 comp_dir_string (void)
20902 {
20903   const char *wd;
20904   char *wd_plus_sep = NULL;
20905   static const char *cached_wd = NULL;
20906 
20907   if (cached_wd != NULL)
20908     return cached_wd;
20909 
20910   wd = get_src_pwd ();
20911   if (wd == NULL)
20912     return NULL;
20913 
20914   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
20915     {
20916       size_t wdlen = strlen (wd);
20917       wd_plus_sep = XNEWVEC (char, wdlen + 2);
20918       strcpy (wd_plus_sep, wd);
20919       wd_plus_sep [wdlen] = DIR_SEPARATOR;
20920       wd_plus_sep [wdlen + 1] = 0;
20921       wd = wd_plus_sep;
20922     }
20923 
20924   cached_wd = remap_debug_filename (wd);
20925 
20926   /* remap_debug_filename can just pass through wd or return a new gc string.
20927      These two types can't be both stored in a GTY(())-tagged string, but since
20928      the cached value lives forever just copy it if needed.  */
20929   if (cached_wd != wd)
20930     {
20931       cached_wd = xstrdup (cached_wd);
20932       if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR && wd_plus_sep != NULL)
20933         free (wd_plus_sep);
20934     }
20935 
20936   return cached_wd;
20937 }
20938 
20939 /* Generate a DW_AT_comp_dir attribute for DIE.  */
20940 
20941 static void
add_comp_dir_attribute(dw_die_ref die)20942 add_comp_dir_attribute (dw_die_ref die)
20943 {
20944   const char * wd = comp_dir_string ();
20945   if (wd != NULL)
20946     add_filepath_AT_string (die, DW_AT_comp_dir, wd);
20947 }
20948 
20949 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
20950    pointer computation, ...), output a representation for that bound according
20951    to the accepted FORMS (see enum dw_scalar_form) and add it to DIE.  See
20952    loc_list_from_tree for the meaning of CONTEXT.  */
20953 
20954 static void
add_scalar_info(dw_die_ref die,enum dwarf_attribute attr,tree value,int forms,struct loc_descr_context * context)20955 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
20956 		 int forms, struct loc_descr_context *context)
20957 {
20958   dw_die_ref context_die, decl_die = NULL;
20959   dw_loc_list_ref list;
20960   bool strip_conversions = true;
20961   bool placeholder_seen = false;
20962 
20963   while (strip_conversions)
20964     switch (TREE_CODE (value))
20965       {
20966       case ERROR_MARK:
20967       case SAVE_EXPR:
20968 	return;
20969 
20970       CASE_CONVERT:
20971       case VIEW_CONVERT_EXPR:
20972 	value = TREE_OPERAND (value, 0);
20973 	break;
20974 
20975       default:
20976 	strip_conversions = false;
20977 	break;
20978       }
20979 
20980   /* If possible and permitted, output the attribute as a constant.  */
20981   if ((forms & dw_scalar_form_constant) != 0
20982       && TREE_CODE (value) == INTEGER_CST)
20983     {
20984       unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
20985 
20986       /* If HOST_WIDE_INT is big enough then represent the bound as
20987 	 a constant value.  We need to choose a form based on
20988 	 whether the type is signed or unsigned.  We cannot just
20989 	 call add_AT_unsigned if the value itself is positive
20990 	 (add_AT_unsigned might add the unsigned value encoded as
20991 	 DW_FORM_data[1248]).  Some DWARF consumers will lookup the
20992 	 bounds type and then sign extend any unsigned values found
20993 	 for signed types.  This is needed only for
20994 	 DW_AT_{lower,upper}_bound, since for most other attributes,
20995 	 consumers will treat DW_FORM_data[1248] as unsigned values,
20996 	 regardless of the underlying type.  */
20997       if (prec <= HOST_BITS_PER_WIDE_INT
20998 	  || tree_fits_uhwi_p (value))
20999 	{
21000 	  if (TYPE_UNSIGNED (TREE_TYPE (value)))
21001 	    add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
21002 	  else
21003 	    add_AT_int (die, attr, TREE_INT_CST_LOW (value));
21004 	}
21005       else if (dwarf_version >= 5
21006 	       && TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (value))) == 128)
21007 	/* Otherwise represent the bound as an unsigned value with
21008 	   the precision of its type.  The precision and signedness
21009 	   of the type will be necessary to re-interpret it
21010 	   unambiguously.  */
21011 	add_AT_wide (die, attr, wi::to_wide (value));
21012       else
21013 	{
21014 	  rtx v = immed_wide_int_const (wi::to_wide (value),
21015 					TYPE_MODE (TREE_TYPE (value)));
21016 	  dw_loc_descr_ref loc
21017 	    = loc_descriptor (v, TYPE_MODE (TREE_TYPE (value)),
21018 			      VAR_INIT_STATUS_INITIALIZED);
21019 	  if (loc)
21020 	    add_AT_loc (die, attr, loc);
21021 	}
21022       return;
21023     }
21024 
21025   /* Otherwise, if it's possible and permitted too, output a reference to
21026      another DIE.  */
21027   if ((forms & dw_scalar_form_reference) != 0)
21028     {
21029       tree decl = NULL_TREE;
21030 
21031       /* Some type attributes reference an outer type.  For instance, the upper
21032 	 bound of an array may reference an embedding record (this happens in
21033 	 Ada).  */
21034       if (TREE_CODE (value) == COMPONENT_REF
21035 	  && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
21036 	  && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
21037 	decl = TREE_OPERAND (value, 1);
21038 
21039       else if (VAR_P (value)
21040 	       || TREE_CODE (value) == PARM_DECL
21041 	       || TREE_CODE (value) == RESULT_DECL)
21042 	decl = value;
21043 
21044       if (decl != NULL_TREE)
21045 	{
21046 	  decl_die = lookup_decl_die (decl);
21047 
21048 	  /* ??? Can this happen, or should the variable have been bound
21049 	     first?  Probably it can, since I imagine that we try to create
21050 	     the types of parameters in the order in which they exist in
21051 	     the list, and won't have created a forward reference to a
21052 	     later parameter.  */
21053 	  if (decl_die != NULL)
21054 	    {
21055 	      if (get_AT (decl_die, DW_AT_location)
21056 		  || get_AT (decl_die, DW_AT_data_member_location)
21057 		  || get_AT (decl_die, DW_AT_const_value))
21058 		{
21059 		  add_AT_die_ref (die, attr, decl_die);
21060 		  return;
21061 		}
21062 	    }
21063 	}
21064     }
21065 
21066   /* Last chance: try to create a stack operation procedure to evaluate the
21067      value.  Do nothing if even that is not possible or permitted.  */
21068   if ((forms & dw_scalar_form_exprloc) == 0)
21069     return;
21070 
21071   list = loc_list_from_tree (value, 2, context);
21072   if (context && context->placeholder_arg)
21073     {
21074       placeholder_seen = context->placeholder_seen;
21075       context->placeholder_seen = false;
21076     }
21077   if (list == NULL || single_element_loc_list_p (list))
21078     {
21079       /* If this attribute is not a reference nor constant, it is
21080 	 a DWARF expression rather than location description.  For that
21081 	 loc_list_from_tree (value, 0, &context) is needed.  */
21082       dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
21083       if (list2 && single_element_loc_list_p (list2))
21084 	{
21085 	  if (placeholder_seen)
21086 	    {
21087 	      struct dwarf_procedure_info dpi;
21088 	      dpi.fndecl = NULL_TREE;
21089 	      dpi.args_count = 1;
21090 	      if (!resolve_args_picking (list2->expr, 1, &dpi))
21091 		return;
21092 	    }
21093 	  add_AT_loc (die, attr, list2->expr);
21094 	  return;
21095 	}
21096     }
21097 
21098   /* If that failed to give a single element location list, fall back to
21099      outputting this as a reference... still if permitted.  */
21100   if (list == NULL
21101       || (forms & dw_scalar_form_reference) == 0
21102       || placeholder_seen)
21103     return;
21104 
21105   if (!decl_die)
21106     {
21107       if (current_function_decl == 0)
21108 	context_die = comp_unit_die ();
21109       else
21110 	context_die = lookup_decl_die (current_function_decl);
21111 
21112       decl_die = new_die (DW_TAG_variable, context_die, value);
21113       add_AT_flag (decl_die, DW_AT_artificial, 1);
21114       add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
21115 			  context_die);
21116     }
21117 
21118   add_AT_location_description (decl_die, DW_AT_location, list);
21119   add_AT_die_ref (die, attr, decl_die);
21120 }
21121 
21122 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
21123    default.  */
21124 
21125 static int
lower_bound_default(void)21126 lower_bound_default (void)
21127 {
21128   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
21129     {
21130     case DW_LANG_C:
21131     case DW_LANG_C89:
21132     case DW_LANG_C99:
21133     case DW_LANG_C11:
21134     case DW_LANG_C_plus_plus:
21135     case DW_LANG_C_plus_plus_11:
21136     case DW_LANG_C_plus_plus_14:
21137     case DW_LANG_ObjC:
21138     case DW_LANG_ObjC_plus_plus:
21139       return 0;
21140     case DW_LANG_Fortran77:
21141     case DW_LANG_Fortran90:
21142     case DW_LANG_Fortran95:
21143     case DW_LANG_Fortran03:
21144     case DW_LANG_Fortran08:
21145       return 1;
21146     case DW_LANG_UPC:
21147     case DW_LANG_D:
21148     case DW_LANG_Python:
21149       return dwarf_version >= 4 ? 0 : -1;
21150     case DW_LANG_Ada95:
21151     case DW_LANG_Ada83:
21152     case DW_LANG_Cobol74:
21153     case DW_LANG_Cobol85:
21154     case DW_LANG_Modula2:
21155     case DW_LANG_PLI:
21156       return dwarf_version >= 4 ? 1 : -1;
21157     default:
21158       return -1;
21159     }
21160 }
21161 
21162 /* Given a tree node describing an array bound (either lower or upper) output
21163    a representation for that bound.  */
21164 
21165 static void
add_bound_info(dw_die_ref subrange_die,enum dwarf_attribute bound_attr,tree bound,struct loc_descr_context * context)21166 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
21167 		tree bound, struct loc_descr_context *context)
21168 {
21169   int dflt;
21170 
21171   while (1)
21172     switch (TREE_CODE (bound))
21173       {
21174       /* Strip all conversions.  */
21175       CASE_CONVERT:
21176       case VIEW_CONVERT_EXPR:
21177 	bound = TREE_OPERAND (bound, 0);
21178 	break;
21179 
21180       /* All fixed-bounds are represented by INTEGER_CST nodes.  Lower bounds
21181 	 are even omitted when they are the default.  */
21182       case INTEGER_CST:
21183 	/* If the value for this bound is the default one, we can even omit the
21184 	   attribute.  */
21185 	if (bound_attr == DW_AT_lower_bound
21186 	    && tree_fits_shwi_p (bound)
21187 	    && (dflt = lower_bound_default ()) != -1
21188 	    && tree_to_shwi (bound) == dflt)
21189 	  return;
21190 
21191 	/* FALLTHRU */
21192 
21193       default:
21194 	/* Because of the complex interaction there can be with other GNAT
21195 	   encodings, GDB isn't ready yet to handle proper DWARF description
21196 	   for self-referencial subrange bounds: let GNAT encodings do the
21197 	   magic in such a case.  */
21198 	if (is_ada ()
21199 	    && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL
21200 	    && contains_placeholder_p (bound))
21201 	  return;
21202 
21203 	add_scalar_info (subrange_die, bound_attr, bound,
21204 			 dw_scalar_form_constant
21205 			 | dw_scalar_form_exprloc
21206 			 | dw_scalar_form_reference,
21207 			 context);
21208 	return;
21209       }
21210 }
21211 
21212 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
21213    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
21214    Note that the block of subscript information for an array type also
21215    includes information about the element type of the given array type.
21216 
21217    This function reuses previously set type and bound information if
21218    available.  */
21219 
21220 static void
add_subscript_info(dw_die_ref type_die,tree type,bool collapse_p)21221 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
21222 {
21223   unsigned dimension_number;
21224   tree lower, upper;
21225   dw_die_ref child = type_die->die_child;
21226 
21227   for (dimension_number = 0;
21228        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
21229        type = TREE_TYPE (type), dimension_number++)
21230     {
21231       tree domain = TYPE_DOMAIN (type);
21232 
21233       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
21234 	break;
21235 
21236       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
21237 	 and (in GNU C only) variable bounds.  Handle all three forms
21238 	 here.  */
21239 
21240       /* Find and reuse a previously generated DW_TAG_subrange_type if
21241 	 available.
21242 
21243          For multi-dimensional arrays, as we iterate through the
21244          various dimensions in the enclosing for loop above, we also
21245          iterate through the DIE children and pick at each
21246          DW_TAG_subrange_type previously generated (if available).
21247          Each child DW_TAG_subrange_type DIE describes the range of
21248          the current dimension.  At this point we should have as many
21249          DW_TAG_subrange_type's as we have dimensions in the
21250          array.  */
21251       dw_die_ref subrange_die = NULL;
21252       if (child)
21253 	while (1)
21254 	  {
21255 	    child = child->die_sib;
21256 	    if (child->die_tag == DW_TAG_subrange_type)
21257 	      subrange_die = child;
21258 	    if (child == type_die->die_child)
21259 	      {
21260 		/* If we wrapped around, stop looking next time.  */
21261 		child = NULL;
21262 		break;
21263 	      }
21264 	    if (child->die_tag == DW_TAG_subrange_type)
21265 	      break;
21266 	  }
21267       if (!subrange_die)
21268 	subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
21269 
21270       if (domain)
21271 	{
21272 	  /* We have an array type with specified bounds.  */
21273 	  lower = TYPE_MIN_VALUE (domain);
21274 	  upper = TYPE_MAX_VALUE (domain);
21275 
21276 	  /* Define the index type.  */
21277 	  if (TREE_TYPE (domain)
21278 	      && !get_AT (subrange_die, DW_AT_type))
21279 	    {
21280 	      /* ??? This is probably an Ada unnamed subrange type.  Ignore the
21281 		 TREE_TYPE field.  We can't emit debug info for this
21282 		 because it is an unnamed integral type.  */
21283 	      if (TREE_CODE (domain) == INTEGER_TYPE
21284 		  && TYPE_NAME (domain) == NULL_TREE
21285 		  && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
21286 		  && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
21287 		;
21288 	      else
21289 		add_type_attribute (subrange_die, TREE_TYPE (domain),
21290 				    TYPE_UNQUALIFIED, false, type_die);
21291 	    }
21292 
21293 	  /* ??? If upper is NULL, the array has unspecified length,
21294 	     but it does have a lower bound.  This happens with Fortran
21295 	       dimension arr(N:*)
21296 	     Since the debugger is definitely going to need to know N
21297 	     to produce useful results, go ahead and output the lower
21298 	     bound solo, and hope the debugger can cope.  */
21299 
21300 	  if (!get_AT (subrange_die, DW_AT_lower_bound))
21301 	    add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
21302 	  if (!get_AT (subrange_die, DW_AT_upper_bound)
21303 	      && !get_AT (subrange_die, DW_AT_count))
21304 	    {
21305 	      if (upper)
21306 		add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
21307 	      else if ((is_c () || is_cxx ()) && COMPLETE_TYPE_P (type))
21308 		/* Zero-length array.  */
21309 		add_bound_info (subrange_die, DW_AT_count,
21310 				build_int_cst (TREE_TYPE (lower), 0), NULL);
21311 	    }
21312 	}
21313 
21314       /* Otherwise we have an array type with an unspecified length.  The
21315 	 DWARF-2 spec does not say how to handle this; let's just leave out the
21316 	 bounds.  */
21317     }
21318 }
21319 
21320 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size.  */
21321 
21322 static void
add_byte_size_attribute(dw_die_ref die,tree tree_node)21323 add_byte_size_attribute (dw_die_ref die, tree tree_node)
21324 {
21325   dw_die_ref decl_die;
21326   HOST_WIDE_INT size;
21327   dw_loc_descr_ref size_expr = NULL;
21328 
21329   switch (TREE_CODE (tree_node))
21330     {
21331     case ERROR_MARK:
21332       size = 0;
21333       break;
21334     case ENUMERAL_TYPE:
21335     case RECORD_TYPE:
21336     case UNION_TYPE:
21337     case QUAL_UNION_TYPE:
21338       if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
21339 	  && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
21340 	{
21341 	  add_AT_die_ref (die, DW_AT_byte_size, decl_die);
21342 	  return;
21343 	}
21344       size_expr = type_byte_size (tree_node, &size);
21345       break;
21346     case FIELD_DECL:
21347       /* For a data member of a struct or union, the DW_AT_byte_size is
21348 	 generally given as the number of bytes normally allocated for an
21349 	 object of the *declared* type of the member itself.  This is true
21350 	 even for bit-fields.  */
21351       size = int_size_in_bytes (field_type (tree_node));
21352       break;
21353     default:
21354       gcc_unreachable ();
21355     }
21356 
21357   /* Support for dynamically-sized objects was introduced by DWARFv3.
21358      At the moment, GDB does not handle variable byte sizes very well,
21359      though.  */
21360   if ((dwarf_version >= 3 || !dwarf_strict)
21361       && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL
21362       && size_expr != NULL)
21363     add_AT_loc (die, DW_AT_byte_size, size_expr);
21364 
21365   /* Note that `size' might be -1 when we get to this point.  If it is, that
21366      indicates that the byte size of the entity in question is variable and
21367      that we could not generate a DWARF expression that computes it.  */
21368   if (size >= 0)
21369     add_AT_unsigned (die, DW_AT_byte_size, size);
21370 }
21371 
21372 /* Add a DW_AT_alignment attribute to DIE with TREE_NODE's non-default
21373    alignment.  */
21374 
21375 static void
add_alignment_attribute(dw_die_ref die,tree tree_node)21376 add_alignment_attribute (dw_die_ref die, tree tree_node)
21377 {
21378   if (dwarf_version < 5 && dwarf_strict)
21379     return;
21380 
21381   unsigned align;
21382 
21383   if (DECL_P (tree_node))
21384     {
21385       if (!DECL_USER_ALIGN (tree_node))
21386 	return;
21387 
21388       align = DECL_ALIGN_UNIT (tree_node);
21389     }
21390   else if (TYPE_P (tree_node))
21391     {
21392       if (!TYPE_USER_ALIGN (tree_node))
21393 	return;
21394 
21395       align = TYPE_ALIGN_UNIT (tree_node);
21396     }
21397   else
21398     gcc_unreachable ();
21399 
21400   add_AT_unsigned (die, DW_AT_alignment, align);
21401 }
21402 
21403 /* For a FIELD_DECL node which represents a bit-field, output an attribute
21404    which specifies the distance in bits from the highest order bit of the
21405    "containing object" for the bit-field to the highest order bit of the
21406    bit-field itself.
21407 
21408    For any given bit-field, the "containing object" is a hypothetical object
21409    (of some integral or enum type) within which the given bit-field lives.  The
21410    type of this hypothetical "containing object" is always the same as the
21411    declared type of the individual bit-field itself.  The determination of the
21412    exact location of the "containing object" for a bit-field is rather
21413    complicated.  It's handled by the `field_byte_offset' function (above).
21414 
21415    Note that it is the size (in bytes) of the hypothetical "containing object"
21416    which will be given in the DW_AT_byte_size attribute for this bit-field.
21417    (See `byte_size_attribute' above).  */
21418 
21419 static inline void
add_bit_offset_attribute(dw_die_ref die,tree decl)21420 add_bit_offset_attribute (dw_die_ref die, tree decl)
21421 {
21422   HOST_WIDE_INT object_offset_in_bytes;
21423   tree original_type = DECL_BIT_FIELD_TYPE (decl);
21424   HOST_WIDE_INT bitpos_int;
21425   HOST_WIDE_INT highest_order_object_bit_offset;
21426   HOST_WIDE_INT highest_order_field_bit_offset;
21427   HOST_WIDE_INT bit_offset;
21428 
21429   /* The containing object is within the DECL_CONTEXT.  */
21430   struct vlr_context ctx = { DECL_CONTEXT (decl), NULL_TREE };
21431 
21432   field_byte_offset (decl, &ctx, &object_offset_in_bytes);
21433 
21434   /* Must be a field and a bit field.  */
21435   gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
21436 
21437   /* We can't yet handle bit-fields whose offsets are variable, so if we
21438      encounter such things, just return without generating any attribute
21439      whatsoever.  Likewise for variable or too large size.  */
21440   if (! tree_fits_shwi_p (bit_position (decl))
21441       || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
21442     return;
21443 
21444   bitpos_int = int_bit_position (decl);
21445 
21446   /* Note that the bit offset is always the distance (in bits) from the
21447      highest-order bit of the "containing object" to the highest-order bit of
21448      the bit-field itself.  Since the "high-order end" of any object or field
21449      is different on big-endian and little-endian machines, the computation
21450      below must take account of these differences.  */
21451   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
21452   highest_order_field_bit_offset = bitpos_int;
21453 
21454   if (! BYTES_BIG_ENDIAN)
21455     {
21456       highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
21457       highest_order_object_bit_offset +=
21458         simple_type_size_in_bits (original_type);
21459     }
21460 
21461   bit_offset
21462     = (! BYTES_BIG_ENDIAN
21463        ? highest_order_object_bit_offset - highest_order_field_bit_offset
21464        : highest_order_field_bit_offset - highest_order_object_bit_offset);
21465 
21466   if (bit_offset < 0)
21467     add_AT_int (die, DW_AT_bit_offset, bit_offset);
21468   else
21469     add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
21470 }
21471 
21472 /* For a FIELD_DECL node which represents a bit field, output an attribute
21473    which specifies the length in bits of the given field.  */
21474 
21475 static inline void
add_bit_size_attribute(dw_die_ref die,tree decl)21476 add_bit_size_attribute (dw_die_ref die, tree decl)
21477 {
21478   /* Must be a field and a bit field.  */
21479   gcc_assert (TREE_CODE (decl) == FIELD_DECL
21480 	      && DECL_BIT_FIELD_TYPE (decl));
21481 
21482   if (tree_fits_uhwi_p (DECL_SIZE (decl)))
21483     add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
21484 }
21485 
21486 /* If the compiled language is ANSI C, then add a 'prototyped'
21487    attribute, if arg types are given for the parameters of a function.  */
21488 
21489 static inline void
add_prototyped_attribute(dw_die_ref die,tree func_type)21490 add_prototyped_attribute (dw_die_ref die, tree func_type)
21491 {
21492   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
21493     {
21494     case DW_LANG_C:
21495     case DW_LANG_C89:
21496     case DW_LANG_C99:
21497     case DW_LANG_C11:
21498     case DW_LANG_ObjC:
21499       if (prototype_p (func_type))
21500 	add_AT_flag (die, DW_AT_prototyped, 1);
21501       break;
21502     default:
21503       break;
21504     }
21505 }
21506 
21507 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
21508    by looking in the type declaration, the object declaration equate table or
21509    the block mapping.  */
21510 
21511 static inline void
add_abstract_origin_attribute(dw_die_ref die,tree origin)21512 add_abstract_origin_attribute (dw_die_ref die, tree origin)
21513 {
21514   dw_die_ref origin_die = NULL;
21515 
21516   /* For late LTO debug output we want to refer directly to the abstract
21517      DIE in the early debug rather to the possibly existing concrete
21518      instance and avoid creating that just for this purpose.  */
21519   sym_off_pair *desc;
21520   if (in_lto_p
21521       && external_die_map
21522       && (desc = external_die_map->get (origin)))
21523     {
21524       add_AT_external_die_ref (die, DW_AT_abstract_origin,
21525 			       desc->sym, desc->off);
21526       return;
21527     }
21528 
21529   if (DECL_P (origin))
21530     origin_die = lookup_decl_die (origin);
21531   else if (TYPE_P (origin))
21532     origin_die = lookup_type_die (origin);
21533   else if (TREE_CODE (origin) == BLOCK)
21534     origin_die = lookup_block_die (origin);
21535 
21536   /* XXX: Functions that are never lowered don't always have correct block
21537      trees (in the case of java, they simply have no block tree, in some other
21538      languages).  For these functions, there is nothing we can really do to
21539      output correct debug info for inlined functions in all cases.  Rather
21540      than die, we'll just produce deficient debug info now, in that we will
21541      have variables without a proper abstract origin.  In the future, when all
21542      functions are lowered, we should re-add a gcc_assert (origin_die)
21543      here.  */
21544 
21545   if (origin_die)
21546     {
21547       dw_attr_node *a;
21548       /* Like above, if we already created a concrete instance DIE
21549 	 do not use that for the abstract origin but the early DIE
21550 	 if present.  */
21551       if (in_lto_p
21552 	  && (a = get_AT (origin_die, DW_AT_abstract_origin)))
21553 	origin_die = AT_ref (a);
21554       add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
21555     }
21556 }
21557 
21558 /* We do not currently support the pure_virtual attribute.  */
21559 
21560 static inline void
add_pure_or_virtual_attribute(dw_die_ref die,tree func_decl)21561 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
21562 {
21563   if (DECL_VINDEX (func_decl))
21564     {
21565       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
21566 
21567       if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
21568 	add_AT_loc (die, DW_AT_vtable_elem_location,
21569 		    new_loc_descr (DW_OP_constu,
21570 				   tree_to_shwi (DECL_VINDEX (func_decl)),
21571 				   0));
21572 
21573       /* GNU extension: Record what type this method came from originally.  */
21574       if (debug_info_level > DINFO_LEVEL_TERSE
21575 	  && DECL_CONTEXT (func_decl))
21576 	add_AT_die_ref (die, DW_AT_containing_type,
21577 			lookup_type_die (DECL_CONTEXT (func_decl)));
21578     }
21579 }
21580 
21581 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
21582    given decl.  This used to be a vendor extension until after DWARF 4
21583    standardized it.  */
21584 
21585 static void
add_linkage_attr(dw_die_ref die,tree decl)21586 add_linkage_attr (dw_die_ref die, tree decl)
21587 {
21588   const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
21589 
21590   /* Mimic what assemble_name_raw does with a leading '*'.  */
21591   if (name[0] == '*')
21592     name = &name[1];
21593 
21594   if (dwarf_version >= 4)
21595     add_AT_string (die, DW_AT_linkage_name, name);
21596   else
21597     add_AT_string (die, DW_AT_MIPS_linkage_name, name);
21598 }
21599 
21600 /* Add source coordinate attributes for the given decl.  */
21601 
21602 static void
add_src_coords_attributes(dw_die_ref die,tree decl)21603 add_src_coords_attributes (dw_die_ref die, tree decl)
21604 {
21605   expanded_location s;
21606 
21607   if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
21608     return;
21609   s = expand_location (DECL_SOURCE_LOCATION (decl));
21610   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
21611   add_AT_unsigned (die, DW_AT_decl_line, s.line);
21612   if (debug_column_info && s.column)
21613     add_AT_unsigned (die, DW_AT_decl_column, s.column);
21614 }
21615 
21616 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl.  */
21617 
21618 static void
add_linkage_name_raw(dw_die_ref die,tree decl)21619 add_linkage_name_raw (dw_die_ref die, tree decl)
21620 {
21621   /* Defer until we have an assembler name set.  */
21622   if (!DECL_ASSEMBLER_NAME_SET_P (decl))
21623     {
21624       limbo_die_node *asm_name;
21625 
21626       asm_name = ggc_cleared_alloc<limbo_die_node> ();
21627       asm_name->die = die;
21628       asm_name->created_for = decl;
21629       asm_name->next = deferred_asm_name;
21630       deferred_asm_name = asm_name;
21631     }
21632   else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
21633     add_linkage_attr (die, decl);
21634 }
21635 
21636 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired.  */
21637 
21638 static void
add_linkage_name(dw_die_ref die,tree decl)21639 add_linkage_name (dw_die_ref die, tree decl)
21640 {
21641   if (debug_info_level > DINFO_LEVEL_NONE
21642       && VAR_OR_FUNCTION_DECL_P (decl)
21643       && TREE_PUBLIC (decl)
21644       && !(VAR_P (decl) && DECL_REGISTER (decl))
21645       && die->die_tag != DW_TAG_member)
21646     add_linkage_name_raw (die, decl);
21647 }
21648 
21649 /* Add a DW_AT_name attribute and source coordinate attribute for the
21650    given decl, but only if it actually has a name.  */
21651 
21652 static void
add_name_and_src_coords_attributes(dw_die_ref die,tree decl,bool no_linkage_name)21653 add_name_and_src_coords_attributes (dw_die_ref die, tree decl,
21654 				    bool no_linkage_name)
21655 {
21656   tree decl_name;
21657 
21658   decl_name = DECL_NAME (decl);
21659   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
21660     {
21661       const char *name = dwarf2_name (decl, 0);
21662       if (name)
21663 	add_name_attribute (die, name);
21664       else
21665 	add_desc_attribute (die, decl);
21666 
21667       if (! DECL_ARTIFICIAL (decl))
21668 	add_src_coords_attributes (die, decl);
21669 
21670       if (!no_linkage_name)
21671 	add_linkage_name (die, decl);
21672     }
21673   else
21674     add_desc_attribute (die, decl);
21675 
21676 #ifdef VMS_DEBUGGING_INFO
21677   /* Get the function's name, as described by its RTL.  This may be different
21678      from the DECL_NAME name used in the source file.  */
21679   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
21680     {
21681       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
21682                   XEXP (DECL_RTL (decl), 0), false);
21683       vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
21684     }
21685 #endif /* VMS_DEBUGGING_INFO */
21686 }
21687 
21688 /* Add VALUE as a DW_AT_discr_value attribute to DIE.  */
21689 
21690 static void
add_discr_value(dw_die_ref die,dw_discr_value * value)21691 add_discr_value (dw_die_ref die, dw_discr_value *value)
21692 {
21693   dw_attr_node attr;
21694 
21695   attr.dw_attr = DW_AT_discr_value;
21696   attr.dw_attr_val.val_class = dw_val_class_discr_value;
21697   attr.dw_attr_val.val_entry = NULL;
21698   attr.dw_attr_val.v.val_discr_value.pos = value->pos;
21699   if (value->pos)
21700     attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
21701   else
21702     attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
21703   add_dwarf_attr (die, &attr);
21704 }
21705 
21706 /* Add DISCR_LIST as a DW_AT_discr_list to DIE.  */
21707 
21708 static void
add_discr_list(dw_die_ref die,dw_discr_list_ref discr_list)21709 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
21710 {
21711   dw_attr_node attr;
21712 
21713   attr.dw_attr = DW_AT_discr_list;
21714   attr.dw_attr_val.val_class = dw_val_class_discr_list;
21715   attr.dw_attr_val.val_entry = NULL;
21716   attr.dw_attr_val.v.val_discr_list = discr_list;
21717   add_dwarf_attr (die, &attr);
21718 }
21719 
21720 static inline dw_discr_list_ref
AT_discr_list(dw_attr_node * attr)21721 AT_discr_list (dw_attr_node *attr)
21722 {
21723   return attr->dw_attr_val.v.val_discr_list;
21724 }
21725 
21726 #ifdef VMS_DEBUGGING_INFO
21727 /* Output the debug main pointer die for VMS */
21728 
21729 void
dwarf2out_vms_debug_main_pointer(void)21730 dwarf2out_vms_debug_main_pointer (void)
21731 {
21732   char label[MAX_ARTIFICIAL_LABEL_BYTES];
21733   dw_die_ref die;
21734 
21735   /* Allocate the VMS debug main subprogram die.  */
21736   die = new_die_raw (DW_TAG_subprogram);
21737   add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
21738   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
21739 			       current_function_funcdef_no);
21740   add_AT_lbl_id (die, DW_AT_entry_pc, label);
21741 
21742   /* Make it the first child of comp_unit_die ().  */
21743   die->die_parent = comp_unit_die ();
21744   if (comp_unit_die ()->die_child)
21745     {
21746       die->die_sib = comp_unit_die ()->die_child->die_sib;
21747       comp_unit_die ()->die_child->die_sib = die;
21748     }
21749   else
21750     {
21751       die->die_sib = die;
21752       comp_unit_die ()->die_child = die;
21753     }
21754 }
21755 #endif /* VMS_DEBUGGING_INFO */
21756 
21757 /* walk_tree helper function for uses_local_type, below.  */
21758 
21759 static tree
uses_local_type_r(tree * tp,int * walk_subtrees,void * data ATTRIBUTE_UNUSED)21760 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
21761 {
21762   if (!TYPE_P (*tp))
21763     *walk_subtrees = 0;
21764   else
21765     {
21766       tree name = TYPE_NAME (*tp);
21767       if (name && DECL_P (name) && decl_function_context (name))
21768 	return *tp;
21769     }
21770   return NULL_TREE;
21771 }
21772 
21773 /* If TYPE involves a function-local type (including a local typedef to a
21774    non-local type), returns that type; otherwise returns NULL_TREE.  */
21775 
21776 static tree
uses_local_type(tree type)21777 uses_local_type (tree type)
21778 {
21779   tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
21780   return used;
21781 }
21782 
21783 /* Return the DIE for the scope that immediately contains this type.
21784    Non-named types that do not involve a function-local type get global
21785    scope.  Named types nested in namespaces or other types get their
21786    containing scope.  All other types (i.e. function-local named types) get
21787    the current active scope.  */
21788 
21789 static dw_die_ref
scope_die_for(tree t,dw_die_ref context_die)21790 scope_die_for (tree t, dw_die_ref context_die)
21791 {
21792   dw_die_ref scope_die = NULL;
21793   tree containing_scope;
21794 
21795   /* Non-types always go in the current scope.  */
21796   gcc_assert (TYPE_P (t));
21797 
21798   /* Use the scope of the typedef, rather than the scope of the type
21799      it refers to.  */
21800   if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
21801     containing_scope = DECL_CONTEXT (TYPE_NAME (t));
21802   else
21803     containing_scope = TYPE_CONTEXT (t);
21804 
21805   /* Use the containing namespace if there is one.  */
21806   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
21807     {
21808       if (context_die == lookup_decl_die (containing_scope))
21809 	/* OK */;
21810       else if (debug_info_level > DINFO_LEVEL_TERSE)
21811 	context_die = get_context_die (containing_scope);
21812       else
21813 	containing_scope = NULL_TREE;
21814     }
21815 
21816   /* Ignore function type "scopes" from the C frontend.  They mean that
21817      a tagged type is local to a parmlist of a function declarator, but
21818      that isn't useful to DWARF.  */
21819   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
21820     containing_scope = NULL_TREE;
21821 
21822   if (SCOPE_FILE_SCOPE_P (containing_scope))
21823     {
21824       /* If T uses a local type keep it local as well, to avoid references
21825 	 to function-local DIEs from outside the function.  */
21826       if (current_function_decl && uses_local_type (t))
21827 	scope_die = context_die;
21828       else
21829 	scope_die = comp_unit_die ();
21830     }
21831   else if (TYPE_P (containing_scope))
21832     {
21833       /* For types, we can just look up the appropriate DIE.  */
21834       if (debug_info_level > DINFO_LEVEL_TERSE)
21835 	scope_die = get_context_die (containing_scope);
21836       else
21837 	{
21838 	  scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
21839 	  if (scope_die == NULL)
21840 	    scope_die = comp_unit_die ();
21841 	}
21842     }
21843   else
21844     scope_die = context_die;
21845 
21846   return scope_die;
21847 }
21848 
21849 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
21850 
21851 static inline int
local_scope_p(dw_die_ref context_die)21852 local_scope_p (dw_die_ref context_die)
21853 {
21854   for (; context_die; context_die = context_die->die_parent)
21855     if (context_die->die_tag == DW_TAG_inlined_subroutine
21856 	|| context_die->die_tag == DW_TAG_subprogram)
21857       return 1;
21858 
21859   return 0;
21860 }
21861 
21862 /* Returns nonzero if CONTEXT_DIE is a class.  */
21863 
21864 static inline int
class_scope_p(dw_die_ref context_die)21865 class_scope_p (dw_die_ref context_die)
21866 {
21867   return (context_die
21868 	  && (context_die->die_tag == DW_TAG_structure_type
21869 	      || context_die->die_tag == DW_TAG_class_type
21870 	      || context_die->die_tag == DW_TAG_interface_type
21871 	      || context_die->die_tag == DW_TAG_union_type));
21872 }
21873 
21874 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
21875    whether or not to treat a DIE in this context as a declaration.  */
21876 
21877 static inline int
class_or_namespace_scope_p(dw_die_ref context_die)21878 class_or_namespace_scope_p (dw_die_ref context_die)
21879 {
21880   return (class_scope_p (context_die)
21881 	  || (context_die && context_die->die_tag == DW_TAG_namespace));
21882 }
21883 
21884 /* Many forms of DIEs require a "type description" attribute.  This
21885    routine locates the proper "type descriptor" die for the type given
21886    by 'type' plus any additional qualifiers given by 'cv_quals', and
21887    adds a DW_AT_type attribute below the given die.  */
21888 
21889 static void
add_type_attribute(dw_die_ref object_die,tree type,int cv_quals,bool reverse,dw_die_ref context_die)21890 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
21891 		    bool reverse, dw_die_ref context_die)
21892 {
21893   enum tree_code code  = TREE_CODE (type);
21894   dw_die_ref type_die  = NULL;
21895 
21896   if (debug_info_level <= DINFO_LEVEL_TERSE)
21897     return;
21898 
21899   /* ??? If this type is an unnamed subrange type of an integral, floating-point
21900      or fixed-point type, use the inner type.  This is because we have no
21901      support for unnamed types in base_type_die.  This can happen if this is
21902      an Ada subrange type.  Correct solution is emit a subrange type die.  */
21903   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
21904       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
21905     type = TREE_TYPE (type), code = TREE_CODE (type);
21906 
21907   if (code == ERROR_MARK
21908       /* Handle a special case.  For functions whose return type is void, we
21909 	 generate *no* type attribute.  (Note that no object may have type
21910 	 `void', so this only applies to function return types).  */
21911       || code == VOID_TYPE)
21912     return;
21913 
21914   type_die = modified_type_die (type,
21915 				cv_quals | TYPE_QUALS (type),
21916 				reverse,
21917 				context_die);
21918 
21919   if (type_die != NULL)
21920     add_AT_die_ref (object_die, DW_AT_type, type_die);
21921 }
21922 
21923 /* Given an object die, add the calling convention attribute for the
21924    function call type.  */
21925 static void
add_calling_convention_attribute(dw_die_ref subr_die,tree decl)21926 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
21927 {
21928   enum dwarf_calling_convention value = DW_CC_normal;
21929 
21930   value = ((enum dwarf_calling_convention)
21931 	   targetm.dwarf_calling_convention (TREE_TYPE (decl)));
21932 
21933   if (is_fortran ()
21934       && id_equal (DECL_ASSEMBLER_NAME (decl), "MAIN__"))
21935     {
21936       /* DWARF 2 doesn't provide a way to identify a program's source-level
21937 	entry point.  DW_AT_calling_convention attributes are only meant
21938 	to describe functions' calling conventions.  However, lacking a
21939 	better way to signal the Fortran main program, we used this for
21940 	a long time, following existing custom.  Now, DWARF 4 has
21941 	DW_AT_main_subprogram, which we add below, but some tools still
21942 	rely on the old way, which we thus keep.  */
21943       value = DW_CC_program;
21944 
21945       if (dwarf_version >= 4 || !dwarf_strict)
21946 	add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
21947     }
21948 
21949   /* Only add the attribute if the backend requests it, and
21950      is not DW_CC_normal.  */
21951   if (value && (value != DW_CC_normal))
21952     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
21953 }
21954 
21955 /* Given a tree pointer to a struct, class, union, or enum type node, return
21956    a pointer to the (string) tag name for the given type, or zero if the type
21957    was declared without a tag.  */
21958 
21959 static const char *
type_tag(const_tree type)21960 type_tag (const_tree type)
21961 {
21962   const char *name = 0;
21963 
21964   if (TYPE_NAME (type) != 0)
21965     {
21966       tree t = 0;
21967 
21968       /* Find the IDENTIFIER_NODE for the type name.  */
21969       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
21970 	  && !TYPE_NAMELESS (type))
21971 	t = TYPE_NAME (type);
21972 
21973       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
21974 	 a TYPE_DECL node, regardless of whether or not a `typedef' was
21975 	 involved.  */
21976       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
21977 	       && ! DECL_IGNORED_P (TYPE_NAME (type)))
21978 	{
21979 	  /* We want to be extra verbose.  Don't call dwarf_name if
21980 	     DECL_NAME isn't set.  The default hook for decl_printable_name
21981 	     doesn't like that, and in this context it's correct to return
21982 	     0, instead of "<anonymous>" or the like.  */
21983 	  if (DECL_NAME (TYPE_NAME (type))
21984 	      && !DECL_NAMELESS (TYPE_NAME (type)))
21985 	    name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
21986 	}
21987 
21988       /* Now get the name as a string, or invent one.  */
21989       if (!name && t != 0)
21990 	name = IDENTIFIER_POINTER (t);
21991     }
21992 
21993   return (name == 0 || *name == '\0') ? 0 : name;
21994 }
21995 
21996 /* Return the type associated with a data member, make a special check
21997    for bit field types.  */
21998 
21999 static inline tree
member_declared_type(const_tree member)22000 member_declared_type (const_tree member)
22001 {
22002   return (DECL_BIT_FIELD_TYPE (member)
22003 	  ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
22004 }
22005 
22006 /* Get the decl's label, as described by its RTL. This may be different
22007    from the DECL_NAME name used in the source file.  */
22008 
22009 #if 0
22010 static const char *
22011 decl_start_label (tree decl)
22012 {
22013   rtx x;
22014   const char *fnname;
22015 
22016   x = DECL_RTL (decl);
22017   gcc_assert (MEM_P (x));
22018 
22019   x = XEXP (x, 0);
22020   gcc_assert (GET_CODE (x) == SYMBOL_REF);
22021 
22022   fnname = XSTR (x, 0);
22023   return fnname;
22024 }
22025 #endif
22026 
22027 /* For variable-length arrays that have been previously generated, but
22028    may be incomplete due to missing subscript info, fill the subscript
22029    info.  Return TRUE if this is one of those cases.  */
22030 static bool
fill_variable_array_bounds(tree type)22031 fill_variable_array_bounds (tree type)
22032 {
22033   if (TREE_ASM_WRITTEN (type)
22034       && TREE_CODE (type) == ARRAY_TYPE
22035       && variably_modified_type_p (type, NULL))
22036     {
22037       dw_die_ref array_die = lookup_type_die (type);
22038       if (!array_die)
22039 	return false;
22040       add_subscript_info (array_die, type, !is_ada ());
22041       return true;
22042     }
22043   return false;
22044 }
22045 
22046 /* These routines generate the internal representation of the DIE's for
22047    the compilation unit.  Debugging information is collected by walking
22048    the declaration trees passed in from dwarf2out_decl().  */
22049 
22050 static void
gen_array_type_die(tree type,dw_die_ref context_die)22051 gen_array_type_die (tree type, dw_die_ref context_die)
22052 {
22053   dw_die_ref array_die;
22054 
22055   /* GNU compilers represent multidimensional array types as sequences of one
22056      dimensional array types whose element types are themselves array types.
22057      We sometimes squish that down to a single array_type DIE with multiple
22058      subscripts in the Dwarf debugging info.  The draft Dwarf specification
22059      say that we are allowed to do this kind of compression in C, because
22060      there is no difference between an array of arrays and a multidimensional
22061      array.  We don't do this for Ada to remain as close as possible to the
22062      actual representation, which is especially important against the language
22063      flexibilty wrt arrays of variable size.  */
22064 
22065   bool collapse_nested_arrays = !is_ada ();
22066 
22067   if (fill_variable_array_bounds (type))
22068     return;
22069 
22070   dw_die_ref scope_die = scope_die_for (type, context_die);
22071   tree element_type;
22072 
22073   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
22074      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
22075   if (TREE_CODE (type) == ARRAY_TYPE
22076       && TYPE_STRING_FLAG (type)
22077       && is_fortran ()
22078       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
22079     {
22080       HOST_WIDE_INT size;
22081 
22082       array_die = new_die (DW_TAG_string_type, scope_die, type);
22083       add_name_attribute (array_die, type_tag (type));
22084       equate_type_number_to_die (type, array_die);
22085       size = int_size_in_bytes (type);
22086       if (size >= 0)
22087 	add_AT_unsigned (array_die, DW_AT_byte_size, size);
22088       /* ???  We can't annotate types late, but for LTO we may not
22089 	 generate a location early either (gfortran.dg/save_6.f90).  */
22090       else if (! (early_dwarf && (flag_generate_lto || flag_generate_offload))
22091 	       && TYPE_DOMAIN (type) != NULL_TREE
22092 	       && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
22093 	{
22094 	  tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
22095 	  tree rszdecl = szdecl;
22096 
22097 	  size = int_size_in_bytes (TREE_TYPE (szdecl));
22098 	  if (!DECL_P (szdecl))
22099 	    {
22100 	      if (TREE_CODE (szdecl) == INDIRECT_REF
22101 		  && DECL_P (TREE_OPERAND (szdecl, 0)))
22102 		{
22103 		  rszdecl = TREE_OPERAND (szdecl, 0);
22104 		  if (int_size_in_bytes (TREE_TYPE (rszdecl))
22105 		      != DWARF2_ADDR_SIZE)
22106 		    size = 0;
22107 		}
22108 	      else
22109 		size = 0;
22110 	    }
22111 	  if (size > 0)
22112 	    {
22113 	      dw_loc_list_ref loc
22114 		= loc_list_from_tree (rszdecl, szdecl == rszdecl ? 2 : 0,
22115 				      NULL);
22116 	      if (loc)
22117 		{
22118 		  add_AT_location_description (array_die, DW_AT_string_length,
22119 					       loc);
22120 		  if (size != DWARF2_ADDR_SIZE)
22121 		    add_AT_unsigned (array_die, dwarf_version >= 5
22122 						? DW_AT_string_length_byte_size
22123 						: DW_AT_byte_size, size);
22124 		}
22125 	    }
22126 	}
22127       return;
22128     }
22129 
22130   array_die = new_die (DW_TAG_array_type, scope_die, type);
22131   add_name_attribute (array_die, type_tag (type));
22132   equate_type_number_to_die (type, array_die);
22133 
22134   if (TREE_CODE (type) == VECTOR_TYPE)
22135     add_AT_flag (array_die, DW_AT_GNU_vector, 1);
22136 
22137   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
22138   if (is_fortran ()
22139       && TREE_CODE (type) == ARRAY_TYPE
22140       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
22141       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
22142     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
22143 
22144 #if 0
22145   /* We default the array ordering.  Debuggers will probably do the right
22146      things even if DW_AT_ordering is not present.  It's not even an issue
22147      until we start to get into multidimensional arrays anyway.  If a debugger
22148      is ever caught doing the Wrong Thing for multi-dimensional arrays,
22149      then we'll have to put the DW_AT_ordering attribute back in.  (But if
22150      and when we find out that we need to put these in, we will only do so
22151      for multidimensional arrays.  */
22152   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
22153 #endif
22154 
22155   if (TREE_CODE (type) == VECTOR_TYPE)
22156     {
22157       /* For VECTOR_TYPEs we use an array die with appropriate bounds.  */
22158       dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
22159       add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
22160       add_bound_info (subrange_die, DW_AT_upper_bound,
22161 		      size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
22162     }
22163   else
22164     add_subscript_info (array_die, type, collapse_nested_arrays);
22165 
22166   /* Add representation of the type of the elements of this array type and
22167      emit the corresponding DIE if we haven't done it already.  */
22168   element_type = TREE_TYPE (type);
22169   if (collapse_nested_arrays)
22170     while (TREE_CODE (element_type) == ARRAY_TYPE)
22171       {
22172 	if (TYPE_STRING_FLAG (element_type) && is_fortran ())
22173 	  break;
22174 	element_type = TREE_TYPE (element_type);
22175       }
22176 
22177   add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
22178 		      TREE_CODE (type) == ARRAY_TYPE
22179 		      && TYPE_REVERSE_STORAGE_ORDER (type),
22180 		      context_die);
22181 
22182   add_gnat_descriptive_type_attribute (array_die, type, context_die);
22183   if (TYPE_ARTIFICIAL (type))
22184     add_AT_flag (array_die, DW_AT_artificial, 1);
22185 
22186   if (get_AT (array_die, DW_AT_name))
22187     add_pubtype (type, array_die);
22188 
22189   add_alignment_attribute (array_die, type);
22190 }
22191 
22192 /* This routine generates DIE for array with hidden descriptor, details
22193    are filled into *info by a langhook.  */
22194 
22195 static void
gen_descr_array_type_die(tree type,struct array_descr_info * info,dw_die_ref context_die)22196 gen_descr_array_type_die (tree type, struct array_descr_info *info,
22197 			  dw_die_ref context_die)
22198 {
22199   const dw_die_ref scope_die = scope_die_for (type, context_die);
22200   const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
22201   struct loc_descr_context context = { type, info->base_decl, NULL,
22202 				       false, false };
22203   enum dwarf_tag subrange_tag = DW_TAG_subrange_type;
22204   int dim;
22205 
22206   add_name_attribute (array_die, type_tag (type));
22207   equate_type_number_to_die (type, array_die);
22208 
22209   if (info->ndimensions > 1)
22210     switch (info->ordering)
22211       {
22212       case array_descr_ordering_row_major:
22213 	add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
22214 	break;
22215       case array_descr_ordering_column_major:
22216 	add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
22217 	break;
22218       default:
22219 	break;
22220       }
22221 
22222   if (dwarf_version >= 3 || !dwarf_strict)
22223     {
22224       if (info->data_location)
22225 	add_scalar_info (array_die, DW_AT_data_location, info->data_location,
22226 			 dw_scalar_form_exprloc, &context);
22227       if (info->associated)
22228 	add_scalar_info (array_die, DW_AT_associated, info->associated,
22229 			 dw_scalar_form_constant
22230 			 | dw_scalar_form_exprloc
22231 			 | dw_scalar_form_reference, &context);
22232       if (info->allocated)
22233 	add_scalar_info (array_die, DW_AT_allocated, info->allocated,
22234 			 dw_scalar_form_constant
22235 			 | dw_scalar_form_exprloc
22236 			 | dw_scalar_form_reference, &context);
22237       if (info->stride)
22238 	{
22239 	  const enum dwarf_attribute attr
22240 	    = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
22241 	  const int forms
22242 	    = (info->stride_in_bits)
22243 	      ? dw_scalar_form_constant
22244 	      : (dw_scalar_form_constant
22245 		 | dw_scalar_form_exprloc
22246 		 | dw_scalar_form_reference);
22247 
22248 	  add_scalar_info (array_die, attr, info->stride, forms, &context);
22249 	}
22250     }
22251   if (dwarf_version >= 5)
22252     {
22253       if (info->rank)
22254 	{
22255 	  add_scalar_info (array_die, DW_AT_rank, info->rank,
22256 			   dw_scalar_form_constant
22257 			   | dw_scalar_form_exprloc, &context);
22258 	  subrange_tag = DW_TAG_generic_subrange;
22259 	  context.placeholder_arg = true;
22260 	}
22261     }
22262 
22263   add_gnat_descriptive_type_attribute (array_die, type, context_die);
22264 
22265   for (dim = 0; dim < info->ndimensions; dim++)
22266     {
22267       dw_die_ref subrange_die = new_die (subrange_tag, array_die, NULL);
22268 
22269       if (info->dimen[dim].bounds_type)
22270 	add_type_attribute (subrange_die,
22271 			    info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
22272 			    false, context_die);
22273       if (info->dimen[dim].lower_bound)
22274 	add_bound_info (subrange_die, DW_AT_lower_bound,
22275 			info->dimen[dim].lower_bound, &context);
22276       if (info->dimen[dim].upper_bound)
22277 	add_bound_info (subrange_die, DW_AT_upper_bound,
22278 			info->dimen[dim].upper_bound, &context);
22279       if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
22280 	add_scalar_info (subrange_die, DW_AT_byte_stride,
22281 			 info->dimen[dim].stride,
22282 			 dw_scalar_form_constant
22283 			 | dw_scalar_form_exprloc
22284 			 | dw_scalar_form_reference,
22285 			 &context);
22286     }
22287 
22288   gen_type_die (info->element_type, context_die);
22289   add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
22290 		      TREE_CODE (type) == ARRAY_TYPE
22291 		      && TYPE_REVERSE_STORAGE_ORDER (type),
22292 		      context_die);
22293 
22294   if (get_AT (array_die, DW_AT_name))
22295     add_pubtype (type, array_die);
22296 
22297   add_alignment_attribute (array_die, type);
22298 }
22299 
22300 #if 0
22301 static void
22302 gen_entry_point_die (tree decl, dw_die_ref context_die)
22303 {
22304   tree origin = decl_ultimate_origin (decl);
22305   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
22306 
22307   if (origin != NULL)
22308     add_abstract_origin_attribute (decl_die, origin);
22309   else
22310     {
22311       add_name_and_src_coords_attributes (decl_die, decl);
22312       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
22313 			  TYPE_UNQUALIFIED, false, context_die);
22314     }
22315 
22316   if (DECL_ABSTRACT_P (decl))
22317     equate_decl_number_to_die (decl, decl_die);
22318   else
22319     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
22320 }
22321 #endif
22322 
22323 /* Walk through the list of incomplete types again, trying once more to
22324    emit full debugging info for them.  */
22325 
22326 static void
retry_incomplete_types(void)22327 retry_incomplete_types (void)
22328 {
22329   set_early_dwarf s;
22330   int i;
22331 
22332   for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
22333     if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
22334       gen_type_die ((*incomplete_types)[i], comp_unit_die ());
22335   vec_safe_truncate (incomplete_types, 0);
22336 }
22337 
22338 /* Determine what tag to use for a record type.  */
22339 
22340 static enum dwarf_tag
record_type_tag(tree type)22341 record_type_tag (tree type)
22342 {
22343   if (! lang_hooks.types.classify_record)
22344     return DW_TAG_structure_type;
22345 
22346   switch (lang_hooks.types.classify_record (type))
22347     {
22348     case RECORD_IS_STRUCT:
22349       return DW_TAG_structure_type;
22350 
22351     case RECORD_IS_CLASS:
22352       return DW_TAG_class_type;
22353 
22354     case RECORD_IS_INTERFACE:
22355       if (dwarf_version >= 3 || !dwarf_strict)
22356 	return DW_TAG_interface_type;
22357       return DW_TAG_structure_type;
22358 
22359     default:
22360       gcc_unreachable ();
22361     }
22362 }
22363 
22364 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
22365    include all of the information about the enumeration values also. Each
22366    enumerated type name/value is listed as a child of the enumerated type
22367    DIE.  */
22368 
22369 static dw_die_ref
gen_enumeration_type_die(tree type,dw_die_ref context_die)22370 gen_enumeration_type_die (tree type, dw_die_ref context_die)
22371 {
22372   dw_die_ref type_die = lookup_type_die (type);
22373   dw_die_ref orig_type_die = type_die;
22374 
22375   if (type_die == NULL)
22376     {
22377       type_die = new_die (DW_TAG_enumeration_type,
22378 			  scope_die_for (type, context_die), type);
22379       equate_type_number_to_die (type, type_die);
22380       add_name_attribute (type_die, type_tag (type));
22381       if ((dwarf_version >= 4 || !dwarf_strict)
22382 	  && ENUM_IS_SCOPED (type))
22383 	add_AT_flag (type_die, DW_AT_enum_class, 1);
22384       if (ENUM_IS_OPAQUE (type) && TYPE_SIZE (type))
22385 	add_AT_flag (type_die, DW_AT_declaration, 1);
22386       if (!dwarf_strict)
22387 	add_AT_unsigned (type_die, DW_AT_encoding,
22388 			 TYPE_UNSIGNED (type)
22389 			 ? DW_ATE_unsigned
22390 			 : DW_ATE_signed);
22391     }
22392   else if (! TYPE_SIZE (type) || ENUM_IS_OPAQUE (type))
22393     return type_die;
22394   else
22395     remove_AT (type_die, DW_AT_declaration);
22396 
22397   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
22398      given enum type is incomplete, do not generate the DW_AT_byte_size
22399      attribute or the DW_AT_element_list attribute.  */
22400   if (TYPE_SIZE (type))
22401     {
22402       tree link;
22403 
22404       if (!ENUM_IS_OPAQUE (type))
22405 	TREE_ASM_WRITTEN (type) = 1;
22406       if (!orig_type_die || !get_AT (type_die, DW_AT_byte_size))
22407 	add_byte_size_attribute (type_die, type);
22408       if (!orig_type_die || !get_AT (type_die, DW_AT_alignment))
22409 	add_alignment_attribute (type_die, type);
22410       if ((dwarf_version >= 3 || !dwarf_strict)
22411 	  && (!orig_type_die || !get_AT (type_die, DW_AT_type)))
22412 	{
22413 	  tree underlying = lang_hooks.types.enum_underlying_base_type (type);
22414 	  add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
22415 			      context_die);
22416 	}
22417       if (TYPE_STUB_DECL (type) != NULL_TREE)
22418 	{
22419 	  if (!orig_type_die || !get_AT (type_die, DW_AT_decl_file))
22420 	    add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
22421 	  if (!orig_type_die || !get_AT (type_die, DW_AT_accessibility))
22422 	    add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
22423 	}
22424 
22425       /* If the first reference to this type was as the return type of an
22426 	 inline function, then it may not have a parent.  Fix this now.  */
22427       if (type_die->die_parent == NULL)
22428 	add_child_die (scope_die_for (type, context_die), type_die);
22429 
22430       for (link = TYPE_VALUES (type);
22431 	   link != NULL; link = TREE_CHAIN (link))
22432 	{
22433 	  dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
22434 	  tree value = TREE_VALUE (link);
22435 
22436 	  if (DECL_P (value))
22437 	    equate_decl_number_to_die (value, enum_die);
22438 
22439 	  gcc_assert (!ENUM_IS_OPAQUE (type));
22440 	  add_name_attribute (enum_die,
22441 			      IDENTIFIER_POINTER (TREE_PURPOSE (link)));
22442 
22443 	  if (TREE_CODE (value) == CONST_DECL)
22444 	    value = DECL_INITIAL (value);
22445 
22446 	  if (simple_type_size_in_bits (TREE_TYPE (value))
22447 	      <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
22448 	    {
22449 	      /* For constant forms created by add_AT_unsigned DWARF
22450 		 consumers (GDB, elfutils, etc.) always zero extend
22451 		 the value.  Only when the actual value is negative
22452 		 do we need to use add_AT_int to generate a constant
22453 		 form that can represent negative values.  */
22454 	      HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
22455 	      if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
22456 		add_AT_unsigned (enum_die, DW_AT_const_value,
22457 				 (unsigned HOST_WIDE_INT) val);
22458 	      else
22459 		add_AT_int (enum_die, DW_AT_const_value, val);
22460 	    }
22461 	  else
22462 	    /* Enumeration constants may be wider than HOST_WIDE_INT.  Handle
22463 	       that here.  TODO: This should be re-worked to use correct
22464 	       signed/unsigned double tags for all cases.  */
22465 	    add_AT_wide (enum_die, DW_AT_const_value, wi::to_wide (value));
22466 	}
22467 
22468       add_gnat_descriptive_type_attribute (type_die, type, context_die);
22469       if (TYPE_ARTIFICIAL (type)
22470 	  && (!orig_type_die || !get_AT (type_die, DW_AT_artificial)))
22471 	add_AT_flag (type_die, DW_AT_artificial, 1);
22472     }
22473   else
22474     add_AT_flag (type_die, DW_AT_declaration, 1);
22475 
22476   add_pubtype (type, type_die);
22477 
22478   return type_die;
22479 }
22480 
22481 /* Generate a DIE to represent either a real live formal parameter decl or to
22482    represent just the type of some formal parameter position in some function
22483    type.
22484 
22485    Note that this routine is a bit unusual because its argument may be a
22486    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
22487    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
22488    node.  If it's the former then this function is being called to output a
22489    DIE to represent a formal parameter object (or some inlining thereof).  If
22490    it's the latter, then this function is only being called to output a
22491    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
22492    argument type of some subprogram type.
22493    If EMIT_NAME_P is true, name and source coordinate attributes
22494    are emitted.  */
22495 
22496 static dw_die_ref
gen_formal_parameter_die(tree node,tree origin,bool emit_name_p,dw_die_ref context_die)22497 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
22498 			  dw_die_ref context_die)
22499 {
22500   tree node_or_origin = node ? node : origin;
22501   tree ultimate_origin;
22502   dw_die_ref parm_die = NULL;
22503 
22504   if (DECL_P (node_or_origin))
22505     {
22506       parm_die = lookup_decl_die (node);
22507 
22508       /* If the contexts differ, we may not be talking about the same
22509 	 thing.
22510 	 ???  When in LTO the DIE parent is the "abstract" copy and the
22511 	 context_die is the specification "copy".  */
22512       if (parm_die
22513 	  && parm_die->die_parent != context_die
22514 	  && (parm_die->die_parent->die_tag != DW_TAG_GNU_formal_parameter_pack
22515 	      || parm_die->die_parent->die_parent != context_die)
22516 	  && !in_lto_p)
22517 	{
22518 	  gcc_assert (!DECL_ABSTRACT_P (node));
22519 	  /* This can happen when creating a concrete instance, in
22520 	     which case we need to create a new DIE that will get
22521 	     annotated with DW_AT_abstract_origin.  */
22522 	  parm_die = NULL;
22523 	}
22524 
22525       if (parm_die && parm_die->die_parent == NULL)
22526 	{
22527 	  /* Check that parm_die already has the right attributes that
22528 	     we would have added below.  If any attributes are
22529 	     missing, fall through to add them.  */
22530 	  if (! DECL_ABSTRACT_P (node_or_origin)
22531 	      && !get_AT (parm_die, DW_AT_location)
22532 	      && !get_AT (parm_die, DW_AT_const_value))
22533 	    /* We are missing  location info, and are about to add it.  */
22534 	    ;
22535 	  else
22536 	    {
22537 	      add_child_die (context_die, parm_die);
22538 	      return parm_die;
22539 	    }
22540 	}
22541     }
22542 
22543   /* If we have a previously generated DIE, use it, unless this is an
22544      concrete instance (origin != NULL), in which case we need a new
22545      DIE with a corresponding DW_AT_abstract_origin.  */
22546   bool reusing_die;
22547   if (parm_die && origin == NULL)
22548     reusing_die = true;
22549   else
22550     {
22551       parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
22552       reusing_die = false;
22553     }
22554 
22555   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
22556     {
22557     case tcc_declaration:
22558       ultimate_origin = decl_ultimate_origin (node_or_origin);
22559       if (node || ultimate_origin)
22560 	origin = ultimate_origin;
22561 
22562       if (reusing_die)
22563 	goto add_location;
22564 
22565       if (origin != NULL)
22566 	add_abstract_origin_attribute (parm_die, origin);
22567       else if (emit_name_p)
22568 	add_name_and_src_coords_attributes (parm_die, node);
22569       if (origin == NULL
22570 	  || (! DECL_ABSTRACT_P (node_or_origin)
22571 	      && variably_modified_type_p (TREE_TYPE (node_or_origin),
22572 					   decl_function_context
22573 							    (node_or_origin))))
22574 	{
22575 	  tree type = TREE_TYPE (node_or_origin);
22576 	  if (decl_by_reference_p (node_or_origin))
22577 	    add_type_attribute (parm_die, TREE_TYPE (type),
22578 				TYPE_UNQUALIFIED,
22579 				false, context_die);
22580 	  else
22581 	    add_type_attribute (parm_die, type,
22582 				decl_quals (node_or_origin),
22583 				false, context_die);
22584 	}
22585       if (origin == NULL && DECL_ARTIFICIAL (node))
22586 	add_AT_flag (parm_die, DW_AT_artificial, 1);
22587     add_location:
22588       if (node && node != origin)
22589         equate_decl_number_to_die (node, parm_die);
22590       if (! DECL_ABSTRACT_P (node_or_origin))
22591 	add_location_or_const_value_attribute (parm_die, node_or_origin,
22592 					       node == NULL);
22593 
22594       break;
22595 
22596     case tcc_type:
22597       /* We were called with some kind of a ..._TYPE node.  */
22598       add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
22599 			  context_die);
22600       break;
22601 
22602     default:
22603       gcc_unreachable ();
22604     }
22605 
22606   return parm_die;
22607 }
22608 
22609 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
22610    children DW_TAG_formal_parameter DIEs representing the arguments of the
22611    parameter pack.
22612 
22613    PARM_PACK must be a function parameter pack.
22614    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
22615    must point to the subsequent arguments of the function PACK_ARG belongs to.
22616    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
22617    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
22618    following the last one for which a DIE was generated.  */
22619 
22620 static dw_die_ref
gen_formal_parameter_pack_die(tree parm_pack,tree pack_arg,dw_die_ref subr_die,tree * next_arg)22621 gen_formal_parameter_pack_die  (tree parm_pack,
22622 				tree pack_arg,
22623 				dw_die_ref subr_die,
22624 				tree *next_arg)
22625 {
22626   tree arg;
22627   dw_die_ref parm_pack_die;
22628 
22629   gcc_assert (parm_pack
22630 	      && lang_hooks.function_parameter_pack_p (parm_pack)
22631 	      && subr_die);
22632 
22633   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
22634   add_src_coords_attributes (parm_pack_die, parm_pack);
22635 
22636   for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
22637     {
22638       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
22639 								 parm_pack))
22640 	break;
22641       gen_formal_parameter_die (arg, NULL,
22642 				false /* Don't emit name attribute.  */,
22643 				parm_pack_die);
22644     }
22645   if (next_arg)
22646     *next_arg = arg;
22647   return parm_pack_die;
22648 }
22649 
22650 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
22651    at the end of an (ANSI prototyped) formal parameters list.  */
22652 
22653 static void
gen_unspecified_parameters_die(tree decl_or_type,dw_die_ref context_die)22654 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
22655 {
22656   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
22657 }
22658 
22659 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
22660    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
22661    parameters as specified in some function type specification (except for
22662    those which appear as part of a function *definition*).  */
22663 
22664 static void
gen_formal_types_die(tree function_or_method_type,dw_die_ref context_die)22665 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
22666 {
22667   tree link;
22668   tree formal_type = NULL;
22669   tree first_parm_type;
22670   tree arg;
22671 
22672   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
22673     {
22674       arg = DECL_ARGUMENTS (function_or_method_type);
22675       function_or_method_type = TREE_TYPE (function_or_method_type);
22676     }
22677   else
22678     arg = NULL_TREE;
22679 
22680   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
22681 
22682   /* Make our first pass over the list of formal parameter types and output a
22683      DW_TAG_formal_parameter DIE for each one.  */
22684   for (link = first_parm_type; link; )
22685     {
22686       dw_die_ref parm_die;
22687 
22688       formal_type = TREE_VALUE (link);
22689       if (formal_type == void_type_node)
22690 	break;
22691 
22692       /* Output a (nameless) DIE to represent the formal parameter itself.  */
22693       parm_die = gen_formal_parameter_die (formal_type, NULL,
22694 					   true /* Emit name attribute.  */,
22695 					   context_die);
22696       if (TREE_CODE (function_or_method_type) == METHOD_TYPE
22697 	  && link == first_parm_type)
22698 	{
22699 	  add_AT_flag (parm_die, DW_AT_artificial, 1);
22700 	  if (dwarf_version >= 3 || !dwarf_strict)
22701 	    add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
22702 	}
22703       else if (arg && DECL_ARTIFICIAL (arg))
22704 	add_AT_flag (parm_die, DW_AT_artificial, 1);
22705 
22706       link = TREE_CHAIN (link);
22707       if (arg)
22708 	arg = DECL_CHAIN (arg);
22709     }
22710 
22711   /* If this function type has an ellipsis, add a
22712      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
22713   if (formal_type != void_type_node)
22714     gen_unspecified_parameters_die (function_or_method_type, context_die);
22715 
22716   /* Make our second (and final) pass over the list of formal parameter types
22717      and output DIEs to represent those types (as necessary).  */
22718   for (link = TYPE_ARG_TYPES (function_or_method_type);
22719        link && TREE_VALUE (link);
22720        link = TREE_CHAIN (link))
22721     gen_type_die (TREE_VALUE (link), context_die);
22722 }
22723 
22724 /* We want to generate the DIE for TYPE so that we can generate the
22725    die for MEMBER, which has been defined; we will need to refer back
22726    to the member declaration nested within TYPE.  If we're trying to
22727    generate minimal debug info for TYPE, processing TYPE won't do the
22728    trick; we need to attach the member declaration by hand.  */
22729 
22730 static void
gen_type_die_for_member(tree type,tree member,dw_die_ref context_die)22731 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
22732 {
22733   gen_type_die (type, context_die);
22734 
22735   /* If we're trying to avoid duplicate debug info, we may not have
22736      emitted the member decl for this function.  Emit it now.  */
22737   if (TYPE_STUB_DECL (type)
22738       && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
22739       && ! lookup_decl_die (member))
22740     {
22741       dw_die_ref type_die;
22742       gcc_assert (!decl_ultimate_origin (member));
22743 
22744       type_die = lookup_type_die_strip_naming_typedef (type);
22745       if (TREE_CODE (member) == FUNCTION_DECL)
22746 	gen_subprogram_die (member, type_die);
22747       else if (TREE_CODE (member) == FIELD_DECL)
22748 	{
22749 	  /* Ignore the nameless fields that are used to skip bits but handle
22750 	     C++ anonymous unions and structs.  */
22751 	  if (DECL_NAME (member) != NULL_TREE
22752 	      || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
22753 	      || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
22754 	    {
22755 	      struct vlr_context vlr_ctx = {
22756 		DECL_CONTEXT (member), /* struct_type */
22757 		NULL_TREE /* variant_part_offset */
22758 	      };
22759 	      gen_type_die (member_declared_type (member), type_die);
22760 	      gen_field_die (member, &vlr_ctx, type_die);
22761 	    }
22762 	}
22763       else
22764 	gen_variable_die (member, NULL_TREE, type_die);
22765     }
22766 }
22767 
22768 /* Forward declare these functions, because they are mutually recursive
22769   with their set_block_* pairing functions.  */
22770 static void set_decl_origin_self (tree);
22771 
22772 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
22773    given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
22774    that it points to the node itself, thus indicating that the node is its
22775    own (abstract) origin.  Additionally, if the BLOCK_ABSTRACT_ORIGIN for
22776    the given node is NULL, recursively descend the decl/block tree which
22777    it is the root of, and for each other ..._DECL or BLOCK node contained
22778    therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
22779    still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
22780    values to point to themselves.  */
22781 
22782 static void
set_block_origin_self(tree stmt)22783 set_block_origin_self (tree stmt)
22784 {
22785   if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
22786     {
22787       BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
22788 
22789       {
22790 	tree local_decl;
22791 
22792 	for (local_decl = BLOCK_VARS (stmt);
22793 	     local_decl != NULL_TREE;
22794 	     local_decl = DECL_CHAIN (local_decl))
22795 	  /* Do not recurse on nested functions since the inlining status
22796 	     of parent and child can be different as per the DWARF spec.  */
22797 	  if (TREE_CODE (local_decl) != FUNCTION_DECL
22798 	      && !DECL_EXTERNAL (local_decl))
22799 	    set_decl_origin_self (local_decl);
22800       }
22801 
22802       {
22803 	tree subblock;
22804 
22805 	for (subblock = BLOCK_SUBBLOCKS (stmt);
22806 	     subblock != NULL_TREE;
22807 	     subblock = BLOCK_CHAIN (subblock))
22808 	  set_block_origin_self (subblock);	/* Recurse.  */
22809       }
22810     }
22811 }
22812 
22813 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
22814    the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
22815    node to so that it points to the node itself, thus indicating that the
22816    node represents its own (abstract) origin.  Additionally, if the
22817    DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
22818    the decl/block tree of which the given node is the root of, and for
22819    each other ..._DECL or BLOCK node contained therein whose
22820    DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
22821    set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
22822    point to themselves.  */
22823 
22824 static void
set_decl_origin_self(tree decl)22825 set_decl_origin_self (tree decl)
22826 {
22827   if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
22828     {
22829       DECL_ABSTRACT_ORIGIN (decl) = decl;
22830       if (TREE_CODE (decl) == FUNCTION_DECL)
22831 	{
22832 	  tree arg;
22833 
22834 	  for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
22835 	    DECL_ABSTRACT_ORIGIN (arg) = arg;
22836 	  if (DECL_INITIAL (decl) != NULL_TREE
22837 	      && DECL_INITIAL (decl) != error_mark_node)
22838 	    set_block_origin_self (DECL_INITIAL (decl));
22839 	}
22840     }
22841 }
22842 
22843 /* Mark the early DIE for DECL as the abstract instance.  */
22844 
22845 static void
dwarf2out_abstract_function(tree decl)22846 dwarf2out_abstract_function (tree decl)
22847 {
22848   dw_die_ref old_die;
22849 
22850   /* Make sure we have the actual abstract inline, not a clone.  */
22851   decl = DECL_ORIGIN (decl);
22852 
22853   if (DECL_IGNORED_P (decl))
22854     return;
22855 
22856   /* In LTO we're all set.  We already created abstract instances
22857      early and we want to avoid creating a concrete instance of that
22858      if we don't output it.  */
22859   if (in_lto_p)
22860     return;
22861 
22862   old_die = lookup_decl_die (decl);
22863   gcc_assert (old_die != NULL);
22864   if (get_AT (old_die, DW_AT_inline))
22865     /* We've already generated the abstract instance.  */
22866     return;
22867 
22868   /* Go ahead and put DW_AT_inline on the DIE.  */
22869   if (DECL_DECLARED_INLINE_P (decl))
22870     {
22871       if (cgraph_function_possibly_inlined_p (decl))
22872 	add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_inlined);
22873       else
22874 	add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_not_inlined);
22875     }
22876   else
22877     {
22878       if (cgraph_function_possibly_inlined_p (decl))
22879 	add_AT_unsigned (old_die, DW_AT_inline, DW_INL_inlined);
22880       else
22881 	add_AT_unsigned (old_die, DW_AT_inline, DW_INL_not_inlined);
22882     }
22883 
22884   if (DECL_DECLARED_INLINE_P (decl)
22885       && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
22886     add_AT_flag (old_die, DW_AT_artificial, 1);
22887 
22888   set_decl_origin_self (decl);
22889 }
22890 
22891 /* Helper function of premark_used_types() which gets called through
22892    htab_traverse.
22893 
22894    Marks the DIE of a given type in *SLOT as perennial, so it never gets
22895    marked as unused by prune_unused_types.  */
22896 
22897 bool
premark_used_types_helper(tree const & type,void *)22898 premark_used_types_helper (tree const &type, void *)
22899 {
22900   dw_die_ref die;
22901 
22902   die = lookup_type_die (type);
22903   if (die != NULL)
22904     die->die_perennial_p = 1;
22905   return true;
22906 }
22907 
22908 /* Helper function of premark_types_used_by_global_vars which gets called
22909    through htab_traverse.
22910 
22911    Marks the DIE of a given type in *SLOT as perennial, so it never gets
22912    marked as unused by prune_unused_types. The DIE of the type is marked
22913    only if the global variable using the type will actually be emitted.  */
22914 
22915 int
premark_types_used_by_global_vars_helper(types_used_by_vars_entry ** slot,void *)22916 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
22917 					  void *)
22918 {
22919   struct types_used_by_vars_entry *entry;
22920   dw_die_ref die;
22921 
22922   entry = (struct types_used_by_vars_entry *) *slot;
22923   gcc_assert (entry->type != NULL
22924 	      && entry->var_decl != NULL);
22925   die = lookup_type_die (entry->type);
22926   if (die)
22927     {
22928       /* Ask cgraph if the global variable really is to be emitted.
22929          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
22930       varpool_node *node = varpool_node::get (entry->var_decl);
22931       if (node && node->definition)
22932 	{
22933 	  die->die_perennial_p = 1;
22934 	  /* Keep the parent DIEs as well.  */
22935 	  while ((die = die->die_parent) && die->die_perennial_p == 0)
22936 	    die->die_perennial_p = 1;
22937 	}
22938     }
22939   return 1;
22940 }
22941 
22942 /* Mark all members of used_types_hash as perennial.  */
22943 
22944 static void
premark_used_types(struct function * fun)22945 premark_used_types (struct function *fun)
22946 {
22947   if (fun && fun->used_types_hash)
22948     fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
22949 }
22950 
22951 /* Mark all members of types_used_by_vars_entry as perennial.  */
22952 
22953 static void
premark_types_used_by_global_vars(void)22954 premark_types_used_by_global_vars (void)
22955 {
22956   if (types_used_by_vars_hash)
22957     types_used_by_vars_hash
22958       ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
22959 }
22960 
22961 /* Mark all variables used by the symtab as perennial.  */
22962 
22963 static void
premark_used_variables(void)22964 premark_used_variables (void)
22965 {
22966   /* Mark DIEs in the symtab as used.  */
22967   varpool_node *var;
22968   FOR_EACH_VARIABLE (var)
22969     {
22970       dw_die_ref die = lookup_decl_die (var->decl);
22971       if (die)
22972 	die->die_perennial_p = 1;
22973     }
22974 }
22975 
22976 /* Generate a DW_TAG_call_site DIE in function DECL under SUBR_DIE
22977    for CA_LOC call arg loc node.  */
22978 
22979 static dw_die_ref
gen_call_site_die(tree decl,dw_die_ref subr_die,struct call_arg_loc_node * ca_loc)22980 gen_call_site_die (tree decl, dw_die_ref subr_die,
22981 		   struct call_arg_loc_node *ca_loc)
22982 {
22983   dw_die_ref stmt_die = NULL, die;
22984   tree block = ca_loc->block;
22985 
22986   while (block
22987 	 && block != DECL_INITIAL (decl)
22988 	 && TREE_CODE (block) == BLOCK)
22989     {
22990       stmt_die = lookup_block_die (block);
22991       if (stmt_die)
22992 	break;
22993       block = BLOCK_SUPERCONTEXT (block);
22994     }
22995   if (stmt_die == NULL)
22996     stmt_die = subr_die;
22997   die = new_die (dwarf_TAG (DW_TAG_call_site), stmt_die, NULL_TREE);
22998   add_AT_lbl_id (die, dwarf_AT (DW_AT_call_return_pc), ca_loc->label);
22999   if (ca_loc->tail_call_p)
23000     add_AT_flag (die, dwarf_AT (DW_AT_call_tail_call), 1);
23001   if (ca_loc->symbol_ref)
23002     {
23003       dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
23004       if (tdie)
23005 	add_AT_die_ref (die, dwarf_AT (DW_AT_call_origin), tdie);
23006       else
23007 	add_AT_addr (die, dwarf_AT (DW_AT_call_origin), ca_loc->symbol_ref,
23008 		     false);
23009     }
23010   return die;
23011 }
23012 
23013 /* Generate a DIE to represent a declared function (either file-scope or
23014    block-local).  */
23015 
23016 static void
gen_subprogram_die(tree decl,dw_die_ref context_die)23017 gen_subprogram_die (tree decl, dw_die_ref context_die)
23018 {
23019   tree origin = decl_ultimate_origin (decl);
23020   dw_die_ref subr_die;
23021   dw_die_ref old_die = lookup_decl_die (decl);
23022   bool old_die_had_no_children = false;
23023 
23024   /* This function gets called multiple times for different stages of
23025      the debug process.  For example, for func() in this code:
23026 
23027 	namespace S
23028 	{
23029 	  void func() { ... }
23030 	}
23031 
23032      ...we get called 4 times.  Twice in early debug and twice in
23033      late debug:
23034 
23035      Early debug
23036      -----------
23037 
23038        1. Once while generating func() within the namespace.  This is
23039           the declaration.  The declaration bit below is set, as the
23040           context is the namespace.
23041 
23042 	  A new DIE will be generated with DW_AT_declaration set.
23043 
23044        2. Once for func() itself.  This is the specification.  The
23045           declaration bit below is clear as the context is the CU.
23046 
23047 	  We will use the cached DIE from (1) to create a new DIE with
23048 	  DW_AT_specification pointing to the declaration in (1).
23049 
23050      Late debug via rest_of_handle_final()
23051      -------------------------------------
23052 
23053        3. Once generating func() within the namespace.  This is also the
23054           declaration, as in (1), but this time we will early exit below
23055           as we have a cached DIE and a declaration needs no additional
23056           annotations (no locations), as the source declaration line
23057           info is enough.
23058 
23059        4. Once for func() itself.  As in (2), this is the specification,
23060           but this time we will re-use the cached DIE, and just annotate
23061           it with the location information that should now be available.
23062 
23063      For something without namespaces, but with abstract instances, we
23064      are also called a multiple times:
23065 
23066         class Base
23067 	{
23068 	public:
23069 	  Base ();	  // constructor declaration (1)
23070 	};
23071 
23072 	Base::Base () { } // constructor specification (2)
23073 
23074     Early debug
23075     -----------
23076 
23077        1. Once for the Base() constructor by virtue of it being a
23078           member of the Base class.  This is done via
23079           rest_of_type_compilation.
23080 
23081 	  This is a declaration, so a new DIE will be created with
23082 	  DW_AT_declaration.
23083 
23084        2. Once for the Base() constructor definition, but this time
23085           while generating the abstract instance of the base
23086           constructor (__base_ctor) which is being generated via early
23087           debug of reachable functions.
23088 
23089 	  Even though we have a cached version of the declaration (1),
23090 	  we will create a DW_AT_specification of the declaration DIE
23091 	  in (1).
23092 
23093        3. Once for the __base_ctor itself, but this time, we generate
23094           an DW_AT_abstract_origin version of the DW_AT_specification in
23095 	  (2).
23096 
23097     Late debug via rest_of_handle_final
23098     -----------------------------------
23099 
23100        4. One final time for the __base_ctor (which will have a cached
23101           DIE with DW_AT_abstract_origin created in (3).  This time,
23102           we will just annotate the location information now
23103           available.
23104   */
23105   int declaration = (current_function_decl != decl
23106 		     || (!DECL_INITIAL (decl) && !origin)
23107 		     || class_or_namespace_scope_p (context_die));
23108 
23109   /* A declaration that has been previously dumped needs no
23110      additional information.  */
23111   if (old_die && declaration)
23112     return;
23113 
23114   if (in_lto_p && old_die && old_die->die_child == NULL)
23115     old_die_had_no_children = true;
23116 
23117   /* Now that the C++ front end lazily declares artificial member fns, we
23118      might need to retrofit the declaration into its class.  */
23119   if (!declaration && !origin && !old_die
23120       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
23121       && !class_or_namespace_scope_p (context_die)
23122       && debug_info_level > DINFO_LEVEL_TERSE)
23123     old_die = force_decl_die (decl);
23124 
23125   /* A concrete instance, tag a new DIE with DW_AT_abstract_origin.  */
23126   if (origin != NULL)
23127     {
23128       gcc_assert (!declaration || local_scope_p (context_die));
23129 
23130       /* Fixup die_parent for the abstract instance of a nested
23131 	 inline function.  */
23132       if (old_die && old_die->die_parent == NULL)
23133 	add_child_die (context_die, old_die);
23134 
23135       if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
23136 	{
23137 	  /* If we have a DW_AT_abstract_origin we have a working
23138 	     cached version.  */
23139 	  subr_die = old_die;
23140 	}
23141       else
23142 	{
23143 	  subr_die = new_die (DW_TAG_subprogram, context_die, decl);
23144 	  add_abstract_origin_attribute (subr_die, origin);
23145 	  /*  This is where the actual code for a cloned function is.
23146 	      Let's emit linkage name attribute for it.  This helps
23147 	      debuggers to e.g, set breakpoints into
23148 	      constructors/destructors when the user asks "break
23149 	      K::K".  */
23150 	  add_linkage_name (subr_die, decl);
23151 	}
23152     }
23153   /* A cached copy, possibly from early dwarf generation.  Reuse as
23154      much as possible.  */
23155   else if (old_die)
23156     {
23157       if (!get_AT_flag (old_die, DW_AT_declaration)
23158 	  /* We can have a normal definition following an inline one in the
23159 	     case of redefinition of GNU C extern inlines.
23160 	     It seems reasonable to use AT_specification in this case.  */
23161 	  && !get_AT (old_die, DW_AT_inline))
23162 	{
23163 	  /* Detect and ignore this case, where we are trying to output
23164 	     something we have already output.  */
23165 	  if (get_AT (old_die, DW_AT_low_pc)
23166 	      || get_AT (old_die, DW_AT_ranges))
23167 	    return;
23168 
23169 	  /* If we have no location information, this must be a
23170 	     partially generated DIE from early dwarf generation.
23171 	     Fall through and generate it.  */
23172 	}
23173 
23174       /* If the definition comes from the same place as the declaration,
23175 	 maybe use the old DIE.  We always want the DIE for this function
23176 	 that has the *_pc attributes to be under comp_unit_die so the
23177 	 debugger can find it.  We also need to do this for abstract
23178 	 instances of inlines, since the spec requires the out-of-line copy
23179 	 to have the same parent.  For local class methods, this doesn't
23180 	 apply; we just use the old DIE.  */
23181       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
23182       struct dwarf_file_data * file_index = lookup_filename (s.file);
23183       if (((is_unit_die (old_die->die_parent)
23184 	    /* This condition fixes the inconsistency/ICE with the
23185 	       following Fortran test (or some derivative thereof) while
23186 	       building libgfortran:
23187 
23188 		  module some_m
23189 		  contains
23190 		     logical function funky (FLAG)
23191 		       funky = .true.
23192 		    end function
23193 		  end module
23194 	     */
23195 	    || (old_die->die_parent
23196 		&& old_die->die_parent->die_tag == DW_TAG_module)
23197 	    || local_scope_p (old_die->die_parent)
23198 	    || context_die == NULL)
23199 	   && (DECL_ARTIFICIAL (decl)
23200 	       || (get_AT_file (old_die, DW_AT_decl_file) == file_index
23201 		   && (get_AT_unsigned (old_die, DW_AT_decl_line)
23202 		       == (unsigned) s.line)
23203 		   && (!debug_column_info
23204 		       || s.column == 0
23205 		       || (get_AT_unsigned (old_die, DW_AT_decl_column)
23206 			   == (unsigned) s.column)))))
23207 	  /* With LTO if there's an abstract instance for
23208 	     the old DIE, this is a concrete instance and
23209 	     thus re-use the DIE.  */
23210 	  || get_AT (old_die, DW_AT_abstract_origin))
23211 	{
23212 	  subr_die = old_die;
23213 
23214 	  /* Clear out the declaration attribute, but leave the
23215 	     parameters so they can be augmented with location
23216 	     information later.  Unless this was a declaration, in
23217 	     which case, wipe out the nameless parameters and recreate
23218 	     them further down.  */
23219 	  if (remove_AT (subr_die, DW_AT_declaration))
23220 	    {
23221 
23222 	      remove_AT (subr_die, DW_AT_object_pointer);
23223 	      remove_child_TAG (subr_die, DW_TAG_formal_parameter);
23224 	    }
23225 	}
23226       /* Make a specification pointing to the previously built
23227 	 declaration.  */
23228       else
23229 	{
23230 	  subr_die = new_die (DW_TAG_subprogram, context_die, decl);
23231 	  add_AT_specification (subr_die, old_die);
23232           add_pubname (decl, subr_die);
23233 	  if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
23234 	    add_AT_file (subr_die, DW_AT_decl_file, file_index);
23235 	  if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
23236 	    add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
23237 	  if (debug_column_info
23238 	      && s.column
23239 	      && (get_AT_unsigned (old_die, DW_AT_decl_column)
23240 		  != (unsigned) s.column))
23241 	    add_AT_unsigned (subr_die, DW_AT_decl_column, s.column);
23242 
23243 	  /* If the prototype had an 'auto' or 'decltype(auto)' in
23244 	     the return type, emit the real type on the definition die.  */
23245 	  if (is_cxx () && debug_info_level > DINFO_LEVEL_TERSE)
23246 	    {
23247 	      dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
23248 	      while (die
23249 		     && (die->die_tag == DW_TAG_reference_type
23250 			 || die->die_tag == DW_TAG_rvalue_reference_type
23251 			 || die->die_tag == DW_TAG_pointer_type
23252 			 || die->die_tag == DW_TAG_const_type
23253 			 || die->die_tag == DW_TAG_volatile_type
23254 			 || die->die_tag == DW_TAG_restrict_type
23255 			 || die->die_tag == DW_TAG_array_type
23256 			 || die->die_tag == DW_TAG_ptr_to_member_type
23257 			 || die->die_tag == DW_TAG_subroutine_type))
23258 		die = get_AT_ref (die, DW_AT_type);
23259 	      if (die == auto_die || die == decltype_auto_die)
23260 		add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
23261 				    TYPE_UNQUALIFIED, false, context_die);
23262 	    }
23263 
23264 	  /* When we process the method declaration, we haven't seen
23265 	     the out-of-class defaulted definition yet, so we have to
23266 	     recheck now.  */
23267 	  if ((dwarf_version >= 5 || ! dwarf_strict)
23268 	      && !get_AT (subr_die, DW_AT_defaulted))
23269 	    {
23270 	      int defaulted
23271 		= lang_hooks.decls.decl_dwarf_attribute (decl,
23272 							 DW_AT_defaulted);
23273 	      if (defaulted != -1)
23274 		{
23275 		  /* Other values must have been handled before.  */
23276 		  gcc_assert (defaulted == DW_DEFAULTED_out_of_class);
23277 		  add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
23278 		}
23279 	    }
23280 	}
23281     }
23282   /* Create a fresh DIE for anything else.  */
23283   else
23284     {
23285       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
23286 
23287       if (TREE_PUBLIC (decl))
23288 	add_AT_flag (subr_die, DW_AT_external, 1);
23289 
23290       add_name_and_src_coords_attributes (subr_die, decl);
23291       add_pubname (decl, subr_die);
23292       if (debug_info_level > DINFO_LEVEL_TERSE)
23293 	{
23294 	  add_prototyped_attribute (subr_die, TREE_TYPE (decl));
23295 	  add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
23296 			      TYPE_UNQUALIFIED, false, context_die);
23297 	}
23298 
23299       add_pure_or_virtual_attribute (subr_die, decl);
23300       if (DECL_ARTIFICIAL (decl))
23301 	add_AT_flag (subr_die, DW_AT_artificial, 1);
23302 
23303       if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
23304 	add_AT_flag (subr_die, DW_AT_noreturn, 1);
23305 
23306       add_alignment_attribute (subr_die, decl);
23307 
23308       add_accessibility_attribute (subr_die, decl);
23309     }
23310 
23311   /* Unless we have an existing non-declaration DIE, equate the new
23312      DIE.  */
23313   if (!old_die || is_declaration_die (old_die))
23314     equate_decl_number_to_die (decl, subr_die);
23315 
23316   if (declaration)
23317     {
23318       if (!old_die || !get_AT (old_die, DW_AT_inline))
23319 	{
23320 	  add_AT_flag (subr_die, DW_AT_declaration, 1);
23321 
23322 	  /* If this is an explicit function declaration then generate
23323 	     a DW_AT_explicit attribute.  */
23324 	  if ((dwarf_version >= 3 || !dwarf_strict)
23325 	      && lang_hooks.decls.decl_dwarf_attribute (decl,
23326 							DW_AT_explicit) == 1)
23327 	    add_AT_flag (subr_die, DW_AT_explicit, 1);
23328 
23329 	  /* If this is a C++11 deleted special function member then generate
23330 	     a DW_AT_deleted attribute.  */
23331 	  if ((dwarf_version >= 5 || !dwarf_strict)
23332 	      && lang_hooks.decls.decl_dwarf_attribute (decl,
23333 							DW_AT_deleted) == 1)
23334 	    add_AT_flag (subr_die, DW_AT_deleted, 1);
23335 
23336 	  /* If this is a C++11 defaulted special function member then
23337 	     generate a DW_AT_defaulted attribute.  */
23338 	  if (dwarf_version >= 5 || !dwarf_strict)
23339 	    {
23340 	      int defaulted
23341 		= lang_hooks.decls.decl_dwarf_attribute (decl,
23342 							 DW_AT_defaulted);
23343 	      if (defaulted != -1)
23344 		add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
23345 	    }
23346 
23347 	  /* If this is a C++11 non-static member function with & ref-qualifier
23348 	     then generate a DW_AT_reference attribute.  */
23349 	  if ((dwarf_version >= 5 || !dwarf_strict)
23350 	      && lang_hooks.decls.decl_dwarf_attribute (decl,
23351 							DW_AT_reference) == 1)
23352 	    add_AT_flag (subr_die, DW_AT_reference, 1);
23353 
23354 	  /* If this is a C++11 non-static member function with &&
23355 	     ref-qualifier then generate a DW_AT_reference attribute.  */
23356 	  if ((dwarf_version >= 5 || !dwarf_strict)
23357 	      && lang_hooks.decls.decl_dwarf_attribute (decl,
23358 							DW_AT_rvalue_reference)
23359 		 == 1)
23360 	    add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
23361 	}
23362     }
23363   /* For non DECL_EXTERNALs, if range information is available, fill
23364      the DIE with it.  */
23365   else if (!DECL_EXTERNAL (decl) && !early_dwarf)
23366     {
23367       HOST_WIDE_INT cfa_fb_offset;
23368 
23369       struct function *fun = DECL_STRUCT_FUNCTION (decl);
23370 
23371       if (!crtl->has_bb_partition)
23372 	{
23373 	  dw_fde_ref fde = fun->fde;
23374 	  if (fde->dw_fde_begin)
23375 	    {
23376 	      /* We have already generated the labels.  */
23377              add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
23378                                  fde->dw_fde_end, false);
23379 	    }
23380 	  else
23381 	    {
23382 	      /* Create start/end labels and add the range.  */
23383 	      char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
23384 	      char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
23385 	      ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
23386 					   current_function_funcdef_no);
23387 	      ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
23388 					   current_function_funcdef_no);
23389              add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
23390                                  false);
23391 	    }
23392 
23393 #if VMS_DEBUGGING_INFO
23394       /* HP OpenVMS Industry Standard 64: DWARF Extensions
23395 	 Section 2.3 Prologue and Epilogue Attributes:
23396 	 When a breakpoint is set on entry to a function, it is generally
23397 	 desirable for execution to be suspended, not on the very first
23398 	 instruction of the function, but rather at a point after the
23399 	 function's frame has been set up, after any language defined local
23400 	 declaration processing has been completed, and before execution of
23401 	 the first statement of the function begins. Debuggers generally
23402 	 cannot properly determine where this point is.  Similarly for a
23403 	 breakpoint set on exit from a function. The prologue and epilogue
23404 	 attributes allow a compiler to communicate the location(s) to use.  */
23405 
23406       {
23407         if (fde->dw_fde_vms_end_prologue)
23408           add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
23409 	    fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
23410 
23411         if (fde->dw_fde_vms_begin_epilogue)
23412           add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
23413 	    fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
23414       }
23415 #endif
23416 
23417 	}
23418       else
23419 	{
23420 	  /* Generate pubnames entries for the split function code ranges.  */
23421 	  dw_fde_ref fde = fun->fde;
23422 
23423 	  if (fde->dw_fde_second_begin)
23424 	    {
23425 	      if (dwarf_version >= 3 || !dwarf_strict)
23426 		{
23427 		  /* We should use ranges for non-contiguous code section
23428 		     addresses.  Use the actual code range for the initial
23429 		     section, since the HOT/COLD labels might precede an
23430 		     alignment offset.  */
23431 		  bool range_list_added = false;
23432 		  add_ranges_by_labels (subr_die, fde->dw_fde_begin,
23433 					fde->dw_fde_end, &range_list_added,
23434 					false);
23435 		  add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
23436 					fde->dw_fde_second_end,
23437 					&range_list_added, false);
23438 		  if (range_list_added)
23439 		    add_ranges (NULL);
23440 		}
23441 	      else
23442 		{
23443 		  /* There is no real support in DW2 for this .. so we make
23444 		     a work-around.  First, emit the pub name for the segment
23445 		     containing the function label.  Then make and emit a
23446 		     simplified subprogram DIE for the second segment with the
23447 		     name pre-fixed by __hot/cold_sect_of_.  We use the same
23448 		     linkage name for the second die so that gdb will find both
23449 		     sections when given "b foo".  */
23450 		  const char *name = NULL;
23451 		  tree decl_name = DECL_NAME (decl);
23452 		  dw_die_ref seg_die;
23453 
23454 		  /* Do the 'primary' section.   */
23455 		  add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
23456                                       fde->dw_fde_end, false);
23457 
23458 		  /* Build a minimal DIE for the secondary section.  */
23459 		  seg_die = new_die (DW_TAG_subprogram,
23460 				     subr_die->die_parent, decl);
23461 
23462 		  if (TREE_PUBLIC (decl))
23463 		    add_AT_flag (seg_die, DW_AT_external, 1);
23464 
23465 		  if (decl_name != NULL
23466 		      && IDENTIFIER_POINTER (decl_name) != NULL)
23467 		    {
23468 		      name = dwarf2_name (decl, 1);
23469 		      if (! DECL_ARTIFICIAL (decl))
23470 			add_src_coords_attributes (seg_die, decl);
23471 
23472 		      add_linkage_name (seg_die, decl);
23473 		    }
23474 		  gcc_assert (name != NULL);
23475 		  add_pure_or_virtual_attribute (seg_die, decl);
23476 		  if (DECL_ARTIFICIAL (decl))
23477 		    add_AT_flag (seg_die, DW_AT_artificial, 1);
23478 
23479 		  name = concat ("__second_sect_of_", name, NULL);
23480 		  add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
23481                                       fde->dw_fde_second_end, false);
23482 		  add_name_attribute (seg_die, name);
23483 		  if (want_pubnames ())
23484 		    add_pubname_string (name, seg_die);
23485 		}
23486 	    }
23487 	  else
23488            add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
23489                                false);
23490 	}
23491 
23492       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
23493 
23494       /* We define the "frame base" as the function's CFA.  This is more
23495 	 convenient for several reasons: (1) It's stable across the prologue
23496 	 and epilogue, which makes it better than just a frame pointer,
23497 	 (2) With dwarf3, there exists a one-byte encoding that allows us
23498 	 to reference the .debug_frame data by proxy, but failing that,
23499 	 (3) We can at least reuse the code inspection and interpretation
23500 	 code that determines the CFA position at various points in the
23501 	 function.  */
23502       if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
23503 	{
23504 	  dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
23505 	  add_AT_loc (subr_die, DW_AT_frame_base, op);
23506 	}
23507       else
23508 	{
23509 	  dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
23510 	  if (list->dw_loc_next)
23511 	    add_AT_loc_list (subr_die, DW_AT_frame_base, list);
23512 	  else
23513 	    add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
23514 	}
23515 
23516       /* Compute a displacement from the "steady-state frame pointer" to
23517 	 the CFA.  The former is what all stack slots and argument slots
23518 	 will reference in the rtl; the latter is what we've told the
23519 	 debugger about.  We'll need to adjust all frame_base references
23520 	 by this displacement.  */
23521       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
23522 
23523       if (fun->static_chain_decl)
23524 	{
23525 	  /* DWARF requires here a location expression that computes the
23526 	     address of the enclosing subprogram's frame base.  The machinery
23527 	     in tree-nested.c is supposed to store this specific address in the
23528 	     last field of the FRAME record.  */
23529 	  const tree frame_type
23530 	    = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
23531 	  const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
23532 
23533 	  tree fb_expr
23534 	    = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
23535 	  fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
23536 			    fb_expr, fb_decl, NULL_TREE);
23537 
23538 	  add_AT_location_description (subr_die, DW_AT_static_link,
23539 				       loc_list_from_tree (fb_expr, 0, NULL));
23540 	}
23541 
23542       resolve_variable_values ();
23543     }
23544 
23545   /* Generate child dies for template paramaters.  */
23546   if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
23547     gen_generic_params_dies (decl);
23548 
23549   /* Now output descriptions of the arguments for this function. This gets
23550      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
23551      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
23552      `...' at the end of the formal parameter list.  In order to find out if
23553      there was a trailing ellipsis or not, we must instead look at the type
23554      associated with the FUNCTION_DECL.  This will be a node of type
23555      FUNCTION_TYPE. If the chain of type nodes hanging off of this
23556      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
23557      an ellipsis at the end.  */
23558 
23559   /* In the case where we are describing a mere function declaration, all we
23560      need to do here (and all we *can* do here) is to describe the *types* of
23561      its formal parameters.  */
23562   if (debug_info_level <= DINFO_LEVEL_TERSE)
23563     ;
23564   else if (declaration)
23565     gen_formal_types_die (decl, subr_die);
23566   else
23567     {
23568       /* Generate DIEs to represent all known formal parameters.  */
23569       tree parm = DECL_ARGUMENTS (decl);
23570       tree generic_decl = early_dwarf
23571 	? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
23572       tree generic_decl_parm = generic_decl
23573 				? DECL_ARGUMENTS (generic_decl)
23574 				: NULL;
23575 
23576       /* Now we want to walk the list of parameters of the function and
23577 	 emit their relevant DIEs.
23578 
23579 	 We consider the case of DECL being an instance of a generic function
23580 	 as well as it being a normal function.
23581 
23582 	 If DECL is an instance of a generic function we walk the
23583 	 parameters of the generic function declaration _and_ the parameters of
23584 	 DECL itself. This is useful because we want to emit specific DIEs for
23585 	 function parameter packs and those are declared as part of the
23586 	 generic function declaration. In that particular case,
23587 	 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
23588 	 That DIE has children DIEs representing the set of arguments
23589 	 of the pack. Note that the set of pack arguments can be empty.
23590 	 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
23591 	 children DIE.
23592 
23593 	 Otherwise, we just consider the parameters of DECL.  */
23594       while (generic_decl_parm || parm)
23595 	{
23596 	  if (generic_decl_parm
23597 	      && lang_hooks.function_parameter_pack_p (generic_decl_parm))
23598 	    gen_formal_parameter_pack_die (generic_decl_parm,
23599 					   parm, subr_die,
23600 					   &parm);
23601 	  else if (parm)
23602 	    {
23603 	      dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
23604 
23605 	      if (early_dwarf
23606 		  && parm == DECL_ARGUMENTS (decl)
23607 		  && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
23608 		  && parm_die
23609 		  && (dwarf_version >= 3 || !dwarf_strict))
23610 		add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
23611 
23612 	      parm = DECL_CHAIN (parm);
23613 	    }
23614 
23615 	  if (generic_decl_parm)
23616 	    generic_decl_parm = DECL_CHAIN (generic_decl_parm);
23617 	}
23618 
23619       /* Decide whether we need an unspecified_parameters DIE at the end.
23620 	 There are 2 more cases to do this for: 1) the ansi ... declaration -
23621 	 this is detectable when the end of the arg list is not a
23622 	 void_type_node 2) an unprototyped function declaration (not a
23623 	 definition).  This just means that we have no info about the
23624 	 parameters at all.  */
23625       if (early_dwarf)
23626 	{
23627 	  if (prototype_p (TREE_TYPE (decl)))
23628 	    {
23629 	      /* This is the prototyped case, check for....  */
23630 	      if (stdarg_p (TREE_TYPE (decl)))
23631 		gen_unspecified_parameters_die (decl, subr_die);
23632 	    }
23633 	  else if (DECL_INITIAL (decl) == NULL_TREE)
23634 	    gen_unspecified_parameters_die (decl, subr_die);
23635 	}
23636       else if ((subr_die != old_die || old_die_had_no_children)
23637 	       && prototype_p (TREE_TYPE (decl))
23638 	       && stdarg_p (TREE_TYPE (decl)))
23639 	gen_unspecified_parameters_die (decl, subr_die);
23640     }
23641 
23642   if (subr_die != old_die)
23643     /* Add the calling convention attribute if requested.  */
23644     add_calling_convention_attribute (subr_die, decl);
23645 
23646   /* Output Dwarf info for all of the stuff within the body of the function
23647      (if it has one - it may be just a declaration).
23648 
23649      OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
23650      a function.  This BLOCK actually represents the outermost binding contour
23651      for the function, i.e. the contour in which the function's formal
23652      parameters and labels get declared. Curiously, it appears that the front
23653      end doesn't actually put the PARM_DECL nodes for the current function onto
23654      the BLOCK_VARS list for this outer scope, but are strung off of the
23655      DECL_ARGUMENTS list for the function instead.
23656 
23657      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
23658      the LABEL_DECL nodes for the function however, and we output DWARF info
23659      for those in decls_for_scope.  Just within the `outer_scope' there will be
23660      a BLOCK node representing the function's outermost pair of curly braces,
23661      and any blocks used for the base and member initializers of a C++
23662      constructor function.  */
23663   tree outer_scope = DECL_INITIAL (decl);
23664   if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
23665     {
23666       int call_site_note_count = 0;
23667       int tail_call_site_note_count = 0;
23668 
23669       /* Emit a DW_TAG_variable DIE for a named return value.  */
23670       if (DECL_NAME (DECL_RESULT (decl)))
23671 	gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
23672 
23673       /* The first time through decls_for_scope we will generate the
23674 	 DIEs for the locals.  The second time, we fill in the
23675 	 location info.  */
23676       decls_for_scope (outer_scope, subr_die);
23677 
23678       if (call_arg_locations && (!dwarf_strict || dwarf_version >= 5))
23679 	{
23680 	  struct call_arg_loc_node *ca_loc;
23681 	  for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
23682 	    {
23683 	      dw_die_ref die = NULL;
23684 	      rtx tloc = NULL_RTX, tlocc = NULL_RTX;
23685 	      rtx arg, next_arg;
23686 	      tree arg_decl = NULL_TREE;
23687 
23688 	      for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
23689 			  ? XEXP (ca_loc->call_arg_loc_note, 0)
23690 			  : NULL_RTX);
23691 		   arg; arg = next_arg)
23692 		{
23693 		  dw_loc_descr_ref reg, val;
23694 		  machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
23695 		  dw_die_ref cdie, tdie = NULL;
23696 
23697 		  next_arg = XEXP (arg, 1);
23698 		  if (REG_P (XEXP (XEXP (arg, 0), 0))
23699 		      && next_arg
23700 		      && MEM_P (XEXP (XEXP (next_arg, 0), 0))
23701 		      && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
23702 		      && REGNO (XEXP (XEXP (arg, 0), 0))
23703 			 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
23704 		    next_arg = XEXP (next_arg, 1);
23705 		  if (mode == VOIDmode)
23706 		    {
23707 		      mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
23708 		      if (mode == VOIDmode)
23709 			mode = GET_MODE (XEXP (arg, 0));
23710 		    }
23711 		  if (mode == VOIDmode || mode == BLKmode)
23712 		    continue;
23713 		  /* Get dynamic information about call target only if we
23714 		     have no static information: we cannot generate both
23715 		     DW_AT_call_origin and DW_AT_call_target
23716 		     attributes.  */
23717 		  if (ca_loc->symbol_ref == NULL_RTX)
23718 		    {
23719 		      if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
23720 			{
23721 			  tloc = XEXP (XEXP (arg, 0), 1);
23722 			  continue;
23723 			}
23724 		      else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
23725 			       && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
23726 			{
23727 			  tlocc = XEXP (XEXP (arg, 0), 1);
23728 			  continue;
23729 			}
23730 		    }
23731 		  reg = NULL;
23732 		  if (REG_P (XEXP (XEXP (arg, 0), 0)))
23733 		    reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
23734 					      VAR_INIT_STATUS_INITIALIZED);
23735 		  else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
23736 		    {
23737 		      rtx mem = XEXP (XEXP (arg, 0), 0);
23738 		      reg = mem_loc_descriptor (XEXP (mem, 0),
23739 						get_address_mode (mem),
23740 						GET_MODE (mem),
23741 						VAR_INIT_STATUS_INITIALIZED);
23742 		    }
23743 		  else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
23744 			   == DEBUG_PARAMETER_REF)
23745 		    {
23746 		      tree tdecl
23747 			= DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
23748 		      tdie = lookup_decl_die (tdecl);
23749 		      if (tdie == NULL)
23750 			continue;
23751 		      arg_decl = tdecl;
23752 		    }
23753 		  else
23754 		    continue;
23755 		  if (reg == NULL
23756 		      && GET_CODE (XEXP (XEXP (arg, 0), 0))
23757 			 != DEBUG_PARAMETER_REF)
23758 		    continue;
23759 		  val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
23760 					    VOIDmode,
23761 					    VAR_INIT_STATUS_INITIALIZED);
23762 		  if (val == NULL)
23763 		    continue;
23764 		  if (die == NULL)
23765 		    die = gen_call_site_die (decl, subr_die, ca_loc);
23766 		  cdie = new_die (dwarf_TAG (DW_TAG_call_site_parameter), die,
23767 				  NULL_TREE);
23768 		  add_desc_attribute (cdie, arg_decl);
23769 		  if (reg != NULL)
23770 		    add_AT_loc (cdie, DW_AT_location, reg);
23771 		  else if (tdie != NULL)
23772 		    add_AT_die_ref (cdie, dwarf_AT (DW_AT_call_parameter),
23773 				    tdie);
23774 		  add_AT_loc (cdie, dwarf_AT (DW_AT_call_value), val);
23775 		  if (next_arg != XEXP (arg, 1))
23776 		    {
23777 		      mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
23778 		      if (mode == VOIDmode)
23779 			mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
23780 		      val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
23781 							    0), 1),
23782 						mode, VOIDmode,
23783 						VAR_INIT_STATUS_INITIALIZED);
23784 		      if (val != NULL)
23785 			add_AT_loc (cdie, dwarf_AT (DW_AT_call_data_value),
23786 				    val);
23787 		    }
23788 		}
23789 	      if (die == NULL
23790 		  && (ca_loc->symbol_ref || tloc))
23791 		die = gen_call_site_die (decl, subr_die, ca_loc);
23792 	      if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
23793 		{
23794 		  dw_loc_descr_ref tval = NULL;
23795 
23796 		  if (tloc != NULL_RTX)
23797 		    tval = mem_loc_descriptor (tloc,
23798 					       GET_MODE (tloc) == VOIDmode
23799 					       ? Pmode : GET_MODE (tloc),
23800 					       VOIDmode,
23801 					       VAR_INIT_STATUS_INITIALIZED);
23802 		  if (tval)
23803 		    add_AT_loc (die, dwarf_AT (DW_AT_call_target), tval);
23804 		  else if (tlocc != NULL_RTX)
23805 		    {
23806 		      tval = mem_loc_descriptor (tlocc,
23807 						 GET_MODE (tlocc) == VOIDmode
23808 						 ? Pmode : GET_MODE (tlocc),
23809 						 VOIDmode,
23810 						 VAR_INIT_STATUS_INITIALIZED);
23811 		      if (tval)
23812 			add_AT_loc (die,
23813 				    dwarf_AT (DW_AT_call_target_clobbered),
23814 				    tval);
23815 		    }
23816 		}
23817 	      if (die != NULL)
23818 		{
23819 		  call_site_note_count++;
23820 		  if (ca_loc->tail_call_p)
23821 		    tail_call_site_note_count++;
23822 		}
23823 	    }
23824 	}
23825       call_arg_locations = NULL;
23826       call_arg_loc_last = NULL;
23827       if (tail_call_site_count >= 0
23828 	  && tail_call_site_count == tail_call_site_note_count
23829 	  && (!dwarf_strict || dwarf_version >= 5))
23830 	{
23831 	  if (call_site_count >= 0
23832 	      && call_site_count == call_site_note_count)
23833 	    add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_calls), 1);
23834 	  else
23835 	    add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_tail_calls), 1);
23836 	}
23837       call_site_count = -1;
23838       tail_call_site_count = -1;
23839     }
23840 
23841   /* Mark used types after we have created DIEs for the functions scopes.  */
23842   premark_used_types (DECL_STRUCT_FUNCTION (decl));
23843 }
23844 
23845 /* Returns a hash value for X (which really is a die_struct).  */
23846 
23847 hashval_t
hash(die_struct * d)23848 block_die_hasher::hash (die_struct *d)
23849 {
23850   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
23851 }
23852 
23853 /* Return nonzero if decl_id and die_parent of die_struct X is the same
23854    as decl_id and die_parent of die_struct Y.  */
23855 
23856 bool
equal(die_struct * x,die_struct * y)23857 block_die_hasher::equal (die_struct *x, die_struct *y)
23858 {
23859   return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
23860 }
23861 
23862 /* Hold information about markers for inlined entry points.  */
23863 struct GTY ((for_user)) inline_entry_data
23864 {
23865   /* The block that's the inlined_function_outer_scope for an inlined
23866      function.  */
23867   tree block;
23868 
23869   /* The label at the inlined entry point.  */
23870   const char *label_pfx;
23871   unsigned int label_num;
23872 
23873   /* The view number to be used as the inlined entry point.  */
23874   var_loc_view view;
23875 };
23876 
23877 struct inline_entry_data_hasher : ggc_ptr_hash <inline_entry_data>
23878 {
23879   typedef tree compare_type;
23880   static inline hashval_t hash (const inline_entry_data *);
23881   static inline bool equal (const inline_entry_data *, const_tree);
23882 };
23883 
23884 /* Hash table routines for inline_entry_data.  */
23885 
23886 inline hashval_t
hash(const inline_entry_data * data)23887 inline_entry_data_hasher::hash (const inline_entry_data *data)
23888 {
23889   return htab_hash_pointer (data->block);
23890 }
23891 
23892 inline bool
equal(const inline_entry_data * data,const_tree block)23893 inline_entry_data_hasher::equal (const inline_entry_data *data,
23894 				 const_tree block)
23895 {
23896   return data->block == block;
23897 }
23898 
23899 /* Inlined entry points pending DIE creation in this compilation unit.  */
23900 
23901 static GTY(()) hash_table<inline_entry_data_hasher> *inline_entry_data_table;
23902 
23903 
23904 /* Return TRUE if DECL, which may have been previously generated as
23905    OLD_DIE, is a candidate for a DW_AT_specification.  DECLARATION is
23906    true if decl (or its origin) is either an extern declaration or a
23907    class/namespace scoped declaration.
23908 
23909    The declare_in_namespace support causes us to get two DIEs for one
23910    variable, both of which are declarations.  We want to avoid
23911    considering one to be a specification, so we must test for
23912    DECLARATION and DW_AT_declaration.  */
23913 static inline bool
decl_will_get_specification_p(dw_die_ref old_die,tree decl,bool declaration)23914 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
23915 {
23916   return (old_die && TREE_STATIC (decl) && !declaration
23917 	  && get_AT_flag (old_die, DW_AT_declaration) == 1);
23918 }
23919 
23920 /* Return true if DECL is a local static.  */
23921 
23922 static inline bool
local_function_static(tree decl)23923 local_function_static (tree decl)
23924 {
23925   gcc_assert (VAR_P (decl));
23926   return TREE_STATIC (decl)
23927     && DECL_CONTEXT (decl)
23928     && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
23929 }
23930 
23931 /* Return true iff DECL overrides (presumably completes) the type of
23932    OLD_DIE within CONTEXT_DIE.  */
23933 
23934 static bool
override_type_for_decl_p(tree decl,dw_die_ref old_die,dw_die_ref context_die)23935 override_type_for_decl_p (tree decl, dw_die_ref old_die,
23936 			  dw_die_ref context_die)
23937 {
23938   tree type = TREE_TYPE (decl);
23939   int cv_quals;
23940 
23941   if (decl_by_reference_p (decl))
23942     {
23943       type = TREE_TYPE (type);
23944       cv_quals = TYPE_UNQUALIFIED;
23945     }
23946   else
23947     cv_quals = decl_quals (decl);
23948 
23949   dw_die_ref type_die = modified_type_die (type,
23950 					   cv_quals | TYPE_QUALS (type),
23951 					   false,
23952 					   context_die);
23953 
23954   dw_die_ref old_type_die = get_AT_ref (old_die, DW_AT_type);
23955 
23956   return type_die != old_type_die;
23957 }
23958 
23959 /* Generate a DIE to represent a declared data object.
23960    Either DECL or ORIGIN must be non-null.  */
23961 
23962 static void
gen_variable_die(tree decl,tree origin,dw_die_ref context_die)23963 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
23964 {
23965   HOST_WIDE_INT off = 0;
23966   tree com_decl;
23967   tree decl_or_origin = decl ? decl : origin;
23968   tree ultimate_origin;
23969   dw_die_ref var_die;
23970   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
23971   bool declaration = (DECL_EXTERNAL (decl_or_origin)
23972 		      || class_or_namespace_scope_p (context_die));
23973   bool specialization_p = false;
23974   bool no_linkage_name = false;
23975 
23976   /* While C++ inline static data members have definitions inside of the
23977      class, force the first DIE to be a declaration, then let gen_member_die
23978      reparent it to the class context and call gen_variable_die again
23979      to create the outside of the class DIE for the definition.  */
23980   if (!declaration
23981       && old_die == NULL
23982       && decl
23983       && DECL_CONTEXT (decl)
23984       && TYPE_P (DECL_CONTEXT (decl))
23985       && lang_hooks.decls.decl_dwarf_attribute (decl, DW_AT_inline) != -1)
23986     {
23987       declaration = true;
23988       if (dwarf_version < 5)
23989 	no_linkage_name = true;
23990     }
23991 
23992   ultimate_origin = decl_ultimate_origin (decl_or_origin);
23993   if (decl || ultimate_origin)
23994     origin = ultimate_origin;
23995   com_decl = fortran_common (decl_or_origin, &off);
23996 
23997   /* Symbol in common gets emitted as a child of the common block, in the form
23998      of a data member.  */
23999   if (com_decl)
24000     {
24001       dw_die_ref com_die;
24002       dw_loc_list_ref loc = NULL;
24003       die_node com_die_arg;
24004 
24005       var_die = lookup_decl_die (decl_or_origin);
24006       if (var_die)
24007 	{
24008 	  if (! early_dwarf && get_AT (var_die, DW_AT_location) == NULL)
24009 	    {
24010 	      loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
24011 	      if (loc)
24012 		{
24013 		  if (off)
24014 		    {
24015 		      /* Optimize the common case.  */
24016 		      if (single_element_loc_list_p (loc)
24017 			  && loc->expr->dw_loc_opc == DW_OP_addr
24018 			  && loc->expr->dw_loc_next == NULL
24019 			  && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
24020 			     == SYMBOL_REF)
24021 			{
24022 			  rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
24023 			  loc->expr->dw_loc_oprnd1.v.val_addr
24024 			    = plus_constant (GET_MODE (x), x , off);
24025 			}
24026 		      else
24027 			loc_list_plus_const (loc, off);
24028 		    }
24029 		  add_AT_location_description (var_die, DW_AT_location, loc);
24030 		  remove_AT (var_die, DW_AT_declaration);
24031 		}
24032 	    }
24033 	  return;
24034 	}
24035 
24036       if (common_block_die_table == NULL)
24037 	common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
24038 
24039       com_die_arg.decl_id = DECL_UID (com_decl);
24040       com_die_arg.die_parent = context_die;
24041       com_die = common_block_die_table->find (&com_die_arg);
24042       if (! early_dwarf)
24043 	loc = loc_list_from_tree (com_decl, 2, NULL);
24044       if (com_die == NULL)
24045 	{
24046 	  const char *cnam
24047 	    = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
24048 	  die_node **slot;
24049 
24050 	  com_die = new_die (DW_TAG_common_block, context_die, decl);
24051 	  add_name_and_src_coords_attributes (com_die, com_decl);
24052 	  if (loc)
24053 	    {
24054 	      add_AT_location_description (com_die, DW_AT_location, loc);
24055 	      /* Avoid sharing the same loc descriptor between
24056 		 DW_TAG_common_block and DW_TAG_variable.  */
24057 	      loc = loc_list_from_tree (com_decl, 2, NULL);
24058 	    }
24059 	  else if (DECL_EXTERNAL (decl_or_origin))
24060 	    add_AT_flag (com_die, DW_AT_declaration, 1);
24061 	  if (want_pubnames ())
24062 	    add_pubname_string (cnam, com_die); /* ??? needed? */
24063 	  com_die->decl_id = DECL_UID (com_decl);
24064 	  slot = common_block_die_table->find_slot (com_die, INSERT);
24065 	  *slot = com_die;
24066 	}
24067       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
24068 	{
24069 	  add_AT_location_description (com_die, DW_AT_location, loc);
24070 	  loc = loc_list_from_tree (com_decl, 2, NULL);
24071 	  remove_AT (com_die, DW_AT_declaration);
24072 	}
24073       var_die = new_die (DW_TAG_variable, com_die, decl);
24074       add_name_and_src_coords_attributes (var_die, decl_or_origin);
24075       add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
24076 			  decl_quals (decl_or_origin), false,
24077 			  context_die);
24078       add_alignment_attribute (var_die, decl);
24079       add_AT_flag (var_die, DW_AT_external, 1);
24080       if (loc)
24081 	{
24082 	  if (off)
24083 	    {
24084 	      /* Optimize the common case.  */
24085 	      if (single_element_loc_list_p (loc)
24086                   && loc->expr->dw_loc_opc == DW_OP_addr
24087 		  && loc->expr->dw_loc_next == NULL
24088 		  && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
24089 		{
24090 		  rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
24091 		  loc->expr->dw_loc_oprnd1.v.val_addr
24092 		    = plus_constant (GET_MODE (x), x, off);
24093 		}
24094 	      else
24095 		loc_list_plus_const (loc, off);
24096 	    }
24097 	  add_AT_location_description (var_die, DW_AT_location, loc);
24098 	}
24099       else if (DECL_EXTERNAL (decl_or_origin))
24100 	add_AT_flag (var_die, DW_AT_declaration, 1);
24101       if (decl)
24102 	equate_decl_number_to_die (decl, var_die);
24103       return;
24104     }
24105 
24106   if (old_die)
24107     {
24108       if (declaration)
24109 	{
24110 	  /* A declaration that has been previously dumped, needs no
24111 	     further annotations, since it doesn't need location on
24112 	     the second pass.  */
24113 	  return;
24114 	}
24115       else if (decl_will_get_specification_p (old_die, decl, declaration)
24116 	       && !get_AT (old_die, DW_AT_specification))
24117 	{
24118 	  /* Fall-thru so we can make a new variable die along with a
24119 	     DW_AT_specification.  */
24120 	}
24121       else if (origin && old_die->die_parent != context_die)
24122 	{
24123 	  /* If we will be creating an inlined instance, we need a
24124 	     new DIE that will get annotated with
24125 	     DW_AT_abstract_origin.  */
24126 	  gcc_assert (!DECL_ABSTRACT_P (decl));
24127 	}
24128       else
24129 	{
24130 	  /* If a DIE was dumped early, it still needs location info.
24131 	     Skip to where we fill the location bits.  */
24132 	  var_die = old_die;
24133 
24134 	  /* ???  In LTRANS we cannot annotate early created variably
24135 	     modified type DIEs without copying them and adjusting all
24136 	     references to them.  Thus we dumped them again.  Also add a
24137 	     reference to them but beware of -g0 compile and -g link
24138 	     in which case the reference will be already present.  */
24139 	  tree type = TREE_TYPE (decl_or_origin);
24140 	  if (in_lto_p
24141 	      && ! get_AT (var_die, DW_AT_type)
24142 	      && variably_modified_type_p
24143 		   (type, decl_function_context (decl_or_origin)))
24144 	    {
24145 	      if (decl_by_reference_p (decl_or_origin))
24146 		add_type_attribute (var_die, TREE_TYPE (type),
24147 				    TYPE_UNQUALIFIED, false, context_die);
24148 	      else
24149 		add_type_attribute (var_die, type, decl_quals (decl_or_origin),
24150 				    false, context_die);
24151 	    }
24152 
24153 	  goto gen_variable_die_location;
24154 	}
24155     }
24156 
24157   /* For static data members, the declaration in the class is supposed
24158      to have DW_TAG_member tag in DWARF{3,4} and we emit it for compatibility
24159      also in DWARF2; the specification should still be DW_TAG_variable
24160      referencing the DW_TAG_member DIE.  */
24161   if (declaration && class_scope_p (context_die) && dwarf_version < 5)
24162     var_die = new_die (DW_TAG_member, context_die, decl);
24163   else
24164     var_die = new_die (DW_TAG_variable, context_die, decl);
24165 
24166   if (origin != NULL)
24167     add_abstract_origin_attribute (var_die, origin);
24168 
24169   /* Loop unrolling can create multiple blocks that refer to the same
24170      static variable, so we must test for the DW_AT_declaration flag.
24171 
24172      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
24173      copy decls and set the DECL_ABSTRACT_P flag on them instead of
24174      sharing them.
24175 
24176      ??? Duplicated blocks have been rewritten to use .debug_ranges.  */
24177   else if (decl_will_get_specification_p (old_die, decl, declaration))
24178     {
24179       /* This is a definition of a C++ class level static.  */
24180       add_AT_specification (var_die, old_die);
24181       specialization_p = true;
24182       if (DECL_NAME (decl))
24183 	{
24184 	  expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
24185 	  struct dwarf_file_data * file_index = lookup_filename (s.file);
24186 
24187 	  if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
24188 	    add_AT_file (var_die, DW_AT_decl_file, file_index);
24189 
24190 	  if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
24191 	    add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
24192 
24193 	  if (debug_column_info
24194 	      && s.column
24195 	      && (get_AT_unsigned (old_die, DW_AT_decl_column)
24196 		  != (unsigned) s.column))
24197 	    add_AT_unsigned (var_die, DW_AT_decl_column, s.column);
24198 
24199 	  if (old_die->die_tag == DW_TAG_member)
24200 	    add_linkage_name (var_die, decl);
24201 	}
24202     }
24203   else
24204     add_name_and_src_coords_attributes (var_die, decl, no_linkage_name);
24205 
24206   if ((origin == NULL && !specialization_p)
24207       || (origin != NULL
24208 	  && !DECL_ABSTRACT_P (decl_or_origin)
24209 	  && variably_modified_type_p (TREE_TYPE (decl_or_origin),
24210 				       decl_function_context
24211 				       (decl_or_origin)))
24212       || (old_die && specialization_p
24213 	  && override_type_for_decl_p (decl_or_origin, old_die, context_die)))
24214     {
24215       tree type = TREE_TYPE (decl_or_origin);
24216 
24217       if (decl_by_reference_p (decl_or_origin))
24218 	add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
24219 			    context_die);
24220       else
24221 	add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
24222 			    context_die);
24223     }
24224 
24225   if (origin == NULL && !specialization_p)
24226     {
24227       if (TREE_PUBLIC (decl))
24228 	add_AT_flag (var_die, DW_AT_external, 1);
24229 
24230       if (DECL_ARTIFICIAL (decl))
24231 	add_AT_flag (var_die, DW_AT_artificial, 1);
24232 
24233       add_alignment_attribute (var_die, decl);
24234 
24235       add_accessibility_attribute (var_die, decl);
24236     }
24237 
24238   if (declaration)
24239     add_AT_flag (var_die, DW_AT_declaration, 1);
24240 
24241   if (decl && (DECL_ABSTRACT_P (decl)
24242 	       || !old_die || is_declaration_die (old_die)))
24243     equate_decl_number_to_die (decl, var_die);
24244 
24245  gen_variable_die_location:
24246   if (! declaration
24247       && (! DECL_ABSTRACT_P (decl_or_origin)
24248 	  /* Local static vars are shared between all clones/inlines,
24249 	     so emit DW_AT_location on the abstract DIE if DECL_RTL is
24250 	     already set.  */
24251 	  || (VAR_P (decl_or_origin)
24252 	      && TREE_STATIC (decl_or_origin)
24253 	      && DECL_RTL_SET_P (decl_or_origin))))
24254     {
24255       if (early_dwarf)
24256 	add_pubname (decl_or_origin, var_die);
24257       else
24258 	add_location_or_const_value_attribute (var_die, decl_or_origin,
24259 					       decl == NULL);
24260     }
24261   else
24262     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
24263 
24264   if ((dwarf_version >= 4 || !dwarf_strict)
24265       && lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
24266 						DW_AT_const_expr) == 1
24267       && !get_AT (var_die, DW_AT_const_expr)
24268       && !specialization_p)
24269     add_AT_flag (var_die, DW_AT_const_expr, 1);
24270 
24271   if (!dwarf_strict)
24272     {
24273       int inl = lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
24274 						       DW_AT_inline);
24275       if (inl != -1
24276 	  && !get_AT (var_die, DW_AT_inline)
24277 	  && !specialization_p)
24278 	add_AT_unsigned (var_die, DW_AT_inline, inl);
24279     }
24280 }
24281 
24282 /* Generate a DIE to represent a named constant.  */
24283 
24284 static void
gen_const_die(tree decl,dw_die_ref context_die)24285 gen_const_die (tree decl, dw_die_ref context_die)
24286 {
24287   dw_die_ref const_die;
24288   tree type = TREE_TYPE (decl);
24289 
24290   const_die = lookup_decl_die (decl);
24291   if (const_die)
24292     return;
24293 
24294   const_die = new_die (DW_TAG_constant, context_die, decl);
24295   equate_decl_number_to_die (decl, const_die);
24296   add_name_and_src_coords_attributes (const_die, decl);
24297   add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
24298   if (TREE_PUBLIC (decl))
24299     add_AT_flag (const_die, DW_AT_external, 1);
24300   if (DECL_ARTIFICIAL (decl))
24301     add_AT_flag (const_die, DW_AT_artificial, 1);
24302   tree_add_const_value_attribute_for_decl (const_die, decl);
24303 }
24304 
24305 /* Generate a DIE to represent a label identifier.  */
24306 
24307 static void
gen_label_die(tree decl,dw_die_ref context_die)24308 gen_label_die (tree decl, dw_die_ref context_die)
24309 {
24310   tree origin = decl_ultimate_origin (decl);
24311   dw_die_ref lbl_die = lookup_decl_die (decl);
24312   rtx insn;
24313   char label[MAX_ARTIFICIAL_LABEL_BYTES];
24314 
24315   if (!lbl_die)
24316     {
24317       lbl_die = new_die (DW_TAG_label, context_die, decl);
24318       equate_decl_number_to_die (decl, lbl_die);
24319 
24320       if (origin != NULL)
24321 	add_abstract_origin_attribute (lbl_die, origin);
24322       else
24323 	add_name_and_src_coords_attributes (lbl_die, decl);
24324     }
24325 
24326   if (DECL_ABSTRACT_P (decl))
24327     equate_decl_number_to_die (decl, lbl_die);
24328   else if (! early_dwarf)
24329     {
24330       insn = DECL_RTL_IF_SET (decl);
24331 
24332       /* Deleted labels are programmer specified labels which have been
24333 	 eliminated because of various optimizations.  We still emit them
24334 	 here so that it is possible to put breakpoints on them.  */
24335       if (insn
24336 	  && (LABEL_P (insn)
24337 	      || ((NOTE_P (insn)
24338 	           && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
24339 	{
24340 	  /* When optimization is enabled (via -O) some parts of the compiler
24341 	     (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
24342 	     represent source-level labels which were explicitly declared by
24343 	     the user.  This really shouldn't be happening though, so catch
24344 	     it if it ever does happen.  */
24345 	  gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
24346 
24347 	  ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
24348           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
24349 	}
24350       else if (insn
24351 	       && NOTE_P (insn)
24352 	       && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
24353 	       && CODE_LABEL_NUMBER (insn) != -1)
24354 	{
24355 	  ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
24356           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
24357 	}
24358     }
24359 }
24360 
24361 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
24362    attributes to the DIE for a block STMT, to describe where the inlined
24363    function was called from.  This is similar to add_src_coords_attributes.  */
24364 
24365 static inline void
add_call_src_coords_attributes(tree stmt,dw_die_ref die)24366 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
24367 {
24368   /* We can end up with BUILTINS_LOCATION here.  */
24369   if (RESERVED_LOCATION_P (BLOCK_SOURCE_LOCATION (stmt)))
24370     return;
24371 
24372   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
24373 
24374   if (dwarf_version >= 3 || !dwarf_strict)
24375     {
24376       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
24377       add_AT_unsigned (die, DW_AT_call_line, s.line);
24378       if (debug_column_info && s.column)
24379 	add_AT_unsigned (die, DW_AT_call_column, s.column);
24380     }
24381 }
24382 
24383 
24384 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
24385    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
24386 
24387 static inline void
add_high_low_attributes(tree stmt,dw_die_ref die)24388 add_high_low_attributes (tree stmt, dw_die_ref die)
24389 {
24390   char label[MAX_ARTIFICIAL_LABEL_BYTES];
24391 
24392   if (inline_entry_data **iedp
24393       = !inline_entry_data_table ? NULL
24394       : inline_entry_data_table->find_slot_with_hash (stmt,
24395 						      htab_hash_pointer (stmt),
24396 						      NO_INSERT))
24397     {
24398       inline_entry_data *ied = *iedp;
24399       gcc_assert (MAY_HAVE_DEBUG_MARKER_INSNS);
24400       gcc_assert (debug_inline_points);
24401       gcc_assert (inlined_function_outer_scope_p (stmt));
24402 
24403       ASM_GENERATE_INTERNAL_LABEL (label, ied->label_pfx, ied->label_num);
24404       add_AT_lbl_id (die, DW_AT_entry_pc, label);
24405 
24406       if (debug_variable_location_views && !ZERO_VIEW_P (ied->view)
24407 	  && !dwarf_strict)
24408 	{
24409 	  if (!output_asm_line_debug_info ())
24410 	    add_AT_unsigned (die, DW_AT_GNU_entry_view, ied->view);
24411 	  else
24412 	    {
24413 	      ASM_GENERATE_INTERNAL_LABEL (label, "LVU", ied->view);
24414 	      /* FIXME: this will resolve to a small number.  Could we
24415 		 possibly emit smaller data?  Ideally we'd emit a
24416 		 uleb128, but that would make the size of DIEs
24417 		 impossible for the compiler to compute, since it's
24418 		 the assembler that computes the value of the view
24419 		 label in this case.  Ideally, we'd have a single form
24420 		 encompassing both the address and the view, and
24421 		 indirecting them through a table might make things
24422 		 easier, but even that would be more wasteful,
24423 		 space-wise, than what we have now.  */
24424 	      add_AT_symview (die, DW_AT_GNU_entry_view, label);
24425 	    }
24426 	}
24427 
24428       inline_entry_data_table->clear_slot (iedp);
24429     }
24430 
24431   if (BLOCK_FRAGMENT_CHAIN (stmt)
24432       && (dwarf_version >= 3 || !dwarf_strict))
24433     {
24434       tree chain, superblock = NULL_TREE;
24435       dw_die_ref pdie;
24436       dw_attr_node *attr = NULL;
24437 
24438       if (!debug_inline_points && inlined_function_outer_scope_p (stmt))
24439 	{
24440 	  ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
24441 				       BLOCK_NUMBER (stmt));
24442           add_AT_lbl_id (die, DW_AT_entry_pc, label);
24443 	}
24444 
24445       /* Optimize duplicate .debug_ranges lists or even tails of
24446 	 lists.  If this BLOCK has same ranges as its supercontext,
24447 	 lookup DW_AT_ranges attribute in the supercontext (and
24448 	 recursively so), verify that the ranges_table contains the
24449 	 right values and use it instead of adding a new .debug_range.  */
24450       for (chain = stmt, pdie = die;
24451 	   BLOCK_SAME_RANGE (chain);
24452 	   chain = BLOCK_SUPERCONTEXT (chain))
24453 	{
24454 	  dw_attr_node *new_attr;
24455 
24456 	  pdie = pdie->die_parent;
24457 	  if (pdie == NULL)
24458 	    break;
24459 	  if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
24460 	    break;
24461 	  new_attr = get_AT (pdie, DW_AT_ranges);
24462 	  if (new_attr == NULL
24463 	      || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
24464 	    break;
24465 	  attr = new_attr;
24466 	  superblock = BLOCK_SUPERCONTEXT (chain);
24467 	}
24468       if (attr != NULL
24469 	  && ((*ranges_table)[attr->dw_attr_val.v.val_offset].num
24470 	      == (int)BLOCK_NUMBER (superblock))
24471 	  && BLOCK_FRAGMENT_CHAIN (superblock))
24472 	{
24473 	  unsigned long off = attr->dw_attr_val.v.val_offset;
24474 	  unsigned long supercnt = 0, thiscnt = 0;
24475 	  for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
24476 	       chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
24477 	    {
24478 	      ++supercnt;
24479 	      gcc_checking_assert ((*ranges_table)[off + supercnt].num
24480 				   == (int)BLOCK_NUMBER (chain));
24481 	    }
24482 	  gcc_checking_assert ((*ranges_table)[off + supercnt + 1].num == 0);
24483 	  for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
24484 	       chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
24485 	    ++thiscnt;
24486 	  gcc_assert (supercnt >= thiscnt);
24487 	  add_AT_range_list (die, DW_AT_ranges, off + supercnt - thiscnt,
24488 			     false);
24489 	  note_rnglist_head (off + supercnt - thiscnt);
24490 	  return;
24491 	}
24492 
24493       unsigned int offset = add_ranges (stmt, true);
24494       add_AT_range_list (die, DW_AT_ranges, offset, false);
24495       note_rnglist_head (offset);
24496 
24497       bool prev_in_cold = BLOCK_IN_COLD_SECTION_P (stmt);
24498       chain = BLOCK_FRAGMENT_CHAIN (stmt);
24499       do
24500 	{
24501 	  add_ranges (chain, prev_in_cold != BLOCK_IN_COLD_SECTION_P (chain));
24502 	  prev_in_cold = BLOCK_IN_COLD_SECTION_P (chain);
24503 	  chain = BLOCK_FRAGMENT_CHAIN (chain);
24504 	}
24505       while (chain);
24506       add_ranges (NULL);
24507     }
24508   else
24509     {
24510       char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
24511       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
24512 				   BLOCK_NUMBER (stmt));
24513       ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
24514 				   BLOCK_NUMBER (stmt));
24515       add_AT_low_high_pc (die, label, label_high, false);
24516     }
24517 }
24518 
24519 /* Generate a DIE for a lexical block.  */
24520 
24521 static void
gen_lexical_block_die(tree stmt,dw_die_ref context_die)24522 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
24523 {
24524   dw_die_ref old_die = lookup_block_die (stmt);
24525   dw_die_ref stmt_die = NULL;
24526   if (!old_die)
24527     {
24528       stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
24529       equate_block_to_die (stmt, stmt_die);
24530     }
24531 
24532   if (BLOCK_ABSTRACT_ORIGIN (stmt))
24533     {
24534       /* If this is an inlined or conrecte instance, create a new lexical
24535 	 die for anything below to attach DW_AT_abstract_origin to.  */
24536       if (old_die)
24537 	stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
24538 
24539       tree origin = block_ultimate_origin (stmt);
24540       if (origin != NULL_TREE && (origin != stmt || old_die))
24541 	add_abstract_origin_attribute (stmt_die, origin);
24542 
24543       old_die = NULL;
24544     }
24545 
24546   if (old_die)
24547     stmt_die = old_die;
24548 
24549   /* A non abstract block whose blocks have already been reordered
24550      should have the instruction range for this block.  If so, set the
24551      high/low attributes.  */
24552   if (!early_dwarf && TREE_ASM_WRITTEN (stmt))
24553     {
24554       gcc_assert (stmt_die);
24555       add_high_low_attributes (stmt, stmt_die);
24556     }
24557 
24558   decls_for_scope (stmt, stmt_die);
24559 }
24560 
24561 /* Generate a DIE for an inlined subprogram.  */
24562 
24563 static void
gen_inlined_subroutine_die(tree stmt,dw_die_ref context_die)24564 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
24565 {
24566   tree decl = block_ultimate_origin (stmt);
24567 
24568   /* Make sure any inlined functions are known to be inlineable.  */
24569   gcc_checking_assert (DECL_ABSTRACT_P (decl)
24570 		       || cgraph_function_possibly_inlined_p (decl));
24571 
24572   dw_die_ref subr_die = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
24573 
24574   if (call_arg_locations || debug_inline_points)
24575     equate_block_to_die (stmt, subr_die);
24576   add_abstract_origin_attribute (subr_die, decl);
24577   if (TREE_ASM_WRITTEN (stmt))
24578     add_high_low_attributes (stmt, subr_die);
24579   add_call_src_coords_attributes (stmt, subr_die);
24580 
24581   /* The inliner creates an extra BLOCK for the parameter setup,
24582      we want to merge that with the actual outermost BLOCK of the
24583      inlined function to avoid duplicate locals in consumers.
24584      Do that by doing the recursion to subblocks on the single subblock
24585      of STMT.  */
24586   bool unwrap_one = false;
24587   if (BLOCK_SUBBLOCKS (stmt) && !BLOCK_CHAIN (BLOCK_SUBBLOCKS (stmt)))
24588     {
24589       tree origin = block_ultimate_origin (BLOCK_SUBBLOCKS (stmt));
24590       if (origin
24591 	  && TREE_CODE (origin) == BLOCK
24592 	  && BLOCK_SUPERCONTEXT (origin) == decl)
24593 	unwrap_one = true;
24594     }
24595   decls_for_scope (stmt, subr_die, !unwrap_one);
24596   if (unwrap_one)
24597     decls_for_scope (BLOCK_SUBBLOCKS (stmt), subr_die);
24598 }
24599 
24600 /* Generate a DIE for a field in a record, or structure.  CTX is required: see
24601    the comment for VLR_CONTEXT.  */
24602 
24603 static void
gen_field_die(tree decl,struct vlr_context * ctx,dw_die_ref context_die)24604 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
24605 {
24606   dw_die_ref decl_die;
24607 
24608   if (TREE_TYPE (decl) == error_mark_node)
24609     return;
24610 
24611   decl_die = new_die (DW_TAG_member, context_die, decl);
24612   add_name_and_src_coords_attributes (decl_die, decl);
24613   add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
24614 		      TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
24615 		      context_die);
24616 
24617   if (DECL_BIT_FIELD_TYPE (decl))
24618     {
24619       add_byte_size_attribute (decl_die, decl);
24620       add_bit_size_attribute (decl_die, decl);
24621       add_bit_offset_attribute (decl_die, decl);
24622     }
24623 
24624   add_alignment_attribute (decl_die, decl);
24625 
24626   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
24627     add_data_member_location_attribute (decl_die, decl, ctx);
24628 
24629   if (DECL_ARTIFICIAL (decl))
24630     add_AT_flag (decl_die, DW_AT_artificial, 1);
24631 
24632   add_accessibility_attribute (decl_die, decl);
24633 
24634   /* Equate decl number to die, so that we can look up this decl later on.  */
24635   equate_decl_number_to_die (decl, decl_die);
24636 }
24637 
24638 /* Generate a DIE for a pointer to a member type.  TYPE can be an
24639    OFFSET_TYPE, for a pointer to data member, or a RECORD_TYPE, for a
24640    pointer to member function.  */
24641 
24642 static void
gen_ptr_to_mbr_type_die(tree type,dw_die_ref context_die)24643 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
24644 {
24645   if (lookup_type_die (type))
24646     return;
24647 
24648   dw_die_ref ptr_die = new_die (DW_TAG_ptr_to_member_type,
24649 				scope_die_for (type, context_die), type);
24650 
24651   equate_type_number_to_die (type, ptr_die);
24652   add_AT_die_ref (ptr_die, DW_AT_containing_type,
24653 		  lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
24654   add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
24655 		      context_die);
24656   add_alignment_attribute (ptr_die, type);
24657 
24658   if (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
24659       && TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE)
24660     {
24661       dw_loc_descr_ref op = new_loc_descr (DW_OP_plus, 0, 0);
24662       add_AT_loc (ptr_die, DW_AT_use_location, op);
24663     }
24664 }
24665 
24666 static char *producer_string;
24667 
24668 /* Given a C and/or C++ language/version string return the "highest".
24669    C++ is assumed to be "higher" than C in this case.  Used for merging
24670    LTO translation unit languages.  */
24671 static const char *
highest_c_language(const char * lang1,const char * lang2)24672 highest_c_language (const char *lang1, const char *lang2)
24673 {
24674   if (strcmp ("GNU C++23", lang1) == 0 || strcmp ("GNU C++23", lang2) == 0)
24675     return "GNU C++23";
24676   if (strcmp ("GNU C++20", lang1) == 0 || strcmp ("GNU C++20", lang2) == 0)
24677     return "GNU C++20";
24678   if (strcmp ("GNU C++17", lang1) == 0 || strcmp ("GNU C++17", lang2) == 0)
24679     return "GNU C++17";
24680   if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
24681     return "GNU C++14";
24682   if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
24683     return "GNU C++11";
24684   if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
24685     return "GNU C++98";
24686 
24687   if (strcmp ("GNU C2X", lang1) == 0 || strcmp ("GNU C2X", lang2) == 0)
24688     return "GNU C2X";
24689   if (strcmp ("GNU C17", lang1) == 0 || strcmp ("GNU C17", lang2) == 0)
24690     return "GNU C17";
24691   if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
24692     return "GNU C11";
24693   if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
24694     return "GNU C99";
24695   if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
24696     return "GNU C89";
24697 
24698   gcc_unreachable ();
24699 }
24700 
24701 
24702 /* Generate the DIE for the compilation unit.  */
24703 
24704 static dw_die_ref
gen_compile_unit_die(const char * filename)24705 gen_compile_unit_die (const char *filename)
24706 {
24707   dw_die_ref die;
24708   const char *language_string = lang_hooks.name;
24709   int language;
24710 
24711   die = new_die (DW_TAG_compile_unit, NULL, NULL);
24712 
24713   if (filename)
24714     {
24715       add_filename_attribute (die, filename);
24716       /* Don't add cwd for <built-in>.  */
24717       if (filename[0] != '<')
24718 	add_comp_dir_attribute (die);
24719     }
24720 
24721   add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
24722 
24723   /* If our producer is LTO try to figure out a common language to use
24724      from the global list of translation units.  */
24725   if (strcmp (language_string, "GNU GIMPLE") == 0)
24726     {
24727       unsigned i;
24728       tree t;
24729       const char *common_lang = NULL;
24730 
24731       FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
24732 	{
24733 	  if (!TRANSLATION_UNIT_LANGUAGE (t))
24734 	    continue;
24735 	  if (!common_lang)
24736 	    common_lang = TRANSLATION_UNIT_LANGUAGE (t);
24737 	  else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
24738 	    ;
24739 	  else if (strncmp (common_lang, "GNU C", 5) == 0
24740 		    && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
24741 	    /* Mixing C and C++ is ok, use C++ in that case.  */
24742 	    common_lang = highest_c_language (common_lang,
24743 					      TRANSLATION_UNIT_LANGUAGE (t));
24744 	  else
24745 	    {
24746 	      /* Fall back to C.  */
24747 	      common_lang = NULL;
24748 	      break;
24749 	    }
24750 	}
24751 
24752       if (common_lang)
24753 	language_string = common_lang;
24754     }
24755 
24756   language = DW_LANG_C;
24757   if (strncmp (language_string, "GNU C", 5) == 0
24758       && ISDIGIT (language_string[5]))
24759     {
24760       language = DW_LANG_C89;
24761       if (dwarf_version >= 3 || !dwarf_strict)
24762 	{
24763 	  if (strcmp (language_string, "GNU C89") != 0)
24764 	    language = DW_LANG_C99;
24765 
24766 	  if (dwarf_version >= 5 /* || !dwarf_strict */)
24767 	    if (strcmp (language_string, "GNU C11") == 0
24768 		|| strcmp (language_string, "GNU C17") == 0
24769 		|| strcmp (language_string, "GNU C2X") == 0)
24770 	      language = DW_LANG_C11;
24771 	}
24772     }
24773   else if (strncmp (language_string, "GNU C++", 7) == 0)
24774     {
24775       language = DW_LANG_C_plus_plus;
24776       if (dwarf_version >= 5 /* || !dwarf_strict */)
24777 	{
24778 	  if (strcmp (language_string, "GNU C++11") == 0)
24779 	    language = DW_LANG_C_plus_plus_11;
24780 	  else if (strcmp (language_string, "GNU C++14") == 0)
24781 	    language = DW_LANG_C_plus_plus_14;
24782 	  else if (strcmp (language_string, "GNU C++17") == 0
24783 		   || strcmp (language_string, "GNU C++20") == 0
24784 		   || strcmp (language_string, "GNU C++23") == 0)
24785 	    /* For now.  */
24786 	    language = DW_LANG_C_plus_plus_14;
24787 	}
24788     }
24789   else if (strcmp (language_string, "GNU F77") == 0)
24790     language = DW_LANG_Fortran77;
24791   else if (dwarf_version >= 3 || !dwarf_strict)
24792     {
24793       if (strcmp (language_string, "GNU Ada") == 0)
24794 	language = DW_LANG_Ada95;
24795       else if (strncmp (language_string, "GNU Fortran", 11) == 0)
24796 	{
24797 	  language = DW_LANG_Fortran95;
24798 	  if (dwarf_version >= 5 /* || !dwarf_strict */)
24799 	    {
24800 	      if (strcmp (language_string, "GNU Fortran2003") == 0)
24801 		language = DW_LANG_Fortran03;
24802 	      else if (strcmp (language_string, "GNU Fortran2008") == 0)
24803 		language = DW_LANG_Fortran08;
24804 	    }
24805 	}
24806       else if (strcmp (language_string, "GNU Objective-C") == 0)
24807 	language = DW_LANG_ObjC;
24808       else if (strcmp (language_string, "GNU Objective-C++") == 0)
24809 	language = DW_LANG_ObjC_plus_plus;
24810       else if (strcmp (language_string, "GNU D") == 0)
24811 	language = DW_LANG_D;
24812       else if (dwarf_version >= 5 || !dwarf_strict)
24813 	{
24814 	  if (strcmp (language_string, "GNU Go") == 0)
24815 	    language = DW_LANG_Go;
24816 	}
24817     }
24818   /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works.  */
24819   else if (strncmp (language_string, "GNU Fortran", 11) == 0)
24820     language = DW_LANG_Fortran90;
24821   /* Likewise for Ada.  */
24822   else if (strcmp (language_string, "GNU Ada") == 0)
24823     language = DW_LANG_Ada83;
24824 
24825   add_AT_unsigned (die, DW_AT_language, language);
24826 
24827   switch (language)
24828     {
24829     case DW_LANG_Fortran77:
24830     case DW_LANG_Fortran90:
24831     case DW_LANG_Fortran95:
24832     case DW_LANG_Fortran03:
24833     case DW_LANG_Fortran08:
24834       /* Fortran has case insensitive identifiers and the front-end
24835 	 lowercases everything.  */
24836       add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
24837       break;
24838     default:
24839       /* The default DW_ID_case_sensitive doesn't need to be specified.  */
24840       break;
24841     }
24842   return die;
24843 }
24844 
24845 /* Generate the DIE for a base class.  */
24846 
24847 static void
gen_inheritance_die(tree binfo,tree access,tree type,dw_die_ref context_die)24848 gen_inheritance_die (tree binfo, tree access, tree type,
24849 		     dw_die_ref context_die)
24850 {
24851   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
24852   struct vlr_context ctx = { type, NULL };
24853 
24854   add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
24855 		      context_die);
24856   add_data_member_location_attribute (die, binfo, &ctx);
24857 
24858   if (BINFO_VIRTUAL_P (binfo))
24859     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
24860 
24861   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
24862      children, otherwise the default is DW_ACCESS_public.  In DWARF2
24863      the default has always been DW_ACCESS_private.  */
24864   if (access == access_public_node)
24865     {
24866       if (dwarf_version == 2
24867 	  || context_die->die_tag == DW_TAG_class_type)
24868       add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
24869     }
24870   else if (access == access_protected_node)
24871     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
24872   else if (dwarf_version > 2
24873 	   && context_die->die_tag != DW_TAG_class_type)
24874     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
24875 }
24876 
24877 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
24878    structure.  */
24879 
24880 static bool
is_variant_part(tree decl)24881 is_variant_part (tree decl)
24882 {
24883   return (TREE_CODE (decl) == FIELD_DECL
24884 	  && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
24885 }
24886 
24887 /* Check that OPERAND is a reference to a field in STRUCT_TYPE.  If it is,
24888    return the FIELD_DECL.  Return NULL_TREE otherwise.  */
24889 
24890 static tree
analyze_discr_in_predicate(tree operand,tree struct_type)24891 analyze_discr_in_predicate (tree operand, tree struct_type)
24892 {
24893   while (CONVERT_EXPR_P (operand))
24894     operand = TREE_OPERAND (operand, 0);
24895 
24896   /* Match field access to members of struct_type only.  */
24897   if (TREE_CODE (operand) == COMPONENT_REF
24898       && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
24899       && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
24900       && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
24901     return TREE_OPERAND (operand, 1);
24902   else
24903     return NULL_TREE;
24904 }
24905 
24906 /* Check that SRC is a constant integer that can be represented as a native
24907    integer constant (either signed or unsigned).  If so, store it into DEST and
24908    return true.  Return false otherwise. */
24909 
24910 static bool
get_discr_value(tree src,dw_discr_value * dest)24911 get_discr_value (tree src, dw_discr_value *dest)
24912 {
24913   tree discr_type = TREE_TYPE (src);
24914 
24915   if (lang_hooks.types.get_debug_type)
24916     {
24917       tree debug_type = lang_hooks.types.get_debug_type (discr_type);
24918       if (debug_type != NULL)
24919 	discr_type = debug_type;
24920     }
24921 
24922   if (TREE_CODE (src) != INTEGER_CST || !INTEGRAL_TYPE_P (discr_type))
24923     return false;
24924 
24925   /* Signedness can vary between the original type and the debug type. This
24926      can happen for character types in Ada for instance: the character type
24927      used for code generation can be signed, to be compatible with the C one,
24928      but from a debugger point of view, it must be unsigned.  */
24929   bool is_orig_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
24930   bool is_debug_unsigned = TYPE_UNSIGNED (discr_type);
24931 
24932   if (is_orig_unsigned != is_debug_unsigned)
24933     src = fold_convert (discr_type, src);
24934 
24935   if (!(is_debug_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
24936     return false;
24937 
24938   dest->pos = is_debug_unsigned;
24939   if (is_debug_unsigned)
24940     dest->v.uval = tree_to_uhwi (src);
24941   else
24942     dest->v.sval = tree_to_shwi (src);
24943 
24944   return true;
24945 }
24946 
24947 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
24948    FIELD_DECL in STRUCT_TYPE that represents a variant part.  If unsuccessful,
24949    store NULL_TREE in DISCR_DECL.  Otherwise:
24950 
24951      - store the discriminant field in STRUCT_TYPE that controls the variant
24952        part to *DISCR_DECL
24953 
24954      - put in *DISCR_LISTS_P an array where for each variant, the item
24955        represents the corresponding matching list of discriminant values.
24956 
24957      - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
24958        the above array.
24959 
24960    Note that when the array is allocated (i.e. when the analysis is
24961    successful), it is up to the caller to free the array.  */
24962 
24963 static void
analyze_variants_discr(tree variant_part_decl,tree struct_type,tree * discr_decl,dw_discr_list_ref ** discr_lists_p,unsigned * discr_lists_length)24964 analyze_variants_discr (tree variant_part_decl,
24965 			tree struct_type,
24966 			tree *discr_decl,
24967 			dw_discr_list_ref **discr_lists_p,
24968 			unsigned *discr_lists_length)
24969 {
24970   tree variant_part_type = TREE_TYPE (variant_part_decl);
24971   tree variant;
24972   dw_discr_list_ref *discr_lists;
24973   unsigned i;
24974 
24975   /* Compute how many variants there are in this variant part.  */
24976   *discr_lists_length = 0;
24977   for (variant = TYPE_FIELDS (variant_part_type);
24978        variant != NULL_TREE;
24979        variant = DECL_CHAIN (variant))
24980     ++*discr_lists_length;
24981 
24982   *discr_decl = NULL_TREE;
24983   *discr_lists_p
24984     = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
24985 				     sizeof (**discr_lists_p));
24986   discr_lists = *discr_lists_p;
24987 
24988   /* And then analyze all variants to extract discriminant information for all
24989      of them.  This analysis is conservative: as soon as we detect something we
24990      do not support, abort everything and pretend we found nothing.  */
24991   for (variant = TYPE_FIELDS (variant_part_type), i = 0;
24992        variant != NULL_TREE;
24993        variant = DECL_CHAIN (variant), ++i)
24994     {
24995       tree match_expr = DECL_QUALIFIER (variant);
24996 
24997       /* Now, try to analyze the predicate and deduce a discriminant for
24998 	 it.  */
24999       if (match_expr == boolean_true_node)
25000 	/* Typically happens for the default variant: it matches all cases that
25001 	   previous variants rejected.  Don't output any matching value for
25002 	   this one.  */
25003 	continue;
25004 
25005       /* The following loop tries to iterate over each discriminant
25006 	 possibility: single values or ranges.  */
25007       while (match_expr != NULL_TREE)
25008 	{
25009 	  tree next_round_match_expr;
25010 	  tree candidate_discr = NULL_TREE;
25011 	  dw_discr_list_ref new_node = NULL;
25012 
25013 	  /* Possibilities are matched one after the other by nested
25014 	     TRUTH_ORIF_EXPR expressions.  Process the current possibility and
25015 	     continue with the rest at next iteration.  */
25016 	  if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
25017 	    {
25018 	      next_round_match_expr = TREE_OPERAND (match_expr, 0);
25019 	      match_expr = TREE_OPERAND (match_expr, 1);
25020 	    }
25021 	  else
25022 	    next_round_match_expr = NULL_TREE;
25023 
25024 	  if (match_expr == boolean_false_node)
25025 	    /* This sub-expression matches nothing: just wait for the next
25026 	       one.  */
25027 	    ;
25028 
25029 	  else if (TREE_CODE (match_expr) == EQ_EXPR)
25030 	    {
25031 	      /* We are matching:  <discr_field> == <integer_cst>
25032 		 This sub-expression matches a single value.  */
25033 	      tree integer_cst = TREE_OPERAND (match_expr, 1);
25034 
25035 	      candidate_discr
25036 	       = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
25037 					     struct_type);
25038 
25039 	      new_node = ggc_cleared_alloc<dw_discr_list_node> ();
25040 	      if (!get_discr_value (integer_cst,
25041 				    &new_node->dw_discr_lower_bound))
25042 		goto abort;
25043 	      new_node->dw_discr_range = false;
25044 	    }
25045 
25046 	  else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
25047 	    {
25048 	      /* We are matching:
25049 		   <discr_field> > <integer_cst>
25050 		   && <discr_field> < <integer_cst>.
25051 		 This sub-expression matches the range of values between the
25052 		 two matched integer constants.  Note that comparisons can be
25053 		 inclusive or exclusive.  */
25054 	      tree candidate_discr_1, candidate_discr_2;
25055 	      tree lower_cst, upper_cst;
25056 	      bool lower_cst_included, upper_cst_included;
25057 	      tree lower_op = TREE_OPERAND (match_expr, 0);
25058 	      tree upper_op = TREE_OPERAND (match_expr, 1);
25059 
25060 	      /* When the comparison is exclusive, the integer constant is not
25061 		 the discriminant range bound we are looking for: we will have
25062 		 to increment or decrement it.  */
25063 	      if (TREE_CODE (lower_op) == GE_EXPR)
25064 		lower_cst_included = true;
25065 	      else if (TREE_CODE (lower_op) == GT_EXPR)
25066 		lower_cst_included = false;
25067 	      else
25068 		goto abort;
25069 
25070 	      if (TREE_CODE (upper_op) == LE_EXPR)
25071 		upper_cst_included = true;
25072 	      else if (TREE_CODE (upper_op) == LT_EXPR)
25073 		upper_cst_included = false;
25074 	      else
25075 		goto abort;
25076 
25077 	      /* Extract the discriminant from the first operand and check it
25078 		 is consistant with the same analysis in the second
25079 		 operand.  */
25080 	      candidate_discr_1
25081 	        = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
25082 					      struct_type);
25083 	      candidate_discr_2
25084 	        = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
25085 					      struct_type);
25086 	      if (candidate_discr_1 == candidate_discr_2)
25087 		candidate_discr = candidate_discr_1;
25088 	      else
25089 		goto abort;
25090 
25091 	      /* Extract bounds from both.  */
25092 	      new_node = ggc_cleared_alloc<dw_discr_list_node> ();
25093 	      lower_cst = TREE_OPERAND (lower_op, 1);
25094 	      upper_cst = TREE_OPERAND (upper_op, 1);
25095 
25096 	      if (!lower_cst_included)
25097 		lower_cst
25098 		  = fold_build2 (PLUS_EXPR, TREE_TYPE (lower_cst), lower_cst,
25099 				 build_int_cst (TREE_TYPE (lower_cst), 1));
25100 	      if (!upper_cst_included)
25101 		upper_cst
25102 		  = fold_build2 (MINUS_EXPR, TREE_TYPE (upper_cst), upper_cst,
25103 				 build_int_cst (TREE_TYPE (upper_cst), 1));
25104 
25105 	      if (!get_discr_value (lower_cst,
25106 				    &new_node->dw_discr_lower_bound)
25107 		  || !get_discr_value (upper_cst,
25108 				       &new_node->dw_discr_upper_bound))
25109 		goto abort;
25110 
25111 	      new_node->dw_discr_range = true;
25112 	    }
25113 
25114 	  else if ((candidate_discr
25115 		      = analyze_discr_in_predicate (match_expr, struct_type))
25116 		   && (TREE_TYPE (candidate_discr) == boolean_type_node
25117 		       || TREE_TYPE (TREE_TYPE (candidate_discr))
25118 			  == boolean_type_node))
25119 	    {
25120 	      /* We are matching:  <discr_field> for a boolean discriminant.
25121 		 This sub-expression matches boolean_true_node.  */
25122 	      new_node = ggc_cleared_alloc<dw_discr_list_node> ();
25123 	      if (!get_discr_value (boolean_true_node,
25124 				    &new_node->dw_discr_lower_bound))
25125 		goto abort;
25126 	      new_node->dw_discr_range = false;
25127 	    }
25128 
25129 	  else
25130 	    /* Unsupported sub-expression: we cannot determine the set of
25131 	       matching discriminant values.  Abort everything.  */
25132 	    goto abort;
25133 
25134 	  /* If the discriminant info is not consistant with what we saw so
25135 	     far, consider the analysis failed and abort everything.  */
25136 	  if (candidate_discr == NULL_TREE
25137 	      || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
25138 	    goto abort;
25139 	  else
25140 	    *discr_decl = candidate_discr;
25141 
25142 	  if (new_node != NULL)
25143 	    {
25144 	      new_node->dw_discr_next = discr_lists[i];
25145 	      discr_lists[i] = new_node;
25146 	    }
25147 	  match_expr = next_round_match_expr;
25148 	}
25149     }
25150 
25151   /* If we reach this point, we could match everything we were interested
25152      in.  */
25153   return;
25154 
25155 abort:
25156   /* Clean all data structure and return no result.  */
25157   free (*discr_lists_p);
25158   *discr_lists_p = NULL;
25159   *discr_decl = NULL_TREE;
25160 }
25161 
25162 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
25163    of STRUCT_TYPE, a record type.  This new DIE is emitted as the next child
25164    under CONTEXT_DIE.
25165 
25166    Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
25167    QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter.  The members for
25168    this type, which are record types, represent the available variants and each
25169    has a DECL_QUALIFIER attribute.  The discriminant and the discriminant
25170    values are inferred from these attributes.
25171 
25172    In trees, the offsets for the fields inside these sub-records are relative
25173    to the variant part itself, whereas the corresponding DIEs should have
25174    offset attributes that are relative to the embedding record base address.
25175    This is why the caller must provide a VARIANT_PART_OFFSET expression: it
25176    must be an expression that computes the offset of the variant part to
25177    describe in DWARF.  */
25178 
25179 static void
gen_variant_part(tree variant_part_decl,struct vlr_context * vlr_ctx,dw_die_ref context_die)25180 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
25181 		  dw_die_ref context_die)
25182 {
25183   const tree variant_part_type = TREE_TYPE (variant_part_decl);
25184   tree variant_part_offset = vlr_ctx->variant_part_offset;
25185   struct loc_descr_context ctx = {
25186     vlr_ctx->struct_type, /* context_type */
25187     NULL_TREE,		  /* base_decl */
25188     NULL,		  /* dpi */
25189     false,		  /* placeholder_arg */
25190     false		  /* placeholder_seen */
25191   };
25192 
25193   /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
25194      NULL_TREE if there is no such field.  */
25195   tree discr_decl = NULL_TREE;
25196   dw_discr_list_ref *discr_lists;
25197   unsigned discr_lists_length = 0;
25198   unsigned i;
25199 
25200   dw_die_ref dwarf_proc_die = NULL;
25201   dw_die_ref variant_part_die
25202     = new_die (DW_TAG_variant_part, context_die, variant_part_type);
25203 
25204   equate_decl_number_to_die (variant_part_decl, variant_part_die);
25205 
25206   analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
25207 			  &discr_decl, &discr_lists, &discr_lists_length);
25208 
25209   if (discr_decl != NULL_TREE)
25210     {
25211       dw_die_ref discr_die = lookup_decl_die (discr_decl);
25212 
25213       if (discr_die)
25214 	add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
25215       else
25216 	/* We have no DIE for the discriminant, so just discard all
25217 	   discrimimant information in the output.  */
25218 	discr_decl = NULL_TREE;
25219     }
25220 
25221   /* If the offset for this variant part is more complex than a constant,
25222      create a DWARF procedure for it so that we will not have to generate DWARF
25223      expressions for it for each member.  */
25224   if (TREE_CODE (variant_part_offset) != INTEGER_CST
25225       && (dwarf_version >= 3 || !dwarf_strict))
25226     {
25227       const tree dwarf_proc_fndecl
25228         = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
25229 		      build_function_type (TREE_TYPE (variant_part_offset),
25230 					   NULL_TREE));
25231       const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
25232       const dw_loc_descr_ref dwarf_proc_body
25233         = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
25234 
25235       dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
25236 					   dwarf_proc_fndecl, context_die);
25237       if (dwarf_proc_die != NULL)
25238 	variant_part_offset = dwarf_proc_call;
25239     }
25240 
25241   /* Output DIEs for all variants.  */
25242   i = 0;
25243   for (tree variant = TYPE_FIELDS (variant_part_type);
25244        variant != NULL_TREE;
25245        variant = DECL_CHAIN (variant), ++i)
25246     {
25247       tree variant_type = TREE_TYPE (variant);
25248       dw_die_ref variant_die;
25249 
25250       /* All variants (i.e. members of a variant part) are supposed to be
25251 	 encoded as structures.  Sub-variant parts are QUAL_UNION_TYPE fields
25252 	 under these records.  */
25253       gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
25254 
25255       variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
25256       equate_decl_number_to_die (variant, variant_die);
25257 
25258       /* Output discriminant values this variant matches, if any.  */
25259       if (discr_decl == NULL || discr_lists[i] == NULL)
25260 	/* In the case we have discriminant information at all, this is
25261 	   probably the default variant: as the standard says, don't
25262 	   output any discriminant value/list attribute.  */
25263 	;
25264       else if (discr_lists[i]->dw_discr_next == NULL
25265 	       && !discr_lists[i]->dw_discr_range)
25266 	/* If there is only one accepted value, don't bother outputting a
25267 	   list.  */
25268 	add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
25269       else
25270 	add_discr_list (variant_die, discr_lists[i]);
25271 
25272       for (tree member = TYPE_FIELDS (variant_type);
25273 	   member != NULL_TREE;
25274 	   member = DECL_CHAIN (member))
25275 	{
25276 	  struct vlr_context vlr_sub_ctx = {
25277 	    vlr_ctx->struct_type, /* struct_type */
25278 	    NULL		  /* variant_part_offset */
25279 	  };
25280 	  if (is_variant_part (member))
25281 	    {
25282 	      /* All offsets for fields inside variant parts are relative to
25283 		 the top-level embedding RECORD_TYPE's base address.  On the
25284 		 other hand, offsets in GCC's types are relative to the
25285 		 nested-most variant part.  So we have to sum offsets each time
25286 		 we recurse.  */
25287 
25288 	      vlr_sub_ctx.variant_part_offset
25289 		= fold_build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
25290 			       variant_part_offset, byte_position (member));
25291 	      gen_variant_part (member, &vlr_sub_ctx, variant_die);
25292 	    }
25293 	  else
25294 	    {
25295 	      vlr_sub_ctx.variant_part_offset = variant_part_offset;
25296 	      gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
25297 	    }
25298 	}
25299     }
25300 
25301   free (discr_lists);
25302 }
25303 
25304 /* Generate a DIE for a class member.  */
25305 
25306 static void
gen_member_die(tree type,dw_die_ref context_die)25307 gen_member_die (tree type, dw_die_ref context_die)
25308 {
25309   tree member;
25310   tree binfo = TYPE_BINFO (type);
25311 
25312   gcc_assert (TYPE_MAIN_VARIANT (type) == type);
25313 
25314   /* If this is not an incomplete type, output descriptions of each of its
25315      members. Note that as we output the DIEs necessary to represent the
25316      members of this record or union type, we will also be trying to output
25317      DIEs to represent the *types* of those members. However the `type'
25318      function (above) will specifically avoid generating type DIEs for member
25319      types *within* the list of member DIEs for this (containing) type except
25320      for those types (of members) which are explicitly marked as also being
25321      members of this (containing) type themselves.  The g++ front- end can
25322      force any given type to be treated as a member of some other (containing)
25323      type by setting the TYPE_CONTEXT of the given (member) type to point to
25324      the TREE node representing the appropriate (containing) type.  */
25325 
25326   /* First output info about the base classes.  */
25327   if (binfo && early_dwarf)
25328     {
25329       vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
25330       int i;
25331       tree base;
25332 
25333       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
25334 	gen_inheritance_die (base,
25335 			     (accesses ? (*accesses)[i] : access_public_node),
25336 			     type,
25337 			     context_die);
25338     }
25339 
25340   /* Now output info about the members. */
25341   for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
25342     {
25343       /* Ignore clones.  */
25344       if (DECL_ABSTRACT_ORIGIN (member))
25345 	continue;
25346 
25347       struct vlr_context vlr_ctx = { type, NULL_TREE };
25348       bool static_inline_p
25349 	= (VAR_P (member)
25350 	   && TREE_STATIC (member)
25351 	   && (lang_hooks.decls.decl_dwarf_attribute (member, DW_AT_inline)
25352 	       != -1));
25353 
25354       /* If we thought we were generating minimal debug info for TYPE
25355 	 and then changed our minds, some of the member declarations
25356 	 may have already been defined.  Don't define them again, but
25357 	 do put them in the right order.  */
25358 
25359       if (dw_die_ref child = lookup_decl_die (member))
25360 	{
25361 	  /* Handle inline static data members, which only have in-class
25362 	     declarations.  */
25363 	  bool splice = true;
25364 
25365 	  dw_die_ref ref = NULL;
25366 	  if (child->die_tag == DW_TAG_variable
25367 	      && child->die_parent == comp_unit_die ())
25368 	    {
25369 	      ref = get_AT_ref (child, DW_AT_specification);
25370 
25371 	      /* For C++17 inline static data members followed by redundant
25372 		 out of class redeclaration, we might get here with
25373 		 child being the DIE created for the out of class
25374 		 redeclaration and with its DW_AT_specification being
25375 		 the DIE created for in-class definition.  We want to
25376 		 reparent the latter, and don't want to create another
25377 		 DIE with DW_AT_specification in that case, because
25378 		 we already have one.  */
25379 	      if (ref
25380 		  && static_inline_p
25381 		  && ref->die_tag == DW_TAG_variable
25382 		  && ref->die_parent == comp_unit_die ()
25383 		  && get_AT (ref, DW_AT_specification) == NULL)
25384 		{
25385 		  child = ref;
25386 		  ref = NULL;
25387 		  static_inline_p = false;
25388 		}
25389 
25390 	      if (!ref)
25391 		{
25392 		  reparent_child (child, context_die);
25393 		  if (dwarf_version < 5)
25394 		    child->die_tag = DW_TAG_member;
25395 		  splice = false;
25396 		}
25397 	    }
25398 	  else if (child->die_tag == DW_TAG_enumerator)
25399 	    /* Enumerators remain under their enumeration even if
25400 	       their names are introduced in the enclosing scope.  */
25401 	    splice = false;
25402 
25403 	  if (splice)
25404 	    splice_child_die (context_die, child);
25405 	}
25406 
25407       /* Do not generate standard DWARF for variant parts if we are generating
25408 	 the corresponding GNAT encodings: DIEs generated for both would
25409 	 conflict in our mappings.  */
25410       else if (is_variant_part (member)
25411 	       && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
25412 	{
25413 	  vlr_ctx.variant_part_offset = byte_position (member);
25414 	  gen_variant_part (member, &vlr_ctx, context_die);
25415 	}
25416       else
25417 	{
25418 	  vlr_ctx.variant_part_offset = NULL_TREE;
25419 	  gen_decl_die (member, NULL, &vlr_ctx, context_die);
25420 	}
25421 
25422       /* For C++ inline static data members emit immediately a DW_TAG_variable
25423 	 DIE that will refer to that DW_TAG_member/DW_TAG_variable through
25424 	 DW_AT_specification.  */
25425       if (static_inline_p)
25426 	{
25427 	  int old_extern = DECL_EXTERNAL (member);
25428 	  DECL_EXTERNAL (member) = 0;
25429 	  gen_decl_die (member, NULL, NULL, comp_unit_die ());
25430 	  DECL_EXTERNAL (member) = old_extern;
25431 	}
25432     }
25433 }
25434 
25435 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
25436    is set, we pretend that the type was never defined, so we only get the
25437    member DIEs needed by later specification DIEs.  */
25438 
25439 static void
gen_struct_or_union_type_die(tree type,dw_die_ref context_die,enum debug_info_usage usage)25440 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
25441 				enum debug_info_usage usage)
25442 {
25443   if (TREE_ASM_WRITTEN (type))
25444     {
25445       /* Fill in the bound of variable-length fields in late dwarf if
25446 	 still incomplete.  */
25447       if (!early_dwarf && variably_modified_type_p (type, NULL))
25448 	for (tree member = TYPE_FIELDS (type);
25449 	     member;
25450 	     member = DECL_CHAIN (member))
25451 	  fill_variable_array_bounds (TREE_TYPE (member));
25452       return;
25453     }
25454 
25455   dw_die_ref type_die = lookup_type_die (type);
25456   dw_die_ref scope_die = 0;
25457   int nested = 0;
25458   int complete = (TYPE_SIZE (type)
25459 		  && (! TYPE_STUB_DECL (type)
25460 		      || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
25461   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
25462   complete = complete && should_emit_struct_debug (type, usage);
25463 
25464   if (type_die && ! complete)
25465     return;
25466 
25467   if (TYPE_CONTEXT (type) != NULL_TREE
25468       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
25469 	  || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
25470     nested = 1;
25471 
25472   scope_die = scope_die_for (type, context_die);
25473 
25474   /* Generate child dies for template paramaters.  */
25475   if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
25476     schedule_generic_params_dies_gen (type);
25477 
25478   if (! type_die || (nested && is_cu_die (scope_die)))
25479     /* First occurrence of type or toplevel definition of nested class.  */
25480     {
25481       dw_die_ref old_die = type_die;
25482 
25483       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
25484 			  ? record_type_tag (type) : DW_TAG_union_type,
25485 			  scope_die, type);
25486       equate_type_number_to_die (type, type_die);
25487       if (old_die)
25488 	add_AT_specification (type_die, old_die);
25489       else
25490 	add_name_attribute (type_die, type_tag (type));
25491     }
25492   else
25493     remove_AT (type_die, DW_AT_declaration);
25494 
25495   /* If this type has been completed, then give it a byte_size attribute and
25496      then give a list of members.  */
25497   if (complete && !ns_decl)
25498     {
25499       /* Prevent infinite recursion in cases where the type of some member of
25500 	 this type is expressed in terms of this type itself.  */
25501       TREE_ASM_WRITTEN (type) = 1;
25502       add_byte_size_attribute (type_die, type);
25503       add_alignment_attribute (type_die, type);
25504       if (TYPE_STUB_DECL (type) != NULL_TREE)
25505 	{
25506 	  add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
25507 	  add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
25508 	}
25509 
25510       /* If the first reference to this type was as the return type of an
25511 	 inline function, then it may not have a parent.  Fix this now.  */
25512       if (type_die->die_parent == NULL)
25513 	add_child_die (scope_die, type_die);
25514 
25515       gen_member_die (type, type_die);
25516 
25517       add_gnat_descriptive_type_attribute (type_die, type, context_die);
25518       if (TYPE_ARTIFICIAL (type))
25519 	add_AT_flag (type_die, DW_AT_artificial, 1);
25520 
25521       /* GNU extension: Record what type our vtable lives in.  */
25522       if (TYPE_VFIELD (type))
25523 	{
25524 	  tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
25525 
25526 	  gen_type_die (vtype, context_die);
25527 	  add_AT_die_ref (type_die, DW_AT_containing_type,
25528 			  lookup_type_die (vtype));
25529 	}
25530     }
25531   else
25532     {
25533       add_AT_flag (type_die, DW_AT_declaration, 1);
25534 
25535       /* We don't need to do this for function-local types.  */
25536       if (TYPE_STUB_DECL (type)
25537 	  && ! decl_function_context (TYPE_STUB_DECL (type)))
25538 	vec_safe_push (incomplete_types, type);
25539     }
25540 
25541   if (get_AT (type_die, DW_AT_name))
25542     add_pubtype (type, type_die);
25543 }
25544 
25545 /* Generate a DIE for a subroutine _type_.  */
25546 
25547 static void
gen_subroutine_type_die(tree type,dw_die_ref context_die)25548 gen_subroutine_type_die (tree type, dw_die_ref context_die)
25549 {
25550   tree return_type = TREE_TYPE (type);
25551   dw_die_ref subr_die
25552     = new_die (DW_TAG_subroutine_type,
25553 	       scope_die_for (type, context_die), type);
25554 
25555   equate_type_number_to_die (type, subr_die);
25556   add_prototyped_attribute (subr_die, type);
25557   add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
25558 		      context_die);
25559   add_alignment_attribute (subr_die, type);
25560   gen_formal_types_die (type, subr_die);
25561 
25562   if (get_AT (subr_die, DW_AT_name))
25563     add_pubtype (type, subr_die);
25564   if ((dwarf_version >= 5 || !dwarf_strict)
25565       && lang_hooks.types.type_dwarf_attribute (type, DW_AT_reference) != -1)
25566     add_AT_flag (subr_die, DW_AT_reference, 1);
25567   if ((dwarf_version >= 5 || !dwarf_strict)
25568       && lang_hooks.types.type_dwarf_attribute (type,
25569 						DW_AT_rvalue_reference) != -1)
25570     add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
25571 }
25572 
25573 /* Generate a DIE for a type definition.  */
25574 
25575 static void
gen_typedef_die(tree decl,dw_die_ref context_die)25576 gen_typedef_die (tree decl, dw_die_ref context_die)
25577 {
25578   dw_die_ref type_die;
25579   tree type;
25580 
25581   if (TREE_ASM_WRITTEN (decl))
25582     {
25583       if (DECL_ORIGINAL_TYPE (decl))
25584 	fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
25585       return;
25586     }
25587 
25588   /* As we avoid creating DIEs for local typedefs (see decl_ultimate_origin
25589      checks in process_scope_var and modified_type_die), this should be called
25590      only for original types.  */
25591   gcc_assert (decl_ultimate_origin (decl) == NULL
25592 	      || decl_ultimate_origin (decl) == decl);
25593 
25594   TREE_ASM_WRITTEN (decl) = 1;
25595   type_die = new_die (DW_TAG_typedef, context_die, decl);
25596 
25597   add_name_and_src_coords_attributes (type_die, decl);
25598   if (DECL_ORIGINAL_TYPE (decl))
25599     {
25600       type = DECL_ORIGINAL_TYPE (decl);
25601       if (type == error_mark_node)
25602 	return;
25603 
25604       gcc_assert (type != TREE_TYPE (decl));
25605       equate_type_number_to_die (TREE_TYPE (decl), type_die);
25606     }
25607   else
25608     {
25609       type = TREE_TYPE (decl);
25610       if (type == error_mark_node)
25611 	return;
25612 
25613       if (is_naming_typedef_decl (TYPE_NAME (type)))
25614 	{
25615 	  /* Here, we are in the case of decl being a typedef naming
25616 	     an anonymous type, e.g:
25617 		 typedef struct {...} foo;
25618 	     In that case TREE_TYPE (decl) is not a typedef variant
25619 	     type and TYPE_NAME of the anonymous type is set to the
25620 	     TYPE_DECL of the typedef. This construct is emitted by
25621 	     the C++ FE.
25622 
25623 	     TYPE is the anonymous struct named by the typedef
25624 	     DECL. As we need the DW_AT_type attribute of the
25625 	     DW_TAG_typedef to point to the DIE of TYPE, let's
25626 	     generate that DIE right away. add_type_attribute
25627 	     called below will then pick (via lookup_type_die) that
25628 	     anonymous struct DIE.  */
25629 	  if (!TREE_ASM_WRITTEN (type))
25630 	    gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
25631 
25632 	  /* This is a GNU Extension.  We are adding a
25633 	     DW_AT_linkage_name attribute to the DIE of the
25634 	     anonymous struct TYPE.  The value of that attribute
25635 	     is the name of the typedef decl naming the anonymous
25636 	     struct.  This greatly eases the work of consumers of
25637 	     this debug info.  */
25638 	  add_linkage_name_raw (lookup_type_die (type), decl);
25639 	}
25640     }
25641 
25642   add_type_attribute (type_die, type, decl_quals (decl), false,
25643 		      context_die);
25644 
25645   if (is_naming_typedef_decl (decl))
25646     /* We want that all subsequent calls to lookup_type_die with
25647        TYPE in argument yield the DW_TAG_typedef we have just
25648        created.  */
25649     equate_type_number_to_die (type, type_die);
25650 
25651   add_alignment_attribute (type_die, TREE_TYPE (decl));
25652 
25653   add_accessibility_attribute (type_die, decl);
25654 
25655   if (DECL_ABSTRACT_P (decl))
25656     equate_decl_number_to_die (decl, type_die);
25657 
25658   if (get_AT (type_die, DW_AT_name))
25659     add_pubtype (decl, type_die);
25660 }
25661 
25662 /* Generate a DIE for a struct, class, enum or union type.  */
25663 
25664 static void
gen_tagged_type_die(tree type,dw_die_ref context_die,enum debug_info_usage usage)25665 gen_tagged_type_die (tree type,
25666 		     dw_die_ref context_die,
25667 		     enum debug_info_usage usage)
25668 {
25669   if (type == NULL_TREE
25670       || !is_tagged_type (type))
25671     return;
25672 
25673   if (TREE_ASM_WRITTEN (type))
25674     ;
25675   /* If this is a nested type whose containing class hasn't been written
25676      out yet, writing it out will cover this one, too.  This does not apply
25677      to instantiations of member class templates; they need to be added to
25678      the containing class as they are generated.  FIXME: This hurts the
25679      idea of combining type decls from multiple TUs, since we can't predict
25680      what set of template instantiations we'll get.  */
25681   else if (TYPE_CONTEXT (type)
25682       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
25683       && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
25684     {
25685       gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
25686 
25687       if (TREE_ASM_WRITTEN (type))
25688 	return;
25689 
25690       /* If that failed, attach ourselves to the stub.  */
25691       context_die = lookup_type_die (TYPE_CONTEXT (type));
25692     }
25693   else if (TYPE_CONTEXT (type) != NULL_TREE
25694 	   && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
25695     {
25696       /* If this type is local to a function that hasn't been written
25697 	 out yet, use a NULL context for now; it will be fixed up in
25698 	 decls_for_scope.  */
25699       context_die = lookup_decl_die (TYPE_CONTEXT (type));
25700       /* A declaration DIE doesn't count; nested types need to go in the
25701 	 specification.  */
25702       if (context_die && is_declaration_die (context_die))
25703 	context_die = NULL;
25704     }
25705   else
25706     context_die = declare_in_namespace (type, context_die);
25707 
25708   if (TREE_CODE (type) == ENUMERAL_TYPE)
25709     {
25710       /* This might have been written out by the call to
25711 	 declare_in_namespace.  */
25712       if (!TREE_ASM_WRITTEN (type))
25713 	gen_enumeration_type_die (type, context_die);
25714     }
25715   else
25716     gen_struct_or_union_type_die (type, context_die, usage);
25717 
25718   /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
25719      it up if it is ever completed.  gen_*_type_die will set it for us
25720      when appropriate.  */
25721 }
25722 
25723 /* Generate a type description DIE.  */
25724 
25725 static void
gen_type_die_with_usage(tree type,dw_die_ref context_die,enum debug_info_usage usage)25726 gen_type_die_with_usage (tree type, dw_die_ref context_die,
25727 			 enum debug_info_usage usage)
25728 {
25729   struct array_descr_info info;
25730 
25731   if (type == NULL_TREE || type == error_mark_node)
25732     return;
25733 
25734   if (flag_checking && type)
25735      verify_type (type);
25736 
25737   if (TYPE_NAME (type) != NULL_TREE
25738       && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
25739       && is_redundant_typedef (TYPE_NAME (type))
25740       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
25741     /* The DECL of this type is a typedef we don't want to emit debug
25742        info for but we want debug info for its underlying typedef.
25743        This can happen for e.g, the injected-class-name of a C++
25744        type.  */
25745     type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
25746 
25747   /* If TYPE is a typedef type variant, let's generate debug info
25748      for the parent typedef which TYPE is a type of.  */
25749   if (typedef_variant_p (type))
25750     {
25751       if (TREE_ASM_WRITTEN (type))
25752 	return;
25753 
25754       tree name = TYPE_NAME (type);
25755       tree origin = decl_ultimate_origin (name);
25756       if (origin != NULL && origin != name)
25757 	{
25758 	  gen_decl_die (origin, NULL, NULL, context_die);
25759 	  return;
25760 	}
25761 
25762       /* Prevent broken recursion; we can't hand off to the same type.  */
25763       gcc_assert (DECL_ORIGINAL_TYPE (name) != type);
25764 
25765       /* Give typedefs the right scope.  */
25766       context_die = scope_die_for (type, context_die);
25767 
25768       TREE_ASM_WRITTEN (type) = 1;
25769 
25770       gen_decl_die (name, NULL, NULL, context_die);
25771       return;
25772     }
25773 
25774   /* If type is an anonymous tagged type named by a typedef, let's
25775      generate debug info for the typedef.  */
25776   if (is_naming_typedef_decl (TYPE_NAME (type)))
25777     {
25778       /* Give typedefs the right scope.  */
25779       context_die = scope_die_for (type, context_die);
25780 
25781       gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
25782       return;
25783     }
25784 
25785   if (lang_hooks.types.get_debug_type)
25786     {
25787       tree debug_type = lang_hooks.types.get_debug_type (type);
25788 
25789       if (debug_type != NULL_TREE && debug_type != type)
25790 	{
25791 	  gen_type_die_with_usage (debug_type, context_die, usage);
25792 	  return;
25793 	}
25794     }
25795 
25796   /* We are going to output a DIE to represent the unqualified version
25797      of this type (i.e. without any const or volatile qualifiers) so
25798      get the main variant (i.e. the unqualified version) of this type
25799      now.  (Vectors and arrays are special because the debugging info is in the
25800      cloned type itself.  Similarly function/method types can contain extra
25801      ref-qualification).  */
25802   if (TREE_CODE (type) == FUNCTION_TYPE
25803       || TREE_CODE (type) == METHOD_TYPE)
25804     {
25805       /* For function/method types, can't use type_main_variant here,
25806 	 because that can have different ref-qualifiers for C++,
25807 	 but try to canonicalize.  */
25808       tree main = TYPE_MAIN_VARIANT (type);
25809       for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
25810 	if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
25811 	    && check_base_type (t, main)
25812 	    && check_lang_type (t, type))
25813 	  {
25814 	    type = t;
25815 	    break;
25816 	  }
25817     }
25818   else if (TREE_CODE (type) != VECTOR_TYPE
25819 	   && TREE_CODE (type) != ARRAY_TYPE)
25820     type = type_main_variant (type);
25821 
25822   /* If this is an array type with hidden descriptor, handle it first.  */
25823   if (!TREE_ASM_WRITTEN (type)
25824       && lang_hooks.types.get_array_descr_info)
25825     {
25826       memset (&info, 0, sizeof (info));
25827       if (lang_hooks.types.get_array_descr_info (type, &info))
25828 	{
25829 	  /* Fortran sometimes emits array types with no dimension.  */
25830 	  gcc_assert (info.ndimensions >= 0
25831 		      && (info.ndimensions
25832 			  <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
25833 	  gen_descr_array_type_die (type, &info, context_die);
25834 	  TREE_ASM_WRITTEN (type) = 1;
25835 	  return;
25836 	}
25837     }
25838 
25839   if (TREE_ASM_WRITTEN (type))
25840     {
25841       /* Variable-length types may be incomplete even if
25842 	 TREE_ASM_WRITTEN.  For such types, fall through to
25843 	 gen_array_type_die() and possibly fill in
25844 	 DW_AT_{upper,lower}_bound attributes.  */
25845       if ((TREE_CODE (type) != ARRAY_TYPE
25846 	   && TREE_CODE (type) != RECORD_TYPE
25847 	   && TREE_CODE (type) != UNION_TYPE
25848 	   && TREE_CODE (type) != QUAL_UNION_TYPE)
25849 	  || !variably_modified_type_p (type, NULL))
25850 	return;
25851     }
25852 
25853   switch (TREE_CODE (type))
25854     {
25855     case ERROR_MARK:
25856       break;
25857 
25858     case POINTER_TYPE:
25859     case REFERENCE_TYPE:
25860       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
25861 	 ensures that the gen_type_die recursion will terminate even if the
25862 	 type is recursive.  Recursive types are possible in Ada.  */
25863       /* ??? We could perhaps do this for all types before the switch
25864 	 statement.  */
25865       TREE_ASM_WRITTEN (type) = 1;
25866 
25867       /* For these types, all that is required is that we output a DIE (or a
25868 	 set of DIEs) to represent the "basis" type.  */
25869       gen_type_die_with_usage (TREE_TYPE (type), context_die,
25870 			       DINFO_USAGE_IND_USE);
25871       break;
25872 
25873     case OFFSET_TYPE:
25874       /* This code is used for C++ pointer-to-data-member types.
25875 	 Output a description of the relevant class type.  */
25876       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
25877 			       DINFO_USAGE_IND_USE);
25878 
25879       /* Output a description of the type of the object pointed to.  */
25880       gen_type_die_with_usage (TREE_TYPE (type), context_die,
25881 			       DINFO_USAGE_IND_USE);
25882 
25883       /* Now output a DIE to represent this pointer-to-data-member type
25884 	 itself.  */
25885       gen_ptr_to_mbr_type_die (type, context_die);
25886       break;
25887 
25888     case FUNCTION_TYPE:
25889       /* Force out return type (in case it wasn't forced out already).  */
25890       gen_type_die_with_usage (TREE_TYPE (type), context_die,
25891 			       DINFO_USAGE_DIR_USE);
25892       gen_subroutine_type_die (type, context_die);
25893       break;
25894 
25895     case METHOD_TYPE:
25896       /* Force out return type (in case it wasn't forced out already).  */
25897       gen_type_die_with_usage (TREE_TYPE (type), context_die,
25898 			       DINFO_USAGE_DIR_USE);
25899       gen_subroutine_type_die (type, context_die);
25900       break;
25901 
25902     case ARRAY_TYPE:
25903     case VECTOR_TYPE:
25904       gen_array_type_die (type, context_die);
25905       break;
25906 
25907     case ENUMERAL_TYPE:
25908     case RECORD_TYPE:
25909     case UNION_TYPE:
25910     case QUAL_UNION_TYPE:
25911       gen_tagged_type_die (type, context_die, usage);
25912       return;
25913 
25914     case VOID_TYPE:
25915     case OPAQUE_TYPE:
25916     case INTEGER_TYPE:
25917     case REAL_TYPE:
25918     case FIXED_POINT_TYPE:
25919     case COMPLEX_TYPE:
25920     case BOOLEAN_TYPE:
25921       /* No DIEs needed for fundamental types.  */
25922       break;
25923 
25924     case NULLPTR_TYPE:
25925     case LANG_TYPE:
25926       /* Just use DW_TAG_unspecified_type.  */
25927       {
25928         dw_die_ref type_die = lookup_type_die (type);
25929         if (type_die == NULL)
25930           {
25931 	    tree name = TYPE_IDENTIFIER (type);
25932             type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
25933 				type);
25934             add_name_attribute (type_die, IDENTIFIER_POINTER (name));
25935             equate_type_number_to_die (type, type_die);
25936           }
25937       }
25938       break;
25939 
25940     default:
25941       if (is_cxx_auto (type))
25942 	{
25943 	  tree name = TYPE_IDENTIFIER (type);
25944 	  dw_die_ref *die = (name == get_identifier ("auto")
25945 			     ? &auto_die : &decltype_auto_die);
25946 	  if (!*die)
25947 	    {
25948 	      *die = new_die (DW_TAG_unspecified_type,
25949 			      comp_unit_die (), NULL_TREE);
25950 	      add_name_attribute (*die, IDENTIFIER_POINTER (name));
25951 	    }
25952 	  equate_type_number_to_die (type, *die);
25953 	  break;
25954 	}
25955       gcc_unreachable ();
25956     }
25957 
25958   TREE_ASM_WRITTEN (type) = 1;
25959 }
25960 
25961 static void
gen_type_die(tree type,dw_die_ref context_die)25962 gen_type_die (tree type, dw_die_ref context_die)
25963 {
25964   if (type != error_mark_node)
25965     {
25966       gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
25967       if (flag_checking)
25968 	{
25969 	  dw_die_ref die = lookup_type_die (type);
25970 	  if (die)
25971 	    check_die (die);
25972 	}
25973     }
25974 }
25975 
25976 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
25977    things which are local to the given block.  */
25978 
25979 static void
gen_block_die(tree stmt,dw_die_ref context_die)25980 gen_block_die (tree stmt, dw_die_ref context_die)
25981 {
25982   int must_output_die = 0;
25983   bool inlined_func;
25984 
25985   /* Ignore blocks that are NULL.  */
25986   if (stmt == NULL_TREE)
25987     return;
25988 
25989   inlined_func = inlined_function_outer_scope_p (stmt);
25990 
25991   /* If the block is one fragment of a non-contiguous block, do not
25992      process the variables, since they will have been done by the
25993      origin block.  Do process subblocks.  */
25994   if (BLOCK_FRAGMENT_ORIGIN (stmt))
25995     {
25996       tree sub;
25997 
25998       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
25999 	gen_block_die (sub, context_die);
26000 
26001       return;
26002     }
26003 
26004   /* Determine if we need to output any Dwarf DIEs at all to represent this
26005      block.  */
26006   if (inlined_func)
26007     /* The outer scopes for inlinings *must* always be represented.  We
26008        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
26009     must_output_die = 1;
26010   else if (lookup_block_die (stmt))
26011     /* If we already have a DIE then it was filled early.  Meanwhile
26012        we might have pruned all BLOCK_VARS as optimized out but we
26013        still want to generate high/low PC attributes so output it.  */
26014     must_output_die = 1;
26015   else if (TREE_USED (stmt)
26016 	   || TREE_ASM_WRITTEN (stmt))
26017     {
26018       /* Determine if this block directly contains any "significant"
26019 	 local declarations which we will need to output DIEs for.  */
26020       if (debug_info_level > DINFO_LEVEL_TERSE)
26021 	{
26022 	  /* We are not in terse mode so any local declaration that
26023 	     is not ignored for debug purposes counts as being a
26024 	     "significant" one.  */
26025 	  if (BLOCK_NUM_NONLOCALIZED_VARS (stmt))
26026 	    must_output_die = 1;
26027 	  else
26028 	    for (tree var = BLOCK_VARS (stmt); var; var = DECL_CHAIN (var))
26029 	      if (!DECL_IGNORED_P (var))
26030 		{
26031 		  must_output_die = 1;
26032 		  break;
26033 		}
26034 	}
26035       else if (!dwarf2out_ignore_block (stmt))
26036 	must_output_die = 1;
26037     }
26038 
26039   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
26040      DIE for any block which contains no significant local declarations at
26041      all.  Rather, in such cases we just call `decls_for_scope' so that any
26042      needed Dwarf info for any sub-blocks will get properly generated. Note
26043      that in terse mode, our definition of what constitutes a "significant"
26044      local declaration gets restricted to include only inlined function
26045      instances and local (nested) function definitions.  */
26046   if (must_output_die)
26047     {
26048       if (inlined_func)
26049 	gen_inlined_subroutine_die (stmt, context_die);
26050       else
26051 	gen_lexical_block_die (stmt, context_die);
26052     }
26053   else
26054     decls_for_scope (stmt, context_die);
26055 }
26056 
26057 /* Process variable DECL (or variable with origin ORIGIN) within
26058    block STMT and add it to CONTEXT_DIE.  */
26059 static void
process_scope_var(tree stmt,tree decl,tree origin,dw_die_ref context_die)26060 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
26061 {
26062   dw_die_ref die;
26063   tree decl_or_origin = decl ? decl : origin;
26064 
26065   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
26066     die = lookup_decl_die (decl_or_origin);
26067   else if (TREE_CODE (decl_or_origin) == TYPE_DECL)
26068     {
26069       if (TYPE_DECL_IS_STUB (decl_or_origin))
26070 	die = lookup_type_die (TREE_TYPE (decl_or_origin));
26071       else
26072 	die = lookup_decl_die (decl_or_origin);
26073       /* Avoid re-creating the DIE late if it was optimized as unused early.  */
26074       if (! die && ! early_dwarf)
26075 	return;
26076     }
26077   else
26078     die = NULL;
26079 
26080   /* Avoid creating DIEs for local typedefs and concrete static variables that
26081      will only be pruned later.  */
26082   if ((origin || decl_ultimate_origin (decl))
26083       && (TREE_CODE (decl_or_origin) == TYPE_DECL
26084 	  || (VAR_P (decl_or_origin) && TREE_STATIC (decl_or_origin))))
26085     {
26086       origin = decl_ultimate_origin (decl_or_origin);
26087       if (decl && VAR_P (decl) && die != NULL)
26088 	{
26089 	  die = lookup_decl_die (origin);
26090 	  if (die != NULL)
26091 	    equate_decl_number_to_die (decl, die);
26092 	}
26093       return;
26094     }
26095 
26096   if (die != NULL && die->die_parent == NULL)
26097     add_child_die (context_die, die);
26098   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
26099     {
26100       if (early_dwarf)
26101 	dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
26102 					     stmt, context_die);
26103     }
26104   else
26105     {
26106       if (decl && DECL_P (decl))
26107 	{
26108 	  die = lookup_decl_die (decl);
26109 
26110 	  /* Early created DIEs do not have a parent as the decls refer
26111 	     to the function as DECL_CONTEXT rather than the BLOCK.  */
26112 	  if (die && die->die_parent == NULL)
26113 	    {
26114 	      gcc_assert (in_lto_p);
26115 	      add_child_die (context_die, die);
26116 	    }
26117 	}
26118 
26119       gen_decl_die (decl, origin, NULL, context_die);
26120     }
26121 }
26122 
26123 /* Generate all of the decls declared within a given scope and (recursively)
26124    all of its sub-blocks.  */
26125 
26126 static void
decls_for_scope(tree stmt,dw_die_ref context_die,bool recurse)26127 decls_for_scope (tree stmt, dw_die_ref context_die, bool recurse)
26128 {
26129   tree decl;
26130   unsigned int i;
26131   tree subblocks;
26132 
26133   /* Ignore NULL blocks.  */
26134   if (stmt == NULL_TREE)
26135     return;
26136 
26137   /* Output the DIEs to represent all of the data objects and typedefs
26138      declared directly within this block but not within any nested
26139      sub-blocks.  Also, nested function and tag DIEs have been
26140      generated with a parent of NULL; fix that up now.  We don't
26141      have to do this if we're at -g1.  */
26142   if (debug_info_level > DINFO_LEVEL_TERSE)
26143     {
26144       for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
26145 	process_scope_var (stmt, decl, NULL_TREE, context_die);
26146       /* BLOCK_NONLOCALIZED_VARs simply generate DIE stubs with abstract
26147 	 origin - avoid doing this twice as we have no good way to see
26148 	 if we've done it once already.  */
26149       if (! early_dwarf)
26150 	for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
26151 	  {
26152 	    decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
26153 	    if (decl == current_function_decl)
26154 	      /* Ignore declarations of the current function, while they
26155 		 are declarations, gen_subprogram_die would treat them
26156 		 as definitions again, because they are equal to
26157 		 current_function_decl and endlessly recurse.  */;
26158 	    else if (TREE_CODE (decl) == FUNCTION_DECL)
26159 	      process_scope_var (stmt, decl, NULL_TREE, context_die);
26160 	    else
26161 	      process_scope_var (stmt, NULL_TREE, decl, context_die);
26162 	  }
26163     }
26164 
26165   /* Even if we're at -g1, we need to process the subblocks in order to get
26166      inlined call information.  */
26167 
26168   /* Output the DIEs to represent all sub-blocks (and the items declared
26169      therein) of this block.  */
26170   if (recurse)
26171     for (subblocks = BLOCK_SUBBLOCKS (stmt);
26172 	 subblocks != NULL;
26173 	 subblocks = BLOCK_CHAIN (subblocks))
26174       gen_block_die (subblocks, context_die);
26175 }
26176 
26177 /* Is this a typedef we can avoid emitting?  */
26178 
26179 static bool
is_redundant_typedef(const_tree decl)26180 is_redundant_typedef (const_tree decl)
26181 {
26182   if (TYPE_DECL_IS_STUB (decl))
26183     return true;
26184 
26185   if (DECL_ARTIFICIAL (decl)
26186       && DECL_CONTEXT (decl)
26187       && is_tagged_type (DECL_CONTEXT (decl))
26188       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
26189       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
26190     /* Also ignore the artificial member typedef for the class name.  */
26191     return true;
26192 
26193   return false;
26194 }
26195 
26196 /* Return TRUE if TYPE is a typedef that names a type for linkage
26197    purposes. This kind of typedefs is produced by the C++ FE for
26198    constructs like:
26199 
26200    typedef struct {...} foo;
26201 
26202    In that case, there is no typedef variant type produced for foo.
26203    Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
26204    struct type.  */
26205 
26206 static bool
is_naming_typedef_decl(const_tree decl)26207 is_naming_typedef_decl (const_tree decl)
26208 {
26209   if (decl == NULL_TREE
26210       || TREE_CODE (decl) != TYPE_DECL
26211       || DECL_NAMELESS (decl)
26212       || !is_tagged_type (TREE_TYPE (decl))
26213       || DECL_IS_UNDECLARED_BUILTIN (decl)
26214       || is_redundant_typedef (decl)
26215       /* It looks like Ada produces TYPE_DECLs that are very similar
26216          to C++ naming typedefs but that have different
26217          semantics. Let's be specific to c++ for now.  */
26218       || !is_cxx (decl))
26219     return FALSE;
26220 
26221   return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
26222 	  && TYPE_NAME (TREE_TYPE (decl)) == decl
26223 	  && (TYPE_STUB_DECL (TREE_TYPE (decl))
26224 	      != TYPE_NAME (TREE_TYPE (decl))));
26225 }
26226 
26227 /* Looks up the DIE for a context.  */
26228 
26229 static inline dw_die_ref
lookup_context_die(tree context)26230 lookup_context_die (tree context)
26231 {
26232   if (context)
26233     {
26234       /* Find die that represents this context.  */
26235       if (TYPE_P (context))
26236 	{
26237 	  context = TYPE_MAIN_VARIANT (context);
26238 	  dw_die_ref ctx = lookup_type_die (context);
26239 	  if (!ctx)
26240 	    return NULL;
26241 	  return strip_naming_typedef (context, ctx);
26242 	}
26243       else
26244 	return lookup_decl_die (context);
26245     }
26246   return comp_unit_die ();
26247 }
26248 
26249 /* Returns the DIE for a context.  */
26250 
26251 static inline dw_die_ref
get_context_die(tree context)26252 get_context_die (tree context)
26253 {
26254   if (context)
26255     {
26256       /* Find die that represents this context.  */
26257       if (TYPE_P (context))
26258 	{
26259 	  context = TYPE_MAIN_VARIANT (context);
26260 	  return strip_naming_typedef (context, force_type_die (context));
26261 	}
26262       else
26263 	return force_decl_die (context);
26264     }
26265   return comp_unit_die ();
26266 }
26267 
26268 /* Returns the DIE for decl.  A DIE will always be returned.  */
26269 
26270 static dw_die_ref
force_decl_die(tree decl)26271 force_decl_die (tree decl)
26272 {
26273   dw_die_ref decl_die;
26274   unsigned saved_external_flag;
26275   tree save_fn = NULL_TREE;
26276   decl_die = lookup_decl_die (decl);
26277   if (!decl_die)
26278     {
26279       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
26280 
26281       decl_die = lookup_decl_die (decl);
26282       if (decl_die)
26283 	return decl_die;
26284 
26285       switch (TREE_CODE (decl))
26286 	{
26287 	case FUNCTION_DECL:
26288 	  /* Clear current_function_decl, so that gen_subprogram_die thinks
26289 	     that this is a declaration. At this point, we just want to force
26290 	     declaration die.  */
26291 	  save_fn = current_function_decl;
26292 	  current_function_decl = NULL_TREE;
26293 	  gen_subprogram_die (decl, context_die);
26294 	  current_function_decl = save_fn;
26295 	  break;
26296 
26297 	case VAR_DECL:
26298 	  /* Set external flag to force declaration die. Restore it after
26299 	   gen_decl_die() call.  */
26300 	  saved_external_flag = DECL_EXTERNAL (decl);
26301 	  DECL_EXTERNAL (decl) = 1;
26302 	  gen_decl_die (decl, NULL, NULL, context_die);
26303 	  DECL_EXTERNAL (decl) = saved_external_flag;
26304 	  break;
26305 
26306 	case NAMESPACE_DECL:
26307 	  if (dwarf_version >= 3 || !dwarf_strict)
26308 	    dwarf2out_decl (decl);
26309 	  else
26310 	    /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
26311 	    decl_die = comp_unit_die ();
26312 	  break;
26313 
26314 	case CONST_DECL:
26315 	  /* Enumerators shouldn't need force_decl_die.  */
26316 	  gcc_assert (DECL_CONTEXT (decl) == NULL_TREE
26317 		      || TREE_CODE (DECL_CONTEXT (decl)) != ENUMERAL_TYPE);
26318 	  gen_decl_die (decl, NULL, NULL, context_die);
26319 	  break;
26320 
26321 	case TRANSLATION_UNIT_DECL:
26322 	  decl_die = comp_unit_die ();
26323 	  break;
26324 
26325 	default:
26326 	  gcc_unreachable ();
26327 	}
26328 
26329       /* We should be able to find the DIE now.  */
26330       if (!decl_die)
26331 	decl_die = lookup_decl_die (decl);
26332       gcc_assert (decl_die);
26333     }
26334 
26335   return decl_die;
26336 }
26337 
26338 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
26339    always returned.  */
26340 
26341 static dw_die_ref
force_type_die(tree type)26342 force_type_die (tree type)
26343 {
26344   dw_die_ref type_die;
26345 
26346   type_die = lookup_type_die (type);
26347   if (!type_die)
26348     {
26349       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
26350 
26351       type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
26352 				    false, context_die);
26353       gcc_assert (type_die);
26354     }
26355   return type_die;
26356 }
26357 
26358 /* Force out any required namespaces to be able to output DECL,
26359    and return the new context_die for it, if it's changed.  */
26360 
26361 static dw_die_ref
setup_namespace_context(tree thing,dw_die_ref context_die)26362 setup_namespace_context (tree thing, dw_die_ref context_die)
26363 {
26364   tree context = (DECL_P (thing)
26365 		  ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
26366   if (context && TREE_CODE (context) == NAMESPACE_DECL)
26367     /* Force out the namespace.  */
26368     context_die = force_decl_die (context);
26369 
26370   return context_die;
26371 }
26372 
26373 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
26374    type) within its namespace, if appropriate.
26375 
26376    For compatibility with older debuggers, namespace DIEs only contain
26377    declarations; all definitions are emitted at CU scope, with
26378    DW_AT_specification pointing to the declaration (like with class
26379    members).  */
26380 
26381 static dw_die_ref
declare_in_namespace(tree thing,dw_die_ref context_die)26382 declare_in_namespace (tree thing, dw_die_ref context_die)
26383 {
26384   dw_die_ref ns_context;
26385 
26386   if (debug_info_level <= DINFO_LEVEL_TERSE)
26387     return context_die;
26388 
26389   /* External declarations in the local scope only need to be emitted
26390      once, not once in the namespace and once in the scope.
26391 
26392      This avoids declaring the `extern' below in the
26393      namespace DIE as well as in the innermost scope:
26394 
26395           namespace S
26396 	  {
26397             int i=5;
26398             int foo()
26399 	    {
26400               int i=8;
26401               extern int i;
26402      	      return i;
26403 	    }
26404           }
26405   */
26406   if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
26407     return context_die;
26408 
26409   /* If this decl is from an inlined function, then don't try to emit it in its
26410      namespace, as we will get confused.  It would have already been emitted
26411      when the abstract instance of the inline function was emitted anyways.  */
26412   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
26413     return context_die;
26414 
26415   ns_context = setup_namespace_context (thing, context_die);
26416 
26417   if (ns_context != context_die)
26418     {
26419       if (is_fortran () || is_dlang ())
26420 	return ns_context;
26421       if (DECL_P (thing))
26422 	gen_decl_die (thing, NULL, NULL, ns_context);
26423       else
26424 	gen_type_die (thing, ns_context);
26425     }
26426   return context_die;
26427 }
26428 
26429 /* Generate a DIE for a namespace or namespace alias.  */
26430 
26431 static void
gen_namespace_die(tree decl,dw_die_ref context_die)26432 gen_namespace_die (tree decl, dw_die_ref context_die)
26433 {
26434   dw_die_ref namespace_die;
26435 
26436   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
26437      they are an alias of.  */
26438   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
26439     {
26440       /* Output a real namespace or module.  */
26441       context_die = setup_namespace_context (decl, comp_unit_die ());
26442       namespace_die = new_die (is_fortran () || is_dlang ()
26443 			       ? DW_TAG_module : DW_TAG_namespace,
26444 			       context_die, decl);
26445       /* For Fortran modules defined in different CU don't add src coords.  */
26446       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
26447 	{
26448 	  const char *name = dwarf2_name (decl, 0);
26449 	  if (name)
26450 	    add_name_attribute (namespace_die, name);
26451 	}
26452       else
26453 	add_name_and_src_coords_attributes (namespace_die, decl);
26454       if (DECL_EXTERNAL (decl))
26455 	add_AT_flag (namespace_die, DW_AT_declaration, 1);
26456       equate_decl_number_to_die (decl, namespace_die);
26457     }
26458   else
26459     {
26460       /* Output a namespace alias.  */
26461 
26462       /* Force out the namespace we are an alias of, if necessary.  */
26463       dw_die_ref origin_die
26464 	= force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
26465 
26466       if (DECL_FILE_SCOPE_P (decl)
26467 	  || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
26468 	context_die = setup_namespace_context (decl, comp_unit_die ());
26469       /* Now create the namespace alias DIE.  */
26470       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
26471       add_name_and_src_coords_attributes (namespace_die, decl);
26472       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
26473       equate_decl_number_to_die (decl, namespace_die);
26474     }
26475   if ((dwarf_version >= 5 || !dwarf_strict)
26476       && lang_hooks.decls.decl_dwarf_attribute (decl,
26477 						DW_AT_export_symbols) == 1)
26478     add_AT_flag (namespace_die, DW_AT_export_symbols, 1);
26479 
26480   /* Bypass dwarf2_name's check for DECL_NAMELESS.  */
26481   if (want_pubnames ())
26482     add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
26483 }
26484 
26485 /* Generate Dwarf debug information for a decl described by DECL.
26486    The return value is currently only meaningful for PARM_DECLs,
26487    for all other decls it returns NULL.
26488 
26489    If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
26490    It can be NULL otherwise.  */
26491 
26492 static dw_die_ref
gen_decl_die(tree decl,tree origin,struct vlr_context * ctx,dw_die_ref context_die)26493 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
26494 	      dw_die_ref context_die)
26495 {
26496   tree decl_or_origin = decl ? decl : origin;
26497   tree class_origin = NULL, ultimate_origin;
26498 
26499   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
26500     return NULL;
26501 
26502   switch (TREE_CODE (decl_or_origin))
26503     {
26504     case ERROR_MARK:
26505       break;
26506 
26507     case CONST_DECL:
26508       if (!is_fortran () && !is_ada () && !is_dlang ())
26509 	{
26510 	  /* The individual enumerators of an enum type get output when we output
26511 	     the Dwarf representation of the relevant enum type itself.  */
26512 	  break;
26513 	}
26514 
26515       /* Emit its type.  */
26516       gen_type_die (TREE_TYPE (decl), context_die);
26517 
26518       /* And its containing namespace.  */
26519       context_die = declare_in_namespace (decl, context_die);
26520 
26521       gen_const_die (decl, context_die);
26522       break;
26523 
26524     case FUNCTION_DECL:
26525 #if 0
26526       /* FIXME */
26527       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
26528 	 on local redeclarations of global functions.  That seems broken.  */
26529       if (current_function_decl != decl)
26530 	/* This is only a declaration.  */;
26531 #endif
26532 
26533       /* We should have abstract copies already and should not generate
26534 	 stray type DIEs in late LTO dumping.  */
26535       if (! early_dwarf)
26536 	;
26537 
26538       /* If we're emitting a clone, emit info for the abstract instance.  */
26539       else if (origin || DECL_ORIGIN (decl) != decl)
26540 	dwarf2out_abstract_function (origin
26541 				     ? DECL_ORIGIN (origin)
26542 				     : DECL_ABSTRACT_ORIGIN (decl));
26543 
26544       /* If we're emitting a possibly inlined function emit it as
26545          abstract instance.  */
26546       else if (cgraph_function_possibly_inlined_p (decl)
26547 	       && ! DECL_ABSTRACT_P (decl)
26548 	       && ! class_or_namespace_scope_p (context_die)
26549 	       /* dwarf2out_abstract_function won't emit a die if this is just
26550 		  a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
26551 		  that case, because that works only if we have a die.  */
26552 	       && DECL_INITIAL (decl) != NULL_TREE)
26553 	dwarf2out_abstract_function (decl);
26554 
26555       /* Otherwise we're emitting the primary DIE for this decl.  */
26556       else if (debug_info_level > DINFO_LEVEL_TERSE)
26557 	{
26558 	  /* Before we describe the FUNCTION_DECL itself, make sure that we
26559 	     have its containing type.  */
26560 	  if (!origin)
26561 	    origin = decl_class_context (decl);
26562 	  if (origin != NULL_TREE)
26563 	    gen_type_die (origin, context_die);
26564 
26565 	  /* And its return type.  */
26566 	  gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
26567 
26568 	  /* And its virtual context.  */
26569 	  if (DECL_VINDEX (decl) != NULL_TREE)
26570 	    gen_type_die (DECL_CONTEXT (decl), context_die);
26571 
26572 	  /* Make sure we have a member DIE for decl.  */
26573 	  if (origin != NULL_TREE)
26574 	    gen_type_die_for_member (origin, decl, context_die);
26575 
26576 	  /* And its containing namespace.  */
26577 	  context_die = declare_in_namespace (decl, context_die);
26578 	}
26579 
26580       /* Now output a DIE to represent the function itself.  */
26581       if (decl)
26582         gen_subprogram_die (decl, context_die);
26583       break;
26584 
26585     case TYPE_DECL:
26586       /* If we are in terse mode, don't generate any DIEs to represent any
26587 	 actual typedefs.  */
26588       if (debug_info_level <= DINFO_LEVEL_TERSE)
26589 	break;
26590 
26591       /* In the special case of a TYPE_DECL node representing the declaration
26592 	 of some type tag, if the given TYPE_DECL is marked as having been
26593 	 instantiated from some other (original) TYPE_DECL node (e.g. one which
26594 	 was generated within the original definition of an inline function) we
26595 	 used to generate a special (abbreviated) DW_TAG_structure_type,
26596 	 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
26597 	 should be actually referencing those DIEs, as variable DIEs with that
26598 	 type would be emitted already in the abstract origin, so it was always
26599 	 removed during unused type prunning.  Don't add anything in this
26600 	 case.  */
26601       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
26602 	break;
26603 
26604       if (is_redundant_typedef (decl))
26605 	gen_type_die (TREE_TYPE (decl), context_die);
26606       else
26607 	/* Output a DIE to represent the typedef itself.  */
26608 	gen_typedef_die (decl, context_die);
26609       break;
26610 
26611     case LABEL_DECL:
26612       if (debug_info_level >= DINFO_LEVEL_NORMAL)
26613 	gen_label_die (decl, context_die);
26614       break;
26615 
26616     case VAR_DECL:
26617     case RESULT_DECL:
26618       /* If we are in terse mode, don't generate any DIEs to represent any
26619 	 variable declarations or definitions unless it is external.  */
26620       if (debug_info_level < DINFO_LEVEL_TERSE
26621 	  || (debug_info_level == DINFO_LEVEL_TERSE
26622 	      && !TREE_PUBLIC (decl_or_origin)))
26623 	break;
26624 
26625       if (debug_info_level > DINFO_LEVEL_TERSE)
26626 	{
26627 	  /* Avoid generating stray type DIEs during late dwarf dumping.
26628 	     All types have been dumped early.  */
26629 	  if (early_dwarf
26630 	      /* ???  But in LTRANS we cannot annotate early created variably
26631 		 modified type DIEs without copying them and adjusting all
26632 		 references to them.  Dump them again as happens for inlining
26633 		 which copies both the decl and the types.  */
26634 	      /* ???  And even non-LTO needs to re-visit type DIEs to fill
26635 		 in VLA bound information for example.  */
26636 	      || (decl && variably_modified_type_p (TREE_TYPE (decl),
26637 						    current_function_decl)))
26638 	    {
26639 	      /* Output any DIEs that are needed to specify the type of this data
26640 		 object.  */
26641 	      if (decl_by_reference_p (decl_or_origin))
26642 		gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
26643 	      else
26644 		gen_type_die (TREE_TYPE (decl_or_origin), context_die);
26645 	    }
26646 
26647 	  if (early_dwarf)
26648 	    {
26649 	      /* And its containing type.  */
26650 	      class_origin = decl_class_context (decl_or_origin);
26651 	      if (class_origin != NULL_TREE)
26652 		gen_type_die_for_member (class_origin, decl_or_origin, context_die);
26653 
26654 	      /* And its containing namespace.  */
26655 	      context_die = declare_in_namespace (decl_or_origin, context_die);
26656 	    }
26657 	}
26658 
26659       /* Now output the DIE to represent the data object itself.  This gets
26660 	 complicated because of the possibility that the VAR_DECL really
26661 	 represents an inlined instance of a formal parameter for an inline
26662 	 function.  */
26663       ultimate_origin = decl_ultimate_origin (decl_or_origin);
26664       if (ultimate_origin != NULL_TREE
26665 	  && TREE_CODE (ultimate_origin) == PARM_DECL)
26666 	gen_formal_parameter_die (decl, origin,
26667 				  true /* Emit name attribute.  */,
26668 				  context_die);
26669       else
26670 	gen_variable_die (decl, origin, context_die);
26671       break;
26672 
26673     case FIELD_DECL:
26674       gcc_assert (ctx != NULL && ctx->struct_type != NULL);
26675       /* Ignore the nameless fields that are used to skip bits but handle C++
26676 	 anonymous unions and structs.  */
26677       if (DECL_NAME (decl) != NULL_TREE
26678 	  || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
26679 	  || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
26680 	{
26681 	  gen_type_die (member_declared_type (decl), context_die);
26682 	  gen_field_die (decl, ctx, context_die);
26683 	}
26684       break;
26685 
26686     case PARM_DECL:
26687       /* Avoid generating stray type DIEs during late dwarf dumping.
26688          All types have been dumped early.  */
26689       if (early_dwarf
26690 	  /* ???  But in LTRANS we cannot annotate early created variably
26691 	     modified type DIEs without copying them and adjusting all
26692 	     references to them.  Dump them again as happens for inlining
26693 	     which copies both the decl and the types.  */
26694 	  /* ???  And even non-LTO needs to re-visit type DIEs to fill
26695 	     in VLA bound information for example.  */
26696 	  || (decl && variably_modified_type_p (TREE_TYPE (decl),
26697 						current_function_decl)))
26698 	{
26699 	  if (DECL_BY_REFERENCE (decl_or_origin))
26700 	    gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
26701 	  else
26702 	    gen_type_die (TREE_TYPE (decl_or_origin), context_die);
26703 	}
26704       return gen_formal_parameter_die (decl, origin,
26705 				       true /* Emit name attribute.  */,
26706 				       context_die);
26707 
26708     case NAMESPACE_DECL:
26709       if (dwarf_version >= 3 || !dwarf_strict)
26710 	gen_namespace_die (decl, context_die);
26711       break;
26712 
26713     case IMPORTED_DECL:
26714       dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
26715 					   DECL_CONTEXT (decl), context_die);
26716       break;
26717 
26718     case NAMELIST_DECL:
26719       gen_namelist_decl (DECL_NAME (decl), context_die,
26720 			 NAMELIST_DECL_ASSOCIATED_DECL (decl));
26721       break;
26722 
26723     default:
26724       /* Probably some frontend-internal decl.  Assume we don't care.  */
26725       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
26726       break;
26727     }
26728 
26729   return NULL;
26730 }
26731 
26732 /* Output initial debug information for global DECL.  Called at the
26733    end of the parsing process.
26734 
26735    This is the initial debug generation process.  As such, the DIEs
26736    generated may be incomplete.  A later debug generation pass
26737    (dwarf2out_late_global_decl) will augment the information generated
26738    in this pass (e.g., with complete location info).  */
26739 
26740 static void
dwarf2out_early_global_decl(tree decl)26741 dwarf2out_early_global_decl (tree decl)
26742 {
26743   set_early_dwarf s;
26744 
26745   /* gen_decl_die() will set DECL_ABSTRACT because
26746      cgraph_function_possibly_inlined_p() returns true.  This is in
26747      turn will cause DW_AT_inline attributes to be set.
26748 
26749      This happens because at early dwarf generation, there is no
26750      cgraph information, causing cgraph_function_possibly_inlined_p()
26751      to return true.  Trick cgraph_function_possibly_inlined_p()
26752      while we generate dwarf early.  */
26753   bool save = symtab->global_info_ready;
26754   symtab->global_info_ready = true;
26755 
26756   /* We don't handle TYPE_DECLs.  If required, they'll be reached via
26757      other DECLs and they can point to template types or other things
26758      that dwarf2out can't handle when done via dwarf2out_decl.  */
26759   if (TREE_CODE (decl) != TYPE_DECL
26760       && TREE_CODE (decl) != PARM_DECL)
26761     {
26762       if (TREE_CODE (decl) == FUNCTION_DECL)
26763 	{
26764 	  tree save_fndecl = current_function_decl;
26765 
26766 	  /* For nested functions, make sure we have DIEs for the parents first
26767 	     so that all nested DIEs are generated at the proper scope in the
26768 	     first shot.  */
26769 	  tree context = decl_function_context (decl);
26770 	  if (context != NULL)
26771 	    {
26772 	      dw_die_ref context_die = lookup_decl_die (context);
26773 	      current_function_decl = context;
26774 
26775 	      /* Avoid emitting DIEs multiple times, but still process CONTEXT
26776 		 enough so that it lands in its own context.  This avoids type
26777 		 pruning issues later on.  */
26778 	      if (context_die == NULL || is_declaration_die (context_die))
26779 		dwarf2out_early_global_decl (context);
26780 	    }
26781 
26782 	  /* Emit an abstract origin of a function first.  This happens
26783 	     with C++ constructor clones for example and makes
26784 	     dwarf2out_abstract_function happy which requires the early
26785 	     DIE of the abstract instance to be present.  */
26786 	  tree origin = DECL_ABSTRACT_ORIGIN (decl);
26787 	  dw_die_ref origin_die;
26788 	  if (origin != NULL
26789 	      /* Do not emit the DIE multiple times but make sure to
26790 	         process it fully here in case we just saw a declaration.  */
26791 	      && ((origin_die = lookup_decl_die (origin)) == NULL
26792 		  || is_declaration_die (origin_die)))
26793 	    {
26794 	      current_function_decl = origin;
26795 	      dwarf2out_decl (origin);
26796 	    }
26797 
26798 	  /* Emit the DIE for decl but avoid doing that multiple times.  */
26799 	  dw_die_ref old_die;
26800 	  if ((old_die = lookup_decl_die (decl)) == NULL
26801 	      || is_declaration_die (old_die))
26802 	    {
26803 	      current_function_decl = decl;
26804 	      dwarf2out_decl (decl);
26805 	    }
26806 
26807 	  current_function_decl = save_fndecl;
26808 	}
26809       else
26810 	dwarf2out_decl (decl);
26811     }
26812   symtab->global_info_ready = save;
26813 }
26814 
26815 /* Return whether EXPR is an expression with the following pattern:
26816    INDIRECT_REF (NOP_EXPR (INTEGER_CST)).  */
26817 
26818 static bool
is_trivial_indirect_ref(tree expr)26819 is_trivial_indirect_ref (tree expr)
26820 {
26821   if (expr == NULL_TREE || TREE_CODE (expr) != INDIRECT_REF)
26822     return false;
26823 
26824   tree nop = TREE_OPERAND (expr, 0);
26825   if (nop == NULL_TREE || TREE_CODE (nop) != NOP_EXPR)
26826     return false;
26827 
26828   tree int_cst = TREE_OPERAND (nop, 0);
26829   return int_cst != NULL_TREE && TREE_CODE (int_cst) == INTEGER_CST;
26830 }
26831 
26832 /* Output debug information for global decl DECL.  Called from
26833    toplev.c after compilation proper has finished.  */
26834 
26835 static void
dwarf2out_late_global_decl(tree decl)26836 dwarf2out_late_global_decl (tree decl)
26837 {
26838   /* Fill-in any location information we were unable to determine
26839      on the first pass.  */
26840   if (VAR_P (decl))
26841     {
26842       dw_die_ref die = lookup_decl_die (decl);
26843 
26844       /* We may have to generate full debug late for LTO in case debug
26845          was not enabled at compile-time or the target doesn't support
26846 	 the LTO early debug scheme.  */
26847       if (! die && in_lto_p)
26848 	dwarf2out_decl (decl);
26849       else if (die)
26850 	{
26851 	  /* We get called via the symtab code invoking late_global_decl
26852 	     for symbols that are optimized out.
26853 
26854 	     Do not add locations for those, except if they have a
26855 	     DECL_VALUE_EXPR, in which case they are relevant for debuggers.
26856 	     Still don't add a location if the DECL_VALUE_EXPR is not a trivial
26857 	     INDIRECT_REF expression, as this could generate relocations to
26858 	     text symbols in LTO object files, which is invalid.  */
26859 	  varpool_node *node = varpool_node::get (decl);
26860 	  if ((! node || ! node->definition)
26861 	      && ! (DECL_HAS_VALUE_EXPR_P (decl)
26862 		    && is_trivial_indirect_ref (DECL_VALUE_EXPR (decl))))
26863 	    tree_add_const_value_attribute_for_decl (die, decl);
26864 	  else
26865 	    add_location_or_const_value_attribute (die, decl, false);
26866 	}
26867     }
26868 }
26869 
26870 /* Output debug information for type decl DECL.  Called from toplev.c
26871    and from language front ends (to record built-in types).  */
26872 static void
dwarf2out_type_decl(tree decl,int local)26873 dwarf2out_type_decl (tree decl, int local)
26874 {
26875   if (!local)
26876     {
26877       set_early_dwarf s;
26878       dwarf2out_decl (decl);
26879     }
26880 }
26881 
26882 /* Output debug information for imported module or decl DECL.
26883    NAME is non-NULL name in the lexical block if the decl has been renamed.
26884    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
26885    that DECL belongs to.
26886    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
26887 static void
dwarf2out_imported_module_or_decl_1(tree decl,tree name,tree lexical_block,dw_die_ref lexical_block_die)26888 dwarf2out_imported_module_or_decl_1 (tree decl,
26889 				     tree name,
26890 				     tree lexical_block,
26891 				     dw_die_ref lexical_block_die)
26892 {
26893   expanded_location xloc;
26894   dw_die_ref imported_die = NULL;
26895   dw_die_ref at_import_die;
26896 
26897   if (TREE_CODE (decl) == IMPORTED_DECL)
26898     {
26899       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
26900       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
26901       gcc_assert (decl);
26902     }
26903   else
26904     xloc = expand_location (input_location);
26905 
26906   if (TREE_CODE (decl) == TYPE_DECL)
26907     {
26908       at_import_die = force_type_die (TREE_TYPE (decl));
26909       /* For namespace N { typedef void T; } using N::T; base_type_die
26910 	 returns NULL, but DW_TAG_imported_declaration requires
26911 	 the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
26912       if (!at_import_die)
26913 	{
26914 	  gcc_assert (TREE_CODE (decl) == TYPE_DECL);
26915 	  gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
26916 	  at_import_die = lookup_type_die (TREE_TYPE (decl));
26917 	  gcc_assert (at_import_die);
26918 	}
26919     }
26920   else
26921     {
26922       at_import_die = lookup_decl_die (decl);
26923       if (!at_import_die)
26924 	{
26925 	  /* If we're trying to avoid duplicate debug info, we may not have
26926 	     emitted the member decl for this field.  Emit it now.  */
26927 	  if (TREE_CODE (decl) == FIELD_DECL)
26928 	    {
26929 	      tree type = DECL_CONTEXT (decl);
26930 
26931 	      if (TYPE_CONTEXT (type)
26932 		  && TYPE_P (TYPE_CONTEXT (type))
26933 		  && !should_emit_struct_debug (TYPE_CONTEXT (type),
26934 						DINFO_USAGE_DIR_USE))
26935 		return;
26936 	      gen_type_die_for_member (type, decl,
26937 				       get_context_die (TYPE_CONTEXT (type)));
26938 	    }
26939 	  if (TREE_CODE (decl) == CONST_DECL)
26940 	    {
26941 	      /* Individual enumerators of an enum type do not get output here
26942 		 (see gen_decl_die), so we cannot call force_decl_die.  */
26943 	      if (!is_fortran () && !is_ada () && !is_dlang ())
26944 		return;
26945 	    }
26946 	  if (TREE_CODE (decl) == NAMELIST_DECL)
26947 	    at_import_die = gen_namelist_decl (DECL_NAME (decl),
26948 					 get_context_die (DECL_CONTEXT (decl)),
26949 					 NULL_TREE);
26950 	  else
26951 	    at_import_die = force_decl_die (decl);
26952 	}
26953     }
26954 
26955   if (TREE_CODE (decl) == NAMESPACE_DECL)
26956     {
26957       if (dwarf_version >= 3 || !dwarf_strict)
26958 	imported_die = new_die (DW_TAG_imported_module,
26959 				lexical_block_die,
26960 				lexical_block);
26961       else
26962 	return;
26963     }
26964   else
26965     imported_die = new_die (DW_TAG_imported_declaration,
26966 			    lexical_block_die,
26967 			    lexical_block);
26968 
26969   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
26970   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
26971   if (debug_column_info && xloc.column)
26972     add_AT_unsigned (imported_die, DW_AT_decl_column, xloc.column);
26973   if (name)
26974     add_AT_string (imported_die, DW_AT_name,
26975 		   IDENTIFIER_POINTER (name));
26976   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
26977 }
26978 
26979 /* Output debug information for imported module or decl DECL.
26980    NAME is non-NULL name in context if the decl has been renamed.
26981    CHILD is true if decl is one of the renamed decls as part of
26982    importing whole module.
26983    IMPLICIT is set if this hook is called for an implicit import
26984    such as inline namespace.  */
26985 
26986 static void
dwarf2out_imported_module_or_decl(tree decl,tree name,tree context,bool child,bool implicit)26987 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
26988 				   bool child, bool implicit)
26989 {
26990   /* dw_die_ref at_import_die;  */
26991   dw_die_ref scope_die;
26992 
26993   if (debug_info_level <= DINFO_LEVEL_TERSE)
26994     return;
26995 
26996   gcc_assert (decl);
26997 
26998   /* For DWARF5, just DW_AT_export_symbols on the DW_TAG_namespace
26999      should be enough, for DWARF4 and older even if we emit as extension
27000      DW_AT_export_symbols add the implicit DW_TAG_imported_module anyway
27001      for the benefit of consumers unaware of DW_AT_export_symbols.  */
27002   if (implicit
27003       && dwarf_version >= 5
27004       && lang_hooks.decls.decl_dwarf_attribute (decl,
27005 						DW_AT_export_symbols) == 1)
27006     return;
27007 
27008   set_early_dwarf s;
27009 
27010   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
27011      We need decl DIE for reference and scope die. First, get DIE for the decl
27012      itself.  */
27013 
27014   /* Get the scope die for decl context. Use comp_unit_die for global module
27015      or decl. If die is not found for non globals, force new die.  */
27016   if (context
27017       && TYPE_P (context)
27018       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
27019     return;
27020 
27021   scope_die = get_context_die (context);
27022 
27023   if (child)
27024     {
27025       /* DW_TAG_imported_module was introduced in the DWARFv3 specification, so
27026 	 there is nothing we can do, here.  */
27027       if (dwarf_version < 3 && dwarf_strict)
27028 	return;
27029 
27030       gcc_assert (scope_die->die_child);
27031       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
27032       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
27033       scope_die = scope_die->die_child;
27034     }
27035 
27036   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
27037   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
27038 }
27039 
27040 /* Output debug information for namelists.   */
27041 
27042 static dw_die_ref
gen_namelist_decl(tree name,dw_die_ref scope_die,tree item_decls)27043 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
27044 {
27045   dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
27046   tree value;
27047   unsigned i;
27048 
27049   if (debug_info_level <= DINFO_LEVEL_TERSE)
27050     return NULL;
27051 
27052   gcc_assert (scope_die != NULL);
27053   nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
27054   add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
27055 
27056   /* If there are no item_decls, we have a nondefining namelist, e.g.
27057      with USE association; hence, set DW_AT_declaration.  */
27058   if (item_decls == NULL_TREE)
27059     {
27060       add_AT_flag (nml_die, DW_AT_declaration, 1);
27061       return nml_die;
27062     }
27063 
27064   FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
27065     {
27066       nml_item_ref_die = lookup_decl_die (value);
27067       if (!nml_item_ref_die)
27068 	nml_item_ref_die = force_decl_die (value);
27069 
27070       nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
27071       add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
27072     }
27073   return nml_die;
27074 }
27075 
27076 
27077 /* Write the debugging output for DECL and return the DIE.  */
27078 
27079 static void
dwarf2out_decl(tree decl)27080 dwarf2out_decl (tree decl)
27081 {
27082   dw_die_ref context_die = comp_unit_die ();
27083 
27084   switch (TREE_CODE (decl))
27085     {
27086     case ERROR_MARK:
27087       return;
27088 
27089     case FUNCTION_DECL:
27090       /* If we're a nested function, initially use a parent of NULL; if we're
27091 	 a plain function, this will be fixed up in decls_for_scope.  If
27092 	 we're a method, it will be ignored, since we already have a DIE.
27093 	 Avoid doing this late though since clones of class methods may
27094 	 otherwise end up in limbo and create type DIEs late.  */
27095       if (early_dwarf
27096 	  && decl_function_context (decl)
27097 	  /* But if we're in terse mode, we don't care about scope.  */
27098 	  && debug_info_level > DINFO_LEVEL_TERSE)
27099 	context_die = NULL;
27100       break;
27101 
27102     case VAR_DECL:
27103       /* For local statics lookup proper context die.  */
27104       if (local_function_static (decl))
27105 	context_die = lookup_decl_die (DECL_CONTEXT (decl));
27106 
27107       /* If we are in terse mode, don't generate any DIEs to represent any
27108 	 variable declarations or definitions unless it is external.  */
27109       if (debug_info_level < DINFO_LEVEL_TERSE
27110 	  || (debug_info_level == DINFO_LEVEL_TERSE
27111 	      && !TREE_PUBLIC (decl)))
27112 	return;
27113       break;
27114 
27115     case CONST_DECL:
27116       if (debug_info_level <= DINFO_LEVEL_TERSE)
27117 	return;
27118       if (!is_fortran () && !is_ada () && !is_dlang ())
27119 	return;
27120       if (TREE_STATIC (decl) && decl_function_context (decl))
27121 	context_die = lookup_decl_die (DECL_CONTEXT (decl));
27122       break;
27123 
27124     case NAMESPACE_DECL:
27125     case IMPORTED_DECL:
27126       if (debug_info_level <= DINFO_LEVEL_TERSE)
27127 	return;
27128       if (lookup_decl_die (decl) != NULL)
27129 	return;
27130       break;
27131 
27132     case TYPE_DECL:
27133       /* Don't emit stubs for types unless they are needed by other DIEs.  */
27134       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
27135 	return;
27136 
27137       /* Don't bother trying to generate any DIEs to represent any of the
27138 	 normal built-in types for the language we are compiling.  */
27139       if (DECL_IS_UNDECLARED_BUILTIN (decl))
27140 	return;
27141 
27142       /* If we are in terse mode, don't generate any DIEs for types.  */
27143       if (debug_info_level <= DINFO_LEVEL_TERSE)
27144 	return;
27145 
27146       /* If we're a function-scope tag, initially use a parent of NULL;
27147 	 this will be fixed up in decls_for_scope.  */
27148       if (decl_function_context (decl))
27149 	context_die = NULL;
27150 
27151       break;
27152 
27153     case NAMELIST_DECL:
27154       break;
27155 
27156     default:
27157       return;
27158     }
27159 
27160   gen_decl_die (decl, NULL, NULL, context_die);
27161 
27162   if (flag_checking)
27163     {
27164       dw_die_ref die = lookup_decl_die (decl);
27165       if (die)
27166 	check_die (die);
27167     }
27168 }
27169 
27170 /* Write the debugging output for DECL.  */
27171 
27172 static void
dwarf2out_function_decl(tree decl)27173 dwarf2out_function_decl (tree decl)
27174 {
27175   dwarf2out_decl (decl);
27176   call_arg_locations = NULL;
27177   call_arg_loc_last = NULL;
27178   call_site_count = -1;
27179   tail_call_site_count = -1;
27180   decl_loc_table->empty ();
27181   cached_dw_loc_list_table->empty ();
27182 }
27183 
27184 /* Output a marker (i.e. a label) for the beginning of the generated code for
27185    a lexical block.  */
27186 
27187 static void
dwarf2out_begin_block(unsigned int line ATTRIBUTE_UNUSED,unsigned int blocknum)27188 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
27189 		       unsigned int blocknum)
27190 {
27191   switch_to_section (current_function_section ());
27192   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
27193 }
27194 
27195 /* Output a marker (i.e. a label) for the end of the generated code for a
27196    lexical block.  */
27197 
27198 static void
dwarf2out_end_block(unsigned int line ATTRIBUTE_UNUSED,unsigned int blocknum)27199 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
27200 {
27201   switch_to_section (current_function_section ());
27202   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
27203 }
27204 
27205 /* Returns nonzero if it is appropriate not to emit any debugging
27206    information for BLOCK, because it doesn't contain any instructions.
27207 
27208    Don't allow this for blocks with nested functions or local classes
27209    as we would end up with orphans, and in the presence of scheduling
27210    we may end up calling them anyway.  */
27211 
27212 static bool
dwarf2out_ignore_block(const_tree block)27213 dwarf2out_ignore_block (const_tree block)
27214 {
27215   tree decl;
27216   unsigned int i;
27217 
27218   for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
27219     if (TREE_CODE (decl) == FUNCTION_DECL
27220 	|| (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
27221       return 0;
27222   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
27223     {
27224       decl = BLOCK_NONLOCALIZED_VAR (block, i);
27225       if (TREE_CODE (decl) == FUNCTION_DECL
27226 	  || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
27227       return 0;
27228     }
27229 
27230   return 1;
27231 }
27232 
27233 /* Hash table routines for file_hash.  */
27234 
27235 bool
equal(dwarf_file_data * p1,const char * p2)27236 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
27237 {
27238   return filename_cmp (p1->filename, p2) == 0;
27239 }
27240 
27241 hashval_t
hash(dwarf_file_data * p)27242 dwarf_file_hasher::hash (dwarf_file_data *p)
27243 {
27244   return htab_hash_string (p->filename);
27245 }
27246 
27247 /* Lookup FILE_NAME (in the list of filenames that we know about here in
27248    dwarf2out.c) and return its "index".  The index of each (known) filename is
27249    just a unique number which is associated with only that one filename.  We
27250    need such numbers for the sake of generating labels (in the .debug_sfnames
27251    section) and references to those files numbers (in the .debug_srcinfo
27252    and .debug_macinfo sections).  If the filename given as an argument is not
27253    found in our current list, add it to the list and assign it the next
27254    available unique index number.  */
27255 
27256 static struct dwarf_file_data *
lookup_filename(const char * file_name)27257 lookup_filename (const char *file_name)
27258 {
27259   struct dwarf_file_data * created;
27260 
27261   if (!file_name)
27262     return NULL;
27263 
27264   if (!file_name[0])
27265     file_name = "<stdin>";
27266 
27267   dwarf_file_data **slot
27268     = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
27269 				       INSERT);
27270   if (*slot)
27271     return *slot;
27272 
27273   created = ggc_alloc<dwarf_file_data> ();
27274   created->filename = file_name;
27275   created->emitted_number = 0;
27276   *slot = created;
27277   return created;
27278 }
27279 
27280 /* If the assembler will construct the file table, then translate the compiler
27281    internal file table number into the assembler file table number, and emit
27282    a .file directive if we haven't already emitted one yet.  The file table
27283    numbers are different because we prune debug info for unused variables and
27284    types, which may include filenames.  */
27285 
27286 static int
maybe_emit_file(struct dwarf_file_data * fd)27287 maybe_emit_file (struct dwarf_file_data * fd)
27288 {
27289   if (! fd->emitted_number)
27290     {
27291       if (last_emitted_file)
27292 	fd->emitted_number = last_emitted_file->emitted_number + 1;
27293       else
27294 	fd->emitted_number = 1;
27295       last_emitted_file = fd;
27296 
27297       if (output_asm_line_debug_info ())
27298 	{
27299 	  fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
27300 	  output_quoted_string (asm_out_file,
27301 				remap_debug_filename (fd->filename));
27302 	  fputc ('\n', asm_out_file);
27303 	}
27304     }
27305 
27306   return fd->emitted_number;
27307 }
27308 
27309 /* Schedule generation of a DW_AT_const_value attribute to DIE.
27310    That generation should happen after function debug info has been
27311    generated. The value of the attribute is the constant value of ARG.  */
27312 
27313 static void
append_entry_to_tmpl_value_parm_die_table(dw_die_ref die,tree arg)27314 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
27315 {
27316   die_arg_entry entry;
27317 
27318   if (!die || !arg)
27319     return;
27320 
27321   gcc_assert (early_dwarf);
27322 
27323   if (!tmpl_value_parm_die_table)
27324     vec_alloc (tmpl_value_parm_die_table, 32);
27325 
27326   entry.die = die;
27327   entry.arg = arg;
27328   vec_safe_push (tmpl_value_parm_die_table, entry);
27329 }
27330 
27331 /* Return TRUE if T is an instance of generic type, FALSE
27332    otherwise.  */
27333 
27334 static bool
generic_type_p(tree t)27335 generic_type_p (tree t)
27336 {
27337   if (t == NULL_TREE || !TYPE_P (t))
27338     return false;
27339   return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
27340 }
27341 
27342 /* Schedule the generation of the generic parameter dies for the
27343   instance of generic type T. The proper generation itself is later
27344   done by gen_scheduled_generic_parms_dies. */
27345 
27346 static void
schedule_generic_params_dies_gen(tree t)27347 schedule_generic_params_dies_gen (tree t)
27348 {
27349   if (!generic_type_p (t))
27350     return;
27351 
27352   gcc_assert (early_dwarf);
27353 
27354   if (!generic_type_instances)
27355     vec_alloc (generic_type_instances, 256);
27356 
27357   vec_safe_push (generic_type_instances, t);
27358 }
27359 
27360 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
27361    by append_entry_to_tmpl_value_parm_die_table. This function must
27362    be called after function DIEs have been generated.  */
27363 
27364 static void
gen_remaining_tmpl_value_param_die_attribute(void)27365 gen_remaining_tmpl_value_param_die_attribute (void)
27366 {
27367   if (tmpl_value_parm_die_table)
27368     {
27369       unsigned i, j;
27370       die_arg_entry *e;
27371 
27372       /* We do this in two phases - first get the cases we can
27373 	 handle during early-finish, preserving those we cannot
27374 	 (containing symbolic constants where we don't yet know
27375 	 whether we are going to output the referenced symbols).
27376 	 For those we try again at late-finish.  */
27377       j = 0;
27378       FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
27379 	{
27380 	  if (!e->die->removed
27381 	      && !tree_add_const_value_attribute (e->die, e->arg))
27382 	    {
27383 	      dw_loc_descr_ref loc = NULL;
27384 	      if (! early_dwarf
27385 		  && (dwarf_version >= 5 || !dwarf_strict))
27386 		loc = loc_descriptor_from_tree (e->arg, 2, NULL);
27387 	      if (loc)
27388 		add_AT_loc (e->die, DW_AT_location, loc);
27389 	      else
27390 		(*tmpl_value_parm_die_table)[j++] = *e;
27391 	    }
27392 	}
27393       tmpl_value_parm_die_table->truncate (j);
27394     }
27395 }
27396 
27397 /* Generate generic parameters DIEs for instances of generic types
27398    that have been previously scheduled by
27399    schedule_generic_params_dies_gen. This function must be called
27400    after all the types of the CU have been laid out.  */
27401 
27402 static void
gen_scheduled_generic_parms_dies(void)27403 gen_scheduled_generic_parms_dies (void)
27404 {
27405   unsigned i;
27406   tree t;
27407 
27408   if (!generic_type_instances)
27409     return;
27410 
27411   FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
27412     if (COMPLETE_TYPE_P (t))
27413       gen_generic_params_dies (t);
27414 
27415   generic_type_instances = NULL;
27416 }
27417 
27418 
27419 /* Replace DW_AT_name for the decl with name.  */
27420 
27421 static void
dwarf2out_set_name(tree decl,tree name)27422 dwarf2out_set_name (tree decl, tree name)
27423 {
27424   dw_die_ref die;
27425   dw_attr_node *attr;
27426   const char *dname;
27427 
27428   die = TYPE_SYMTAB_DIE (decl);
27429   if (!die)
27430     return;
27431 
27432   dname = dwarf2_name (name, 0);
27433   if (!dname)
27434     return;
27435 
27436   attr = get_AT (die, DW_AT_name);
27437   if (attr)
27438     {
27439       struct indirect_string_node *node;
27440 
27441       node = find_AT_string (dname);
27442       /* replace the string.  */
27443       attr->dw_attr_val.v.val_str = node;
27444     }
27445 
27446   else
27447     add_name_attribute (die, dname);
27448 }
27449 
27450 /* True if before or during processing of the first function being emitted.  */
27451 static bool in_first_function_p = true;
27452 /* True if loc_note during dwarf2out_var_location call might still be
27453    before first real instruction at address equal to .Ltext0.  */
27454 static bool maybe_at_text_label_p = true;
27455 /* One above highest N where .LVLN label might be equal to .Ltext0 label.  */
27456 static unsigned int first_loclabel_num_not_at_text_label;
27457 
27458 /* Look ahead for a real insn.  */
27459 
27460 static rtx_insn *
dwarf2out_next_real_insn(rtx_insn * loc_note)27461 dwarf2out_next_real_insn (rtx_insn *loc_note)
27462 {
27463   rtx_insn *next_real = NEXT_INSN (loc_note);
27464 
27465   while (next_real)
27466     if (INSN_P (next_real))
27467       break;
27468     else
27469       next_real = NEXT_INSN (next_real);
27470 
27471   return next_real;
27472 }
27473 
27474 /* Called by the final INSN scan whenever we see a var location.  We
27475    use it to drop labels in the right places, and throw the location in
27476    our lookup table.  */
27477 
27478 static void
dwarf2out_var_location(rtx_insn * loc_note)27479 dwarf2out_var_location (rtx_insn *loc_note)
27480 {
27481   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
27482   struct var_loc_node *newloc;
27483   rtx_insn *next_real;
27484   rtx_insn *call_insn = NULL;
27485   static const char *last_label;
27486   static const char *last_postcall_label;
27487   static bool last_in_cold_section_p;
27488   static rtx_insn *expected_next_loc_note;
27489   tree decl;
27490   bool var_loc_p;
27491   var_loc_view view = 0;
27492 
27493   if (!NOTE_P (loc_note))
27494     {
27495       if (CALL_P (loc_note))
27496 	{
27497 	  maybe_reset_location_view (loc_note, cur_line_info_table);
27498 	  call_site_count++;
27499 	  if (SIBLING_CALL_P (loc_note))
27500 	    tail_call_site_count++;
27501 	  if (find_reg_note (loc_note, REG_CALL_ARG_LOCATION, NULL_RTX))
27502 	    {
27503 	      call_insn = loc_note;
27504 	      loc_note = NULL;
27505 	      var_loc_p = false;
27506 
27507 	      next_real = dwarf2out_next_real_insn (call_insn);
27508 	      cached_next_real_insn = NULL;
27509 	      goto create_label;
27510 	    }
27511 	  if (optimize == 0 && !flag_var_tracking)
27512 	    {
27513 	      /* When the var-tracking pass is not running, there is no note
27514 		 for indirect calls whose target is compile-time known. In this
27515 		 case, process such calls specifically so that we generate call
27516 		 sites for them anyway.  */
27517 	      rtx x = PATTERN (loc_note);
27518 	      if (GET_CODE (x) == PARALLEL)
27519 		x = XVECEXP (x, 0, 0);
27520 	      if (GET_CODE (x) == SET)
27521 		x = SET_SRC (x);
27522 	      if (GET_CODE (x) == CALL)
27523 		x = XEXP (x, 0);
27524 	      if (!MEM_P (x)
27525 		  || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
27526 		  || !SYMBOL_REF_DECL (XEXP (x, 0))
27527 		  || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
27528 		      != FUNCTION_DECL))
27529 		{
27530 		  call_insn = loc_note;
27531 		  loc_note = NULL;
27532 		  var_loc_p = false;
27533 
27534 		  next_real = dwarf2out_next_real_insn (call_insn);
27535 		  cached_next_real_insn = NULL;
27536 		  goto create_label;
27537 		}
27538 	    }
27539 	}
27540       else if (!debug_variable_location_views)
27541 	gcc_unreachable ();
27542       else
27543 	maybe_reset_location_view (loc_note, cur_line_info_table);
27544 
27545       return;
27546     }
27547 
27548   var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
27549   if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
27550     return;
27551 
27552   /* Optimize processing a large consecutive sequence of location
27553      notes so we don't spend too much time in next_real_insn.  If the
27554      next insn is another location note, remember the next_real_insn
27555      calculation for next time.  */
27556   next_real = cached_next_real_insn;
27557   if (next_real)
27558     {
27559       if (expected_next_loc_note != loc_note)
27560 	next_real = NULL;
27561     }
27562 
27563   if (! next_real)
27564     next_real = dwarf2out_next_real_insn (loc_note);
27565 
27566   if (next_real)
27567     {
27568       rtx_insn *next_note = NEXT_INSN (loc_note);
27569       while (next_note != next_real)
27570 	{
27571 	  if (! next_note->deleted ()
27572 	      && NOTE_P (next_note)
27573 	      && NOTE_KIND (next_note) == NOTE_INSN_VAR_LOCATION)
27574 	    break;
27575 	  next_note = NEXT_INSN (next_note);
27576 	}
27577 
27578       if (next_note == next_real)
27579 	cached_next_real_insn = NULL;
27580       else
27581 	{
27582 	  expected_next_loc_note = next_note;
27583 	  cached_next_real_insn = next_real;
27584 	}
27585     }
27586   else
27587     cached_next_real_insn = NULL;
27588 
27589   /* If there are no instructions which would be affected by this note,
27590      don't do anything.  */
27591   if (var_loc_p
27592       && next_real == NULL_RTX
27593       && !NOTE_DURING_CALL_P (loc_note))
27594     return;
27595 
27596 create_label:
27597 
27598   if (next_real == NULL_RTX)
27599     next_real = get_last_insn ();
27600 
27601   /* If there were any real insns between note we processed last time
27602      and this note (or if it is the first note), clear
27603      last_{,postcall_}label so that they are not reused this time.  */
27604   if (last_var_location_insn == NULL_RTX
27605       || last_var_location_insn != next_real
27606       || last_in_cold_section_p != in_cold_section_p)
27607     {
27608       last_label = NULL;
27609       last_postcall_label = NULL;
27610     }
27611 
27612   if (var_loc_p)
27613     {
27614       const char *label
27615 	= NOTE_DURING_CALL_P (loc_note) ? last_postcall_label : last_label;
27616       view = cur_line_info_table->view;
27617       decl = NOTE_VAR_LOCATION_DECL (loc_note);
27618       newloc = add_var_loc_to_decl (decl, loc_note, label, view);
27619       if (newloc == NULL)
27620 	return;
27621     }
27622   else
27623     {
27624       decl = NULL_TREE;
27625       newloc = NULL;
27626     }
27627 
27628   /* If there were no real insns between note we processed last time
27629      and this note, use the label we emitted last time.  Otherwise
27630      create a new label and emit it.  */
27631   if (last_label == NULL)
27632     {
27633       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
27634       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
27635       loclabel_num++;
27636       last_label = ggc_strdup (loclabel);
27637       /* See if loclabel might be equal to .Ltext0.  If yes,
27638 	 bump first_loclabel_num_not_at_text_label.  */
27639       if (!have_multiple_function_sections
27640 	  && in_first_function_p
27641 	  && maybe_at_text_label_p)
27642 	{
27643 	  static rtx_insn *last_start;
27644 	  rtx_insn *insn;
27645 	  for (insn = loc_note; insn; insn = previous_insn (insn))
27646 	    if (insn == last_start)
27647 	      break;
27648 	    else if (!NONDEBUG_INSN_P (insn))
27649 	      continue;
27650 	    else
27651 	      {
27652 		rtx body = PATTERN (insn);
27653 		if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
27654 		  continue;
27655 		/* Inline asm could occupy zero bytes.  */
27656 		else if (GET_CODE (body) == ASM_INPUT
27657 			 || asm_noperands (body) >= 0)
27658 		  continue;
27659 #ifdef HAVE_ATTR_length /* ??? We don't include insn-attr.h.  */
27660 		else if (HAVE_ATTR_length && get_attr_min_length (insn) == 0)
27661 		  continue;
27662 #endif
27663 		else
27664 		  {
27665 		    /* Assume insn has non-zero length.  */
27666 		    maybe_at_text_label_p = false;
27667 		    break;
27668 		  }
27669 	      }
27670 	  if (maybe_at_text_label_p)
27671 	    {
27672 	      last_start = loc_note;
27673 	      first_loclabel_num_not_at_text_label = loclabel_num;
27674 	    }
27675 	}
27676     }
27677 
27678   gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
27679 	      || (loc_note != NULL_RTX && call_insn == NULL_RTX));
27680 
27681   if (!var_loc_p)
27682     {
27683       struct call_arg_loc_node *ca_loc
27684 	= ggc_cleared_alloc<call_arg_loc_node> ();
27685       rtx_insn *prev = call_insn;
27686 
27687       ca_loc->call_arg_loc_note
27688 	= find_reg_note (call_insn, REG_CALL_ARG_LOCATION, NULL_RTX);
27689       ca_loc->next = NULL;
27690       ca_loc->label = last_label;
27691       gcc_assert (prev
27692 		  && (CALL_P (prev)
27693 		      || (NONJUMP_INSN_P (prev)
27694 			  && GET_CODE (PATTERN (prev)) == SEQUENCE
27695 			  && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
27696       if (!CALL_P (prev))
27697 	prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
27698       ca_loc->tail_call_p = SIBLING_CALL_P (prev);
27699 
27700       /* Look for a SYMBOL_REF in the "prev" instruction.  */
27701       rtx x = get_call_rtx_from (prev);
27702       if (x)
27703 	{
27704 	  /* Try to get the call symbol, if any.  */
27705 	  if (MEM_P (XEXP (x, 0)))
27706 	    x = XEXP (x, 0);
27707 	  /* First, look for a memory access to a symbol_ref.  */
27708 	  if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
27709 	      && SYMBOL_REF_DECL (XEXP (x, 0))
27710 	      && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
27711 	    ca_loc->symbol_ref = XEXP (x, 0);
27712 	  /* Otherwise, look at a compile-time known user-level function
27713 	     declaration.  */
27714 	  else if (MEM_P (x)
27715 		   && MEM_EXPR (x)
27716 		   && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
27717 	    ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
27718 	}
27719 
27720       ca_loc->block = insn_scope (prev);
27721       if (call_arg_locations)
27722 	call_arg_loc_last->next = ca_loc;
27723       else
27724 	call_arg_locations = ca_loc;
27725       call_arg_loc_last = ca_loc;
27726     }
27727   else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
27728     {
27729       newloc->label = last_label;
27730       newloc->view = view;
27731     }
27732   else
27733     {
27734       if (!last_postcall_label)
27735 	{
27736 	  sprintf (loclabel, "%s-1", last_label);
27737 	  last_postcall_label = ggc_strdup (loclabel);
27738 	}
27739       newloc->label = last_postcall_label;
27740       /* ??? This view is at last_label, not last_label-1, but we
27741 	 could only assume view at last_label-1 is zero if we could
27742 	 assume calls always have length greater than one.  This is
27743 	 probably true in general, though there might be a rare
27744 	 exception to this rule, e.g. if a call insn is optimized out
27745 	 by target magic.  Then, even the -1 in the label will be
27746 	 wrong, which might invalidate the range.  Anyway, using view,
27747 	 though technically possibly incorrect, will work as far as
27748 	 ranges go: since L-1 is in the middle of the call insn,
27749 	 (L-1).0 and (L-1).V shouldn't make any difference, and having
27750 	 the loclist entry refer to the .loc entry might be useful, so
27751 	 leave it like this.  */
27752       newloc->view = view;
27753     }
27754 
27755   if (var_loc_p && flag_debug_asm)
27756     {
27757       const char *name, *sep, *patstr;
27758       if (decl && DECL_NAME (decl))
27759 	name = IDENTIFIER_POINTER (DECL_NAME (decl));
27760       else
27761 	name = "";
27762       if (NOTE_VAR_LOCATION_LOC (loc_note))
27763 	{
27764 	  sep = " => ";
27765 	  patstr = str_pattern_slim (NOTE_VAR_LOCATION_LOC (loc_note));
27766 	}
27767       else
27768 	{
27769 	  sep = " ";
27770 	  patstr = "RESET";
27771 	}
27772       fprintf (asm_out_file, "\t%s DEBUG %s%s%s\n", ASM_COMMENT_START,
27773 	       name, sep, patstr);
27774     }
27775 
27776   last_var_location_insn = next_real;
27777   last_in_cold_section_p = in_cold_section_p;
27778 }
27779 
27780 /* Check whether BLOCK, a lexical block, is nested within OUTER, or is
27781    OUTER itself.  If BOTHWAYS, check not only that BLOCK can reach
27782    OUTER through BLOCK_SUPERCONTEXT links, but also that there is a
27783    path from OUTER to BLOCK through BLOCK_SUBBLOCKs and
27784    BLOCK_FRAGMENT_ORIGIN links.  */
27785 static bool
block_within_block_p(tree block,tree outer,bool bothways)27786 block_within_block_p (tree block, tree outer, bool bothways)
27787 {
27788   if (block == outer)
27789     return true;
27790 
27791   /* Quickly check that OUTER is up BLOCK's supercontext chain.  */
27792   for (tree context = BLOCK_SUPERCONTEXT (block);
27793        context != outer;
27794        context = BLOCK_SUPERCONTEXT (context))
27795     if (!context || TREE_CODE (context) != BLOCK)
27796       return false;
27797 
27798   if (!bothways)
27799     return true;
27800 
27801   /* Now check that each block is actually referenced by its
27802      parent.  */
27803   for (tree context = BLOCK_SUPERCONTEXT (block); ;
27804        context = BLOCK_SUPERCONTEXT (context))
27805     {
27806       if (BLOCK_FRAGMENT_ORIGIN (context))
27807 	{
27808 	  gcc_assert (!BLOCK_SUBBLOCKS (context));
27809 	  context = BLOCK_FRAGMENT_ORIGIN (context);
27810 	}
27811       for (tree sub = BLOCK_SUBBLOCKS (context);
27812 	   sub != block;
27813 	   sub = BLOCK_CHAIN (sub))
27814 	if (!sub)
27815 	  return false;
27816       if (context == outer)
27817 	return true;
27818       else
27819 	block = context;
27820     }
27821 }
27822 
27823 /* Called during final while assembling the marker of the entry point
27824    for an inlined function.  */
27825 
27826 static void
dwarf2out_inline_entry(tree block)27827 dwarf2out_inline_entry (tree block)
27828 {
27829   gcc_assert (debug_inline_points);
27830 
27831   /* If we can't represent it, don't bother.  */
27832   if (!(dwarf_version >= 3 || !dwarf_strict))
27833     return;
27834 
27835   gcc_assert (DECL_P (block_ultimate_origin (block)));
27836 
27837   /* Sanity check the block tree.  This would catch a case in which
27838      BLOCK got removed from the tree reachable from the outermost
27839      lexical block, but got retained in markers.  It would still link
27840      back to its parents, but some ancestor would be missing a link
27841      down the path to the sub BLOCK.  If the block got removed, its
27842      BLOCK_NUMBER will not be a usable value.  */
27843   if (flag_checking)
27844     gcc_assert (block_within_block_p (block,
27845 				      DECL_INITIAL (current_function_decl),
27846 				      true));
27847 
27848   gcc_assert (inlined_function_outer_scope_p (block));
27849   gcc_assert (!lookup_block_die (block));
27850 
27851   if (BLOCK_FRAGMENT_ORIGIN (block))
27852     block = BLOCK_FRAGMENT_ORIGIN (block);
27853   /* Can the entry point ever not be at the beginning of an
27854      unfragmented lexical block?  */
27855   else if (!(BLOCK_FRAGMENT_CHAIN (block)
27856 	     || (cur_line_info_table
27857 		 && !ZERO_VIEW_P (cur_line_info_table->view))))
27858     return;
27859 
27860   if (!inline_entry_data_table)
27861     inline_entry_data_table
27862       = hash_table<inline_entry_data_hasher>::create_ggc (10);
27863 
27864 
27865   inline_entry_data **iedp
27866     = inline_entry_data_table->find_slot_with_hash (block,
27867 						    htab_hash_pointer (block),
27868 						    INSERT);
27869   if (*iedp)
27870     /* ??? Ideally, we'd record all entry points for the same inlined
27871        function (some may have been duplicated by e.g. unrolling), but
27872        we have no way to represent that ATM.  */
27873     return;
27874 
27875   inline_entry_data *ied = *iedp = ggc_cleared_alloc<inline_entry_data> ();
27876   ied->block = block;
27877   ied->label_pfx = BLOCK_INLINE_ENTRY_LABEL;
27878   ied->label_num = BLOCK_NUMBER (block);
27879   if (cur_line_info_table)
27880     ied->view = cur_line_info_table->view;
27881 
27882   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_INLINE_ENTRY_LABEL,
27883 			  BLOCK_NUMBER (block));
27884 }
27885 
27886 /* Called from finalize_size_functions for size functions so that their body
27887    can be encoded in the debug info to describe the layout of variable-length
27888    structures.  */
27889 
27890 static void
dwarf2out_size_function(tree decl)27891 dwarf2out_size_function (tree decl)
27892 {
27893   set_early_dwarf s;
27894   function_to_dwarf_procedure (decl);
27895 }
27896 
27897 /* Note in one location list that text section has changed.  */
27898 
27899 int
var_location_switch_text_section_1(var_loc_list ** slot,void *)27900 var_location_switch_text_section_1 (var_loc_list **slot, void *)
27901 {
27902   var_loc_list *list = *slot;
27903   if (list->first)
27904     list->last_before_switch
27905       = list->last->next ? list->last->next : list->last;
27906   return 1;
27907 }
27908 
27909 /* Note in all location lists that text section has changed.  */
27910 
27911 static void
var_location_switch_text_section(void)27912 var_location_switch_text_section (void)
27913 {
27914   if (decl_loc_table == NULL)
27915     return;
27916 
27917   decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
27918 }
27919 
27920 /* Create a new line number table.  */
27921 
27922 static dw_line_info_table *
new_line_info_table(void)27923 new_line_info_table (void)
27924 {
27925   dw_line_info_table *table;
27926 
27927   table = ggc_cleared_alloc<dw_line_info_table> ();
27928   table->file_num = 1;
27929   table->line_num = 1;
27930   table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
27931   FORCE_RESET_NEXT_VIEW (table->view);
27932   table->symviews_since_reset = 0;
27933 
27934   return table;
27935 }
27936 
27937 /* Lookup the "current" table into which we emit line info, so
27938    that we don't have to do it for every source line.  */
27939 
27940 static void
set_cur_line_info_table(section * sec)27941 set_cur_line_info_table (section *sec)
27942 {
27943   dw_line_info_table *table;
27944 
27945   if (sec == text_section)
27946     table = text_section_line_info;
27947   else if (sec == cold_text_section)
27948     {
27949       table = cold_text_section_line_info;
27950       if (!table)
27951 	{
27952 	  cold_text_section_line_info = table = new_line_info_table ();
27953 	  table->end_label = cold_end_label;
27954 	}
27955     }
27956   else
27957     {
27958       const char *end_label;
27959 
27960       if (crtl->has_bb_partition)
27961 	{
27962 	  if (in_cold_section_p)
27963 	    end_label = crtl->subsections.cold_section_end_label;
27964 	  else
27965 	    end_label = crtl->subsections.hot_section_end_label;
27966 	}
27967       else
27968 	{
27969 	  char label[MAX_ARTIFICIAL_LABEL_BYTES];
27970 	  ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
27971 				       current_function_funcdef_no);
27972 	  end_label = ggc_strdup (label);
27973 	}
27974 
27975       table = new_line_info_table ();
27976       table->end_label = end_label;
27977 
27978       vec_safe_push (separate_line_info, table);
27979     }
27980 
27981   if (output_asm_line_debug_info ())
27982     table->is_stmt = (cur_line_info_table
27983 		      ? cur_line_info_table->is_stmt
27984 		      : DWARF_LINE_DEFAULT_IS_STMT_START);
27985   cur_line_info_table = table;
27986 }
27987 
27988 
27989 /* We need to reset the locations at the beginning of each
27990    function. We can't do this in the end_function hook, because the
27991    declarations that use the locations won't have been output when
27992    that hook is called.  Also compute have_multiple_function_sections here.  */
27993 
27994 static void
dwarf2out_begin_function(tree fun)27995 dwarf2out_begin_function (tree fun)
27996 {
27997   section *sec = function_section (fun);
27998 
27999   if (sec != text_section)
28000     have_multiple_function_sections = true;
28001 
28002   if (crtl->has_bb_partition && !cold_text_section)
28003     {
28004       gcc_assert (current_function_decl == fun);
28005       cold_text_section = unlikely_text_section ();
28006       switch_to_section (cold_text_section);
28007       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
28008       switch_to_section (sec);
28009     }
28010 
28011   dwarf2out_note_section_used ();
28012   call_site_count = 0;
28013   tail_call_site_count = 0;
28014 
28015   set_cur_line_info_table (sec);
28016   FORCE_RESET_NEXT_VIEW (cur_line_info_table->view);
28017 }
28018 
28019 /* Helper function of dwarf2out_end_function, called only after emitting
28020    the very first function into assembly.  Check if some .debug_loc range
28021    might end with a .LVL* label that could be equal to .Ltext0.
28022    In that case we must force using absolute addresses in .debug_loc ranges,
28023    because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
28024    .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
28025    list terminator.
28026    Set have_multiple_function_sections to true in that case and
28027    terminate htab traversal.  */
28028 
28029 int
find_empty_loc_ranges_at_text_label(var_loc_list ** slot,int)28030 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
28031 {
28032   var_loc_list *entry = *slot;
28033   struct var_loc_node *node;
28034 
28035   node = entry->first;
28036   if (node && node->next && node->next->label)
28037     {
28038       unsigned int i;
28039       const char *label = node->next->label;
28040       char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
28041 
28042       for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
28043 	{
28044 	  ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
28045 	  if (strcmp (label, loclabel) == 0)
28046 	    {
28047 	      have_multiple_function_sections = true;
28048 	      return 0;
28049 	    }
28050 	}
28051     }
28052   return 1;
28053 }
28054 
28055 /* Hook called after emitting a function into assembly.
28056    This does something only for the very first function emitted.  */
28057 
28058 static void
dwarf2out_end_function(unsigned int)28059 dwarf2out_end_function (unsigned int)
28060 {
28061   if (in_first_function_p
28062       && !have_multiple_function_sections
28063       && first_loclabel_num_not_at_text_label
28064       && decl_loc_table)
28065     decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
28066   in_first_function_p = false;
28067   maybe_at_text_label_p = false;
28068 }
28069 
28070 /* Temporary holder for dwarf2out_register_main_translation_unit.  Used to let
28071    front-ends register a translation unit even before dwarf2out_init is
28072    called.  */
28073 static tree main_translation_unit = NULL_TREE;
28074 
28075 /* Hook called by front-ends after they built their main translation unit.
28076    Associate comp_unit_die to UNIT.  */
28077 
28078 static void
dwarf2out_register_main_translation_unit(tree unit)28079 dwarf2out_register_main_translation_unit (tree unit)
28080 {
28081   gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
28082 	      && main_translation_unit == NULL_TREE);
28083   main_translation_unit = unit;
28084   /* If dwarf2out_init has not been called yet, it will perform the association
28085      itself looking at main_translation_unit.  */
28086   if (decl_die_table != NULL)
28087     equate_decl_number_to_die (unit, comp_unit_die ());
28088 }
28089 
28090 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE.  */
28091 
28092 static void
push_dw_line_info_entry(dw_line_info_table * table,enum dw_line_info_opcode opcode,unsigned int val)28093 push_dw_line_info_entry (dw_line_info_table *table,
28094 			 enum dw_line_info_opcode opcode, unsigned int val)
28095 {
28096   dw_line_info_entry e;
28097   e.opcode = opcode;
28098   e.val = val;
28099   vec_safe_push (table->entries, e);
28100 }
28101 
28102 /* Output a label to mark the beginning of a source code line entry
28103    and record information relating to this source line, in
28104    'line_info_table' for later output of the .debug_line section.  */
28105 /* ??? The discriminator parameter ought to be unsigned.  */
28106 
28107 static void
dwarf2out_source_line(unsigned int line,unsigned int column,const char * filename,int discriminator,bool is_stmt)28108 dwarf2out_source_line (unsigned int line, unsigned int column,
28109 		       const char *filename,
28110                        int discriminator, bool is_stmt)
28111 {
28112   unsigned int file_num;
28113   dw_line_info_table *table;
28114   static var_loc_view lvugid;
28115 
28116   if (debug_info_level < DINFO_LEVEL_TERSE)
28117     return;
28118 
28119   table = cur_line_info_table;
28120 
28121   if (line == 0)
28122     {
28123       if (debug_variable_location_views
28124 	  && output_asm_line_debug_info ()
28125 	  && table && !RESETTING_VIEW_P (table->view))
28126 	{
28127 	  /* If we're using the assembler to compute view numbers, we
28128 	     can't issue a .loc directive for line zero, so we can't
28129 	     get a view number at this point.  We might attempt to
28130 	     compute it from the previous view, or equate it to a
28131 	     subsequent view (though it might not be there!), but
28132 	     since we're omitting the line number entry, we might as
28133 	     well omit the view number as well.  That means pretending
28134 	     it's a view number zero, which might very well turn out
28135 	     to be correct.  ??? Extend the assembler so that the
28136 	     compiler could emit e.g. ".locview .LVU#", to output a
28137 	     view without changing line number information.  We'd then
28138 	     have to count it in symviews_since_reset; when it's omitted,
28139 	     it doesn't count.  */
28140 	  if (!zero_view_p)
28141 	    zero_view_p = BITMAP_GGC_ALLOC ();
28142 	  bitmap_set_bit (zero_view_p, table->view);
28143 	  if (flag_debug_asm)
28144 	    {
28145 	      char label[MAX_ARTIFICIAL_LABEL_BYTES];
28146 	      ASM_GENERATE_INTERNAL_LABEL (label, "LVU", table->view);
28147 	      fprintf (asm_out_file, "\t%s line 0, omitted view ",
28148 		       ASM_COMMENT_START);
28149 	      assemble_name (asm_out_file, label);
28150 	      putc ('\n', asm_out_file);
28151 	    }
28152 	  table->view = ++lvugid;
28153 	}
28154       return;
28155     }
28156 
28157   /* The discriminator column was added in dwarf4.  Simplify the below
28158      by simply removing it if we're not supposed to output it.  */
28159   if (dwarf_version < 4 && dwarf_strict)
28160     discriminator = 0;
28161 
28162   if (!debug_column_info)
28163     column = 0;
28164 
28165   file_num = maybe_emit_file (lookup_filename (filename));
28166 
28167   /* ??? TODO: Elide duplicate line number entries.  Traditionally,
28168      the debugger has used the second (possibly duplicate) line number
28169      at the beginning of the function to mark the end of the prologue.
28170      We could eliminate any other duplicates within the function.  For
28171      Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
28172      that second line number entry.  */
28173   /* Recall that this end-of-prologue indication is *not* the same thing
28174      as the end_prologue debug hook.  The NOTE_INSN_PROLOGUE_END note,
28175      to which the hook corresponds, follows the last insn that was
28176      emitted by gen_prologue.  What we need is to precede the first insn
28177      that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
28178      insn that corresponds to something the user wrote.  These may be
28179      very different locations once scheduling is enabled.  */
28180 
28181   if (0 && file_num == table->file_num
28182       && line == table->line_num
28183       && column == table->column_num
28184       && discriminator == table->discrim_num
28185       && is_stmt == table->is_stmt)
28186     return;
28187 
28188   switch_to_section (current_function_section ());
28189 
28190   /* If requested, emit something human-readable.  */
28191   if (flag_debug_asm)
28192     {
28193       if (debug_column_info)
28194 	fprintf (asm_out_file, "\t%s %s:%d:%d\n", ASM_COMMENT_START,
28195 		 filename, line, column);
28196       else
28197 	fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
28198 		 filename, line);
28199     }
28200 
28201   if (output_asm_line_debug_info ())
28202     {
28203       /* Emit the .loc directive understood by GNU as.  */
28204       /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
28205 	 file_num, line, is_stmt, discriminator */
28206       fputs ("\t.loc ", asm_out_file);
28207       fprint_ul (asm_out_file, file_num);
28208       putc (' ', asm_out_file);
28209       fprint_ul (asm_out_file, line);
28210       putc (' ', asm_out_file);
28211       fprint_ul (asm_out_file, column);
28212 
28213       if (is_stmt != table->is_stmt)
28214 	{
28215 #if HAVE_GAS_LOC_STMT
28216 	  fputs (" is_stmt ", asm_out_file);
28217 	  putc (is_stmt ? '1' : '0', asm_out_file);
28218 #endif
28219 	}
28220       if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
28221 	{
28222 	  gcc_assert (discriminator > 0);
28223 	  fputs (" discriminator ", asm_out_file);
28224 	  fprint_ul (asm_out_file, (unsigned long) discriminator);
28225 	}
28226       if (debug_variable_location_views)
28227 	{
28228 	  if (!RESETTING_VIEW_P (table->view))
28229 	    {
28230 	      table->symviews_since_reset++;
28231 	      if (table->symviews_since_reset > symview_upper_bound)
28232 		symview_upper_bound = table->symviews_since_reset;
28233 	      /* When we're using the assembler to compute view
28234 		 numbers, we output symbolic labels after "view" in
28235 		 .loc directives, and the assembler will set them for
28236 		 us, so that we can refer to the view numbers in
28237 		 location lists.  The only exceptions are when we know
28238 		 a view will be zero: "-0" is a forced reset, used
28239 		 e.g. in the beginning of functions, whereas "0" tells
28240 		 the assembler to check that there was a PC change
28241 		 since the previous view, in a way that implicitly
28242 		 resets the next view.  */
28243 	      fputs (" view ", asm_out_file);
28244 	      char label[MAX_ARTIFICIAL_LABEL_BYTES];
28245 	      ASM_GENERATE_INTERNAL_LABEL (label, "LVU", table->view);
28246 	      assemble_name (asm_out_file, label);
28247 	      table->view = ++lvugid;
28248 	    }
28249 	  else
28250 	    {
28251 	      table->symviews_since_reset = 0;
28252 	      if (FORCE_RESETTING_VIEW_P (table->view))
28253 		fputs (" view -0", asm_out_file);
28254 	      else
28255 		fputs (" view 0", asm_out_file);
28256 	      /* Mark the present view as a zero view.  Earlier debug
28257 		 binds may have already added its id to loclists to be
28258 		 emitted later, so we can't reuse the id for something
28259 		 else.  However, it's good to know whether a view is
28260 		 known to be zero, because then we may be able to
28261 		 optimize out locviews that are all zeros, so take
28262 		 note of it in zero_view_p.  */
28263 	      if (!zero_view_p)
28264 		zero_view_p = BITMAP_GGC_ALLOC ();
28265 	      bitmap_set_bit (zero_view_p, lvugid);
28266 	      table->view = ++lvugid;
28267 	    }
28268 	}
28269       putc ('\n', asm_out_file);
28270     }
28271   else
28272     {
28273       unsigned int label_num = ++line_info_label_num;
28274 
28275       targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
28276 
28277       if (debug_variable_location_views && !RESETTING_VIEW_P (table->view))
28278 	push_dw_line_info_entry (table, LI_adv_address, label_num);
28279       else
28280 	push_dw_line_info_entry (table, LI_set_address, label_num);
28281       if (debug_variable_location_views)
28282 	{
28283 	  bool resetting = FORCE_RESETTING_VIEW_P (table->view);
28284 	  if (resetting)
28285 	    table->view = 0;
28286 
28287 	  if (flag_debug_asm)
28288 	    fprintf (asm_out_file, "\t%s view %s%d\n",
28289 		     ASM_COMMENT_START,
28290 		     resetting ? "-" : "",
28291 		     table->view);
28292 
28293 	  table->view++;
28294 	}
28295       if (file_num != table->file_num)
28296 	push_dw_line_info_entry (table, LI_set_file, file_num);
28297       if (discriminator != table->discrim_num)
28298 	push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
28299       if (is_stmt != table->is_stmt)
28300 	push_dw_line_info_entry (table, LI_negate_stmt, 0);
28301       push_dw_line_info_entry (table, LI_set_line, line);
28302       if (debug_column_info)
28303 	push_dw_line_info_entry (table, LI_set_column, column);
28304     }
28305 
28306   table->file_num = file_num;
28307   table->line_num = line;
28308   table->column_num = column;
28309   table->discrim_num = discriminator;
28310   table->is_stmt = is_stmt;
28311   table->in_use = true;
28312 }
28313 
28314 /* Record the beginning of a new source file.  */
28315 
28316 static void
dwarf2out_start_source_file(unsigned int lineno,const char * filename)28317 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
28318 {
28319   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28320     {
28321       macinfo_entry e;
28322       e.code = DW_MACINFO_start_file;
28323       e.lineno = lineno;
28324       e.info = ggc_strdup (filename);
28325       vec_safe_push (macinfo_table, e);
28326     }
28327 }
28328 
28329 /* Record the end of a source file.  */
28330 
28331 static void
dwarf2out_end_source_file(unsigned int lineno ATTRIBUTE_UNUSED)28332 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
28333 {
28334   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28335     {
28336       macinfo_entry e;
28337       e.code = DW_MACINFO_end_file;
28338       e.lineno = lineno;
28339       e.info = NULL;
28340       vec_safe_push (macinfo_table, e);
28341     }
28342 }
28343 
28344 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
28345    the tail part of the directive line, i.e. the part which is past the
28346    initial whitespace, #, whitespace, directive-name, whitespace part.  */
28347 
28348 static void
dwarf2out_define(unsigned int lineno ATTRIBUTE_UNUSED,const char * buffer ATTRIBUTE_UNUSED)28349 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
28350 		  const char *buffer ATTRIBUTE_UNUSED)
28351 {
28352   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28353     {
28354       macinfo_entry e;
28355       /* Insert a dummy first entry to be able to optimize the whole
28356 	 predefined macro block using DW_MACRO_import.  */
28357       if (macinfo_table->is_empty () && lineno <= 1)
28358 	{
28359 	  e.code = 0;
28360 	  e.lineno = 0;
28361 	  e.info = NULL;
28362 	  vec_safe_push (macinfo_table, e);
28363 	}
28364       e.code = DW_MACINFO_define;
28365       e.lineno = lineno;
28366       e.info = ggc_strdup (buffer);
28367       vec_safe_push (macinfo_table, e);
28368     }
28369 }
28370 
28371 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
28372    the tail part of the directive line, i.e. the part which is past the
28373    initial whitespace, #, whitespace, directive-name, whitespace part.  */
28374 
28375 static void
dwarf2out_undef(unsigned int lineno ATTRIBUTE_UNUSED,const char * buffer ATTRIBUTE_UNUSED)28376 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
28377 		 const char *buffer ATTRIBUTE_UNUSED)
28378 {
28379   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28380     {
28381       macinfo_entry e;
28382       /* Insert a dummy first entry to be able to optimize the whole
28383 	 predefined macro block using DW_MACRO_import.  */
28384       if (macinfo_table->is_empty () && lineno <= 1)
28385 	{
28386 	  e.code = 0;
28387 	  e.lineno = 0;
28388 	  e.info = NULL;
28389 	  vec_safe_push (macinfo_table, e);
28390 	}
28391       e.code = DW_MACINFO_undef;
28392       e.lineno = lineno;
28393       e.info = ggc_strdup (buffer);
28394       vec_safe_push (macinfo_table, e);
28395     }
28396 }
28397 
28398 /* Helpers to manipulate hash table of CUs.  */
28399 
28400 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
28401 {
28402   static inline hashval_t hash (const macinfo_entry *);
28403   static inline bool equal (const macinfo_entry *, const macinfo_entry *);
28404 };
28405 
28406 inline hashval_t
hash(const macinfo_entry * entry)28407 macinfo_entry_hasher::hash (const macinfo_entry *entry)
28408 {
28409   return htab_hash_string (entry->info);
28410 }
28411 
28412 inline bool
equal(const macinfo_entry * entry1,const macinfo_entry * entry2)28413 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
28414 			     const macinfo_entry *entry2)
28415 {
28416   return !strcmp (entry1->info, entry2->info);
28417 }
28418 
28419 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
28420 
28421 /* Output a single .debug_macinfo entry.  */
28422 
28423 static void
output_macinfo_op(macinfo_entry * ref)28424 output_macinfo_op (macinfo_entry *ref)
28425 {
28426   int file_num;
28427   size_t len;
28428   struct indirect_string_node *node;
28429   char label[MAX_ARTIFICIAL_LABEL_BYTES];
28430   struct dwarf_file_data *fd;
28431 
28432   switch (ref->code)
28433     {
28434     case DW_MACINFO_start_file:
28435       fd = lookup_filename (ref->info);
28436       file_num = maybe_emit_file (fd);
28437       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
28438       dw2_asm_output_data_uleb128 (ref->lineno,
28439 				   "Included from line number %lu",
28440 				   (unsigned long) ref->lineno);
28441       dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
28442       break;
28443     case DW_MACINFO_end_file:
28444       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
28445       break;
28446     case DW_MACINFO_define:
28447     case DW_MACINFO_undef:
28448       len = strlen (ref->info) + 1;
28449       if ((!dwarf_strict || dwarf_version >= 5)
28450 	  && len > (size_t) dwarf_offset_size
28451 	  && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
28452 	  && (debug_str_section->common.flags & SECTION_MERGE) != 0)
28453 	{
28454 	  if (dwarf_split_debug_info && dwarf_version >= 5)
28455 	    ref->code = ref->code == DW_MACINFO_define
28456 			? DW_MACRO_define_strx : DW_MACRO_undef_strx;
28457 	  else
28458 	    ref->code = ref->code == DW_MACINFO_define
28459 			? DW_MACRO_define_strp : DW_MACRO_undef_strp;
28460 	  output_macinfo_op (ref);
28461 	  return;
28462 	}
28463       dw2_asm_output_data (1, ref->code,
28464 			   ref->code == DW_MACINFO_define
28465 			   ? "Define macro" : "Undefine macro");
28466       dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
28467 				   (unsigned long) ref->lineno);
28468       dw2_asm_output_nstring (ref->info, -1, "The macro");
28469       break;
28470     case DW_MACRO_define_strp:
28471       dw2_asm_output_data (1, ref->code, "Define macro strp");
28472       goto do_DW_MACRO_define_strpx;
28473     case DW_MACRO_undef_strp:
28474       dw2_asm_output_data (1, ref->code, "Undefine macro strp");
28475       goto do_DW_MACRO_define_strpx;
28476     case DW_MACRO_define_strx:
28477       dw2_asm_output_data (1, ref->code, "Define macro strx");
28478       goto do_DW_MACRO_define_strpx;
28479     case DW_MACRO_undef_strx:
28480       dw2_asm_output_data (1, ref->code, "Undefine macro strx");
28481       /* FALLTHRU */
28482     do_DW_MACRO_define_strpx:
28483       /* NB: dwarf2out_finish performs:
28484 	   1. save_macinfo_strings
28485 	   2. hash table traverse of index_string
28486 	   3. output_macinfo -> output_macinfo_op
28487 	   4. output_indirect_strings
28488 		-> hash table traverse of output_index_string
28489 
28490 	 When output_macinfo_op is called, all index strings have been
28491 	 added to hash table by save_macinfo_strings and we can't pass
28492 	 INSERT to find_slot_with_hash which may expand hash table, even
28493 	 if no insertion is needed, and change hash table traverse order
28494 	 between index_string and output_index_string.  */
28495       node = find_AT_string (ref->info, NO_INSERT);
28496       gcc_assert (node
28497 		  && (node->form == DW_FORM_strp
28498 		      || node->form == dwarf_FORM (DW_FORM_strx)));
28499       dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
28500 				   (unsigned long) ref->lineno);
28501       if (node->form == DW_FORM_strp)
28502         dw2_asm_output_offset (dwarf_offset_size, node->label,
28503                                debug_str_section, "The macro: \"%s\"",
28504                                ref->info);
28505       else
28506         dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
28507                                      ref->info);
28508       break;
28509     case DW_MACRO_import:
28510       dw2_asm_output_data (1, ref->code, "Import");
28511       ASM_GENERATE_INTERNAL_LABEL (label,
28512 				   DEBUG_MACRO_SECTION_LABEL,
28513 				   ref->lineno + macinfo_label_base);
28514       dw2_asm_output_offset (dwarf_offset_size, label, NULL, NULL);
28515       break;
28516     default:
28517       fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
28518 	       ASM_COMMENT_START, (unsigned long) ref->code);
28519       break;
28520     }
28521 }
28522 
28523 /* Attempt to make a sequence of define/undef macinfo ops shareable with
28524    other compilation unit .debug_macinfo sections.  IDX is the first
28525    index of a define/undef, return the number of ops that should be
28526    emitted in a comdat .debug_macinfo section and emit
28527    a DW_MACRO_import entry referencing it.
28528    If the define/undef entry should be emitted normally, return 0.  */
28529 
28530 static unsigned
optimize_macinfo_range(unsigned int idx,vec<macinfo_entry,va_gc> * files,macinfo_hash_type ** macinfo_htab)28531 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
28532 			macinfo_hash_type **macinfo_htab)
28533 {
28534   macinfo_entry *first, *second, *cur, *inc;
28535   char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
28536   unsigned char checksum[16];
28537   struct md5_ctx ctx;
28538   char *grp_name, *tail;
28539   const char *base;
28540   unsigned int i, count, encoded_filename_len, linebuf_len;
28541   macinfo_entry **slot;
28542 
28543   first = &(*macinfo_table)[idx];
28544   second = &(*macinfo_table)[idx + 1];
28545 
28546   /* Optimize only if there are at least two consecutive define/undef ops,
28547      and either all of them are before first DW_MACINFO_start_file
28548      with lineno {0,1} (i.e. predefined macro block), or all of them are
28549      in some included header file.  */
28550   if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
28551     return 0;
28552   if (vec_safe_is_empty (files))
28553     {
28554       if (first->lineno > 1 || second->lineno > 1)
28555 	return 0;
28556     }
28557   else if (first->lineno == 0)
28558     return 0;
28559 
28560   /* Find the last define/undef entry that can be grouped together
28561      with first and at the same time compute md5 checksum of their
28562      codes, linenumbers and strings.  */
28563   md5_init_ctx (&ctx);
28564   for (i = idx; macinfo_table->iterate (i, &cur); i++)
28565     if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
28566       break;
28567     else if (vec_safe_is_empty (files) && cur->lineno > 1)
28568       break;
28569     else
28570       {
28571 	unsigned char code = cur->code;
28572 	md5_process_bytes (&code, 1, &ctx);
28573 	checksum_uleb128 (cur->lineno, &ctx);
28574 	md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
28575       }
28576   md5_finish_ctx (&ctx, checksum);
28577   count = i - idx;
28578 
28579   /* From the containing include filename (if any) pick up just
28580      usable characters from its basename.  */
28581   if (vec_safe_is_empty (files))
28582     base = "";
28583   else
28584     base = lbasename (files->last ().info);
28585   for (encoded_filename_len = 0, i = 0; base[i]; i++)
28586     if (ISIDNUM (base[i]) || base[i] == '.')
28587       encoded_filename_len++;
28588   /* Count . at the end.  */
28589   if (encoded_filename_len)
28590     encoded_filename_len++;
28591 
28592   sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
28593   linebuf_len = strlen (linebuf);
28594 
28595   /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum>  */
28596   grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
28597 			 + 16 * 2 + 1);
28598   memcpy (grp_name, dwarf_offset_size == 4 ? "wm4." : "wm8.", 4);
28599   tail = grp_name + 4;
28600   if (encoded_filename_len)
28601     {
28602       for (i = 0; base[i]; i++)
28603 	if (ISIDNUM (base[i]) || base[i] == '.')
28604 	  *tail++ = base[i];
28605       *tail++ = '.';
28606     }
28607   memcpy (tail, linebuf, linebuf_len);
28608   tail += linebuf_len;
28609   *tail++ = '.';
28610   for (i = 0; i < 16; i++)
28611     sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
28612 
28613   /* Construct a macinfo_entry for DW_MACRO_import
28614      in the empty vector entry before the first define/undef.  */
28615   inc = &(*macinfo_table)[idx - 1];
28616   inc->code = DW_MACRO_import;
28617   inc->lineno = 0;
28618   inc->info = ggc_strdup (grp_name);
28619   if (!*macinfo_htab)
28620     *macinfo_htab = new macinfo_hash_type (10);
28621   /* Avoid emitting duplicates.  */
28622   slot = (*macinfo_htab)->find_slot (inc, INSERT);
28623   if (*slot != NULL)
28624     {
28625       inc->code = 0;
28626       inc->info = NULL;
28627       /* If such an entry has been used before, just emit
28628 	 a DW_MACRO_import op.  */
28629       inc = *slot;
28630       output_macinfo_op (inc);
28631       /* And clear all macinfo_entry in the range to avoid emitting them
28632 	 in the second pass.  */
28633       for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
28634 	{
28635 	  cur->code = 0;
28636 	  cur->info = NULL;
28637 	}
28638     }
28639   else
28640     {
28641       *slot = inc;
28642       inc->lineno = (*macinfo_htab)->elements ();
28643       output_macinfo_op (inc);
28644     }
28645   return count;
28646 }
28647 
28648 /* Save any strings needed by the macinfo table in the debug str
28649    table.  All strings must be collected into the table by the time
28650    index_string is called.  */
28651 
28652 static void
save_macinfo_strings(void)28653 save_macinfo_strings (void)
28654 {
28655   unsigned len;
28656   unsigned i;
28657   macinfo_entry *ref;
28658 
28659   for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
28660     {
28661       switch (ref->code)
28662         {
28663           /* Match the logic in output_macinfo_op to decide on
28664              indirect strings.  */
28665           case DW_MACINFO_define:
28666           case DW_MACINFO_undef:
28667             len = strlen (ref->info) + 1;
28668 	    if ((!dwarf_strict || dwarf_version >= 5)
28669                 && len > (unsigned) dwarf_offset_size
28670                 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
28671                 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
28672               set_indirect_string (find_AT_string (ref->info));
28673             break;
28674 	  case DW_MACINFO_start_file:
28675 	    /* -gsplit-dwarf -g3 will also output filename as indirect
28676 	       string.  */
28677 	    if (!dwarf_split_debug_info)
28678 	      break;
28679 	    /* Fall through. */
28680 	  case DW_MACRO_define_strp:
28681 	  case DW_MACRO_undef_strp:
28682 	  case DW_MACRO_define_strx:
28683 	  case DW_MACRO_undef_strx:
28684             set_indirect_string (find_AT_string (ref->info));
28685             break;
28686           default:
28687             break;
28688         }
28689     }
28690 }
28691 
28692 /* Output macinfo section(s).  */
28693 
28694 static void
output_macinfo(const char * debug_line_label,bool early_lto_debug)28695 output_macinfo (const char *debug_line_label, bool early_lto_debug)
28696 {
28697   unsigned i;
28698   unsigned long length = vec_safe_length (macinfo_table);
28699   macinfo_entry *ref;
28700   vec<macinfo_entry, va_gc> *files = NULL;
28701   macinfo_hash_type *macinfo_htab = NULL;
28702   char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
28703 
28704   if (! length)
28705     return;
28706 
28707   /* output_macinfo* uses these interchangeably.  */
28708   gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_define
28709 	      && (int) DW_MACINFO_undef == (int) DW_MACRO_undef
28710 	      && (int) DW_MACINFO_start_file == (int) DW_MACRO_start_file
28711 	      && (int) DW_MACINFO_end_file == (int) DW_MACRO_end_file);
28712 
28713   /* AIX Assembler inserts the length, so adjust the reference to match the
28714      offset expected by debuggers.  */
28715   strcpy (dl_section_ref, debug_line_label);
28716   if (XCOFF_DEBUGGING_INFO)
28717     strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
28718 
28719   /* For .debug_macro emit the section header.  */
28720   if (!dwarf_strict || dwarf_version >= 5)
28721     {
28722       dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
28723 			   "DWARF macro version number");
28724       if (dwarf_offset_size == 8)
28725 	dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
28726       else
28727 	dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
28728       dw2_asm_output_offset (dwarf_offset_size, debug_line_label,
28729                              debug_line_section, NULL);
28730     }
28731 
28732   /* In the first loop, it emits the primary .debug_macinfo section
28733      and after each emitted op the macinfo_entry is cleared.
28734      If a longer range of define/undef ops can be optimized using
28735      DW_MACRO_import, the DW_MACRO_import op is emitted and kept in
28736      the vector before the first define/undef in the range and the
28737      whole range of define/undef ops is not emitted and kept.  */
28738   for (i = 0; macinfo_table->iterate (i, &ref); i++)
28739     {
28740       switch (ref->code)
28741 	{
28742 	case DW_MACINFO_start_file:
28743 	  vec_safe_push (files, *ref);
28744 	  break;
28745 	case DW_MACINFO_end_file:
28746 	  if (!vec_safe_is_empty (files))
28747 	    files->pop ();
28748 	  break;
28749 	case DW_MACINFO_define:
28750 	case DW_MACINFO_undef:
28751 	  if ((!dwarf_strict || dwarf_version >= 5)
28752 	      && HAVE_COMDAT_GROUP
28753 	      && vec_safe_length (files) != 1
28754 	      && i > 0
28755 	      && i + 1 < length
28756 	      && (*macinfo_table)[i - 1].code == 0)
28757 	    {
28758 	      unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
28759 	      if (count)
28760 		{
28761 		  i += count - 1;
28762 		  continue;
28763 		}
28764 	    }
28765 	  break;
28766 	case 0:
28767 	  /* A dummy entry may be inserted at the beginning to be able
28768 	     to optimize the whole block of predefined macros.  */
28769 	  if (i == 0)
28770 	    continue;
28771 	default:
28772 	  break;
28773 	}
28774       output_macinfo_op (ref);
28775       ref->info = NULL;
28776       ref->code = 0;
28777     }
28778 
28779   if (!macinfo_htab)
28780     return;
28781 
28782   /* Save the number of transparent includes so we can adjust the
28783      label number for the fat LTO object DWARF.  */
28784   unsigned macinfo_label_base_adj = macinfo_htab->elements ();
28785 
28786   delete macinfo_htab;
28787   macinfo_htab = NULL;
28788 
28789   /* If any DW_MACRO_import were used, on those DW_MACRO_import entries
28790      terminate the current chain and switch to a new comdat .debug_macinfo
28791      section and emit the define/undef entries within it.  */
28792   for (i = 0; macinfo_table->iterate (i, &ref); i++)
28793     switch (ref->code)
28794       {
28795       case 0:
28796 	continue;
28797       case DW_MACRO_import:
28798 	{
28799 	  char label[MAX_ARTIFICIAL_LABEL_BYTES];
28800 	  tree comdat_key = get_identifier (ref->info);
28801 	  /* Terminate the previous .debug_macinfo section.  */
28802 	  dw2_asm_output_data (1, 0, "End compilation unit");
28803 	  targetm.asm_out.named_section (debug_macinfo_section_name,
28804 					 SECTION_DEBUG
28805 					 | SECTION_LINKONCE
28806 					 | (early_lto_debug
28807 					    ? SECTION_EXCLUDE : 0),
28808 					 comdat_key);
28809 	  ASM_GENERATE_INTERNAL_LABEL (label,
28810 				       DEBUG_MACRO_SECTION_LABEL,
28811 				       ref->lineno + macinfo_label_base);
28812 	  ASM_OUTPUT_LABEL (asm_out_file, label);
28813 	  ref->code = 0;
28814 	  ref->info = NULL;
28815 	  dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
28816 			       "DWARF macro version number");
28817 	  if (dwarf_offset_size == 8)
28818 	    dw2_asm_output_data (1, 1, "Flags: 64-bit");
28819 	  else
28820 	    dw2_asm_output_data (1, 0, "Flags: 32-bit");
28821 	}
28822 	break;
28823       case DW_MACINFO_define:
28824       case DW_MACINFO_undef:
28825 	output_macinfo_op (ref);
28826 	ref->code = 0;
28827 	ref->info = NULL;
28828 	break;
28829       default:
28830 	gcc_unreachable ();
28831       }
28832 
28833   macinfo_label_base += macinfo_label_base_adj;
28834 }
28835 
28836 /* As init_sections_and_labels may get called multiple times, have a
28837    generation count for labels.  */
28838 static unsigned init_sections_and_labels_generation;
28839 
28840 /* Initialize the various sections and labels for dwarf output and prefix
28841    them with PREFIX if non-NULL.  Returns the generation (zero based
28842    number of times function was called).  */
28843 
28844 static unsigned
init_sections_and_labels(bool early_lto_debug)28845 init_sections_and_labels (bool early_lto_debug)
28846 {
28847   if (early_lto_debug)
28848     {
28849       if (!dwarf_split_debug_info)
28850 	{
28851 	  debug_info_section = get_section (DEBUG_LTO_INFO_SECTION,
28852 					    SECTION_DEBUG | SECTION_EXCLUDE,
28853 					    NULL);
28854 	  debug_abbrev_section = get_section (DEBUG_LTO_ABBREV_SECTION,
28855 					      SECTION_DEBUG | SECTION_EXCLUDE,
28856 					      NULL);
28857 	  debug_macinfo_section_name
28858 	    = ((dwarf_strict && dwarf_version < 5)
28859 	       ? DEBUG_LTO_MACINFO_SECTION : DEBUG_LTO_MACRO_SECTION);
28860 	  debug_macinfo_section = get_section (debug_macinfo_section_name,
28861 					       SECTION_DEBUG
28862 					       | SECTION_EXCLUDE, NULL);
28863 	}
28864       else
28865 	{
28866 	  /* ???  Which of the following do we need early?  */
28867 	  debug_info_section = get_section (DEBUG_LTO_DWO_INFO_SECTION,
28868 					    SECTION_DEBUG | SECTION_EXCLUDE,
28869 					    NULL);
28870 	  debug_abbrev_section = get_section (DEBUG_LTO_DWO_ABBREV_SECTION,
28871 					      SECTION_DEBUG | SECTION_EXCLUDE,
28872 					      NULL);
28873 	  debug_skeleton_info_section = get_section (DEBUG_LTO_INFO_SECTION,
28874 						     SECTION_DEBUG
28875 						     | SECTION_EXCLUDE, NULL);
28876 	  debug_skeleton_abbrev_section
28877 	    = get_section (DEBUG_LTO_ABBREV_SECTION,
28878 			   SECTION_DEBUG | SECTION_EXCLUDE, NULL);
28879 	  ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
28880 				       DEBUG_SKELETON_ABBREV_SECTION_LABEL,
28881 				       init_sections_and_labels_generation);
28882 
28883 	  /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
28884 	     stay in the main .o, but the skeleton_line goes into the split
28885 	     off dwo.  */
28886 	  debug_skeleton_line_section
28887 	    = get_section (DEBUG_LTO_LINE_SECTION,
28888 			   SECTION_DEBUG | SECTION_EXCLUDE, NULL);
28889 	  ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
28890 				       DEBUG_SKELETON_LINE_SECTION_LABEL,
28891 				       init_sections_and_labels_generation);
28892 	  debug_str_offsets_section
28893 	    = get_section (DEBUG_LTO_DWO_STR_OFFSETS_SECTION,
28894 			   SECTION_DEBUG | SECTION_EXCLUDE,
28895 			   NULL);
28896 	  ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
28897 				       DEBUG_SKELETON_INFO_SECTION_LABEL,
28898 				       init_sections_and_labels_generation);
28899 	  debug_str_dwo_section = get_section (DEBUG_LTO_STR_DWO_SECTION,
28900 					       DEBUG_STR_DWO_SECTION_FLAGS,
28901 					       NULL);
28902 	  debug_macinfo_section_name
28903 	    = ((dwarf_strict && dwarf_version < 5)
28904 	       ? DEBUG_LTO_DWO_MACINFO_SECTION : DEBUG_LTO_DWO_MACRO_SECTION);
28905 	  debug_macinfo_section = get_section (debug_macinfo_section_name,
28906 					       SECTION_DEBUG | SECTION_EXCLUDE,
28907 					       NULL);
28908 	}
28909       /* For macro info and the file table we have to refer to a
28910 	 debug_line section.  */
28911       debug_line_section = get_section (DEBUG_LTO_LINE_SECTION,
28912 					SECTION_DEBUG | SECTION_EXCLUDE, NULL);
28913       ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
28914 				   DEBUG_LINE_SECTION_LABEL,
28915 				   init_sections_and_labels_generation);
28916 
28917       debug_str_section = get_section (DEBUG_LTO_STR_SECTION,
28918 				       DEBUG_STR_SECTION_FLAGS
28919 				       | SECTION_EXCLUDE, NULL);
28920       if (!dwarf_split_debug_info)
28921 	debug_line_str_section
28922 	  = get_section (DEBUG_LTO_LINE_STR_SECTION,
28923 			 DEBUG_STR_SECTION_FLAGS | SECTION_EXCLUDE, NULL);
28924     }
28925   else
28926     {
28927       if (!dwarf_split_debug_info)
28928 	{
28929 	  debug_info_section = get_section (DEBUG_INFO_SECTION,
28930 					    SECTION_DEBUG, NULL);
28931 	  debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
28932 					      SECTION_DEBUG, NULL);
28933 	  debug_loc_section = get_section (dwarf_version >= 5
28934 					   ? DEBUG_LOCLISTS_SECTION
28935 					   : DEBUG_LOC_SECTION,
28936 					   SECTION_DEBUG, NULL);
28937 	  debug_macinfo_section_name
28938 	    = ((dwarf_strict && dwarf_version < 5)
28939 	       ? DEBUG_MACINFO_SECTION : DEBUG_MACRO_SECTION);
28940 	  debug_macinfo_section = get_section (debug_macinfo_section_name,
28941 					       SECTION_DEBUG, NULL);
28942 	}
28943       else
28944 	{
28945 	  debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
28946 					    SECTION_DEBUG | SECTION_EXCLUDE,
28947 					    NULL);
28948 	  debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
28949 					      SECTION_DEBUG | SECTION_EXCLUDE,
28950 					      NULL);
28951 	  debug_addr_section = get_section (DEBUG_ADDR_SECTION,
28952 					    SECTION_DEBUG, NULL);
28953 	  debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
28954 						     SECTION_DEBUG, NULL);
28955 	  debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
28956 						       SECTION_DEBUG, NULL);
28957 	  ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
28958 				       DEBUG_SKELETON_ABBREV_SECTION_LABEL,
28959 				       init_sections_and_labels_generation);
28960 
28961 	  /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
28962 	     stay in the main .o, but the skeleton_line goes into the
28963 	     split off dwo.  */
28964 	  debug_skeleton_line_section
28965 	      = get_section (DEBUG_DWO_LINE_SECTION,
28966 			     SECTION_DEBUG | SECTION_EXCLUDE, NULL);
28967 	  ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
28968 				       DEBUG_SKELETON_LINE_SECTION_LABEL,
28969 				       init_sections_and_labels_generation);
28970 	  debug_str_offsets_section
28971 	    = get_section (DEBUG_DWO_STR_OFFSETS_SECTION,
28972 			   SECTION_DEBUG | SECTION_EXCLUDE, NULL);
28973 	  ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
28974 				       DEBUG_SKELETON_INFO_SECTION_LABEL,
28975 				       init_sections_and_labels_generation);
28976 	  debug_loc_section = get_section (dwarf_version >= 5
28977 					   ? DEBUG_DWO_LOCLISTS_SECTION
28978 					   : DEBUG_DWO_LOC_SECTION,
28979 					   SECTION_DEBUG | SECTION_EXCLUDE,
28980 					   NULL);
28981 	  debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
28982 					       DEBUG_STR_DWO_SECTION_FLAGS,
28983 					       NULL);
28984 	  debug_macinfo_section_name
28985 	    = ((dwarf_strict && dwarf_version < 5)
28986 	       ? DEBUG_DWO_MACINFO_SECTION : DEBUG_DWO_MACRO_SECTION);
28987 	  debug_macinfo_section = get_section (debug_macinfo_section_name,
28988 					       SECTION_DEBUG | SECTION_EXCLUDE,
28989 					       NULL);
28990 	  if (dwarf_version >= 5)
28991 	    debug_ranges_dwo_section
28992 	      = get_section (DEBUG_DWO_RNGLISTS_SECTION,
28993 			     SECTION_DEBUG | SECTION_EXCLUDE, NULL);
28994 	}
28995       debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
28996 					   SECTION_DEBUG, NULL);
28997       debug_line_section = get_section (DEBUG_LINE_SECTION,
28998 					SECTION_DEBUG, NULL);
28999       debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
29000 					    SECTION_DEBUG, NULL);
29001       debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
29002 					    SECTION_DEBUG, NULL);
29003       debug_str_section = get_section (DEBUG_STR_SECTION,
29004 				       DEBUG_STR_SECTION_FLAGS, NULL);
29005       if ((!dwarf_split_debug_info && !output_asm_line_debug_info ())
29006 	  || asm_outputs_debug_line_str ())
29007 	debug_line_str_section = get_section (DEBUG_LINE_STR_SECTION,
29008 					      DEBUG_STR_SECTION_FLAGS, NULL);
29009 
29010       debug_ranges_section = get_section (dwarf_version >= 5
29011 					  ? DEBUG_RNGLISTS_SECTION
29012 					  : DEBUG_RANGES_SECTION,
29013 					  SECTION_DEBUG, NULL);
29014       debug_frame_section = get_section (DEBUG_FRAME_SECTION,
29015 					 SECTION_DEBUG, NULL);
29016     }
29017 
29018   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
29019 			       DEBUG_ABBREV_SECTION_LABEL,
29020 			       init_sections_and_labels_generation);
29021   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
29022 			       DEBUG_INFO_SECTION_LABEL,
29023 			       init_sections_and_labels_generation);
29024   info_section_emitted = false;
29025   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
29026 			       DEBUG_LINE_SECTION_LABEL,
29027 			       init_sections_and_labels_generation);
29028   /* There are up to 6 unique ranges labels per generation.
29029      See also output_rnglists.  */
29030   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
29031 			       DEBUG_RANGES_SECTION_LABEL,
29032 			       init_sections_and_labels_generation * 6);
29033   if (dwarf_version >= 5 && dwarf_split_debug_info)
29034     ASM_GENERATE_INTERNAL_LABEL (ranges_base_label,
29035 				 DEBUG_RANGES_SECTION_LABEL,
29036 				 1 + init_sections_and_labels_generation * 6);
29037   ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
29038 			       DEBUG_ADDR_SECTION_LABEL,
29039 			       init_sections_and_labels_generation);
29040   ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
29041 			       (dwarf_strict && dwarf_version < 5)
29042 			       ? DEBUG_MACINFO_SECTION_LABEL
29043 			       : DEBUG_MACRO_SECTION_LABEL,
29044 			       init_sections_and_labels_generation);
29045   ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL,
29046 			       init_sections_and_labels_generation);
29047 
29048   ++init_sections_and_labels_generation;
29049   return init_sections_and_labels_generation - 1;
29050 }
29051 
29052 /* Set up for Dwarf output at the start of compilation.  */
29053 
29054 static void
dwarf2out_init(const char * filename ATTRIBUTE_UNUSED)29055 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
29056 {
29057   /* Allocate the file_table.  */
29058   file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
29059 
29060 #ifndef DWARF2_LINENO_DEBUGGING_INFO
29061   /* Allocate the decl_die_table.  */
29062   decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
29063 
29064   /* Allocate the decl_loc_table.  */
29065   decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
29066 
29067   /* Allocate the cached_dw_loc_list_table.  */
29068   cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
29069 
29070   /* Allocate the initial hunk of the abbrev_die_table.  */
29071   vec_alloc (abbrev_die_table, 256);
29072   /* Zero-th entry is allocated, but unused.  */
29073   abbrev_die_table->quick_push (NULL);
29074 
29075   /* Allocate the dwarf_proc_stack_usage_map.  */
29076   dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
29077 
29078   /* Allocate the pubtypes and pubnames vectors.  */
29079   vec_alloc (pubname_table, 32);
29080   vec_alloc (pubtype_table, 32);
29081 
29082   vec_alloc (incomplete_types, 64);
29083 
29084   vec_alloc (used_rtx_array, 32);
29085 
29086   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
29087     vec_alloc (macinfo_table, 64);
29088 #endif
29089 
29090   /* If front-ends already registered a main translation unit but we were not
29091      ready to perform the association, do this now.  */
29092   if (main_translation_unit != NULL_TREE)
29093     equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
29094 }
29095 
29096 /* Called before compile () starts outputtting functions, variables
29097    and toplevel asms into assembly.  */
29098 
29099 static void
dwarf2out_assembly_start(void)29100 dwarf2out_assembly_start (void)
29101 {
29102   if (text_section_line_info)
29103     return;
29104 
29105 #ifndef DWARF2_LINENO_DEBUGGING_INFO
29106   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
29107   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
29108   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
29109 			       COLD_TEXT_SECTION_LABEL, 0);
29110   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
29111 
29112   switch_to_section (text_section);
29113   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
29114 #endif
29115 
29116   /* Make sure the line number table for .text always exists.  */
29117   text_section_line_info = new_line_info_table ();
29118   text_section_line_info->end_label = text_end_label;
29119 
29120 #ifdef DWARF2_LINENO_DEBUGGING_INFO
29121   cur_line_info_table = text_section_line_info;
29122 #endif
29123 
29124   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
29125       && dwarf2out_do_cfi_asm ()
29126       && !dwarf2out_do_eh_frame ())
29127     fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
29128 
29129 #if defined(HAVE_AS_GDWARF_5_DEBUG_FLAG) && defined(HAVE_AS_WORKING_DWARF_N_FLAG)
29130   if (output_asm_line_debug_info () && dwarf_version >= 5)
29131     {
29132       /* When gas outputs DWARF5 .debug_line[_str] then we have to
29133 	 tell it the comp_dir and main file name for the zero entry
29134 	 line table.  */
29135       const char *comp_dir, *filename0;
29136 
29137       comp_dir = comp_dir_string ();
29138       if (comp_dir == NULL)
29139 	comp_dir = "";
29140 
29141       filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
29142       if (filename0 == NULL)
29143 	filename0 = "";
29144 
29145       fprintf (asm_out_file, "\t.file 0 ");
29146       output_quoted_string (asm_out_file, remap_debug_filename (comp_dir));
29147       fputc (' ', asm_out_file);
29148       output_quoted_string (asm_out_file, remap_debug_filename (filename0));
29149       fputc ('\n', asm_out_file);
29150     }
29151 #endif
29152 }
29153 
29154 /* A helper function for dwarf2out_finish called through
29155    htab_traverse.  Assign a string its index.  All strings must be
29156    collected into the table by the time index_string is called,
29157    because the indexing code relies on htab_traverse to traverse nodes
29158    in the same order for each run. */
29159 
29160 int
index_string(indirect_string_node ** h,unsigned int * index)29161 index_string (indirect_string_node **h, unsigned int *index)
29162 {
29163   indirect_string_node *node = *h;
29164 
29165   find_string_form (node);
29166   if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29167     {
29168       gcc_assert (node->index == NO_INDEX_ASSIGNED);
29169       node->index = *index;
29170       *index += 1;
29171     }
29172   return 1;
29173 }
29174 
29175 /* A helper function for output_indirect_strings called through
29176    htab_traverse.  Output the offset to a string and update the
29177    current offset.  */
29178 
29179 int
output_index_string_offset(indirect_string_node ** h,unsigned int * offset)29180 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
29181 {
29182   indirect_string_node *node = *h;
29183 
29184   if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29185     {
29186       /* Assert that this node has been assigned an index.  */
29187       gcc_assert (node->index != NO_INDEX_ASSIGNED
29188                   && node->index != NOT_INDEXED);
29189       dw2_asm_output_data (dwarf_offset_size, *offset,
29190                            "indexed string 0x%x: %s", node->index, node->str);
29191       *offset += strlen (node->str) + 1;
29192     }
29193   return 1;
29194 }
29195 
29196 /* A helper function for dwarf2out_finish called through
29197    htab_traverse.  Output the indexed string.  */
29198 
29199 int
output_index_string(indirect_string_node ** h,unsigned int * cur_idx)29200 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
29201 {
29202   struct indirect_string_node *node = *h;
29203 
29204   if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29205     {
29206       /* Assert that the strings are output in the same order as their
29207          indexes were assigned.  */
29208       gcc_assert (*cur_idx == node->index);
29209       assemble_string (node->str, strlen (node->str) + 1);
29210       *cur_idx += 1;
29211     }
29212   return 1;
29213 }
29214 
29215 /* A helper function for output_indirect_strings.  Counts the number
29216    of index strings offsets.  Must match the logic of the functions
29217    output_index_string[_offsets] above.  */
29218 int
count_index_strings(indirect_string_node ** h,unsigned int * last_idx)29219 count_index_strings (indirect_string_node **h, unsigned int *last_idx)
29220 {
29221   struct indirect_string_node *node = *h;
29222 
29223   if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29224     *last_idx += 1;
29225   return 1;
29226 }
29227 
29228 /* A helper function for dwarf2out_finish called through
29229    htab_traverse.  Emit one queued .debug_str string.  */
29230 
29231 int
output_indirect_string(indirect_string_node ** h,enum dwarf_form form)29232 output_indirect_string (indirect_string_node **h, enum dwarf_form form)
29233 {
29234   struct indirect_string_node *node = *h;
29235 
29236   node->form = find_string_form (node);
29237   if (node->form == form && node->refcount > 0)
29238     {
29239       ASM_OUTPUT_LABEL (asm_out_file, node->label);
29240       assemble_string (node->str, strlen (node->str) + 1);
29241     }
29242 
29243   return 1;
29244 }
29245 
29246 /* Output the indexed string table.  */
29247 
29248 static void
output_indirect_strings(void)29249 output_indirect_strings (void)
29250 {
29251   switch_to_section (debug_str_section);
29252   if (!dwarf_split_debug_info)
29253     debug_str_hash->traverse<enum dwarf_form,
29254 			     output_indirect_string> (DW_FORM_strp);
29255   else
29256     {
29257       unsigned int offset = 0;
29258       unsigned int cur_idx = 0;
29259 
29260       if (skeleton_debug_str_hash)
29261         skeleton_debug_str_hash->traverse<enum dwarf_form,
29262 					  output_indirect_string> (DW_FORM_strp);
29263 
29264       switch_to_section (debug_str_offsets_section);
29265       /* For DWARF5 the .debug_str_offsets[.dwo] section needs a unit
29266 	 header.  Note that we don't need to generate a label to the
29267 	 actual index table following the header here, because this is
29268 	 for the split dwarf case only.  In an .dwo file there is only
29269 	 one string offsets table (and one debug info section).  But
29270 	 if we would start using string offset tables for the main (or
29271 	 skeleton) unit, then we have to add a DW_AT_str_offsets_base
29272 	 pointing to the actual index after the header.  Split dwarf
29273 	 units will never have a string offsets base attribute.  When
29274 	 a split unit is moved into a .dwp file the string offsets can
29275 	 be found through the .debug_cu_index section table.  */
29276       if (dwarf_version >= 5)
29277 	{
29278 	  unsigned int last_idx = 0;
29279 	  unsigned long str_offsets_length;
29280 
29281 	  debug_str_hash->traverse_noresize
29282 	    <unsigned int *, count_index_strings> (&last_idx);
29283 	  str_offsets_length = last_idx * dwarf_offset_size + 4;
29284 	  if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
29285 	    dw2_asm_output_data (4, 0xffffffff,
29286 				 "Escape value for 64-bit DWARF extension");
29287 	  dw2_asm_output_data (dwarf_offset_size, str_offsets_length,
29288 			       "Length of string offsets unit");
29289 	  dw2_asm_output_data (2, 5, "DWARF string offsets version");
29290 	  dw2_asm_output_data (2, 0, "Header zero padding");
29291 	}
29292       debug_str_hash->traverse_noresize
29293 	<unsigned int *, output_index_string_offset> (&offset);
29294       switch_to_section (debug_str_dwo_section);
29295       debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
29296 	(&cur_idx);
29297     }
29298 }
29299 
29300 /* Callback for htab_traverse to assign an index to an entry in the
29301    table, and to write that entry to the .debug_addr section.  */
29302 
29303 int
output_addr_table_entry(addr_table_entry ** slot,unsigned int * cur_index)29304 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
29305 {
29306   addr_table_entry *entry = *slot;
29307 
29308   if (entry->refcount == 0)
29309     {
29310       gcc_assert (entry->index == NO_INDEX_ASSIGNED
29311                   || entry->index == NOT_INDEXED);
29312       return 1;
29313     }
29314 
29315   gcc_assert (entry->index == *cur_index);
29316   (*cur_index)++;
29317 
29318   switch (entry->kind)
29319     {
29320       case ate_kind_rtx:
29321         dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
29322                                  "0x%x", entry->index);
29323         break;
29324       case ate_kind_rtx_dtprel:
29325         gcc_assert (targetm.asm_out.output_dwarf_dtprel);
29326         targetm.asm_out.output_dwarf_dtprel (asm_out_file,
29327                                              DWARF2_ADDR_SIZE,
29328                                              entry->addr.rtl);
29329         fputc ('\n', asm_out_file);
29330         break;
29331       case ate_kind_label:
29332         dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
29333                                  "0x%x", entry->index);
29334         break;
29335       default:
29336         gcc_unreachable ();
29337     }
29338   return 1;
29339 }
29340 
29341 /* A helper function for dwarf2out_finish.  Counts the number
29342    of indexed addresses.  Must match the logic of the functions
29343    output_addr_table_entry above.  */
29344 int
count_index_addrs(addr_table_entry ** slot,unsigned int * last_idx)29345 count_index_addrs (addr_table_entry **slot, unsigned int *last_idx)
29346 {
29347   addr_table_entry *entry = *slot;
29348 
29349   if (entry->refcount > 0)
29350     *last_idx += 1;
29351   return 1;
29352 }
29353 
29354 /* Produce the .debug_addr section.  */
29355 
29356 static void
output_addr_table(void)29357 output_addr_table (void)
29358 {
29359   unsigned int index = 0;
29360   if (addr_index_table == NULL || addr_index_table->size () == 0)
29361     return;
29362 
29363   switch_to_section (debug_addr_section);
29364   /* GNU DebugFission https://gcc.gnu.org/wiki/DebugFission
29365      which GCC uses to implement -gsplit-dwarf as DWARF GNU extension
29366      before DWARF5, didn't have a header for .debug_addr units.
29367      DWARF5 specifies a small header when address tables are used.  */
29368   if (dwarf_version >= 5)
29369     {
29370       unsigned int last_idx = 0;
29371       unsigned long addrs_length;
29372 
29373       addr_index_table->traverse_noresize
29374 	<unsigned int *, count_index_addrs> (&last_idx);
29375       addrs_length = last_idx * DWARF2_ADDR_SIZE + 4;
29376 
29377       if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
29378 	dw2_asm_output_data (4, 0xffffffff,
29379 			     "Escape value for 64-bit DWARF extension");
29380       dw2_asm_output_data (dwarf_offset_size, addrs_length,
29381 			   "Length of Address Unit");
29382       dw2_asm_output_data (2, 5, "DWARF addr version");
29383       dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
29384       dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
29385     }
29386   ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
29387 
29388   addr_index_table
29389     ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
29390 }
29391 
29392 #if ENABLE_ASSERT_CHECKING
29393 /* Verify that all marks are clear.  */
29394 
29395 static void
verify_marks_clear(dw_die_ref die)29396 verify_marks_clear (dw_die_ref die)
29397 {
29398   dw_die_ref c;
29399 
29400   gcc_assert (! die->die_mark);
29401   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
29402 }
29403 #endif /* ENABLE_ASSERT_CHECKING */
29404 
29405 /* Clear the marks for a die and its children.
29406    Be cool if the mark isn't set.  */
29407 
29408 static void
prune_unmark_dies(dw_die_ref die)29409 prune_unmark_dies (dw_die_ref die)
29410 {
29411   dw_die_ref c;
29412 
29413   if (die->die_mark)
29414     die->die_mark = 0;
29415   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
29416 }
29417 
29418 /* Given LOC that is referenced by a DIE we're marking as used, find all
29419    referenced DWARF procedures it references and mark them as used.  */
29420 
29421 static void
prune_unused_types_walk_loc_descr(dw_loc_descr_ref loc)29422 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
29423 {
29424   for (; loc != NULL; loc = loc->dw_loc_next)
29425     switch (loc->dw_loc_opc)
29426       {
29427       case DW_OP_implicit_pointer:
29428       case DW_OP_convert:
29429       case DW_OP_reinterpret:
29430       case DW_OP_GNU_implicit_pointer:
29431       case DW_OP_GNU_convert:
29432       case DW_OP_GNU_reinterpret:
29433 	if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
29434 	  prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
29435 	break;
29436       case DW_OP_GNU_variable_value:
29437 	if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
29438 	  {
29439 	    dw_die_ref ref
29440 	      = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
29441 	    if (ref == NULL)
29442 	      break;
29443 	    loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
29444 	    loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
29445 	    loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
29446 	  }
29447 	/* FALLTHRU */
29448       case DW_OP_call2:
29449       case DW_OP_call4:
29450       case DW_OP_call_ref:
29451       case DW_OP_const_type:
29452       case DW_OP_GNU_const_type:
29453       case DW_OP_GNU_parameter_ref:
29454 	gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
29455 	prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
29456 	break;
29457       case DW_OP_regval_type:
29458       case DW_OP_deref_type:
29459       case DW_OP_GNU_regval_type:
29460       case DW_OP_GNU_deref_type:
29461 	gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
29462 	prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
29463 	break;
29464       case DW_OP_entry_value:
29465       case DW_OP_GNU_entry_value:
29466 	gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
29467 	prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
29468 	break;
29469       default:
29470 	break;
29471       }
29472 }
29473 
29474 /* Given DIE that we're marking as used, find any other dies
29475    it references as attributes and mark them as used.  */
29476 
29477 static void
prune_unused_types_walk_attribs(dw_die_ref die)29478 prune_unused_types_walk_attribs (dw_die_ref die)
29479 {
29480   dw_attr_node *a;
29481   unsigned ix;
29482 
29483   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29484     {
29485       switch (AT_class (a))
29486 	{
29487 	/* Make sure DWARF procedures referenced by location descriptions will
29488 	   get emitted.  */
29489 	case dw_val_class_loc:
29490 	  prune_unused_types_walk_loc_descr (AT_loc (a));
29491 	  break;
29492 	case dw_val_class_loc_list:
29493 	  for (dw_loc_list_ref list = AT_loc_list (a);
29494 	       list != NULL;
29495 	       list = list->dw_loc_next)
29496 	    prune_unused_types_walk_loc_descr (list->expr);
29497 	  break;
29498 
29499 	case dw_val_class_view_list:
29500 	  /* This points to a loc_list in another attribute, so it's
29501 	     already covered.  */
29502 	  break;
29503 
29504 	case dw_val_class_die_ref:
29505 	  /* A reference to another DIE.
29506 	     Make sure that it will get emitted.
29507 	     If it was broken out into a comdat group, don't follow it.  */
29508           if (! AT_ref (a)->comdat_type_p
29509               || a->dw_attr == DW_AT_specification)
29510 	    prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
29511 	  break;
29512 
29513 	case dw_val_class_str:
29514 	  /* Set the string's refcount to 0 so that prune_unused_types_mark
29515 	     accounts properly for it.  */
29516 	  a->dw_attr_val.v.val_str->refcount = 0;
29517 	  break;
29518 
29519 	default:
29520 	  break;
29521 	}
29522     }
29523 }
29524 
29525 /* Mark the generic parameters and arguments children DIEs of DIE.  */
29526 
29527 static void
prune_unused_types_mark_generic_parms_dies(dw_die_ref die)29528 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
29529 {
29530   dw_die_ref c;
29531 
29532   if (die == NULL || die->die_child == NULL)
29533     return;
29534   c = die->die_child;
29535   do
29536     {
29537       if (is_template_parameter (c))
29538 	prune_unused_types_mark (c, 1);
29539       c = c->die_sib;
29540     } while (c && c != die->die_child);
29541 }
29542 
29543 /* Mark DIE as being used.  If DOKIDS is true, then walk down
29544    to DIE's children.  */
29545 
29546 static void
prune_unused_types_mark(dw_die_ref die,int dokids)29547 prune_unused_types_mark (dw_die_ref die, int dokids)
29548 {
29549   dw_die_ref c;
29550 
29551   if (die->die_mark == 0)
29552     {
29553       /* We haven't done this node yet.  Mark it as used.  */
29554       die->die_mark = 1;
29555       /* If this is the DIE of a generic type instantiation,
29556 	 mark the children DIEs that describe its generic parms and
29557 	 args.  */
29558       prune_unused_types_mark_generic_parms_dies (die);
29559 
29560       /* We also have to mark its parents as used.
29561 	 (But we don't want to mark our parent's kids due to this,
29562 	 unless it is a class.)  */
29563       if (die->die_parent)
29564 	prune_unused_types_mark (die->die_parent,
29565 				 class_scope_p (die->die_parent));
29566 
29567       /* Mark any referenced nodes.  */
29568       prune_unused_types_walk_attribs (die);
29569 
29570       /* If this node is a specification,
29571 	 also mark the definition, if it exists.  */
29572       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
29573 	prune_unused_types_mark (die->die_definition, 1);
29574     }
29575 
29576   if (dokids && die->die_mark != 2)
29577     {
29578       /* We need to walk the children, but haven't done so yet.
29579 	 Remember that we've walked the kids.  */
29580       die->die_mark = 2;
29581 
29582       /* If this is an array type, we need to make sure our
29583 	 kids get marked, even if they're types.  If we're
29584 	 breaking out types into comdat sections, do this
29585 	 for all type definitions.  */
29586       if (die->die_tag == DW_TAG_array_type
29587           || (use_debug_types
29588               && is_type_die (die) && ! is_declaration_die (die)))
29589 	FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
29590       else
29591 	FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
29592     }
29593 }
29594 
29595 /* For local classes, look if any static member functions were emitted
29596    and if so, mark them.  */
29597 
29598 static void
prune_unused_types_walk_local_classes(dw_die_ref die)29599 prune_unused_types_walk_local_classes (dw_die_ref die)
29600 {
29601   dw_die_ref c;
29602 
29603   if (die->die_mark == 2)
29604     return;
29605 
29606   switch (die->die_tag)
29607     {
29608     case DW_TAG_structure_type:
29609     case DW_TAG_union_type:
29610     case DW_TAG_class_type:
29611     case DW_TAG_interface_type:
29612       break;
29613 
29614     case DW_TAG_subprogram:
29615       if (!get_AT_flag (die, DW_AT_declaration)
29616 	  || die->die_definition != NULL)
29617 	prune_unused_types_mark (die, 1);
29618       return;
29619 
29620     default:
29621       return;
29622     }
29623 
29624   /* Mark children.  */
29625   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
29626 }
29627 
29628 /* Walk the tree DIE and mark types that we actually use.  */
29629 
29630 static void
prune_unused_types_walk(dw_die_ref die)29631 prune_unused_types_walk (dw_die_ref die)
29632 {
29633   dw_die_ref c;
29634 
29635   /* Don't do anything if this node is already marked and
29636      children have been marked as well.  */
29637   if (die->die_mark == 2)
29638     return;
29639 
29640   switch (die->die_tag)
29641     {
29642     case DW_TAG_structure_type:
29643     case DW_TAG_union_type:
29644     case DW_TAG_class_type:
29645     case DW_TAG_interface_type:
29646       if (die->die_perennial_p)
29647 	break;
29648 
29649       for (c = die->die_parent; c; c = c->die_parent)
29650 	if (c->die_tag == DW_TAG_subprogram)
29651 	  break;
29652 
29653       /* Finding used static member functions inside of classes
29654 	 is needed just for local classes, because for other classes
29655 	 static member function DIEs with DW_AT_specification
29656 	 are emitted outside of the DW_TAG_*_type.  If we ever change
29657 	 it, we'd need to call this even for non-local classes.  */
29658       if (c)
29659 	prune_unused_types_walk_local_classes (die);
29660 
29661       /* It's a type node --- don't mark it.  */
29662       return;
29663 
29664     case DW_TAG_const_type:
29665     case DW_TAG_packed_type:
29666     case DW_TAG_pointer_type:
29667     case DW_TAG_reference_type:
29668     case DW_TAG_rvalue_reference_type:
29669     case DW_TAG_volatile_type:
29670     case DW_TAG_typedef:
29671     case DW_TAG_array_type:
29672     case DW_TAG_friend:
29673     case DW_TAG_enumeration_type:
29674     case DW_TAG_subroutine_type:
29675     case DW_TAG_string_type:
29676     case DW_TAG_set_type:
29677     case DW_TAG_subrange_type:
29678     case DW_TAG_ptr_to_member_type:
29679     case DW_TAG_file_type:
29680       /* Type nodes are useful only when other DIEs reference them --- don't
29681 	 mark them.  */
29682       /* FALLTHROUGH */
29683 
29684     case DW_TAG_dwarf_procedure:
29685       /* Likewise for DWARF procedures.  */
29686 
29687       if (die->die_perennial_p)
29688 	break;
29689 
29690       return;
29691 
29692     case DW_TAG_variable:
29693       if (flag_debug_only_used_symbols)
29694 	{
29695 	  if (die->die_perennial_p)
29696 	    break;
29697 
29698 	  /* For static data members, the declaration in the class is supposed
29699 	     to have DW_TAG_member tag in DWARF{3,4} but DW_TAG_variable in
29700 	     DWARF5.  DW_TAG_member will be marked, so mark even such
29701 	     DW_TAG_variables in DWARF5, as long as it has DW_AT_const_value
29702 	     attribute.  */
29703 	  if (dwarf_version >= 5
29704 	      && class_scope_p (die->die_parent)
29705 	      && get_AT (die, DW_AT_const_value))
29706 	    break;
29707 
29708 	  /* premark_used_variables marks external variables --- don't mark
29709 	     them here.  But function-local externals are always considered
29710 	     used.  */
29711 	  if (get_AT (die, DW_AT_external))
29712 	    {
29713 	      for (c = die->die_parent; c; c = c->die_parent)
29714 		if (c->die_tag == DW_TAG_subprogram)
29715 		  break;
29716 	      if (!c)
29717 		return;
29718 	    }
29719 	}
29720       /* FALLTHROUGH */
29721 
29722     default:
29723       /* Mark everything else.  */
29724       break;
29725   }
29726 
29727   if (die->die_mark == 0)
29728     {
29729       die->die_mark = 1;
29730 
29731       /* Now, mark any dies referenced from here.  */
29732       prune_unused_types_walk_attribs (die);
29733     }
29734 
29735   die->die_mark = 2;
29736 
29737   /* Mark children.  */
29738   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
29739 }
29740 
29741 /* Increment the string counts on strings referred to from DIE's
29742    attributes.  */
29743 
29744 static void
prune_unused_types_update_strings(dw_die_ref die)29745 prune_unused_types_update_strings (dw_die_ref die)
29746 {
29747   dw_attr_node *a;
29748   unsigned ix;
29749 
29750   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29751     if (AT_class (a) == dw_val_class_str)
29752       {
29753 	struct indirect_string_node *s = a->dw_attr_val.v.val_str;
29754 	s->refcount++;
29755 	/* Avoid unnecessarily putting strings that are used less than
29756 	   twice in the hash table.  */
29757 	if (s->form != DW_FORM_line_strp
29758 	    && (s->refcount
29759 		== ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2)))
29760 	  {
29761 	    indirect_string_node **slot
29762 	      = debug_str_hash->find_slot_with_hash (s->str,
29763 						     htab_hash_string (s->str),
29764 						     INSERT);
29765 	    gcc_assert (*slot == NULL);
29766 	    *slot = s;
29767 	  }
29768       }
29769 }
29770 
29771 /* Mark DIE and its children as removed.  */
29772 
29773 static void
mark_removed(dw_die_ref die)29774 mark_removed (dw_die_ref die)
29775 {
29776   dw_die_ref c;
29777   die->removed = true;
29778   FOR_EACH_CHILD (die, c, mark_removed (c));
29779 }
29780 
29781 /* Remove from the tree DIE any dies that aren't marked.  */
29782 
29783 static void
prune_unused_types_prune(dw_die_ref die)29784 prune_unused_types_prune (dw_die_ref die)
29785 {
29786   dw_die_ref c;
29787 
29788   gcc_assert (die->die_mark);
29789   prune_unused_types_update_strings (die);
29790 
29791   if (! die->die_child)
29792     return;
29793 
29794   c = die->die_child;
29795   do {
29796     dw_die_ref prev = c, next;
29797     for (c = c->die_sib; ! c->die_mark; c = next)
29798       if (c == die->die_child)
29799 	{
29800 	  /* No marked children between 'prev' and the end of the list.  */
29801 	  if (prev == c)
29802 	    /* No marked children at all.  */
29803 	    die->die_child = NULL;
29804 	  else
29805 	    {
29806 	      prev->die_sib = c->die_sib;
29807 	      die->die_child = prev;
29808 	    }
29809 	  c->die_sib = NULL;
29810 	  mark_removed (c);
29811 	  return;
29812 	}
29813       else
29814 	{
29815 	  next = c->die_sib;
29816 	  c->die_sib = NULL;
29817 	  mark_removed (c);
29818 	}
29819 
29820     if (c != prev->die_sib)
29821       prev->die_sib = c;
29822     prune_unused_types_prune (c);
29823   } while (c != die->die_child);
29824 }
29825 
29826 /* Remove dies representing declarations that we never use.  */
29827 
29828 static void
prune_unused_types(void)29829 prune_unused_types (void)
29830 {
29831   unsigned int i;
29832   limbo_die_node *node;
29833   comdat_type_node *ctnode;
29834   pubname_entry *pub;
29835   dw_die_ref base_type;
29836 
29837 #if ENABLE_ASSERT_CHECKING
29838   /* All the marks should already be clear.  */
29839   verify_marks_clear (comp_unit_die ());
29840   for (node = limbo_die_list; node; node = node->next)
29841     verify_marks_clear (node->die);
29842   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
29843     verify_marks_clear (ctnode->root_die);
29844 #endif /* ENABLE_ASSERT_CHECKING */
29845 
29846   /* Mark types that are used in global variables.  */
29847   premark_types_used_by_global_vars ();
29848 
29849   /* Mark variables used in the symtab.  */
29850   if (flag_debug_only_used_symbols)
29851     premark_used_variables ();
29852 
29853   /* Set the mark on nodes that are actually used.  */
29854   prune_unused_types_walk (comp_unit_die ());
29855   for (node = limbo_die_list; node; node = node->next)
29856     prune_unused_types_walk (node->die);
29857   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
29858     {
29859       prune_unused_types_walk (ctnode->root_die);
29860       prune_unused_types_mark (ctnode->type_die, 1);
29861     }
29862 
29863   /* Also set the mark on nodes referenced from the pubname_table.  Enumerators
29864      are unusual in that they are pubnames that are the children of pubtypes.
29865      They should only be marked via their parent DW_TAG_enumeration_type die,
29866      not as roots in themselves.  */
29867   FOR_EACH_VEC_ELT (*pubname_table, i, pub)
29868     if (pub->die->die_tag != DW_TAG_enumerator)
29869       prune_unused_types_mark (pub->die, 1);
29870   for (i = 0; base_types.iterate (i, &base_type); i++)
29871     prune_unused_types_mark (base_type, 1);
29872 
29873   /* Also set the mark on nodes that could be referenced by
29874      DW_TAG_call_site DW_AT_call_origin (i.e. direct call callees) or
29875      by DW_TAG_inlined_subroutine origins.  */
29876   cgraph_node *cnode;
29877   FOR_EACH_FUNCTION (cnode)
29878     if (cnode->referred_to_p (false))
29879       {
29880 	dw_die_ref die = lookup_decl_die (cnode->decl);
29881 	if (die == NULL || die->die_mark)
29882 	  continue;
29883 	for (cgraph_edge *e = cnode->callers; e; e = e->next_caller)
29884 	  if (e->caller != cnode)
29885 	    {
29886 	      prune_unused_types_mark (die, 1);
29887 	      break;
29888 	    }
29889       }
29890 
29891   if (debug_str_hash)
29892     debug_str_hash->empty ();
29893   if (skeleton_debug_str_hash)
29894     skeleton_debug_str_hash->empty ();
29895   prune_unused_types_prune (comp_unit_die ());
29896   for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
29897     {
29898       node = *pnode;
29899       if (!node->die->die_mark)
29900 	*pnode = node->next;
29901       else
29902 	{
29903 	  prune_unused_types_prune (node->die);
29904 	  pnode = &node->next;
29905 	}
29906     }
29907   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
29908     prune_unused_types_prune (ctnode->root_die);
29909 
29910   /* Leave the marks clear.  */
29911   prune_unmark_dies (comp_unit_die ());
29912   for (node = limbo_die_list; node; node = node->next)
29913     prune_unmark_dies (node->die);
29914   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
29915     prune_unmark_dies (ctnode->root_die);
29916 }
29917 
29918 /* Helpers to manipulate hash table of comdat type units.  */
29919 
29920 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
29921 {
29922   static inline hashval_t hash (const comdat_type_node *);
29923   static inline bool equal (const comdat_type_node *, const comdat_type_node *);
29924 };
29925 
29926 inline hashval_t
hash(const comdat_type_node * type_node)29927 comdat_type_hasher::hash (const comdat_type_node *type_node)
29928 {
29929   hashval_t h;
29930   memcpy (&h, type_node->signature, sizeof (h));
29931   return h;
29932 }
29933 
29934 inline bool
equal(const comdat_type_node * type_node_1,const comdat_type_node * type_node_2)29935 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
29936 			   const comdat_type_node *type_node_2)
29937 {
29938   return (! memcmp (type_node_1->signature, type_node_2->signature,
29939                     DWARF_TYPE_SIGNATURE_SIZE));
29940 }
29941 
29942 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
29943    to the location it would have been added, should we know its
29944    DECL_ASSEMBLER_NAME when we added other attributes.  This will
29945    probably improve compactness of debug info, removing equivalent
29946    abbrevs, and hide any differences caused by deferring the
29947    computation of the assembler name, triggered by e.g. PCH.  */
29948 
29949 static inline void
move_linkage_attr(dw_die_ref die)29950 move_linkage_attr (dw_die_ref die)
29951 {
29952   unsigned ix = vec_safe_length (die->die_attr);
29953   dw_attr_node linkage = (*die->die_attr)[ix - 1];
29954 
29955   gcc_assert (linkage.dw_attr == DW_AT_linkage_name
29956 	      || linkage.dw_attr == DW_AT_MIPS_linkage_name);
29957 
29958   while (--ix > 0)
29959     {
29960       dw_attr_node *prev = &(*die->die_attr)[ix - 1];
29961 
29962       if (prev->dw_attr == DW_AT_decl_line
29963 	  || prev->dw_attr == DW_AT_decl_column
29964 	  || prev->dw_attr == DW_AT_name)
29965 	break;
29966     }
29967 
29968   if (ix != vec_safe_length (die->die_attr) - 1)
29969     {
29970       die->die_attr->pop ();
29971       die->die_attr->quick_insert (ix, linkage);
29972     }
29973 }
29974 
29975 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
29976    referenced from typed stack ops and count how often they are used.  */
29977 
29978 static void
mark_base_types(dw_loc_descr_ref loc)29979 mark_base_types (dw_loc_descr_ref loc)
29980 {
29981   dw_die_ref base_type = NULL;
29982 
29983   for (; loc; loc = loc->dw_loc_next)
29984     {
29985       switch (loc->dw_loc_opc)
29986 	{
29987 	case DW_OP_regval_type:
29988 	case DW_OP_deref_type:
29989 	case DW_OP_GNU_regval_type:
29990 	case DW_OP_GNU_deref_type:
29991 	  base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
29992 	  break;
29993 	case DW_OP_convert:
29994 	case DW_OP_reinterpret:
29995 	case DW_OP_GNU_convert:
29996 	case DW_OP_GNU_reinterpret:
29997 	  if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
29998 	    continue;
29999 	  /* FALLTHRU */
30000 	case DW_OP_const_type:
30001 	case DW_OP_GNU_const_type:
30002 	  base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
30003 	  break;
30004 	case DW_OP_entry_value:
30005 	case DW_OP_GNU_entry_value:
30006 	  mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
30007 	  continue;
30008 	default:
30009 	  continue;
30010 	}
30011       gcc_assert (base_type->die_parent == comp_unit_die ());
30012       if (base_type->die_mark)
30013 	base_type->die_mark++;
30014       else
30015 	{
30016 	  base_types.safe_push (base_type);
30017 	  base_type->die_mark = 1;
30018 	}
30019     }
30020 }
30021 
30022 /* Comparison function for sorting marked base types.  */
30023 
30024 static int
base_type_cmp(const void * x,const void * y)30025 base_type_cmp (const void *x, const void *y)
30026 {
30027   dw_die_ref dx = *(const dw_die_ref *) x;
30028   dw_die_ref dy = *(const dw_die_ref *) y;
30029   unsigned int byte_size1, byte_size2;
30030   unsigned int encoding1, encoding2;
30031   unsigned int align1, align2;
30032   if (dx->die_mark > dy->die_mark)
30033     return -1;
30034   if (dx->die_mark < dy->die_mark)
30035     return 1;
30036   byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
30037   byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
30038   if (byte_size1 < byte_size2)
30039     return 1;
30040   if (byte_size1 > byte_size2)
30041     return -1;
30042   encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
30043   encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
30044   if (encoding1 < encoding2)
30045     return 1;
30046   if (encoding1 > encoding2)
30047     return -1;
30048   align1 = get_AT_unsigned (dx, DW_AT_alignment);
30049   align2 = get_AT_unsigned (dy, DW_AT_alignment);
30050   if (align1 < align2)
30051     return 1;
30052   if (align1 > align2)
30053     return -1;
30054   return 0;
30055 }
30056 
30057 /* Move base types marked by mark_base_types as early as possible
30058    in the CU, sorted by decreasing usage count both to make the
30059    uleb128 references as small as possible and to make sure they
30060    will have die_offset already computed by calc_die_sizes when
30061    sizes of typed stack loc ops is computed.  */
30062 
30063 static void
move_marked_base_types(void)30064 move_marked_base_types (void)
30065 {
30066   unsigned int i;
30067   dw_die_ref base_type, die, c;
30068 
30069   if (base_types.is_empty ())
30070     return;
30071 
30072   /* Sort by decreasing usage count, they will be added again in that
30073      order later on.  */
30074   base_types.qsort (base_type_cmp);
30075   die = comp_unit_die ();
30076   c = die->die_child;
30077   do
30078     {
30079       dw_die_ref prev = c;
30080       c = c->die_sib;
30081       while (c->die_mark)
30082 	{
30083 	  remove_child_with_prev (c, prev);
30084 	  /* As base types got marked, there must be at least
30085 	     one node other than DW_TAG_base_type.  */
30086 	  gcc_assert (die->die_child != NULL);
30087 	  c = prev->die_sib;
30088 	}
30089     }
30090   while (c != die->die_child);
30091   gcc_assert (die->die_child);
30092   c = die->die_child;
30093   for (i = 0; base_types.iterate (i, &base_type); i++)
30094     {
30095       base_type->die_mark = 0;
30096       base_type->die_sib = c->die_sib;
30097       c->die_sib = base_type;
30098       c = base_type;
30099     }
30100 }
30101 
30102 /* Helper function for resolve_addr, attempt to resolve
30103    one CONST_STRING, return true if successful.  Similarly verify that
30104    SYMBOL_REFs refer to variables emitted in the current CU.  */
30105 
30106 static bool
resolve_one_addr(rtx * addr)30107 resolve_one_addr (rtx *addr)
30108 {
30109   rtx rtl = *addr;
30110 
30111   if (GET_CODE (rtl) == CONST_STRING)
30112     {
30113       size_t len = strlen (XSTR (rtl, 0)) + 1;
30114       tree t = build_string (len, XSTR (rtl, 0));
30115       tree tlen = size_int (len - 1);
30116       TREE_TYPE (t)
30117 	= build_array_type (char_type_node, build_index_type (tlen));
30118       rtl = lookup_constant_def (t);
30119       if (!rtl || !MEM_P (rtl))
30120 	return false;
30121       rtl = XEXP (rtl, 0);
30122       if (GET_CODE (rtl) == SYMBOL_REF
30123 	  && SYMBOL_REF_DECL (rtl)
30124 	  && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
30125 	return false;
30126       vec_safe_push (used_rtx_array, rtl);
30127       *addr = rtl;
30128       return true;
30129     }
30130 
30131   if (GET_CODE (rtl) == SYMBOL_REF
30132       && SYMBOL_REF_DECL (rtl))
30133     {
30134       if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
30135 	{
30136 	  if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
30137 	    return false;
30138 	}
30139       else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
30140 	return false;
30141     }
30142 
30143   if (GET_CODE (rtl) == CONST)
30144     {
30145       subrtx_ptr_iterator::array_type array;
30146       FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
30147 	if (!resolve_one_addr (*iter))
30148 	  return false;
30149     }
30150 
30151   return true;
30152 }
30153 
30154 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
30155    if possible, and create DW_TAG_dwarf_procedure that can be referenced
30156    from DW_OP_implicit_pointer if the string hasn't been seen yet.  */
30157 
30158 static rtx
string_cst_pool_decl(tree t)30159 string_cst_pool_decl (tree t)
30160 {
30161   rtx rtl = output_constant_def (t, 1);
30162   unsigned char *array;
30163   dw_loc_descr_ref l;
30164   tree decl;
30165   size_t len;
30166   dw_die_ref ref;
30167 
30168   if (!rtl || !MEM_P (rtl))
30169     return NULL_RTX;
30170   rtl = XEXP (rtl, 0);
30171   if (GET_CODE (rtl) != SYMBOL_REF
30172       || SYMBOL_REF_DECL (rtl) == NULL_TREE)
30173     return NULL_RTX;
30174 
30175   decl = SYMBOL_REF_DECL (rtl);
30176   if (!lookup_decl_die (decl))
30177     {
30178       len = TREE_STRING_LENGTH (t);
30179       vec_safe_push (used_rtx_array, rtl);
30180       ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
30181       array = ggc_vec_alloc<unsigned char> (len);
30182       memcpy (array, TREE_STRING_POINTER (t), len);
30183       l = new_loc_descr (DW_OP_implicit_value, len, 0);
30184       l->dw_loc_oprnd2.val_class = dw_val_class_vec;
30185       l->dw_loc_oprnd2.v.val_vec.length = len;
30186       l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
30187       l->dw_loc_oprnd2.v.val_vec.array = array;
30188       add_AT_loc (ref, DW_AT_location, l);
30189       equate_decl_number_to_die (decl, ref);
30190     }
30191   return rtl;
30192 }
30193 
30194 /* Helper function of resolve_addr_in_expr.  LOC is
30195    a DW_OP_addr followed by DW_OP_stack_value, either at the start
30196    of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
30197    resolved.  Replace it (both DW_OP_addr and DW_OP_stack_value)
30198    with DW_OP_implicit_pointer if possible
30199    and return true, if unsuccessful, return false.  */
30200 
30201 static bool
optimize_one_addr_into_implicit_ptr(dw_loc_descr_ref loc)30202 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
30203 {
30204   rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
30205   HOST_WIDE_INT offset = 0;
30206   dw_die_ref ref = NULL;
30207   tree decl;
30208 
30209   if (GET_CODE (rtl) == CONST
30210       && GET_CODE (XEXP (rtl, 0)) == PLUS
30211       && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
30212     {
30213       offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
30214       rtl = XEXP (XEXP (rtl, 0), 0);
30215     }
30216   if (GET_CODE (rtl) == CONST_STRING)
30217     {
30218       size_t len = strlen (XSTR (rtl, 0)) + 1;
30219       tree t = build_string (len, XSTR (rtl, 0));
30220       tree tlen = size_int (len - 1);
30221 
30222       TREE_TYPE (t)
30223 	= build_array_type (char_type_node, build_index_type (tlen));
30224       rtl = string_cst_pool_decl (t);
30225       if (!rtl)
30226 	return false;
30227     }
30228   if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
30229     {
30230       decl = SYMBOL_REF_DECL (rtl);
30231       if (VAR_P (decl) && !DECL_EXTERNAL (decl))
30232 	{
30233 	  ref = lookup_decl_die (decl);
30234 	  if (ref && (get_AT (ref, DW_AT_location)
30235 		      || get_AT (ref, DW_AT_const_value)))
30236 	    {
30237 	      loc->dw_loc_opc = dwarf_OP (DW_OP_implicit_pointer);
30238 	      loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30239 	      loc->dw_loc_oprnd1.val_entry = NULL;
30240 	      loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30241 	      loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30242 	      loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
30243 	      loc->dw_loc_oprnd2.v.val_int = offset;
30244 	      return true;
30245 	    }
30246 	}
30247     }
30248   return false;
30249 }
30250 
30251 /* Helper function for resolve_addr, handle one location
30252    expression, return false if at least one CONST_STRING or SYMBOL_REF in
30253    the location list couldn't be resolved.  */
30254 
30255 static bool
resolve_addr_in_expr(dw_attr_node * a,dw_loc_descr_ref loc)30256 resolve_addr_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
30257 {
30258   dw_loc_descr_ref keep = NULL;
30259   for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
30260     switch (loc->dw_loc_opc)
30261       {
30262       case DW_OP_addr:
30263 	if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
30264 	  {
30265 	    if ((prev == NULL
30266 		 || prev->dw_loc_opc == DW_OP_piece
30267 		 || prev->dw_loc_opc == DW_OP_bit_piece)
30268 		&& loc->dw_loc_next
30269 		&& loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
30270 		&& (!dwarf_strict || dwarf_version >= 5)
30271 		&& optimize_one_addr_into_implicit_ptr (loc))
30272 	      break;
30273 	    return false;
30274 	  }
30275 	break;
30276       case DW_OP_GNU_addr_index:
30277       case DW_OP_addrx:
30278       case DW_OP_GNU_const_index:
30279       case DW_OP_constx:
30280 	if ((loc->dw_loc_opc == DW_OP_GNU_addr_index
30281 	     || loc->dw_loc_opc == DW_OP_addrx)
30282 	    || ((loc->dw_loc_opc == DW_OP_GNU_const_index
30283 		 || loc->dw_loc_opc == DW_OP_constx)
30284 		&& loc->dtprel))
30285           {
30286             rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
30287             if (!resolve_one_addr (&rtl))
30288               return false;
30289             remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
30290 	    loc->dw_loc_oprnd1.val_entry
30291 	      = add_addr_table_entry (rtl, ate_kind_rtx);
30292           }
30293 	break;
30294       case DW_OP_const4u:
30295       case DW_OP_const8u:
30296 	if (loc->dtprel
30297 	    && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
30298 	  return false;
30299 	break;
30300       case DW_OP_plus_uconst:
30301 	if (size_of_loc_descr (loc)
30302 	    > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
30303 	      + 1
30304 	    && loc->dw_loc_oprnd1.v.val_unsigned > 0)
30305 	  {
30306 	    dw_loc_descr_ref repl
30307 	      = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
30308 	    add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
30309 	    add_loc_descr (&repl, loc->dw_loc_next);
30310 	    *loc = *repl;
30311 	  }
30312 	break;
30313       case DW_OP_implicit_value:
30314 	if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
30315 	    && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
30316 	  return false;
30317 	break;
30318       case DW_OP_implicit_pointer:
30319       case DW_OP_GNU_implicit_pointer:
30320       case DW_OP_GNU_parameter_ref:
30321       case DW_OP_GNU_variable_value:
30322 	if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
30323 	  {
30324 	    dw_die_ref ref
30325 	      = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
30326 	    if (ref == NULL)
30327 	      return false;
30328 	    loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30329 	    loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30330 	    loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30331 	  }
30332 	if (loc->dw_loc_opc == DW_OP_GNU_variable_value)
30333 	  {
30334 	    if (prev == NULL
30335 		&& loc->dw_loc_next == NULL
30336 		&& AT_class (a) == dw_val_class_loc)
30337 	      switch (a->dw_attr)
30338 		{
30339 		  /* Following attributes allow both exprloc and reference,
30340 		     so if the whole expression is DW_OP_GNU_variable_value
30341 		     alone we could transform it into reference.  */
30342 		case DW_AT_byte_size:
30343 		case DW_AT_bit_size:
30344 		case DW_AT_lower_bound:
30345 		case DW_AT_upper_bound:
30346 		case DW_AT_bit_stride:
30347 		case DW_AT_count:
30348 		case DW_AT_allocated:
30349 		case DW_AT_associated:
30350 		case DW_AT_byte_stride:
30351 		  a->dw_attr_val.val_class = dw_val_class_die_ref;
30352 		  a->dw_attr_val.val_entry = NULL;
30353 		  a->dw_attr_val.v.val_die_ref.die
30354 		    = loc->dw_loc_oprnd1.v.val_die_ref.die;
30355 		  a->dw_attr_val.v.val_die_ref.external = 0;
30356 		  return true;
30357 		default:
30358 		  break;
30359 		}
30360 	    if (dwarf_strict)
30361 	      return false;
30362 	  }
30363 	break;
30364       case DW_OP_const_type:
30365       case DW_OP_regval_type:
30366       case DW_OP_deref_type:
30367       case DW_OP_convert:
30368       case DW_OP_reinterpret:
30369       case DW_OP_GNU_const_type:
30370       case DW_OP_GNU_regval_type:
30371       case DW_OP_GNU_deref_type:
30372       case DW_OP_GNU_convert:
30373       case DW_OP_GNU_reinterpret:
30374 	while (loc->dw_loc_next
30375 	       && (loc->dw_loc_next->dw_loc_opc == DW_OP_convert
30376 		   || loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert))
30377 	  {
30378 	    dw_die_ref base1, base2;
30379 	    unsigned enc1, enc2, size1, size2;
30380 	    if (loc->dw_loc_opc == DW_OP_regval_type
30381 		|| loc->dw_loc_opc == DW_OP_deref_type
30382 		|| loc->dw_loc_opc == DW_OP_GNU_regval_type
30383 		|| loc->dw_loc_opc == DW_OP_GNU_deref_type)
30384 	      base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
30385 	    else if (loc->dw_loc_oprnd1.val_class
30386 		     == dw_val_class_unsigned_const)
30387 	      break;
30388 	    else
30389 	      base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
30390 	    if (loc->dw_loc_next->dw_loc_oprnd1.val_class
30391 		== dw_val_class_unsigned_const)
30392 	      break;
30393 	    base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
30394 	    gcc_assert (base1->die_tag == DW_TAG_base_type
30395 			&& base2->die_tag == DW_TAG_base_type);
30396 	    enc1 = get_AT_unsigned (base1, DW_AT_encoding);
30397 	    enc2 = get_AT_unsigned (base2, DW_AT_encoding);
30398 	    size1 = get_AT_unsigned (base1, DW_AT_byte_size);
30399 	    size2 = get_AT_unsigned (base2, DW_AT_byte_size);
30400 	    if (size1 == size2
30401 		&& (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
30402 		     && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
30403 		     && loc != keep)
30404 		    || enc1 == enc2))
30405 	      {
30406 		/* Optimize away next DW_OP_convert after
30407 		   adjusting LOC's base type die reference.  */
30408 		if (loc->dw_loc_opc == DW_OP_regval_type
30409 		    || loc->dw_loc_opc == DW_OP_deref_type
30410 		    || loc->dw_loc_opc == DW_OP_GNU_regval_type
30411 		    || loc->dw_loc_opc == DW_OP_GNU_deref_type)
30412 		  loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
30413 		else
30414 		  loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
30415 		loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
30416 		continue;
30417 	      }
30418 	    /* Don't change integer DW_OP_convert after e.g. floating
30419 	       point typed stack entry.  */
30420 	    else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
30421 	      keep = loc->dw_loc_next;
30422 	    break;
30423 	  }
30424 	break;
30425       default:
30426 	break;
30427       }
30428   return true;
30429 }
30430 
30431 /* Helper function of resolve_addr.  DIE had DW_AT_location of
30432    DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
30433    and DW_OP_addr couldn't be resolved.  resolve_addr has already
30434    removed the DW_AT_location attribute.  This function attempts to
30435    add a new DW_AT_location attribute with DW_OP_implicit_pointer
30436    to it or DW_AT_const_value attribute, if possible.  */
30437 
30438 static void
optimize_location_into_implicit_ptr(dw_die_ref die,tree decl)30439 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
30440 {
30441   if (!VAR_P (decl)
30442       || lookup_decl_die (decl) != die
30443       || DECL_EXTERNAL (decl)
30444       || !TREE_STATIC (decl)
30445       || DECL_INITIAL (decl) == NULL_TREE
30446       || DECL_P (DECL_INITIAL (decl))
30447       || get_AT (die, DW_AT_const_value))
30448     return;
30449 
30450   tree init = DECL_INITIAL (decl);
30451   HOST_WIDE_INT offset = 0;
30452   /* For variables that have been optimized away and thus
30453      don't have a memory location, see if we can emit
30454      DW_AT_const_value instead.  */
30455   if (tree_add_const_value_attribute (die, init))
30456     return;
30457   if (dwarf_strict && dwarf_version < 5)
30458     return;
30459   /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
30460      and ADDR_EXPR refers to a decl that has DW_AT_location or
30461      DW_AT_const_value (but isn't addressable, otherwise
30462      resolving the original DW_OP_addr wouldn't fail), see if
30463      we can add DW_OP_implicit_pointer.  */
30464   STRIP_NOPS (init);
30465   if (TREE_CODE (init) == POINTER_PLUS_EXPR
30466       && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
30467     {
30468       offset = tree_to_shwi (TREE_OPERAND (init, 1));
30469       init = TREE_OPERAND (init, 0);
30470       STRIP_NOPS (init);
30471     }
30472   if (TREE_CODE (init) != ADDR_EXPR)
30473     return;
30474   if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
30475        && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
30476       || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
30477 	  && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
30478 	  && TREE_OPERAND (init, 0) != decl))
30479     {
30480       dw_die_ref ref;
30481       dw_loc_descr_ref l;
30482 
30483       if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
30484 	{
30485 	  rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
30486 	  if (!rtl)
30487 	    return;
30488 	  decl = SYMBOL_REF_DECL (rtl);
30489 	}
30490       else
30491 	decl = TREE_OPERAND (init, 0);
30492       ref = lookup_decl_die (decl);
30493       if (ref == NULL
30494 	  || (!get_AT (ref, DW_AT_location)
30495 	      && !get_AT (ref, DW_AT_const_value)))
30496 	return;
30497       l = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
30498       l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30499       l->dw_loc_oprnd1.v.val_die_ref.die = ref;
30500       l->dw_loc_oprnd1.v.val_die_ref.external = 0;
30501       add_AT_loc (die, DW_AT_location, l);
30502     }
30503 }
30504 
30505 /* Return NULL if l is a DWARF expression, or first op that is not
30506    valid DWARF expression.  */
30507 
30508 static dw_loc_descr_ref
non_dwarf_expression(dw_loc_descr_ref l)30509 non_dwarf_expression (dw_loc_descr_ref l)
30510 {
30511   while (l)
30512     {
30513       if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
30514 	return l;
30515       switch (l->dw_loc_opc)
30516 	{
30517 	case DW_OP_regx:
30518 	case DW_OP_implicit_value:
30519 	case DW_OP_stack_value:
30520 	case DW_OP_implicit_pointer:
30521 	case DW_OP_GNU_implicit_pointer:
30522 	case DW_OP_GNU_parameter_ref:
30523 	case DW_OP_piece:
30524 	case DW_OP_bit_piece:
30525 	  return l;
30526 	default:
30527 	  break;
30528 	}
30529       l = l->dw_loc_next;
30530     }
30531   return NULL;
30532 }
30533 
30534 /* Return adjusted copy of EXPR:
30535    If it is empty DWARF expression, return it.
30536    If it is valid non-empty DWARF expression,
30537    return copy of EXPR with DW_OP_deref appended to it.
30538    If it is DWARF expression followed by DW_OP_reg{N,x}, return
30539    copy of the DWARF expression with DW_OP_breg{N,x} <0> appended.
30540    If it is DWARF expression followed by DW_OP_stack_value, return
30541    copy of the DWARF expression without anything appended.
30542    Otherwise, return NULL.  */
30543 
30544 static dw_loc_descr_ref
copy_deref_exprloc(dw_loc_descr_ref expr)30545 copy_deref_exprloc (dw_loc_descr_ref expr)
30546 {
30547   dw_loc_descr_ref tail = NULL;
30548 
30549   if (expr == NULL)
30550     return NULL;
30551 
30552   dw_loc_descr_ref l = non_dwarf_expression (expr);
30553   if (l && l->dw_loc_next)
30554     return NULL;
30555 
30556   if (l)
30557     {
30558       if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
30559 	tail = new_loc_descr ((enum dwarf_location_atom)
30560 			      (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
30561 			      0, 0);
30562       else
30563 	switch (l->dw_loc_opc)
30564 	  {
30565 	  case DW_OP_regx:
30566 	    tail = new_loc_descr (DW_OP_bregx,
30567 				  l->dw_loc_oprnd1.v.val_unsigned, 0);
30568 	    break;
30569 	  case DW_OP_stack_value:
30570 	    break;
30571 	  default:
30572 	    return NULL;
30573 	  }
30574     }
30575   else
30576     tail = new_loc_descr (DW_OP_deref, 0, 0);
30577 
30578   dw_loc_descr_ref ret = NULL, *p = &ret;
30579   while (expr != l)
30580     {
30581       *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
30582       (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
30583       (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
30584       p = &(*p)->dw_loc_next;
30585       expr = expr->dw_loc_next;
30586     }
30587   *p = tail;
30588   return ret;
30589 }
30590 
30591 /* For DW_AT_string_length attribute with DW_OP_GNU_variable_value
30592    reference to a variable or argument, adjust it if needed and return:
30593    -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
30594       attribute if present should be removed
30595    0 keep the attribute perhaps with minor modifications, no need to rescan
30596    1 if the attribute has been successfully adjusted.  */
30597 
30598 static int
optimize_string_length(dw_attr_node * a)30599 optimize_string_length (dw_attr_node *a)
30600 {
30601   dw_loc_descr_ref l = AT_loc (a), lv;
30602   dw_die_ref die;
30603   if (l->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
30604     {
30605       tree decl = l->dw_loc_oprnd1.v.val_decl_ref;
30606       die = lookup_decl_die (decl);
30607       if (die)
30608 	{
30609 	  l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30610 	  l->dw_loc_oprnd1.v.val_die_ref.die = die;
30611 	  l->dw_loc_oprnd1.v.val_die_ref.external = 0;
30612 	}
30613       else
30614 	return -1;
30615     }
30616   else
30617     die = l->dw_loc_oprnd1.v.val_die_ref.die;
30618 
30619   /* DWARF5 allows reference class, so we can then reference the DIE.
30620      Only do this for DW_OP_GNU_variable_value DW_OP_stack_value.  */
30621   if (l->dw_loc_next != NULL && dwarf_version >= 5)
30622     {
30623       a->dw_attr_val.val_class = dw_val_class_die_ref;
30624       a->dw_attr_val.val_entry = NULL;
30625       a->dw_attr_val.v.val_die_ref.die = die;
30626       a->dw_attr_val.v.val_die_ref.external = 0;
30627       return 0;
30628     }
30629 
30630   dw_attr_node *av = get_AT (die, DW_AT_location);
30631   dw_loc_list_ref d;
30632   bool non_dwarf_expr = false;
30633 
30634   if (av == NULL)
30635     return dwarf_strict ? -1 : 0;
30636   switch (AT_class (av))
30637     {
30638     case dw_val_class_loc_list:
30639       for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
30640 	if (d->expr && non_dwarf_expression (d->expr))
30641 	  non_dwarf_expr = true;
30642       break;
30643     case dw_val_class_view_list:
30644       gcc_unreachable ();
30645     case dw_val_class_loc:
30646       lv = AT_loc (av);
30647       if (lv == NULL)
30648 	return dwarf_strict ? -1 : 0;
30649       if (non_dwarf_expression (lv))
30650 	non_dwarf_expr = true;
30651       break;
30652     default:
30653       return dwarf_strict ? -1 : 0;
30654     }
30655 
30656   /* If it is safe to transform DW_OP_GNU_variable_value DW_OP_stack_value
30657      into DW_OP_call4  or DW_OP_GNU_variable_value into
30658      DW_OP_call4 DW_OP_deref, do so.  */
30659   if (!non_dwarf_expr
30660       && (l->dw_loc_next != NULL || AT_class (av) == dw_val_class_loc))
30661     {
30662       l->dw_loc_opc = DW_OP_call4;
30663       if (l->dw_loc_next)
30664 	l->dw_loc_next = NULL;
30665       else
30666 	l->dw_loc_next = new_loc_descr (DW_OP_deref, 0, 0);
30667       return 0;
30668     }
30669 
30670   /* For DW_OP_GNU_variable_value DW_OP_stack_value, we can just
30671      copy over the DW_AT_location attribute from die to a.  */
30672   if (l->dw_loc_next != NULL)
30673     {
30674       a->dw_attr_val = av->dw_attr_val;
30675       return 1;
30676     }
30677 
30678   dw_loc_list_ref list, *p;
30679   switch (AT_class (av))
30680     {
30681     case dw_val_class_loc_list:
30682       p = &list;
30683       list = NULL;
30684       for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
30685 	{
30686 	  lv = copy_deref_exprloc (d->expr);
30687 	  if (lv)
30688 	    {
30689 	      *p = new_loc_list (lv, d->begin, d->vbegin, d->end, d->vend, d->section);
30690 	      p = &(*p)->dw_loc_next;
30691 	    }
30692 	  else if (!dwarf_strict && d->expr)
30693 	    return 0;
30694 	}
30695       if (list == NULL)
30696 	return dwarf_strict ? -1 : 0;
30697       a->dw_attr_val.val_class = dw_val_class_loc_list;
30698       gen_llsym (list);
30699       *AT_loc_list_ptr (a) = list;
30700       return 1;
30701     case dw_val_class_loc:
30702       lv = copy_deref_exprloc (AT_loc (av));
30703       if (lv == NULL)
30704 	return dwarf_strict ? -1 : 0;
30705       a->dw_attr_val.v.val_loc = lv;
30706       return 1;
30707     default:
30708       gcc_unreachable ();
30709     }
30710 }
30711 
30712 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
30713    an address in .rodata section if the string literal is emitted there,
30714    or remove the containing location list or replace DW_AT_const_value
30715    with DW_AT_location and empty location expression, if it isn't found
30716    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
30717    to something that has been emitted in the current CU.  */
30718 
30719 static void
resolve_addr(dw_die_ref die)30720 resolve_addr (dw_die_ref die)
30721 {
30722   dw_die_ref c;
30723   dw_attr_node *a;
30724   dw_loc_list_ref *curr, *start, loc;
30725   unsigned ix;
30726   bool remove_AT_byte_size = false;
30727 
30728   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30729     switch (AT_class (a))
30730       {
30731       case dw_val_class_loc_list:
30732 	start = curr = AT_loc_list_ptr (a);
30733 	loc = *curr;
30734 	gcc_assert (loc);
30735 	/* The same list can be referenced more than once.  See if we have
30736 	   already recorded the result from a previous pass.  */
30737 	if (loc->replaced)
30738 	  *curr = loc->dw_loc_next;
30739 	else if (!loc->resolved_addr)
30740 	  {
30741 	    /* As things stand, we do not expect or allow one die to
30742 	       reference a suffix of another die's location list chain.
30743 	       References must be identical or completely separate.
30744 	       There is therefore no need to cache the result of this
30745 	       pass on any list other than the first; doing so
30746 	       would lead to unnecessary writes.  */
30747 	    while (*curr)
30748 	      {
30749 		gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
30750 		if (!resolve_addr_in_expr (a, (*curr)->expr))
30751 		  {
30752 		    dw_loc_list_ref next = (*curr)->dw_loc_next;
30753                     dw_loc_descr_ref l = (*curr)->expr;
30754 
30755 		    if (next && (*curr)->ll_symbol)
30756 		      {
30757 			gcc_assert (!next->ll_symbol);
30758 			next->ll_symbol = (*curr)->ll_symbol;
30759 			next->vl_symbol = (*curr)->vl_symbol;
30760 		      }
30761                     if (dwarf_split_debug_info)
30762                       remove_loc_list_addr_table_entries (l);
30763 		    *curr = next;
30764 		  }
30765 		else
30766 		  {
30767 		    mark_base_types ((*curr)->expr);
30768 		    curr = &(*curr)->dw_loc_next;
30769 		  }
30770 	      }
30771 	    if (loc == *start)
30772 	      loc->resolved_addr = 1;
30773 	    else
30774 	      {
30775 		loc->replaced = 1;
30776 		loc->dw_loc_next = *start;
30777 	      }
30778 	  }
30779 	if (!*start)
30780 	  {
30781 	    remove_AT (die, a->dw_attr);
30782 	    ix--;
30783 	  }
30784 	break;
30785       case dw_val_class_view_list:
30786 	{
30787 	  gcc_checking_assert (a->dw_attr == DW_AT_GNU_locviews);
30788 	  gcc_checking_assert (dwarf2out_locviews_in_attribute ());
30789 	  dw_val_node *llnode
30790 	    = view_list_to_loc_list_val_node (&a->dw_attr_val);
30791 	  /* If we no longer have a loclist, or it no longer needs
30792 	     views, drop this attribute.  */
30793 	  if (!llnode || !llnode->v.val_loc_list->vl_symbol)
30794 	    {
30795 	      remove_AT (die, a->dw_attr);
30796 	      ix--;
30797 	    }
30798 	  break;
30799 	}
30800       case dw_val_class_loc:
30801 	{
30802 	  dw_loc_descr_ref l = AT_loc (a);
30803 	  /* DW_OP_GNU_variable_value DW_OP_stack_value or
30804 	     DW_OP_GNU_variable_value in DW_AT_string_length can be converted
30805 	     into DW_OP_call4 or DW_OP_call4 DW_OP_deref, which is standard
30806 	     DWARF4 unlike DW_OP_GNU_variable_value.  Or for DWARF5
30807 	     DW_OP_GNU_variable_value DW_OP_stack_value can be replaced
30808 	     with DW_FORM_ref referencing the same DIE as
30809 	     DW_OP_GNU_variable_value used to reference.  */
30810 	  if (a->dw_attr == DW_AT_string_length
30811 	      && l
30812 	      && l->dw_loc_opc == DW_OP_GNU_variable_value
30813 	      && (l->dw_loc_next == NULL
30814 		  || (l->dw_loc_next->dw_loc_next == NULL
30815 		      && l->dw_loc_next->dw_loc_opc == DW_OP_stack_value)))
30816 	    {
30817 	      switch (optimize_string_length (a))
30818 		{
30819 		case -1:
30820 		  remove_AT (die, a->dw_attr);
30821 		  ix--;
30822 		  /* If we drop DW_AT_string_length, we need to drop also
30823 		     DW_AT_{string_length_,}byte_size.  */
30824 		  remove_AT_byte_size = true;
30825 		  continue;
30826 		default:
30827 		  break;
30828 		case 1:
30829 		  /* Even if we keep the optimized DW_AT_string_length,
30830 		     it might have changed AT_class, so process it again.  */
30831 		  ix--;
30832 		  continue;
30833 		}
30834 	    }
30835 	  /* For -gdwarf-2 don't attempt to optimize
30836 	     DW_AT_data_member_location containing
30837 	     DW_OP_plus_uconst - older consumers might
30838 	     rely on it being that op instead of a more complex,
30839 	     but shorter, location description.  */
30840 	  if ((dwarf_version > 2
30841 	       || a->dw_attr != DW_AT_data_member_location
30842 	       || l == NULL
30843 	       || l->dw_loc_opc != DW_OP_plus_uconst
30844 	       || l->dw_loc_next != NULL)
30845 	      && !resolve_addr_in_expr (a, l))
30846 	    {
30847 	      if (dwarf_split_debug_info)
30848 		remove_loc_list_addr_table_entries (l);
30849 	      if (l != NULL
30850 		  && l->dw_loc_next == NULL
30851 		  && l->dw_loc_opc == DW_OP_addr
30852 		  && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
30853 		  && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
30854 		  && a->dw_attr == DW_AT_location)
30855 		{
30856 		  tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
30857 		  remove_AT (die, a->dw_attr);
30858 		  ix--;
30859 		  optimize_location_into_implicit_ptr (die, decl);
30860 		  break;
30861 		}
30862 	      if (a->dw_attr == DW_AT_string_length)
30863 		/* If we drop DW_AT_string_length, we need to drop also
30864 		   DW_AT_{string_length_,}byte_size.  */
30865 		remove_AT_byte_size = true;
30866 	      remove_AT (die, a->dw_attr);
30867 	      ix--;
30868 	    }
30869 	  else
30870 	    mark_base_types (l);
30871 	}
30872 	break;
30873       case dw_val_class_addr:
30874 	if (a->dw_attr == DW_AT_const_value
30875 	    && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
30876 	  {
30877             if (AT_index (a) != NOT_INDEXED)
30878               remove_addr_table_entry (a->dw_attr_val.val_entry);
30879 	    remove_AT (die, a->dw_attr);
30880 	    ix--;
30881 	  }
30882 	if ((die->die_tag == DW_TAG_call_site
30883 	     && a->dw_attr == DW_AT_call_origin)
30884 	    || (die->die_tag == DW_TAG_GNU_call_site
30885 		&& a->dw_attr == DW_AT_abstract_origin))
30886 	  {
30887 	    tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
30888 	    dw_die_ref tdie = lookup_decl_die (tdecl);
30889 	    dw_die_ref cdie;
30890 	    if (tdie == NULL
30891 		&& DECL_EXTERNAL (tdecl)
30892 		&& DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
30893 		&& (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
30894 	      {
30895 		dw_die_ref pdie = cdie;
30896 		/* Make sure we don't add these DIEs into type units.
30897 		   We could emit skeleton DIEs for context (namespaces,
30898 		   outer structs/classes) and a skeleton DIE for the
30899 		   innermost context with DW_AT_signature pointing to the
30900 		   type unit.  See PR78835.  */
30901 		while (pdie && pdie->die_tag != DW_TAG_type_unit)
30902 		  pdie = pdie->die_parent;
30903 		if (pdie == NULL)
30904 		  {
30905 		    /* Creating a full DIE for tdecl is overly expensive and
30906 		       at this point even wrong when in the LTO phase
30907 		       as it can end up generating new type DIEs we didn't
30908 		       output and thus optimize_external_refs will crash.  */
30909 		    tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
30910 		    add_AT_flag (tdie, DW_AT_external, 1);
30911 		    add_AT_flag (tdie, DW_AT_declaration, 1);
30912 		    add_linkage_attr (tdie, tdecl);
30913 		    add_name_and_src_coords_attributes (tdie, tdecl, true);
30914 		    equate_decl_number_to_die (tdecl, tdie);
30915 		  }
30916 	      }
30917 	    if (tdie)
30918 	      {
30919 		a->dw_attr_val.val_class = dw_val_class_die_ref;
30920 		a->dw_attr_val.v.val_die_ref.die = tdie;
30921 		a->dw_attr_val.v.val_die_ref.external = 0;
30922 	      }
30923 	    else
30924 	      {
30925                 if (AT_index (a) != NOT_INDEXED)
30926                   remove_addr_table_entry (a->dw_attr_val.val_entry);
30927 		remove_AT (die, a->dw_attr);
30928 		ix--;
30929 	      }
30930 	  }
30931 	break;
30932       default:
30933 	break;
30934       }
30935 
30936   if (remove_AT_byte_size)
30937     remove_AT (die, dwarf_version >= 5
30938 		    ? DW_AT_string_length_byte_size
30939 		    : DW_AT_byte_size);
30940 
30941   FOR_EACH_CHILD (die, c, resolve_addr (c));
30942 }
30943 
30944 /* Helper routines for optimize_location_lists.
30945    This pass tries to share identical local lists in .debug_loc
30946    section.  */
30947 
30948 /* Iteratively hash operands of LOC opcode into HSTATE.  */
30949 
30950 static void
hash_loc_operands(dw_loc_descr_ref loc,inchash::hash & hstate)30951 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
30952 {
30953   dw_val_ref val1 = &loc->dw_loc_oprnd1;
30954   dw_val_ref val2 = &loc->dw_loc_oprnd2;
30955 
30956   switch (loc->dw_loc_opc)
30957     {
30958     case DW_OP_const4u:
30959     case DW_OP_const8u:
30960       if (loc->dtprel)
30961 	goto hash_addr;
30962       /* FALLTHRU */
30963     case DW_OP_const1u:
30964     case DW_OP_const1s:
30965     case DW_OP_const2u:
30966     case DW_OP_const2s:
30967     case DW_OP_const4s:
30968     case DW_OP_const8s:
30969     case DW_OP_constu:
30970     case DW_OP_consts:
30971     case DW_OP_pick:
30972     case DW_OP_plus_uconst:
30973     case DW_OP_breg0:
30974     case DW_OP_breg1:
30975     case DW_OP_breg2:
30976     case DW_OP_breg3:
30977     case DW_OP_breg4:
30978     case DW_OP_breg5:
30979     case DW_OP_breg6:
30980     case DW_OP_breg7:
30981     case DW_OP_breg8:
30982     case DW_OP_breg9:
30983     case DW_OP_breg10:
30984     case DW_OP_breg11:
30985     case DW_OP_breg12:
30986     case DW_OP_breg13:
30987     case DW_OP_breg14:
30988     case DW_OP_breg15:
30989     case DW_OP_breg16:
30990     case DW_OP_breg17:
30991     case DW_OP_breg18:
30992     case DW_OP_breg19:
30993     case DW_OP_breg20:
30994     case DW_OP_breg21:
30995     case DW_OP_breg22:
30996     case DW_OP_breg23:
30997     case DW_OP_breg24:
30998     case DW_OP_breg25:
30999     case DW_OP_breg26:
31000     case DW_OP_breg27:
31001     case DW_OP_breg28:
31002     case DW_OP_breg29:
31003     case DW_OP_breg30:
31004     case DW_OP_breg31:
31005     case DW_OP_regx:
31006     case DW_OP_fbreg:
31007     case DW_OP_piece:
31008     case DW_OP_deref_size:
31009     case DW_OP_xderef_size:
31010       hstate.add_object (val1->v.val_int);
31011       break;
31012     case DW_OP_skip:
31013     case DW_OP_bra:
31014       {
31015 	int offset;
31016 
31017 	gcc_assert (val1->val_class == dw_val_class_loc);
31018 	offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
31019 	hstate.add_object (offset);
31020       }
31021       break;
31022     case DW_OP_implicit_value:
31023       hstate.add_object (val1->v.val_unsigned);
31024       switch (val2->val_class)
31025 	{
31026 	case dw_val_class_const:
31027 	  hstate.add_object (val2->v.val_int);
31028 	  break;
31029 	case dw_val_class_vec:
31030 	  {
31031 	    unsigned int elt_size = val2->v.val_vec.elt_size;
31032 	    unsigned int len = val2->v.val_vec.length;
31033 
31034 	    hstate.add_int (elt_size);
31035 	    hstate.add_int (len);
31036 	    hstate.add (val2->v.val_vec.array, len * elt_size);
31037 	  }
31038 	  break;
31039 	case dw_val_class_const_double:
31040 	  hstate.add_object (val2->v.val_double.low);
31041 	  hstate.add_object (val2->v.val_double.high);
31042 	  break;
31043 	case dw_val_class_wide_int:
31044 	  hstate.add (val2->v.val_wide->get_val (),
31045 		      get_full_len (*val2->v.val_wide)
31046 		      * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
31047 	  break;
31048 	case dw_val_class_addr:
31049 	  inchash::add_rtx (val2->v.val_addr, hstate);
31050 	  break;
31051 	default:
31052 	  gcc_unreachable ();
31053 	}
31054       break;
31055     case DW_OP_bregx:
31056     case DW_OP_bit_piece:
31057       hstate.add_object (val1->v.val_int);
31058       hstate.add_object (val2->v.val_int);
31059       break;
31060     case DW_OP_addr:
31061     hash_addr:
31062       if (loc->dtprel)
31063 	{
31064 	  unsigned char dtprel = 0xd1;
31065 	  hstate.add_object (dtprel);
31066 	}
31067       inchash::add_rtx (val1->v.val_addr, hstate);
31068       break;
31069     case DW_OP_GNU_addr_index:
31070     case DW_OP_addrx:
31071     case DW_OP_GNU_const_index:
31072     case DW_OP_constx:
31073       {
31074         if (loc->dtprel)
31075           {
31076             unsigned char dtprel = 0xd1;
31077 	    hstate.add_object (dtprel);
31078           }
31079         inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
31080       }
31081       break;
31082     case DW_OP_implicit_pointer:
31083     case DW_OP_GNU_implicit_pointer:
31084       hstate.add_int (val2->v.val_int);
31085       break;
31086     case DW_OP_entry_value:
31087     case DW_OP_GNU_entry_value:
31088       hstate.add_object (val1->v.val_loc);
31089       break;
31090     case DW_OP_regval_type:
31091     case DW_OP_deref_type:
31092     case DW_OP_GNU_regval_type:
31093     case DW_OP_GNU_deref_type:
31094       {
31095 	unsigned int byte_size
31096 	  = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
31097 	unsigned int encoding
31098 	  = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
31099 	hstate.add_object (val1->v.val_int);
31100 	hstate.add_object (byte_size);
31101 	hstate.add_object (encoding);
31102       }
31103       break;
31104     case DW_OP_convert:
31105     case DW_OP_reinterpret:
31106     case DW_OP_GNU_convert:
31107     case DW_OP_GNU_reinterpret:
31108       if (val1->val_class == dw_val_class_unsigned_const)
31109 	{
31110 	  hstate.add_object (val1->v.val_unsigned);
31111 	  break;
31112 	}
31113       /* FALLTHRU */
31114     case DW_OP_const_type:
31115     case DW_OP_GNU_const_type:
31116       {
31117 	unsigned int byte_size
31118 	  = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
31119 	unsigned int encoding
31120 	  = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
31121 	hstate.add_object (byte_size);
31122 	hstate.add_object (encoding);
31123 	if (loc->dw_loc_opc != DW_OP_const_type
31124 	    && loc->dw_loc_opc != DW_OP_GNU_const_type)
31125 	  break;
31126 	hstate.add_object (val2->val_class);
31127 	switch (val2->val_class)
31128 	  {
31129 	  case dw_val_class_const:
31130 	    hstate.add_object (val2->v.val_int);
31131 	    break;
31132 	  case dw_val_class_vec:
31133 	    {
31134 	      unsigned int elt_size = val2->v.val_vec.elt_size;
31135 	      unsigned int len = val2->v.val_vec.length;
31136 
31137 	      hstate.add_object (elt_size);
31138 	      hstate.add_object (len);
31139 	      hstate.add (val2->v.val_vec.array, len * elt_size);
31140 	    }
31141 	    break;
31142 	  case dw_val_class_const_double:
31143 	    hstate.add_object (val2->v.val_double.low);
31144 	    hstate.add_object (val2->v.val_double.high);
31145 	    break;
31146 	  case dw_val_class_wide_int:
31147 	    hstate.add (val2->v.val_wide->get_val (),
31148 			get_full_len (*val2->v.val_wide)
31149 			* HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
31150 	    break;
31151 	  default:
31152 	    gcc_unreachable ();
31153 	  }
31154       }
31155       break;
31156 
31157     default:
31158       /* Other codes have no operands.  */
31159       break;
31160     }
31161 }
31162 
31163 /* Iteratively hash the whole DWARF location expression LOC into HSTATE.  */
31164 
31165 static inline void
hash_locs(dw_loc_descr_ref loc,inchash::hash & hstate)31166 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
31167 {
31168   dw_loc_descr_ref l;
31169   bool sizes_computed = false;
31170   /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed.  */
31171   size_of_locs (loc);
31172 
31173   for (l = loc; l != NULL; l = l->dw_loc_next)
31174     {
31175       enum dwarf_location_atom opc = l->dw_loc_opc;
31176       hstate.add_object (opc);
31177       if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
31178 	{
31179 	  size_of_locs (loc);
31180 	  sizes_computed = true;
31181 	}
31182       hash_loc_operands (l, hstate);
31183     }
31184 }
31185 
31186 /* Compute hash of the whole location list LIST_HEAD.  */
31187 
31188 static inline void
hash_loc_list(dw_loc_list_ref list_head)31189 hash_loc_list (dw_loc_list_ref list_head)
31190 {
31191   dw_loc_list_ref curr = list_head;
31192   inchash::hash hstate;
31193 
31194   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
31195     {
31196       hstate.add (curr->begin, strlen (curr->begin) + 1);
31197       hstate.add (curr->end, strlen (curr->end) + 1);
31198       hstate.add_object (curr->vbegin);
31199       hstate.add_object (curr->vend);
31200       if (curr->section)
31201 	hstate.add (curr->section, strlen (curr->section) + 1);
31202       hash_locs (curr->expr, hstate);
31203     }
31204   list_head->hash = hstate.end ();
31205 }
31206 
31207 /* Return true if X and Y opcodes have the same operands.  */
31208 
31209 static inline bool
compare_loc_operands(dw_loc_descr_ref x,dw_loc_descr_ref y)31210 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
31211 {
31212   dw_val_ref valx1 = &x->dw_loc_oprnd1;
31213   dw_val_ref valx2 = &x->dw_loc_oprnd2;
31214   dw_val_ref valy1 = &y->dw_loc_oprnd1;
31215   dw_val_ref valy2 = &y->dw_loc_oprnd2;
31216 
31217   switch (x->dw_loc_opc)
31218     {
31219     case DW_OP_const4u:
31220     case DW_OP_const8u:
31221       if (x->dtprel)
31222 	goto hash_addr;
31223       /* FALLTHRU */
31224     case DW_OP_const1u:
31225     case DW_OP_const1s:
31226     case DW_OP_const2u:
31227     case DW_OP_const2s:
31228     case DW_OP_const4s:
31229     case DW_OP_const8s:
31230     case DW_OP_constu:
31231     case DW_OP_consts:
31232     case DW_OP_pick:
31233     case DW_OP_plus_uconst:
31234     case DW_OP_breg0:
31235     case DW_OP_breg1:
31236     case DW_OP_breg2:
31237     case DW_OP_breg3:
31238     case DW_OP_breg4:
31239     case DW_OP_breg5:
31240     case DW_OP_breg6:
31241     case DW_OP_breg7:
31242     case DW_OP_breg8:
31243     case DW_OP_breg9:
31244     case DW_OP_breg10:
31245     case DW_OP_breg11:
31246     case DW_OP_breg12:
31247     case DW_OP_breg13:
31248     case DW_OP_breg14:
31249     case DW_OP_breg15:
31250     case DW_OP_breg16:
31251     case DW_OP_breg17:
31252     case DW_OP_breg18:
31253     case DW_OP_breg19:
31254     case DW_OP_breg20:
31255     case DW_OP_breg21:
31256     case DW_OP_breg22:
31257     case DW_OP_breg23:
31258     case DW_OP_breg24:
31259     case DW_OP_breg25:
31260     case DW_OP_breg26:
31261     case DW_OP_breg27:
31262     case DW_OP_breg28:
31263     case DW_OP_breg29:
31264     case DW_OP_breg30:
31265     case DW_OP_breg31:
31266     case DW_OP_regx:
31267     case DW_OP_fbreg:
31268     case DW_OP_piece:
31269     case DW_OP_deref_size:
31270     case DW_OP_xderef_size:
31271       return valx1->v.val_int == valy1->v.val_int;
31272     case DW_OP_skip:
31273     case DW_OP_bra:
31274       /* If splitting debug info, the use of DW_OP_GNU_addr_index
31275         can cause irrelevant differences in dw_loc_addr.  */
31276       gcc_assert (valx1->val_class == dw_val_class_loc
31277 		  && valy1->val_class == dw_val_class_loc
31278                   && (dwarf_split_debug_info
31279                       || x->dw_loc_addr == y->dw_loc_addr));
31280       return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
31281     case DW_OP_implicit_value:
31282       if (valx1->v.val_unsigned != valy1->v.val_unsigned
31283 	  || valx2->val_class != valy2->val_class)
31284 	return false;
31285       switch (valx2->val_class)
31286 	{
31287 	case dw_val_class_const:
31288 	  return valx2->v.val_int == valy2->v.val_int;
31289 	case dw_val_class_vec:
31290 	  return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
31291 		 && valx2->v.val_vec.length == valy2->v.val_vec.length
31292 		 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
31293 			    valx2->v.val_vec.elt_size
31294 			    * valx2->v.val_vec.length) == 0;
31295 	case dw_val_class_const_double:
31296 	  return valx2->v.val_double.low == valy2->v.val_double.low
31297 		 && valx2->v.val_double.high == valy2->v.val_double.high;
31298 	case dw_val_class_wide_int:
31299 	  return *valx2->v.val_wide == *valy2->v.val_wide;
31300 	case dw_val_class_addr:
31301 	  return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
31302 	default:
31303 	  gcc_unreachable ();
31304 	}
31305     case DW_OP_bregx:
31306     case DW_OP_bit_piece:
31307       return valx1->v.val_int == valy1->v.val_int
31308 	     && valx2->v.val_int == valy2->v.val_int;
31309     case DW_OP_addr:
31310     hash_addr:
31311       return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
31312     case DW_OP_GNU_addr_index:
31313     case DW_OP_addrx:
31314     case DW_OP_GNU_const_index:
31315     case DW_OP_constx:
31316       {
31317         rtx ax1 = valx1->val_entry->addr.rtl;
31318         rtx ay1 = valy1->val_entry->addr.rtl;
31319         return rtx_equal_p (ax1, ay1);
31320       }
31321     case DW_OP_implicit_pointer:
31322     case DW_OP_GNU_implicit_pointer:
31323       return valx1->val_class == dw_val_class_die_ref
31324 	     && valx1->val_class == valy1->val_class
31325 	     && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
31326 	     && valx2->v.val_int == valy2->v.val_int;
31327     case DW_OP_entry_value:
31328     case DW_OP_GNU_entry_value:
31329       return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
31330     case DW_OP_const_type:
31331     case DW_OP_GNU_const_type:
31332       if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
31333 	  || valx2->val_class != valy2->val_class)
31334 	return false;
31335       switch (valx2->val_class)
31336 	{
31337 	case dw_val_class_const:
31338 	  return valx2->v.val_int == valy2->v.val_int;
31339 	case dw_val_class_vec:
31340 	  return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
31341 		 && valx2->v.val_vec.length == valy2->v.val_vec.length
31342 		 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
31343 			    valx2->v.val_vec.elt_size
31344 			    * valx2->v.val_vec.length) == 0;
31345 	case dw_val_class_const_double:
31346 	  return valx2->v.val_double.low == valy2->v.val_double.low
31347 		 && valx2->v.val_double.high == valy2->v.val_double.high;
31348 	case dw_val_class_wide_int:
31349 	  return *valx2->v.val_wide == *valy2->v.val_wide;
31350 	default:
31351 	  gcc_unreachable ();
31352 	}
31353     case DW_OP_regval_type:
31354     case DW_OP_deref_type:
31355     case DW_OP_GNU_regval_type:
31356     case DW_OP_GNU_deref_type:
31357       return valx1->v.val_int == valy1->v.val_int
31358 	     && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
31359     case DW_OP_convert:
31360     case DW_OP_reinterpret:
31361     case DW_OP_GNU_convert:
31362     case DW_OP_GNU_reinterpret:
31363       if (valx1->val_class != valy1->val_class)
31364 	return false;
31365       if (valx1->val_class == dw_val_class_unsigned_const)
31366 	return valx1->v.val_unsigned == valy1->v.val_unsigned;
31367       return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
31368     case DW_OP_GNU_parameter_ref:
31369       return valx1->val_class == dw_val_class_die_ref
31370 	     && valx1->val_class == valy1->val_class
31371 	     && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
31372     default:
31373       /* Other codes have no operands.  */
31374       return true;
31375     }
31376 }
31377 
31378 /* Return true if DWARF location expressions X and Y are the same.  */
31379 
31380 static inline bool
compare_locs(dw_loc_descr_ref x,dw_loc_descr_ref y)31381 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
31382 {
31383   for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
31384     if (x->dw_loc_opc != y->dw_loc_opc
31385 	|| x->dtprel != y->dtprel
31386 	|| !compare_loc_operands (x, y))
31387       break;
31388   return x == NULL && y == NULL;
31389 }
31390 
31391 /* Hashtable helpers.  */
31392 
31393 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
31394 {
31395   static inline hashval_t hash (const dw_loc_list_struct *);
31396   static inline bool equal (const dw_loc_list_struct *,
31397 			    const dw_loc_list_struct *);
31398 };
31399 
31400 /* Return precomputed hash of location list X.  */
31401 
31402 inline hashval_t
hash(const dw_loc_list_struct * x)31403 loc_list_hasher::hash (const dw_loc_list_struct *x)
31404 {
31405   return x->hash;
31406 }
31407 
31408 /* Return true if location lists A and B are the same.  */
31409 
31410 inline bool
equal(const dw_loc_list_struct * a,const dw_loc_list_struct * b)31411 loc_list_hasher::equal (const dw_loc_list_struct *a,
31412 			const dw_loc_list_struct *b)
31413 {
31414   if (a == b)
31415     return 1;
31416   if (a->hash != b->hash)
31417     return 0;
31418   for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
31419     if (strcmp (a->begin, b->begin) != 0
31420 	|| strcmp (a->end, b->end) != 0
31421 	|| (a->section == NULL) != (b->section == NULL)
31422 	|| (a->section && strcmp (a->section, b->section) != 0)
31423 	|| a->vbegin != b->vbegin || a->vend != b->vend
31424 	|| !compare_locs (a->expr, b->expr))
31425       break;
31426   return a == NULL && b == NULL;
31427 }
31428 
31429 typedef hash_table<loc_list_hasher> loc_list_hash_type;
31430 
31431 
31432 /* Recursively optimize location lists referenced from DIE
31433    children and share them whenever possible.  */
31434 
31435 static void
optimize_location_lists_1(dw_die_ref die,loc_list_hash_type * htab)31436 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
31437 {
31438   dw_die_ref c;
31439   dw_attr_node *a;
31440   unsigned ix;
31441   dw_loc_list_struct **slot;
31442   bool drop_locviews = false;
31443   bool has_locviews = false;
31444 
31445   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
31446     if (AT_class (a) == dw_val_class_loc_list)
31447       {
31448 	dw_loc_list_ref list = AT_loc_list (a);
31449 	/* TODO: perform some optimizations here, before hashing
31450 	   it and storing into the hash table.  */
31451 	hash_loc_list (list);
31452 	slot = htab->find_slot_with_hash (list, list->hash, INSERT);
31453 	if (*slot == NULL)
31454 	  {
31455 	    *slot = list;
31456 	    if (loc_list_has_views (list))
31457 	      gcc_assert (list->vl_symbol);
31458 	    else if (list->vl_symbol)
31459 	      {
31460 		drop_locviews = true;
31461 		list->vl_symbol = NULL;
31462 	      }
31463 	  }
31464 	else
31465 	  {
31466 	    if (list->vl_symbol && !(*slot)->vl_symbol)
31467 	      drop_locviews = true;
31468 	    a->dw_attr_val.v.val_loc_list = *slot;
31469 	  }
31470       }
31471     else if (AT_class (a) == dw_val_class_view_list)
31472       {
31473 	gcc_checking_assert (a->dw_attr == DW_AT_GNU_locviews);
31474 	has_locviews = true;
31475       }
31476 
31477 
31478   if (drop_locviews && has_locviews)
31479     remove_AT (die, DW_AT_GNU_locviews);
31480 
31481   FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
31482 }
31483 
31484 
31485 /* Recursively assign each location list a unique index into the debug_addr
31486    section.  */
31487 
31488 static void
index_location_lists(dw_die_ref die)31489 index_location_lists (dw_die_ref die)
31490 {
31491   dw_die_ref c;
31492   dw_attr_node *a;
31493   unsigned ix;
31494 
31495   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
31496     if (AT_class (a) == dw_val_class_loc_list)
31497       {
31498         dw_loc_list_ref list = AT_loc_list (a);
31499         dw_loc_list_ref curr;
31500         for (curr = list; curr != NULL; curr = curr->dw_loc_next)
31501           {
31502             /* Don't index an entry that has already been indexed
31503 	       or won't be output.  Make sure skip_loc_list_entry doesn't
31504 	       call size_of_locs, because that might cause circular dependency,
31505 	       index_location_lists requiring address table indexes to be
31506 	       computed, but adding new indexes through add_addr_table_entry
31507 	       and address table index computation requiring no new additions
31508 	       to the hash table.  In the rare case of DWARF[234] >= 64KB
31509 	       location expression, we'll just waste unused address table entry
31510 	       for it.  */
31511 	    if (curr->begin_entry != NULL || skip_loc_list_entry (curr))
31512               continue;
31513 
31514             curr->begin_entry
31515 	      = add_addr_table_entry (xstrdup (curr->begin), ate_kind_label);
31516 	    if (dwarf_version >= 5 && !HAVE_AS_LEB128)
31517 	      curr->end_entry
31518 		= add_addr_table_entry (xstrdup (curr->end), ate_kind_label);
31519           }
31520       }
31521 
31522   FOR_EACH_CHILD (die, c, index_location_lists (c));
31523 }
31524 
31525 /* Optimize location lists referenced from DIE
31526    children and share them whenever possible.  */
31527 
31528 static void
optimize_location_lists(dw_die_ref die)31529 optimize_location_lists (dw_die_ref die)
31530 {
31531   loc_list_hash_type htab (500);
31532   optimize_location_lists_1 (die, &htab);
31533 }
31534 
31535 /* Traverse the limbo die list, and add parent/child links.  The only
31536    dies without parents that should be here are concrete instances of
31537    inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
31538    For concrete instances, we can get the parent die from the abstract
31539    instance.  */
31540 
31541 static void
flush_limbo_die_list(void)31542 flush_limbo_die_list (void)
31543 {
31544   limbo_die_node *node;
31545 
31546   /* get_context_die calls force_decl_die, which can put new DIEs on the
31547      limbo list in LTO mode when nested functions are put in a different
31548      partition than that of their parent function.  */
31549   while ((node = limbo_die_list))
31550     {
31551       dw_die_ref die = node->die;
31552       limbo_die_list = node->next;
31553 
31554       if (die->die_parent == NULL)
31555 	{
31556 	  dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
31557 
31558 	  if (origin && origin->die_parent)
31559 	    add_child_die (origin->die_parent, die);
31560 	  else if (is_cu_die (die))
31561 	    ;
31562 	  else if (seen_error ())
31563 	    /* It's OK to be confused by errors in the input.  */
31564 	    add_child_die (comp_unit_die (), die);
31565 	  else
31566 	    {
31567 	      /* In certain situations, the lexical block containing a
31568 		 nested function can be optimized away, which results
31569 		 in the nested function die being orphaned.  Likewise
31570 		 with the return type of that nested function.  Force
31571 		 this to be a child of the containing function.
31572 
31573 		 It may happen that even the containing function got fully
31574 		 inlined and optimized out.  In that case we are lost and
31575 		 assign the empty child.  This should not be big issue as
31576 		 the function is likely unreachable too.  */
31577 	      gcc_assert (node->created_for);
31578 
31579 	      if (DECL_P (node->created_for))
31580 		origin = get_context_die (DECL_CONTEXT (node->created_for));
31581 	      else if (TYPE_P (node->created_for))
31582 		origin = scope_die_for (node->created_for, comp_unit_die ());
31583 	      else
31584 		origin = comp_unit_die ();
31585 
31586 	      add_child_die (origin, die);
31587 	    }
31588 	}
31589     }
31590 }
31591 
31592 /* Reset DIEs so we can output them again.  */
31593 
31594 static void
reset_dies(dw_die_ref die)31595 reset_dies (dw_die_ref die)
31596 {
31597   dw_die_ref c;
31598 
31599   /* Remove stuff we re-generate.  */
31600   die->die_mark = 0;
31601   die->die_offset = 0;
31602   die->die_abbrev = 0;
31603   remove_AT (die, DW_AT_sibling);
31604 
31605   FOR_EACH_CHILD (die, c, reset_dies (c));
31606 }
31607 
31608 /* reset_indirect_string removed the references coming from DW_AT_name
31609    and DW_AT_comp_dir attributes on compilation unit DIEs.  Readd them as
31610    .debug_line_str strings again.  */
31611 
31612 static void
adjust_name_comp_dir(dw_die_ref die)31613 adjust_name_comp_dir (dw_die_ref die)
31614 {
31615   for (int i = 0; i < 2; i++)
31616     {
31617       dwarf_attribute attr_kind = i ? DW_AT_comp_dir : DW_AT_name;
31618       dw_attr_node *a = get_AT (die, attr_kind);
31619       if (a == NULL || a->dw_attr_val.val_class != dw_val_class_str)
31620 	continue;
31621 
31622       if (!debug_line_str_hash)
31623 	debug_line_str_hash
31624 	  = hash_table<indirect_string_hasher>::create_ggc (10);
31625 
31626       struct indirect_string_node *node
31627 	= find_AT_string_in_table (a->dw_attr_val.v.val_str->str,
31628 				   debug_line_str_hash);
31629       set_indirect_string (node);
31630       node->form = DW_FORM_line_strp;
31631       a->dw_attr_val.v.val_str = node;
31632     }
31633 }
31634 
31635 /* Output stuff that dwarf requires at the end of every file,
31636    and generate the DWARF-2 debugging info.  */
31637 
31638 static void
dwarf2out_finish(const char * filename)31639 dwarf2out_finish (const char *filename)
31640 {
31641   comdat_type_node *ctnode;
31642   dw_die_ref main_comp_unit_die;
31643   unsigned char checksum[16];
31644   char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
31645 
31646   /* Flush out any latecomers to the limbo party.  */
31647   flush_limbo_die_list ();
31648 
31649   if (inline_entry_data_table)
31650     gcc_assert (inline_entry_data_table->is_empty ());
31651 
31652   if (flag_checking)
31653     {
31654       verify_die (comp_unit_die ());
31655       for (limbo_die_node *node = cu_die_list; node; node = node->next)
31656 	verify_die (node->die);
31657     }
31658 
31659   /* We shouldn't have any symbols with delayed asm names for
31660      DIEs generated after early finish.  */
31661   gcc_assert (deferred_asm_name == NULL);
31662 
31663   gen_remaining_tmpl_value_param_die_attribute ();
31664 
31665   if (flag_generate_lto || flag_generate_offload)
31666     {
31667       gcc_assert (flag_fat_lto_objects || flag_generate_offload);
31668 
31669       /* Prune stuff so that dwarf2out_finish runs successfully
31670 	 for the fat part of the object.  */
31671       reset_dies (comp_unit_die ());
31672       for (limbo_die_node *node = cu_die_list; node; node = node->next)
31673 	reset_dies (node->die);
31674 
31675       hash_table<comdat_type_hasher> comdat_type_table (100);
31676       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
31677 	{
31678 	  comdat_type_node **slot
31679 	      = comdat_type_table.find_slot (ctnode, INSERT);
31680 
31681 	  /* Don't reset types twice.  */
31682 	  if (*slot != HTAB_EMPTY_ENTRY)
31683 	    continue;
31684 
31685 	  /* Remove the pointer to the line table.  */
31686 	  remove_AT (ctnode->root_die, DW_AT_stmt_list);
31687 
31688 	  if (debug_info_level >= DINFO_LEVEL_TERSE)
31689 	    reset_dies (ctnode->root_die);
31690 
31691 	  *slot = ctnode;
31692 	}
31693 
31694       /* Reset die CU symbol so we don't output it twice.  */
31695       comp_unit_die ()->die_id.die_symbol = NULL;
31696 
31697       /* Remove DW_AT_macro and DW_AT_stmt_list from the early output.  */
31698       remove_AT (comp_unit_die (), DW_AT_stmt_list);
31699       if (have_macinfo)
31700 	remove_AT (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE);
31701 
31702       /* Remove indirect string decisions.  */
31703       debug_str_hash->traverse<void *, reset_indirect_string> (NULL);
31704       if (debug_line_str_hash)
31705 	{
31706 	  debug_line_str_hash->traverse<void *, reset_indirect_string> (NULL);
31707 	  debug_line_str_hash = NULL;
31708 	  if (asm_outputs_debug_line_str ())
31709 	    {
31710 	      adjust_name_comp_dir (comp_unit_die ());
31711 	      for (limbo_die_node *node = cu_die_list; node; node = node->next)
31712 		adjust_name_comp_dir (node->die);
31713 	    }
31714 	}
31715     }
31716 
31717 #if ENABLE_ASSERT_CHECKING
31718   {
31719     dw_die_ref die = comp_unit_die (), c;
31720     FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
31721   }
31722 #endif
31723   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
31724     resolve_addr (ctnode->root_die);
31725   resolve_addr (comp_unit_die ());
31726   move_marked_base_types ();
31727 
31728   if (dump_file)
31729     {
31730       fprintf (dump_file, "DWARF for %s\n", filename);
31731       print_die (comp_unit_die (), dump_file);
31732     }
31733 
31734   /* Initialize sections and labels used for actual assembler output.  */
31735   unsigned generation = init_sections_and_labels (false);
31736 
31737   /* Traverse the DIE's and add sibling attributes to those DIE's that
31738      have children.  */
31739   add_sibling_attributes (comp_unit_die ());
31740   limbo_die_node *node;
31741   for (node = cu_die_list; node; node = node->next)
31742     add_sibling_attributes (node->die);
31743   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
31744     add_sibling_attributes (ctnode->root_die);
31745 
31746   /* When splitting DWARF info, we put some attributes in the
31747      skeleton compile_unit DIE that remains in the .o, while
31748      most attributes go in the DWO compile_unit_die.  */
31749   if (dwarf_split_debug_info)
31750     {
31751       limbo_die_node *cu;
31752       main_comp_unit_die = gen_compile_unit_die (NULL);
31753       if (dwarf_version >= 5)
31754 	main_comp_unit_die->die_tag = DW_TAG_skeleton_unit;
31755       cu = limbo_die_list;
31756       gcc_assert (cu->die == main_comp_unit_die);
31757       limbo_die_list = limbo_die_list->next;
31758       cu->next = cu_die_list;
31759       cu_die_list = cu;
31760     }
31761   else
31762     main_comp_unit_die = comp_unit_die ();
31763 
31764   /* Output a terminator label for the .text section.  */
31765   switch_to_section (text_section);
31766   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
31767   if (cold_text_section)
31768     {
31769       switch_to_section (cold_text_section);
31770       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
31771     }
31772 
31773   /* We can only use the low/high_pc attributes if all of the code was
31774      in .text.  */
31775   if (!have_multiple_function_sections
31776       || (dwarf_version < 3 && dwarf_strict))
31777     {
31778       /* Don't add if the CU has no associated code.  */
31779       if (text_section_used)
31780         add_AT_low_high_pc (main_comp_unit_die, text_section_label,
31781                             text_end_label, true);
31782     }
31783   else
31784     {
31785       unsigned fde_idx;
31786       dw_fde_ref fde;
31787       bool range_list_added = false;
31788 
31789       if (text_section_used)
31790         add_ranges_by_labels (main_comp_unit_die, text_section_label,
31791                               text_end_label, &range_list_added, true);
31792       if (cold_text_section_used)
31793         add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
31794                               cold_end_label, &range_list_added, true);
31795 
31796       FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
31797 	{
31798 	  if (DECL_IGNORED_P (fde->decl))
31799 	    continue;
31800 	  if (!fde->in_std_section)
31801             add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
31802                                   fde->dw_fde_end, &range_list_added,
31803                                   true);
31804 	  if (fde->dw_fde_second_begin && !fde->second_in_std_section)
31805             add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
31806                                   fde->dw_fde_second_end, &range_list_added,
31807                                   true);
31808 	}
31809 
31810       if (range_list_added)
31811 	{
31812 	  /* We need to give .debug_loc and .debug_ranges an appropriate
31813 	     "base address".  Use zero so that these addresses become
31814 	     absolute.  Historically, we've emitted the unexpected
31815 	     DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
31816 	     Emit both to give time for other tools to adapt.  */
31817           add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
31818 	  if (! dwarf_strict && dwarf_version < 4)
31819             add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
31820 
31821 	  add_ranges (NULL);
31822 	}
31823     }
31824 
31825   /* AIX Assembler inserts the length, so adjust the reference to match the
31826      offset expected by debuggers.  */
31827   strcpy (dl_section_ref, debug_line_section_label);
31828   if (XCOFF_DEBUGGING_INFO)
31829     strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
31830 
31831   if (debug_info_level >= DINFO_LEVEL_TERSE)
31832     add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
31833 		    dl_section_ref);
31834 
31835   if (have_macinfo)
31836     add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
31837 		   macinfo_section_label);
31838 
31839   if (dwarf_split_debug_info)
31840     {
31841       if (have_location_lists)
31842 	{
31843 	  /* Since we generate the loclists in the split DWARF .dwo
31844 	     file itself, we don't need to generate a loclists_base
31845 	     attribute for the split compile unit DIE.  That attribute
31846 	     (and using relocatable sec_offset FORMs) isn't allowed
31847 	     for a split compile unit.  Only if the .debug_loclists
31848 	     section was in the main file, would we need to generate a
31849 	     loclists_base attribute here (for the full or skeleton
31850 	     unit DIE).  */
31851 
31852 	  /* optimize_location_lists calculates the size of the lists,
31853 	     so index them first, and assign indices to the entries.
31854 	     Although optimize_location_lists will remove entries from
31855 	     the table, it only does so for duplicates, and therefore
31856 	     only reduces ref_counts to 1.  */
31857 	  index_location_lists (comp_unit_die ());
31858 	}
31859 
31860       if (dwarf_version >= 5 && !vec_safe_is_empty (ranges_table))
31861 	index_rnglists ();
31862 
31863       if (addr_index_table != NULL)
31864         {
31865           unsigned int index = 0;
31866           addr_index_table
31867 	    ->traverse_noresize<unsigned int *, index_addr_table_entry>
31868 	    (&index);
31869         }
31870     }
31871 
31872   loc_list_idx = 0;
31873   if (have_location_lists)
31874     {
31875       optimize_location_lists (comp_unit_die ());
31876       /* And finally assign indexes to the entries for -gsplit-dwarf.  */
31877       if (dwarf_version >= 5 && dwarf_split_debug_info)
31878 	assign_location_list_indexes (comp_unit_die ());
31879     }
31880 
31881   save_macinfo_strings ();
31882 
31883   if (dwarf_split_debug_info)
31884     {
31885       unsigned int index = 0;
31886 
31887       /* Add attributes common to skeleton compile_units and
31888          type_units.  Because these attributes include strings, it
31889          must be done before freezing the string table.  Top-level
31890          skeleton die attrs are added when the skeleton type unit is
31891          created, so ensure it is created by this point.  */
31892       add_top_level_skeleton_die_attrs (main_comp_unit_die);
31893       debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
31894     }
31895 
31896   /* Output all of the compilation units.  We put the main one last so that
31897      the offsets are available to output_pubnames.  */
31898   for (node = cu_die_list; node; node = node->next)
31899     output_comp_unit (node->die, 0, NULL);
31900 
31901   hash_table<comdat_type_hasher> comdat_type_table (100);
31902   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
31903     {
31904       comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
31905 
31906       /* Don't output duplicate types.  */
31907       if (*slot != HTAB_EMPTY_ENTRY)
31908         continue;
31909 
31910       /* Add a pointer to the line table for the main compilation unit
31911          so that the debugger can make sense of DW_AT_decl_file
31912          attributes.  */
31913       if (debug_info_level >= DINFO_LEVEL_TERSE)
31914         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
31915                         (!dwarf_split_debug_info
31916                          ? dl_section_ref
31917                          : debug_skeleton_line_section_label));
31918 
31919       output_comdat_type_unit (ctnode, false);
31920       *slot = ctnode;
31921     }
31922 
31923   if (dwarf_split_debug_info)
31924     {
31925       int mark;
31926       struct md5_ctx ctx;
31927 
31928       /* Compute a checksum of the comp_unit to use as the dwo_id.  */
31929       md5_init_ctx (&ctx);
31930       mark = 0;
31931       die_checksum (comp_unit_die (), &ctx, &mark);
31932       unmark_all_dies (comp_unit_die ());
31933       md5_finish_ctx (&ctx, checksum);
31934 
31935       if (dwarf_version < 5)
31936 	{
31937 	  /* Use the first 8 bytes of the checksum as the dwo_id,
31938 	     and add it to both comp-unit DIEs.  */
31939 	  add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
31940 	  add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
31941 	}
31942 
31943       /* Add the base offset of the ranges table to the skeleton
31944         comp-unit DIE.  */
31945       if (!vec_safe_is_empty (ranges_table))
31946 	{
31947 	  if (dwarf_version < 5)
31948 	    add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
31949 			    ranges_section_label);
31950 	}
31951 
31952       output_addr_table ();
31953     }
31954 
31955   /* Output the main compilation unit if non-empty or if .debug_macinfo
31956      or .debug_macro will be emitted.  */
31957   output_comp_unit (comp_unit_die (), have_macinfo,
31958 		    dwarf_split_debug_info ? checksum : NULL);
31959 
31960   if (dwarf_split_debug_info && info_section_emitted)
31961     output_skeleton_debug_sections (main_comp_unit_die, checksum);
31962 
31963   /* Output the abbreviation table.  */
31964   if (vec_safe_length (abbrev_die_table) != 1)
31965     {
31966       switch_to_section (debug_abbrev_section);
31967       ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
31968       output_abbrev_section ();
31969     }
31970 
31971   /* Output location list section if necessary.  */
31972   if (have_location_lists)
31973     {
31974       char l1[MAX_ARTIFICIAL_LABEL_BYTES];
31975       char l2[MAX_ARTIFICIAL_LABEL_BYTES];
31976       /* Output the location lists info.  */
31977       switch_to_section (debug_loc_section);
31978       if (dwarf_version >= 5)
31979 	{
31980 	  ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_LOC_SECTION_LABEL, 2);
31981 	  ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_LOC_SECTION_LABEL, 3);
31982 	  if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
31983 	    dw2_asm_output_data (4, 0xffffffff,
31984 				 "Initial length escape value indicating "
31985 				 "64-bit DWARF extension");
31986 	  dw2_asm_output_delta (dwarf_offset_size, l2, l1,
31987 			    "Length of Location Lists");
31988 	  ASM_OUTPUT_LABEL (asm_out_file, l1);
31989 	  output_dwarf_version ();
31990 	  dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
31991 	  dw2_asm_output_data (1, 0, "Segment Size");
31992 	  dw2_asm_output_data (4, dwarf_split_debug_info ? loc_list_idx : 0,
31993 			       "Offset Entry Count");
31994 	}
31995       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
31996       if (dwarf_version >= 5 && dwarf_split_debug_info)
31997 	{
31998 	  unsigned int save_loc_list_idx = loc_list_idx;
31999 	  loc_list_idx = 0;
32000 	  output_loclists_offsets (comp_unit_die ());
32001 	  gcc_assert (save_loc_list_idx == loc_list_idx);
32002 	}
32003       output_location_lists (comp_unit_die ());
32004       if (dwarf_version >= 5)
32005 	ASM_OUTPUT_LABEL (asm_out_file, l2);
32006     }
32007 
32008   output_pubtables ();
32009 
32010   /* Output the address range information if a CU (.debug_info section)
32011      was emitted.  We output an empty table even if we had no functions
32012      to put in it.  This because the consumer has no way to tell the
32013      difference between an empty table that we omitted and failure to
32014      generate a table that would have contained data.  */
32015   if (info_section_emitted)
32016     {
32017       switch_to_section (debug_aranges_section);
32018       output_aranges ();
32019     }
32020 
32021   /* Output ranges section if necessary.  */
32022   if (!vec_safe_is_empty (ranges_table))
32023     {
32024       if (dwarf_version >= 5)
32025 	{
32026 	  if (dwarf_split_debug_info)
32027 	    {
32028 	      /* We don't know right now whether there are any
32029 		 ranges for .debug_rnglists and any for .debug_rnglists.dwo.
32030 		 Depending on into which of those two belongs the first
32031 		 ranges_table entry, emit that section first and that
32032 		 output_rnglists call will return true if the other kind of
32033 		 ranges needs to be emitted as well.  */
32034 	      bool dwo = (*ranges_table)[0].idx != DW_RANGES_IDX_SKELETON;
32035 	      if (output_rnglists (generation, dwo))
32036 		output_rnglists (generation, !dwo);
32037 	    }
32038 	  else
32039 	    output_rnglists (generation, false);
32040 	}
32041       else
32042 	output_ranges ();
32043     }
32044 
32045   /* Have to end the macro section.  */
32046   if (have_macinfo)
32047     {
32048       switch_to_section (debug_macinfo_section);
32049       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
32050       output_macinfo (!dwarf_split_debug_info ? debug_line_section_label
32051 		      : debug_skeleton_line_section_label, false);
32052       dw2_asm_output_data (1, 0, "End compilation unit");
32053     }
32054 
32055   /* Output the source line correspondence table.  We must do this
32056      even if there is no line information.  Otherwise, on an empty
32057      translation unit, we will generate a present, but empty,
32058      .debug_info section.  IRIX 6.5 `nm' will then complain when
32059      examining the file.  This is done late so that any filenames
32060      used by the debug_info section are marked as 'used'.  */
32061   switch_to_section (debug_line_section);
32062   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
32063   if (! output_asm_line_debug_info ())
32064     output_line_info (false);
32065 
32066   if (dwarf_split_debug_info && info_section_emitted)
32067     {
32068       switch_to_section (debug_skeleton_line_section);
32069       ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
32070       output_line_info (true);
32071     }
32072 
32073   /* If we emitted any indirect strings, output the string table too.  */
32074   if (debug_str_hash || skeleton_debug_str_hash)
32075     output_indirect_strings ();
32076   if (debug_line_str_hash)
32077     {
32078       switch_to_section (debug_line_str_section);
32079       const enum dwarf_form form = DW_FORM_line_strp;
32080       debug_line_str_hash->traverse<enum dwarf_form,
32081 				    output_indirect_string> (form);
32082     }
32083 
32084   /* ??? Move lvugid out of dwarf2out_source_line and reset it too?  */
32085   symview_upper_bound = 0;
32086   if (zero_view_p)
32087     bitmap_clear (zero_view_p);
32088 }
32089 
32090 /* Returns a hash value for X (which really is a variable_value_struct).  */
32091 
32092 inline hashval_t
hash(variable_value_struct * x)32093 variable_value_hasher::hash (variable_value_struct *x)
32094 {
32095   return (hashval_t) x->decl_id;
32096 }
32097 
32098 /* Return nonzero if decl_id of variable_value_struct X is the same as
32099    UID of decl Y.  */
32100 
32101 inline bool
equal(variable_value_struct * x,tree y)32102 variable_value_hasher::equal (variable_value_struct *x, tree y)
32103 {
32104   return x->decl_id == DECL_UID (y);
32105 }
32106 
32107 /* Helper function for resolve_variable_value, handle
32108    DW_OP_GNU_variable_value in one location expression.
32109    Return true if exprloc has been changed into loclist.  */
32110 
32111 static bool
resolve_variable_value_in_expr(dw_attr_node * a,dw_loc_descr_ref loc)32112 resolve_variable_value_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
32113 {
32114   dw_loc_descr_ref next;
32115   for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = next)
32116     {
32117       next = loc->dw_loc_next;
32118       if (loc->dw_loc_opc != DW_OP_GNU_variable_value
32119 	  || loc->dw_loc_oprnd1.val_class != dw_val_class_decl_ref)
32120 	continue;
32121 
32122       tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
32123       if (DECL_CONTEXT (decl) != current_function_decl)
32124 	continue;
32125 
32126       dw_die_ref ref = lookup_decl_die (decl);
32127       if (ref)
32128 	{
32129 	  loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
32130 	  loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
32131 	  loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
32132 	  continue;
32133 	}
32134       dw_loc_list_ref l = loc_list_from_tree (decl, 0, NULL);
32135       if (l == NULL)
32136 	continue;
32137       if (l->dw_loc_next)
32138 	{
32139 	  if (AT_class (a) != dw_val_class_loc)
32140 	    continue;
32141 	  switch (a->dw_attr)
32142 	    {
32143 	    /* Following attributes allow both exprloc and loclist
32144 	       classes, so we can change them into a loclist.  */
32145 	    case DW_AT_location:
32146 	    case DW_AT_string_length:
32147 	    case DW_AT_return_addr:
32148 	    case DW_AT_data_member_location:
32149 	    case DW_AT_frame_base:
32150 	    case DW_AT_segment:
32151 	    case DW_AT_static_link:
32152 	    case DW_AT_use_location:
32153 	    case DW_AT_vtable_elem_location:
32154 	      if (prev)
32155 		{
32156 		  prev->dw_loc_next = NULL;
32157 		  prepend_loc_descr_to_each (l, AT_loc (a));
32158 		}
32159 	      if (next)
32160 		add_loc_descr_to_each (l, next);
32161 	      a->dw_attr_val.val_class = dw_val_class_loc_list;
32162 	      a->dw_attr_val.val_entry = NULL;
32163 	      a->dw_attr_val.v.val_loc_list = l;
32164 	      have_location_lists = true;
32165 	      return true;
32166 	    /* Following attributes allow both exprloc and reference,
32167 	       so if the whole expression is DW_OP_GNU_variable_value alone
32168 	       we could transform it into reference.  */
32169 	    case DW_AT_byte_size:
32170 	    case DW_AT_bit_size:
32171 	    case DW_AT_lower_bound:
32172 	    case DW_AT_upper_bound:
32173 	    case DW_AT_bit_stride:
32174 	    case DW_AT_count:
32175 	    case DW_AT_allocated:
32176 	    case DW_AT_associated:
32177 	    case DW_AT_byte_stride:
32178 	      if (prev == NULL && next == NULL)
32179 		break;
32180 	      /* FALLTHRU */
32181 	    default:
32182 	      if (dwarf_strict)
32183 		continue;
32184 	      break;
32185 	    }
32186 	  /* Create DW_TAG_variable that we can refer to.  */
32187 	  gen_decl_die (decl, NULL_TREE, NULL,
32188 			lookup_decl_die (current_function_decl));
32189 	  ref = lookup_decl_die (decl);
32190 	  if (ref)
32191 	    {
32192 	      loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
32193 	      loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
32194 	      loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
32195 	    }
32196 	  continue;
32197 	}
32198       if (prev)
32199 	{
32200 	  prev->dw_loc_next = l->expr;
32201 	  add_loc_descr (&prev->dw_loc_next, next);
32202 	  free_loc_descr (loc, NULL);
32203 	  next = prev->dw_loc_next;
32204 	}
32205       else
32206 	{
32207 	  memcpy (loc, l->expr, sizeof (dw_loc_descr_node));
32208 	  add_loc_descr (&loc, next);
32209 	  next = loc;
32210 	}
32211       loc = prev;
32212     }
32213   return false;
32214 }
32215 
32216 /* Attempt to resolve DW_OP_GNU_variable_value using loc_list_from_tree.  */
32217 
32218 static void
resolve_variable_value(dw_die_ref die)32219 resolve_variable_value (dw_die_ref die)
32220 {
32221   dw_attr_node *a;
32222   dw_loc_list_ref loc;
32223   unsigned ix;
32224 
32225   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
32226     switch (AT_class (a))
32227       {
32228       case dw_val_class_loc:
32229 	if (!resolve_variable_value_in_expr (a, AT_loc (a)))
32230 	  break;
32231 	/* FALLTHRU */
32232       case dw_val_class_loc_list:
32233 	loc = AT_loc_list (a);
32234 	gcc_assert (loc);
32235 	for (; loc; loc = loc->dw_loc_next)
32236 	  resolve_variable_value_in_expr (a, loc->expr);
32237 	break;
32238       default:
32239 	break;
32240       }
32241 }
32242 
32243 /* Attempt to optimize DW_OP_GNU_variable_value refering to
32244    temporaries in the current function.  */
32245 
32246 static void
resolve_variable_values(void)32247 resolve_variable_values (void)
32248 {
32249   if (!variable_value_hash || !current_function_decl)
32250     return;
32251 
32252   struct variable_value_struct *node
32253     = variable_value_hash->find_with_hash (current_function_decl,
32254 					   DECL_UID (current_function_decl));
32255 
32256   if (node == NULL)
32257     return;
32258 
32259   unsigned int i;
32260   dw_die_ref die;
32261   FOR_EACH_VEC_SAFE_ELT (node->dies, i, die)
32262     resolve_variable_value (die);
32263 }
32264 
32265 /* Helper function for note_variable_value, handle one location
32266    expression.  */
32267 
32268 static void
note_variable_value_in_expr(dw_die_ref die,dw_loc_descr_ref loc)32269 note_variable_value_in_expr (dw_die_ref die, dw_loc_descr_ref loc)
32270 {
32271   for (; loc; loc = loc->dw_loc_next)
32272     if (loc->dw_loc_opc == DW_OP_GNU_variable_value
32273 	&& loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
32274       {
32275 	tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
32276 	dw_die_ref ref = lookup_decl_die (decl);
32277 	if (! ref && (flag_generate_lto || flag_generate_offload))
32278 	  {
32279 	    /* ???  This is somewhat a hack because we do not create DIEs
32280 	       for variables not in BLOCK trees early but when generating
32281 	       early LTO output we need the dw_val_class_decl_ref to be
32282 	       fully resolved.  For fat LTO objects we'd also like to
32283 	       undo this after LTO dwarf output.  */
32284 	    gcc_assert (DECL_CONTEXT (decl));
32285 	    dw_die_ref ctx = lookup_decl_die (DECL_CONTEXT (decl));
32286 	    gcc_assert (ctx != NULL);
32287 	    gen_decl_die (decl, NULL_TREE, NULL, ctx);
32288 	    ref = lookup_decl_die (decl);
32289 	    gcc_assert (ref != NULL);
32290 	  }
32291 	if (ref)
32292 	  {
32293 	    loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
32294 	    loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
32295 	    loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
32296 	    continue;
32297 	  }
32298 	if (VAR_P (decl)
32299 	    && DECL_CONTEXT (decl)
32300 	    && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
32301 	    && lookup_decl_die (DECL_CONTEXT (decl)))
32302 	  {
32303 	    if (!variable_value_hash)
32304 	      variable_value_hash
32305 		= hash_table<variable_value_hasher>::create_ggc (10);
32306 
32307 	    tree fndecl = DECL_CONTEXT (decl);
32308 	    struct variable_value_struct *node;
32309 	    struct variable_value_struct **slot
32310 	      = variable_value_hash->find_slot_with_hash (fndecl,
32311 							  DECL_UID (fndecl),
32312 							  INSERT);
32313 	    if (*slot == NULL)
32314 	      {
32315 		node = ggc_cleared_alloc<variable_value_struct> ();
32316 		node->decl_id = DECL_UID (fndecl);
32317 		*slot = node;
32318 	      }
32319 	    else
32320 	      node = *slot;
32321 
32322 	    vec_safe_push (node->dies, die);
32323 	  }
32324       }
32325 }
32326 
32327 /* Walk the tree DIE and note DIEs with DW_OP_GNU_variable_value still
32328    with dw_val_class_decl_ref operand.  */
32329 
32330 static void
note_variable_value(dw_die_ref die)32331 note_variable_value (dw_die_ref die)
32332 {
32333   dw_die_ref c;
32334   dw_attr_node *a;
32335   dw_loc_list_ref loc;
32336   unsigned ix;
32337 
32338   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
32339     switch (AT_class (a))
32340       {
32341       case dw_val_class_loc_list:
32342 	loc = AT_loc_list (a);
32343 	gcc_assert (loc);
32344 	if (!loc->noted_variable_value)
32345 	  {
32346 	    loc->noted_variable_value = 1;
32347 	    for (; loc; loc = loc->dw_loc_next)
32348 	      note_variable_value_in_expr (die, loc->expr);
32349 	  }
32350 	break;
32351       case dw_val_class_loc:
32352 	note_variable_value_in_expr (die, AT_loc (a));
32353 	break;
32354       default:
32355 	break;
32356       }
32357 
32358   /* Mark children.  */
32359   FOR_EACH_CHILD (die, c, note_variable_value (c));
32360 }
32361 
32362 /* Perform any cleanups needed after the early debug generation pass
32363    has run.  */
32364 
32365 static void
dwarf2out_early_finish(const char * filename)32366 dwarf2out_early_finish (const char *filename)
32367 {
32368   set_early_dwarf s;
32369   char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
32370 
32371   /* PCH might result in DW_AT_producer string being restored from the
32372      header compilation, so always fill it with empty string initially
32373      and overwrite only here.  */
32374   dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
32375 
32376   if (dwarf_record_gcc_switches)
32377     producer_string = gen_producer_string (lang_hooks.name,
32378 					   save_decoded_options,
32379 					   save_decoded_options_count);
32380   else
32381     producer_string = concat (lang_hooks.name, " ", version_string, NULL);
32382 
32383   producer->dw_attr_val.v.val_str->refcount--;
32384   producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
32385 
32386   /* Add the name for the main input file now.  We delayed this from
32387      dwarf2out_init to avoid complications with PCH.  */
32388   add_filename_attribute (comp_unit_die (), remap_debug_filename (filename));
32389   add_comp_dir_attribute (comp_unit_die ());
32390 
32391   /* With LTO early dwarf was really finished at compile-time, so make
32392      sure to adjust the phase after annotating the LTRANS CU DIE.  */
32393   if (in_lto_p)
32394     {
32395       early_dwarf_finished = true;
32396       if (dump_file)
32397 	{
32398 	  fprintf (dump_file, "LTO EARLY DWARF for %s\n", filename);
32399 	  print_die (comp_unit_die (), dump_file);
32400 	}
32401       return;
32402     }
32403 
32404   /* Walk through the list of incomplete types again, trying once more to
32405      emit full debugging info for them.  */
32406   retry_incomplete_types ();
32407 
32408   gen_scheduled_generic_parms_dies ();
32409   gen_remaining_tmpl_value_param_die_attribute ();
32410 
32411   /* The point here is to flush out the limbo list so that it is empty
32412      and we don't need to stream it for LTO.  */
32413   flush_limbo_die_list ();
32414 
32415   /* Add DW_AT_linkage_name for all deferred DIEs.  */
32416   for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
32417     {
32418       tree decl = node->created_for;
32419       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
32420 	  /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
32421 	     ended up in deferred_asm_name before we knew it was
32422 	     constant and never written to disk.  */
32423 	  && DECL_ASSEMBLER_NAME (decl))
32424 	{
32425 	  add_linkage_attr (node->die, decl);
32426 	  move_linkage_attr (node->die);
32427 	}
32428     }
32429   deferred_asm_name = NULL;
32430 
32431   if (flag_eliminate_unused_debug_types)
32432     prune_unused_types ();
32433 
32434   /* Generate separate COMDAT sections for type DIEs. */
32435   if (use_debug_types)
32436     {
32437       break_out_comdat_types (comp_unit_die ());
32438 
32439       /* Each new type_unit DIE was added to the limbo die list when created.
32440          Since these have all been added to comdat_type_list, clear the
32441          limbo die list.  */
32442       limbo_die_list = NULL;
32443 
32444       /* For each new comdat type unit, copy declarations for incomplete
32445          types to make the new unit self-contained (i.e., no direct
32446          references to the main compile unit).  */
32447       for (comdat_type_node *ctnode = comdat_type_list;
32448 	   ctnode != NULL; ctnode = ctnode->next)
32449         copy_decls_for_unworthy_types (ctnode->root_die);
32450       copy_decls_for_unworthy_types (comp_unit_die ());
32451 
32452       /* In the process of copying declarations from one unit to another,
32453          we may have left some declarations behind that are no longer
32454          referenced.  Prune them.  */
32455       prune_unused_types ();
32456     }
32457 
32458   /* Traverse the DIE's and note DIEs with DW_OP_GNU_variable_value still
32459      with dw_val_class_decl_ref operand.  */
32460   note_variable_value (comp_unit_die ());
32461   for (limbo_die_node *node = cu_die_list; node; node = node->next)
32462     note_variable_value (node->die);
32463   for (comdat_type_node *ctnode = comdat_type_list; ctnode != NULL;
32464        ctnode = ctnode->next)
32465     note_variable_value (ctnode->root_die);
32466   for (limbo_die_node *node = limbo_die_list; node; node = node->next)
32467     note_variable_value (node->die);
32468 
32469   /* The AT_pubnames attribute needs to go in all skeleton dies, including
32470      both the main_cu and all skeleton TUs.  Making this call unconditional
32471      would end up either adding a second copy of the AT_pubnames attribute, or
32472      requiring a special case in add_top_level_skeleton_die_attrs.  */
32473   if (!dwarf_split_debug_info)
32474     add_AT_pubnames (comp_unit_die ());
32475 
32476   /* The early debug phase is now finished.  */
32477   early_dwarf_finished = true;
32478   if (dump_file)
32479     {
32480       fprintf (dump_file, "EARLY DWARF for %s\n", filename);
32481       print_die (comp_unit_die (), dump_file);
32482     }
32483 
32484   /* Do not generate DWARF assembler now when not producing LTO bytecode.  */
32485   if ((!flag_generate_lto && !flag_generate_offload)
32486       /* FIXME: Disable debug info generation for (PE-)COFF targets since the
32487 	 copy_lto_debug_sections operation of the simple object support in
32488 	 libiberty is not implemented for them yet.  */
32489       || TARGET_PECOFF || TARGET_COFF)
32490     return;
32491 
32492   /* Now as we are going to output for LTO initialize sections and labels
32493      to the LTO variants.  We don't need a random-seed postfix as other
32494      LTO sections as linking the LTO debug sections into one in a partial
32495      link is fine.  */
32496   init_sections_and_labels (true);
32497 
32498   /* The output below is modeled after dwarf2out_finish with all
32499      location related output removed and some LTO specific changes.
32500      Some refactoring might make both smaller and easier to match up.  */
32501 
32502   /* Traverse the DIE's and add sibling attributes to those DIE's
32503      that have children.  */
32504   add_sibling_attributes (comp_unit_die ());
32505   for (limbo_die_node *node = limbo_die_list; node; node = node->next)
32506     add_sibling_attributes (node->die);
32507   for (comdat_type_node *ctnode = comdat_type_list;
32508        ctnode != NULL; ctnode = ctnode->next)
32509     add_sibling_attributes (ctnode->root_die);
32510 
32511   /* AIX Assembler inserts the length, so adjust the reference to match the
32512      offset expected by debuggers.  */
32513   strcpy (dl_section_ref, debug_line_section_label);
32514   if (XCOFF_DEBUGGING_INFO)
32515     strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
32516 
32517   if (debug_info_level >= DINFO_LEVEL_TERSE)
32518     add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list, dl_section_ref);
32519 
32520   if (have_macinfo)
32521     add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
32522 		   macinfo_section_label);
32523 
32524   save_macinfo_strings ();
32525 
32526   if (dwarf_split_debug_info)
32527     {
32528       unsigned int index = 0;
32529       debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
32530     }
32531 
32532   /* Output all of the compilation units.  We put the main one last so that
32533      the offsets are available to output_pubnames.  */
32534   for (limbo_die_node *node = limbo_die_list; node; node = node->next)
32535     output_comp_unit (node->die, 0, NULL);
32536 
32537   hash_table<comdat_type_hasher> comdat_type_table (100);
32538   for (comdat_type_node *ctnode = comdat_type_list;
32539        ctnode != NULL; ctnode = ctnode->next)
32540     {
32541       comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
32542 
32543       /* Don't output duplicate types.  */
32544       if (*slot != HTAB_EMPTY_ENTRY)
32545         continue;
32546 
32547       /* Add a pointer to the line table for the main compilation unit
32548          so that the debugger can make sense of DW_AT_decl_file
32549          attributes.  */
32550       if (debug_info_level >= DINFO_LEVEL_TERSE)
32551         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
32552                         (!dwarf_split_debug_info
32553                          ? debug_line_section_label
32554                          : debug_skeleton_line_section_label));
32555 
32556       output_comdat_type_unit (ctnode, true);
32557       *slot = ctnode;
32558     }
32559 
32560   /* Stick a unique symbol to the main debuginfo section.  */
32561   compute_comp_unit_symbol (comp_unit_die ());
32562 
32563   /* Output the main compilation unit.  We always need it if only for
32564      the CU symbol.  */
32565   output_comp_unit (comp_unit_die (), true, NULL);
32566 
32567   /* Output the abbreviation table.  */
32568   if (vec_safe_length (abbrev_die_table) != 1)
32569     {
32570       switch_to_section (debug_abbrev_section);
32571       ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
32572       output_abbrev_section ();
32573     }
32574 
32575   /* Have to end the macro section.  */
32576   if (have_macinfo)
32577     {
32578       /* We have to save macinfo state if we need to output it again
32579 	 for the FAT part of the object.  */
32580       vec<macinfo_entry, va_gc> *saved_macinfo_table = macinfo_table;
32581       if (flag_fat_lto_objects)
32582 	macinfo_table = macinfo_table->copy ();
32583 
32584       switch_to_section (debug_macinfo_section);
32585       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
32586       output_macinfo (debug_line_section_label, true);
32587       dw2_asm_output_data (1, 0, "End compilation unit");
32588 
32589       if (flag_fat_lto_objects)
32590 	{
32591 	  vec_free (macinfo_table);
32592 	  macinfo_table = saved_macinfo_table;
32593 	}
32594     }
32595 
32596   /* Emit a skeleton debug_line section.  */
32597   switch_to_section (debug_line_section);
32598   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
32599   output_line_info (true);
32600 
32601   /* If we emitted any indirect strings, output the string table too.  */
32602   if (debug_str_hash || skeleton_debug_str_hash)
32603     output_indirect_strings ();
32604   if (debug_line_str_hash)
32605     {
32606       switch_to_section (debug_line_str_section);
32607       const enum dwarf_form form = DW_FORM_line_strp;
32608       debug_line_str_hash->traverse<enum dwarf_form,
32609 				    output_indirect_string> (form);
32610     }
32611 
32612   /* Switch back to the text section.  */
32613   switch_to_section (text_section);
32614 }
32615 
32616 /* Reset all state within dwarf2out.c so that we can rerun the compiler
32617    within the same process.  For use by toplev::finalize.  */
32618 
32619 void
dwarf2out_c_finalize(void)32620 dwarf2out_c_finalize (void)
32621 {
32622   last_var_location_insn = NULL;
32623   cached_next_real_insn = NULL;
32624   used_rtx_array = NULL;
32625   incomplete_types = NULL;
32626   debug_info_section = NULL;
32627   debug_skeleton_info_section = NULL;
32628   debug_abbrev_section = NULL;
32629   debug_skeleton_abbrev_section = NULL;
32630   debug_aranges_section = NULL;
32631   debug_addr_section = NULL;
32632   debug_macinfo_section = NULL;
32633   debug_line_section = NULL;
32634   debug_skeleton_line_section = NULL;
32635   debug_loc_section = NULL;
32636   debug_pubnames_section = NULL;
32637   debug_pubtypes_section = NULL;
32638   debug_str_section = NULL;
32639   debug_line_str_section = NULL;
32640   debug_str_dwo_section = NULL;
32641   debug_str_offsets_section = NULL;
32642   debug_ranges_section = NULL;
32643   debug_ranges_dwo_section = NULL;
32644   debug_frame_section = NULL;
32645   fde_vec = NULL;
32646   debug_str_hash = NULL;
32647   debug_line_str_hash = NULL;
32648   skeleton_debug_str_hash = NULL;
32649   dw2_string_counter = 0;
32650   have_multiple_function_sections = false;
32651   text_section_used = false;
32652   cold_text_section_used = false;
32653   cold_text_section = NULL;
32654   current_unit_personality = NULL;
32655 
32656   early_dwarf = false;
32657   early_dwarf_finished = false;
32658 
32659   next_die_offset = 0;
32660   single_comp_unit_die = NULL;
32661   comdat_type_list = NULL;
32662   limbo_die_list = NULL;
32663   file_table = NULL;
32664   decl_die_table = NULL;
32665   common_block_die_table = NULL;
32666   decl_loc_table = NULL;
32667   call_arg_locations = NULL;
32668   call_arg_loc_last = NULL;
32669   call_site_count = -1;
32670   tail_call_site_count = -1;
32671   cached_dw_loc_list_table = NULL;
32672   abbrev_die_table = NULL;
32673   delete dwarf_proc_stack_usage_map;
32674   dwarf_proc_stack_usage_map = NULL;
32675   line_info_label_num = 0;
32676   cur_line_info_table = NULL;
32677   text_section_line_info = NULL;
32678   cold_text_section_line_info = NULL;
32679   separate_line_info = NULL;
32680   info_section_emitted = false;
32681   pubname_table = NULL;
32682   pubtype_table = NULL;
32683   macinfo_table = NULL;
32684   ranges_table = NULL;
32685   ranges_by_label = NULL;
32686   rnglist_idx = 0;
32687   have_location_lists = false;
32688   loclabel_num = 0;
32689   poc_label_num = 0;
32690   last_emitted_file = NULL;
32691   label_num = 0;
32692   tmpl_value_parm_die_table = NULL;
32693   generic_type_instances = NULL;
32694   frame_pointer_fb_offset = 0;
32695   frame_pointer_fb_offset_valid = false;
32696   base_types.release ();
32697   XDELETEVEC (producer_string);
32698   producer_string = NULL;
32699   output_line_info_generation = 0;
32700   init_sections_and_labels_generation = 0;
32701 }
32702 
32703 #include "gt-dwarf2out.h"
32704